{"draft":null,"from":110,"to":111,"files":[{"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;"}],"unmatched":[]}