Enforce retained view cache budget on access
of shared·codex-capacity-policy··Not checked
Version 103 · Files at version 104
2 files, 111 lines added, 2 removed.
--- /dev/null
+++ b/worker/project/test/view-cache.test.ts
@@ -0,0 +1,107 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { buildBase, Graph, View } from "../../../src/engine/index.ts"; 4+import { ViewCache } from "../views.ts"; 6+const VIEW_BUDGET = 24_000_000; 8+type WeightControls = { 9+ setIncomingWeights(values: number[]): void; 10+ setWeight(view: View, weight: number): void; 13+function withControlledWeights(run: (controls: WeightControls) => void): void { 14+ const original = Object.getOwnPropertyDescriptor(View.prototype, "weight")!; 15+ const weights = new WeakMap<View, number>(); 16+ let newWeights: number[] = []; 17+ Object.defineProperty(View.prototype, "weight", { 20+ if (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0); 21+ return weights.get(this)!; 24+ const controls: WeightControls = { 25+ setWeight: (view, weight) => { 26+ weights.set(view, weight); 28+ setIncomingWeights: (values) => { 29+ newWeights = [...values]; 35+ Object.defineProperty(View.prototype, "weight", original); 39+function get(cache: ViewCache, key: string): View { 40+ return cache.keyed(key, () => new Set()); 43+test("the incoming view counts toward the retained byte budget", () => { 44+ withControlledWeights((control) => { 45+ const cache = new ViewCache(new Graph()); 46+ control.setIncomingWeights([10_000_000, 15_000_001]); 47+ const old = get(cache, "old"); 48+ const incoming = get(cache, "incoming"); 49+ assert.equal(get(cache, "incoming"), incoming); 50+ assert.notEqual(get(cache, "old"), old); 54+test("the cache retains exactly 32 views and evicts the least recently used one", () => { 55+ withControlledWeights((control) => { 56+ const cache = new ViewCache(new Graph()); 57+ control.setIncomingWeights(Array(33).fill(0)); 58+ const first = get(cache, "key-0"); 59+ const retained = Array.from({ length: 31 }, (_, i) => get(cache, `key-${i + 1}`)); 60+ assert.equal(get(cache, "key-0"), first); 61+ const last = get(cache, "key-32"); 62+ assert.equal(get(cache, "key-0"), first); 63+ assert.equal(get(cache, "key-32"), last); 64+ for (let i = 1; i < retained.length; i++) assert.equal(get(cache, `key-${i + 1}`), retained[i]); 65+ assert.notEqual(get(cache, "key-1"), retained[0]); 69+test("a repeated hit reweighs lazy growth and evicts older entries", () => { 70+ withControlledWeights((control) => { 71+ const cache = new ViewCache(new Graph()); 72+ control.setIncomingWeights([10_000_000, 10_000_000]); 73+ const growing = get(cache, "growing"); 74+ const older = get(cache, "older"); 75+ control.setWeight(growing, 16_000_000); 76+ assert.equal(get(cache, "growing"), growing); 77+ assert.notEqual(get(cache, "older"), older); 81+test("an oversized view is returned but not retained", () => { 82+ withControlledWeights((control) => { 83+ const cache = new ViewCache(new Graph()); 84+ control.setIncomingWeights([VIEW_BUDGET + 1, 0]); 85+ const oversized = get(cache, "large"); 86+ assert.ok(oversized instanceof View); 87+ assert.notEqual(get(cache, "large"), oversized); 91+test("cache eviction preserves real Graph and View layouts", () => { 92+ const graph = new Graph(); 95+ { "notes.txt": "one\ntwo\n", "folder/readme.md": "cached view\n" }, 96+ { author: "test", message: "view cache" }, 99+ const set = new Set(["a0"]); 100+ const cache = new ViewCache(graph); 101+ const cached = cache.view(set); 102+ const expected = new View(graph, set); 103+ assert.deepEqual(cached.files(), expected.files()); 104+ assert.equal(cached.text("notes.txt"), expected.text("notes.txt")); 105+ for (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set); 106+ assert.deepEqual(cache.view(set).files(), new View(graph, set).files()); --- a/worker/project/views.ts
+++ b/worker/project/views.ts
@@ -28,7 +28,7 @@
2929 for (const v of this.#cache.values()) total += v.weight(); 3030 for (const [key, v] of this.#cache) { 31− if (total <= VIEW_BUDGET && this.#cache.size < 32) break;
31+ if (total <= VIEW_BUDGET && this.#cache.size <= 32) break; 3232 total -= v.weight(); 3333 this.#cache.delete(key); @@ -51,9 +51,11 @@
5151 this.#cache.delete(key); 5353 view = new View(this.#graph, set()); 54− this.#trim();
55+ // Account the selected view, including growth since its last access, 56+ // before evicting older entries. An oversized view may evict itself. 5657 this.#cache.set(key, view);