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.

questions: the pane before a prompt the daemon typed stands for "before its turn" (#961)

Deeaeae06af294f
diff --git a/cli/src/core/input.spec.ts b/cli/src/core/input.spec.ts
index 920247d1a..fd1a8d31c 100644
--- a/cli/src/core/input.spec.ts
+++ b/cli/src/core/input.spec.ts
@@ -174,6 +174,27 @@ describe('the session input controller\'s dependencies', () => {
expect(answers).toEqual(['permission_open', null, null])
})
+ it('hands the pane as it read it to the question watcher right before typing a prompt, and not when it holds the prompt', async () => {
+ const order: string[] = []
+ const promptTyped = vi.fn((session: { agentId: string }, capture: string | null) => { order.push(`prompt ${session.agentId} ${capture === READY_PANE}`) })
+ const screens = [READY_PANE, CLAUDE_PERMISSION]
+ const write = messageWriter({
+ resolve: (id) => agents.get(id),
+ terminal: {
+ validateTerminal: vi.fn(async () => true),
+ captureTerminal: vi.fn(async () => screens.shift() ?? READY_PANE),
+ submitTerminalAction: vi.fn(async () => { order.push('submit'); return ok }),
+ } as unknown as InputDeps['terminal'],
+ promptTyped,
+ })
+ expect(await write('a1', 'hello')).toBe(ok)
+ // Before the paste: the turn the prompt starts is seen to start only later.
+ expect(order).toEqual(['prompt a1 true', 'submit'])
+ // A permission prompt on the pane: nothing typed, nothing said.
+ expect(await write('a1', 'hello')).toMatchObject({ dispatch: 'not_started', reason: 'permission_open' })
+ expect(promptTyped).toHaveBeenCalledTimes(1)
+ })
+
it('holds the Enter back when the pane cannot be read for three seconds, or shows no composer that long', async () => {
for (const screen of [null, '✻ Welcome to Claude Code']) {
let reads = 0
diff --git a/cli/src/core/input.ts b/cli/src/core/input.ts
index cdd8b1b79..66bddacdf 100644
--- a/cli/src/core/input.ts
+++ b/cli/src/core/input.ts
@@ -46,6 +46,9 @@ export interface InputDeps {
/** Command Code's normalizer for a session, which opens its turn on our paste. */
commandcode: (sessionId: string) => CommandCodeNormalizer | undefined
emit: (sessionId: string, events: LiveEvent[]) => void
+ /** A prompt is about to be typed, and `capture` is the pane as read right before: what is on it belongs
+ * to the turns before the one it starts (the question watcher's, lib/askQuestion.ts `notePrompt`). */
+ promptTyped?: (session: RegisteredSession, capture: string | null) => void
}
/**
@@ -61,7 +64,7 @@ export interface InputDeps {
* reason. `submitTerminalAction` is called here and nowhere else in this file, and input.spec.ts keeps
* it so.
*/
-export function messageWriter({ resolve, terminal: { captureTerminal, validateTerminal, submitTerminalAction } }: Pick<InputDeps, 'resolve' | 'terminal'>) {
+export function messageWriter({ resolve, terminal: { captureTerminal, validateTerminal, submitTerminalAction }, promptTyped }: Pick<InputDeps, 'resolve' | 'terminal' | 'promptTyped'>) {
return async (id: string, text: string, hold?: (session: RegisteredSession, capture: string | null) => string | null): Promise<TerminalActionResult> => {
const session = resolve(id)
if (!session) return terminalActionNotStarted('terminal agent is unavailable')
@@ -74,6 +77,7 @@ export function messageWriter({ resolve, terminal: { captureTerminal, validateTe
const capture = await captureTerminal(id)
const reason = hold?.(session, capture) ?? messageHold(session.engine, capture)
if (reason) return terminalActionNotStarted(reason)
+ promptTyped?.(session, capture)
return submitTerminalAction(id, text, {
beforeEnter: async () => {
// A read that came back empty, or a composer caught between frames, is asked again for a moment
diff --git a/cli/src/core/main.ts b/cli/src/core/main.ts
index a612d3b7f..b6886eee5 100644
--- a/cli/src/core/main.ts
+++ b/cli/src/core/main.ts
@@ -1000,6 +1000,8 @@ async function runForeground(session: AuthSession | null): Promise<void> {
commandcode: (sessionId) => commandcodeNormalizers.get(sessionId),
// Reassigned further down (the funnel): always the current one.
emit: (sessionId, events) => emitSessionEvents(sessionId, events),
+ // Declared further down: read as a prompt is typed, never now.
+ promptTyped: (session, capture) => { if (session.sessionId) questionWatcher.notePrompt(session.sessionId, capture) },
})
const input = inputs.input
const deviceInput = inputs.deviceInput
diff --git a/cli/src/lib/askQuestion.more.spec.ts b/cli/src/lib/askQuestion.more.spec.ts
index 63816612c..1514d1b32 100644
--- a/cli/src/lib/askQuestion.more.spec.ts
+++ b/cli/src/lib/askQuestion.more.spec.ts
@@ -761,3 +761,77 @@ describe('QuestionWatcher.noteTurnStart — the edges', () => {
expect(seen).toEqual([idOf(DRINK), idOf(DRINK)])
})
})
+
+describe('QuestionWatcher.notePrompt — the pane before a prompt the daemon typed', () => {
+ const settle = async (): Promise<void> => { for (let i = 0; i < 5; i++) await Promise.resolve() }
+ afterEach(() => { vi.useRealTimers() })
+ const watcher = (session: Partial<RegisteredSession> | undefined, capture: () => string | null) => {
+ const seen: string[] = []
+ const read = vi.fn(async () => capture())
+ const w = new QuestionWatcher({
+ getSession: () => session as RegisteredSession | undefined,
+ capture: read, hasDevice: () => true,
+ onQuestion: (_s, id) => { seen.push(id) },
+ })
+ const tick = (): Promise<void> => (w as unknown as { tick: (s: string) => Promise<void> }).tick('s1')
+ return { w, seen, read, tick }
+ }
+
+ it('announces the question an engine drew before its turn was seen to start, which the start\'s own read took for the turn before\'s', async () => {
+ // The pane, as the turn is seen to start: the turn's own question is already up (the start was late).
+ const late = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ late.w.noteTurnStart('s1')
+ await settle()
+ await late.tick()
+ expect(late.seen).toEqual([])
+ // The same, with the pane as the daemon read it right before typing the prompt: no dialog then.
+ const typed = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ typed.w.notePrompt('s1', CLOSED)
+ typed.w.noteTurnStart('s1')
+ await settle()
+ expect(typed.read).not.toHaveBeenCalled()
+ await typed.tick()
+ expect(typed.seen).toEqual([idOf(DRINK)])
+ })
+
+ it('still keeps a dialog that was on the pane as the prompt was typed from being announced as the new turn\'s', async () => {
+ const { w, seen, tick } = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ w.notePrompt('s1', DRINK)
+ w.noteTurnStart('s1')
+ await tick()
+ expect(seen).toEqual([])
+ })
+
+ it('reads the pane at the turn\'s start as before, for a prompt it did not type, one whose read failed, an engine it does not watch, or one typed long ago', async () => {
+ vi.useFakeTimers()
+ const drawn = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ drawn.w.notePrompt('s1', null)
+ drawn.w.noteTurnStart('s1')
+ await settle()
+ expect(drawn.read).toHaveBeenCalledTimes(1)
+ const stale = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ stale.w.notePrompt('s1', CLOSED)
+ vi.advanceTimersByTime(120_001)
+ stale.w.noteTurnStart('s1')
+ await settle()
+ expect(stale.read).toHaveBeenCalledTimes(1)
+ // Used once: the turn after reads its own start.
+ const once = watcher({ sessionId: 's1', engine: 'claude' }, () => DRINK)
+ once.w.notePrompt('s1', CLOSED)
+ once.w.noteTurnStart('s1')
+ once.w.noteTurnStart('s1')
+ await settle()
+ expect(once.read).toHaveBeenCalledTimes(1)
+ for (const session of [undefined, { sessionId: 's1', engine: 'gemini' }] as const) {
+ const ignored = watcher(session as Partial<RegisteredSession> | undefined, () => DRINK)
+ ignored.w.notePrompt('s1', CLOSED)
+ expect((ignored.w as unknown as { beforePrompt: Map<string, unknown> }).beforePrompt.size).toBe(0)
+ }
+ // An engine with no name is read as Claude; everything is forgotten when the watcher stops.
+ const nameless = watcher({ sessionId: 's1' }, () => DRINK)
+ nameless.w.notePrompt('s1', DRINK)
+ expect((nameless.w as unknown as { beforePrompt: Map<string, unknown> }).beforePrompt.size).toBe(1)
+ nameless.w.stopAll()
+ expect((nameless.w as unknown as { beforePrompt: Map<string, unknown> }).beforePrompt.size).toBe(0)
+ })
+})
diff --git a/cli/src/lib/askQuestion.ts b/cli/src/lib/askQuestion.ts
index c9b19ae56..7e38e3fed 100644
--- a/cli/src/lib/askQuestion.ts
+++ b/cli/src/lib/askQuestion.ts
@@ -1047,6 +1047,10 @@ export interface QuestionWatcherDeps {
}
const POLL_MS = 1500
+/** How long the pane read before a prompt stands for the turn it starts: past this, that turn's own start
+ * is read instead (a prompt that never became a turn). Turns were seen to start 8 s after their prompt
+ * under load. */
+const PROMPT_BASELINE_MS = 120_000
// Consecutive empty polls before a question is declared gone.
//
// NOT 1. A capture taken mid-repaint parses as no-dialog, and announcing a close on that would yank a
@@ -1087,6 +1091,8 @@ export class QuestionWatcher {
private readonly blocked = new Map<string, string>()
/** sessionId → the dialog that was ALREADY on the pane when this turn began. */
private readonly preTurn = new Map<string, string>()
+ /** sessionId → the pane as the daemon read it right before typing a prompt: its dialog, if any, and when. */
+ private readonly beforePrompt = new Map<string, { fingerprint: string | null; at: number }>()
constructor(private readonly deps: QuestionWatcherDeps) {}
@@ -1113,6 +1119,14 @@ export class QuestionWatcher {
noteTurnStart(sessionId: string): void {
this.preTurn.delete(sessionId)
this.cancelPending(sessionId)
+ // The daemon typed this turn's prompt, and read the pane as it did: what was on it then is the turns
+ // before's, and nothing drawn since is (notePrompt says why).
+ const typed = this.beforePrompt.get(sessionId)
+ this.beforePrompt.delete(sessionId)
+ if (typed && Date.now() - typed.at <= PROMPT_BASELINE_MS) {
+ if (typed.fingerprint) this.preTurn.set(sessionId, typed.fingerprint)
+ return
+ }
const pending = { cancelled: false }
this.pendingBaselines.set(sessionId, pending)
void (async () => {
@@ -1137,6 +1151,27 @@ export class QuestionWatcher {
})()
}
+ /**
+ * The daemon is typing a prompt, and `capture` is the pane as it read it right before (core/input.ts
+ * messageWriter): the honest "before this turn" for the turn that prompt starts.
+ *
+ * A capture taken when the turn is SEEN to start can be too late for that. The turn is seen to start
+ * from the transcript, after the prompt is confirmed typed, and an engine can draw its question before
+ * that: measured in the soak run (e2e/endurance.e2e.ts) under load, Codex took the prompt at 20:42:54.9
+ * (its prompt hook), drew its question a moment later, and the turn was seen to start at 20:43:02.2, 7 s
+ * on. The capture at that start held the turn's own question, recorded it as the turns before's, and it
+ * was never announced: the agent waited on a question no window or device was shown.
+ *
+ * A read that failed supplies nothing: the turn's start reads the pane itself, as before.
+ */
+ notePrompt(sessionId: string, capture: string | null): void {
+ const session = this.deps.getSession(sessionId)
+ if (capture === null || !session || (session.engine && !pollsQuestions(session.engine))) return
+ const view = parseEngineQuestionPane(session.engine ?? 'claude', capture)
+ const dialog = view && view.kind === 'question' && view.question && view.rows.length > 0 ? fingerprintOf(view) : null
+ this.beforePrompt.set(sessionId, { fingerprint: dialog, at: Date.now() })
+ }
+
/** Poll this session's pane while its turn is open (called on turn_started). */
start(sessionId: string): void {
if (this.watching.has(sessionId)) return
@@ -1184,6 +1219,7 @@ export class QuestionWatcher {
for (const pending of this.pendingPolls.values()) pending.cancelled = true
for (const pending of this.pendingBaselines.values()) pending.cancelled = true
this.preTurn.clear()
+ this.beforePrompt.clear()
this.blocked.clear()
this.last.clear()
this.lastId.clear()