Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
type: undo moves content, never identity — a revoked key stays revoked (#618)
nyblneta2e02497f5a1
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.mdindex c0062774..c571ad10 100644--- a/docs/DECISIONS.md+++ b/docs/DECISIONS.md@@ -7790,3 +7790,29 @@ needs those more than anyone. Remote edits arrive through `touch()`, not`commit`, so a locked copy still follows the room. `copyIsReceiveOnly` is thelock's question (a document with no `collab` is local and editable — unlike`copyCanWrite`, which says no only because there is no room to write to).++## 2026-10-04 — Undo moves content, never identity (type)++**Undo and redo take a document's `docId`, `collab` and `readonly` from the+live document, never from the snapshot.** bento/type's `Store.#apply` swapped+whole-document snapshots in wholesale, so it rolled back identity and+capability along with text. Measured on that code: "Reset access" — which is+revocation — then ⌘Z put the revoked room key and room back, so a copy the+owner had just cut off could rejoin the next time they went live; "Stop+sharing" then ⌘Z turned sharing back on. ⌘Z means "undo what I wrote", not+"undo who this file is or who may reach it". It is also what keeps #588's+read-only lock honest: the lock reads `collab`, so undo can never unlock a+view-only copy.++The list (`FROM_LIVE`, type/src/store.ts) is the same as bento/slides'+(restoregate.ts, applied to undo in #605). It is a local copy for now; kernel+will lift one shared list. In type, `collab` is the field that changes in+place (the sharing actions); `docId` and `readonly` change only when another+document is loaded over this one. So undoing a whole-document REPLACE brings+back the earlier content under the identity that replace brought in — identity+is not undoable whichever action changed it. slides behaves the same.++`test-type-store.ts` drives the real sharing helpers through the session+adapter, undoes all the way back checking at every step, and covers each field+including one that is absent in the live document. Reverting to the full swap+reddens every identity row and the undo-must-not-unlock pair.diff --git a/scripts/test-type-store.ts b/scripts/test-type-store.tsindex 5b79621f..c816fb40 100644--- a/scripts/test-type-store.ts+++ b/scripts/test-type-store.ts@@ -204,10 +204,90 @@ console.log('\n— a receive-only copy takes no edits, and still receives —');ok(st.locked, 'and LOCKS when a reader copy is loaded into it later (loadDoc, Replace from JSON, recovery)');edit(st);ok(text(st) === 'agreed', 'so the newly-arrived copy refuses edits too');+ // Undo NEVER unlocks: the lock reads `collab`, and undo takes collab from+ // the live document (store.ts FROM_LIVE). This case first asserted the+ // opposite — that undoing the replace restored the writer copy and unlocked+ // it — which was the old full-swap undo, and the rule it encoded is the bug.st.undo();- ok(!st.locked, 'undoing that replace restores the writer copy — and unlocks with it');+ ok(st.locked, 'undoing that replace does NOT unlock — a view-only copy stays view-only whatever ⌘Z does');edit(st);- ok(text(st) === 'CHANGED', 'which edits again');+ ok(text(st) === 'agreed', 'so it still refuses edits after the undo');+}++console.log('\n— undo and redo move content, never identity —');+{+ // Driven through the app's REAL identity actions — kernel online.ts's+ // sharing helpers via the session adapter, exactly as collab.ts calls them —+ // not by hand-setting fields. In type, collab is the field that changes in+ // place; docId and readonly change only when a document is loaded over this+ // one, so those rows go through replace.+ const { stopSharing, rotateKeys } = await import('../kernel/src/sync/online.ts');+ const ses = { removeTransport() {} } as never;+ // Through parseDoc, like the section above, and fresh per call.+ const docOf = (collab?: object): TypeDoc => {+ const d: TypeDoc = { ...emptyDoc(), body: [{ id: 'p1', kind: 'para', text: 'agreed' }] };+ const r = parseDoc(JSON.stringify(collab ? { ...d, collab } : d));+ if (!r.ok) throw new Error('fixture did not parse');+ return r.doc;+ };+ const room = { v: 2, on: true, room: 'w1', key: 'K', owner: 'OP' };+ const local = () => docOf();+ const text = (st: Store) => st.doc.body[0].text;+ const shared = (over: object = {}) => docOf({ ...room, ownerPriv: 'OK', room: 'OLD-ROOM', key: 'OLD-KEY', ...over });+ const typed = (st: Store, t: string) => st.commit(d => { d.body[0].text = t; });++ // collab: Stop sharing+ let s1 = new Store(shared()); typed(s1, 'v2');+ stopSharing(ses, hostStore(s1));+ s1.undo();+ ok(s1.doc.collab?.on === false, 'Stop sharing, then ⌘Z: sharing STAYS off');+ s1.redo();+ ok(s1.doc.collab?.on === false, 'and redo does not turn it back on either');++ // collab: Reset access — revocation, the case that matters most+ let s2 = new Store(shared()); typed(s2, 'v2');+ await rotateKeys(ses, hostStore(s2));+ const fresh = s2.doc.collab?.key;+ ok(fresh !== 'OLD-KEY', 'Reset access mints a new room key');+ // Undo ALL the way back, checking at EVERY step — rotateKeys commits twice+ // (stopSharing, then the fresh credentials), so the revoked key is sitting+ // in more than one snapshot, and any single step could have resurrected it.+ let steps = 0, everOld = false;+ while (s2.canUndo) { s2.undo(); steps++; if (s2.doc.collab?.key === 'OLD-KEY' || s2.doc.collab?.room === 'OLD-ROOM') everOld = true; }+ ok(!everOld && s2.doc.collab?.key === fresh,+ `and ⌘Z all the way back (${steps} steps) never brings the REVOKED key or room back, at any step`);+ ok(text(s2) === 'agreed', 'while the CONTENT still undoes all the way back');+ while (s2.canRedo) { s2.redo(); if (s2.doc.collab?.key !== fresh) everOld = true; }+ ok(!everOld && text(s2) === 'v2', 'redo moves content forward and leaves the new key alone at every step');++ // docId: identity survives undo across a load+ const other = docOf({ ...room, ownerPriv: 'OK' }); other.docId = 'doc-LOADED';+ let s3 = new Store(shared()); const before = s3.doc.docId;+ s3.replace(other);+ s3.undo();+ ok(s3.doc.docId === 'doc-LOADED' && s3.doc.docId !== before,+ 'docId is not undoable: ⌘Z after a load keeps the live identity');+ ok(text(s3) === 'agreed', 'though the earlier content comes back');+ s3.redo();+ ok(s3.doc.docId === 'doc-LOADED', 'and redo keeps it');++ // readonly: the file's mode survives undo across a load+ const player = docOf(); (player as { readonly?: boolean }).readonly = true;+ let s4 = new Store(local()); s4.replace(player);+ s4.undo();+ ok((s4.doc as { readonly?: boolean }).readonly === true, 'readonly is not undoable: the mode the file is in stays');+ s4.redo();+ ok((s4.doc as { readonly?: boolean }).readonly === true, 'across redo too');++ // and the field going ABSENT is kept as absent, not resurrected from the snapshot+ let s5 = new Store(player); s5.replace(local());+ s5.undo();+ ok(!('readonly' in s5.doc), 'a live field that is ABSENT stays absent — undo does not resurrect it from the snapshot');++ // the ordinary case is untouched: content undo still works+ let s6 = new Store(shared()); typed(s6, 'v2'); typed(s6, 'v3');+ s6.undo();+ ok(text(s6) === 'v2', 'an ordinary edit still undoes');}console.log(`\n${checks - failures}/${checks} checks passed`);diff --git a/type/src/store.ts b/type/src/store.tsindex ed9bb4df..4d9264fb 100644--- a/type/src/store.ts+++ b/type/src/store.ts@@ -31,6 +31,42 @@ type Listener = (doc: TypeDoc) => void;const clone = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T;const LIMIT = 200;+/**+ * What undo and redo must NEVER move: the open file's identity and capability.+ *+ * A whole-document snapshot carries every field the document had, so swapping+ * one in used to roll these back too — measured on this code before the fix:+ * "Reset access" then ⌘Z put the REVOKED room key back (a copy you had just+ * cut off could rejoin the next time you went live), and "Stop sharing" then+ * ⌘Z turned sharing back on. ⌘Z means "undo what I wrote", not "undo who this+ * file is or who may reach it".+ *+ * So undo/redo move CONTENT and take these from the live document:+ * docId — identity; regenerating or reverting it orphans autosave,+ * recovery and sync for the file+ * collab — the room, its keys and whether it is on: capability, and the+ * thing #588's read-only lock reads, so undo can never unlock a+ * view-only copy+ * readonly — the file's mode+ * The same list as bento/slides' FROM_LIVE (restoregate.ts). A local copy for+ * now; kernel will lift one shared list for every app.+ *+ * Consequence worth knowing: undoing a whole-document REPLACE (Replace from+ * JSON, loadDoc, restoring a recovery snapshot) brings back the earlier+ * CONTENT under the identity that replace brought in — identity is not+ * undoable, whichever action changed it. bento/slides behaves the same.+ */+export const FROM_LIVE = ['docId', 'collab', 'readonly'] as const;++function keepLiveIdentity(doc: TypeDoc, live: TypeDoc): void {+ const d = doc as unknown as Record<string, unknown>;+ const l = live as unknown as Record<string, unknown>;+ for (const k of FROM_LIVE) {+ if (l[k] !== undefined) d[k] = l[k];+ else delete d[k];+ }+}+export class Store {#doc: TypeDoc;#undo: Snap[] = [];@@ -126,7 +162,9 @@ export class Store {#apply(s: Snap): Snap {if (s.kind === 'doc') {const inverse: Snap = { kind: 'doc', doc: clone(this.#doc) };+ const live = this.#doc;this.#doc = s.doc;+ keepLiveIdentity(this.#doc, live);return inverse;}// A block snapshot restores that block in place. If the block is gone (a