mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
+45
-1
@@ -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<string, unknown>) => {
|
||||
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()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
+45
@@ -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<AgentStatusEntry>, 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)
|
||||
})
|
||||
})
|
||||
+19
-5
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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<string> = 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user