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): mount a project's nodes under the session that owns them

review2e9684300569
diff --git a/CLAUDE.md b/CLAUDE.md
index 0bdcfd315..306453fad 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -540,6 +540,18 @@ project's nodes only.** The contract:
`liveCanvasHolds` (tag AND active id; otherwise the store) and queues a FUNCTIONAL update
(`rebaseOnLatest`). Consequence for readers: code pairing the tag with the RENDERED `nodes`
(a render-time publish, an effect keyed on `nodes`) reads `renderedProjectId`, never the ref.
+- **The node subtree's session follows the project whose nodes React Flow's STORE holds**
+ (`useFlowStoreOwner`, `canvas/flowStoreOwner.ts`), never `activeProjectId`. xyflow copies the
+ `nodes` prop into its store in a PASSIVE effect and `ReactFlowProvider` sits above the keyed
+ `SessionProvider`, so a key that flipped with the active id remounted the OUTGOING project's
+ nodes under the INCOMING session for a commit: a team tab's terminals were created on this
+ desktop's own core, and a local terminal on the relay host. Each array handed to
+ `<ReactFlow nodes>` is recorded against `renderedProjectId` and the store's array is looked up
+ (unknown ⇒ the last known owner); the same id feeds `OwningProjectContext`. A local → local
+ switch keeps the key `'local'` (no remount). A session-changing remount now lands one commit
+ AFTER the load applied the project's camera and builds a fresh pan/zoom, so `<ReactFlow>` gets
+ `defaultViewport={viewportRef.current}`, or the switch resets the camera to the origin.
+ `flowStoreOwner.test.tsx` replays the switches through real React Flow.
- Switching away unmounts the old project's `TerminalNode`s → their tmux clients detach but
the sessions keep running; switching back reattaches. tmux session names are per-node-id
(globally unique), so projects never collide.
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index e077f0137..f0fdbfd0a 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -799,6 +799,8 @@ import { snapNodeToGrid } from '../lib/nodeSizing'
import { snapResizeChanges } from '../lib/resizeSnap'
import { canClearDirty, canCommitCanvas, canCreateOnCanvas, liveCanvasHolds } from '../state/persistGuards'
import { mirrorLatest, rebaseOnLatest, useNodesEpoch } from './nodesEpoch'
+import { useFlowStoreOwner } from './flowStoreOwner'
+import { OwningProjectContext } from '../session/owning-project'
import { boardLiveNodeIds, createKanbanPublisher } from './kanban-sync'
import { applyToStoredCopy, createStoredCanvasPublisher } from './stored-publish'
import { isHidden } from '../lib/ui-visibility'
@@ -2879,6 +2881,11 @@ export function Canvas() {
// eslint-disable-next-line react-hooks/exhaustive-deps
[nodes, ephemeralNodes, keepAliveEntries]
)
+ // The project whose nodes React Flow's STORE holds, which a switch moves one commit after
+ // `nodes` (see flowStoreOwner.ts). The node subtree's session is keyed on it, never on the
+ // active id, so the outgoing project's nodes never remount under the incoming project's session.
+ const flowOwnerProjectId = useFlowStoreOwner(allNodes, renderedProjectId)
+ const nodeSession = sessionForProject(flowOwnerProjectId ?? '')
// Context-link edges, statically styled (no per-message activity in the pull model).
const accent = settings.accent
@@ -19630,15 +19637,25 @@ export function Canvas() {
</div>
</div>
)}
- {/* The active project's node subtree runs under ITS session (local for a local tab, the
- relay session for a remote tab). Keyed by session id so an api swap REMOUNTS the nodes
- (obligation 3): TerminalNode/EditorNode capture `api` in []-effects, so a live
- api change must remount them or they keep talking to the old core. For an all-local user
- the key is always 'local', so this never remounts — zero behavior change. */}
- <SessionProvider session={sessionForProject(activeProjectId || '')} key={sessionForProject(activeProjectId || '').id}>
+ {/* The node subtree runs under the session of the project whose nodes React Flow's STORE
+ holds (`flowOwnerProjectId`) — local for a local tab, the relay session for a remote
+ tab. Keyed by session id so an api swap REMOUNTS the nodes (obligation 3):
+ TerminalNode/EditorNode capture `api` in []-effects, so a live api change must remount
+ them or they keep talking to the old core. NOT keyed on `activeProjectId`: xyflow copies
+ the `nodes` prop into its store in a passive effect and `ReactFlowProvider` sits above
+ this provider, so a key that flipped with the active id remounted whatever the store
+ still held — the OUTGOING nodes — under the INCOMING session (a team tab's terminals
+ were created on this desktop's core). For an all-local user the key is always 'local',
+ so this never remounts. */}
+ <SessionProvider session={nodeSession} key={nodeSession.id}>
+ <OwningProjectContext.Provider value={flowOwnerProjectId}>
<ReactFlow
className={wallpaperBg ? 'has-wallpaper' : undefined}
nodes={allNodes}
+ // Read only at mount. A session-changing switch remounts this <ReactFlow> (key above) one
+ // commit AFTER the load effect applied the project's camera, and the fresh pan/zoom starts
+ // here — so hand it the live camera; xyflow's default put every such switch at the origin.
+ defaultViewport={viewportRef.current}
edges={displayEdges}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
@@ -19783,6 +19800,7 @@ export function Canvas() {
<StatusAwareMiniMap onNodeDoubleClick={goToNode} />
</MinimapDock>
</ReactFlow>
+ </OwningProjectContext.Provider>
</SessionProvider>
{/* MUST stay OUTSIDE <ReactFlow>. The library's wrapper carries inline
diff --git a/src/renderer/canvas/flowStoreOwner.test.tsx b/src/renderer/canvas/flowStoreOwner.test.tsx
new file mode 100644
index 000000000..2accf60f5
--- /dev/null
+++ b/src/renderer/canvas/flowStoreOwner.test.tsx
@@ -0,0 +1,516 @@
+// @vitest-environment jsdom
+//
+// Bug F, root cause B (report-F.md): during a project switch the OUTGOING project's nodes rendered
+// under the INCOMING project's session, so a hosted team tab's terminals were created on this
+// desktop's own core (and a local terminal on the relay HOST).
+//
+// Canvas keyed the node subtree's SessionProvider on `sessionForProject(activeProjectId)`, computed
+// at RENDER, while the nodes are swapped by the active-project load EFFECT. Worse, ReactFlowProvider
+// sits ABOVE that keyed provider and xyflow copies the `nodes` prop into its store in a PASSIVE
+// effect, so any key flip remounts whatever the store still holds — the outgoing nodes. The fix
+// keys the subtree on the project whose nodes the store holds (`useFlowStoreOwner`).
+//
+// The node component below is a PROBE, not TerminalNode (Canvas/TerminalNode have no render
+// harness): it applies TerminalNode's create rule (TerminalNode.tsx, the lifecycle effect's create
+// path — `dialsSsh`, the near-side refusal, `requireRemote: sshRemoteTmux`). `CanvasReplica` is
+// Canvas cut down to the lines that matter: the load effect (`setNodes` + `installEpoch` + the
+// saved viewport), the merged array handed to `<ReactFlow nodes>`, and the keyed provider — wired
+// with the REAL `useNodesEpoch` and the REAL `useFlowStoreOwner`. The source pin at the bottom fails
+// if Canvas stops wiring the provider the same way, so the replica cannot silently drift.
+import { act, useEffect, useMemo, useRef, useState } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import {
+ ReactFlow,
+ ReactFlowProvider,
+ useReactFlow,
+ useStoreApi,
+ type Node,
+ type NodeProps,
+ type Viewport
+} from '@xyflow/react'
+import { create } from 'zustand'
+import fs from 'fs'
+import path from 'path'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import type { CanvasNodeState, NodeTerminalApi, Project, PtyCreateOptions } from '@shared/types'
+import {
+ bindProjectToSession,
+ createSession,
+ resetSessionsForTest,
+ sessionForProject,
+ SessionProvider,
+ useSession
+} from '../session/session'
+import { OwningProjectContext, useOwningProjectId } from '../session/owning-project'
+import { sanitizeRelayProject } from '../session/relay-ssh'
+import { localizeAdoptedNode } from '../../shared/adopt-cwd'
+import { LocalTransport } from '../terminal/local-transport'
+import { nodeStatesToFlow, type CanvasNode } from '../state/workspace'
+import { useNodesEpoch } from './nodesEpoch'
+import { useFlowStoreOwner } from './flowStoreOwner'
+
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+// ── The field run's ids (2026-10-03) ─────────────────────────────────────────────────────────────
+const TEAM_ALPHA = 'project-musvmmle-8443e887' // the host's proj-alpha = the team tab's id
+const TEAM_BETA = 'project-host-beta' // a second tab of the SAME team (same relay session)
+const SSH_ALPHA = 'project-musuo41w-198f61e1' // the desktop's own (closed, handed-off) SSH proj-alpha
+const SSH_BETA = 'project-beta-ssh' // the new SSH project "Connect over SSH…" created
+const LOCAL_ONE = 'project-local-one'
+const LOCAL_TWO = 'project-local-two'
+const AGENT = 'term-musuoygd-979507b8'
+const PLAIN = 'term-musur58h-4ab48dbe'
+const HOST_HOME = '/home/e2e'
+
+const sshServer = { host: 'h.example', user: 'e2e', port: 22 }
+/** The desktop SSH project's nodes, as its mirror wrote them into the host's project.json. */
+const sshNodes = (): CanvasNodeState[] =>
+ [
+ { id: AGENT, kind: 'terminal', agentId: 'claude' },
+ { id: PLAIN, kind: 'terminal' }
+ ].map(
+ (n) =>
+ ({
+ ...n,
+ title: n.id,
+ color: '#888',
+ position: { x: 0, y: n.id === AGENT ? 0 : 400 },
+ size: { width: 600, height: 360 },
+ cwd: '~/proj-alpha',
+ ssh: sshServer,
+ sshRemoteTmux: true
+ }) as unknown as CanvasNodeState
+ )
+
+/** What the team tab holds: the host's adopted (localized) project, through the relay ingest. */
+function teamTabNodes(): CanvasNodeState[] {
+ return sanitizeRelayProject({
+ id: TEAM_ALPHA,
+ name: 'proj-alpha',
+ color: '#888',
+ cwd: `${HOST_HOME}/proj-alpha`,
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: sshNodes().map((n) => localizeAdoptedNode(n, HOST_HOME))
+ } as unknown as Project).nodes
+}
+
+function plainTerminal(id: string, y = 0): CanvasNodeState {
+ return {
+ id,
+ kind: 'terminal',
+ title: id,
+ color: '#888',
+ position: { x: 0, y },
+ size: { width: 600, height: 360 },
+ cwd: '/root/work'
+ } as unknown as CanvasNodeState
+}
+
+// ── Two cores ─────────────────────────────────────────────────────────────────────────────────────
+type Core = 'local' | 'host'
+type Create = PtyCreateOptions & { core: Core }
+let creates: Create[] = []
+let refusals: Array<{ core: Core; id: string }> = []
+let mounts: Array<{ session: string; owner: string | null; id: string; sshRemoteTmux: boolean }> = []
+function fakeApi(core: Core): NodeTerminalApi {
+ return {
+ core,
+ pty: {
+ create: vi.fn(async (o: PtyCreateOptions) => {
+ creates.push({ ...o, core })
+ return { sessionId: `${core}-${o.persistKey}`, fresh: true }
+ }),
+ kill: vi.fn()
+ }
+ } as unknown as NodeTerminalApi
+}
+
+// ── The probe: TerminalNode's create rule ─────────────────────────────────────────────────────────
+function ProbeTerminal({ id, data }: NodeProps<CanvasNode>) {
+ const session = useSession()
+ const owner = useOwningProjectId()
+ useEffect(() => {
+ mounts.push({ session: session.id, owner, id, sshRemoteTmux: !!data.sshRemoteTmux })
+ const ssh = data.ssh
+ const sshRemoteTmux = !!data.sshRemoteTmux
+ const localSsh = !!ssh && !sshRemoteTmux
+ const dialsSsh = sshRemoteTmux && session.source !== 'relay'
+ // `dialsSsh && ssh ? await resolveSshRemote(…) : undefined` — no master exists in this test.
+ const sshRemote = undefined
+ if (dialsSsh && !sshRemote) {
+ refusals.push({ core: session.source === 'local' ? 'local' : 'host', id })
+ return
+ }
+ void new LocalTransport(session.api).create({
+ cols: 80,
+ rows: 24,
+ shell: localSsh ? 'ssh' : (data.shell as string | undefined),
+ cwd: data.cwd as string | undefined,
+ persistKey: id,
+ agentId: data.agentId as string | undefined,
+ sshRemote,
+ requireRemote: sshRemoteTmux
+ })
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- mount-once, like the real lifecycle effect
+ }, [])
+ return <div style={{ width: 600, height: 360 }} />
+}
+const nodeTypes = { terminal: ProbeTerminal }
+
+// ── Canvas, cut down to the lines that matter ─────────────────────────────────────────────────────
+const useActive = create<{ id: string }>(() => ({ id: TEAM_ALPHA }))
+let stored: Record<string, CanvasNodeState[]> = {}
+let viewports: Record<string, Viewport> = {}
+/** Every key the node-subtree provider rendered with, in order. */
+let providerKeys: string[] = []
+let readViewport: (() => Viewport) | null = null
+function CanvasReplica() {
+ const activeProjectId = useActive((s) => s.id)
+ const [nodes, setNodes] = useState<CanvasNode[]>([])
+ const { renderedProjectId, installEpoch } = useNodesEpoch(nodes)
+ const { setViewport, getViewport } = useReactFlow()
+ readViewport = getViewport
+ const viewportRef = useRef<Viewport>({ x: 0, y: 0, zoom: 1 })
+ // Canvas's active-project load effect: `setNodes(flow)` + `installEpoch` + the saved viewport.
+ useEffect(() => {
+ // Canvas's bail-out: no active project (welcome screen) leaves the PREVIOUS nodes mounted.
+ if (!activeProjectId) {
+ installEpoch(null)
+ return
+ }
+ const flow = nodeStatesToFlow(stored[activeProjectId] ?? [])
+ setNodes(flow)
+ installEpoch(activeProjectId, flow)
+ const vp = viewports[activeProjectId]
+ if (vp) {
+ viewportRef.current = vp
+ void setViewport(vp)
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [activeProjectId])
+ // Canvas merges the ephemeral cards and the keep-alive pool into a NEW array per change.
+ const allNodes = useMemo(() => [...nodes], [nodes])
+ const flowOwnerProjectId = useFlowStoreOwner(allNodes, renderedProjectId)
+ const nodeSession = sessionForProject(flowOwnerProjectId ?? '')
+ providerKeys.push(nodeSession.id)
+ return (
+ <SessionProvider session={nodeSession} key={nodeSession.id}>
+ <OwningProjectContext.Provider value={flowOwnerProjectId}>
+ <div style={{ width: 1200, height: 900 }}>
+ <ReactFlow
+ nodes={allNodes}
+ edges={[]}
+ nodeTypes={nodeTypes}
+ defaultViewport={viewportRef.current}
+ onMove={(_e, vp) => {
+ viewportRef.current = vp
+ }}
+ />
+ </div>
+ </OwningProjectContext.Provider>
+ </SessionProvider>
+ )
+}
+
+let root: Root | null = null
+let host: HTMLDivElement | null = null
+async function mount(): Promise<void> {
+ host = document.createElement('div')
+ document.body.append(host)
+ root = createRoot(host)
+ await act(async () => {
+ root!.render(
+ <ReactFlowProvider>
+ <CanvasReplica />
+ </ReactFlowProvider>
+ )
+ })
+ await act(async () => {
+ await new Promise((r) => setTimeout(r, 0))
+ })
+}
+async function switchTo(projectId: string): Promise<void> {
+ // `openSshProject` / `switchProject` / `closeProject`: one store write of `activeProjectId`.
+ await act(async () => {
+ useActive.setState({ id: projectId })
+ })
+ await act(async () => {
+ await new Promise((r) => setTimeout(r, 0))
+ })
+}
+const teamIds = [AGENT, PLAIN]
+const localCreatesOfTeamNodes = (): Create[] =>
+ creates.filter((c) => c.core === 'local' && teamIds.includes(c.persistKey as string))
+const mountCount = (id: string): number => mounts.filter((m) => m.id === id).length
+/** Every node mounted under the session of the project the owning context names, and that project
+ * really holds the node. The invariant the whole fix exists for. */
+function expectMountsUnderTheirOwners(): void {
+ for (const m of mounts) {
+ expect(m.owner, `${m.id} mounted with no owning project`).not.toBeNull()
+ expect(sessionForProject(m.owner!).id, `${m.id} mounted under the wrong session`).toBe(m.session)
+ expect((stored[m.owner!] ?? []).map((n) => n.id)).toContain(m.id)
+ }
+}
+
+beforeEach(() => {
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ observe(): void {}
+ unobserve(): void {}
+ disconnect(): void {}
+ }
+ )
+ creates = []
+ refusals = []
+ mounts = []
+ providerKeys = []
+ stored = {}
+ viewports = {}
+ readViewport = null
+ resetSessionsForTest()
+ createSession('local', fakeApi('local'), 'This machine')
+ const relay = createSession('relay', fakeApi('host'), 'e2ehost')
+ bindProjectToSession(TEAM_ALPHA, relay.id)
+ bindProjectToSession(TEAM_BETA, relay.id)
+ useActive.setState({ id: TEAM_ALPHA })
+})
+afterEach(async () => {
+ await act(async () => root?.unmount())
+ root = null
+ host?.remove()
+ host = null
+ vi.unstubAllGlobals()
+})
+
+describe('why the team tab is exposed: it receives plain terminals (the host localized them)', () => {
+ it('localizeAdoptedNode drops sshRemoteTmux, so nothing on the node itself keeps it off this core', () => {
+ for (const n of teamTabNodes()) {
+ expect(n.sshRemoteTmux).toBeUndefined()
+ expect(n.ssh).toBeUndefined()
+ expect(n.cwd).toBe(`${HOST_HOME}/proj-alpha`)
+ }
+ })
+})
+
+describe('the node subtree runs under the session of the project React Flow STORE holds', () => {
+ it('(a) team tab → new SSH project: no team node id is created on the local core', async () => {
+ stored = { [TEAM_ALPHA]: teamTabNodes(), [SSH_BETA]: [] }
+ await mount()
+ // While the team tab is active, its terminals are created on the HOST core only.
+ expect(creates.map((c) => `${c.core}:${c.persistKey}`).sort()).toEqual([`host:${AGENT}`, `host:${PLAIN}`])
+ creates = []
+
+ await switchTo(SSH_BETA)
+
+ // Field: two LOCAL tmux sessions nt-term-musuoygd-979507b8 / nt-term-musur58h-4ab48dbe in the
+ // local $HOME, `-e NODETERM_AGENT_ID=claude`, created by the outgoing nodes' remount.
+ expect(localCreatesOfTeamNodes()).toEqual([])
+ expect(mountCount(AGENT)).toBe(1)
+ expect(mountCount(PLAIN)).toBe(1)
+ expectMountsUnderTheirOwners()
+ })
+
+ it('(b) team tab → "Open here anyway" SSH project (same ids): no local create without requireRemote, both refuse near-side', async () => {
+ // Same node ids in both projects. Keyed on the ACTIVE id, the window mounted the TEAM nodes
+ // under 'local'; the load then swapped the SSH project's data into those same instances, whose
+ // lifecycle effect never re-ran with `requireRemote`.
+ stored = { [TEAM_ALPHA]: teamTabNodes(), [SSH_ALPHA]: sshNodes() }
+ await mount()
+ creates = []
+ refusals = []
+
+ await switchTo(SSH_ALPHA)
+
+ expect(creates.filter((c) => c.core === 'local' && c.requireRemote !== true)).toEqual([])
+ // The SSH nodes mount FRESH under the local session and stop at the near-side guard (no master).
+ expect(refusals.filter((r) => r.core === 'local').map((r) => r.id).sort()).toEqual([AGENT, PLAIN].sort())
+ const localMounts = mounts.filter((m) => m.session === 'local')
+ expect(localMounts.map((m) => `${m.id}:${m.sshRemoteTmux}`).sort()).toEqual([`${AGENT}:true`, `${PLAIN}:true`].sort())
+ expectMountsUnderTheirOwners()
+ })
+
+ it('(c) local project → team tab: the local node is never created on the host', async () => {
+ stored = { [LOCAL_ONE]: [plainTerminal('term-local-1')], [TEAM_ALPHA]: [] }
+ useActive.setState({ id: LOCAL_ONE })
+ await mount()
+ expect(creates.map((c) => `${c.core}:${c.persistKey}`)).toEqual(['local:term-local-1'])
+ creates = []
+
+ await switchTo(TEAM_ALPHA)
+
+ expect(creates.filter((c) => c.core === 'host' && c.persistKey === 'term-local-1')).toEqual([])
+ expect(mountCount('term-local-1')).toBe(1)
+ expectMountsUnderTheirOwners()
+ })
+
+ it('(d) local → local: the key stays "local" and a node present before the switch is never remounted', async () => {
+ // Two local projects holding the same node id (a second worktree of one committed canvas):
+ // its terminal must survive the switch in place — parked terminals and the webview keep-alive
+ // pool rely on a local switch never remounting the subtree.
+ stored = {
+ [LOCAL_ONE]: [plainTerminal('term-shared'), plainTerminal('term-one', 400)],
+ [LOCAL_TWO]: [plainTerminal('term-shared'), plainTerminal('term-two', 400)]
+ }
+ useActive.setState({ id: LOCAL_ONE })
+ await mount()
+ expect(mountCount('term-shared')).toBe(1)
+ expect(mountCount('term-one')).toBe(1)
+
+ await switchTo(LOCAL_TWO)
+
+ expect(mountCount('term-shared')).toBe(1) // not remounted
+ expect(mountCount('term-one')).toBe(1) // unmounted, never remounted
+ expect(mountCount('term-two')).toBe(1)
+ expect(new Set(providerKeys)).toEqual(new Set(['local']))
+ expect(creates.filter((c) => c.persistKey === 'term-shared')).toHaveLength(1)
+ expect(creates.every((c) => c.core === 'local')).toBe(true)
+ })
+
+ it('(e) team → team (same session): no local create and no remount', async () => {
+ stored = { [TEAM_ALPHA]: teamTabNodes(), [TEAM_BETA]: [plainTerminal('term-beta-1')] }
+ await mount()
+ const relayKey = sessionForProject(TEAM_ALPHA).id
+ providerKeys = []
+
+ await switchTo(TEAM_BETA)
+
+ expect(creates.filter((c) => c.core === 'local')).toEqual([])
+ expect(mountCount(AGENT)).toBe(1)
+ expect(mountCount(PLAIN)).toBe(1)
+ expect(mountCount('term-beta-1')).toBe(1)
+ expect(new Set(providerKeys)).toEqual(new Set([relayKey]))
+ expectMountsUnderTheirOwners()
+ })
+
+ it('a session-changing switch keeps the incoming project\'s saved camera (the remount comes after the load)', async () => {
+ // The key now flips one commit AFTER the load effect applied the viewport, and a remounted
+ // <ReactFlow> builds a fresh pan/zoom from `defaultViewport` — so Canvas hands it the live
+ // camera, or every local ↔ team switch would land at the origin.
+ stored = { [LOCAL_ONE]: [plainTerminal('term-local-1')], [TEAM_ALPHA]: teamTabNodes() }
+ viewports = { [LOCAL_ONE]: { x: 120, y: -40, zoom: 0.75 }, [TEAM_ALPHA]: { x: -300, y: 25, zoom: 1.5 } }
+ useActive.setState({ id: LOCAL_ONE })
+ await mount()
+ expect(readViewport!()).toEqual(viewports[LOCAL_ONE])
+
+ await switchTo(TEAM_ALPHA)
+ expect(providerKeys.at(-1)).toBe(sessionForProject(TEAM_ALPHA).id)
+ expect(readViewport!()).toEqual(viewports[TEAM_ALPHA])
+
+ await switchTo(LOCAL_ONE)
+ expect(providerKeys.at(-1)).toBe('local')
+ expect(readViewport!()).toEqual(viewports[LOCAL_ONE])
+ })
+})
+
+// ── The hook alone ────────────────────────────────────────────────────────────────────────────────
+describe('useFlowStoreOwner', () => {
+ let renders = 0
+ let owner: string | null | undefined
+ let storeApi: ReturnType<typeof useStoreApi> | null = null
+ function Harness({ handed, projectId }: { handed: Node[]; projectId: string | null }) {
+ renders++
+ storeApi = useStoreApi()
+ owner = useFlowStoreOwner(handed, projectId)
+ return null
+ }
+ async function render(handed: Node[], projectId: string | null): Promise<void> {
+ await act(async () => {
+ root!.render(
+ <ReactFlowProvider>
+ <Harness handed={handed} projectId={projectId} />
+ </ReactFlowProvider>
+ )
+ })
+ }
+ /** What xyflow's StoreUpdater does with the `nodes` prop, in its passive effect. */
+ async function storeTakes(arr: Node[]): Promise<void> {
+ await act(async () => {
+ storeApi!.getState().setNodes(arr)
+ })
+ }
+ beforeEach(() => {
+ renders = 0
+ owner = undefined
+ host = document.createElement('div')
+ document.body.append(host)
+ root = createRoot(host)
+ })
+
+ it('names the project whose array the store holds, not the one handed this render', async () => {
+ const a: Node[] = []
+ await render(a, null)
+ expect(owner).toBeNull() // nothing known yet
+ await render(a, 'project-a')
+ expect(owner).toBeNull() // recorded, but the store still holds xyflow's own initial array
+ await storeTakes(a)
+ expect(owner).toBe('project-a')
+ const b: Node[] = []
+ await render(b, 'project-b')
+ expect(owner).toBe('project-a') // handed, not yet copied into the store
+ await storeTakes(b)
+ expect(owner).toBe('project-b')
+ })
+
+ it('keeps the last known owner while the store holds an array nobody handed (a reset)', async () => {
+ const a: Node[] = []
+ await render(a, 'project-a')
+ await storeTakes(a)
+ expect(owner).toBe('project-a')
+ await storeTakes([]) // e.g. a remounted <ReactFlow>'s store reset
+ expect(owner).toBe('project-a')
+ })
+
+ it('does not record an array handed while no project is mounted (the welcome-screen bail-out)', async () => {
+ const a: Node[] = []
+ await render(a, 'project-a')
+ await storeTakes(a)
+ const orphan: Node[] = []
+ await render(orphan, null)
+ await storeTakes(orphan)
+ expect(owner).toBe('project-a')
+ })
+
+ it('a store update that keeps the same owner does not re-render (one primitive per drag frame)', async () => {
+ const a1: Node[] = []
+ const a2: Node[] = []
+ await render(a1, 'project-a')
+ await render(a2, 'project-a')
+ await storeTakes(a1)
+ const before = renders
+ await storeTakes(a2) // a drag frame: a new array, same project
+ expect(renders).toBe(before)
+ expect(owner).toBe('project-a')
+ })
+})
+
+// ── Source pin: the replica is only as good as this ───────────────────────────────────────────────
+describe('source pin (the replica mirrors Canvas)', () => {
+ const CANVAS = fs.readFileSync(path.join(__dirname, 'Canvas.tsx'), 'utf8').replace(/\r\n/g, '\n')
+
+ it('Canvas records the exact array it hands <ReactFlow nodes> and keys the node subtree on its owner', () => {
+ const memo = CANVAS.indexOf('const allNodes = useMemo(')
+ const hook = CANVAS.indexOf('const flowOwnerProjectId = useFlowStoreOwner(allNodes, renderedProjectId)')
+ expect(memo).toBeGreaterThan(0)
+ expect(hook).toBeGreaterThan(memo)
+ expect(CANVAS).toContain("const nodeSession = sessionForProject(flowOwnerProjectId ?? '')")
+ const provider = CANVAS.indexOf('<SessionProvider session={nodeSession} key={nodeSession.id}>')
+ const owning = CANVAS.indexOf('<OwningProjectContext.Provider value={flowOwnerProjectId}>')
+ const flow = CANVAS.indexOf('<ReactFlow\n')
+ expect(provider).toBeGreaterThan(0)
+ expect(owning).toBeGreaterThan(provider)
+ expect(flow).toBeGreaterThan(owning)
+ // The props of THIS <ReactFlow> run up to its edges prop.
+ const props = CANVAS.slice(flow, CANVAS.indexOf('edges={displayEdges}', flow))
+ expect(props).toContain('nodes={allNodes}')
+ expect(props).toContain('defaultViewport={viewportRef.current}')
+ // Not on the active id: that flip is what remounted the outgoing nodes under the incoming session.
+ expect(CANVAS).not.toMatch(/<SessionProvider session=\{sessionForProject\(activeProjectId/)
+ expect(CANVAS).not.toMatch(/key=\{sessionForProject\(activeProjectId/)
+ })
+
+ it('Canvas loads nodes in an effect (the window the keying has to survive)', () => {
+ expect(CANVAS).toMatch(/nodeStatesToFlow\(project\.nodes\)[\s\S]{0,200}\n {4}setNodes\(flow\)/)
+ expect(CANVAS).toContain('installEpoch(project.id, flow)')
+ })
+})
diff --git a/src/renderer/canvas/flowStoreOwner.ts b/src/renderer/canvas/flowStoreOwner.ts
new file mode 100644
index 000000000..d3a5241a9
--- /dev/null
+++ b/src/renderer/canvas/flowStoreOwner.ts
@@ -0,0 +1,34 @@
+import { useRef } from 'react'
+import { useStore } from '@xyflow/react'
+
+/**
+ * The project whose nodes React Flow's STORE holds right now — which is not always the project
+ * whose nodes Canvas just handed to `<ReactFlow nodes>`, and not the active project either.
+ *
+ * xyflow copies the `nodes` prop into its store in a PASSIVE effect (StoreUpdater), and the store
+ * lives in `ReactFlowProvider`, which sits ABOVE anything Canvas keys. So for a commit after a
+ * project switch the store still holds the OUTGOING project's nodes, and whatever remounts in that
+ * commit renders THEM. Keying the node subtree's session on the active id — or on the array Canvas
+ * handed over (`renderedProjectId`), measured to leak the same way — remounted the outgoing nodes
+ * under the incoming project's session: a team tab's terminals were created on this desktop's own
+ * core, and a local terminal on the relay host (report-F, root cause B).
+ *
+ * Every array Canvas hands over is recorded against the project it belongs to (`renderedProjectId`,
+ * the project THIS render's nodes are) in a per-hook WeakMap, and the store's array is looked up in
+ * it. An array the map does not know — xyflow's own initial/reset array, or one handed while no
+ * project was mounted (`renderedProjectId` null, the welcome-screen bail-out, whose array is still
+ * the previous project's nodes) — answers the last known owner instead, initially `null`.
+ *
+ * The selector returns a primitive, so a drag frame (a new array, same project) re-renders nothing.
+ * Must be called inside a `ReactFlowProvider`, with the EXACT array passed to `<ReactFlow nodes>`.
+ */
+export function useFlowStoreOwner(handed: readonly unknown[], renderedProjectId: string | null): string | null {
+ const ownersRef = useRef<WeakMap<object, string> | null>(null)
+ if (ownersRef.current === null) ownersRef.current = new WeakMap()
+ const owners = ownersRef.current
+ if (renderedProjectId !== null) owners.set(handed, renderedProjectId)
+ const held = useStore((s) => owners.get(s.nodes))
+ const lastKnownRef = useRef<string | null>(null)
+ if (held !== undefined) lastKnownRef.current = held
+ return lastKnownRef.current
+}
diff --git a/src/renderer/session/owning-project.ts b/src/renderer/session/owning-project.ts
new file mode 100644
index 000000000..a5f41489a
--- /dev/null
+++ b/src/renderer/session/owning-project.ts
@@ -0,0 +1,27 @@
+import { createContext, useContext } from 'react'
+import type { SessionSource } from './session'
+
+/**
+ * The project whose nodes the surrounding subtree renders — set by Canvas around `<ReactFlow>`
+ * (to the project React Flow's store holds, `useFlowStoreOwner`) and by a card modal around its
+ * terminal. `null` = not known: outside any provider, or before React Flow has held a project.
+ *
+ * It exists beside the SessionProvider because a session alone cannot say whether a node is in
+ * the right place: a remote project with no live relay binding resolves to the LOCAL session, so a
+ * terminal that asked only for its session would happily create its host-side id on this core.
+ */
+export const OwningProjectContext = createContext<string | null>(null)
+
+export function useOwningProjectId(): string | null {
+ return useContext(OwningProjectContext)
+}
+
+/**
+ * True when a spawn for a node of `project` would land on the wrong core: the project lives on
+ * another machine (`remote`, a hosted team / relay tab) but the session the node runs under is not
+ * a relay session — it is this desktop's own core. The caller must then start nothing.
+ * An unknown project (undefined) is not evidence of anything and answers false.
+ */
+export function spawnBelongsElsewhere(project: { remote?: boolean } | undefined, source: SessionSource): boolean {
+ return !!project?.remote && source !== 'relay'
+}