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(canvas): Approve/Deny header buttons hide while a question is asked

enes46f9f69e4de2
diff --git a/src/core/agent-status-mirror.test.ts b/src/core/agent-status-mirror.test.ts
index 2798460e4..658d7bdf8 100644
--- a/src/core/agent-status-mirror.test.ts
+++ b/src/core/agent-status-mirror.test.ts
@@ -657,6 +657,63 @@ describe('question options (AskUserQuestion)', () => {
})
})
+// The enrichment recordAgentEvent RETURNS is what the shells broadcast to the canvas — the single
+// source of truth for the approve/deny gate (field report: buttons showed during AskUserQuestion).
+describe('recordAgentEvent enrichment (returned broadcast event)', () => {
+ beforeEach(() => _resetForTest())
+ afterEach(() => _resetForTest())
+
+ it('a stash-classified question strips pendingId and gains askKind:question', () => {
+ recordRawToolEvent('e1', {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'AskUserQuestion',
+ tool_input: { questions: [{ question: 'Which theme?', options: [{ label: 'Dark' }, { label: 'Light' }] }] }
+ })
+ // The CLI signals the picker as a permission-style blocked carrying a held-hook pendingId.
+ const out = recordAgentEvent(ev({ nodeId: 'e1', state: 'blocked', pendingId: 'e1-1-1' }))
+ expect(out.state).toBe('blocked')
+ expect(out.askKind).toBe('question')
+ expect(out.pendingId).toBeUndefined() // suppressed — approve/deny on a question is wrong UX
+ })
+
+ it('a genuine approval keeps its pendingId and gains askKind:approval', () => {
+ const out = recordAgentEvent(ev({ nodeId: 'e2', state: 'blocked', pendingId: 'e2-1-1' }))
+ expect(out.state).toBe('blocked')
+ expect(out.askKind).toBe('approval')
+ expect(out.pendingId).toBe('e2-1-1')
+ })
+
+ it('a plain waiting (no stash) is a question with no options and no pendingId', () => {
+ const out = recordAgentEvent(ev({ nodeId: 'e3', state: 'waiting', lastMessage: 'Pick one' }))
+ expect(out.askKind).toBe('question')
+ expect(out.pendingId).toBeUndefined()
+ })
+
+ it('non-needs-you events pass through untouched (no askKind added)', () => {
+ const working = ev({ nodeId: 'e4', state: 'working', newTurn: true })
+ const outW = recordAgentEvent(working)
+ expect(outW).toBe(working) // same reference — nothing to enrich
+ expect('askKind' in outW).toBe(false)
+ const outD = recordAgentEvent(ev({ nodeId: 'e4', state: 'done', lastMessage: 'done' }))
+ expect(outD.askKind).toBeUndefined()
+ const outS = recordAgentEvent(ev({ nodeId: 'e4', kind: 'session', sessionPhase: 'end' }))
+ expect(outS.askKind).toBeUndefined()
+ })
+
+ it('enrichment does not mutate the caller-supplied event', () => {
+ recordRawToolEvent('e5', {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'AskUserQuestion',
+ tool_input: { questions: [{ options: [{ label: 'A' }] }] }
+ })
+ const original = ev({ nodeId: 'e5', state: 'blocked', pendingId: 'e5-1-1' })
+ const out = recordAgentEvent(original)
+ expect(original.pendingId).toBe('e5-1-1') // unchanged
+ expect(original.askKind).toBeUndefined() // unchanged
+ expect(out).not.toBe(original) // enriched is a copy
+ })
+})
+
// ---- multiSelect (rides the AskUserQuestion pipeline) ---------------------------------------
describe('question multiSelect (AskUserQuestion)', () => {
diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.ts
index a441d459a..704563845 100644
--- a/src/core/agent-status-mirror.ts
+++ b/src/core/agent-status-mirror.ts
@@ -815,18 +815,46 @@ function resolveFile(): string | null {
}
}
-/** Fold a normalized agent event into the mirror (main state + inbox feed) and schedule a
- * debounced write. Called with EVERY normalized event by both shells. */
-export function recordAgentEvent(ev: NormalizedAgentEvent): void {
- if (!ev?.nodeId) return
+/**
+ * The stash-priority classification of a needs-you (blocked/waiting) event, computed once by
+ * `produceInboxFromState` and reused to ENRICH the event the shells broadcast. `kind` is what the
+ * inbox card was tagged; `pendingId` is the hook-reply ticket the card carries (present only for a
+ * genuine approval — a question strips it). Absent for non-needs-you events.
+ */
+interface NeedsYouClassification {
+ kind: 'question' | 'approval'
+ pendingId?: string
+}
+
+/**
+ * Fold a normalized agent event into the mirror (main state + inbox feed), schedule a debounced
+ * write, and RETURN the event the shells should broadcast — ENRICHED for a needs-you edge so the
+ * ONE stash-priority classification the mirror already computes is the single source of truth for
+ * the canvas too:
+ * - a `question` (fresh AskUserQuestion stash) gains `askKind:'question'` and has its `pendingId`
+ * STRIPPED — so TerminalNode's `blocked && pendingId` gate never renders approve/deny on a
+ * question (field report: the buttons showed during an AskUserQuestion);
+ * - a genuine `approval` gains `askKind:'approval'` and keeps its `pendingId` unchanged.
+ * Every other event (working / done / session / subagent / recurring) passes through untouched
+ * (same reference). Internal behavior — inbox production, live-update seams, disk writes — is
+ * unchanged; this only affects what the caller then broadcasts. Called with EVERY normalized event
+ * by both shells.
+ */
+export function recordAgentEvent(ev: NormalizedAgentEvent): NormalizedAgentEvent {
+ if (!ev?.nodeId) return ev
const nodeId = ev.nodeId
const now = Date.now()
const prev = state.get(nodeId)
const prevState = prev?.state
const next = reduceEntry(prev, ev, now)
state.set(nodeId, next)
- produceInboxFromState(nodeId, ev, prevState, next.state, now)
+ const classification = produceInboxFromState(nodeId, ev, prevState, next.state, now)
scheduleWrite()
+ if (!classification) return ev
+ // Enrich the broadcast event from the SAME classification the inbox used. A question drops
+ // pendingId (approve/deny is wrong UX for a picker); an approval keeps ev.pendingId as-is.
+ if (classification.kind === 'question') return { ...ev, askKind: 'question', pendingId: undefined }
+ return { ...ev, askKind: 'approval' }
}
/**
@@ -840,7 +868,7 @@ function produceInboxFromState(
prevState: AgentState | undefined,
nextState: AgentState | undefined,
now: number
-): void {
+): 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.)
if (ev.kind === 'session' || (ev.kind === 'state' && ev.state === 'working' && ev.newTurn)) {
@@ -920,16 +948,20 @@ function produceInboxFromState(
...(pendingId ? { pendingId } : {})
})
}
- if (newestUnresolved(inboxEvents, nodeId)?.title === title) return
- pushInboxEvent({
- ...baseEvent,
- kind,
- title,
- ...(detail ? { detail } : {}),
- ...(options ? { options } : {}),
- ...(multiSelect ? { multiSelect: true } : {}),
- ...(pendingId ? { pendingId } : {})
- })
+ // Dedup a re-asserted SAME ask (title match) — but always return the classification below so
+ // the broadcast enrichment is consistent across the re-assert.
+ if (newestUnresolved(inboxEvents, nodeId)?.title !== title) {
+ pushInboxEvent({
+ ...baseEvent,
+ kind,
+ title,
+ ...(detail ? { detail } : {}),
+ ...(options ? { options } : {}),
+ ...(multiSelect ? { multiSelect: true } : {}),
+ ...(pendingId ? { pendingId } : {})
+ })
+ }
+ return { kind, pendingId }
} else if (nextState === 'done' && prevState !== 'done') {
// The turn ended: emit one `done` on the edge into done (reduceEntry's holdoff keeps a late
// working from re-entering, and the normalizer collapses an Esc-spam into one done).
diff --git a/src/main/index.ts b/src/main/index.ts
index 63412eecd..5b89bd671 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -1039,8 +1039,11 @@ app.whenReady().then(async () => {
// and the mobile-facing mirror. Named so the deterministic-approval answer handler below can reuse
// it for the optimistic flip.
const emitAgentStatus = (e: NormalizedAgentEvent): void => {
- sendToMain(IPC.agentStatus, e)
- recordAgentEvent(e)
+ // Record FIRST: recordAgentEvent computes the stash-priority classification and returns the
+ // event ENRICHED for a needs-you edge (a question strips its pendingId), so the canvas keys off
+ // the same single source of truth as the mirror/phone. Then broadcast the enriched event.
+ const enriched = recordAgentEvent(e) ?? e
+ sendToMain(IPC.agentStatus, enriched)
}
hookServer.setListener(emitAgentStatus)
// Deterministic hook-reply approvals (docs/hook-reply-approvals.md): the canvas Approve/Deny
diff --git a/src/server/agent-status.test.ts b/src/server/agent-status.test.ts
index 7176da0dd..bc06aa240 100644
--- a/src/server/agent-status.test.ts
+++ b/src/server/agent-status.test.ts
@@ -4,6 +4,7 @@ import os from 'os'
import path from 'path'
import { ServerPlatform } from './platform-server'
import { wireAgentStatus } from './agent-status'
+import { _resetForTest } from '../core/agent-status-mirror'
import { IPC } from '../shared/ipc'
import { decodePtyData } from '../shared/rpc'
@@ -42,6 +43,7 @@ function recTail() {
let dir: string, platform: ServerPlatform, sent: Array<{ channel: string; args: unknown[] }>
beforeEach(() => {
+ _resetForTest() // isolate the mirror singleton (stash/state) between tests
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-agst-'))
platform = new ServerPlatform({ userDataDir: dir, appVersion: '0' })
sent = []
@@ -54,7 +56,19 @@ beforeEach(() => {
}
})
})
-afterEach(() => fs.rmSync(dir, { recursive: true, force: true }))
+afterEach(() => {
+ fs.rmSync(dir, { recursive: true, force: true })
+ _resetForTest()
+})
+
+// Newest agent:status broadcast payload (the enriched event the shell records-then-broadcasts).
+function lastAgentStatus(): Record<string, unknown> | undefined {
+ for (let i = sent.length - 1; i >= 0; i--) {
+ const m = sent[i] as { channel?: string; args?: unknown[] }
+ if (m.channel === IPC.agentStatus) return m.args?.[0] as Record<string, unknown>
+ }
+ return undefined
+}
describe('wireAgentStatus', () => {
it('broadcasts a normalized agent event on agent:status', () => {
@@ -65,6 +79,35 @@ describe('wireAgentStatus', () => {
expect(sent).toContainEqual({ t: 'ev', channel: IPC.agentStatus, args: [ev] })
})
+ it('broadcasts the ENRICHED event: an AskUserQuestion blocked edge loses its pendingId (askKind question)', () => {
+ const fh = fakeHooks()
+ wireAgentStatus(platform, { hooks: fh.hooks as never })
+ // The raw AskUserQuestion PreToolUse stashes the picker options (recordRawToolEvent).
+ fh.fireRaw('claude', 'q1', {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'AskUserQuestion',
+ tool_input: { questions: [{ question: 'Which theme?', options: [{ label: 'Dark' }, { label: 'Light' }] }] }
+ })
+ // The CLI then signals the picker as a permission-style blocked carrying a held-hook pendingId.
+ fh.fireNormalized({ nodeId: 'q1', agentId: 'claude', kind: 'state', state: 'blocked', pendingId: 'q1-1-1' })
+ const e = lastAgentStatus()!
+ expect(e.state).toBe('blocked')
+ expect(e.askKind).toBe('question')
+ // The pendingId is STRIPPED so the canvas approve/deny gate (blocked && pendingId) never fires.
+ expect('pendingId' in e).toBe(false)
+ })
+
+ it('broadcasts a genuine approval unchanged (keeps pendingId, askKind approval)', () => {
+ const fh = fakeHooks()
+ wireAgentStatus(platform, { hooks: fh.hooks as never })
+ // No AskUserQuestion stash → a blocked edge stays a real permission request.
+ fh.fireNormalized({ nodeId: 'a1', agentId: 'claude', kind: 'state', state: 'blocked', pendingId: 'a1-1-1' })
+ const e = lastAgentStatus()!
+ expect(e.state).toBe('blocked')
+ expect(e.askKind).toBe('approval')
+ expect(e.pendingId).toBe('a1-1-1')
+ })
+
it('tracks a subagent on PreToolUse(Task) and finishes it on PostToolUse', () => {
const fh = fakeHooks()
const sub = recTail()
diff --git a/src/server/agent-status.ts b/src/server/agent-status.ts
index 3b4f15c8f..125365456 100644
--- a/src/server/agent-status.ts
+++ b/src/server/agent-status.ts
@@ -93,8 +93,11 @@ export function wireAgentStatus(platform: ServerPlatform, opts: WireAgentStatusO
)
hooks.setListener((e) => {
- platform.broadcast(IPC.agentStatus, e)
- recordAgentEvent(e)
+ // Record FIRST: recordAgentEvent computes the stash-priority classification and returns the
+ // event ENRICHED for a needs-you edge (a question strips its pendingId), so the browser canvas
+ // keys off the same single source of truth as the mirror/phone. Then broadcast the enriched one.
+ const enriched = recordAgentEvent(e) ?? e
+ platform.broadcast(IPC.agentStatus, enriched)
})
// Security: hook POSTs can be forged, so a forged POST could set transcript_path to an
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index d9fcb2b29..68130e0a4 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -22,6 +22,14 @@ export interface NormalizedAgentEvent {
// `nodeterm_pending_id` (merged into the payload by the hook server) so the phone/canvas can
// answer the held hook. Absent = no held hook (legacy prompt path). See docs/hook-reply-approvals.md.
pendingId?: string
+ // needs-you (blocked/waiting) only: how the shell classified this ask AFTER the mirror's
+ // stash-priority reclassification (see agent-status-mirror.recordAgentEvent). 'question' = an
+ // AskUserQuestion picker (its `pendingId` is stripped — approve/deny on a question is wrong UX);
+ // 'approval' = a genuine permission request (its `pendingId`, if any, is kept). Absent on every
+ // non-needs-you event. This is the ENRICHED field the shells broadcast — it is not produced by
+ // the normalizers themselves. Present for future UI; the canvas already keys the approve/deny
+ // buttons off `pendingId`, which is now absent on a question. */
+ askKind?: 'question' | 'approval'
// session
sessionTitle?: string
// session lifecycle phase: 'start' resets to idle, 'end' resets + clears loop/fan-out