diff --git a/docs/e2e.md b/docs/e2e.md index 6619906..146cd41 100644 --- a/docs/e2e.md +++ b/docs/e2e.md @@ -28,8 +28,8 @@ the real daemon/plugin/browser, never `MOCK.*` data. The v1 gate is green when: -- **A0–A13** (API/WS against a real headless nvim + real pi) pass. -- **B1–B14** (Playwright against the daemon-served production bundle) pass. +- **A0–A14** (API/WS against a real headless nvim + real pi) pass. +- **B1–B15** (Playwright against the daemon-served production bundle) pass. - **C1–C4** (responsive, theme, language, no console errors) pass. - `tsc --noEmit`, `vitest run` (daemon), `vitest run` (web) stay green. - Every failure found while running this plan is either fixed and re-verified, or @@ -110,8 +110,8 @@ then the browser suite, each in its own temp root. Both drivers are standalone w ## 4. Scenarios -All scenario ids below are implemented in the two drivers: `scenario("A0", …)` … `scenario("A13", …)` -in `api-e2e.mjs`, `scenario("B1", …)` … `scenario("B14", …)` and `scenario("C1", …)` … +All scenario ids below are implemented in the two drivers: `scenario("A0", …)` … `scenario("A14", …)` +in `api-e2e.mjs`, `scenario("B1", …)` … `scenario("B15", …)` and `scenario("C1", …)` … `scenario("C4", …)` in `ui-e2e.mjs`. A driver exits non-zero only on **FAIL**; SKIP is tolerant, and every skip is printed with its reason and written into the report. @@ -235,6 +235,18 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s daemon (next request still 200). - every error is a JSON body with `code`, `message`, `retryable`. +**A14 Prompt history recall** (`GET /workspaces/:id/prompt-history`, protocol §3.1.1) +- the prompt A4 sent through `prompt.send` is in the plugin's archive: `live:true`, + `supported:true`, and the text appears **exactly once** in `entries` — the proof that + the round trip browser → daemon → plugin archive → daemon → browser works, and that a + web send is recorded by the plugin at all (`prompt.send` does not go through the chat + UI, so this is a real regression guard). +- `?limit=1` answers exactly one entry equal to the newest of the full read. +- a freshly added workspace with **no** instance answers `200 {live:false, supported:false, + entries:[]}`, and its `/workspaces` row still has `instance: null` afterwards: the read + must never spawn (`instances.ensure()` would boot an nvim on a keystroke). +- unknown workspace → `404`. + ### 4.2 B — Browser suite (Playwright, daemon-served bundle) **B1 Unpaired guard** @@ -326,6 +338,15 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s back restores English; no missing-key placeholder. - switch theme light/dark/system: `` changes and persists. +**B15 Composer recall** +- open the B6 session in a fresh page, delete `localStorage` `pi2-prompt-history` and reload, + then confirm the console still fetched `GET /workspaces/:id/prompt-history` (200) — recall + must work from the plugin's archive, not from the browser mirror. +- `↑` recalls the newest archived prompt, and walking `↑` reaches the prompt B6 sent (later + scenarios write into the same workspace archive, so the newest entry is not necessarily + B6's); walking `↓` back out restores the empty draft; `Ctrl+P` recalls from a mid-line caret. +- recall sends nothing: no `prompt.send` may appear while browsing. + ### 4.3 C — Responsive / visual - **C1** 1440×900, 768×1024, 380×780 on `/workspaces`, `/chat`, `/status`, `/connect` @@ -384,10 +405,10 @@ which were skipped and why (non-blocking only), and the command that reproduces ```bash cd # the pi2 CLI/daemon checkout (this repository) -# backend only (A0–A13): tsc, then `pi2 daemon` + headless nvim + real pi +# backend only (A0–A14): tsc, then `pi2 daemon` + headless nvim + real pi bash scripts/e2e/run-api.sh -# browser (B1–B14, C1–C4): also builds web/dist (pnpm), then Playwright +# browser (B1–B15, C1–C4): also builds web/dist (pnpm), then Playwright bash scripts/e2e/run-ui.sh # both, API first; exit 0 only when neither suite failed diff --git a/scripts/e2e/api-e2e.mjs b/scripts/e2e/api-e2e.mjs index 694332b..7412e8e 100644 --- a/scripts/e2e/api-e2e.mjs +++ b/scripts/e2e/api-e2e.mjs @@ -597,6 +597,8 @@ function deviation(key, title, detail, scenarioId) { const state = { w1: null, w2: null, + /** A workspace that must stay un-spawned (A14's live-only prompt-history read). */ + w3: null, sid: null, // backend session id of the W1 session sidTmp: null, // placeholder id returned by session.create sessionFile: null, @@ -606,6 +608,8 @@ const state = { childId: null, routedWithWorkspace: false, attentionFile: null, + /** A4 landed a `prompt.send`, so the plugin archive must hold that text. */ + promptSent: false, }; // --------------------------------------------------------------------------- @@ -631,6 +635,25 @@ async function sessionsList(workspaceId) { return { res, rows: rows ?? [] }; } +/** + * `GET /workspaces/:id/prompt-history` — the composer's ↑/↓ feed + * (`docs/protocol.md` §3.1.1). `query` is appended verbatim, so a caller can pass + * `"?limit=1"` without a second helper. + */ +async function promptHistory(workspaceId, query = "") { + const res = await http("GET", `/api/v1/workspaces/${workspaceId}/prompt-history${query}`, { + token: CLI_TOKEN, + }); + const body = res.json ?? {}; + return { + res, + body, + live: body.live, + supported: body.supported, + entries: Array.isArray(body.entries) ? body.entries : null, + }; +} + async function addWorkspace(name, ctx) { const cwd = join(WORKSPACE_ROOTS, name); mkdirSync(cwd, { recursive: true }); @@ -1406,6 +1429,8 @@ scenario("A4", "Prompt + streaming (real model)", async (ctx) => { observed: apiError(send), }); if (send.status !== 202) return; + // A14 reads this prompt back out of the plugin's archive. + state.promptSent = true; let settled; try { @@ -3023,6 +3048,78 @@ scenario("A13", "Console login handoff", async (ctx) => { ctx.eq("the new token works → 200", freshToken.status, 200); }); +// --------------------------------------------------------------------------- +// A14 — prompt history recall (the composer's ↑/↓) +// --------------------------------------------------------------------------- + +scenario("A14", "Prompt history recall", async (ctx) => { + if (!state.w1) skip("A1 did not produce a workspace id"); + if (!state.promptSent) skip("A4 did not send a prompt"); + + // The read behind the composer's ↑/↓. A4 sent PONG_PROMPT through + // `prompt.send`, and only the plugin can answer this — the daemon stores + // nothing — so a hit here is the whole round trip proven: + // browser → daemon → plugin archive → daemon → browser. + const full = await promptHistory(state.w1); + ctx.eq("GET /workspaces/:id/prompt-history → 200", full.res.status, 200); + ctx.eq("the read is versioned (v=1)", full.body.v, 1); + ctx.eq("workspaceId is echoed", full.body.workspaceId, state.w1); + ctx.eq("a connected plugin answers the read", full.live, true); + ctx.eq("the plugin advertises the promptHistory capability", full.supported, true); + ctx.check("entries is a string[]", full.entries !== null, { + expected: "entries: string[]", + observed: fmt(full.body, 300), + }); + const entries = full.entries ?? []; + const hits = entries.filter((entry) => entry === PONG_PROMPT).length; + ctx.check("the prompt sent from the console was recorded by the plugin, exactly once", hits === 1, { + expected: `"${PONG_PROMPT}" ×1`, + observed: { hits, total: entries.length, tail: entries.slice(-3) }, + }); + + // `limit` trims from the newest end: the plugin's archive is oldest-first, so + // a recall feed asks for the tail. + const one = await promptHistory(state.w1, "?limit=1"); + ctx.eq("?limit=1 → 200", one.res.status, 200); + const single = one.entries ?? []; + ctx.check("?limit=1 answers exactly one entry", single.length === 1, { + expected: 1, + observed: single.length, + }); + ctx.eq("?limit=1 is the newest archived prompt", single[0], entries.at(-1)); + + // A workspace with no connected plugin answers `live:false` **without + // spawning**: recall is a convenience, never a lifecycle event. This is the + // assertion that would catch `instances.ensure()` creeping back into the read. + const cold = await addWorkspace("w3-history", ctx); + state.w3 = cold.workspaceId; + const coldRead = await promptHistory(state.w3); + ctx.eq("a workspace with no instance still answers 200", coldRead.res.status, 200); + ctx.eq("…with live:false", coldRead.live, false); + ctx.eq("…with supported:false", coldRead.supported, false); + ctx.check("…and no entries", coldRead.entries !== null && coldRead.entries.length === 0, { + expected: "entries: []", + observed: fmt(coldRead.body, 200), + }); + const coldRow = await workspaceRow(state.w3); + ctx.check("the cold read did not spawn an instance", (coldRow?.instance ?? null) === null, { + expected: "instance: null", + observed: coldRow?.instance ?? null, + }); + const coldAgain = await promptHistory(state.w3); + ctx.eq("a second cold read is equally idle", coldAgain.live, false); + + const unknown = await promptHistory("0123456789abcdef"); + ctx.eq("an unknown workspace → 404", unknown.res.status, 404); + + ctx.evidence.promptHistory = { + workspaceId: state.w1, + archived: entries.length, + newest: entries.at(-1)?.slice(0, 80), + coldWorkspace: state.w3, + }; +}); + // --------------------------------------------------------------------------- // reporting // --------------------------------------------------------------------------- diff --git a/scripts/e2e/ui-e2e.mjs b/scripts/e2e/ui-e2e.mjs index 7c941c0..7c02e99 100644 --- a/scripts/e2e/ui-e2e.mjs +++ b/scripts/e2e/ui-e2e.mjs @@ -663,6 +663,9 @@ async function waitEnabled(page, selector, timeout = 30000) { const COMPOSER_INPUT = '[data-testid="composer-input"]'; +/** The short prompt B6 sends; B15 recalls it out of the plugin's archive. */ +const PONG_PROMPT = "Reply with exactly the word PONG and nothing else."; + /** `article.msg[data-entry-id]` projections (id/role come from the product test ids). */ async function streamArticles(page) { return page.evaluate(() => @@ -1606,7 +1609,7 @@ scenario("B6", "Prompt + streaming: PONG turn, one user entry, no .streaming aft await textarea.fill(""); // --- Enter sends, the assistant streams and settles ------------------ - const prompt = "Reply with exactly the word PONG and nothing else."; + const prompt = PONG_PROMPT; await textarea.fill(prompt); await page.keyboard.press("Enter"); const sent = await waitFor(async () => ctx.cmdRequests.find((request) => request.method === "prompt.send") ?? null, 15000); @@ -2657,6 +2660,98 @@ scenario("B14", "i18n + theme: language menu switches html[lang]/copy, theme seg await ctx.shot(page, "b14-theme-i18n"); }); +/* ------------------------------------------------------------------ */ +/* B15 — composer ↑/↓ recall from the plugin archive */ +/* ------------------------------------------------------------------ */ + +scenario("B15", "Composer recall: ↑ restores the prompt B6 sent, over the relay (mirror wiped)", async (ctx) => { + const pong = state.pongSession; + if (!pong) ctx.skip("B6 did not produce a session to reuse"); + const { page } = await ctx.openPage("paired"); + + ctx.check("chat page becomes usable", await openChat(page, pong.sessionId, pong.workspaceId), { + expected: "textarea[aria-label='Message input'] enabled", + observed: { composer: await composerState(page) }, + }); + + // Delete this browser's fallback mirror and reload, so recall can only work + // from the relayed archive (docs/prompt-history.md: the plugin owns the text, + // the mirror is only a fallback). A regression that dropped the relay would + // leave nothing to recall here. + await page.evaluate(() => localStorage.removeItem("pi2-prompt-history")); + await page.reload({ waitUntil: "domcontentloaded" }); + ctx.check("chat page reloads usable", await openChat(page, pong.sessionId, pong.workspaceId), { + expected: "composer enabled after reload", + observed: { composer: await composerState(page) }, + }); + + const relayed = await waitFor( + async () => ctx.apiCalls.find((call) => call.path.includes("/prompt-history") && call.status === 200) ?? null, + 20000, + ); + ctx.check("the console reads GET /workspaces/:id/prompt-history", Boolean(relayed), { + expected: "200 on /prompt-history", + observed: ctx.apiCalls.map((call) => `${call.method} ${call.path} ${call.status}`).slice(-10), + }); + + const textarea = page.locator(COMPOSER_INPUT); + await textarea.click(); + await textarea.fill(""); + + // The archive is workspace-scoped, and later scenarios (B7's abort, B10's + // attention turn) send into the *same* workspace — so the newest entry is not + // necessarily B6's. Recall the newest first, then walk back until B6's prompt + // shows up; both directions are part of the contract, so this asserts more + // than a single-step recall would. + await page.keyboard.press("ArrowUp"); + const newest = await waitFor(async () => (await textarea.inputValue()).trim() || null, 15000); + ctx.check("↑ recalls the newest archived prompt", Boolean(newest), { + expected: "a non-empty prompt", + observed: newest, + }); + + let reachedPong = newest === PONG_PROMPT; + for (let step = 0; step < 30 && !reachedPong; step += 1) { + // `↑` only recalls from the first line (nvim's rule), so park the caret + // there before each step. + await page.keyboard.press("Control+Home"); + await page.keyboard.press("ArrowUp"); + await sleep(120); + reachedPong = (await textarea.inputValue()) === PONG_PROMPT; + } + ctx.check("↑ walks back to the prompt B6 sent (archive round trip)", reachedPong, { + expected: PONG_PROMPT, + observed: await textarea.inputValue(), + }); + + // ↓ all the way back out: the stashed (empty) draft returns. + let draftBack = false; + for (let step = 0; step < 40 && !draftBack; step += 1) { + await page.keyboard.press("ArrowDown"); + await sleep(120); + draftBack = (await textarea.inputValue()) === ""; + } + ctx.check("↓ walks back out to the empty draft", draftBack, { + expected: "", + observed: await textarea.inputValue(), + }); + + // Ctrl+P is the readline alias: it recalls without moving the caret first. + await textarea.fill("half typed"); + await page.keyboard.press("Control+p"); + const viaCtrlP = await waitFor(async () => (await textarea.inputValue()) || null, 10000); + ctx.check("Ctrl+P recalls the newest entry from a mid-line caret", viaCtrlP === newest, { + expected: newest, + observed: viaCtrlP, + }); + + ctx.check("recall never sent anything", + !ctx.cmdRequests.some((request) => request.method === "prompt.send"), + { observed: ctx.cmdRequests.map((request) => request.method) }, + ); + await ctx.shot(page, "b15-composer-recall"); +}); + /* ------------------------------------------------------------------ */ /* C1 — responsive overflow */ /* ------------------------------------------------------------------ */