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): replay a subagent's last activity with its start

Erik Zwepb1abb41681c9
diff --git a/src/core/subagent-replay.test.ts b/src/core/subagent-replay.test.ts
index 9815f622a..42713c332 100644
--- a/src/core/subagent-replay.test.ts
+++ b/src/core/subagent-replay.test.ts
@@ -42,7 +42,7 @@ describe('subagent reload memory', () => {
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 }
+ { ...start, subagentType: undefined, subagentStartedAt: 100, subagentLastActivityAt: 100 + WORKING_STALE_MS * 2 }
])
expect(replay.snapshot(100 + WORKING_STALE_MS * 3 + 1)).toEqual([])
})
diff --git a/src/core/subagent-replay.ts b/src/core/subagent-replay.ts
index 437d475ec..b9a227542 100644
--- a/src/core/subagent-replay.ts
+++ b/src/core/subagent-replay.ts
@@ -46,7 +46,10 @@ export class SubagentReplay {
snapshot(now = Date.now()): NormalizedAgentEvent[] {
this.prune(now)
- return [...this.starts.values()].map((e) => ({ ...e }))
+ return [...this.starts].map(([key, e]) => {
+ const last = this.lastActivity.get(key)
+ return last === undefined ? { ...e } : { ...e, subagentLastActivityAt: last }
+ })
}
clearParent(nodeId: string): void {
for (const [key, e] of this.starts) if (e.nodeId === nodeId) this.forget(key)
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 72a50b0d8..fb0c6cdab 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -15852,7 +15852,8 @@ export function Canvas() {
parentNodeId: e.nodeId,
type: e.subagentType,
label: e.taskLabel,
- startedAt: e.subagentStartedAt
+ startedAt: e.subagentStartedAt,
+ lastActivityAt: e.subagentLastActivityAt
},
// A Claude native card replacing the card its tool call drew (claude-subagent-lifecycle).
e.supersedes
diff --git a/src/renderer/state/agentNodes.ts b/src/renderer/state/agentNodes.ts
index 51766c067..caec7e379 100644
--- a/src/renderer/state/agentNodes.ts
+++ b/src/renderer/state/agentNodes.ts
@@ -97,10 +97,14 @@ interface AgentNodesState {
* expansion and selection move to the new key, so the user sees one card that never moved. Its
* streamed activity is DROPPED, not moved: the native tail reads the same transcript file from
* its first byte, and moving it would print everything twice.
+ *
+ * `lastActivityAt` seeds the sweep's clock for a card replayed after a reload; it is kept out of
+ * `byId` like every activity time. A superseded card's clock is carried too (it is a time, not
+ * text, so it cannot double) — the new key must not look older than the card it replaces.
*/
start(
toolUseId: string,
- viz: Omit<SubagentViz, 'state' | 'startedAt'> & { startedAt?: number },
+ viz: Omit<SubagentViz, 'state' | 'startedAt'> & { startedAt?: number; lastActivityAt?: number },
supersedes?: string
): void
/**
@@ -285,12 +289,19 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
return { positions, sizes }
}),
- start: (toolUseId, viz, supersedes) =>
+ start: (toolUseId, { lastActivityAt: seed, ...viz }, supersedes) =>
set((s) => {
const old = supersedes && supersedes !== toolUseId ? s.byId[supersedes] : undefined
const startedAt = viz.startedAt ?? s.byId[toolUseId]?.startedAt ?? old?.startedAt ?? Date.now()
const card: SubagentViz = { ...viz, state: 'working', startedAt }
- if (!old || !supersedes) return { byId: { ...s.byId, [toolUseId]: card } }
+ // The sweep's clock: the newest of the replayed seed, this key's own and the replaced card's.
+ const clocks = [seed, s.lastActivityAt[toolUseId], old && supersedes ? s.lastActivityAt[supersedes] : undefined]
+ .filter((t): t is number => t !== undefined)
+ const withClock = (m: Record<string, number>): Record<string, number> =>
+ clocks.length ? { ...m, [toolUseId]: Math.max(...clocks) } : m
+ if (!old || !supersedes) {
+ return { byId: { ...s.byId, [toolUseId]: card }, lastActivityAt: withClock(s.lastActivityAt) }
+ }
const next = dropCards(s, [supersedes]) as AgentNodesState
// Move what the user did to the old card, unless the new key already carries its own.
const inherit = !(toolUseId in s.byId)
@@ -299,6 +310,7 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
return {
...next,
byId: { ...next.byId, [toolUseId]: card },
+ lastActivityAt: withClock(next.lastActivityAt),
positions: carry(next.positions, s.positions),
sizes: carry(next.sizes, s.sizes),
expanded: carry(next.expanded, s.expanded),
diff --git a/src/renderer/state/agentNodes.turn-boundary.test.ts b/src/renderer/state/agentNodes.turn-boundary.test.ts
index 00fd8f8bf..cd5e0638f 100644
--- a/src/renderer/state/agentNodes.turn-boundary.test.ts
+++ b/src/renderer/state/agentNodes.turn-boundary.test.ts
@@ -187,6 +187,16 @@ describe('sweepStaleWorking counts from the LAST activity, not from the start',
expect(useAgentNodes.getState().byId).toBe(before)
})
+ it('a replayed card keeps the host-reported last activity, so the first sweep after a reload spares it', () => {
+ const s = useAgentNodes.getState()
+ const now = 1_000_000 + WORKING_STALE_MS * 3
+ vi.setSystemTime(now)
+ s.start('tu1', { parentNodeId: 'n1', startedAt: 1_000_000, lastActivityAt: now - 30_000 })
+ expect(useAgentNodes.getState().byId['tu1']).not.toHaveProperty('lastActivityAt')
+ s.sweepStaleWorking(now + 60_000)
+ expect(useAgentNodes.getState().byId['tu1'].state).toBe('working')
+ })
+
it('forgets the activity time when the card is dropped', () => {
const s = useAgentNodes.getState()
s.start('tu1', { parentNodeId: 'n1' })
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index d4baf7897..413f12049 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -135,6 +135,9 @@ export interface NormalizedAgentEvent {
backgroundSubagentIds?: string[]
/** Host-observed start time for display-only renderer reload replay. */
subagentStartedAt?: number
+ /** Host-observed time of the subagent's last transcript chunk, replayed with the start so a
+ * reloaded card keeps its remaining silence window (see `WORKING_STALE_MS`). */
+ subagentLastActivityAt?: number
// grok StopCancelled only: normalized state-less so the mirror can make the session-aware badge
// decision (a subagent cancellation must not end its parent session).
cancelReason?: