{"version":136,"agent":"codex-capacity-chair","message":"Avoid JSON decoding for bounded numeric cache pages and scalar metadata","time":1791843806498,"timeIso":"2026-10-12T22:23:26.498Z","changes":2,"change":1284,"files":["src/engine/state-source.ts","src/engine/test/state-cache.test.ts"],"draft":null,"diff":[{"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}"},{"path":"src/engine/test/state-cache.test.ts","added":48,"removed":0,"diff":"--- a/src/engine/test/state-cache.test.ts\n+++ b/src/engine/test/state-cache.test.ts\n@@ -3,6 +3,54 @@\n import { CachedStateStorage } from \"../state-source.ts\";\n import { SqlState } from \"./storage-helper.ts\";\n \n+test(\"numeric cache pages are detached on first and repeated reads, and rollback invalidates them\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 4096, 4);\n+\t\tcache.write(\"numbers\", \"page\", [1, 2, 3]);\n+\t\t(cache.read(\"numbers\", \"page\") as number[])[0] = 99;\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\t(cache.read(\"numbers\", \"page\") as number[]).push(4);\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"numbers\", \"page\", [8]);\n+\t\t\t\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [8]);\n+\t\t\t\t\tthrow new Error(\"rollback numeric page\");\n+\t\t\t\t}),\n+\t\t\t/rollback numeric page/,\n+\t\t);\n+\t\tassert.equal(cache.retainedBytes, 0);\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\tcache.remove(\"numbers\", \"page\");\n+\t\tassert.equal(cache.read(\"numbers\", \"page\"), undefined);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"cached scalar metadata and bounded numeric pages need no JSON decode on hits\", (t) => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 8);\n+\t\tconst values = [undefined, null, true, \"metadata\", 42, Array.from({ length: 256 }, (_, n) => n)];\n+\t\tfor (let i = 0; i < values.length; i++) {\n+\t\t\tcache.write(\"rows\", String(i), values[i]);\n+\t\t\tcache.read(\"rows\", String(i));\n+\t\t}\n+\t\tconst reads = source.reads;\n+\t\tt.mock.method(JSON, \"parse\", () => {\n+\t\t\tthrow new Error(\"cache hit unexpectedly decoded JSON\");\n+\t\t});\n+\t\tfor (let i = 0; i < values.length; i++) assert.deepEqual(cache.read(\"rows\", String(i)), values[i]);\n+\t\tassert.equal(source.reads, reads);\n+\t\tassert.ok(cache.retainedBytes <= 8192);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\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);"}],"ci":null}