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): never commit one project's canvas under another project's id

enesc74b038cdcf7
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 9f881d0c0..98198f300 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -241,6 +241,7 @@ import type {
import type { KanbanCreateChoice, KanbanSession } from '../components/kanban/KanbanView'
import { assignNode, assignedTo, defaultKanban, labelsForCard, migrateProjectTags, resolveColumnRef, unassigned } from '../lib/kanban'
import { registerWorkspaceDirty } from '../state/workspaceDirty'
+import { canCommitCanvas } from '../state/persistGuards'
import { isHidden } from '../lib/ui-visibility'
import { boardLogEvents } from '../lib/boardLogDiff'
import { useBoardLog } from '../state/boardLog'
@@ -904,6 +905,13 @@ export function Canvas() {
const settings = useSettings((s) => s.settings)
const viewportRef = useRef<Viewport>({ x: 0, y: 0, zoom: 1 })
const nodesRef = useRef<CanvasNode[]>(nodes)
+ /**
+ * WHICH project's nodes `nodesRef` currently holds — the epoch tag that pairs with
+ * `activeProjectId` (see canCommitCanvas). Written only where the load effect installs a
+ * project's nodes, and invalidated (null) on its bail-out paths; null until the first load, so
+ * the initial empty `useNodesState([])` can never be committed as some project's canvas.
+ */
+ const nodesProjectIdRef = useRef<string | null>(null)
// focusNodeById, for callbacks declared ABOVE its definition (openFile's dedupe focuses the
// already-open node). Assigned right after the definition, same render-mirror idiom as nodesRef.
const focusNodeRef = useRef<(nodeId: string) => void>(() => {})
@@ -1578,9 +1586,17 @@ export function Canvas() {
// (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).
setActiveSession(sessionForProject(activeProjectId || '').id)
- if (!activeProjectId) return
+ // Both bail-outs below leave the PREVIOUS project's nodes mounted in React Flow. Invalidate the
+ // epoch tag on the way out so nothing commits them under the new id (field bug 2026-08-10).
+ if (!activeProjectId) {
+ nodesProjectIdRef.current = null
+ return
+ }
const project = useProjects.getState().getProject(activeProjectId)
- if (!project) return
+ if (!project) {
+ nodesProjectIdRef.current = null
+ return
+ }
// SSH project: (re)open its ControlMaster and record the controlPath so this project's
// terminal nodes can run over it. Idempotent in main (a live master is reused), so a tab
// switch back to a connected project is a no-op. Remote tmux is unaffected by the master.
@@ -1605,6 +1621,12 @@ export function Canvas() {
loadingRef.current = true
const flow = nodeStatesToFlow(project.nodes)
setNodes(flow)
+ // React Flow now holds THIS project's canvas: the commit guard may pair it with the active id
+ // again. Both refs are assigned HERE, synchronously, because `setNodes` only lands on the next
+ // render — mirroring the nodes (same idiom as the peer-mutation path) keeps the array and its
+ // epoch tag atomic, so no timer firing in between can commit the previous project's nodes.
+ nodesRef.current = flow
+ nodesProjectIdRef.current = project.id
// Worktree facts are per project: drop the previous project's (reset also clears its
// statuses), then re-resolve from this project's cwd. SSH projects are skipped — local git
// cannot reason about a remote path. Fire-and-forget: the store is epoch-guarded + fails open.
@@ -1743,7 +1765,11 @@ export function Canvas() {
// ---- persistence helpers ----
const commitActiveToStore = useCallback(() => {
const id = useProjects.getState().activeProjectId
- if (id)
+ // Epoch pairing: only commit while the nodes React Flow holds belong to the ACTIVE project.
+ // The normal switch flow still commits — every caller commits BEFORE `setActive`, while the two
+ // ids still agree — but an autosave timer armed under the previous project now skips instead of
+ // writing its nodes under the new project's id (field bug 2026-08-10).
+ if (canCommitCanvas(nodesProjectIdRef.current, id))
useProjects
.getState()
.commitCanvas(
diff --git a/src/renderer/state/persistGuards.test.ts b/src/renderer/state/persistGuards.test.ts
new file mode 100644
index 000000000..ef662b5d3
--- /dev/null
+++ b/src/renderer/state/persistGuards.test.ts
@@ -0,0 +1,28 @@
+import { describe, it, expect } from 'vitest'
+import { canCommitCanvas } from './persistGuards'
+
+describe('canCommitCanvas', () => {
+ it('commits while the nodes in hand belong to the active project', () => {
+ expect(canCommitCanvas('a', 'a')).toBe(true)
+ })
+
+ // The field bug (2026-08-10): the 800ms autosave timer is armed under project A, the user
+ // switches to B (zustand updates synchronously), and the timer fires BEFORE the load effect has
+ // installed B's nodes — so A's nodes would be committed under B's id, wiping B.
+ it('refuses to commit one project canvas under another project id', () => {
+ expect(canCommitCanvas('a', 'b')).toBe(false)
+ })
+
+ // Before the first load effect runs, React Flow holds the initial `useNodesState([])` — an empty
+ // array that belongs to NO project. Committing it is the "both canvases went blank" wipe.
+ it('refuses to commit nodes that belong to no project yet', () => {
+ expect(canCommitCanvas(null, 'a')).toBe(false)
+ })
+
+ // No project open (welcome screen): there is no id to write under.
+ it('refuses to commit with no active project', () => {
+ expect(canCommitCanvas('a', '')).toBe(false)
+ expect(canCommitCanvas(null, '')).toBe(false)
+ expect(canCommitCanvas('', '')).toBe(false)
+ })
+})
diff --git a/src/renderer/state/persistGuards.ts b/src/renderer/state/persistGuards.ts
new file mode 100644
index 000000000..c352533bd
--- /dev/null
+++ b/src/renderer/state/persistGuards.ts
@@ -0,0 +1,24 @@
+// Pure decisions that guard the renderer's whole-file workspace save. Canvas owns the refs and the
+// timers; the RULES live here so they are unit-testable without React Flow, and so the reasoning
+// behind them survives the next edit of an 8k-line component.
+
+/**
+ * May the live React Flow canvas be committed into the store under `activeProjectId`?
+ *
+ * Field bug 2026-08-10 (two projects + rapid tab switching wiped both canvases): the active id
+ * comes from the zustand store, which updates SYNCHRONOUSLY on a tab switch, while the nodes array
+ * React Flow holds is installed by a passive effect one render later. So the 800ms autosave timer
+ * armed under project A can fire with the store already saying B and A's nodes still in hand — and
+ * the commit then writes A's nodes (or the initial empty `useNodesState([])`) under B's id.
+ *
+ * `nodesProjectId` is the epoch tag: WHICH project's nodes React Flow currently holds — null before
+ * the first load, and whenever the load effect bailed out without installing anything (unknown /
+ * no active project), so a stale array can never be mistaken for the new project's truth.
+ *
+ * A mismatch is a transient the next render resolves, so it is skipped SILENTLY: never written, and
+ * never written under the wrong id.
+ */
+export function canCommitCanvas(nodesProjectId: string | null, activeProjectId: string): boolean {
+ if (!activeProjectId) return false
+ return nodesProjectId === activeProjectId
+}