Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

sync: a property REMOVAL crashed every replica that received it (#219)

nyblnetd92a1afeab42
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d5fa870c..11f80ab9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -211,6 +211,15 @@ jobs:
slides/node_modules/.bin/esbuild scripts/test-clipboard.ts --bundle --platform=node --format=esm --outfile="$RUNNER_TEMP/test-clipboard.mjs"
node "$RUNNER_TEMP/test-clipboard.mjs"
+ - name: CRDT property-removal rig
+ # Removing a property travels as a `set` op with `v` ABSENT, and the
+ # receiving replica threw on it: JSON.stringify(undefined) is undefined,
+ # and a debug line sliced it. Live in shipped files. The convergence rig
+ # missed it through 300 seeds because its generator assigns properties
+ # and never removes one — a property-based rig only explores the
+ # mutations it was taught.
+ run: node scripts/test-crdt-delprop.ts
+
- name: CRDT convergence rig
# 40 seeds locally; deeper on CI where the seconds are free.
run: SEEDS=300 node scripts/test-sync.ts
diff --git a/scripts/test-crdt-delprop.ts b/scripts/test-crdt-delprop.ts
new file mode 100644
index 00000000..e722f47e
--- /dev/null
+++ b/scripts/test-crdt-delprop.ts
@@ -0,0 +1,100 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// CRDT property-REMOVAL rig.
+//
+// node scripts/test-crdt-delprop.ts
+//
+// WHAT THIS PROVES. Removing a property is an ordinary edit — the editor does
+// it in a dozen places (fill style → solid deletes `colorGradient`, "none"
+// deletes `textStroke`, Unlink deletes a chart's `source`, ⇧⌘G deletes
+// `groupId`) — and it travels as a `set` op whose `v` is ABSENT, which is how
+// the engine spells "delete the key".
+//
+// On the RECEIVING replica that threw:
+//
+// TypeError: Cannot read properties of undefined (reading 'slice')
+//
+// `JSON.stringify(undefined)` is `undefined`, not `"undefined"`, and the debug
+// line sliced it. A template literal evaluates its expressions eagerly, so the
+// dbg() call being a no-op did not save it — every collaborator applying that
+// op crashed, live, in shipped files.
+//
+// The convergence rig never caught it in 300 seeds because its random mutations
+// assign properties and never REMOVE one. That is the lesson worth keeping: a
+// property-based rig only explores the mutations you taught it, so an operation
+// the UI performs and the generator does not is invisible no matter how many
+// seeds you run.
+
+import { SyncState } from '../slides/src/sync/crdt.ts'
+
+let failures = 0
+let checks = 0
+function ok(cond: boolean, msg: string) {
+ checks++
+ if (!cond) { failures++; console.log(` FAIL ${msg}`) }
+ else console.log(` ok ${msg}`)
+}
+
+const el = (extra: Record<string, unknown> = {}) => ({
+ id: 'e1', type: 'text', x: 0, y: 0, w: 100, h: 50, rotation: 0, opacity: 1,
+ html: 'hi', fontSize: 20, fontFamily: 'x', fontWeight: 400,
+ color: '#000', align: 'left', valign: 'top', lineHeight: 1.2, ...extra,
+})
+const doc = (els: unknown[]) => ({
+ format: 'bento/slides', version: '1', docId: 'd1', title: 't',
+ size: { width: 1280, height: 720 }, theme: { accent: '#000' }, assets: {}, fonts: null,
+ slides: [{ id: 's1', name: 'one', background: '#fff', transition: 'fade', notes: '', elements: els }],
+}) as any
+
+// Each of these is a real editor action that deletes a key.
+const REMOVALS: Array<[string, Record<string, unknown>]> = [
+ ['shadow (Shadow → none)', { shadow: { blur: 8, color: '#0003' } }],
+ ['colorGradient (Fill style → solid)', { colorGradient: { angle: 90, stops: [{ at: 0, color: '#f00' }] } }],
+ ['textStroke (Outline → none)', { textStroke: { width: 2, color: '#000' } }],
+ ['groupId (⇧⌘G ungroup)', { groupId: 'g1' }],
+ ['link (clear the jump target)', { link: 's2' }],
+]
+
+for (const [label, extra] of REMOVALS) {
+ const before = doc([el(extra)])
+ const after = doc([el()])
+
+ const author = new SyncState('alice')
+ author.adopt(before)
+ const ops = author.diff(before, after)
+
+ const setOps = ops.filter((o: any) => o.op === 'set')
+ ok(setOps.length > 0 && setOps.some((o: any) => o.v === undefined),
+ `${label}: travels as a set op with v absent`)
+
+ // The receiving replica is where it broke.
+ const peer = new SyncState('bob')
+ const target = doc([el(extra)])
+ peer.adopt(target)
+ let threw: string | null = null
+ try { peer.apply(target, ops as any) } catch (e: any) { threw = `${e.constructor.name}: ${e.message}` }
+
+ ok(threw === null, `${label}: a peer applies it without throwing${threw ? ` (got ${threw})` : ''}`)
+ const key = Object.keys(extra)[0]
+ ok(threw !== null || (target.slides[0].elements[0] as any)[key] === undefined,
+ `${label}: the property is actually gone on the peer`)
+}
+
+// The same op arriving for a node the peer has not seen yet parks in `pending`
+// and is replayed later — that path re-applies the op, so it must not throw either.
+{
+ const before = doc([el({ shadow: { blur: 4, color: '#000' } })])
+ const author = new SyncState('alice')
+ author.adopt(before)
+ const ops = author.diff(before, doc([el()]))
+ const peer = new SyncState('bob')
+ const empty = doc([])
+ peer.adopt(empty)
+ let threw: string | null = null
+ try { peer.apply(empty, ops as any) } catch (e: any) { threw = `${e.constructor.name}: ${e.message}` }
+ ok(threw === null, `a removal for an unknown node pends without throwing${threw ? ` (got ${threw})` : ''}`)
+}
+
+console.log(`\n${checks - failures}/${checks} checks passed`)
+if (failures) process.exit(1)
diff --git a/slides/src/sync/crdt.ts b/slides/src/sync/crdt.ts
index d6257068..33dcb301 100644
--- a/slides/src/sync/crdt.ts
+++ b/slides/src/sync/crdt.ts
@@ -733,7 +733,10 @@ export class SyncState {
;(this.pending[nodeId] ??= []).push(op)
return
}
- dbg(nodeId, `set ${op.k}@${op.l},${op.a} APPLY ${JSON.stringify(op.v).slice(0, 40)}`)
+ // NB: v === undefined means "delete the key" (handled below), and
+ // JSON.stringify(undefined) is undefined — String() keeps this debug
+ // line from throwing on every property REMOVAL.
+ dbg(nodeId, `set ${op.k}@${op.l},${op.a} APPLY ${String(JSON.stringify(op.v)).slice(0, 40)}`)
if (op.el && op.k === 'html') {
const gen = this.txt[op.el]
if (gen && cmpReg([op.l, op.a], gen.sd) < 0) {