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(b5): canvas-sync wire-format (applyMutation/diffToMutations)

enese601403e95a6
diff --git a/src/main/remote/canvas-sync.ts b/src/main/remote/canvas-sync.ts
new file mode 100644
index 000000000..b0dbf7d83
--- /dev/null
+++ b/src/main/remote/canvas-sync.ts
@@ -0,0 +1,72 @@
+// Pure helpers for the remote canvas mirror: turn a host's node snapshot into a
+// wire-syncable form and apply mutations. No Electron, no sockets — shared by the
+// host broadcast and the client mirror in later tasks.
+
+import type { CanvasMutation, CanvasNodeState } from '@shared/types'
+
+export type { CanvasMutation, CanvasState } from '@shared/types'
+
+/**
+ * Apply a single mutation to a node list, returning a NEW array (the input is never
+ * mutated). `upsert` replaces the node with the matching id, or appends it if absent;
+ * `remove` filters out the node with the given id.
+ */
+export function applyMutation(
+ nodes: CanvasNodeState[],
+ m: CanvasMutation
+): CanvasNodeState[] {
+ if (m.op === 'remove') {
+ return nodes.filter((node) => node.id !== m.id)
+ }
+ // upsert
+ const idx = nodes.findIndex((node) => node.id === m.node.id)
+ if (idx === -1) {
+ return [...nodes, m.node]
+ }
+ const next = nodes.slice()
+ next[idx] = m.node
+ return next
+}
+
+/** Stable JSON stringify (keys sorted) so deep-equality is order-independent. */
+function stableStringify(value: unknown): string {
+ return JSON.stringify(value, (_key, val) => {
+ if (val && typeof val === 'object' && !Array.isArray(val)) {
+ const sorted: Record<string, unknown> = {}
+ for (const k of Object.keys(val as Record<string, unknown>).sort()) {
+ sorted[k] = (val as Record<string, unknown>)[k]
+ }
+ return sorted
+ }
+ return val
+ })
+}
+
+/**
+ * Diff two node snapshots into the minimal mutation list (deterministic): an `upsert`
+ * for every node that was added or changed (deep-equal via stable stringify), and a
+ * `remove` for every node that was dropped. Never throws on normal input.
+ */
+export function diffToMutations(
+ prev: CanvasNodeState[],
+ next: CanvasNodeState[]
+): CanvasMutation[] {
+ const mutations: CanvasMutation[] = []
+ const prevById = new Map(prev.map((node) => [node.id, node]))
+ const nextIds = new Set(next.map((node) => node.id))
+
+ // upserts: added or changed nodes (in next-array order for determinism).
+ for (const node of next) {
+ const before = prevById.get(node.id)
+ if (!before || stableStringify(before) !== stableStringify(node)) {
+ mutations.push({ op: 'upsert', node })
+ }
+ }
+ // removes: nodes present in prev but gone from next (in prev-array order).
+ for (const node of prev) {
+ if (!nextIds.has(node.id)) {
+ mutations.push({ op: 'remove', id: node.id })
+ }
+ }
+ return mutations
+}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 655c2b03b..8850038f5 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -54,6 +54,22 @@ export interface CanvasNodeState {
commitOid?: string
}
+/**
+ * A snapshot of one canvas's nodes in the form sent over the remote mirror wire.
+ * Reuses the persisted node shape (`CanvasNodeState`) so host and client agree on layout.
+ */
+export interface CanvasState {
+ nodes: CanvasNodeState[]
+}
+
+/**
+ * A minimal change to a canvas node list: replace-or-append a node by id, or drop one by id.
+ * Used for the client's optimistic edits and host-side diffing (see `applyMutation`/`diffToMutations`).
+ */
+export type CanvasMutation =
+ | { op: 'upsert'; node: CanvasNodeState }
+ | { op: 'remove'; id: string }
+
/** Canvas pan/zoom state. */
export interface Viewport {
x: number
diff --git a/test/remote/canvas-sync.test.ts b/test/remote/canvas-sync.test.ts
new file mode 100644
index 000000000..b2c31819d
--- /dev/null
+++ b/test/remote/canvas-sync.test.ts
@@ -0,0 +1,19 @@
+import { describe, it, expect } from 'vitest'
+import { applyMutation, diffToMutations } from '../../src/main/remote/canvas-sync'
+const n = (id: string, x = 0) => ({ id, kind: 'terminal', position: { x, y: 0 }, data: {} }) as never
+describe('canvas-sync', () => {
+ it('upsert adds / replaces by id, immutably', () => {
+ const a = [n('1')]
+ expect(applyMutation(a, { op: 'upsert', node: n('2') })).toHaveLength(2)
+ expect(applyMutation(a, { op: 'upsert', node: n('1', 9) })[0].position.x).toBe(9)
+ expect(a[0].position.x).toBe(0)
+ })
+ it('remove drops by id', () => {
+ expect(applyMutation([n('1'), n('2')], { op: 'remove', id: '1' }).map((x) => x.id)).toEqual(['2'])
+ })
+ it('diffToMutations finds moves and drops', () => {
+ expect(diffToMutations([n('1')], [n('1', 5)])).toEqual([{ op: 'upsert', node: n('1', 5) }])
+ expect(diffToMutations([n('1'), n('2')], [n('1')])).toEqual([{ op: 'remove', id: '2' }])
+ expect(diffToMutations([n('1')], [n('1')])).toEqual([])
+ })
+})