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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(terminal): age the working park snapshot; correct the relay claim (#126)
enes93c0a0b2d619
diff --git a/docs/remote-sessions.md b/docs/remote-sessions.mdindex 807b96b58..368251bdf 100644--- a/docs/remote-sessions.md+++ b/docs/remote-sessions.md@@ -422,6 +422,16 @@ the dialect deletion orphaned, blanking the phone; that flag is gone).next live mutation. Re-running `api.workspace.load()` into the bound project on reconnect closes it.- **Change the shared project without dropping the session** — sharing is fixed per hosting session(change project = stop + restart hosting). A live "share a different project" control is a follow-up.+- **Agent status is not routed into the per-session stores** — `SessionStores.agentStatus`+ (`agentStatusForApi(api)`) is built for every session, but nothing writes the non-local+ instances: Canvas's `agent:status` subscription sits above the per-project session provider and+ writes the DEFAULT store, and `useSessionStores()` has no consumers. Cosmetic until #126's+ plain-shell protection landed, which reads a node's own session store to decide whether killing+ its terminal would end a running agent (`terminal/live-work.ts`) — so **relay parks/offscreen+ releases are unprotected**: a relay node always reads `undefined` there, and `persistent:false`+ can now legitimately arrive from a tmux-less relay HOST. The consumer side is already written+ against the seam and needs no change; what is missing is a subscription per session feeding+ `SessionStores.agentStatus`.- **SDK chat node over relay** — refuses (`E_UNSUPPORTED`) in a relay tab, matching Server Edition.- **The preload↔main relay IPC boundary is not covered by vitest** (it needs Electron). Two realsend/handle + payload-shape bugs were found and fixed by inspection during 4c; the two-instancediff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsxindex 78c1cc47b..61eae6866 100644--- a/src/renderer/nodes/TerminalNode.tsx+++ b/src/renderer/nodes/TerminalNode.tsx@@ -295,6 +295,8 @@ interface ParkedTerminal {* lever reads later than that, so without this snapshot every park looks agent-less. See* `effectiveAgentState`. */parkedAgentState?: AgentState+ /** When this entry was parked — what ages the snapshot above (`parkedStateFloor`). */+ parkedAt: number/** The node's agent state RIGHT NOW, read from the store of the session this node belongs to* (a relay tab's status lives in its own instance, not the default one). Closed over at park* time because the module-level levers have no access to the component's session. */@@ -324,6 +326,7 @@ function parkDisposable(key: string): boolean {return canDisposeParkedEntry({tmuxBacked: p.tmuxBacked,parkedAgentState: p.parkedAgentState,+ parkedAt: p.parkedAt,liveAgentState: p.readAgentState()})}@@ -846,17 +849,27 @@ export function TerminalNode({// unmounts the node), so capturing it in the once-mounted lifecycle effect below is safe, like `api`.const presence = useActiveSessionPresence()/**- * THIS node's agent-status store, for the memory levers that must not kill live work.+ * THIS node's agent-status store, for the memory levers that must not kill live work — resolved+ * the way the session registry resolves it (`buildStores` → `agentStatusForApi`, memoized by api+ * identity), so this node is judged against the status table of the core it actually runs on.*- * Resolved the way the session registry resolves it — memoized by api identity — so a relay- * tab's node reads the RELAY core's status table rather than the local one. That matters now- * that `persistent:false` can arrive from a tmux-less relay HOST: such a node is exactly a- * plain-shell agent session, and against the default store its state would always read- * `undefined` (= disposable), i.e. unprotected. For the local session this IS `useAgentStatus`- * (the WeakMap is seeded with `window.nodeTerminal`), so nothing about local behavior changes.+ * WHAT THAT MEANS TODAY, PLAINLY: for the local session this resolves to the default persisted+ * instance — the exact object `useAgentStatus` exports — so local nodes are protected. For a+ * RELAY tab it resolves to that core's keyless instance, and NOTHING WRITES THAT INSTANCE YET:+ * Canvas's `agent:status` subscription sits above the per-project session provider and writes+ * the default store, and `useSessionStores()` currently has no consumers at all. So a relay+ * node's state always reads `undefined` here, and RELAY PARKS REMAIN UNPROTECTED — which now+ * matters, because `persistent:false` can arrive from a tmux-less relay HOST, i.e. exactly a+ * plain-shell agent session on someone else's machine.+ *+ * Resolving it this way regardless is deliberate and is the cheap half of the fix: routing relay+ * agent-status into the per-session stores (`useSessionStores` / `SessionStores.agentStatus` is+ * the intended seam) is a real feature and belongs in its own change. When it lands, this code+ * lights up unchanged — no branch here to find and update.** Scoped deliberately to the protection paths. The other status reads in this file predate- * per-session stores and are left exactly as they were; widening them is a separate change.+ * per-session stores and are left exactly as they were; widening them is that same separate+ * change.*/const agentStatusStore = agentStatusForApi(api).storeconst readAgentState = useCallback(@@ -3117,6 +3130,7 @@ export function TerminalNode({// Snapshot NOW, because the departure effect declared below this one clears this node's// agent status on this very unmount — every lever reads later and would see nothing.parkedAgentState: readAgentState(),+ parkedAt: Date.now(),readAgentState,cleanups,life,diff --git a/src/renderer/terminal/live-work.test.ts b/src/renderer/terminal/live-work.test.tsindex 6371d99b2..3c10eddf0 100644--- a/src/renderer/terminal/live-work.test.ts+++ b/src/renderer/terminal/live-work.test.ts@@ -1,5 +1,6 @@import { describe, it, expect } from 'vitest'-import { effectiveAgentState, wouldKillLiveWork } from './live-work'+import { WORKING_STALE_MS } from '@shared/agents/stale'+import { effectiveAgentState, parkedStateFloor, wouldKillLiveWork } from './live-work'describe('wouldKillLiveWork', () => {it('is true only for a NON-tmux session whose agent is working/waiting/blocked', () => {@@ -38,3 +39,30 @@ describe('effectiveAgentState — the snapshot FLOOR under a live store read', (expect(effectiveAgentState(undefined, undefined)).toBeUndefined()})})++describe('parkedStateFloor — a `working` snapshot ages out, the others do not', () => {+ const t0 = 1_000_000+ it('holds a working snapshot inside the stale-working window', () => {+ expect(parkedStateFloor('working', t0, t0)).toBe('working')+ expect(parkedStateFloor('working', t0, t0 + WORKING_STALE_MS)).toBe('working')+ })+ it('drops a working snapshot past it — no sweep can reach a parked entry to do it for us', () => {+ // The renderer sweep matches `state === 'working'`, and the departure clear already set this+ // node's entry to undefined; the core sweep's synthetic end edge feeds onNodeStateChange+ // consumers, never the renderer's agent:status IPC. So a CLI that died silently would leave+ // this park re-arming forever, holding a PARK_MAX slot for a session that no longer exists.+ expect(parkedStateFloor('working', t0, t0 + WORKING_STALE_MS + 1)).toBeUndefined()+ expect(parkedStateFloor('working', t0, t0 + 10 * WORKING_STALE_MS)).toBeUndefined()+ })+ it('never ages waiting/blocked — a question held open is not stale, it is waiting for the user', () => {+ expect(parkedStateFloor('waiting', t0, t0 + 10 * WORKING_STALE_MS)).toBe('waiting')+ expect(parkedStateFloor('blocked', t0, t0 + 10 * WORKING_STALE_MS)).toBe('blocked')+ })+ it('passes through done/absent, and cannot age a snapshot with no timestamp', () => {+ expect(parkedStateFloor('done', t0, t0 + 10 * WORKING_STALE_MS)).toBe('done')+ expect(parkedStateFloor(undefined, t0, t0)).toBeUndefined()+ // No stamp ⇒ no age ⇒ treated as fresh. TerminalNode always stamps; this is the safe answer+ // for a caller that forgot, since the alternative is dropping a live agent's protection.+ expect(parkedStateFloor('working', undefined, t0 + 10 * WORKING_STALE_MS)).toBe('working')+ })+})diff --git a/src/renderer/terminal/live-work.ts b/src/renderer/terminal/live-work.tsindex 7edb9e5b3..f8f09631a 100644--- a/src/renderer/terminal/live-work.ts+++ b/src/renderer/terminal/live-work.ts@@ -21,6 +21,7 @@* reclaiming it is free); a plain terminal, a finished agent and an unknown state are never* protected either (nothing is running to lose).*/+import { WORKING_STALE_MS } from '@shared/agents/stale'import type { AgentState } from '@shared/agents/normalize'/**@@ -76,3 +77,37 @@ export function effectiveAgentState(): AgentState | undefined {return live ?? atParkTime}++/**+ * The park-time snapshot, AGED: a `working` snapshot stops counting once it is older than+ * `WORKING_STALE_MS`; every other state keeps counting indefinitely.+ *+ * WHY THE SNAPSHOT NEEDS AN EXPIRY THE LIVE STATE DOES NOT. A `working` session that dies without+ * saying so is an ordinary event (Esc during a tool call runs no Stop hook, a killed CLI, a slept+ * machine, a dropped link), and the product already has one answer for it: the stale-working+ * sweep. Neither copy of that sweep can reach a PARKED node's snapshot, though — the renderer's+ * matches `state === 'working'` in the store, and the departure clear on the parking unmount has+ * already blanked that entry; the core's mirror fires an `onNodeStateChange` edge that reaches its+ * own consumers but never the renderer's `agent:status` IPC. So nothing would ever retire this+ * evidence, and the park would re-arm its expiry for the rest of the run, permanently holding one+ * of `PARK_MAX` slots for a session that no longer exists.+ *+ * The window is the SHARED constant, not a new number: this is the same judgment call about the+ * same signal ("a working node this quiet is presumed gone"), and two numbers for one question+ * drift apart. A LIVE `working` reading is deliberately never aged here — the store still knows+ * about that node, so the renderer sweep can and does correct it, and second-guessing a store that+ * says "working" right now would kill a real turn.+ *+ * `waiting`/`blocked` never expires, and that asymmetry is the point: a question held open is not+ * a stale signal, it is a session waiting for the user, and no elapsed time makes ending it right.+ * A snapshot with no timestamp cannot be aged and is treated as fresh — the safe direction, since+ * the alternative drops a live agent's protection over a bookkeeping omission.+ */+export function parkedStateFloor(+ atParkTime: AgentState | undefined,+ parkedAt: number | undefined,+ now: number+): AgentState | undefined {+ if (atParkTime !== 'working' || parkedAt === undefined) return atParkTime+ return now - parkedAt > WORKING_STALE_MS ? undefined : atParkTime+}diff --git a/src/renderer/terminal/offscreen-policy.ts b/src/renderer/terminal/offscreen-policy.tsindex 4928bd564..4ed36f555 100644--- a/src/renderer/terminal/offscreen-policy.ts+++ b/src/renderer/terminal/offscreen-policy.ts@@ -87,12 +87,14 @@ export const OFFSCREEN_DEFER_RETRY_MS = 60_000* for is the user's own session ending, and "give up and release anyway" IS the reported bug — a* cap would just be the same kill with a delay in front of it. So no clock is an input at all.*- * BE HONEST ABOUT WHAT THAT COSTS. `working` always resolves — the stale-working sweep- * (`shared/agents/stale.ts`, 20 minutes) fires a synthetic end edge for a session that died- * without saying so, and that reaches this store like any other event. `waiting`/`blocked` has no- * such sweep, and legitimately so: a question held open for the user is not stale, it is waiting- * for them. A user who never answers therefore holds this node's viewer for as long as the app- * runs.+ * BE HONEST ABOUT WHAT THAT COSTS. `working` resolves within `WORKING_STALE_MS` even when the CLI+ * dies without saying so: Canvas's 60-second `sweepStaleWorking` blanks a working entry that has+ * gone that quiet, and this predicate reads that entry. (It works HERE because the node is still+ * MOUNTED, so its store entry is intact — the park path cannot rely on the same sweep, which is+ * why the park's snapshot ages itself out instead; see `live-work.ts`'s `parkedStateFloor`.)+ * `waiting`/`blocked` has no such sweep, and legitimately so: a question held open for the user is+ * not stale, it is waiting for them. A user who never answers therefore holds this node's viewer+ * for as long as the app runs.** That is accepted, because the exposure is bounded in the dimension that matters. It is not* unbounded growth: it is at most one held viewer (~15 MB) per non-tmux node that is actuallydiff --git a/src/renderer/terminal/park-budget.test.ts b/src/renderer/terminal/park-budget.test.tsindex e4919a979..87c4cefb8 100644--- a/src/renderer/terminal/park-budget.test.ts+++ b/src/renderer/terminal/park-budget.test.ts@@ -1,4 +1,5 @@import { describe, it, expect, beforeEach } from 'vitest'+import { WORKING_STALE_MS } from '@shared/agents/stale'import {PARK_MAX,PARK_RECHECK_MS,@@ -126,6 +127,38 @@ describe('a parked plain-shell agent survives the departure clear (#126)', () =>expect(disposable('ask')).toBe(false)})+ /**+ * The one bound the snapshot needs, because no sweep can supply it.+ *+ * A `working` snapshot outlives its evidence when the CLI dies without saying so (Esc mid-tool,+ * a killed process, a slept machine). Neither stale-working sweep can correct a PARKED node: the+ * renderer's matches `state === 'working'` and the departure clear already blanked the entry;+ * the core's fires an `onNodeStateChange` edge that never reaches the renderer's agent:status+ * IPC. Unaged, that park re-arms its expiry forever and permanently eats a PARK_MAX slot.+ */+ describe('the working snapshot ages out at WORKING_STALE_MS', () => {+ const parkedAt = 5_000_000+ const working: ParkedEntryState = { tmuxBacked: false, parkedAgentState: 'working', parkedAt }++ it('protects inside the window and releases past it', () => {+ expect(canDisposeParkedEntry(working, parkedAt + WORKING_STALE_MS)).toBe(false)+ expect(canDisposeParkedEntry(working, parkedAt + WORKING_STALE_MS + 1)).toBe(true)+ })++ it('keeps protecting a waiting snapshot past the same window', () => {+ const waiting: ParkedEntryState = { ...working, parkedAgentState: 'waiting' }+ expect(canDisposeParkedEntry(waiting, parkedAt + 10 * WORKING_STALE_MS)).toBe(false)+ })++ it('never ages a LIVE working reading — the live sweep owns that case', () => {+ // The store says it is working NOW, so the snapshot's age is irrelevant: this is a real+ // session mid-turn (and the renderer sweep CAN reach a node the store still knows about).+ expect(+ canDisposeParkedEntry({ ...working, liveAgentState: 'working' }, parkedAt + 10 * WORKING_STALE_MS)+ ).toBe(false)+ })+ })+it('still disposes the cases that were always disposable, snapshot or not', () => {expect(canDisposeParkedEntry({ tmuxBacked: true, parkedAgentState: 'working' })).toBe(true)expect(canDisposeParkedEntry({ tmuxBacked: false, parkedAgentState: 'done' })).toBe(true)diff --git a/src/renderer/terminal/park-budget.ts b/src/renderer/terminal/park-budget.tsindex bac1f60da..c3b79c8aa 100644--- a/src/renderer/terminal/park-budget.ts+++ b/src/renderer/terminal/park-budget.ts@@ -1,4 +1,9 @@-import { effectiveAgentState, wouldKillLiveWork, type LiveWorkInput } from './live-work'+import {+ effectiveAgentState,+ parkedStateFloor,+ wouldKillLiveWork,+ type LiveWorkInput+} from './live-work'import type { AgentState } from '@shared/agents/normalize'/**@@ -25,13 +30,13 @@ export const PARK_MAX = 12* changed. The cost of the coarseness is bounded and small — one already-parked xterm held at* most a minute past the moment it became disposable.*- * How long a protected park can live in the worst case: a `working` one resolves within the- * stale-working sweep's window (`shared/agents/stale.ts`, 20 minutes) even if the CLI died without- * saying so, because that sweep's synthetic end edge reaches the store like any other event. A- * `waiting`/`blocked` one has no such sweep and is held until the user answers the question the- * badge is showing them — possibly for the whole run. Accepted, and bounded by NODE COUNT rather- * than by time: at most one held xterm buffer per non-tmux node actually sitting on an unanswered- * prompt. See `offscreen-policy.ts`'s live-work deferral for the same argument at more length.+ * How long a protected park can live in the worst case. A `working` one is bounded at+ * `WORKING_STALE_MS` — NOT because a sweep corrects it (neither sweep can reach a parked entry;+ * see `parkedStateFloor`, which is why the snapshot ages itself out) but because the floor stops+ * counting there. A `waiting`/`blocked` one is NOT bounded in time at all: it is held until the+ * user answers the question the badge is showing them, possibly for the whole run. Accepted, and+ * bounded by NODE COUNT instead — at most one held xterm buffer per non-tmux node actually sitting+ * on an unanswered prompt. See `offscreen-policy.ts`'s live-work deferral for the longer argument.*/export const PARK_RECHECK_MS = 60_000@@ -58,17 +63,23 @@ export interface ParkedEntryState {/** The node's agent state AT PARK TIME. The floor — see `effectiveAgentState` for why a park* cannot rely on a live read alone. */parkedAgentState?: AgentState+ /** When the snapshot was taken. Absent ⇒ it cannot be aged (see `parkedStateFloor`). */+ parkedAt?: number/** The node's agent state RIGHT NOW, read from that node's own agent-status store. */liveAgentState?: AgentState}-/** `canDisposePark` for a park entry: the live read with the park-time snapshot under it. This is- * the form all three park levers actually ask (they hold entries, not states), and the one place- * the floor is applied — a lever that assembled the state itself could forget it. */-export function canDisposeParkedEntry(e: ParkedEntryState): boolean {+/** `canDisposePark` for a park entry: the live read, with the AGED park-time snapshot under it.+ * This is the form all three park levers actually ask (they hold entries, not states), and the+ * one place the floor is assembled — a lever that built the state itself could forget half of+ * it. `now` is injected for tests. */+export function canDisposeParkedEntry(e: ParkedEntryState, now: number = Date.now()): boolean {return canDisposePark({tmuxBacked: e.tmuxBacked,- agentState: effectiveAgentState(e.liveAgentState, e.parkedAgentState)+ agentState: effectiveAgentState(+ e.liveAgentState,+ parkedStateFloor(e.parkedAgentState, e.parkedAt, now)+ )})}