{"version":253,"agent":"capacity","message":"The writer's row cache survives projection scratch and big rows (abso Capacity, issue 664).\n\n- CachedStateStorage dropped its whole cache whenever a transaction rolled back. Every projection scratch scope ends in a rollback on purpose, so the cache emptied many times within a call and at every call of a kept reader. A rollback now drops only the keys written or removed in that transaction (and any read back after), and a scope's own scratch rows; a nested commit's keys go with an outer rollback.\n- A row bigger than a per-row limit is read but not kept (the writer passes 16 KB). abso's 109 agents, each with all the news it was told, are 4.3 MB of rows read on every call, and they pushed every small page out of the 4 MB cache.\n\nOn a fresh abso adoption (my machine), asked again: where 229 to 237 ms after its second ask (was 0.32 to 0.46 s), drafts 198 ms (0.31 s), collisions 207 ms. A full run matches 33 of 33 answers; held heap at most 39.8 MB.\n\nTests: state-cache (new: a rollback drops only what its transaction wrote, a nested commit's writes go with an outer rollback; a row over the limit isn't kept and pushes nothing out). projection-scratch's check now expects the canonical value written before a scratch scope to stay cached. All of src/engine/test (294) and worker/project/test (349) pass.\n\ncodex-capacity-policy has older unpushed changes in lines 1 to 25 of state-source.ts (and a new test file); this touches the class body only.","time":1792089883079,"timeIso":"2026-10-15T18:44:43.079Z","changes":10,"change":3942,"files":["src/engine/state-source.ts","src/engine/test/projection-scratch.test.ts","src/engine/test/state-cache.test.ts","worker/project/project.ts"],"draft":null,"diff":[{"path":"src/engine/state-source.ts","added":39,"removed":6,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -30,12 +30,31 @@\n \treadonly #maxBytes: number;\n \treadonly #maxEntries: number;\n \t#bytes = 0;\n-\tconstructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096) {\n-\t\tif (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || !Number.isSafeInteger(maxEntries) || maxEntries < 0)\n+\t/**\n+\t * The keys written or removed in each open transaction, innermost last (abso Capacity). A rollback drops only those:\n+\t * every projection scratch scope ends in a rollback, and dropping the whole cache there read the same rows from\n+\t * storage again within a call and on every call of a kept reader.\n+\t */\n+\treadonly #touched: Set<string>[] = [];\n+\t/**\n+\t * The biggest one row may be and still be kept (abso Capacity). A few big rows read on every call (each agent with\n+\t * all the news it was told: 4.3 MB for abso's 109 agents) otherwise push every small page out of the cache.\n+\t */\n+\treadonly #maxEntryBytes: number;\n+\tconstructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096, maxEntryBytes = maxBytes) {\n+\t\tif (\n+\t\t\t!Number.isSafeInteger(maxBytes) ||\n+\t\t\tmaxBytes < 0 ||\n+\t\t\t!Number.isSafeInteger(maxEntries) ||\n+\t\t\tmaxEntries < 0 ||\n+\t\t\t!Number.isSafeInteger(maxEntryBytes) ||\n+\t\t\tmaxEntryBytes < 0\n+\t\t)\n \t\t\tthrow new Error(\"invalid writer cache limits\");\n \t\tthis.#source = source;\n \t\tthis.#maxBytes = maxBytes;\n \t\tthis.#maxEntries = maxEntries;\n+\t\tthis.#maxEntryBytes = Math.min(maxEntryBytes, maxBytes);\n \t}\n \tget retainedBytes(): number {\n \t\treturn this.#bytes;\n@@ -89,12 +108,14 @@\n \t\t\tentry = { kind: \"json\", json, bytes: overhead + 2 * json.length };\n \t\t}\n \t\tthis.#remember(cacheKey, entry);\n+\t\t// A projection scope's own rows are never read again once it ends in its rollback: forget them with it.\n+\t\tif (collection.startsWith(\"projection-scratch:\")) this.#touched.at(-1)?.add(cacheKey);\n \t\treturn value;\n \t}\n \t#remember(cacheKey: string, entry: CachedValue): void {\n \t\tthis.#drop(cacheKey);\n \t\t// Payload accounting is bounded, not a measurement of actual isolate heap.\n-\t\tif (entry.bytes <= this.#maxBytes && this.#maxEntries > 0) {\n+\t\tif (entry.bytes <= this.#maxEntryBytes && this.#maxEntries > 0) {\n \t\t\twhile (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries)\n \t\t\t\tthis.#drop(this.#cache.keys().next().value!);\n \t\t\tthis.#cache.set(cacheKey, entry);\n@@ -104,6 +125,7 @@\n \twrite(collection: string, key: string, value: unknown): void {\n \t\tconst cacheKey = JSON.stringify([collection, key]);\n \t\tthis.#drop(cacheKey);\n+\t\tthis.#touched.at(-1)?.add(cacheKey);\n \t\tthis.#source.write(collection, key, value);\n \t\t// Allocation counters are immutable scalars. Keep successful writes within\n \t\t// the existing cache budget instead of immediately reading them back.\n@@ -111,17 +133,28 @@\n \t\t\tthis.#remember(cacheKey, { kind: \"scalar\", value, bytes: 2 * cacheKey.length + 176 });\n \t}\n \tremove(collection: string, key: string): void {\n-\t\tthis.#drop(JSON.stringify([collection, key]));\n+\t\tconst cacheKey = JSON.stringify([collection, key]);\n+\t\tthis.#drop(cacheKey);\n+\t\tthis.#touched.at(-1)?.add(cacheKey);\n \t\tthis.#source.remove(collection, key);\n \t}\n \tentries(collection: string): Iterable<readonly [string, unknown]> {\n \t\treturn this.#source.entries(collection);\n \t}\n \ttransaction<T>(body: () => T): T {\n+\t\tconst touched = new Set<string>();\n+\t\tthis.#touched.push(touched);\n \t\ttry {\n-\t\t\treturn this.#source.transaction(body);\n+\t\t\tconst result = this.#source.transaction(body);\n+\t\t\tthis.#touched.pop();\n+\t\t\t// Committed into the outer transaction, which may still roll back.\n+\t\t\tconst outer = this.#touched.at(-1);\n+\t\t\tif (outer) for (const key of touched) outer.add(key);\n+\t\t\treturn result;\n \t\t} catch (error) {\n-\t\t\tthis.clear();\n+\t\t\tthis.#touched.pop();\n+\t\t\t// What the transaction wrote, and anything read back after it wrote it, is gone with the rollback.\n+\t\t\tfor (const key of touched) this.#drop(key);\n \t\t\tthrow error;\n \t\t}\n \t}"},{"path":"src/engine/test/projection-scratch.test.ts","added":3,"removed":1,"diff":"--- a/src/engine/test/projection-scratch.test.ts\n+++ b/src/engine/test/projection-scratch.test.ts\n@@ -32,7 +32,9 @@\n \t\t\t);\n \t\t\tassert.deepEqual(result, { value: 99, data: new Int32Array([1, 2]) });\n \t\t\tassert.throws(() => escaped!.write(\"x\", \"y\", true), /ended/);\n-\t\t\tassert.equal(storage.retainedEntries, 0, \"scratch rollback invalidates every cached value\");\n+\t\t\t// A scratch rollback drops only what the scratch wrote (abso Capacity): the canonical value written before it, in\n+\t\t\t// the outer transaction, stays cached and is still right.\n+\t\t\tassert.equal(storage.retainedEntries, 1, \"only the canonical value written before the scratch remains\");\n \t\t\tassert.equal(storage.read(\"canonical\", \"before\"), 1);\n \t\t\tstorage.write(\"canonical\", \"after\", 2);\n \t\t});"},{"path":"src/engine/test/state-cache.test.ts","added":58,"removed":0,"diff":"--- a/src/engine/test/state-cache.test.ts\n+++ b/src/engine/test/state-cache.test.ts\n@@ -105,3 +105,61 @@\n \tassert.equal(cache.retainedBytes, 0);\n \tsource.db.close();\n });\n+\n+test(\"a rollback drops only what its transaction wrote; a nested commit's writes go with an outer rollback (abso Capacity)\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 16);\n+\t\tsource.write(\"rows\", \"kept\", \"k\");\n+\t\tsource.write(\"rows\", \"inner\", \"i0\");\n+\t\tsource.write(\"rows\", \"outer\", \"o0\");\n+\t\tfor (const key of [\"kept\", \"inner\", \"outer\"]) cache.read(\"rows\", key);\n+\t\tconst reads = source.reads;\n+\t\t// A projection scratch scope: rolled back on purpose, and the rows read around it stay cached.\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"scratch\", \"x\", \"temporary\");\n+\t\t\t\t\tthrow new Error(\"scratch rollback\");\n+\t\t\t\t}),\n+\t\t\t/scratch rollback/,\n+\t\t);\n+\t\tassert.equal(cache.read(\"rows\", \"kept\"), \"k\");\n+\t\tassert.equal(source.reads, reads, \"the rows read before were not read again\");\n+\t\tassert.equal(cache.read(\"scratch\", \"x\"), undefined);\n+\t\t// A nested transaction commits, then the outer one rolls back: both writes are undone, and the cache forgets both.\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"rows\", \"outer\", \"o1\");\n+\t\t\t\t\tcache.transaction(() => cache.write(\"rows\", \"inner\", \"i1\"));\n+\t\t\t\t\tassert.equal(cache.read(\"rows\", \"inner\"), \"i1\");\n+\t\t\t\t\tthrow new Error(\"outer rollback\");\n+\t\t\t\t}),\n+\t\t\t/outer rollback/,\n+\t\t);\n+\t\tassert.equal(cache.read(\"rows\", \"inner\"), \"i0\");\n+\t\tassert.equal(cache.read(\"rows\", \"outer\"), \"o0\");\n+\t\tassert.equal(cache.read(\"rows\", \"kept\"), \"k\");\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"a row over the per-row limit is read but not kept, and pushes nothing out (abso Capacity)\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 64, 1024);\n+\t\tsource.write(\"rows\", \"small\", \"s\");\n+\t\tsource.write(\"rows\", \"big\", \"x\".repeat(2000));\n+\t\tcache.read(\"rows\", \"small\");\n+\t\tconst reads = source.reads;\n+\t\tassert.equal((cache.read(\"rows\", \"big\") as string).length, 2000);\n+\t\tassert.equal(cache.read(\"rows\", \"small\"), \"s\");\n+\t\tassert.equal(source.reads, reads + 1, \"the big row was read from storage once and the small one stayed cached\");\n+\t\tassert.equal((cache.read(\"rows\", \"big\") as string).length, 2000);\n+\t\tassert.equal(source.reads, reads + 2, \"the big row isn't kept\");\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"},{"path":"worker/project/project.ts","added":3,"removed":1,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -253,6 +253,8 @@\n  */\n const WRITER_CACHE_BYTES = 4 * 1_048_576;\n const WRITER_CACHE_ROWS = 16_384;\n+/** The biggest row the writer cache keeps (abso Capacity): an agent and all the news it was told can be 100 KB. */\n+const WRITER_CACHE_ROW_BYTES = 16_384;\n export interface WriterSource {\n \treadonly generation: number;\n \treadonly logLength: number;\n@@ -403,7 +405,7 @@\n \t\t\tthrow Error(\"accepted atom indexing requires writer storage\");\n \t\tthis.#store = store;\n \t\tthis.#writerStorage = options.writerStorage\n-\t\t\t? new CachedStateStorage(options.writerStorage, WRITER_CACHE_BYTES, WRITER_CACHE_ROWS)\n+\t\t\t? new CachedStateStorage(options.writerStorage, WRITER_CACHE_BYTES, WRITER_CACHE_ROWS, WRITER_CACHE_ROW_BYTES)\n \t\t\t: undefined;\n \t\tif (this.#writerStorage) {\n \t\t\tconst storage = this.#writerStorage;"}],"ci":null}