{"draft":null,"from":157,"to":158,"files":[{"path":"server/src/disk-first-runtime.ts","added":167,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/disk-first-runtime.ts\n@@ -0,0 +1,167 @@\n+import { canonicalJson } from \"../../worker/project/operation-protocol.ts\";\n+import { Project, type WriterSource } from \"../../worker/project/project.ts\";\n+import type { ProjectOptions } from \"../../worker/project/project-types.ts\";\n+import { EngineProject, SqliteLog, type SqliteStorage } from \"./engine-project.ts\";\n+import { SqliteProjectionStore } from \"./projection-store.ts\";\n+import { SqliteWriterHost, SqliteWriterState } from \"./writer-host.ts\";\n+\n+type Services = Pick<ProjectOptions, \"hasBlob\" | \"whole\">;\n+type Progress = { ready: boolean; phase: \"empty\" | \"writer\" | \"projection\" | \"ready\"; next: number };\n+\n+/** One synchronous page or call per invocation. No object retains a loaded Project.\n+ * The owner schedules subsequent pages and supplies fresh authorization for every call. */\n+export class DiskFirstRuntime {\n+\treadonly #storage: SqliteStorage;\n+\treadonly #namespace: string;\n+\treadonly #projection: SqliteProjectionStore;\n+\treadonly #services: Services;\n+\tconstructor(storage: SqliteStorage, database: string, namespace: string, services: Services = {}) {\n+\t\tthis.#storage = storage;\n+\t\tthis.#namespace = namespace;\n+\t\tthis.#services = services;\n+\t\tthis.#projection = new SqliteProjectionStore(\n+\t\t\t{\n+\t\t\t\tdatabase,\n+\t\t\t\tnamespace: `${namespace}:views`,\n+\t\t\t\tregistry: `${namespace}:roots`,\n+\t\t\t\tdefaultFile: \"__abso_projection_default__\",\n+\t\t\t},\n+\t\t\t1,\n+\t\t\t{ indexedAtoms: true },\n+\t\t);\n+\t}\n+\t#host() {\n+\t\treturn new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, { indexProjectionAtoms: true });\n+\t}\n+\t#state() {\n+\t\treturn new SqliteWriterState(this.#storage, this.#namespace);\n+\t}\n+\t#validate(validate: () => void) {\n+\t\tconst result: unknown = validate();\n+\t\tif (result && (typeof result === \"object\" || typeof result === \"function\") && \"then\" in result)\n+\t\t\tthrow Error(\"runtime authorization must be synchronous\");\n+\t}\n+\t#setup() {\n+\t\tSqliteWriterState.initialize(this.#storage);\n+\t\tconst state = this.#state();\n+\t\tconst format = state.read(\"runtime\", \"format\");\n+\t\tif (format !== undefined && format !== 1) throw Error(\"unsupported disk-first runtime format\");\n+\t\tif (format === undefined) state.write(\"runtime\", \"format\", 1);\n+\t\treturn state;\n+\t}\n+\t/** Initializes an empty canonical log without replay. Existing history must be adopted. */\n+\tinitialize<T>(operation: (calls: EngineProject) => T, validate: () => void): T {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tthis.#validate(validate);\n+\t\t\tconst log = new SqliteLog(this.#storage);\n+\t\t\tif (log.seq() !== 0) throw Error(\"disk-first initialization requires an empty log\");\n+\t\t\tconst project = new Project(log, { ...this.#services, now: () => Date.now(), shapes: log.shapes() });\n+\t\t\tconst result = structuredClone(operation(new EngineProject(project)));\n+\t\t\tthis.#validate(validate);\n+\t\t\treturn result;\n+\t\t});\n+\t}\n+\tpreparePage(validate: () => void): Progress {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tthis.#validate(validate);\n+\t\t\tconst log = new SqliteLog(this.#storage);\n+\t\t\tif (log.seq() === 0) {\n+\t\t\t\tthis.#validate(validate);\n+\t\t\t\treturn { ready: false, phase: \"empty\", next: 0 };\n+\t\t\t}\n+\t\t\tconst state = this.#setup();\n+\t\t\tconst host = this.#host();\n+\t\t\tconst ready = state.read(\"runtime\", \"ready\");\n+\t\t\tif (ready !== undefined && ready !== true) throw Error(\"invalid runtime readiness marker\");\n+\t\t\tif (ready === true) {\n+\t\t\t\tconst root = host.root();\n+\t\t\t\tconst scope = this.#projection.open({ state, log, root });\n+\t\t\t\tscope.close();\n+\t\t\t\tthis.#validate(validate);\n+\t\t\t\treturn { ready: true, phase: \"ready\", next: root.logLength };\n+\t\t\t}\n+\t\t\tconst bound = state.read(\"host\", \"canonical-root\");\n+\t\t\tif (bound === undefined) {\n+\t\t\t\tconst cursor = state.read(\"host\", \"adoption-root\") as\n+\t\t\t\t\t| { source: WriterSource; next: number; digest: string }\n+\t\t\t\t\t| undefined;\n+\t\t\t\tconst page = host.adoptVerifiedPage(\n+\t\t\t\t\t{\n+\t\t\t\t\t\texpected: cursor?.source ?? host.source(),\n+\t\t\t\t\t\tfrom: cursor?.next ?? 0,\n+\t\t\t\t\t\tdigest: cursor?.digest ?? \"\",\n+\t\t\t\t\t\tmaxEntries: 32,\n+\t\t\t\t\t\tmaxBytes: 1_048_576,\n+\t\t\t\t\t},\n+\t\t\t\t\tvalidate,\n+\t\t\t\t);\n+\t\t\t\tthis.#validate(validate);\n+\t\t\t\treturn { ready: false, phase: \"writer\", next: page.next };\n+\t\t\t}\n+\t\t\tconst root = host.root();\n+\t\t\tconst page = this.#projection.adopt({\n+\t\t\t\tstate,\n+\t\t\t\texpected: root,\n+\t\t\t\tcurrentRoot: () => host.root(),\n+\t\t\t\tmaxAtoms: 32,\n+\t\t\t\tmaxVersions: 1,\n+\t\t\t\tmaxSteps: 32,\n+\t\t\t});\n+\t\t\tthis.#validate(validate);\n+\t\t\tif (page.ready) state.write(\"runtime\", \"ready\", true);\n+\t\t\treturn { ready: page.ready, phase: page.ready ? \"ready\" : \"projection\", next: root.logLength };\n+\t\t});\n+\t}\n+\t#requireReady() {\n+\t\tif (\n+\t\t\tthis.#storage.sql.exec(\"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'writer_state'\").toArray()\n+\t\t\t\t.length === 0\n+\t\t)\n+\t\t\tthrow Error(\"disk-first adoption is not complete\");\n+\t\tif (this.#state().read(\"runtime\", \"format\") !== 1 || this.#state().read(\"runtime\", \"ready\") !== true)\n+\t\t\tthrow Error(\"disk-first adoption is not complete\");\n+\t}\n+\tread<T>(operation: (calls: EngineProject) => T, validate: () => void): T {\n+\t\tthis.#requireReady();\n+\t\tconst host = this.#host();\n+\t\treturn host.readCalls({\n+\t\t\texpected: host.source(),\n+\t\t\tvalidateAuthority: validate,\n+\t\t\tservices: this.#services,\n+\t\t\toperation: (calls) => structuredClone(operation(calls)),\n+\t\t});\n+\t}\n+\twrite<T>(\n+\t\toperation: (calls: EngineProject) => T,\n+\t\tvalidate: () => void,\n+\t\trequest?: { id: string; identity: unknown },\n+\t): T {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tthis.#validate(validate);\n+\t\t\tthis.#requireReady();\n+\t\t\tconst state = this.#state();\n+\t\t\tconst host = this.#host();\n+\t\t\thost.root();\n+\t\t\tif (request && (!request.id || request.id.length > 128)) throw Error(\"invalid disk-first request identity\");\n+\t\t\tconst identity = request ? canonicalJson(request.identity) : undefined;\n+\t\t\tif (request) {\n+\t\t\t\tconst receipt = state.read(\"runtime:receipts\", request.id) as { identity: string; result: T } | undefined;\n+\t\t\t\tif (receipt) {\n+\t\t\t\t\tif (receipt.identity !== identity) throw Error(\"disk-first request identity was reused\");\n+\t\t\t\t\tthis.#validate(validate);\n+\t\t\t\t\treturn structuredClone(receipt.result);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\treturn host.runCalls({\n+\t\t\t\texpected: host.source(),\n+\t\t\t\tvalidateAuthority: validate,\n+\t\t\t\tservices: this.#services,\n+\t\t\t\tcheckerReady: false,\n+\t\t\t\toperation: (calls) => structuredClone(operation(calls)),\n+\t\t\t\tcommitReceipt: (result) => {\n+\t\t\t\t\tif (request) state.write(\"runtime:receipts\", request.id, { identity, result });\n+\t\t\t\t},\n+\t\t\t});\n+\t\t});\n+\t}\n+}"}],"unmatched":[]}