From ee3db6fe8cc651ea72ee68271e156715a9d40f9a Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:25:29 -0800 Subject: [PATCH] fix(agent-status): a skipped Codex async question no longer stays pending (#41013) Codex's skip key (Ctrl+]) drops an async question only in its TUI: no hook, no transcript line. Orca kept the question open and the row waiting until the turn ended. The renderer now forwards the skip keystroke as a codexSkip question inference; main drops one question from the tracker and restates the row, returning it to working once none remain. --- .../server/server-codex-question-skip.ts | 41 +++++++++++++++++ .../server/server-status-disposition.ts | 4 +- ...server-ingest-codex-async-question.test.ts | 46 ++++++++++++++++++- .../agent-question-answered-inference.test.ts | 45 ++++++++++++++++++ .../agent-question-answered-inference.ts | 24 ++++++++-- .../providers/codex-async-questions.ts | 24 +++++++++- .../providers/codex-question-skip.ts | 46 +++++++++++++++++++ .../agent-question-answered-intent.ts | 2 + src/shared/codex-async-question-input.ts | 13 ++++++ 9 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 src/main/agent-hooks/server/server-codex-question-skip.ts create mode 100644 src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.test.ts create mode 100644 src/shared/agent-hook-listener/providers/codex-question-skip.ts diff --git a/src/main/agent-hooks/server/server-codex-question-skip.ts b/src/main/agent-hooks/server/server-codex-question-skip.ts new file mode 100644 index 00000000000..e59e028baaf --- /dev/null +++ b/src/main/agent-hooks/server/server-codex-question-skip.ts @@ -0,0 +1,41 @@ +import { codexRowAfterQuestionSkip } from '../../../shared/agent-hook-listener/providers/codex-question-skip' +import { AGENT_STATUS_STALE_AFTER_MS } from '../../../shared/agent-status/contracts/agent-status-freshness' +import type { AgentQuestionAnsweredInferenceRequest } from '../../../shared/agent-status/lifecycle/agent-question-answered-intent' +import { CODEX_ASYNC_QUESTION_TOOL } from '../../../shared/codex-async-question-input' +import { AgentHookServerStatusInference } from './server-status-inference' +import { isValidPaneKey } from './server-status-identity' +import type { EnrichedAgentHookEventPayload } from './server-types' + +/** Codex's skip key drops an async question in its TUI and reports nothing, so the renderer + * forwards the keystroke as a `codexSkip` question inference for a waiting Codex row. */ +export abstract class AgentHookServerCodexQuestionSkip extends AgentHookServerStatusInference { + override inferQuestionAnswered(request: AgentQuestionAnsweredInferenceRequest): boolean { + if (!request.codexSkip) { + return super.inferQuestionAnswered(request) + } + if (!isValidPaneKey(request.paneKey)) { + return false + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the server stores only enriched rows here, as inferQuestionAnswered already assumes. + const existing = this.state.lastStatusByPaneKey.get(request.paneKey) as + | EnrichedAgentHookEventPayload + | undefined + const payload = existing?.payload + // Codex keeps emitting hooks behind an open question and a skip may follow a skip, so the + // row's clock is no baseline; the prompt still fences a keystroke from a later turn. + if ( + !existing || + existing.providerSessionOnly || + existing.restoredUnconfirmed || + payload?.agentType !== 'codex' || + payload.toolName !== CODEX_ASYNC_QUESTION_TOOL || + payload.state !== 'waiting' || + payload.prompt !== request.baselinePrompt || + Date.now() - existing.receivedAt > AGENT_STATUS_STALE_AFTER_MS + ) { + return false + } + const row = codexRowAfterQuestionSkip(this.state, existing) + return row !== null && this.applyRestatedStatus(row) !== undefined + } +} diff --git a/src/main/agent-hooks/server/server-status-disposition.ts b/src/main/agent-hooks/server/server-status-disposition.ts index 33a1750d1b7..a52c68af8eb 100644 --- a/src/main/agent-hooks/server/server-status-disposition.ts +++ b/src/main/agent-hooks/server/server-status-disposition.ts @@ -12,9 +12,9 @@ import { RETIRED_PANE_FENCES_MAX } from './server-constants' import type { RetiredPaneAlias, RetiredPaneFence } from './server-types' -import { AgentHookServerStatusInference } from './server-status-inference' +import { AgentHookServerCodexQuestionSkip } from './server-codex-question-skip' -export abstract class AgentHookServerStatusDisposition extends AgentHookServerStatusInference { +export abstract class AgentHookServerStatusDisposition extends AgentHookServerCodexQuestionSkip { protected markTabClosedForAgentStatus(tabId: string): void { // Refresh insertion order so only the oldest closed tabs are evicted. this.closedAgentStatusTabIds.delete(tabId) diff --git a/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts b/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts index 79714496a2f..fef89b48fd9 100644 --- a/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts +++ b/src/main/agent-hooks/server/testing/providers/codex/server-ingest-codex-async-question.test.ts @@ -3,7 +3,7 @@ import { normalizeHookPayload } from '../../../../../../shared/agent-hook-listen import { createHookListenerState } from '../../../../../../shared/agent-hook-listener/listener-state' import { buildRelayHookEnvelope } from '../../../../../../wsl-guest/agent-hook-envelope-build' import { AgentHookServer, _internals } from '../../../agent-hook-server' -import { PANE, buildBody } from '../../server.test-fixtures' +import { PANE, buildBody, postHookEvent } from '../../server.test-fixtures' vi.mock('../../../../../telemetry/client', () => ({ track: vi.fn() })) vi.mock('../../../../../telemetry/cohort-classifier', () => ({ @@ -103,4 +103,48 @@ describe('Codex async questions', () => { expect(answered).toMatchObject({ state: 'working' }) expect(answered?.interactivePrompt).toBeUndefined() }) + + it('drops one question per skip keystroke on a local pane, since Codex reports no skip', async () => { + const server = new AgentHookServer() + await server.start({ env: 'production' }) + const row = () => server.getStatusSnapshotForPane(PANE)[0]! + const post = async (payload: Record) => { + await postHookEvent(server, buildBody(payload), '/hook/codex') + return row() + } + const baselineOf = (baseline: { + receivedAt: number + stateStartedAt: number + prompt: string + }) => ({ + paneKey: PANE, + baselineUpdatedAt: baseline.receivedAt, + baselineStateStartedAt: baseline.stateStartedAt, + baselinePrompt: baseline.prompt, + baselineAgentType: 'codex' as const + }) + const skip = (baseline: { receivedAt: number; stateStartedAt: number; prompt: string }) => + server.inferQuestionAnswered({ ...baselineOf(baseline), codexSkip: true }) + try { + await post({ hook_event_name: 'UserPromptSubmit', turn_id: 'turn-1', prompt: 'Set up a db' }) + await post(ASK) + expect(await post(ACCEPTED)).toMatchObject(waitingOnQuestion) + + // An answer inference (e.g. Enter sent over RPC) is not a skip. + expect(server.inferQuestionAnswered(baselineOf(row()))).toBe(false) + // Two quick skips share the renderer's stale baseline; both must land. + const stale = row() + expect(skip(stale)).toBe(true) + expect(row()).toMatchObject(waitingOnQuestion) + expect(skip(stale)).toBe(true) + expect(row()).toMatchObject({ state: 'working', mainAgent: { state: 'working' } }) + expect(row().interactivePrompt).toBeUndefined() + // Later hooks in the same turn must not resurrect the skipped question. + const bash = { hook_event_name: 'PreToolUse', turn_id: 'turn-1', tool_name: 'Bash' } + expect(await post(bash)).toMatchObject({ state: 'working' }) + expect(skip(row())).toBe(false) + } finally { + server.stop() + } + }) }) diff --git a/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.test.ts b/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.test.ts new file mode 100644 index 00000000000..2e0d1e933e7 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.test.ts @@ -0,0 +1,45 @@ +import { describe, expect, it, vi } from 'vitest' +import type { AgentStatusEntry } from '../../../../../../shared/agent-status/contracts/agent-status-types' +import { createAgentQuestionAnsweredInference } from './agent-question-answered-inference' + +const NOW = 1_000_000 + +function observe(entry: Partial, data: string): boolean { + const inferQuestionAnswered = vi.fn() + const status: AgentStatusEntry = { + state: 'waiting', + prompt: 'p', + updatedAt: NOW, + stateStartedAt: NOW, + paneKey: 'tab-1:pane', + stateHistory: [], + ...entry + } + createAgentQuestionAnsweredInference({ + paneKey: 'tab-1:pane', + getStatusEntry: () => status, + inferQuestionAnswered, + now: () => NOW + }).observeSentTerminalInput(data) + return inferQuestionAnswered.mock.calls.length > 0 +} + +const codexAsync = { + agentType: 'codex', + toolName: 'request_user_input_async' +} as const + +describe('Codex async question skip', () => { + it.each(['\x1d', '\x1b[93;5u'])('infers a skip from Ctrl+] (%j)', (data) => { + expect(observe(codexAsync, data)).toBe(true) + }) + + it('ignores Enter, which Codex reports through a reply hook', () => { + expect(observe(codexAsync, '\r')).toBe(false) + }) + + it('ignores Ctrl+] for other rows', () => { + expect(observe({ agentType: 'claude', toolName: 'AskUserQuestion' }, '\x1d')).toBe(false) + expect(observe({ ...codexAsync, state: 'working' }, '\x1d')).toBe(false) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.ts b/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.ts index fe175c0b135..a7f093124f8 100644 --- a/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.ts +++ b/src/renderer/src/components/terminal-pane/agent-status/input-inference/agent-question-answered-inference.ts @@ -9,6 +9,10 @@ import { isQuestionAnsweredSubmitInput, type AgentQuestionAnsweredInferenceRequest } from '../../../../../../shared/agent-status/lifecycle/agent-question-answered-intent' +import { + CODEX_ASYNC_QUESTION_TOOL, + isCodexSkipQuestionInput +} from '../../../../../../shared/codex-async-question-input' export type AgentQuestionAnsweredInference = { observeSentTerminalInput(data: string): void @@ -25,14 +29,17 @@ type AgentQuestionAnsweredInferenceDeps = { function inferQuestionAnsweredFromEntry( deps: AgentQuestionAnsweredInferenceDeps, - entry: AgentStatusEntry | undefined + entry: AgentStatusEntry | undefined, + /** Codex async questions resolve only by skip; their answers arrive as hooks. */ + codexSkip = false ): boolean { const now = deps.now ?? Date.now if ( !entry || entry.state !== 'waiting' || - entry.agentType !== 'claude' || - !isAskUserQuestionTool(entry.toolName) || + !(codexSkip + ? entry.agentType === 'codex' && entry.toolName === CODEX_ASYNC_QUESTION_TOOL + : entry.agentType === 'claude' && isAskUserQuestionTool(entry.toolName)) || !isExplicitAgentStatusFresh(entry, now(), AGENT_STATUS_STALE_AFTER_MS) ) { return false @@ -42,7 +49,8 @@ function inferQuestionAnsweredFromEntry( baselineUpdatedAt: entry.updatedAt, baselineStateStartedAt: entry.stateStartedAt, baselinePrompt: entry.prompt, - baselineAgentType: entry.agentType + baselineAgentType: entry.agentType, + ...(codexSkip ? { codexSkip: true as const } : {}) }) return true } @@ -61,8 +69,14 @@ export function createAgentQuestionAnsweredInference({ inferQuestionAnswered, now = () => Date.now() }: AgentQuestionAnsweredInferenceDeps): AgentQuestionAnsweredInference { + const deps = { paneKey, getStatusEntry, inferQuestionAnswered, now } return { observeSentTerminalInput(data) { + // Codex's skip removes an async question in the TUI only; no hook or transcript line reports it. + if (isCodexSkipQuestionInput(data)) { + inferQuestionAnsweredFromEntry(deps, getStatusEntry(), true) + return + } // Reject ordinary hot-path terminal input before store reads or prompt JSON parsing. if (!isPotentialQuestionAnsweredSubmitInput(data)) { return @@ -71,7 +85,7 @@ export function createAgentQuestionAnsweredInference({ if (!entry || !isQuestionAnsweredSubmitInput(data, entry.interactivePrompt)) { return } - inferQuestionAnsweredFromEntry({ paneKey, getStatusEntry, inferQuestionAnswered, now }, entry) + inferQuestionAnsweredFromEntry(deps, entry) } } } diff --git a/src/shared/agent-hook-listener/providers/codex-async-questions.ts b/src/shared/agent-hook-listener/providers/codex-async-questions.ts index 8e13c291fc2..4f0e47921f9 100644 --- a/src/shared/agent-hook-listener/providers/codex-async-questions.ts +++ b/src/shared/agent-hook-listener/providers/codex-async-questions.ts @@ -24,8 +24,7 @@ export type CodexPendingAsyncQuestion = { toolName: string; prompt: string } /** * Codex asks a non-blocking question mid-turn: its status stays working, PostToolUse lands ~20 ms * after PreToolUse, and each answer arrives as a UserPromptSubmit reply. Only the hooks show it, - * so track it here. A skip (Ctrl+]) sends no hook and stays pending until the turn ends or the - * user sends a plain message. + * so track it here. A skip (Ctrl+]) sends no hook; Orca infers it from the keystroke. */ export function trackCodexAsyncQuestions( state: HookListenerState, @@ -58,6 +57,27 @@ export function pendingCodexAsyncQuestion( return newest ? { toolName: CODEX_ASYNC_QUESTION_TOOL, prompt: newest.prompt } : undefined } +/** Codex's TUI skips the focused question, which is the oldest unless the user navigated. */ +export function skipCodexAsyncQuestion(state: HookListenerState, paneKey: string): boolean { + const calls = state.codexAsyncQuestionsByPaneKey.get(paneKey) + const oldest = calls?.entries().next().value + if (!calls || !oldest) { + return false + } + const [callId, call] = oldest + const index = call.unanswered.values().next().value + if (index !== undefined) { + call.unanswered.delete(index) + } + if (call.unanswered.size === 0) { + calls.delete(callId) + } + if (calls.size === 0) { + state.codexAsyncQuestionsByPaneKey.delete(paneKey) + } + return true +} + function openCall( state: HookListenerState, paneKey: string, diff --git a/src/shared/agent-hook-listener/providers/codex-question-skip.ts b/src/shared/agent-hook-listener/providers/codex-question-skip.ts new file mode 100644 index 00000000000..cce40781eb0 --- /dev/null +++ b/src/shared/agent-hook-listener/providers/codex-question-skip.ts @@ -0,0 +1,46 @@ +import type { AgentHookEventPayload } from '../listener-event' +import type { HookListenerState } from '../listener-state' +import { pendingCodexAsyncQuestion, skipCodexAsyncQuestion } from './codex-async-questions' +import { buildCodexStatusPayload } from './codex-events' +import { resolveCodexPaneStatus, setCodexMainAgentTurnState } from './codex-state' + +/** Restate a local Codex row after the user skips its focused async question; null when none was + * open. Codex keeps working behind the question, so only the open question held the row waiting. */ +export function codexRowAfterQuestionSkip( + state: HookListenerState, + row: AgentHookEventPayload +): AgentHookEventPayload | null { + const lead = state.codexLeadStateByPaneKey.get(row.paneKey) + // A relayed pane's questions are tracked on its execution host, out of reach here. + if (row.connectionId || !lead || !skipCodexAsyncQuestion(state, row.paneKey)) { + return null + } + const record = pendingCodexAsyncQuestion(state, row.paneKey) + ? lead + : setCodexMainAgentTurnState(state, row.paneKey, { state: 'working', model: lead.model }) + const payload = buildCodexStatusPayload( + state, + undefined, + '', + row.paneKey, + {}, + { + ...resolveCodexPaneStatus(state, row.paneKey, record), + updateLead: false + } + ) + // Carry only the row's identity, as Orca's other inferred restatements do. + return ( + payload && { + paneKey: row.paneKey, + tabId: row.tabId, + worktreeId: row.worktreeId, + connectionId: row.connectionId, + providerSession: row.providerSession, + launchToken: row.launchToken, + hostTurnRevision: row.hostTurnRevision, + source: row.source, + payload + } + ) +} diff --git a/src/shared/agent-status/lifecycle/agent-question-answered-intent.ts b/src/shared/agent-status/lifecycle/agent-question-answered-intent.ts index 0ef8d377298..47c98a7a667 100644 --- a/src/shared/agent-status/lifecycle/agent-question-answered-intent.ts +++ b/src/shared/agent-status/lifecycle/agent-question-answered-intent.ts @@ -8,6 +8,8 @@ export type AgentQuestionAnsweredInferenceRequest = { baselineStateStartedAt: number baselinePrompt: string baselineAgentType: AgentType | undefined + /** Codex's skip key, not an answer: Codex reports neither, but only a skip drops a question. */ + codexSkip?: true } /** True for the ask-the-user-a-question tool across agents: Claude's diff --git a/src/shared/codex-async-question-input.ts b/src/shared/codex-async-question-input.ts index 7d1fb003071..573cf4f7be3 100644 --- a/src/shared/codex-async-question-input.ts +++ b/src/shared/codex-async-question-input.ts @@ -21,3 +21,16 @@ export function parseCodexAsyncQuestionsInput(input: unknown): AskPrompt | null } return questions.length > 0 ? { questions, replyAsMessage: true } : null } + +/** Codex's default `chat.skip_question` key (Ctrl+]) as legacy, kitty and modifyOtherKeys bytes; + * a remapped key is not seen. */ +const CODEX_SKIP_QUESTION_INPUTS: ReadonlySet = new Set([ + '\x1d', + '\x1b[93;5u', + '\x1b[93;5:1u', + '\x1b[27;5;93~' +]) + +export function isCodexSkipQuestionInput(data: string): boolean { + return CODEX_SKIP_QUESTION_INPUTS.has(data) +}