{"draft":null,"from":161,"to":162,"files":[{"path":"server/test/disk-first-runtime.test.ts","added":358,"removed":2,"diff":"--- a/server/test/disk-first-runtime.test.ts\n+++ b/server/test/disk-first-runtime.test.ts\n@@ -4,11 +4,96 @@\n import { join } from \"node:path\";\n import { test } from \"node:test\";\n import { answer } from \"../../worker/project/http.ts\";\n-import { Project } from \"../../worker/project/project.ts\";\n+import { MemoryStore, Project } from \"../../worker/project/project.ts\";\n+import { where } from \"../../worker/project/where.ts\";\n import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n-import { SqliteLog } from \"../src/engine-project.ts\";\n+import { type EngineProject, SqliteLog } from \"../src/engine-project.ts\";\n+import { SqliteProjectionStore } from \"../src/projection-store.ts\";\n+import { SqliteWriterHost, SqliteWriterState } from \"../src/writer-host.ts\";\n import { memoryStorage } from \"./helpers.ts\";\n \n+test(\"runtime defers a large later frame and adopts it on its own page\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst memory = new MemoryStore();\n+\tconst project = new Project(memory);\n+\tproject.init({ \"a.txt\": \"small\\n\" }, 0);\n+\tproject.record(\"alice\", { \"a.txt\": `${\"x\".repeat(1_100_000)}\\n` }, 1);\n+\tproject.publish(\"alice\", \"Large entry\", 2);\n+\tconst log = new SqliteLog(storage);\n+\tlog.append(memory.entries);\n+\tconst large = memory.entries.findIndex((entry) => Buffer.byteLength(JSON.stringify(entry)) > 1_048_576);\n+\tassert.ok(large > 0);\n+\tconst before = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tt.mock.method(Project, \"load\", () => {\n+\t\tthrow Error(\"full-history replay forbidden\");\n+\t});\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"large-later\", \"large-later\");\n+\tconst first = runtime().preparePage(() => {});\n+\tassert.equal(first.phase, \"writer\");\n+\tassert.equal(first.next, large);\n+\tlet pages = 0;\n+\twhile (!runtime().preparePage(() => {}).ready) assert.ok(++pages < 100);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(calls) => calls.statusAt(log.seq()).version,\n+\t\t\t() => {},\n+\t\t),\n+\t\t1,\n+\t);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(calls) => calls.shared().files[\"a.txt\"]?.length,\n+\t\t\t() => {},\n+\t\t),\n+\t\t1_100_001,\n+\t);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), before);\n+});\n+\n+test(\"historical runtime preserves named draft bases and intermediate states after landing\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst memory = new MemoryStore();\n+\tconst project = new Project(memory);\n+\tproject.init({ \"a.txt\": \"one\\ntwo\\n\", \"b.txt\": \"other\\n\" }, 0);\n+\tproject.switchTo(\"alice\", \"rewrite\", 1);\n+\tproject.record(\"alice\", { \"a.txt\": \"ONE\\ntwo\\n\" }, 2);\n+\tproject.publish(\"alice\", \"First draft version\", 3);\n+\tproject.switchTo(\"bob\", \"rewrite\", 4);\n+\tproject.record(\"bob\", { \"b.txt\": \"OTHER\\n\" }, 5);\n+\tproject.publish(\"bob\", \"Second draft version\", 6);\n+\tassert.equal(project.land(\"bob\", \"Land rewrite\", 7).ok, true);\n+\tconst expected = memory.entries.map((_, i) => {\n+\t\tconst prefix = Project.load(memory.entries.slice(0, i + 1), new MemoryStore());\n+\t\treturn { status: prefix.status(), where: where(prefix) };\n+\t});\n+\tnew SqliteLog(storage).append(memory.entries);\n+\tt.mock.method(Project, \"load\", () => {\n+\t\tthrow Error(\"full-history replay forbidden\");\n+\t});\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"named-history\", \"named-history\");\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tfor (let at = 1; at <= expected.length; at++) {\n+\t\tassert.deepEqual(\n+\t\t\truntime().read(\n+\t\t\t\t(calls) => calls.statusAt(at),\n+\t\t\t\t() => {},\n+\t\t\t),\n+\t\t\t{ at, ...expected[at - 1]!.status },\n+\t\t\t`status at ${at}`,\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\truntime().read(\n+\t\t\t\t(calls) => calls.whereAt(at),\n+\t\t\t\t() => {},\n+\t\t\t),\n+\t\t\t{ at, ...expected[at - 1]!.where },\n+\t\t\t`where at ${at}`,\n+\t\t);\n+\t}\n+});\n+\n test(\"runtime resumes from disk and detaches reads, with exact authorized retries\", (t) => {\n \tconst directory = mkdtempSync(join(tmpdir(), \"abso-runtime-\"));\n \tconst filename = join(directory, \"state.sqlite\");\n@@ -42,12 +127,29 @@\n \t\tstorage = memoryStorage(filename);\n \t}\n \tassert.ok(pages > 0);\n+\tconst initialLength = new SqliteLog(storage).seq();\n+\tconst initialStatus = runtime().read((calls) => calls.statusAt(initialLength), validate);\n+\tconst initialWhere = runtime().read((calls) => calls.whereAt(initialLength), validate);\n+\t// Reproduce the published runtime's ready state before historical indexing existed.\n+\tnew SqliteWriterState(storage, \"disk-v1\").remove(\"runtime\", \"history-ready\");\n+\tstorage.db.exec(\n+\t\t\"DELETE FROM writer_state WHERE namespace LIKE 'disk-v1:history%'; DROP TABLE writer_history_keys; DROP TABLE writer_history_heads;\",\n+\t);\n+\tassert.throws(() => runtime().read((calls) => calls.status(), validate), /not complete/);\n+\tassert.throws(() => runtime().write(() => undefined, validate), /not complete/);\n+\twhile (!runtime().preparePage(validate).ready) {}\n+\tassert.deepEqual(\n+\t\truntime().read((calls) => calls.statusAt(initialLength), validate),\n+\t\tinitialStatus,\n+\t);\n \tconst req = { id: \"record-1\", identity: { agent: \"alice\", operation: \"record\", files: { \"a.txt\": \"changed\\n\" } } };\n \tconst operation = (calls: Parameters<Parameters<DiskFirstRuntime[\"write\"]>[0]>[0]) =>\n \t\tanswer(calls, \"record\", \"alice\", { files: req.identity.files });\n \tconst first = runtime().write(operation, validate, req);\n \tassert.equal(first.status, 200);\n \tconst length = new SqliteLog(storage).seq();\n+\tconst draftStatus = runtime().read((calls) => calls.statusAt(length), validate);\n+\tconst draftWhere = runtime().read((calls) => calls.whereAt(length), validate);\n \tstorage.db.close();\n \tstorage = memoryStorage(filename);\n \tassert.deepEqual(\n@@ -83,7 +185,32 @@\n \tstorage.db.close();\n \tstorage = memoryStorage(filename);\n \tassert.equal(runtime().preparePage(validate).ready, true);\n+\tassert.deepEqual(\n+\t\truntime().read((calls) => calls.statusAt(initialLength), validate),\n+\t\tinitialStatus,\n+\t);\n+\tassert.deepEqual(\n+\t\truntime().read((calls) => calls.whereAt(initialLength), validate),\n+\t\tinitialWhere,\n+\t);\n+\tconst end = new SqliteLog(storage).seq();\n+\tassert.deepEqual(\n+\t\truntime().read((calls) => calls.statusAt(length), validate),\n+\t\tdraftStatus,\n+\t);\n+\tassert.deepEqual(\n+\t\truntime().read((calls) => calls.whereAt(length), validate),\n+\t\tdraftWhere,\n+\t);\n \tassert.equal(\n+\t\truntime().read((calls) => calls.statusAt(end).version, validate),\n+\t\t1,\n+\t);\n+\tassert.equal(\n+\t\truntime().read((calls) => calls.whereAt(end).version, validate),\n+\t\t1,\n+\t);\n+\tassert.equal(\n \t\truntime().read((calls) => calls.shared().files[\"a.txt\"], validate),\n \t\t\"changed\\n\",\n \t);\n@@ -129,4 +256,233 @@\n \t\t/authority changed/,\n \t);\n \tassert.deepEqual(rows(), adopted);\n+\tconst originalWrite = SqliteWriterState.prototype.write;\n+\tconst failure = t.mock.method(\n+\t\tSqliteWriterState.prototype,\n+\t\t\"write\",\n+\t\tfunction (this: SqliteWriterState, collection: string, key: string, value: unknown) {\n+\t\t\tif (collection === \"runtime:receipts\") throw Error(\"receipt persistence failed\");\n+\t\t\treturn originalWrite.call(this, collection, key, value);\n+\t\t},\n+\t);\n+\tassert.throws(\n+\t\t() =>\n+\t\t\truntime.write(\n+\t\t\t\t(calls) => answer(calls, \"record\", \"alice\", { files: { \"a.txt\": \"two\\n\" } }),\n+\t\t\t\t() => {},\n+\t\t\t\t{ id: \"receipt-failed\", identity: \"record\" },\n+\t\t\t),\n+\t\t/receipt persistence failed/,\n+\t);\n+\tassert.deepEqual(rows(), adopted, \"receipt failure must roll back canonical state, history and its cursor\");\n+\tfailure.mock.restore();\n });\n+\n+test(\"runtime recovers after a writer without historical support commits\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst namespace = \"old-writer\";\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"recovery\", namespace);\n+\truntime().initialize(\n+\t\t(calls) => answer(calls, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst historical = runtime().read(\n+\t\t(calls) => calls.statusAt(1),\n+\t\t() => {},\n+\t);\n+\tconst projection = new SqliteProjectionStore(\n+\t\t{\n+\t\t\tdatabase: \"recovery\",\n+\t\t\tnamespace: `${namespace}:views`,\n+\t\t\tregistry: `${namespace}:roots`,\n+\t\t\tdefaultFile: \"__abso_projection_default__\",\n+\t\t},\n+\t\t1,\n+\t\t{ indexedAtoms: true },\n+\t);\n+\tconst oldWriter = () => new SqliteWriterHost(storage, namespace, projection, { indexProjectionAtoms: true });\n+\tfor (const [op, body] of [\n+\t\t[\"record\", { files: { \"a.txt\": \"two\\n\" } }],\n+\t\t[\"publish\", { message: \"Old writer\" }],\n+\t] as const) {\n+\t\tassert.equal(\n+\t\t\toldWriter().runCalls({\n+\t\t\t\texpected: oldWriter().source(),\n+\t\t\t\tvalidateAuthority: () => {},\n+\t\t\t\tcheckerReady: false,\n+\t\t\t\tcommitReceipt: () => {},\n+\t\t\t\toperation: (calls) => answer(calls, op, \"alice\", body),\n+\t\t\t}).status,\n+\t\t\t200,\n+\t\t);\n+\t}\n+\tt.mock.method(Project, \"load\", () => {\n+\t\tthrow Error(\"full-history load forbidden\");\n+\t});\n+\tlet ready = false;\n+\tfor (let page = 0; page < 100 && !ready; page++) ready = runtime().preparePage(() => {}).ready;\n+\tassert.equal(ready, true);\n+\tassert.deepEqual(\n+\t\truntime().read(\n+\t\t\t(calls) => calls.statusAt(1),\n+\t\t\t() => {},\n+\t\t),\n+\t\thistorical,\n+\t);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(calls) => calls.shared().files[\"a.txt\"],\n+\t\t\t() => {},\n+\t\t),\n+\t\t\"two\\n\",\n+\t);\n+\tassert.equal(\n+\t\truntime().write(\n+\t\t\t(calls) => answer(calls, \"record\", \"alice\", { files: { \"a.txt\": \"three\\n\" } }),\n+\t\t\t() => {},\n+\t\t).status,\n+\t\t200,\n+\t);\n+});\n+\n+test(\"a large retry response must not roll back an otherwise successful write\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"large-receipt\", \"large-receipt\");\n+\truntime().initialize(\n+\t\t(calls) => answer(calls, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst large = \"x\".repeat(8_100_000);\n+\tconst request = { id: \"large-response\", identity: { agent: \"alice\", operation: \"large-fixture\" } };\n+\tconst result = runtime().write(\n+\t\t(calls) => {\n+\t\t\tconst recorded = answer(calls, \"record\", \"alice\", { files: { \"a.txt\": \"two\\n\" } });\n+\t\t\tassert.equal(recorded.status, 200);\n+\t\t\treturn { status: 200, body: large };\n+\t\t},\n+\t\t() => {},\n+\t\trequest,\n+\t);\n+\tassert.equal(result.body.length, large.length);\n+\tconst before = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tconst replay = runtime().write(\n+\t\t() => {\n+\t\t\tthrow Error(\"receipt replay must not execute twice\");\n+\t\t},\n+\t\t() => {},\n+\t\trequest,\n+\t) as typeof result;\n+\tassert.deepEqual(replay, result);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), before);\n+});\n+\n+test(\"chunked receipts roll back failures and reject damaged replay\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"receipt-failure\", \"receipt-failure\");\n+\truntime().initialize(\n+\t\t(calls) => answer(calls, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst rows = () => ({\n+\t\tlog: storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(),\n+\t\tstate: storage.db.prepare(\"SELECT * FROM writer_state ORDER BY namespace,collection,key,part\").all(),\n+\t\thistory: storage.db.prepare(\"SELECT * FROM writer_history_keys ORDER BY namespace,collection,key,revision\").all(),\n+\t\theads: storage.db.prepare(\"SELECT * FROM writer_history_heads ORDER BY namespace\").all(),\n+\t});\n+\tconst before = rows();\n+\tconst original = SqliteWriterState.prototype.write;\n+\tconst failure = t.mock.method(\n+\t\tSqliteWriterState.prototype,\n+\t\t\"write\",\n+\t\tfunction (this: SqliteWriterState, collection: string, key: string, value: unknown) {\n+\t\t\toriginal.call(this, collection, key, value);\n+\t\t\tif (collection === \"runtime:receipt:chunk-failure\" && key === \"1\") throw Error(\"injected receipt chunk failure\");\n+\t\t},\n+\t);\n+\tconst request = { id: \"chunk-failure\", identity: { operation: \"fixture\" } };\n+\tconst operation = (calls: EngineProject) => {\n+\t\tassert.equal(answer(calls, \"record\", \"alice\", { files: { \"a.txt\": \"two\\n\" } }).status, 200);\n+\t\treturn { body: \"x\".repeat(140000) };\n+\t};\n+\tassert.throws(() => runtime().write(operation, () => {}, request), /injected receipt chunk failure/);\n+\tassert.deepEqual(rows(), before);\n+\tfailure.mock.restore();\n+\tconst result = runtime().write(operation, () => {}, request);\n+\tconst committed = rows();\n+\tassert.deepEqual(\n+\t\truntime().write(\n+\t\t\t() => {\n+\t\t\t\tthrow Error(\"must not execute\");\n+\t\t\t},\n+\t\t\t() => {},\n+\t\t\trequest,\n+\t\t),\n+\t\tresult,\n+\t);\n+\tassert.deepEqual(rows(), committed);\n+\tconst state = new SqliteWriterState(storage, \"receipt-failure\");\n+\tconst chunk = state.read(\"runtime:receipt:chunk-failure\", \"1\") as string;\n+\tstate.write(\"runtime:receipt:chunk-failure\", \"1\", \"y\" + chunk.slice(1));\n+\tassert.throws(\n+\t\t() =>\n+\t\t\truntime().write(\n+\t\t\t\t() => {\n+\t\t\t\t\tthrow Error(\"must not execute\");\n+\t\t\t\t},\n+\t\t\t\t() => {},\n+\t\t\t\trequest,\n+\t\t\t),\n+\t\t/digest mismatch/,\n+\t);\n+\tstate.remove(\"runtime:receipt:chunk-failure\", \"1\");\n+\tassert.throws(\n+\t\t() =>\n+\t\t\truntime().write(\n+\t\t\t\t() => {\n+\t\t\t\t\tthrow Error(\"must not execute\");\n+\t\t\t\t},\n+\t\t\t\t() => {},\n+\t\t\t\trequest,\n+\t\t\t),\n+\t\t/incomplete receipt/,\n+\t);\n+\tassert.deepEqual(rows().log, committed.log);\n+});\n+\n+test(\"runtime format upgrade is authorized, atomic, and rejects unknown formats\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"format-test\", \"format-test\");\n+\truntime().initialize(\n+\t\t(calls) => answer(calls, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst state = new SqliteWriterState(storage, \"format-test\");\n+\tassert.equal(state.read(\"runtime\", \"format\"), 5);\n+\tstate.write(\"runtime\", \"format\", 3);\n+\tconst before = storage.db.prepare(\"SELECT * FROM writer_state ORDER BY namespace,collection,key,part\").all();\n+\tlet validations = 0;\n+\tassert.throws(\n+\t\t() =>\n+\t\t\truntime().preparePage(() => {\n+\t\t\t\tif (++validations === 2) throw Error(\"revoked during upgrade\");\n+\t\t\t}),\n+\t\t/revoked/,\n+\t);\n+\tassert.deepEqual(\n+\t\tstorage.db.prepare(\"SELECT * FROM writer_state ORDER BY namespace,collection,key,part\").all(),\n+\t\tbefore,\n+\t);\n+\tassert.equal(runtime().preparePage(() => {}).ready, true);\n+\tassert.equal(state.read(\"runtime\", \"format\"), 5);\n+\tstate.write(\"runtime\", \"format\", 999);\n+\tassert.throws(() => runtime().preparePage(() => {}), /unsupported disk-first runtime format/);\n+\tassert.equal(state.read(\"runtime\", \"format\"), 999);\n+});"}],"unmatched":[]}