diff --git a/plan2code-app/ui/app.js b/plan2code-app/ui/app.js index ae952ec..b267c13 100644 --- a/plan2code-app/ui/app.js +++ b/plan2code-app/ui/app.js @@ -3,7 +3,7 @@ // request) with a drag handle. import { mountTerminal } from './terminal.js'; -import { NEW_RELEASE_COMMAND, attentionSignal, barNextSteps, continueKeys, defaultRightFraction, folderName, lastAgentNotice, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopLocks, loopStopLabels, loopWindowTitle } from './meter.js'; +import { NEW_RELEASE_COMMAND, attentionSignal, barNextSteps, continueKeys, defaultRightFraction, folderName, lastAgentNotice, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopLocks, loopStopLabels, loopWindowTitle, helloRetryAction, retryLabel, RETRY_AFTER_SECONDS } from './meter.js'; const { invoke, Channel } = window.__TAURI__.core; @@ -910,8 +910,13 @@ async function onNoHello() { if (frameReady || !consoleKnown || restarting) return; // 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. - if (loopRunning && loopStep === 'Starting') return; + // The next phase's console is on its way; the poll says so instead. Look + // again later all the same: dropped for good, a page that never said hello + // left its message on screen with no way out once the step moved on. + if (helloRetryAction({ loopRunning, loopStep }) === 'rearm') { + helloTimer = setTimeout(onNoHello, HELLO_WAIT_MS); + return; + } let status = null; try { status = await invoke('console_status'); @@ -928,7 +933,21 @@ async function onNoHello() { } } -function showState(text, buttonLabel, onButton, onCancel, { tired = false } = {}) { +// A waiting message that offers a way out after a countdown: the button shows +// disabled, counting down, then turns into `buttonLabel`'s plain retry. +let retryTimer = null; + +function stopRetryCountdown() { + clearInterval(retryTimer); + retryTimer = null; + stateButton.disabled = false; +} + +function showState(text, buttonLabel, onButton, onCancel, { tired = false, retryAfter = 0 } = {}) { + // The poll repeats the same waiting message every tick: that must not + // restart a countdown that is already running (or its button). + if (retryAfter > 0 && !stateBox.hidden && stateText.textContent === text && stateButton.textContent.startsWith('Try reconnecting')) return; + stopRetryCountdown(); hideLaunchCard(); hideFirstRun(); // statePlannyEl is an : unlike HTMLElement, it does not reflect the @@ -942,9 +961,20 @@ function showState(text, buttonLabel, onButton, onCancel, { tired = false } = {} stateCancelEl.hidden = !onCancel; stateCancelEl.onclick = onCancel || null; stateBox.hidden = false; + if (retryAfter > 0 && buttonLabel) { + let left = retryAfter; + stateButton.disabled = true; + stateButton.textContent = retryLabel(left); + retryTimer = setInterval(() => { + left -= 1; + stateButton.textContent = retryLabel(left); + if (left <= 0) stopRetryCountdown(); + }, 1000); + } } function hideState() { + stopRetryCountdown(); stateBox.hidden = true; } @@ -983,7 +1013,7 @@ async function pollConsole() { if (action === 'wait-for-phase') { // Covers the old phase's page and its "Lost contact" until the new one // says hello. Never over a question the box is already asking. - if (stateCancelEl.hidden) showState(loopDetailEl.textContent); + if (stateCancelEl.hidden) showState(loopDetailEl.textContent, 'Try reconnecting', reconnectConsole, undefined, { retryAfter: RETRY_AFTER_SECONDS }); return; } markConsoleDead(); @@ -1167,6 +1197,7 @@ function applyLoopState(state) { } if (!loopRunning) loopTypingNoted = false; loopRunning = true; + const stepBefore = loopStep; loopStep = state.step || ''; restartBtn.hidden = true; applyLoopLocks(state); @@ -1176,6 +1207,9 @@ function applyLoopState(state) { // Only a change of words: this is a live region, and screen readers read it out. if (loopTitleEl.textContent !== banner.title) loopTitleEl.textContent = banner.title; if (loopDetailEl.textContent !== banner.detail) loopDetailEl.textContent = banner.detail; + // A phase starting: look at the console now, so its waiting message and + // countdown begin at once, not at the next status poll. + if (loopStep === 'Starting' && stepBefore !== 'Starting' && consoleKnown) void pollConsole(); loopHintEl.textContent = banner.hint; loopSpecEl.textContent = state.specName || ''; loopPipsEl.replaceChildren( diff --git a/plan2code-app/ui/meter.js b/plan2code-app/ui/meter.js index 1a48a69..8244a4c 100644 --- a/plan2code-app/ui/meter.js +++ b/plan2code-app/ui/meter.js @@ -253,6 +253,24 @@ export function deadConsoleAction({ loopRunning, loopStep, frameReady, reconnect return reconnects < limit ? "reconnect" : "offer-button"; } +/** + * What the window does when a page's hello did not come in time. While a loop + * starts a phase the poll words the wait, so the check is not run now, but it + * must be armed again: dropped for good, a page that never says hello left the + * center message on screen with no way out once the step moved on. + */ +export function helloRetryAction({ loopRunning, loopStep }) { + return loopRunning && loopStep === "Starting" ? "rearm" : "check"; +} + +/** Seconds a waiting message stays before it offers Try reconnecting. */ +export const RETRY_AFTER_SECONDS = 10; + +/** The retry button's label: counting down, then plain. */ +export function retryLabel(secondsLeft) { + return secondsLeft > 0 ? `Try reconnecting in ${secondsLeft}s` : "Try reconnecting"; +} + /** * A project folder's own name, for the bar's project menu: the same last * segment the window title uses, or the whole path when there is none (a diff --git a/scripts/test-app-meter.mjs b/scripts/test-app-meter.mjs index 30ad978..4d0b09f 100644 --- a/scripts/test-app-meter.mjs +++ b/scripts/test-app-meter.mjs @@ -11,7 +11,7 @@ import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; -import { attentionSignal, barNextSteps, continueKeys, defaultRightFraction, folderName, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopRunText, loopLocks, loopStopLabels, loopWindowTitle, CONSOLE_ASIDE_BREAKPOINT_PX, NEXT_STEP_LABELS, NEW_RELEASE_COMMAND } from "../plan2code-app/ui/meter.js"; +import { helloRetryAction, retryLabel, RETRY_AFTER_SECONDS, attentionSignal, barNextSteps, continueKeys, defaultRightFraction, folderName, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopRunText, loopLocks, loopStopLabels, loopWindowTitle, CONSOLE_ASIDE_BREAKPOINT_PX, NEXT_STEP_LABELS, NEW_RELEASE_COMMAND } from "../plan2code-app/ui/meter.js"; import { findBrowser, launchBrowser } from "./browser.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); @@ -423,6 +423,27 @@ test("a console dead between a loop's phases waits for the next phase; any other assert.equal(deadConsoleAction({ ...base, frameReady: false }), "no-hello"); }); +test("a page that said no hello is looked at again while a loop starts a phase, and the retry button counts down", () => { + assert.equal(helloRetryAction({ loopRunning: true, loopStep: "Starting" }), "rearm", "the poll covers Starting, but nothing may drop the check for good"); + assert.equal(helloRetryAction({ loopRunning: true, loopStep: "Build" }), "check"); + assert.equal(helloRetryAction({ loopRunning: false, loopStep: "Starting" }), "check"); + assert.equal(retryLabel(10), "Try reconnecting in 10s"); + assert.equal(retryLabel(1), "Try reconnecting in 1s"); + assert.equal(retryLabel(0), "Try reconnecting"); + assert.equal(retryLabel(-3), "Try reconnecting"); + assert.equal(RETRY_AFTER_SECONDS, 10); +}); + +test("app.js re-arms the hello check and offers Try reconnecting on the loop's waiting message", () => { + const app = fs.readFileSync(path.join(HERE, "..", "plan2code-app", "ui", "app.js"), "utf8"); + const noHello = app.slice(app.indexOf("async function onNoHello"), app.indexOf("function showState")); + assert.match(noHello, /helloRetryAction\(/); + assert.match(noHello, /setTimeout\(onNoHello, HELLO_WAIT_MS\)/, "a swallowed hello check is armed again"); + assert.match(app, /showState\(loopDetailEl\.textContent[^)]*retryAfter/s, "the loop's wait for a phase's page offers the button"); + assert.doesNotMatch(app, /showState\('Opening the page'[^)]*retryAfter/s, "onNoHello swaps that message at 8s, so a 10s countdown there never finishes"); + assert.match(app, /loopStep === 'Starting' && stepBefore !== 'Starting' && consoleKnown\) void pollConsole\(\)/, "a phase starting looks at the console at once, so the countdown does not wait for the next poll"); +}); + test("app.js refills the reconnect budget on a new page and brings the page back when a loop ends", () => { const app = fs.readFileSync(path.join(HERE, "..", "plan2code-app", "ui", "app.js"), "utf8"); const announced = app.slice(app.indexOf("function showAnnounced"), app.indexOf("window.__TAURI__.event.listen('console-page'"));