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(agent-status): Esc on an AskUserQuestion no longer leaves NEEDS YOU stuck

enes1f95ed81d11d
diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.ts
index 47e0f5a69..9d0c9cfde 100644
--- a/src/core/agent-status-mirror.ts
+++ b/src/core/agent-status-mirror.ts
@@ -1375,6 +1375,11 @@ export function setNodeSessionName(nodeId: string, name: string): boolean {
return true
}
+/** A node's current main state, or undefined when unknown. Read-only peek for the shells. */
+export function nodeState(nodeId: string): AgentState | undefined {
+ return state.get(nodeId)?.state
+}
+
/** The entries the session-name sweep walks: id + what it needs to resolve and dedupe. */
export function sessionNameSweepEntries(): {
nodeId: string
diff --git a/src/core/context-tail.test.ts b/src/core/context-tail.test.ts
index 1294ddc03..7550dab7e 100644
--- a/src/core/context-tail.test.ts
+++ b/src/core/context-tail.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import fs from 'fs'
import os from 'os'
import path from 'path'
-import { createContextTail, parseLatestUsage, parseTaskNotifications } from './context-tail'
+import { createContextTail, parseLatestUsage, parseTaskNotifications, hasToolResult } from './context-tail'
describe('parseLatestUsage', () => {
it('returns the LAST assistant usage in the text (sum of input + cache tokens)', () => {
@@ -79,3 +79,39 @@ describe('createContextTail — task notifications', () => {
tail.untrack('s1')
}, 8000)
})
+
+
+describe('hasToolResult (the declined-ask rescue)', () => {
+ const decline = JSON.stringify({
+ type: 'user',
+ message: {
+ role: 'user',
+ content: [
+ { type: 'tool_result', tool_use_id: 'toolu_1', content: 'User declined to answer questions' }
+ ]
+ }
+ })
+
+ it('sees a tool result — the moment a blocking ask settled', () => {
+ expect(hasToolResult(decline)).toBe(true)
+ // Any tool result counts: the caller gates on the node still being in needs-you, so a normal
+ // turn's stream of results is free.
+ const ok = JSON.stringify({
+ type: 'user',
+ message: { content: [{ type: 'tool_result', tool_use_id: 't2', content: 'ok' }] }
+ })
+ expect(hasToolResult(`${ok}\n${decline}`)).toBe(true)
+ })
+
+ it('ignores everything else, including a mention of the words', () => {
+ const assistant = JSON.stringify({
+ type: 'assistant',
+ message: { content: [{ type: 'text', text: 'about to call a tool_result handler' }] }
+ })
+ expect(hasToolResult(assistant)).toBe(false)
+ expect(hasToolResult('')).toBe(false)
+ expect(hasToolResult('not json at all')).toBe(false)
+ // A torn line must not throw (the tail carries it to the next read).
+ expect(hasToolResult('{"type":"user","message":{"content":[{"type":"tool_res')).toBe(false)
+ })
+})
diff --git a/src/core/context-tail.ts b/src/core/context-tail.ts
index dab924d99..2b7c462ad 100644
--- a/src/core/context-tail.ts
+++ b/src/core/context-tail.ts
@@ -86,9 +86,42 @@ export function parseTaskNotifications(text: string): TaskNotification[] {
return out
}
+/**
+ * Did this chunk of transcript carry a TOOL RESULT? That is the moment a tool the CLI was blocked
+ * on has settled — which is the only signal we get when an ask ENDS without a hook.
+ *
+ * The case: an `AskUserQuestion` picker is up (node = needs-you) and the user presses Esc. Claude
+ * records "User declined to answer questions" as the tool's result and carries on, but the aborted
+ * tool fires no PostToolUse, and Stop does not run either — so nothing told us the ask was over and
+ * the node sat on NEEDS YOU (badge, notch capsule, phone card) until the next prompt hours later.
+ *
+ * Deliberately not decline-specific: any tool_result means the blocking tool finished, whatever the
+ * answer was. The caller only acts on it while the node is still in needs-you, so a normal turn's
+ * constant stream of results costs nothing.
+ */
+export function hasToolResult(text: string): boolean {
+ for (const line of text.split('\n')) {
+ const s = line.trim()
+ // Cheap pre-filter before the parse — this runs over every transcript chunk.
+ if (!s || !s.includes('tool_result')) continue
+ let o: { message?: { content?: unknown } }
+ try {
+ o = JSON.parse(s)
+ } catch {
+ continue
+ }
+ const content = o.message?.content
+ if (!Array.isArray(content)) continue
+ if (content.some((c) => (c as { type?: string })?.type === 'tool_result')) return true
+ }
+ return false
+}
+
export interface ContextTailOptions {
/** Fired when a tracked session's transcript announces a completed async subagent. */
onTaskNotification?: (sessionId: string, n: TaskNotification) => void
+ /** Fired when a tracked session's transcript records a tool RESULT — see `hasToolResult`. */
+ onToolResult?: (sessionId: string) => void
}
interface Tracked {
@@ -190,6 +223,7 @@ export function createContextTail(
if (opts?.onTaskNotification) {
for (const n of parseTaskNotifications(complete)) opts.onTaskNotification(sessionId, n)
}
+ if (opts?.onToolResult && hasToolResult(complete)) opts.onToolResult(sessionId)
}
}
diff --git a/src/main/index.ts b/src/main/index.ts
index 8ef8f2857..f66452f45 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -55,7 +55,8 @@ import {
isEventUnresolved,
type MirrorSettings,
setNodeSessionName,
- sessionNameSweepEntries
+ sessionNameSweepEntries,
+ nodeState
} from '../core/agent-status-mirror'
import { createPushNotify, createLiveUpdatePush } from '../core/push-notify'
import { createGrantsAccessor } from '../core/push-grants'
@@ -996,6 +997,29 @@ app.whenReady().then(async () => {
// transcript — their PostToolUse is only a launch ack (see the raw listener below). The
// context tails already read that transcript, so they surface the notification here and we
// emit the synthetic subagent-end the hooks never send, then release the transcript tail.
+ /**
+ * A tool RESULT landed in a tracked transcript. Only interesting while the node is still in
+ * needs-you: an `AskUserQuestion` the user declined with Esc fires no PostToolUse and no Stop, so
+ * nothing ever moved the node off NEEDS YOU — badge, notch capsule and phone card all stuck until
+ * the next prompt. The result proves the ask settled; `working` is the honest next state (Claude
+ * carries on with "User declined to answer questions"), and any real event corrects it anyway.
+ */
+ const onToolResult = (sessionId: string): void => {
+ let nodeId: string | undefined
+ for (const [nid, sid] of nodeContextSession) if (sid === sessionId) nodeId = nid
+ if (!nodeId) return
+ const st = nodeState(nodeId)
+ if (st !== 'blocked' && st !== 'waiting') return
+ const ev = {
+ nodeId,
+ agentId: 'claude',
+ sessionId,
+ kind: 'state',
+ state: 'working'
+ } satisfies NormalizedAgentEvent
+ sendToMain(IPC.agentStatus, ev)
+ recordAgentEvent(ev)
+ }
const onTaskNotification = (sessionId: string, n: TaskNotification): void => {
let nodeId: string | undefined
for (const [nid, sid] of nodeContextSession) if (sid === sessionId) nodeId = nid
@@ -1027,7 +1051,7 @@ app.whenReady().then(async () => {
break
}
}
- }, { onTaskNotification })
+ }, { onTaskNotification, onToolResult })
// Remote (SSH-project) counterparts: a node whose pty runs on a remote host has its Claude
// transcript on that host, so its meter / subagent transcript / search must read over the
// project's ControlMaster. One RemoteFile bound to the SSH-project manager's own ssh runner
@@ -1037,7 +1061,7 @@ app.whenReady().then(async () => {
const remoteFile = new RemoteFile((args) =>
sshProjectManager ? sshProjectManager.sshRun(args) : Promise.resolve({ code: 1, stdout: '' })
)
- const remoteContextTail = createRemoteContextTail(win, remoteFile, { onTaskNotification })
+ const remoteContextTail = createRemoteContextTail(win, remoteFile, { onTaskNotification, onToolResult })
const remoteSubagentTail = createRemoteSubagentTail(win, remoteFile)
// Remote transcript ref learned from the hook raw-listener, keyed by sessionId — lets the
// search/chat read handlers (which receive only sessionId + cwd) read remotely without a
diff --git a/src/main/remote-context-tail.ts b/src/main/remote-context-tail.ts
index 58683d216..e6583c50b 100644
--- a/src/main/remote-context-tail.ts
+++ b/src/main/remote-context-tail.ts
@@ -8,7 +8,12 @@ import { type BrowserWindow } from 'electron'
import { IPC } from '../shared/ipc'
import type { ContextWindowUsage } from '../shared/types'
import { cachedWindowFor, resolveModelWindow } from '../core/model-window'
-import { parseLatestUsage, parseTaskNotifications, type ContextTailOptions } from '../core/context-tail'
+import {
+ parseLatestUsage,
+ parseTaskNotifications,
+ hasToolResult,
+ type ContextTailOptions
+} from '../core/context-tail'
import { splitCompleteLines } from '../core/subagent-tail'
import type { RemoteFile, RemoteFileRef } from './remote-ssh/remote-file'
@@ -63,6 +68,7 @@ export function createRemoteContextTail(
t.used = latest.used
t.model = latest.model ?? t.model
}
+ if (opts?.onToolResult && hasToolResult(complete)) opts.onToolResult(sessionId)
if (opts?.onTaskNotification) {
for (const n of parseTaskNotifications(complete)) opts.onTaskNotification(sessionId, n)
}
diff --git a/src/server/agent-status.ts b/src/server/agent-status.ts
index 125365456..4ddd7d8c3 100644
--- a/src/server/agent-status.ts
+++ b/src/server/agent-status.ts
@@ -10,7 +10,9 @@
import { resolve } from 'path'
import { homedir } from 'os'
import { hookServer } from '../core/agents/hook-server'
-import { recordAgentEvent, recordRawToolEvent, recordContextUsage } from '../core/agent-status-mirror'
+import { recordAgentEvent, recordRawToolEvent, recordContextUsage,
+ nodeState
+} from '../core/agent-status-mirror'
import { createSubagentTail, type SubagentTail } from '../core/subagent-tail'
import { createContextTail, type ContextTail, type TaskNotification } from '../core/context-tail'
import { setNodeTranscript } from '../core/context-link'
@@ -74,6 +76,25 @@ export function wireAgentStatus(platform: ServerPlatform, opts: WireAgentStatusO
nodeSubagents.get(nodeId)?.delete(n.toolUseId)
}
+ /** See the identical handler in src/main/index.ts: a tool RESULT settles an ask that ended with
+ * no hook (Esc on an AskUserQuestion), which otherwise left the node stuck on needs-you. */
+ const onToolResult = (sessionId: string): void => {
+ let nodeId: string | undefined
+ for (const [nid, sid] of nodeContextSession) if (sid === sessionId) nodeId = nid
+ if (!nodeId) return
+ const st = nodeState(nodeId)
+ if (st !== 'blocked' && st !== 'waiting') return
+ const ev = {
+ nodeId,
+ agentId: 'claude',
+ sessionId,
+ kind: 'state',
+ state: 'working'
+ } satisfies NormalizedAgentEvent
+ platform.broadcast(IPC.agentStatus, ev)
+ recordAgentEvent(ev)
+ }
+
const contextTail =
opts.contextTail ??
createContextTail(
@@ -89,7 +110,7 @@ export function wireAgentStatus(platform: ServerPlatform, opts: WireAgentStatusO
}
}
},
- { onTaskNotification }
+ { onTaskNotification, onToolResult }
)
hooks.setListener((e) => {