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): decay a subagent card from its last activity, not its start

Erik Zwepdab267ed818b
diff --git a/src/renderer/state/agentNodes.ts b/src/renderer/state/agentNodes.ts
index 8e7e3114a..51766c067 100644
--- a/src/renderer/state/agentNodes.ts
+++ b/src/renderer/state/agentNodes.ts
@@ -40,6 +40,11 @@ interface AgentNodesState {
* expanded SubagentNode subscribes here, per id.
*/
activityById: Record<string, string>
+ /**
+ * When each subagent last streamed transcript, for `sweepStaleWorking`. Kept out of `byId` for the
+ * same reason as `activityById`: it changes on every chunk, and Canvas lays out from `byId`.
+ */
+ lastActivityAt: Record<string, number>
/**
* Per-ephemeral-node UI overrides (keyed by node id: subagent ids + `loop-<parentId>`).
* `positions` holds an OFFSET FROM THE PARENT AGENT NODE, not a canvas position: the card
@@ -136,7 +141,7 @@ interface AgentNodesState {
*/
clearFinishedForParent(parentNodeId: string): void
/**
- * Mark every card still `working` past `staleMs` as done — the decay `clearFinishedForParent`
+ * Mark every card still `working` with no sign of life for `staleMs` as done — the decay `clearFinishedForParent`
* depends on.
*
* Without it a subagent whose `finish()` never arrives (crashed CLI, killed pane, slept machine)
@@ -145,6 +150,10 @@ interface AgentNodesState {
* exists because three surfaces each invented their own timeout, and a subagent card inventing a
* fourth would be the same mistake in a new place.
*
+ * "No sign of life" counts from the card's last streamed activity, falling back to its start.
+ * Counting from the start alone declared every subagent that ran longer than the window dead
+ * while its transcript was still streaming, and the next turn boundary then removed its card.
+ *
* It marks done rather than deleting, so the card stays readable and the next turn boundary takes
* it; `finish()` landing late is a no-op on an entry that is already done.
*/
@@ -200,6 +209,7 @@ function dropCards(s: AgentNodesState, ids: string[]): Partial<AgentNodesState>
if (!ids.length) return s
const byId = { ...s.byId }
const activityById = { ...s.activityById }
+ const lastActivityAt = { ...s.lastActivityAt }
const positions = { ...s.positions }
const sizes = { ...s.sizes }
const expanded = { ...s.expanded }
@@ -208,6 +218,7 @@ function dropCards(s: AgentNodesState, ids: string[]): Partial<AgentNodesState>
for (const id of ids) {
delete byId[id]
delete activityById[id]
+ delete lastActivityAt[id]
delete positions[id]
delete sizes[id]
delete expanded[id]
@@ -215,7 +226,7 @@ function dropCards(s: AgentNodesState, ids: string[]): Partial<AgentNodesState>
// A card that just vanished must not stay "selected" — a later card reusing the id would come
// back pre-selected.
const selectedId = s.selectedId && ids.includes(s.selectedId) ? null : s.selectedId
- return { byId, activityById, positions, sizes, expanded, selectedId }
+ return { byId, activityById, lastActivityAt, positions, sizes, expanded, selectedId }
}
function saveLoopOverrides(s: Overrides): void {
@@ -234,6 +245,7 @@ function saveLoopOverrides(s: Overrides): void {
export const useAgentNodes = create<AgentNodesState>((set) => ({
byId: {},
activityById: {},
+ lastActivityAt: {},
selectedId: null,
autoHideFinished: false,
...loadLoopOverrides(),
@@ -318,7 +330,10 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
set((s) => {
if (!s.byId[toolUseId]) return s
const activity = ((s.activityById[toolUseId] ?? '') + chunk).slice(-12000) // bounded tail
- return { activityById: { ...s.activityById, [toolUseId]: activity } }
+ return {
+ activityById: { ...s.activityById, [toolUseId]: activity },
+ lastActivityAt: { ...s.lastActivityAt, [toolUseId]: Date.now() }
+ }
}),
clearForParent: (parentNodeId) => set((s) => dropCards(s, cardsOf(s, parentNodeId))),
@@ -329,7 +344,9 @@ export const useAgentNodes = create<AgentNodesState>((set) => ({
sweepStaleWorking: (now = Date.now(), staleMs = WORKING_STALE_MS) =>
set((s) => {
const stale = Object.keys(s.byId).filter(
- (id) => s.byId[id].state === 'working' && now - s.byId[id].startedAt > staleMs
+ (id) =>
+ s.byId[id].state === 'working' &&
+ now - Math.max(s.byId[id].startedAt, s.lastActivityAt[id] ?? 0) > staleMs
)
if (!stale.length) return s
const byId = { ...s.byId }
diff --git a/src/renderer/state/agentNodes.turn-boundary.test.ts b/src/renderer/state/agentNodes.turn-boundary.test.ts
index 4763dba14..00fd8f8bf 100644
--- a/src/renderer/state/agentNodes.turn-boundary.test.ts
+++ b/src/renderer/state/agentNodes.turn-boundary.test.ts
@@ -11,7 +11,7 @@
// exercised here rather than left to the store test above it: the guard's evidence ("any card that
// has not finished pins its parent") is derived from this same store, so the wipe let a parent with
// live background agents read as idle and get its CLI `/exit`ed.
-import { beforeEach, describe, expect, it } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useAgentNodes } from './agentNodes'
import { buildHibernationCandidates } from '../lib/hibernationCandidates'
import { WORKING_STALE_MS } from '@shared/agents/stale'
@@ -20,6 +20,7 @@ const reset = (): void => {
useAgentNodes.setState({
byId: {},
activityById: {},
+ lastActivityAt: {},
positions: {},
sizes: {},
expanded: {},
@@ -147,6 +148,54 @@ describe('sweepStaleWorking — the decay a kept card owes', () => {
})
})
+describe('sweepStaleWorking counts from the LAST activity, not from the start', () => {
+ // WORKING_STALE_MS means "this long without any sign of life". Counting it from `startedAt`
+ // declared every subagent that ran longer than the window dead while its transcript was still
+ // streaming, and the next turn boundary then removed the card for good.
+ beforeEach(() => {
+ reset()
+ vi.useFakeTimers()
+ vi.setSystemTime(1_000_000)
+ })
+ afterEach(() => vi.useRealTimers())
+
+ it('keeps a long-running card working while its transcript keeps streaming', () => {
+ const s = useAgentNodes.getState()
+ s.start('tu1', { parentNodeId: 'n1' })
+ vi.setSystemTime(1_000_000 + WORKING_STALE_MS * 3)
+ s.appendActivity('tu1', '$ Read file.ts\n')
+ s.sweepStaleWorking(1_000_000 + WORKING_STALE_MS * 3 + 60_000)
+ expect(useAgentNodes.getState().byId['tu1'].state).toBe('working')
+ useAgentNodes.getState().clearFinishedForParent('n1')
+ expect(useAgentNodes.getState().byId['tu1']).toBeDefined()
+ })
+
+ it('still decays a card whose last activity is older than the window', () => {
+ const s = useAgentNodes.getState()
+ s.start('tu1', { parentNodeId: 'n1' })
+ vi.setSystemTime(1_000_000 + 60_000)
+ s.appendActivity('tu1', 'last words\n')
+ s.sweepStaleWorking(1_000_000 + 60_000 + WORKING_STALE_MS + 1)
+ expect(useAgentNodes.getState().byId['tu1'].state).toBe('done')
+ })
+
+ it('does not touch byId on activity, so the canvas does not re-render per chunk', () => {
+ const s = useAgentNodes.getState()
+ s.start('tu1', { parentNodeId: 'n1' })
+ const before = useAgentNodes.getState().byId
+ s.appendActivity('tu1', 'chunk\n')
+ expect(useAgentNodes.getState().byId).toBe(before)
+ })
+
+ it('forgets the activity time when the card is dropped', () => {
+ const s = useAgentNodes.getState()
+ s.start('tu1', { parentNodeId: 'n1' })
+ s.appendActivity('tu1', 'chunk\n')
+ useAgentNodes.getState().clearForParent('n1')
+ expect(useAgentNodes.getState().lastActivityAt['tu1']).toBeUndefined()
+ })
+})
+
describe('autoHideFinished: dropping a card the moment its subagent finishes', () => {
beforeEach(reset)