{"draft":null,"from":116,"to":318,"files":[{"path":"worker/project/test/view-cache.test.ts","added":90,"removed":10,"diff":"--- a/worker/project/test/view-cache.test.ts\n+++ b/worker/project/test/view-cache.test.ts\n@@ -1,22 +1,47 @@\n import assert from \"node:assert/strict\";\n import { test } from \"node:test\";\n+import type { ReadView } from \"../../../src/engine/index.ts\";\n import { buildBase, Graph, View } from \"../../../src/engine/index.ts\";\n-import { ViewCache } from \"../views.ts\";\n+import { StableOverlaySet } from \"../../../src/engine/state-source.ts\";\n+import type { AtomId } from \"../../../src/engine/types.ts\";\n+import { VIEW_COUNT, ViewCache } from \"../views.ts\";\n \n const VIEW_BUDGET = 24_000_000;\n \n+test(\"indexed view cache keys never enumerate membership and remain bounded by eviction\", () => {\n+\tclass Counted extends StableOverlaySet<AtomId> {\n+\t\titerations = 0;\n+\t\toverride values(): SetIterator<AtomId> {\n+\t\t\tthis.iterations++;\n+\t\t\treturn super.values();\n+\t\t}\n+\t}\n+\tconst graph = new Graph();\n+\tgraph.add(buildBase({ \"a.txt\": \"one\\n\" }, { author: \"test\", message: \"base\" }));\n+\tconst cache = new ViewCache(graph);\n+\tconst set = new Counted([\"a0\"]);\n+\tconst view = cache.view(set);\n+\tassert.equal(set.iterations, 1, \"only View closure validation enumerates membership\");\n+\tassert.equal(cache.view(set), view);\n+\tassert.equal(set.iterations, 1, \"cache hit must not construct a full-history key\");\n+\tassert.equal(view.text(\"a.txt\"), \"one\\n\");\n+\tfor (let i = 0; i < VIEW_COUNT + 1; i++) cache.view(new Counted([\"a0\"]));\n+\tassert.notEqual(cache.view(set), view, \"indexed identities obey the same cache eviction limit\");\n+\tassert.equal(set.iterations, 2);\n+});\n+\n type WeightControls = {\n \tsetIncomingWeights(values: number[]): void;\n-\tsetWeight(view: View, weight: number): void;\n+\tsetWeight(view: ReadView, 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+\tconst weights = new WeakMap<ReadView, number>();\n \tlet newWeights: number[] = [];\n \tObject.defineProperty(View.prototype, \"weight\", {\n \t\tconfigurable: true,\n-\t\tvalue(this: View) {\n+\t\tvalue(this: ReadView) {\n \t\t\tif (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0);\n \t\t\treturn weights.get(this)!;\n \t\t},\n@@ -36,7 +61,7 @@\n \t}\n }\n \n-function get(cache: ViewCache, key: string): View {\n+function get(cache: ViewCache, key: string): ReadView {\n \treturn cache.keyed(key, () => new Set());\n }\n \n@@ -51,16 +76,16 @@\n \t});\n });\n \n-test(\"the cache retains exactly 32 views and evicts the least recently used one\", () => {\n+test(\"the cache retains exactly VIEW_COUNT 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\tcontrol.setIncomingWeights(Array(VIEW_COUNT + 1).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\tconst retained = Array.from({ length: VIEW_COUNT - 1 }, (_, 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\tconst last = get(cache, `key-${VIEW_COUNT}`);\n \t\tassert.equal(get(cache, \"key-0\"), first);\n-\t\tassert.equal(get(cache, \"key-32\"), last);\n+\t\tassert.equal(get(cache, `key-${VIEW_COUNT}`), 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@@ -78,6 +103,20 @@\n \t});\n });\n \n+test(\"trim reweighs growth with no further hit and evicts until the views fit (abso Capacity)\", () => {\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 newer = get(cache, \"newer\");\n+\t\t// A call lays files out in the older view after the cache last weighed it, and nothing asks for it again.\n+\t\tcontrol.setWeight(growing, 16_000_000);\n+\t\tcache.trim();\n+\t\tassert.equal(get(cache, \"newer\"), newer);\n+\t\tassert.notEqual(get(cache, \"growing\"), growing);\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@@ -105,3 +144,44 @@\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 });\n+\n+test(\"trim lets go of the least recently used file layouts across views before whole views (abso Capacity)\", () => {\n+\tconst graph = new Graph();\n+\tconst lines = (name: string) => Array.from({ length: 40 }, (_, i) => `${name} line ${i}\\n`).join(\"\");\n+\tgraph.add(\n+\t\tbuildBase({ \"a.txt\": lines(\"a\"), \"b.txt\": lines(\"b\"), \"c.txt\": lines(\"c\") }, { author: \"test\", message: \"base\" }),\n+\t);\n+\tconst set = new Set([\"a0\"]);\n+\t// a.txt in x first, then b.txt in y, then c.txt in x: a.txt is the least recently used.\n+\tconst use = (cache: ViewCache) => {\n+\t\tconst x = cache.keyed(\"x\", () => set);\n+\t\tconst y = cache.keyed(\"y\", () => set);\n+\t\tx.text(\"a.txt\");\n+\t\ty.text(\"b.txt\");\n+\t\tx.text(\"c.txt\");\n+\t\treturn { x: x as View, y: y as View };\n+\t};\n+\tconst probe = use(new ViewCache(graph));\n+\tconst total = probe.x.weight() + probe.y.weight();\n+\tconst cache = new ViewCache(graph, undefined, total - 1);\n+\tconst { x, y } = use(cache);\n+\tcache.trim();\n+\tassert.deepEqual(\n+\t\t[...x.laidOut()].map((f) => f.path),\n+\t\t[\"c.txt\"],\n+\t);\n+\tassert.deepEqual(\n+\t\t[...y.laidOut()].map((f) => f.path),\n+\t\t[\"b.txt\"],\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"x\", () => set),\n+\t\tx,\n+\t\t\"both views are still cached\",\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"y\", () => set),\n+\t\ty,\n+\t);\n+\tassert.equal(x.text(\"a.txt\"), lines(\"a\"), \"a.txt is laid out again, the same\");\n+});"}],"unmatched":[]}