fix(agent-status): clear answered Claude question waits at answer time (#9074)

* fix(agent-status): clear answered Claude question waits at answer time

An answered AskUserQuestion left the amber "waiting" indicator on sidebar
rows and tabs until the agent's next tool hook or turn end — unbounded
linger while the model thinks or streams after the answer (measured 17s
for a 1000-word reply, 44s for 3000 words).

Root cause is an event-shape change: newer Claude reports the
AskUserQuestion wait as PermissionRequest (not the PreToolUse shape #7852
special-cased), so the wait inherited real-permission stickiness and
shouldKeepClaudePermissionVisible swallowed the answer-time
PostToolUse(AskUserQuestion) working event — the identity match can never
succeed because the question's PermissionRequest carries no inheritable
tool_use_id. That silently undid #8311 for questions.

Two scoped changes, both keyed on the tool name rather than the hook
event name:

- Sticky permission hold now exempts AskUserQuestion waits, so the real
  answer-time hook (when Claude sends one) clears the wait as #8311
  intended.
- New guarded inference for the hook Claude may never send: the submit
  keystroke (Enter or digit quick-select) into a pane whose fresh status
  is a waiting AskUserQuestion synthesizes the post-answer state, exactly
  mirroring the existing interrupt inference (renderer baseline capture,
  main-process re-validation, listener lead-state sync so child-driven
  refreshes cannot resurrect the dismissed question).

Real permission waits (other tools) keep their sticky semantics; batched
input and pastes never match the submit classifier.

Verified live against a real claude CLI: waiting -> working within ~50ms
of both Enter and digit answers, question card dropped, unanswered
questions still hold amber, permission stickiness covered by tests.

* fix(agent-status): guard question answer inference

Keep multi-question, multi-select, and free-text selector interactions waiting until the full prompt is submitted. Wire native-chat answers into the same guarded inference only after every paced runtime write succeeds, with cancellation and delivery-failure coverage.

* chore(skills): refresh manifest for rc.2

* fix(agent-status): verify native chat answer delivery

* fix(agent-status): await verified question delivery

* fix(agent-status): pin native-chat answer baseline before delivery

The native-chat question-answered inference read the live pane status at
settle time (after the paced send + remote acceptance, which can span
seconds on SSH). If a replacement AskUserQuestion became current in that
window, the settle callback minted a fresh baseline from the new question
and the server cleared *its* wait — dismissing a question the user never
answered.

Capture the answered question's baseline before delivery and have the
inference getter return it, so the server re-validates against the pinned
baseline and rejects a changed status — the same capture-then-revalidate
contract the terminal keystroke path already uses. Also hoist the
shouldStepNativeChatAskAnswer predicate to a single evaluation.

Regression test swaps the live status between sendAnswer and settle and
asserts the answered question's baseline is used (fails against the prior
live-read getter).
This commit is contained in:
Brennan Benson
2026-07-17 12:47:04 -07:00
committed by GitHub
parent b67106805f
commit 2cb5d4e149
19 changed files with 1018 additions and 55 deletions
@@ -57,6 +57,34 @@ describe('Claude interactive-question status transitions', () => {
])
})
it('clears a PermissionRequest-shaped AskUserQuestion wait when later tool work starts', () => {
// Why: newer Claude reports the AskUserQuestion wait as PermissionRequest;
// the question must not inherit real-permission stickiness from that shape.
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'waiting',
hookEventName: 'PermissionRequest',
toolName: 'AskUserQuestion',
toolUseId: 'tool-question'
})
ingestClaudeStatus(server, {
state: 'working',
hookEventName: 'PreToolUse',
toolName: 'Read',
toolUseId: 'tool-after-answer'
})
expect(server.getStatusSnapshot()).toEqual([
expect.objectContaining({
paneKey: PANE_KEY,
state: 'working',
agentType: 'claude',
toolName: 'Read'
})
])
})
it('keeps an actual permission request sticky during unrelated tool work', () => {
const server = new AgentHookServer()
@@ -83,3 +111,102 @@ describe('Claude interactive-question status transitions', () => {
])
})
})
function answeredRequestFromSnapshot(
server: AgentHookServer
): Parameters<AgentHookServer['inferQuestionAnswered']>[0] {
const [entry] = server.getStatusSnapshot()
return {
paneKey: entry.paneKey,
baselineUpdatedAt: entry.receivedAt,
baselineStateStartedAt: entry.stateStartedAt,
// Why: mirror the renderer, which echoes the entry's prompt verbatim —
// these ingests carry none, and the server must strict-match that.
baselinePrompt: entry.prompt as string,
baselineAgentType: entry.agentType
}
}
describe('inferQuestionAnswered', () => {
it('clears an AskUserQuestion wait when the submit keystroke is reported', () => {
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'waiting',
hookEventName: 'PreToolUse',
toolName: 'AskUserQuestion',
toolUseId: 'tool-question'
})
expect(server.inferQuestionAnswered(answeredRequestFromSnapshot(server))).toBe(true)
// Why: the answered question must also drop the tool identity so the
// question card cannot linger on the working row.
const [entry] = server.getStatusSnapshot()
expect(entry).toMatchObject({ paneKey: PANE_KEY, state: 'working', agentType: 'claude' })
expect(entry.toolName).toBeUndefined()
expect(entry.interactivePrompt).toBeUndefined()
})
it('clears a PermissionRequest-shaped AskUserQuestion wait (newer Claude)', () => {
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'waiting',
hookEventName: 'PermissionRequest',
toolName: 'AskUserQuestion',
toolUseId: 'tool-question'
})
expect(server.inferQuestionAnswered(answeredRequestFromSnapshot(server))).toBe(true)
expect(server.getStatusSnapshot()).toEqual([
expect.objectContaining({ paneKey: PANE_KEY, state: 'working', agentType: 'claude' })
])
})
it('refuses when the cached status changed since the baseline was captured', () => {
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'waiting',
hookEventName: 'PreToolUse',
toolName: 'AskUserQuestion',
toolUseId: 'tool-question'
})
const staleRequest = {
...answeredRequestFromSnapshot(server),
baselineUpdatedAt: 1
}
expect(server.inferQuestionAnswered(staleRequest)).toBe(false)
expect(server.getStatusSnapshot()).toEqual([
expect.objectContaining({ state: 'waiting', toolName: 'AskUserQuestion' })
])
})
it('never clears a real permission request', () => {
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'waiting',
hookEventName: 'PermissionRequest',
toolName: 'Bash',
toolUseId: 'tool-needs-permission'
})
expect(server.inferQuestionAnswered(answeredRequestFromSnapshot(server))).toBe(false)
expect(server.getStatusSnapshot()).toEqual([
expect.objectContaining({ state: 'waiting', toolName: 'Bash' })
])
})
it('ignores panes that are not waiting on a question', () => {
const server = new AgentHookServer()
ingestClaudeStatus(server, {
state: 'working',
hookEventName: 'PreToolUse',
toolName: 'Read',
toolUseId: 'tool-working'
})
expect(server.inferQuestionAnswered(answeredRequestFromSnapshot(server))).toBe(false)
expect(server.getStatusSnapshot()).toEqual([
expect.objectContaining({ state: 'working', toolName: 'Read' })
])
})
})
+74 -1
View File
@@ -22,6 +22,7 @@ import { ORCA_HOOK_PROTOCOL_VERSION } from '../../shared/agent-hook-types'
import {
clearAllListenerCaches,
clearPaneCacheState,
clearClaudeAnsweredQuestionWait,
createHookListenerState,
getEndpointFileName,
hasPendingAgentResultText,
@@ -57,6 +58,10 @@ import {
isAgentInterruptInputIntent,
type AgentInterruptInferenceRequest
} from '../../shared/agent-interrupt-intent'
import {
isAskUserQuestionTool,
type AgentQuestionAnsweredInferenceRequest
} from '../../shared/agent-question-answered-intent'
import { parseLegacyNumericPaneKey, parsePaneKey } from '../../shared/stable-pane-id'
import type { LegacyPaneKeyAliasEntry } from '../../shared/types'
import { normalizeAgentProviderSession } from '../../shared/agent-session-resume'
@@ -353,7 +358,12 @@ function shouldKeepClaudePermissionVisible(
return false
}
// Why: only real permission requests stay sticky across concurrent subagent
// activity; interactive questions clear on the next working hook.
// activity; interactive questions clear on the next working hook. Newer
// Claude reports the AskUserQuestion wait AS a PermissionRequest, so the
// tool name — not the event name — decides which rule applies.
if (isAskUserQuestionTool(previous.payload.toolName)) {
return false
}
return true
}
@@ -626,6 +636,69 @@ export class AgentHookServer {
return true
}
/** Guarded fallback for a hook Claude never sends: answering AskUserQuestion
* produces no event, so the amber wait would otherwise linger until the
* agent's next tool or turn end. The renderer reports the submit keystroke;
* this re-validates its baseline against the cached status (a racing real
* hook wins) and synthesizes the post-answer state. */
inferQuestionAnswered(request: AgentQuestionAnsweredInferenceRequest): boolean {
if (!isValidPaneKey(request.paneKey)) {
return false
}
const existing = this.state.lastStatusByPaneKey.get(request.paneKey) as
| EnrichedAgentHookEventPayload
| undefined
if (!existing) {
return false
}
const payload = existing.payload
// Why: only Claude's interactive question may clear on typed input. The
// tool name is the discriminator, not the hook event — Claude versions
// differ on whether the AskUserQuestion wait arrives as PreToolUse or
// PermissionRequest. Real permission waits (other tools) stay sticky until
// the approved tool resumes — a denied or ignored permission must keep
// demanding attention even though approving is also a keystroke.
if (
payload.agentType !== 'claude' ||
payload.state !== 'waiting' ||
!isAskUserQuestionTool(payload.toolName)
) {
return false
}
if (
payload.agentType !== request.baselineAgentType ||
payload.prompt !== request.baselinePrompt ||
existing.receivedAt !== request.baselineUpdatedAt ||
existing.stateStartedAt !== request.baselineStateStartedAt ||
Date.now() - existing.receivedAt > AGENT_STATUS_STALE_AFTER_MS
) {
return false
}
// Why: sync the listener's lead-turn record too — a later child lifecycle
// event would otherwise re-emit the stale waiting state and resurrect the
// dismissed question card.
const restored = clearClaudeAnsweredQuestionWait(this.state, existing.paneKey)
const inferred = this.applyNormalizedStatus({
paneKey: existing.paneKey,
tabId: existing.tabId,
worktreeId: existing.worktreeId,
connectionId: existing.connectionId,
providerSession: existing.providerSession,
payload: {
state: restored.state,
prompt: payload.prompt,
agentType: payload.agentType,
...(restored.state === 'done' && restored.interrupted ? { interrupted: true } : {}),
...(payload.subagents ? { subagents: payload.subagents } : {})
}
})
console.debug('[agent-hooks] inferred answered question status', {
paneKey: inferred.paneKey,
state: inferred.payload.state
})
return true
}
getStatusChangeSnapshot(): AgentHookStatusChangeEntry[] {
return Array.from(this.state.lastStatusByPaneKey.entries(), ([paneKey, entry]) => {
const enriched = entry as EnrichedAgentHookEventPayload