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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(inbox): AskUserQuestion pickers are questions, not approvals
enes89bfcc6a6ae6
diff --git a/src/core/agent-status-mirror.test.ts b/src/core/agent-status-mirror.test.tsindex 9a5ab3543..06d305e03 100644--- a/src/core/agent-status-mirror.test.ts+++ b/src/core/agent-status-mirror.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect, beforeEach, afterEach } from 'vitest'+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'import fs from 'fs'import os from 'os'import path from 'path'@@ -20,6 +20,7 @@ import {toolActivity,firstLine,extractQuestionOptions,+ extractQuestionText,onNodeStateChange,onNodeNowChange,_resetForTest,@@ -27,6 +28,7 @@ import {_inboxSnapshot,DONE_HOLDOFF_MS,EXPIRE_MS,+ STASH_MAX_AGE_MS,INBOX_EVENTS_CAP,type MirrorEntry,type MirrorFile,@@ -512,16 +514,76 @@ describe('question options (AskUserQuestion)', () => {expect(_inboxSnapshot().events.find((e) => e.nodeId === 'n2')!.options).toBeUndefined()})- it('approvals never carry options even when a stash exists', () => {+ it('extracts the first question text, clipped to 120, fail-open on bad shape', () => {+ expect(extractQuestionText({ questions: [{ question: 'Which theme?' }] })).toBe('Which theme?')+ // only the first question+ expect(extractQuestionText({ questions: [{ question: 'A?' }, { question: 'B?' }] })).toBe('A?')+ // clips to 120+ const long = 'x'.repeat(200)+ const out = extractQuestionText({ questions: [{ question: long }] })!+ expect(out.length).toBe(120)+ expect(out.endsWith('…')).toBe(true)+ // fail-open+ expect(extractQuestionText(undefined)).toBeUndefined()+ expect(extractQuestionText({})).toBeUndefined()+ expect(extractQuestionText({ questions: [] })).toBeUndefined()+ expect(extractQuestionText({ questions: [{ options: [] }] })).toBeUndefined()+ })++ // Stash-priority classification (fixes: AskUserQuestion always shown as an approval on the+ // phone). A pending picker reaches us as a permission-style `blocked`; a fresh stash reclassifies+ // it as a question with its options and the real question text, and suppresses the hook ticket.+ it('a blocked edge WITH a stash becomes a question (options + question-text title + NO pendingId)', () => {recordRawToolEvent('n3', {hook_event_name: 'PreToolUse',tool_name: 'AskUserQuestion',- tool_input: q1([{ label: 'A' }])+ tool_input: { questions: [{ question: 'Which theme?', options: [{ label: 'Dark' }, { label: 'Light' }] }] }})- recordAgentEvent(ev({ nodeId: 'n3', state: 'blocked', lastMessage: 'Approve?' }))+ // The CLI signals the picker as a permission-style blocked carrying a held-hook pendingId.+ recordAgentEvent(ev({ nodeId: 'n3', state: 'blocked', lastMessage: 'Approve?', pendingId: 'n3-1-1' }))const e = _inboxSnapshot().events.find((x) => x.nodeId === 'n3')!+ expect(e.kind).toBe('question')+ expect(e.options).toEqual(['Dark', 'Light'])+ expect(e.title).toBe('Which theme?') // question text, not the lastMessage / generic+ expect('pendingId' in e).toBe(false) // suppressed — approve/deny on a question is wrong UX+ })++ it('a blocked edge WITHOUT a stash stays an approval with no options (unchanged)', () => {+ recordAgentEvent(ev({ nodeId: 'n3b', state: 'blocked', lastMessage: 'Approve write' }))+ const e = _inboxSnapshot().events.find((x) => x.nodeId === 'n3b')!+ expect(e.kind).toBe('approval')+ expect(e.options).toBeUndefined()+ })++ it('a stash-blocked question with no question text falls back to the lastMessage title', () => {+ recordRawToolEvent('n3c', {+ hook_event_name: 'PreToolUse',+ tool_name: 'AskUserQuestion',+ tool_input: q1([{ label: 'A' }]) // options only, no `question`+ })+ recordAgentEvent(ev({ nodeId: 'n3c', state: 'blocked', lastMessage: 'Pick something' }))+ const e = _inboxSnapshot().events.find((x) => x.nodeId === 'n3c')!+ expect(e.kind).toBe('question')+ expect(e.title).toBe('Pick something')+ expect(e.options).toEqual(['A'])+ })++ it('ignores a stale stash (older than STASH_MAX_AGE_MS) — a later blocked stays an approval', () => {+ const spy = vi.spyOn(Date, 'now')+ spy.mockReturnValue(1_000_000)+ recordRawToolEvent('n3d', {+ hook_event_name: 'PreToolUse',+ tool_name: 'AskUserQuestion',+ tool_input: { questions: [{ question: 'Q', options: [{ label: 'A' }] }] }+ })+ // Jump past the freshness window before an UNRELATED approval fires.+ spy.mockReturnValue(1_000_000 + STASH_MAX_AGE_MS + 1)+ recordAgentEvent(ev({ nodeId: 'n3d', state: 'blocked', lastMessage: 'Approve rm -rf', pendingId: 'p' }))+ const e = _inboxSnapshot().events.find((x) => x.nodeId === 'n3d')!expect(e.kind).toBe('approval')expect(e.options).toBeUndefined()+ expect(e.pendingId).toBe('p') // a real approval keeps its held-hook ticket+ spy.mockRestore()})it('clears the stash when the node leaves waiting (no reuse on a later question)', () => {@@ -635,6 +697,23 @@ describe('onNodeStateChange seam', () => {expect('pendingId' in ny).toBe(false)})+ it('a stash-blocked edge emits kind:question + options + question-text message and drops pendingId', () => {+ const seen: NodeStateChange[] = []+ onNodeStateChange((c) => seen.push(c))+ recordRawToolEvent('n1', {+ hook_event_name: 'PreToolUse',+ tool_name: 'AskUserQuestion',+ tool_input: { questions: [{ question: 'Pick a theme', options: [{ label: 'Dark' }, { label: 'Light' }] }] }+ })+ // Picker signaled as a permission-style blocked with a held-hook pendingId.+ recordAgentEvent(ev({ state: 'blocked', lastMessage: 'Approve?', pendingId: 'n1-1-1' }))+ const ny = seen.find((c) => c.state === 'needsYou')!+ expect(ny.kind).toBe('question')+ expect(ny.options).toEqual(['Dark', 'Light'])+ expect(ny.message).toBe('Pick a theme')+ expect('pendingId' in ny).toBe(false) // approval ticket suppressed on a question edge+ })+it('a plain waiting edge (no AskUserQuestion stash) is kind:question with no options', () => {const seen: NodeStateChange[] = []onNodeStateChange((c) => seen.push(c))diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.tsindex a6ac56791..fa0acb8dc 100644--- a/src/core/agent-status-mirror.ts+++ b/src/core/agent-status-mirror.ts@@ -517,10 +517,33 @@ function fireNodeNowChange(c: NodeNowChange): void {}}-// Per-node stash of the pending AskUserQuestion option labels, set from the raw-hook seam and-// attached to the next `question` InboxEvent for that node (cleared on state-leave / new turn /-// session boundary).-const pendingOptions = new Map<string, string[]>()+// Per-node stash of the pending AskUserQuestion picker (option labels + the question text), set+// from the raw-hook seam and attached to the next needs-you InboxEvent for that node. Cleared on+// state-leave / new turn / session boundary, and additionally time-guarded (STASH_MAX_AGE_MS)+// so a stash that was never consumed (e.g. a picker that auto-resolved) can't be picked up by a+// genuinely unrelated later approval in the same turn.+interface QuestionStash {+ /** ≤4 labels, each ≤60 chars. */+ options: string[]+ /** The first question's prompt text, clipped to INBOX_TITLE_MAX. Absent if not present/parseable. */+ question?: string+ /** When the AskUserQuestion PreToolUse stashed this — freshness-guard anchor. */+ at: number+}+const pendingQuestions = new Map<string, QuestionStash>()+// A stash older than this is ignored (and swept) — see the comment above.+export const STASH_MAX_AGE_MS = 5 * 60_000++/** The node's stash IFF it exists and is fresh (< STASH_MAX_AGE_MS old); a stale one is dropped. */+function freshStash(nodeId: string, now: number): QuestionStash | undefined {+ const s = pendingQuestions.get(nodeId)+ if (!s) return undefined+ if (now - s.at > STASH_MAX_AGE_MS) {+ pendingQuestions.delete(nodeId)+ return undefined+ }+ return s+}/*** Extract question option labels from an AskUserQuestion `tool_input` (first question only, v1):@@ -548,6 +571,26 @@ export function extractQuestionOptions(}}+/**+ * Extract the first question's prompt text from an AskUserQuestion `tool_input`+ * (`questions[0].question`), clipped to INBOX_TITLE_MAX. Used as the question InboxEvent's title so+ * the phone shows the actual question rather than the last assistant line. Pure; FAIL-OPEN — any+ * shape mismatch (missing/non-string) yields `undefined`.+ */+export function extractQuestionText(+ toolInput: Record<string, unknown> | undefined+): string | undefined {+ try {+ const questions = (toolInput as { questions?: unknown })?.questions+ if (!Array.isArray(questions) || questions.length === 0) return undefined+ const q = (questions[0] as { question?: unknown })?.question+ if (typeof q !== 'string' || !q) return undefined+ return clip(q, INBOX_TITLE_MAX)+ } catch {+ return undefined+ }+}+function pushInboxEvent(e: Omit<InboxEvent, 'id'>): void {const id = `${e.ts}-${++inboxSeq}`const full: InboxEvent = { id, ...e }@@ -668,13 +711,13 @@ function produceInboxFromState(// 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)) {- pendingOptions.delete(nodeId)+ pendingQuestions.delete(nodeId)}// Leaving blocked/waiting (any newer, different state — incl. a session reset to idle) resolves// that node's pending approval/question cards; they move to the phone's archive.if ((prevState === 'blocked' || prevState === 'waiting') && nextState !== prevState) {resolveUnresolvedFor(nodeId)- pendingOptions.delete(nodeId)+ pendingQuestions.delete(nodeId)}const baseEvent = { ts: now, nodeId, agentId: ev.agentId, sessionId: ev.sessionId }const stateBase = { nodeId, agentId: ev.agentId, sessionId: ev.sessionId, ts: now }@@ -684,46 +727,55 @@ function produceInboxFromState(if (nextState === 'working' && prevState !== 'working') {fireNodeStateChange({ ...stateBase, event: 'start', state: 'working' })}- // approval/question dedup is TITLE-based (not edge-based): a re-asserted blocked with the SAME+ // approval/question dedup is TITLE-based (not edge-based): a re-asserted needs-you with the SAME// ask is a no-op, but a genuinely different ask still lands — so the guard can't be a plain// "same-state, skip". `done` alone is edge-guarded (one per turn).- if (nextState === 'blocked') {- const title = firstLine(ev.lastMessage, INBOX_TITLE_MAX) || 'Needs approval'- // needsYou live-update on the EDGE into blocked (a re-assert keeps the same activity live).- // Carries kind:'approval' + the deterministic-approval ticket (from the same `ev.pendingId`- // the approval InboxEvent below stashes) so an intent can answer the held hook straight from- // the Live Activity (spec: interactive-push-live-activities addendum).- if (prevState !== 'blocked') {+ if (nextState === 'blocked' || nextState === 'waiting') {+ // STASH-PRIORITY CLASSIFICATION (fixes: AskUserQuestion always rendered as an approval on the+ // phone). A pending AskUserQuestion picker reaches us as a permission-style signal — a+ // PermissionRequest hook and/or a Notification `permission_prompt`, both of which normalize to+ // `blocked` — even though it is a QUESTION with options, not an approve/deny. So when a fresh+ // AskUserQuestion stash exists we produce a `question` (with its options) REGARDLESS of which+ // needs-you state the CLI signaled; without a stash we keep today's mapping (blocked→approval,+ // waiting→question).+ const stash = freshStash(nodeId, now)+ const kind: 'approval' | 'question' = stash || nextState === 'waiting' ? 'question' : 'approval'+ const options = stash?.options+ // Title: the AskUserQuestion's own question text wins (so the phone shows the real question),+ // else the last assistant line, else a kind-appropriate generic.+ const title =+ stash?.question ||+ firstLine(ev.lastMessage, INBOX_TITLE_MAX) ||+ (kind === 'approval' ? 'Needs approval' : 'Waiting for input')+ // pendingId (the deterministic hook-reply ticket) rides ONLY an approval. A stash-question+ // suppresses it even when a PermissionRequest fired for the AskUserQuestion tool itself:+ // approve/deny on a question is wrong UX — the phone answers with digit send-keys that drive+ // the picker directly. That held PermissionRequest (if any) simply times out after 45s and the+ // picker shows anyway (acceptable). See docs/hook-reply-approvals.md.+ const pendingId = kind === 'approval' ? ev.pendingId : undefined+ // needsYou live-update on the EDGE into the needs-you state (a re-assert of the SAME state+ // keeps the activity live). Carries the classified kind + options (question) / pendingId+ // (approval) so the Live Activity renders straight from this same code path+ // (spec: interactive-push-live-activities addendum).+ if (prevState !== nextState) {fireNodeStateChange({...stateBase,event: 'update',state: 'needsYou',- kind: 'approval',+ kind,message: clip(title, LIVE_MESSAGE_MAX),- ...(ev.pendingId ? { pendingId: ev.pendingId } : {})+ ...(options ? { options } : {}),+ ...(pendingId ? { pendingId } : {})})}if (newestUnresolved(inboxEvents, nodeId)?.title === title) return- // Carry the deterministic-approval ticket (present only when the wait-branch of the managed- // hook ran) so the phone can answer the held hook. See docs/hook-reply-approvals.md.- pushInboxEvent({ ...baseEvent, kind: 'approval', title, ...(ev.pendingId ? { pendingId: ev.pendingId } : {}) })- } else if (nextState === 'waiting') {- const title = firstLine(ev.lastMessage, INBOX_TITLE_MAX) || 'Waiting for input'- const options = pendingOptions.get(nodeId)- // needsYou live-update on the EDGE into waiting, carrying kind:'question' + the AskUserQuestion- // choices from the stash so the Live Activity can render numbered option buttons.- if (prevState !== 'waiting') {- fireNodeStateChange({- ...stateBase,- event: 'update',- state: 'needsYou',- kind: 'question',- message: clip(title, LIVE_MESSAGE_MAX),- ...(options ? { options } : {})- })- }- if (newestUnresolved(inboxEvents, nodeId)?.title === title) return- pushInboxEvent({ ...baseEvent, kind: 'question', title, ...(options ? { options } : {}) })+ pushInboxEvent({+ ...baseEvent,+ kind,+ title,+ ...(options ? { options } : {}),+ ...(pendingId ? { 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).@@ -766,11 +818,13 @@ export function recordRawToolEvent(nodeId: string, payload: Record<string, unknopayload.tool_input && typeof payload.tool_input === 'object'? (payload.tool_input as Record<string, unknown>): undefined- // AskUserQuestion: stash the choice labels so the next `question` InboxEvent + push carry them- // (spec: interactive-push-live-activities). Fail-open — a shape mismatch leaves no stash.+ // AskUserQuestion: stash the choice labels + the question text so the next needs-you event ++ // push carry them and are classified as a `question` even when the CLI signals `blocked`+ // (spec: interactive-push-live-activities; fixes AskUserQuestion-as-approval). Fail-open — a+ // shape mismatch leaves no stash.if (toolName === 'AskUserQuestion') {const opts = extractQuestionOptions(toolInput)- if (opts) pendingOptions.set(nodeId, opts)+ if (opts) pendingQuestions.set(nodeId, { options: opts, question: extractQuestionText(toolInput), at: now })}const activity = toolActivity(toolName, toolInput)const cur = inboxNodes.get(nodeId)@@ -811,7 +865,7 @@ export function recordContextUsage(nodeId: string, percent: number): void {export function clearNode(nodeId: string): void {let changed = state.delete(nodeId)if (inboxNodes.delete(nodeId)) changed = true- pendingOptions.delete(nodeId)+ pendingQuestions.delete(nodeId)// History stays; unresolved cards for a gone node can never be answered → archive them.for (const e of inboxEvents) {if (e.nodeId === nodeId && !e.resolved && (e.kind === 'approval' || e.kind === 'question')) {@@ -876,7 +930,7 @@ export function _resetForTest(): void {inboxActionableListeners.clear()nodeStateChangeListeners.clear()nodeNowChangeListeners.clear()- pendingOptions.clear()+ pendingQuestions.clear()}/** Snapshot the in-memory map. Test-only. */