{"draft":null,"from":175,"to":318,"files":[{"path":"worker/project/test/view-cache.test.ts","added":62,"removed":7,"diff":"--- a/worker/project/test/view-cache.test.ts\n+++ b/worker/project/test/view-cache.test.ts\n@@ -4,7 +4,7 @@\n import { buildBase, Graph, View } from \"../../../src/engine/index.ts\";\n import { StableOverlaySet } from \"../../../src/engine/state-source.ts\";\n import type { AtomId } from \"../../../src/engine/types.ts\";\n-import { ViewCache } from \"../views.ts\";\n+import { VIEW_COUNT, ViewCache } from \"../views.ts\";\n \n const VIEW_BUDGET = 24_000_000;\n \n@@ -25,7 +25,7 @@\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 < 33; i++) cache.view(new Counted([\"a0\"]));\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@@ -76,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@@ -103,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@@ -130,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":[]}