Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

applyRestore: a cleared history field stays cleared on recovery (#726)

nyblneta0ae854731d5
diff --git a/kernel/src/docfields.ts b/kernel/src/docfields.ts
index 7300fe92..1da0474e 100644
--- a/kernel/src/docfields.ts
+++ b/kernel/src/docfields.ts
@@ -97,12 +97,22 @@ export interface RestoreRules {
* is data loss, not exposure. Under FALSE an absent field keeps the live
* value. This keys on the EFFECTIVE keepLive, not the class: a history field an
* app OVERRODE to 'follows' (e.g. comments) is treated as content, so a deleted
- * comment stays deleted. A history field left at its default 'live' keep
- * (revisions, signatures) 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.) */
+ * comment stays deleted. A history field at its default 'live' keep (revisions,
+ * signatures) absent from a WHOLE snapshot is ALSO removed — the snapshot
+ * carries history, so an absent key was cleared — unless the field opts out via
+ * `keepLiveWhenAbsent`. 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
+ /** History fields (default 'live' keep) whose ABSENCE from a whole snapshot does
+ * NOT mean the user cleared them — a projection-shaped sub-field, a per-day
+ * trail, anything an app recomputes — so on 'recover' they keep the live value
+ * when absent instead of being removed. Only meaningful for history-class
+ * fields (content already follows the whole-snapshot rule, and the floor is
+ * always live); `validateRestoreRules` throws on a non-history entry. A field
+ * with a recover hook still runs its hook when the snapshot CARRIES the field —
+ * this governs only the absent case. */
+ readonly keepLiveWhenAbsent?: readonly string[]
}
/** Resolve a whole-document restore and return the doc to INSTALL.
@@ -164,13 +174,20 @@ export function applyRestore<T extends object>(
}
// 'live'
if (direction === 'recover' && cls === 'history') {
- // A missing key keeps live BEFORE any hook runs — so absent-never-wipes
- // does not depend on each hook remembering it, and a hook never sees an
- // undefined incoming.
+ // A hook runs ONLY with a present incoming (never an undefined one — the
+ // contract hook authors rely on). When incoming is present it decides.
const hook = I[k] !== undefined ? ownGet(rules.recover, k) : undefined
if (hook) { const v = hook(L[k], I[k], at); if (v !== undefined) out[k] = v; continue }
- if (I[k] !== undefined) out[k] = I[k]
- else if (L[k] !== undefined) out[k] = L[k] // absent never wipes a history field on recover
+ if (I[k] !== undefined) { out[k] = I[k]; continue }
+ // Absent. A recovery snapshot is contentOnly(doc) — it strips only collab,
+ // so it CARRIES history; an absent default-live history key therefore means
+ // the user CLEARED it. Under a WHOLE snapshot (the default) it stays removed
+ // (cleared stays cleared — else cleared drafts resurrect and ride the next
+ // shared copy). It keeps live only for a PROJECTION (wholeSnapshot:false) or
+ // a field listed in keepLiveWhenAbsent, whose absence does not imply a user
+ // clear (e.g. a per-day trail).
+ const keepAbsent = !whole || (rules.keepLiveWhenAbsent?.includes(k) ?? false)
+ if (keepAbsent && L[k] !== undefined) out[k] = L[k]
continue
}
if (L[k] !== undefined) out[k] = L[k]
@@ -184,12 +201,24 @@ export function applyRestore<T extends object>(
* so a slipped override is SAFE, not merely caught. */
export function validateRestoreRules(rules: RestoreRules): void {
const kl = rules.keepLive
- if (!kl) return
- for (const k of Object.keys(kl)) {
+ if (kl) {
+ for (const k of Object.keys(kl)) {
+ if (PROTO_KEYS.has(k)) continue
+ const cls = ownGet(rules.classes, k)
+ if (cls && FLOOR_CLASSES.has(cls) && ownGet(kl, k) !== 'live') {
+ throw new Error(`keepLive override for "${k}" (class ${cls}) must be 'live' — identity, mode and capability fields never follow a restore`)
+ }
+ }
+ }
+ // keepLiveWhenAbsent only governs the absent case of a 'live' HISTORY field.
+ // A content field already follows the whole-snapshot rule and the floor is
+ // always live, so a non-history entry is a config mistake — fail loudly.
+ for (const k of rules.keepLiveWhenAbsent ?? []) {
if (PROTO_KEYS.has(k)) continue
const cls = ownGet(rules.classes, k)
- if (cls && FLOOR_CLASSES.has(cls) && ownGet(kl, k) !== 'live') {
- throw new Error(`keepLive override for "${k}" (class ${cls}) must be 'live' — identity, mode and capability fields never follow a restore`)
+ const kept = ownGet(rules.keepLive, k) ?? defaultKeepLive(cls)
+ if (cls !== 'history' || kept !== 'live') {
+ throw new Error(`keepLiveWhenAbsent entry "${k}" (class ${cls ?? 'unknown'}, keepLive ${kept}) must be a default-live history field — it governs only the absent case of a 'live' history key`)
}
}
}
diff --git a/scripts/test-restore.ts b/scripts/test-restore.ts
index 1b07341a..dbc6b9ab 100644
--- a/scripts/test-restore.ts
+++ b/scripts/test-restore.ts
@@ -35,6 +35,7 @@ let failures = 0, checks = 0
const ok = (c: boolean, m: string) => { checks++; if (!c) { failures++; console.log(` FAIL ${m}`) } else console.log(` ok ${m}`) }
const H = (s: string) => console.log(`\n=== ${s} ===`)
const eq = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
+const throws = (fn: () => void, m: string) => { let t = false; try { fn() } catch { t = true } ok(t, m) }
type Rec = Record<string, unknown>
const CLASSES = {
@@ -93,16 +94,60 @@ H('recover of a WHOLE snapshot (the default): a deleted field stays removed —
// capability — so an absent field means the user DELETED it before the crash.
// wholeSnapshot defaults true. Removal keys on EFFECTIVE keepLive, not class:
// an effective-'follows' field (content, unknown, AND a history field OVERRIDDEN
- // to 'follows' like comments) absent → REMOVED; a history field at its default
- // 'live' keep (revisions) absent → kept live. (comments is the case security
- // required: a deleted comment/redaction must not resurrect and ride the next
- // shared copy. Regression first introduced in #701, partly fixed in #716.)
+ // to 'follows' like comments) absent → REMOVED; a DEFAULT-live history field
+ // (revisions) absent is ALSO removed — the snapshot carries history, so an
+ // absent key was cleared. (comments is the case security required: a deleted
+ // comment/redaction must not resurrect and ride the next shared copy.
+ // Regression first introduced in #701, fixed across #716/#722/this.)
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(!('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(!('comments' in r), 'an OVERRIDDEN history field (comments, keepLive follows) deleted stays removed')
- ok(eq(r.revisions, live().revisions), 'but a DEFAULT-live history field (revisions) absent keeps the live value')
+ ok(!('revisions' in r), 'a cleared DEFAULT-live history field (revisions) stays removed — not resurrected')
+}
+
+H('recover: a whole-snapshot cleared history field stays cleared; keepLiveWhenAbsent opts out')
+{
+ // The redaction-resurrection class for DEFAULT-live history: clearHistory →
+ // crash → Restore must not bring the cleared drafts back (they would ride the
+ // next shared copy). But a field whose absence is NOT a user clear (a per-day
+ // trail, a recomputed sub-field) opts out and keeps live. (security, measured
+ // on spaces #714 with real clearHistory.)
+ const KLA: RestoreRules = { ...RULES, keepLiveWhenAbsent: ['trail'] }
+ const live2 = (): Rec => ({ ...live(), trail: [{ day: '2026-01-01' }] })
+ const inc = incoming(); delete inc.revisions; delete inc.trail // both cleared/absent from the whole snapshot
+ const r = applyRestore(live2(), inc, 'recover', KLA) as Rec
+ ok(!('revisions' in r), 'a cleared history field with no opt-out stays removed under a whole snapshot')
+ ok(eq(r.trail, live2().trail), 'an opted-out history field (trail) keeps live when absent (absence is not a clear)')
+ // projection: absence is not a clear regardless of opt-out
+ const proj = applyRestore(live2(), { ...inc }, 'recover', { ...RULES, wholeSnapshot: false }) as Rec
+ ok(eq(proj.revisions, live2().revisions), 'under a projection an absent history field keeps live even without an opt-out')
+}
+
+H('recover: the opt-out governs ONLY the absent case — a carried field still runs its hook')
+{
+ // keepLiveWhenAbsent is about absence. When the snapshot CARRIES the field, its
+ // recover hook runs as usual. (security ask: pin that the opt-out does not
+ // short-circuit the present-case hook.)
+ const RH: RestoreRules = { classes: CLASSES, recover: { revisions: newestListWins }, keepLiveWhenAbsent: ['revisions'] }
+ const incNew = incoming({ revisions: [{ id: 'r1', at: '2026-01-01' }, { id: 'r2', at: '2026-02-01' }, { id: 'r3', at: '2026-03-01' }] })
+ const carried = (applyRestore(live(), incNew, 'recover', RH) as Rec).revisions as unknown[]
+ ok(carried.length === 3, 'a CARRIED history field still runs its recover hook (newestListWins took the newer snapshot)')
+ const gone = incoming(); delete gone.revisions
+ const r = applyRestore(live(), gone, 'recover', RH) as Rec
+ ok(eq(r.revisions, live().revisions), 'and the opt-out keeps it live only when ABSENT')
+}
+
+H('validateRestoreRules: keepLiveWhenAbsent must name a default-live history field')
+{
+ throws(() => validateRestoreRules({ classes: CLASSES, keepLiveWhenAbsent: ['body'] }),
+ 'rejects a CONTENT field (it already follows the whole-snapshot rule)')
+ throws(() => validateRestoreRules({ classes: CLASSES, keepLive: { comments: 'follows' }, keepLiveWhenAbsent: ['comments'] }),
+ 'rejects a history field OVERRIDDEN to follows (it is governed by the follows branch)')
+ throws(() => validateRestoreRules({ classes: CLASSES, keepLiveWhenAbsent: ['docId'] }),
+ 'rejects an identity/floor field')
+ validateRestoreRules({ classes: CLASSES, keepLiveWhenAbsent: ['trail', 'revisions'] }) // default-live history: ok
}
H('recover of a PROJECTION snapshot (wholeSnapshot:false): absent keeps live')
@@ -150,7 +195,9 @@ H('a recover hook is called ONLY when the snapshot HAS the field')
{
let calls = 0
const blind: RecoverHook = (_l, inc) => { calls++; return inc } // returns incoming (undefined if absent) blindly
- const RB: RestoreRules = { classes: CLASSES, recover: { revisions: blind } }
+ // Opt out so the absent case keeps live (a whole snapshot would remove it) —
+ // this test is about the HOOK CONTRACT, that a blind hook never sees undefined.
+ const RB: RestoreRules = { classes: CLASSES, recover: { revisions: blind }, keepLiveWhenAbsent: ['revisions'] }
// snapshot LACKS revisions → the hook must NOT run, live is kept untouched
const inc = incoming(); delete inc.revisions
const r = applyRestore(live(), inc, 'recover', RB) as Rec