diff --git a/plan2code-app/ui/app.js b/plan2code-app/ui/app.js index f7f606c..ec3f2ed 100644 --- a/plan2code-app/ui/app.js +++ b/plan2code-app/ui/app.js @@ -972,6 +972,29 @@ let helloTimer = null; // Nothing has shown here yet: no agent has opened the dashboard. No polling, // no "reconnect" button makes sense until that first page lands. let consoleKnown = false; +// Which console the checks below ask about. A project switch moves it on, so +// an answer that comes back afterward is about a console that is gone. +let consoleEpoch = 0; + +// The project moved on, and its console was stopped on purpose: nothing is +// known here again until the new agent opens a page. Left known, the next +// poll found no console and put "The console stopped" over the new agent's +// start, or the launch card; left in the frame, the old page showed its "Lost +// contact" around it. +function forgetConsole() { + consoleEpoch += 1; + consoleKnown = false; + clearInterval(pollTimer); + pollTimer = null; + clearTimeout(helloTimer); + currentUrl = null; + frameReady = false; + // As for any page going away (loadFrame): its taskbar dot and its game go too. + clearAttention(); + setGameFull(false); + frame.src = 'about:blank'; + markConsoleAlive(); +} function loadFrame(url) { currentUrl = url; @@ -1252,6 +1275,7 @@ setWindowTheme(document.documentElement.getAttribute('data-theme') === 'light' ? // No hello in time: ask the console why, and say so with a way forward. async function onNoHello() { if (frameReady || !consoleKnown || restarting) return; + const epoch = consoleEpoch; // A page that does not answer cannot say it is done needing the person. clearAttention(); // The next phase's console is on its way; the poll says so instead. Look @@ -1267,6 +1291,7 @@ async function onNoHello() { } catch { // The status check itself failed; treat it as a console that is not answering. } + if (epoch !== consoleEpoch) return; if (!status || status.state !== 'running') { showState('The console stopped, so the page could not show.', 'Reconnect the console', reconnectConsole); } else { @@ -1434,12 +1459,15 @@ let pollTimer = null; // on its own session. The app never opens a dashboard on its own (the agent // does, by running the skill), so there is nothing to start before that. async function reconnectConsole() { + const epoch = consoleEpoch; markConsoleAlive(); showState('Reconnecting to the console...'); try { const info = await invoke('console_start'); + if (epoch !== consoleEpoch) return; loadFrame(info.url); } catch (error) { + if (epoch !== consoleEpoch) return; showErrorState(error, 'reach the console', 'Retry', reconnectConsole); // An old console cannot be framed: the fix is the skills update, so offer it. if (String(error).includes('too old')) setVersionState({ kind: 'too-old' }); @@ -1453,8 +1481,10 @@ const RECONNECT_LIMIT = 3; let reconnects = 0; async function pollConsole() { + const epoch = consoleEpoch; try { const status = await invoke('console_status'); + if (epoch !== consoleEpoch) return; if (status.state === 'running') { markConsoleAlive(); return; @@ -2115,8 +2145,7 @@ async function doChangeProject(prior = null) { setProjectDir(dir); if (appState) appState.projectDir = dir; restarting = false; - currentUrl = null; - frameReady = false; + forgetConsole(); // An agent was in use: start the same one (agent, model, effort and // permission mode) in the new folder, rather than leaving the launch card // and an empty terminal. Nothing to carry over: the launch card as before. diff --git a/scripts/test-app-meter.mjs b/scripts/test-app-meter.mjs index f9f44fc..94b6f44 100644 --- a/scripts/test-app-meter.mjs +++ b/scripts/test-app-meter.mjs @@ -540,6 +540,31 @@ test("app.js refills the reconnect budget on a new page and brings the page back assert.match(app.slice(app.indexOf("async function showLoopEnd"), app.indexOf("function applyLoopState")), /reconnectConsole\(\)/); }); +test("app.js forgets the old project's console on a project switch, so no check of it covers the new agent's start or the launch card", () => { + const app = fs.readFileSync(path.join(HERE, "..", "plan2code-app", "ui", "app.js"), "utf8"); + const at = app.indexOf("function forgetConsole"); + assert.ok(at > 0, "left known, the old console's poll put \"The console stopped\" over the new agent's start, or the launch card, five seconds after a switch"); + const forget = app.slice(at, app.indexOf("\n}\n", at)); + assert.match(forget, /consoleKnown = false/, "nothing is known again until the new agent opens a page"); + assert.match(forget, /clearInterval\(pollTimer\);\s*pollTimer = null/, "the poll stops, and showAnnounced can start it again"); + assert.match(forget, /clearTimeout\(helloTimer\)/, "a hello still awaited from the old page is not waited for"); + assert.match(forget, /markConsoleAlive\(\)/, "the bar drops a Console stopped it was showing"); + assert.match(forget, /frame\.src = 'about:blank'/, "the stopped console's page leaves the frame, or its Lost contact shows around the launch card"); + assert.match(forget, /clearAttention\(\);\s*setGameFull\(false\)/, "and takes its taskbar dot and its full-screen game with it, as loadFrame does"); + assert.match(forget, /consoleEpoch \+= 1/, "a check already under way can tell the project moved on"); + const change = app.slice(app.indexOf("async function doChangeProject"), app.indexOf("function changeProject")); + assert.match(change, /forgetConsole\(\);[^}]*if \(prior\) await startPriorAgent\(prior\);\s*else afterProjectKnown\(\)/s, "forgotten before the new agent starts or the launch card shows"); + // Each check that waits on the app answers for the console it asked about. + for (const name of ["onNoHello", "reconnectConsole", "pollConsole"]) { + const from = app.indexOf(`async function ${name}`); + const body = app.slice(from, app.indexOf("\n}\n", from)); + assert.match(body, /const epoch = consoleEpoch;/, `${name} notes which console it asks about`); + const awaits = body.split("await invoke(").length - 1; + const guards = body.split("if (epoch !== consoleEpoch) return;").length - 1; + assert.ok(guards >= awaits, `${name} drops an answer that comes back after a switch (${guards} checks for ${awaits} waits)`); + } +}); + test("the window title says a loop is running, and when it needs the person", () => { assert.equal(loopWindowTitle("C:/git/lunch", { phase: 2, phases: 7, step: "Build" }), "Loop: Phase 2 of 7 - lunch"); assert.equal(loopWindowTitle("C:/git/lunch", { phase: 2, phases: 7, step: "Waiting on you" }), "Loop needs you: Phase 2 - lunch");