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): frame a just-loaded project only once React Flow holds it

reviewff4fb28121ea
diff --git a/CLAUDE.md b/CLAUDE.md
index 306453fad..8d6c7ff70 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -550,8 +550,14 @@ project's nodes only.** The contract:
(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.
+ `defaultViewport={viewportRef.current}`, or the switch resets the camera to the origin. For the
+ same reason the load's settle timer must not frame or read the incoming nodes itself: the
+ pending focus / "Open recent" resume / `nodeterm://` link it finishes run through
+ `useFlowLanding` (`canvas/flowLanding.ts`) once the owner IS that project — React may run the
+ store copy's passive effect after a due timer, which then framed the pan/zoom the remount
+ destroys and read the outgoing copy of a shared node id. The rule holds for a local → local
+ switch too (no remount, but the owner still flips). `flowStoreOwner.test.tsx` replays the
+ switches through real React Flow, the timer firing inside that window included.
- 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 f0fdbfd0a..38fef55f9 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -800,6 +800,7 @@ import { snapResizeChanges } from '../lib/resizeSnap'
import { canClearDirty, canCommitCanvas, canCreateOnCanvas, liveCanvasHolds } from '../state/persistGuards'
import { mirrorLatest, rebaseOnLatest, useNodesEpoch } from './nodesEpoch'
import { useFlowStoreOwner } from './flowStoreOwner'
+import { useFlowLanding } from './flowLanding'
import { OwningProjectContext } from '../session/owning-project'
import { boardLiveNodeIds, createKanbanPublisher } from './kanban-sync'
import { applyToStoredCopy, createStoredCanvasPublisher } from './stored-publish'
@@ -2886,6 +2887,8 @@ export function Canvas() {
// 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 ?? '')
+ // Work that frames a just-loaded project waits for that same ownership (see flowLanding.ts).
+ const flowLanding = useFlowLanding(flowOwnerProjectId)
// Context-link edges, statically styled (no per-message activity in the pull model).
const accent = settings.accent
@@ -3441,41 +3444,49 @@ export function Canvas() {
} else {
setResumeProject(null)
}
- // Finish an "Open recent" resume that had to land on this project first.
- const pendingResume = pendingResumeRef.current
- if (pendingResume && pendingResume.projectId === project.id) {
- pendingResumeRef.current = null
- resumeCreateRef.current(pendingResume.conv)
- }
- // Finish a `nodeterm://open` link that had to land on this project first.
- const pendingLink = pendingLinkRef.current
- if (pendingLink && pendingLink.projectId === project.id) {
- pendingLinkRef.current = null
- if (Date.now() - pendingLink.at < PENDING_LINK_MAX_MS) linkCreateRef.current(pendingLink.open)
- }
- // Consume a cross-project focus request (notification click on a background node).
- const pending = pendingFocusRef.current
- if (pending) {
- pendingFocusRef.current = null
- const node = nodesRef.current.find((n) => n.id === pending)
- if (node) {
- // Same rule as focusNodeById: if the project we just landed on shows the BOARD, the
- // node lives on a card, not on the canvas hidden under it.
- if (isGlobalKanbanOpen() || isKanbanOpen(useProjects.getState().activeProjectId)) {
- useViewMode.getState().requestCard(pending)
- } else {
- setNodes((ns) => ns.map((n) => ({ ...n, selected: n.id === pending })))
- goToNode(node)
- // Same as focusNodeById: after the cross-project switch lands, hand the keyboard to the
- // target terminal so the user can type without a second click.
- useTerminalFocus.getState().request(pending)
+ // The rest frames the new project or reads its nodes off React Flow's store, so it waits
+ // until the store holds THIS project's nodes (flowLanding.ts): a session-changing switch
+ // remounts <ReactFlow> after that copy, and this timer can fire before it.
+ flowLanding.land(project.id, () => {
+ // Finish an "Open recent" resume that had to land on this project first.
+ const pendingResume = pendingResumeRef.current
+ if (pendingResume && pendingResume.projectId === project.id) {
+ pendingResumeRef.current = null
+ resumeCreateRef.current(pendingResume.conv)
+ }
+ // Finish a `nodeterm://open` link that had to land on this project first.
+ const pendingLink = pendingLinkRef.current
+ if (pendingLink && pendingLink.projectId === project.id) {
+ pendingLinkRef.current = null
+ if (Date.now() - pendingLink.at < PENDING_LINK_MAX_MS) linkCreateRef.current(pendingLink.open)
+ }
+ // Consume a cross-project focus request (notification click on a background node).
+ const pending = pendingFocusRef.current
+ if (pending) {
+ pendingFocusRef.current = null
+ const node = nodesRef.current.find((n) => n.id === pending)
+ if (node) {
+ // Same rule as focusNodeById: if the project we just landed on shows the BOARD, the
+ // node lives on a card, not on the canvas hidden under it.
+ if (isGlobalKanbanOpen() || isKanbanOpen(useProjects.getState().activeProjectId)) {
+ useViewMode.getState().requestCard(pending)
+ } else {
+ setNodes((ns) => ns.map((n) => ({ ...n, selected: n.id === pending })))
+ goToNode(node)
+ // Same as focusNodeById: after the cross-project switch lands, hand the keyboard to
+ // the target terminal so the user can type without a second click.
+ useTerminalFocus.getState().request(pending)
+ }
+ useAgentStatus.getState().setActive(pending, true)
+ useAgentStatus.getState().clearUnread(pending)
}
- useAgentStatus.getState().setActive(pending, true)
- useAgentStatus.getState().clearUnread(pending)
}
- }
+ })
}, 0)
- return () => clearTimeout(t)
+ return () => {
+ clearTimeout(t)
+ flowLanding.cancel()
+ }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeProjectId, reloadNonce, setNodes, setViewport, installEpoch])
diff --git a/src/renderer/canvas/flowLanding.ts b/src/renderer/canvas/flowLanding.ts
new file mode 100644
index 000000000..cb0034d1b
--- /dev/null
+++ b/src/renderer/canvas/flowLanding.ts
@@ -0,0 +1,54 @@
+import { useCallback, useEffect, useMemo, useRef } from 'react'
+
+/**
+ * Runs work that frames a just-loaded project, or reads its nodes off React Flow's store, only once
+ * that store holds the project's nodes (`flowOwnerProjectId`, see flowStoreOwner.ts).
+ *
+ * The load effect's settle timer used to run that work directly. A session-changing switch
+ * remounts `<ReactFlow>` one commit AFTER the store copies the incoming nodes, and that copy runs
+ * in a passive effect React may defer to a later task — so a due timer could fire first. The work
+ * then framed the OUTGOING pan/zoom instance, which the remount destroys (the new one starts from
+ * the saved camera), and `getInternalNode` answered from the outgoing project's copy of a shared
+ * node id. A local → local switch has no remount, but its owner still flips, so the same rule holds.
+ *
+ * `land(projectId, run)` queues `run` and runs it at once if the store already holds that project
+ * (an in-place reload, or a timer that fired after the copy); otherwise the owner effect runs it
+ * when ownership arrives. One item at a time: a later `land` replaces it, and `cancel` drops it —
+ * the load effect's cleanup calls it, so a project the user moved on from never runs its work late.
+ *
+ * The owner is read from a ref set in a PASSIVE effect, never at render time: Canvas's own effects
+ * run after its children's, so by then the remounted `<ReactFlow>` has handed its store the nodes
+ * and its new pan/zoom instance.
+ */
+export function useFlowLanding(flowOwnerProjectId: string | null): {
+ land: (projectId: string, run: () => void) => void
+ cancel: () => void
+} {
+ const committedOwnerRef = useRef<string | null>(null)
+ const queuedRef = useRef<{ projectId: string; run: () => void } | null>(null)
+
+ const flush = useCallback(() => {
+ const queued = queuedRef.current
+ if (!queued || queued.projectId !== committedOwnerRef.current) return
+ queuedRef.current = null
+ queued.run()
+ }, [])
+
+ useEffect(() => {
+ committedOwnerRef.current = flowOwnerProjectId
+ flush()
+ }, [flowOwnerProjectId, flush])
+
+ const land = useCallback(
+ (projectId: string, run: () => void) => {
+ queuedRef.current = { projectId, run }
+ flush()
+ },
+ [flush]
+ )
+ const cancel = useCallback(() => {
+ queuedRef.current = null
+ }, [])
+
+ return useMemo(() => ({ land, cancel }), [land, cancel])
+}
diff --git a/src/renderer/canvas/flowStoreOwner.test.tsx b/src/renderer/canvas/flowStoreOwner.test.tsx
index 2accf60f5..6aa97c0f0 100644
--- a/src/renderer/canvas/flowStoreOwner.test.tsx
+++ b/src/renderer/canvas/flowStoreOwner.test.tsx
@@ -14,10 +14,10 @@
// 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
+// saved viewport + the settle timer's framing), the merged array handed to `<ReactFlow nodes>`, and
+// the keyed provider — wired with the REAL `useNodesEpoch`, `useFlowStoreOwner` and `useFlowLanding`. 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 { act, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import {
ReactFlow,
@@ -48,6 +48,7 @@ import { LocalTransport } from '../terminal/local-transport'
import { nodeStatesToFlow, type CanvasNode } from '../state/workspace'
import { useNodesEpoch } from './nodesEpoch'
import { useFlowStoreOwner } from './flowStoreOwner'
+import { useFlowLanding } from './flowLanding'
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
@@ -164,14 +165,44 @@ let viewports: Record<string, Viewport> = {}
/** Every key the node-subtree provider rendered with, in order. */
let providerKeys: string[] = []
let readViewport: (() => Viewport) | null = null
+let readStore: ReturnType<typeof useStoreApi> | null = null
+// ── The load's settle timer and the cross-project focus it consumes (Canvas's pendingFocusRef) ──
+/** A node to frame once its project lands, as a notification click / sidebar jump stages it. */
+let pendingFocus: string | null = null
+/** false = 2e9684300's ordering: the settle timer frames at once instead of waiting for the store. */
+let gateOnOwner = true
+/** How the load schedules its settle callback; Canvas: `setTimeout(…, 0)`. Returns the cancel. */
+let scheduleSettle: (fn: () => void) => () => void = (fn) => {
+ const t = setTimeout(fn, 0)
+ return () => clearTimeout(t)
+}
+/** A settle callback held back by a test, fired by `settleAtCommitOf` below. */
+let heldSettle: (() => void) | null = null
+/** When set, the held settle fires in the COMMIT of the render that hands this project's nodes —
+ * after that render, before its passive effects copy them into the store. That is the window a due
+ * timer hits when React runs those passive effects in a later task (the field ordering). */
+let settleAtCommitOf: string | null = null
+/** Every landing that ran, by project. */
+let landings: string[] = []
+/** What each framing saw: the target's y in React Flow's store, and the pan/zoom it drove. */
+let framings: Array<{ id: string; y: number | null; panZoom: unknown }> = []
function CanvasReplica() {
const activeProjectId = useActive((s) => s.id)
const [nodes, setNodes] = useState<CanvasNode[]>([])
const { renderedProjectId, installEpoch } = useNodesEpoch(nodes)
- const { setViewport, getViewport } = useReactFlow()
+ const { setViewport, getViewport, getInternalNode } = useReactFlow()
+ const store = useStoreApi()
readViewport = getViewport
+ readStore = store
const viewportRef = useRef<Viewport>({ x: 0, y: 0, zoom: 1 })
- // Canvas's active-project load effect: `setNodes(flow)` + `installEpoch` + the saved viewport.
+ // 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 ?? '')
+ const flowLanding = useFlowLanding(flowOwnerProjectId)
+ providerKeys.push(nodeSession.id)
+ // Canvas's active-project load effect: `setNodes(flow)` + `installEpoch` + the saved viewport,
+ // then the settle timer whose framing work waits for the store (`flowLanding.land`).
useEffect(() => {
// Canvas's bail-out: no active project (welcome screen) leaves the PREVIOUS nodes mounted.
if (!activeProjectId) {
@@ -186,13 +217,32 @@ function CanvasReplica() {
viewportRef.current = vp
void setViewport(vp)
}
+ const projectId = activeProjectId
+ // Canvas's pendingFocusRef consumer, cut down to the two reads that matter: the node off
+ // React Flow's store (`getInternalNode`, as frameNode does) and the pan/zoom the camera moves.
+ const consume = (): void => {
+ landings.push(projectId)
+ const id = pendingFocus
+ if (!id) return
+ pendingFocus = null
+ if (!flow.some((n) => n.id === id)) return // Canvas: `nodesRef.current.find`, the node not here
+ const y = getInternalNode(id)?.internals.positionAbsolute.y ?? null
+ framings.push({ id, y, panZoom: store.getState().panZoom })
+ if (y !== null) void setViewport({ x: 40, y: 100 - y, zoom: 1 })
+ }
+ const cancelSettle = scheduleSettle(() => (gateOnOwner ? flowLanding.land(projectId, consume) : consume()))
+ return () => {
+ cancelSettle()
+ flowLanding.cancel()
+ }
// 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)
+ useLayoutEffect(() => {
+ if (settleAtCommitOf === null || renderedProjectId !== settleAtCommitOf) return
+ const fire = heldSettle
+ heldSettle = null
+ fire?.()
+ }, [renderedProjectId])
return (
<SessionProvider session={nodeSession} key={nodeSession.id}>
<OwningProjectContext.Provider value={flowOwnerProjectId}>
@@ -268,6 +318,17 @@ beforeEach(() => {
stored = {}
viewports = {}
readViewport = null
+ readStore = null
+ pendingFocus = null
+ gateOnOwner = true
+ scheduleSettle = (fn) => {
+ const t = setTimeout(fn, 0)
+ return () => clearTimeout(t)
+ }
+ heldSettle = null
+ settleAtCommitOf = null
+ landings = []
+ framings = []
resetSessionsForTest()
createSession('local', fakeApi('local'), 'This machine')
const relay = createSession('relay', fakeApi('host'), 'e2ehost')
@@ -403,6 +464,118 @@ describe('the node subtree runs under the session of the project React Flow STOR
})
})
+// ── The load's framing work waits for the store (review of 2e9684300) ──────────────────────────────
+describe('a just-loaded project is framed only once React Flow\'s store holds its nodes', () => {
+ /** Hold the next settle callback and fire it in the commit that hands `projectId`'s nodes —
+ * before that commit's passive effects copy them into the store. No `act` boundary sits between
+ * the two: React runs them in its own order inside one flush. */
+ function settleBeforeTheStoreCopy(projectId: string): void {
+ scheduleSettle = (fn) => {
+ heldSettle = fn
+ return () => {
+ if (heldSettle === fn) heldSettle = null
+ }
+ }
+ settleAtCommitOf = projectId
+ }
+ /** One node id in both projects ("Open here anyway", a second worktree): only its y tells the
+ * incoming copy (2000) from the outgoing one (0). */
+ function sharedNode(from: string, to: string): void {
+ stored = { [from]: [plainTerminal('term-shared', 0)], [to]: [plainTerminal('term-shared', 2000)] }
+ useActive.setState({ id: from })
+ }
+
+ it('session-changing switch: the framing reads the INCOMING copy and drives the remounted pan/zoom', async () => {
+ sharedNode(LOCAL_ONE, TEAM_ALPHA)
+ await mount()
+ landings = []
+ pendingFocus = 'term-shared'
+ settleBeforeTheStoreCopy(TEAM_ALPHA)
+
+ await switchTo(TEAM_ALPHA)
+
+ expect(heldSettle).toBeNull() // the timer did fire, in the window
+ expect(providerKeys.at(-1)).toBe(sessionForProject(TEAM_ALPHA).id) // and the remount happened
+ expect(landings).toEqual([TEAM_ALPHA])
+ expect(framings).toHaveLength(1)
+ expect(framings[0].y).toBe(2000)
+ expect(framings[0].panZoom).toBe(readStore!.getState().panZoom)
+ expect(readViewport!()).toEqual({ x: 40, y: -1900, zoom: 1 })
+ })
+
+ it('local → local (no remount): the owner still flips, and the framing waits for it', async () => {
+ sharedNode(LOCAL_ONE, LOCAL_TWO)
+ await mount()
+ landings = []
+ pendingFocus = 'term-shared'
+ settleBeforeTheStoreCopy(LOCAL_TWO)
+
+ await switchTo(LOCAL_TWO)
+
+ expect(heldSettle).toBeNull()
+ expect(new Set(providerKeys)).toEqual(new Set(['local']))
+ expect(landings).toEqual([LOCAL_TWO])
+ expect(framings.map((f) => f.y)).toEqual([2000])
+ expect(readViewport!()).toEqual({ x: 40, y: -1900, zoom: 1 })
+ })
+
+ it.each([
+ ['session-changing', LOCAL_ONE, TEAM_ALPHA],
+ ['local → local', LOCAL_ONE, LOCAL_TWO]
+ ])('control (%s): on 2e9684300\'s ordering the same timer frames the OUTGOING copy', async (_kind, from, to) => {
+ // The window is real: without the gate, the framing reads the copy the store still holds.
+ sharedNode(from, to)
+ await mount()
+ gateOnOwner = false
+ pendingFocus = 'term-shared'
+ settleBeforeTheStoreCopy(to)
+
+ await switchTo(to)
+
+ expect(heldSettle).toBeNull()
+ expect(framings.map((f) => f.y)).toEqual([0])
+ if (sessionForProject(from).id !== sessionForProject(to).id) {
+ // ...and moved a pan/zoom instance the remount then threw away.
+ expect(framings[0].panZoom).not.toBe(readStore!.getState().panZoom)
+ }
+ })
+
+ it.each([
+ ['session-changing', TEAM_ALPHA],
+ ['local → local', LOCAL_TWO]
+ ])('%s switch into a project with NO nodes still releases the landing, once', async (_kind, to) => {
+ stored = { [LOCAL_ONE]: [plainTerminal('term-x')], [to]: [] }
+ useActive.setState({ id: LOCAL_ONE })
+ await mount()
+ landings = []
+ pendingFocus = 'term-x'
+ settleBeforeTheStoreCopy(to)
+
+ await switchTo(to)
+
+ expect(landings).toEqual([to])
+ expect(pendingFocus).toBeNull() // consumed: the node is not on this canvas, so nothing framed
+ expect(framings).toEqual([])
+ await act(async () => {
+ readStore!.getState().setNodes([]) // a later store change with the same owner
+ })
+ expect(landings).toEqual([to])
+ })
+
+ it('a timer that fires after the store copy frames at once', async () => {
+ sharedNode(LOCAL_ONE, TEAM_ALPHA)
+ await mount()
+ landings = []
+ pendingFocus = 'term-shared'
+
+ await switchTo(TEAM_ALPHA) // the default setTimeout fires after the remount
+
+ expect(landings).toEqual([TEAM_ALPHA])
+ expect(framings.map((f) => f.y)).toEqual([2000])
+ expect(framings[0].panZoom).toBe(readStore!.getState().panZoom)
+ })
+})
+
// ── The hook alone ────────────────────────────────────────────────────────────────────────────────
describe('useFlowStoreOwner', () => {
let renders = 0
@@ -457,8 +630,10 @@ describe('useFlowStoreOwner', () => {
await render(a, 'project-a')
await storeTakes(a)
expect(owner).toBe('project-a')
+ const before = renders
await storeTakes([]) // e.g. a remounted <ReactFlow>'s store reset
expect(owner).toBe('project-a')
+ expect(renders).toBe(before) // the fallback is selected, so nothing changed and nothing rendered
})
it('does not record an array handed while no project is mounted (the welcome-screen bail-out)', async () => {
@@ -484,6 +659,73 @@ describe('useFlowStoreOwner', () => {
})
})
+describe('useFlowLanding', () => {
+ let landing: ReturnType<typeof useFlowLanding> | null = null
+ function Harness({ owner }: { owner: string | null }) {
+ landing = useFlowLanding(owner)
+ return null
+ }
+ async function render(owner: string | null): Promise<void> {
+ await act(async () => {
+ root!.render(<Harness owner={owner} />)
+ })
+ }
+ beforeEach(() => {
+ landing = null
+ host = document.createElement('div')
+ document.body.append(host)
+ root = createRoot(host)
+ })
+
+ it('holds work until the store holds its project, then runs it exactly once', async () => {
+ const ran: string[] = []
+ await render('project-a')
+ landing!.land('project-b', () => ran.push('b'))
+ expect(ran).toEqual([])
+ await render('project-a')
+ expect(ran).toEqual([])
+ await render('project-b')
+ expect(ran).toEqual(['b'])
+ await render('project-a')
+ await render('project-b')
+ expect(ran).toEqual(['b'])
+ })
+
+ it('runs at once when the store already holds the project (an in-place reload, a late timer)', async () => {
+ const ran: string[] = []
+ await render('project-a')
+ landing!.land('project-a', () => ran.push('a'))
+ expect(ran).toEqual(['a'])
+ })
+
+ it('cancel drops the work: a project the user moved on from never runs it late', async () => {
+ const ran: string[] = []
+ await render('project-a')
+ landing!.land('project-b', () => ran.push('b'))
+ landing!.cancel()
+ await render('project-b')
+ expect(ran).toEqual([])
+ })
+
+ it('a later land replaces the earlier one', async () => {
+ const ran: string[] = []
+ await render('project-a')
+ landing!.land('project-b', () => ran.push('b'))
+ landing!.land('project-c', () => ran.push('c'))
+ await render('project-b')
+ expect(ran).toEqual([])
+ await render('project-c')
+ expect(ran).toEqual(['c'])
+ })
+
+ it('is one stable object (Canvas leaves it out of the load effect\'s deps)', async () => {
+ await render('project-a')
+ const first = landing
+ await render('project-b')
+ expect(landing).toBe(first)
+ })
+})
+
// ── 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')
@@ -509,6 +751,26 @@ describe('source pin (the replica mirrors Canvas)', () => {
expect(CANVAS).not.toMatch(/key=\{sessionForProject\(activeProjectId/)
})
+ it('Canvas frames a just-loaded project only through the owner-gated landing', () => {
+ const hook = CANVAS.indexOf('const flowOwnerProjectId = useFlowStoreOwner(allNodes, renderedProjectId)')
+ expect(CANVAS.indexOf('const flowLanding = useFlowLanding(flowOwnerProjectId)')).toBeGreaterThan(hook)
+ const timer = CANVAS.indexOf('const t = setTimeout(() => {\n loadingRef.current = false')
+ const land = CANVAS.indexOf('flowLanding.land(project.id, () => {', timer)
+ const end = CANVAS.indexOf('\n }, 0)\n', land)
+ expect(timer).toBeGreaterThan(0)
+ expect(land).toBeGreaterThan(timer)
+ expect(end).toBeGreaterThan(land)
+ const beforeLand = CANVAS.slice(timer, land)
+ const inLand = CANVAS.slice(land, end)
+ for (const ref of ['pendingFocusRef', 'pendingResumeRef', 'pendingLinkRef']) {
+ expect(beforeLand).not.toContain(ref)
+ expect(inLand).toContain(`${ref}.current`)
+ }
+ expect(inLand).toContain('goToNode(node)')
+ // The load effect's cleanup drops it, so a project the user moved on from never frames late.
+ expect(CANVAS.slice(end, end + 120)).toContain('flowLanding.cancel()')
+ })
+
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
index d3a5241a9..660516add 100644
--- a/src/renderer/canvas/flowStoreOwner.ts
+++ b/src/renderer/canvas/flowStoreOwner.ts
@@ -27,8 +27,10 @@ export function useFlowStoreOwner(handed: readonly unknown[], renderedProjectId:
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
+ // The fallback lives INSIDE the selector: a remount's store reset (`[]`) then selects the same
+ // owner and schedules no render, where an `undefined` outside it cost a whole Canvas render.
+ const held = useStore((s) => owners.get(s.nodes) ?? lastKnownRef.current)
+ lastKnownRef.current = held
+ return held
}