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(server): untrack agent tails on node close (ptyDestroy) + clear backpressure paused map on detach

enes15fdcc209bb1
diff --git a/src/server/agent-status.test.ts b/src/server/agent-status.test.ts
index e2c64f5a6..7176da0dd 100644
--- a/src/server/agent-status.test.ts
+++ b/src/server/agent-status.test.ts
@@ -95,4 +95,41 @@ describe('wireAgentStatus', () => {
fh.fireRaw('codex', 'n1', { hook_event_name: 'PreToolUse', tool_name: 'Task', tool_use_id: 'x' })
expect(sub.calls).toEqual([])
})
+
+ it('ptyDestroy untracks the node context tail and finishes its subagents, clearing the maps', () => {
+ const fh = fakeHooks()
+ const sub = recTail()
+ const ctx = recTail()
+ wireAgentStatus(platform, {
+ hooks: fh.hooks as never,
+ subagentTail: sub.tail as never,
+ contextTail: ctx.tail as never
+ })
+ // A safe local transcript path so contextTail.track runs and nodeContextSession is set.
+ const transcriptPath = path.join(os.homedir(), '.claude', 'projects', 't.jsonl')
+ // Populate the maps: a raw PreToolUse(Task) sets nodeContextSession + tracks a subagent.
+ fh.fireRaw('claude', 'n1', {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'Task',
+ tool_use_id: 'tu1',
+ session_id: 's1',
+ transcript_path: transcriptPath
+ })
+ // Node closes → ptyDestroy cast fires the teardown listener.
+ platform.cast(
+ platform.attach({ sendText: () => {}, sendBinary: () => {} }),
+ IPC.ptyDestroy,
+ ['n1']
+ )
+ expect(ctx.calls.some((c) => c.m === 'untrack' && c.args[0] === 's1')).toBe(true)
+ expect(sub.calls.some((c) => c.m === 'finish' && c.args[0] === 'tu1')).toBe(true)
+ // Re-destroying the same node is a harmless no-op (maps already cleared).
+ const before = ctx.calls.length + sub.calls.length
+ platform.cast(
+ platform.attach({ sendText: () => {}, sendBinary: () => {} }),
+ IPC.ptyDestroy,
+ ['n1']
+ )
+ expect(ctx.calls.length + sub.calls.length).toBe(before)
+ })
})
diff --git a/src/server/agent-status.ts b/src/server/agent-status.ts
index 2d1f57bee..26eb5f97b 100644
--- a/src/server/agent-status.ts
+++ b/src/server/agent-status.ts
@@ -141,4 +141,22 @@ export function wireAgentStatus(platform: ServerPlatform, opts: WireAgentStatusO
nodeSubagents.delete(nodeId)
}
})
+
+ // Node close → tear down its tails and clear the maps (server parity with desktop
+ // `src/main/index.ts`'s local `ipcMain.on(IPC.ptyDestroy, …)` branch; the remote/SSH lines are
+ // dropped — the server has no SSH-project manager). Coexists with PtyManager's own ptyDestroy
+ // listener via the multi-listener `on`: that one kills the tmux session, this untracks the
+ // tails. Untracking a non-tracked session/subagent is a no-op, so re-destroy is harmless.
+ platform.on(IPC.ptyDestroy, (nodeId: string) => {
+ const sessionId = nodeContextSession.get(nodeId)
+ if (sessionId) {
+ contextTail.untrack(sessionId)
+ nodeContextSession.delete(nodeId)
+ }
+ const subs = nodeSubagents.get(nodeId)
+ if (subs) {
+ for (const toolUseId of subs) subagentTail.finish(toolUseId)
+ nodeSubagents.delete(nodeId)
+ }
+ })
}
diff --git a/src/server/platform-server.test.ts b/src/server/platform-server.test.ts
index 1f5b03a22..790aa5bfe 100644
--- a/src/server/platform-server.test.ts
+++ b/src/server/platform-server.test.ts
@@ -79,6 +79,21 @@ describe('ServerPlatform', () => {
expect(b.texts).toHaveLength(1)
})
+ it('detach clears the backpressure paused map', () => {
+ const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })
+ p.setFlowController(() => {})
+ const ui = p.attach({ sendText: () => {}, sendBinary: () => {}, bufferedAmount: () => 2_000_000 })
+ p.sendTo(ui, 'pty:data:s1', 'x') // buffered > high → session s1 marked paused
+ p.detach(ui)
+ // After detach the paused entry is gone: a fresh attach + low-buffer send does not emit a
+ // spurious resume.
+ const flow: Array<{ sid: string; resume: boolean }> = []
+ p.setFlowController((sid, resume) => flow.push({ sid, resume }))
+ const ui2 = p.attach({ sendText: () => {}, sendBinary: () => {}, bufferedAmount: () => 0 })
+ p.sendTo(ui2, 'pty:data:s1', 'x')
+ expect(flow).toEqual([]) // not paused (map was cleared) → no resume fired
+ })
+
it('exposes userDataDir/appVersion/isPackaged; openExternal rejects', async () => {
const p = new ServerPlatform({ userDataDir: '/data', appVersion: '9.9.9' })
expect(p.userDataDir).toBe('/data')
diff --git a/src/server/platform-server.ts b/src/server/platform-server.ts
index 6b98165b0..76d0f4489 100644
--- a/src/server/platform-server.ts
+++ b/src/server/platform-server.ts
@@ -109,6 +109,9 @@ export class ServerPlatform implements CorePlatform {
detach(uiId: number): void {
this.sinks.delete(uiId)
+ // v1 is single-UI: every paused session belonged to the departing connection, so drop the
+ // whole backpressure map to avoid a stale pause leaking into the next attach.
+ this.paused.clear()
}
async dispatch(uiId: number, req: RpcRequest): Promise<RpcOk | RpcErr> {