{"version":176,"agent":"codex-capacity-chair","message":"Avoid quadratic insertion-run offsets; cleared in review ask 52","time":1791970476979,"timeIso":"2026-10-14T09:34:36.979Z","changes":2,"change":2753,"files":["src/engine/graph.ts","src/engine/test/json-string-chunks.test.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":16,"removed":6,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -380,6 +380,20 @@\n \t\t\t\t\t\t\t\t\tthrow Error(\"stored atom text range missing\");\n \t\t\t\t\t\t\t\t}\n \t\t\t\t\t\t\t: undefined;\n+\t\t\t\t\t// Sequential reads advance once per run without retaining a history-sized offset array.\n+\t\t\t\t\tlet offsetRun = 0;\n+\t\t\t\t\tlet offsetLine = 0;\n+\t\t\t\t\tconst insertionStart = (index: number): number => {\n+\t\t\t\t\t\tif (index < offsetRun) {\n+\t\t\t\t\t\t\toffsetRun = 0;\n+\t\t\t\t\t\t\toffsetLine = 0;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\twhile (offsetRun < index) {\n+\t\t\t\t\t\t\toffsetLine += readonlyStoredArray(storage, `atom-inserts:${atom.seq}:lines:${offsetRun}`).length;\n+\t\t\t\t\t\t\toffsetRun++;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\treturn offsetLine;\n+\t\t\t\t\t};\n \t\t\t\t\tconst streamed = (target: Atom): Atom => {\n \t\t\t\t\t\tregisterJsonChunkValue(target, () => {\n \t\t\t\t\t\t\tconst line = (raw: unknown, index: number): unknown => {\n@@ -397,9 +411,7 @@\n \t\t\t\t\t\t\t\t\tconst lines = `${collection}:lines:${index}`;\n \t\t\t\t\t\t\t\t\tif (run.lines !== lines || typeof run.file !== \"string\" || typeof run.parent !== \"string\")\n \t\t\t\t\t\t\t\t\t\tthrow Error(\"invalid stored insertion run\");\n-\t\t\t\t\t\t\t\t\tlet start = 0;\n-\t\t\t\t\t\t\t\t\tfor (let prior = 0; prior < index; prior++)\n-\t\t\t\t\t\t\t\t\t\tstart += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;\n+\t\t\t\t\t\t\t\t\tconst start = insertionStart(index);\n \t\t\t\t\t\t\t\t\treturn {\n \t\t\t\t\t\t\t\t\t\tfile: run.file,\n \t\t\t\t\t\t\t\t\t\tparent: run.parent,\n@@ -440,9 +452,7 @@\n \t\t\t\t\t\t\t\tlines: readonlyStoredArray<string>(storage, lines, (rawLine, lineIndex) => {\n \t\t\t\t\t\t\t\t\tif (typeof rawLine === \"string\") return rawLine;\n \t\t\t\t\t\t\t\t\tconst ref = rawLine as TextReference;\n-\t\t\t\t\t\t\t\t\tlet atomLine = lineIndex;\n-\t\t\t\t\t\t\t\t\tfor (let prior = 0; prior < index; prior++)\n-\t\t\t\t\t\t\t\t\t\tatomLine += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;\n+\t\t\t\t\t\t\t\t\tconst atomLine = insertionStart(index) + lineIndex;\n \t\t\t\t\t\t\t\t\tif (!ref || ref.text !== JSON.stringify([String(atom.seq), atomLine]))\n \t\t\t\t\t\t\t\t\t\tthrow Error(\"invalid stored insertion text\");\n \t\t\t\t\t\t\t\t\treturn atomText.read(ref.text);"},{"path":"src/engine/test/json-string-chunks.test.ts","added":18,"removed":0,"diff":"--- a/src/engine/test/json-string-chunks.test.ts\n+++ b/src/engine/test/json-string-chunks.test.ts\n@@ -6,6 +6,24 @@\n import { StoredText } from \"../stored-text.ts\";\n import { SqlState } from \"./storage-helper.ts\";\n \n+test(\"many stored insertion runs serialize with linear reads and support backwards access\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst files = Object.fromEntries(Array.from({ length: 128 }, (_, i) => [`f${i}`, `${i}:${\"x\".repeat(17000)}\\n`]));\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(buildBase(files, { author: \"writer\", message: \"many runs\" }));\n+\tconst stored = graph.atom(\"a0\");\n+\tconst expected = JSON.stringify(stored);\n+\tconst before = storage.reads;\n+\tassert.equal([...jsonValueChunks(stored)].join(\"\"), expected);\n+\tassert.ok(storage.reads - before < 128 * 25, `serialization used ${storage.reads - before} reads`);\n+\tfor (const index of [127, 0, 64, 2, 126, 1]) {\n+\t\tconst run = stored.inserts[index]!;\n+\t\tassert.equal(run.lines[0], files[run.file]!.trimEnd());\n+\t}\n+\tassert.equal([...jsonValueChunks(stored)].join(\"\"), expected);\n+});\n+\n test(\"chunk-backed JSON strings preserve escaping and split UTF-16 exactly\", () => {\n \tconst values = [\n \t\t\"\","}],"ci":null}