{"version":167,"agent":"codex-capacity-chair","message":"Stream canonical log append and bound JSON string decoding allocations","time":1791956852426,"timeIso":"2026-10-14T05:47:32.426Z","changes":4,"change":2499,"files":["server/src/engine-project.ts","server/src/json-chunk-cursor.ts","server/test/streamed-log-append.test.ts","server/test/streamed-log-transaction.test.ts"],"draft":null,"diff":[{"path":"server/src/engine-project.ts","added":21,"removed":4,"diff":"--- a/server/src/engine-project.ts\n+++ b/server/src/engine-project.ts\n@@ -3,6 +3,7 @@\n // 2 MB) is split across rows and joined on load. Ported from the platform's worker/src/engine-project.ts; the\n // writer-adoption and R2 version parts are left out.\n \n+import { jsonValueChunks } from \"../../src/engine/json-value-chunks.ts\";\n import { blame, folderBlame, why } from \"../../worker/project/blame.ts\";\n import { feedEvents } from \"../../worker/project/feed.ts\";\n import { type Entry, type ProjectStore as LogStore, MemoryStore, Project } from \"../../worker/project/project.ts\";\n@@ -33,6 +34,22 @@\n \treturn parts;\n }\n \n+/** Retains one log row plus an encoder fragment, preserving legacy row boundaries. */\n+export function* streamedLogParts(value: unknown): IterableIterator<string> {\n+\tlet pending = \"\";\n+\tfor (const chunk of jsonValueChunks(value)) {\n+\t\tpending += chunk;\n+\t\twhile (pending.length > LOG_PART_CHARACTERS) {\n+\t\t\tlet end = LOG_PART_CHARACTERS;\n+\t\t\tconst last = pending.charCodeAt(end - 1);\n+\t\t\tif (last >= 0xd800 && last <= 0xdbff) end--;\n+\t\t\tyield pending.slice(0, end);\n+\t\t\tpending = pending.slice(end);\n+\t\t}\n+\t}\n+\tif (pending) yield pending;\n+}\n+\n /** The lines an entry's atoms write: what a loaded project's size grows with (#46, #56). */\n function linesIn(entry: Entry): number {\n \tlet n = 0;\n@@ -424,11 +441,11 @@\n \t\t\tif (keys > 0) this.storage.sql.exec(\"UPDATE counts SET value = value + ? WHERE name = 'keys'\", keys);\n \t\t\tlet chars = 0;\n \t\t\tfor (const entry of entries) {\n-\t\t\t\tconst json = JSON.stringify(entry);\n-\t\t\t\tlogParts(json).forEach((text, part) => {\n+\t\t\t\tlet part = 0;\n+\t\t\t\tfor (const text of streamedLogParts(entry)) {\n \t\t\t\t\tchars += text.length;\n-\t\t\t\t\tthis.storage.sql.exec(\"INSERT INTO log (seq, part, text) VALUES (?, ?, ?)\", next, part, text);\n-\t\t\t\t});\n+\t\t\t\t\tthis.storage.sql.exec(\"INSERT INTO log (seq, part, text) VALUES (?, ?, ?)\", next, part++, text);\n+\t\t\t\t}\n \t\t\t\tnext++;\n \t\t\t}\n \t\t\t// Chars are only added to once counted: a log from before the count is summed whole first (logSize)."},{"path":"server/src/json-chunk-cursor.ts","added":18,"removed":0,"diff":"--- a/server/src/json-chunk-cursor.ts\n+++ b/server/src/json-chunk-cursor.ts\n@@ -27,6 +27,24 @@\n \t\tif (this.take() !== '\"') throw Error(\"expected JSON string\");\n \t\tlet buffer = \"\";\n \t\tfor (;;) {\n+\t\t\t// Copy ordinary text in bounded slices instead of allocating a string\n+\t\t\t// concatenation for every character of a large canonical field.\n+\t\t\tthis.peek();\n+\t\t\tconst start = this.#offset;\n+\t\t\tconst end = Math.min(this.#chunk.length, start + 16384 - buffer.length);\n+\t\t\twhile (this.#offset < end) {\n+\t\t\t\tconst code = this.#chunk.charCodeAt(this.#offset);\n+\t\t\t\tif (code < 32 || code === 34 || code === 92) break;\n+\t\t\t\tthis.#offset++;\n+\t\t\t}\n+\t\t\tif (this.#offset > start) {\n+\t\t\t\tbuffer += this.#chunk.slice(start, this.#offset);\n+\t\t\t\tif (buffer.length === 16384) {\n+\t\t\t\t\tyield buffer;\n+\t\t\t\t\tbuffer = \"\";\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tlet char = this.take();\n \t\t\tif (char === '\"') {\n \t\t\t\tif (buffer) yield buffer;"},{"path":"server/test/streamed-log-append.test.ts","added":27,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/streamed-log-append.test.ts\n@@ -0,0 +1,27 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { LOG_PART_CHARACTERS, logParts, streamedLogParts } from \"../src/engine-project.ts\";\n+\n+test(\"streamed canonical rows exactly match legacy serialization and boundaries\", () => {\n+\tfor (const offset of [-2, -1, 0, 1, 2]) {\n+\t\tconst value = { text: \"x\".repeat(LOG_PART_CHARACTERS - 9 + offset) + '😀\\ud800\\n\"', rest: [null, false, 3] };\n+\t\tassert.deepEqual([...streamedLogParts(value)], logParts(JSON.stringify(value)));\n+\t}\n+\tconst value = { files: Array.from({ length: 100 }, (_, i) => `${i}:` + \"中\".repeat(20000)) };\n+\tassert.deepEqual([...streamedLogParts(value)], logParts(JSON.stringify(value)));\n+});\n+\n+test(\"canonical row streaming never stringifies a whole object or a large string\", (t) => {\n+\tconst stringify = JSON.stringify;\n+\tt.mock.method(JSON, \"stringify\", (value: unknown) => {\n+\t\tassert.ok(typeof value !== \"object\", \"whole-object serialization is forbidden\");\n+\t\tif (typeof value === \"string\") assert.ok(value.length <= 16384);\n+\t\treturn stringify(value);\n+\t});\n+\tlet length = 0;\n+\tfor (const part of streamedLogParts({ text: \"x\".repeat(6_000_000) })) {\n+\t\tassert.ok(part.length <= LOG_PART_CHARACTERS);\n+\t\tlength += part.length;\n+\t}\n+\tassert.equal(length, 6_000_011);\n+});"},{"path":"server/test/streamed-log-transaction.test.ts","added":31,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/streamed-log-transaction.test.ts\n@@ -0,0 +1,31 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { MemoryStore, Project } from \"../../worker/project/project.ts\";\n+import { logParts, SqliteLog } from \"../src/engine-project.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"streamed append preserves exact rows and rolls back a partial frame on storage failure\", (t) => {\n+\tconst memory = new MemoryStore();\n+\tnew Project(memory).init({ \"large.txt\": \"😀\".repeat(700000) }, 0);\n+\tconst expected = memory.entries.flatMap((entry, seq) =>\n+\t\tlogParts(JSON.stringify(entry)).map((text, part) => ({ seq, part, text })),\n+\t);\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst log = new SqliteLog(storage);\n+\tconst beforeGeneration = log.generation();\n+\tconst exec = storage.sql.exec.bind(storage.sql);\n+\tlet inserts = 0;\n+\tstorage.sql.exec = (query, ...bindings) => {\n+\t\tif (query.startsWith(\"INSERT INTO log\") && ++inserts === 2) throw Error(\"injected row failure\");\n+\t\treturn exec(query, ...bindings);\n+\t};\n+\tassert.throws(() => log.append(memory.entries), /injected row failure/);\n+\tstorage.sql.exec = exec;\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log\").all(), []);\n+\tassert.equal(log.generation(), beforeGeneration);\n+\tlog.append(memory.entries);\n+\tconst rows = storage.db.prepare(\"SELECT seq, part, text FROM log ORDER BY seq, part\").all();\n+\tassert.deepEqual(JSON.parse(JSON.stringify(rows)), expected);\n+\tassert.equal(log.generation(), beforeGeneration + 1);\n+});"}],"ci":null}