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(sessions): preserve status on switch

Darren Kattan4ea9cba4cc0d
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 5e6cd8484..1bd295aca 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -3308,9 +3308,9 @@ export function TerminalNode({
// project switch every node unmounts, and an unconditional clear could undo the focus the
// node we just moved into already published.
presence.releaseFocus(id)
- // (The live agent state + subagent fan-out are cleared by the UNMOUNT-only effect below, not
- // here: this cleanup also runs for a respawn and for an offscreen dispose, neither of which
- // is a departure. See that effect for the whole argument.)
+ // Subagent render overrides are cleared by the UNMOUNT-only effect below, not here: this
+ // cleanup also runs for a respawn and for an offscreen dispose. Live agent state survives
+ // both this cleanup and a project-switch unmount; hooks and explicit deletion own it.
termRef.current = null
fitRef.current = null
searchAddonRef.current = null
@@ -3587,31 +3587,14 @@ export function TerminalNode({
}, [termKey])
/**
- * DEPARTURE-only bookkeeping: the node is leaving the canvas (project switch, or deletion).
- *
- * This lives in its own unmount-scoped effect rather than in the lifecycle cleanup because that
- * cleanup runs for three different events — a park, a respawn, and now an offscreen dispose — and
- * only the first of those is a departure. Clearing the live state on an offscreen dispose drops
- * the RUNNING / NEEDS-YOU badge and the subagent fan-out off a node the user can still SEE (its
- * kanban card most of all, which reads the same store) for a node that never went anywhere. And
- * guarding the clear inside that cleanup instead is worse still: a node that is DOWN registers no
- * cleanup at all, so an unmount that follows a dispose would clear nothing, ever — a leaked store
- * entry per node, in a branch about memory.
- *
- * The RESPAWN case (worktree move, Refresh) changes with it, and is correct for the same reason:
- * that node did not depart either, and its session comes back within seconds — reattached or
- * cold-restored — at which point the agent's own hooks re-fire and Canvas's `agent:status`
- * listener overwrites the state (a SessionStart is what clears it). Clearing it here would only
- * blank the badge for the gap in between, on a node the user is looking at.
- *
- * What is NOT cleared here is as deliberate as it was before: the node's PERSISTED status
- * (unread / session / sessionId) stays, because a project switch is a detach and the context
- * meter looks that sessionId up on remount. Real deletion drops the whole entry in
- * `Canvas.deleteNodes`.
+ * Remove transient subagent render overrides when this React node leaves the active canvas.
+ * Live agent status deliberately survives the unmount: selecting a session in another project
+ * swaps the whole active canvas, but the session we left is still alive and must remain Running
+ * or Waiting in the cross-project sidebar. Real node deletion removes the status explicitly in
+ * `Canvas.deleteNodes`; a later hook/session event owns every other state transition.
*/
useEffect(
() => () => {
- useAgentStatus.getState().setState(id, undefined)
useAgentNodes.getState().clearForParent(id)
},
[id]
diff --git a/src/renderer/nodes/status-lifecycle.test.ts b/src/renderer/nodes/status-lifecycle.test.ts
new file mode 100644
index 000000000..de7f14af0
--- /dev/null
+++ b/src/renderer/nodes/status-lifecycle.test.ts
@@ -0,0 +1,14 @@
+import { readFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { describe, expect, it } from 'vitest'
+
+const SOURCE = readFileSync(join(__dirname, 'TerminalNode.tsx'), 'utf8')
+
+describe('TerminalNode status lifecycle', () => {
+ it('does not clear live status when a project switch unmounts the node', () => {
+ // The sidebar spans projects, so leaving one canvas is not a session-state transition. Actual
+ // deletion owns status removal in Canvas.deleteNodes, and hooks own live state changes.
+ expect(SOURCE).not.toContain('useAgentStatus.getState().setState(id, undefined)')
+ expect(SOURCE).toContain('useAgentNodes.getState().clearForParent(id)')
+ })
+})
diff --git a/src/renderer/state/agentStatus.test.ts b/src/renderer/state/agentStatus.test.ts
index 76f0ab9db..87481be8f 100644
--- a/src/renderer/state/agentStatus.test.ts
+++ b/src/renderer/state/agentStatus.test.ts
@@ -82,6 +82,22 @@ describe('stale-working sweeper', () => {
})
})
+describe('focus tracking', () => {
+ it('switches the watched node without changing either session state', () => {
+ const left = nid()
+ const right = nid()
+ const status = useAgentStatus.getState()
+ status.setState(left, 'done', 'claude')
+ status.setState(right, 'working', 'claude')
+ status.setActive(left, true)
+ status.setActive(right, true)
+
+ expect(useAgentStatus.getState().activeId).toBe(right)
+ expect(useAgentStatus.getState().byId[left].state).toBe('done')
+ expect(useAgentStatus.getState().byId[right].state).toBe('working')
+ })
+})
+
describe('interrupt inference (Esc/Ctrl-C with no final hook)', () => {
it('flips a still-working node to done after the settle window', () => {
const id = nid()