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(relay): session-scope terminal park/co-state keys + dispose relay session on load failure (project-scope Task 4 review)
enes490a22e9d1b0
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex 8ff3cbb26..e3a13f994 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -1380,7 +1380,8 @@ export function Canvas() {if (projectId !== useProjects.getState().activeProjectId) {// Not on screen (a parked / background project): no terminal is mounted, but one may be// PARKED from a recent project switch — dispose it, as an active-project remove does.- if (mutation.op === 'remove') disposeTerminalOnUnmount(mutation.id)+ if (mutation.op === 'remove')+ disposeTerminalOnUnmount(sessionForProject(projectId).id, mutation.id)if (useProjects.getState().applyNodeMutation(projectId, mutation)) markDirty()return}@@ -1394,7 +1395,8 @@ export function Canvas() {// module-level state survives the node, and if the owner UNDOES the delete we are left// holding a node that reads "closed by another user" while its session is alive again.const gone = nodesRef.current.find((n) => n.id === mutation.id)- if (gone?.type === 'terminal') disposeTerminalOnUnmount(gone.id)+ if (gone?.type === 'terminal')+ disposeTerminalOnUnmount(sessionForProject(projectId).id, gone.id)}// Keep the ref in step immediately: a burst (a peer's bulk delete) arrives within one tick,// before React re-renders, and each mutation must build on the previous one.@@ -2485,7 +2487,8 @@ export function Canvas() {if (!set.has(n.id)) return// Permanent delete: the upcoming unmount must dispose the xterm, not park it (the// session is being destroyed right here). Also drops an already-parked entry.- if (n.type === 'terminal') disposeTerminalOnUnmount(n.id)+ if (n.type === 'terminal')+ disposeTerminalOnUnmount(sessionForProject(useProjects.getState().activeProjectId ?? '').id, n.id)if (n.type === 'terminal') transport.destroy(n.id)// Chat nodes: permanently kill the SDK driver + drop the live chat state. The driver// lives across project switches (only permanent delete kills it), so this belongs here,@@ -4496,7 +4499,7 @@ export function Canvas() {if (projectId === activeProjectId) {deleteNodes([id])} else {- disposeTerminalOnUnmount(id) // node may be parked from the project switch+ disposeTerminalOnUnmount(sessionForProject(projectId).id, id) // node may be parked from the project switchtransport.destroy(id)// Chat nodes in an inactive project keep their driver running across the switch;// dispose is a no-op for non-chat ids, so call it unconditionally like destroy.@@ -5048,7 +5051,7 @@ export function Canvas() {const project = store.getProject(id)project?.nodes.forEach((n) => {if ((n.kind ?? 'terminal') === 'terminal') {- disposeTerminalOnUnmount(n.id) // may be parked from a recent switch away+ disposeTerminalOnUnmount(sessionForProject(id).id, n.id) // may be parked from a recent switch awaytransport.destroy(n.id)}if ((n.kind ?? 'terminal') === 'chat') {diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsxindex b550e6e99..01f679fe1 100644--- a/src/renderer/nodes/TerminalNode.tsx+++ b/src/renderer/nodes/TerminalNode.tsx@@ -37,6 +37,7 @@ import {shouldApplyResync,stripTrailingNewline,takeRecycled,+ terminalKey,terminalKeyAction,toXtermText,SHIFT_ENTER_SEQ,@@ -182,26 +183,34 @@ function disposeParked(p: ParkedTerminal): void {p.term.dispose()}-/** Drop a node's parked terminal (if any), detaching its PTY client. Canvas calls this on- * permanent deletion — the node may be unmounted (parked) when it is deleted. */-export function disposeParkedTerminal(id: string): void {- const p = parkedTerminals.get(id)+/** Drop a node's parked terminal (if any), detaching its PTY client. `key` is the session-scoped+ * `terminalKey(sessionId, nodeId)` — the module maps are keyed by it so a local and a relay node+ * that share a bare id never collide. */+export function disposeParkedTerminal(key: string): void {+ const p = parkedTerminals.get(key)if (!p) return- parkedTerminals.delete(id)+ parkedTerminals.delete(key)disposeParked(p)}-/** Nodes whose next unmount must dispose (not park) — set on permanent deletion, where the- * unmount runs AFTER the session was already destroyed, so parking would keep a dead xterm. */+/** Session-scoped keys (`terminalKey`) whose next unmount must dispose (not park) — set on permanent+ * deletion, where the unmount runs AFTER the session was already destroyed, so parking would keep a+ * dead xterm. */const noParkIds = new Set<string>()/** Canvas calls this when permanently deleting a terminal node: drops an already-parked entry- * AND makes the upcoming unmount (if the node is currently mounted) dispose instead of park. */-export function disposeTerminalOnUnmount(id: string): void {- noParkIds.add(id)- disposeParkedTerminal(id)- coStates.delete(id)- forgetNodeTermState(id)+ * AND makes the upcoming unmount (if the node is currently mounted) dispose instead of park. Takes+ * the node's `sessionId` (the session its tab is bound to) so the composite key matches the one the+ * mounted `TerminalNode` uses — a local node resolves to `'local'`, i.e. its historical bare-id+ * behavior. `forgetNodeTermState` stays node-id keyed: `fittedByNode`/`recycledIds` are transient+ * per-mount and only one node with a given id mounts at a time, so a cross-session collision there+ * is benign. */+export function disposeTerminalOnUnmount(sessionId: string, nodeId: string): void {+ const key = terminalKey(sessionId, nodeId)+ noParkIds.add(key)+ disposeParkedTerminal(key)+ coStates.delete(key)+ forgetNodeTermState(nodeId)}/**@@ -276,19 +285,19 @@ const coSubs = new Map<string, (s: CoState) => void>()*/const restartSubs = new Map<string, () => void>()-function getCo(id: string): CoState {- return coStates.get(id) ?? NO_CO+function getCo(key: string): CoState {+ return coStates.get(key) ?? NO_CO}-function setCo(id: string, patch: Partial<CoState>): void {- const prev = getCo(id)+function setCo(key: string, patch: Partial<CoState>): void {+ const prev = getCo(key)const next = { ...prev, ...patch }// A no-op write must stay a no-op: applyFit clears the letterbox on every fit, and handing the// node a fresh object each time would re-render it for nothing (and, solo, on every resize tick).if (next.letterbox === prev.letterbox && next.closed === prev.closed && next.ended === prev.ended)return- coStates.set(id, next)- coSubs.get(id)?.(next)+ coStates.set(key, next)+ coSubs.get(key)?.(next)}/**@@ -303,7 +312,14 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// This node's core api (a context read — stable for the session's lifetime, so using it// inside the once-mounted lifecycle effect is safe and never re-runs that effect). Core-bound// namespaces (pty, fs) go through it; app-global ones (clipboard, shell) stay on the global.- const { api } = useSession()+ const session = useSession()+ const { api } = session+ // Session-scope the module-global node-keyed maps (parkedTerminals / coStates / coSubs /+ // restartSubs / noParkIds): a relay tab adopts the host's project KEEPING node ids, so a local+ // node and a relay node can share a bare id. `session.id` is stable for this node's lifetime+ // ('local' for the local session, relay-N for a relay tab — both survive project switches), so+ // `termKey` is stable across a park→remount of the same terminal yet distinct across sessions.+ const termKey = terminalKey(session.id, id)// The transport is ALWAYS `LocalTransport` over THIS session's api — one protocol, no// RemoteTransport. For the local session `api.pty` is the preload; for a relay tab it is Task 5's// bridged pty (the relay tunnel), so LocalTransport over the bridged api IS the remote transport.@@ -417,14 +433,14 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// missing at spawn (Task 3 fallback) — flags the account chip with a warning tint + tooltip.const [accountFallback, setAccountFallback] = useState(false)// Co-attach state published by the (park-surviving) transport listeners — see CoState.- const [co, setCo_] = useState<CoState>(() => getCo(id))+ const [co, setCo_] = useState<CoState>(() => getCo(termKey))useEffect(() => {- coSubs.set(id, setCo_)- setCo_(getCo(id)) // catch up anything published while this instance was mounting+ coSubs.set(termKey, setCo_)+ setCo_(getCo(termKey)) // catch up anything published while this instance was mountingreturn () => {- if (coSubs.get(id) === setCo_) coSubs.delete(id)+ if (coSubs.get(termKey) === setCo_) coSubs.delete(termKey)}- }, [id])+ }, [termKey])// Publish this instance's restart trigger for the (park-surviving) onRecycled listener — see// restartSubs. Bumping `respawnNonce` re-runs the lifecycle effect below, which is exactly what// the mover's own canvas does; the transient nonce is never persisted.@@ -433,11 +449,11 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNupdateNodeData(id, (n) => ({respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1}))- restartSubs.set(id, restart)+ restartSubs.set(termKey, restart)return () => {- if (restartSubs.get(id) === restart) restartSubs.delete(id)+ if (restartSubs.get(termKey) === restart) restartSubs.delete(termKey)}- }, [id, updateNodeData])+ }, [termKey, id, updateNodeData])// The name of the peer who closed this node. Read NON-reactively (getState, not a selector): the// presence store is written at cursor rate and its perf contract reserves subscriptions for the// presence components — a per-terminal subscriber would run on every one of those writes. The@@ -447,7 +463,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// "Session ended" (a recycle whose replacement never came — see CoState.ended): the user asks for// a shell explicitly. Only now do we spawn, in THIS client's cwd — no silent stale-cwd respawn.const reopenEnded = (): void => {- setCo(id, { ended: false })+ setCo(termKey, { ended: false })updateNodeData(id, (n) => ({respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1}))@@ -516,9 +532,9 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// xterm + session are built. `myNonce` vs the render-updated ref tells the cleanup below// whether it runs for a respawn (worktree move — must NOT park) or a plain unmount.const myNonce = data.respawnNonce- const parked = parkedTerminals.get(id)+ const parked = parkedTerminals.get(termKey)if (parked) {- parkedTerminals.delete(id)+ parkedTerminals.delete(termKey)clearTimeout(parked.timer)}@@ -628,7 +644,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNsentCols = size.colssentRows = size.rowsresizeTerm(term, size.cols, size.rows)- setCo(id, { letterbox: false })+ setCo(termKey, { letterbox: false })// Before the session exists we are the only voice: the size rides the initial `create()`.if (sessionId) transport.resize(sessionId, size.cols, size.rows)} catch {@@ -759,7 +775,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// Prefetch the persisted scrollback in parallel with the spawn so it's ready to replay the// instant the session resolves (a cold restart after a reboot recreates the tmux session// empty — see the `fresh` handling below). Cheap no-op ('') when there's no snapshot.- const noSpawn = !!getCo(id).closed || getCo(id).ended+ const noSpawn = !!getCo(termKey).closed || getCo(termKey).endedconst scrollbackPromise =parked || noSpawn? Promise.resolve('')@@ -806,7 +822,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// was spawned — land in the same "closed by <name>" state a subscribed co-viewer gets.// BEFORE `onDisposed()`: there is no session here, so there is nothing to kill or unwire.if (closed) {- setCo(id, { closed })+ setCo(termKey, { closed })if (!disposed) term.write('\r\n\x1b[90m[session closed by another user]\x1b[0m\r\n')return}@@ -840,7 +856,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNresizeTerm(term, size.cols, size.rows)// Measured against the CURRENT mount's fit (the registry, not a closure): this// listener outlives the mount that wired it — see terminal-config's fittedByNode.- setCo(id, { letterbox: letterboxFor(id, size) })+ setCo(termKey, { letterbox: letterboxFor(id, size) })}))}@@ -849,7 +865,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNif (transport.onClosed) {cleanups.push(transport.onClosed(sid, ({ by }) => {- setCo(id, { closed: { by } })+ setCo(termKey, { closed: { by } })term.write('\r\n\x1b[90m[session closed by another user]\x1b[0m\r\n')}))@@ -865,14 +881,14 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNcleanups.push(transport.onRecycled(sid, (info) => {if (recycleAction(info) === 'ended') {- disposeParkedTerminal(id) // the park holds a dead pty either way- setCo(id, { ended: true })+ disposeParkedTerminal(termKey) // the park holds a dead pty either way+ setCo(termKey, { ended: true })term.write('\r\n\x1b[90m[session ended — reopen to restart]\x1b[0m\r\n')return}- const restart = restartSubs.get(id)+ const restart = restartSubs.get(termKey)if (!restart) {- disposeParkedTerminal(id) // unmounted: drop the park, the next mount creates fresh+ disposeParkedTerminal(termKey) // unmounted: drop the park, the next mount creates freshreturn}markRecycled(id)@@ -1147,8 +1163,8 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN// another client destroyed — or ended under us with no replacement — is gone: nothing to park// (and nothing left to keep alive).const isRespawn = respawnNonceRef.current !== myNonce- const co = getCo(id)- if (sessionId && !isRespawn && !co.closed && !co.ended && !noParkIds.delete(id)) {+ const co = getCo(termKey)+ if (sessionId && !isRespawn && !co.closed && !co.ended && !noParkIds.delete(termKey)) {// Park = "subscribed, but not viewing": report no size at all, so this window's (possibly// small) grid stops clamping every other subscriber's terminal for the next five minutes.// The subscription itself stays — output keeps streaming into the parked xterm — and the@@ -1164,14 +1180,14 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNcleanups,life,timer: setTimeout(() => {- if (parkedTerminals.get(id) === entry) {- parkedTerminals.delete(id)+ if (parkedTerminals.get(termKey) === entry) {+ parkedTerminals.delete(termKey)disposeParked(entry)}}, TERM_PARK_MS)}- disposeParkedTerminal(id) // defensive: never stack two entries for one node- parkedTerminals.set(id, entry)+ disposeParkedTerminal(termKey) // defensive: never stack two entries for one node+ parkedTerminals.set(termKey, entry)// A spawn continuation still awaiting its history seed reads this to know the session// survived this unmount (parked, or adopted by a remount) and must be finished, not killed.handedOff = entrydiff --git a/src/renderer/session/relay-tab.test.ts b/src/renderer/session/relay-tab.test.tsindex 11aa621c5..e23a07b6b 100644--- a/src/renderer/session/relay-tab.test.ts+++ b/src/renderer/session/relay-tab.test.ts@@ -153,6 +153,25 @@ describe('openRelayTab (connect → tab → mount)', () => {expect(sessionForProject('proj-1').source).toBe('relay')})+ it('GUARD: a POST-approval workspace.load() rejection disposes the just-created session (no leak)', async () => {+ // The load runs AFTER createSession + the two held teardowns, so a host that vanishes between+ // approval and load must dispose the session — else the SESSIONS entry, its presence+ // subscription (the peer lingers in host facepiles), and the relay socket all leak.+ const { api, presenceUnsub, workspaceLoad } = fakeBridgedApi()+ workspaceLoad.mockRejectedValue(new Error('host gone before load'))+ const close = vi.fn()+ const handle: RelayApiHandle = { api, ready: () => Promise.resolve(), close }+ const { deps } = makeDeps({ handle })++ await expect(openRelayTab('conn-load-fail', 'doomed', deps)).rejects.toThrow(/host gone/)++ // The session was disposed: presence teardown + relay socket close ran exactly once, and no+ // relay session lingers in the registry.+ expect(presenceUnsub).toHaveBeenCalledTimes(1)+ expect(close).toHaveBeenCalledTimes(1)+ expect(sessionCount()).toBe(1) // only the local session remains+ })+it('GUARD: a pre-approval socket drop REJECTS the bootstrap (never hangs) and closes the handle', async () => {const { api } = fakeBridgedApi()const close = vi.fn()diff --git a/src/renderer/session/relay-tab.ts b/src/renderer/session/relay-tab.tsindex 2a8a4f6c3..8968ed8b6 100644--- a/src/renderer/session/relay-tab.ts+++ b/src/renderer/session/relay-tab.ts@@ -88,20 +88,29 @@ export async function openRelayTab(// transport to the relay api; Stage-3 sync then keeps it live. If the host shared nothing (or the// project was deleted), OR we're reconnecting (no `adoptProject` dep — the existing tab is reused),// fall back to the labelled tab so it still opens rather than throwing.- const ws = await handle.api.workspace.load()- const hostProject = ws.projects[0]- const projectId =- hostProject && deps.adoptProject- ? deps.adoptProject({ ...hostProject, remote: true }).id- : deps.addProject(label).id- bindProjectToSession(projectId, session.id)- setActiveSession(session.id)- deps.setActiveProject(projectId)-- return {- sessionId: session.id,- projectId,- dispose: () => disposeSession(session.id),+ // The load runs AFTER createSession + the held teardowns, so a host that vanishes between approval+ // and load would leave the SESSIONS entry, its presence subscription (the peer lingers in host+ // facepiles) and the relay socket all leaking. Dispose the just-created session before rethrowing+ // — `disposeSession` runs the held teardowns exactly once (idempotent).+ try {+ const ws = await handle.api.workspace.load()+ const hostProject = ws.projects[0]+ const projectId =+ hostProject && deps.adoptProject+ ? deps.adoptProject({ ...hostProject, remote: true }).id+ : deps.addProject(label).id+ bindProjectToSession(projectId, session.id)+ setActiveSession(session.id)+ deps.setActiveProject(projectId)++ return {+ sessionId: session.id,+ projectId,+ dispose: () => disposeSession(session.id),+ }+ } catch (err) {+ disposeSession(session.id)+ throw err}}diff --git a/src/renderer/terminal/terminal-config.test.ts b/src/renderer/terminal/terminal-config.test.tsindex 5763b6119..59da4ac82 100644--- a/src/renderer/terminal/terminal-config.test.ts+++ b/src/renderer/terminal/terminal-config.test.ts@@ -18,6 +18,7 @@ import {shouldApplyResync,stripTrailingNewline,takeRecycled,+ terminalKey,terminalKeyAction,toXtermText,xtermScrollback,@@ -40,6 +41,23 @@ const ev = (p: Partial<CopyShortcutEvent>): CopyShortcutEvent => ({...p})+describe('terminalKey (session-scope the node-keyed renderer-global maps)', () => {+ it('same session + node → the same key (stable across a park→remount)', () => {+ expect(terminalKey('local', 'abc')).toBe(terminalKey('local', 'abc'))+ expect(terminalKey('relay-1', 'abc')).toBe(terminalKey('relay-1', 'abc'))+ })++ it('distinct sessions with the SAME node id → distinct keys (no local↔relay collision)', () => {+ // The core bug: a relay tab adopts the host's project KEEPING node ids, so a local node and a+ // relay node can share a bare id. Scoping by session id must keep them apart.+ expect(terminalKey('local', 'abc')).not.toBe(terminalKey('relay-1', 'abc'))+ })++ it('distinct node ids in the same session → distinct keys', () => {+ expect(terminalKey('local', 'abc')).not.toBe(terminalKey('local', 'def'))+ })+})+describe('reportedSize', () => {it('reports the fit proposal, floored at 1 (a collapsed node can propose 0)', () => {expect(reportedSize({ cols: 132, rows: 43 })).toEqual({ cols: 132, rows: 43 })diff --git a/src/renderer/terminal/terminal-config.ts b/src/renderer/terminal/terminal-config.tsindex 102640ab5..4af922599 100644--- a/src/renderer/terminal/terminal-config.ts+++ b/src/renderer/terminal/terminal-config.ts@@ -53,6 +53,22 @@ export function isLetterboxed(effective: TermSize, fitted: TermSize | null): boo* comparing the pty's size against the pre-park one, permanently letterboxing a terminal that* should fill its node (or un-letterboxing one that shouldn't).*/+/**+ * Session-scope a renderer-global map that is keyed by node id.+ *+ * `TerminalNode`'s `parkedTerminals` / `coStates` / `coSubs` / `restartSubs` (and `noParkIds`) are+ * module-global. A relay tab adopts the HOST's project KEEPING its node ids (they are tmux session+ * names — see `adoptProject`), so a local node and a relay node can share a bare id and collide:+ * switching from a local tab to a relay tab that share id `abc` would let the relay node adopt the+ * local node's parked pty. Keying by `${sessionId}:${nodeId}` keeps them apart. The session id is+ * stable while a tab is open — `'local'` for the local session, `relay-N` for a relay tab, both+ * surviving project switches — so the key is STABLE across a park→remount of the same logical+ * terminal yet DISTINCT across sessions.+ */+export function terminalKey(sessionId: string, nodeId: string): string {+ return `${sessionId}:${nodeId}`+}+const fittedByNode = new Map<string, TermSize>()/** Record what this client last REPORTED it can render (called from every applyFit). */