Skip to content

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.

fix(cli): ignore answered dialogs above the live composer (#1012)

Dee49c2395c0e1f
diff --git a/cli/e2e/harness/fakeEngine.mjs b/cli/e2e/harness/fakeEngine.mjs
index 436b6acfd..6643b49af 100644
--- a/cli/e2e/harness/fakeEngine.mjs
+++ b/cli/e2e/harness/fakeEngine.mjs
@@ -680,7 +680,13 @@ export async function run(engine, config = {}, { native = false } = {}) {
const dialogKeys = (chunk) => {
for (const key of chunk.match(/\x1b\[200~[\s\S]*?(?:\x1b\[201~|$)|\x1b\[[AB]|\x1b|\r|\n|./gs) ?? []) {
if (!dialog) return
- const settle = (choice) => { eraseDialog(); const asked = dialog; dialog = null; notes = asked.notes ?? ''; drawComposer(); asked.resolve(choice) }
+ const settle = (choice) => {
+ // An old dialog can survive in tmux scrollback after a redraw/resize, as in the chaos run.
+ // Keep the whole frame for that acceptance case; the composer below it is the live screen.
+ if (dialog.keepInScrollback) process.stdout.write('\r\n')
+ else eraseDialog()
+ const asked = dialog; dialog = null; notes = asked.notes ?? ''; drawComposer(); asked.resolve(choice)
+ }
const rows = dialog.kind === 'permit' ? 3 : engine === 'claude' ? CHOICES.length : CHOICES.length + 1
if (key.startsWith('\x1b[200~')) {
if (dialog.kind !== 'permit' && engine === 'codex') dialog.notes = key.slice(6).replace(/\x1b\[201~$/, '')
@@ -1202,11 +1208,11 @@ export async function run(engine, config = {}, { native = false } = {}) {
await permitted(command, row)
return
}
- if (directive?.[1] === 'ask') {
+ if (directive?.[1] === 'ask' || directive?.[1] === 'askscrollback') {
// A real engine thinks before it asks; a dialog already on screen when its turn began reads to the
// daemon as the previous turn's (askQuestion.ts `noteTurnStart`).
await new Promise((resolve) => setTimeout(resolve, 2_000))
- const choice = await new Promise((resolve) => { eraseComposer(); dialog = { cursor: 0, drawn: 0, resolve }; drawDialog() })
+ const choice = await new Promise((resolve) => { eraseComposer(); dialog = { cursor: 0, drawn: 0, keepInScrollback: directive[1] === 'askscrollback', resolve }; drawDialog() })
if (choice === null) {
say('(question cancelled)\r\n')
await finish('(question cancelled)')
diff --git a/cli/e2e/questions.e2e.ts b/cli/e2e/questions.e2e.ts
index 612994340..f5e75272d 100644
--- a/cli/e2e/questions.e2e.ts
+++ b/cli/e2e/questions.e2e.ts
@@ -75,6 +75,26 @@ describe('an agent asks a question', () => {
client.close()
})
+ it.each(engines)('%s: an answered question in scrollback does not block the next prompt or accept a late answer', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `ask-scrollback-${engine}`)
+ const asked = client.next((frame) => frame.type === 'commander_question' && frame.agentId === agent.id, 30_000, 'commander_question')
+ client.send('message', { agentId: agent.id, content: '!askscrollback' })
+ const question = (await asked).payload!
+ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ const answers = { [question.questions[0].q]: 'Coffee' }
+ expect((await answer(client, agent.id, question.requestId, answers)).error).toBeUndefined()
+ await ended
+ // Read the same history the daemon reads, proving the old dialog really remains in the capture.
+ const capture = await d.tmux.run('capture-pane', '-p', '-e', '-J', '-S', '-100', '-t', agent.tmuxPane)
+ expect(capture).toContain(engine === 'claude' ? 'Enter to select' : 'enter to submit answer')
+ expect(capture).toContain('you chose Coffee')
+ expect((await answer(client, agent.id, question.requestId, answers)).error).toBe('STALE_QUESTION')
+ await turn(client, agent.id, 'after the answered question in scrollback')
+ client.close()
+ })
+
it.each(engines)('%s: a permission the agent asks for reaches the window, and Yes there runs the command, No does not', async (engine) => {
const d = await fresh()
const client = await LocalClient.connect(d)
diff --git a/cli/src/lib/askQuestion.scrollback.spec.ts b/cli/src/lib/askQuestion.scrollback.spec.ts
new file mode 100644
index 000000000..20c899745
--- /dev/null
+++ b/cli/src/lib/askQuestion.scrollback.spec.ts
@@ -0,0 +1,52 @@
+import { readFileSync } from 'node:fs'
+import { describe, expect, it, vi } from 'vitest'
+import { AskQuestionController, parseEngineQuestionPane, questionRequestId, type QuestionView } from './askQuestion.js'
+import { messageHold } from './messageHold.js'
+import { CLAUDE_PROMPT, CODEX_PROMPT } from './__fixtures__/rewindPickers.js'
+import type { RegisteredSession } from './registry.js'
+
+const fixture = (name: string) => readFileSync(new URL(`./__fixtures__/${name}.txt`, import.meta.url), 'utf8')
+
+it('keeps a partial current Codex dialog open below an old composer', () => {
+ const partial = `${CODEX_PROMPT}\n\n 3. None of the above\n enter to submit answer | esc to interrupt`
+ expect(parseEngineQuestionPane('codex', partial)).toMatchObject({ kind: 'question', partial: true })
+ expect(messageHold('codex', partial)).toBe('question_open')
+})
+
+// A capture includes scrollback. Compose the recorded dialog and the engine's composer to reproduce
+// the long chaos run: the answer was accepted, but the old dialog kept every later prompt on hold.
+describe.each([
+ ['claude', 'question-single', CLAUDE_PROMPT],
+ ['codex', 'question-codex', CODEX_PROMPT],
+] as const)('%s with an answered dialog in scrollback', (engine, name, composer) => {
+ const dialog = fixture(name)
+ const answered = `${dialog}\n\n${composer}`
+
+ it('does not treat the old question or permission as open below a live composer', () => {
+ for (const old of [dialog, fixture(`permission-${engine}`)]) {
+ expect(parseEngineQuestionPane(engine, `${old}\n\n${composer}`)).toBeNull()
+ expect(messageHold(engine, `${old}\n\n${composer}`)).toBeNull()
+ }
+ })
+
+ it('still holds messages when the current dialog is below an old composer', () => {
+ expect(messageHold(engine, `${composer}\n\n${dialog}`)).toBe('question_open')
+ expect(messageHold(engine, `${composer}\n\n${fixture(`permission-${engine}`)}`)).toBe('permission_open')
+ })
+
+ it('finishes an answer once the composer returns, and refuses a late answer without keys', async () => {
+ const session = { agentId: 'a1', sessionId: 's1', engine } as RegisteredSession
+ const capture = vi.fn().mockResolvedValueOnce(dialog).mockResolvedValue(answered)
+ const sendKey = vi.fn().mockResolvedValue(true)
+ const sendText = vi.fn().mockResolvedValue(true)
+ const controller = new AskQuestionController({ getSession: () => session, capture, sendKey, sendText, wait: async () => {} })
+ const view = parseEngineQuestionPane(engine, dialog) as QuestionView
+ const payload = { agentId: 'a1', requestId: questionRequestId('s1', view), answers: { [view.question]: view.rows[0].label } }
+ expect(await controller.answer(payload)).toEqual({ ok: true })
+ const count = sendKey.mock.calls.length
+ expect(count).toBeGreaterThan(0)
+ expect(await controller.answer(payload)).toMatchObject({ ok: false, error: 'STALE_QUESTION' })
+ expect(sendKey).toHaveBeenCalledTimes(count)
+ expect(sendText).not.toHaveBeenCalled()
+ })
+})
diff --git a/cli/src/lib/askQuestion.ts b/cli/src/lib/askQuestion.ts
index 7e38e3fed..9dcc77e41 100644
--- a/cli/src/lib/askQuestion.ts
+++ b/cli/src/lib/askQuestion.ts
@@ -34,6 +34,7 @@ import { parseGrokQuestionPane } from '../engines/grok/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'
+import { composerState } from './composerScreen.js'
/** Device-facing question shape — byte-for-byte the hosted runtime’s `commanderQuestions()` output. */
export interface ShapedQuestion {
@@ -180,6 +181,16 @@ function lowest(...found: Array<FoundDialog | null>): PaneView {
* its own parser rather than more branches in here.
*/
export function parseEngineQuestionPane(engine: AgentEngine, capture: string): PaneView {
+ if (engine === 'claude' || engine === 'codex') {
+ const found = locateQuestionPane(capture)
+ if (!found) return null
+ // Captures include scrollback. The long chaos run returned to the composer after answering, but
+ // the old dialog still held every prompt as question_open. A full composer BELOW that dialog makes
+ // it history. One above it proves nothing: a newer dialog can have only its last rows visible.
+ const below = capture.split('\n').slice(found.at + 1).join('\n')
+ if (composerState(engine, below) !== 'absent') return null
+ return engine === 'codex' ? withCodexLabels(found.view) : found.view
+ }
// Devin's two dialogs are told apart by one word in the footer (`↵ select` vs `↵ confirm`), so they can
// 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)))
@@ -224,7 +235,6 @@ export function parseEngineQuestionPane(engine: AgentEngine, capture: string): P
const plain = unframe(capture)
return lowest(asPermission(locateKiloQuestion(capture)), locateOpencodeReview(plain), locateQuestionPane(plain))
}
- if (engine === 'codex') return withCodexLabels(parseQuestionPane(capture))
return parseQuestionPane(capture)
}