Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(agents): preserve lifecycle and sequential questions during approval holds

reviewae6e3f04fcff
diff --git a/CLAUDE.md b/CLAUDE.md
index eb531fcc0..cead839c3 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -4961,3 +4961,8 @@ own ticket; the picker stays pending until its correlated answer or explicit res
When the parent answers first, retain concurrent approval tickets and blocked attention until
their own replies; ordinary tool activity cannot settle them. Explicit turn/session resets
still cancel both kinds of pending attention.
+
+Held approval attention must not replace subagent, recurring or background-task events, or refresh
+state evidence from those lifecycle hooks. A parent may ask several questions while a child ticket
+is outstanding: track each new picker and preserve child approval cards independently, including
+when their display titles match. Answering either resolves only that question or ticket.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 605dae7a9..c515ca6d0 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -841,3 +841,8 @@ shared nonfatal startup path and surface its actionable diagnostic. A responding
nodeterm identity: verify bearer acceptance and rejection. Legacy SSH endpoint migration requires
ownership proof, an unchanged-file check, and stdin-only credential transfer; a project name alone
is not permission to replace another installation's advertisement.
+
+Held approval attention must not replace subagent, recurring or background-task events, or refresh
+state evidence from those lifecycle hooks. A parent may ask several questions while a child ticket
+is outstanding: track each new picker and preserve child approval cards independently, including
+when their display titles match. Answering either resolves only that question or ticket.
diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.ts
index 81932660c..7fe1cb23f 100644
--- a/src/core/agent-status-mirror.ts
+++ b/src/core/agent-status-mirror.ts
@@ -503,14 +503,15 @@ function reduceEffectiveEntry(
if (!reset && !answered) return next
delete next.pendingQuestion
}
- if (next.concurrentApprovalIds?.length) {
- // The picker answer and unrelated parent tool activity cannot answer child permissions.
- commitState('blocked', !!ev.verified)
- return next
- }
if (ev.questionId && ev.sessionId) {
next.pendingQuestion = { sessionId: ev.sessionId, toolUseId: ev.questionId }
}
+ if (next.concurrentApprovalIds?.length && !next.pendingQuestion) {
+ // A new picker is independent of outstanding child permissions. Other state events
+ // hold blocked attention; lifecycle events must not refresh that state's evidence.
+ if (ev.kind === 'state') commitState('blocked', !!ev.verified)
+ return next
+ }
// Identity is captured off ANY event (mirrors the renderer's per-event setSessionId +
// agentId threading). agentId is always present on a NormalizedAgentEvent.
if (ev.agentId) next.agentId = ev.agentId
@@ -1198,10 +1199,11 @@ export function isEventUnresolved(nodeId: string, eventId: string): boolean {
return false
}
-/** Mark a node's unresolved approval/question events resolved (it left blocked/waiting). */
-function resolveUnresolvedFor(nodeId: string): void {
+/** Settle older attention cards, optionally retaining independent child approval tickets. */
+function resolveUnresolvedFor(nodeId: string, keepApprovalIds: readonly string[] = []): void {
for (const e of inboxEvents) {
- if (e.nodeId === nodeId && !e.resolved && (e.kind === 'approval' || e.kind === 'question')) {
+ if (e.nodeId === nodeId && !e.resolved && (e.kind === 'approval' || e.kind === 'question') &&
+ !(e.kind === 'approval' && e.pendingId && keepApprovalIds.includes(e.pendingId))) {
e.resolved = true
}
}
@@ -1449,6 +1451,10 @@ export function recordAgentEvent(rawEvent: NormalizedAgentEvent): NormalizedAgen
}
}
if (next.concurrentApprovalIds?.length && !next.pendingQuestion) {
+ if (ev.kind !== 'state' && ev.kind !== 'session') {
+ scheduleWrite()
+ return ev
+ }
if (ev.pendingId && ev.state === 'blocked' && ev.askKind === 'approval') {
produceInboxFromState(nodeId, ev, prevState, 'blocked', now, true)
}
@@ -1483,7 +1489,8 @@ export function recordAgentEvent(rawEvent: NormalizedAgentEvent): NormalizedAgen
if (ev.kind === 'state' && ev.state === 'done' && next.awaitingInput && next.state === 'waiting') {
out = { ...ev, state: 'waiting' }
}
- const classification = produceInboxFromState(nodeId, out, prevState, next.state, now)
+ const classification = produceInboxFromState(nodeId, out, prevState, next.state, now, false,
+ next.concurrentApprovalIds)
scheduleWrite()
if (!classification) return out
// Enrich the broadcast event from the SAME classification the inbox used. A question drops
@@ -1503,7 +1510,8 @@ function produceInboxFromState(
prevState: AgentState | undefined,
nextState: AgentState | undefined,
now: number,
- concurrentApproval = false
+ concurrentApproval = false,
+ keepApprovalIds: readonly string[] = []
): NeedsYouClassification | undefined {
// Clear any stashed question options on a new turn or session boundary — a stale option set must
// never attach to a later, unrelated question. (State-leave clearing is handled below.)
@@ -1592,7 +1600,8 @@ function produceInboxFromState(
// enrichment stays consistent across the re-assert.
const dup = concurrentApproval
? inboxEvents.find(e => e.nodeId === nodeId && e.kind === 'approval' && !e.resolved && e.pendingId === ev.pendingId)
- : newestUnresolved(inboxEvents, nodeId)
+ : newestUnresolved(inboxEvents.filter(e =>
+ !(e.kind === 'approval' && e.pendingId && keepApprovalIds.includes(e.pendingId))), nodeId)
const sameTitle = !!dup && dup.title === title
const freshDup = sameTitle && dup ? now - dup.ts < QUESTION_DEDUP_WINDOW_MS : false
const newAsk = !freshDup
@@ -1615,11 +1624,10 @@ function produceInboxFromState(
})
}
if (newAsk) {
- // A NEW ask settles every older one for this node: the CLI blocks on an ask, so it cannot be
- // asking something else unless the previous one was answered. Without this the Inbox kept a
- // card per ask and the user had to dismiss answered questions by hand — the state never
- // leaves `blocked` between them, so the transition-based resolve never ran.
- if (!concurrentApproval) resolveUnresolvedFor(nodeId)
+ // A new parent ask settles older parent asks, but child approval tickets are independent.
+ // Preserve those tickets while the parent proceeds to another picker; settle older questions
+ // even when the state never leaves needs-you between them.
+ if (!concurrentApproval) resolveUnresolvedFor(nodeId, keepApprovalIds)
pushInboxEvent({
...baseEvent,
kind,
diff --git a/src/core/pending-question.test.ts b/src/core/pending-question.test.ts
index 05519a3c7..a490b189d 100644
--- a/src/core/pending-question.test.ts
+++ b/src/core/pending-question.test.ts
@@ -166,3 +166,84 @@ describe('subagent attention forwarding (W2)', () => {
expect(_snapshot().node.state).toBe('blocked')
})
})
+
+
+describe('independent question, approval and lifecycle streams', () => {
+ const approval = { agent_id: 'child', session_id: 'child-session', tool_name: 'Bash',
+ tool_input: { command: 'echo child' }, nodeterm_pending_id: 'ticket-1' }
+ const open = () => {
+ ask()
+ hook('PermissionRequest', approval)
+ hook('PermissionRequest', { ...approval, nodeterm_pending_id: 'ticket-2' })
+ }
+ const live = () => _inboxSnapshot().events.filter(e => !e.resolved)
+
+ describe.each([false, true])('parent answered: %s', answered => {
+ it.each([
+ ['PreToolUse', { tool_name: 'Agent', tool_use_id: 'task-1', tool_input: { description: 'Work' } }, 'subagent-start'],
+ ['PostToolUse', { tool_name: 'Agent', tool_use_id: 'task-1', tool_response: { content: [{ type: 'text', text: 'Done' }] } }, 'subagent-end'],
+ ['PreToolUse', { tool_name: 'CronCreate', tool_input: { cron: '* * * * *', prompt: 'Check' } }, 'recurring'],
+ ['PreToolUse', { tool_name: 'CronDelete' }, 'recurring'],
+ ['PreToolUse', { tool_name: 'Bash', tool_input: { run_in_background: true } }, 'background-task']
+ ] as const)('preserves %s %j as %s without refreshing held state', (event, extra, kind) => {
+ open()
+ if (answered) recordQuestionResult('node', 'parent', 'ask-1')
+ const before = _snapshot().node
+ const cards = _inboxSnapshot().events
+ vi.setSystemTime(Date.now() + 5000)
+ const payload = { hook_event_name: event, session_id: 'parent', ...extra }
+ recordRawToolEvent('node', payload)
+ const normalized = normalizeClaude({ nodeId: 'node', agentId: 'claude', payload })!
+ expect(normalized.kind).toBe(kind)
+ expect(recordAgentEvent(normalized)).toBe(normalized)
+ expect(_snapshot().node).toEqual(before)
+ expect(_inboxSnapshot().events).toEqual(cards)
+ })
+ })
+
+ it('does not deduplicate a new picker against an independent approval with the same title', () => {
+ open()
+ recordQuestionResult('node', 'parent', 'ask-1')
+ const title = live().find(e => e.pendingId === 'ticket-2')!.title
+ expect(hook('PreToolUse', { tool_name: 'AskUserQuestion', tool_use_id: 'ask-2',
+ tool_input: { questions: [{ question: title, options: [{ label: 'A' }, { label: 'B' }] }] } }))
+ .toMatchObject({ state: 'waiting', askKind: 'question' })
+ expect(live()).toHaveLength(3)
+ expect(live().find(e => e.kind === 'question')).toMatchObject({ title, options: ['A', 'B'] })
+ expect(live().filter(e => e.kind === 'approval').map(e => e.pendingId)).toEqual(['ticket-1', 'ticket-2'])
+ })
+
+ it.each(['allow', 'deny'])('keeps sequential pickers and independent tickets through both answer orders (%s)', decision => {
+ open()
+ recordQuestionResult('node', 'parent', 'ask-1')
+ // A parent can ask and answer repeatedly while both child permissions remain outstanding.
+ for (const id of ['ask-2', 'ask-3']) {
+ expect(ask(id)).toMatchObject({ kind: 'state', state: 'waiting', askKind: 'question', questionId: id })
+ expect(_snapshot().node.pendingQuestion).toEqual({ sessionId: 'parent', toolUseId: id })
+ expect(live().filter(e => e.kind === 'question')).toHaveLength(1)
+ expect(live().filter(e => e.kind === 'approval').map(e => e.pendingId)).toEqual(['ticket-1', 'ticket-2'])
+ // Reassertions and an older delayed result cannot duplicate or answer the new question.
+ ask(id)
+ expect(live()).toHaveLength(3)
+ expect(recordQuestionResult('node', 'parent', 'ask-1')).toBeUndefined()
+ expect(hook('PostToolUse', { tool_name: 'AskUserQuestion', tool_use_id: 'ask-1' })?.state).toBe('waiting')
+ expect(recordQuestionResult('node', 'parent', id)).toMatchObject({ state: 'blocked', pendingId: 'ticket-2' })
+ expect(live().map(e => e.pendingId)).toEqual(['ticket-1', 'ticket-2'])
+ }
+ // Now answer one child first, then the next parent picker, then the final child.
+ ask('ask-4')
+ expect(hook('PermissionRequest', { ...approval, nodeterm_answered: decision }))
+ .toMatchObject({ state: 'waiting', askKind: 'question' })
+ expect(_snapshot().node.pendingQuestion?.toolUseId).toBe('ask-4')
+ expect(live().filter(e => e.kind === 'approval').map(e => e.pendingId)).toEqual(['ticket-2'])
+ expect(recordQuestionResult('node', 'parent', 'ask-4')).toMatchObject({ state: 'blocked', pendingId: 'ticket-2' })
+ ask('ask-5')
+ expect(hook('PermissionRequest', { ...approval, nodeterm_pending_id: 'ticket-2', nodeterm_answered: decision }))
+ .toMatchObject({ state: 'waiting', askKind: 'question' })
+ expect(live()).toHaveLength(1)
+ expect(live()[0].kind).toBe('question')
+ expect(_snapshot().node.concurrentApprovalIds).toBeUndefined()
+ expect(recordQuestionResult('node', 'parent', 'ask-5')).toMatchObject({ state: 'working' })
+ expect(live()).toHaveLength(0)
+ })
+})