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-sync): rebuild removes and edge ops from their known fields, and cap removes (D1)

review18e00e955af9
diff --git a/src/core/canvas-sync.test.ts b/src/core/canvas-sync.test.ts
index 5597b2f4c..450aaec2e 100644
--- a/src/core/canvas-sync.test.ts
+++ b/src/core/canvas-sync.test.ts
@@ -228,6 +228,19 @@ describe('initCanvasSync (reflector)', () => {
])
})
+ it('forwards only the fields a remove or an edge op defines, and refuses an oversized remove (D1)', () => {
+ t.setClients([1, 2])
+ const pad = 'x'.repeat(100_000)
+ t.cast(1, 'p1', { op: 'remove', id: 'a', pad, src: 'cv-a' })
+ t.cast(1, 'p1', { op: 'edge-upsert', kind: 'rope', edge: { id: 'e', source: 'a', target: 'b', pad }, pad })
+ t.cast(1, 'p1', { op: 'remove', id: 'b', pad: 'x'.repeat(MUTATION_MAX_BYTES) })
+ const to2 = t.sent.filter((x) => x.to === 2).map((x) => x.args[1])
+ expect(to2).toEqual([
+ { op: 'remove', id: 'a', src: 'cv-a', seq: 1 },
+ { op: 'edge-upsert', kind: 'rope', edge: { id: 'e', source: 'a', target: 'b' }, seq: 2 }
+ ])
+ })
+
it('is NOT rate-limited: a bulk delete of many nodes reflects every one', () => {
t.setClients([1, 2])
for (let i = 0; i < 200; i++) t.cast(1, 'p1', { op: 'remove', id: `n${i}` })
diff --git a/src/core/canvas-sync.ts b/src/core/canvas-sync.ts
index 288fa0a2b..4f3933e59 100644
--- a/src/core/canvas-sync.ts
+++ b/src/core/canvas-sync.ts
@@ -230,7 +230,9 @@ export function initCanvasSync(): void {
// - a kanban op is rebuilt by `sanitizeKanbanOp` (unknown fields dropped, a label colour off the
// palette → `default`, an invalid rank / priority / dueAt / category dropped), keeping its
// `src` / `seen` so the order can still judge it. Kanban fields land in a git-shared
- // project.json, and the authority sanitizes again before it writes (the two-seam rule).
+ // project.json, and the authority sanitizes again before it writes (the two-seam rule);
+ // - a remove or an edge op is rebuilt from the fields its op defines (and its stamp), so no extra
+ // field a client attached is forwarded to every peer.
const fromOwner = p.isOwnerClient?.(senderId) === true
const clean = sanitizeCanvasMutation(mutation, fromOwner)
if (!clean) return
diff --git a/src/shared/canvas-mutations.test.ts b/src/shared/canvas-mutations.test.ts
index ae71341b8..bbebe2f5c 100644
--- a/src/shared/canvas-mutations.test.ts
+++ b/src/shared/canvas-mutations.test.ts
@@ -451,12 +451,46 @@ describe('sanitizeCanvasMutation', () => {
expect(sanitizeCanvasMutation({ op: 'kb-card-remove', nodeId: '' })).toBeNull()
})
- it('strips the exec-enabling fields off a node upsert, and passes other ops through', () => {
+ it('strips the exec-enabling fields off a node upsert', () => {
const withShell = { op: 'upsert', node: { ...n('1'), shell: '/bin/evil' } } as CanvasMutation
const out = sanitizeCanvasMutation(withShell) as Extract<CanvasMutation, { op: 'upsert' }>
expect(out.node.shell).toBeUndefined()
- const rm: CanvasMutation = { op: 'remove', id: '1', src: 'a' }
- expect(sanitizeCanvasMutation(rm)).toBe(rm)
+ })
+
+ // D1: every field of a cast is forwarded to every client, so a remove or an edge op is rebuilt
+ // from the fields its op defines — plus the stamp fields the order judges — and nothing else.
+ it('rebuilds remove / edge-remove / edge-upsert from their known fields, keeping src / seq / seen', () => {
+ const pad = 'x'.repeat(10_000)
+ const stamp = { src: 'cv-a', seq: 7, seen: 6 }
+ const rm = { op: 'remove', id: '1', pad, ...stamp } as unknown as CanvasMutation
+ expect(sanitizeCanvasMutation(rm)).toEqual({ op: 'remove', id: '1', ...stamp })
+ const er = { op: 'edge-remove', kind: 'rope', id: 'e1', pad, ...stamp } as unknown as CanvasMutation
+ expect(sanitizeCanvasMutation(er)).toEqual({ op: 'edge-remove', kind: 'rope', id: 'e1', ...stamp })
+ const eu = {
+ op: 'edge-upsert',
+ kind: 'bridge',
+ edge: { id: 'e1', source: 'a', target: 'b', pad },
+ pad,
+ ...stamp
+ } as unknown as CanvasMutation
+ expect(sanitizeCanvasMutation(eu)).toEqual({
+ op: 'edge-upsert',
+ kind: 'bridge',
+ edge: { id: 'e1', source: 'a', target: 'b' },
+ ...stamp
+ })
+ // An absent stamp stays absent (no `src: undefined` key goes on the wire).
+ expect(Object.keys(sanitizeCanvasMutation({ op: 'remove', id: '1' })!)).toEqual(['op', 'id'])
+ })
+})
+
+describe('isCanvasMutation — the byte cap covers removes too (D1)', () => {
+ it('refuses an oversized remove and edge-remove, like every other op', () => {
+ const pad = 'x'.repeat(MUTATION_MAX_BYTES)
+ expect(isCanvasMutation({ op: 'remove', id: '1', pad })).toBe(false)
+ expect(isCanvasMutation({ op: 'edge-remove', kind: 'rope', id: 'e1', pad })).toBe(false)
+ // The shape verdict alone (the reducer's guard) still accepts them.
+ expect(isWellFormedMutation({ op: 'remove', id: '1', pad })).toBe(true)
})
})
diff --git a/src/shared/canvas-mutations.ts b/src/shared/canvas-mutations.ts
index 6ad637016..9c5dc29ed 100644
--- a/src/shared/canvas-mutations.ts
+++ b/src/shared/canvas-mutations.ts
@@ -99,8 +99,11 @@ function checkMutation(value: unknown, sized: boolean): value is CanvasMutation
const fits = (x: unknown): boolean => !sized || withinSizeLimit(x)
const m = value as { op?: unknown; id?: unknown; node?: unknown; kind?: unknown; edge?: unknown }
if (isKanbanOp(m)) return sanitizeKanbanOp(value) !== null && fits(value)
- if (m.op === 'remove') return isRefId(m.id)
- if (m.op === 'edge-remove') return isEdgeKind(m.kind) && isRefId(m.id)
+ // Removes are held to the byte cap too: the reflector rebuilds them from their known fields
+ // (`sanitizeCanvasMutation`), but an 8 MiB frame must not reach even that far, nor the authority's
+ // op log.
+ if (m.op === 'remove') return isRefId(m.id) && fits(value)
+ if (m.op === 'edge-remove') return isEdgeKind(m.kind) && isRefId(m.id) && fits(value)
if (m.op === 'edge-upsert') {
if (!isEdgeKind(m.kind)) return false
const edge = m.edge as { id?: unknown; source?: unknown; target?: unknown } | undefined
@@ -130,23 +133,36 @@ function checkMutation(value: unknown, sized: boolean): value is CanvasMutation
* - a kanban op is rebuilt by `sanitizeKanbanOp` (unknown fields dropped, colour / rank / priority /
* dueAt / category repaired or dropped), KEEPING the stamp fields `src` / `seq` / `seen` — the
* order still has to judge it;
- * - everything else passes through unchanged.
+ * - a `remove`, `edge-remove` or `edge-upsert` is rebuilt from the fields its op defines (an edge is
+ * its three ids), with the same stamp fields: whatever else a cast carries would otherwise be
+ * forwarded to every client and kept in the authority's op log (D1).
* `null` = refused (a kanban op `sanitizeKanbanOp` refuses). The caller still runs
- * `isCanvasMutation` first for the size cap; this adds no size check of its own.
+ * `isCanvasMutation` first for the shape and the size cap; this adds no check of its own.
*/
export function sanitizeCanvasMutation(m: CanvasMutation, keepLaunch = false): CanvasMutation | null {
if (isKanbanOp(m)) {
const clean = sanitizeKanbanOp(m)
if (!clean) return null
- const out: CanvasMutation = { ...clean }
- if (m.src !== undefined) out.src = m.src
- if (m.seq !== undefined) out.seq = m.seq
- if (m.seen !== undefined) out.seen = m.seen
- return out
+ return withStamp({ ...clean }, m)
+ }
+ if (m.op === 'remove') return withStamp({ op: 'remove', id: m.id }, m)
+ if (m.op === 'edge-remove') return withStamp({ op: 'edge-remove', kind: m.kind, id: m.id }, m)
+ if (m.op === 'edge-upsert') {
+ const { id, source, target } = m.edge
+ return withStamp({ op: 'edge-upsert', kind: m.kind, edge: { id, source, target } }, m)
}
return sanitizeInboundMutation(m, keepLaunch)
}
+/** Copy the order's stamp fields (`src`, `seq`, `seen`) from `from` onto a rebuilt op; an absent one
+ * stays absent. `origin` is deliberately not among them: only the core adds it, per recipient. */
+function withStamp(out: CanvasMutation, from: CanvasMutation): CanvasMutation {
+ if (from.src !== undefined) out.src = from.src
+ if (from.seq !== undefined) out.seq = from.seq
+ if (from.seen !== undefined) out.seen = from.seen
+ return out
+}
+
function withinSizeLimit(m: unknown): boolean {
try {
// The wire form is JSON on the server and a structured clone on the desktop; either way this