{"version":147,"agent":"codex-capacity-chair","message":"Batch immutable locator writes for sequence updates","time":1791858855844,"timeIso":"2026-10-13T02:34:15.844Z","changes":5,"change":1284,"files":["src/engine/experimental/projection-locator.ts","src/engine/experimental/projection-sequence.ts","src/engine/test/projection-locator.test.ts"],"draft":null,"diff":[{"path":"src/engine/experimental/projection-locator.ts","added":40,"removed":0,"diff":"--- a/src/engine/experimental/projection-locator.ts\n+++ b/src/engine/experimental/projection-locator.ts\n@@ -63,6 +63,46 @@\n \t\tthis.#storage.write(this.#collection, \"next\", next + 1);\n \t\treturn next;\n \t}\n+\t/** Persist only pages reachable from the final root of a bounded update batch. */\n+\tsetMany(root: Page, values: ReadonlyArray<readonly [number, T]>): Page {\n+\t\tpage(root);\n+\t\tif (values.length > 256) throw Error(\"locator batch limit\");\n+\t\treturn this.#storage.transaction(() => {\n+\t\t\tconst staged = new Map<string, unknown>();\n+\t\t\tconst storage: GraphStateStorage = {\n+\t\t\t\tread: (collection, key) =>\n+\t\t\t\t\tcollection === this.#collection && staged.has(key) ? staged.get(key) : this.#storage.read(collection, key),\n+\t\t\t\twrite: (collection, key, value) => {\n+\t\t\t\t\tif (collection !== this.#collection) throw Error(\"unexpected batch collection\");\n+\t\t\t\t\tstaged.set(key, value);\n+\t\t\t\t},\n+\t\t\t\tremove: () => {\n+\t\t\t\t\tthrow Error(\"unexpected batch removal\");\n+\t\t\t\t},\n+\t\t\t\tentries: () => {\n+\t\t\t\t\tthrow Error(\"unexpected batch scan\");\n+\t\t\t\t},\n+\t\t\t\ttransaction: (body) => body(),\n+\t\t\t};\n+\t\t\t// The namespace is encoded into the collection at construction.\n+\t\t\tconst namespace = JSON.parse(this.#collection.slice(\"experimental-locator:\".length)) as string;\n+\t\t\tconst index = new ProjectionLocator<T>(storage, namespace, this.#decode);\n+\t\t\tlet result = root;\n+\t\t\tfor (const [id, value] of values) result = index.set(result, id, value);\n+\t\t\tconst retained = new Set<number>();\n+\t\t\tconst visit = (id: Page): void => {\n+\t\t\t\tif (id === null || retained.has(id) || !staged.has(String(id))) return;\n+\t\t\t\tretained.add(id);\n+\t\t\t\tconst node = staged.get(String(id)) as Node;\n+\t\t\t\tif (node.kind === \"branch\") for (const child of Object.values(node.children)) visit(child);\n+\t\t\t};\n+\t\t\tvisit(result);\n+\t\t\tfor (const id of retained) this.#storage.write(this.#collection, String(id), staged.get(String(id)));\n+\t\t\tif (staged.has(\"next\")) this.#storage.write(this.#collection, \"next\", staged.get(\"next\"));\n+\t\t\treturn result;\n+\t\t});\n+\t}\n+\n \tget(root: Page, id: number): T | undefined {\n \t\tpage(root);\n \t\tconst key = keyOf(id);"},{"path":"src/engine/experimental/projection-sequence.ts","added":11,"removed":1,"diff":"--- a/src/engine/experimental/projection-sequence.ts\n+++ b/src/engine/experimental/projection-sequence.ts\n@@ -300,7 +300,17 @@\n \t\t\t\t\tsave(node);\n \t\t\t\t}\n \t\t\t} else throw Error(\"unknown operation\");\n-\t\t\tfor (const [id, node] of changed) map = this.set(map, id, node);\n+\t\t\t// Keep locator staging bounded even if a valid split/merge changes more\n+\t\t\t// than one batch. The enclosing transaction commits all batches together.\n+\t\t\tlet batch: Array<readonly [number, SequenceNode]> = [];\n+\t\t\tfor (const entry of changed) {\n+\t\t\t\tbatch.push(entry);\n+\t\t\t\tif (batch.length === 256) {\n+\t\t\t\t\tmap = this.#locator.setMany(map, batch);\n+\t\t\t\t\tbatch = [];\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tmap = this.#locator.setMany(map, batch);\n \t\t\treturn { map, root, next };\n \t\t});\n \t}"},{"path":"src/engine/test/projection-locator.test.ts","added":56,"removed":0,"diff":"--- a/src/engine/test/projection-locator.test.ts\n+++ b/src/engine/test/projection-locator.test.ts\n@@ -47,3 +47,59 @@\n \t\tstorage.db.close();\n \t}\n });\n+\n+test(\"bounded locator batches preserve old roots, discard intermediate pages, and roll back writes\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst index = new ProjectionLocator(db, \"batch\", decode);\n+\t\tconst old = index.set(null, 0, 1);\n+\t\tconst values = Array.from({ length: 64 }, (_, i) => [i, i + 10] as const);\n+\t\tconst before = db.writes;\n+\t\tconst root = index.setMany(old, values);\n+\t\tconst batchWrites = db.writes - before;\n+\t\tassert.equal(index.get(old, 0), 1);\n+\t\tfor (const [id, value] of values) assert.equal(index.get(root, id), value);\n+\t\tconst plain = new ProjectionLocator(db, \"plain\", decode);\n+\t\tlet sequential: number | null = plain.set(null, 0, 1);\n+\t\tconst baseline = db.writes;\n+\t\tfor (const [id, value] of values) sequential = plain.set(sequential, id, value);\n+\t\tassert.ok(batchWrites < (db.writes - baseline) / 2);\n+\t\tconst rows = db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\t// Fail every persisted page and the final allocation-counter write.\n+\t\tfor (let offset = 1; offset <= batchWrites; offset++) {\n+\t\t\tdb.failWrite = db.writes + offset;\n+\t\t\tassert.throws(() => index.setMany(old, values), /injected/);\n+\t\t\tassert.deepEqual(db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), rows);\n+\t\t}\n+\t\tdb.failWrite = Infinity;\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tindex.setMany(\n+\t\t\t\t\troot,\n+\t\t\t\t\tArray.from({ length: 257 }, () => [0, 1] as const),\n+\t\t\t\t),\n+\t\t\t/batch limit/,\n+\t\t);\n+\t\tassert.equal(index.setMany(root, []), root);\n+\t\tconst duplicate = index.setMany(root, [\n+\t\t\t[0, 100],\n+\t\t\t[0, 200],\n+\t\t\t[0xffffffff, 300],\n+\t\t]);\n+\t\tassert.equal(index.get(duplicate, 0), 200);\n+\t\tassert.equal(index.get(duplicate, 0xffffffff), 300);\n+\t\tassert.equal(index.get(root, 0), 10);\n+\t\tconst stable = db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tindex.setMany(duplicate, [\n+\t\t\t\t\t[1, 3],\n+\t\t\t\t\t[2 ** 32, 4],\n+\t\t\t\t]),\n+\t\t\t/identity limit/,\n+\t\t);\n+\t\tassert.deepEqual(db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), stable);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});"}],"ci":null}