The writer's row cache survives projection scratch and big rows (abso Capacity, issue 664). Version 253 of shared· capacity· Oct 15 (2026-10-15 18:44 UTC) · Not checked
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. 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. On 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.
Tests: 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.
codex-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.
Version 252 · Files at version 253
4 files, 103 lines added, 8 removed.
--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -30,12 +30,31 @@
30 30 readonly #maxBytes: number; 31 31 readonly #maxEntries: number; 33 − constructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096) {
34 − if (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || !Number.isSafeInteger(maxEntries) || maxEntries < 0)
34 + * The keys written or removed in each open transaction, innermost last (abso Capacity). A rollback drops only those: 35 + * every projection scratch scope ends in a rollback, and dropping the whole cache there read the same rows from 36 + * storage again within a call and on every call of a kept reader. 38 + readonly #touched: Set<string>[] = []; 40 + * The biggest one row may be and still be kept (abso Capacity). A few big rows read on every call (each agent with 41 + * all the news it was told: 4.3 MB for abso's 109 agents) otherwise push every small page out of the cache. 43 + readonly #maxEntryBytes: number; 44 + constructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096, maxEntryBytes = maxBytes) { 46 + !Number.isSafeInteger(maxBytes) || 48 + !Number.isSafeInteger(maxEntries) || 50 + !Number.isSafeInteger(maxEntryBytes) || 35 53 throw new Error("invalid writer cache limits"); 36 54 this.#source = source; 37 55 this.#maxBytes = maxBytes; 38 56 this.#maxEntries = maxEntries; 57 + this.#maxEntryBytes = Math.min(maxEntryBytes, maxBytes); 40 59 get retainedBytes(): number { @@ -89,12 +108,14 @@
89 108 entry = { kind: "json", json, bytes: overhead + 2 * json.length }; 91 110 this.#remember(cacheKey, entry); 111 + // A projection scope's own rows are never read again once it ends in its rollback: forget them with it. 112 + if (collection.startsWith("projection-scratch:")) this.#touched.at(-1)?.add(cacheKey); 94 115 #remember(cacheKey: string, entry: CachedValue): void { 95 116 this.#drop(cacheKey); 96 117 // Payload accounting is bounded, not a measurement of actual isolate heap. 97 − if (entry.bytes <= this.#maxBytes && this.#maxEntries > 0) {
118 + if (entry.bytes <= this.#maxEntryBytes && this.#maxEntries > 0) { 98 119 while (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries) 99 120 this.#drop(this.#cache.keys().next().value!); 100 121 this.#cache.set(cacheKey, entry); @@ -104,6 +125,7 @@
104 125 write(collection: string, key: string, value: unknown): void { 105 126 const cacheKey = JSON.stringify([collection, key]); 106 127 this.#drop(cacheKey); 128 + this.#touched.at(-1)?.add(cacheKey); 107 129 this.#source.write(collection, key, value); 108 130 // Allocation counters are immutable scalars. Keep successful writes within 109 131 // the existing cache budget instead of immediately reading them back. @@ -111,17 +133,28 @@
111 133 this.#remember(cacheKey, { kind: "scalar", value, bytes: 2 * cacheKey.length + 176 }); 113 135 remove(collection: string, key: string): void { 114 − this.#drop(JSON.stringify([collection, key]));
136 + const cacheKey = JSON.stringify([collection, key]); 137 + this.#drop(cacheKey); 138 + this.#touched.at(-1)?.add(cacheKey); 115 139 this.#source.remove(collection, key); 117 141 entries(collection: string): Iterable<readonly [string, unknown]> { 118 142 return this.#source.entries(collection); 120 144 transaction<T>(body: () => T): T { 145 + const touched = new Set<string>(); 146 + this.#touched.push(touched); 122 − return this.#source.transaction(body);
148 + const result = this.#source.transaction(body); 149 + this.#touched.pop(); 150 + // Committed into the outer transaction, which may still roll back. 151 + const outer = this.#touched.at(-1); 152 + if (outer) for (const key of touched) outer.add(key); 124 − this.clear();
155 + this.#touched.pop(); 156 + // What the transaction wrote, and anything read back after it wrote it, is gone with the rollback. 157 + for (const key of touched) this.#drop(key); --- a/src/engine/test/projection-scratch.test.ts
+++ b/src/engine/test/projection-scratch.test.ts
@@ -32,7 +32,9 @@
33 33 assert.deepEqual(result, { value: 99, data: new Int32Array([1, 2]) }); 34 34 assert.throws(() => escaped!.write("x", "y", true), /ended/); 35 − assert.equal(storage.retainedEntries, 0, "scratch rollback invalidates every cached value");
35 + // A scratch rollback drops only what the scratch wrote (abso Capacity): the canonical value written before it, in 36 + // the outer transaction, stays cached and is still right. 37 + assert.equal(storage.retainedEntries, 1, "only the canonical value written before the scratch remains"); 36 38 assert.equal(storage.read("canonical", "before"), 1); 37 39 storage.write("canonical", "after", 2); --- a/src/engine/test/state-cache.test.ts
+++ b/src/engine/test/state-cache.test.ts
@@ -105,3 +105,61 @@
105 105 assert.equal(cache.retainedBytes, 0); 106 106 source.db.close(); 109 + test("a rollback drops only what its transaction wrote; a nested commit's writes go with an outer rollback (abso Capacity)", () => { 110 + const source = new SqlState(); 112 + const cache = new CachedStateStorage(source, 8192, 16); 113 + source.write("rows", "kept", "k"); 114 + source.write("rows", "inner", "i0"); 115 + source.write("rows", "outer", "o0"); 116 + for (const key of ["kept", "inner", "outer"]) cache.read("rows", key); 117 + const reads = source.reads; 118 + // A projection scratch scope: rolled back on purpose, and the rows read around it stay cached. 121 + cache.transaction(() => { 122 + cache.write("scratch", "x", "temporary"); 123 + throw new Error("scratch rollback"); 127 + assert.equal(cache.read("rows", "kept"), "k"); 128 + assert.equal(source.reads, reads, "the rows read before were not read again"); 129 + assert.equal(cache.read("scratch", "x"), undefined); 130 + // A nested transaction commits, then the outer one rolls back: both writes are undone, and the cache forgets both. 133 + cache.transaction(() => { 134 + cache.write("rows", "outer", "o1"); 135 + cache.transaction(() => cache.write("rows", "inner", "i1")); 136 + assert.equal(cache.read("rows", "inner"), "i1"); 137 + throw new Error("outer rollback"); 141 + assert.equal(cache.read("rows", "inner"), "i0"); 142 + assert.equal(cache.read("rows", "outer"), "o0"); 143 + assert.equal(cache.read("rows", "kept"), "k"); 149 + test("a row over the per-row limit is read but not kept, and pushes nothing out (abso Capacity)", () => { 150 + const source = new SqlState(); 152 + const cache = new CachedStateStorage(source, 8192, 64, 1024); 153 + source.write("rows", "small", "s"); 154 + source.write("rows", "big", "x".repeat(2000)); 155 + cache.read("rows", "small"); 156 + const reads = source.reads; 157 + assert.equal((cache.read("rows", "big") as string).length, 2000); 158 + assert.equal(cache.read("rows", "small"), "s"); 159 + assert.equal(source.reads, reads + 1, "the big row was read from storage once and the small one stayed cached"); 160 + assert.equal((cache.read("rows", "big") as string).length, 2000); 161 + assert.equal(source.reads, reads + 2, "the big row isn't kept"); --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -253,6 +253,8 @@
254 254 const WRITER_CACHE_BYTES = 4 * 1_048_576; 255 255 const WRITER_CACHE_ROWS = 16_384; 256 + /** The biggest row the writer cache keeps (abso Capacity): an agent and all the news it was told can be 100 KB. */ 257 + const WRITER_CACHE_ROW_BYTES = 16_384; 256 258 export interface WriterSource { 257 259 readonly generation: number; 258 260 readonly logLength: number; @@ -403,7 +405,7 @@
403 405 throw Error("accepted atom indexing requires writer storage"); 404 406 this.#store = store; 405 407 this.#writerStorage = options.writerStorage 406 − ? new CachedStateStorage(options.writerStorage, WRITER_CACHE_BYTES, WRITER_CACHE_ROWS)
408 + ? new CachedStateStorage(options.writerStorage, WRITER_CACHE_BYTES, WRITER_CACHE_ROWS, WRITER_CACHE_ROW_BYTES) 408 410 if (this.#writerStorage) { 409 411 const storage = this.#writerStorage;