{"version":111,"agent":"codex-capacity-chair","message":"Bound persistent writer read caching and verify rollback isolation","time":1791827375765,"timeIso":"2026-10-12T17:49:35.765Z","changes":4,"change":1284,"files":["src/engine/state-source.ts","src/engine/test/state-cache.test.ts","src/engine/test/storage-helper.ts","worker/project/project.ts"],"draft":null,"diff":[{"path":"src/engine/state-source.ts","added":77,"removed":0,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -13,6 +13,83 @@\n \ttransaction<T>(body: () => T): T;\n }\n \n+/**\n+ * Operation-local read cache with fixed payload and entry limits. Serialized\n+ * values preserve the storage copy contract. Discard it after an outer host\n+ * rollback or any mutation made through another storage instance.\n+ */\n+export class CachedStateStorage implements GraphStateStorage {\n+\treadonly #source: GraphStateStorage;\n+\treadonly #cache = new Map<string, { json: string; bytes: number }>();\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\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}\n+\tget retainedBytes(): number {\n+\t\treturn this.#bytes;\n+\t}\n+\tget retainedEntries(): number {\n+\t\treturn this.#cache.size;\n+\t}\n+\tclear(): void {\n+\t\tthis.#cache.clear();\n+\t\tthis.#bytes = 0;\n+\t}\n+\t#drop(key: string): void {\n+\t\tconst previous = this.#cache.get(key);\n+\t\tif (previous) {\n+\t\t\tthis.#cache.delete(key);\n+\t\t\tthis.#bytes -= previous.bytes;\n+\t\t}\n+\t}\n+\tread(collection: string, key: string): unknown {\n+\t\tconst cacheKey = JSON.stringify([collection, key]);\n+\t\tconst hit = this.#cache.get(cacheKey);\n+\t\tif (hit) {\n+\t\t\tthis.#cache.delete(cacheKey);\n+\t\t\tthis.#cache.set(cacheKey, hit);\n+\t\t\treturn (JSON.parse(hit.json) as { value?: unknown }).value;\n+\t\t}\n+\t\tconst value = this.#source.read(collection, key);\n+\t\tconst json = JSON.stringify({ value });\n+\t\t// Charge two bytes per UTF-16 unit and a conservative fixed entry cost.\n+\t\t// This bounds retained payload; it is not an isolate heap measurement.\n+\t\tconst bytes = 2 * (json.length + cacheKey.length) + 128;\n+\t\tif (bytes <= this.#maxBytes && this.#maxEntries > 0) {\n+\t\t\twhile (this.#bytes + 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, { json, bytes });\n+\t\t\tthis.#bytes += bytes;\n+\t\t}\n+\t\treturn value;\n+\t}\n+\twrite(collection: string, key: string, value: unknown): void {\n+\t\tthis.#drop(JSON.stringify([collection, key]));\n+\t\tthis.#source.write(collection, key, value);\n+\t}\n+\tremove(collection: string, key: string): void {\n+\t\tthis.#drop(JSON.stringify([collection, key]));\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\ttry {\n+\t\t\treturn this.#source.transaction(body);\n+\t\t} catch (error) {\n+\t\t\tthis.clear();\n+\t\t\tthrow error;\n+\t\t}\n+\t}\n+}\n+\n /** Immutable membership whose implementation does not retain its enumerated keys. */\n export abstract class StableSet<T> implements ReadonlySet<T> {\n \tabstract has(value: T): boolean;"},{"path":"src/engine/test/state-cache.test.ts","added":59,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/state-cache.test.ts\n@@ -0,0 +1,59 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { CachedStateStorage } from \"../state-source.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"bounded writer cache copies values, invalidates writes and clears after rollback\", () => {\n+\tconst source = new SqlState();\n+\tconst cache = new CachedStateStorage(source, 1024, 3);\n+\tsource.write(\"rows\", \"a\", { list: [1] });\n+\tconst first = cache.read(\"rows\", \"a\") as { list: number[] };\n+\tfirst.list.push(2);\n+\tconst reads = source.reads;\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [1] });\n+\tassert.equal(source.reads, reads);\n+\tcache.write(\"rows\", \"a\", { list: [3] });\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [3] });\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tcache.transaction(() => {\n+\t\t\t\tcache.write(\"rows\", \"a\", { list: [4] });\n+\t\t\t\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [4] });\n+\t\t\t\tthrow new Error(\"rollback\");\n+\t\t\t}),\n+\t\t/rollback/,\n+\t);\n+\tassert.equal(cache.retainedEntries, 0);\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [3] });\n+\tcache.remove(\"rows\", \"a\");\n+\tassert.equal(cache.read(\"rows\", \"a\"), undefined);\n+\tcache.write(\"rows\", \"a\", \"restored\");\n+\tassert.equal(cache.read(\"rows\", \"a\"), \"restored\");\n+\tsource.db.close();\n+});\n+\n+test(\"writer cache evicts by both payload and entry count and skips oversized values\", () => {\n+\tconst source = new SqlState();\n+\tconst cache = new CachedStateStorage(source, 1024, 3);\n+\tfor (let i = 0; i < 100; i++) {\n+\t\tcache.write(\"rows\", String(i), \"small\");\n+\t\tassert.equal(cache.read(\"rows\", String(i)), \"small\");\n+\t\tassert.ok(cache.retainedBytes <= 1024);\n+\t\tassert.ok(cache.retainedEntries <= 3);\n+\t}\n+\tconst reads = source.reads;\n+\tcache.read(\"rows\", \"99\");\n+\tassert.equal(source.reads, reads);\n+\tcache.read(\"rows\", \"0\");\n+\tassert.equal(source.reads, reads + 1);\n+\tcache.write(\"rows\", \"large\", \"x\".repeat(1024));\n+\tconst before = cache.retainedBytes;\n+\tcache.read(\"rows\", \"large\");\n+\tassert.equal(cache.retainedBytes, before);\n+\tconst afterLarge = source.reads;\n+\tcache.read(\"rows\", \"large\");\n+\tassert.equal(source.reads, afterLarge + 1);\n+\tcache.clear();\n+\tassert.equal(cache.retainedBytes, 0);\n+\tsource.db.close();\n+});"},{"path":"src/engine/test/storage-helper.ts","added":6,"removed":3,"diff":"--- a/src/engine/test/storage-helper.ts\n+++ b/src/engine/test/storage-helper.ts\n@@ -2,13 +2,16 @@\n import type { GraphStateStorage } from \"../state-source.ts\";\n \n export class SqlState implements GraphStateStorage {\n-\treadonly db = new DatabaseSync(\":memory:\");\n+\treadonly db: DatabaseSync;\n \treads = 0;\n \twrites = 0;\n \tscans = 0;\n \tfailWrite = Infinity;\n-\tconstructor() {\n-\t\tthis.db.exec(\"CREATE TABLE state (collection TEXT, key TEXT, value TEXT, PRIMARY KEY(collection,key))\");\n+\tconstructor(filename = \":memory:\") {\n+\t\tthis.db = new DatabaseSync(filename);\n+\t\tthis.db.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS state (collection TEXT, key TEXT, value TEXT, PRIMARY KEY(collection,key))\",\n+\t\t);\n \t}\n \tread(collection: string, key: string): unknown {\n \t\tthis.reads++;"},{"path":"worker/project/project.ts","added":6,"removed":5,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -14,6 +14,7 @@\n \n import { type Atom, type AtomId, EngineError, Graph, type LineId, type View } from \"../../src/engine/index.ts\";\n import {\n+\tCachedStateStorage,\n \ttype GraphStateStorage,\n \tStableOverlaySet,\n \tStableSet,\n@@ -346,9 +347,9 @@\n \n \tconstructor(store: ProjectStore, options: ProjectOptions = {}, checkpoint?: typeof WRITER_REOPEN) {\n \t\tthis.#store = store;\n-\t\tthis.#writerStorage = options.writerStorage;\n-\t\tif (options.writerStorage) {\n-\t\t\tconst storage = options.writerStorage;\n+\t\tthis.#writerStorage = options.writerStorage ? new CachedStateStorage(options.writerStorage) : undefined;\n+\t\tif (this.#writerStorage) {\n+\t\t\tconst storage = this.#writerStorage;\n \t\t\t// A stored Graph cannot be replayed over itself. Check before any\n \t\t\t// constructor writes so an accidental reopen preserves the namespace.\n \t\t\tif (\n@@ -398,8 +399,8 @@\n \t\t\tthrow new Error(`log entry ${at} holds no atom ${seq}`);\n \t\t};\n \t\t// Line text and each atom's deletes and claim keys stay in the log (#46), read back when asked for.\n-\t\tthis.#graph = options.writerStorage\n-\t\t\t? new Graph({ storage: options.writerStorage })\n+\t\tthis.#graph = this.#writerStorage\n+\t\t\t? new Graph({ storage: this.#writerStorage })\n \t\t\t: entryAt\n \t\t\t\t? new Graph({ texts: (seq) => stored(seq).inserts.map((r) => r.lines), lists: (seq) => stored(seq) })\n \t\t\t\t: new Graph();"}],"ci":null}