{"draft":null,"from":243,"to":318,"files":[{"path":"src/engine/state-source.ts","added":50,"removed":7,"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}\n@@ -315,7 +348,9 @@\n \treturn new Proxy([] as T[], {\n \t\tget(target, key, receiver) {\n \t\t\tif (key === \"length\") return length();\n-\t\t\tif (indexKey(key)) return Number(key) < length() ? read(key) : undefined;\n+\t\t\t// No length read first (abso Capacity): set writes the row and the length together and nothing shrinks, so no\n+\t\t\t// row lies past the length. Half of a cold layout's reads were this length.\n+\t\t\tif (indexKey(key)) return read(key);\n \t\t\treturn Reflect.get(target, key, receiver);\n \t\t},\n \t\tset(_target, key, value) {\n@@ -386,6 +421,14 @@\n \t\t\t}\n \t\t\tfor (const [key] of storage.entries(`${name}:lengths`)) yield key;\n \t\t},\n+\t\t/** Values [index * PAGE, (index + 1) * PAGE) of `key`'s list: one page, not the whole list (abso Capacity). */\n+\t\tpageAt(key: string, index: number): readonly V[] {\n+\t\t\tconst count = length(key);\n+\t\t\tconst values = page(key, index);\n+\t\t\tif (values.length !== Math.min(pageSize, count - index * pageSize))\n+\t\t\t\tthrow new Error(`incomplete writer list page in ${name}`);\n+\t\t\treturn values;\n+\t\t},\n \t\t*values(key: string): IterableIterator<V> {\n \t\t\tconst count = length(key);\n \t\t\tfor (let start = 0; start < count; start += pageSize) {"}],"unmatched":[]}