{"version":104,"agent":"codex-capacity-policy","message":"Enforce retained view cache budget on access","time":1791764358789,"timeIso":"2026-10-12T00:19:18.789Z","changes":2,"change":1237,"files":["worker/project/test/view-cache.test.ts","worker/project/views.ts"],"draft":null,"diff":[{"path":"worker/project/test/view-cache.test.ts","added":107,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/view-cache.test.ts\n@@ -0,0 +1,107 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase, Graph, View } from \"../../../src/engine/index.ts\";\n+import { ViewCache } from \"../views.ts\";\n+\n+const VIEW_BUDGET = 24_000_000;\n+\n+type WeightControls = {\n+\tsetIncomingWeights(values: number[]): void;\n+\tsetWeight(view: View, weight: number): void;\n+};\n+\n+function withControlledWeights(run: (controls: WeightControls) => void): void {\n+\tconst original = Object.getOwnPropertyDescriptor(View.prototype, \"weight\")!;\n+\tconst weights = new WeakMap<View, number>();\n+\tlet newWeights: number[] = [];\n+\tObject.defineProperty(View.prototype, \"weight\", {\n+\t\tconfigurable: true,\n+\t\tvalue(this: View) {\n+\t\t\tif (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0);\n+\t\t\treturn weights.get(this)!;\n+\t\t},\n+\t});\n+\tconst controls: WeightControls = {\n+\t\tsetWeight: (view, weight) => {\n+\t\t\tweights.set(view, weight);\n+\t\t},\n+\t\tsetIncomingWeights: (values) => {\n+\t\t\tnewWeights = [...values];\n+\t\t},\n+\t};\n+\ttry {\n+\t\trun(controls);\n+\t} finally {\n+\t\tObject.defineProperty(View.prototype, \"weight\", original);\n+\t}\n+}\n+\n+function get(cache: ViewCache, key: string): View {\n+\treturn cache.keyed(key, () => new Set());\n+}\n+\n+test(\"the incoming view counts toward the retained byte budget\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 15_000_001]);\n+\t\tconst old = get(cache, \"old\");\n+\t\tconst incoming = get(cache, \"incoming\");\n+\t\tassert.equal(get(cache, \"incoming\"), incoming);\n+\t\tassert.notEqual(get(cache, \"old\"), old);\n+\t});\n+});\n+\n+test(\"the cache retains exactly 32 views and evicts the least recently used one\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights(Array(33).fill(0));\n+\t\tconst first = get(cache, \"key-0\");\n+\t\tconst retained = Array.from({ length: 31 }, (_, i) => get(cache, `key-${i + 1}`));\n+\t\tassert.equal(get(cache, \"key-0\"), first);\n+\t\tconst last = get(cache, \"key-32\");\n+\t\tassert.equal(get(cache, \"key-0\"), first);\n+\t\tassert.equal(get(cache, \"key-32\"), last);\n+\t\tfor (let i = 1; i < retained.length; i++) assert.equal(get(cache, `key-${i + 1}`), retained[i]);\n+\t\tassert.notEqual(get(cache, \"key-1\"), retained[0]);\n+\t});\n+});\n+\n+test(\"a repeated hit reweighs lazy growth and evicts older entries\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 10_000_000]);\n+\t\tconst growing = get(cache, \"growing\");\n+\t\tconst older = get(cache, \"older\");\n+\t\tcontrol.setWeight(growing, 16_000_000);\n+\t\tassert.equal(get(cache, \"growing\"), growing);\n+\t\tassert.notEqual(get(cache, \"older\"), older);\n+\t});\n+});\n+\n+test(\"an oversized view is returned but not retained\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([VIEW_BUDGET + 1, 0]);\n+\t\tconst oversized = get(cache, \"large\");\n+\t\tassert.ok(oversized instanceof View);\n+\t\tassert.notEqual(get(cache, \"large\"), oversized);\n+\t});\n+});\n+\n+test(\"cache eviction preserves real Graph and View layouts\", () => {\n+\tconst graph = new Graph();\n+\tgraph.add(\n+\t\tbuildBase(\n+\t\t\t{ \"notes.txt\": \"one\\ntwo\\n\", \"folder/readme.md\": \"cached view\\n\" },\n+\t\t\t{ author: \"test\", message: \"view cache\" },\n+\t\t),\n+\t);\n+\tconst set = new Set([\"a0\"]);\n+\tconst cache = new ViewCache(graph);\n+\tconst cached = cache.view(set);\n+\tconst expected = new View(graph, set);\n+\tassert.deepEqual(cached.files(), expected.files());\n+\tassert.equal(cached.text(\"notes.txt\"), expected.text(\"notes.txt\"));\n+\tfor (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);\n+\tassert.deepEqual(cache.view(set).files(), new View(graph, set).files());\n+});"},{"path":"worker/project/views.ts","added":4,"removed":2,"diff":"--- a/worker/project/views.ts\n+++ b/worker/project/views.ts\n@@ -28,7 +28,7 @@\n \t\tlet total = 0;\n \t\tfor (const v of this.#cache.values()) total += v.weight();\n \t\tfor (const [key, v] of this.#cache) {\n-\t\t\tif (total <= VIEW_BUDGET && this.#cache.size < 32) break;\n+\t\t\tif (total <= VIEW_BUDGET && this.#cache.size <= 32) break;\n \t\t\ttotal -= v.weight();\n \t\t\tthis.#cache.delete(key);\n \t\t}\n@@ -51,9 +51,11 @@\n \t\t\tthis.#cache.delete(key);\n \t\t} else {\n \t\t\tview = new View(this.#graph, set());\n-\t\t\tthis.#trim();\n \t\t}\n+\t\t// Account the selected view, including growth since its last access,\n+\t\t// before evicting older entries. An oversized view may evict itself.\n \t\tthis.#cache.set(key, view);\n+\t\tthis.#trim();\n \t\treturn view;\n \t}\n }"}],"ci":null}