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.

feat(relay): Canvas presence connect/report follow the active session (collab Task 3)

enes088918a5220c
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 2ca2a9437..32ea4ec41 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -83,7 +83,7 @@ import { UsageIndicator } from '../components/UsageIndicator'
import { PresenceLayer } from '../components/PresenceLayer'
import { Facepile } from '../components/Facepile'
import { PresenceNamePrompt } from '../components/PresenceNamePrompt'
-import { connectPresence, reportProject, usePresence } from '../state/presence'
+import { usePresence } from '../state/presence'
import { nodeTravel, projectTravel } from '../lib/presenceTravel'
import { RemoteAccessDialog } from '../components/RemoteAccessDialog'
import { SshProjectDialog } from '../components/SshProjectDialog'
@@ -139,6 +139,7 @@ import {
useSession,
SessionProvider,
sessionForProject,
+ presenceForProject,
setActiveSession,
disposeSession,
} from '../session/session'
@@ -1009,8 +1010,10 @@ export function Canvas() {
// tab switch). Peers only draw each other's cursors and node chips when the project matches —
// each project is its own canvas with its own coordinate space. No project open (welcome
// screen) → null, which is exactly what the early returns below mean. reportProject dedups,
- // and Canvas deliberately never READS the presence store (see the connectPresence effect).
- reportProject(activeProjectId || null)
+ // and Canvas deliberately never READS the presence store (see the connect effect). Route to the
+ // ACTIVE session's presence (a relay tab tells its host, over the right core; a local tab hits
+ // `defaultPresence` — byte-identical to before), resolved by binding from the project we switched to.
+ presenceForProject(activeProjectId || '').reportProject(activeProjectId || null)
// Track the active session by the tab we switched to, so non-component api accessors
// (activeSessionApi() in scmDraft/worktrees) hit the active tab's core — the local session for
// a local tab, the relay session for a remote tab. Resolution is by binding (never persisted).
@@ -3890,12 +3893,19 @@ export function Canvas() {
useEffect(() => window.nodeTerminal.onFocusNode(focusNodeById), [focusNodeById])
- // Team presence: subscribe to the peer stream and announce ourselves ONCE per session ([] deps —
- // connectPresence is idempotent, but a second live connection whose teardown ran first would tear
- // the shared one down under the survivor). Canvas deliberately does NOT read the presence store:
- // only PresenceLayer / Facepile / PresenceChips subscribe, so a peer's 20 Hz cursor never
- // re-renders this component (docs/team-presence.md → UI).
- useEffect(() => connectPresence(), [])
+ // Team presence: subscribe to the peer stream and announce ourselves ONCE per session. Bound to
+ // the ACTIVE session's presence (a relay tab connects the relay core; a local tab hits
+ // `defaultPresence` — byte-identical to before), re-keyed on `presence` so a tab switch tears the
+ // old session's connection down and connects the new one. `presenceForProject` is a plain,
+ // allocation-free resolve of the memoized (WeakMap-per-core) PresenceSession from the project we
+ // switched to — NOT a reactive subscription to the peer table — so `presence` is a stable
+ // reference per session and the effect re-runs ONLY when the active session changes. connect() is
+ // idempotent, and a second live connection whose teardown ran first would tear the shared one down
+ // under the survivor. Canvas deliberately does NOT read the presence store: only PresenceLayer /
+ // Facepile / PresenceChips subscribe, so a peer's 20 Hz cursor never re-renders this component
+ // (docs/team-presence.md → UI, PERF CONTRACT).
+ const presence = presenceForProject(activeProjectId || '')
+ useEffect(() => presence.connect(), [presence])
// A browser guest's new-window (target=_blank / window.open) request → open another browser node
// (never a real popup; main denies the real one) roped below/right of the source. Reads the
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 01f679fe1..fc7b70622 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -54,7 +54,7 @@ import { ContextMeter } from '../components/ContextMeter'
import { isZoomModifierHeld } from '../lib/zoomModifier'
import { useSettings } from '../state/settings'
import { useAgentStatus, inferInterruptAfterSettle } from '../state/agentStatus'
-import { reportFocus, releaseFocus, usePresence } from '../state/presence'
+import { usePresence } from '../state/presence'
import type { ClientId } from '@shared/presence'
import { PresenceChips } from '../components/PresenceChips'
import { useAgentNodes } from '../state/agentNodes'
@@ -62,7 +62,7 @@ import { useProjects } from '../state/projects'
import { useSshConn } from '../state/sshConn'
import { useWorktrees } from '../state/worktrees'
import { isRemoteSessionNode } from '@shared/worktree'
-import { useSession } from '../session/session'
+import { useSession, useActiveSessionPresence } from '../session/session'
import { accountChipLabel, COLLAPSED_HEIGHT, NODE_COLORS, type CanvasNode } from '../state/workspace'
import { hasHooks, canRecur, canContextLink, hasUsage, canChat, canResume, canRename, resumeCommand, withPermissionMode, agentConfig, type AgentId } from '@shared/agents/config'
import { ensureActivePermissionMode } from '../state/permissionMode'
@@ -314,6 +314,11 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// namespaces (pty, fs) go through it; app-global ones (clipboard, shell) stay on the global.
const session = useSession()
const { api } = session
+ // The ACTIVE session's presence — where our focus/blur casts go. This node renders under Canvas's
+ // active-session provider, so a relay tab reports focus over the relay core and a local tab hits
+ // `defaultPresence` (byte-identical to before). Stable for the node's lifetime (a tab switch
+ // unmounts the node), so capturing it in the once-mounted lifecycle effect below is safe, like `api`.
+ const presence = useActiveSessionPresence()
// 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
@@ -1140,7 +1145,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// Teammates stop seeing us in this node's header. releaseFocus, not reportFocus(null): on a
// project switch every node unmounts, and an unconditional clear could undo the focus the
// node we just moved into already published.
- releaseFocus(id)
+ presence.releaseFocus(id)
// Unmount happens on a project switch (a detach — the tmux session keeps running) as
// well as on real deletion, and we can't tell them apart here. Don't wipe the node's
// persisted status (that would drop the sessionId the context meter looks up on remount,
@@ -1256,7 +1261,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
// "I am working in this node" — the same signal the agent-status active flag uses, i.e. the
// dwell has elapsed and the terminal actually took the keyboard (a mouse merely passing over
// never gets here). Deduped in the store, so re-entering the same node costs nothing.
- reportFocus(id)
+ presence.reportFocus(id)
}
dwellRef.current = setTimeout(enter, panHoverDelay)
}
@@ -1265,7 +1270,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
setArmed(true)
termRef.current?.blur()
useAgentStatus.getState().setActive(id, false)
- releaseFocus(id)
+ presence.releaseFocus(id)
}
// While armed, a mousedown might start a node drag — pause the dwell timer so the
// terminal doesn't grab focus mid-drag; restart it on release.
@@ -1318,7 +1323,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
term.focus()
term.paste(paths.join(' ') + ' ')
useAgentStatus.getState().setActive(id, true)
- reportFocus(id)
+ presence.reportFocus(id)
}
// A rename-capable agent's session name follows the node title: push `/rename <name>` into