Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Sync: composite element identity so shared ids survive collab (v0.8.12)
nyblnet9cae893e49f3
diff --git a/CLAUDE.md b/CLAUDE.mdindex 5ccd75f4..04367f37 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -138,8 +138,14 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.default closed. Add new panel content under a section header and theaccordion picks it up automatically.- `src/sync/` — **bento-sync live collaboration** (design: docs/collab-design.md).- `crdt.ts` is the engine (pure data, runs in node too): per-(node,key) LWW- regs ordered by (lamport, actor); liveness = births vs tombs (an INS is a+ `crdt.ts` is the engine (pure data, runs in node too): element node+ identity is the COMPOSITE `slideId U+001F elementId` (elKey) — bare ids+ repeat across slides (the morph idiom), each per-slide copy is its own+ node, the format never sees composite keys, and cross-slide moves diff+ as del+ins (ord never re-parents); state+wire are versioned (SYNC_V:+ `v` in SyncStateJSON, `pv` on frames — pre-v2 saved sync state and+ frames are discarded, the file rejoins as a fresh adopt); per-(node,key)+ LWW regs ordered by (lamport, actor); liveness = births vs tombs (an INS is awhole-node ASSIGNMENT — it resurrects by out-stamping the tomb andsupersedes all older property regs; slide-ins = slide assignment + anindependent element-ins per member, member processing must NEVER bediff --git a/docs/collab-design.md b/docs/collab-design.mdindex 0d598af6..ee9fee4a 100644--- a/docs/collab-design.md+++ b/docs/collab-design.md@@ -86,6 +86,25 @@ raised to `max(local, seen)+1` on every received op. `(lamport, actorId)` isthe total order used for all conflict resolution. The document's `docId`(already shipped) is the room key.+**Node identity (v2, July 2026).** Slides key by their id, but element+nodes key by the **composite `slideId U+001F elementId`** (`elKey` in+`crdt.ts`). The bare element id is deliberately NOT unique across slides —+same-id-on-many-slides is the format's core morph idiom (`data-flip-id`+pairing; the starter deck's `sd-tile-*` cast rides through every slide) —+so v1's bare-id keying collapsed those copies into one node and replicas+diverged (one dropped a copy, the other kept it). Under composite keys+each per-slide copy is its own CRDT node; the document format is untouched+(payloads and materialized JSON carry bare ids; morph pairing happens at+render time and never sees CRDT keys). Consequences: an element node is+pinned to one slide for life, so a cross-slide move diffs as+`del(oldKey)+ins(newKey)` — concurrent moves of the same element to two+different slides duplicate it (both users keep their copy), and concurrent+moves onto the SAME slide collapse to one LWW winner (bare ids stay unique+*within* a slide — the editor invariant). The wire and saved sync state+are versioned (`SYNC_V = 2`: `pv` on frames, `v` in `SyncStateJSON`);+pre-v2 frames, room snapshots, and `doc.collab.sync` blobs are discarded+on sight — an old-scheme file simply rejoins as a never-synced adopt.+**Ops** (all JSON, ~one line each):```jsonc@@ -107,8 +126,9 @@ the total order used for all conflict resolution. The document's `docId`- `ins` — create with the full node payload and a fractional-index `ord`.- `del` — tombstone. Delete beats concurrent edits (standard, predictable).Tombstones live in the op log, never in the document JSON.-- `ord` — LWW on a node's fractional index (slide order, element z-order,- moves between slides carry a `sl` too).+- `ord` — LWW on a node's fractional index (slide order, element z-order).+ Since node identity v2, an element's parent slide is part of its key, so+ `ord` never re-parents — cross-slide moves are `del`+`ins`.**Order.** Slides and elements carry fractional-index keys (short base-62strings, jittered to avoid collisions — the Figma/Linear technique). Thediff --git a/scripts/test-sync.ts b/scripts/test-sync.tsindex d14c0298..d17042e2 100644--- a/scripts/test-sync.ts+++ b/scripts/test-sync.ts@@ -8,7 +8,9 @@// delivered in random interleavings, and at quiescence every replica's// materialized document AND sync state must be identical. Plus targeted// cases: delete-wins, undo-resurrection, move-vs-delete, RGA text merge,-// gap buffering, snapshot merge (file forks).+// gap buffering, snapshot merge (file forks), and same-element-id on many+// slides (the morph idiom — baseDoc carries a duplicated 'cast' id so every+// random run exercises composite element identity).import { SyncState, keyBetween, spreadKey, tokenize, materialize } from '../slides/src/sync/crdt.ts'import type { Op } from '../slides/src/sync/crdt.ts'@@ -127,6 +129,9 @@ function baseDoc(): Doc {{ id: 's1-t1', type: 'text', x: 100, y: 100, w: 600, h: 80, rotation: 0, opacity: 1, html: 'Hello <b>world</b>', fontSize: 40, fontFamily: 'x', fontWeight: 700, color: '#fff', align: 'left', valign: 'top', lineHeight: 1.2 },{ id: 's1-r1', type: 'shape', shape: 'rect', x: 200, y: 300, w: 200, h: 120, rotation: 0, opacity: 1, fill: '#FF9E8A', stroke: 'none', strokeWidth: 0, radius: 8 },{ id: 's1-r2', type: 'shape', shape: 'ellipse', x: 500, y: 300, w: 90, h: 90, rotation: 0, opacity: 1, fill: '#5E7699', stroke: 'none', strokeWidth: 0, radius: 0 },+ // 'cast' appears on s1 AND s2 — the id-continuity morph idiom+ // (starterdeck's sd-tile-*). Every random run exercises it.+ { id: 'cast', type: 'text', x: 900, y: 100, w: 300, h: 60, rotation: 0, opacity: 1, html: 'Morph <b>me</b>', fontSize: 28, fontFamily: 'x', fontWeight: 700, color: '#fff', align: 'left', valign: 'top', lineHeight: 1.2 },],},{@@ -136,6 +141,7 @@ function baseDoc(): Doc {notes: 'second',elements: [{ id: 's2-t1', type: 'text', x: 120, y: 120, w: 500, h: 60, rotation: 0, opacity: 1, html: 'Numbers & facts', fontSize: 30, fontFamily: 'x', fontWeight: 400, color: '#111', align: 'left', valign: 'top', lineHeight: 1.3 },+ { id: 'cast', type: 'text', x: 200, y: 500, w: 600, h: 120, rotation: 0, opacity: 1, html: 'Morph <b>me</b>', fontSize: 56, fontFamily: 'x', fontWeight: 700, color: '#111', align: 'left', valign: 'top', lineHeight: 1.2 },],},{ id: 's3', background: '#16273E', transition: 'fade', notes: '', elements: [] },@@ -237,11 +243,18 @@ function randomMutation(r: Replica, rnd: () => number): (doc: Doc) => void {break}case 6: {- // move element across slides+ // move element across slides. Target must not already carry the+ // element's id: bare ids are unique WITHIN a slide (format+ // invariant the editor upholds); duplication ACROSS slides is the+ // morph idiom and legal.const from = pick(slides.filter((x: any) => x.elements.length)) as anyif (!from || slides.length < 2) break- const to = pick(slides.filter((x: any) => x !== from)) as any- const [el] = from.elements.splice(Math.floor(rnd() * from.elements.length), 1)+ const i = Math.floor(rnd() * from.elements.length)+ const to = pick(+ slides.filter((x: any) => x !== from && !x.elements.some((e: any) => e.id === from.elements[i].id)),+ ) as any+ if (!to) break+ const [el] = from.elements.splice(i, 1)to.elements.splice(Math.floor(rnd() * (to.elements.length + 1)), 0, el)break}@@ -403,7 +416,7 @@ function randomMutation(r: Replica, rnd: () => number): (doc: Doc) => void {B.receive(res)ok(B.doc.slides.some((s: any) => s.id === 's2'), 'B saw the resurrection')ok(A.fingerprint() === B.fingerprint(), 'resurrection converged')- ok(B.doc.slides.find((s: any) => s.id === 's2').elements.length === 1, 'slide content restored')+ ok(B.doc.slides.find((s: any) => s.id === 's2').elements.length === 2, 'slide content restored')}{console.log('move-out vs slide-delete cascade…')@@ -524,5 +537,141 @@ function randomMutation(r: Replica, rnd: () => number): (doc: Doc) => void {ok(A.fingerprint() === B.fingerprint(), 'log catch-up converged')}+// ---------------------------------------------------------------------------+// duplicated element ids across slides (the morph idiom) — regression for+// the v1 bare-id identity collapse that dropped one copy per replica+// ---------------------------------------------------------------------------+const castCount = (d: Doc) =>+ d.slides.filter((s: any) => s.elements.some((e: any) => e.id === 'cast')).length+{+ console.log('dup id across slides: adopt + structural op keeps every copy…')+ // the launch-blocker repro: both replicas adopt the same file with 'cast'+ // on s1 AND s2; any structural op must not evict either copy anywhere+ const A = new Replica('A')+ const B = new Replica('B')+ const ops = A.mutate((d) =>+ d.slides[0].elements.push({ id: 'A-extra', type: 'shape', shape: 'rect', x: 3, y: 3, w: 10, h: 10, rotation: 0, opacity: 1, fill: '#123', stroke: 'none', strokeWidth: 0, radius: 0 }),+ )+ B.receive(ops)+ ok(A.fingerprint() === B.fingerprint(), 'adopt-dup structural op converged')+ ok(castCount(A.doc) === 2, `A keeps cast on both slides (${castCount(A.doc)})`)+ ok(castCount(B.doc) === 2, `B keeps cast on both slides (${castCount(B.doc)})`)+}+{+ console.log('dup id: concurrent edits land on the right copy…')+ const A = new Replica('A')+ const B = new Replica('B')+ const castOn = (d: Doc, sid: string) =>+ d.slides.find((s: any) => s.id === sid).elements.find((e: any) => e.id === 'cast')+ const ea = A.mutate((d) => (castOn(d, 's1').x = 111))+ const eb = B.mutate((d) => (castOn(d, 's2').x = 222))+ A.receive(eb)+ B.receive(ea)+ ok(A.fingerprint() === B.fingerprint(), 'per-copy edits converged')+ ok(castOn(A.doc, 's1').x === 111 && castOn(A.doc, 's2').x === 222, 'each copy took its own edit on A')+ ok(castOn(B.doc, 's1').x === 111 && castOn(B.doc, 's2').x === 222, 'each copy took its own edit on B')+}+{+ console.log('dup id: concurrent RGA text edits stay per-copy…')+ const A = new Replica('A')+ const B = new Replica('B')+ const castOn = (d: Doc, sid: string) =>+ d.slides.find((s: any) => s.id === sid).elements.find((e: any) => e.id === 'cast')+ const ea = A.mutate((d) => (castOn(d, 's1').html = 'ONE ' + castOn(d, 's1').html))+ const eb = B.mutate((d) => (castOn(d, 's2').html = castOn(d, 's2').html + ' TWO'))+ A.receive(eb)+ B.receive(ea)+ ok(A.fingerprint() === B.fingerprint(), 'per-copy text edits converged')+ ok(castOn(A.doc, 's1').html === 'ONE Morph <b>me</b>', `s1 copy text: "${castOn(A.doc, 's1').html}"`)+ ok(castOn(A.doc, 's2').html === 'Morph <b>me</b> TWO', `s2 copy text: "${castOn(A.doc, 's2').html}"`)+}+{+ console.log('dup id: created by diff (paste-with-same-id) replicates…')+ const A = new Replica('A')+ const B = new Replica('B')+ const mk = () => ({ id: 'dup-x', type: 'shape', shape: 'rect', x: 7, y: 7, w: 20, h: 20, rotation: 0, opacity: 1, fill: '#777', stroke: 'none', strokeWidth: 0, radius: 0 })+ const o1 = A.mutate((d) => d.slides[0].elements.push(mk()))+ const o2 = A.mutate((d) => d.slides[1].elements.push({ ...mk(), x: 77 }))+ B.receive(o1)+ B.receive(o2)+ ok(A.fingerprint() === B.fingerprint(), 'diff-created dup converged')+ const on = (d: Doc, sid: string) =>+ d.slides.find((s: any) => s.id === sid).elements.find((e: any) => e.id === 'dup-x')+ ok(on(B.doc, 's1')?.x === 7 && on(B.doc, 's2')?.x === 77, 'both copies present on B with their own props')+}+{+ console.log('dup id: delete one copy, edit the other concurrently…')+ const A = new Replica('A')+ const B = new Replica('B')+ const del = A.mutate((d) => {+ const s1 = d.slides.find((s: any) => s.id === 's1')+ s1.elements = s1.elements.filter((e: any) => e.id !== 'cast')+ })+ const edit = B.mutate((d) => {+ const c = d.slides.find((s: any) => s.id === 's2').elements.find((e: any) => e.id === 'cast')+ c.fill = undefined+ c.x = 999+ })+ A.receive(edit)+ B.receive(del)+ ok(A.fingerprint() === B.fingerprint(), 'delete-one-copy converged')+ ok(!A.doc.slides.find((s: any) => s.id === 's1').elements.some((e: any) => e.id === 'cast'), 's1 copy gone')+ ok(A.doc.slides.find((s: any) => s.id === 's2').elements.find((e: any) => e.id === 'cast').x === 999, 's2 copy edited')+}+{+ console.log('dup id: concurrent moves onto one slide converge…')+ // A moves s1's cast → s3 while B moves s2's cast → s3: both replicas must+ // agree (the two copies collapse to one under the within-slide id+ // invariant — documented degradation, never divergence)+ const A = new Replica('A')+ const B = new Replica('B')+ const mv = (d: Doc, from: string) => {+ const f = d.slides.find((s: any) => s.id === from)+ const s3 = d.slides.find((s: any) => s.id === 's3')+ const i = f.elements.findIndex((e: any) => e.id === 'cast')+ const [el] = f.elements.splice(i, 1)+ s3.elements.push(el)+ }+ const ma = A.mutate((d) => mv(d, 's1'))+ const mb = B.mutate((d) => mv(d, 's2'))+ A.receive(mb)+ B.receive(ma)+ ok(A.fingerprint() === B.fingerprint(), 'colliding moves converged')+ ok(+ A.doc.slides.find((s: any) => s.id === 's3').elements.filter((e: any) => e.id === 'cast').length === 1,+ 'exactly one cast on s3 after collision',+ )+}+{+ console.log('dup id: snapshot fork merge keeps both copies…')+ const A = new Replica('A')+ const B = new Replica('B')+ A.mutate((d) => (d.slides.find((s: any) => s.id === 's1').elements.find((e: any) => e.id === 'cast').y = 41))+ B.mutate((d) => (d.slides.find((s: any) => s.id === 's2').elements.find((e: any) => e.id === 'cast').y = 42))+ const aDoc = JSON.parse(JSON.stringify(A.doc))+ const aState = JSON.parse(JSON.stringify(A.state.toJSON()))+ const bDoc = JSON.parse(JSON.stringify(B.doc))+ const bState = JSON.parse(JSON.stringify(B.state.toJSON()))+ A.state.mergeSnapshot(A.doc, bDoc, bState)+ A.shadow = JSON.stringify(A.doc)+ B.state.mergeSnapshot(B.doc, aDoc, aState)+ B.shadow = JSON.stringify(B.doc)+ ok(A.fingerprint() === B.fingerprint(), 'dup-id fork merge converged')+ ok(castCount(A.doc) === 2, 'both copies survive the fork merge')+ const y = (sid: string) =>+ A.doc.slides.find((s: any) => s.id === sid).elements.find((e: any) => e.id === 'cast').y+ ok(y('s1') === 41 && y('s2') === 42, 'fork edits landed on their own copies')+}+{+ console.log('pre-v2 saved state and snapshots are discarded…')+ const A = new Replica('A')+ const legacy = { lamport: 9, vv: { Z: 4 }, regs: { 'cast x': [9, 'Z'] }, pos: {}, births: {}, tombs: {}, txt: {}, stash: {}, limbo: {} }+ const s = SyncState.fromJSON('A', legacy as any)+ ok(s.lamport === 0 && Object.keys(s.regs).length === 0, 'v1 state ignored by fromJSON')+ const before = A.fingerprint()+ const r = A.state.mergeSnapshot(A.doc, baseDoc(), legacy as any)+ ok(!r.changed && A.fingerprint() === before, 'v1 snapshot ignored by mergeSnapshot')+}+console.log(failures === 0 ? `\nALL PASS (${checks} checks)` : `\n${failures} FAILURES of ${checks} checks`)process.exit(failures ? 1 : 0)diff --git a/slides/package.json b/slides/package.jsonindex 15b7788e..c84abd57 100644--- a/slides/package.json+++ b/slides/package.json@@ -1,7 +1,7 @@{"name": "bento-slides","private": true,- "version": "0.8.11",+ "version": "0.8.12","type": "module","scripts": {"dev": "vite",diff --git a/slides/src/sync/crdt.ts b/slides/src/sync/crdt.tsindex ad7f9fc0..75fde1fd 100644--- a/slides/src/sync/crdt.ts+++ b/slides/src/sync/crdt.ts@@ -3,8 +3,15 @@// node convergence rig (scripts/test-sync.ts, node --experimental-strip-types).//// Design: docs/collab-design.md. Summary of the algebra:-// - identity: every slide/element id is a register namespace; `@doc` is the-// document's own namespace.+// - identity: slides key by their id; ELEMENTS key by the composite+// `slideId U+001F elementId` (elKey) — the same element id on many+// slides is the format's core morph idiom (data-flip-id pairing), so+// each per-slide copy must be its own CRDT node. The doc format never+// sees composite keys (element `id` stays the bare morph id); they+// live in this layer's registers and on the wire. A cross-slide move+// changes the key, so the differ emits del(old)+ins(new) — concurrent+// moves of one element to two slides duplicate it (both users keep+// their copy) instead of racing. `@doc` is the document's namespace.// - per-(node, key) LWW registers ordered by (lamport, actorId).// - order + parentage: one `pos` register per node (fractional base-62 key// + parent id) — arrays in the doc are always *materialized* pos-order.@@ -30,6 +37,22 @@ import type { BentoDoc, Slide, SlideElement } from '../model'export const DOC_NODE = '@doc'+/**+ * Sync format version — stamped into SyncStateJSON (`v`) and every wire+ * frame (`pv`). v1 keyed elements by bare id, which collapsed the same id+ * appearing on multiple slides (the morph idiom); v2 state/ops are keyed+ * by composite element keys and are NOT interoperable, so v1 saved state+ * and v1 frames are discarded on sight.+ */+export const SYNC_V = 2++/** composite element node key: slide id + separator + element id. U+001F+ * never appears in model ids (uid() emits [a-z0-9-]; generators use ASCII). */+const SEP = '\u001f'+export const elKey = (sl: string, el: string): string => sl + SEP + el+export const keySlide = (key: string): string => key.slice(0, key.indexOf(SEP))+export const keyEl = (key: string): string => key.slice(key.indexOf(SEP) + 1)+/* eslint-disable no-console */const dbg = (id: string, msg: string) => {const g = globalThis as unknown as { __dbgEl?: string; __dbgTag?: string }@@ -101,7 +124,7 @@ export interface OpBase {}export interface SetOp extends OpBase {op: 'set'- /** node id: element id, slide id, or absent → @doc */+ /** node id: composite element key (elKey), slide id, or absent → @doc */el?: stringsl?: string/** property key; doc-level supports dotted sub-keys 'assets.<k>' */@@ -112,30 +135,38 @@ export interface SetOp extends OpBase {export interface InsOp extends OpBase {op: 'ins'kind: 'slide' | 'element'+ /** slide id, or composite element key (its slide part = `sl`) */id: string/** parent: slide id for elements, ignored for slides */sl?: stringord: string+ /** doc-shaped payload — element ids inside stay bare (the format never+ * sees composite keys) */node: Slide | SlideElement}export interface DelOp extends OpBase {op: 'del'kind: 'slide' | 'element'+ /** slide id, or composite element key */id: string- /** slide delete: element ids the deleter saw inside (cascade tombstones) */+ /** slide delete: composite keys of the elements the deleter saw inside+ * (cascade tombstones) */cas?: string[]}export interface OrdOp extends OpBase {op: 'ord'kind: 'slide' | 'element'+ /** slide id, or composite element key */id: stringord: string- /** elements: (possibly new) parent slide */+ /** elements: the parent slide (constant for a given key — moves across+ * slides change the key itself via del+ins) */sl?: string}/** M3 — text RGA delta for one element's html. */export interface TxtOp extends OpBase {op: 'txt'+ /** composite element key */el: string/** seed reference: (l,a) of the RGA base this delta applies to */sd: Reg@@ -171,6 +202,8 @@ export interface TxtState {}export interface SyncStateJSON {+ /** sync format version (SYNC_V) — mismatched saved state is discarded */+ v: numberlamport: numbervv: Record<string, number>regs: Record<string, Reg>@@ -234,6 +267,7 @@ export class SyncState {toJSON(): SyncStateJSON {return {+ v: SYNC_V,lamport: this.lamport,vv: this.vv,regs: this.regs,@@ -248,6 +282,7 @@ export class SyncState {static fromJSON(actor: string, j: SyncStateJSON): SyncState {const s = new SyncState(actor)+ if (j.v !== SYNC_V) return s // pre-v2 state keyed elements by bare id — unusables.lamport = j.lamports.vv = j.vv ?? {}s.seq = s.vv[actor] ?? 0@@ -281,7 +316,8 @@ export class SyncState {if (!this.pos[sl.id]) this.pos[sl.id] = { p: DOC_NODE, o: spreadKey(i, ns), r: [0, ''] }const ne = sl.elements.lengthsl.elements.forEach((el, j) => {- if (!this.pos[el.id]) this.pos[el.id] = { p: sl.id, o: spreadKey(j, ne), r: [0, ''] }+ const k = elKey(sl.id, el.id)+ if (!this.pos[k]) this.pos[k] = { p: sl.id, o: spreadKey(j, ne), r: [0, ''] }})})}@@ -321,27 +357,29 @@ export class SyncState {this.regs[`${DOC_NODE} ${k}`] = [o.l, o.a]}- // ---- slides by id (elements matched globally — they move across slides)+ // ---- slides by id; elements by COMPOSITE key (slide + bare id) — the+ // same element id on many slides is the morph idiom, each copy is its+ // own node. A cross-slide move therefore diffs as del(old)+ins(new).const bSlides = new Map(before.slides.map((s) => [s.id, s]))const aSlides = new Map(after.slides.map((s) => [s.id, s]))const bEls = new Map<string, { sl: string; el: SlideElement }>()const aEls = new Map<string, { sl: string; el: SlideElement }>()- before.slides.forEach((s) => s.elements.forEach((el) => bEls.set(el.id, { sl: s.id, el })))- after.slides.forEach((s) => s.elements.forEach((el) => aEls.set(el.id, { sl: s.id, el })))+ before.slides.forEach((s) => s.elements.forEach((el) => bEls.set(elKey(s.id, el.id), { sl: s.id, el })))+ after.slides.forEach((s) => s.elements.forEach((el) => aEls.set(elKey(s.id, el.id), { sl: s.id, el })))// deleted slides (cascade the elements the deleter saw, minus survivors)for (const [id, sl] of bSlides) {if (aSlides.has(id)) continue- const cas = sl.elements.map((e) => e.id).filter((eid) => !aEls.has(eid))+ const cas = sl.elements.map((e) => elKey(id, e.id)).filter((k) => !aEls.has(k))const o = push<DelOp>({ ...this.stamp(), op: 'del', kind: 'slide', id, cas })this.tombs[id] = [o.l, o.a]this.stashNode(sl as unknown as Record<string, unknown>, id)- cas.forEach((eid) => {- this.tombs[eid] = [o.l, o.a]- const node = sl.elements.find((e) => e.id === eid)- if (node) this.stashNode(node as unknown as Record<string, unknown>, eid)- delete this.limbo[eid]- delete this.txt[eid] // local tomb is the freshest stamp — always out-ranks+ cas.forEach((ek) => {+ this.tombs[ek] = [o.l, o.a]+ const node = sl.elements.find((e) => elKey(id, e.id) === ek)+ if (node) this.stashNode(node as unknown as Record<string, unknown>, ek)+ delete this.limbo[ek]+ delete this.txt[ek] // local tomb is the freshest stamp — always out-ranks})}// inserted (or resurrected) slides@@ -358,10 +396,11 @@ export class SyncState {delete this.stash[id] // fresh birth voids parked values (receivers do this in replayStash)const ne = sl.elements.lengthsl.elements.forEach((el, j) => {- this.births[el.id] = [o.l, o.a]- this.pos[el.id] = { p: id, o: spreadKey(j, ne), r: [o.l, o.a] }- delete this.txt[el.id] // rebirth voids stale text generations- delete this.stash[el.id]+ const k = elKey(id, el.id)+ this.births[k] = [o.l, o.a]+ this.pos[k] = { p: id, o: spreadKey(j, ne), r: [o.l, o.a] }+ delete this.txt[k] // rebirth voids stale text generations+ delete this.stash[k]})}// kept slides: prop diffs@@ -396,7 +435,7 @@ export class SyncState {const prev = bEls.get(id)if (!prev) {if (this.births[id] && !this.dead(id) && this.pos[id]?.p === sl) continue // came with a fresh slide ins above- const sib = aSlides.get(sl)!.elements.map((e) => e.id)+ const sib = aSlides.get(sl)!.elements.map((e) => elKey(sl, e.id))const ord = this.keyAround(sl, sib, sib.indexOf(id))const o = push<InsOp>({ ...this.stamp(), op: 'ins', kind: 'element', id, sl, ord, node: clone(el) })this.births[id] = [o.l, o.a]@@ -423,17 +462,13 @@ export class SyncState {if (k === 'html') delete this.txt[id] // LWW reset wins over RGA state}}- if (prev.sl !== sl) {- const sib = aSlides.get(sl)!.elements.map((e) => e.id)- const ord = this.keyAround(sl, sib, sib.indexOf(id))- const o = push<OrdOp>({ ...this.stamp(), op: 'ord', kind: 'element', id, sl, ord })- this.pos[id] = { p: sl, o: ord, r: [o.l, o.a] }- }+ // no cross-slide move branch: the composite key IS (slide, id), so a+ // moved element always lands in the delete/insert passes above}// element order within each surviving slide (all ids — see slide pass)for (const [id, sl] of aSlides) {if (!bSlides.has(id)) continue- this.diffOrder(sl.elements.map((e) => e.id), id, 'element', push)+ this.diffOrder(sl.elements.map((e) => elKey(id, e.id)), id, 'element', push)}// remote ops that pended awaiting a seed can resolve against seeds this@@ -624,12 +659,12 @@ export class SyncState {private findSlide(doc: BentoDoc, id: string): Slide | undefined {return doc.slides.find((s) => s.id === id)}- private findEl(doc: BentoDoc, id: string): SlideElement | undefined {- for (const s of doc.slides) {- const el = s.elements.find((e) => e.id === id)- if (el) return el- }- return this.limbo[id]+ /** composite-key lookup: an element node only ever lives on its key's+ * slide (or in limbo) — the same bare id on other slides is other nodes */+ private findEl(doc: BentoDoc, key: string): SlideElement | undefined {+ const s = this.findSlide(doc, keySlide(key))+ const bare = keyEl(key)+ return s?.elements.find((e) => e.id === bare) ?? this.limbo[key]}private applySet(doc: BentoDoc, op: SetOp, res: ApplyResult) {@@ -706,9 +741,9 @@ export class SyncState {const src = op.node as Slidethis.insertSlideLevel(doc, op.id, op.ord, src, stamp, res)const ne = src.elements.length- src.elements.forEach((e, j) => this.insertElement(doc, e.id, op.id, spreadKey(j, ne), e, stamp, res))+ src.elements.forEach((e, j) => this.insertElement(doc, elKey(op.id, e.id), op.id, spreadKey(j, ne), e, stamp, res))this.drainPending(doc, op.id)- src.elements.forEach((e) => this.drainPending(doc, e.id))+ src.elements.forEach((e) => this.drainPending(doc, elKey(op.id, e.id)))} else {this.insertElement(doc, op.id, op.sl!, op.ord, op.node as SlideElement, stamp, res)this.drainPending(doc, op.id)@@ -857,23 +892,22 @@ export class SyncState {const bump = (id: string) => {if (!this.tombs[id] || newer(op.l, op.a, this.tombs[id])) this.tombs[id] = [op.l, op.a]}- const removeElement = (id: string) => {- dbg(id, `removeElement (tomb ${JSON.stringify(this.tombs[id])})`)- const lb = this.limbo[id]- if (lb) this.stashNode(lb as unknown as Record<string, unknown>, id)- delete this.limbo[id]+ const removeElement = (key: string) => {+ dbg(key, `removeElement (tomb ${JSON.stringify(this.tombs[key])})`)+ const lb = this.limbo[key]+ if (lb) this.stashNode(lb as unknown as Record<string, unknown>, key)+ delete this.limbo[key]// a tombstone kills only generations it out-ranks; an out-ranking gen// survives the node's death (pend-on-dead replicas rebuild it on// resurrection from their buffered ops — this side keeps it directly)- const g = this.txt[id]- if (g && cmpReg(this.tombs[id] ?? [0, ''], g.sd) > 0) delete this.txt[id]- for (const s of doc.slides) {- const i = s.elements.findIndex((e) => e.id === id)- if (i >= 0) {- this.stashNode(s.elements[i] as unknown as Record<string, unknown>, id)- s.elements.splice(i, 1)- return- }+ const g = this.txt[key]+ if (g && cmpReg(this.tombs[key] ?? [0, ''], g.sd) > 0) delete this.txt[key]+ const s = this.findSlide(doc, keySlide(key))+ const bare = keyEl(key)+ const i = s ? s.elements.findIndex((e) => e.id === bare) : -1+ if (s && i >= 0) {+ this.stashNode(s.elements[i] as unknown as Record<string, unknown>, key)+ s.elements.splice(i, 1)}}bump(op.id)@@ -889,9 +923,10 @@ export class SyncState {if (i >= 0) {const [gone] = doc.slides.splice(i, 1)this.stashNode(gone as unknown as Record<string, unknown>, op.id)- // survivors (concurrently inserted/moved-in) park in limbo+ // survivors (concurrently inserted) park in limbo under their keyfor (const el of gone.elements) {- if (!this.dead(el.id)) this.limbo[el.id] = el+ const k = elKey(op.id, el.id)+ if (!this.dead(k)) this.limbo[k] = el}}}@@ -1002,39 +1037,24 @@ export class SyncState {}doc.slides.sort((s1, s2) => cmp(s1.id, s2.id))const slideById = new Map(doc.slides.map((s) => [s.id, s]))- for (const [id, el] of Object.entries(this.limbo)) {- const p = this.pos[id]?.p- if (p && slideById.has(p) && !this.dead(p) && !this.dead(id)) {+ for (const [key, el] of Object.entries(this.limbo)) {+ const p = this.pos[key]?.p+ if (p && slideById.has(p) && !this.dead(p) && !this.dead(key)) {const dest = slideById.get(p)!- if (!dest.elements.some((e) => e.id === id)) dest.elements.push(el)- else dbg(id, `limbo-restore DROP dup x=${(el as any).x}`)- delete this.limbo[id]- this.drainPending(doc, id)- }- }- // pass 1: relocate every element to its winning parent (no sorting yet —- // a sort-as-you-go would miss elements pushed into already-visited slides)- for (const sl of doc.slides) {- for (let i = sl.elements.length - 1; i >= 0; i--) {- const el = sl.elements[i]- const p = this.pos[el.id]?.p- if (p && p !== sl.id) {- sl.elements.splice(i, 1)- const dest = slideById.get(p)- if (dest && !this.dead(p)) {- if (!dest.elements.some((e) => e.id === el.id)) dest.elements.push(el)- else dbg(el.id, `remat DROP dup evicted copy x=${(el as any).x}`)- } else this.limbo[el.id] = el- }+ if (!dest.elements.some((e) => e.id === el.id)) dest.elements.push(el)+ else dbg(key, `limbo-restore DROP dup x=${(el as any).x}`)+ delete this.limbo[key]+ this.drainPending(doc, key)}}- // pass 2: sort by pos key and dedupe (a node whose data travelled two- // routes can transiently duplicate — keep the first, drop the rest)+ // elements never relocate across slides (the composite key pins them to+ // one slide for life) — only sort by pos key and dedupe within the slide+ // (a node whose data travelled two routes can transiently duplicate)for (const sl of doc.slides) {- sl.elements.sort((e1, e2) => cmp(e1.id, e2.id))+ sl.elements.sort((e1, e2) => cmp(elKey(sl.id, e1.id), elKey(sl.id, e2.id)))sl.elements = sl.elements.filter((e, i) => {const dup = i > 0 && e.id === sl.elements[i - 1].id- if (dup) dbg(e.id, `remat dedupe DROP x=${(e as any).x}`)+ if (dup) dbg(elKey(sl.id, e.id), `remat dedupe DROP x=${(e as any).x}`)return !dup})}@@ -1049,6 +1069,7 @@ export class SyncState {*/mergeSnapshot(doc: BentoDoc, rdoc: BentoDoc, rstate: SyncStateJSON): ApplyResult {const res: ApplyResult = { changed: false, structure: false }+ if (rstate.v !== SYNC_V) return res // pre-v2 snapshot: bare-id keys, unusablethis.lamport = Math.max(this.lamport, rstate.lamport)for (const [a, s] of Object.entries(rstate.vv ?? {})) {if ((this.vv[a] ?? 0) < s) {@@ -1095,15 +1116,17 @@ export class SyncState {doc.slides.splice(i, 1)this.stashNode(sl as unknown as Record<string, unknown>, sl.id)for (const el of sl.elements) {- if (!this.dead(el.id)) this.limbo[el.id] = el- else this.stashNode(el as unknown as Record<string, unknown>, el.id)+ const k = elKey(sl.id, el.id)+ if (!this.dead(k)) this.limbo[k] = el+ else this.stashNode(el as unknown as Record<string, unknown>, k)}res.structure = trueres.changed = true} else {for (let j = sl.elements.length - 1; j >= 0; j--) {- if (this.dead(sl.elements[j].id)) {- this.stashNode(sl.elements[j] as unknown as Record<string, unknown>, sl.elements[j].id)+ const k = elKey(sl.id, sl.elements[j].id)+ if (this.dead(k)) {+ this.stashNode(sl.elements[j] as unknown as Record<string, unknown>, k)sl.elements.splice(j, 1)res.structure = trueres.changed = true@@ -1117,21 +1140,21 @@ export class SyncState {delete this.limbo[id]}}- // adopt remote nodes we don't have (slides first, then elements;- // remote limbo nodes count — they're invisible but their data is real)+ // adopt remote nodes we don't have (slides first, then elements, both+ // keyed composite; remote limbo nodes count — they're invisible but+ // their data is real)const rEls = new Map<string, SlideElement>()- rdoc.slides.forEach((s) => s.elements.forEach((e) => rEls.set(e.id, e)))- for (const [id, el] of Object.entries(rstate.limbo ?? {})) if (!rEls.has(id)) rEls.set(id, el)+ rdoc.slides.forEach((s) => s.elements.forEach((e) => rEls.set(elKey(s.id, e.id), e)))+ for (const [key, el] of Object.entries(rstate.limbo ?? {})) if (!rEls.has(key)) rEls.set(key, el)for (const sl of rdoc.slides) {if (this.dead(sl.id) || this.findSlide(doc, sl.id)) continueconst copy = clone(sl)- const inDoc = (id: string) => doc.slides.some((s) => s.elements.some((x) => x.id === id))copy.elements = copy.elements- .filter((e) => !this.dead(e.id) && !inDoc(e.id))+ .filter((e) => !this.dead(elKey(sl.id, e.id))).map((e) => {- const lb = this.limbo[e.id]+ const lb = this.limbo[elKey(sl.id, e.id)]if (lb) {- delete this.limbo[e.id]+ delete this.limbo[elKey(sl.id, e.id)]return lb}return e@@ -1140,12 +1163,12 @@ export class SyncState {res.changed = trueres.structure = true}- for (const [id, el] of rEls) {- if (this.dead(id) || this.findEl(doc, id)) continue- const p = this.pos[id]?.p+ for (const [key, el] of rEls) {+ if (this.dead(key) || this.findEl(doc, key)) continue+ const p = this.pos[key]?.pconst host = p ? this.findSlide(doc, p) : undefinedif (host && !this.dead(host.id)) host.elements.push(clone(el))- else this.limbo[id] = clone(el)+ else this.limbo[key] = clone(el)res.changed = trueres.structure = true}diff --git a/slides/src/sync/session.ts b/slides/src/sync/session.tsindex 0067fa28..6bc0b984 100644--- a/slides/src/sync/session.ts+++ b/slides/src/sync/session.ts@@ -18,7 +18,7 @@import type { Store } from '../store'import type { BentoDoc, Slide } from '../model'import { uid } from '../model'-import { SyncState, type Op } from './crdt'+import { SyncState, SYNC_V, type Op } from './crdt'import { mintCollab } from './online'export interface PresenceInfo {@@ -49,7 +49,12 @@ interface SnapFrame {doc: BentoDocstate: import('./crdt').SyncStateJSON}-export type Frame = HelloFrame | OpsFrame | NeedFrame | PresFrame | ByeFrame | SnapFrame+/** every frame is stamped with the sync protocol version (`pv: SYNC_V`) by+ * send(); frames without the current pv (old builds, stale relay logs from+ * the bare-id era) are dropped in onFrame — their keys don't interoperate */+export type Frame = (HelloFrame | OpsFrame | NeedFrame | PresFrame | ByeFrame | SnapFrame) & {+ pv?: number+}export interface Transport {readonly kind: string@@ -133,7 +138,7 @@ export class SyncSession {// just works. Dormant (on:false) until the Share button flips it.if (!doc.collab) doc.collab = mintCollab()const saved = doc.collab?.sync- if (saved) {+ if (saved && saved.v === SYNC_V) {// this file was saved during/after a shared session: restore the CRDT// state so our offline edits carry real registers, remote replay// dedups by version vector, and a state-based snapshot exchange can@@ -141,6 +146,9 @@ export class SyncSession {this.state = SyncState.fromJSON(this.actor, JSON.parse(JSON.stringify(saved)))this.forkPending = true} else {+ // no saved state, or state from the pre-composite-key era (v1) — that+ // scheme collapsed same-id-on-many-slides, so it is discarded and the+ // file joins as a never-synced adopt (deterministic keys from the doc)this.state = new SyncState(this.actor)}this.state.adopt(doc)@@ -208,6 +216,7 @@ export class SyncSession {// --- remote frames --------------------------------------------------------private onFrame(f: Frame) {+ if (f.pv !== SYNC_V) return // old-protocol frame (bare-id keys) — ignoreif (f.a === this.actor) returnswitch (f.t) {case 'hello': {@@ -364,6 +373,7 @@ export class SyncSession {/** merge a remote snapshot (relay replay for far-behind joiners) */applySnapshot(rdoc: BentoDoc, rstate: import('./crdt').SyncStateJSON) {+ if (!rstate || rstate.v !== SYNC_V) return // pre-v2 room snapshot — unusablethis.flush()this.applying = truetry {@@ -413,7 +423,8 @@ export class SyncSession {// --- plumbing -------------------------------------------------------------private send(frame: Frame) {- for (const tr of this.transports) tr.send(frame)+ const stamped = { ...frame, pv: SYNC_V }+ for (const tr of this.transports) tr.send(stamped)}private broadcast(frame: Frame) {