Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
spaces: undo, redo and every whole-document restore keep the live identity (#607)
nyblnet3ff57dbaa557
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.mdindex 6f2a184f..ea9b959e 100644--- a/docs/DECISIONS.md+++ b/docs/DECISIONS.md@@ -7442,6 +7442,37 @@ rigs treat a drawn arrow as a deliberate difference: named in`test-maths-lite.ts`, counted with the identical ones in the coverage floor.Cost: +966 B of shell.+## 2026-10-04 — Spaces: undo, redo and every whole-document restore keep the live identity (`FROM_LIVE`)++`spaces/src/store.ts` exports `FROM_LIVE = ['docId', 'collab', 'readonly',+'template']`: top-level keys that a whole-document restore always takes from+the LIVE document, never from what it restores. It has the same name and shape+as slides' `FROM_LIVE` (`slides/src/restoregate.ts`, the same fix for slides'+snapshot undo, #605), so the kernel can lift one shared list later. It applies+to undo, redo and `replaceDoc`, which is the path for version history, the+recovery banner, Replace from JSON and `bento.loadDoc`. Before this fix, an+edit followed by Stop sharing came back ON at the next ⌘Z.++Each key, and why it is identity rather than content:+- `docId` never changes (PLATFORM §3). Versions and recovery are keyed by it,+ so a restore that changed it would detach the space from its own history.+- `collab` is a capability, and its `on` switch is a choice made about this+ copy. A snapshot from before Stop sharing would rejoin the room. One from+ before Rotate keys would bring back the revoked key.+- `readonly` is the file's sealed mode, and undo must not unseal it.+- `template` re-mints `docId` on every open, so it controls identity. Slides+ has no such field, which is the one difference between the two lists.++Kept OFF the list on purpose: `theme` and any other design setting are+content and undo like content. `format`, `version` and `policy` describe how+the content is encoded and travel with it. `assets` is a different case: undo+and redo keep the live assets because snapshots leave them out to save space,+but a `replaceDoc` brings its own.++Version restore keeps the live `docId` and `collab` for the same reason undo+does. A version is a past state of the content, and the room and keys are+this copy's present capability. Restoring from before a rotation or before+Stop sharing must not re-arm what the person turned off.## 2026-10-04 — dash: a restore takes content from the snapshot, identity from the live workbookDash's recovery banner and Version history now go throughdiff --git a/docs/spaces-agents.md b/docs/spaces-agents.mdindex 8d151b43..04e44a3a 100644--- a/docs/spaces-agents.md+++ b/docs/spaces-agents.md@@ -313,7 +313,7 @@ bento.removeBlocks([ids]) // → {ok:true, removed, missing, adbento.moveBlock(id, {pageId?, afterId?, beforeId?, parent?})bento.updatePage(id, patch) // → {ok:true, id} | {ok:false, err}bento.removePage(id, {descendants?}) // → {ok:true, removed, rehomed, links}-bento.loadDoc(json) // replace everything (one undo step)+bento.loadDoc(json) // replace the content (one undo step); docId, collab, readonly, template staybento.serialize() // the whole .bento.html filebento.undo() / bento.redo()diff --git a/scripts/test-spaces-undo.ts b/scripts/test-spaces-undo.tsindex 1f86f1bd..c1f73460 100644--- a/scripts/test-spaces-undo.ts+++ b/scripts/test-spaces-undo.ts@@ -218,5 +218,159 @@ console.log('bento/spaces undo\n')ok(s.doc.pages.length === 4 && s.doc.assets?.k1 !== undefined, 'redo brings the import back intact')}+// ---- undo moves CONTENT, never identity ------------------------------------+//+// Undo is snapshot-based: a doc entry restores the whole document. It used to+// restore the snapshot's top-level fields too, so an edit followed by Stop+// sharing, a duplicate's new identity or a sealed mode came back undone by the+// next ⌘Z — silently rejoining a room the person had left, or pointing this+// space at its ancestor's docId and room. store.ts FROM_LIVE keeps those keys+// from the LIVE document for undo, redo and every replaceDoc. Slides had the+// same bug (slides/src/restoregate.ts FROM_LIVE).+//+// Spaces' own Duplicate and reading-copy saves write a SEPARATE file and leave+// the open document alone, so rows 2 and 3 move the live fields directly: what+// is under test is the store's guarantee, whatever moves them.+{+ const shared = () => {+ const d = load([page('a', 'A0'), page('b', 'B0')])+ ;(d as Record<string, unknown>).collab = { v: 2, room: 'w-room1', key: 'k1', on: true }+ return new Store(d)+ }+ const on = (s: Store) => (s.doc.collab as { on?: boolean } | undefined)?.on+ const room = (s: Store) => (s.doc.collab as { room?: string } | undefined)?.room++ // 1. Stop sharing — as kernel stopSharing does it on main today (its own+ // store.commit, so it is an undo step) …+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.commit(() => { (s.doc.collab as { on?: boolean }).on = false })+ s.undo(); s.undo()+ ok(textOf(s, 'a') === 'A0', 'stop sharing (as a commit): the edit before it still undoes')+ ok(on(s) === false, 'stop sharing (as a commit): two ⌘Z later, sharing is still OFF')+ s.redo(); s.redo()+ ok(textOf(s, 'a') === 'A1' && on(s) === false, 'stop sharing (as a commit): redo keeps it OFF too')+ }+ // … and as it does once collab changes stop being undo steps (markShared).+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ ;(s.doc.collab as { on?: boolean }).on = false+ s.setDirty(true)+ s.undo()+ ok(textOf(s, 'a') === 'A0' && on(s) === false, 'stop sharing (not a commit): ⌘Z reverts the edit, sharing stays OFF')+ s.redo()+ ok(textOf(s, 'a') === 'A1' && on(s) === false, 'stop sharing (not a commit): redo keeps it OFF')+ }+ // Rotate keys: the snapshot holds the revoked room/key.+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.doc.collab = { v: 2, room: 'w-room2', key: 'k2', on: true } as SpacesDoc['collab']+ s.undo()+ ok(room(s) === 'w-room2', 'rotate keys: ⌘Z never resurrects the revoked room')+ s.redo()+ ok(room(s) === 'w-room2', 'rotate keys: nor does redo')+ }++ // 2. Duplicate as a new space: a fresh docId and no credentials.+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.doc.docId = 'dup'+ delete (s.doc as Record<string, unknown>).collab+ s.undo()+ ok(textOf(s, 'a') === 'A0', 'duplicate: the edit before it still undoes')+ ok(s.doc.docId === 'dup', 'duplicate: ⌘Z keeps the duplicate\'s docId, not its ancestor\'s')+ ok(s.doc.collab === undefined, 'duplicate: ⌘Z does not bring back the ancestor\'s room')+ s.redo()+ ok(s.doc.docId === 'dup' && s.doc.collab === undefined, 'duplicate: redo keeps the duplicate\'s identity')+ }++ // 3. A sealed reading copy / frozen mode, and the template mode.+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.doc.readonly = true+ s.undo()+ ok(textOf(s, 'a') === 'A0' && s.doc.readonly === true, 'readonly: ⌘Z reverts the edit, the mode stays')+ s.redo()+ ok(s.doc.readonly === true, 'readonly: redo keeps it')+ }+ {+ const d = load([page('a', 'A0')])+ d.template = true+ const s = new Store(d)+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ delete s.doc.template+ s.undo()+ ok(s.doc.template === undefined, 'template: ⌘Z does not turn the space back into a template (which re-mints docId on open)')+ s.redo()+ ok(s.doc.template === undefined, 'template: nor does redo')+ }++ // REDO in its own right. The redo rows above pass whatever restore does,+ // because a redo entry is taken at undo time, when the live identity is+ // already in place. The case that bites is the identity moving BETWEEN the+ // undo and the redo: the redo entry then holds the OLD identity. Edit, ⌘Z,+ // then the change, then ⌘⇧Z.+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.undo()+ ;(s.doc.collab as { on?: boolean }).on = false+ s.redo()+ ok(textOf(s, 'a') === 'A1' && on(s) === false, 'redo after Stop sharing: the edit comes back, sharing stays OFF')+ }+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.undo()+ s.doc.docId = 'dup'+ delete (s.doc as Record<string, unknown>).collab+ s.redo()+ ok(s.doc.docId === 'dup' && s.doc.collab === undefined, 'redo after Duplicate: docId and collab stay the duplicate\'s')+ }+ {+ const s = shared()+ s.commit(() => { s.doc.pages[0].blocks[0].html = 'A1' })+ s.undo()+ s.doc.readonly = true+ s.redo()+ ok(s.doc.readonly === true, 'redo after a readonly seal: the mode stays')+ }++ // 4. Every replaceDoc — version history, recovery, Replace from JSON,+ // bento.loadDoc — imports content, never identity.+ {+ const s = shared()+ ;(s.doc.collab as { on?: boolean }).on = false // stopped after the version was taken+ const foreign = load([page('z', 'Z0')])+ foreign.docId = 'someone-else'+ ;(foreign as Record<string, unknown>).collab = { v: 2, room: 'w-theirs', key: 'kx', on: true }+ foreign.readonly = true+ foreign.template = true+ s.replaceDoc(foreign)+ ok(textOf(s, 'z') === 'Z0', 'replaceDoc: the content is replaced')+ ok(s.doc.docId === 'u' && room(s) === 'w-room1' && on(s) === false,+ 'replaceDoc: docId and collab (incl. its off switch) stay the live ones')+ ok(s.doc.readonly === undefined && s.doc.template === undefined, 'replaceDoc: the file mode stays the live one')+ s.undo()+ ok(textOf(s, 'a') === 'A0' && s.doc.docId === 'u' && on(s) === false, 'replaceDoc: ⌘Z puts the content back, identity unmoved')+ }++ // CONTROL: content — including the design — still undoes.+ {+ const s = shared()+ const was = JSON.stringify(s.doc.theme)+ s.commit(() => { s.doc.title = 'Renamed'; s.doc.theme = { ...s.doc.theme, accent: '#123456' } })+ s.undo()+ ok(s.doc.title === 'U' && JSON.stringify(s.doc.theme) === was, 'control: title and theme are content, and undo')+ s.redo()+ ok(s.doc.title === 'Renamed' && s.doc.theme.accent === '#123456', 'control: and redo')+ }+}+console.log(`\n${checks - failures}/${checks} checks passed`)if (failures) process.exit(1)diff --git a/spaces/CHANGELOG.md b/spaces/CHANGELOG.mdindex e2973980..7b76f375 100644--- a/spaces/CHANGELOG.md+++ b/spaces/CHANGELOG.md@@ -477,6 +477,17 @@ Versions follow `0.MINOR.PATCH` while pre-1.0.2026, from the same file. `bento.journal()` opens today's for an agent, and`bento.journal('2026-08-06')` any day's.+- **⌘Z no longer undoes who a space is.** Undo restores a snapshot of the+ whole document, and it used to bring that snapshot's identity back with the+ content: edit something, then Stop sharing, and the next ⌘Z switched sharing+ back on, silently rejoining the room you had just left. A key rotation could+ be undone into the revoked key the same way, and a new docId or a read-only+ mode reverted to whatever the snapshot held. Undo and redo now move content+ only. The docId, the live-session credentials and the read-only and template+ modes always stay as they are in the open space. Restoring a version, the+ recovery banner, Replace from JSON and `bento.loadDoc` follow the same rule:+ they replace the pages, title and theme, and never the space's identity.+ Bento Slides had the same bug.- **A copy that cannot write never gets writer chrome.** Whether a copy maywrite was decided by `collab.role !== 'reader'`, so the live-show`'audience'` role — and any role added later — passed as a writer: andiff --git a/spaces/src/about.ts b/spaces/src/about.tsindex 1485adae..69068bad 100644--- a/spaces/src/about.ts+++ b/spaces/src/about.ts@@ -657,10 +657,9 @@ export function openAbout(hooks: AboutHooks): void {// The live session belongs to THIS document, not to the pasted text.// Content is imported; identity and capability are not — adopting the// pasted `collab` would either wipe the room credentials or silently- // move this space into somebody else's room.- const keep = store.doc.collab- if (keep) res.doc.collab = keep- else delete res.doc.collab+ // move this space into somebody else's room. replaceDoc keeps the live+ // docId, collab and file mode itself (store.ts FROM_LIVE), for this and+ // every other whole-document restore.store.replaceDoc(res.doc)onRepaint()close()diff --git a/spaces/src/store.ts b/spaces/src/store.tsindex 252f5705..230e433a 100644--- a/spaces/src/store.ts+++ b/spaces/src/store.ts@@ -24,6 +24,45 @@ const RUN_IDLE_MS = 600* snapshots retain tens of MB. Assets are excluded from snapshots entirely. */const UNDO_BUDGET = 24 * 1024 * 1024+/**+ * Top-level keys a whole-document restore ALWAYS takes from the LIVE document,+ * never from what it restores: undo, redo, and every replaceDoc (version+ * history, the recovery banner, Replace from JSON, bento.loadDoc).+ *+ * These are who the space IS and what this copy may do, not what it says:+ * · `docId` — identity (PLATFORM §3). A snapshot taken before "Duplicate as+ * a new space" or pasted from another file must not make this+ * space claim somebody else's id, its versions and its room.+ * · `collab` — the live-session capability and its on/off switch. A+ * snapshot from before Stop sharing would silently rejoin the+ * room; one from before Rotate keys would resurrect a revoked key.+ * · `readonly` — the file's sealed-reading-copy mode. Undo must not unfreeze it.+ * · `template` — a file mode that RE-MINTS `docId` on every open (model.ts),+ * so it governs identity: pasted or restored, it would make+ * this space a new document each time it is opened.+ *+ * NOT on the list, deliberately: `theme` (and any design setting) is content+ * and undoes like content; `format`/`version`/`policy` describe how the content+ * is encoded and travel with it; `assets` is kept by undo/redo for a different+ * reason — snapshots omit it for size — and a replaceDoc brings its own.+ *+ * Same name and shape as slides' FROM_LIVE (slides/src/restoregate.ts) so the+ * kernel can lift one shared list; spaces adds `template`, which slides lacks.+ */+export const FROM_LIVE = ['docId', 'collab', 'readonly', 'template'] as const++/** Overwrite `next`'s FROM_LIVE keys with `live`'s — including deleting one the+ * live document does not have. Mutates and returns `next`. */+function keepLiveIdentity(live: SpacesDoc, next: SpacesDoc): SpacesDoc {+ const from = live as Record<string, unknown>+ const to = next as Record<string, unknown>+ for (const k of FROM_LIVE) {+ if (from[k] !== undefined) to[k] = from[k]+ else delete to[k]+ }+ return next+}+/*** One undoable step.*@@ -262,8 +301,12 @@ export class Store {// reach here, and dropping the entry is better than throwing.if (at >= 0) this.doc.pages[at] = JSON.parse(entry.json) as Page} else {- const assets = this.doc.assets- this.doc = { ...(JSON.parse(entry.json) as SpacesDoc), ...(assets ? { assets } : {}) }+ // CONTENT moves, identity does not: the snapshot's docId/collab/readonly+ // are whatever they were when it was taken, and Stop sharing, a key+ // rotation or a mode change since then must survive ⌘Z and ⌘⇧Z alike.+ const live = this.doc+ const assets = live.assets+ this.doc = keepLiveIdentity(live, { ...(JSON.parse(entry.json) as SpacesDoc), ...(assets ? { assets } : {}) })}this.pageId = entry.viewIdthis.reindex()@@ -316,12 +359,17 @@ export class Store {this.emit('selection')}- /** Replace the whole document — the AI round-trip and version restore path. */+ /**+ * Replace the whole document — the AI round-trip, version restore and+ * recovery path. Content is replaced; identity is not (FROM_LIVE): a version+ * from before Stop sharing or Rotate keys, or JSON pasted from another space,+ * never brings its own docId, room credentials or file mode with it.+ */replaceDoc(next: SpacesDoc): void {this.dirty = truethis.endRun()this.checkpoint()- this.doc = next+ this.doc = keepLiveIdentity(this.doc, next)this.reindex()this.pageId = homePage(next)?.id ?? ''this.emit('doc')