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): clamp a future activity time so a card still decays after a clock step

Erik Zwep1743ccd51cbc
diff --git a/src/core/subagent-replay.test.ts b/src/core/subagent-replay.test.ts
index 42713c332..4d5062e69 100644
--- a/src/core/subagent-replay.test.ts
+++ b/src/core/subagent-replay.test.ts
@@ -46,6 +46,13 @@ describe('subagent reload memory', () => {
])
expect(replay.snapshot(100 + WORKING_STALE_MS * 3 + 1)).toEqual([])
})
+ it('an activity time left in the future by a clock step back is clamped, so the start still expires', () => {
+ const replay = new SubagentReplay()
+ replay.record(start, 100)
+ replay.touch('child', 10_000_000) // stamped before the clock stepped back
+ expect(replay.snapshot(200)).toHaveLength(1) // clamped to now, the window restarts once
+ expect(replay.snapshot(200 + WORKING_STALE_MS)).toEqual([])
+ })
it('touch ignores unknown ids and never resurrects an ended start', () => {
const replay = new SubagentReplay()
replay.touch('child', 100)
diff --git a/src/core/subagent-replay.ts b/src/core/subagent-replay.ts
index b9a227542..55ca76bde 100644
--- a/src/core/subagent-replay.ts
+++ b/src/core/subagent-replay.ts
@@ -59,7 +59,11 @@ export class SubagentReplay {
private key(e: NormalizedAgentEvent): string { return JSON.stringify([e.nodeId, e.toolUseId]) }
private prune(now: number): void {
for (const [key, e] of this.starts) {
- const lastSeen = Math.max(e.subagentStartedAt!, this.lastActivity.get(key) ?? 0)
+ // An activity time ahead of `now` (the clock stepped back after it was stamped) would keep
+ // the start alive until wall time caught up; clamp it, so the silence window restarts once.
+ const last = this.lastActivity.get(key)
+ if (last !== undefined && last > now) this.lastActivity.set(key, now)
+ const lastSeen = Math.max(e.subagentStartedAt!, Math.min(last ?? 0, now))
if (now - lastSeen >= WORKING_STALE_MS || now < e.subagentStartedAt!) this.forget(key)
}
}
diff --git a/src/renderer/state/agentNodes.ts b/src/renderer/state/agentNodes.ts
index caec7e379..0aa4932e9 100644
--- a/src/renderer/state/agentNodes.ts
+++ b/src/renderer/state/agentNodes.ts
@@ -355,12 +355,18 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
sweepStaleWorking: (now = Date.now(), staleMs = WORKING_STALE_MS) =>
set((s) => {
+ // An activity time ahead of `now` (the clock stepped back after it was stamped) would keep
+ // the card working until wall time caught up; clamp it, so the silence window restarts once.
+ const future = Object.keys(s.lastActivityAt).filter((id) => s.lastActivityAt[id] > now)
+ const lastActivityAt = future.length
+ ? { ...s.lastActivityAt, ...Object.fromEntries(future.map((id) => [id, now])) }
+ : s.lastActivityAt
const stale = Object.keys(s.byId).filter(
(id) =>
s.byId[id].state === 'working' &&
- now - Math.max(s.byId[id].startedAt, s.lastActivityAt[id] ?? 0) > staleMs
+ now - Math.max(s.byId[id].startedAt, lastActivityAt[id] ?? 0) > staleMs
)
- if (!stale.length) return s
+ if (!stale.length) return future.length ? { lastActivityAt } : s
const byId = { ...s.byId }
for (const id of stale) {
const prev = byId[id]
@@ -368,7 +374,7 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
// still true, and it is `finish()`'s own fallback for the async case.
byId[id] = { ...prev, state: 'done', durationMs: prev.durationMs ?? now - prev.startedAt }
}
- return { byId }
+ return future.length ? { byId, lastActivityAt } : { byId }
}),
tidyFanout: (parentNodeId) =>
diff --git a/src/renderer/state/agentNodes.turn-boundary.test.ts b/src/renderer/state/agentNodes.turn-boundary.test.ts
index cd5e0638f..611c9f4aa 100644
--- a/src/renderer/state/agentNodes.turn-boundary.test.ts
+++ b/src/renderer/state/agentNodes.turn-boundary.test.ts
@@ -197,6 +197,16 @@ describe('sweepStaleWorking counts from the LAST activity, not from the start',
expect(useAgentNodes.getState().byId['tu1'].state).toBe('working')
})
+ it('an activity time left in the future by a clock step back is clamped, so the card still decays', () => {
+ const s = useAgentNodes.getState()
+ s.start('tu1', { parentNodeId: 'n1', startedAt: 1_000_000, lastActivityAt: 50_000_000 })
+ s.sweepStaleWorking(2_000_000)
+ expect(useAgentNodes.getState().byId['tu1'].state).toBe('working')
+ expect(useAgentNodes.getState().lastActivityAt['tu1']).toBe(2_000_000)
+ s.sweepStaleWorking(2_000_000 + WORKING_STALE_MS + 1)
+ expect(useAgentNodes.getState().byId['tu1'].state).toBe('done')
+ })
+
it('forgets the activity time when the card is dropped', () => {
const s = useAgentNodes.getState()
s.start('tu1', { parentNodeId: 'n1' })