Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

spaces: sync doc-level keys by allowlist (bind the field map) (#686)

nyblnet1bde5c38b98a
diff --git a/scripts/test-sync-spaces.ts b/scripts/test-sync-spaces.ts
index 63d13d7b..f73cd797 100644
--- a/scripts/test-sync-spaces.ts
+++ b/scripts/test-sync-spaces.ts
@@ -40,6 +40,7 @@ import { effectiveParents, descendantsOf } from '../spaces/src/model.ts'
// (`export … from` re-exports without binding the name locally, and this rig
// uses it below — so it is imported and re-exported.)
import { SPACES_SHAPE } from '../spaces/src/sync/crdt.ts'
+import { SPACES_FIELDS } from '../spaces/src/docclass.ts'
export { SPACES_SHAPE }
class SpacesSync extends SyncEngine {
constructor(actor: string) { super(actor, SPACES_SHAPE) }
@@ -310,6 +311,83 @@ console.log('')
ok(JSON.stringify(a.doc) === JSON.stringify(b.doc), 'both replicas end byte-identical')
}
+// ---------------------------------------------------------------------------
+// DOC-LEVEL KEYS GO BY ALLOWLIST (kernel #683, bound in spaces/src/sync/crdt.ts).
+//
+// SPACES_SHAPE carries the real field map, so a replica EMITS only the doc keys
+// docclass.ts classes 'content'. Every non-content key spaces has today is also
+// on the kernel's skipDoc floor, so the half that is NEW here is the UNKNOWN key:
+// a field a newer build added, opened in this build, is never echoed to the
+// room — and one that ARRIVES from such a peer is kept in this file but, again,
+// never re-emitted. Not emitting a key is not deleting it.
+//
+// Mutation: drop SPACES_FIELDS from the shape() call → the unknown-key rows go
+// red (the shipped denylist emits anything it does not explicitly skip).
+// ---------------------------------------------------------------------------
+{
+ type R = Record<string, unknown>
+ const docSets = (ops: unknown[]): string[] => (ops as R[])
+ .filter((o) => o.op === 'set' && o.el === undefined && o.sl === undefined)
+ .map((o) => String(o.k).split('.')[0])
+ const NON_CONTENT = Object.keys(SPACES_FIELDS).filter((k) => (SPACES_FIELDS as R)[k] !== 'content')
+
+ ok(SPACES_SHAPE.classes === SPACES_FIELDS, 'the spaces sync shape is bound to the real field map (docclass.ts)')
+
+ // SENDER — every field at once, so a row names which key leaked
+ {
+ const a = new SpacesSync('al-a')
+ const d = clone(baseDoc()) as unknown as R
+ a.adopt(d as never)
+ const before = clone(d)
+ d.title = 'renamed'
+ d.footnotes = { n1: 'a note' }
+ d.design = 'ledger'
+ d.periods = { s1: { label: 'Sprint 1', from: '2026-10-01', to: '2026-10-14', base: {} } }
+ for (const k of NON_CONTENT) d[k] = k === 'collab' ? { room: 'wX', key: 'LEAK' } : `changed-${k}`
+ d.futureField = 'a field a newer build added'
+ const sent = docSets(a.diff(before as never, d as never, { text: true }) as never)
+ ok(['title', 'footnotes', 'design', 'periods'].every((k) => sent.includes(k)),
+ `content keys are emitted, the per-key periods map included (${sent.join(', ')})`)
+ const leaked = NON_CONTENT.filter((k) => sent.includes(k))
+ ok(leaked.length === 0, `no non-content key is emitted (${NON_CONTENT.join(', ')}${leaked.length ? ` — LEAKED ${leaked.join(', ')}` : ''})`)
+ ok(!sent.includes('futureField'), 'an UNKNOWN key (a newer build\'s field opened here) is never emitted')
+ const out = a.outboundDoc(clone(d) as never) as R
+ ok(out.futureField === undefined && NON_CONTENT.every((k) => out[k] === undefined || k === 'collab' && out[k] === undefined),
+ 'the snapshot doc a replica sends carries neither the unknown key nor any non-content key')
+ }
+
+ // RECEIVER — an older, unbound build emits whatever it does not skip
+ {
+ const OLD = new SyncEngine('al-old', { ...SPACES_SHAPE, classes: undefined })
+ const od = clone(baseDoc()) as unknown as R
+ OLD.adopt(od as never)
+ const a = new SpacesSync('al-recv')
+ const ad = clone(od)
+ a.adopt(ad as never)
+ const before = clone(od)
+ od.futureField = 'from a newer build'
+ od.title = 'old peer renamed it'
+ a.apply(ad as never, OLD.diff(before as never, od as never, { text: true }) as never)
+ ok(ad.futureField === 'from a newer build' && ad.title === 'old peer renamed it',
+ 'an unknown key from a peer is APPLIED here (kept in this file), beside the content edit')
+ const regs = Object.keys((a.toJSON() as { regs: R }).regs)
+ ok(!regs.some((k) => k.endsWith(' futureField')) && regs.some((k) => k.endsWith(' title')),
+ '…and never echoed: the stamped state carries the title register, not the unknown one')
+ // this replica's next edits: delivered to the old peer, in order (an
+ // undelivered op would leave a sequence gap the engine rightly holds back)
+ const step = (change: R): unknown[] => {
+ const prev = clone(ad)
+ Object.assign(ad, change)
+ const ops = a.diff(prev as never, ad as never, { text: true }) as unknown[]
+ OLD.apply(od as never, ops as never)
+ return ops
+ }
+ ok(!docSets(step({ title: 'again' })).includes('futureField'), '…nor does this replica\'s next diff re-send it')
+ step({ design: 'riso' })
+ ok(od.design === 'riso' && od.title === 'again', 'a bound and an unbound replica still converge on content (mixed-version room)')
+ }
+}
+
const STRICT = process.env.STRICT === '1'
if (STRICT) {
ok(worst.dupBlockId === 0, `no merge produced the same block id on two pages (worst ${worst.dupBlockId})`)
diff --git a/spaces/CHANGELOG.md b/spaces/CHANGELOG.md
index b62df520..e5463ffa 100644
--- a/spaces/CHANGELOG.md
+++ b/spaces/CHANGELOG.md
@@ -1337,6 +1337,10 @@ Versions follow `0.MINOR.PATCH` while pre-1.0.
saves the invite with no versions, so the deleted text in them stays behind;
the file you are editing keeps all of them either way. The default is to
include it. A space with no history goes straight to saving, as before.
+- **A live session shares only the space's content.** Spaces now tells the
+ sync engine which document fields are content, so a collaborator's copy only
+ ever sends those. A field written by a newer version of Bento is kept in your
+ file when it arrives, but never passed on to the rest of the room.
## [0.1.0] — 2026-08-03
diff --git a/spaces/src/sync/crdt.ts b/spaces/src/sync/crdt.ts
index 5e0bec0d..9042a5b0 100644
--- a/spaces/src/sync/crdt.ts
+++ b/spaces/src/sync/crdt.ts
@@ -14,6 +14,7 @@ export * from '../../../kernel/src/sync/crdt.ts'
import { SyncEngine, shape } from '../../../kernel/src/sync/crdt.ts'
import type { DocShape } from '../../../kernel/src/sync/crdt.ts'
import { DOC_MAPS } from '../docmaps.ts'
+import { SPACES_FIELDS } from '../docclass.ts'
/**
* bento/spaces: the document holds `pages`, a page holds `blocks`.
@@ -41,7 +42,11 @@ import { DOC_MAPS } from '../docmaps.ts'
* keys back using the same names, and two copies of that list is exactly how
* the hazard and its mitigation drift apart.
*/
-export const SPACES_SHAPE: DocShape = shape('pages', 'blocks', 'html', DOC_MAPS)
+// The field map is bound (kernel #683): doc-level keys sync by ALLOWLIST, so a
+// replica emits only what docclass.ts classes 'content' and never echoes a key a
+// newer build added. Every non-content field spaces has is also on the kernel's
+// skipDoc floor; the map adds the unknown-key half.
+export const SPACES_SHAPE: DocShape = shape('pages', 'blocks', 'html', DOC_MAPS, SPACES_FIELDS)
/** The engine bound to bento/spaces. */
export class SyncState extends SyncEngine {