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.

feat(ssh): pure decision for whether a remote node is still working

enesbe274da32848
diff --git a/src/core/remote-ssh/agent-resync-decide.test.ts b/src/core/remote-ssh/agent-resync-decide.test.ts
new file mode 100644
index 000000000..41a0e71c2
--- /dev/null
+++ b/src/core/remote-ssh/agent-resync-decide.test.ts
@@ -0,0 +1,74 @@
+import { describe, it, expect } from 'vitest'
+import { decideFromPane, decideFromTranscriptTail, decideNode } from './agent-resync-decide'
+
+const assistantText = (text: string): string =>
+ JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text }] } })
+
+const assistantToolUse = (id: string, name: string): string =>
+ JSON.stringify({ type: 'assistant', message: { content: [{ type: 'tool_use', id, name }] } })
+
+const toolResult = (id: string): string =>
+ JSON.stringify({ type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: id }] } })
+
+describe('decideFromPane', () => {
+ it('a shell owning the pane means the CLI exited', () => {
+ expect(decideFromPane('zsh')).toBe('ended')
+ expect(decideFromPane('-bash')).toBe('ended')
+ })
+
+ it('the CLI still owning the pane decides nothing — it cannot tell mid-turn from waiting', () => {
+ expect(decideFromPane('claude')).toBe('undecided')
+ })
+
+ it('a failed or empty probe decides nothing', () => {
+ expect(decideFromPane(null)).toBe('undecided')
+ expect(decideFromPane('')).toBe('undecided')
+ })
+})
+
+describe('decideFromTranscriptTail', () => {
+ it('a closed assistant message with no outstanding tool call means the turn ended', () => {
+ const tail = [assistantToolUse('t1', 'Bash'), toolResult('t1'), assistantText('All done.')].join('\n')
+ expect(decideFromTranscriptTail(tail)).toBe('ended')
+ })
+
+ it('an unanswered tool_use means it is still working — this is the long Bash call', () => {
+ const tail = [assistantText('Let me check.'), assistantToolUse('t9', 'Bash')].join('\n')
+ expect(decideFromTranscriptTail(tail)).toBe('working')
+ })
+
+ it('a tool_result for a tool_use opened before the tail window is ignored, not miscounted', () => {
+ const tail = [toolResult('opened-earlier'), assistantText('Finished.')].join('\n')
+ expect(decideFromTranscriptTail(tail)).toBe('ended')
+ })
+
+ it('a truncated first line is skipped rather than poisoning the verdict', () => {
+ const tail = ['{"type":"assist', assistantText('Finished.')].join('\n')
+ expect(decideFromTranscriptTail(tail)).toBe('ended')
+ })
+
+ it('a tail ending on a user prompt decides nothing', () => {
+ const tail = [assistantText('Done.'), JSON.stringify({ type: 'user', message: { content: 'go on' } })].join('\n')
+ expect(decideFromTranscriptTail(tail)).toBe('undecided')
+ })
+
+ it('an empty or unparseable tail decides nothing', () => {
+ expect(decideFromTranscriptTail('')).toBe('undecided')
+ expect(decideFromTranscriptTail('not json at all')).toBe('undecided')
+ })
+})
+
+describe('decideNode', () => {
+ it('the pane wins when it is decisive — no transcript read needed', () => {
+ expect(decideNode('zsh', null)).toBe('ended')
+ })
+
+ it('falls through to the transcript when the CLI still owns the pane', () => {
+ expect(decideNode('claude', assistantText('Finished.'))).toBe('ended')
+ expect(decideNode('claude', assistantToolUse('t1', 'Bash'))).toBe('working')
+ })
+
+ it('an unread transcript decides nothing', () => {
+ expect(decideNode('claude', null)).toBe('undecided')
+ })
+})
diff --git a/src/core/remote-ssh/agent-resync-decide.ts b/src/core/remote-ssh/agent-resync-decide.ts
new file mode 100644
index 000000000..49b2ef598
--- /dev/null
+++ b/src/core/remote-ssh/agent-resync-decide.ts
@@ -0,0 +1,78 @@
+// Is a remote node that the mirror still calls `working` actually still working?
+//
+// Hook POSTs are fire-and-forget: an event that fires while the reverse tunnel is down is gone,
+// so a node can sit at `working` long after its turn ended (until the 20-minute stale sweep guesses
+// from silence — see shared/agents/stale.ts). After a reconnect we can ask the host directly.
+//
+// Pure by design: this decides, the caller in src/main does the ssh. The ONE rule that matters is
+// that `undecided` is the default — a wrong `ended` costs the user a false "finished" notification,
+// so every uncertainty resolves to leaving the node alone.
+
+import { isShellCommand } from '@shared/agents/pane'
+
+export type ResyncVerdict = 'ended' | 'working' | 'undecided'
+
+/**
+ * What the pane's foreground command proves. A shell owns it ⇒ the agent CLI exited ⇒ the turn
+ * cannot still be running. The CLI still owning it proves NOTHING: "mid-turn" and "finished,
+ * sitting at its prompt" look identical from here, which is why the transcript leg exists.
+ */
+export function decideFromPane(paneCommand: string | null | undefined): ResyncVerdict {
+ if (!paneCommand) return 'undecided'
+ return isShellCommand(paneCommand) ? 'ended' : 'undecided'
+}
+
+/**
+ * What the tail of a claude transcript proves.
+ *
+ * A turn ends with an assistant message carrying no tool call. While a tool is running, the
+ * assistant's `tool_use` sits in the file with no matching `tool_result` — that is the signature of
+ * Claude's Bash tool, which can run ~10 minutes writing nothing else, and mistaking it for a
+ * finished turn is the failure mode this whole function is shaped to avoid.
+ *
+ * The input is a bounded TAIL, so its first line is usually truncated and tool calls opened before
+ * the window are never seen. Both are handled by only ever tracking ids opened INSIDE the window:
+ * an orphan `tool_result` is dropped, an unparseable line is skipped.
+ */
+export function decideFromTranscriptTail(tail: string): ResyncVerdict {
+ const pending = new Set<string>()
+ let last: 'assistant-text' | 'assistant-tool' | 'user' | undefined
+ for (const line of tail.split('\n')) {
+ const s = line.trim()
+ if (!s) continue
+ let rec: { type?: string; message?: { content?: unknown } } | undefined
+ try {
+ rec = JSON.parse(s)
+ } catch {
+ continue // truncated head of the window, or a partially flushed write
+ }
+ const content = rec?.message?.content
+ if (rec?.type === 'assistant' && Array.isArray(content)) {
+ let sawTool = false
+ for (const c of content as { type?: string; id?: string }[]) {
+ if (c?.type === 'tool_use' && typeof c.id === 'string') {
+ pending.add(c.id)
+ sawTool = true
+ }
+ }
+ last = sawTool ? 'assistant-tool' : 'assistant-text'
+ } else if (rec?.type === 'user') {
+ if (Array.isArray(content)) {
+ for (const c of content as { type?: string; tool_use_id?: string }[]) {
+ if (c?.type === 'tool_result' && typeof c.tool_use_id === 'string') pending.delete(c.tool_use_id)
+ }
+ }
+ last = 'user'
+ }
+ }
+ if (pending.size > 0) return 'working'
+ return last === 'assistant-text' ? 'ended' : 'undecided'
+}
+
+/** The cheap probe first; the transcript only when the pane could not answer. */
+export function decideNode(paneCommand: string | null | undefined, tail: string | null): ResyncVerdict {
+ const byPane = decideFromPane(paneCommand)
+ if (byPane === 'ended') return 'ended'
+ if (tail === null) return 'undecided'
+ return decideFromTranscriptTail(tail)
+}