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(canvas): context links work for agents launched by hand in plain terminals

DIodide14aa9a977a46
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index a223f94e6..3a9ec678a 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -1713,13 +1713,18 @@ export function Canvas() {
[onNodesChange, markDirty]
)
- // Resolve a node's agent id, with a tags fallback for not-yet-migrated legacy nodes.
+ // Resolve a node's agent id, with a tags fallback for not-yet-migrated legacy nodes and a
+ // hook-status fallback for plain terminals where the user launched an agent CLI by hand:
+ // every local session carries the hook env (pty-manager defaults agentId to 'claude'), so
+ // the managed hooks report who's actually running inside even when data.agentId was never
+ // set at node creation.
const agentIdOf = useCallback((id: string): AgentId | undefined => {
const n = nodesRef.current.find((x) => x.id === id)
if (!n || n.type !== 'terminal') return undefined
return (
(n.data.agentId as AgentId | undefined) ??
- (((n.data.tags as string[]) ?? []).includes('claude') ? 'claude' : undefined)
+ (((n.data.tags as string[]) ?? []).includes('claude') ? 'claude' : undefined) ??
+ useAgentStatus.getState().byId[id]?.agentId
)
}, [])
@@ -1835,11 +1840,16 @@ export function Canvas() {
// Rewrite link files when a linked node's session starts/changes: main resolves
// codex/gemini transcripts by sessionId, so a session that appears after the edge was
- // drawn must trigger a rewrite. Primitive signature, not the byId map (see loopSig).
+ // drawn must trigger a rewrite. agentId is part of the signature for the same reason: a
+ // plain terminal's identity arrives from hooks after the fact, and the map entry gains
+ // its agentId/sessionId only once it's known. Primitive signature, not the byId map (see
+ // loopSig).
const linkSessionSig = useAgentStatus((s) => {
let sig = ''
for (const e of linkEdges) {
- sig += (s.byId[e.source]?.sessionId ?? '') + '|' + (s.byId[e.target]?.sessionId ?? '') + '|'
+ const a = s.byId[e.source]
+ const b = s.byId[e.target]
+ sig += `${a?.agentId ?? ''}:${a?.sessionId ?? ''}|${b?.agentId ?? ''}:${b?.sessionId ?? ''}|`
}
return sig
})
@@ -1873,7 +1883,12 @@ export function Canvas() {
// project's map would sever the links of background projects whose agents keep running.
const { projects, activeProjectId } = useProjects.getState()
const map = {
- ...buildBackgroundLinkMaps(projects, activeProjectId, (id) => useAgentStatus.getState().byId[id]?.sessionId),
+ ...buildBackgroundLinkMaps(
+ projects,
+ activeProjectId,
+ (id) => useAgentStatus.getState().byId[id]?.sessionId,
+ (id) => useAgentStatus.getState().byId[id]?.agentId
+ ),
...buildLinkMap(valid, infoOf)
}
const t = setTimeout(() => void window.nodeTerminal.contextLink.setLinks(map), 150)
diff --git a/src/renderer/lib/noteLink.test.ts b/src/renderer/lib/noteLink.test.ts
index f7b785469..3eda7a776 100644
--- a/src/renderer/lib/noteLink.test.ts
+++ b/src/renderer/lib/noteLink.test.ts
@@ -186,6 +186,32 @@ describe('buildBackgroundLinkMaps', () => {
sessionId: 'sess-b2'
})
})
+ it('falls back to the hook-reported agentId for plain terminals', () => {
+ // A hand-launched `claude` in a plain terminal: the serialized node has no agentId, but
+ // the status store (fed by the managed hooks) knows who's running inside.
+ const plainProjects = [
+ {
+ id: 'p-bg',
+ nodes: [node({ id: 'm1', title: 'Manual', cwd: '/m' }), node({ id: 'm2', title: 'Also', cwd: '/m' })],
+ bridges: [{ id: 'e', source: 'm1', target: 'm2' }]
+ }
+ ]
+ const map = buildBackgroundLinkMaps(
+ plainProjects,
+ null,
+ (id) => (id === 'm2' ? 'sess-m2' : undefined),
+ (id) => (id === 'm2' ? 'claude' : undefined)
+ )
+ expect(map['m1']).toContainEqual({
+ id: 'm2',
+ title: 'Also',
+ cwd: '/m',
+ agentId: 'claude',
+ sessionId: 'sess-m2'
+ })
+ // m2's entry for m1 stays a bare terminal (no agentId reported for m1).
+ expect(map['m2']).toContainEqual({ id: 'm1', title: 'Manual', cwd: '/m' })
+ })
it('drops edges whose endpoints are gone from the serialized nodes', () => {
const map = buildBackgroundLinkMaps(
[{ id: 'p', nodes: [node({ id: 'z1' })], bridges: [{ id: 'e', source: 'z1', target: 'gone' }] }],
diff --git a/src/renderer/lib/noteLink.ts b/src/renderer/lib/noteLink.ts
index 020b8162c..1638af92c 100644
--- a/src/renderer/lib/noteLink.ts
+++ b/src/renderer/lib/noteLink.ts
@@ -115,11 +115,17 @@ export function buildLinkMap(
* pushed map — pushing only the active project's map deleted the link files of background
* projects whose tmux sessions (and agents mid-task) were still running.
* Node ids are globally unique across projects, so the maps merge without collisions.
+ *
+ * `agentIdOf` is the hook-status fallback for plain terminals where the user launched an
+ * agent CLI by hand: the serialized node carries no agentId, but the status store (fed by
+ * the managed hooks, node ids are per-core so background projects share it) knows who's
+ * running inside.
*/
export function buildBackgroundLinkMaps(
projects: Array<{ id: string; nodes: CanvasNodeState[]; bridges?: BridgeLink[] }>,
activeProjectId: string | null,
- sessionIdOf: (nodeId: string) => string | undefined
+ sessionIdOf: (nodeId: string) => string | undefined,
+ agentIdOf?: (nodeId: string) => string | undefined
): ContextLinkMap {
const map: ContextLinkMap = {}
for (const p of projects) {
@@ -129,14 +135,15 @@ export function buildBackgroundLinkMaps(
const infoOf = (id: string): LinkNodeInfo => {
const n = byId.get(id)!
const sticky = n.kind === 'sticky'
+ const agentId = sticky ? undefined : (n.agentId ?? agentIdOf?.(id))
return {
id,
title: n.title || id,
cwd: n.cwd ?? '',
note: sticky ? (n.text ?? '') : undefined,
sticky,
- agentId: sticky ? undefined : n.agentId,
- sessionId: !sticky && n.agentId ? sessionIdOf(id) : undefined,
+ agentId,
+ sessionId: agentId ? sessionIdOf(id) : undefined,
accountId: sticky ? undefined : n.accountId
}
}
diff --git a/src/renderer/state/agentStatus.ts b/src/renderer/state/agentStatus.ts
index 784e5c88d..3021e41f3 100644
--- a/src/renderer/state/agentStatus.ts
+++ b/src/renderer/state/agentStatus.ts
@@ -5,8 +5,12 @@ import type { NodeTerminalApi } from '@shared/types'
/**
* Transient per-node status for agent (e.g. Claude Code) sessions, driven by the agent's hooks.
- * `unread`, `session` and `sessionId` are persisted to localStorage so they survive a
- * reload/restart; the live `state` (working/waiting/…) is not (it'd be stale on relaunch).
+ * `unread`, `session`, `sessionId` and `agentId` are persisted to localStorage so they survive
+ * a reload/restart; the live `state` (working/waiting/…) is not (it'd be stale on relaunch).
+ * `agentId` is durable because a PLAIN terminal's agent identity exists nowhere else: an
+ * explicit agent node re-derives it from `data.agentId`, but a hand-launched `claude` in a
+ * plain terminal is only known here, and its context links must keep classifying across
+ * restarts (tmux keeps the session — and the agent — alive through them).
*
* ONE STORE PER CORE (stage 4): `createAgentStatusSession(persistKey?)` builds an isolated
* instance — node ids are per-core, so status tables from two cores must never mix. The module
@@ -160,7 +164,7 @@ export function createAgentStatusSession(persistKey?: string): AgentStatusSessio
const data = JSON.parse(raw) as Record<string, Partial<AgentNodeStatus>>
const out: Record<string, AgentNodeStatus> = {}
for (const [id, v] of Object.entries(data)) {
- out[id] = { unread: !!v.unread, session: v.session, sessionId: v.sessionId }
+ out[id] = { unread: !!v.unread, session: v.session, sessionId: v.sessionId, agentId: v.agentId }
// A recurring job (cron/schedule — and tmux keeps in-session loops alive too) outlives
// the app: restore its card. Minimal shape check so a corrupt entry can't break load.
if (v.loop && typeof v.loop === 'object' && v.loop.kind) {
@@ -185,8 +189,14 @@ export function createAgentStatusSession(persistKey?: string): AgentStatusSessio
try {
const out: Record<string, Partial<AgentNodeStatus>> = {}
for (const [id, v] of Object.entries(byId)) {
- if (v.unread || v.session || v.sessionId || v.loop) {
- out[id] = { unread: v.unread, session: v.session, sessionId: v.sessionId, loop: v.loop }
+ if (v.unread || v.session || v.sessionId || v.loop || v.agentId) {
+ out[id] = {
+ unread: v.unread,
+ session: v.session,
+ sessionId: v.sessionId,
+ agentId: v.agentId,
+ loop: v.loop
+ }
}
}
localStorage.setItem(persistKey, JSON.stringify(out))