{"draft":null,"from":135,"to":136,"files":[{"path":"src/engine/state-source.ts","added":39,"removed":11,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -14,13 +14,19 @@\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+ * Operation-local read cache with fixed payload and entry limits. Private numeric\n+ * pages, immutable scalars and serialized objects preserve the copy contract. Discard it after an outer host\n  * rollback or any mutation made through another storage instance.\n  */\n+type CachedValue = { bytes: number } & (\n+\t| { kind: \"scalar\"; value: string | number | boolean | null | undefined }\n+\t| { kind: \"numbers\"; value: readonly number[] }\n+\t| { kind: \"json\"; json: string }\n+);\n+\n export class CachedStateStorage implements GraphStateStorage {\n \treadonly #source: GraphStateStorage;\n-\treadonly #cache = new Map<string, { json: string; bytes: number }>();\n+\treadonly #cache = new Map<string, CachedValue>();\n \treadonly #maxBytes: number;\n \treadonly #maxEntries: number;\n \t#bytes = 0;\n@@ -54,18 +60,40 @@\n \t\tif (hit) {\n \t\t\tthis.#cache.delete(cacheKey);\n \t\t\tthis.#cache.set(cacheKey, hit);\n+\t\t\tif (hit.kind === \"scalar\") return hit.value;\n+\t\t\tif (hit.kind === \"numbers\") return hit.value.slice();\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\tconst overhead = 2 * cacheKey.length + 160;\n+\t\tlet entry: CachedValue;\n+\t\tif (\n+\t\t\tvalue === undefined ||\n+\t\t\tvalue === null ||\n+\t\t\ttypeof value === \"string\" ||\n+\t\t\ttypeof value === \"boolean\" ||\n+\t\t\t(typeof value === \"number\" && Number.isFinite(value))\n+\t\t) {\n+\t\t\tentry = { kind: \"scalar\", value, bytes: overhead + (typeof value === \"string\" ? 2 * value.length : 16) };\n+\t\t} else if (\n+\t\t\tArray.isArray(value) &&\n+\t\t\tvalue.length <= 256 &&\n+\t\t\tObject.keys(value).length === value.length &&\n+\t\t\tvalue.every(Number.isSafeInteger)\n+\t\t) {\n+\t\t\t// Store one private, bounded numeric page. Public reads still receive a\n+\t\t\t// fresh copy; neither the source result nor a reader can mutate the cache.\n+\t\t\tentry = { kind: \"numbers\", value: value.slice(), bytes: overhead + 32 + value.length * 8 };\n+\t\t} else {\n+\t\t\tconst json = JSON.stringify({ value });\n+\t\t\tentry = { kind: \"json\", json, bytes: overhead + 2 * json.length };\n+\t\t}\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\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, { json, bytes });\n-\t\t\tthis.#bytes += bytes;\n+\t\t\tthis.#cache.set(cacheKey, entry);\n+\t\t\tthis.#bytes += entry.bytes;\n \t\t}\n \t\treturn value;\n \t}"}],"unmatched":[]}