Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

applyRestore: wholeSnapshot — a deleted field stays removed on recovery (#716)

nyblnet7c877a9eaa95
diff --git a/kernel/src/docfields.ts b/kernel/src/docfields.ts
index 1945313f..2ff362ab 100644
--- a/kernel/src/docfields.ts
+++ b/kernel/src/docfields.ts
@@ -88,12 +88,27 @@ export interface RestoreRules {
/** Per-field recover hook — used only for a 'live' history field on 'recover'.
* A field with no hook follows incoming when present, keeps live when absent. */
readonly recover?: Readonly<Record<string, RecoverHook>>
+ /** Is `incoming` a WHOLE document (the default) or a PROJECTION of one? Every
+ * app's recovery snapshot is whole — `putRecovery` stores `contentOnly(doc)`,
+ * the full document minus capability — so on 'recover' an absent CONTENT (or
+ * unknown) field means the user DELETED it before the crash, and it stays
+ * removed. A PROJECTION snapshot — one that legitimately omits fields it still
+ * has — MUST pass FALSE: forget it and those fields are REMOVED, so its failure
+ * is data loss, not exposure. Under FALSE an absent field keeps the live
+ * value. A HISTORY field
+ * keeps live when absent either way, and the identity/mode/capability floor is
+ * unchanged. Only 'recover' is affected — 'back' already drops an absent
+ * field. (maintainer ruling 2026-10-15: whole is the default.) */
+ readonly wholeSnapshot?: boolean
}
/** Resolve a whole-document restore and return the doc to INSTALL.
* Over the union of live + incoming keys:
* - a 'follows' field takes `incoming` ('back': absent means dropped; 'recover':
- * an absent key never wipes a live value — a snapshot may be a projection);
+ * absent means dropped too for a WHOLE snapshot — the default, since a
+ * recovery snapshot is contentOnly(doc) and an absent field was deleted — but
+ * absent keeps live for a PROJECTION (wholeSnapshot:false) and for a HISTORY
+ * field either way);
* - a 'live' field keeps the live value, EXCEPT a 'live' history field on
* 'recover', which uses its recover hook, or (no hook) follows incoming when
* present and keeps live when absent.
@@ -121,6 +136,7 @@ export function applyRestore<T extends object>(
at: { live?: number; incoming?: number } = {},
): T {
const L = live as Obj, I = incoming as Obj
+ const whole = rules.wholeSnapshot ?? true // incoming is a whole document unless told otherwise
const out: Obj = {}
for (const k of new Set([...Object.keys(L), ...Object.keys(I)])) {
if (PROTO_KEYS.has(k)) continue // never assign a prototype key from an untrusted snapshot
@@ -131,7 +147,11 @@ export function applyRestore<T extends object>(
const kl = override ?? defaultKeepLive(cls)
if (kl === 'follows') {
if (I[k] !== undefined) out[k] = I[k]
- else if (direction === 'recover' && L[k] !== undefined) out[k] = L[k] // absent never wipes on recover
+ // On 'recover', an absent follows-field keeps live ONLY for a PROJECTION
+ // snapshot (wholeSnapshot:false) or a HISTORY field. In a WHOLE snapshot
+ // (the default), an absent content or unknown field means the user DELETED
+ // it, so it stays removed — not resurrected.
+ else if (direction === 'recover' && L[k] !== undefined && (!whole || cls === 'history')) out[k] = L[k]
continue
}
// 'live'
diff --git a/scripts/test-restore.ts b/scripts/test-restore.ts
index e95d58e5..b41072f5 100644
--- a/scripts/test-restore.ts
+++ b/scripts/test-restore.ts
@@ -87,14 +87,35 @@ H('recover (crash): the newer snapshot wins content AND history; identity still
ok((r.comments as Rec)?.t1 === 'incoming', 'comments follow the snapshot on recover')
}
-H('absent never wipes on recover; but a back restore reflects the version exactly')
+H('recover of a WHOLE snapshot (the default): a deleted content/unknown field stays removed')
{
- const inc = incoming(); delete inc.revisions; delete inc.comments; delete inc.mystery
+ // Every app's recovery snapshot is contentOnly(doc) — the WHOLE document minus
+ // capability — so an absent field means the user DELETED it before the crash.
+ // wholeSnapshot defaults true, so a 'follows' content or unknown field absent
+ // from the snapshot is REMOVED, not resurrected. A HISTORY field still keeps
+ // live. (Regression #701 introduced; maintainer ruling 2026-10-15.)
+ const inc = incoming(); delete inc.body; delete inc.mystery; delete inc.revisions; delete inc.comments
const r = applyRestore(live(), inc, 'recover', RULES) as Rec
- ok(eq(r.revisions, live().revisions), 'a history field ABSENT from the snapshot keeps the live value (never wiped)')
- ok((r.comments as Rec)?.t1 === 'live', 'a follows field absent on recover keeps live')
- ok(r.mystery === 'live-x', 'an unknown field absent on recover keeps live')
- // version restore is exact: a field the version lacks is dropped
+ ok(!('body' in r), 'a CONTENT field the user deleted stays removed (not brought back)')
+ ok(!('mystery' in r), 'an UNKNOWN field absent from the whole snapshot is removed too')
+ ok(eq(r.revisions, live().revisions), 'but a HISTORY field absent keeps the live value (trail/revisions rule unchanged)')
+ ok((r.comments as Rec)?.t1 === 'live', 'and a history-class follows field (comments) absent keeps live')
+}
+
+H('recover of a PROJECTION snapshot (wholeSnapshot:false): absent keeps live')
+{
+ // An incoming that legitimately omits fields it still has — a projection — must
+ // not have those fields removed. The caller opts out of the whole-doc default.
+ const PROJ: RestoreRules = { ...RULES, wholeSnapshot: false }
+ const inc = incoming(); delete inc.body; delete inc.mystery; delete inc.revisions
+ const r = applyRestore(live(), inc, 'recover', PROJ) as Rec
+ ok(r.body === 'live body', 'a content field absent from a PROJECTION keeps live (never wiped)')
+ ok(r.mystery === 'live-x', 'an unknown field absent from a projection keeps live')
+ ok(eq(r.revisions, live().revisions), 'and history keeps live either way')
+}
+
+H('a back restore reflects the version exactly (wholeSnapshot does not apply)')
+{
const inc2 = incoming(); delete inc2.comments
const r2 = applyRestore(live(), inc2, 'back', RULES) as Rec
ok(!('comments' in r2), 'on back, a follows field the version lacks is dropped (the version had none)')