Skip to content

Commit fbd9624

Browse files
committed
fix: a turn that edits silently still refreshes everything it touched
1 parent 6d84411 commit fbd9624

7 files changed

Lines changed: 58 additions & 21 deletions

File tree

‎backend/src/chat/chat.controller.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { WorkspaceService } from '../project/workspace.service';
1414
import type { ChangedFile } from '../project/workspace';
1515
import { lintArtifact, type LintFinding } from './lint-artifact';
1616
import { busy, queueForProject } from '../project/project-queue';
17+
import { scenarioOfPage } from '../project/scenarios';
1718

1819
/**
1920
* Best-effort label for what a tool call is acting on. Tool arguments arrive as
@@ -114,9 +115,7 @@ export class ChatController {
114115
try {
115116
const workspace = await this.workspaces.for(project.projectPath);
116117
const html = await workspace.readFile('index.html');
117-
return (
118-
html?.match(/name="codefox-scenario"\s+content="([\w-]+)"/)?.[1] ?? null
119-
);
118+
return scenarioOfPage(html);
120119
} catch {
121120
return null;
122121
}

‎backend/src/project/__tests__/scenarios.spec.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { SCENARIOS, scenario, scenarioChoices } from '../scenarios';
1+
import {
2+
SCENARIOS,
3+
scenario,
4+
scenarioChoices,
5+
scenarioMeta,
6+
scenarioOfPage,
7+
} from '../scenarios';
28

39
/**
410
* The scenario is what the user actually answers, and it decides two things
@@ -48,14 +54,18 @@ describe('scenarios', () => {
4854
// the scaffold writes and the pattern the turn reads back have to agree.
4955
// They sit in different files and would drift silently.
5056
it('round-trips through the meta tag the scaffold writes', () => {
51-
const written = (id: string) =>
52-
`<meta name="codefox-scenario" content="${id}" />`;
53-
const read = /name="codefox-scenario"\s+content="([\w-]+)"/;
57+
// Both sides imported, not re-declared: re-typing the tag and the regex
58+
// here would assert this file against itself and pass no matter how far
59+
// scaffold.ts and chat.controller.ts drifted apart.
5460
for (const s of SCENARIOS) {
55-
expect(written(s.id).match(read)?.[1]).toBe(s.id);
61+
expect(scenarioOfPage(scenarioMeta(s.id))).toBe(s.id);
5662
}
5763
// A page scaffolded before scenarios existed reads as nothing at all.
58-
expect('<head><title>x</title></head>'.match(read)?.[1] ?? null).toBeNull();
64+
expect(scenarioOfPage('<head><title>x</title></head>')).toBeNull();
65+
expect(scenarioOfPage(null)).toBeNull();
66+
// And the tag survives being embedded in a real <head>.
67+
const head = `<head><meta charset="utf-8" />${scenarioMeta('deck')}<title>x</title></head>`;
68+
expect(scenarioOfPage(head)).toBe('deck');
5969
});
6070

6171
it("does not tell a deck to use scrollIntoView", () => {

‎backend/src/project/scaffold.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fsExtra from 'fs-extra';
66
import simpleGit from 'simple-git';
77
import { getProjectsDir, getRootDir } from '../common/utils/common-path';
88
import { DesignSystem, designSystem } from './design-systems';
9-
import { Scenario, scenario } from './scenarios';
9+
import { Scenario, scenario, scenarioMeta } from './scenarios';
1010

1111
const { copy, existsSync, readdirSync, remove, symlink } = fsExtra;
1212
const exec = promisify(execFile);
@@ -156,7 +156,7 @@ const htmlStarter = (style: DesignSystem, kind: Scenario): string => `<!doctype
156156
<meta name="viewport" content="width=device-width, initial-scale=1" />
157157
<!-- What this page is. Read back on every turn to remind the agent what
158158
shape it is building; the style lives in :root the same way. -->
159-
<meta name="codefox-scenario" content="${kind.id}" />
159+
${scenarioMeta(kind.id)}
160160
<title>New Project</title>
161161
<script src="https://cdn.tailwindcss.com"></script>
162162
<style>

‎backend/src/project/scenarios.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ current nav item and inline links.`,
156156
* is both the commonest answer and the safest thing to build from a vague
157157
* brief.
158158
*/
159+
/**
160+
* How a project remembers what it is: a meta tag in its own index.html.
161+
* Written by the scaffold, read back on every turn — the two live in
162+
* different modules, so both use these rather than their own copy.
163+
*/
164+
export const scenarioMeta = (id: string): string =>
165+
`<meta name="codefox-scenario" content="${id}" />`;
166+
167+
export const SCENARIO_META_RE = /name="codefox-scenario"\s+content="([\w-]+)"/;
168+
169+
/** The scenario a page declares, or null for one scaffolded before this. */
170+
export function scenarioOfPage(html?: string | null): string | null {
171+
return html?.match(SCENARIO_META_RE)?.[1] ?? null;
172+
}
173+
159174
export function scenario(id?: string | null): Scenario {
160175
return SCENARIOS.find((s) => s.id === id) ?? SCENARIOS[0];
161176
}

‎frontend/src/components/chat/code-engine/web-view.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ function PreviewContent({
221221
// exits or times out, so a returned url already means the port answers —
222222
// and the browser cannot re-check it anyway (cross-origin, the preflight
223223
// is refused). What was here polled a function hardcoded to `true`.
224-
const MAX_ATTEMPTS = 12; // ~1 min at 5s, past a cold `next dev` boot.
224+
// Not ~1 min: each attempt awaits the backend's own 90s waitForPort before
225+
// failing, so 12 attempts is ~19 min of real budget, not 12 × the 5s gap.
226+
// Raising this to "give a slow box more time" would be raising 19 minutes.
227+
const MAX_ATTEMPTS = 12;
225228

226229
useEffect(() => {
227230
const initWebUrl = async () => {

‎frontend/src/hooks/useChatStream.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,21 @@ export const useChatStream = ({
188188

189189
setActivity(null);
190190

191-
// Here rather than beside refreshProjects below: that sits after an
192-
// awaited saveMessage and behind an early `return` for an empty answer,
193-
// so the file panels stayed stale for a whole round trip — or forever,
194-
// on a turn that edited files but said nothing. This also lands in the
191+
// Both refreshes here, before the early `return` for an empty answer
192+
// and before the awaited saveMessage. Either one below that line goes
193+
// stale for a whole round trip — or forever, on a turn that edited
194+
// files but said nothing, which is a real turn with real edits and a
195+
// cover image that no longer matches. turnFinished() also lands in the
195196
// same commit as the lint findings above, so the Changes list and its
196197
// findings repaint together instead of tearing.
197-
if (touchedFiles) turnFinished();
198+
if (touchedFiles) {
199+
turnFinished();
200+
// Not awaited: saving the reply must not queue behind a refetch. The
201+
// backend rescue-saves a turn the client abandons, and widening that
202+
// window to include a network round trip is how a reply gets stored
203+
// twice or not at all.
204+
void refreshProjects();
205+
}
198206

199207
// Only the answer is kept. The working notes exist to be watched while
200208
// the turn runs; storing them would replay "let me check X…" forever on
@@ -219,9 +227,6 @@ export const useChatStream = ({
219227
} as ChatInputType,
220228
},
221229
});
222-
223-
// Only re-read the project when the agent actually wrote something.
224-
if (touchedFiles) await refreshProjects();
225230
} catch (err) {
226231
// An auto-started first turn that died before producing anything is the
227232
// caller's to retry — right after creation the backend may still be

‎scripts/check-changes-refresh.mjs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import assert from 'node:assert/strict';
22
import { readFileSync } from 'node:fs';
33

4-
const src = readFileSync('frontend/src/components/chat/code-engine/tabs/code-tab.tsx', 'utf8');
4+
// Relative to this file, not the cwd — the other check scripts do the same,
5+
// and a cwd-relative path made this one fail when run from frontend/.
6+
const src = readFileSync(
7+
new URL('../frontend/src/components/chat/code-engine/tabs/code-tab.tsx', import.meta.url),
8+
'utf8',
9+
);
510
assert.ok(src.includes('{changesLoading && !changes ? ('), 'spinner no longer gated on !changes — a refetch blanks the list');
611
assert.ok(src.includes('{!changesLoading && lint && lint.length > 0 && ('), 'findings no longer gated on changesLoading — they tear from the file list');
712
assert.ok(src.includes('}, [projectPath, turnsDone]);'), 'changes effect no longer refetches on turn end');

0 commit comments

Comments
 (0)