diff --git a/.agents-docs/AGENTS-architecture.md b/.agents-docs/AGENTS-architecture.md index 043ac29..5bfc047 100644 --- a/.agents-docs/AGENTS-architecture.md +++ b/.agents-docs/AGENTS-architecture.md @@ -461,7 +461,7 @@ Optional local browser UI shipped with **every** skill. Source lives in `src/web | `public/screens/cleanup.js` | User Preferences' Cleanup section, on the System tab | | `public/screens/core.js` | the core every screen sits inside: connection, the render loop, the page chrome and Send; the only file that imports the screens, and only `app.js` imports it | | `public/screens/dashboard.js` | the dashboard: cards, the spec picker, handoff chips, delete, and the first-open wake-up | -| `public/screens/loop-setup.js` | the Implementation Loop's setup screen on the dashboard (app only): the fields, their defaults and read-only facts, the Start checks, and the request to the app. The page locks while `state.loop.driving` is set (`loopDriving()` in `selectors.js`) until the app posts `loopEnd`; Activities, Notebook, Help and Preferences stay usable | +| `public/screens/loop-setup.js` | the Implementation Loop's setup screen on the dashboard (app only): the fields, their defaults and read-only facts, the Start checks, and the request to the app. The page locks while `state.loop.driving` is set (`loopDriving()` in `selectors.js`) until the app posts `loopEnd`; Activities, Notebook, Help and Preferences stay usable. `loopEnd.can` accepts `ask` (`LOOP_CAN` in `patches.mjs`); the app adds it first to every stop that offers `resume`, and the stopped screen's **Ask a question** wakes an ordinary agent on the same session | | `public/screens/docs.js` | documents: tabs, blocks, print and download, and the brief button | | `public/screens/ending.js` | how a session ends: the finished screen, its offers, Stop and the way home | | `public/screens/git-host.js` | hosts the git board: tabs, panels and what each click sends | diff --git a/.readme/web-console.md b/.readme/web-console.md index 8f4e2d7..79a820f 100644 --- a/.readme/web-console.md +++ b/.readme/web-console.md @@ -1583,6 +1583,12 @@ stopped, its actions sit beside a **Back to the dashboard** button that opens a fresh dashboard in the frame, no agent needed, and its status line marks it as your move rather than leaving a stale "starting" line behind. +A stopped loop also offers **Ask a question**, first in the row. The app ended +its agent when the loop stopped, so the Ask tab says no agent is available; +the button wakes an ordinary agent on the same page, without starting the loop +or changing anything, and **Resume the loop** is still there afterwards. **Back +to the dashboard** ends that agent too. + You don't have to finish first. On every skill but the dashboard itself, a small **triangle** pointing left, in your highlight color, sits beside *Stop session*. Press it, confirm, and the agent saves your place exactly as a stop diff --git a/docs/desktop-app.md b/docs/desktop-app.md index f349805..d5acafa 100644 --- a/docs/desktop-app.md +++ b/docs/desktop-app.md @@ -222,7 +222,7 @@ evidence, when: - the Smart pick skipped a Critical finding. A crashed or silent agent gets exactly one retry per phase when the retry tick is on, and none when it is off. -It never retries a real failure. On a stop screen, **Resume the loop** carries on from the stopped +It never retries a real failure. On a stop screen, **Ask a question** (first in the row, offered whenever Resume is) wakes an ordinary agent on the stopped phase on the same page, so the Ask tab answers; it starts no loop and changes nothing, and **Resume the loop** is still there afterwards. **Resume the loop** carries on from the stopped phase and **Fix it by hand** opens an ordinary Implement session on it. **The report.** One report follows the whole build, across stops and Resume. For every phase it says what diff --git a/plan2code-app/src-tauri/src/loop_driver.rs b/plan2code-app/src-tauri/src/loop_driver.rs index b68446c..251e7fd 100644 --- a/plan2code-app/src-tauri/src/loop_driver.rs +++ b/plan2code-app/src-tauri/src/loop_driver.rs @@ -1554,6 +1554,27 @@ pub fn fix_prompt(spec: &str) -> String { format!("/plan2code-3-implement {spec}/overview.md --web") } +/// The prompt behind Ask a question: an ordinary agent on the stopped phase +/// that builds and fixes nothing. It opens the console with `--web`, which +/// joins the session already on screen, and answers through its Ask tab. +pub fn ask_prompt(spec: &str) -> String { + format!( + "The Implementation Loop for {spec} stopped. Change nothing: do not start the loop, \ + do not edit files and do not run git. Read {spec}/overview.md and the report so far, \ + then open the web console with --web (it joins the session already on screen) and \ + answer the person's questions through its Ask tab until they press a button on the \ + stopped screen." + ) +} + +/// Start the Ask agent: `start_with` without the forget step, so the agent's +/// `open` joins the session on screen and the stopped screen stays up. The +/// prompt is transient, as `start_with`'s is. +pub fn start_asking(agent: &AgentManager, launch: &Launch, prompt: String) -> Result<(), String> { + let (kind, model, effort, mode) = launch.clone(); + agent.start_transient(kind, model, effort, mode, prompt) +} + pub fn finalize_prompt(spec: &str) -> String { format!("/plan2code-4-finalize {spec}/overview.md --web") } @@ -2752,6 +2773,19 @@ fn post_end(ctx: &Ctx, rec: &RunningRecord, patch: &Value) { let _ = loop_tools::console_post(&ctx.console_dir, &sid, &plain); } +/// The buttons a stopped screen offers: `can`, with Ask a question first +/// whenever Resume is offered (the person can ask before choosing), and Back +/// to the dashboard last. +fn stop_can(can: &[&str]) -> Vec { + let ask = can.contains(&"resume") && !can.contains(&"ask"); + ask.then_some("ask") + .into_iter() + .chain(can.iter().copied()) + .chain(["dashboard"]) + .map(String::from) + .collect() +} + /// End the loop, the one place every stop goes through: stop the agent, mark /// the phase stopped, write the evidence and the end to the page, clear the /// record and tell the strip. `can` names the buttons the stop screen offers. @@ -2831,7 +2865,7 @@ fn stop_loop(ctx: &Ctx, cause: StopCause, line: String, can: &[&str]) { "evidence": "evidence", "phases": phase_rows(&rec), "uncommitted": uncommitted, - "can": can.iter().copied().chain(["dashboard"]).collect::>(), + "can": stop_can(can), }, }); post_end(ctx, &rec, &patch); @@ -6450,6 +6484,94 @@ second line" ); } + #[test] + fn the_ask_prompt_names_the_spec_and_changes_nothing() { + let prompt = ask_prompt("specs/x"); + assert!(prompt.contains("specs/x"), "{prompt}"); + assert!(prompt.contains("--web"), "{prompt}"); + assert!(!prompt.contains("--loop"), "{prompt}"); + assert!(!prompt.contains("--phase"), "{prompt}"); + assert!(prompt.to_lowercase().contains("change nothing"), "{prompt}"); + assert!(!prompt.contains('\u{2014}'), "{prompt}"); + } + + #[test] + fn every_resumable_stop_offers_ask_first_and_a_finalize_only_stop_does_not() { + assert_eq!( + stop_can(&["resume", "fix"]), + ["ask", "resume", "fix", "dashboard"] + ); + assert_eq!(stop_can(&["resume"]), ["ask", "resume", "dashboard"]); + assert_eq!(stop_can(&["finalize"]), ["finalize", "dashboard"]); + // Never twice when a caller already named it. + assert_eq!(stop_can(&["ask", "resume"]), ["ask", "resume", "dashboard"]); + } + + #[test] + fn asking_leaves_the_session_on_screen_and_fix_it_by_hand_still_forgets_it() { + let (agent, seen) = fake_agent_manager(Path::new(".")); + let forgets = Arc::new(Mutex::new(0)); + let counter = Arc::clone(&forgets); + agent.set_forget_session(Arc::new(move || *lock(&counter) += 1)); + let launch = fake_agent_launch(); + + start_asking(&agent, &launch, ask_prompt("specs/x")).unwrap(); + wait_until("the Ask agent ready", || { + lock(&seen).matches("FAKE-AGENT ready").count() >= 1 + }); + assert_eq!(*lock(&forgets), 0, "Ask must join the session on screen"); + + // The Ask agent is still running when the next button is pressed, as in + // the app: Fix it by hand replaces it and forgets the session. + start_with(&agent, &launch, fix_prompt("specs/x")).unwrap(); + wait_until("the fix agent ready", || { + lock(&seen).matches("FAKE-AGENT ready").count() >= 2 + }); + assert_eq!(*lock(&forgets), 1, "Fix it by hand must forget the session"); + let prompts = launched_prompts(&seen); + assert_eq!( + prompts, + vec![ask_prompt("specs/x"), fix_prompt("specs/x")], + "the fix prompt replaced the Ask agent's" + ); + agent.stop(); + } + + #[test] + fn resuming_the_loop_after_asking_replaces_the_ask_agent_and_forgets_the_session() { + let (agent, seen) = fake_agent_manager(Path::new(".")); + let forgets = Arc::new(Mutex::new(0)); + let counter = Arc::clone(&forgets); + agent.set_forget_session(Arc::new(move || *lock(&counter) += 1)); + let launch = fake_agent_launch(); + + start_asking(&agent, &launch, ask_prompt("specs/x")).unwrap(); + wait_until("the Ask agent ready", || { + lock(&seen).matches("FAKE-AGENT ready").count() >= 1 + }); + assert_eq!(*lock(&forgets), 0, "Ask must join the session on screen"); + + let row = strength_rows()["balanced"].clone(); + start_phase_with( + &agent, + &launch, + "resume prompt".into(), + &row, + Strength::Balanced, + ) + .unwrap(); + wait_until("the resumed agent ready", || { + lock(&seen).matches("FAKE-AGENT ready").count() >= 2 + }); + assert_eq!(*lock(&forgets), 1, "Resume must forget the session"); + assert_eq!( + launched_prompts(&seen), + vec![ask_prompt("specs/x"), "resume prompt".to_string()], + "the resumed phase replaced the Ask agent's" + ); + agent.stop(); + } + /// The `ARGS` lines the fake agent has printed, one per generation, in the /// order they started: the prompts each launch ran with. fn launched_prompts(seen: &Arc>) -> Vec { diff --git a/plan2code-app/src-tauri/src/main.rs b/plan2code-app/src-tauri/src/main.rs index 3826ae3..e3025ec 100644 --- a/plan2code-app/src-tauri/src/main.rs +++ b/plan2code-app/src-tauri/src/main.rs @@ -1229,6 +1229,16 @@ fn loop_fix(state: State<'_, AppState>) -> Result<(), String> { loop_driver::start_with(&agent, &last.launch, loop_driver::fix_prompt(&last.spec)) } +// Ask a question: an ordinary agent on the stopped phase that changes nothing. +// It starts without the forget step, so its `open` joins the stopped screen's +// session and the Ask tab comes online beside the buttons. +#[tauri::command] +fn loop_ask(state: State<'_, AppState>) -> Result<(), String> { + let last = last_loop(&state)?; + let agent = current_agent(&state).ok_or("No agent is available")?; + loop_driver::start_asking(&agent, &last.launch, loop_driver::ask_prompt(&last.spec)) +} + #[tauri::command] fn loop_finalize(state: State<'_, AppState>) -> Result<(), String> { let last = last_loop(&state)?; @@ -1254,6 +1264,11 @@ fn loop_dashboard(state: State<'_, AppState>) -> Result { invoke('loop_facts') .then((facts) => source.postMessage({ plan2code: 'loop-facts', facts }, '*')) .catch(() => {}); - } else if (kind === 'loop-resume' || kind === 'loop-fix' || kind === 'loop-finalize') { + } else if (kind === 'loop-resume' || kind === 'loop-ask' || kind === 'loop-fix' || kind === 'loop-finalize') { // Fixed requests, no free text: the page answers with ok or the error. const source = event.source; - const command = { 'loop-resume': 'loop_resume', 'loop-fix': 'loop_fix', 'loop-finalize': 'loop_finalize' }[kind]; + const command = { 'loop-resume': 'loop_resume', 'loop-ask': 'loop_ask', 'loop-fix': 'loop_fix', 'loop-finalize': 'loop_finalize' }[kind]; invoke(command) .then(() => source.postMessage({ plan2code: `${kind}-result`, ok: true, error: null }, '*')) .catch((error) => source.postMessage({ plan2code: `${kind}-result`, ok: false, error: String(error) }, '*')); diff --git a/scripts/test-web-console-page-loop.mjs b/scripts/test-web-console-page-loop.mjs index c25771d..e7ba782 100644 --- a/scripts/test-web-console-page-loop.mjs +++ b/scripts/test-web-console-page-loop.mjs @@ -294,6 +294,65 @@ async function openLoopPage(t) { }); } +const STOPPED = { + kind: "stopped", + line: "Phase 2 stopped", + phases: [{ n: 2, name: "Docs", commit: null, unfixed: 0, retries: 0 }], + uncommitted: [], + can: ["ask", "resume", "fix", "dashboard"], +}; +const ASK_HINT = "Press Ask a question to wake an agent, then ask in the Ask tab."; +const SETTLED = { id: "q1", kind: "text", title: "What to call it", body: "A short name.", status: "answered", answer: { text: "Flock" } }; + +// The notes panel (#aside) of a stopped loop's screen: the agent is gone, so a +// note box would reach nobody. It says how to ask instead. +async function asideOf(s) { + await s.page.locator("#aside").waitFor(); + return { text: await textOf(s.page.locator("#aside")), boxes: await s.page.locator("#aside textarea, #aside .field").count() }; +} + +test("a stopped loop's notes panel says to press Ask a question, then ask in the Ask tab, and draws no note box", async (t) => { + const s = await openLoopPage(t); + s.post({ items: [SETTLED], loopEnd: STOPPED, agent: { status: "waiting" } }); + await s.page.locator(".loop-end-actions").waitFor(); + const aside = await asideOf(s); + assert.ok(aside.text.includes(ASK_HINT), aside.text); + assert.doesNotMatch(aside.text, /Nothing was said about this one/); + assert.equal(aside.boxes, 0, "no note box on the stopped screen"); +}); + +test("a stopped loop with a question left open still says how to ask and draws no note box", async (t) => { + const s = await openLoopPage(t); + s.post({ items: [{ id: "q2", kind: "text", title: "Which name", body: "A short name." }], loopEnd: STOPPED, agent: { status: "waiting" } }); + await s.page.locator(".loop-end-actions").waitFor(); + const aside = await asideOf(s); + assert.ok(aside.text.includes(ASK_HINT), aside.text); + assert.equal(aside.boxes, 0, "no agent to read a note"); +}); + +test("a stopped loop that asked nothing still says how to ask", async (t) => { + const s = await openLoopPage(t); + s.post({ loopEnd: STOPPED, agent: { status: "waiting" } }); + await s.page.locator(".loop-end-actions").waitFor(); + const aside = await asideOf(s); + assert.ok(aside.text.includes(ASK_HINT), aside.text); + assert.equal(aside.boxes, 0); +}); + +test("a finished loop and a stop with no Ask keep the notes panel's old words", async (t) => { + const s = await openLoopPage(t); + s.post({ items: [SETTLED], loopEnd: { ...STOPPED, kind: "finished", line: "All phases done", can: ["finalize", "dashboard"] }, agent: { status: "waiting" } }); + await s.page.locator(".loop-end-actions").waitFor(); + let aside = await asideOf(s); + assert.ok(!aside.text.includes(ASK_HINT), "a finished loop does not offer Ask"); + assert.match(aside.text, /Nothing was said about this one/); + s.post({ loopEnd: { ...STOPPED, can: ["resume", "fix", "dashboard"] } }); + await s.page.getByRole("button", { name: "Resume the loop" }).waitFor(); + aside = await asideOf(s); + assert.ok(!aside.text.includes(ASK_HINT), "no Ask button, so no hint to press it"); + assert.match(aside.text, /Nothing was said about this one/); +}); + test("a loop at work tells the person they have nothing to do, and Send comes back when a question waits", async (t) => { const s = await openLoopPage(t); const foot = s.page.locator(".footer"); diff --git a/src/web-console/public/app-bridge.js b/src/web-console/public/app-bridge.js index d38947a..eed4e20 100644 --- a/src/web-console/public/app-bridge.js +++ b/src/web-console/public/app-bridge.js @@ -63,7 +63,7 @@ export function wireLoopMessages(store, redraw) { store.loopNotifyTest = { waiting: false, ok: data.ok === true, sent: data.sent === true, error: data.ok === true ? null : String(data.error || "The app could not send the test.") }; redraw(); }); - for (const kind of ["loop-resume-result", "loop-fix-result", "loop-finalize-result", "loop-dashboard-result"]) { + for (const kind of ["loop-resume-result", "loop-ask-result", "loop-fix-result", "loop-finalize-result", "loop-dashboard-result"]) { onAppMessage(kind, (data) => { store.loopAction = { kind, ok: data.ok === true, error: data.ok === true ? null : String(data.error || "The app could not do that.") }; redraw(); diff --git a/src/web-console/public/screens/ask.js b/src/web-console/public/screens/ask.js index 2f76ce3..fc8df4f 100644 --- a/src/web-console/public/screens/ask.js +++ b/src/web-console/public/screens/ask.js @@ -617,6 +617,14 @@ async function decideChat(re, decision, text) { redraw(); } +// A stopped loop's screen, with its Ask a question button on offer: its agent +// is gone, so a note here would reach nobody, and the panel says how to ask. +const askOffered = () => { + const end = page.S && page.S.loopEnd; + return Boolean(end && end.kind !== "finished" && Array.isArray(end.can) && end.can.includes("ask") && !finish()); +}; +const ASK_HINT = "Press Ask a question to wake an agent, then ask in the Ask tab."; + export function renderAside() { const aside = $("aside"); // Redrawing empties the panel, which drops its scroll to the top. An image @@ -630,7 +638,7 @@ export function renderAside() { const item = page.view === "end" ? null : byId(page.selected); if (!item) { // The menu has no questions, so no notes either: the pane stays empty. - if (!isDashboard()) aside.appendChild(el("p", "aside-empty", "Pick a question to see its notes.")); + if (!isDashboard()) aside.appendChild(el("p", "aside-empty", askOffered() ? ASK_HINT : "Pick a question to see its notes.")); return; } // Off the question's own page (a doc tab, Ask) "this" points at nothing on @@ -644,9 +652,11 @@ export function renderAside() { el( "p", "aside-empty", - isOpen(item) && !finish() - ? "No back and forth yet. Anything you type here goes with your next send." - : "Nothing was said about this one." + askOffered() + ? ASK_HINT + : isOpen(item) && !finish() + ? "No back and forth yet. Anything you type here goes with your next send." + : "Nothing was said about this one." ) ); } @@ -689,7 +699,8 @@ export function renderAside() { // A finished session has nobody left for a note to reach, so the box would // be a place to type words that go nowhere. - if (!isOpen(item) || finish()) { + // A stopped loop has no agent either, even with a question left open. + if (!isOpen(item) || finish() || askOffered()) { aside.scrollTop = place; return; } diff --git a/src/web-console/public/screens/ending.js b/src/web-console/public/screens/ending.js index 3437fd7..d2adbda 100644 --- a/src/web-console/public/screens/ending.js +++ b/src/web-console/public/screens/ending.js @@ -162,6 +162,7 @@ function closeGuideHeld() { let loopPressed = null; const LOOP_BUTTONS = [ + ["ask", "loop-ask", "Ask a question", ""], ["resume", "loop-resume", "Resume the loop", "primary"], ["fix", "loop-fix", "Fix it by hand", ""], ["finalize", "loop-finalize", "Finalize", "primary"],