{"draft":null,"from":175,"to":318,"files":[{"path":"server/src/disk-first-runtime.ts","added":20,"removed":14,"diff":"--- a/server/src/disk-first-runtime.ts\n+++ b/server/src/disk-first-runtime.ts\n@@ -1,10 +1,11 @@\n import { createHash } from \"node:crypto\";\n-import { canonicalJson } from \"../../worker/project/operation-protocol.ts\";\n+import { jsonValueChunks } from \"../../src/engine/json-value-chunks.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 { SqliteHistoryHost } from \"./history-host.ts\";\n import { SqliteProjectionStore } from \"./projection-store.ts\";\n+import { requestIdentityDigest } from \"./request-identity-digest.ts\";\n import { SqliteWriterHost, SqliteWriterState } from \"./writer-host.ts\";\n \n type Services = Pick<ProjectOptions, \"hasBlob\" | \"whole\">;\n@@ -46,17 +47,27 @@\n \t#saveReceipt(state: SqliteWriterState, id: string, identity: string, result: unknown) {\n \t\t// Receipt payloads can exceed the graph-state value bound. Keep each stored\n \t\t// piece small without increasing that bound for engine state.\n-\t\tconst json = JSON.stringify({ result });\n \t\tconst characters = 64 * 1024;\n-\t\tconst parts = Math.ceil(json.length / characters);\n-\t\tfor (let part = 0; part < parts; part++)\n-\t\t\tstate.write(`runtime:receipt:${id}`, String(part), json.slice(part * characters, (part + 1) * characters));\n+\t\tconst digest = createHash(\"sha256\");\n+\t\tlet pending = \"\",\n+\t\t\tparts = 0,\n+\t\t\tlength = 0;\n+\t\tfor (const chunk of jsonValueChunks({ result }, { maxDepth: Infinity, unboxPrimitives: true })) {\n+\t\t\tdigest.update(chunk);\n+\t\t\tlength += chunk.length;\n+\t\t\tpending += chunk;\n+\t\t\twhile (pending.length >= characters) {\n+\t\t\t\tstate.write(`runtime:receipt:${id}`, String(parts++), pending.slice(0, characters));\n+\t\t\t\tpending = pending.slice(characters);\n+\t\t\t}\n+\t\t}\n+\t\tif (pending) state.write(`runtime:receipt:${id}`, String(parts++), pending);\n \t\tstate.write(\"runtime:receipts\", id, {\n \t\t\tformat: 3,\n \t\t\tidentity,\n \t\t\tparts,\n-\t\t\tcharacters: json.length,\n-\t\t\tdigest: createHash(\"sha256\").update(json).digest(\"hex\"),\n+\t\t\tcharacters: length,\n+\t\t\tdigest: digest.digest(\"hex\"),\n \t\t});\n \t}\n \t#receiptResult<T>(\n@@ -117,12 +128,7 @@\n \t\t\tconst result = structuredClone(operation(new EngineProject(project)));\n \t\t\tthis.#validate(validate);\n \t\t\tif (request && log.seq() > 0)\n-\t\t\t\tthis.#saveReceipt(\n-\t\t\t\t\tthis.#setup(),\n-\t\t\t\t\trequest.id,\n-\t\t\t\t\tcreateHash(\"sha256\").update(canonicalJson(request.identity)).digest(\"hex\"),\n-\t\t\t\t\tresult,\n-\t\t\t\t);\n+\t\t\t\tthis.#saveReceipt(this.#setup(), request.id, requestIdentityDigest(request.identity), result);\n \t\t\treturn result;\n \t\t});\n \t}\n@@ -223,7 +229,7 @@\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 ? createHash(\"sha256\").update(canonicalJson(request.identity)).digest(\"hex\") : undefined;\n+\t\t\tconst identity = request ? requestIdentityDigest(request.identity) : undefined;\n \t\t\tif (request) {\n \t\t\t\tconst receipt = state.read(\"runtime:receipts\", request.id) as\n \t\t\t\t\t| { format: number; identity: string; result: T }"}],"unmatched":[]}