diff --git a/.agents/web.md b/.agents/web.md index 21f9c38..8f4ff18 100644 --- a/.agents/web.md +++ b/.agents/web.md @@ -488,8 +488,9 @@ sets sessionStorage `kw-resume` and reloads the same URL; the load reads it once and resumes without asking. A plain load offers to resume (`ui.ask` with Resume and Boot fresh) before the kernel starts. -- `session.ts` keeps one session in IndexedDB `kwsnap:session-v1`, its own - database: `meta` (time, the page's kernel URL, the banner, the device +- `session.ts` keeps one session in IndexedDB `kwsnap:session-v2`, its own + database (the first open on a page deletes `kwsnap:session-v1` and any + other older version, unawaited): `meta` (time, the page's kernel URL, the banner, the device options, the disks' snapshot state, the console's last 64 KiB) and `bytes` (the `encodeSnapshot()` Blob), written in one transaction. It never opens `kwdisk:kwdata-v1`. diff --git a/web/public/reset.html b/web/public/reset.html index 9d7d05c..de94d81 100644 --- a/web/public/reset.html +++ b/web/public/reset.html @@ -40,7 +40,7 @@ "use strict"; const DATA_DISK = "kwdisk:kwdata-v1"; /** Databases the page is known to open, for browsers without indexedDB.databases(). */ -const KNOWN_DBS = [DATA_DISK, "kwsnap:session-v1"]; +const KNOWN_DBS = [DATA_DISK, "kwsnap:session-v2", "kwsnap:session-v1"]; const out = document.getElementById("log"); const log = (s) => { out.textContent += s + "\n"; }; diff --git a/web/src/session.ts b/web/src/session.ts index 7d65e32..37f4c44 100644 --- a/web/src/session.ts +++ b/web/src/session.ts @@ -2,8 +2,9 @@ // Copyright (C) 2026 Pooya Parsa // The suspended session: Suspend (Ctrl-A z, KW.suspend()) saves the running // machine with the SDK's snapshot({ stop: true }) and keeps it here, in -// IndexedDB "kwsnap:session-v1" (its own database; the data disk's -// kwdisk:kwdata-v1 is never touched here); the next load offers to resume +// IndexedDB "kwsnap:session-v2" (its own database, older versions are +// deleted on first use; the data disk's kwdisk:kwdata-v1 is never touched +// here); the next load offers to resume // it (boot.ts). One session at a time: "meta" (small, read to decide // whether to offer) and "bytes" (the encoded snapshot, a Blob) are written // in one transaction and deleted together. @@ -16,9 +17,25 @@ // guest keeps writing /data, which makes it unrestorable within seconds. import { decodeSnapshot, encodeSnapshot, type DiskSnapshotState, type KernelSnapshot } from "./sdk.ts"; -const DB = "kwsnap:session-v1"; +const DB_PREFIX = "kwsnap:session-v"; +const DB_VERSION = 2; +const DB = `${DB_PREFIX}${DB_VERSION}`; const STORE = "session"; +let oldDropped = false; +/** + * Delete the databases of older versions (kwsnap:session-v1 up to the one + * before DB_VERSION), once per page. Not awaited: deleting a missing one is + * a no-op, and one another tab still holds is deleted when that tab closes. + */ +function dropOldDbs(): void { + if (oldDropped) return; + oldDropped = true; + for (let v = 1; v < DB_VERSION; v++) { + try { indexedDB.deleteDatabase(`${DB_PREFIX}${v}`); } catch { /* ignore */ } + } +} + /** What the page needs to decide whether it can offer to resume. */ export interface SessionMeta { /** Date.now() when it was saved. */ @@ -59,6 +76,7 @@ const reqP = (r: IDBRequest): Promise => new Promise((res, rej) => { r.onsuccess = () => res(r.result); r.onerror = () => rej(r.error); }); function openDb(): Promise { + dropOldDbs(); return new Promise((resolve, reject) => { const r = indexedDB.open(DB, 1); r.onupgradeneeded = () => { r.result.createObjectStore(STORE); };