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(subagents): keep a long-running subagent in the reload replay while it streams

Erik Zwep54d85691445a
diff --git a/src/core/subagent-replay.test.ts b/src/core/subagent-replay.test.ts
index 5d25b3af9..9815f622a 100644
--- a/src/core/subagent-replay.test.ts
+++ b/src/core/subagent-replay.test.ts
@@ -35,6 +35,27 @@ describe('subagent reload memory', () => {
replay.record(start, 500)
expect(replay.snapshot(499)).toEqual([])
})
+ it('keeps a long-running start while its transcript streams, and expires it after the window of silence', () => {
+ // WORKING_STALE_MS is "this long without a sign of life": counted from the start alone, a
+ // subagent running longer than the window was dropped here, so a renderer reload lost its card.
+ const replay = new SubagentReplay()
+ replay.record(start, 100)
+ replay.touch('child', 100 + WORKING_STALE_MS * 2)
+ expect(replay.snapshot(100 + WORKING_STALE_MS * 2 + 60_000)).toEqual([
+ { ...start, subagentType: undefined, subagentStartedAt: 100 }
+ ])
+ expect(replay.snapshot(100 + WORKING_STALE_MS * 3 + 1)).toEqual([])
+ })
+ it('touch ignores unknown ids and never resurrects an ended start', () => {
+ const replay = new SubagentReplay()
+ replay.touch('child', 100)
+ replay.record(start, 200)
+ replay.record({ ...start, kind: 'subagent-end' }, 300)
+ replay.touch('child', 400)
+ expect(replay.snapshot(400)).toEqual([])
+ replay.record(start, 500)
+ expect(replay.snapshot(500 + WORKING_STALE_MS)).toEqual([])
+ })
it('the shared mirror path records starts and synthetic ends; node deletion clears replay', () => {
recordAgentEvent(start)
expect(subagentReplay.snapshot()).toHaveLength(1)
diff --git a/src/core/subagent-replay.ts b/src/core/subagent-replay.ts
index 7815867ed..437d475ec 100644
--- a/src/core/subagent-replay.ts
+++ b/src/core/subagent-replay.ts
@@ -5,6 +5,8 @@ import { WORKING_STALE_MS } from '../shared/agents/stale'
* Replays only running starts: old approvals, turn completions and alerts must not fire again. */
export class SubagentReplay {
private readonly starts = new Map<string, NormalizedAgentEvent>()
+ /** Last streamed transcript chunk per start, so expiry counts from the last sign of life. */
+ private readonly lastActivity = new Map<string, number>()
constructor(private readonly limit = 512) {}
record(event: NormalizedAgentEvent, now = Date.now()): void {
@@ -12,7 +14,7 @@ export class SubagentReplay {
if (event.kind === 'session' && event.sessionPhase) {
this.clearParent(event.nodeId)
} else if (event.kind === 'subagent-end' && event.toolUseId) {
- this.starts.delete(this.key(event))
+ this.forget(this.key(event))
} else if (event.kind === 'subagent-start' && event.toolUseId) {
const key = this.key(event)
// A native card REPLACING the card its tool call drew (core/claude-subagent-lifecycle.ts):
@@ -32,22 +34,30 @@ export class SubagentReplay {
taskLabel: event.taskLabel?.slice(0, 4000), subagentStartedAt: replaced?.subagentStartedAt ?? now
})
}
- while (this.starts.size > this.limit) this.starts.delete(this.starts.keys().next().value!)
+ while (this.starts.size > this.limit) this.forget(this.starts.keys().next().value!)
}
}
+ /** A subagent streamed transcript. Activity is keyed by tool_use_id only (that is all the tails
+ * know); an id with no running start is ignored, so this never creates or revives an entry. */
+ touch(toolUseId: string, now = Date.now()): void {
+ for (const [key, e] of this.starts) if (e.toolUseId === toolUseId) this.lastActivity.set(key, now)
+ }
+
snapshot(now = Date.now()): NormalizedAgentEvent[] {
this.prune(now)
return [...this.starts.values()].map((e) => ({ ...e }))
}
clearParent(nodeId: string): void {
- for (const [key, e] of this.starts) if (e.nodeId === nodeId) this.starts.delete(key)
+ for (const [key, e] of this.starts) if (e.nodeId === nodeId) this.forget(key)
}
- clear(): void { this.starts.clear() }
+ clear(): void { this.starts.clear(); this.lastActivity.clear() }
+ private forget(key: string): void { this.starts.delete(key); this.lastActivity.delete(key) }
private key(e: NormalizedAgentEvent): string { return JSON.stringify([e.nodeId, e.toolUseId]) }
private prune(now: number): void {
for (const [key, e] of this.starts) {
- if (now - e.subagentStartedAt! >= WORKING_STALE_MS || now < e.subagentStartedAt!) this.starts.delete(key)
+ const lastSeen = Math.max(e.subagentStartedAt!, this.lastActivity.get(key) ?? 0)
+ if (now - lastSeen >= WORKING_STALE_MS || now < e.subagentStartedAt!) this.forget(key)
}
}
}
diff --git a/src/main/index.ts b/src/main/index.ts
index 0897489bc..7ec0a9ddf 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -2574,6 +2574,7 @@ app.whenReady().then(async () => {
// A raw listener drives the transcript-tailing features (context meter + subagent transcript),
// which need the raw transcript_path the NormalizedAgentEvent intentionally drops.
const subagentTail = createSubagentTail(({ toolUseId, chunk }) => {
+ subagentReplay.touch(toolUseId) // a streaming subagent is alive: keep it in the reload replay
if (!win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
})
// Claude's two subagent signal paths merged into one card per child (native SubagentStart/Stop
diff --git a/src/main/remote-subagent-tail.ts b/src/main/remote-subagent-tail.ts
index 2c1d089fc..8aeb677a1 100644
--- a/src/main/remote-subagent-tail.ts
+++ b/src/main/remote-subagent-tail.ts
@@ -14,6 +14,7 @@
// a capped read lands mid-multibyte routinely, and only base64 keeps the byte accounting exact.
import { type BrowserWindow } from 'electron'
import { IPC } from '../shared/ipc'
+import { subagentReplay } from '../core/subagent-replay'
import { formatSubagentChunk, splitCompleteLines, SUBAGENT_READ_CAP } from '../core/subagent-tail'
import type { RemoteFile, RemoteFileRef } from './remote-ssh/remote-file'
@@ -40,7 +41,9 @@ export function createRemoteSubagentTail(win: BrowserWindow, remoteFile: RemoteF
const resumeAt = new Map<string, { path: string; offset: number }>()
const send = (toolUseId: string, chunk: string): void => {
- if (chunk && !win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
+ if (!chunk) return
+ subagentReplay.touch(toolUseId) // a streaming subagent is alive: keep it in the reload replay
+ if (!win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
}
// One async read+stream pass. Fail-open: RemoteFile returns empty on error. The `reading`
diff --git a/src/server/agent-status.ts b/src/server/agent-status.ts
index c3bdd7cd8..53c93dcf8 100644
--- a/src/server/agent-status.ts
+++ b/src/server/agent-status.ts
@@ -31,6 +31,7 @@ import { linkedClaudeConfigDirs } from '../core/claude-config-dir'
import { isAsyncSubagentLaunch, grokRawFields, type NormalizedAgentEvent } from '../shared/agents/normalize'
import { applyGrokHookSession } from '../core/grok-hook-session'
import { IPC } from '../shared/ipc'
+import { subagentReplay } from '../core/subagent-replay'
import type { ServerPlatform } from './platform-server'
/** The narrow surface of the hook server this module needs — injectable for tests. */
@@ -81,6 +82,7 @@ export function wireAgentStatus(
const subagentTail =
opts.subagentTail ??
createSubagentTail(({ toolUseId, chunk }) => {
+ subagentReplay.touch(toolUseId) // a streaming subagent is alive: keep it in the reload replay
platform.broadcast(IPC.agentSubagentActivity, { toolUseId, chunk })
})
// Claude's two subagent signal paths merged into one card per child (native SubagentStart/Stop