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.

test(canvas-sync): behaviour-test Canvas's applyToStored (D12)

review26477e38b95d
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 0b6e57fdf..82c6fd023 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -652,7 +652,7 @@ import { snapResizeChanges } from '../lib/resizeSnap'
import { canClearDirty, canCommitCanvas, canCreateOnCanvas, liveCanvasHolds } from '../state/persistGuards'
import { mirrorLatest, rebaseOnLatest, useNodesEpoch } from './nodesEpoch'
import { boardLiveNodeIds, createKanbanPublisher } from './kanban-sync'
-import { createStoredCanvasPublisher } from './stored-publish'
+import { applyToStoredCopy, createStoredCanvasPublisher } from './stored-publish'
import { isHidden } from '../lib/ui-visibility'
import { boardLogEvents } from '../lib/boardLogDiff'
import { useBoardLog } from '../state/boardLog'
@@ -3783,20 +3783,13 @@ export function Canvas() {
/**
* Apply ONE op to a project's STORED copy, through the store reducer (`applyCanvasOp`) — never
- * through `setProjectKanban`, whose publish hook would cast it again. The path for a peer's op on
- * a project React Flow does not hold, for EVERY peer board op (the board reads the store, not
- * React Flow), and for the kanban publisher's local repair (ruling R2). Save only when that copy
- * actually changed: the store writes nothing for an op that changes nothing (a duplicate cast —
- * every Server Edition tab re-casts what it receives — or a remove of something already gone), so
- * the project object's identity is the answer, and a no-op schedules no save.
+ * through `setProjectKanban`, whose publish hook would cast it again — and save only when that copy
+ * actually changed. The path for a peer's op on a project React Flow does not hold, for EVERY peer
+ * board op, and for the kanban publisher's local repair (ruling R2). The body is
+ * `applyToStoredCopy` (./stored-publish), behaviour-tested in kanban-sync.test.ts.
*/
const applyToStored = useCallback(
- (projectId: string, mutation: CanvasMutation): void => {
- const store = useProjects.getState()
- const before = store.getProject(projectId)
- if (store.applyCanvasOp(projectId, mutation) && useProjects.getState().getProject(projectId) !== before)
- markDirty()
- },
+ (projectId: string, mutation: CanvasMutation): void => applyToStoredCopy(projectId, mutation, markDirty),
[markDirty]
)
diff --git a/src/renderer/canvas/kanban-sync-wiring.test.ts b/src/renderer/canvas/kanban-sync-wiring.test.ts
index b6110514f..ab8095699 100644
--- a/src/renderer/canvas/kanban-sync-wiring.test.ts
+++ b/src/renderer/canvas/kanban-sync-wiring.test.ts
@@ -102,8 +102,10 @@ describe('kanban sync wiring', () => {
// Before the edge and node branches: a board op never touches React Flow, active project or not.
expect(recv.indexOf('isKanbanOp(mutation)')).toBeLessThan(recv.indexOf('isEdgeMutation(mutation)'))
expect(recv).not.toMatch(/setProjectKanban\(/)
+ // The body is `applyToStoredCopy` (behaviour-tested in kanban-sync.test.ts, D12); Canvas only
+ // hands it the save trigger.
const apply = between(canvas, 'const applyToStored = useCallback(', '[markDirty]')
- expect(apply).toContain('store.applyCanvasOp(projectId, mutation)')
+ expect(apply).toContain('applyToStoredCopy(projectId, mutation, markDirty)')
})
// Spec §5: only the originating client records a board-log entry — a client applying a peer's op
diff --git a/src/renderer/canvas/kanban-sync.test.ts b/src/renderer/canvas/kanban-sync.test.ts
index 733ee7e32..179da01f6 100644
--- a/src/renderer/canvas/kanban-sync.test.ts
+++ b/src/renderer/canvas/kanban-sync.test.ts
@@ -3,7 +3,8 @@ import { boardLiveNodeIds, createKanbanPublisher, type KanbanPublisherDeps } fro
import { applyKanbanOp } from '@shared/kanban-ops'
import { defaultKanbanFor } from '@shared/kanban-default-board'
import type { CanvasMutation, KanbanOp, ProjectKanban } from '@shared/types'
-import { setKanbanPublishHook, useProjects } from '../state/projects'
+import { setKanbanPublishHook, setStoredCanvasPublishHook, useProjects } from '../state/projects'
+import { applyToStoredCopy, createStoredCanvasPublisher } from './stored-publish'
import { nodeStatesToFlow } from '../state/workspace'
import { pruneAssignments } from '../lib/kanban'
import { kanbanSessionsFrom } from './toKanbanSession'
@@ -243,3 +244,89 @@ describe('the store funnel publishes, and only the funnel', () => {
expect(sent).toEqual([])
})
})
+
+// D12: Canvas's `applyToStored` — the receive handler's write for EVERY peer board op, and for a
+// peer's node or edge op on a project React Flow does not hold. It is also where D5's sender-side
+// convergence lands (our own last order op's echo is applied, not dropped). Canvas delegates to
+// `applyToStoredCopy` with its `markDirty`; this drives that with both publish hooks registered the
+// way Canvas registers them, and a background project.
+describe('applyToStored: a peer op lands in the stored copy and is never published again (D12)', () => {
+ const nodeState = (id: string) =>
+ ({ id, kind: 'terminal', title: id, color: '#fff', group: '', position: { x: 0, y: 0 }, size: { width: 10, height: 10 } }) as never
+ let sent: Array<[string, CanvasMutation]>
+ let dirty: number
+ const apply = (id: string, m: CanvasMutation): void => applyToStoredCopy(id, m, () => { dirty++ })
+
+ beforeEach(() => {
+ useProjects.getState().hydrate({ version: 2, activeProjectId: '', projects: [] })
+ const active = useProjects.getState().addProject('active').id
+ const bg = useProjects.getState().addProject('bg').id
+ useProjects.getState().commitCanvas(bg, [nodeState('n1'), nodeState('n2')], { x: 0, y: 0, zoom: 1 })
+ useProjects.getState().setActive(active)
+ sent = []
+ dirty = 0
+ const send = (id: string, m: CanvasMutation): boolean => { sent.push([id, m]); return true }
+ const kanban = createKanbanPublisher({
+ send,
+ liveNodeIds: (id) => new Set((useProjects.getState().getProject(id)?.nodes ?? []).map((n) => n.id)),
+ shouldPublish: () => true,
+ applyLocal: (id, m) => applyToStoredCopy(id, m, () => { dirty++ })
+ })
+ setKanbanPublishHook((id, prev, next) => kanban.publish(id, prev, next))
+ // Every gate open, the background project governed: if the write reached a publish hook, it casts.
+ setStoredCanvasPublishHook(
+ createStoredCanvasPublisher({ renderedProjectId: () => active, isGoverned: () => true, shouldPublish: () => true, send })
+ )
+ })
+ afterEach(() => {
+ setKanbanPublishHook(null)
+ setStoredCanvasPublishHook(null)
+ })
+
+ const bgId = (): string => useProjects.getState().projects.find((p) => p.name === 'bg')!.id
+
+ it('a peer board op for a background project is stored and saved once; a duplicate is neither', () => {
+ const id = bgId()
+ const column = defaultKanbanFor(id).columns[1].id
+ const op: KanbanOp = { op: 'kb-card', assignment: { nodeId: 'n1', columnId: column } }
+ apply(id, op)
+ expect(useProjects.getState().getProject(id)?.kanban?.assignments).toEqual([op.assignment])
+ expect(dirty).toBe(1)
+ expect(sent).toEqual([])
+ // Every Server Edition tab re-casts what it receives: the same op again changes nothing.
+ const before = useProjects.getState().getProject(id)
+ apply(id, op)
+ expect(useProjects.getState().getProject(id)).toBe(before)
+ expect(dirty).toBe(1)
+ expect(sent).toEqual([])
+ })
+
+ it('a peer node or edge op for a background project is stored, not re-cast by the stored hook', () => {
+ const id = bgId()
+ apply(id, { op: 'upsert', node: nodeState('n3') })
+ apply(id, { op: 'edge-upsert', kind: 'bridge', edge: { id: 'b1', source: 'n1', target: 'n3' } })
+ expect(useProjects.getState().getProject(id)?.nodes.map((n) => n.id)).toEqual(['n1', 'n2', 'n3'])
+ expect(useProjects.getState().getProject(id)?.bridges).toEqual([{ id: 'b1', source: 'n1', target: 'n3' }])
+ expect(dirty).toBe(2)
+ expect(sent).toEqual([])
+ })
+
+ it('D5: our own last order op’s echo sorts a teammate’s concurrent column in, and publishes nothing', () => {
+ const id = bgId()
+ const [a, b, c] = defaultKanbanFor(id).columns.map((col) => col.id)
+ // A teammate's new column arrived while our reorder was in flight: it sits unlisted, at the end.
+ apply(id, { op: 'kb-column', column: { id: 'late', title: 'Late', color: '#fff' } as never })
+ expect(useProjects.getState().getProject(id)?.kanban?.columns.map((col) => col.id)).toEqual([a, b, c, 'late'])
+ dirty = 0
+ // The echo of OUR order op, which every other replica applied (it lists what we knew).
+ apply(id, { op: 'kb-column-order', ids: [c, a, b] })
+ const order = useProjects.getState().getProject(id)?.kanban?.columns.map((col) => col.id)
+ expect(order?.slice(0, 3)).toEqual([c, a, b])
+ expect(dirty).toBe(1)
+ expect(sent).toEqual([])
+ // With nothing concurrent the echo is a fixed point: no store write, no save.
+ apply(id, { op: 'kb-column-order', ids: [c, a, b] })
+ expect(dirty).toBe(1)
+ expect(sent).toEqual([])
+ })
+})
diff --git a/src/renderer/canvas/stored-publish.ts b/src/renderer/canvas/stored-publish.ts
index dbd009e53..5da25a6db 100644
--- a/src/renderer/canvas/stored-publish.ts
+++ b/src/renderer/canvas/stored-publish.ts
@@ -18,7 +18,7 @@
// - THE SAME GATE AND THE SAME CAST as every other family (`shouldPublishFor`, `castFor`): the same
// core, a role that may publish, the order's pending entry and the size guard. A held launch rides
// the owner leg as usual.
-import type { StoredCanvasPublishHook } from '../state/projects'
+import { useProjects, type StoredCanvasPublishHook } from '../state/projects'
import type { CanvasMutation } from '@shared/types'
export interface StoredCanvasPublisherDeps {
@@ -40,3 +40,21 @@ export function createStoredCanvasPublisher(deps: StoredCanvasPublisherDeps): St
for (const m of ops()) deps.send(projectId, m)
}
}
+
+/**
+ * The RECEIVE side's write into a project's stored copy — Canvas's `applyToStored`, which calls this
+ * with its `markDirty`. Applies ONE op through the store reducer (`applyCanvasOp`), which calls
+ * neither publish hook (this module's, nor the board's `setProjectKanban` funnel), so an op that came
+ * from someone else is never cast again as ours. It is the path for EVERY peer board op (the board
+ * reads the store, not React Flow), for a peer's node or edge op on a project React Flow does not
+ * hold, for the kanban publisher's local repair (ruling R2), and for the echo of our own last order
+ * op (D5). `onChanged` runs only when the stored copy actually changed: the store writes nothing for
+ * an op that changes nothing (a duplicate cast — every Server Edition tab re-casts what it receives —
+ * or a remove of something already gone), so the project object's identity is the answer, and a
+ * no-op schedules no save.
+ */
+export function applyToStoredCopy(projectId: string, mutation: CanvasMutation, onChanged: () => void): void {
+ const store = useProjects.getState()
+ const before = store.getProject(projectId)
+ if (store.applyCanvasOp(projectId, mutation) && useProjects.getState().getProject(projectId) !== before) onChanged()
+}