Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

kernel: per-document storage under bento/home (DocStore) (#669)

nyblnet626cd2732ccc
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f9a91475..38ee6d30 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -833,6 +833,13 @@ jobs:
# a host polyfill), reconnect BEFORE serialize so activation stays fresh.
run: node scripts/test-file-handle.ts
+ - name: DocStore (per-document storage under the extension)
+ # Under bento/home, the crash-recovery copy and the member key's private
+ # half live in the extension's per-document store; localStorage keeps
+ # only the public key its readers need. No host, or a host that refuses
+ # or never answers: the old storage, unchanged.
+ run: node scripts/test-docstore.ts
+
- name: Guarded storage rig
# localStorage's absence is hostile: reading the PROPERTY throws, so
# `if (localStorage)` is itself the crash and one bare read at module
diff --git a/kernel/src/autosave.ts b/kernel/src/autosave.ts
index a06fede2..a8fe9d28 100644
--- a/kernel/src/autosave.ts
+++ b/kernel/src/autosave.ts
@@ -17,6 +17,7 @@
import type { KernelDoc } from './doc.ts'
import { appConfig } from './app.ts'
+import { docStore, getJSON, setJSON } from './docstore.ts'
/**
* One database PER APP.
@@ -192,17 +193,71 @@ function contentOnly(doc: KernelDoc): string {
* that is exactly where a shared deck tends to be opened. Claiming a backstop
* that isn't there would be worse than saying nothing.
*/
+/** Documents whose recovery copy the host refused (e.g. too large) in this page. */
+const recoveryRefused = new Set<string>()
+const recoveryOffListeners = new Set<(docId: string, reason: string) => void>()
+
+/** Has crash recovery been switched off for this document in this page? True
+ * after the per-document store refused its recovery copy (too large, say). */
+export function recoveryOff(docId: string): boolean {
+ return recoveryRefused.has(docId)
+}
+
+/** Be told, once per document, that its crash recovery is off — so the app can
+ * say so instead of implying a backstop that isn't there. Returns unsubscribe. */
+export function onRecoveryOff(fn: (docId: string, reason: string) => void): () => void {
+ recoveryOffListeners.add(fn)
+ return () => { recoveryOffListeners.delete(fn) }
+}
+
export async function putRecovery(doc: KernelDoc): Promise<boolean> {
- const key = await tx(RECOVERY, 'readwrite', (s) =>
- s.put({ docId: doc.docId, at: Date.now(), title: doc.title, json: contentOnly(doc) } as Snapshot))
+ const snap: Snapshot = { docId: doc.docId, at: Date.now(), title: doc.title, json: contentOnly(doc) }
+ // Under a host with a per-document store (docstore.ts), the recovery copy
+ // lives THERE — out of the storage origin every local file shares — and any
+ // copy this origin still holds is removed. While that host is present the
+ // copy NEVER falls back to this origin: a host that doesn't answer in time
+ // may still be writing (the next autosave cycle writes again), and one that
+ // refuses (too large) turns recovery off for the document, said once.
+ const ds = docStore()
+ if (ds) {
+ if (recoveryRefused.has(doc.docId)) return false
+ const out = await setJSON(ds, 'recovery', snap)
+ if (out === 'stored') {
+ void tx(RECOVERY, 'readwrite', (s) => s.delete(doc.docId))
+ return true
+ }
+ if (out === 'refused') {
+ recoveryRefused.add(doc.docId)
+ const reason = 'the extension declined to keep a recovery copy of this document'
+ console.info('[bento] crash recovery is off for this document:', reason)
+ for (const fn of recoveryOffListeners) { try { fn(doc.docId, reason) } catch { /* a listener's problem */ } }
+ }
+ return false
+ }
+ const key = await tx(RECOVERY, 'readwrite', (s) => s.put(snap))
return key != null
}
export async function getRecovery(docId: string): Promise<Snapshot | null> {
+ const ds = docStore()
+ if (ds) {
+ try {
+ // the entry is per FILE, so check it is this document's before using it
+ const held = await getJSON<Snapshot>(ds, 'recovery')
+ if (held && held.docId === docId) return held
+ } catch { /* host unavailable: fall back to this origin's store */ }
+ }
return (await tx<Snapshot>(RECOVERY, 'readonly', (s) => s.get(docId))) ?? null
}
export async function clearRecovery(docId: string): Promise<void> {
+ const ds = docStore()
+ if (ds) {
+ try {
+ const held = await getJSON<Snapshot>(ds, 'recovery')
+ if (!held || held.docId === docId) await ds.delete('recovery')
+ } catch { /* host unavailable: nothing to clear there */ }
+ }
await tx(RECOVERY, 'readwrite', (s) => s.delete(docId))
}
diff --git a/kernel/src/docstore.ts b/kernel/src/docstore.ts
new file mode 100644
index 00000000..ec5a09e9
--- /dev/null
+++ b/kernel/src/docstore.ts
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+//
+// DocStore: a document's side data, held by the HOST, per document.
+//
+// A document opened from disk shares one browser storage origin with every
+// other local file, so anything it keeps in localStorage or IndexedDB is
+// readable by any other document opened the same way. When the page runs
+// under the bento/home browser extension (home/webext), the extension offers a
+// small store kept in ITS origin and partitioned by the file path the browser
+// reports for this page. The page never names a partition; the extension
+// derives it, so one document can't reach another's entries.
+//
+// Exactly two entries, by name:
+// 'recovery' — the crash-recovery copy of unsaved work (autosave.ts)
+// 'memberkey' — this device's member key for the document (sync/online.ts)
+// Values are opaque bytes. With no such host, every caller keeps the storage
+// it always used, unchanged.
+//
+// Wire: the page-world request/response protocol the extension's relay speaks
+// (home/webext/src/relay.js) — `{__bento_tray__, dir:'req', id, op, payload}`
+// answered by `{dir:'res', id, result}`, ops store.get / set / list / delete,
+// bytes as Uint8Array.
+
+export type DocStoreName = 'recovery' | 'memberkey'
+
+/** What became of a write: the host STORED it; the host answered and REFUSED
+ * it (e.g. too large); a NEWER write to the same name was committed first, so
+ * this older one was dropped (SUPERSEDED — the newer value is what's kept, so
+ * this is not a refusal of the document); or it never answered in time. A
+ * write that went unanswered may still complete on the host's side. */
+export type SetOutcome = 'stored' | 'refused' | 'superseded' | 'unanswered'
+
+export interface DocStore {
+ /** The stored bytes, or null when the entry is absent. THROWS if the host
+ * did not answer or refused, so a caller can tell "nothing kept" apart. */
+ get(name: DocStoreName): Promise<Uint8Array | null>
+ set(name: DocStoreName, bytes: Uint8Array): Promise<SetOutcome>
+ /** Names present for this document. THROWS if the host did not answer. */
+ list(): Promise<DocStoreName[]>
+ /** Whether the host removed it (true also when it was already absent). */
+ delete(name: DocStoreName): Promise<boolean>
+}
+// While a host with this store is present, callers do NOT fall back to the
+// storage origin every local file shares when a request fails: a host that
+// didn't answer in time may still complete the write. See autosave.ts and
+// sync/online.ts deviceIdentity.
+
+/** How long to wait, by kind of request (ms). */
+export interface DocStoreTimeouts {
+ /** Until the host has answered once. The first request of a page also waits
+ * on the host reading this document's file from disk, on a worker that may
+ * be starting cold. */
+ firstProbe: number
+ get: number
+ /** A write: a base, plus a share per transfer chunk (large values travel as
+ * several runtime round trips and a commit). */
+ setBase: number
+ setPerChunk: number
+ /** list / delete. */
+ small: number
+}
+export const DEFAULT_TIMEOUTS: DocStoreTimeouts = { firstProbe: 15_000, get: 60_000, setBase: 10_000, setPerChunk: 2_000, small: 10_000 }
+/** The transfer chunk the extension relay uses for a large value. */
+const CHUNK = 3 * 1024 * 1024
+
+const CH = '__bento_tray__'
+// Distinct from the extension's own page-bridge ids, which share the channel.
+const ID_PREFIX = 'bento-docstore-'
+let seq = 0
+
+type HostResult = { ok?: boolean; reason?: string; bytes?: unknown; names?: unknown } | undefined
+const TIMEOUT = 'timeout'
+
+/** The extension-backed store. One round trip per call; a host that never
+ * answers resolves as a failure after a bounded wait instead of hanging. */
+export class ExtensionBackend implements DocStore {
+ // explicit fields, not parameter properties: node rigs run strip-only TS
+ private readonly t: DocStoreTimeouts
+ private answered = false
+ constructor(timeouts: Partial<DocStoreTimeouts> = {}) { this.t = { ...DEFAULT_TIMEOUTS, ...timeouts } }
+
+ private ask(op: string, payload: object, ms: number): Promise<HostResult> {
+ // until the host has answered once, every request may be the slow first one
+ const wait = this.answered ? ms : Math.max(ms, this.t.firstProbe)
+ return new Promise((resolve) => {
+ const id = `${ID_PREFIX}${Date.now()}-${seq++}`
+ const onMessage = (ev: MessageEvent) => {
+ const d = ev.data as Record<string, unknown> | null
+ if (ev.source !== window || !d || d[CH] !== true || d.dir !== 'res' || d.id !== id) return
+ window.removeEventListener('message', onMessage)
+ clearTimeout(timer)
+ this.answered = true
+ resolve(d.result as HostResult)
+ }
+ const timer = setTimeout(() => {
+ window.removeEventListener('message', onMessage)
+ resolve({ ok: false, reason: TIMEOUT })
+ }, wait)
+ window.addEventListener('message', onMessage)
+ window.postMessage({ [CH]: true, dir: 'req', id, op, payload }, '*')
+ })
+ }
+
+ async get(name: DocStoreName): Promise<Uint8Array | null> {
+ const r = await this.ask('store.get', { name }, this.t.get)
+ if (!r?.ok) throw new Error(`DocStore get ${name}: ${r?.reason ?? 'no answer'}`)
+ return r.bytes instanceof Uint8Array ? r.bytes : null
+ }
+
+ async set(name: DocStoreName, bytes: Uint8Array): Promise<SetOutcome> {
+ const ms = this.t.setBase + this.t.setPerChunk * Math.ceil(bytes.length / CHUNK)
+ const r = await this.ask('store.set', { name, bytes }, ms)
+ if (r?.ok === true) return 'stored'
+ if (r?.reason === TIMEOUT) return 'unanswered'
+ // two overlapping writes: the host keeps the newer and drops this one
+ if (r?.reason === 'superseded') return 'superseded'
+ return 'refused'
+ }
+
+ async list(): Promise<DocStoreName[]> {
+ const r = await this.ask('store.list', { prefix: '' }, this.t.small)
+ if (!r?.ok || !Array.isArray(r.names)) throw new Error(`DocStore list: ${r?.reason ?? 'no answer'}`)
+ return r.names.filter((n): n is DocStoreName => n === 'recovery' || n === 'memberkey')
+ }
+
+ async delete(name: DocStoreName): Promise<boolean> {
+ const r = await this.ask('store.delete', { name }, this.t.small)
+ return r?.ok === true
+ }
+}
+
+let cached: DocStore | null | undefined
+
+/** The host's DocStore for this page, or null when there is none — in which
+ * case the caller uses the storage it always did. Decided once per page: the
+ * host announces its capabilities (`window.__bentoHost.ops`) before the
+ * document's runtime boots. */
+export function docStore(): DocStore | null {
+ if (cached !== undefined) return cached
+ const host = (globalThis as { __bentoHost?: { ops?: readonly string[] } }).__bentoHost
+ const can = typeof window !== 'undefined' && typeof window.postMessage === 'function' &&
+ Array.isArray(host?.ops) && host.ops.includes('store')
+ cached = can ? new ExtensionBackend() : null
+ return cached
+}
+
+/** Forget the decision above. For rigs that toggle the host between cases. */
+export function resetDocStoreForTest(store?: DocStore | null): void {
+ cached = store
+}
+
+const enc = new TextEncoder()
+const dec = new TextDecoder()
+
+/** A JSON value from the store, or null when absent or unreadable. THROWS if
+ * the host could not answer. */
+export async function getJSON<T>(store: DocStore, name: DocStoreName): Promise<T | null> {
+ const bytes = await store.get(name)
+ if (!bytes) return null
+ try { return JSON.parse(dec.decode(bytes)) as T } catch { return null }
+}
+
+/** Store a JSON value. */
+export function setJSON(store: DocStore, name: DocStoreName, value: unknown): Promise<SetOutcome> {
+ return store.set(name, enc.encode(JSON.stringify(value)))
+}
diff --git a/kernel/src/sync/online.ts b/kernel/src/sync/online.ts
index 7199424f..c83c6811 100644
--- a/kernel/src/sync/online.ts
+++ b/kernel/src/sync/online.ts
@@ -13,6 +13,7 @@
import type { Op, SyncStateJSON } from './crdt.ts'
import type { Frame, HostStore, RefusalCode, SyncDoc, SyncSession, Transport } from './session.ts'
import { lsGet, lsSet } from '../storage.ts'
+import { docStore, getJSON, setJSON } from '../docstore.ts'
import { carryThroughRotation } from '../docfields.ts'
import { offlineEnabled } from '../update.ts'
// Every request in the app goes through the one chokepoint (kernel/src/net.ts)
@@ -92,16 +93,84 @@ export async function mintInvite(ownerPrivB64: string, role: 'writer' | 'comment
return { pub: kp.pub, priv: kp.priv, role, ...(exp ? { exp } : {}), sig }
}
-/** This device's member identity for a doc — minted once, kept in localStorage,
- * NEVER in the file (the file travels; a device key must not). */
+/** Member identities this page has resolved AND the store holds, by docId (one
+ * host round trip per document per page, not one per connect). */
+const memberCache = new Map<string, { pub: string; priv: string }>()
+/** Identities the host has not yet stored (it refused, or did not answer in
+ * time): used for this page, offered to the host again on the next call. */
+const unstored = new Map<string, { pub: string; priv: string }>()
+
+/** This device's member pubkey for a doc, as resolved in this page. The
+ * presence beat reads it synchronously. */
+export function cachedMemberPub(docId: string): string | undefined {
+ return (memberCache.get(docId) ?? unstored.get(docId))?.pub
+}
+
+/** This device's member identity for a doc — minted once, NEVER in the file
+ * (the file travels; a device key must not).
+ *
+ * Under a host with a per-document store (docstore.ts), the PRIVATE key lives
+ * there, out of the storage origin every local file shares. localStorage keeps
+ * only the PUBLIC key under the same name, because the apps' People views read
+ * `bento-member-<docId>`.pub to recognise this device — and a pubkey is public
+ * anyway. The first time, a full key this browser already holds is ADOPTED
+ * (the device keeps its People entry) and its private half removed from that
+ * origin.
+ *
+ * While that host is present NOTHING falls back to the shared origin:
+ * - a READ the host can't answer THROWS — it neither mints a key (that would
+ * change this device's identity) nor writes one; the caller retries;
+ * - a WRITE the host refuses or doesn't answer keeps the identity for this
+ * page and offers it to the host again on the next call; the private key is
+ * never written to localStorage.
+ * Without such a host, the whole key lives in localStorage as before. */
export async function deviceIdentity(docId: string): Promise<{ pub: string; priv: string }> {
+ const hit = memberCache.get(docId)
+ if (hit) return hit
const k = `bento-member-${docId}`
- try {
- const saved = lsGet(k)
- if (saved) return JSON.parse(saved)
- } catch { /* storage unavailable → ephemeral identity */ }
+ /** a FULL identity from localStorage — a pub-only mirror is not one */
+ const saved = (): { pub: string; priv: string } | null => {
+ try {
+ const s = lsGet(k)
+ const v = s ? JSON.parse(s) as { pub?: string; priv?: string } : null
+ return v?.pub && v.priv ? { pub: v.pub, priv: v.priv } : null
+ } catch { return null }
+ }
+ const ds = docStore()
+ if (ds) {
+ const kept = unstored.get(docId)
+ let held: { docId?: string; pub?: string; priv?: string } | null
+ try {
+ held = await getJSON(ds, 'memberkey')
+ } catch (e) {
+ // can't read the host: an identity already in use this page carries on;
+ // otherwise fail this attempt — never mint or write while blind
+ if (kept) return kept
+ throw e
+ }
+ // the entry is per FILE, so check it is this document's before using it
+ if (held?.docId === docId && held.pub && held.priv) {
+ const id = { pub: held.pub, priv: held.priv }
+ unstored.delete(docId)
+ memberCache.set(docId, id)
+ lsSet(k, JSON.stringify({ pub: id.pub }))
+ return id
+ }
+ const id = kept ?? saved() ?? await mintKeypair()
+ if (await setJSON(ds, 'memberkey', { docId, pub: id.pub, priv: id.priv }) === 'stored') {
+ unstored.delete(docId)
+ memberCache.set(docId, id)
+ lsSet(k, JSON.stringify({ pub: id.pub })) // the private half leaves this origin
+ return id
+ }
+ unstored.set(docId, id) // this page only; the host is offered it again next time
+ return id
+ }
+ const prior = saved()
+ if (prior) { memberCache.set(docId, prior); return prior }
const id = await mintKeypair()
lsSet(k, JSON.stringify(id))
+ memberCache.set(docId, id)
return id
}
@@ -373,7 +442,16 @@ export class OnlineTransport implements Transport {
// offload. Sent by every writer shape; honoured only after `prove`.
this.url = `${room}?tok=${tok}&bt=1&w=${a.pub}`
} else if (a?.kind === 'chain') {
- const id = await deviceIdentity(this.docId)
+ // Under a host with per-document storage, this device's key can't be
+ // READ while the host is busy, and deviceIdentity then fails rather than
+ // invent a new key (that would change this device's identity). Wait and
+ // retry; init is not awaited, so a throw here would leave the socket
+ // never connecting. Without such a host this never fails.
+ let id: { pub: string; priv: string } | null = null
+ for (let wait = 2000; !id; wait = Math.min(wait * 2, 30_000)) {
+ if (this.closed) return
+ try { id = await deviceIdentity(this.docId) } catch { await new Promise((r) => setTimeout(r, wait)) }
+ }
try { this.signKey = await importSignKey(id.priv) } catch { this.signKey = null }
this.myPub = id.pub
const iv = a.invite
diff --git a/kernel/src/sync/session.ts b/kernel/src/sync/session.ts
index 3fb47e95..eaf630a3 100644
--- a/kernel/src/sync/session.ts
+++ b/kernel/src/sync/session.ts
@@ -32,7 +32,7 @@ import {
// every consumer meets them through the session.
export type { BlobRef, CollabCreds, SyncDoc }
import { putBlob, getBlob, dataUriToBytes, bytesToDataUri, encodedSize, MAX_BLOB } from './blobs.ts'
-import { mintCollab } from './online.ts'
+import { mintCollab, cachedMemberPub } from './online.ts'
import { lsGet, lsJson } from '../storage.ts'
/**
@@ -817,7 +817,9 @@ export class SyncSession {
else if (c.v === 2 && c.ownerPriv) { role = 'owner'; pub = c.owner }
else if (c.v === 2 && c.invite) {
role = 'editor'
- pub = lsJson<{ pub?: string } | null>(`bento-member-${this.store.doc.docId}`, null)?.pub
+ // the identity this page resolved first; the stored pubkey otherwise
+ pub = cachedMemberPub(this.store.doc.docId) ??
+ lsJson<{ pub?: string } | null>(`bento-member-${this.store.doc.docId}`, null)?.pub
} else if (c.writerPriv) { role = 'editor'; pub = c.writerPub }
}
const where = this.host.presence()
diff --git a/scripts/test-docstore.ts b/scripts/test-docstore.ts
new file mode 100644
index 00000000..8a0bccf2
--- /dev/null
+++ b/scripts/test-docstore.ts
@@ -0,0 +1,270 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// DocStore rig: per-document side data held by the host.
+//
+// node scripts/test-docstore.ts
+//
+// Under the bento/home extension, a document's crash-recovery copy and its
+// member key live in the extension's per-document store instead of the storage
+// origin every local file shares. This rig stands in for the extension: a page
+// that announces `window.__bentoHost.ops` including 'store', answering the same
+// request/response protocol the extension's relay speaks, and accepting only the
+// names the real worker accepts. It proves:
+// - with no host, nothing changes (localStorage / IndexedDB as before);
+// - with a host, recovery round-trips through it, and only for THIS document;
+// - the member key's PRIVATE half lives in the host; localStorage keeps only
+// the public key, so every existing reader of `bento-member-<docId>`.pub
+// still recognises this device;
+// - a key the browser already held is adopted (same pubkey, same People
+// entry), and its private half removed from localStorage;
+// - while the host is present, NOTHING falls back to the shared origin: a
+// host that refuses or never answers leaves no new private key in
+// localStorage and no new recovery copy in IndexedDB; a failed read never
+// mints a key (the device's identity can't change behind a slow host);
+// - a refused recovery turns recovery off for that document, said once;
+// - only the two agreed names are ever used, and values travel as bytes.
+//
+// Mutations (verified by editing kernel/src and re-running; each fails by exit
+// code): drop the per-file docId check in getRecovery; keep the private key in
+// localStorage on adoption; drop the public-key mirror; restore the shared-
+// origin fallback on a failed member-key read.
+
+// ---- browser surface: localStorage, IndexedDB, a window that posts ---------
+const ls = new Map<string, string>()
+;(globalThis as { localStorage?: unknown }).localStorage = {
+ getItem: (k: string) => (ls.has(k) ? ls.get(k)! : null),
+ setItem: (k: string, v: string) => { ls.set(k, String(v)) },
+ removeItem: (k: string) => { ls.delete(k) },
+}
+// A recording IndexedDB, enough for autosave.ts: every write to an object store
+// is logged, so a fallback into the shared origin can be SEEN, not assumed.
+const idb = new Map<string, unknown>()
+const idbWrites: string[] = []
+const later = <T>(r: { result?: T; onsuccess?: () => void }, v: T) => { setTimeout(() => { r.result = v; r.onsuccess?.() }, 0); return r }
+;(globalThis as { indexedDB?: unknown }).indexedDB = {
+ open: () => {
+ const db = {
+ objectStoreNames: { contains: () => true },
+ createObjectStore: () => ({ createIndex() {} }),
+ close() {},
+ transaction: () => {
+ const t: { oncomplete?: () => void; objectStore: (s: string) => unknown } = {
+ objectStore: (s: string) => ({
+ put: (v: { docId: string }) => { idbWrites.push(`put ${s}`); idb.set(`${s}:${v.docId}`, v); return later({}, v.docId) },
+ add: () => { idbWrites.push(`add ${s}`); return later({}, 1) },
+ get: (k: string) => later({}, idb.get(`${s}:${k}`)),
+ getAll: () => later({}, [...idb].filter(([k]) => k.startsWith(`${s}:`)).map(([, v]) => v)),
+ delete: (k: string) => { idb.delete(`${s}:${k}`); return later({}, undefined) },
+ }),
+ }
+ setTimeout(() => t.oncomplete?.(), 0)
+ return t
+ },
+ }
+ const req: { result?: unknown; onupgradeneeded?: () => void; onsuccess?: () => void } = { result: db }
+ setTimeout(() => { req.onupgradeneeded?.(); req.onsuccess?.() }, 0)
+ return req
+ },
+}
+type Listener = (ev: { data: unknown; source: unknown }) => void
+const listeners = new Set<Listener>()
+const win = {
+ addEventListener: (t: string, fn: Listener) => { if (t === 'message') listeners.add(fn) },
+ removeEventListener: (t: string, fn: Listener) => { if (t === 'message') listeners.delete(fn) },
+ postMessage: (data: unknown) => {
+ const clone = structuredClone(data) // what the browser hands the other side
+ setTimeout(() => { for (const fn of [...listeners]) fn({ data: clone, source: win }) }, 0)
+ },
+ setTimeout, clearTimeout, setInterval, clearInterval,
+}
+;(globalThis as { window?: unknown }).window = win
+
+// ---- the stand-in extension ------------------------------------------------
+const CH = '__bento_tray__'
+const NAMES = new Set(['recovery', 'memberkey']) // what the real worker accepts
+let mode: 'ok' | 'refuse' | 'silent' | 'supersede' = 'ok'
+const store = new Map<string, Uint8Array>()
+const seen: Array<{ op: string; name?: string; bytesIsU8?: boolean }> = []
+win.addEventListener('message', (ev) => {
+ const d = ev.data as Record<string, unknown> | null
+ if (!d || d[CH] !== true || d.dir !== 'req' || typeof d.op !== 'string' || !d.op.startsWith('store.')) return
+ const p = (d.payload ?? {}) as { name?: string; bytes?: unknown }
+ seen.push({ op: d.op, name: p.name, bytesIsU8: d.op === 'store.set' ? p.bytes instanceof Uint8Array : undefined })
+ if (mode === 'silent') return
+ let result: unknown
+ if (d.op !== 'store.list' && !NAMES.has(String(p.name))) result = { ok: false, reason: 'bad name' }
+ else if (d.op === 'store.set') result = mode === 'refuse' ? { ok: false, reason: 'too large' }
+ : mode === 'supersede' ? { ok: false, reason: 'superseded' } // a newer write committed first
+ : (store.set(p.name!, p.bytes as Uint8Array), { ok: true })
+ else if (d.op === 'store.get') result = { ok: true, bytes: store.get(p.name!) ?? null }
+ else if (d.op === 'store.delete') result = (store.delete(p.name!), { ok: true })
+ else if (d.op === 'store.list') result = { ok: true, names: [...store.keys()].sort() }
+ win.postMessage({ [CH]: true, dir: 'res', id: d.id, result })
+})
+const setHost = (on: boolean) => {
+ if (on) (globalThis as { __bentoHost?: unknown }).__bentoHost = { name: 'test', ops: ['claim', 'write', 'store'] }
+ else delete (globalThis as { __bentoHost?: unknown }).__bentoHost
+}
+
+// session.ts (reached through online.ts) uses a TS parameter property, which
+// strip-only node can't load; the app's own esbuild transform, as the session
+// rigs do. Registered before the dynamic imports below.
+const { register } = await import('node:module')
+register('./lib/ts-resolve-hooks.mjs', import.meta.url)
+
+const { configureApp } = await import('../kernel/src/app.ts')
+configureApp({ appId: 'bento-rig', appName: 'Rig', manifestUrl: 'https://example.invalid/m.json' })
+const { docStore, resetDocStoreForTest, ExtensionBackend } = await import('../kernel/src/docstore.ts')
+const { putRecovery, getRecovery, clearRecovery, recoveryOff, onRecoveryOff } = await import('../kernel/src/autosave.ts')
+// One module instance; the member-key cache is per docId, and every row uses
+// its own docId, so no row sees another's cached identity.
+const { deviceIdentity, cachedMemberPub } = await import('../kernel/src/sync/online.ts')
+
+let checks = 0, failures = 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 doc = (docId: string, title = 'A deck') => ({ docId, title, slides: [{ id: 's1', note: 'unsaved work' }], collab: { key: 'K' } })
+const dec = new TextDecoder()
+const enc = new TextEncoder()
+const stored = (name: string) => { const b = store.get(name); return b ? JSON.parse(dec.decode(b)) : null }
+const reset = () => { store.clear(); ls.clear(); idb.clear(); idbWrites.length = 0; seen.length = 0; mode = 'ok' }
+const fast = () => new ExtensionBackend({ firstProbe: 40, get: 40, setBase: 40, setPerChunk: 0, small: 40 })
+
+H('no host: nothing changes')
+{
+ reset(); setHost(false); resetDocStoreForTest()
+ ok(docStore() === null, 'no __bentoHost store capability → no DocStore')
+ const id = await deviceIdentity('d-nohost')
+ const raw = JSON.parse(ls.get('bento-member-d-nohost') ?? 'null')
+ ok(raw?.pub === id.pub && raw?.priv === id.priv, 'the member key stays whole in localStorage, as before')
+ ok(await putRecovery(doc('d-nohost') as never) === true && idbWrites.includes('put recovery'), 'recovery goes to IndexedDB, as before')
+ ok(seen.length === 0, 'nothing is sent to a host that is not there')
+}
+
+H('recovery round-trips through the host, for THIS document only')
+{
+ reset(); setHost(true); resetDocStoreForTest()
+ ok(docStore() !== null, 'a host announcing store → a DocStore')
+ ok(await putRecovery(doc('d1') as never) === true, 'putRecovery reports it stored')
+ const held = stored('recovery')
+ ok(held?.docId === 'd1' && typeof held?.json === 'string' && held.json.includes('unsaved work'), 'the host holds this document\'s recovery copy')
+ ok(!held?.json.includes('"collab"'), 'the recovery copy is content-only, as before')
+ ok(!idbWrites.includes('put recovery'), 'and nothing was written to IndexedDB')
+ const back = await getRecovery('d1')
+ ok(back?.docId === 'd1' && back.json === held.json, 'getRecovery returns it')
+ ok(await getRecovery('some-other-doc') === null, 'a DIFFERENT document at this path gets nothing (the entry is per file)')
+ await clearRecovery('d1')
+ ok(!store.has('recovery'), 'clearRecovery removes it from the host')
+ // migration: an old copy this origin holds from before the host is still readable
+ idb.set('recovery:d1old', { docId: 'd1old', at: 1, title: 't', json: '{}' })
+ ok((await getRecovery('d1old'))?.docId === 'd1old', 'an older copy left in IndexedDB is still read (migration)')
+}
+
+H('member key: the private half lives in the host; localStorage keeps only the pubkey')
+{
+ reset(); setHost(true); resetDocStoreForTest()
+ const id = await deviceIdentity('d2')
+ const held = stored('memberkey')
+ ok(held?.docId === 'd2' && held.pub === id.pub && held.priv === id.priv, 'the host holds the full member key, tagged with its document')
+ const mirror = JSON.parse(ls.get('bento-member-d2') ?? 'null')
+ ok(mirror?.pub === id.pub, 'localStorage still names this device\'s pubkey (the People views read it)')
+ ok(mirror && !('priv' in mirror), 'and holds NO private key')
+ ok(cachedMemberPub('d2') === id.pub, 'presence can read the pubkey this page resolved')
+ // a later page: the host already holds this document's key, localStorage has nothing
+ store.set('memberkey', enc.encode(JSON.stringify({ docId: 'd2b', pub: 'HELD-PUB', priv: 'HELD-PRIV' })))
+ const id2 = await deviceIdentity('d2b')
+ ok(id2.pub === 'HELD-PUB' && id2.priv === 'HELD-PRIV', 'a later page gets its identity back from the host')
+ ok(JSON.parse(ls.get('bento-member-d2b') ?? 'null')?.pub === 'HELD-PUB', 'and re-publishes the pubkey for the readers')
+}
+
+H('a key this browser already held is adopted, and its private half leaves localStorage')
+{
+ reset(); setHost(true); resetDocStoreForTest()
+ ls.set('bento-member-d3', JSON.stringify({ pub: 'OLD-PUB', priv: 'OLD-PRIV' }))
+ const id = await deviceIdentity('d3')
+ ok(id.pub === 'OLD-PUB' && id.priv === 'OLD-PRIV', 'the same identity: this device keeps its People entry')
+ ok(stored('memberkey')?.priv === 'OLD-PRIV', 'the host now holds the private key')
+ const mirror = JSON.parse(ls.get('bento-member-d3') ?? 'null')
+ ok(mirror?.pub === 'OLD-PUB' && !('priv' in mirror), 'localStorage keeps the pubkey and no longer the private key')
+}
+
+H('a host entry for another document is never used')
+{
+ reset(); setHost(true); resetDocStoreForTest()
+ store.set('memberkey', enc.encode(JSON.stringify({ docId: 'someone-else', pub: 'X-PUB', priv: 'X-PRIV' })))
+ const id = await deviceIdentity('d4')
+ ok(id.pub !== 'X-PUB', 'a key tagged for another document is not adopted')
+ ok(stored('memberkey')?.docId === 'd4', 'this document\'s own key replaces it')
+}
+
+H('a REFUSING host: nothing falls back to the shared origin')
+{
+ reset(); setHost(true); resetDocStoreForTest(); mode = 'refuse'
+ const lsBefore = JSON.stringify([...ls])
+ const id = await deviceIdentity('d5')
+ ok(JSON.stringify([...ls]) === lsBefore, 'no new entry in localStorage — the private key is never written there')
+ ok(cachedMemberPub('d5') === id.pub, 'the identity is kept for this page')
+ // the host goes SILENT while this page holds that unstored identity: a failed
+ // read carries on with it (no throw, no new key), so the connection doesn't stall
+ resetDocStoreForTest(fast()); mode = 'silent'
+ let kept: { pub: string } | null = null
+ try { kept = await deviceIdentity('d5') } catch { kept = null }
+ ok(kept?.pub === id.pub, 'a failed read with an identity already in use this page carries on with it')
+ ok(JSON.stringify([...ls]) === lsBefore, 'still nothing new in localStorage')
+ // the host comes back: the SAME identity is offered again and stored
+ resetDocStoreForTest(); mode = 'ok'
+ const again = await deviceIdentity('d5')
+ ok(again.pub === id.pub && stored('memberkey')?.pub === id.pub, 'next call offers the same identity to the host, which now stores it')
+ // recovery refused (too large): off for that document, said once, nothing in IndexedDB
+ mode = 'refuse'
+ let told = 0
+ const off = onRecoveryOff(() => { told++ })
+ ok(await putRecovery(doc('d5') as never) === false, 'a refused recovery reports not stored')
+ await putRecovery(doc('d5') as never)
+ off()
+ ok(recoveryOff('d5') && told === 1, 'recovery is turned off for that document, and the app is told once')
+ ok(!idbWrites.some((w) => w.endsWith('recovery')), 'and nothing was written to IndexedDB')
+}
+
+H('a SILENT host: no fallback, and a failed read never mints or writes a key')
+{
+ reset(); setHost(true); resetDocStoreForTest(fast()); mode = 'silent'
+ // this device already has an identity: the host stored it on an earlier page,
+ // and localStorage carries only its pubkey
+ ls.set('bento-member-d6', JSON.stringify({ pub: 'MY-PUB' }))
+ let threw = false
+ try { await deviceIdentity('d6') } catch { threw = true }
+ ok(threw, 'the read fails the attempt (the caller retries) instead of inventing a key')
+ ok(JSON.parse(ls.get('bento-member-d6') ?? 'null')?.pub === 'MY-PUB' && !ls.get('bento-member-d6')!.includes('priv'),
+ 'the device\'s pubkey is unchanged and no private key was written')
+ ok(await putRecovery(doc('d6') as never) === false, 'recovery: not stored this cycle')
+ ok(!idbWrites.some((w) => w.endsWith('recovery')) && !recoveryOff('d6'), 'nothing in IndexedDB, and recovery stays on for the next cycle')
+ ok(await getRecovery('d6') === null, 'getRecovery falls back to reading only, and returns nothing (no throw)')
+}
+
+H('a SUPERSEDED write (a newer one committed first) is not a refusal')
+{
+ // Two overlapping autosave writes: the host keeps the newer and answers the
+ // older 'superseded'. That must not switch recovery off — the document's
+ // newer copy is exactly what the host holds.
+ reset(); setHost(true); resetDocStoreForTest(); mode = 'supersede'
+ ok(await putRecovery(doc('d8') as never) === false, 'the superseded write reports not stored (this one lost to a newer)')
+ ok(!recoveryOff('d8'), 'recovery stays ON for the document')
+ ok(!idbWrites.some((w) => w.endsWith('recovery')), 'and nothing was written to IndexedDB')
+}
+
+H('only the two agreed names are used, and values travel as bytes')
+{
+ reset(); setHost(true); resetDocStoreForTest()
+ await putRecovery(doc('d7') as never)
+ await deviceIdentity('d7')
+ const sets = seen.filter((s) => s.op === 'store.set')
+ ok(seen.every((s) => s.op === 'store.list' || s.name === 'recovery' || s.name === 'memberkey'),
+ `names used: ${[...new Set(seen.map((s) => s.name))].join(', ')}`)
+ ok(sets.length >= 2 && sets.every((s) => s.bytesIsU8), 'every store.set carries a Uint8Array')
+ ok((await docStore()!.list()).join(',') === 'memberkey,recovery', 'list returns exactly the two entries')
+}
+
+console.log(`\n${checks - failures}/${checks} checks passed`)
+process.exit(failures ? 1 : 0)