// Live browser check for the web client: serve the page, open it in headless // Chrome, connect to a running Pipelines server through the page's own proxy, // and capture what actually rendered. // // node test/ui-smoke.mjs [--port PORT] [--pipelines 127.0.0.1:8080] [--out DIR] [--scratch] // // Without --port the OS picks a free port, so concurrent checks do not collide. // // This is the UI evidence a review stage produces: real screenshots of the // rendered client, not a claim. It needs a running Pipelines server (the live one // or a scratch one) and the installed Chrome; it has no npm dependencies. // // `--scratch` drives ONLY the P5 create-from-template flow (captures 51/52/53) // against a scratch server and skips the live suite, which needs the operator's // real runs and chat session. The default run never sends a `submit`: the live // server's pipeline list is never mutated. // // `--viewer` drives ONLY R8's deterministic markdown/text viewer fixture (no // dependency on the operator's current run history) and captures 44–48. // // `--decide` drives ONLY R11's live human-decision flow: it reuses an existing // entry-stage human pipeline or submits one uniquely named create-only // definition, starts two throwaway runs in fresh temp workspaces, and presses // the real card/sheet/Confirm decision for both the feedback and no-feedback // paths. It is deliberately opt-in because it is the only mode that submits. import { spawn } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import net from "node:net"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { eventually, launchChrome } from "./browser.mjs"; import { SERVER_URL_HELPER, SERVER_URL_PLACEHOLDER, SERVER_URL_STORAGE_KEY, TEMPLATES_CARD_LABEL, TEMPLATES_CARD_SECONDARY, TRUST_BODY, TRUST_TITLE, WORKSPACE_ERROR, WORKSPACE_HELPER, WORKSPACE_PLACEHOLDER, WORKSPACE_STORAGE_KEY, } from "../src/settings.js"; import { NOTIFICATION_DEFAULT, NOTIFICATION_DENIED, NOTIFICATION_GRANTED, NOTIFY_STORAGE_KEY, } from "../src/notify.js"; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), ".."); /** A port that is bound and immediately released, so nothing listens on it. */ async function freePort() { return new Promise((resolvePort) => { const server = net.createServer(); server.listen(0, "127.0.0.1", () => { const { port: bound } = server.address(); server.close(() => resolvePort(bound)); }); }); } function flag(argv, name, fallback) { const i = argv.indexOf(name); return i !== -1 && argv[i + 1] ? argv[i + 1] : fallback; } // No fixed default port: an explicit --port/WEB_UI_PORT still wins, but otherwise // each run asks the OS for a free one. Concurrent checks (parallel pipeline items) // therefore never collide on 8091 — the earlier fixed default made a second // simultaneous UI stage fail with EADDRINUSE. const explicitPort = flag(process.argv, "--port", process.env.WEB_UI_PORT); const port = explicitPort ? Number(explicitPort) : await freePort(); const pipelines = flag(process.argv, "--pipelines", process.env.PIPELINES_TARGET ?? "127.0.0.1:8080"); const out = resolve( flag(process.argv, "--out", process.env.WEB_UI_OUT ?? join(tmpdir(), "pipelines-web-ui")), ); // Scratch mode: only the P5 create-from-template flow runs, against a throwaway // server with its own database. The default (live) run sends no submit. const scratch = process.argv.includes("--scratch"); // Focused notification evidence only; skips the unrelated live-suite steps. const notifyOnly = process.argv.includes("--notify"); // Focused R11 evidence only; the one explicit mode allowed to submit a pipeline. const decideOnly = process.argv.includes("--decide"); // Focused R8 evidence only: the deterministic markdown viewer fixture, with no // dependency on the operator's current run history. const viewerOnly = process.argv.includes("--viewer"); // Artifact URLs resolve against the configured server. Point them at the page's // own proxy origin (serve.mjs forwards /artifacts/ and /connect), so the viewer's // markdown fetch and the image/video tiles are same-origin exactly as they are // in a correctly configured deployment. const artifactServerUrl = `ws://127.0.0.1:${port}/connect`; const pipelineUrl = `ws://${pipelines}/connect`; const shots = []; let serve = null; let browser = null; let page = null; let failed = false; // The throwaway chat session this run creates for the running-turn capture. It // is closed again in cleanup; null once closed (or when none was created). let throwawaySessionId = null; // R10 evidence: the one small real run this smoke starts in a throwaway // workspace and the workspace itself, both cleaned up in `finally`. let liveRunId = null; let liveRunScratch = null; // R11 evidence: the human-stage pipeline used (reused or uniquely submitted), // every throwaway run started for the decision path, and their temp workspaces. // Cleanup cancels only these runs while they are still active and removes only // these directories; the submitted pipeline definition is intentionally retained. let decidePipelineName = null; let decidePipelineReused = false; const decideRunIds = []; const decideScratchDirs = []; // C2's one real session. It is closed in its own step; kept here only so a // failure before that step can still close it rather than leave it in the list. let createdSessionId = null; function step(label) { console.log(`• ${label}`); } async function shot(name) { const path = `${out}/${name}`; shots.push(await page.screenshot(path)); console.log(` capture ${path}`); return path; } const text = (selector) => page.evaluate(`document.querySelector(${JSON.stringify(selector)})?.textContent ?? ""`); // Bring the composer at the foot of the conversation into view before a capture: // the transcript above it is scrollable and can be taller than the viewport, so a // composer-state screenshot scrolls the page to its very bottom (past the // composer, into the trailing padding) rather than just to the composer's edge. const showComposer = () => page.evaluate(`new Promise((resolve) => { const composer = document.querySelector("#chatComposer"); if (composer) composer.scrollIntoView({ block: "end" }); window.scrollTo(0, document.documentElement.scrollHeight); requestAnimationFrame(() => requestAnimationFrame(() => resolve(true))); })`); // Show the foot of an open conversation without scrolling the whole document. // The session list can be taller than the chat pane (the smoke leaves closed // throwaway rows behind), so scrolling to the document bottom would push the // conversation above the viewport and capture an empty pane. The transcript is // the only scroller that needs to move for a command-notice capture. const showChatNotice = () => page.evaluate(`new Promise((resolve) => { window.scrollTo(0, 0); const transcript = document.querySelector("#chatTranscript"); if (transcript) transcript.scrollTop = transcript.scrollHeight; requestAnimationFrame(() => requestAnimationFrame(() => resolve(true))); })`); // The shared live server can drop a long-lived socket while the earlier screens // are captured (observed as a spurious 1006 mid-run). A capture step must not // depend on an unasserted connection, so this nudges the page's own recovery — // its `online` handler resets the backoff — and waits for the shell's green // status word before the step reads anything. Connection lives in Settings now; // the rail/dock status control is the only page-wide indicator. async function ensureConnected(timeout = 25000) { const deadline = Date.now() + timeout; while (Date.now() < deadline) { const ok = await page.evaluate( `document.querySelector("#shellStatus")?.dataset.state === "connected"`, ); if (ok) return; // The page's own recovery path: an `online` event resets the client's // backoff and re-opens the configured endpoint. A dropped long-lived socket // can take a few tries on the shared server, so nudge until the deadline. await page.evaluate(`window.dispatchEvent(new Event("online"))`); await new Promise((resolve) => setTimeout(resolve, 1000)); } // One last honest wait, so the failure names the selector. await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 2000 }); } /** Click the visible destination control (rail on wide, dock on narrow). */ async function clickDestination(label) { const key = label.toLowerCase(); const done = await page.evaluate(`(() => { const all = [...document.querySelectorAll('[data-destination="${key}"], [data-settings-destination="${key}"]')]; const el = all.find((node) => node.offsetParent !== null) ?? all[0]; if (!el) return false; el.click(); return true; })()`); if (!done) throw new Error(`clickDestination: no ${label} control`); } /** Reach Templates through its Settings card (it is a route, not a destination). */ async function openTemplates() { await clickDestination("Settings"); await page.waitForSelector("#browseTemplates", { timeout: 15000 }); await page.click("#browseTemplates"); await page.waitForSelector(".cards button.card", { timeout: 25000 }); } /** Connect (or reconnect) through Settings' one connection section. */ /** Wait for layout/paint after a shell change. */ const settle = (ms = 160) => new Promise((resolve) => setTimeout(resolve, ms)); /** * The shell's observable contract: what is visible, how many scroll owners each * visible pane has, and the tab semantics of every visible tablist. */ async function shellState() { return page.evaluate(`(() => { const visible = (el) => !!el && el.offsetParent !== null; const scrollable = (root) => { if (!root) return []; return [...root.querySelectorAll("*")] .filter((el) => { const style = getComputedStyle(el); return /(auto|scroll)/.test(style.overflowY) && el.clientHeight > 0 && el.scrollHeight > el.clientHeight + 1; }) .map((el) => el.id || el.className); }; const doc = document.scrollingElement || document.documentElement; const listPane = [...document.querySelectorAll(".list-pane")].find(visible) ?? null; const detailPane = [...document.querySelectorAll(".detail-pane")].find(visible) ?? null; const tablists = [...document.querySelectorAll('[role="tablist"]')] .filter(visible) .map((list) => ({ label: list.getAttribute("aria-label") ?? "", tabs: [...list.querySelectorAll(':scope > [role="tab"]')].map((tab) => ({ label: tab.textContent.trim(), selected: tab.getAttribute("aria-selected"), tabindex: tab.getAttribute("tabindex"), controls: tab.getAttribute("aria-controls"), current: tab.getAttribute("aria-current"), controlExists: !!document.getElementById(tab.getAttribute("aria-controls") ?? ""), })), })); const strip = document.querySelector("#sectionStrip"); const dock = document.querySelector("#destinationDock"); return { wide: matchMedia("(min-width: 600px)").matches, removedChrome: { url: !!document.querySelector("#url"), connect: !!document.querySelector("#connectBtn") || !!document.querySelector("#reconnectBtn"), banner: !!document.querySelector(".conn-banner"), topTabs: !!document.querySelector("nav.tabs"), topbar: !!document.querySelector(".topbar"), trustStrip: !!document.querySelector("#noscript-hint"), }, railVisible: visible(document.querySelector("#appRail")), dockVisible: visible(dock), railTabCount: document.querySelectorAll("#railDestinations [role=tab]").length, dockTabCount: document.querySelectorAll("#dockDestinations [role=tab]").length, searchInRail: visible(document.querySelector("#searchToggle")), searchInDock: visible(document.querySelector("#dockSearch")), page: { scrollHeight: doc.scrollHeight, innerHeight: window.innerHeight, scrollWidth: document.documentElement.scrollWidth, innerWidth: window.innerWidth, }, listScrollers: scrollable(listPane), detailScrollers: scrollable(detailPane), detailScroller: detailPane ? getComputedStyle(detailPane.querySelector(".pane-scroll") ?? detailPane).overflowY : getComputedStyle(document.querySelector("#detail-panel") ?? document.body).overflowY, detailPaneWidth: detailPane ? Math.round(detailPane.getBoundingClientRect().width) : 0, detailTitleWidth: document.querySelector("#detailTitle") ? Math.round(document.querySelector("#detailTitle").getBoundingClientRect().width) : 0, h1: [...document.querySelectorAll("h1")].map((node) => node.textContent.trim()), listHeading: document.querySelector("#listTitle")?.textContent.trim() ?? document.querySelector(".settings-page-title")?.textContent.trim() ?? "", tablists, strip: strip && visible(strip) ? { bottom: Math.round(strip.getBoundingClientRect().bottom), top: Math.round(strip.getBoundingClientRect().top), tabs: [...strip.querySelectorAll('[role="tab"]')].map((tab) => tab.textContent.trim()), } : null, dock: dock && visible(dock) ? { top: Math.round(dock.getBoundingClientRect().top) } : null, detailTitle: document.querySelector("#detailTitle")?.textContent.trim() ?? "", activeDestination: document.querySelector('[data-destination][aria-selected="true"]')?.getAttribute("data-destination") ?? "", settingsActive: !!document.querySelector('[data-settings-destination="settings"][aria-current="page"]'), focus: document.activeElement?.id ?? document.activeElement?.className ?? "", }; })()`); } function assertShell(label, state, { wide }) { const removed = state.removedChrome; if (removed.url || removed.connect || removed.banner || removed.topTabs || removed.topbar || removed.trustStrip) { throw new Error(`${label}: removed chrome is still present ${JSON.stringify(removed)}`); } if (wide) { if (!state.railVisible || state.dockVisible) throw new Error(`${label}: expected the rail and no dock`); if (state.railTabCount !== 3) throw new Error(`${label}: rail has ${state.railTabCount} tabs`); if (!state.searchInRail) throw new Error(`${label}: no Search control in the rail`); } else { if (state.railVisible || !state.dockVisible) throw new Error(`${label}: expected the dock and no rail`); if (state.dockTabCount !== 3) throw new Error(`${label}: dock has ${state.dockTabCount} tabs`); if (!state.searchInDock) throw new Error(`${label}: no Search control in the dock`); } const { scrollHeight, innerHeight, scrollWidth, innerWidth } = state.page; if (scrollHeight > innerHeight + 1) throw new Error(`${label}: page scrolls (${scrollHeight} > ${innerHeight})`); if (scrollWidth > innerWidth + 1) throw new Error(`${label}: page scrolls horizontally (${scrollWidth} > ${innerWidth})`); if (state.h1.length !== 1) throw new Error(`${label}: ${state.h1.length} h1 elements (${state.h1.join(" | ")})`); if (!state.listHeading) throw new Error(`${label}: the visible list has no heading`); if (state.listScrollers.length > 1) throw new Error(`${label}: nested list scrollers ${state.listScrollers}`); if (state.detailScrollers.length > 1) throw new Error(`${label}: nested detail scrollers ${state.detailScrollers}`); if (!state.tablists.length) throw new Error(`${label}: no visible tablist`); for (const list of state.tablists) { const selected = list.tabs.filter((tab) => tab.selected === "true"); const zero = list.tabs.filter((tab) => tab.tabindex === "0"); // Settings is not a destination tab; while it is open the rail has none. const settingsRail = list.label === "Destinations" && state.settingsActive; if (selected.length !== 1 && !(settingsRail && selected.length === 0)) { throw new Error(`${label}: ${list.label} has ${selected.length} selected tabs`); } if (zero.length !== 1) throw new Error(`${label}: ${list.label} has ${zero.length} tabindex=0 tabs`); for (const tab of list.tabs) { if (!tab.controls || !tab.controlExists) { throw new Error(`${label}: ${list.label} tab ${tab.label} has no aria-controls target (${tab.controls})`); } } } const destinations = state.tablists.find((list) => list.tabs.some((tab) => tab.current === "page")); if (!destinations && !state.settingsActive) { throw new Error(`${label}: no destination carries aria-current=page`); } if (destinations && !destinations.tabs.some((tab) => tab.label && tab.selected === "true")) { throw new Error(`${label}: no destination tab is selected`); } } /** Close any open detail through its narrow Back control (clickable in both layouts). */ async function showList(destination) { await clickDestination(destination); await settle(200); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); } /** * R15: what the Runs heading's bell actually rendered — the glyph's SVG shape, * the badge, and the two rects the absolutely positioned badge must not move. */ async function attentionBellProbe() { return page.evaluate(`(() => { const button = document.querySelector("#attentionBell"); const svg = button?.querySelector("svg.attention-bell-icon"); const badge = button?.querySelector(".attention-badge"); const paths = svg ? [...svg.querySelectorAll("path")] : []; const rect = (el) => { if (!el) return null; const r = el.getBoundingClientRect(); const round = (n) => Math.round(n * 100) / 100; return { x: round(r.x), y: round(r.y), width: round(r.width), height: round(r.height) }; }; return { exists: !!button, visible: !!button && button.getClientRects().length > 0, label: button?.getAttribute("aria-label") ?? null, title: button?.getAttribute("title") ?? null, svg: !!svg, tagName: svg?.tagName ?? null, viewBox: svg?.getAttribute("viewBox") ?? null, pathCount: paths.length, paths: paths.map((path) => ({ d: path.getAttribute("d"), fill: path.getAttribute("fill"), stroke: path.getAttribute("stroke"), computedFill: getComputedStyle(path).fill, computedStroke: getComputedStyle(path).stroke, strokeWidth: path.getAttribute("stroke-width"), })), width: svg ? getComputedStyle(svg).width : null, height: svg ? getComputedStyle(svg).height : null, text: button?.textContent ?? null, glyphSpans: document.querySelectorAll(".attention-bell-glyph").length, badge: badge ? { text: badge.textContent, dataCount: badge.dataset.count ?? null, position: getComputedStyle(badge).position, } : null, bellRect: rect(button), titleRect: rect(document.getElementById("listTitle")), }; })()`); } /** R15: the layout delta caused only by hiding the badge, then restoring it. */ async function attentionBellBadgeShift() { return page.evaluate(`(() => { const button = document.querySelector("#attentionBell"); const badge = button?.querySelector(".attention-badge"); const title = document.getElementById("listTitle"); const rect = (el) => { const r = el.getBoundingClientRect(); const round = (n) => Math.round(n * 100) / 100; return { x: round(r.x), y: round(r.y), width: round(r.width), height: round(r.height) }; }; if (!badge || !title) return null; const before = { bell: rect(button), title: rect(title) }; badge.style.display = "none"; const hidden = { bell: rect(button), title: rect(title) }; badge.style.display = ""; return { before, hidden }; })()`); } /** The largest edge/corner difference between two measured rects, in px. */ function rectDelta(a, b) { return Math.max( Math.abs(a.x - b.x), Math.abs(a.y - b.y), Math.abs(a.width - b.width), Math.abs(a.height - b.height), ); } const EMOJI = /\p{Extended_Pictographic}/u; /** R15: the bell's glyph must be the decorative monochrome SVG, never an emoji. */ function assertAttentionBellGlyph(probe, count, where) { if (!probe.exists || !probe.visible) throw new Error(`${where}: the bell is not visible`); if (!probe.svg) throw new Error(`${where}: no svg.attention-bell-icon inside #attentionBell`); if (probe.tagName !== "svg") throw new Error(`${where}: the glyph is a ${probe.tagName}, not an svg`); if (probe.viewBox !== "0 0 24 24") throw new Error(`${where}: viewBox is ${probe.viewBox}`); if (probe.pathCount < 1) throw new Error(`${where}: the glyph has no paths`); if (probe.glyphSpans !== 0) throw new Error(`${where}: the old .attention-bell-glyph span is still there`); for (const path of probe.paths) { if (path.fill !== "none" || path.computedFill !== "none") { throw new Error(`${where}: a glyph path is filled (${path.fill}/${path.computedFill})`); } if (path.stroke !== "currentColor" || path.computedStroke === "none") { throw new Error(`${where}: a glyph path does not stroke currentColor (${path.stroke}/${path.computedStroke})`); } } if (probe.width !== "20px" || probe.height !== "20px") { throw new Error(`${where}: the glyph is ${probe.width}×${probe.height}, not 20×20`); } if (EMOJI.test(probe.text ?? "")) throw new Error(`${where}: the bell still draws an emoji (${JSON.stringify(probe.text)})`); const label = count > 0 ? `Notifications, ${count} needing attention` : "Notifications"; if (probe.label !== label) throw new Error(`${where}: the accessible name is "${probe.label}", not "${label}"`); if (probe.title !== label) throw new Error(`${where}: the title is "${probe.title}", not "${label}"`); if (count > 0) { if (!probe.badge) throw new Error(`${where}: no badge at count ${count}`); if (probe.badge.text !== String(count) || probe.badge.dataCount !== String(count)) { throw new Error(`${where}: the badge reads ${JSON.stringify(probe.badge)}, not ${count}`); } if (probe.badge.position !== "absolute") { throw new Error(`${where}: the badge is ${probe.badge.position}, not absolute`); } if ((probe.text ?? "").trim() !== String(count)) { throw new Error(`${where}: the button's text is ${JSON.stringify(probe.text)}, not just the badge count`); } } else { if (probe.badge) throw new Error(`${where}: a badge is still drawn at zero`); if ((probe.text ?? "").trim() !== "") { throw new Error(`${where}: the zero-state button still has text ${JSON.stringify(probe.text)}`); } } if (!probe.bellRect || !probe.titleRect) throw new Error(`${where}: the bell or Runs title has no rect`); } /** R15: hiding the absolute badge must not move the Runs title or the button. */ function assertAttentionBellBadgeShift(shift, where) { if (!shift) throw new Error(`${where}: no badge to hide`); const bellDelta = rectDelta(shift.before.bell, shift.hidden.bell); const titleDelta = rectDelta(shift.before.title, shift.hidden.title); if (bellDelta > 1 || titleDelta > 1) { throw new Error(`${where}: hiding the badge moved the bell ${bellDelta}px / title ${titleDelta}px`); } return { bellDelta, titleDelta }; } /** R15: zero-state and post-dismissal glyphs must be the same SVG, not a rebuild. */ function assertSameAttentionGlyph(probe, reference, where) { const shape = (p) => p.paths.map((path) => `${path.d}:${path.fill}:${path.stroke}:${path.strokeWidth}`).join("|"); if (probe.pathCount !== reference.pathCount || shape(probe) !== shape(reference)) { throw new Error(`${where}: the glyph's paths changed (${probe.pathCount} vs ${reference.pathCount})`); } if (probe.viewBox !== reference.viewBox || probe.width !== reference.width || probe.height !== reference.height) { throw new Error(`${where}: the glyph's box changed`); } } /** R15: leave an open run detail through Back to list so the Runs heading is visible. */ async function returnToRunsList() { if (await page.evaluate(`!!document.querySelector(".pane-back button")`)) { // Hidden on the wide layout, but the control's own handler is the same one // showList uses; the narrow capture below needs the list pane active. await page.evaluate(`document.querySelector(".pane-back button").click()`); await eventually(() => page.evaluate(`location.pathname === "/runs"`), { what: "the Runs list after Back to list", }); } await page.waitForSelector("#listTitle", { timeout: 15000 }); await settle(200); } /** * An open conversation is a chat detail: one h1 naming the conversation, and the * row activation lands focus on it rather than leaving focus on the removed row. */ async function assertOpenChatHeading(label) { const chat = await page.evaluate(`({ h1: [...document.querySelectorAll("h1")].map((node) => node.textContent.trim()), focus: document.activeElement?.id ?? "", })`); if (chat.h1.length !== 1) { throw new Error(`${label}: ${chat.h1.length} h1 elements while a chat is open (${chat.h1.join(" | ")})`); } if (chat.focus !== "detailTitle") { throw new Error(`${label}: activating a chat row left focus on ${chat.focus || "body"}, not the conversation h1`); } } async function runShellChecks() { step("L2/L3/L4: shell at 1440x900 and 420x900 over every destination"); // Start the shell pass from a clean document: the long chat suite above leaves // in-memory chat scroll/selection state, and a fresh open is what the transcript // follow assertion (and the route-focus checks) are about. The device-local // saved transcript positions survive a reload, so drop them too: otherwise the // conversation opens honestly at the operator's old position, not at its newest // item, and the follow assertion would be testing the wrong thing. await page.evaluate(`localStorage.removeItem("pipelines.web.chatScroll.v1")`); await page.goto(`http://127.0.0.1:${port}/`); await page.waitForSelector("#shellStatus", { timeout: 15000 }); await ensureConnected(); await page.resize(1440, 900); await settle(300); // Pipelines (wide): the rail is the only destination chrome. await showList("Pipelines"); await page.waitForSelector("#pipelineRows .row", { timeout: 25000 }); let state = await shellState(); assertShell("wide pipelines", state, { wide: true }); if (!state.tablists.some((list) => list.tabs.every((tab) => tab.current === null || tab.current === "page"))) { throw new Error("wide pipelines: the destination tablist is missing"); } await shot("80-shell-wide-pipelines-1440x900.png"); console.log(` wide pipelines: rail ${state.railTabCount} tabs incl. Search; no page scroll; one list scroller (${state.listScrollers})`); // Pipelines detail (wide): the section rail replaces the old in-detail row. await page.evaluate(`document.querySelector("#pipelineRows .row")?.click()`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); state = await shellState(); assertShell("wide pipeline detail", state, { wide: true }); const railTabs = state.tablists.find((list) => list.label === "Pipeline sections"); if (!railTabs || railTabs.tabs.length !== 3) { throw new Error(`wide pipeline detail: section rail ${JSON.stringify(railTabs)}`); } await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); await clickDestination("Runs"); await page.waitForSelector("#chatRows, .list.runs .run-row", { timeout: 25000 }); state = await shellState(); assertShell("wide runs", state, { wide: true }); const typeAndNumbers = await page.evaluate(`(() => { const title = document.querySelector(".run-row .run-row-title"); const metric = document.querySelector(".metrics .metric-value"); const meta = document.querySelector(".run-row-meta"); const cs = (el) => (el ? getComputedStyle(el) : null); return { titleOverflow: cs(title)?.overflow ?? "", titleEllipsis: cs(title)?.textOverflow ?? "", metricNumeric: cs(metric)?.fontVariantNumeric ?? "", metaNumeric: cs(meta)?.fontVariantNumeric ?? "", }; })()`); if (typeAndNumbers.titleOverflow !== "hidden" || typeAndNumbers.titleEllipsis !== "ellipsis") { throw new Error(`run names are not ellipsized: ${JSON.stringify(typeAndNumbers)}`); } if (!typeAndNumbers.metaNumeric.includes("tabular-nums")) { throw new Error(`durations are not tabular: ${JSON.stringify(typeAndNumbers)}`); } await shot("80-shell-wide-runs-1440x900.png"); console.log(` wide runs: list ${state.listScrollers} / detail ${state.detailScrollers}`); // A run detail (wide): the section rail is visible and the strip is not. await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); const openedRunId = await page.evaluate(`document.querySelector(".list.runs .run-row")?.getAttribute("data-run-id") ?? ""`); await page.evaluate(`document.querySelector(".list.runs .run-row")?.click()`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await settle(250); state = await shellState(); assertShell("wide run detail", state, { wide: true }); const rowFocus = await page.evaluate(`document.activeElement?.id ?? ""`); if (rowFocus !== "detailTitle") { throw new Error(`activating a run row left focus on ${rowFocus || "body"}, not the detail h1`); } const runSections = state.tablists.find((list) => list.label === "Run sections"); if (!runSections || runSections.tabs.length !== 3) { throw new Error(`wide run detail: section rail ${JSON.stringify(runSections)}`); } // On a wide screen the sections are the detail's own tab row (the rail keeps // only destinations): exactly one visible "Run sections" tablist, the strip. if (!state.strip || state.strip.tabs.length !== 3) { throw new Error(`wide run detail: the section tab row is not visible ${JSON.stringify(state.strip)}`); } const sectionLists = state.tablists.filter((list) => list.label === "Run sections"); if (sectionLists.length !== 1) { throw new Error(`wide run detail: ${sectionLists.length} visible section tablists`); } // ArrowRight moves selection and focus inside the section tablist. const sectionMoved = await page.evaluate(`(() => { const active = [...document.querySelectorAll(".strip-tab")].find((tab) => tab.getAttribute("aria-selected") === "true"); active.focus(); active.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true })); return active.textContent.trim(); })()`); await settle(200); const sectionAfter = await page.evaluate(`({ selected: [...document.querySelectorAll(".strip-tab")].find((tab) => tab.getAttribute("aria-selected") === "true")?.textContent.trim() ?? "", focus: document.activeElement?.textContent?.trim() ?? "", })`); if (!sectionAfter.selected || sectionAfter.selected === sectionMoved || sectionAfter.focus !== sectionAfter.selected) { throw new Error(`section ArrowRight did not move selection and focus: ${JSON.stringify({ from: sectionMoved, ...sectionAfter })}`); } await clickDetailTab("Summary"); await shot("81-shell-wide-run-detail-1440x900.png"); console.log(` wide run detail: 3 section tabs; ArrowRight "${sectionMoved}" -> "${sectionAfter.selected}"`); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); const restoredRunRow = await page.evaluate(`document.activeElement?.getAttribute("data-run-id") ?? ""`); if (restoredRunRow !== openedRunId) { throw new Error(`Back from the run detail restored focus to ${restoredRunRow || "no row"}, not row ${openedRunId}`); } await clickDestination("Chat"); await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 }); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); state = await shellState(); assertShell("wide chat", state, { wide: true }); await shot("80-shell-wide-chat-1440x900.png"); // The transcript is the chat detail's one scroller: a freshly opened // conversation follows its newest item, and the transcript can reach its own // bottom without moving the document. const openedChatId = await page.evaluate(`document.querySelector("#chatRows .chat-row")?.getAttribute("data-session") ?? ""`); await page.evaluate(`document.querySelector("#chatRows .chat-row")?.click()`); await page.waitForSelector("#chatTranscript", { timeout: 25000 }); await settle(400); await assertOpenChatHeading("wide chat detail"); const transcript = await page.evaluate(`(() => { const el = document.querySelector("#chatTranscript"); if (!el) return null; return { followsNewest: el.scrollTop + el.clientHeight >= el.scrollHeight - 2, before: el.scrollTop, scrollHeight: el.scrollHeight, clientHeight: el.clientHeight, }; })()`); if (!transcript || !transcript.followsNewest) { throw new Error(`the chat transcript did not follow its newest item: ${JSON.stringify(transcript)}`); } const reachedBottom = await page.evaluate(`(() => { const el = document.querySelector("#chatTranscript"); if (!el) return false; el.scrollTop = el.scrollHeight; return el.scrollTop + el.clientHeight >= el.scrollHeight - 2; })()`); if (!reachedBottom) throw new Error("the chat transcript cannot reach its own bottom"); const stillNoPageScroll = await page.evaluate(`(() => { const doc = document.scrollingElement; return doc.scrollHeight <= innerHeight + 1 && document.documentElement.scrollWidth <= innerWidth + 1; })()`); if (!stillNoPageScroll) throw new Error("opening a chat conversation introduced a page-level scrollbar"); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); const restoredChatRow = await page.evaluate(`document.activeElement?.closest("[data-session]")?.getAttribute("data-session") ?? ""`); if (restoredChatRow !== openedChatId) { throw new Error(`Back from the chat detail restored focus to ${restoredChatRow || "no row"}, not session ${openedChatId}`); } console.log(" wide chat: transcript follows the newest turn, reaches its own bottom, no page scroll; Back restores the row"); await clickDestination("Settings"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); await ensureConnected(); state = await shellState(); assertShell("wide settings", state, { wide: true }); const settingsHosted = await page.evaluate(`({ url: !!document.querySelector("#settingsServerUrl"), connect: !!document.querySelector("#settingsPrimary"), status: !!document.querySelector(".settings-status"), trust: !!document.querySelector(".settings-warning"), h1: document.querySelector("h1")?.textContent ?? "", })`); if (!settingsHosted.url || !settingsHosted.connect || !settingsHosted.status || !settingsHosted.trust || settingsHosted.h1 !== "Settings") { throw new Error(`Settings does not host the connection UI: ${JSON.stringify(settingsHosted)}`); } await shot("80-shell-wide-settings-1440x900.png"); await shot("84-shell-settings-connection-1440x900.png"); console.log(` wide settings: connection card hosted; h1 ${JSON.stringify(settingsHosted.h1)}`); // Templates stays a route (reached from its Settings card) with exactly one h1. await openTemplates(); const templatesState = await shellState(); if (templatesState.h1.length !== 1 || templatesState.h1[0] !== "Templates") { throw new Error(`Templates h1: ${JSON.stringify(templatesState.h1)}`); } if (!templatesState.listHeading) throw new Error("Templates gallery has no list heading"); console.log(" Templates route: one h1 and a list heading, still reachable from Settings"); // The status control is a Settings shortcut; the rail footer is the wide mount. await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); await page.click("#shellStatus"); await eventually(() => page.evaluate(`location.pathname === "/settings"`), { timeout: 15000, what: "the status control to open Settings", }); await page.waitForSelector("#detailTitle", { timeout: 15000 }); await settle(120); const statusShortcut = await page.evaluate(`({ focus: document.activeElement?.id ?? "", title: document.querySelector("#detailTitle")?.textContent ?? "", current: document.querySelector('[data-destination][aria-selected="true"]')?.getAttribute("data-destination") ?? "", settingsCurrent: !!document.querySelector('[data-settings-destination="settings"][aria-current="page"]'), })`); if (statusShortcut.focus !== "detailTitle" || statusShortcut.title !== "Settings" || !statusShortcut.settingsCurrent) { throw new Error(`status shortcut did not land on Settings: ${JSON.stringify(statusShortcut)}`); } await page.evaluate(`document.querySelector("#shellStatus").focus()`); await shot("85-shell-status-indicator-1440x900.png"); console.log(" status indicator -> /settings, focus on the Settings h1"); // Destination ArrowRight: selection follows focus, and the active route moves. await clickDestination("Pipelines"); await page.waitForSelector("#pipelineRows .row", { timeout: 25000 }); const beforeDest = await page.evaluate(`document.querySelector('[data-destination][aria-selected="true"]')?.getAttribute("data-destination")`); await page.evaluate(`(() => { const active = [...document.querySelectorAll(".rail-item")].find((tab) => tab.getAttribute("aria-selected") === "true"); active.focus(); active.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true })); })()`); await settle(350); const afterDest = await page.evaluate(`({ selected: document.querySelector('[data-destination][aria-selected="true"]')?.getAttribute("data-destination") ?? "", focus: document.activeElement?.getAttribute("data-destination") ?? "", path: location.pathname, })`); if (beforeDest !== "pipelines" || afterDest.selected !== "runs" || afterDest.focus !== "runs" || afterDest.path !== "/runs") { throw new Error(`destination ArrowRight failed: ${JSON.stringify({ beforeDest, ...afterDest })}`); } console.log(` destination ArrowRight: ${beforeDest} -> ${afterDest.selected}, focus and URL (${afterDest.path}) agree`); // Skip link is the first focusable on every route. const firstFocusable = await page.evaluate(`(() => { const all = [...document.querySelectorAll('a[href], button, input, select, textarea, [tabindex]:not([tabindex="-1"])')] .filter((el) => !el.disabled && el.getClientRects().length > 0); return all[0]?.className ?? ""; })()`); if (firstFocusable !== "skip-link") throw new Error(`first focusable is ${firstFocusable}, not the skip link`); // Narrow: the same four destinations in the dock, the strip above the bar. await page.resize(420, 900); await settle(350); await clickDestination("Pipelines"); await page.waitForSelector("#pipelineRows .row", { timeout: 25000 }); state = await shellState(); assertShell("narrow pipelines", state, { wide: false }); await shot("82-shell-narrow-pipelines-420x900.png"); console.log(` narrow pipelines: dock ${state.dockTabCount} tabs + Search; no page scroll`); // A pipeline detail narrow: the section strip is directly above the bar too. await page.evaluate(`document.querySelector("#pipelineRows .row")?.click()`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await settle(250); state = await shellState(); assertShell("narrow pipeline detail", state, { wide: false }); if (!state.strip || !state.dock || state.strip.bottom > state.dock.top + 1) { throw new Error(`narrow pipeline detail: strip/bar not adjacent ${JSON.stringify({ strip: state.strip, dock: state.dock })}`); } console.log(` narrow pipeline detail: strip (${state.strip.tabs.join("/")}) above the bar`); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); state = await shellState(); assertShell("narrow runs", state, { wide: false }); await shot("82-shell-narrow-runs-420x900.png"); await page.evaluate(`document.querySelector(".list.runs .run-row")?.click()`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await settle(250); state = await shellState(); assertShell("narrow run detail", state, { wide: false }); if (!state.strip) throw new Error("narrow run detail: the section strip is missing"); if (!state.dock) throw new Error("narrow run detail: the destination bar is missing"); if (state.strip.bottom > state.dock.top + 1 || state.dock.top - state.strip.bottom > 2) { throw new Error(`narrow run detail: strip bottom ${state.strip.bottom} is not directly above the bar top ${state.dock.top}`); } if (state.strip.tabs.length !== 3) throw new Error(`narrow run detail: strip tabs ${state.strip.tabs}`); const stripMoved = await page.evaluate(`(() => { const active = [...document.querySelectorAll(".strip-tab")].find((tab) => tab.getAttribute("aria-selected") === "true"); active.focus(); active.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true })); return active.textContent.trim(); })()`); await settle(200); const stripAfter = await page.evaluate(`({ selected: [...document.querySelectorAll(".strip-tab")].find((tab) => tab.getAttribute("aria-selected") === "true")?.textContent.trim() ?? "", focus: document.activeElement?.textContent?.trim() ?? "", })`); if (!stripAfter.selected || stripAfter.selected === stripMoved || stripAfter.focus !== stripAfter.selected) { throw new Error(`strip ArrowRight did not move selection and focus: ${JSON.stringify({ from: stripMoved, ...stripAfter })}`); } await clickDetailTab("Summary"); await shot("83-shell-narrow-run-detail-420x900.png"); console.log(` narrow run detail: strip (${state.strip.tabs.join("/")}) bottom ${state.strip.bottom}px, bar top ${state.dock.top}px; ArrowRight "${stripMoved}" -> "${stripAfter.selected}"`); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); await clickDestination("Chat"); await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 }); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); state = await shellState(); assertShell("narrow chat", state, { wide: false }); // Opening a chat on the narrow layout also yields exactly one h1 and moves focus // to it, then Back returns to the list for the capture. await page.evaluate(`document.querySelector("#chatRows .chat-row")?.click()`); await page.waitForSelector("#chatTranscript", { timeout: 25000 }); await settle(300); await assertOpenChatHeading("narrow chat detail"); await page.evaluate(`document.querySelector(".pane-back button")?.click()`); await settle(200); await shot("82-shell-narrow-chat-420x900.png"); await clickDestination("Settings"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); // The dock's status control is the narrow counterpart of the rail footer. await page.click("#shellStatusNarrow"); await eventually(() => page.evaluate(`location.pathname === "/settings"`), { timeout: 15000, what: "the narrow status control to open Settings", }); await settle(150); const narrowStatusFocus = await page.evaluate(`({ focus: document.activeElement?.id ?? "", title: document.querySelector("#detailTitle")?.textContent ?? "", })`); if (narrowStatusFocus.focus !== "detailTitle" || narrowStatusFocus.title !== "Settings") { throw new Error(`narrow status shortcut failed: ${JSON.stringify(narrowStatusFocus)}`); } state = await shellState(); assertShell("narrow settings", state, { wide: false }); if (state.detailScroller !== "auto") { throw new Error(`narrow settings: the Settings column is not its own scroller (${state.detailScroller})`); } await shot("82-shell-narrow-settings-420x900.png"); console.log(` narrow settings: one scrolled Settings column (${state.detailScroller})`); // The 600px breakpoint is the tightest wide layout: the rail is 200px and the // workspace 400px, so the list (up to 360px) must not starve the detail below // its 200px minimum. This is the regression the design review measured. await page.resize(600, 900); await settle(300); await clickDestination("Pipelines"); await page.waitForSelector("#pipelineRows .row", { timeout: 25000 }); await page.evaluate(`document.querySelector("#pipelineRows .row")?.click()`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await settle(250); state = await shellState(); assertShell("600px pipeline detail", state, { wide: true }); if (state.detailPaneWidth < 199) { throw new Error(`600px pipeline detail: the detail pane is only ${state.detailPaneWidth}px wide`); } if (state.detailTitleWidth < 120) { throw new Error(`600px pipeline detail: the title box is only ${state.detailTitleWidth}px wide`); } console.log(` 600px pipeline detail: detail pane ${state.detailPaneWidth}px, title ${state.detailTitleWidth}px, no page scroll`); await page.resize(1440, 900); await settle(250); } async function clickDetailTab(label) { const done = await page.evaluate(`(() => { const all = [...document.querySelectorAll('.rail-tab, .strip-tab')]; const el = all.find((node) => node.offsetParent !== null && node.textContent.trim() === ${JSON.stringify(label)}); if (!el) return false; el.click(); return true; })()`); if (!done) throw new Error(`clickDetailTab: no visible ${label} tab`); await new Promise((resolve) => setTimeout(resolve, 30)); } /** The visible detail section labels, in author order. */ async function detailTabLabels() { return page.evaluate( `[...document.querySelectorAll('.rail-tab, .strip-tab')].filter((node) => node.offsetParent !== null).map((node) => node.textContent.trim())`, ); } async function connectFromSettings() { await clickDestination("Settings"); await page.waitForSelector("#settingsPrimary", { timeout: 15000 }); const primary = await text("#settingsPrimary"); if (primary === "Connect" || primary === "Reconnect") { await page.click("#settingsPrimary"); } await ensureConnected(); } /** * Hard-navigate to a page route and connect, exactly as a pasted link would: a * full document load parses the URL at boot and re-opens the endpoint already * persisted in localStorage (connection moved into Settings, so a deep link must * not require a detour through that destination). With no persisted URL the * caller connects through Settings instead. */ async function openDeepLink(path) { await page.goto(`http://127.0.0.1:${port}${path}`); await page.waitForSelector("#shellStatus", { timeout: 15000 }); const hasEndpoint = await page.evaluate(`!!localStorage.getItem("pipelines.web.serverUrl.v2")`); if (!hasEndpoint) await connectFromSettings(); await ensureConnected(30000); } /** The current path+query the page shows. */ const currentLocation = () => page.evaluate(`location.pathname + location.search`); // Open a run's detail. The connection is asserted first, and if the socket drops // between steps the page's reconnect does not replay the detail request, so the // click is retried once after re-establishing the connection. async function openRunDetail(runId) { const row = `.list.runs .run-row-meta[data-run-id="${runId}"]`; for (let attempt = 0; attempt < 2; attempt += 1) { await ensureConnected(); try { await page.waitForSelector(row, { timeout: 20000 }); await page.click(row); await page.waitForSelector("#detailTitle", { timeout: 20000 }); // The section is sticky across runs now, so a run can open on whatever tab // the previous one showed. This helper's callers expect the Summary. await clickDetailTab("Summary"); // The tabstrip already exists from the previously opened run, so it is not // proof this run's detail loaded. Wait for this run's own identity line // (`run {id} · …`) before returning, or the next assertion can read the // previous run's actions. await eventually( () => page.evaluate( `document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${runId} ") ?? false`, ), { timeout: 20000, what: `run ${runId}'s detail to load` }, ); return; } catch (error) { if (attempt === 1) throw error; console.log(` retrying run ${runId} after a dropped connection`); } } } // --- R10: one small real run for live-push evidence ------------------------ // Runs the CLI in a throwaway workspace (never the repo) so a real `run_event` // push can be observed against the live server. `--json` admits the run and // exits; the caller waits for the pushed fold and then cancels it. Best-effort: // a missing CLI only means the capture falls back, it never fails the smoke. function runCli(args, cwd) { const bin = process.env.PIPELINES_BIN || "pipelines"; const child = spawn(bin, args, { cwd, stdio: ["ignore", "pipe", "pipe"] }); let stdout = ""; let stderr = ""; child.stdout.on("data", (chunk) => (stdout += chunk.toString())); child.stderr.on("data", (chunk) => (stderr += chunk.toString())); return new Promise((resolvePromise) => { child.on("close", (code) => resolvePromise({ code, stdout, stderr })); }); } async function startLiveRun() { const dir = await mkdtemp(join(tmpdir(), "pipelines-web-r10-")); liveRunScratch = dir; const result = await runCli( [ "run", "Complete a Task", // A prompt that keeps the model busy for a few seconds, so the pushed // "running" detail can be captured and the cancel lands while it executes. "Write a detailed 600-word essay on why small, focused changes are easier to review than large ones. Do not use any tools and do not change any files.", "--title", "Web R10 live badge check", "--json", "--yes", ], dir, ); if (result.code !== 0) { throw new Error(`pipelines run exited ${result.code}: ${result.stderr.trim().slice(-300)}`); } const id = Number(JSON.parse(result.stdout).run_detail?.id); if (!(id > 0)) throw new Error(`pipelines run returned no run id: ${result.stdout.slice(0, 200)}`); liveRunId = id; return id; } async function cancelLiveRun(id) { const result = await runCli(["cancel", String(id), "--yes", "--json"], liveRunScratch ?? tmpdir()); if (result.code !== 0) { console.log(` cancel ${id} exited ${result.code}: ${result.stderr.trim().slice(-200)}`); } } // --- R11: live human-decision evidence ------------------------------------- // A human stage parks a run without starting a model, so it is safe to run on // the live server in throwaway workspaces. This path is reached only with the // explicit `--decide` flag: the default suite still never submits a pipeline. const DECIDE_PROMPT = "R11 live decision: review the run and choose approve or hold."; const DECIDE_OPTIONS = ["approve", "hold"]; function cliJson(result, what) { if (result.code !== 0) { throw new Error(`${what} exited ${result.code}: ${result.stderr.trim().slice(-400)}`); } try { return JSON.parse(result.stdout); } catch { throw new Error(`${what} returned no JSON: ${result.stdout.slice(0, 300)}`); } } async function inspectRun(id) { return cliJson( await runCli(["inspect", String(id), "--json", "--url", pipelineUrl], tmpdir()), `pipelines inspect ${id}`, ); } async function waitForRun(id, predicate, what, timeout = 60000) { const deadline = Date.now() + timeout; let detail = null; while (Date.now() < deadline) { detail = await inspectRun(id); if (predicate(detail)) return detail; await new Promise((resolvePromise) => setTimeout(resolvePromise, 250)); } throw new Error(`run ${id} did not ${what}; last status ${detail?.status ?? "unknown"}`); } /** * The one pipeline R11 may use without submitting: an existing definition whose * entry stage is already `type=human`, so starting it runs no model stage. A * human stage later in a model pipeline is deliberately not reused. */ async function findEntryHumanPipeline() { const list = cliJson( await runCli(["list", "--json", "--url", pipelineUrl], tmpdir()), "pipelines list", ); for (const item of list.pipelines ?? []) { const shown = await runCli(["show", String(item.id), "--json", "--url", pipelineUrl], tmpdir()); if (shown.code !== 0) continue; let detail = null; try { detail = JSON.parse(shown.stdout); } catch { continue; } if (detail?.graph?.stages?.[0]?.type === "human") { return { id: Number(item.id), name: String(detail.name ?? item.name) }; } } return null; } /** The tiny create-only definition: one human stage, both options succeed. */ function humanOnlySource() { return [ "# stage gate", "", "```output", "Decision evidence.", "```", "", "```prompt", DECIDE_PROMPT, "```", "", "```edges", "approve -> #success", "hold -> #success", "error -> #failure", "```", "", "```configs", "type=human", "```", "", ].join("\n"); } /** * Use an existing entry-human pipeline, or submit exactly one uniquely named * create-only definition. Then start two runs in separate fresh workspaces (the * server permits one run per workspace) and wait for both to park in `waiting`. * Every run id and workspace is tracked for the `finally` cleanup. */ async function prepareHumanDecisionRuns() { const existing = await findEntryHumanPipeline(); if (existing) { decidePipelineName = existing.name; decidePipelineReused = true; console.log(` reusing entry-human pipeline ${existing.name} (id ${existing.id}); no submit`); } else { const definitionDir = await mkdtemp(join(tmpdir(), "pipelines-web-r11-def-")); decideScratchDirs.push(definitionDir); const definition = join(definitionDir, "human.md"); await writeFile(definition, humanOnlySource(), "utf8"); const stamp = new Date().toISOString().replace(/[:.]/g, "-"); const requested = `Web R11 decision ${stamp}`; const submitted = cliJson( await runCli( [ "submit", definition, "--name", requested, "--create-only", "--yes", "--json", "--url", pipelineUrl, ], definitionDir, ), "pipelines submit", ); decidePipelineName = String(submitted.name ?? requested).trim() || requested; console.log(` submitted create-only pipeline ${JSON.stringify(decidePipelineName)}`); } const prepared = []; for (const [index, suffix] of [ [1, "feedback"], [2, "no-feedback"], ]) { const workspace = await mkdtemp(join(tmpdir(), "pipelines-web-r11-")); decideScratchDirs.push(workspace); const title = `Web R11 decision ${index} (${suffix})`; const started = cliJson( await runCli( [ "run", decidePipelineName, "R11 web decision smoke", "--workspace", workspace, "--title", title, "--json", "--yes", "--url", pipelineUrl, ], workspace, ), `pipelines run ${decidePipelineName}`, ); const id = Number(started.run_detail?.id); if (!(id > 0)) throw new Error(`pipelines run returned no run id: ${started ? JSON.stringify(started).slice(0, 200) : "no output"}`); decideRunIds.push(id); prepared.push({ id, title, suffix, workspace }); } for (const run of prepared) { const detail = await waitForRun( run.id, (d) => d.status === "waiting" && d.current_stage === "gate", "reach waiting on the gate stage", ); const waitingStep = (detail.steps ?? []).find((step_) => step_.status === "waiting"); const options = waitingStep?.output?.options ?? []; if (JSON.stringify(options) !== JSON.stringify(DECIDE_OPTIONS)) { throw new Error(`run ${run.id} waiting options: ${JSON.stringify(options)}`); } if (waitingStep?.output?.prompt !== DECIDE_PROMPT) { throw new Error(`run ${run.id} waiting instructions: ${JSON.stringify(waitingStep?.output?.prompt)}`); } console.log(` run ${run.id} waiting on gate in ${run.workspace} (options ${options.join(", ")})`); } return prepared; } /** * One complete live decision: card, sheet, deliberate selection, one Confirm, * the resumed run and the recorded verdict. Feedback is optional; the same flow * proves both wire shapes. */ async function exerciseDecisionRun(run, { option, feedback, labels }) { const { id, title } = run; const expectedMeta = `gate · run ${id} · ${title}`; console.log(`• R11: ${labels.label} (run ${id})`); await ensureConnected(); await eventually( () => page.evaluate(`!!document.querySelector('.list.runs .run-row-meta[data-run-id="${id}"]')`), { timeout: 30000, what: `run ${id}'s waiting row to appear` }, ); await openRunDetail(id); await page.waitForSelector(".run-detail .decision-card", { timeout: 20000 }); const card = await page.evaluate(`(() => ({ title: document.querySelector(".decision-card .decision-card-title")?.textContent ?? "", meta: document.querySelector(".decision-card .decision-card-meta")?.textContent ?? "", action: document.querySelector(".decision-card .decision-open")?.textContent ?? "", }))()`); if (card.title !== "Waiting for your decision") throw new Error(`card title: ${card.title}`); if (card.meta !== expectedMeta) throw new Error(`card meta: ${card.meta}`); if (card.action !== "Decide") throw new Error(`card action: ${card.action}`); console.log(` card: ${card.title} — ${card.meta}`); shots.push(await page.screenshot(`${out}/${labels.card}`)); await page.click(".decision-card .decision-open"); await page.waitForSelector(".decision-sheet .decision-option", { timeout: 20000 }); const sheet = await page.evaluate(`(() => ({ title: document.querySelector(".decision-sheet-title")?.textContent ?? "", meta: document.querySelector(".decision-sheet-meta")?.textContent ?? "", instructions: document.querySelector(".decision-instructions")?.textContent ?? "", options: [...document.querySelectorAll(".decision-sheet .decision-option")].map((n) => ({ key: n.querySelector(".decision-option-key")?.textContent ?? "", target: n.querySelector(".decision-option-target")?.textContent ?? "", })), feedbackLabel: document.querySelector(".decision-field-label")?.textContent ?? "", confirm: document.querySelector(".decision-confirm")?.textContent ?? "", confirmDisabled: document.querySelector(".decision-confirm")?.disabled ?? null, }))()`); if (sheet.title !== "Decide gate") throw new Error(`sheet title: ${sheet.title}`); if (sheet.meta !== `run ${id} · ${title}`) throw new Error(`sheet meta: ${sheet.meta}`); if (sheet.instructions !== DECIDE_PROMPT) { throw new Error(`sheet instructions: ${JSON.stringify(sheet.instructions)}`); } const optionKeys = sheet.options.map((row) => row.key); if (JSON.stringify(optionKeys) !== JSON.stringify(DECIDE_OPTIONS)) { throw new Error(`sheet options: ${JSON.stringify(optionKeys)}`); } if (sheet.options.some((row) => row.target !== "\u2192 SUCCESS")) { throw new Error(`sheet targets: ${JSON.stringify(sheet.options)}`); } if (sheet.feedbackLabel !== "Feedback (optional)") { throw new Error(`sheet feedback label: ${sheet.feedbackLabel}`); } if (sheet.confirm !== "Confirm decision") throw new Error(`sheet confirm: ${sheet.confirm}`); if (sheet.confirmDisabled !== true) throw new Error("Confirm decision must start disabled"); console.log( ` sheet: ${sheet.title} — ${sheet.meta}; ${sheet.options .map((row) => `${row.key} ${row.target}`) .join(", ")}; ${sheet.feedbackLabel}; Confirm disabled`, ); shots.push(await page.screenshot(`${out}/${labels.sheet}`)); await page.click(`.decision-sheet .decision-option[data-option="${option}"]`); await eventually(() => page.evaluate(`document.querySelector(".decision-confirm")?.disabled === false`), { timeout: 5000, what: "Confirm to enable after selecting an option", }); const selected = await page.evaluate(`(() => ({ checked: document.querySelector('.decision-sheet .decision-option[data-option="${option}"]')?.getAttribute("aria-checked") ?? "", decideBodies: (window.__decideBodies ?? []).filter((body) => Number(body.run_id) === ${id}), }))()`); if (selected.checked !== "true") throw new Error(`option ${option} did not render selected`); if (selected.decideBodies.length !== 0) { throw new Error(`selecting an option sent decide_run: ${JSON.stringify(selected.decideBodies)}`); } const stillWaiting = await inspectRun(id); if (stillWaiting.status !== "waiting") { throw new Error(`run ${id} left waiting on selection: ${stillWaiting.status}`); } console.log(" selecting sent no decide_run; the run is still waiting"); if (feedback) await page.fill(".decision-sheet .decision-feedback", feedback); shots.push(await page.screenshot(`${out}/${labels.selected}`)); await page.click(".decision-sheet .decision-confirm"); await eventually( () => page.evaluate( `!document.querySelector(".decision-sheet") && !document.querySelector(".decision-card")`, ), { timeout: 20000, what: "the sheet and card to close after Confirm" }, ); const final = await waitForRun( id, (d) => d.status === "succeeded", "succeed after the decision", ); const bodies = await page.evaluate( `(window.__decideBodies ?? []).filter((body) => Number(body.run_id) === ${id})`, ); if (bodies.length !== 1) throw new Error(`expected exactly one decide_run, got ${JSON.stringify(bodies)}`); const body = bodies[0]; if (body.type !== "decide_run" || Number(body.run_id) !== id || body.stage !== "gate" || body.option !== option) { throw new Error(`decide_run body: ${JSON.stringify(body)}`); } if (feedback) { if (body.feedback !== feedback) throw new Error(`decide_run feedback: ${JSON.stringify(body.feedback)}`); } else if (Object.hasOwn(body, "feedback")) { throw new Error(`no-feedback decide_run carried feedback: ${JSON.stringify(body)}`); } const gate = (final.steps ?? [])[0] ?? {}; if (gate.outcome !== option || gate.output?.option !== option) { throw new Error(`recorded decision: ${JSON.stringify(gate.output)} outcome ${gate.outcome}`); } const recordedFeedback = String(gate.output?.feedback ?? "").trim(); if (recordedFeedback !== feedback) { throw new Error(`recorded feedback: ${JSON.stringify(gate.output?.feedback)}`); } const expectedVerdict = `${option} \u2192 success`; if (gate.verdict?.option !== expectedVerdict) { throw new Error(`recorded verdict option: ${JSON.stringify(gate.verdict?.option)}`); } if (String(gate.verdict?.reason ?? "") !== feedback) { throw new Error(`recorded verdict reason: ${JSON.stringify(gate.verdict?.reason)}`); } console.log( ` one decide_run ${JSON.stringify(body)}; run succeeded with ${expectedVerdict}` + (feedback ? ` and the exact reason` : " and no reason"), ); await eventually( () => page.evaluate( `["succeeded","Succeeded"].includes(document.querySelector(".run-detail .run-chip-row .chip")?.textContent ?? "")`, ), { timeout: 20000, what: `run ${id}'s detail to show succeeded` }, ); shots.push(await page.screenshot(`${out}/${labels.confirmed}`)); await clickDetailTab("Timeline"); await page.waitForSelector(`.detail .timeline-row[data-stage-key="gate"]`, { timeout: 20000 }); await page.click(`.detail .timeline-row[data-stage-key="gate"]`); await page.waitForSelector(".stage-pane .verdict", { timeout: 20000 }); const verdict = await page.evaluate(`(() => ({ title: document.querySelector(".stage-pane .verdict-title")?.textContent ?? "", option: document.querySelector(".stage-pane .verdict .outcome-chip")?.textContent ?? "", reason: document.querySelector(".stage-pane .verdict-reason")?.textContent ?? "", }))()`); if (verdict.title !== "Verdict") throw new Error(`verdict heading: ${verdict.title}`); if (verdict.option !== expectedVerdict) throw new Error(`verdict chip: ${verdict.option}`); if (verdict.reason !== feedback) throw new Error(`verdict reason: ${JSON.stringify(verdict.reason)}`); console.log(` verdict: ${verdict.option}${feedback ? ` — ${verdict.reason}` : " — no reason"}`); shots.push(await page.screenshot(`${out}/${labels.verdict}`)); await page.evaluate( `document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`, ); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the stage pane to close", }); } /** The focused R11 suite: two real waiting runs, both decided in the browser. */ async function runDecisionChecks() { const prepared = await prepareHumanDecisionRuns(); await ensureConnected(); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); await exerciseDecisionRun(prepared[0], { option: "approve", feedback: "Approved in the live browser decision sheet.", labels: { label: "decision with feedback", card: `41-run-${prepared[0].id}-decision-card.png`, sheet: `42-run-${prepared[0].id}-decision-sheet.png`, selected: `43-run-${prepared[0].id}-decision-selected.png`, confirmed: `44-run-${prepared[0].id}-decision-confirmed.png`, verdict: `45-run-${prepared[0].id}-decision-verdict.png`, }, }); await exerciseDecisionRun(prepared[1], { option: "hold", feedback: "", labels: { label: "decision without feedback", card: `46-run-${prepared[1].id}-decision-card.png`, sheet: `47-run-${prepared[1].id}-decision-sheet.png`, selected: `48-run-${prepared[1].id}-decision-selected.png`, confirmed: `49-run-${prepared[1].id}-decision-confirmed.png`, verdict: `50-run-${prepared[1].id}-decision-verdict.png`, }, }); console.log( ` pipeline ${JSON.stringify(decidePipelineName)} (${decidePipelineReused ? "reused" : "submitted create-only"}), runs ${decideRunIds.join(", ")} both succeeded`, ); } /** Expand the CLOSED chats group so a just-closed row is in the DOM to inspect. */ async function expandClosedChats() { await page.evaluate(`(() => { const toggle = [...document.querySelectorAll(".chat-group-toggle")].find((node) => node.textContent.trim().toLowerCase().startsWith("closed"), ); if (toggle && toggle.getAttribute("aria-expanded") !== "true") toggle.click(); return !!toggle; })()`); await new Promise((resolve) => setTimeout(resolve, 250)); } // Open one chat row's Chat actions menu (the row's management items live behind // it since C13: Rename… / Close chat… or Reopen / Hide on this device). async function openChatRowMenu(sessionId) { // A closed session lives in the collapsed CLOSED group; reveal it first. const present = await page.evaluate( `!!document.querySelector('#chatRows .chat-row[data-session="${sessionId}"]')`, ); if (!present) await expandClosedChats(); // The row can be re-rendered by a just-finished create/refresh right under the // click, which closes the menu again; retry once before failing. for (let attempt = 0; attempt < 2; attempt += 1) { // The actions cluster is revealed by hover or focus (it occupies no column); // focus the button first so a synthetic click cannot land on a hidden one. await page.evaluate( `document.querySelector('#chatRows .chat-row[data-session="${sessionId}"] .chat-menu-button')?.focus()`, ); await page.click(`#chatRows .chat-row[data-session="${sessionId}"] .chat-menu-button`); const opened = await eventually( () => page.evaluate( `!!document.querySelector('#chatRows .chat-row[data-session="${sessionId}"] .chat-menu-panel')`, ), { timeout: attempt === 0 ? 3000 : 10000, what: "the Chat actions menu" }, ).catch(() => false); if (opened) return; } throw new Error(`the Chat actions menu did not open for ${sessionId}`); } // --- P5: create a pipeline from a template (scratch server only) ----------- // The scratch server has no real runs and no live chat, so this is the whole // point of a `--scratch` run. It opens a template, opens the create action, // captures the blank/disabled dialog, fills a unique name, creates it, captures // the success dialog and then the Pipelines list holding the new row. It sends a // real validate + submit, but only ever against the scratch database. async function runScratchCreate() { step("P5: creating a pipeline from a template (scratch server)"); await ensureConnected(); await openTemplates(); await page.waitForSelector(".cards button.card", { timeout: 25000 }); const starterKeys = await page.evaluate(`Array.from(document.querySelectorAll('[data-template-key]'), node => node.dataset.templateKey)`); for (const key of ["task", "research", "summarize", "compare", "plan", "three-stage", "review", "debug"]) { if (!starterKeys.includes(key)) throw new Error(`Missing alpha starter ${key}`); } await shot("54-alpha-starter-gallery-desktop.png"); await page.resize(390, 844); await settle(); if (await page.evaluate("document.documentElement.scrollWidth > innerWidth")) throw new Error("Starter gallery overflows at 390px"); const clipped = await page.evaluate(`Array.from(document.querySelectorAll('[data-template-key]')).filter(card => card.querySelector('.card-meta')?.getBoundingClientRect().bottom > card.getBoundingClientRect().bottom).map(card => card.dataset.templateKey)`); if (clipped.length) throw new Error(`Starter metadata outside cards: ${clipped.join(", ")}`); await shot("55-alpha-starter-gallery-mobile.png"); await page.resize(1440, 900); await settle(); await page.click(".cards button.card"); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await page.waitForSelector(".template-create-open", { timeout: 15000 }); const actionLabel = await text(".template-create-open"); if (actionLabel !== "Create pipeline from this template") { throw new Error(`create action label: ${actionLabel}`); } await page.click(".template-create-open"); await page.waitForSelector(".template-create-dialog", { timeout: 15000 }); const blank = await page.evaluate(`(() => ({ title: document.querySelector(".template-create-title")?.textContent ?? "", label: document.querySelector(".template-create-label")?.textContent ?? "", note: document.querySelector(".template-create-note")?.textContent ?? "", confirm: document.querySelector(".template-create-confirm")?.textContent ?? "", dismiss: document.querySelector(".template-create-cancel")?.textContent ?? "", confirmDisabled: document.querySelector(".template-create-confirm")?.disabled ?? null, fieldDisabled: document.querySelector(".template-create-name")?.disabled ?? null, }))()`); if (!blank.title.startsWith("Create from ")) throw new Error(`create dialog title: ${blank.title}`); if (blank.label !== "New pipeline name (required)") throw new Error(`create field label: ${blank.label}`); if (blank.note !== "Creates a new draft pipeline on the server from this template's source; it is not run.") { throw new Error(`create note: ${blank.note}`); } if (blank.confirm !== "Create") throw new Error(`create confirm: ${blank.confirm}`); if (blank.dismiss !== "Cancel") throw new Error(`create dismiss: ${blank.dismiss}`); if (blank.confirmDisabled !== true) throw new Error("Create was enabled with a blank name"); if (blank.fieldDisabled !== false) throw new Error("the name field started disabled"); console.log(` blank dialog: "${blank.title}" — Create disabled, field enabled, Cancel offered`); await shot("51-template-create-dialog.png"); const newName = `Scratch create ${Date.now()}`; await page.fill(".template-create-name", newName); await eventually( () => page.evaluate(`!document.querySelector(".template-create-confirm")?.disabled`), { timeout: 5000, what: "Create to enable after a non-blank name" }, ); await page.click(".template-create-confirm"); await page.waitForText(`Created "${newName}". It is in the Pipelines list.`, { timeout: 25000 }); const success = await page.evaluate(`(() => ({ text: document.querySelector(".template-create-success")?.textContent ?? "", dismiss: document.querySelector(".template-create-cancel")?.textContent ?? "", fieldDisabled: document.querySelector(".template-create-name")?.disabled ?? null, }))()`); if (success.text !== `Created "${newName}". It is in the Pipelines list.`) { throw new Error(`create success line: ${success.text}`); } if (success.dismiss !== "Close") throw new Error(`dismiss after success: ${success.dismiss}`); if (success.fieldDisabled !== true) throw new Error("the name field was not locked after a success"); console.log(` created: ${JSON.stringify(success.text)}; dismiss now reads ${success.dismiss}`); await shot("52-template-created.png"); // Cancel now reads Close: dismissing it leaves the Templates view, then the // refreshed Pipelines list must hold the new row. await page.click(".template-create-cancel"); await eventually(() => page.evaluate(`!document.querySelector(".template-create-dialog")`), { timeout: 5000, what: "the create dialog to close", }); await clickDestination("Pipelines"); await page.waitForText(newName, { timeout: 25000 }); const listed = await page.evaluate( `document.querySelector("#pipelineRows")?.textContent.includes(${JSON.stringify(newName)}) ?? false`, ); if (!listed) throw new Error("the created pipeline is not in the Pipelines list"); console.log(" the created pipeline appears in the scratch server's Pipelines list"); await shot("53-pipeline-created-list.png"); } // --- Omni-search (S1/S2) --------------------------------------------------- // The page's own search UI drives every one of the helpers below; nothing is // stubbed. `searchTargets` opens the panel (if it is not already open), types the // query, applies the kind filter and returns the `data-target` of every row of // that kind once one has rendered. async function searchTargets(query, kind) { await ensureConnected(); if (!(await page.evaluate(`!!document.querySelector("#searchInput")`))) { await page.click("#searchToggle"); await page.waitForSelector("#searchInput", { timeout: 15000 }); } await page.fill("#searchInput", query); await page.evaluate(`(() => { const select = document.querySelector("#searchKind"); select.value = ${JSON.stringify(kind)}; select.dispatchEvent(new Event("change", { bubbles: true })); })()`); await eventually( () => page.evaluate( `[...document.querySelectorAll("#searchResults .search-row")].some((r) => r.dataset.kind === ${JSON.stringify(kind)})`, ), { timeout: 25000, what: `a ${kind} search result` }, ); return page.evaluate( `[...document.querySelectorAll("#searchResults .search-row")] .filter((r) => r.dataset.kind === ${JSON.stringify(kind)}) .map((r) => r.dataset.target ?? "")`, ); } /** * Click one search row by its exact data-target; routing closes the panel. The * lookup and the click happen in one evaluate so a reply landing between them * cannot replace the row first. This drives the row's own click handler, exactly * as a mouse click would. */ async function clickSearchTarget(target) { return page.evaluate(`(() => { const row = [...document.querySelectorAll("#searchResults .search-row")] .find((r) => (r.dataset.target ?? "") === ${JSON.stringify(target)}); if (!row) return false; row.click(); return true; })()`); } /** * Route candidate rows of `kind` until the expected destination renders (routing * always closes the panel, so each attempt reopens and re-queries). Live data can * change between runs, so up to `attempts` rows are tried before giving up; * returns the target that landed or null. */ async function routeSearchKind(query, kind, landed, attempts = 4) { const targets = await searchTargets(query, kind); for (const target of targets.slice(0, attempts)) { await searchTargets(query, kind); if (!(await clickSearchTarget(target))) continue; if (await landed()) return target; await page.evaluate(`document.querySelector(".stage-pane-back")?.click()`); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, }).catch(() => {}); } return null; } // The mounted graph's current transform, read back so the pan/zoom clamp checks // assert the real rendered state rather than a claim. const graphState = () => page.evaluate(`(() => { const content = document.querySelector(".graph-content"); const viewport = document.querySelector(".graph-viewport"); if (!content || !viewport) return null; const m = /translate\\(([-0-9.]+)px, ([-0-9.]+)px\\) scale\\(([-0-9.]+)\\)/.exec( content.style.transform, ); if (!m) return null; return { tx: Number(m[1]), ty: Number(m[2]), scale: Number(m[3]), width: parseFloat(content.style.width), height: parseFloat(content.style.height), viewportWidth: viewport.clientWidth, viewportHeight: viewport.clientHeight, }; })()`); // Dispatch real wheel events at the viewport centre (the page handler is // `{ passive: false }` and prevents default, so a synthetic event exercises the // same clamp path a user gesture does). const wheel = (deltaY, times) => page.evaluate(`(() => { const viewport = document.querySelector(".graph-viewport"); const r = viewport.getBoundingClientRect(); for (let i = 0; i < ${times}; i += 1) { viewport.dispatchEvent(new WheelEvent("wheel", { bubbles: true, cancelable: true, deltaY: ${deltaY}, clientX: r.left + r.width / 2, clientY: r.top + r.height / 2, })); } return true; })()`); // Dispatch a real pointer drag across the viewport. const drag = (dx, dy) => page.evaluate(`(() => { const viewport = document.querySelector(".graph-viewport"); const r = viewport.getBoundingClientRect(); const fire = (type, x, y) => viewport.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, pointerId: 7, button: 0, buttons: type === "pointerup" ? 0 : 1, clientX: x, clientY: y, })); const startX = r.left + r.width / 2; const startY = r.top + r.height / 2; fire("pointerdown", startX, startY); for (let i = 1; i <= 20; i += 1) { fire("pointermove", startX + (${dx} * i) / 20, startY + (${dy} * i) / 20); } fire("pointerup", startX + ${dx}, startY + ${dy}); return true; })()`); // L5: the notification Settings states and the synthetic transition assertions. // Extracted so `--notify` can run exactly this evidence without the unrelated // live-suite steps; the default full run calls it in its L1/L5 position. async function runNotificationChecks() { // L5: notifications. A real OS notification cannot be captured reliably in // headless Chrome, so the browser's Notification constructor is stubbed and // recorded before this document loads; the qualifying transitions are then // pushed through the page's own socket `onmessage`/`frame` path and matched // against the pure ledger's decisions. The Settings states are real renders. // The stub also pre-dismisses one synthetic run+kind so suppression is proven // through the same attention identity the page loads at boot. step("L5: notification settings and synthetic run transitions"); await page.addInitScript(`(() => { if (window.__notificationStubInstalled) return; window.__notificationStubInstalled = true; window.__notificationCalls = []; window.__notificationObjects = []; window.__notificationRequests = 0; window.__notificationPermission = "default"; function FakeNotification(title, options) { const call = { title: String(title), body: String(options && options.body != null ? options.body : ""), tag: String(options && options.tag != null ? options.tag : ""), }; window.__notificationCalls.push(call); window.__notificationObjects.push(this); this.title = call.title; this.body = call.body; this.tag = call.tag; this.onclick = null; this.close = function () {}; } Object.defineProperty(FakeNotification, "permission", { configurable: true, get: function () { return window.__notificationPermission; }, }); FakeNotification.requestPermission = function () { window.__notificationRequests += 1; window.__notificationPermission = "granted"; return Promise.resolve("granted"); }; Object.defineProperty(window, "Notification", { configurable: true, writable: true, value: FakeNotification, }); localStorage.setItem("pipelines.web.attention.v1", JSON.stringify(["990001:Failed"])); })()`); await page.evaluate(`location.reload()`); await page.waitForSelector("#shellStatus", { timeout: 15000 }); await ensureConnected(); await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 25000 }); // The watch is sent once per connected epoch, after the list has seeded the // ledger. Wait for that send (and its ack continuation) so the synthetic // frames below are observed as deltas rather than as part of the snapshot. await eventually( () => page.evaluate(`(window.__pipelinesSentTypes || []).includes("subscribe")`), { timeout: 15000, what: "the run-event subscribe frame" }, ); await page.evaluate(`new Promise((resolve) => setTimeout(resolve, 250))`); await clickDestination("Settings"); await page.waitForSelector("#settingsNotificationState", { timeout: 15000 }); // Default state: honest wording, one explicit control, and nothing asked on // load or on connect. const defaultNotificationState = await text("#settingsNotificationState"); if (defaultNotificationState !== NOTIFICATION_DEFAULT) { throw new Error(`default notification copy: ${defaultNotificationState}`); } const notificationsOnLoad = await page.evaluate(`window.__notificationRequests`); if (notificationsOnLoad !== 0) { throw new Error(`the page requested notification permission on load: ${notificationsOnLoad}`); } const enableControl = await page.evaluate( `document.querySelector("#settingsEnableNotifications")?.textContent ?? ""`, ); if (enableControl !== "Enable notifications") { throw new Error(`notification request control: ${enableControl}`); } await page.evaluate(`document.querySelector("#settingsNotifications")?.scrollIntoView({ block: "center" })`); await shot("L5-notifications-settings.png"); console.log(` notifications default: "${defaultNotificationState}" (0 permission requests on load)`); // The explicit control grants through the stub; the page must remember that it // asked and stop offering the control. await page.click("#settingsEnableNotifications"); await eventually( () => page.evaluate(`document.querySelector("#settingsNotificationState")?.textContent === ${JSON.stringify(NOTIFICATION_GRANTED)}`), { timeout: 10000, what: "the granted notification wording" }, ); const grantState = await page.evaluate(`({ requests: window.__notificationRequests, stored: localStorage.getItem(${JSON.stringify(NOTIFY_STORAGE_KEY)}), control: !!document.querySelector("#settingsEnableNotifications"), })`); if (grantState.requests !== 1 || grantState.control) { throw new Error(`grant state: ${JSON.stringify(grantState)}`); } if (grantState.stored !== JSON.stringify({ asked: true })) { throw new Error(`asked flag not stored: ${grantState.stored}`); } await page.evaluate(`document.querySelector("#settingsNotifications")?.scrollIntoView({ block: "center" })`); await shot("L5-notifications-granted.png"); console.log(` notifications granted after one explicit click; asked=${grantState.stored}`); // Drive the real frame path. The run id is a real run so the notification's // click can open it through the page's existing Runs navigation (L6 is todo). await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row-meta[data-run-id]", { timeout: 25000 }); const realRunId = await page.evaluate( `Number(document.querySelector(".list.runs .run-row-meta[data-run-id]")?.getAttribute("data-run-id"))`, ); if (!(realRunId > 0)) throw new Error(`no real run id for the notification click: ${realRunId}`); const runFrame = (runId, status, extra = {}) => ({ run_id: runId, status, name: "Smoke pipeline", title: "Smoke run", ...extra, }); const replayRunEvents = (frames) => page.evaluate(`(() => { const ws = window.__pipelinesSocket; if (!ws || typeof ws.onmessage !== "function") return false; for (const frame of ${JSON.stringify(frames)}) { ws.onmessage({ data: JSON.stringify({ type: "run_event", run_event: frame }) }); } return true; })()`); const notificationCount = () => page.evaluate(`window.__notificationCalls.length`); const notifications = () => page.evaluate(`window.__notificationCalls`); // queued -> failed raises exactly one; duplicate/terminal/unknown frames add none. if (!(await replayRunEvents([runFrame(realRunId, "queued")]))) { throw new Error("L5: the socket shim did not expose the live socket for run events"); } await replayRunEvents([runFrame(realRunId, "failed")]); if ((await notificationCount()) !== 1) { throw new Error(`queued->failed should notify once: ${JSON.stringify(await notifications())}`); } const failedAlert = (await notifications())[0]; const failedTag = `${realRunId}:Failed`; if ( failedAlert.title !== "A run failed" || failedAlert.body !== "Smoke run \u00b7 Smoke pipeline" || failedAlert.tag !== failedTag ) { throw new Error(`failed alert wording/tag: ${JSON.stringify(failedAlert)}`); } await replayRunEvents([runFrame(realRunId, "failed")]); if ((await notificationCount()) !== 1) throw new Error("a duplicate failed frame repeated the notification"); for (const status of ["succeeded", "canceled", "interrupted", "mystery"]) { await replayRunEvents([runFrame(realRunId, status)]); } if ((await notificationCount()) !== 1) { throw new Error(`a non-qualifying status raised a notification: ${JSON.stringify(await notifications())}`); } console.log(` queued -> failed raised one: ${JSON.stringify(failedAlert)}`); // running -> waiting raises exactly one decision alert; a duplicate adds none. await replayRunEvents([runFrame(realRunId, "running"), runFrame(realRunId, "waiting")]); if ((await notificationCount()) !== 2) { throw new Error(`running->waiting should notify once: ${JSON.stringify(await notifications())}`); } const waitingAlert = (await notifications())[1]; if ( waitingAlert.title !== "A run needs your decision" || waitingAlert.body !== "Smoke run \u00b7 Smoke pipeline" || waitingAlert.tag !== `${realRunId}:Decision` ) { throw new Error(`waiting alert wording/tag: ${JSON.stringify(waitingAlert)}`); } await replayRunEvents([runFrame(realRunId, "waiting")]); if ((await notificationCount()) !== 2) throw new Error("a duplicate waiting frame repeated the notification"); // First-seen failed/waiting (the subscribe/seed rule) and a dismissed key // raise nothing; the dismissed identity is the one loaded at boot. await replayRunEvents([ runFrame(990002, "failed"), runFrame(990003, "waiting"), runFrame(990001, "queued"), runFrame(990001, "failed"), ]); if ((await notificationCount()) !== 2) { throw new Error(`pre-existing/dismissed runs notified: ${JSON.stringify(await notifications())}`); } console.log(" running -> waiting raised one; duplicates, terminal/unknown statuses, first-seen and dismissed runs raised none"); // The click focuses the page and uses the existing Runs navigation to select // the run (L6's URL route is still todo; no route is invented). await page.evaluate(`window.__notificationObjects[0]?.onclick?.()`); await eventually( () => page.evaluate( `!!document.querySelector('.list.runs .run-row.selected .run-row-meta[data-run-id="${realRunId}"]')`, ), { timeout: 20000, what: "the notification click to open Runs with the run selected" }, ); console.log(` notification click opened Runs with run ${realRunId} selected`); // The denied state is rendered from the same pure wording and offers no control. await page.evaluate(`window.__notificationPermission = "denied"`); await clickDestination("Settings"); await eventually( () => page.evaluate(`document.querySelector("#settingsNotificationState")?.textContent === ${JSON.stringify(NOTIFICATION_DENIED)}`), { timeout: 10000, what: "the denied notification wording" }, ); const deniedControl = await page.evaluate(`!!document.querySelector("#settingsEnableNotifications")`); if (deniedControl) throw new Error("the denied state still offers the request control"); await page.evaluate(`document.querySelector("#settingsNotifications")?.scrollIntoView({ block: "center" })`); await shot("L5-notifications-denied.png"); console.log(` notifications denied: "${NOTIFICATION_DENIED}" (no request control)`); console.log( " NOTE: the notification frames above are synthetic and the browser Notification was recorded by a stub; no real OS notification was observed", ); } // --- R8: the markdown-first artifact viewer -------------------------------- // A deterministic synthetic run served by the page's own static origin. The run // is injected over the real get_run command (never a new protocol field): its // ordinary artifacts include the stage-turn names and a small media+markdown // list, so the gallery, the viewer, keyboard paging, the backdrop and the honest // failure path are exercised without depending on whatever history the operator // currently has (the live fixture may be wiped between runs). const VIEWER_FIXTURE_RUN_ID = 990001; function viewerFixtureRun() { const fixture = (name, contentType, url) => ({ name, content_type: contentType, size_bytes: 96, url, }); return { id: VIEWER_FIXTURE_RUN_ID, pipeline_id: 1, version: 1, name: "Viewer fixture", title: "Markdown viewer fixture", status: "succeeded", executing: false, outcome: "success", current_stage: "", content_hash: "viewerfixture", created_at: "2026-10-19T20:00:00Z", finished_at: "2026-10-19T20:01:00Z", completed_steps: 1, diagnostic: "", resource_wait: null, input: { prompt: "Render the stage turn." }, output: { result: "ready" }, graph: { stages: [ { key: "implementer", type: "model", edges: { success: "#success" }, prompt: "Render." }, ], }, steps: [ { sequence: 1, stage: "implementer", status: "completed", outcome: "success", target: "#success", diagnostic: "", session_id: "", agent_active: false, control: { visit: 1, max_visits: 1 }, started_at: "2026-10-19T20:00:00Z", finished_at: "2026-10-19T20:01:00Z", input: { markdown: "# Turn input\n\nPlan the next small change.\n\n```sh\ngo test ./...\n```\n", }, output: { markdown: "# Output\n\nThe change is ready for review.\n\n- viewer renders markdown\n- keyboard paging works\n", }, artifacts: [ fixture("input.md", "text/markdown; charset=utf-8", "/test/fixtures/artifacts/input.md"), fixture("shot.png", "image/png", "/test/fixtures/artifacts/shot.png"), fixture("memory.md", "text/plain; charset=utf-8", "/test/fixtures/artifacts/memory.md"), fixture("output.md", "text/markdown; charset=utf-8", "/test/fixtures/artifacts/output.md"), // Deliberately absent from the static origin: the viewer must show the // honest fetch failure, not an empty or invented document. fixture("missing.txt", "text/plain; charset=utf-8", "/test/fixtures/artifacts/missing.txt"), ], }, ], }; } /** The visible gallery tiles' stable metadata and document marking. */ async function viewerTiles(scope) { return page.evaluate(`[...document.querySelectorAll(${JSON.stringify(scope)} + " .artifact-tile")].map((tile) => ({ name: tile.getAttribute("data-artifact-name") ?? "", kind: tile.getAttribute("data-artifact-kind") ?? "", label: tile.querySelector(".artifact-kind")?.textContent ?? "", glyph: !!tile.querySelector(".artifact-doc-glyph"), }))`); } async function runMarkdownViewerChecks() { step("R8: the deterministic markdown/text stage-turn fixture run"); const fixture = viewerFixtureRun(); // This init script only answers get_run for the fixture id: every other frame // (the socket, the real list, the operator's own data) still goes to the server. await page.addInitScript(`(() => { window.__viewerFixtureRun = ${JSON.stringify(fixture)}; window.__pipelinesIntercept = (frame) => ( frame.type === "get_run" && frame.run_id === ${VIEWER_FIXTURE_RUN_ID} ? { id: frame.id, type: "run_detail", run_detail: window.__viewerFixtureRun } : null ); })()`); await openDeepLink(`/runs/${VIEWER_FIXTURE_RUN_ID}`); await page.waitForSelector("#detailTitle", { timeout: 20000 }); await eventually( () => page.evaluate( `document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${VIEWER_FIXTURE_RUN_ID} ") ?? false`, ), { timeout: 20000, what: "the markdown fixture run detail to load" }, ); step("R8: the galleries show the stage's artifacts, not the turn's system files"); await page.clickText(".run-detail .section-toggle", "Artifacts"); await page.waitForSelector('.run-detail .artifact-gallery .artifact-tile[data-artifact-name="shot.png"]', { timeout: 15000, }); const runTiles = await viewerTiles(".run-detail .artifact-gallery"); const runNames = runTiles.map((tile) => tile.name); for (const system of ["input.md", "memory.md", "output.md", "session.html"]) { if (runNames.includes(system)) throw new Error(`the run gallery still shows ${system}`); } const runImage = runTiles.find((tile) => tile.name === "shot.png"); if (!runImage || runImage.kind !== "Image" || runImage.glyph) { throw new Error(`shot.png is not presented as media: ${JSON.stringify(runImage)}`); } console.log(` run gallery: ${runNames.join(", ")} (no turn files)`); await shot("44-viewer-turn-documents.png"); step("R8: the stage audit carries the turn files; Input renders markdown"); await clickDetailTab("Timeline"); await page.click('.detail .timeline-row[data-stage-key="implementer"]'); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.clickText(".stage-pane .section-toggle", "Artifacts"); await page.waitForSelector('.stage-pane .artifact-tile[data-artifact-name="shot.png"]', { timeout: 15000 }); const visitTiles = await viewerTiles(".stage-pane"); for (const system of ["input.md", "memory.md", "output.md"]) { if (visitTiles.some((tile) => tile.name === system)) { throw new Error(`the visit gallery still shows ${system}: ${JSON.stringify(visitTiles)}`); } } await page.click(".stage-pane .audit-open"); await page.waitForSelector(".audit-viewer", { timeout: 15000 }); await page.click('.audit-tab[data-audit-tab="input"]'); await page.waitForSelector(".audit-body .field-markdown h3.md-heading", { timeout: 15000 }); const auditInput = await page.evaluate(`(() => { const body = document.querySelector(".audit-body"); return { heading: body?.querySelector(".field-markdown h3.md-heading")?.textContent ?? "", code: body?.querySelector(".field-markdown .md-code-block pre code")?.textContent ?? "", tabs: [...document.querySelectorAll(".audit-tab")].map((tab) => tab.textContent), }; })()`); if (auditInput.heading !== "Turn input" || !auditInput.code.includes("go test ./...")) { throw new Error(`the audit input did not render markdown: ${JSON.stringify(auditInput)}`); } if (auditInput.tabs.join("|") !== "Input|Output|Memory|Session|Definition") { throw new Error(`audit tabs: ${JSON.stringify(auditInput.tabs)}`); } await page.click('.audit-tab[data-audit-tab="memory"]'); await eventually( () => page.evaluate(`!!document.querySelector(".audit-body .field-markdown")`), { timeout: 15000, what: "the memory tab to render" }, ); const memoryText = await page.evaluate(`document.querySelector(".audit-body")?.textContent ?? ""`); if (!memoryText.includes("Memory")) { throw new Error(`the memory tab did not render memory.md: ${JSON.stringify(memoryText.slice(0, 120))}`); } const memoryFetched = await page.evaluate( `performance.getEntriesByType("resource").some((entry) => entry.name.endsWith("/test/fixtures/artifacts/memory.md"))`, ); if (!memoryFetched) throw new Error("the memory tab did not fetch memory.md"); console.log(` audit: ${auditInput.heading}; memory.md fetched and rendered`); await shot("45-viewer-markdown-rendered.png"); await page.click(".audit-close"); await eventually(() => page.evaluate(`!document.querySelector(".audit-viewer")`), { timeout: 5000, what: "the audit dialog to close", }); step("R8: the viewer pages the stage artifacts and fails honestly"); await page.click('.stage-pane .artifact-tile[data-artifact-name="shot.png"]'); await page.waitForSelector(".artifact-viewer .viewer-image", { timeout: 15000 }); await eventually( () => page.evaluate(`(() => { const image = document.querySelector(".artifact-viewer .viewer-image"); return !!image && image.complete && image.naturalWidth > 0; })()`), { timeout: 15000, what: "the first artifact image to load" }, ); const first = await page.evaluate(`(() => { const viewer = document.querySelector(".artifact-viewer"); return { title: viewer?.querySelector(".viewer-title")?.textContent ?? "", position: viewer?.querySelector(".viewer-position")?.textContent ?? "", prevDisabled: viewer?.querySelector('.viewer-page[data-viewer-focus="prev"]')?.disabled ?? null, nextDisabled: viewer?.querySelector('.viewer-page[data-viewer-focus="next"]')?.disabled ?? null, }; })()`); if (first.title !== "shot.png" || !first.position.startsWith("1 of 2") || first.prevDisabled !== true) { throw new Error(`the first artifact's paging state is wrong: ${JSON.stringify(first)}`); } await page.evaluate( `document.activeElement?.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true }))`, ); await page.waitForSelector(".artifact-viewer .viewer-error-message", { timeout: 15000 }); const failure = await page.evaluate(`(() => { const viewer = document.querySelector(".artifact-viewer"); const external = viewer?.querySelector(".viewer-external"); return { title: viewer?.querySelector(".viewer-title")?.textContent ?? "", position: viewer?.querySelector(".viewer-position")?.textContent ?? "", message: viewer?.querySelector(".viewer-error-message")?.textContent ?? "", externalText: external?.textContent ?? "", externalHref: external?.getAttribute("href") ?? "", nextDisabled: viewer?.querySelector('.viewer-page[data-viewer-focus="next"]')?.disabled ?? null, }; })()`); if (failure.title !== "missing.txt" || failure.message.trim() === "") { throw new Error(`the failed fetch did not show an honest message: ${JSON.stringify(failure)}`); } if ( failure.externalText !== "Open missing.txt externally" || !failure.externalHref.endsWith("/test/fixtures/artifacts/missing.txt") || failure.nextDisabled !== true ) { throw new Error(`the external fallback is wrong: ${JSON.stringify(failure)}`); } console.log(` failure: ${failure.message.trim()} · ${failure.externalText}`); await shot("46-viewer-markdown-next.png"); await page.evaluate( `document.activeElement?.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowLeft", bubbles: true, cancelable: true }))`, ); await eventually( () => page.evaluate(`document.querySelector(".artifact-viewer .viewer-title")?.textContent === "shot.png"`), { timeout: 10000, what: "ArrowLeft to return to the image" }, ); await page.evaluate(`document.querySelector(".artifact-viewer .viewer-title")?.click()`); if (!(await page.evaluate(`!!document.querySelector(".artifact-viewer")`))) { throw new Error("a click inside the viewer panel closed it"); } const closedByBackdrop = await page.evaluate(`(() => { const overlay = document.querySelector(".artifact-viewer"); if (!overlay) return false; const rect = overlay.getBoundingClientRect(); const target = document.elementFromPoint(rect.left + 3, rect.top + 3); if (!target) return false; target.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true })); return true; })()`); if (!closedByBackdrop) throw new Error("the backdrop had no point to click"); await eventually(() => page.evaluate(`!document.querySelector(".artifact-viewer")`), { timeout: 10000, what: "the backdrop click to close the viewer", }); console.log(" a panel click kept the viewer open; the dimmed backdrop closed it"); await shot("47-viewer-markdown-backdrop-closed.png"); // Drop the fixture intercept so later documents (the shell pass) talk only to // the real server; the init script is removed by navigating away from here. await page.evaluate(`(() => { window.__pipelinesIntercept = null; return true; })()`); } try { await mkdir(out, { recursive: true }); step(`serving web/ on 127.0.0.1:${port} (proxy /connect -> ${pipelines})`); serve = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", pipelines], { cwd: ROOT, env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" }, stdio: ["ignore", "pipe", "pipe"], }); let serveLog = ""; serve.stdout.on("data", (chunk) => (serveLog += chunk.toString())); serve.stderr.on("data", (chunk) => (serveLog += chunk.toString())); await eventually( async () => { if (serve.exitCode != null) throw new Error(`serve.mjs exited: ${serveLog.trim().slice(-300)}`); const res = await fetch(`http://127.0.0.1:${port}/`).catch(() => null); return res?.ok; }, { what: "the page to be served" }, ); step("launching headless Chrome"); browser = await launchChrome({ width: 1440, height: 900 }); page = await browser.openPage(`http://127.0.0.1:${port}/`); // C5 evidence needs to replay exact `chat_event` frames against the real page // (a real live turn is not available on demand without disrupting the operator's // server). A tiny WebSocket shim is installed before the page's own scripts on // every document from now on: it constructs the real socket and records it on // `window`, so the smoke can hand frames to the socket's own `onmessage` and the // app folds them through its real path. Every other frame (the subscribe ack, the // history reply) still comes from the real server. step("installing the WebSocket recorder shim before the page scripts"); await page.addInitScript(`(() => { const Native = window.WebSocket; if (!Native || window.__pipelinesSocketShim) return; window.__pipelinesSocketShim = true; window.__pipelinesSockets = []; // C10: the synthetic approval request ids whose outgoing chat_reply the smoke // records and suppresses, acking them locally so the page's own optimistic // answer path is exercised without ever sending a real decision to the server. window.__suppressChatReplyIds = []; // C10: request ids whose reply is answered with a server error instead of an // ack, so the card's refusal path (reopen with the reason) is exercised. window.__failChatReplyIds = []; window.__chatReplyCounts = { suppressed: 0, forwarded: 0, failed: 0 }; // R11: the exact decide_run bodies the page actually sends, retained (not // suppressed) so the smoke can prove a selection sends nothing and Confirm // sends exactly one body with the chosen option and optional feedback. window.__decideBodies = []; function Shim(url, protocols) { const ws = protocols === undefined ? new Native(url) : new Native(url, protocols); const nativeSend = ws.send.bind(ws); window.__pipelinesSockets.push(ws); window.__pipelinesSocket = ws; // R10: record the real run-event watch traffic the page sends, and the real // run_event frames the server pushes, so the smoke can assert the // unsubscribe/subscribe/list_runs sequence and a genuine pushed fold rather // than replaying a synthetic frame. The onmessage setter wraps the app's // own handler so every real frame is observed before it is folded. window.__runSubscribes = window.__runSubscribes || []; window.__runUnsubscribes = window.__runUnsubscribes || 0; window.__runEventFrames = window.__runEventFrames || []; const nativeOnMessage = Object.getOwnPropertyDescriptor(Native.prototype, "onmessage"); let handler = null; Object.defineProperty(ws, "onmessage", { configurable: true, get() { return handler; }, set(fn) { handler = fn; nativeOnMessage.set.call(ws, (event) => { try { const message = JSON.parse(event.data); if (message && message.type === "run_event" && message.run_event) { window.__runEventFrames.push(message.run_event); } } catch (_) { /* not a frame we observe */ } if (typeof fn === "function") fn.call(ws, event); }); }, }); // One send path for every scripted hook, in order: record the frame type for // assertions (C11/C10), suppress a synthetic chat_reply (C10, answering it // locally so no real decision reaches the server), then honour the generic // scripted-reply hook (C5/C10/C11), and otherwise send natively. ws.send = function (data) { let frame = null; try { frame = JSON.parse(data); } catch (_) { frame = null; } if (frame) { (window.__pipelinesSentTypes = window.__pipelinesSentTypes || []).push(frame.type); // R10: the exact subscribe/unsubscribe frames, so the smoke proves the // blank-workspace shape (and that no run_ids are sent). if (frame.type === "subscribe") window.__runSubscribes.push(frame); if (frame.type === "unsubscribe") window.__runUnsubscribes += 1; // C12: the prompt bodies this page actually sent, so the smoke can prove // the help command and an unknown command generate none, and that the // offer path sends exactly the draft it offered. if (frame.type === "chat_prompt") { (window.__chatPromptTexts = window.__chatPromptTexts || []).push( frame.chat_prompt?.text ?? "", ); } // R11: retain the real decision frames without altering them. if (frame.type === "decide_run") window.__decideBodies.push(frame); // C2: the create bodies this page actually sent, so the smoke can prove // the picker's chosen value reached chat_create. if (frame.type === "chat_create") { (window.__chatCreateBodies = window.__chatCreateBodies || []).push( frame.chat_create ?? null, ); } if (frame.type === "chat_reply") { const requestId = frame.chat_reply && frame.chat_reply.request_id; if (window.__suppressChatReplyIds.includes(requestId)) { window.__chatReplyCounts.suppressed += 1; const failed = window.__failChatReplyIds.includes(requestId); if (failed) window.__chatReplyCounts.failed += 1; if (typeof ws.onmessage === "function") { ws.onmessage({ data: JSON.stringify( failed ? { id: frame.id, type: "error", error: "This approval request is no longer available." } : { id: frame.id, type: "chat_reply" }, ), }); } return; } window.__chatReplyCounts.forwarded += 1; } const reply = window.__pipelinesIntercept ? window.__pipelinesIntercept(frame) : null; if (reply) { const delay = (window.__pipelinesInterceptDelay ?? 0) || 0; setTimeout(() => { try { if (typeof ws.onmessage === "function") ws.onmessage({ data: JSON.stringify(reply) }); } catch (_) {} }, delay); return; } } return nativeSend(data); }; return ws; } Shim.prototype = Native.prototype; for (const key of ["CONNECTING", "OPEN", "CLOSING", "CLOSED"]) Shim[key] = Native[key]; window.WebSocket = Shim; })()`); // Deterministic dismissals: the attention set is read from localStorage once at // boot, so clear the key and reload before connecting so the surface derives // from an empty set regardless of any stale profile state. step("clearing the attention storage key before connecting"); await page.evaluate(`localStorage.removeItem("pipelines.web.attention.v1")`); await page.evaluate(`location.reload()`); await page.waitForSelector("#shellStatus", { timeout: 15000 }); step("connecting the page to the server through its Settings connection section"); await connectFromSettings(); await clickDestination("Pipelines"); // A scratch server starts with no pipelines at all, so the pipeline row that // proves the live list rendered is skipped there; the connection banner is // still awaited either way. if (!scratch) await page.waitForSelector(".row-title", { timeout: 25000 }); await page.waitForText("Connected", { timeout: 25000 }); // Scratch mode runs only the P5 create flow above and skips the live suite, // which needs the operator's real pipelines, runs and chat session. The guard // below wraps the live steps without reindenting them, keeping this diff // reviewable; both branches share this file's cleanup in the `finally`. if (scratch) { await runScratchCreate(); console.log("\nUI SMOKE PASS"); for (const shot of shots) console.log(` ${shot}`); } if (decideOnly) { await runDecisionChecks(); console.log("\nUI SMOKE PASS"); for (const shot of shots) console.log(` ${shot}`); } else if (notifyOnly) { await runNotificationChecks(); console.log("\nUI SMOKE PASS"); for (const shot of shots) console.log(` ${shot}`); } else if (viewerOnly) { await runMarkdownViewerChecks(); console.log("\nUI SMOKE PASS"); for (const shot of shots) console.log(` ${shot}`); } else if (!scratch) { const pipelineCount = await page.evaluate( `document.querySelectorAll("#pipelineRows button.row").length`, ); if (!pipelineCount) throw new Error("connected but no pipelines rendered"); console.log(` pipelines rendered: ${pipelineCount}`); // 1. The list with its filter menu open: the name field, the Runs section, the // Last run status multi-select and the Clear filters reset (P1 + P2). step("capturing the pipeline list with the filter menu open"); await page.click("#filterToggle"); await page.waitForSelector(".filter-panel"); await shot("01-pipelines-controls.png"); // 2. The sort menu, and exercising it changes the ordering (P2). step("capturing and exercising the sort menu"); await page.click("#sortToggle"); await page.waitForSelector(".sort-panel"); await shot("02-pipelines-sort.png"); await page.clickText(".sort-panel .sort-option", "Name A\u2013Z"); const sorted = await text("#sortToggle"); if (!sorted.includes("Name A\u2013Z")) throw new Error(`sort did not apply: ${sorted}`); await page.click("#sortToggle"); await page.waitForSelector(".sort-panel"); await page.clickText(".sort-panel button", "Reset"); const reset = await text("#sortToggle"); if (!reset.includes("Server order")) throw new Error(`sort reset failed: ${reset}`); // 3. The name facet narrows live; an unmatched name shows the name-only empty state. step("narrowing by name and capturing the name-only empty state"); await page.click("#filterToggle"); await page.waitForSelector(".filter-panel"); const filterName = await page.evaluate( `document.querySelector("#pipelineRows .row-title")?.textContent?.trim() ?? ""`, ); await page.fill("#pipelineNameFilter", filterName); const narrowed = await page.evaluate( `document.querySelectorAll("#pipelineRows button.row").length`, ); if (!narrowed || narrowed >= pipelineCount) { throw new Error(`name filter did not narrow (${narrowed} of ${pipelineCount})`); } console.log(` name filter narrowed ${pipelineCount} -> ${narrowed}`); await page.fill("#pipelineNameFilter", "zzz-no-such-pipeline"); await page.waitForText('No pipelines match "zzz-no-such-pipeline".'); await shot("03-pipelines-empty-name.png"); await page.click(".empty button"); await page.waitForSelector("#pipelineRows button.row"); // 4. A run facet with no matches shows the general empty state and a reset. step("capturing the run-facet empty state"); await page.click("#status-interrupted"); await page.waitForText("No pipelines match the current filters."); await shot("04-pipelines-empty-filters.png"); await page.click(".empty button"); await page.waitForSelector("#pipelineRows button.row"); await page.click("#filterToggle"); // 5. Pipeline detail: Summary (name, subtitle, content hash, created/updated). // Prefer a multi-stage pipeline so the Graph tab below has a real layout to // render; fall back to whatever the server lists first. step("opening a multi-stage pipeline detail (Summary)"); const openedPipeline = await page.evaluate(`(() => { const rows = [...document.querySelectorAll("#pipelineRows button.row")]; const wanted = rows.find((row) => /\b[4-9] stages\b/.test(row.textContent)) ?? rows[0]; if (!wanted) return null; wanted.setAttribute("data-test-click", "1"); return wanted.querySelector(".row-title")?.textContent?.trim() ?? "first row"; })()`); if (!openedPipeline) throw new Error("no pipeline row to open"); console.log(` opened pipeline: ${openedPipeline}`); await page.click("[data-test-click='1']"); await page.evaluate( `document.querySelector("[data-test-click='1']")?.removeAttribute("data-test-click")`, ); await page.waitForSelector("#detailTitle"); await page.waitForSelector(".monospace"); await page.evaluate(`document.querySelector(".detail")?.scrollIntoView({ block: "start" })`); await shot("05-pipeline-summary.png"); // 6. The Versions section is collapsed by default; expanding shows its rows. step("expanding the Versions section"); await page.click(".section-toggle"); await page.waitForSelector(".versions-list"); await page.evaluate(`document.querySelector(".versions-list")?.scrollIntoView({ block: "start" })`); await shot("06-pipeline-summary-versions.png"); // 7. Source tab: the rendered pipeline source in a monospace block. step("opening the Source tab"); await clickDetailTab("Source"); await page.waitForSelector(".source-code"); await page.evaluate(`document.querySelector(".detail")?.scrollIntoView({ block: "start" })`); await shot("07-pipeline-source.png"); // 8. Graph tab (P6): the rendered workflow graph — stage nodes in declaration // order, labelled outcome edges, the success and // failure terminals — then an opened definition pane, the zoom/pan clamps and // the expanded panel. Every assertion below is a real behaviour of the page. step("opening the Graph tab: the rendered workflow graph (P6)"); await clickDetailTab("Graph"); await page.waitForSelector(".graph-note"); await page.waitForSelector(".graph-viewport"); await page.waitForSelector(".graph-node"); // Auto-fit runs on the first layout (after the host is attached), so wait for // the whole graph to sit inside the viewport before asserting. await eventually( async () => { const current = await graphState(); if (!current || !(current.scale > 0)) return false; const scaledWidth = current.width * current.scale; const scaledHeight = current.height * current.scale; return ( current.tx >= -0.5 && current.ty >= -0.5 && current.tx + scaledWidth <= current.viewportWidth + 0.5 && current.ty + scaledHeight <= current.viewportHeight + 0.5 ); }, { what: "the graph to be auto-fitted into the viewport" }, ); const note = await text(".graph-note"); if (!note.includes("Mouse wheel to zoom, drag to pan, click a stage for details.")) { throw new Error(`unexpected graph guidance note: ${note}`); } const graphCounts = await page.evaluate(`(() => ({ nodes: document.querySelectorAll(".graph-node").length, stages: document.querySelectorAll(".graph-node:not([class*='terminal'])").length, edges: document.querySelectorAll(".graph-edge-path").length, labels: [...document.querySelectorAll(".graph-edge-label")].map((n) => n.textContent), failure: !!document.querySelector(".graph-node-terminal-failure"), success: !!document.querySelector(".graph-node-terminal-success"), }))()`); if (!graphCounts.stages || !graphCounts.edges || !graphCounts.labels.length) { throw new Error(`the graph did not render nodes/edges/labels: ${JSON.stringify(graphCounts)}`); } if (!graphCounts.failure || !graphCounts.success) { throw new Error(`the graph is missing a terminal: ${JSON.stringify(graphCounts)}`); } const fitted = await graphState(); if (!(fitted && fitted.scale > 0 && fitted.scale <= 1 + 1e-6)) { throw new Error(`the graph was not auto-fitted: ${JSON.stringify(fitted)}`); } console.log( ` graph: ${graphCounts.stages} stages, ${graphCounts.edges} edges, ` + `${graphCounts.nodes} nodes, labels ${graphCounts.labels.join(",")}; fitted at ${fitted.scale.toFixed(2)}`, ); await shot("08-pipeline-graph.png"); // Clicking empty space must do nothing; clicking a stage opens its pane. step("checking empty-space click and opening a stage's definition pane"); await page.evaluate(`document.querySelector(".graph-viewport")?.click()`); const paneOnEmpty = await page.evaluate( `(() => { const p = document.querySelector(".graph-pane"); return !!p && !p.hidden; })()`, ); if (paneOnEmpty) throw new Error("clicking empty space opened the stage pane"); await page.click(".graph-node-model"); await page.waitForSelector(".graph-pane:not([hidden])"); const pane = await page.evaluate(`(() => { const p = document.querySelector(".graph-pane"); return { text: p.textContent, key: document.querySelector(".graph-node-model")?.dataset.key ?? "", type: document.querySelector(".graph-type-chip")?.textContent ?? "", fields: document.querySelectorAll(".graph-pane .graph-field").length, }; })()`); if (!pane.key || !pane.text.includes(pane.key)) { throw new Error(`the pane did not name the stage key (${pane.key}): ${pane.text.slice(0, 120)}`); } if (!pane.type) throw new Error("the pane did not show the stage type"); if (!pane.text.includes("Stage definition") || !pane.fields) { throw new Error(`the pane has no Stage definition fields: ${pane.text.slice(0, 120)}`); } await page.rect(".graph-pane"); await shot("08a-pipeline-graph-stage-pane.png"); console.log(` stage pane: ${pane.key} (${pane.type}), ${pane.fields} definition field(s)`); // Wheel zoom clamps to [0.3, 3.0]; a drag can never leave an empty viewport. step("exercising wheel-zoom clamps, drag clamping and Fit"); await wheel(400, 60); const zoomedOut = await graphState(); if (!(Math.abs(zoomedOut.scale - 0.3) < 1e-6)) { throw new Error(`zoom-out was not clamped to 0.3: ${zoomedOut.scale}`); } await wheel(-400, 60); const zoomedIn = await graphState(); if (!(Math.abs(zoomedIn.scale - 3.0) < 1e-6)) { throw new Error(`zoom-in was not clamped to 3.0: ${zoomedIn.scale}`); } await drag(4000, 3000); const panned = await graphState(); const visibleX = Math.min(panned.tx + panned.width * panned.scale, panned.viewportWidth) - Math.max(panned.tx, 0); const visibleY = Math.min(panned.ty + panned.height * panned.scale, panned.viewportHeight) - Math.max(panned.ty, 0); if (visibleX < 39.5 || visibleY < 39.5) { throw new Error(`panning left an empty viewport: visible ${visibleX}x${visibleY}`); } await page.clickText(".graph-control", "Fit graph to view"); const refitted = await graphState(); if (!(refitted.scale >= 0.3 && refitted.scale <= 1 + 1e-6)) { throw new Error(`Fit did not re-fit the graph: ${refitted.scale}`); } console.log( ` zoom clamp 0.3..3.0 ok; after a 4000x3000 drag the viewport still shows ` + `${Math.round(visibleX)}x${Math.round(visibleY)}px; Fit re-fitted to ${refitted.scale.toFixed(2)}`, ); step("toggling the graph panel's expansion (expanded by default)"); const startsExpanded = await page.evaluate( `document.querySelector(".graph-panel")?.classList.contains("expanded") === true`, ); if (!startsExpanded) { await page.clickText(".graph-control", "Expand graph"); await page.waitForText("Collapse graph"); } const expandedHeight = await page.evaluate( `document.querySelector(".graph-viewport").clientHeight`, ); await page.rect(".graph-viewport"); await shot("08b-pipeline-graph-expanded.png"); console.log( ` the graph starts expanded (${startsExpanded}); expanded viewport height: ${expandedHeight}px`, ); await page.clickText(".graph-control", "Collapse graph"); await page.waitForText("Expand graph"); step("checking a node with no stage shows the app's empty pane message"); const noDetails = await page.evaluate(`(async () => { const { mount } = await import("/src/graph.js"); const host = document.createElement("div"); host.style.cssText = "position:fixed;left:-2400px;top:0;width:600px;height:300px"; document.body.append(host); const graph = { stages: [{ key: "alpha", type: "model", edges: { success: "#success" } }] }; const handle = mount(host, graph, {}); host.querySelector(".graph-node-terminal-failure").click(); const text = host.querySelector(".graph-pane").textContent; handle.unmount(); const emptyAfterUnmount = host.childElementCount === 0; host.remove(); return { text, emptyAfterUnmount }; })()`); if (!noDetails.text.includes("No details recorded for this node.")) { throw new Error(`a stage-less node did not show the empty message: ${noDetails.text}`); } if (!noDetails.emptyAfterUnmount) throw new Error("unmount left the graph DOM behind"); console.log(" a node with no stage reads: No details recorded for this node."); step("switching to Runs: the day-grouped list"); await ensureConnected(); await clickDestination("Runs"); await eventually( () => page.evaluate( `!!document.querySelector(".list.runs .day-header") || !!document.querySelector(".list.runs .empty")`, ), { timeout: 25000, what: "the runs list" }, ); let fullDateHeader = ""; const dayHeaders = await page.evaluate( `[...document.querySelectorAll(".list.runs .day-header")].map((n) => n.textContent)`, ); if (!dayHeaders.length) { console.log(" -- no runs recorded; day grouping and the empty state are unit-tested"); } else { if (!dayHeaders.includes("Today")) { // The operator's history can legitimately have nothing from today (for // example a fresh day after a reset); the grouping rule is unit-tested. console.log(` -- no Today group in this history; newest group: ${dayHeaders[0]}`); } fullDateHeader = dayHeaders.find((label) => /^\d{1,2} \w{3} \d{4}$/.test(label)) ?? ""; if (!fullDateHeader) { // A wiped or brand-new history has only today's runs: the grouping rule is // unit-tested, so this capture is opportunistic. console.log(` -- no older day group in this history: ${dayHeaders.join(" | ")}`); } } shots.push(await page.screenshot(`${out}/09-runs-day-groups.png`)); console.log(` day headers: ${dayHeaders.slice(0, 3).join(" | ")}`); // --------------------------------------------------------------------- // R15: the attention / "Needs you" surface. The bell lives in the Runs list // heading; its badge, the sheet's rows and the dismissal identities all // come from the pure src/attention.js model. The live server currently has // failed runs and no run parked in `waiting`, so the sheet is exercised with // real failed entries and that live-data limitation is reported below. The // decision path (open without dismissing) is unit-tested. // --------------------------------------------------------------------- step("checking the attention bell + badge"); await page.waitForSelector("#attentionBell", { timeout: 15000 }); const bell = await page.evaluate(`(() => { const b = document.querySelector("#attentionBell"); const badge = b.querySelector(".attention-badge"); return { label: b.getAttribute("aria-label"), hasBadge: !!badge, count: badge ? badge.textContent : null, dataCount: badge ? badge.dataset.count : null, }; })()`); const attentionCount = Number(bell.dataCount ?? 0); const expectedLabel = attentionCount > 0 ? `Notifications, ${attentionCount} needing attention` : "Notifications"; if (bell.label !== expectedLabel) { throw new Error(`the bell's accessible name is wrong: ${bell.label}`); } if (attentionCount > 0 && (!bell.hasBadge || bell.count !== String(attentionCount))) { throw new Error(`the badge is wrong: ${JSON.stringify(bell)}`); } if (attentionCount === 0 && bell.hasBadge) { throw new Error(`a zero-count bell still has a badge: ${JSON.stringify(bell)}`); } if (await page.evaluate(`!!document.querySelector(".attention-sheet")`)) { throw new Error("the sheet was already open before the bell was clicked"); } console.log( attentionCount > 0 ? ` bell: aria-label="${bell.label}", badge=${bell.count}` : " bell: no current attention entries; asserting the zero state", ); shots.push(await page.screenshot(`${out}/35-attention-bell.png`)); const countProbe = await attentionBellProbe(); assertAttentionBellGlyph(countProbe, attentionCount, "wide bell"); if (attentionCount > 0) { step("checking the bell's monochrome SVG glyph and the badge's layout isolation"); const wideShift = assertAttentionBellBadgeShift( await attentionBellBadgeShift(), "wide bell with a count", ); console.log( ` glyph: <${countProbe.tagName} viewBox="${countProbe.viewBox}"> with ${countProbe.pathCount} ` + `unfilled currentColor paths at ${countProbe.width}×${countProbe.height}; ` + `badge "${countProbe.badge.text}" (${countProbe.badge.position})`, ); console.log( ` no-shift with the badge hidden: bell ${wideShift.bellDelta}px, ` + `Runs title ${wideShift.titleDelta}px`, ); step("checking the bell at 420x900"); await page.resize(420, 900); await eventually(() => page.evaluate(`matchMedia("(min-width: 600px)").matches === false`), { what: "the narrow layout at 420px", }); await settle(200); const narrowProbe = await attentionBellProbe(); assertAttentionBellGlyph(narrowProbe, attentionCount, "narrow bell with a count"); assertAttentionBellBadgeShift(await attentionBellBadgeShift(), "narrow bell with a count"); shots.push(await page.screenshot(`${out}/35a-attention-bell-narrow-420x900.png`)); await page.resize(1440, 900); await eventually(() => page.evaluate(`matchMedia("(min-width: 600px)").matches === true`), { what: "the wide layout back at 1440px", }); await settle(200); step("opening the Needs you sheet"); await page.click("#attentionBell"); await page.waitForSelector(".attention-sheet", { timeout: 15000 }); const sheet = await page.evaluate(`(() => ({ title: document.querySelector(".attention-sheet-title")?.textContent ?? "", clearAll: document.querySelector(".attention-clear")?.textContent ?? null, empty: document.querySelector(".attention-empty")?.textContent ?? null, focused: document.activeElement?.className ?? "", rows: [...document.querySelectorAll(".attention-sheet .attention-row")].map((row) => ({ runId: Number(row.querySelector(".attention-row-open")?.dataset.runId), kind: row.querySelector(".attention-row-open")?.dataset.kind ?? "", label: row.querySelector(".attention-label")?.textContent ?? "", desc: row.querySelector(".attention-desc")?.textContent ?? "", dismissName: row.querySelector(".attention-dismiss")?.getAttribute("aria-label") ?? "", })), }))()`); if (sheet.title !== "Needs you") throw new Error(`the sheet title is wrong: ${sheet.title}`); if (sheet.empty !== null) throw new Error(`a non-empty sheet showed the empty copy: ${sheet.empty}`); if (sheet.clearAll !== "Clear all") throw new Error(`Clear all is missing: ${sheet.clearAll}`); if (sheet.rows.length !== attentionCount) { throw new Error(`the sheet drew ${sheet.rows.length} rows for a badge of ${attentionCount}`); } if (!sheet.focused.includes("attention-sheet-close")) { throw new Error(`focus did not move into the sheet on open: ${sheet.focused}`); } for (const row of sheet.rows) { if (!row.label || !row.desc.includes(" · ")) { throw new Error(`row ${row.runId} is missing its label or description: ${JSON.stringify(row)}`); } if (!row.dismissName.startsWith("Dismiss ")) { throw new Error(`row ${row.runId}'s trailing control is not named Dismiss: ${row.dismissName}`); } } console.log(` sheet: ${sheet.rows.length} row(s); first ${sheet.rows[0].runId} (${sheet.rows[0].kind})`); shots.push(await page.screenshot(`${out}/36-attention-sheet.png`)); step("dismissing one entry individually"); await page.evaluate(`document.querySelector(".attention-sheet .attention-row .attention-dismiss").click()`); await eventually( async () => (await page.evaluate(`document.querySelectorAll(".attention-sheet .attention-row").length`)) === attentionCount - 1, { what: "the individual dismissal to land" }, ); const afterDismiss = await page.evaluate(`(() => ({ rows: document.querySelectorAll(".attention-sheet .attention-row").length, badge: document.querySelector("#attentionBell .attention-badge")?.dataset.count ?? null, stored: localStorage.getItem("pipelines.web.attention.v1"), }))()`); if (afterDismiss.rows !== attentionCount - 1 || Number(afterDismiss.badge) !== attentionCount - 1) { throw new Error(`the dismissal did not update the sheet/badge: ${JSON.stringify(afterDismiss)}`); } console.log(` dismissed one; rows ${attentionCount} -> ${afterDismiss.rows}`); shots.push(await page.screenshot(`${out}/37-attention-dismissed.png`)); step("reopening the sheet and clearing all"); if (await page.evaluate(`!!document.querySelector(".attention-sheet")`)) { await page.evaluate(`document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`); await eventually(async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)), { what: "the attention sheet to close before reopening", }); } await page.click("#attentionBell"); await page.waitForSelector(".attention-sheet", { timeout: 15000 }); const beforeClear = await page.evaluate(`document.querySelectorAll(".attention-sheet .attention-row").length`); if (beforeClear > 0) { await page.evaluate(`document.querySelector(".attention-sheet .attention-clear").click()`); await eventually( async () => (await page.evaluate(`document.querySelector(".attention-empty")?.textContent ?? ""`)) === "Nothing needs you right now.", { what: "the empty attention state after Clear all" }, ); if (await page.evaluate(`!!document.querySelector("#attentionBell .attention-badge")`)) { throw new Error("the badge is still visible after Clear all"); } } console.log(" Clear all left the empty attention state and no badge"); shots.push(await page.screenshot(`${out}/38-attention-cleared.png`)); step("closing the sheet with Escape returns focus to the bell"); await page.evaluate(`document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`); await eventually(async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)), { what: "Escape to close the attention sheet", }); if ((await page.evaluate(`document.activeElement?.id ?? ""`)) !== "attentionBell") { throw new Error("Escape did not return focus to the bell"); } } else { step("opening an empty Needs you sheet"); await page.click("#attentionBell"); await page.waitForSelector(".attention-sheet", { timeout: 15000 }); const empty = await page.evaluate(`(() => ({ title: document.querySelector(".attention-sheet-title")?.textContent ?? "", empty: document.querySelector(".attention-empty")?.textContent ?? "", rows: document.querySelectorAll(".attention-sheet .attention-row").length, clear: !!document.querySelector(".attention-clear"), focused: document.activeElement?.className ?? "", }))()`); if (empty.title !== "Needs you") throw new Error(`the sheet title is wrong: ${empty.title}`); if (empty.empty !== "Nothing needs you right now.") throw new Error(`the empty copy is wrong: ${empty.empty}`); if (empty.rows !== 0 || empty.clear) throw new Error(`an empty sheet drew rows or Clear all: ${JSON.stringify(empty)}`); if (!empty.focused.includes("attention-sheet-close")) throw new Error("focus did not move into the empty sheet"); await page.evaluate(`document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`); await eventually(async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)), { what: "Escape to close the empty attention sheet", }); console.log(" empty sheet: Nothing needs you right now.; no rows, no Clear all"); } step("capturing the zero-state bell at 1440x900 and 420x900"); await returnToRunsList(); await page.evaluate(`document.activeElement?.blur?.()`); await settle(150); const zeroProbe = await attentionBellProbe(); assertAttentionBellGlyph(zeroProbe, 0, "wide bell at zero"); assertSameAttentionGlyph(zeroProbe, countProbe, "wide bell at zero"); shots.push(await page.screenshot(`${out}/38a-attention-bell-zero-1440x900.png`)); console.log(` zero: no badge, aria-label="${zeroProbe.label}", ${zeroProbe.pathCount} unchanged paths`); await page.resize(420, 900); await eventually(() => page.evaluate(`matchMedia("(min-width: 600px)").matches === false`), { what: "the narrow layout at 420px", }); await settle(200); const zeroNarrowProbe = await attentionBellProbe(); assertAttentionBellGlyph(zeroNarrowProbe, 0, "narrow bell at zero"); assertSameAttentionGlyph(zeroNarrowProbe, countProbe, "narrow bell at zero"); shots.push(await page.screenshot(`${out}/38b-attention-bell-zero-420x900.png`)); console.log(` zero narrow: no badge, aria-label="${zeroNarrowProbe.label}"`); await page.resize(1440, 900); await eventually(() => page.evaluate(`matchMedia("(min-width: 600px)").matches === true`), { what: "the wide layout back at 1440px", }); await settle(200); step(`scrolling the list to the older day group (${fullDateHeader})`); const scrolledTo = await page.evaluate(`(() => { const scroll = document.querySelector(".run-scroll"); const header = [...document.querySelectorAll(".list.runs .day-header")] .find((n) => /^\\d{1,2} \\w{3} \\d{4}$/.test(n.textContent)); if (!scroll || !header) return null; scroll.scrollTop += header.getBoundingClientRect().top - scroll.getBoundingClientRect().top; return header.textContent; })()`); if (!scrolledTo) { console.log(" -- no older day group to scroll to in this history; skipped the capture"); } else { shots.push(await page.screenshot(`${out}/10-runs-older-day-group.png`)); console.log(` scrolled to: ${scrolledTo}`); } step("opening the filter menu: it must stay in the viewport and be reachable"); await page.click("#runFilterBtn"); await page.waitForSelector("#runFilterMenu"); const menuLabels = await page.evaluate( `[...document.querySelectorAll("#runFilterMenu .filter-check span")].map((n) => n.textContent)`, ); for (const label of [ "queued", "running", "waiting", "succeeded", "failed", "canceled", "interrupted", "Any pipeline", "Any time", "Today", "Last 7 days", "Last 30 days", ]) { if (!menuLabels.includes(label)) throw new Error(`filter menu missing: ${label}`); } const menuActions = await page.evaluate( `[...document.querySelectorAll("#runFilterMenu .menu-actions button")].map((n) => n.textContent)`, ); for (const label of ["Clear filters", "Done"]) { if (!menuActions.includes(label)) throw new Error(`filter menu action missing: ${label}`); } // The gate is reachability, not DOM presence: with a busy history the menu is // taller than the viewport, so it must be bounded and scroll its own box rather // than being clipped by an ancestor with no way for a user to reach the actions. const reach = await page.evaluate(`(() => { const menu = document.querySelector("#runFilterMenu"); const overflowY = getComputedStyle(menu).overflowY; const rect = menu.getBoundingClientRect(); menu.scrollTop = menu.scrollHeight; const actions = menu.querySelector(".menu-actions").getBoundingClientRect(); return { overflowY, height: Math.round(rect.height), viewport: window.innerHeight, fitsViewport: rect.height <= window.innerHeight && rect.bottom <= window.innerHeight + 1, actionsReachable: actions.bottom <= window.innerHeight + 1 && actions.top >= -1, }; })()`); if (reach.overflowY !== "auto" && reach.overflowY !== "scroll") { throw new Error(`the filter menu does not scroll its own box: overflow-y=${reach.overflowY}`); } if (!reach.fitsViewport) { throw new Error(`the filter menu overflows the viewport: ${JSON.stringify(reach)}`); } if (!reach.actionsReachable) { throw new Error(`the filter menu actions are unreachable: ${JSON.stringify(reach)}`); } shots.push(await page.screenshot(`${out}/11-runs-filter-menu.png`)); console.log(` filter menu ${reach.height}px tall in a ${reach.viewport}px viewport; actions reachable`); step("applying a status filter"); await page.evaluate( `document.querySelector('#runFilterMenu input[data-status="failed"]').click()`, ); const filterLabel = await page.evaluate(`document.querySelector("#runFilterBtn").textContent`); if (!filterLabel.includes("(1)")) { throw new Error(`filter control is missing the active count: ${filterLabel}`); } await page.click("#runFilterBtn"); // close the menu so the list is unobstructed const filteredChips = await page.evaluate( `[...document.querySelectorAll(".list.runs .run-row .run-row-head .status-icon")].map((n) => n.getAttribute("data-status"))`, ); if (!filteredChips.length) throw new Error("the status filter produced no rows"); if (!filteredChips.every((chip) => chip === "failed")) { throw new Error(`filtered rows are not all failed: ${filteredChips.join(",")}`); } shots.push(await page.screenshot(`${out}/12-runs-filtered.png`)); console.log(` filtered rows: ${filteredChips.length}, all failed`); step("filtering to a state with no runs: the filtered-empty state + Clear filters"); await page.click("#runFilterBtn"); await page.waitForSelector("#runFilterMenu"); await page.evaluate( `document.querySelector('#runFilterMenu input[data-status="failed"]').click()`, ); await page.evaluate( `document.querySelector('#runFilterMenu input[data-status="interrupted"]').click()`, ); await page.click("#runFilterBtn"); // close the menu to see the list await page.waitForSelector(".list.runs .empty-state"); const emptyMessage = await page.evaluate( `document.querySelector(".list.runs .empty-state span")?.textContent ?? ""`, ); if (emptyMessage !== "No runs match the current filters.") { throw new Error(`unexpected filtered-empty message: ${emptyMessage}`); } const clearAction = await page.evaluate( `document.querySelector(".list.runs .empty-state button")?.textContent ?? null`, ); if (clearAction !== "Clear filters") { throw new Error(`the filtered-empty state should offer Clear filters, got: ${clearAction}`); } shots.push(await page.screenshot(`${out}/13-runs-filtered-empty.png`)); console.log(` empty state: ${emptyMessage} (+ ${clearAction})`); step("clearing the filters from the empty state and changing the sort"); await ensureConnected(); await page.click(".list.runs .empty-state button"); await page.waitForSelector(".list.runs .day-header", { timeout: 25000 }); const resetLabel = await page.evaluate(`document.querySelector("#runFilterBtn").textContent`); if (resetLabel.includes("(")) throw new Error(`Clear filters did not reset the count: ${resetLabel}`); await page.click("#runSortBtn"); await page.waitForSelector("#runSortMenu"); const sortItems = await page.evaluate( `[...document.querySelectorAll("#runSortMenu .menu-item")].map((n) => n.textContent)`, ); for (const label of [ "Newest first", "Oldest first", "Longest duration", "Shortest duration", "Pipeline name", "Status", "Reset", ]) { if (!sortItems.includes(label)) throw new Error(`sort menu missing: ${label}`); } shots.push(await page.screenshot(`${out}/14-runs-sort-menu.png`)); await page.clickText("#runSortMenu .menu-item", "Pipeline name"); const sortLabel = await page.evaluate(`document.querySelector("#runSortBtn").textContent`); if (!sortLabel.includes("Pipeline name")) { throw new Error(`the sort control did not change: ${sortLabel}`); } await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); // Let the list's FLIP settle before measuring a row for a click. await settle(250); shots.push(await page.screenshot(`${out}/15-runs-sorted.png`)); console.log(` sort control now: ${sortLabel}`); step("opening a run detail"); await page.click(".list.runs .run-row"); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await shot("16-run-detail.png"); step("opening the Templates gallery"); await openTemplates(); await page.waitForSelector(".cards button.card", { timeout: 25000 }); shots.push(await page.screenshot(`${out}/17-templates-gallery.png`)); const templateCount = await page.evaluate(`document.querySelectorAll(".cards button.card").length`); console.log(` templates rendered: ${templateCount}`); step("opening a template detail (Summary)"); await page.click(".cards button.card"); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await page.waitForText("Key", { timeout: 25000 }); shots.push(await page.screenshot(`${out}/18-template-detail-summary.png`)); step("opening the template Source tab"); await clickDetailTab("Source"); await page.waitForText("Definition source", { timeout: 25000 }); shots.push(await page.screenshot(`${out}/19-template-source.png`)); // Artifact URLs resolve against whatever the URL field holds. Point it at the // page's own proxy origin so tiles and the viewer load real files same-origin: // serve.mjs forwards /artifacts/ to the Pipelines server (the socket stays on // that same proxy too). // The curated history this section grew against (runs 428/430/438/440/442/ // 449/450, with their artifacts and captures) is not part of a fresh database. // Discover whether any of it is present; if not, say so and skip the fixtures // instead of waiting forever for a row that will never exist. const curatedRunFixtures = await page.evaluate(`(() => { const ids = new Set([...document.querySelectorAll(".list.runs .run-row-meta[data-run-id]")].map((n) => Number(n.dataset.runId))); return [428, 430, 438, 440, 442, 449, 450].filter((id) => ids.has(id)).length; })()`); const hasCuratedRunFixtures = curatedRunFixtures > 0; if (!hasCuratedRunFixtures) { step("skipping the curated run fixtures (this history no longer contains them)"); } else { step("returning to Runs and opening run 430 (Summary tab)"); await clickDestination("Settings"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); await page.fill("#settingsServerUrl", artifactServerUrl); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); await openRunDetail(430); // The run-detail tabs, in the app's order (Summary / Graph / Input / Output / // Timeline; the Graph body is R5 and now rendered). const runTabs = await detailTabLabels(); if ( JSON.stringify(runTabs) !== JSON.stringify(["Summary", "Graph", "Input", "Output", "Timeline"]) ) { throw new Error(`run tabs are wrong: ${runTabs.join(",")}`); } console.log(` run tabs: ${runTabs.join(" / ")}`); // The run identity line, in the exact recorded/completed vocabulary. const identity = await page.evaluate(`document.querySelector(".run-identity")?.textContent ?? ""`); if (!/^run 430 · \d+ recorded visits? · \d+ completed visits?$/.test(identity)) { throw new Error(`run identity line is wrong: ${identity}`); } const runTitle = await page.evaluate( `document.querySelector(".run-detail-title")?.textContent ?? ""`, ); console.log(` summary: ${runTitle} — ${identity}`); shots.push(await page.screenshot(`${out}/20-run-430-summary.png`)); // The run-level artifacts gallery (R8), collapsed by default in Summary. step("expanding run 430's summary artifacts section (R8)"); await page.clickText(".run-detail .section-toggle", "Artifacts"); await page.waitForSelector(".run-detail .artifact-gallery .artifact-tile", { timeout: 25000 }); const runTiles = await page.evaluate( `document.querySelectorAll(".run-detail .artifact-gallery .artifact-tile").length`, ); console.log(` run 430 run-level gallery tiles: ${runTiles}`); if (runTiles !== 18) throw new Error(`expected 18 run-level artifact tiles, got ${runTiles}`); await page.rect(".run-detail .artifact-gallery"); shots.push(await page.screenshot(`${out}/04-run-430-artifacts.png`)); step("opening an image artifact in the full-screen viewer"); await page.click(".run-detail .artifact-gallery .artifact-tile img"); await page.waitForSelector(".artifact-viewer", { timeout: 15000 }); await page.waitForText("Close viewer", { timeout: 15000 }); const position = await page.evaluate( `document.querySelector(".viewer-position")?.textContent ?? ""`, ); console.log(` viewer position: ${position}`); // Best effort: give the full-resolution image a moment to decode. await eventually( () => page.evaluate( `(() => { const i = document.querySelector(".viewer-image"); return !!i && i.complete && i.naturalWidth > 0; })()`, ), { timeout: 8000, what: "the viewer image" }, ).catch(() => console.log(" (viewer image still loading)")); shots.push(await page.screenshot(`${out}/05-artifact-viewer.png`)); await page.click(".viewer-close"); await eventually(() => page.evaluate(`!document.querySelector(".artifact-viewer")`), { timeout: 5000, what: "the viewer to close", }); // R6: one collapsed field section per recorded input/output entry. step("opening the Input tab and expanding a field section"); await clickDetailTab("Input"); await page.waitForSelector(".detail .run-tab-body .collapsible .section-toggle", { timeout: 15000 }); const inputEmpty = await page.evaluate( `document.querySelector(".detail .run-tab-body .run-empty")?.textContent ?? ""`, ); if (inputEmpty !== "") throw new Error(`run 430 input unexpectedly empty: ${inputEmpty}`); await page.click(".detail .run-tab-body .collapsible .section-toggle"); await page.waitForSelector(".detail .run-tab-body .field-block", { timeout: 15000 }); const inputTitles = await page.evaluate( `[...document.querySelectorAll(".detail .run-tab-body .section-title")].map((n) => n.textContent)`, ); console.log(` input fields: ${inputTitles.join(", ")}`); shots.push(await page.screenshot(`${out}/21-run-430-input.png`)); step("opening the Output tab and expanding a field section"); await clickDetailTab("Output"); await page.waitForSelector(".detail .run-tab-body .collapsible .section-toggle", { timeout: 15000 }); await page.click(".detail .run-tab-body .collapsible .section-toggle"); await page.waitForSelector(".detail .run-tab-body .field-block", { timeout: 15000 }); const outputTitles = await page.evaluate( `[...document.querySelectorAll(".detail .run-tab-body .section-title")].map((n) => n.textContent)`, ); console.log(` output fields: ${outputTitles.join(", ")}`); shots.push(await page.screenshot(`${out}/22-run-430-output.png`)); // R7: one compact row per visit, in step order. step("opening the Timeline tab"); await clickDetailTab("Timeline"); await page.waitForSelector(".detail .timeline-row", { timeout: 15000 }); const timelineRows = await page.evaluate( `[...document.querySelectorAll(".detail .timeline-row .timeline-title")].map((n) => n.textContent)`, ); console.log(` timeline: ${timelineRows.join(" | ")}`); if (timelineRows.length !== 3) throw new Error(`expected 3 timeline rows, got ${timelineRows.length}`); if (timelineRows[0] !== "reviewer · visit 3") { throw new Error(`first timeline row is wrong: ${timelineRows[0]}`); } shots.push(await page.screenshot(`${out}/23-run-430-timeline.png`)); // R3: the stage pane: headline rows, Verdict block and the visit's gallery. step("opening the ui_reviewer stage pane: headline, Verdict and its artifact gallery"); await page.click('.detail .timeline-row[data-stage-key="ui_reviewer"]'); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.waitForText("Verdict", { timeout: 15000 }); const headline = await page.evaluate( `[...document.querySelectorAll(".stage-pane-body > .kv-row > .kv-key")].map((n) => n.textContent)`, ); console.log(` pane headline: ${headline.join(", ")}`); for (const label of ["Status", "Outcome", "Duration", "Visit", "Target", "Session"]) { if (!headline.includes(label)) throw new Error(`stage pane headline missing: ${label}`); } const verdictTitle = await page.evaluate( `document.querySelector(".stage-pane .verdict-title")?.textContent ?? ""`, ); const verdictOption = await page.evaluate( `document.querySelector(".stage-pane .verdict .outcome-chip")?.textContent ?? ""`, ); if (verdictTitle !== "Verdict") throw new Error(`verdict heading is wrong: ${verdictTitle}`); console.log(` verdict: ${verdictTitle} — ${verdictOption}`); await page.clickText(".stage-pane .section-toggle", "Artifacts"); await page.waitForSelector(".stage-pane .artifact-tile", { timeout: 15000 }); const paneTiles = await page.evaluate(`document.querySelectorAll(".stage-pane .artifact-tile").length`); console.log(` ui_reviewer visit 2 pane tiles: ${paneTiles}`); shots.push(await page.screenshot(`${out}/24-run-430-stage-artifacts.png`)); // Escape closes the pane and returns focus to the timeline row that opened it. step("closing the pane with Escape returns focus to the timeline row"); await page.evaluate( `document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`, ); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the stage pane to close on Escape", }); const restored = await page.evaluate( `(() => { const el = document.activeElement; return el ? el.getAttribute("data-stage-key") ?? "" : ""; })()`, ); if (restored !== "ui_reviewer") throw new Error(`focus did not return to the timeline row: ${restored}`); console.log(` focus returned to timeline row: ${restored}`); // R9: the visit's exported session file, opened from the pane's Session section. step("opening the reviewer stage pane and its Session export link (R9)"); await page.click('.detail .timeline-row[data-stage-key="reviewer"]'); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.clickText(".stage-pane .section-toggle", "Session"); await page.waitForSelector(".stage-pane .session-link", { timeout: 15000 }); const sessionHref = await page.evaluate( `document.querySelector(".stage-pane .session-link")?.getAttribute("href") ?? ""`, ); if (!/session\.html$/.test(sessionHref)) { throw new Error(`the session export link did not resolve: ${sessionHref}`); } console.log(` session export link: ${sessionHref}`); shots.push(await page.screenshot(`${out}/06-session-export-link.png`)); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the stage pane to close on Back", }); // The verdict-bearing stage pane on the failed run 440. step("opening run 440 and capturing its verdict-bearing stage pane"); await openRunDetail(440); await clickDetailTab("Timeline"); await page.waitForSelector(".detail .timeline-row", { timeout: 15000 }); await page.click(".detail .timeline-row"); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.waitForText("Verdict", { timeout: 15000 }); const verdict440 = await page.evaluate( `document.querySelector(".stage-pane .verdict .outcome-chip")?.textContent ?? ""`, ); console.log(` run 440 verdict: ${verdict440}`); shots.push(await page.screenshot(`${out}/25-run-440-verdict.png`)); await page.evaluate( `document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`, ); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the run 440 pane to close", }); // The Timeline empty state, on a run that recorded no visits (R7). The exact // empty wording is unit-tested; this is the live capture, so it is taken only // when the server still holds a run with no visits (run 428 once was one, but // the live history grows and its steps can gain visits), and otherwise reported // as skipped rather than failing the whole check on drifting live data. step("opening run 428: the Timeline empty state"); let emptyTimeline = ""; try { await openRunDetail(428); await clickDetailTab("Timeline"); await page.waitForSelector(".detail .run-tab-body .run-empty", { timeout: 15000 }); emptyTimeline = await page.evaluate( `document.querySelector(".detail .run-tab-body .run-empty")?.textContent ?? ""`, ); } catch { emptyTimeline = ""; } if (emptyTimeline === "No visits recorded.") { console.log(` timeline empty state: ${emptyTimeline}`); shots.push(await page.screenshot(`${out}/26-run-428-timeline-empty.png`)); } else { console.log(" -- no run with an empty Timeline on this server; empty-state wording is unit-tested"); } // R5: the run-detail Graph tab — the shared workflow graph in run mode. Run 438 // is a finished run with several visits and the full outcome set: its graph // shows the Executed / Not executed legend, solid executed nodes and arrowed // executed routes, and dashed untaken routes. Clicking a stage opens the // existing visit pane (with its Verdict) at that stage's latest visit. Run 440 // shows the hollow, dashed nodes for the stages the failed run never reached. step("opening run 438 and capturing its Graph tab (R5)"); await page.click('.list.runs .run-row-meta[data-run-id="438"]'); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await clickDetailTab("Graph"); await page.waitForSelector(".run-detail .graph-node", { timeout: 15000 }); // The run graph has no guidance paragraph (operator request) and fills most of // the tab body rather than a fixed strip. if (await page.evaluate(`!!document.querySelector(".run-detail .graph-note")`)) { throw new Error("the run graph still renders its guidance paragraph"); } const runGraphHeight = await page.evaluate( `Math.round(document.querySelector(".run-detail .graph-viewport")?.getBoundingClientRect().height ?? 0)`, ); if (runGraphHeight < Math.round(0.5 * 900)) { throw new Error(`the run graph viewport is only ${runGraphHeight}px tall`); } await eventually(async () => { const current = await graphState(); return current != null && current.scale > 0; }, { what: "the run graph to lay out" }); const legend = await page.evaluate( `[...document.querySelectorAll(".run-detail .graph-legend .graph-legend-label")].map((n) => n.textContent)`, ); if (JSON.stringify(legend) !== JSON.stringify(["Executed", "Not executed"])) { throw new Error(`the run graph legend is wrong: ${JSON.stringify(legend)}`); } const graph438 = await page.evaluate(`(() => ({ executedNodes: document.querySelectorAll(".run-detail .graph-node.executed").length, notExecutedNodes: document.querySelectorAll(".run-detail .graph-node.not-executed").length, executedEdges: document.querySelectorAll(".run-detail .graph-edge-path.executed").length, notExecutedEdges: document.querySelectorAll(".run-detail .graph-edge-path.not-executed").length, arrowedEdges: document.querySelectorAll('.run-detail .graph-edge-path[marker-end]').length, active: document.querySelectorAll(".run-detail .graph-node.active").length, }))()`); if (!graph438.executedNodes || !graph438.executedEdges || !graph438.notExecutedEdges) { throw new Error(`run 438 should show executed stages and both executed/untaken routes: ${JSON.stringify(graph438)}`); } if (graph438.arrowedEdges !== graph438.executedEdges) { throw new Error(`only executed routes carry an arrowhead: ${JSON.stringify(graph438)}`); } if (graph438.active !== 0) { throw new Error(`a finished run must have no active halo: ${JSON.stringify(graph438)}`); } console.log( ` run 438 graph: ${graph438.executedNodes} executed / ${graph438.notExecutedNodes} not-executed nodes, ` + `${graph438.executedEdges} arrowed / ${graph438.notExecutedEdges} dashed routes, legend ${legend.join("/")}`, ); await page.rect(".run-detail .graph-viewport"); shots.push(await page.screenshot(`${out}/28-run-438-graph.png`)); step("opening a stage's visit pane from the run 438 graph"); await page.evaluate( `document.querySelector('.run-detail .graph-node[data-key="ui_reviewer"]').click()`, ); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.waitForText("Verdict", { timeout: 15000 }); const graphPane = await page.evaluate(`(() => ({ title: document.querySelector(".stage-pane-title")?.textContent ?? "", verdict: document.querySelector(".stage-pane .verdict .outcome-chip")?.textContent ?? "", }))()`); if (graphPane.title !== "ui_reviewer") { throw new Error(`the graph-opened pane named the wrong stage: ${graphPane.title}`); } if (!graphPane.verdict) throw new Error("the graph-opened pane did not show a verdict"); console.log(` graph-opened pane: ${graphPane.title} — verdict ${graphPane.verdict}`); await page.rect(".stage-pane-sheet"); shots.push(await page.screenshot(`${out}/29-run-438-graph-pane.png`)); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the graph stage pane to close", }); step("opening run 440 to capture the hollow, not-executed stages (R5)"); await page.click('.list.runs .run-row-meta[data-run-id="440"]'); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await clickDetailTab("Graph"); await page.waitForSelector(".run-detail .graph-node", { timeout: 15000 }); const graph440 = await page.evaluate(`(() => ({ executedNodes: document.querySelectorAll(".run-detail .graph-node.executed").length, notExecutedNodes: document.querySelectorAll(".run-detail .graph-node.not-executed").length, hollow: [...document.querySelectorAll(".run-detail .graph-node.not-executed")].every( (n) => getComputedStyle(n).borderStyle === "dashed", ), }))()`); if (!graph440.executedNodes || !graph440.notExecutedNodes) { throw new Error(`run 440 should show visited and unvisited stages: ${JSON.stringify(graph440)}`); } if (!graph440.hollow) { throw new Error(`an unvisited node is not hollow/dashed: ${JSON.stringify(graph440)}`); } console.log(` run 440 graph: ${graph440.executedNodes} executed / ${graph440.notExecutedNodes} not-executed (dashed) nodes`); await page.rect(".run-detail .graph-viewport"); shots.push(await page.screenshot(`${out}/30-run-440-graph.png`)); // R5: a live run's Graph tab — the current stage carries the pulsing halo and // a ticking elapsed badge; a finished run (above) carries neither. Best effort: // the run may stop before we look, so skip rather than fail if none is running. step("opening a live run's Graph tab (R5: halo + elapsed badge)"); const liveGraphRunId = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-running")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (liveGraphRunId == null) { console.log(" -- no running run available; skipped the live graph halo capture"); } else { await page.click(`.list.runs .run-row-meta[data-run-id="${liveGraphRunId}"]`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await clickDetailTab("Graph"); await page.waitForSelector(".run-detail .graph-node", { timeout: 15000 }); // The run can stop (or be reported `running` with no stage in flight) between // the list read and this graph, so treat the halo as the best effort its // comment promises rather than failing the whole run on a race. const live = await eventually( async () => page.evaluate(`(() => { const active = document.querySelector(".run-detail .graph-node.active"); if (!active) return null; return { activeKey: active.dataset.key ?? "", pulsing: active.classList.contains("pulsing"), badge: active.querySelector(".graph-elapsed")?.textContent ?? "", }; })()`), { timeout: 15000, what: "the live run's active stage" }, ).catch(() => null); // The row was running when it was picked but may have stopped while its // detail loaded; this capture is documented as best effort, so say so rather // than fail an unrelated step on live-data timing. if (!live) { console.log(` -- run ${liveGraphRunId} shows no stage in flight; skipped the live graph halo capture`); } else { if (!live.badge) throw new Error(`the active stage showed no elapsed badge: ${JSON.stringify(live)}`); console.log(` live run ${liveGraphRunId}: active stage ${live.activeKey} (pulsing=${live.pulsing}), badge ${live.badge}`); await page.rect(".run-detail .graph-viewport"); shots.push(await page.screenshot(`${out}/31-run-live-graph.png`)); } } // The Output empty state, on the running run that has not produced output yet. step("opening the Output tab on a run with no output recorded"); const runningId = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-running")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (runningId == null) { console.log(" -- no running run available; skipped the output empty state"); } else { await openRunDetail(runningId); await clickDetailTab("Output"); await page.waitForSelector(".detail .run-tab-body .run-empty", { timeout: 15000 }); const emptyOutput = await page.evaluate( `document.querySelector(".detail .run-tab-body .run-empty")?.textContent ?? ""`, ); if (emptyOutput !== "No output recorded.") { throw new Error(`output empty state is wrong: ${emptyOutput}`); } console.log(` output empty state (run ${runningId}): ${emptyOutput}`); shots.push(await page.screenshot(`${out}/27-run-output-empty.png`)); } // T5: connection now lives in Settings. The shell's compact control names the // state at a glance; Settings owns the full status card and the server build. // The runs list carries no live badge (the shell's Connected status is the one // honest indicator). } step("capturing Settings' connected state, the server build and the shell status (T5)"); await clickDestination("Settings"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); await ensureConnected(); const connectedText = await text("#shellStatus"); if (!connectedText.includes("Connected")) throw new Error(`shell status text: ${connectedText}`); const statusCard = await text(".settings-status"); if (!statusCard.startsWith("Connected")) throw new Error(`connected status card: ${statusCard}`); const buildLabel = statusCard; if (!buildLabel.includes("Server build ")) throw new Error(`missing server build line: ${buildLabel}`); await shot("28-connected-banner.png"); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); if (await page.evaluate(`!!document.querySelector(".list.runs .live-badge")`)) { throw new Error("the runs list still renders a live badge"); } console.log(` status: ${connectedText}; ${buildLabel}; no runs-list live badge`); // T3/T4: disconnect in Settings, point it at a closed port and use Reconnect. step("capturing the disconnected Settings status and the shell indicator (T3/T4)"); const closedPort = await freePort(); await clickDestination("Settings"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); await page.fill("#settingsServerUrl", `ws://127.0.0.1:${closedPort}/connect`); await page.click("#settingsPrimary"); // currently connected: this Disconnects await page.waitForSelector('#shellStatus[data-state="idle"]', { timeout: 15000 }); const idleText = await text("#shellStatus"); if (idleText !== "Not connected") throw new Error(`idle shell status text: ${idleText}`); const idleCard = await text(".settings-status"); if (idleCard !== "Not connected") throw new Error(`idle status card: ${idleCard}`); await page.click("#settingsSecondary"); // Reconnect the closed port now await page.waitForSelector('#shellStatus[data-state="disconnected"]', { timeout: 15000 }); await page.waitForText("Retrying with backoff", { timeout: 15000 }); const disconnectedText = await text(".settings-status"); if (!/^Disconnected/.test(disconnectedText) || !disconnectedText.includes("Retrying with backoff")) { throw new Error(`disconnected status card: ${disconnectedText}`); } // The shell status follows the socket everywhere, including the runs list. await clickDestination("Runs"); await page.waitForSelector('#shellStatus[data-state="disconnected"]', { timeout: 10000 }); console.log(" disconnected: the shell status reads disconnected on the runs list"); console.log(` disconnected status: ${disconnectedText}`); await clickDestination("Settings"); await shot("29-disconnected-banner.png"); // T4: return-to-foreground recovery. Point back at the live server and let the // page's own `online` handler drive the socket back without a Reconnect click. // The decision itself (foregroundAction) is unit-tested in // test/connection.test.mjs; this proves the rendered recovery in the browser. step("recovering via the browser's online event (T4)"); await page.waitForSelector("#settingsServerUrl", { timeout: 15000 }); await page.fill("#settingsServerUrl", `ws://127.0.0.1:${port}/connect`); const foregroundText = await text("#shellStatus"); if (foregroundText !== "Disconnected") { throw new Error(`expected a disconnected state before the online event: ${foregroundText}`); } await page.evaluate(`window.dispatchEvent(new Event("online"))`); await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 25000 }); const recoveredText = await text("#shellStatus"); if (!recoveredText.includes("Connected")) throw new Error(`recovered shell text: ${recoveredText}`); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 15000 }); console.log(` recovered after window online: ${recoveredText} (shell status connected again)`); await shot("30-reconnect-recovered.png"); // R10: the live run stream. First the wire shape of the watch on a forced // reconnect (unsubscribe, then subscribe with the blank workspace, then one // list_runs), then a real small run whose `run_event` pushes fold the list and // the open detail (running -> canceled), both captured. step("R10: unsubscribe/subscribe/list_runs on a real reconnect"); await ensureConnected(); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); await page.evaluate(`window.__pipelinesSocket && window.__pipelinesSocket.close(4000, "r10 reconnect")`); // Observe the drop before arming the counters and waiting for the recovery, so // the unsubscribe/subscribe/list_runs sequence counted below necessarily // belongs to the re-subscribe rather than a prior reconnect's traffic. await eventually( () => page.evaluate(`document.querySelector("#shellStatus")?.dataset.state !== "connected"`), { timeout: 15000, what: "the socket to drop before the run-event reconnect" }, ); await page.evaluate( `(() => { window.__pipelinesSentTypes = []; window.__runSubscribes = []; window.__runUnsubscribes = 0; window.__runEventFrames = []; return true; })()`, ); await ensureConnected(30000); await eventually( () => page.evaluate(`(window.__pipelinesSentTypes || []).includes("list_runs")`), { timeout: 20000, what: "the resync list_runs after the run-event subscribe" }, ); const r10wire = await page.evaluate(`(() => { const types = window.__pipelinesSentTypes || []; const subscribes = window.__runSubscribes || []; const last = subscribes.length ? { ...subscribes[subscribes.length - 1] } : null; if (last) delete last.id; return { unsubscribe: window.__runUnsubscribes || 0, unsubscribeAt: types.indexOf("unsubscribe"), subscribeAt: types.indexOf("subscribe"), listRunsAt: types.indexOf("list_runs"), lastSubscribe: last, }; })()`); if (r10wire.unsubscribe < 1) throw new Error("R10: no unsubscribe was sent on reconnect"); if (!(r10wire.unsubscribeAt >= 0 && r10wire.subscribeAt > r10wire.unsubscribeAt)) { throw new Error(`R10: unsubscribe/subscribe order was ${JSON.stringify(r10wire)}`); } if (!(r10wire.listRunsAt > r10wire.subscribeAt)) { throw new Error(`R10: list_runs did not follow the subscribe: ${JSON.stringify(r10wire)}`); } if (r10wire.lastSubscribe?.type !== "subscribe" || r10wire.lastSubscribe?.workspace !== "" || "run_ids" in (r10wire.lastSubscribe ?? {})) { throw new Error(`R10: subscribe frame was ${JSON.stringify(r10wire.lastSubscribe)}`); } console.log( ` reconnect wire: ${r10wire.unsubscribe} unsubscribe, subscribe ${JSON.stringify(r10wire.lastSubscribe)}, then list_runs (positions ${r10wire.unsubscribeAt}/${r10wire.subscribeAt}/${r10wire.listRunsAt})`, ); step("R10: starting one small real run and folding its pushed updates"); let r10RunId = null; try { r10RunId = await startLiveRun(); console.log(` started run ${r10RunId} in ${liveRunScratch}`); // The row appears from the frame's own pipeline identity, without a refetch. await eventually( () => page.evaluate(`!!document.querySelector('.list.runs .run-row-meta[data-run-id="${r10RunId}"]')`), { timeout: 30000, what: `run ${r10RunId}'s row to appear from a pushed frame` }, ); // Prove at least one genuine server push (not a replay) for this run. await eventually( () => page.evaluate(`(window.__runEventFrames || []).some((f) => Number(f.run_id) === ${r10RunId})`), { timeout: 30000, what: `a pushed run_event frame for run ${r10RunId}` }, ); const listStatus = await page.evaluate( `document.querySelector('.list.runs .run-row-meta[data-run-id="${r10RunId}"]')?.closest(".run-row")?.querySelector(".status-icon")?.getAttribute("data-status") ?? ""`, ); const listBadge = await page.evaluate( `document.querySelector(".list.runs .live-badge .live-word")?.textContent ?? ""`, ); console.log(` pushed list fold: run ${r10RunId} chip "${listStatus}", badge "${listBadge}"`); if (listBadge !== "live") throw new Error(`R10: list badge read ${JSON.stringify(listBadge)}`); await shot("75-run-live-list.png"); await openRunDetail(r10RunId); const detailBadge = await page.evaluate( `document.querySelector(".run-detail .live-badge .live-word")?.textContent ?? ""`, ); const detailStatus = await page.evaluate( `document.querySelector(".run-detail .run-chip-row .chip")?.textContent ?? ""`, ); if (detailBadge !== "live") throw new Error(`R10: detail badge read ${JSON.stringify(detailBadge)}`); console.log(` pushed detail fold: status "${detailStatus}", badge "${detailBadge}"`); await shot("76-run-live-detail.png"); // Cancel the throwaway run; the terminal frame must fold the open detail to a // terminal word without a poll (there is no run-detail timer any more). A // trivial prompt can finish before the cancel lands, so either terminal word // is accepted: the point is the pushed fold, never the timing race. await cancelLiveRun(r10RunId); await eventually( () => page.evaluate( `["canceled","succeeded","failed","interrupted"].includes(document.querySelector(".run-detail .run-chip-row .chip")?.textContent ?? "")`, ), { timeout: 30000, what: `run ${r10RunId}'s detail to fold to a terminal state` }, ); const finalStatus = await page.evaluate( `document.querySelector(".run-detail .run-chip-row .chip")?.textContent ?? ""`, ); console.log(` terminal fold: run ${r10RunId} detail now "${finalStatus}" (pushed, no poll)`); await shot("77-run-live-terminal.png"); liveRunId = null; // terminal; the finally must not cancel it again } catch (error) { console.log(` R10: could not observe a real pushed run (${error.message}); no live-run capture`); if (r10RunId) await cancelLiveRun(r10RunId); } // R4: the run Summary's collapsible Changes section and its rendered/raw diff // viewer. Run 438 changed 22 unique files across 6 steps with every capture // partial, so the header/row count are asserted against each other rather than // against a hardcoded total. const changesToggle = () => page.evaluate(`(() => { const t = [...document.querySelectorAll(".run-detail .collapsible .section-toggle")] .find((n) => /\\d+ files? changed/.test(n.querySelector(".section-title")?.textContent ?? "")); if (!t) return null; return { title: t.querySelector(".section-title")?.textContent ?? "", expanded: t.getAttribute("aria-expanded"), rows: t.parentElement?.querySelectorAll(".change-row").length ?? 0, }; })()`); const expandChanges = async () => { const marked = await page.evaluate(`(() => { const t = [...document.querySelectorAll(".run-detail .collapsible .section-toggle")] .find((n) => /\\d+ files? changed/.test(n.querySelector(".section-title")?.textContent ?? "")); if (!t) return false; t.scrollIntoView({ block: "center" }); t.setAttribute("data-test-changes-toggle", "1"); return true; })()`); if (!marked) throw new Error("no Changes section toggle rendered"); await page.click("[data-test-changes-toggle='1']"); await page.evaluate( `document.querySelector("[data-test-changes-toggle='1']")?.removeAttribute("data-test-changes-toggle")`, ); await page.waitForSelector(".run-detail .changes-list .change-row", { timeout: 15000 }); }; const closeChangeViewer = async (how) => { if (how === "escape") { await page.evaluate( `document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`, ); } else if (how === "backdrop") { await page.evaluate(`document.querySelector(".change-viewer").click()`); } else { await page.click(".change-viewer-close"); } await eventually(() => page.evaluate(`!document.querySelector(".change-viewer")`), { timeout: 5000, what: `the diff viewer to close (${how})`, }); }; if (!hasCuratedRunFixtures) { step("skipping the curated change/capture fixtures (this history no longer contains them)"); } else { step("opening run 438: the collapsed Changes section (R4)"); await page.click('.list.runs .run-row-meta[data-run-id="438"]'); await page.waitForText("run 438 ·", { timeout: 25000 }); await page.waitForSelector("#detailTitle", { timeout: 25000 }); const collapsed = await changesToggle(); if (!collapsed) throw new Error("run 438's Summary rendered no Changes section"); if (collapsed.expanded !== "false") { throw new Error(`the Changes section is not collapsed on first render: ${collapsed.expanded}`); } if (collapsed.rows !== 0) throw new Error(`a collapsed Changes section drew ${collapsed.rows} row(s)`); const changesCount = Number((/^(\d+) files? changed/.exec(collapsed.title) ?? [])[1] ?? 0); if (!changesCount) throw new Error(`the Changes header has no file count: ${collapsed.title}`); if (!collapsed.title.includes("partial capture")) { throw new Error(`run 438's Changes header should note the partial capture: ${collapsed.title}`); } console.log(` collapsed changes header: ${collapsed.title}`); shots.push(await page.screenshot(`${out}/28-run-438-changes-collapsed.png`)); step("expanding the Changes section: the observed-file rows"); await expandChanges(); const expanded = await page.evaluate(`(() => ({ summary: document.querySelector(".run-detail .changes-summary")?.textContent ?? "", note: document.querySelector(".run-detail .changes-note")?.textContent ?? "", rows: document.querySelectorAll(".run-detail .changes-list .change-row").length, kinds: [...document.querySelectorAll(".run-detail .changes-list .change-kind")].map((n) => n.textContent), expanded: [...document.querySelectorAll(".run-detail .collapsible .section-toggle")] .find((n) => /\\d+ files? changed/.test(n.querySelector(".section-title")?.textContent ?? "")) ?.getAttribute("aria-expanded"), }))()`); if (expanded.expanded !== "true") throw new Error("the Changes section did not expand"); if (expanded.rows !== changesCount) { throw new Error(`the Changes section drew ${expanded.rows} rows for a header count of ${changesCount}`); } if (!expanded.note.startsWith("Partial capture: some changes may be missing.")) { throw new Error(`the partial-capture note is missing: ${expanded.note}`); } console.log(` expanded: ${expanded.summary} — ${expanded.rows} row(s)`); console.log(` note: ${expanded.note}`); console.log(` kinds: ${[...new Set(expanded.kinds)].join(", ")}`); // The summary is the final state: retry_test.go was modified in two // implementer visits (+30 -12, then +42 -12). The row must show the last // visit's +42 -12, not their +72 -24 sum, and only the one stage name. const finalState = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".run-detail .changes-list .change-row")] .find((n) => n.querySelector(".change-path")?.textContent === "internal/server/retry_test.go"); if (!row) return null; return { stat: [...row.querySelectorAll(".change-stat span")].map((n) => n.textContent).join(" "), meta: row.querySelector(".change-meta")?.textContent ?? "", }; })()`); if (!finalState) throw new Error("run 438's Changes list lost internal/server/retry_test.go"); if (finalState.stat !== "+42 -12") { throw new Error(`run 438 did not show retry_test.go's final-visit stat: ${finalState.stat}`); } if (finalState.meta !== "changed by implementer") { throw new Error(`run 438's retry_test.go stage list is wrong: ${finalState.meta}`); } if (!collapsed.title.includes("+934 -156")) { throw new Error(`run 438's header is not the final-state stat: ${collapsed.title}`); } console.log(` final state: retry_test.go ${finalState.stat} — ${finalState.meta}`); await page.evaluate(`document.querySelector(".run-detail .changes-list .change-row")?.scrollIntoView({ block: "center" })`); shots.push(await page.screenshot(`${out}/29-run-438-changes-expanded.png`)); step("opening the rendered diff viewer on the first changed file"); await page.click(".run-detail .changes-list .change-row"); await page.waitForSelector(".change-viewer", { timeout: 15000 }); const rendered = await page.evaluate(`(() => ({ kind: document.querySelector(".change-viewer .change-kind")?.textContent ?? "", path: document.querySelector(".change-viewer-path")?.textContent ?? "", position: document.querySelector(".change-viewer-position")?.textContent ?? "", renderedActive: document.querySelector('.change-form[data-form="rendered"]')?.classList.contains("active") ?? false, renderedDisabled: document.querySelector('.change-form[data-form="rendered"]')?.disabled ?? null, rawDisabled: document.querySelector('.change-form[data-form="raw"]')?.disabled ?? null, hunks: document.querySelectorAll(".change-viewer .change-hunk").length, lines: document.querySelectorAll(".change-viewer .change-line").length, gutter: document.querySelector(".change-viewer .change-gutter")?.textContent ?? "", adds: document.querySelectorAll(".change-viewer .change-text.diff-add").length, removes: document.querySelectorAll(".change-viewer .change-text.diff-remove").length, prevDisabled: document.querySelector('.change-page[data-page="prev"]')?.disabled ?? null, }))()`); if (!rendered.renderedActive || rendered.renderedDisabled !== true) { throw new Error(`Rendered is not the default selected form: ${JSON.stringify(rendered)}`); } if (!rendered.path || !rendered.position.includes("of ")) { throw new Error(`the viewer did not identify its file: ${JSON.stringify(rendered)}`); } if (!rendered.hunks || !rendered.lines) { throw new Error(`the rendered viewer drew no hunks/lines: ${JSON.stringify(rendered)}`); } if (rendered.gutter.trim() === "") throw new Error("the rendered viewer drew no gutter numbers"); if (rendered.prevDisabled !== true) throw new Error("Previous should be disabled on the first file"); console.log( ` rendered: ${rendered.kind} ${rendered.path} — ${rendered.position}; ` + `${rendered.hunks} hunk(s), ${rendered.lines} line(s), ${rendered.adds}+/${rendered.removes}-`, ); shots.push(await page.screenshot(`${out}/30-run-438-changes-diff-rendered.png`)); step("switching the diff viewer to the Raw patch"); await page.evaluate(`document.querySelector('.change-form[data-form="raw"]').click()`); await page.waitForSelector(".change-viewer .change-raw", { timeout: 15000 }); const raw = await page.evaluate(`(() => ({ rawActive: document.querySelector('.change-form[data-form="raw"]')?.classList.contains("active") ?? false, renderedDisabled: document.querySelector('.change-form[data-form="rendered"]')?.disabled ?? null, text: document.querySelector(".change-viewer .change-raw")?.textContent ?? "", }))()`); if (!raw.rawActive) throw new Error("Raw patch did not become the selected form"); if (!raw.text.includes("@@")) { throw new Error(`the raw patch does not look like a unified diff: ${raw.text.slice(0, 80)}`); } console.log(` raw patch: ${raw.text.split("\n")[0]} (${raw.text.length} chars)`); shots.push(await page.screenshot(`${out}/31-run-438-changes-diff-raw.png`)); step("paging between files: Next to the last, then Previous"); await page.evaluate(`document.querySelector('.change-page[data-page="next"]').click()`); await eventually( async () => (await page.evaluate(`document.querySelector(".change-viewer-position")?.textContent ?? ""`)).startsWith("2 of "), { what: "the viewer to page to file 2" }, ); await page.evaluate(`(() => { for (let i = 0; i < 500; i += 1) { const next = document.querySelector('.change-page[data-page="next"]'); if (!next || next.disabled) break; next.click(); } })()`); const last = await page.evaluate(`(() => ({ position: document.querySelector(".change-viewer-position")?.textContent ?? "", nextDisabled: document.querySelector('.change-page[data-page="next"]')?.disabled ?? null, prevDisabled: document.querySelector('.change-page[data-page="prev"]')?.disabled ?? null, }))()`); if (last.nextDisabled !== true) throw new Error("Next is not disabled on the last file"); if (!last.position.startsWith(`${changesCount} of `)) { throw new Error(`the viewer did not reach the last file: ${last.position}`); } console.log(` paged to the last file: ${last.position}`); await page.evaluate(`document.querySelector('.change-page[data-page="prev"]').click()`); await eventually( async () => (await page.evaluate(`document.querySelector(".change-viewer-position")?.textContent ?? ""`)).startsWith(`${changesCount - 1} of `), { what: "the viewer to page back" }, ); step("closing the diff viewer with Escape returns focus to the row"); await closeChangeViewer("escape"); const restoredFocus = await page.evaluate( `document.activeElement?.getAttribute?.("data-change-index") ?? ""`, ); // The viewer was paged back one file just before closing, so focus returns to // that row (index changesCount - 2). if (restoredFocus !== String(changesCount - 2)) { throw new Error(`focus did not return to the opened row: ${restoredFocus}`); } console.log(` focus returned to change row ${restoredFocus}`); step("reopening and closing the diff viewer by a backdrop click"); await page.click(".run-detail .changes-list .change-row"); await page.waitForSelector(".change-viewer", { timeout: 15000 }); await closeChangeViewer("backdrop"); // Part 1: each visit's "This run" carries its own capture. Run 438's second // implementer visit changed only retry_test.go and must not show the run-wide // aggregate (or the first visit's docs/runner rows). step("opening run 438's second implementer visit: its own File changes"); await page.clickText(".rail-tab, .strip-tab", "Timeline"); await page.waitForSelector(".detail .timeline-row", { timeout: 15000 }); await page.click('.detail .timeline-row[data-stage-key="implementer"][data-visit-index="1"]'); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.clickText(".stage-pane .section-toggle", "This run"); await page.waitForSelector(".stage-pane .codeblock", { timeout: 15000 }); const visitChanges = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".stage-pane .kv-row")] .find((n) => n.querySelector(".kv-key")?.textContent === "File changes"); const block = [...document.querySelectorAll(".stage-pane .codeblock")] .find((n) => n.querySelector(".codeblock-label")?.textContent === "Changed files"); return { summary: row?.querySelector(".kv-val")?.textContent ?? "", files: block?.querySelector(".codeblock-body")?.textContent ?? "", labels: [...document.querySelectorAll(".stage-pane .codeblock-label")].map((n) => n.textContent), }; })()`); if (!visitChanges.summary.startsWith("partial (1 files), 2 skipped · Excluded protected")) { throw new Error(`the implementer visit's File changes is wrong: ${visitChanges.summary}`); } if (visitChanges.files !== "modified internal/server/retry_test.go +42 -12") { throw new Error(`the implementer visit's Changed files is wrong: ${visitChanges.files}`); } if (visitChanges.files.includes("docs/notes") || visitChanges.files.includes("runner.go")) { throw new Error(`the visit pane leaked another visit's files: ${visitChanges.files}`); } console.log(` implementer visit 2: ${visitChanges.summary}`); console.log(` changed files: ${visitChanges.files}`); await page.evaluate(`(() => { const row = [...document.querySelectorAll(".stage-pane .kv-row")] .find((n) => n.querySelector(".kv-key")?.textContent === "File changes"); row?.scrollIntoView({ block: "center" }); })()`); shots.push(await page.screenshot(`${out}/78-run-438-implementer-visit-changes.png`)); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the implementer visit pane to close", }); // A binary capture: the honest message in place of a diff, and Raw disabled. // Run 449 succeeded and recorded binary captures; choose its binary row (its // first row is a text file). step("opening a binary capture (run 449): the honest message and a disabled Raw patch"); await openRunDetail(449); await expandChanges(); const openedBinary = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".run-detail .changes-list .change-row")] .find((n) => (n.querySelector(".change-meta")?.textContent ?? "").includes("binary")); if (!row) return false; row.click(); return true; })()`); if (!openedBinary) throw new Error("run 449's Changes list showed no binary row"); await page.waitForSelector(".change-viewer .change-honest", { timeout: 15000 }); const binary = await page.evaluate(`(() => ({ message: document.querySelector(".change-viewer .change-honest")?.textContent ?? "", rawDisabled: document.querySelector('.change-form[data-form="raw"]')?.disabled ?? null, hunks: document.querySelectorAll(".change-viewer .change-hunk").length, }))()`); if ( binary.message !== "This is a binary file: the server recorded the change but has no textual diff to show." ) { throw new Error(`the binary message is wrong: ${binary.message}`); } if (binary.rawDisabled !== true) throw new Error("Raw patch should be disabled for a binary file"); if (binary.hunks !== 0) throw new Error("a binary file must not render a fabricated diff"); console.log(` binary: ${binary.message}`); shots.push(await page.screenshot(`${out}/32-run-449-changes-binary.png`)); await closeChangeViewer("close"); // A truncated capture: the truncation reason, still no fabricated diff. step("opening a truncated capture (run 450): the truncation reason"); await page.click('.list.runs .run-row-meta[data-run-id="450"]'); await page.waitForText("run 450 ·", { timeout: 25000 }); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await expandChanges(); const openedTruncated = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".run-detail .changes-list .change-row")] .find((n) => (n.querySelector(".change-meta")?.textContent ?? "").includes("truncated")); if (!row) return false; row.click(); return true; })()`); if (!openedTruncated) throw new Error("run 450's Changes list showed no truncated row"); await page.waitForSelector(".change-viewer .change-honest", { timeout: 15000 }); const truncated = await page.evaluate( `document.querySelector(".change-viewer .change-honest")?.textContent ?? ""`, ); if ( truncated !== "The server truncated this file's patch at its capture limit; no diff text was recorded." ) { throw new Error(`the truncation reason is wrong: ${truncated}`); } console.log(` truncated: ${truncated}`); shots.push(await page.screenshot(`${out}/33-run-450-changes-truncated.png`)); await closeChangeViewer("close"); // Part 3: a failed run has no settled final state, so its Summary carries no // aggregate Changes section at all. The visit pane still carries that visit's // own capture. step("opening failed run 440: no Summary Changes section"); await openRunDetail(440); const failedSummaryTitles = await page.evaluate( `[...document.querySelectorAll(".run-detail .collapsible .section-title")].map((n) => n.textContent ?? "")`, ); const leakedAggregate = failedSummaryTitles.find( (title) => /^Changes · /.test(title) || /^\d+ files? changed/.test(title), ); if (leakedAggregate) { throw new Error(`failed run 440 still shows an aggregate Changes section: ${leakedAggregate}`); } console.log(` run 440 summary sections: ${failedSummaryTitles.join(" | ")}`); shots.push(await page.screenshot(`${out}/79-run-440-no-summary-changes.png`)); step("run 440's visit pane keeps that visit's own File changes"); await page.clickText(".rail-tab, .strip-tab", "Timeline"); await page.waitForSelector(".detail .timeline-row", { timeout: 15000 }); await page.click(".detail .timeline-row"); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.clickText(".stage-pane .section-toggle", "This run"); await page.waitForSelector(".stage-pane .kv-row", { timeout: 15000 }); const failedVisit = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".stage-pane .kv-row")] .find((n) => n.querySelector(".kv-key")?.textContent === "File changes"); const block = [...document.querySelectorAll(".stage-pane .codeblock")] .find((n) => n.querySelector(".codeblock-label")?.textContent === "Changed files"); return { summary: row?.querySelector(".kv-val")?.textContent ?? "", files: block?.querySelector(".codeblock-body")?.textContent ?? "", }; })()`); // This visit's capture recorded no file rows (0 files, 2 skipped), so the // honest summary is shown and Changed files is correctly absent. if (!failedVisit.summary.startsWith("partial, 2 skipped · Excluded protected")) { throw new Error(`run 440's visit File changes is wrong: ${failedVisit.summary}`); } if (failedVisit.files !== "") { throw new Error(`run 440's visit invented Changed files with no recorded rows: ${failedVisit.files}`); } console.log(` run 440 visit: ${failedVisit.summary} (no recorded file rows)`); await page.evaluate(`(() => { const row = [...document.querySelectorAll(".stage-pane .kv-row")] .find((n) => n.querySelector(".kv-key")?.textContent === "File changes"); row?.scrollIntoView({ block: "center" }); })()`); shots.push(await page.screenshot(`${out}/80-run-440-visit-changes.png`)); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the run 440 visit pane to close", }); // A failed run that did record file rows: run 442's implementer visit keeps // its own Changed files even though the Summary aggregate is suppressed. step("run 442's failed implementer visit keeps its Changed files"); await openRunDetail(442); await page.clickText(".rail-tab, .strip-tab", "Timeline"); await page.waitForSelector(".detail .timeline-row", { timeout: 15000 }); await page.click('.detail .timeline-row[data-stage-key="implementer"][data-visit-index="0"]'); await page.waitForSelector(".stage-pane", { timeout: 15000 }); await page.clickText(".stage-pane .section-toggle", "This run"); await page.waitForSelector(".stage-pane .codeblock", { timeout: 15000 }); const failedFiles = await page.evaluate(`(() => { const block = [...document.querySelectorAll(".stage-pane .codeblock")] .find((n) => n.querySelector(".codeblock-label")?.textContent === "Changed files"); return block?.querySelector(".codeblock-body")?.textContent ?? ""; })()`); if (!failedFiles.includes("web/src/")) { throw new Error(`run 442's implementer visit lost its Changed files: ${failedFiles}`); } console.log(` run 442 implementer visit changed files: ${failedFiles.split("\n").length} rows`); await page.evaluate(`(() => { const block = [...document.querySelectorAll(".stage-pane .codeblock")] .find((n) => n.querySelector(".codeblock-label")?.textContent === "Changed files"); block?.scrollIntoView({ block: "center" }); })()`); shots.push(await page.screenshot(`${out}/81-run-442-failed-visit-changes.png`)); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the run 442 visit pane to close", }); // A running run has no settled final state either: invert the old pending- // header probe and assert the aggregate is absent while it executes. step("a running run's Summary has no Changes section"); await page.click('[data-destination="runs"]'); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); const runningRunId = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")] .find((n) => n.querySelector(".chip-running")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (runningRunId == null) { console.log(" -- no running run available; skipped the no-aggregate check"); } else { await openRunDetail(runningRunId); const runningTitles = await page.evaluate( `[...document.querySelectorAll(".run-detail .collapsible .section-title")].map((n) => n.textContent ?? "")`, ); const runningAggregate = runningTitles.find( (title) => /^Changes · /.test(title) || /^\d+ files? changed/.test(title), ); if (runningAggregate) { throw new Error(`running run ${runningRunId} shows an aggregate Changes section: ${runningAggregate}`); } console.log(` running run ${runningRunId}: no Summary Changes section`); shots.push(await page.screenshot(`${out}/82-run-${runningRunId}-no-summary-changes.png`)); } // S1/S2: omni-search over the real server. The panel is opened from the top // bar; the query is typed through the page's own field, the flat result list // and its controls are asserted, a kind filter is applied and captured, and one // result of each kind is routed to its real destination. Every capture below is // a real screenshot of the rendered page. } step("opening omni-search from the top bar (S1)"); await ensureConnected(); await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); const viewSnapshot = () => page.evaluate(`(() => ({ tab: document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? "", detail: document.querySelector("#detailTitle")?.textContent ?? "", }))()`); const beforeOpen = await viewSnapshot(); await page.click("#searchToggle"); await page.waitForSelector("#searchInput", { timeout: 15000 }); const chrome = await page.evaluate(`(() => { const input = document.querySelector("#searchInput"); return { placeholder: input?.getAttribute("placeholder") ?? "", autofocused: document.activeElement === input, close: document.querySelector("#searchClose")?.textContent ?? "", }; })()`); if (chrome.placeholder !== "Search pipelines, runs and artifacts") { throw new Error(`search placeholder is wrong: ${chrome.placeholder}`); } if (!chrome.autofocused) throw new Error("the search field was not autofocused on open"); if (chrome.close !== "Close search") throw new Error(`the close control is wrong: ${chrome.close}`); const afterOpen = await viewSnapshot(); if (afterOpen.tab !== beforeOpen.tab || afterOpen.detail !== beforeOpen.detail) { throw new Error(`opening search changed the view: ${JSON.stringify({ beforeOpen, afterOpen })}`); } console.log( ` field autofocused, placeholder "${chrome.placeholder}", "${chrome.close}" present; view stayed ${afterOpen.tab}`, ); const SEARCH_QUERY = "plan"; step(`running a real query ("${SEARCH_QUERY}") and capturing the flat result list (S1)`); await page.fill("#searchInput", SEARCH_QUERY); await page.waitForSelector("#searchResults .search-row", { timeout: 25000 }); const list = await page.evaluate(`(() => ({ kinds: [...document.querySelectorAll("#searchKind option")].map((o) => o.textContent), sorts: [...document.querySelectorAll("#searchSort option")].map((o) => o.textContent), kindValue: document.querySelector("#searchKind")?.value ?? "", sortValue: document.querySelector("#searchSort")?.value ?? "", summary: document.querySelector("#searchSummary")?.textContent ?? "", clear: document.querySelector("#searchClearFilter")?.textContent ?? "", rows: document.querySelectorAll("#searchResults .search-row").length, headers: document.querySelectorAll("#searchResults h1, #searchResults h2, #searchResults h3").length, kindsPresent: [...new Set([...document.querySelectorAll("#searchResults .search-row")].map((r) => r.dataset.kind))], matchedSpans: document.querySelectorAll("#searchResults .search-match").length, }))()`); if ( JSON.stringify(list.kinds) !== JSON.stringify(["All kinds", "Pipelines", "Runs", "Steps", "Fields", "Artifacts", "Versions"]) ) { throw new Error(`kind options are wrong: ${list.kinds.join(",")}`); } if (JSON.stringify(list.sorts) !== JSON.stringify(["Relevance", "Newest first", "Name"])) { throw new Error(`sort options are wrong: ${list.sorts.join(",")}`); } if (list.kindValue !== "" || list.sortValue !== "relevance") { throw new Error(`the default controls are wrong: kind=${list.kindValue} sort=${list.sortValue}`); } if (list.summary !== "All kinds · Relevance") throw new Error(`the summary line is wrong: ${list.summary}`); if (list.clear !== "Clear filter") throw new Error(`the reset control is wrong: ${list.clear}`); if (!list.rows) throw new Error("the query rendered no result rows"); if (list.headers !== 0) throw new Error("the result list carries grouping headers, not a flat list"); if (list.kindsPresent.length < 2) { throw new Error(`the query returned only ${list.kindsPresent.join(",")}, expected a mixed list`); } console.log( ` ${list.rows} flat rows across ${list.kindsPresent.join(",")}; summary "${list.summary}"; ` + `${list.matchedSpans} emphasised snippet span(s)`, ); await page.rect("#searchPanel"); await shot("01-search-results.png"); step("applying a kind filter and capturing it (S1)"); await page.evaluate(`(() => { const select = document.querySelector("#searchKind"); select.value = "pipeline"; select.dispatchEvent(new Event("change", { bubbles: true })); })()`); await eventually( () => page.evaluate(`(() => { const rows = [...document.querySelectorAll("#searchResults .search-row")]; return rows.length > 0 && rows.every((r) => r.dataset.kind === "pipeline"); })()`), { timeout: 25000, what: "the kind filter to narrow the list to pipelines" }, ); const filteredSummary = await text("#searchSummary"); if (filteredSummary !== "Pipelines · Relevance") { throw new Error(`the filtered summary is wrong: ${filteredSummary}`); } const filteredRows = await page.evaluate(`document.querySelectorAll("#searchResults .search-row").length`); console.log(` Pipelines filter: ${filteredRows} row(s), summary "${filteredSummary}"`); await page.rect("#searchPanel"); await shot("02-search-kind-filter.png"); step("Clear filter returns the kind to All kinds"); await page.click("#searchClearFilter"); await eventually(async () => (await text("#searchSummary")) === "All kinds · Relevance", { timeout: 15000, what: "Clear filter to reset the kind", }); console.log(' Clear filter reset the kind to "All kinds"'); step("a Fields filter emphasises the matched snippet in the page, never as markup (S1)"); await page.evaluate(`(() => { const select = document.querySelector("#searchKind"); select.value = "field"; select.dispatchEvent(new Event("change", { bubbles: true })); })()`); await eventually( () => page.evaluate(`!!document.querySelector("#searchResults .search-row .search-match")`), { timeout: 25000, what: "an emphasised snippet span" }, ); const emphasis = await page.evaluate(`(() => { const node = document.querySelector("#searchResults .search-row .search-match"); return { tag: node?.tagName ?? "", inner: node?.firstElementChild?.tagName ?? "", text: node?.textContent ?? "", kind: node?.closest(".search-row")?.dataset.kind ?? "", }; })()`); if (emphasis.tag !== "STRONG" || emphasis.inner !== "U") { throw new Error(`the matched span is not : ${JSON.stringify(emphasis)}`); } if (!emphasis.text) throw new Error("the emphasised span carries no text"); if (emphasis.kind !== "field") throw new Error(`the emphasis is not on a field hit: ${emphasis.kind}`); console.log(` matched snippet emphasised as <${emphasis.tag}><${emphasis.inner}>: "${emphasis.text}"`); // Back to every kind for the routing steps. await page.evaluate(`(() => { const select = document.querySelector("#searchKind"); select.value = ""; select.dispatchEvent(new Event("change", { bubbles: true })); })()`); await eventually(() => page.evaluate(`!!document.querySelector("#searchResults .search-row")`), { timeout: 25000, what: "the unfiltered list", }); step("routing a pipeline hit to its detail (S2)"); const pipelineTarget = await routeSearchKind(SEARCH_QUERY, "pipeline", async () => { await page.waitForSelector("#detailTitle", { timeout: 25000 }); // The section is sticky across pipelines now; this step checks the Summary. await clickDetailTab("Summary"); await page.waitForSelector("#content .detail .monospace", { timeout: 25000 }); return true; }); if (!pipelineTarget) throw new Error("no pipeline result routed to a pipeline detail"); const pipelineView = await page.evaluate(`(() => ({ active: document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? "", title: document.querySelector("#detailTitle")?.textContent ?? "", searchOpen: !!document.querySelector("#searchInput"), }))()`); if (!pipelineView.active.startsWith("Pipelines")) { throw new Error(`a pipeline hit landed on ${pipelineView.active}`); } if (!pipelineView.title) throw new Error("the routed pipeline detail has no heading"); if (pipelineView.searchOpen) throw new Error("selecting a result did not close the search panel"); console.log(` ${pipelineTarget} -> Pipelines detail "${pipelineView.title}"`); await page.rect("#content .detail"); await shot("03-search-route-pipeline.png"); step("routing a run hit to its detail (S2)"); const runTarget = await routeSearchKind(SEARCH_QUERY, "run", async () => { await eventually( () => page.evaluate(`(() => { const tab = document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? ""; return tab.startsWith("Runs") && !!document.querySelector("#detailTitle"); })()`), { timeout: 25000, what: "the routed run detail" }, ); return true; }); if (!runTarget) throw new Error("no run result routed to a run detail"); const runView = await page.evaluate(`(() => ({ active: document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? "", title: document.querySelector("#detailTitle")?.textContent ?? "", tabs: [...document.querySelectorAll(".rail-tab, .strip-tab")].filter((n) => n.offsetParent !== null).map((n) => n.textContent), }))()`); if (!runView.active.startsWith("Runs")) throw new Error(`a run hit landed on ${runView.active}`); if (!runView.tabs.includes("Summary")) throw new Error(`the routed run detail has no tabs: ${runView.tabs}`); console.log(` ${runTarget} -> Runs detail "${runView.title}" (${runView.tabs.join("/")})`); await page.rect("#content .detail"); await shot("04-search-route-run.png"); step("routing a field hit to the run-detail tab holding the match (S2)"); const fieldTargets = await searchTargets(SEARCH_QUERY, "field"); let fieldRoute = null; for (const target of fieldTargets.slice(0, 4)) { // data-target is run:{id}:{sequence}:{matchedField}. const matchedField = target.split(":")[3] ?? ""; const sectionTitle = matchedField === "input" ? "Input" : matchedField === "output" ? "Output" : ""; await searchTargets(SEARCH_QUERY, "field"); if (!(await clickSearchTarget(target))) continue; const landed = await eventually( () => page.evaluate(`(() => { const tab = document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? ""; const active = [...document.querySelectorAll(".rail-tab, .strip-tab")].find((n) => n.offsetParent !== null && n.getAttribute("aria-selected") === "true")?.textContent ?? ""; const section = ${JSON.stringify(sectionTitle)}; const expanded = !section || [...document.querySelectorAll("button.section-toggle")].some((b) => { const title = b.querySelector(".section-title")?.textContent ?? ""; return b.getAttribute("aria-expanded") === "true" && title.startsWith(section); }); return tab.startsWith("Runs") && active === "Summary" && expanded; })()`), { timeout: 25000, what: `the Summary with ${sectionTitle || "the"} field section` }, ) .then(() => true) .catch(() => false); if (landed) { fieldRoute = { target, matchedField, expectedTab: "Summary", sectionTitle }; break; } } if (!fieldRoute) throw new Error("no field result opened the run-detail tab holding its match"); console.log(` ${fieldRoute.target} (matched_field ${fieldRoute.matchedField}) -> ${fieldRoute.expectedTab}${fieldRoute.sectionTitle ? ` + ${fieldRoute.sectionTitle}` : ""}`); await page.rect("#content .detail"); await shot("05-search-route-field.png"); step("routing a step hit to the visit's stage pane (S2)"); const stepTarget = await routeSearchKind(SEARCH_QUERY, "step", async () => { await eventually(() => page.evaluate(`!!document.querySelector(".stage-pane")`), { timeout: 20000, what: "the routed visit stage pane", }); return true; }); if (!stepTarget) throw new Error("no step result opened a visit stage pane"); const stepPane = await page.evaluate(`(() => ({ title: document.querySelector(".stage-pane-title")?.textContent ?? "", tab: document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? "", }))()`); if (!stepPane.title) throw new Error("the routed stage pane named no stage"); if (!stepPane.tab.startsWith("Runs")) throw new Error(`the step route landed on ${stepPane.tab}`); console.log(` ${stepTarget} -> run stage pane "${stepPane.title}"`); await page.rect(".stage-pane-sheet"); await shot("06-search-route-step.png"); await page.click(".stage-pane-back"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the step stage pane to close", }); step("routing an artifact hit opens the artifact itself (S2)"); // Query a known image artifact by a name this history actually recorded. const artifactTarget = await routeSearchKind("viewer-markdown", "artifact", async () => { // An artifact hit opens the file, not the run/visit that recorded it: media in // the viewer, everything else in a new tab. await eventually( () => page.evaluate( `!!document.querySelector(".artifact-viewer") || !!document.querySelector(".stage-pane")`, ), { timeout: 20000, what: "the routed artifact to open" }, ); return true; }); if (artifactTarget) { // The image element exists when the viewer mounts; wait for it to decode. await eventually( () => page.evaluate(`(() => { const i = document.querySelector(".artifact-viewer img"); return !!i && i.complete && i.naturalWidth > 0; })()`), { timeout: 15000, what: "the artifact viewer image to load" }, ); const artifactOpen = await page.evaluate(`({ viewer: !!document.querySelector(".artifact-viewer"), stagePane: !!document.querySelector(".stage-pane"), imageLoaded: (() => { const i = document.querySelector(".artifact-viewer img"); return !!i && i.complete && i.naturalWidth > 0; })(), })`); if (artifactOpen.stagePane || !artifactOpen.viewer || !artifactOpen.imageLoaded) { throw new Error(`an artifact hit did not open the artifact: ${JSON.stringify(artifactOpen)}`); } console.log(` ${artifactTarget} -> artifact viewer (image loaded)`); await page.rect(".artifact-viewer"); await shot("07-search-route-artifact.png"); await page.evaluate(`document.querySelector(".artifact-viewer")?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`); await eventually(() => page.evaluate(`!document.querySelector(".artifact-viewer")`), { timeout: 5000, what: "the artifact viewer to close", }); } else { console.log(" -- no artifact hit routed; skipped the artifact capture"); } step("routing a version hit to its pipeline detail (S2)"); const versionTarget = await routeSearchKind(SEARCH_QUERY, "version", async () => { await eventually( () => page.evaluate(`(() => { const tab = document.querySelector('[data-destination][aria-selected="true"]')?.textContent ?? ""; return tab.startsWith("Pipelines") && !!document.querySelector("#detailTitle"); })()`), { timeout: 25000, what: "the pipeline detail for the version" }, ); return true; }); if (versionTarget) { const versionTitle = await text("#detailTitle"); console.log(` ${versionTarget} -> Pipelines detail "${versionTitle}"`); await page.rect("#content .detail"); await shot("08-search-route-version.png"); } else { console.log(" -- no version hit routed; skipped the version capture"); } step("Closing search keeps the view and resumes the query (S1)"); // Use the same query whose field hits this run already proved; a live server's // data changes, and "release" may simply have no field hit today (nothing about // the close/resume path depends on the word). await searchTargets(SEARCH_QUERY, "field"); const beforeClose = await viewSnapshot(); await page.click("#searchClose"); await eventually(() => page.evaluate(`!document.querySelector("#searchInput")`), { timeout: 10000, what: "the search panel to close", }); const afterClose = await viewSnapshot(); if (afterClose.tab !== beforeClose.tab || afterClose.detail !== beforeClose.detail) { throw new Error(`closing search changed the view: ${JSON.stringify({ beforeClose, afterClose })}`); } // The spotlight resumes where it was left: same query, results shown again. await page.click("#searchToggle"); await page.waitForSelector("#searchInput", { timeout: 15000 }); const reopenedQuery = await page.evaluate(`document.querySelector("#searchInput")?.value ?? ""`); if (reopenedQuery !== SEARCH_QUERY) { throw new Error(`reopening did not resume the query: "${reopenedQuery}"`); } await eventually(() => page.evaluate(`document.querySelectorAll("#searchResults .search-row").length > 0`), { timeout: 20000, what: "the resumed results", }); console.log(` the spotlight resumed "${SEARCH_QUERY}" with its results`); // Ctrl+/ toggles it closed again for the following steps. await page.evaluate(`document.dispatchEvent(new KeyboardEvent("keydown", { key: "/", ctrlKey: true, bubbles: true, cancelable: true }))`); await eventually(() => page.evaluate(`!document.querySelector("#searchInput")`), { timeout: 10000, what: "Ctrl+/ to close the spotlight", }); console.log(" Ctrl+/ closed the spotlight"); await page.evaluate( `document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`, ); await eventually(() => page.evaluate(`!document.querySelector("#searchInput")`), { timeout: 10000, what: "Escape to close the search", }); console.log(" Close search cleared the query and kept the view; Escape also closed the field"); // R13/R14: the run-level actions on run 440 (failed, not executing). The // Retry action is offered; Cancel is not. The Retry confirm and its secondary // Restart-from-start confirm are captured and dismissed with Keep — no run is // started here. The rename field is captured pre-filled, then a blank rename is // actually sent: the live server refuses it without mutating anything, so the // shared Action-failed dialog shows the server's own reason. step("R13/R14: run 440 (failed) — Retry eligibility, restart confirm and the rename field"); // The search block above leaves the routed detail (a Pipelines detail) on // screen; R13/R14 needs the Runs list rendered before it can open a failed run. await clickDestination("Runs"); await page.waitForSelector(".list.runs .run-row", { timeout: 25000 }); // R13/R14 needs a failed, non-executing run; the history is the operator's, so // discover one rather than naming a row a reset may have removed. const failedFixtureRun = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-failed")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (failedFixtureRun == null) throw new Error("R13/R14 needs a failed run in the history"); await openRunDetail(failedFixtureRun); const actions440 = await page.evaluate(`(() => ({ retry: !!document.querySelector(".run-detail .run-actions .retry-run"), cancel: !!document.querySelector(".run-detail .run-actions .cancel-run"), }))()`); if (!actions440.retry) throw new Error(`run ${failedFixtureRun} (failed, not executing) offered no Retry action`); if (actions440.cancel) throw new Error(`run ${failedFixtureRun} (not executing) must not offer a Cancel action`); shots.push(await page.screenshot(`${out}/35-run-440-run-actions.png`)); step("R13: the Retry confirm — resume is the default, Restart from start is secondary"); await page.click(".run-detail .run-actions .retry-run"); await page.waitForSelector(".confirm-dialog.confirm-retry", { timeout: 15000 }); const retryDialog = await page.evaluate(`(() => ({ title: document.querySelector(".confirm-retry .confirm-title")?.textContent ?? "", message: document.querySelector(".confirm-retry .confirm-message")?.textContent ?? "", confirm: document.querySelector(".confirm-retry .confirm-accept")?.textContent ?? "", dismiss: document.querySelector(".confirm-retry .confirm-dismiss")?.textContent ?? "", secondary: document.querySelector(".confirm-retry .confirm-secondary")?.textContent ?? "", }))()`); if (retryDialog.title !== `Retry run ${failedFixtureRun}?`) throw new Error(`retry title: ${retryDialog.title}`); if (retryDialog.message !== "Only failed runs can be retried. The server starts a new run from the same input.") { throw new Error(`retry message: ${retryDialog.message}`); } if (retryDialog.confirm !== "Retry") throw new Error(`retry confirm: ${retryDialog.confirm}`); if (retryDialog.dismiss !== "Keep") throw new Error(`retry dismiss: ${retryDialog.dismiss}`); if (retryDialog.secondary !== "Restart from start") { throw new Error(`retry secondary: ${retryDialog.secondary}`); } console.log(` retry dialog: "${retryDialog.title}" — ${retryDialog.message}`); shots.push(await page.screenshot(`${out}/36-run-440-retry-confirm.png`)); step("R13: the Restart-from-start confirm states the whole-pipeline replay"); await page.click(".confirm-retry .confirm-secondary"); await page.waitForSelector(".confirm-dialog.confirm-restart", { timeout: 15000 }); const restartDialog = await page.evaluate(`(() => ({ title: document.querySelector(".confirm-restart .confirm-title")?.textContent ?? "", message: document.querySelector(".confirm-restart .confirm-message")?.textContent ?? "", confirm: document.querySelector(".confirm-restart .confirm-accept")?.textContent ?? "", dismiss: document.querySelector(".confirm-restart .confirm-dismiss")?.textContent ?? "", }))()`); if (restartDialog.title !== `Restart run ${failedFixtureRun}?`) throw new Error(`restart title: ${restartDialog.title}`); if (!restartDialog.message.includes("replays the whole pipeline")) { throw new Error(`restart message does not state the whole-pipeline replay: ${restartDialog.message}`); } if (!restartDialog.message.includes("instead of resuming the failed stage")) { throw new Error(`restart message does not contrast with resume: ${restartDialog.message}`); } if (restartDialog.confirm !== "Restart from start") { throw new Error(`restart confirm: ${restartDialog.confirm}`); } if (restartDialog.dismiss !== "Keep") throw new Error(`restart dismiss: ${restartDialog.dismiss}`); console.log(` restart dialog: "${restartDialog.title}" — ${restartDialog.message}`); shots.push(await page.screenshot(`${out}/37-run-440-restart-confirm.png`)); step("R13: dismissing the dialog with Keep sends nothing"); await page.click(".confirm-restart .confirm-dismiss"); await eventually(() => page.evaluate(`!document.querySelector(".confirm-dialog")`), { timeout: 5000, what: "the confirm dialog to close", }); step("R14: the rename field is pre-filled with the current title"); await page.click(".run-detail .rename-open"); await page.waitForSelector(".run-detail .rename-input", { timeout: 15000 }); const renameValue = await page.evaluate( `document.querySelector(".run-detail .rename-input")?.value ?? ""`, ); if (!renameValue) throw new Error("the rename field was not pre-filled with the current title"); console.log(` rename field pre-filled with: ${JSON.stringify(renameValue)}`); shots.push(await page.screenshot(`${out}/38-run-440-rename-field.png`)); step("Action failed: a refused rename shows the server's exact reason"); await page.fill(".run-detail .rename-input", ""); await page.click(".run-detail .rename-save"); await page.waitForSelector(".action-failed", { timeout: 15000 }); const actionFailed = await page.evaluate(`(() => ({ title: document.querySelector(".action-failed .action-failed-title")?.textContent ?? "", message: document.querySelector(".action-failed .action-failed-message")?.textContent ?? "", ok: document.querySelector(".action-failed .action-failed-ok")?.textContent ?? "", }))()`); if (actionFailed.title !== "Action failed") throw new Error(`action-failed title: ${actionFailed.title}`); if (actionFailed.ok !== "OK") throw new Error(`action-failed ok: ${actionFailed.ok}`); if (actionFailed.message !== "a title with usable text is required") { throw new Error(`action-failed message is not the server's own reason: ${actionFailed.message}`); } console.log(` action failed: "${actionFailed.title}" — ${actionFailed.message}`); shots.push(await page.screenshot(`${out}/39-run-440-action-failed.png`)); await page.click(".action-failed .action-failed-ok"); await eventually(() => page.evaluate(`!document.querySelector(".action-failed")`), { timeout: 5000, what: "the Action-failed dialog to close", }); // Leave the rename field without changing anything. await page.click(".run-detail .rename-cancel"); // R12: the Cancel confirm on an executing run. Shown and dismissed with Keep — // no cancel_run is sent (a real cancel is unsafe against a shared live run). step("R12: the Cancel confirm on an executing run (shown, not executed)"); const cancelRunId = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-running")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (cancelRunId == null) { console.log(" -- no executing run available; skipped the Cancel confirm capture"); } else { await openRunDetail(cancelRunId); const cancelOffered = await page.evaluate( `!!document.querySelector(".run-detail .run-actions .cancel-run")`, ); if (!cancelOffered) { console.log(` -- run ${cancelRunId} stopped before the Cancel action could be shown`); } else { await page.click(".run-detail .run-actions .cancel-run"); await page.waitForSelector(".confirm-dialog.confirm-cancel", { timeout: 15000 }); const cancelDialog = await page.evaluate(`(() => ({ title: document.querySelector(".confirm-cancel .confirm-title")?.textContent ?? "", message: document.querySelector(".confirm-cancel .confirm-message")?.textContent ?? "", confirm: document.querySelector(".confirm-cancel .confirm-accept")?.textContent ?? "", dismiss: document.querySelector(".confirm-cancel .confirm-dismiss")?.textContent ?? "", }))()`); if (cancelDialog.title !== `Cancel run ${cancelRunId}?`) { throw new Error(`cancel title: ${cancelDialog.title}`); } if (cancelDialog.message !== "This asks the server to stop the active run.") { throw new Error(`cancel message: ${cancelDialog.message}`); } if (cancelDialog.confirm !== "Cancel run") throw new Error(`cancel confirm: ${cancelDialog.confirm}`); if (cancelDialog.dismiss !== "Keep") throw new Error(`cancel dismiss: ${cancelDialog.dismiss}`); console.log(` cancel dialog (run ${cancelRunId}, not executed): "${cancelDialog.title}" — ${cancelDialog.message}`); shots.push(await page.screenshot(`${out}/40-run-${cancelRunId}-cancel-confirm.png`)); await page.click(".confirm-cancel .confirm-dismiss"); await eventually(() => page.evaluate(`!document.querySelector(".confirm-dialog")`), { timeout: 5000, what: "the Cancel dialog to close", }); } } // R11: the decision card/sheet, only when the live server already has a run // waiting on a person with recorded options. This default suite never submits // and never confirms: it captures the waiting card, the sheet and the selected // state and then dismisses with Cancel. The full live Confirm (both feedback // paths) is exercised by the opt-in `--decide` mode above. step("R11: the human-decision card/sheet (only when a waiting run is present)"); const waitingRunId = await page.evaluate(`(() => { const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-waiting")); const meta = row?.querySelector(".run-row-meta[data-run-id]"); return meta ? Number(meta.getAttribute("data-run-id")) : null; })()`); if (waitingRunId == null) { console.log(" -- no waiting run available; skipped the decision card/sheet capture (full live path covered by --decide)"); } else { await openRunDetail(waitingRunId); await page.waitForSelector(".run-detail .decision-card", { timeout: 15000 }); const cardText = await page.evaluate(`(() => ({ title: document.querySelector(".decision-card .decision-card-title")?.textContent ?? "", meta: document.querySelector(".decision-card .decision-card-meta")?.textContent ?? "", action: document.querySelector(".decision-card .decision-open")?.textContent ?? "", }))()`); if (cardText.title !== "Waiting for your decision") throw new Error(`card title: ${cardText.title}`); if (cardText.action !== "Decide") throw new Error(`card action: ${cardText.action}`); console.log(` decision card (run ${waitingRunId}): ${cardText.title} — ${cardText.meta}`); shots.push(await page.screenshot(`${out}/41-run-${waitingRunId}-decision-card.png`)); await page.click(".decision-card .decision-open"); await page.waitForSelector(".decision-sheet .decision-option", { timeout: 15000 }); const sheet = await page.evaluate(`(() => ({ title: document.querySelector(".decision-sheet-title")?.textContent ?? "", meta: document.querySelector(".decision-sheet-meta")?.textContent ?? "", label: document.querySelector(".decision-field-label")?.textContent ?? "", options: [...document.querySelectorAll(".decision-sheet .decision-option-key")].map((n) => n.textContent), confirmDisabled: document.querySelector(".decision-confirm")?.disabled ?? null, }))()`); if (sheet.label !== "Feedback (optional)") throw new Error(`sheet feedback label: ${sheet.label}`); if (sheet.confirmDisabled !== true) { throw new Error("Confirm decision must be disabled until an option is selected"); } console.log(` decision sheet (run ${waitingRunId}): ${sheet.title} — ${sheet.meta}; options ${sheet.options.join(", ")}`); shots.push(await page.screenshot(`${out}/42-run-${waitingRunId}-decision-sheet.png`)); // Selecting an option enables the confirm but sends nothing. await page.click(".decision-sheet .decision-option"); await eventually( () => page.evaluate(`document.querySelector(".decision-confirm")?.disabled === false`), { timeout: 5000, what: "the confirm to enable after selecting an option" }, ); shots.push(await page.screenshot(`${out}/43-run-${waitingRunId}-decision-selected.png`)); // Dismiss with Cancel: no decide_run is sent for a real waiting run here. await page.click(".decision-sheet .decision-cancel"); await eventually(() => page.evaluate(`!document.querySelector(".decision-sheet")`), { timeout: 5000, what: "the decision sheet to close", }); } // C1/C3/C4/C7/C14: the chat list, its transcript (markdown + the code the live // session carries), paging and the close dialog. The one live operator session // is never mutated: the close confirm is shown and dismissed with Cancel. step("C1/C3/C4/C7/C14: the chat list, transcript, paging and close dialog"); await ensureConnected(); await clickDestination("Chat"); await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 }); // The operator's own session is the one this step reads; the list may lead with // another run's closed row (C2 closes its create, and history sits by recency), // so pick the operator row rather than assuming the first. const expectedSession = "ses_eae413f50ffeLQDExlg8cYnscN"; const chatList = await page.evaluate(`(() => ({ label: document.querySelector("#chatSearch")?.getAttribute("aria-label") ?? "", placeholder: document.querySelector("#chatSearch")?.getAttribute("placeholder") ?? "", rows: [...document.querySelectorAll("#chatRows .chat-row")].map((row) => ({ id: row.dataset.session ?? "", title: row.querySelector(".chat-row-title")?.textContent ?? "", status: row.querySelector(".chat-activity")?.textContent?.trim() ?? "", directory: row.querySelector(".chat-row-meta")?.textContent ?? "", model: row.querySelector(".chat-row-model")?.textContent ?? "", star: !!row.querySelector(".chat-star"), close: !!row.querySelector(".chat-close"), closed: !!row.querySelector(".chat-activity-closed"), menu: !!row.querySelector(".chat-menu-button"), menuLabel: row.querySelector(".chat-menu-button")?.getAttribute("aria-label") ?? "", })), }))()`); if (chatList.label !== "Search chats") throw new Error(`chat search label: ${chatList.label}`); if (chatList.placeholder !== "Search chats") throw new Error(`chat search placeholder: ${chatList.placeholder}`); if (chatList.rows.length === 0) throw new Error("no chat sessions rendered"); // The list's first row is the newest session; a shared live server can carry a // closed verification row above the operator's when another client closed it, // so read the operator's own row (or the first open row) rather than rows[0]. const operatorRow = chatList.rows.find((row) => row.id === expectedSession) ?? chatList.rows.find((row) => !row.closed && row.menu); if (operatorRow) { if (!operatorRow.title) throw new Error("the operator chat row has no title"); // The row leads with the title; a live/unseen/closed row may carry a status // badge, while an ordinary idle row shows only its recency and directory. if (!["", "Streaming", "New", "Closed"].includes(operatorRow.status)) { throw new Error(`chat row status: ${JSON.stringify(operatorRow)}`); } // M6 names the control for its row: `Chat actions for {title}`, with the // session id only when titles repeat. Assert the shared prefix and title. if (!operatorRow.menu || !operatorRow.menuLabel.startsWith("Chat actions for ")) { throw new Error(`the open chat row's Chat actions menu: ${JSON.stringify(operatorRow)}`); } } console.log( ` chat list: ${chatList.rows.length} session(s); ` + (operatorRow ? `"${operatorRow.title}" (${operatorRow.status}) — ${operatorRow.directory}${operatorRow.model ? ` · ${operatorRow.model}` : ""}` : "no open row"), ); await shot("44-chat-sessions.png"); // The row's own Close chat… action with NO conversation open: the list offers // it on every open row (behind the Chat actions menu), so the confirm must // still appear. Shown and dismissed with Cancel; no chat_close is ever sent for // any live session. A shared server with no open row skips the row-level // confirm instead of failing the unrelated suite. if (!operatorRow) { console.log(" -- no open chat row on this shared server; skipped the row-level Close confirm"); } else { await openChatRowMenu(operatorRow.id); await page.click(`#chatRows .chat-row[data-session="${operatorRow.id}"] .chat-menu-close`); await page.waitForSelector(".confirm-chat-close", { timeout: 15000 }); const listClose = await page.evaluate( `document.querySelector(".confirm-chat-close .confirm-message")?.textContent ?? ""`, ); if (!listClose.startsWith('Closes "')) { throw new Error(`row-level close message with no conversation open: ${listClose}`); } await page.click(".confirm-chat-close .confirm-dismiss"); await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), { timeout: 5000, what: "the row-level close dialog to close", }); console.log( " row-level 'Close chat…' (no conversation open) showed its confirm; dismissed with Cancel", ); } // The narrowed-to-nothing empty state and its Clear search action (the field // itself is the narrowing; the list's order is never touched). await page.fill("#chatSearch", "zzz-no-such-chat"); await page.waitForText('No chats match "zzz-no-such-chat".', { timeout: 10000 }); await shot("50-chat-no-match.png"); await page.clickText(".chat-rows .empty button", "Clear search"); await page.waitForSelector("#chatRows .chat-row", { timeout: 10000 }); const clearedQuery = await page.evaluate(`document.querySelector("#chatSearch")?.value ?? null`); if (clearedQuery !== "") { throw new Error(`Clear search left the query: ${JSON.stringify(clearedQuery)}`); } // C2: create a chat through the directory picker. This step creates exactly one // REAL session (the app's own path, not the ephemeral `/new` verification path), // captures it, then closes it with `chat_close` so it moves to closed history // instead of sitting active in the operator's list. The operator's own session // is never selected, closed or prompted here. step("C2: the create pane — directory picker, one real create, then close"); await ensureConnected(); await page.evaluate(`(() => { window.__pipelinesEphemeralChat = false; window.__chatCreateBodies = []; return true; })()`); await page.click(".chat-toolbar .chat-new"); await page.waitForSelector(".chat-create", { timeout: 15000 }); await page.waitForSelector(".chat-create-choice", { timeout: 20000 }); const picker = await page.evaluate(`(() => ({ title: document.querySelector(".chat-create-title")?.textContent ?? "", back: document.querySelector(".chat-create-back")?.getAttribute("aria-label") ?? "", backTitle: document.querySelector(".chat-create-back")?.getAttribute("title") ?? "", prompt: document.querySelector(".chat-create-prompt")?.textContent ?? "", loading: !!document.querySelector(".chat-create-loading"), note: document.querySelector(".chat-create-model-note")?.textContent ?? "", start: document.querySelector(".chat-create-start")?.textContent ?? "", cancel: document.querySelector(".chat-create-cancel")?.textContent ?? "", cancelDisabled: document.querySelector(".chat-create-cancel")?.disabled ?? null, confirm: document.querySelector(".chat-create-confirm")?.textContent ?? "", confirmDisabled: document.querySelector(".chat-create-confirm")?.disabled ?? null, rows: [...document.querySelectorAll(".chat-create-choice")].map((row) => ({ label: row.querySelector(".chat-create-choice-label")?.textContent ?? "", path: row.querySelector(".chat-create-choice-path")?.textContent ?? "", directory: row.dataset.directory ?? "", selected: row.getAttribute("aria-selected") === "true", })), }))()`); if (picker.title !== "Start a chat") throw new Error(`C2 title: ${picker.title}`); if (picker.back !== "Cancel new chat" || picker.backTitle !== "Cancel new chat") { throw new Error(`C2 back label: ${JSON.stringify({ aria: picker.back, title: picker.backTitle })}`); } if (picker.prompt !== "Where should this chat live?") { throw new Error(`C2 prompt: ${picker.prompt}`); } if (picker.loading) throw new Error("C2 still shows Loading directories… after the choices rendered"); if ( picker.note !== "This server has no request for choosing a chat's model, so a new chat uses the server's default." ) { throw new Error(`C2 model note: ${picker.note}`); } if (picker.start !== "Start in ~") throw new Error(`C2 footer: ${picker.start}`); if (picker.cancel !== "Cancel") throw new Error(`C2 cancel: ${picker.cancel}`); if (picker.cancelDisabled !== false || picker.confirmDisabled !== false) { throw new Error(`C2 controls disabled before sending: ${JSON.stringify(picker)}`); } if (picker.confirm.trim() !== "New chat") throw new Error(`C2 confirm: ${picker.confirm}`); if (picker.rows.length === 0) throw new Error("C2 rendered no directory choices"); const homeRow = picker.rows[0]; if (homeRow.label !== "Home (~)" || homeRow.directory !== "~" || !homeRow.selected) { throw new Error(`C2 home row is not first and selected: ${JSON.stringify(homeRow)}`); } if (homeRow.path === "" || homeRow.path === "~") { throw new Error(`C2 home row has no absolute path beneath it: ${JSON.stringify(homeRow)}`); } const recents = picker.rows.slice(1); // A recent's own label is its full path (only the home row carries a separate // secondary path), so the data-directory value and the label must agree. if (recents.some((row) => row.label !== row.directory || row.directory === "")) { throw new Error(`C2 recent rows are not labelled by full path: ${JSON.stringify(recents)}`); } console.log( ` create pane: "${picker.title}" (back "${picker.back}"); home "${homeRow.label}" ` + `${homeRow.path} selected; ${recents.length} recent(s); footer "${picker.start}"`, ); await shot("75-chat-create-picker.png"); // Choose home explicitly (it is already the default), then create exactly one // session. The request body is recorded by the socket shim. await page.click(".chat-create-choice"); await page.evaluate(`window.__chatCreateBodies = []`); await page.click(".chat-create-confirm"); const beforeCreate = await page.evaluate( `document.querySelector(".chat-row.selected")?.dataset.session ?? ""`, ); const createdId = await eventually( async () => { const id = await page.evaluate(`document.querySelector(".chat-row.selected")?.dataset.session ?? ""`); return id && id !== beforeCreate ? id : false; }, { timeout: 30000, what: "the C2-created session to be selected" }, ); createdSessionId = createdId; // The created conversation loads its history asynchronously; sample it only once // it has settled (a loaded OpenCode endpoint can take a few seconds). const created = await eventually( () => page .evaluate(`(() => { const rows = [...document.querySelectorAll("#chatRows .chat-row")]; const row = document.querySelector('.chat-row[data-session="${createdId}"]'); return { first: rows[0]?.dataset.session ?? "", firstActive: rows.find((candidate) => !candidate.querySelector(".chat-star.starred")) ?.dataset.session ?? "", title: row?.querySelector(".chat-row-title")?.textContent ?? "", selected: row?.classList.contains("selected") ?? false, closed: !!row?.querySelector(".chat-activity-closed"), header: document.querySelector(".chat-bar-title")?.textContent ?? "", empty: document.querySelector(".chat-detail .chat-transcript-empty")?.textContent ?? "", composer: !!document.querySelector("#chatComposer"), pickerGone: !document.querySelector(".chat-create"), }; })()`) .then((value) => value.empty === "This chat has no messages yet. Ask something below." ? value : false, ), { timeout: 30000, what: "the created chat's empty conversation to load" }, ); const createBodies = await page.evaluate(`window.__chatCreateBodies || []`); if (createBodies.length !== 1) { throw new Error(`C2 sent ${createBodies.length} chat_create request(s) for one create`); } const body = createBodies[0] ?? {}; if ( body.title !== "New chat" || body.directory !== "~" || typeof body.operation_id !== "string" || body.operation_id === "" || Object.keys(body).sort().join(",") !== "directory,operation_id,title" ) { throw new Error(`C2 chat_create body: ${JSON.stringify(body)}`); } if (!created.pickerGone || !created.selected || created.closed) { throw new Error(`C2 did not close the pane onto the new conversation: ${JSON.stringify(created)}`); } if (created.firstActive !== createdId) { throw new Error( `C2 created row is not at the head of the active group: first=${created.firstActive} created=${createdId}`, ); } if (created.title !== "New chat" || created.header !== "New chat") { throw new Error(`C2 created title: ${JSON.stringify(created)}`); } if (created.empty !== "This chat has no messages yet. Ask something below." || !created.composer) { throw new Error(`C2 did not load the empty conversation: ${JSON.stringify(created)}`); } console.log( ` chat_create sent exactly {title:"New chat",directory:"~",operation_id:${JSON.stringify(body.operation_id)}}; ` + `session ${createdId} at the head of the active group, active with empty history`, ); await shot("76-chat-created-list.png"); // Close the one real session it created (chat_close through the C3 confirm, // behind C13's Chat actions menu) and capture the Closed chip. Nothing is // deleted; the row stays as closed history. await openChatRowMenu(createdId); await page.click(`#chatRows .chat-row[data-session="${createdId}"] .chat-menu-close`); await page.waitForSelector(".confirm-chat-close", { timeout: 15000 }); await page.click(".confirm-chat-close .confirm-accept"); await expandClosedChats(); await eventually( () => page.evaluate( `!!document.querySelector('.chat-row[data-session="${createdId}"] .chat-activity-closed')`, ), { timeout: 20000, what: "the C2 session to show as closed" }, ); // The confirmed close re-renders the row with its menu dismissed, so reopen // the Chat actions menu before checking that it now offers Reopen (not Close). await openChatRowMenu(createdId); const createdClosed = await page.evaluate(`(() => { const row = document.querySelector('.chat-row[data-session="${createdId}"]'); row?.scrollIntoView({ block: "center" }); return { closed: !!row?.querySelector(".chat-activity-closed") }; })()`); if (!createdClosed.closed) throw new Error(`C2 closed chip missing: ${JSON.stringify(createdClosed)}`); // C13 moved the row actions behind the Chat actions menu, so the closed row's // Reopen (and the absence of Close) is asserted with that menu open. await openChatRowMenu(createdId); const closedMenu = await page.evaluate(`(() => { const row = document.querySelector('.chat-row[data-session="${createdId}"]'); return { hasClose: !!row?.querySelector(".chat-menu-close"), hasReopen: !!row?.querySelector(".chat-menu-reopen"), }; })()`); if (closedMenu.hasClose || !closedMenu.hasReopen) { throw new Error(`C2 closed row menu: ${JSON.stringify(closedMenu)}`); } await page.click(`#chatRows .chat-row[data-session="${createdId}"] .chat-menu-button`); // Close the actions menu so the capture shows the row itself, not the panel. await page.click(`#chatRows .chat-row[data-session="${createdId}"] .chat-menu-button`); console.log( ` C2: created real session ${createdId} ("New chat" in ~) and closed it via chat_close; ` + "it now shows the Closed chip (kept as history, not deleted)", ); await shot("77-chat-created-closed.png"); createdSessionId = null; // The live session set changes between runs: the first row can be a // freshly-created empty "New chat" (which renders the empty state, not a // transcript). Prefer the operator's own session; otherwise try the listed rows // in order until one carries a real transcript, and say plainly if none does, // rather than waiting on a hard-coded session that the server no longer lists. const preferred = chatList.rows.find((row) => row.id === expectedSession); const openRows = chatList.rows .filter((row) => row.menu && row.status !== "Closed") .map((row) => row.id); const candidates = [ ...new Set([...(preferred ? [preferred.id] : []), ...openRows, ...chatList.rows.map((r) => r.id)]), ]; // Record the frames the page sends from before the first genuine open. With // more than one row the test switches away and re-opens, so the assertion below // sees that fresh open; with a server that lists only one session the first // click is itself the fresh open (clicking it again would early-return), and // clearing here would throw that real request away. await page.evaluate(`(() => { window.__pipelinesSentTypes = []; return true; })()`); let openedId = null; for (const candidate of candidates) { await page.click(`#chatRows .chat-row[data-session="${candidate}"]`); const opened = await eventually(() => page.evaluate(`!!document.querySelector("#chatTranscript")`), { timeout: 12000, what: `a transcript for ${candidate}`, }).catch(() => false); if (opened) { openedId = candidate; break; } } if (!openedId) { throw new Error("no chat session on the live server carried a transcript to review"); } if (openedId !== expectedSession) { console.log(` -- the live server's first session with history is ${openedId}, not ${expectedSession}`); } // Leave the discovered session first: the loop above already opened it, and // openChatSession keeps a loaded thread, so clicking it again would early-return // and send nothing. Opening a different row restores a genuine fresh open. With // only one listed session there is no other row, so the loop's own open above is // the fresh-open proof. const otherRow = candidates.find((id) => id !== openedId); if (otherRow) { await page.click(`#chatRows .chat-row[data-session="${otherRow}"]`); await new Promise((resolve) => setTimeout(resolve, 600)); await page.evaluate(`(() => { window.__pipelinesSentTypes = []; return true; })()`); } else { console.log(" -- the server lists one session, so the first open above is the fresh-open proof"); } await page.click(`#chatRows .chat-row[data-session="${openedId}"]`); await page.waitForSelector("#chatTranscript", { timeout: 25000 }); await eventually( () => page.evaluate(`(window.__pipelinesSentTypes || []).includes("chat_shells")`), { timeout: 15000, what: "chat_shells to be fetched when the conversation opens (C11)" }, ); console.log(" chat_shells fetched when the conversation opened (real request)"); const transcript = await page.evaluate(`(() => { const el = document.querySelector("#chatTranscript"); if (!el) return null; return { userBubbles: el.querySelectorAll(".chat-turn-user").length, agentBubbles: el.querySelectorAll(".chat-turn-agent").length, // The folding invariant that is independent of how the live session grows: // two agent bubbles must never be adjacent, because consecutive assistant // messages fold into one turn. A window that starts mid-turn may legitimately // begin with an agent bubble, so the count alone is not the invariant. adjacentAgentTurns: (() => { const turns = [...el.querySelectorAll(".chat-turn-user, .chat-turn-agent")]; let adjacent = 0; for (let i = 1; i < turns.length; i += 1) { if ( turns[i].classList.contains("chat-turn-agent") && turns[i - 1].classList.contains("chat-turn-agent") ) { adjacent += 1; } } return adjacent; })(), markdown: el.querySelectorAll(".markdown").length, tables: el.querySelectorAll(".md-table").length, inlineCode: el.querySelectorAll(".md-code").length, codeBlocks: el.querySelectorAll(".md-code-block").length, toolChips: [...el.querySelectorAll(".chat-tool-chip")].map((chip) => chip.textContent), reasoning: el.querySelectorAll(".chat-reasoning").length, client: el.querySelector(".chat-turn-user .chat-client")?.textContent ?? "", hasLoadOlder: !!document.querySelector(".chat-load-older"), }; })()`); if (!transcript) throw new Error("the opened transcript did not render"); // The live session grows as the operator uses it, so the folding invariant is // asserted directly rather than by count: every run of consecutive assistant // messages is exactly one agent bubble, which means two agent bubbles are never // adjacent. (A count such as "no more agent bubbles than user prompts" breaks as // soon as the newest page begins mid-turn, which the operator session now does.) if (transcript.userBubbles < 1) throw new Error(`expected a user bubble, got ${transcript.userBubbles}`); if (transcript.agentBubbles < 1) { throw new Error(`expected at least one folded agent turn, got ${transcript.agentBubbles}`); } if (transcript.adjacentAgentTurns !== 0) { throw new Error( `consecutive assistant messages did not fold: ${transcript.adjacentAgentTurns} adjacent ` + `agent turn pair(s) among ${transcript.agentBubbles} agent bubble(s)`, ); } if (transcript.markdown === 0) throw new Error("the agent turn rendered no markdown"); if (transcript.tables === 0) { // The live transcript's first page is whatever the operator session holds; a // table may legitimately be absent. The table path is proven below on the // page's own renderer with a fixed sample. console.log(" -- the loaded live turn carries no table; the renderer sample covers tables"); } if (transcript.inlineCode === 0) { // The live transcript is the operator's own session; an inline-code span is // not guaranteed. The renderer itself is covered by the markdown unit suite. console.log(" -- the loaded live turn carries no inline code; the renderer sample covers it"); } if (transcript.toolChips.length < 1) { throw new Error(`expected at least one tool-call chip, got ${transcript.toolChips.length}`); } console.log( ` transcript: ${transcript.userBubbles} user + ${transcript.agentBubbles} folded agent turn(s); ` + `markdown ${transcript.tables} table(s), ${transcript.inlineCode} inline code span(s), ` + `${transcript.toolChips.length} tool chip(s) ("${transcript.toolChips[0]}"); client label "${transcript.client}"`, ); await shot("45-chat-transcript-newest.png"); // The top of the conversation, reached by a genuine operator wheel gesture on // the transcript (the app's own listener). Only the operator's gesture changes // follow intent — a programmatic scrollTo cannot — so this is the only way // "Jump to latest" may appear; the control is asserted, not assumed. const wheelGesture = await page.evaluate(`(() => { const el = document.querySelector("#chatTranscript"); if (!el) return null; el.scrollTop = 0; el.dispatchEvent(new WheelEvent("wheel", { deltaY: -240, bubbles: true })); return { jumpHidden: document.querySelector("#chatJump")?.hidden ?? null, top: el.scrollTop, }; })()`); if (!wheelGesture || wheelGesture.jumpHidden !== false) { throw new Error(`Jump to latest was not offered after a real wheel gesture: ${JSON.stringify(wheelGesture)}`); } await shot("46-chat-transcript-top.png"); console.log( ` after a real wheel gesture (scrollTop ${wheelGesture.top}) 'Jump to latest' is offered at the top`, ); // The live assistant answer carries markdown but no fenced block: the session's // fenced code lives in its reasoning, which the app renders as plain text // (Markdown.kt draws reasoning with a plain Text, never the markdown subset). if (transcript.reasoning > 0) { await page.evaluate(`document.querySelector("#chatTranscript .chat-reasoning-toggle")?.click()`); await page.waitForSelector(".chat-reasoning-viewer", { timeout: 5000 }); const expanded = await page.evaluate(`(() => { const viewer = document.querySelector(".chat-reasoning-viewer"); const body = viewer?.querySelector(".chat-reasoning-modal-body"); if (!body) return null; const fence = String.fromCharCode(96).repeat(3); const text = body.textContent ?? ""; const index = text.indexOf(fence); // Bring the fenced block into the popup's view, not the transcript's. if (index >= 0 && body.firstChild && body.firstChild.nodeType === 3) { const range = document.createRange(); range.setStart(body.firstChild, index); range.setEnd(body.firstChild, index + fence.length); const rect = range.getBoundingClientRect(); body.scrollTop += Math.round(rect.top - body.getBoundingClientRect().top) - 40; } return { label: viewer.querySelector(".chat-tool-panel-count")?.textContent ?? "", hasFence: index >= 0, length: text.length, }; })()`); if (expanded) { console.log(` reasoning popup "${expanded.label}" (${expanded.length} chars); carries a fenced block: ${expanded.hasFence}`); } await shot("47-chat-reasoning-code.png"); await page.evaluate(`document.querySelector(".chat-reasoning-viewer .chat-tool-close")?.click()`); await eventually(() => page.evaluate(`!document.querySelector(".chat-reasoning-viewer")`), { timeout: 5000, what: "the reasoning popup to close", }); } // C7's code-block path. The live assistant answer carries no fenced block (its // fences live in the reasoning transcript, drawn as plain text — capture 47), // so exercise the page's own markdown renderer on a labelled sample and capture // the rendered
 block; the sample is removed again straight after.
  const fence = String.fromCharCode(96).repeat(3);
  const codeSample = `${fence}js\nconst answer = 42;\n${fence}\n\n| name | value |\n| --- | --- |\n| alpha | 1 |\n| beta | 2 |`;
  const codeBlock = await page.evaluate(`(async () => {
    const mod = await import("/src/app.js");
    const node = mod.renderMarkdown(${JSON.stringify(codeSample)});
    node.id = "markdownCodeSample";
    const wrap = document.querySelector(".chat-detail .chat-transcript-wrap");
    if (!wrap || !wrap.parentNode) return null;
    wrap.parentNode.insertBefore(node, wrap);
    return {
      label: node.querySelector(".md-code-label")?.textContent ?? "",
      hasPre: !!node.querySelector("pre.md-pre code"),
      text: node.querySelector("pre.md-pre code")?.textContent ?? "",
      tableRows: node.querySelectorAll("table tr").length,
    };
  })()`);
  if (!codeBlock || !codeBlock.hasPre) {
    throw new Error("the page's markdown renderer drew no fenced code block");
  }
  if (codeBlock.label !== "js") throw new Error(`code block label: ${codeBlock.label}`);
  if (codeBlock.tableRows < 3) {
    throw new Error(`the markdown renderer drew no table from the sample: ${codeBlock.tableRows} rows`);
  }
  console.log(
    `  markdown code block (page renderer sample): label "${codeBlock.label}", text ${JSON.stringify(codeBlock.text)}`,
  );
  await shot("47b-chat-markdown-code.png");
  await page.evaluate(`document.querySelector("#markdownCodeSample")?.remove()`);

  // C6: the turn's reasoning block and its tool-call chip + panel. The live
  // operator session carries one folded agent turn with real reasoning and tool
  // parts, so the chip, the panel and the rows are exercised against live data.
  // Labels, aria names and the C14 scroll-invariance are asserted, not assumed.
  step("C6: the reasoning block and the tool-call chip + panel");
  const reasoningNow = await page.evaluate(`(() => {
    const block = document.querySelector("#chatTranscript .chat-reasoning");
    if (!block) return null;
    const toggle = block.querySelector(".chat-reasoning-toggle");
    const icon = block.querySelector(".chat-reasoning-icon");
    return {
      label: block.querySelector(".chat-reasoning-label")?.textContent ?? "",
      aria: icon?.getAttribute("aria-label") ?? "",
      hasPopup: toggle?.getAttribute("aria-haspopup") ?? "",
      inlineBody: !!block.querySelector(".chat-reasoning-body"),
    };
  })()`);
  if (!reasoningNow) throw new Error("C6: no reasoning row rendered in the live transcript");
  if (reasoningNow.aria !== "Open reasoning" || reasoningNow.hasPopup !== "dialog") {
    throw new Error(`C6: the reasoning control should open a dialog: ${JSON.stringify(reasoningNow)}`);
  }
  if (reasoningNow.inlineBody) throw new Error("C6: reasoning still expands inline");
  if (!/^Reasoning( · \d+ steps)?/.test(reasoningNow.label)) {
    throw new Error(`C6: settled reasoning label: ${reasoningNow.label}`);
  }
  await page.evaluate(`document.querySelector("#chatTranscript .chat-reasoning-toggle").click()`);
  await page.waitForSelector(".chat-reasoning-viewer", { timeout: 5000 });
  const reasoningPopup = await page.evaluate(`(() => {
    const viewer = document.querySelector(".chat-reasoning-viewer");
    return {
      title: viewer?.querySelector(".chat-tool-panel-count")?.textContent ?? "",
      body: viewer?.querySelector(".chat-reasoning-modal-body")?.textContent ?? "",
      close: viewer?.querySelector(".chat-tool-close")?.getAttribute("aria-label") ?? "",
    };
  })()`);
  if (!reasoningPopup.title.startsWith("Reasoning") || reasoningPopup.body.length === 0 || reasoningPopup.close !== "Close reasoning") {
    throw new Error(`C6: reasoning popup: ${JSON.stringify({ ...reasoningPopup, body: reasoningPopup.body.length })}`);
  }
  console.log(`  reasoning popup "${reasoningPopup.title}" (${reasoningPopup.body.length} chars, close "${reasoningPopup.close}")`);
  await shot("51-chat-reasoning-popup.png");
  await page.evaluate(`document.querySelector(".chat-reasoning-viewer .chat-tool-close").click()`);
  await eventually(() => page.evaluate(`!document.querySelector(".chat-reasoning-viewer")`), {
    timeout: 5000,
    what: "the reasoning popup to close",
  });

  // The chip: its count, its screen-reader announcement and its chevron. The
  // count is the number of rows the panel carries, so the two can never disagree.
  const chipInfo = await page.evaluate(`(() => {
    const chip = document.querySelector("#chatTranscript .chat-tool-chip");
    if (!chip) return null;
    chip.scrollIntoView({ block: "center" });
    // The rows belong to *this* chip's own panel: the live session may carry more
    // than one agent turn, and a document-wide count would sum every panel.
    const wrap = chip.closest(".chat-tools");
    return {
      text: chip.textContent ?? "",
      aria: chip.getAttribute("aria-label") ?? "",
      chevron: chip.querySelector(".chat-tool-chevron")?.getAttribute("aria-label") ?? "",
      rows: wrap?.querySelectorAll(".chat-tool-panel .chat-tool-row").length ?? 0,
    };
  })()`);
  if (!chipInfo) throw new Error("C6: no tool-call chip rendered in the live transcript");
  const parsedCount = Number(/(\d+)/.exec(chipInfo.text)?.[1] ?? NaN);
  const toolCount = Number.isFinite(parsedCount) ? parsedCount : chipInfo.rows;
  const toolLabel = toolCount === 1 ? "1 tool" : `${toolCount} tools`;
  const toolAria =
    toolCount === 1 ? "1 tool call, expand for details" : `${toolCount} tool calls, expand for details`;
  if (!chipInfo.text.includes(toolLabel)) {
    throw new Error(`C6: the chip should read "${toolLabel}", got ${JSON.stringify(chipInfo.text)}`);
  }
  if (chipInfo.aria !== toolAria) {
    throw new Error(`C6: chip aria-label ${chipInfo.aria} (expected ${toolAria})`);
  }
  if (chipInfo.chevron !== "Open tool calls") {
    throw new Error(`C6: collapsed chip chevron: ${chipInfo.chevron}`);
  }
  console.log(`  tool chip "${toolLabel}" (aria "${chipInfo.aria}", ${toolCount} live row(s))`);
  await shot("53-chat-tool-chip.png");

  // Open the panel by dispatching the chip's own click (not a synthetic scroll),
  // then assert the header, the close control and the C14 invariant: opening the
  // panel must not move the transcript behind it.
  const scrollBeforePanel = await page.evaluate(
    `Math.round(document.querySelector("#chatTranscript").scrollTop)`,
  );
  const openedPanel = await page.evaluate(`(() => {
    const chip = document.querySelector("#chatTranscript .chat-tool-chip");
    chip.click();
    const panel = document.querySelector(".chat-tools-viewer .chat-tool-panel");
    return {
      open: !!document.querySelector(".chat-tools-viewer"),
      head: document.querySelector(".chat-tools-viewer .chat-tool-panel-count")?.textContent ?? "",
      close: document.querySelector(".chat-tools-viewer .chat-tool-close")?.getAttribute("aria-label") ?? "",
      rows: panel?.querySelectorAll(".chat-tool-row").length ?? 0,
    };
  })()`);
  if (!openedPanel.open) throw new Error("C6: the tool modal did not open");
  if (openedPanel.head !== toolLabel) throw new Error(`C6: panel header ${openedPanel.head}`);
  if (openedPanel.close !== "Close tool calls") {
    throw new Error(`C6: panel close control ${openedPanel.close}`);
  }
  if (openedPanel.rows !== toolCount) {
    throw new Error(`C6: the panel lists ${openedPanel.rows} rows, the chip counts ${toolCount}`);
  }
  const scrollAfterOpen = await page.evaluate(
    `Math.round(document.querySelector("#chatTranscript").scrollTop)`,
  );
  if (Math.abs(scrollAfterOpen - scrollBeforePanel) > 2) {
    throw new Error(`C6: opening the tool panel moved the transcript (${scrollBeforePanel} -> ${scrollAfterOpen})`);
  }
  console.log(
    `  tool panel open: header "${openedPanel.head}", close "Close tool calls", ${openedPanel.rows} row(s)`,
  );

  // Expand the first row in place and assert its chevron name, its INPUT and its
  // OUTPUT (the live first call is a settled `read` carrying both).
  const firstToolRow = await page.evaluate(`(() => {
    const chip = document.querySelector("#chatTranscript .chat-tool-chip");
    const panel = document.querySelector(".chat-tools-viewer .chat-tool-panel");
    const list = panel.querySelector(".chat-tool-list");
    list.scrollTop = 0;
    const first = () => panel.querySelector(".chat-tool-row-head");
    const name = first().querySelector(".chat-tool-name")?.textContent ?? "";
    const chevronBefore = first().querySelector(".chat-tool-row-chevron")?.getAttribute("aria-label") ?? "";
    first().click();
    const head = first();
    const row = head.closest(".chat-tool-row");
    return {
      name,
      chevronBefore,
      chevronAfter: head.querySelector(".chat-tool-row-chevron")?.getAttribute("aria-label") ?? "",
      sections: [...row.querySelectorAll(".chat-tool-section-label")].map((n) => n.textContent),
      bodies: [...row.querySelectorAll(".chat-tool-section-body")].map((n) => n.textContent),
    };
  })()`);
  if (firstToolRow.chevronBefore !== `Expand ${firstToolRow.name}`) {
    throw new Error(`C6: collapsed row chevron ${firstToolRow.chevronBefore} for ${firstToolRow.name}`);
  }
  if (firstToolRow.chevronAfter !== `Collapse ${firstToolRow.name}`) {
    throw new Error(`C6: expanded row chevron ${firstToolRow.chevronAfter} for ${firstToolRow.name}`);
  }
  if (!firstToolRow.sections.includes("OUTPUT")) {
    throw new Error(`C6: first row sections ${JSON.stringify(firstToolRow.sections)}`);
  }
  if (!firstToolRow.sections.includes("INPUT")) {
    // The live operator turn's first call may record only its output.
    console.log("  --  this live tool call carries no INPUT section; OUTPUT is present");
  }
  if (firstToolRow.bodies.some((body) => body.trim() === "")) {
    throw new Error(`C6: a first-row section rendered blank: ${JSON.stringify(firstToolRow.bodies)}`);
  }
  console.log(
    `  first row "${firstToolRow.name}": ${firstToolRow.sections.join(" + ")} (chevron "${firstToolRow.chevronAfter}")`,
  );
  await shot("54-chat-tool-panel.png");

  // Closing it must put the transcript back exactly where it was.
  const closedPanel = await page.evaluate(`(() => {
    const chip = document.querySelector("#chatTranscript .chat-tool-chip");
    document.querySelector(".chat-tools-viewer .chat-tool-close")?.click();
    return {
      closed: !document.querySelector(".chat-tools-viewer"),
      chevron: chip?.querySelector(".chat-tool-chevron")?.getAttribute("aria-label"),
      top: Math.round(document.querySelector("#chatTranscript").scrollTop),
    };
  })()`);
  if (!closedPanel.closed || closedPanel.chevron !== "Open tool calls") {
    throw new Error(`C6: closing the tool modal: ${JSON.stringify(closedPanel)}`);
  }
  if (Math.abs(closedPanel.top - scrollBeforePanel) > 2) {
    throw new Error(`C6: opening/closing the tool panel moved the transcript (${scrollBeforePanel} -> ${closedPanel.top})`);
  }
  console.log(
    `  the tool panel left the transcript where it was across open/close (top ${scrollBeforePanel} -> ${closedPanel.top})`,
  );

  // The live history carries no failed call and no edit-family call, so render
  // both through the page's own row builder (the 47b precedent): a failed row
  // marked "Failed tool call" with an ERROR section, and an edit row with the LCS
  // diff and its true "+N -M" stat. The sample is removed straight after.
  const sampleRows = await page.evaluate(`(async () => {
    const app = await import("/src/app.js");
    const host = document.createElement("div");
    host.id = "chatToolSample";
    host.className = "chat-tool-sample";
    const note = document.createElement("div");
    note.className = "chat-tool-sample-note";
    note.textContent =
      "Page-rendered sample — the live history carries no failed or edit call " +
      "(the chip, panel and rows above are live)";
    host.append(note);
    const failedPart = {
      type: "tool", id: "sample-failed", name: "shell",
      state: { status: "error", input: { command: "false" }, error: { message: "exit status 1" } },
    };
    const failedRow = app.renderToolCallRow(failedPart, { expanded: true });
    host.append(failedRow);
    const editPart = {
      type: "tool", id: "sample-edit", name: "edit",
      state: {
        status: "completed",
        input: { filePath: "web/src/chat.js", oldString: "const a = 1;\\nconst b = 2;", newString: "const a = 1;\\nconst b = 3;\\nconst c = 4;" },
        content: [{ type: "text", text: "applied" }],
      },
      time: { created: 1, ran: 2, completed: 103 },
    };
    const editRow = app.renderToolCallRow(editPart, { expanded: true });
    host.append(editRow);
    document.body.append(host);
    return {
      failedAria: failedRow.querySelector('.chat-tool-icon[aria-label="Failed tool call"]') ? "Failed tool call" : "",
      failedSections: [...failedRow.querySelectorAll(".chat-tool-section-label")].map((n) => n.textContent),
      editStat: editRow.querySelector(".chat-tool-stat")?.textContent ?? "",
      diffLines: [...editRow.querySelectorAll(".chat-tool-diff-line")].map((n) => ({ cls: n.className, text: n.textContent })),
    };
  })()`);
  if (!sampleRows) throw new Error("C6: could not render the page-rendered tool-row samples");
  if (sampleRows.failedAria !== "Failed tool call") {
    throw new Error(`C6: the failed row is not marked 'Failed tool call': ${JSON.stringify(sampleRows)}`);
  }
  if (!sampleRows.failedSections.includes("INPUT") || !sampleRows.failedSections.includes("ERROR")) {
    throw new Error(`C6: failed row sections ${JSON.stringify(sampleRows.failedSections)}`);
  }
  if (sampleRows.editStat !== "+2 -1") {
    throw new Error(`C6: edit diff stat ${sampleRows.editStat}`);
  }
  if (
    !sampleRows.diffLines.some((line) => line.cls.includes("diff-remove") && line.text === "- const b = 2;") ||
    !sampleRows.diffLines.some((line) => line.cls.includes("diff-add") && line.text === "+ const b = 3;")
  ) {
    throw new Error(`C6: the edit diff lines: ${JSON.stringify(sampleRows.diffLines)}`);
  }
  console.log(
    `  page-rendered samples: failed row "Failed tool call" (${sampleRows.failedSections.join(" + ")}), ` +
      `edit row stat ${sampleRows.editStat} with ${sampleRows.diffLines.length} diff line(s)`,
  );
  await shot("55-chat-edit-diff-sample.png");
  await page.evaluate(`document.querySelector("#chatToolSample")?.remove()`);

  if (transcript.hasLoadOlder) {
    // The control was present in capture 45. Press it and assert the paging
    // invariant that does not depend on how large the operator's live session has
    // grown: an older page either prepends turns (the control stays while the
    // server still reports has_more) or exhausts the history (the control
    // disappears). Turns must never shrink, and the control must eventually
    // settle within a bounded number of presses.
    const readPaging = () =>
      page.evaluate(`({
        present: !!document.querySelector(".chat-load-older"),
        turns: document.querySelectorAll("#chatTranscript .chat-turn-user, #chatTranscript .chat-turn-agent").length,
      })`);
    let paging = await readPaging();
    const startTurns = paging.turns;
    let presses = 0;
    while (paging.present && presses < 5) {
      await page.evaluate(`document.querySelector(".chat-load-older")?.click()`);
      // The control is disabled while the page is in flight (thread.loading), so
      // "settled" means it is gone or enabled again — independent of whether the
      // older page added a visible turn.
      await new Promise((resolve) => setTimeout(resolve, 300));
      await eventually(
        () =>
          page.evaluate(
            `(() => { const b = document.querySelector(".chat-load-older"); return !b || !b.disabled; })()`,
          ),
        { timeout: 15000, what: "an older history page to settle" },
      );
      paging = await readPaging();
      if (paging.turns < startTurns) {
        throw new Error(`load older shrank the transcript: ${startTurns} -> ${paging.turns}`);
      }
      presses += 1;
    }
    await shot("48-chat-load-older.png");
    console.log(
      `  'Load older messages' was shown while has_more (capture 45); ${presses} press(es) ` +
        `${paging.turns > startTurns ? `prepended ${paging.turns - startTurns} turn(s)` : "loaded no more turns"}; ` +
        `${paging.present ? "older pages still remain (bounded check stopped)" : "history exhausted and the control disappeared"}`,
    );
  } else {
    console.log("  --  no 'Load older messages' control: the server did not report has_more");
  }

  // The close confirm: shown, then dismissed with Cancel. The operator's only
  // live session is never mutated, so no chat_close is sent. The transcript is
  // mid-scroll (following false), so this also asserts the C14 fix: opening and
  // dismissing the dialog must not move the operator's view.
  const beforeDialog = await page.evaluate(`(() => {
    const el = document.querySelector("#chatTranscript");
    return {
      top: el ? Math.round(el.scrollTop) : null,
      jumpHidden: document.querySelector("#chatJump")?.hidden ?? null,
    };
  })()`);
  if (!beforeDialog || beforeDialog.top == null || beforeDialog.top === 0) {
    throw new Error(`the transcript was not mid-scroll before the dialog: ${JSON.stringify(beforeDialog)}`);
  }
  await openChatRowMenu(openedId);
  await page.click(`#chatRows .chat-row[data-session="${openedId}"] .chat-menu-close`);
  await page.waitForSelector(".confirm-chat-close", { timeout: 15000 });
  const closeDialog = await page.evaluate(`(() => ({
    title: document.querySelector(".confirm-chat-close .confirm-title")?.textContent ?? "",
    message: document.querySelector(".confirm-chat-close .confirm-message")?.textContent ?? "",
    confirm: document.querySelector(".confirm-chat-close .confirm-accept")?.textContent ?? "",
    dismiss: document.querySelector(".confirm-chat-close .confirm-dismiss")?.textContent ?? "",
  }))()`);
  if (closeDialog.title !== "Close chat?") throw new Error(`close title: ${closeDialog.title}`);
  if (!closeDialog.message.startsWith("Closes \"")) throw new Error(`close message: ${closeDialog.message}`);
  if (!closeDialog.message.includes("marks it closed on every device")) {
    throw new Error(`close message: ${closeDialog.message}`);
  }
  if (!closeDialog.message.includes("Nothing is deleted")) {
    throw new Error(`close message: ${closeDialog.message}`);
  }
  if (closeDialog.confirm !== "Close chat") throw new Error(`close confirm: ${closeDialog.confirm}`);
  if (closeDialog.dismiss !== "Cancel") throw new Error(`close dismiss: ${closeDialog.dismiss}`);
  const whileDialog = await page.evaluate(`(() => {
    const el = document.querySelector("#chatTranscript");
    return {
      top: el ? Math.round(el.scrollTop) : null,
      jumpHidden: document.querySelector("#chatJump")?.hidden ?? null,
    };
  })()`);
  // The dialog must not move the view or change what the scroll affordance offers:
  // compare against the state captured immediately before opening it (the earlier
  // wheel-gesture step already proves the control is offered when it should be).
  if (
    Math.abs((whileDialog.top ?? -1) - beforeDialog.top) > 2 ||
    whileDialog.jumpHidden !== beforeDialog.jumpHidden
  ) {
    throw new Error(
      `opening the close dialog moved the transcript: ${JSON.stringify(beforeDialog)} -> ${JSON.stringify(whileDialog)}`,
    );
  }
  console.log(`  close dialog (shown, NOT executed): "${closeDialog.title}" — ${closeDialog.message}`);
  console.log(`  the transcript stayed put across the dialog (top ${beforeDialog.top} -> ${whileDialog.top})`);
  await shot("49-chat-close-confirm.png");
  await page.click(".confirm-chat-close .confirm-dismiss");
  await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), {
    timeout: 5000,
    what: "the close dialog to close",
  });
  const afterDialog = await page.evaluate(`(() => {
    const el = document.querySelector("#chatTranscript");
    return el ? Math.round(el.scrollTop) : null;
  })()`);
  if (afterDialog == null || Math.abs(afterDialog - beforeDialog.top) > 2) {
    throw new Error(`dismissing the close dialog moved the transcript: ${beforeDialog.top} -> ${afterDialog}`);
  }
  console.log(
    "  the mutating confirm was NOT executed: no chat_close was sent for the operator's live session " +
      `${openedId}`,
  );

  // C8/C9: the composer — its draft, the slash-command palette, an unknown-command
  // notice, the attachment chips and the Stop control while a turn is running. No
  // prompt is ever sent to the operator's live session: the running-turn capture
  // uses a freshly created throwaway session (via `/new`), which is closed again
  // in cleanup. Every other state is reached by driving the composer without
  // submitting.
  step("C8/C9: the composer, slash commands and attachments");
  await ensureConnected();
  await page.waitForSelector("#chatInput", { timeout: 15000 });
  // The previous sections may have left a replayed turn marked running for the
  // operator's session. This section tests the composer's default state, so settle
  // any such turn once (a real pushed "ended" frame, through the page's own fold)
  // rather than asserting against leftover state.
  if (await page.evaluate(`!!document.querySelector("#chatStop")`)) {
    const settled = await page.evaluate(`(() => {
      const ws = window.__pipelinesSocket;
      const sessionId = document.querySelector(".chat-row.selected")?.dataset.session ?? "";
      if (!ws || typeof ws.onmessage !== "function" || !sessionId) return false;
      ws.onmessage({ data: JSON.stringify({
        type: "chat_event",
        chat_event: { session_id: sessionId, kind: "step", step: { status: "ended" } },
      }) });
      return true;
    })()`);
    if (!settled) throw new Error("C8: could not settle the leftover running turn");
    await eventually(() => page.evaluate(`!document.querySelector("#chatStop")`), {
      timeout: 10000,
      what: "the leftover turn to settle before the composer checks",
    });
    console.log("  settled a leftover running turn before the composer checks");
  }

  // (a) A draft: the placeholder, Send present and enabled, Stop absent.
  await page.fill("#chatInput", "A draft from the smoke");
  const draftState = await page.evaluate(`(() => ({
    placeholder: document.querySelector("#chatInput")?.getAttribute("placeholder") ?? "",
    send: !!document.querySelector("#chatSend"),
    sendDisabled: document.querySelector("#chatSend")?.disabled ?? null,
    sendLabel: document.querySelector("#chatSend")?.getAttribute("aria-label") ?? "",
    stop: !!document.querySelector("#chatStop"),
  }))()`);
  if (draftState.placeholder !== "Message the agent… or / for commands") {
    throw new Error(`composer placeholder: ${draftState.placeholder}`);
  }
  if (!draftState.send || draftState.sendDisabled !== false || draftState.sendLabel !== "Send") {
    throw new Error(`composer Send control: ${JSON.stringify(draftState)}`);
  }
  if (draftState.stop) throw new Error("the composer shows Stop while no turn is running");
  console.log(`  composer draft: placeholder "${draftState.placeholder}", Send enabled, no Stop`);
  await showComposer();
  await shot("56-chat-composer-draft.png");

  // (b) The command palette for a bare "/": all five commands, the first selected.
  await page.fill("#chatInput", "/");
  await page.waitForSelector(".chat-command-palette .chat-command-row", { timeout: 10000 });
  const palette = await page.evaluate(`(() => ({
    hidden: document.querySelector(".chat-command-palette")?.hidden ?? null,
    rows: [...document.querySelectorAll(".chat-command-palette .chat-command-row")].map((row) => ({
      name: row.querySelector(".chat-command-name")?.textContent ?? "",
      summary: row.querySelector(".chat-command-summary")?.textContent ?? "",
      selected: row.getAttribute("aria-selected"),
    })),
  }))()`);
  const expectedCommands = [
    ["/new", "Start a fresh session in this directory"],
    ["/close", "Finish this session for every client, keeping its history"],
    ["/rename", "Rename this session on this device"],
    ["/stop", "Stop the turn that is running"],
    ["/help", "List every command"],
  ];
  if (palette.hidden || palette.rows.length !== expectedCommands.length) {
    throw new Error(`command palette: ${JSON.stringify(palette)}`);
  }
  palette.rows.forEach((row, index) => {
    if (row.name !== expectedCommands[index][0] || row.summary !== expectedCommands[index][1]) {
      throw new Error(`palette row ${index}: ${JSON.stringify(row)}`);
    }
    if (row.selected !== (index === 0 ? "true" : "false")) {
      throw new Error(`palette row ${index} selected: ${row.selected}`);
    }
  });
  console.log(`  command palette: ${palette.rows.map((row) => row.name).join(" ")}`);
  await showComposer();
  await shot("57-chat-command-palette.png");

  // Up/Down move the highlight; Enter runs the highlighted row.
  const highlight = await page.evaluate(`(() => {
    const input = document.querySelector("#chatInput");
    const key = (name) => input.dispatchEvent(new KeyboardEvent("keydown", { key: name, bubbles: true, cancelable: true }));
    key("ArrowDown");
    key("ArrowDown");
    return [...document.querySelectorAll(".chat-command-palette .chat-command-row")].map((row) => row.getAttribute("aria-selected"));
  })()`);
  if (highlight[2] !== "true" || highlight[0] !== "false") {
    throw new Error(`ArrowDown did not move the highlight: ${JSON.stringify(highlight)}`);
  }
  console.log("  ArrowDown moved the highlighted command row (Enter runs it)");
  await showComposer();
  await shot("58-chat-command-palette-selected.png");

  // Type a prefix that narrows the list case-insensitively.
  await page.fill("#chatInput", "/ST");
  const narrowedCommands = await page.evaluate(
    `[...document.querySelectorAll(".chat-command-palette .chat-command-row .chat-command-name")].map((n) => n.textContent)`,
  );
  if (narrowedCommands.length !== 1 || narrowedCommands[0] !== "/stop") {
    throw new Error(`prefix filter: ${JSON.stringify(narrowedCommands)}`);
  }

  // (c) C12: local command notices on this (read-only) conversation. A `/help`
  // success line and an unknown command's failed line render full width after the
  // turns, never as message bubbles; both are local, so the recorded send
  // assertions below must show no chat_prompt for either. The failed one keeps
  // Android's `Send as message` / `Cancel` pair; an offerless line is dismissible
  // by its own `Dismiss` control (the browser completion recorded in PARITY).
  await page.evaluate(`(() => {
    window.__pipelinesSentTypes = [];
    window.__chatPromptTexts = [];
    return true;
  })()`);
  const runSlashDraft = async (text) => {
    await page.fill("#chatInput", text);
    await page.evaluate(
      `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
    );
  };

  await runSlashDraft("/help");
  const helpNotice = await eventually(
    () =>
      page.evaluate(`(() => {
        const node = [...document.querySelectorAll("#chatTranscript .chat-command-notice:not(.chat-command-notice-failed)")]
          .find((n) => (n.querySelector(".chat-command-notice-text")?.textContent ?? "").includes("/help — List every command"));
        if (!node) return null;
        node.scrollIntoView({ block: "center" });
        const transcript = document.querySelector("#chatTranscript");
        const children = [...transcript.children];
        const index = children.indexOf(node);
        const lastTurn = children.reduce(
          (last, child, i) => (child.matches(".chat-turn-user, .chat-turn-agent") ? i : last),
          -1,
        );
        return {
          id: node.dataset.noticeId ?? "",
          label: node.querySelector(".chat-command-notice-label")?.textContent ?? "",
          glyph: node.querySelector(".chat-command-notice-glyph")?.textContent ?? "",
          text: node.querySelector(".chat-command-notice-text")?.textContent ?? "",
          buttons: [...node.querySelectorAll(".chat-command-notice-actions button")].map((b) => b.textContent),
          dismissLabel: node.querySelector(".chat-command-dismiss")?.getAttribute("aria-label") ?? "",
          bubble:
            node.classList.contains("chat-turn-user") ||
            node.classList.contains("chat-turn-agent") ||
            !!node.closest(".chat-bubble"),
          width: Math.round(node.getBoundingClientRect().width),
          contentWidth: Math.round(transcript.clientWidth - 24),
          afterTurns: index > lastTurn,
          turns: children.filter((child) => child.matches(".chat-turn-user, .chat-turn-agent")).length,
        };
      })()`),
    { timeout: 10000, what: "the /help command notice" },
  );
  if (helpNotice.label !== "Command") throw new Error(`/help notice label: ${helpNotice.label}`);
  if (helpNotice.glyph !== ">_") throw new Error(`/help notice glyph: ${helpNotice.glyph}`);
  if (!helpNotice.text.startsWith("/new — Start a fresh session in this directory")) {
    throw new Error(`/help notice text: ${helpNotice.text}`);
  }
  if (!helpNotice.text.includes("/help — List every command")) {
    throw new Error(`/help notice is missing its last command line: ${helpNotice.text}`);
  }
  if (helpNotice.buttons.join("|") !== "Dismiss") {
    throw new Error(`offerless notice controls: ${JSON.stringify(helpNotice.buttons)}`);
  }
  if (helpNotice.dismissLabel !== "Dismiss command notice") {
    throw new Error(`dismiss accessible name: ${helpNotice.dismissLabel}`);
  }
  if (helpNotice.bubble || !helpNotice.afterTurns || helpNotice.turns < 1) {
    throw new Error(
      `the /help line is not a full-width transcript line after the turns: ${JSON.stringify(helpNotice)}`,
    );
  }
  if (helpNotice.width < helpNotice.contentWidth - 3) {
    throw new Error(
      `the /help line is not full width: ${helpNotice.width}px of ${helpNotice.contentWidth}px`,
    );
  }
  const promptless = await page.evaluate(
    `({ types: window.__pipelinesSentTypes || [], texts: window.__chatPromptTexts || [] })`,
  );
  if (promptless.types.includes("chat_prompt") || promptless.texts.length !== 0) {
    throw new Error(`/help reached the prompt path: ${JSON.stringify(promptless)}`);
  }
  console.log(
    `  /help: "${helpNotice.label}" line (${helpNotice.width}px of ${helpNotice.contentWidth}px, ` +
      `${helpNotice.turns} turn(s) above it), no bubble; only a Dismiss control; no chat_prompt sent`,
  );
  await showChatNotice();
  await shot("72-chat-command-notice.png");

  // The unknown command's failed line: the app's wording, the shared terminal
  // glyph and the error tint, with the offer's exact control pair.
  await runSlashDraft("/nope");
  await page.waitForSelector(".chat-command-notice-failed", { timeout: 10000 });
  const notice = await page.evaluate(`(() => {
    const node = document.querySelector(".chat-command-notice-failed");
    node.scrollIntoView({ block: "center" });
    return {
      id: node.dataset.noticeId ?? "",
      label: node.querySelector(".chat-command-notice-label")?.textContent ?? "",
      glyph: node.querySelector(".chat-command-notice-glyph")?.textContent ?? "",
      text: node.querySelector(".chat-command-notice-text")?.textContent ?? "",
      buttons: [...node.querySelectorAll(".chat-command-notice-actions button")].map((b) => b.textContent),
      failed: node.classList.contains("chat-command-notice-failed"),
      draft: document.querySelector("#chatInput")?.value ?? null,
      send: !!document.querySelector("#chatSend"),
      stop: !!document.querySelector("#chatStop"),
    };
  })()`);
  if (notice.label !== "Command failed") throw new Error(`notice label: ${notice.label}`);
  if (notice.glyph !== ">_") throw new Error(`failed notice glyph: ${notice.glyph}`);
  if (
    notice.text !==
    "“/nope” is not a command, so nothing was sent to the agent. Send it as a message, or cancel."
  ) {
    throw new Error(`unknown-command notice text: ${notice.text}`);
  }
  if (notice.buttons.join("|") !== "Send as message|Cancel") {
    throw new Error(`unknown-command offer buttons: ${JSON.stringify(notice.buttons)}`);
  }
  if (!notice.failed) throw new Error("the failed notice does not carry the error styling class");
  if (notice.draft !== "") throw new Error(`the command did not consume the draft: ${JSON.stringify(notice.draft)}`);
  const tint = await page.evaluate(`(() => {
    const failedNode = document.querySelector('.chat-command-notice-failed[data-notice-id="${notice.id}"]');
    const okNode = document.querySelector('.chat-command-notice[data-notice-id="${helpNotice.id}"]');
    return {
      failedBorder: failedNode ? getComputedStyle(failedNode).borderColor : "",
      okBorder: okNode ? getComputedStyle(okNode).borderColor : "",
    };
  })()`);
  if (!tint.failedBorder || tint.failedBorder === tint.okBorder) {
    throw new Error(`the failed notice is not tinted differently from a command line: ${JSON.stringify(tint)}`);
  }
  const stillPromptless = await page.evaluate(`window.__chatPromptTexts || []`);
  if (stillPromptless.length !== 0) {
    throw new Error(`an unknown command reached the prompt path: ${JSON.stringify(stillPromptless)}`);
  }
  console.log(`  unknown command: "${notice.text}" (offer: ${notice.buttons.join(" / ")})`);
  console.log(
    `  both command lines stayed local: no chat_prompt was sent (recorded types: ${JSON.stringify(
      (await page.evaluate(`window.__pipelinesSentTypes || []`)).filter((type) => type.startsWith("chat_")),
    )})`,
  );
  await showChatNotice();
  await shot("59-chat-command-unknown.png");
  await showChatNotice();
  await shot("73-chat-command-failed-offer.png");

  // Dismissal removes exactly the named notice: the offerless `/help` line goes,
  // the failed offer notice stays with its draft and controls.
  const dismissed = await page.evaluate(`(() => {
    const help = document.querySelector('.chat-command-notice[data-notice-id="${helpNotice.id}"]');
    const idsBefore = [...document.querySelectorAll("#chatTranscript .chat-command-notice")].map(
      (node) => node.dataset.noticeId,
    );
    help?.querySelector(".chat-command-dismiss")?.click();
    return { idsBefore, clicked: !!help };
  })()`);
  if (!dismissed.clicked || !dismissed.idsBefore.includes(helpNotice.id)) {
    throw new Error(`the /help notice was not dismissed: ${JSON.stringify(dismissed)}`);
  }
  await eventually(
    () =>
      page.evaluate(
        `!document.querySelector('.chat-command-notice[data-notice-id="${helpNotice.id}"]')`,
      ),
    { timeout: 5000, what: "the dismissed /help notice to be gone" },
  );
  const noticeRemoval = await page.evaluate(`(() => ({
    ids: [...document.querySelectorAll("#chatTranscript .chat-command-notice")].map((node) => node.dataset.noticeId),
    failed: document.querySelector('.chat-command-notice-failed[data-notice-id="${notice.id}"] .chat-command-notice-text')?.textContent ?? "",
    buttons: [...document.querySelectorAll('.chat-command-notice-failed[data-notice-id="${notice.id}"] .chat-command-notice-actions button')].map((b) => b.textContent),
  }))()`);
  if (
    noticeRemoval.ids.includes(helpNotice.id) ||
    !noticeRemoval.ids.includes(notice.id) ||
    noticeRemoval.ids.length !== dismissed.idsBefore.length - 1 ||
    noticeRemoval.failed !== notice.text ||
    noticeRemoval.buttons.join("|") !== "Send as message|Cancel"
  ) {
    throw new Error(`dismissing one notice changed the wrong ones: ${JSON.stringify({ dismissed, noticeRemoval })}`);
  }
  console.log(
    `  dismissed only ${helpNotice.id} (${dismissed.idsBefore.length} -> ${noticeRemoval.ids.length} notices; ` +
      `${notice.id} and its offer remain)`,
  );
  await showChatNotice();
  await shot("74-chat-command-dismissed.png");
  await page.clickText(`.chat-command-notice-failed[data-notice-id="${notice.id}"] button`, "Cancel");
  await eventually(
    () => page.evaluate(`!document.querySelector('.chat-command-notice[data-notice-id="${notice.id}"]')`),
    { timeout: 5000, what: "the unknown-command notice to be dismissed" },
  );

  // (d) Attachment chips: a small file with its size, and a file over the 25 MiB
  // limit with its error. Driving the real file input (a DataTransfer) exercises
  // the page's own resolve path; nothing is sent. An unreadable-file error cannot
  // be produced through a browser file picker, so that branch is unit-tested only
  // and stated in PARITY.md.
  await page.fill("#chatInput", "see the attachment");
  const attached = await page.evaluate(`(() => {
    const input = document.querySelector("#chatFileInput");
    if (!input) return false;
    const transfer = new DataTransfer();
    transfer.items.add(new File(["hello"], "notes.txt", { type: "text/plain" }));
    transfer.items.add(new File([new Uint8Array(${26 * 1024 * 1024})], "huge.bin", { type: "application/octet-stream" }));
    input.files = transfer.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
    return true;
  })()`);
  if (!attached) throw new Error("the composer has no file input");
  await page.waitForSelector(".chat-attachment-chip", { timeout: 10000 });
  const chips = await page.evaluate(`(() => {
    document.querySelector("#chatComposer")?.scrollIntoView({ block: "end" });
    return {
      chips: [...document.querySelectorAll(".chat-attachment-chip")].map((chip) => ({
        name: chip.querySelector(".chat-attachment-name")?.textContent ?? "",
        meta: chip.querySelector(".chat-attachment-meta")?.textContent ?? "",
        error: chip.querySelector(".chat-attachment-error")?.textContent ?? "",
        failed: chip.classList.contains("chat-attachment-chip-failed"),
        remove: chip.querySelector(".chat-attachment-remove")?.getAttribute("aria-label") ?? "",
      })),
      sendDisabled: document.querySelector("#chatSend")?.disabled ?? null,
    };
  })()`);
  if (chips.chips.length !== 2) throw new Error(`attachment chips: ${JSON.stringify(chips)}`);
  if (chips.chips[0].name !== "notes.txt" || chips.chips[0].meta !== "5 B" || chips.chips[0].failed) {
    throw new Error(`small attachment chip: ${JSON.stringify(chips.chips[0])}`);
  }
  if (chips.chips[0].remove !== "Remove notes.txt") {
    throw new Error(`attachment remove label: ${chips.chips[0].remove}`);
  }
  if (
    !chips.chips[1].failed ||
    chips.chips[1].error !== '"huge.bin" is too large (25.0 MiB limit).'
  ) {
    throw new Error(`oversized attachment chip: ${JSON.stringify(chips.chips[1])}`);
  }
  if (chips.sendDisabled !== true) {
    throw new Error("a failed attachment must block sending");
  }
  console.log(
    `  attachment chips: "${chips.chips[0].name}" (${chips.chips[0].meta}) + a failed ` +
      `"${chips.chips[1].name}" (${chips.chips[1].error}); Send disabled`,
  );
  await showComposer();
  await shot("60-chat-attachment-chips.png");

  // Removing the failed chip unblocks Send (the draft is non-blank).
  await page.evaluate(
    `document.querySelectorAll(".chat-attachment-chip")[1]?.querySelector(".chat-attachment-remove")?.click()`,
  );
  await eventually(
    () => page.evaluate(`document.querySelectorAll(".chat-attachment-chip").length === 1`),
    { timeout: 5000, what: "the failed attachment to be removed" },
  );
  const afterRemove = await page.evaluate(`(() => ({
    sendDisabled: document.querySelector("#chatSend")?.disabled ?? null,
    chips: document.querySelectorAll(".chat-attachment-chip").length,
  }))()`);
  if (afterRemove.sendDisabled !== false) {
    throw new Error(`Send should be enabled after removing the failed chip: ${JSON.stringify(afterRemove)}`);
  }
  console.log("  removing the failed attachment re-enabled Send");
  await showComposer();
  await shot("61-chat-attachment-removed.png");
  await page.evaluate(
    `document.querySelector(".chat-attachment-remove")?.click()`,
  );
  await eventually(() => page.evaluate(`document.querySelectorAll(".chat-attachment-chip").length === 0`), {
    timeout: 5000,
    what: "the last attachment to be removed",
  });
  await page.fill("#chatInput", "");

  // (e) `/new` creates a fresh throwaway session (C2's chat_create, sent directly
  // until C2 lands) and opens it. The operator's session is left untouched. The
  // throwaway must stay a listed server session: C12's notice check switches away
  // to another row and back to it, and C13 reads its row for the device-local
  // rename; an ephemeral create (2fe6301's `__pipelinesEphemeralChat`) is hidden
  // from `chat_sessions`, so it is deliberately NOT used here. The session is
  // closed again in cleanup, like the New-marker throwaway C13 creates itself.
  await page.fill("#chatInput", "/new");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
  );
  // The opened conversation, not the list row, is the durable proof: an
  // ephemeral session has no listed row, and the list refresh that follows the
  // create removes it, so the selected row is only briefly present and a poll can
  // miss it. The transcript's own session id stays for as long as the pane is open.
  const throwawayId = await eventually(
    async () => {
      const id = await page.evaluate(`document.querySelector("#chatTranscript")?.dataset?.session ?? ""`);
      return id && id !== openedId ? id : false;
    },
    { timeout: 25000, what: "the new throwaway session to open" },
  );
  await page.waitForSelector("#chatInput", { timeout: 10000 });
  const throwawayTitle = await page.evaluate(`document.querySelector(".chat-bar-title")?.textContent ?? ""`);
  // A given server build usually echoes the title back; when it does not, the app
  // falls back to the session id (sessionTitle), which is honest and still proves
  // the pane opened on the created session.
  if (throwawayTitle !== "New chat" && !throwawayTitle.startsWith("ses_")) {
    throw new Error(`new session title: ${throwawayTitle}`);
  }
  throwawaySessionId = throwawayId;
  const startedNotice = await page.evaluate(
    `document.querySelector(".chat-command-notice .chat-command-notice-text")?.textContent ?? ""`,
  );
  if (!startedNotice.startsWith("Started a new session “")) {
    throw new Error(`/new notice: ${startedNotice}`);
  }
  // Only the throwaway is a verification chat: clear the marker so C2's own
  // create (which must appear in the list as its acceptance) is a normal chat.
  await page.evaluate(`window.__pipelinesEphemeralChat = false`);
  console.log(`  /new opened throwaway session ${throwawayId} ("${throwawayTitle}") — ${startedNotice}`);
  await showComposer();
  await shot("62-chat-new-session.png");

  // C12: notices belong to one conversation and survive switching away and back.
  // The offer notice is added on this throwaway session (never the operator's),
  // then the pane switches to the operator session and back. Finally the offer is
  // taken: the notice is gone before the prompt is sent, a stale second tap sends
  // nothing, and the draft goes through the ordinary chat_prompt path. The shim
  // answers that one prompt locally so this step does not add a live turn; the
  // real prompt below still exercises the live turn and Stop.
  await page.evaluate(`(() => {
    window.__pipelinesSentTypes = [];
    window.__chatPromptTexts = [];
    return true;
  })()`);
  await page.fill("#chatInput", "/nope");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
  );
  const offerNotice = await eventually(
    () =>
      page.evaluate(`(() => {
        const node = document.querySelector('#chatTranscript[data-session="${throwawayId}"] .chat-command-notice-failed');
        if (!node) return null;
        return {
          id: node.dataset.noticeId ?? "",
          text: node.querySelector(".chat-command-notice-text")?.textContent ?? "",
          buttons: [...node.querySelectorAll(".chat-command-notice-actions button")].map((b) => b.textContent),
        };
      })()`),
    { timeout: 10000, what: "the throwaway command notice" },
  );
  if (offerNotice.buttons.join("|") !== "Send as message|Cancel") {
    throw new Error(`throwaway notice controls: ${JSON.stringify(offerNotice.buttons)}`);
  }
  const throwawayPromptless = await page.evaluate(`window.__chatPromptTexts || []`);
  if (throwawayPromptless.length !== 0) {
    throw new Error(`the unknown command on the throwaway reached the prompt path: ${JSON.stringify(throwawayPromptless)}`);
  }

  await page.evaluate(`document.querySelector('#chatRows .chat-row[data-session="${openedId}"]')?.click()`);
  await page.waitForSelector(`#chatTranscript[data-session="${openedId}"]`, { timeout: 25000 });
  const leaked = await page.evaluate(
    `!!document.querySelector('.chat-command-notice[data-notice-id="${offerNotice.id}"]')`,
  );
  if (leaked) throw new Error("the throwaway's notice leaked into the operator's conversation");
  await page.evaluate(`document.querySelector('#chatRows .chat-row[data-session="${throwawayId}"]')?.click()`);
  await page.waitForSelector(`#chatTranscript[data-session="${throwawayId}"]`, { timeout: 25000 });
  const survived = await eventually(
    () =>
      page.evaluate(`(() => {
        const node = document.querySelector('#chatTranscript[data-session="${throwawayId}"] .chat-command-notice[data-notice-id="${offerNotice.id}"]');
        if (!node) return null;
        return {
          text: node.querySelector(".chat-command-notice-text")?.textContent ?? "",
          buttons: [...node.querySelectorAll(".chat-command-notice-actions button")].map((b) => b.textContent),
        };
      })()`),
    { timeout: 15000, what: "the throwaway notice after switching away and back" },
  );
  if (survived.text !== offerNotice.text || survived.buttons.join("|") !== "Send as message|Cancel") {
    throw new Error(`the notice did not survive the switch: ${JSON.stringify({ offerNotice, survived })}`);
  }
  console.log(
    `  the notice survived a switch away and back on ${throwawayId} (absent from the operator's conversation)`,
  );

  const offerClicked = await page.evaluate(`(() => {
    const node = document.querySelector('#chatTranscript[data-session="${throwawayId}"] .chat-command-notice[data-notice-id="${offerNotice.id}"]');
    const button = node?.querySelector(".chat-command-send-as-message") ?? null;
    window.__staleOfferButton = button;
    window.__promptCountBeforeOffer = (window.__pipelinesSentTypes || []).length;
    window.__pipelinesIntercept = (frame) =>
      frame.type === "chat_prompt" ? { id: frame.id, type: "chat_prompt", chat_queued: false } : null;
    button?.click();
    return !!button;
  })()`);
  if (!offerClicked) throw new Error("the throwaway notice has no Send as message control");
  await eventually(
    () =>
      page.evaluate(
        `!document.querySelector('#chatTranscript[data-session="${throwawayId}"] .chat-command-notice[data-notice-id="${offerNotice.id}"]')`,
      ),
    { timeout: 10000, what: "the taken offer notice to be removed" },
  );
  const offeredSend = await page.evaluate(`(() => {
    const types = (window.__pipelinesSentTypes || []).slice(window.__promptCountBeforeOffer);
    return { chatPrompts: types.filter((type) => type === "chat_prompt").length, texts: window.__chatPromptTexts || [] };
  })()`);
  if (offeredSend.chatPrompts !== 1 || offeredSend.texts.length !== 1 || offeredSend.texts[0] !== "/nope") {
    throw new Error(`the offer did not take the ordinary prompt path: ${JSON.stringify(offeredSend)}`);
  }
  const staleTap = await page.evaluate(`(() => {
    const before = (window.__pipelinesSentTypes || []).length;
    window.__staleOfferButton?.click();
    return {
      before,
      after: (window.__pipelinesSentTypes || []).length,
      texts: (window.__chatPromptTexts || []).length,
    };
  })()`);
  if (staleTap.after !== staleTap.before || staleTap.texts !== 1) {
    throw new Error(`a stale Send as message tap sent something: ${JSON.stringify(staleTap)}`);
  }
  console.log(
    "  Send as message removed the notice first, then sent /nope once through chat_prompt; " +
      "a stale second tap sent nothing",
  );
  // Settle the (locally answered) prompt so the real turn below starts from Send,
  // and drop the one-shot prompt interceptor.
  const settledOffer = await page.evaluate(`(() => {
    const ws = window.__pipelinesSocket;
    if (!ws || typeof ws.onmessage !== "function") return false;
    ws.onmessage({
      data: JSON.stringify({
        type: "chat_event",
        chat_event: { session_id: "${throwawayId}", kind: "step", step: { status: "ended" } },
      }),
    });
    return true;
  })()`);
  if (!settledOffer) throw new Error("the socket shim did not expose the live socket to settle the offered prompt");
  await eventually(() => page.evaluate(`!document.querySelector("#chatStop")`), {
    timeout: 10000,
    what: "the offered prompt's running state to settle",
  });
  await page.evaluate(`(() => { window.__pipelinesIntercept = null; return true; })()`);

  // A real prompt to the throwaway session turns the Send control into Stop.
  await page.fill("#chatInput", "Reply with exactly one word: hi");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", ctrlKey: true, bubbles: true, cancelable: true }))`,
  );
  await page.waitForSelector("#chatStop", { timeout: 20000 });
  const running = await page.evaluate(`(() => ({
    stopLabel: document.querySelector("#chatStop")?.getAttribute("aria-label") ?? "",
    stopTitle: document.querySelector("#chatStop")?.getAttribute("title") ?? "",
    send: !!document.querySelector("#chatSend"),
    draft: document.querySelector("#chatInput")?.value ?? null,
  }))()`);
  if (running.stopLabel !== "Stop" || running.stopTitle !== "Stop" || !running.send) {
    throw new Error(`the Stop and queued-Send controls: ${JSON.stringify(running)}`);
  }
  if (running.draft !== "") throw new Error(`the sent draft was not cleared: ${JSON.stringify(running.draft)}`);
  console.log("  a real prompt kept Send available and added the Stop control");
  await showComposer();
  await shot("63-chat-composer-stop.png");

  // The Stop control must really interrupt (chat_interrupt), not fall through the
  // inert submit path: click it and observe the page's own interrupting notice,
  // with no Action failed dialog. The throwaway turn is the only one stopped.
  await page.click("#chatStop");
  const stopNotice = await eventually(
    async () => {
      const notices = await page.evaluate(
        `[...document.querySelectorAll(".chat-command-notice:not(.chat-command-notice-failed) .chat-command-notice-text")].map((node) => node.textContent)`,
      );
      return notices.includes("Stopping the running turn…") ? "Stopping the running turn…" : false;
    },
    { timeout: 15000, what: "the interrupt notice after clicking Stop" },
  );
  const stopFailed = await page.evaluate(
    `document.querySelector(".action-failed .action-failed-message")?.textContent ?? ""`,
  );
  if (stopFailed !== "") throw new Error(`clicking Stop failed: ${stopFailed}`);
  console.log(`  clicking Stop sent chat_interrupt: "${stopNotice}"`);
  await showComposer();
  await shot("63b-chat-stop-interrupt.png");

  // Cleanup: `/close` the throwaway through the C3 confirm. It is the only session
  // this stage mutates and it is closed again (any in-flight turn is stopped).
  await page.fill("#chatInput", "/close");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
  );
  await page.waitForSelector(".confirm-chat-close", { timeout: 15000 });
  const throwawayClose = await page.evaluate(
    `document.querySelector(".confirm-chat-close .confirm-message")?.textContent ?? ""`,
  );
  if (!throwawayClose.startsWith("Closes ")) {
    throw new Error(`throwaway close message: ${throwawayClose}`);
  }
  await page.click(".confirm-chat-close .confirm-accept");
  await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), {
    timeout: 15000,
    what: "the throwaway close dialog to close",
  });
  await expandClosedChats();
  await eventually(
    () =>
      page.evaluate(
        `!!document.querySelector('.chat-row[data-session="${throwawayId}"] .chat-activity-closed')`,
      ),
    { timeout: 15000, what: "the throwaway session to show as closed" },
  );
  await openChatRowMenu(throwawayId);
  const closedRow = await page.evaluate(`(() => {
    const row = document.querySelector('.chat-row[data-session="${throwawayId}"]');
    row?.scrollIntoView({ block: "center" });
    return {
      closed: !!row?.querySelector(".chat-activity-closed"),
      items: [...row.querySelectorAll(".chat-menu-panel .chat-menu-item")].map((b) => b.textContent),
      hasClose: !!row?.querySelector(".chat-menu-close"),
      hasReopen: !!row?.querySelector(".chat-menu-reopen"),
    };
  })()`);
  if (!closedRow.closed || !closedRow.hasReopen || closedRow.hasClose) {
    throw new Error(`the closed row's Chat actions menu: ${JSON.stringify(closedRow)}`);
  }
  if (closedRow.items.join("|") !== "Rename…|Reopen|Hide on this device") {
    throw new Error(`the closed row's menu items and order: ${JSON.stringify(closedRow.items)}`);
  }
  // Close the menu again (a second press on its own button toggles it shut).
  await page.evaluate(
    `document.querySelector('.chat-row[data-session="${throwawayId}"] .chat-menu-button')?.click()`,
  );
  await eventually(
    () => page.evaluate(`!document.querySelector('.chat-row[data-session="${throwawayId}"] .chat-menu-panel')`),
    { timeout: 5000, what: "the closed row's menu to close" },
  );
  // Closing finishes the session: the conversation leaves the pane (back to the
  // list with the select hint) and no row stays selected, as Android's
  // finishSession does.
  const afterThrowawayClose = await page.evaluate(`(() => ({
    selected: !!document.querySelector(".chat-row.selected"),
    hint: document.querySelector(".chat-detail .empty .muted")?.textContent ?? "",
    composer: !!document.querySelector("#chatComposer"),
  }))()`);
  if (
    afterThrowawayClose.selected ||
    afterThrowawayClose.composer ||
    afterThrowawayClose.hint !== "Select a chat to open it."
  ) {
    throw new Error(`/close did not leave the conversation: ${JSON.stringify(afterThrowawayClose)}`);
  }
  console.log(`  /close closed the throwaway session ${throwawayId} (${JSON.stringify(closedRow)}) and left the pane`);
  await shot("64-chat-throwaway-closed.png");
  throwawaySessionId = null;

  // C5: the shared live stream. A real live turn needs a prompt the operator's
  // server may not be running, so the real page is subscribed to the real
  // session and then exact `chat_event` frames are replayed through the socket's
  // own `onmessage` (see the shim installed before the page's scripts). The
  // subscribe ack and every history reply are real server traffic; only the
  // frames replayed below are synthetic, and this log says so explicitly.
  step("C5: the live stream — subscribe, replayed chat_event frames, stream state");
  await ensureConnected();
  // The composer section above closes its throwaway session and returns to the
  // list, so no conversation is open here. Re-open the operator's own session to
  // observe its live stream — read-only: the `chat_subscribe` and every history
  // fetch are real, and the frames below are injected into the page's own socket
  // handler rather than sent to the server.
  const chatOpen = await page.evaluate(`!!document.querySelector("#chatInput")`);
  if (!chatOpen) {
    await page.waitForSelector(`#chatRows .chat-row[data-session="${openedId}"]`, { timeout: 15000 });
    await page.click(`#chatRows .chat-row[data-session="${openedId}"]`);
    await page.waitForSelector("#chatInput", { timeout: 15000 });
    // The newest history page arrives asynchronously and its render replaces the
    // held messages, so wait for it to land (`#chatTranscript` is rendered only
    // once the thread is loaded) before replaying frames into the page.
    await page.waitForSelector("#chatTranscript", { timeout: 20000 });
  }
  await page.evaluate(`document.querySelector("#chatJump")?.click()`);

  // The chat pane carries no stream badge (the shell's Connected status is the one
  // indicator); the subscription itself is proven by the replayed frames landing.
  const chatBadge = () => page.evaluate(`document.querySelector("#shellStatus")?.textContent ?? ""`);
  const chatLive = await chatBadge();
  if (!chatLive.includes("Connected")) {
    throw new Error(`the shell should read Connected while the chat is subscribed, got ${JSON.stringify(chatLive)}`);
  }
  console.log(`  shell status while chat-subscribed: "${chatLive}" (real chat_subscribe ack + real history)`);

  const sessionId = openedId;
  const replayFrames = (frames) =>
    page.evaluate(`(() => {
      const ws = window.__pipelinesSocket;
      if (!ws || typeof ws.onmessage !== "function") return false;
      for (const frame of ${JSON.stringify(frames)}) {
        ws.onmessage({ data: JSON.stringify({ type: "chat_event", chat_event: frame }) });
      }
      return true;
    })()`);
  const agentCount = () => page.evaluate(`document.querySelectorAll("#chatTranscript .chat-turn-agent").length`);
  const activity = () =>
    page.evaluate(`(() => {
      const el = [...document.querySelectorAll("#chatTranscript .chat-activity")].at(-1);
      if (!el) return null;
      return {
        spinner: !!el.querySelector(".chat-activity-spinner"),
        label: el.querySelector(".chat-reasoning-label")?.textContent ?? "",
        phase: el.querySelector(".chat-activity-phase")?.textContent ?? "",
        elapsed: el.querySelector(".chat-activity-elapsed")?.textContent ?? "",
        chip: el.querySelector(".chat-tool-chip")?.textContent ?? "",
        current: el.querySelector(".chat-tool-current")?.textContent ?? "",
        long: el.querySelector(".chat-tool-long")?.textContent ?? "",
        indicator: !!document.querySelector("#chatTranscript .chat-activity"),
      };
    })()`);

  const applyFrames = async (frames, label) => {
    const ok = await replayFrames(frames);
    if (!ok) throw new Error(`C5: the socket shim did not expose the live socket to replay ${label}`);
    await eventually(() => page.evaluate(`!!document.querySelector("#chatTranscript")`), {
      what: "the transcript to render",
    });
  };

  const agentBefore = await agentCount();
  const sessionFrame = (extra) => ({ session_id: sessionId, ...extra });

  // 1. Reasoning and text deltas on one assistant message id: the answer grows
  //    inside the existing agent bubble.
  await applyFrames(
    [
      sessionFrame({ message_id: "cap-live-1", kind: "reasoning", part: { id: "cap-live-1#reasoning#0" }, delta: "Planning the live capture." }),
      sessionFrame({ message_id: "cap-live-1", kind: "text", part: { id: "cap-live-1#text#0" }, delta: "The live turn is " }),
      sessionFrame({ message_id: "cap-live-1", kind: "text", part: { id: "cap-live-1#text#0" }, delta: "growing." }),
    ],
    "the reasoning/text deltas",
  );
  const liveTurn = await page.evaluate(`(() => {
    const transcript = document.querySelector("#chatTranscript");
    const bubbles = [...transcript.querySelectorAll(".chat-turn-agent")];
    const answer = bubbles.find((b) => b.textContent.includes("The live turn is growing."));
    return {
      agents: bubbles.length,
      answers: bubbles.filter((b) => b.textContent.includes("The live turn is growing.")).length,
      reasoning: bubbles.some((b) => b.textContent.includes("Planning the live capture.")),
      oneBubble: !!(answer && answer.textContent.includes("Planning the live capture.")),
    };
  })()`);
  console.log(
    `  replayed 3 frames (reasoning + 2 text deltas) → ${liveTurn.answers} agent bubble carries ` +
      `"The live turn is growing." + reasoning (transcript agent bubbles ${agentBefore} -> ${liveTurn.agents})`,
  );
  // The live session may already end with an agent turn (in which case the replay
  // grows it) or with a user turn (in which case it starts one new bubble); either
  // way the replayed deltas are one reply and never fragment.
  const grewBy = liveTurn.agents - agentBefore;
  if (liveTurn.answers !== 1 || !liveTurn.reasoning || !liveTurn.oneBubble || grewBy < 0 || grewBy > 1) {
    throw new Error(`C5: the replayed deltas did not grow one agent bubble: ${JSON.stringify(liveTurn)}`);
  }
  await shot("56-chat-live-turn.png");

  // 2. A running tool: the indicator's headline is `Running a tool…` with a
  //    running-tool line and a live `· {elapsed}`.
  await applyFrames(
    [sessionFrame({ message_id: "cap-live-1", kind: "tool", part: { type: "tool", id: "cap-tool-1", name: "shell", state: { status: "running", input: { command: "npm run build" } } } })],
    "the running tool",
  );
  await new Promise((resolve) => setTimeout(resolve, 1300));
  const runningChip = await activity();
  if (
    !runningChip ||
    !runningChip.spinner ||
    !runningChip.current.includes("shell") ||
    !runningChip.current.includes("npm run build") ||
    runningChip.indicator
  ) {
    throw new Error(`C5: the activity line is wrong: ${JSON.stringify(runningChip)}`);
  }
  console.log(`  one activity line: "${runningChip.label || runningChip.phase}" · "${runningChip.current}" ${runningChip.elapsed}`);
  await shot("57-chat-running.png");

  // 3. A second running tool that started 61 s ago (protocol `time.ran`): the
  //    `· long` mark, tinted with the error hue. Two assistant message ids are in
  //    play now and the turn is still one bubble.
  await applyFrames(
    [
      sessionFrame({
        message_id: "cap-live-2",
        kind: "reasoning",
        part: { id: "cap-live-2#reasoning#0" },
        delta: "Streaming the second step.",
      }),
      sessionFrame({
        message_id: "cap-live-2",
        kind: "tool",
        part: { type: "tool", id: "cap-tool-2", name: "shell", state: { status: "running", input: { command: "npm run long" } }, time: { ran: Date.now() - 61000 } },
      }),
    ],
    "the long-running tool",
  );
  const longCall = await activity();
  if (!longCall?.current.includes("npm run long") || !longCall.long.includes("long")) {
    throw new Error(`C5: the long-running tool is not on the count line: ${JSON.stringify(longCall)}`);
  }
  const agentsWithTwoIds = await agentCount();
  console.log(`  long-call chip: "${longCall.current}${longCall.long}"`);
  if (agentsWithTwoIds - agentBefore > 1) {
    throw new Error(`C5: two assistant message ids fragmented the live turn into ${agentsWithTwoIds} bubbles`);
  }
  await shot("58-chat-long-call.png");

  // 4. Finish the tools and end the step: the one activity line settles in place
  //    (no spinner) and the same single agent bubble stands.
  await applyFrames(
    [
      sessionFrame({ message_id: "cap-live-1", kind: "tool", part: { type: "tool", id: "cap-tool-1", state: { status: "completed", content: [{ type: "text", text: "build ok" }] } } }),
      sessionFrame({ message_id: "cap-live-2", kind: "tool", part: { type: "tool", id: "cap-tool-2", state: { status: "completed", content: [{ type: "text", text: "long ok" }] } } }),
      sessionFrame({ message_id: "cap-live-2", kind: "step", step: { status: "ended" } }),
    ],
    "the completed tools and the ended step",
  );
  const settled = await page.evaluate(`(() => {
    const transcript = document.querySelector("#chatTranscript");
    const bubbles = [...transcript.querySelectorAll(".chat-turn-agent")];
    const answer = bubbles.find((b) => b.textContent.includes("The live turn is growing."));
    return {
      indicator: !!document.querySelector("#chatTranscript .chat-activity"),
      activity: !!document.querySelector("#chatTranscript .chat-activity"),
      spinner: !!document.querySelector("#chatTranscript .chat-activity-spinner"),
      agents: bubbles.length,
      answers: bubbles.filter((b) => b.textContent.includes("The live turn is growing.")).length,
      // The one bubble that carries the live answer must also carry the replayed
      // reasoning and the two tool rows: proof the turn never fragmented.
      settledOneBubble: !!answer && answer.textContent.includes("Planning the live capture."),
    };
  })()`);
  if (settled.indicator) throw new Error("C5: a second running indicator survived the ended step");
  if (!settled.activity || settled.spinner) {
    throw new Error(`C5: the one activity line did not settle in place: ${JSON.stringify(settled)}`);
  }
  if (settled.agents - agentBefore > 1 || settled.answers !== 1 || !settled.settledOneBubble) {
    throw new Error(`C5: the settled turn is not the same single agent bubble: ${JSON.stringify(settled)}`);
  }
  console.log(
    `  after the tools completed and the step ended: one settled activity line, still ${settled.agents} agent bubble ` +
      `carrying the live answer (unchanged from ${agentBefore})`,
  );
  await shot("59-chat-settled.png");

  // The stream-state words. Dropping the socket unexpectedly reads `reconnecting`
  // while the client comes back; the reconnect re-subscribes the open chat and
  // refreshes its history wholesale, so the replayed turn above is gone — which is
  // exactly the promised wholesale replacement.
  await page.evaluate(`window.__pipelinesSocket && window.__pipelinesSocket.close(4000, "smoke drop")`);
  await eventually(
    () => page.evaluate(`document.querySelector("#shellStatus")?.getAttribute("data-state") !== "connected"`),
    { timeout: 10000, what: "the shell to leave the connected state" },
  );
  console.log(`  chat stream badge after an unexpected drop: "${await chatBadge()}"`);
  await ensureConnected(30000);
  await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 30000 });
  // The badge turns live as soon as the subscription is re-attached; the history
  // refresh that replaces the conversation follows it, so wait for the replayed
  // marker to actually disappear rather than racing the fetch.
  await eventually(
    () =>
      page.evaluate(
        `!document.querySelector("#chatTranscript")?.textContent?.includes("The live turn is growing.")`,
      ),
    { timeout: 20000, what: "the reconnect to replace the conversation with fresh history" },
  );
  console.log(
    '  after reconnect: chat badge "live" again and history replaced the replayed turn wholesale (marker gone)',
  );

  // C10: the permission/approval card. A real pending approval is not guaranteed
  // to occur, so the card is driven by synthetic `chat_permission` frames replayed
  // through the page's own socket handler (the same shim as C5) while the real
  // session stays subscribed. The page answers on its real `chat_reply` path, but
  // the shim records and suppresses that frame for the synthetic request id and
  // acks it locally, so no approval decision ever reaches the operator's server.
  step("C10: the approval card — replayed chat_permission frames, no real decision sent");
  const approvalRequestId = "cap-approval-1";
  await page.evaluate(`window.__suppressChatReplyIds = ${JSON.stringify([approvalRequestId])}`);

  const replayPermission = (frame) =>
    page.evaluate(`(() => {
      const ws = window.__pipelinesSocket;
      if (!ws || typeof ws.onmessage !== "function") return false;
      ws.onmessage({ data: JSON.stringify({ type: "chat_permission", chat_permission: ${JSON.stringify(frame)} }) });
      return true;
    })()`);

  // A synthetic in-flight turn, so the activity row would be showing before
  // the approval arrives — and must stand down while the request is pending.
  await applyFrames(
    [sessionFrame({ message_id: "cap-approval-turn", kind: "step", step: { status: "started" } })],
    "the synthetic in-flight turn",
  );
  await eventually(() => page.evaluate(`!!document.querySelector("#chatTranscript .chat-activity")`), {
    timeout: 8000,
    what: "the activity row before the approval",
  });
  const runningBeforeApproval = await page.evaluate(`!!document.querySelector("#chatTranscript .chat-activity")`);

  const readApproval = () =>
    page.evaluate(`(() => {
      const card = document.querySelector("#chatApproval");
      const transcript = document.querySelector("#chatTranscript");
      const top = document.querySelector(".chat-approval-jump");
      return {
        present: !!card,
        last: !!card && !!transcript && transcript.lastElementChild === card,
        headline: card?.querySelector(".chat-approval-headline")?.textContent ?? "",
        status: card?.querySelector(".chat-approval-status")?.textContent ?? "",
        resources: (card?.querySelector(".chat-approval-resources")?.textContent ?? "").split("\\n").filter((line) => line !== ""),
        resourceLines: (() => {
          const el = card?.querySelector(".chat-approval-resources-text");
          if (!el) return 0;
          const lineHeight = parseFloat(getComputedStyle(el).lineHeight) || 0;
          return lineHeight > 0 ? Math.round(el.clientHeight / lineHeight) : 0;
        })(),
        resourceFullLines: (() => {
          // The same content without the six-line clamp, so the capture can prove
          // the clamp actually did work rather than the sample merely fitting.
          const el = card?.querySelector(".chat-approval-resources-text");
          if (!el) return 0;
          const clone = el.cloneNode(true);
          clone.style.webkitLineClamp = "unset";
          clone.style.display = "block";
          clone.style.position = "absolute";
          clone.style.visibility = "hidden";
          clone.style.width = el.clientWidth + "px";
          document.body.appendChild(clone);
          const cs = getComputedStyle(clone);
          const lineHeight = parseFloat(cs.lineHeight) || 0;
          const padding = (parseFloat(cs.paddingTop) || 0) + (parseFloat(cs.paddingBottom) || 0);
          const lines = lineHeight > 0 ? Math.round((clone.getBoundingClientRect().height - padding) / lineHeight) : 0;
          clone.remove();
          return lines;
        })(),
        resourceClip: (() => {
          // The clamp block must itself be padding-free and end exactly at the
          // frame's content edge: a padded clamp element clips at its *padding*
          // box, which paints the top of the seventh line into that strip.
          const el = card?.querySelector(".chat-approval-resources-text");
          const frame = card?.querySelector(".chat-approval-resources");
          if (!el || !frame) return null;
          const cs = getComputedStyle(el);
          const fcs = getComputedStyle(frame);
          const lineHeight = parseFloat(cs.lineHeight) || 0;
          const bottomPadding = parseFloat(cs.paddingBottom) || 0;
          const framePadding = parseFloat(fcs.paddingBottom) || 0;
          const frameBorder = parseFloat(fcs.borderBottomWidth) || 0;
          const frameRect = frame.getBoundingClientRect();
          const elRect = el.getBoundingClientRect();
          return {
            lineHeight,
            bottomPadding,
            framePadding,
            frameBorder,
            clippedLines: lineHeight > 0 ? Math.round(el.clientHeight / lineHeight) : 0,
            overflows: el.scrollHeight > el.clientHeight,
            // How much content the six-line box hides. A full line or more proves
            // the clamp is genuinely exercised (the clone-based unclamped measure
            // is unreliable once the block sits inside a clamped frame).
            scrollOverflow: el.scrollHeight - el.clientHeight,
            // The frame's content edge sits above its bottom border and padding.
            endsAtFrameContentEdge:
              Math.abs(frameRect.bottom - frameBorder - framePadding - elRect.bottom) < 0.5,
            overflow: cs.overflowY,
          };
        })(),
        primaryFilled: (() => {
          // The Allow once control is the app's filled Button, the other two its
          // TextButtons (ChatApprovalCard.kt:98-100), so its computed background
          // must differ from Always allow's once the primary rule exists.
          const controls = [...(card?.querySelectorAll(".chat-approval-control") ?? [])];
          if (controls.length < 2) return false;
          return getComputedStyle(controls[0]).backgroundColor !== getComputedStyle(controls[1]).backgroundColor;
        })(),
        riskLabel: card?.querySelector(".chat-approval-risk-label")?.textContent ?? "",
        riskText: card?.querySelector(".chat-approval-risk-text")?.textContent ?? "",
        shield: (() => {
          // The app's amber outlined Shield: an SVG stroked with currentColor, so
          // the warn hue actually tints it (a colour emoji would not).
          const el = card?.querySelector(".chat-approval-shield");
          if (!el) return null;
          const stroke = el.querySelector("path")?.getAttribute("stroke") ?? "";
          return {
            svg: el.tagName.toLowerCase() === "svg",
            stroke,
            color: getComputedStyle(el).color,
            tinted: getComputedStyle(el).color !== "rgb(0, 0, 0)",
          };
        })(),
        controls: [...(card?.querySelectorAll(".chat-approval-control") ?? [])].map((b) => b.textContent),
        topControl: top?.textContent ?? "",
        running: !!document.querySelector("#chatTranscript .chat-activity"),
      };
    })()`);

  const pendingPermission = {
    session_id: sessionId,
    request_id: approvalRequestId,
    action: "bash",
    resources: [
      "",
      "/repo/src/app.js",
      // A long, wrapping resource kept inside the first six lines, so the rendered
      // six-line clamp is actually exercised (a short sample would fit anyway).
      "/Users/alan/repos/pipelines/internal/server/chat_stream_handlers/approval_and_permission_frames/very_long_segment_name/another_deep_segment/file_with_a_deliberately_long_name.js",
      // A second long path, so the clamped sample overflows by enough lines that the
      // six-line clamp is exercised at any realistic card width (a single long path
      // shrank to exactly six unclamped lines after the merged layout).
      "/Users/alan/repos/pipelines/internal/server/another/deeply/nested/segment/that/wraps/across/several/rendered/lines/in/the/approval/resources/panel/and_keeps_going.js",
      "/repo/src/repository.js",
      "/repo/src/protocol.js",
      "/repo/styles.css",
      "/repo/test/chat.test.mjs",
      "/repo/test/repository.test.mjs",
      "/repo/PARITY.md",
    ],
    message: "Runs a shell command in the workspace.",
  };
  const replayedPending = await replayPermission(pendingPermission);
  if (!replayedPending) {
    throw new Error("C10: the socket shim did not expose the live socket for the permission replay");
  }
  await eventually(() => page.evaluate(`!!document.querySelector("#chatApproval")`), {
    timeout: 8000,
    what: "the approval card to render",
  });

  const pending = await readApproval();
  if (!pending.present || !pending.last) {
    throw new Error(`C10: the pending card is not the transcript's last element: ${JSON.stringify(pending)}`);
  }
  if (pending.headline !== "Approve bash") throw new Error(`C10: pending headline: ${JSON.stringify(pending.headline)}`);
  if (pending.status !== "Waiting for your response") throw new Error(`C10: pending status: ${JSON.stringify(pending.status)}`);
  if (pending.resources.length !== 6) {
    throw new Error(`C10: expected six resource lines, got ${JSON.stringify(pending.resources)}`);
  }
  if (pending.resourceLines > 6) {
    throw new Error(
      `C10: the resource block rendered ${pending.resourceLines} lines, above the six-line cap ` +
        `(unclamped it would be ${pending.resourceFullLines})`,
    );
  }
  if (pending.resourceLines !== 6) {
    throw new Error(`C10: the six-line clamp did not land on exactly six lines: ${pending.resourceLines}`);
  }
  const clip = pending.resourceClip;
  if (
    !clip ||
    clip.bottomPadding !== 0 ||
    clip.framePadding <= 0 ||
    !clip.overflows ||
    clip.overflow !== "hidden" ||
    !clip.endsAtFrameContentEdge
  ) {
    throw new Error(
      `C10: the clamp block would let a seventh line paint into the frame's padding: ${JSON.stringify(clip)}`,
    );
  }
  if (!clip || !(clip.scrollOverflow >= clip.lineHeight)) {
    throw new Error(
      `C10: the resource sample did not overflow by a full line, so the six-line clamp was not ` +
        `exercised: ${JSON.stringify(clip)}`,
    );
  }
  if (!pending.primaryFilled) {
    throw new Error("C10: the Allow once control is not visually distinct from Always allow");
  }
  if (pending.riskLabel !== "RISK REVIEW" || pending.riskText !== "Runs a shell command in the workspace.") {
    throw new Error(`C10: risk panel: ${JSON.stringify([pending.riskLabel, pending.riskText])}`);
  }
  if (!pending.shield || !pending.shield.svg || pending.shield.stroke !== "currentColor" || !pending.shield.tinted) {
    throw new Error(`C10: the card's shield is not the tinted outlined SVG: ${JSON.stringify(pending.shield)}`);
  }
  if (JSON.stringify(pending.controls) !== JSON.stringify(["Allow once", "Always allow", "Reject"])) {
    throw new Error(`C10: controls: ${JSON.stringify(pending.controls)}`);
  }
  if (pending.topControl !== "Waiting for your approval") {
    throw new Error(`C10: top-bar control: ${JSON.stringify(pending.topControl)}`);
  }
  if (runningBeforeApproval && pending.running) {
    throw new Error("C10: the activity row did not stand down while the approval is pending");
  }
  console.log(
    `  pending card: "${pending.headline}" / "${pending.status}", ${pending.resources.length} resource lines ` +
      `(rendered ${pending.resourceLines} of ${pending.resourceFullLines} unclamped), risk "${pending.riskLabel}", ` +
      `controls ${JSON.stringify(pending.controls)} (Allow once filled: ${pending.primaryFilled}), ` +
      `top-bar "${pending.topControl}", one activity row ${pending.running ? "present" : "absent"}`,
  );
  await shot("65-chat-approval-pending.png");

  // Answer Allow once: the card settles optimistically, the controls and the
  // top-bar control go, and the reply is recorded/suppressed by the shim.
  await page.evaluate(`document.querySelectorAll("#chatApproval .chat-approval-control")[0].click()`);
  await eventually(
    () => page.evaluate(`document.querySelectorAll("#chatApproval .chat-approval-control").length === 0`),
    { timeout: 8000, what: "the answered card to drop its controls" },
  );
  const answered = await readApproval();
  if (answered.headline !== "Allowed once") throw new Error(`C10: answered headline: ${JSON.stringify(answered.headline)}`);
  if (answered.status !== "Waiting for the agent to continue…") {
    throw new Error(`C10: answered status: ${JSON.stringify(answered.status)}`);
  }
  if (answered.controls.length !== 0 || answered.topControl !== "") {
    throw new Error(`C10: answered card kept a control: ${JSON.stringify(answered)}`);
  }
  console.log(
    `  after Allow once: "${answered.headline}" / "${answered.status}", controls gone, top-bar control gone, no decision sent`,
  );
  await shot("66-chat-approval-answered.png");

  // The same request re-pushed keeps the local answer; a server-resolved frame
  // then wins and shows its note with no controls and no risk panel.
  await replayPermission(pendingPermission);
  await eventually(
    () => page.evaluate(`document.querySelector("#chatApproval .chat-approval-headline")?.textContent === "Allowed once"`),
    { timeout: 8000, what: "the same-request re-push to keep the local answer" },
  );
  const repushed = await readApproval();
  if (repushed.headline !== "Allowed once" || repushed.controls.length !== 0) {
    throw new Error(`C10: the same-request re-push did not keep the local answer: ${JSON.stringify(repushed)}`);
  }
  await replayPermission({
    ...pendingPermission,
    resolved: true,
    decision: "always",
    resolved_by: "Pixel 10 Pro Fold",
    message: "Another client already allowed this.",
  });
  await eventually(
    () => page.evaluate(`document.querySelector("#chatApproval .chat-approval-headline")?.textContent === "Always allowed"`),
    { timeout: 8000, what: "the server resolution to win over the local answer" },
  );
  const resolved = await readApproval();
  if (resolved.status !== "Another client already allowed this.") {
    throw new Error(`C10: resolved status: ${JSON.stringify(resolved.status)}`);
  }
  if (resolved.controls.length !== 0 || resolved.topControl !== "") {
    throw new Error(`C10: resolved card kept a control: ${JSON.stringify(resolved)}`);
  }
  if (resolved.riskLabel !== "") throw new Error(`C10: resolved card kept its risk panel: ${JSON.stringify(resolved.riskLabel)}`);
  console.log(
    `  resolved by another client: "${resolved.headline}" / "${resolved.status}", no controls, no risk panel, no top-bar control`,
  );
  await shot("67-chat-approval-resolved.png");

  // A brand-new request replaces the settled card and clears the answer; a
  // refused chat_reply must reopen it with the server's reason (never show an
  // unaccepted decision as accepted), and a rejection keeps the risk panel.
  const secondRequestId = "cap-approval-2";
  await page.evaluate(
    `window.__suppressChatReplyIds = ${JSON.stringify([approvalRequestId, secondRequestId])}`,
  );
  await page.evaluate(`window.__failChatReplyIds = ${JSON.stringify([secondRequestId])}`);
  const secondPermission = {
    session_id: sessionId,
    request_id: secondRequestId,
    action: "",
    resources: ["/repo/src/app.js"],
    message: "",
  };
  await replayPermission(secondPermission);
  await eventually(
    () =>
      page.evaluate(
        `document.querySelector("#chatApproval")?.getAttribute("data-request-id") === ${JSON.stringify(secondRequestId)}`,
      ),
    { timeout: 8000, what: "the new request to replace the settled card" },
  );
  const replaced = await readApproval();
  if (replaced.headline !== "Approve tool call" || replaced.status !== "Waiting for your response") {
    throw new Error(`C10: the new request did not replace the settled card: ${JSON.stringify(replaced)}`);
  }
  if (replaced.controls.length !== 3 || replaced.topControl !== "Waiting for your approval") {
    throw new Error(`C10: the new request is not actionable: ${JSON.stringify(replaced)}`);
  }
  if (replaced.riskText !== "The agent is asking to run a tool call; review the request before allowing.") {
    throw new Error(`C10: the blank-action risk fallback: ${JSON.stringify(replaced.riskText)}`);
  }
  console.log(`  a new request replaced the settled card: "${replaced.headline}", controls back, risk fallback shown`);

  // Always allow -> the shim answers with a server error, so the card must reopen
  // with the reason and the thread's error line, and no accepted decision shows.
  await page.evaluate(`[...document.querySelectorAll("#chatApproval .chat-approval-control")].find((b) => b.textContent === "Always allow").click()`);
  await eventually(
    () => page.evaluate(`document.querySelector("#chatApproval .chat-approval-headline")?.textContent === "Approve tool call"`),
    { timeout: 8000, what: "the refused reply to reopen the request" },
  );
  const reopened = await page.evaluate(`(() => {
    const card = document.querySelector("#chatApproval");
    return {
      headline: card?.querySelector(".chat-approval-headline")?.textContent ?? "",
      controls: [...card.querySelectorAll(".chat-approval-control")].map((b) => b.textContent),
      error: document.querySelector(".chat-transcript-error")?.textContent ?? "",
      topControl: document.querySelector(".chat-approval-jump")?.textContent ?? "",
    };
  })()`);
  if (
    reopened.headline !== "Approve tool call" ||
    reopened.controls.length !== 3 ||
    reopened.topControl !== "Waiting for your approval" ||
    reopened.error !== "This approval request is no longer available."
  ) {
    throw new Error(`C10: a refused reply did not reopen the request with its reason: ${JSON.stringify(reopened)}`);
  }
  console.log(
    `  a refused chat_reply reopened "${reopened.headline}" with its reason: "${reopened.error}", controls back`,
  );

  // Reject now succeeds (the shim acks it): the card reads Rejected and keeps the
  // amber risk panel, matching Android's showRisk for a denial.
  await page.evaluate(`window.__failChatReplyIds = []`);
  await page.evaluate(`[...document.querySelectorAll("#chatApproval .chat-approval-control")].find((b) => b.textContent === "Reject").click()`);
  await eventually(
    () => page.evaluate(`document.querySelector("#chatApproval .chat-approval-headline")?.textContent === "Rejected"`),
    { timeout: 8000, what: "the rejected card to settle" },
  );
  const rejected = await readApproval();
  if (rejected.status !== "Waiting for the agent to continue…" || rejected.controls.length !== 0) {
    throw new Error(`C10: the rejected card did not settle: ${JSON.stringify(rejected)}`);
  }
  if (rejected.riskLabel !== "RISK REVIEW" || rejected.riskText === "") {
    throw new Error(`C10: a rejected card dropped its risk panel: ${JSON.stringify(rejected)}`);
  }
  const errorCleared = await page.evaluate(
    `!document.querySelector(".chat-transcript-error")`,
  );
  if (!errorCleared) {
    throw new Error("C10: the accepted Reject left the previous refusal's reason on the error line");
  }
  console.log(
    `  after Reject: "${rejected.headline}" / "${rejected.status}", risk panel kept ("${rejected.riskLabel}"), ` +
      `controls gone, the refusal's error line cleared`,
  );
  await shot("68-chat-approval-rejected.png");

  // The blank-decision server resolution: a request settled without a decision
  // reads `Already resolved` and falls back to the neutral status sentence.
  const thirdRequestId = "cap-approval-3";
  await replayPermission({
    session_id: sessionId,
    request_id: thirdRequestId,
    action: "edit",
    resources: ["/repo/src/app.js"],
    resolved: true,
  });
  await eventually(
    () =>
      page.evaluate(
        `document.querySelector("#chatApproval .chat-approval-headline")?.textContent === "Already resolved"`,
      ),
    { timeout: 8000, what: "the blank-decision resolution to render" },
  );
  const alreadyResolved = await readApproval();
  if (
    alreadyResolved.status !== "Another client already answered this request." ||
    alreadyResolved.controls.length !== 0 ||
    alreadyResolved.topControl !== "" ||
    alreadyResolved.riskLabel !== ""
  ) {
    throw new Error(`C10: the blank-decision resolution: ${JSON.stringify(alreadyResolved)}`);
  }
  console.log(
    `  a resolution with no decision: "${alreadyResolved.headline}" / "${alreadyResolved.status}", no controls, no risk panel`,
  );

  const replyCounts = await page.evaluate(`window.__chatReplyCounts`);
  if (replyCounts.suppressed !== 3 || replyCounts.failed !== 1 || replyCounts.forwarded !== 0) {
    throw new Error(`C10: unexpected chat_reply counters: ${JSON.stringify(replyCounts)}`);
  }
  console.log(
    `  suppressed outgoing chat_reply for the synthetic requests: ${replyCounts.suppressed} ` +
      `(one refused with a server error: ${replyCounts.failed}); forwarded real chat_reply: ${replyCounts.forwarded}`,
  );
  console.log(
    "  NOTE: the approval card above was driven by replayed chat_permission frames; no real approval decision was sent to the server",
  );

  // The third word: disconnecting entirely reads `not live`.
  await clickDestination("Settings");
  await page.waitForSelector("#settingsPrimary", { timeout: 15000 });
  await page.click("#settingsPrimary");
  await clickDestination("Chat");
  await page.waitForSelector('#shellStatus[data-state="idle"]', { timeout: 15000 });
  console.log(`  chat stream badge after Disconnect: "${await chatBadge()}"`);

  console.log(
    "  NOTE: the chat_subscribe ack and every chat_history reply above came from the real server; only the chat_event frames were replayed",
  );

  // L1: the Settings destination. The live-badge step just disconnected on
  // purpose, so reconnect first, then open Settings and assert the connected
  // Connection section against the real pong, exercise the Workspace field and
  // the Templates card, and prove both persistence keys restore on a reload.
  step("reconnecting for the Settings destination (L1)");
  await clickDestination("Settings");
  await page.waitForSelector("#settingsPrimary", { timeout: 15000 });
  await page.click("#settingsPrimary");
  await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 25000 });

  step("opening Settings and asserting the Connection section (L1)");
  await clickDestination("Settings");
  await page.waitForSelector("#settingsServerUrl", { timeout: 15000 });
  await eventually(
    () => page.evaluate(`document.querySelector("#settingsPrimary")?.textContent === "Disconnect"`),
    { timeout: 15000, what: "the Settings primary action to read Disconnect while connected" },
  );
  const settingsPrimary = await text("#settingsPrimary");
  const settingsSecondary = await text("#settingsSecondary");
  if (settingsPrimary !== "Disconnect" || settingsSecondary !== "Connect") {
    throw new Error(`settings actions: primary=${settingsPrimary} secondary=${settingsSecondary}`);
  }
  // The secondary `Connect` label is connect-only (Android's AppViewModel.connect),
  // never a toggle: pressing it while connected re-opens the shared socket and must
  // never leave the page idle. Record the shell status transitions so a same-frame
  // return to green cannot hide a teardown.
  await page.evaluate(`(() => {
    window.__connTransitions = [];
    const status = document.querySelector("#shellStatus");
    new MutationObserver(() => window.__connTransitions.push(status.dataset.state)).observe(status, {
      attributes: true,
      attributeFilter: ["data-state"],
    });
  })()`);
  await page.click("#settingsSecondary");
  await eventually(
    () =>
      page.evaluate(`(() => {
        const status = document.querySelector("#shellStatus");
        return status?.dataset.state === "connected" && window.__connTransitions.includes("connecting");
      })()`),
    { timeout: 25000, what: "the secondary Connect to re-open the socket back to connected" },
  );
  const afterSecondary = await page.evaluate(`({
    primary: document.querySelector("#settingsPrimary")?.textContent ?? "",
    secondary: document.querySelector("#settingsSecondary")?.textContent ?? "",
    status: document.querySelector("#shellStatus")?.dataset.state ?? "",
    transitions: window.__connTransitions,
  })`);
  if (afterSecondary.status !== "connected") {
    throw new Error(`secondary Connect left the shell ${afterSecondary.status}`);
  }
  if (afterSecondary.transitions.includes("idle")) {
    throw new Error(`secondary Connect tore the socket down: ${JSON.stringify(afterSecondary.transitions)}`);
  }
  if (afterSecondary.primary !== "Disconnect" || afterSecondary.secondary !== "Connect") {
    throw new Error(
      `secondary Connect changed the actions: primary=${afterSecondary.primary} secondary=${afterSecondary.secondary}`,
    );
  }
  console.log(
    `  secondary Connect re-opened the existing client (connect-only, no toggle): ${afterSecondary.transitions.join(" -> ")}`,
  );
  const settingsUrlPlaceholder = await page.evaluate(
    `document.querySelector("#settingsServerUrl")?.placeholder ?? ""`,
  );
  if (settingsUrlPlaceholder !== SERVER_URL_PLACEHOLDER) {
    throw new Error(`settings URL placeholder: ${settingsUrlPlaceholder}`);
  }
  const settingsUrlHelper = await page.evaluate(`(() => {
    const section = document.querySelector("#settingsServerUrl")?.closest(".settings-section");
    return section?.querySelector(".settings-helper")?.textContent ?? "";
  })()`);
  if (settingsUrlHelper !== SERVER_URL_HELPER) {
    throw new Error(`settings URL helper: ${settingsUrlHelper}`);
  }
  // The connected status card, read from the real pong: Connected + the build
  // line + the server-time line (not just screenshotted).
  await eventually(
    () => page.evaluate(`(() => {
      const lines = [...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent);
      return lines[0] === "Connected" && lines.some((l) => l.startsWith("Server build ")) && lines.some((l) => l.startsWith("Server time "));
    })()`),
    { timeout: 20000, what: "the connected Settings status card with build and time lines" },
  );
  const statusLines = await page.evaluate(
    `[...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent)`,
  );
  const trustTitle = await text(".settings-warning .settings-warning-title");
  const trustBody = await text(".settings-warning .settings-warning-body");
  if (trustTitle !== TRUST_TITLE || trustBody !== TRUST_BODY) {
    throw new Error(`trust warning: ${trustTitle} / ${trustBody}`);
  }
  console.log(`  settings connection card: ${statusLines.join(" | ")}`);
  await page.evaluate(`window.scrollTo(0, 0)`);
  await shot("L1-settings-connection.png");

  // F1 (blocking review finding): an invalid URL is reported as the Settings
  // field's own error (Android's `urlError`) with aria-invalid/aria-describedby,
  // and is cleared by the next edit — never left as a stale page-level banner.
  // Settings is now the only URL field, so there is no second draft to mirror.
  const goodUrl = await page.evaluate(`document.querySelector("#settingsServerUrl")?.value ?? ""`);
  // An earlier section can leave a page banner behind; the assertion below is
  // about where THIS refusal is surfaced, so clear any stale one with the page's
  // own path (re-selecting the active destination) before triggering it.
  await page.evaluate(`(() => {
    if (!document.querySelector("#content .banner.error")) return true;
    const tab = [...document.querySelectorAll('[data-destination]')].find((b) => b.getAttribute("aria-selected") === "true");
    if (tab) tab.click();
    return !document.querySelector("#content .banner.error");
  })()`);
  await page.fill("#settingsServerUrl", "not a url");
  await page.click("#settingsSecondary");
  const urlErrorState = await eventually(
    () =>
      page
        .evaluate(`(() => {
          const helper = document.querySelector("#settingsUrlHelper");
          const field = document.querySelector("#settingsServerUrl");
          return {
            helper: helper?.textContent ?? "",
            cls: helper?.className ?? "",
            invalid: field?.getAttribute("aria-invalid") ?? "",
            describedby: field?.getAttribute("aria-describedby") ?? "",
            pageBanner: !!document.querySelector("#content .banner.error"),
          };
        })()`)
        .then((value) => (value.helper === "That is not a valid URL." ? value : false)),
    { timeout: 10000, what: "the URL field's own error after an invalid Connect" },
  );
  if (urlErrorState.invalid !== "true" || urlErrorState.describedby !== "settingsUrlHelper") {
    throw new Error(`URL field error is not the field's own state: ${JSON.stringify(urlErrorState)}`);
  }
  if (!urlErrorState.cls.includes("settings-helper-error")) {
    throw new Error(`URL error helper is not error-tinted: ${JSON.stringify(urlErrorState)}`);
  }
  if (urlErrorState.pageBanner) {
    throw new Error("the invalid URL was surfaced as a page-level banner, not the field error");
  }
  await page.evaluate(`document.querySelector("#settingsUrlHelper")?.scrollIntoView({ block: "center" })`);
  await shot("L1-settings-url-error.png");
  // F1: correcting the field (Android's updateServerUrlDraft) clears its own
  // error and restores the helper line.
  await page.fill("#settingsServerUrl", "ws://10.7.7.7:8099/connect");
  const corrected = await page.evaluate(`(() => ({
    helper: document.querySelector("#settingsUrlHelper")?.textContent ?? "",
    invalid: document.querySelector("#settingsServerUrl")?.hasAttribute("aria-invalid") ?? null,
  }))()`);
  if (corrected.invalid !== false || corrected.helper !== SERVER_URL_HELPER) {
    throw new Error(`editing the URL did not clear the error: ${JSON.stringify(corrected)}`);
  }
  console.log("  invalid URL is the field's own error (aria-invalid, no page banner), cleared on edit");
  await page.fill("#settingsServerUrl", goodUrl);

  // The other three status-card states, live: Disconnect drives the page to idle
  // (`Not connected`, Connect emphasised, Reconnect offered); a bad endpoint
  // drives it to `Disconnected` with the server's message and the retry suffix.
  step("capturing the idle and disconnected Settings status card (L1)");
  await page.click("#settingsPrimary");
  await eventually(
    () =>
      page.evaluate(`(() => {
        const lines = [...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent);
        return (
          lines.length === 1 &&
          lines[0] === "Not connected" &&
          document.querySelector("#settingsPrimary")?.textContent === "Connect" &&
          document.querySelector("#settingsSecondary")?.textContent === "Reconnect"
        );
      })()`),
    { timeout: 15000, what: "the idle Settings card after Disconnect" },
  );
  console.log("  settings idle card: Not connected (Connect primary, Reconnect secondary)");
  await shot("L1-settings-idle.png");

  await page.fill("#settingsServerUrl", `ws://127.0.0.1:${port}/not-a-connect-endpoint`);
  await page.click("#settingsPrimary");
  await eventually(
    () =>
      page.evaluate(`(() => {
        const lines = [...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent);
        return (
          lines[0] === "Disconnected" &&
          lines.length >= 3 &&
          lines.includes("Retrying with backoff\\u2026")
        );
      })()`),
    { timeout: 20000, what: "the disconnected Settings card with its retry suffix" },
  );
  const disconnectedLines = await page.evaluate(
    `[...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent)`,
  );
  console.log(`  settings disconnected card: ${disconnectedLines.join(" | ")}`);
  await shot("L1-settings-disconnected.png");

  // The fourth state: an endpoint that neither accepts nor refuses (a
  // non-routable address) leaves the client Connecting, so the attempt number is
  // visible in the card. Disconnect then ends the attempt before the restore.
  await page.fill("#settingsServerUrl", `ws://10.255.255.1:${port}/connect`);
  await page.click("#settingsPrimary");
  await eventually(
    () =>
      page.evaluate(`(() => {
        const lines = [...document.querySelectorAll(".settings-status .settings-status-line")].map((n) => n.textContent);
        return lines.length === 1 && /^Connecting \\(attempt \\d+\\)\\u2026$/.test(lines[0]);
      })()`),
    { timeout: 15000, what: "the connecting Settings card with its attempt number" },
  );
  const connectingLine = await text(".settings-status .settings-status-line");
  const connectingSpinner = await page.evaluate(
    `!!document.querySelector(".settings-status .settings-status-spinner")`,
  );
  if (!connectingSpinner) throw new Error("the Connecting card should show the spinner (SettingsScreen.kt)");
  console.log(`  settings connecting card: ${connectingLine}`);
  await shot("L1-settings-connecting.png");
  await page.click("#settingsPrimary");

  // Restore the real endpoint (and the persisted server URL) before the
  // workspace step, so the reload assertion still sees a working configuration.
  await page.fill("#settingsServerUrl", goodUrl);
  await page.click("#settingsPrimary");
  await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 25000 });

  step("exercising the Settings workspace field (L1)");
  const workspacePlaceholder = await page.evaluate(
    `document.querySelector("#settingsWorkspace")?.placeholder ?? ""`,
  );
  if (workspacePlaceholder !== WORKSPACE_PLACEHOLDER) {
    throw new Error(`workspace placeholder: ${workspacePlaceholder}`);
  }
  const blankWorkspaceHelper = await text("#workspaceHelper");
  if (blankWorkspaceHelper !== WORKSPACE_HELPER) {
    throw new Error(`workspace helper: ${blankWorkspaceHelper}`);
  }
  const blankSaved = await text("#savedWorkspace");
  if (blankSaved !== "Saved workspace: (none)") throw new Error(`saved line on blank: ${blankSaved}`);
  const blankSaveDisabled = await page.evaluate(`document.querySelector("#saveWorkspace")?.disabled ?? null`);
  if (blankSaveDisabled !== true) throw new Error("Save workspace should be disabled while the field is blank");
  // A relative path keeps Save disabled and names the rule (the helper line).
  await page.fill("#settingsWorkspace", "repos/pipelines");
  const relativeSaveDisabled = await page.evaluate(`document.querySelector("#saveWorkspace")?.disabled ?? null`);
  if (relativeSaveDisabled !== true) throw new Error("Save workspace should stay disabled for a relative path");
  const relativeHelper = await text("#workspaceHelper");
  if (relativeHelper !== WORKSPACE_ERROR) throw new Error(`workspace error line: ${relativeHelper}`);
  // F3 (minor review finding): the workspace error is distinguishable as an error
  // (error tint + aria-invalid/aria-describedby), mirroring Android's isError.
  const workspaceErrorState = await page.evaluate(`(() => ({
    helperClass: document.querySelector("#workspaceHelper")?.className ?? "",
    invalid: document.querySelector("#settingsWorkspace")?.getAttribute("aria-invalid") ?? "",
    describedby: document.querySelector("#settingsWorkspace")?.getAttribute("aria-describedby") ?? "",
  }))()`);
  if (!workspaceErrorState.helperClass.includes("settings-helper-error")) {
    throw new Error(`workspace helper is not error-tinted: ${JSON.stringify(workspaceErrorState)}`);
  }
  if (workspaceErrorState.invalid !== "true" || workspaceErrorState.describedby !== "workspaceHelper") {
    throw new Error(`workspace field is not marked invalid: ${JSON.stringify(workspaceErrorState)}`);
  }
  await page.evaluate(`document.querySelector("#workspaceHelper")?.scrollIntoView({ block: "center" })`);
  await shot("L1-settings-workspace-error.png");
  // An absolute path (padded with spaces) enables Save; saving stores the trimmed value.
  await page.fill("#settingsWorkspace", "  /Users/alan/repos/pipelines  ");
  const absoluteSaveEnabled = await page.evaluate(`document.querySelector("#saveWorkspace")?.disabled === false`);
  if (!absoluteSaveEnabled) throw new Error("Save workspace should be enabled for an absolute path");
  const workspaceValidState = await page.evaluate(`(() => ({
    helperClass: document.querySelector("#workspaceHelper")?.className ?? "",
    invalid: document.querySelector("#settingsWorkspace")?.hasAttribute("aria-invalid") ?? null,
  }))()`);
  if (workspaceValidState.invalid !== false || workspaceValidState.helperClass.includes("settings-helper-error")) {
    throw new Error(`the workspace error did not clear for a valid path: ${JSON.stringify(workspaceValidState)}`);
  }
  console.log("  workspace error is error-tinted with aria-invalid; a valid path clears it");
  const keysBeforeSave = await page.evaluate(`Object.keys(localStorage)`);
  await page.evaluate(`document.querySelector("#saveWorkspace")?.click()`);
  const savedLine = await eventually(
    async () => {
      const line = await text("#savedWorkspace");
      return line === "Saved workspace: /Users/alan/repos/pipelines" ? line : false;
    },
    { timeout: 10000, what: "the saved workspace line" },
  );
  const storedWorkspace = await page.evaluate(`localStorage.getItem(${JSON.stringify(WORKSPACE_STORAGE_KEY)})`);
  if (storedWorkspace !== "/Users/alan/repos/pipelines") throw new Error(`stored workspace: ${storedWorkspace}`);
  // Saving the workspace writes exactly that one key, and no credential-like key
  // is ever stored (the server has no authentication).
  const keysAfterSave = await page.evaluate(`Object.keys(localStorage)`);
  const addedKeys = keysAfterSave.filter((key) => !keysBeforeSave.includes(key));
  if (addedKeys.length !== 1 || addedKeys[0] !== WORKSPACE_STORAGE_KEY) {
    throw new Error(`workspace save wrote unexpected key(s): ${JSON.stringify(addedKeys)}`);
  }
  const credentialKey = await page.evaluate(
    `Object.keys(localStorage).find((k) => /credential|token|secret|password/i.test(k)) ?? null`,
  );
  if (credentialKey) throw new Error(`a credential-like key was stored: ${credentialKey}`);
  // No second copy of either setting under another name: every settings-shaped
  // key in localStorage is exactly one of the two documented keys.
  const settingsKeys = await page.evaluate(
    `Object.keys(localStorage).filter((k) => /settings|serverurl|workspace/i.test(k)).sort()`,
  );
  if (
    settingsKeys.length !== 2 ||
    !settingsKeys.includes(WORKSPACE_STORAGE_KEY) ||
    !settingsKeys.includes(SERVER_URL_STORAGE_KEY)
  ) {
    throw new Error(`unexpected settings keys: ${JSON.stringify(settingsKeys)}`);
  }
  const storedUrl = await page.evaluate(`localStorage.getItem(${JSON.stringify(SERVER_URL_STORAGE_KEY)})`);
  if (storedUrl !== storedUrl.trim() || !storedUrl.startsWith("ws://")) {
    throw new Error(`stored server URL is not the trimmed endpoint: ${storedUrl}`);
  }
  console.log(`  settings keys: ${settingsKeys.join(", ")} (server URL ${storedUrl})`);
  console.log(`  settings workspace saved (trimmed): ${savedLine}`);
  await shot("L1-settings-workspace.png");

  step("asserting the Settings Templates card (L1)");
  const templatesCardLabel = await text("#browseTemplates .card-name");
  const templatesCardSecondary = await text("#browseTemplates .card-desc");
  if (templatesCardLabel !== TEMPLATES_CARD_LABEL || templatesCardSecondary !== TEMPLATES_CARD_SECONDARY) {
    throw new Error(`templates card: ${templatesCardLabel} / ${templatesCardSecondary}`);
  }
  // The card opens the existing Templates gallery (same destination as the tab).
  // The capture is taken after the click, so it is independent evidence of the
  // entry actually working rather than a duplicate of the Workspace frame (the
  // page is shorter than the viewport, so the card is already visible there).
  await page.click("#browseTemplates");
  await page.waitForSelector("#listTitle", { timeout: 20000 });
  const galleryTitle = await text("#listTitle");
  if (galleryTitle !== "Templates") throw new Error(`templates gallery title: ${galleryTitle}`);
  await shot("L1-settings-templates.png");
  console.log("  Browse templates opened the existing Templates gallery");

  step("reloading to prove the two settings keys restore (L1)");
  const persisted = await page.evaluate(`({
    serverUrl: localStorage.getItem(${JSON.stringify(SERVER_URL_STORAGE_KEY)}),
    workspace: localStorage.getItem(${JSON.stringify(WORKSPACE_STORAGE_KEY)}),
  })`);
  if (persisted.workspace !== "/Users/alan/repos/pipelines") {
    throw new Error(`persisted workspace: ${persisted.workspace}`);
  }
  if (!persisted.serverUrl || !persisted.serverUrl.startsWith("ws://")) {
    throw new Error(`persisted server URL: ${persisted.serverUrl}`);
  }
  await page.evaluate(`location.reload()`);
  await page.waitForSelector("#shellStatus", { timeout: 15000 });
  await clickDestination("Settings");
  await page.waitForSelector("#settingsServerUrl", { timeout: 15000 });
  const restoredUrl = await page.evaluate(`document.querySelector("#settingsServerUrl")?.value ?? ""`);
  if (restoredUrl !== persisted.serverUrl) throw new Error(`restored server URL: ${restoredUrl}`);
  await page.waitForSelector("#settingsWorkspace", { timeout: 15000 });
  const restoredWorkspace = await page.evaluate(`document.querySelector("#settingsWorkspace")?.value ?? ""`);
  if (restoredWorkspace !== "/Users/alan/repos/pipelines") {
    throw new Error(`restored workspace: ${restoredWorkspace}`);
  }
  const restoredSaved = await text("#savedWorkspace");
  if (restoredSaved !== "Saved workspace: /Users/alan/repos/pipelines") {
    throw new Error(`restored saved line: ${restoredSaved}`);
  }
  console.log(`  reload restored url=${restoredUrl} workspace=${restoredWorkspace}`);

  await runNotificationChecks();

  // C11: the background-tasks bar. The live server has no running shell to read
  // safely, so the bar is driven from synthetic `shell` frames replayed through
  // the page's own socket handler (the same shim) and the bounded output pages are
  // answered by a controlled reply hook on that socket. Every other frame — the
  // chat_shells list, the subscribe ack and history — still comes from the real
  // server. This log states the split plainly.
  step("C11: the background-tasks bar (replayed shell frames + bounded output pages)");
  // The C5 section ends on a deliberate Disconnect (idle), which the browser's
  // `online` handler deliberately does not auto-recover; use Settings' own
  // connection section to bring the page back before the bar is captured.
  if ((await page.evaluate(`document.querySelector("#shellStatus")?.dataset.state`)) !== "connected") {
    await connectFromSettings();
  }
  await ensureConnected(30000);
  // The conversation pane persists across the reconnect; bring it back explicitly
  // if a render left another view, clicking through the page's own controls.
  const c11Transcript = await page.evaluate(`!!document.querySelector("#chatTranscript")`);
  if (!c11Transcript) {
    await page.evaluate(`(() => {
      const tab = [...document.querySelectorAll('[data-destination]')].find((b) => b.getAttribute("data-destination") === "chat");
      if (tab) tab.click();
      return !!tab;
    })()`);
    await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 });
    await page.evaluate(
      `document.querySelector('#chatRows .chat-row[data-session="${sessionId}"]')?.click()`,
    );
  }
  await page.waitForSelector("#chatTranscript", { timeout: 25000 });
  await page.evaluate(`document.querySelector("#chatJump")?.click()`);

  // The bounded output pages the page will read are replayed synthetic wire
  // replies; the request shape (chat_shell_output_query, cursor 0, limit 65536)
  // is the client's own and is unit-tested in repository.test.mjs.
  await page.evaluate(`(() => {
    window.__pipelinesInterceptDelay = 0;
    window.__pipelinesPages = {
      "cap-run": { shell: { id: "cap-run", status: "running", command: "go test ./... -count=1" }, output: "ok  \\tpipelines\\t0.42s\\n", cursor: 0, size: 20 },
      "cap-empty": { shell: { id: "cap-empty", status: "running", command: "true" }, output: "", cursor: 0, size: 0 },
      "cap-unavail": { shell: { id: "cap-unavail", status: "running", command: "sleep 30" }, unavailable: true },
      "cap-trunc": { shell: { id: "cap-trunc", status: "running", command: "yes | head -c 200000" }, output: "x".repeat(400), cursor: 400, size: 200000, truncated: true },
      "cap-slow": { shell: { id: "cap-slow", status: "running", command: "sleep 60" }, output: "slow page\\n" },
    };
    window.__pipelinesIntercept = (frame) => {
      if (frame.type !== "chat_shell_output") return null;
      const query = frame.chat_shell_output_query || {};
      (window.__pipelinesShellReads = window.__pipelinesShellReads || []).push({
        shell_id: query.shell_id,
        cursor: query.cursor,
        limit: query.limit,
      });
      const page = window.__pipelinesPages[query.shell_id];
      if (!page) return null;
      return { id: frame.id, type: "chat_shell_output", chat_shell_output: page };
    };
    return true;
  })()`);

  const shellFrame = (id, command, status) => ({ session_id: sessionId, kind: "shell", shell: { id, status, command } });
  const replayShells = (frames) =>
    page.evaluate(`(() => {
      const ws = window.__pipelinesSocket;
      if (!ws || typeof ws.onmessage !== "function") return false;
      for (const frame of ${JSON.stringify(
        [
          shellFrame("cap-run", "go test ./... -count=1", "running"),
          shellFrame("cap-empty", "true", "running"),
          shellFrame("cap-unavail", "sleep 30", "running"),
          shellFrame("cap-trunc", "yes | head -c 200000", "running"),
          shellFrame("cap-slow", "sleep 60", "running"),
        ],
      )}) {
        ws.onmessage({ data: JSON.stringify({ type: "chat_event", chat_event: frame }) });
      }
      return true;
    })()`);

  const ok = await replayShells();
  if (!ok) throw new Error("C11: the socket shim did not expose the live socket for the shell frames");
  await eventually(() => page.evaluate(`!!document.querySelector("#chatBackgroundTasks")`), {
    timeout: 10000,
    what: "the background-tasks bar to render",
  });

  // A transient ping failure during the section's own reconnect can leave the
  // page-level `Error:` banner up (a pre-existing surface from onConnected's
  // ping). Clicking the active tab is the page's own way to clear it, and the
  // conversation and the bar are rebuilt from thread state, so the capture shows
  // the feature rather than a stale unrelated error.
  const clearBanner = () =>
    page.evaluate(`(() => {
      if (!document.querySelector(".banner.error")) return true;
      const tab = [...document.querySelectorAll('[data-destination]')].find((b) => b.getAttribute("aria-selected") === "true");
      if (tab) tab.click();
      return !document.querySelector(".banner.error");
    })()`);

  const c11Bar = await page.evaluate(`(() => {
    const el = document.querySelector("#chatBackgroundTasks");
    if (!el) return null;
    const list = el.querySelector(".chat-bg-tasks-list");
    const rows = [...el.querySelectorAll(".chat-bg-task")];
    return {
      title: el.querySelector(".chat-bg-tasks-title")?.textContent ?? "",
      count: el.querySelector(".chat-bg-tasks-count")?.textContent ?? "",
      rows: rows.length,
      scrollable: list ? list.scrollHeight > list.clientHeight + 1 : false,
      listHeight: list ? list.clientHeight : 0,
      rowHeight: rows[0] ? Math.round(rows[0].getBoundingClientRect().height) : 0,
      commands: rows.map((r) => r.querySelector(".chat-bg-task-command")?.textContent ?? ""),
      glyphs: el.querySelectorAll(".chat-bg-task-glyph").length,
      statuses: rows.map((r) => r.querySelector(".chat-bg-task-status")?.textContent ?? ""),
    };
  })()`);
  if (!c11Bar) throw new Error("C11: the background-tasks bar did not render");
  if (c11Bar.title !== "Background tasks") throw new Error(`C11: bar title ${JSON.stringify(c11Bar.title)}`);
  if (c11Bar.count !== "5 running") throw new Error(`C11: bar count ${JSON.stringify(c11Bar.count)}`);
  if (c11Bar.rows !== 5) throw new Error(`C11: expected 5 rows, got ${c11Bar.rows}`);
  if (!c11Bar.scrollable) throw new Error(`C11: a 5-row list did not scroll (height ${c11Bar.listHeight}px)`);
  if (c11Bar.listHeight > c11Bar.rowHeight * 3 + 16) {
    throw new Error(`C11: the list is not capped near three rows (${c11Bar.listHeight}px for ${c11Bar.rowHeight}px rows)`);
  }
  if (c11Bar.glyphs !== 5) throw new Error(`C11: every row should carry a play glyph, got ${c11Bar.glyphs}`);
  if (!c11Bar.statuses.every((word) => word === "Running")) {
    throw new Error(`C11: row statuses: ${JSON.stringify(c11Bar.statuses)}`);
  }
  console.log(
    `  background-tasks bar "${c11Bar.title}" (${c11Bar.count}); ${c11Bar.rows} rows, ` +
      `3-row cap (list ${c11Bar.listHeight}px, row ${c11Bar.rowHeight}px), scrolling: ${c11Bar.scrollable}`,
  );
  console.log(
    "  NOTE: the shell frames above are replayed synthetic chat_event frames; every other frame (chat_shells, subscribe, history) came from the real server",
  );
  await clearBanner();
  await shot("65-chat-background-tasks.png");

  // A selected task's detail pane: the exact label order and wording, and the
  // bounded output page it read from its own request. Rows are clicked through
  // their own handler (and scrolled into view) so a row below the three-row fold
  // is reachable without a coordinate click landing on the clipped list. A
  // spurious socket drop can fail a single bounded read (the client refuses to
  // send while reconnecting), so each read is retried a bounded number of times
  // through the page's own controls after the socket settles.
  const clickShellRow = (id) =>
    page.evaluate(`(() => {
      const el = document.querySelector('#chatBackgroundTasks .chat-bg-task[data-shell="${id}"]');
      if (!el) return false;
      el.scrollIntoView({ block: "center" });
      el.click();
      return true;
    })()`);

  const openShell = async (shellId, readyExpr, what) => {
    for (let attempt = 1; attempt <= 3; attempt += 1) {
      // A transient drop can clear the connection and (on reconnect) the replayed
      // shells; settle the socket and re-establish the bar before selecting.
      await ensureConnected(30000).catch(() => {});
      if (!(await page.evaluate(`!!document.querySelector("#chatBackgroundTasks")`))) {
        await replayShells();
        await eventually(() => page.evaluate(`!!document.querySelector("#chatBackgroundTasks")`), {
          timeout: 5000,
          what: "the bar to render again after a reconnect",
        });
      }
      const selected = await page.evaluate(
        `document.querySelector("#chatBackgroundTasks .chat-bg-task.selected")?.dataset.shell ?? ""`,
      );
      // Selecting the already-selected row is the documented toggle, so close it
      // first to force a fresh bounded read.
      if (selected === shellId && !(await clickShellRow(shellId))) {
        throw new Error(`C11: the ${shellId} row was not present`);
      }
      if (!(await clickShellRow(shellId))) throw new Error(`C11: the ${shellId} row was not present`);
      try {
        await eventually(() => page.evaluate(readyExpr), { timeout: 8000, what });
        return;
      } catch {
        console.log(`  ${shellId}: the read did not land (attempt ${attempt}); settling the socket and retrying`);
        await page.evaluate(`window.dispatchEvent(new Event("online"))`);
      }
    }
    throw new Error(`C11: ${what} never rendered for ${shellId}`);
  };

  await openShell(
    "cap-run",
    `!!document.querySelector("#chatBackgroundTasks .chat-bg-task-output")`,
    "the output pane for cap-run",
  );
  const c11Detail = await page.evaluate(`(() => {
    const detail = document.querySelector("#chatBackgroundTasks .chat-bg-task-detail");
    return {
      labels: [...detail.querySelectorAll(".chat-bg-task-label")].map((n) => n.textContent),
      command: detail.querySelector(".chat-bg-task-command-full")?.textContent ?? "",
      id: detail.querySelector(".chat-bg-task-id")?.textContent ?? "",
      idMono: !!detail.querySelector(".chat-bg-task-id.monospace"),
      status: detail.querySelector(".chat-bg-task-status-line")?.textContent ?? "",
      output: detail.querySelector(".chat-bg-task-output")?.textContent ?? "",
      refresh: detail.querySelector(".chat-bg-task-refresh")?.textContent ?? "",
      selected: document.querySelector("#chatBackgroundTasks .chat-bg-task.selected")?.dataset.shell ?? "",
      detailsWord: document.querySelector("#chatBackgroundTasks .chat-bg-task.selected .chat-bg-task-details")?.textContent ?? "",
    };
  })()`);
  if (!c11Detail.labels.includes("Command")) throw new Error(`C11: missing Command label: ${JSON.stringify(c11Detail.labels)}`);
  if (!c11Detail.labels.includes("Task identity")) throw new Error(`C11: missing Task identity label`);
  if (!c11Detail.labels.includes("Output")) throw new Error(`C11: missing Output label`);
  if (c11Detail.command !== "go test ./... -count=1") throw new Error(`C11: command ${JSON.stringify(c11Detail.command)}`);
  if (c11Detail.id !== "cap-run" || !c11Detail.idMono) throw new Error(`C11: task identity ${JSON.stringify(c11Detail.id)} mono=${c11Detail.idMono}`);
  if (c11Detail.status !== "Status: running") throw new Error(`C11: status ${JSON.stringify(c11Detail.status)}`);
  if (!c11Detail.output.includes("ok")) throw new Error(`C11: output ${JSON.stringify(c11Detail.output)}`);
  if (c11Detail.refresh !== "Refresh") throw new Error(`C11: refresh control ${JSON.stringify(c11Detail.refresh)}`);
  if (c11Detail.selected !== "cap-run") throw new Error(`C11: selected row ${JSON.stringify(c11Detail.selected)}`);
  if (c11Detail.detailsWord !== "Details") throw new Error(`C11: details word ${JSON.stringify(c11Detail.detailsWord)}`);
  console.log(
    `  selected task detail: labels [${c11Detail.labels.join(", ")}], command "${c11Detail.command}", ` +
      `id "${c11Detail.id}" (monospace), "${c11Detail.status}", output ${JSON.stringify(c11Detail.output)}`,
  );
  await clearBanner();
  await shot("66-chat-background-task-detail.png");

  // Refresh always rereads the bounded page from cursor 0 and never appends: the
  // page's own Refresh control issues a second read with the same bounds (the
  // intercepted request is the page's real wire frame).
  await page.evaluate(`window.__pipelinesShellReads = []`);
  await page.evaluate(`document.querySelector("#chatBackgroundTasks .chat-bg-task-refresh").click()`);
  await eventually(
    () =>
      page.evaluate(
        `(window.__pipelinesShellReads || []).some((r) => r.shell_id === "cap-run" && r.cursor === 0 && r.limit === 65536)`,
      ),
    { timeout: 8000, what: "the Refresh re-read at cursor 0 / 64 KiB" },
  );
  const refreshReads = await page.evaluate(`window.__pipelinesShellReads`);
  console.log(
    `  Refresh re-read: ${refreshReads.length} read(s), ` +
      refreshReads.map((r) => `{shell:${r.shell_id},cursor:${r.cursor},limit:${r.limit}}`).join(", "),
  );

  // The empty page, the server-reported unavailable page and a truncated page.
  const noteIs = (expected) =>
    `document.querySelector("#chatBackgroundTasks .chat-bg-task-note")?.textContent === ${JSON.stringify(expected)}`;
  await openShell("cap-empty", noteIs("No output yet."), "the No output yet. note");
  console.log("  cap-empty: \"No output yet.\"");
  await clearBanner();
  await shot("67-chat-background-task-empty.png");
  await openShell("cap-unavail", noteIs("Output unavailable for this task."), "the unavailable note");
  console.log("  cap-unavail: \"Output unavailable for this task.\"");
  await clearBanner();
  await shot("68-chat-background-task-unavailable.png");

  await openShell("cap-trunc", noteIs("Output is bounded to 64 KiB."), "the 64 KiB bound note");
  const c11Truncated = await page.evaluate(`(() => {
    const detail = document.querySelector("#chatBackgroundTasks .chat-bg-task-detail");
    return {
      note: detail.querySelector(".chat-bg-task-note")?.textContent ?? "",
      length: detail.querySelector(".chat-bg-task-output")?.textContent.length ?? 0,
      refresh: !!detail.querySelector(".chat-bg-task-refresh"),
    };
  })()`);
  if (c11Truncated.note !== "Output is bounded to 64 KiB.") throw new Error(`C11: truncated note ${JSON.stringify(c11Truncated.note)}`);
  if (c11Truncated.length !== 400) throw new Error(`C11: truncated output length ${c11Truncated.length}`);
  if (!c11Truncated.refresh) throw new Error("C11: a truncated page should still offer Refresh");
  console.log(`  cap-trunc: "${c11Truncated.note}" (${c11Truncated.length}-char page, Refresh offered)`);
  await clearBanner();
  await shot("69-chat-background-task-truncated.png");

  // The loading spinner: the same select, with the replayed page delayed.
  await page.evaluate(`window.__pipelinesInterceptDelay = 1500`);
  if (!(await clickShellRow("cap-slow"))) throw new Error("C11: the cap-slow row was not present");
  await eventually(
    () => page.evaluate(`!!document.querySelector("#chatBackgroundTasks .chat-bg-task-detail .spinner")`),
    { timeout: 5000, what: "the loading spinner for cap-slow" },
  );
  const loading = await page.evaluate(`(() => ({
    spinner: !!document.querySelector("#chatBackgroundTasks .chat-bg-task-detail .spinner"),
    status: document.querySelector("#chatBackgroundTasks .chat-bg-task-status-line")?.textContent ?? "",
    output: !!document.querySelector("#chatBackgroundTasks .chat-bg-task-output"),
  }))()`);
  if (!loading.spinner || loading.output) throw new Error(`C11: loading pane ${JSON.stringify(loading)}`);
  console.log(`  cap-slow: spinner while loading ("${loading.status}")`);
  await clearBanner();
  await shot("70-chat-background-task-loading.png");
  for (let attempt = 1; attempt <= 3; attempt += 1) {
    let loaded = true;
    try {
      await eventually(
        () => page.evaluate(`!!document.querySelector("#chatBackgroundTasks .chat-bg-task-output")`),
        { timeout: 8000, what: "the slow page to arrive" },
      );
    } catch {
      loaded = false;
    }
    if (loaded) break;
    console.log(`  cap-slow: the page did not arrive (attempt ${attempt}); settling and retrying`);
    await page.evaluate(`window.dispatchEvent(new Event("online"))`);
    await ensureConnected(30000).catch(() => {});
    if (!(await page.evaluate(`!!document.querySelector("#chatBackgroundTasks")`))) await replayShells();
    await page.evaluate(`document.querySelector("#chatBackgroundTasks .chat-bg-task-retry")?.click()`);
  }
  await page.evaluate(`window.__pipelinesInterceptDelay = 0`);

  // The last exit clears the bar: replay an exited frame for every task and assert
  // the bar disappears (an exited shell is never listed as a task).
  await page.evaluate(`(() => {
    const ws = window.__pipelinesSocket;
    const ids = ["cap-run", "cap-empty", "cap-unavail", "cap-trunc", "cap-slow"];
    for (const id of ids) {
      ws.onmessage({ data: JSON.stringify({ type: "chat_event", chat_event: { session_id: ${JSON.stringify(sessionId)}, kind: "shell", shell: { id, status: "exited", command: "" } } }) });
    }
    return true;
  })()`);
  await eventually(() => page.evaluate(`!document.querySelector("#chatBackgroundTasks")`), {
    timeout: 5000,
    what: "the bar to disappear with the last exit",
  });
  console.log("  all shells exited: the background-tasks bar is gone");
  await clearBanner();
  await shot("71-chat-background-tasks-exited.png");

  // The list is re-learned on a stream reconnect: a real chat_shells request is
  // sent after the drop (real traffic), so a gap that swallowed an exit is repaired.
  await page.evaluate(`window.__pipelinesSentTypes = []`);
  await page.evaluate(`window.__pipelinesSocket && window.__pipelinesSocket.close(4000, "c11 drop")`);
  await ensureConnected(30000);
  await eventually(
    () => page.evaluate(`(window.__pipelinesSentTypes || []).includes("chat_shells")`),
    { timeout: 20000, what: "chat_shells after the stream reconnect" },
  );
  console.log("  chat_shells re-fetched after the stream reconnect (real request)");

  // --- L6: URL routes, deep links and history -------------------------------
  // Every later hard navigation records uncaught errors/unhandled rejections, so
  // a route that throws can never pass as a rendered fallback.
  await page.addInitScript(`(() => {
    window.__pipelinesPageErrors = [];
    window.addEventListener("error", (event) => {
      window.__pipelinesPageErrors.push(String(event.message || event.error || "error"));
    });
    window.addEventListener("unhandledrejection", (event) => {
      window.__pipelinesPageErrors.push(String(event.reason || "unhandled rejection"));
    });
  })()`);

  step("L6: deep links, URL routing and Back/Forward");
  await clickDestination("Runs");
  await page.waitForSelector(".list.runs .run-row", { timeout: 25000 });
  const discovered = await page.evaluate(`(() => {
    const rows = [...document.querySelectorAll(".list.runs .run-row")];
    const idOf = (row) =>
      Number(row.querySelector(".run-row-meta[data-run-id]")?.dataset.runId ?? 0);
    const ids = rows.map(idOf).filter((id) => id > 0);
    const waitingRow = rows.find((row) => row.querySelector(".chip-waiting"));
    return {
      ids,
      preferred: ids.includes(440) ? 440 : (ids[0] ?? 0),
      waiting: waitingRow ? idOf(waitingRow) : 0,
    };
  })()`);
  if (!discovered.preferred) throw new Error("L6: no real run id on the Runs list");
  console.log(
    `  live run ids on the list: ${discovered.ids.join(", ")} (using ${discovered.preferred})`,
  );

  // An in-page pipeline selection is a real pushState: the detail route records
  // the id and version, and the hard link below uses the id it found.
  await clickDestination("Pipelines");
  await page.waitForSelector("#pipelineRows button.row", { timeout: 25000 });
  const pipelineSelection = await page.evaluate(`(async () => {
    const rows = [...document.querySelectorAll("#pipelineRows button.row")];
    const row = rows[0];
    if (!row) return null;
    row.click();
    const deadline = Date.now() + 20000;
    while (Date.now() < deadline) {
      if (document.querySelector("#detailTitle")) break;
      await new Promise((resolve) => setTimeout(resolve, 100));
    }
    return {
      path: location.search ? location.pathname + location.search : location.pathname,
      name: document.querySelector("#detailTitle")?.textContent ?? "",
    };
  })()`);
  if (!pipelineSelection?.path) throw new Error("L6: the pipeline detail did not open");
  const pipelineId = Number((/^\/pipelines\/([0-9]+)/.exec(pipelineSelection.path) ?? [])[1] ?? 0);
  if (!pipelineId) throw new Error(`L6: pipeline detail route ${JSON.stringify(pipelineSelection.path)}`);
  console.log(
    `  in-page pipeline selection pushed ${pipelineSelection.path} (${JSON.stringify(pipelineSelection.name)})`,
  );

  // A hard navigation to a run route: boot parses the URL, the first connection
  // loads the runs list and then the run, and the detail owns the requested id.
  const runPath = `/runs/${discovered.preferred}`;
  await openDeepLink(runPath);
  await eventually(
    () =>
      page.evaluate(
        `document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${discovered.preferred} ") ?? false`,
      ),
    { timeout: 30000, what: `the deep-linked run ${discovered.preferred}` },
  );
  const deepRunUrl = await currentLocation();
  if (deepRunUrl !== runPath) throw new Error(`L6: run URL is ${deepRunUrl}, expected ${runPath}`);
  // The nav badge reads the pipeline count; a run deep link must not leave a
  // false `Pipelines (0)` beside a populated list.
  const runNav = await page.evaluate(
    `[...document.querySelectorAll('.rail-item[data-destination]')].map((b) => b.textContent.trim())`,
  );
  const pipelinesTab = runNav.find((label) => label.startsWith("Pipelines")) ?? "";
  if (pipelinesTab === "Pipelines (0)") {
    throw new Error(`L6: the run deep link left a false Pipelines (0) in the nav: ${JSON.stringify(runNav)}`);
  }
  console.log(`  hard navigation ${runPath} opened run ${discovered.preferred}'s detail; nav ${JSON.stringify(runNav)}`);
  await shot(`L6-deeplink-run-${discovered.preferred}.png`);

  // The same for a pipeline route: /pipelines/:id means latest.
  const pipelinePath = `/pipelines/${pipelineId}`;
  await openDeepLink(pipelinePath);
  await eventually(
    () => page.evaluate(`!!document.querySelector("#detailTitle")`),
    { timeout: 30000, what: `the deep-linked pipeline ${pipelineId}` },
  );
  const deepPipeline = await page.evaluate(`(() => ({
    path: location.pathname + location.search,
    name: document.querySelector("#detailTitle")?.textContent ?? "",
  }))()`);
  if (deepPipeline.path !== pipelinePath) {
    throw new Error(`L6: pipeline URL is ${deepPipeline.path}, expected ${pipelinePath}`);
  }
  if (pipelineSelection.name && deepPipeline.name !== pipelineSelection.name) {
    throw new Error(
      `L6: deep-linked pipeline rendered ${JSON.stringify(deepPipeline.name)}, expected ${JSON.stringify(pipelineSelection.name)}`,
    );
  }
  console.log(`  hard navigation ${pipelinePath} opened ${JSON.stringify(deepPipeline.name)}`);
  await shot(`L6-deeplink-pipeline-${pipelineId}.png`);

  // A pipeline-stage route opens the Graph tab and the named definition pane,
  // and brings that pane into view (the graph above it is taller than a viewport).
  // The stage key comes from the pipeline itself: definitions name their stages.
  const stageDetail = cliJson(
    await runCli(["show", String(pipelineId), "--json"]),
    "show the pipeline for a stage key",
  );
  const stageKey = stageDetail?.graph?.stages?.[0]?.key;
  if (!stageKey) throw new Error("L6: the pipeline has no stage to deep-link");
  const stagePath = `/pipelines/${pipelineId}/stage/${encodeURIComponent(stageKey)}`;
  await openDeepLink(stagePath);
  await eventually(
    () =>
      page.evaluate(
        `[...document.querySelectorAll(".rail-tab, .strip-tab")].find((n) => n.offsetParent !== null && n.getAttribute("aria-selected") === "true")?.textContent === "Graph" &&
         !!document.querySelector(".graph-pane") && !document.querySelector(".graph-pane").hidden`,
      ),
    { timeout: 30000, what: "the deep-linked pipeline stage pane" },
  );
  const stageState = await page.evaluate(`(() => {
    const pane = document.querySelector(".graph-pane");
    const rect = pane.getBoundingClientRect();
    const head = document.querySelector(".graph-pane-head")?.getBoundingClientRect() ?? null;
    const close = document.querySelector(".graph-pane-close")?.getBoundingClientRect() ?? null;
    const chrome = document.querySelector("#content")?.getBoundingClientRect() ?? null;
    return {
      path: location.pathname + location.search,
      tab: [...document.querySelectorAll(".rail-tab, .strip-tab")].find((n) => n.offsetParent !== null && n.getAttribute("aria-selected") === "true")?.textContent ?? "",
      title: document.querySelector(".graph-pane-title")?.textContent ?? "",
      top: Math.round(rect.top),
      viewport: window.innerHeight,
      chromeBottom: 0,
      headTop: head ? Math.round(head.top) : null,
      headBottom: head ? Math.round(head.bottom) : null,
      closeTop: close ? Math.round(close.top) : null,
    };
  })()`);
  if (stageState.path !== stagePath) throw new Error(`L6: pipeline stage URL is ${stageState.path}`);
  if (stageState.tab !== "Graph") throw new Error(`L6: pipeline stage opened tab ${stageState.tab}`);
  if (!stageState.title) throw new Error("L6: the pipeline stage pane has no title");
  if (stageState.headTop == null || stageState.headTop < stageState.chromeBottom) {
    throw new Error(
      `L6: the opened stage pane head (top ${stageState.headTop}) is outside the viewport (chrome bottom ${stageState.chromeBottom})`,
    );
  }
  if (stageState.headBottom > stageState.viewport) {
    throw new Error(`L6: the opened stage pane head is below the viewport (bottom ${stageState.headBottom})`);
  }
  if (stageState.closeTop == null || stageState.closeTop < stageState.chromeBottom) {
    throw new Error(
      `L6: the stage pane Close control (top ${stageState.closeTop}) is outside the viewport (chrome bottom ${stageState.chromeBottom})`,
    );
  }
  console.log(
    `  hard navigation ${stagePath} opened the Graph tab and the ${JSON.stringify(stageState.title)} pane (head ${stageState.headTop}px, chrome bottom ${stageState.chromeBottom}px)`,
  );
  await shot(`L6-deeplink-pipeline-${pipelineId}-stage.png`);

  // The graph also selects its terminals, and the URL it pushes for one must
  // re-open the same pane (a terminal is not a named stage, but it is a node the
  // page itself writes a /stage/ URL for). Find a terminal in the live graph.
  const terminalKey = await page.evaluate(`(() => {
    const node = [...document.querySelectorAll(".graph-node[data-key]")]
      .find((n) => (n.dataset.key ?? "").startsWith("#"));
    return node?.dataset.key ?? "";
  })()`);
  if (terminalKey) {
    const terminalPath = `/pipelines/${pipelineId}/stage/${encodeURIComponent(terminalKey)}`;
    await openDeepLink(terminalPath);
    await eventually(
      () =>
        page.evaluate(
          `!!document.querySelector(".graph-pane") && !document.querySelector(".graph-pane").hidden`,
        ),
      { timeout: 30000, what: `the deep-linked terminal pane ${terminalKey}` },
    );
    const terminalState = await page.evaluate(`(() => ({
      path: location.pathname + location.search,
      title: document.querySelector(".graph-pane-title")?.textContent ?? "",
      headTop: Math.round(document.querySelector(".graph-pane-head")?.getBoundingClientRect().top ?? -1),
      chromeBottom: 0,
    }))()`);
    if (terminalState.path !== terminalPath) {
      throw new Error(
        `L6: the terminal stage URL was rewritten to ${terminalState.path}, expected ${terminalPath}`,
      );
    }
    if (!terminalState.title) throw new Error("L6: the terminal stage pane has no title");
    if (terminalState.headTop < terminalState.chromeBottom) {
      throw new Error("L6: the terminal stage pane head is outside the viewport");
    }
    console.log(
      `  hard navigation ${terminalPath} re-opened the ${JSON.stringify(terminalState.title)} terminal pane (URL unchanged)`,
    );
    await shot(`L6-deeplink-pipeline-${pipelineId}-terminal.png`);
  } else {
    console.log("  --  the live graph has no terminal node; skipped the terminal stage deep link");
  }

  // In-page moves: Pipelines -> Runs -> a run detail, then Back/Forward. A window
  // marker proves the document was never reloaded.
  const marker = `L6-no-reload-${Date.now()}`;
  await page.evaluate(`window.__pipelinesNoReload = ${JSON.stringify(marker)}`);
  await clickDestination("Runs");
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs" && !!document.querySelector(".list.runs .run-row")`,
      ),
    { timeout: 25000, what: "the Runs list after a pushState move" },
  );
  console.log(`  pushState Pipelines -> Runs: ${await currentLocation()}`);

  const runRowSelector = `.list.runs .run-row-meta[data-run-id="${discovered.preferred}"]`;
  const clickedRun = await page.evaluate(`(() => {
    const node = document.querySelector(${JSON.stringify(runRowSelector)});
    if (!node) return false;
    node.click();
    return true;
  })()`);
  if (!clickedRun) throw new Error(`L6: run ${discovered.preferred} was not on the Runs list`);
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs/${discovered.preferred}" &&
         (document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${discovered.preferred} ") ?? false)`,
      ),
    { timeout: 30000, what: `run ${discovered.preferred} after a pushState selection` },
  );
  console.log(`  pushState Runs -> run detail: ${await currentLocation()}`);

  await page.evaluate(`history.back()`);
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs" && !!document.querySelector(".list.runs .run-row")`,
      ),
    { timeout: 25000, what: "Back to the Runs list" },
  );
  const backState = await page.evaluate(`(() => ({
    path: location.pathname,
    marker: window.__pipelinesNoReload ?? null,
    staleDetail: !!document.querySelector(".run-detail .run-identity"),
  }))()`);
  if (backState.path !== "/runs") throw new Error(`L6: Back landed on ${backState.path}`);
  if (backState.staleDetail) throw new Error("L6: Back left a stale run detail rendered");
  if (backState.marker !== marker) throw new Error("L6: Back reloaded the document");
  console.log(`  history.back() -> /runs, marker survived (${backState.marker})`);
  await shot("L6-history-back.png");

  await page.evaluate(`history.forward()`);
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs/${discovered.preferred}" &&
         (document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${discovered.preferred} ") ?? false)`,
      ),
    { timeout: 30000, what: "Forward back to the run detail" },
  );
  const forwardState = await page.evaluate(`(() => ({
    path: location.pathname,
    marker: window.__pipelinesNoReload ?? null,
  }))()`);
  if (forwardState.marker !== marker) throw new Error("L6: Forward reloaded the document");
  console.log(`  history.forward() -> ${forwardState.path}, marker survived`);

  // Leaving Runs while an executing run's poll is active must stop that poll: a
  // silent get_run loop for a detail the view no longer shows would otherwise run
  // forever. Conditional because the live server may have no executing run.
  const executingId = await page.evaluate(`(() => {
    const row = [...document.querySelectorAll(".list.runs .run-row")].find((n) => n.querySelector(".chip-running"));
    const meta = row?.querySelector(".run-row-meta[data-run-id]");
    return meta ? Number(meta.getAttribute("data-run-id")) : null;
  })()`);
  if (executingId != null) {
    await page.click(`.list.runs .run-row-meta[data-run-id="${executingId}"]`);
    await eventually(
      () =>
        page.evaluate(
          `(document.querySelector(".run-detail .run-identity")?.textContent?.startsWith("run ${executingId} ") ?? false)`,
        ),
      { timeout: 30000, what: `the executing run ${executingId}` },
    );
    await page.evaluate(`window.__pipelinesSentTypes = []`);
    await clickDestination("Pipelines");
    await eventually(() => page.evaluate(`location.pathname === "/pipelines"`), {
      timeout: 15000,
      what: "Pipelines after leaving the executing run",
    });
    await new Promise((resolve) => setTimeout(resolve, 2600));
    const leftoverPolls = await page.evaluate(
      `(window.__pipelinesSentTypes || []).filter((type) => type === "get_run").length`,
    );
    if (leftoverPolls > 0) {
      throw new Error(
        `L6: leaving Runs left the executing run's poll active (${leftoverPolls} get_run frame(s) after 2.6s)`,
      );
    }
    console.log(
      `  left executing run ${executingId} for /pipelines; no get_run poll fired in the next 2.6s`,
    );
  } else {
    console.log("  --  no executing run available; skipped the leave-Runs poll check");
  }

  // The decide deep link is exercised only against a genuinely waiting run; with
  // none, say so rather than claiming a capture of a state that never rendered.
  if (discovered.waiting) {
    await openDeepLink(`/runs/${discovered.waiting}?action=decide`);
    await eventually(() => page.evaluate(`!!document.querySelector(".decision-sheet")`), {
      timeout: 30000,
      what: `the decision sheet for waiting run ${discovered.waiting}`,
    });
    const decideUrl = await currentLocation();
    if (decideUrl !== `/runs/${discovered.waiting}`) {
      throw new Error(`L6: the decide action was not consumed to /runs/${discovered.waiting}: ${decideUrl}`);
    }
    console.log(
      `  hard navigation /runs/${discovered.waiting}?action=decide opened the decision sheet, action consumed`,
    );
    await shot(`L6-deeplink-decide-${discovered.waiting}.png`);
    await page.evaluate(`document.querySelector(".decision-sheet .decision-cancel")?.click()`);
    await eventually(() => page.evaluate(`!document.querySelector(".decision-sheet")`), {
      timeout: 5000,
      what: "the decision sheet to close without sending",
    });
  } else {
    console.log(
      "  NOTE: the ?action=decide path was not exercised live (no waiting run on the server); test/routes.test.mjs covers its parsing",
    );
  }

  // A missing run falls back honestly: the list with real rows, /runs in the URL,
  // the server's refusal in the banner and no stale run identity.
  await openDeepLink("/runs/900000000");
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs" && document.querySelectorAll(".list.runs .run-row").length > 0`,
      ),
    { timeout: 30000, what: "the missing-run fallback to the Runs list" },
  );
  const missingState = await page.evaluate(`(() => ({
    path: location.pathname + location.search,
    rows: document.querySelectorAll(".list.runs .run-row").length,
    staleDetail: !!document.querySelector(".run-detail .run-identity"),
    error: document.querySelector(".banner.error")?.textContent ?? "",
    pageErrors: window.__pipelinesPageErrors ?? [],
  }))()`);
  if (missingState.path !== "/runs") throw new Error(`L6: missing run fell back to ${missingState.path}`);
  if (!missingState.rows) throw new Error("L6: missing run rendered no run rows");
  if (missingState.staleDetail) throw new Error("L6: missing run left a stale run detail");
  if (!missingState.error) throw new Error("L6: missing run showed no server refusal");
  if (missingState.pageErrors.length) {
    throw new Error(`L6: page errors: ${missingState.pageErrors.join(" | ")}`);
  }
  console.log(
    `  missing run -> /runs with ${missingState.rows} real rows, banner ${JSON.stringify(missingState.error.replace(/^Error:\\s*/, ""))}`,
  );
  await shot("L6-missing-run.png");

  // The same honest fallback for a pipeline the server does not have.
  await openDeepLink("/pipelines/900000000");
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/pipelines" && document.querySelectorAll("#pipelineRows button.row").length > 0`,
      ),
    { timeout: 30000, what: "the missing-pipeline fallback to the Pipelines list" },
  );
  const missingPipeline = await page.evaluate(`(() => ({
    path: location.pathname + location.search,
    rows: document.querySelectorAll("#pipelineRows button.row").length,
    staleDetail: !!document.querySelector("#detailTitle"),
    error: document.querySelector(".banner.error")?.textContent ?? "",
    pageErrors: window.__pipelinesPageErrors ?? [],
  }))()`);
  if (missingPipeline.path !== "/pipelines") {
    throw new Error(`L6: missing pipeline fell back to ${missingPipeline.path}`);
  }
  if (!missingPipeline.rows) throw new Error("L6: missing pipeline rendered no pipeline rows");
  if (missingPipeline.staleDetail) throw new Error("L6: missing pipeline left a stale pipeline detail");
  if (!missingPipeline.error) throw new Error("L6: missing pipeline showed no server refusal");
  if (missingPipeline.pageErrors.length) {
    throw new Error(`L6: page errors: ${missingPipeline.pageErrors.join(" | ")}`);
  }
  console.log(
    `  missing pipeline -> /pipelines with ${missingPipeline.rows} real rows, banner ${JSON.stringify(missingPipeline.error.replace(/^Error:\\s*/, ""))}`,
  );
  await shot("L6-missing-pipeline.png");

  // A destination move while disconnected still updates the URL. Connection now
  // lives in Settings, so connecting is one Settings visit and the destination is
  // then chosen again; the stale boot route must never win at connect time.
  await page.goto(`http://127.0.0.1:${port}/`);
  await page.waitForSelector("#shellStatus", { timeout: 15000 });
  await clickDestination("Runs");
  const preConnect = await currentLocation();
  if (preConnect !== "/runs") {
    throw new Error(`L6: a Runs choice before connecting left ${preConnect}, expected /runs`);
  }
  await connectFromSettings();
  await clickDestination("Runs");
  await eventually(
    () =>
      page.evaluate(
        `location.pathname === "/runs" && !!document.querySelector(".list.runs .run-row")`,
      ),
    { timeout: 30000, what: "the Runs destination after connecting" },
  );
  console.log("  a Runs choice made before connecting survived the first connection (/runs, list rendered)");

  // C13: this device's own chat changes — a local rename that wins over the
  // server's title, the hide + Undo report, and the seen/New marker. Every
  // action here is device-local (localStorage): nothing is sent to the server for
  // a rename or a hide, and the closed throwaway session from C8 is the row used
  // for the rename/hide captures so the operator's live session is never hidden
  // in a way the run leaves behind (Hide is undone before the marker step).
  step("C13: local rename/hide, the hide snackbar and the seen/New marker");
  await ensureConnected();
  await page.evaluate(`(() => {
    const tab = [...document.querySelectorAll('[data-destination]')].find((b) => b.getAttribute("data-destination") === "chat");
    if (tab) tab.click();
    return !!tab;
  })()`);
  await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 });

  // (a) Rename… opens the dialog: its exact title, the field label, the pre-fill
  // and the helper, and Save disabled while the trimmed value is blank.
  await openChatRowMenu(throwawayId);
  await page.click(`#chatRows .chat-row[data-session="${throwawayId}"] .chat-menu-rename`);
  await page.waitForSelector(".confirm-chat-rename", { timeout: 10000 });
  await page.waitForSelector("#chatRenameInput", { timeout: 5000 });
  const renameDialog = await page.evaluate(`(() => ({
    title: document.querySelector(".confirm-chat-rename .confirm-title")?.textContent ?? "",
    label: document.querySelector(".confirm-chat-rename .chat-rename-label")?.textContent ?? "",
    value: document.querySelector("#chatRenameInput")?.value ?? null,
    helper: document.querySelector(".confirm-chat-rename .chat-rename-helper")?.textContent ?? "",
    save: document.querySelector(".chat-rename-save")?.textContent ?? "",
    cancel: document.querySelector(".chat-rename-cancel")?.textContent ?? "",
  }))()`);
  if (renameDialog.title !== "Rename chat") throw new Error(`rename title: ${renameDialog.title}`);
  if (renameDialog.label !== "Name") throw new Error(`rename field label: ${renameDialog.label}`);
  if (renameDialog.value !== "New chat") throw new Error(`rename pre-fill: ${JSON.stringify(renameDialog.value)}`);
  if (
    renameDialog.helper !==
    "This name is shared with every device."
  ) {
    throw new Error(`rename helper: ${renameDialog.helper}`);
  }
  if (renameDialog.save !== "Save" || renameDialog.cancel !== "Cancel") {
    throw new Error(`rename buttons: ${JSON.stringify(renameDialog)}`);
  }
  // Typing toggles Save in place: blank (whitespace) disables it, a real value
  // enables it, with no re-render of the transcript underneath.
  await page.fill("#chatRenameInput", "   ");
  await eventually(
    () => page.evaluate(`document.querySelector(".chat-rename-save")?.disabled === true`),
    { timeout: 5000, what: "Save to disable while the name is blank" },
  );
  console.log(`  rename dialog: "${renameDialog.title}" / "${renameDialog.label}" pre-filled "${renameDialog.value}"; Save disabled while blank`);
  await shot("72-chat-rename-dialog.png");

  // (b) Save a locally-titled name; the row shows it now and after a server list
  // refresh, and the overrides JSON is in localStorage under its own key.
  await page.fill("#chatRenameInput", "Local capture chat");
  await eventually(
    () => page.evaluate(`document.querySelector(".chat-rename-save")?.disabled === false`),
    { timeout: 5000, what: "Save to enable for a non-blank name" },
  );
  await page.click(".chat-rename-save");
  await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-rename")`), {
    timeout: 5000,
    what: "the rename dialog to close on Save",
  });
  const renamedTitle = await eventually(
    async () => {
      const value = await page.evaluate(
        `document.querySelector('.chat-row[data-session="${throwawayId}"] .chat-row-title')?.textContent ?? ""`,
      );
      return value === "Local capture chat" ? value : false;
    },
    { timeout: 8000, what: "the renamed row to show its local title" },
  );
  console.log(`  row ${throwawayId} shows the local title "${renamedTitle}"`);
  // Re-enter the destination to reload the list from the server (the list has no
  // manual Refresh control); the local title must still win over the server's own.
  await clickDestination("Pipelines");
  await clickDestination("Chat");
  await page.waitForSelector("#chatRows .chat-row", { timeout: 15000 });
  await eventually(
    () =>
      page.evaluate(
        `document.querySelector('.chat-row[data-session="${throwawayId}"] .chat-row-title')?.textContent === "Local capture chat"`,
      ),
    { timeout: 15000, what: "the local title to survive a server list refresh" },
  );
  const storedOverrides = await page.evaluate(`localStorage.getItem("pipelines.web.chatOverrides.v1")`);
  const overridesJson = JSON.parse(storedOverrides ?? "{}");
  if (overridesJson.titles?.[throwawayId] !== "Local capture chat") {
    throw new Error(`the local title was not persisted: ${storedOverrides}`);
  }
  console.log(`  after a server list refresh the row still reads "${renamedTitle}"; localStorage ${storedOverrides}`);
  await shot("73-chat-renamed-row.png");

  // The final row's menu must not be clipped by the list's rounded-corner
  // overflow: assert the Hide item's own centre is the element a real click
  // would hit (the review measured the panel cut off at the list's bottom edge
  // on the last rows). The menu is closed again without acting on it.
  const lastRowId = await page.evaluate(
    `(() => {
      const rows = [...document.querySelectorAll("#chatRows .chat-row")]
        .filter((row) => !row.querySelector(".chat-activity-closed"));
      return rows.at(-1)?.dataset.session ?? "";
    })()`,
  );
  if (!lastRowId) throw new Error("C13: no last row to test the menu against");
  // Scroll the ROW LIST itself to the bottom (the list, not the page, is the
  // scroller), then the row, so the last-row menu check really starts with the row
  // on screen whatever the session count has grown to.
  await page.evaluate(`(() => {
    const list = document.querySelector(".chat-list") || document.querySelector("#chatRows");
    if (list) list.scrollTop = list.scrollHeight;
    document.querySelector('.chat-row[data-session="${lastRowId}"]')?.scrollIntoView({ block: "center" });
    return true;
  })()`);
  await new Promise((resolve) => setTimeout(resolve, 250));
  await openChatRowMenu(lastRowId);
  const lastMenuHit = await page.evaluate(`(() => {
    const button = document.querySelector('.chat-row[data-session="${lastRowId}"] .chat-menu-hide');
    if (!button) return { found: false };
    const rect = button.getBoundingClientRect();
    const x = rect.left + rect.width / 2;
    const y = rect.top + rect.height / 2;
    const hit = document.elementFromPoint(x, y);
    return {
      found: true,
      label: button.textContent,
      hit: hit === button || button.contains(hit),
      rect: { top: rect.top, bottom: rect.bottom },
      viewport: window.innerHeight,
      listBottom: document.querySelector(".chat-list")?.getBoundingClientRect().bottom ?? null,
    };
  })()`);
  if (!lastMenuHit.found || !lastMenuHit.hit) {
    throw new Error(`C13: the last row's menu item is not clickable: ${JSON.stringify(lastMenuHit)}`);
  }
  console.log(
    `  last row ${lastRowId}: "${lastMenuHit.label}" is hit-testable at its centre (menu bottom ${Math.round(lastMenuHit.rect.bottom)}, viewport ${lastMenuHit.viewport})`,
  );
  await page.click(`#chatRows .chat-row[data-session="${lastRowId}"] .chat-menu-button`);
  await eventually(
    () => page.evaluate(`!document.querySelector('.chat-row[data-session="${lastRowId}"] .chat-menu-panel')`),
    { timeout: 5000, what: "the last row's menu to close again" },
  );

  // (c) Hide the row the conversation is open on: it disappears, its conversation
  // closes, and one snackbar carries the exact message and Undo. Undo puts the
  // row back and leaves the server untouched.
  // Open a conversation when none is open: the preceding sections close their
  // throwaway through /close, and which chat is open is section state, not
  // something this device-local check should depend on.
  let activeBeforeHide = await page.evaluate(
    `document.querySelector(".chat-row.selected")?.dataset.session ?? ""`,
  );
  if (!activeBeforeHide) {
    await page.evaluate(`document.querySelector("#chatRows .chat-row")?.click()`);
    await page.waitForSelector("#chatInput", { timeout: 15000 });
    activeBeforeHide = await page.evaluate(
      `document.querySelector(".chat-row.selected")?.dataset.session ?? ""`,
    );
  }
  if (!activeBeforeHide) throw new Error("C13: no conversation was open before the hide");
  await openChatRowMenu(activeBeforeHide);
  await page.click(`#chatRows .chat-row[data-session="${activeBeforeHide}"] .chat-menu-hide`);
  await page.waitForSelector(".chat-snackbar", { timeout: 10000 });
  const hiddenState = await page.evaluate(`(() => ({
    row: !!document.querySelector('.chat-row[data-session="${activeBeforeHide}"]'),
    snackbars: document.querySelectorAll(".chat-snackbar").length,
    message: document.querySelector(".chat-snackbar-message")?.textContent ?? "",
    undo: document.querySelector(".chat-snackbar-undo")?.textContent ?? "",
    hint: document.querySelector(".chat-detail .empty .muted")?.textContent ?? "",
    composer: !!document.querySelector("#chatComposer"),
    hidden: JSON.parse(localStorage.getItem("pipelines.web.chatOverrides.v1") ?? "{}").hidden ?? [],
  }))()`);
  if (hiddenState.row) throw new Error(`C13: the hidden row is still listed: ${JSON.stringify(hiddenState)}`);
  if (hiddenState.snackbars !== 1) throw new Error(`C13: expected one snackbar, got ${hiddenState.snackbars}`);
  if (hiddenState.message !== "Hidden on this device; the chat stays on the server.") {
    throw new Error(`C13: snackbar message ${JSON.stringify(hiddenState.message)}`);
  }
  if (hiddenState.undo !== "Undo") throw new Error(`C13: snackbar action ${JSON.stringify(hiddenState.undo)}`);
  if (hiddenState.composer || hiddenState.hint !== "Select a chat to open it.") {
    throw new Error(`C13: hiding the open conversation did not close it: ${JSON.stringify(hiddenState)}`);
  }
  if (!hiddenState.hidden.includes(activeBeforeHide)) {
    throw new Error(`C13: the hidden id was not persisted: ${JSON.stringify(hiddenState.hidden)}`);
  }
  console.log(
    `  hid ${activeBeforeHide}: row gone, conversation closed, one snackbar "${hiddenState.message}" with "${hiddenState.undo}"`,
  );
  await shot("74-chat-hide-snackbar.png");

  // Undo restores the row (and only here: the conversation stays closed, exactly
  // as Android's undoRemove does) and clears the hidden list.
  await page.click(".chat-snackbar-undo");
  await eventually(
    () =>
      page.evaluate(
        `!!document.querySelector('.chat-row[data-session="${activeBeforeHide}"]') && !document.querySelector(".chat-snackbar")`,
      ),
    { timeout: 8000, what: "Undo to restore the hidden row and clear the snackbar" },
  );
  const afterUndo = await page.evaluate(
    `JSON.parse(localStorage.getItem("pipelines.web.chatOverrides.v1") ?? "{}").hidden ?? []`,
  );
  if (afterUndo.includes(activeBeforeHide)) {
    throw new Error(`C13: Undo left the id hidden: ${JSON.stringify(afterUndo)}`);
  }
  console.log(`  Undo restored ${activeBeforeHide}; hidden ids now ${JSON.stringify(afterUndo)}`);

  // (d) The "New" marker, honestly: the restart rule. A fresh throwaway is
  // prompted and stopped, and the page is then reloaded — its threads are empty
  // while the persisted watermark predates the turn, so the non-active row reads
  // New after the reconnect. First reopen the operator conversation so `/new`
  // has a directory.
  await page.evaluate(`document.querySelector('.chat-row[data-session="${sessionId}"]')?.click()`);
  await page.waitForSelector("#chatTranscript", { timeout: 20000 });
  await page.fill("#chatInput", "/new");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
  );
  const newMarkerId = await eventually(
    async () => {
      const id = await page.evaluate(`document.querySelector(".chat-row.selected")?.dataset.session ?? ""`);
      return id && id !== sessionId && id !== throwawayId ? id : false;
    },
    { timeout: 25000, what: "the fresh throwaway session to open for the New-marker step" },
  );
  await page.waitForSelector("#chatInput", { timeout: 10000 });
  // The watermark for this session is seeded at its creation recency; a real
  // prompt then advances the server's updated_at past it.
  await page.fill("#chatInput", "Reply with exactly one word: hi");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", ctrlKey: true, bubbles: true, cancelable: true }))`,
  );
  await page.waitForSelector("#chatStop", { timeout: 20000 });
  await page.click("#chatStop");
  await eventually(
    () =>
      page.evaluate(
        `[...document.querySelectorAll(".chat-command-notice-text")].some((n) => n.textContent === "Stopping the running turn…")`,
      ),
    { timeout: 15000, what: "the New-marker throwaway turn to be interrupted" },
  );
  const watermarkBefore = await page.evaluate(
    `JSON.parse(localStorage.getItem("pipelines.web.chatSeen.v1") ?? "{}").seenAt?.[${JSON.stringify(newMarkerId)}] ?? 0`,
  );
  // A reload empties the page's threads and re-reads the server list: the row is
  // no longer active, and its server recency has moved past the stored watermark.
  await page.evaluate(`location.reload()`);
  await page.waitForSelector("#shellStatus", { timeout: 15000 });
  // A reload re-opens the persisted endpoint; wait for the socket rather than
  // clicking a control that no longer lives in the chrome.
  await ensureConnected(30000);
  await clickDestination("Chat");
  await page.waitForSelector("#chatRows .chat-row", { timeout: 25000 });
  const newMarker = await eventually(
    async () => {
      const value = await page.evaluate(`(() => {
        const row = document.querySelector('.chat-row[data-session="${newMarkerId}"]');
        const word = row?.querySelector(".chat-activity-new")?.textContent ?? "";
        return { word, selected: !!row?.classList.contains("selected") };
      })()`);
      return value.word === "New" ? value : false;
    },
    { timeout: 15000, what: "the non-active row to carry the New marker after the reload" },
  );
  const watermarkAfter = await page.evaluate(
    `JSON.parse(localStorage.getItem("pipelines.web.chatSeen.v1") ?? "{}").seenAt?.[${JSON.stringify(newMarkerId)}] ?? 0`,
  );
  console.log(
    `  after the reload, non-active ${newMarkerId} carries the New marker ` +
      `(watermark before the turn ${watermarkBefore}, after the reload ${watermarkAfter})`,
  );
  await shot("75-chat-new-marker.png");

  // Cleanup: open the New-marker throwaway and /close it, so the run leaves no
  // open session of its own (the rename override on the earlier throwaway is
  // device-local and the browser profile is temporary).
  await page.evaluate(`document.querySelector('.chat-row[data-session="${newMarkerId}"]')?.click()`);
  await page.waitForSelector("#chatInput", { timeout: 20000 });
  await page.fill("#chatInput", "/close");
  await page.evaluate(
    `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,
  );
  await page.waitForSelector(".confirm-chat-close", { timeout: 15000 });
  await page.click(".confirm-chat-close .confirm-accept");
  await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), {
    timeout: 15000,
    what: "the New-marker throwaway to close",
  });
  await expandClosedChats();
  await eventually(
    () =>
      page.evaluate(
        `!!document.querySelector('.chat-row[data-session="${newMarkerId}"] .chat-activity-closed')`,
      ),
    { timeout: 15000, what: "the New-marker throwaway to show as closed" },
  );
  console.log(`  cleanup: /close closed the New-marker throwaway ${newMarkerId}`);

  await runMarkdownViewerChecks();

  await runShellChecks();

  console.log("\nUI SMOKE PASS");
  for (const shot of shots) console.log(`  ${shot}`);
  }
} catch (error) {
  failed = true;
  console.error(`\nUI SMOKE FAIL: ${error.message}`);
  if (page) {
    try {
      shots.push(await page.screenshot(`${out}/failure.png`));
      console.error(`  failure capture: ${shots.at(-1)}`);
      console.error((await page.text()).split("\n").slice(0, 25).join("\n"));
    } catch {
      // the page may be gone; the error above is the real signal
    }
  }
} finally {
  // Best-effort cleanup of C2's real session: if the run failed between creating
  // it and its own close, close it now so it never sits active in the operator's
  // list. Nothing is deleted either way.
  if (createdSessionId && page) {
    try {
      const closed = await page.evaluate(
        `!!document.querySelector('.chat-row[data-session="${createdSessionId}"] .chat-activity-closed')`,
      );
      if (!closed) {
        await page.evaluate(
          `document.querySelector('#chatRows .chat-row[data-session="${createdSessionId}"]')?.click()`,
        );
        await page.waitForSelector("#chatInput", { timeout: 8000 }).catch(() => {});
        await page.evaluate(`(async () => {
          const input = document.querySelector("#chatInput");
          if (!input) return;
          input.value = "/close";
          input.dispatchEvent(new Event("input", { bubbles: true }));
          input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
          await new Promise((resolve) => setTimeout(resolve, 400));
          document.querySelector(".confirm-chat-close .confirm-accept")?.click();
        })()`);
        await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), {
          timeout: 8000,
          what: "the C2 cleanup close to finish",
        }).catch(() => {});
        console.log(`  cleanup: closed the C2-created session ${createdSessionId}`);
      }
    } catch {
      // the page may already be gone; the failure above is the real signal
    }
  }
  // Best-effort cleanup of the R10 throwaway run: if the smoke failed before it
  // canceled the run, cancel it now so it does not keep executing.
  if (liveRunId) {
    try {
      await cancelLiveRun(liveRunId);
      console.log(`  cleanup: canceled the R10 run ${liveRunId}`);
    } catch {
      // the CLI may be gone; the failure above is the real signal
    }
  }
  if (liveRunScratch) {
    try {
      await rm(liveRunScratch, { recursive: true, force: true });
    } catch {
      // leave the scratch directory rather than failing cleanup
    }
  }
  // Best-effort cleanup of the R11 decision runs: cancel only these tracked runs
  // while they are still active (never another run), then remove only the temp
  // workspaces/definition directories this smoke created. The uniquely submitted
  // human-only pipeline definition is intentionally retained and named in PARITY.
  for (const id of decideRunIds) {
    try {
      const detail = await inspectRun(id);
      if (["queued", "running", "waiting"].includes(detail.status)) {
        const result = await runCli(
          ["cancel", String(id), "--yes", "--json", "--url", pipelineUrl],
          tmpdir(),
        );
        console.log(
          result.code === 0
            ? `  cleanup: canceled the R11 decision run ${id} (${detail.status})`
            : `  cleanup: cancel ${id} exited ${result.code}`,
        );
      }
    } catch {
      // the server/CLI may be gone; the failure above is the real signal
    }
  }
  for (const dir of decideScratchDirs) {
    try {
      await rm(dir, { recursive: true, force: true });
    } catch {
      // leave the scratch directory rather than failing cleanup
    }
  }
  // Best-effort cleanup of the throwaway session this run created: if the run
  // failed before its `/close` step, close it now so it is never left open. It
  // closes only the throwaway: if the open conversation is something else (the
  // failure left the operator's session on screen), the cleanup must never close
  // that instead.
  if (throwawaySessionId && page) {
    try {
      await page.evaluate(`(async () => {
        const open = document.querySelector("#chatTranscript")?.dataset?.session ?? "";
        if (open !== ${JSON.stringify(throwawaySessionId)}) return;
        const input = document.querySelector("#chatInput");
        if (!input) return;
        input.value = "/close";
        input.dispatchEvent(new Event("input", { bubbles: true }));
        input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
        await new Promise((resolve) => setTimeout(resolve, 400));
        document.querySelector(".confirm-chat-close .confirm-accept")?.click();
      })()`);
      await eventually(() => page.evaluate(`!document.querySelector(".confirm-chat-close")`), {
        timeout: 8000,
        what: "the cleanup close to finish",
      }).catch(() => {});
      console.log(`  cleanup: closed the throwaway session ${throwawaySessionId}`);
    } catch {
      // the page may already be gone; the failure above is the real signal
    }
  }
  await page?.close().catch(() => {});
  await browser?.close().catch(() => {});
  serve?.kill("SIGTERM");
  await eventually(async () => serve?.exitCode != null, { timeout: 5000, what: "serve exit" }).catch(
    () => {},
  );
  if (!failed) await rm(`${out}/failure.png`, { force: true }).catch(() => {});
}

process.exit(failed ? 1 : 0);