Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/test/view-cache.test.ts?version=308&format=json
187 linescodex-capacity-policy 52% · capacity 33% · codex-capacity-chair 15%Last change capacity in version 263, 6 versions changed it

You're reading version 308. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1import assert from "node:assert/strict";
2import { test } from "node:test";
3import type { ReadView } from "../../../src/engine/index.ts";
4import { buildBase, Graph, View } from "../../../src/engine/index.ts";
5import { StableOverlaySet } from "../../../src/engine/state-source.ts";
6import type { AtomId } from "../../../src/engine/types.ts";
7import { VIEW_COUNT, ViewCache } from "../views.ts";
8
9const VIEW_BUDGET = 24_000_000;
11test("indexed view cache keys never enumerate membership and remain bounded by eviction", () => {
12 class Counted extends StableOverlaySet<AtomId> {
13 iterations = 0;
14 override values(): SetIterator<AtomId> {
15 this.iterations++;
16 return super.values();
17 }
18 }
19 const graph = new Graph();
20 graph.add(buildBase({ "a.txt": "one\n" }, { author: "test", message: "base" }));
21 const cache = new ViewCache(graph);
22 const set = new Counted(["a0"]);
23 const view = cache.view(set);
24 assert.equal(set.iterations, 1, "only View closure validation enumerates membership");
25 assert.equal(cache.view(set), view);
26 assert.equal(set.iterations, 1, "cache hit must not construct a full-history key");
27 assert.equal(view.text("a.txt"), "one\n");
28 for (let i = 0; i < VIEW_COUNT + 1; i++) cache.view(new Counted(["a0"]));
29 assert.notEqual(cache.view(set), view, "indexed identities obey the same cache eviction limit");
30 assert.equal(set.iterations, 2);
31});
33type WeightControls = {
34 setIncomingWeights(values: number[]): void;
35 setWeight(view: ReadView, weight: number): void;
36};
38function withControlledWeights(run: (controls: WeightControls) => void): void {
39 const original = Object.getOwnPropertyDescriptor(View.prototype, "weight")!;
40 const weights = new WeakMap<ReadView, number>();
41 let newWeights: number[] = [];
42 Object.defineProperty(View.prototype, "weight", {
43 configurable: true,
44 value(this: ReadView) {
45 if (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0);
46 return weights.get(this)!;
47 },
48 });
49 const controls: WeightControls = {
50 setWeight: (view, weight) => {
51 weights.set(view, weight);
52 },
53 setIncomingWeights: (values) => {
54 newWeights = [...values];
55 },
56 };
57 try {
58 run(controls);
59 } finally {
60 Object.defineProperty(View.prototype, "weight", original);
61 }
64function get(cache: ViewCache, key: string): ReadView {
65 return cache.keyed(key, () => new Set());
68test("the incoming view counts toward the retained byte budget", () => {
69 withControlledWeights((control) => {
70 const cache = new ViewCache(new Graph());
71 control.setIncomingWeights([10_000_000, 15_000_001]);
72 const old = get(cache, "old");
73 const incoming = get(cache, "incoming");
74 assert.equal(get(cache, "incoming"), incoming);
75 assert.notEqual(get(cache, "old"), old);
76 });
77});
79test("the cache retains exactly VIEW_COUNT views and evicts the least recently used one", () => {
80 withControlledWeights((control) => {
81 const cache = new ViewCache(new Graph());
82 control.setIncomingWeights(Array(VIEW_COUNT + 1).fill(0));
83 const first = get(cache, "key-0");
84 const retained = Array.from({ length: VIEW_COUNT - 1 }, (_, i) => get(cache, `key-${i + 1}`));
85 assert.equal(get(cache, "key-0"), first);
86 const last = get(cache, `key-${VIEW_COUNT}`);
87 assert.equal(get(cache, "key-0"), first);
88 assert.equal(get(cache, `key-${VIEW_COUNT}`), last);
89 for (let i = 1; i < retained.length; i++) assert.equal(get(cache, `key-${i + 1}`), retained[i]);
90 assert.notEqual(get(cache, "key-1"), retained[0]);
91 });
92});
94test("a repeated hit reweighs lazy growth and evicts older entries", () => {
95 withControlledWeights((control) => {
96 const cache = new ViewCache(new Graph());
97 control.setIncomingWeights([10_000_000, 10_000_000]);
98 const growing = get(cache, "growing");
99 const older = get(cache, "older");
100 control.setWeight(growing, 16_000_000);
101 assert.equal(get(cache, "growing"), growing);
102 assert.notEqual(get(cache, "older"), older);
103 });
104});
106test("trim reweighs growth with no further hit and evicts until the views fit (abso Capacity)", () => {
107 withControlledWeights((control) => {
108 const cache = new ViewCache(new Graph());
109 control.setIncomingWeights([10_000_000, 10_000_000]);
110 const growing = get(cache, "growing");
111 const newer = get(cache, "newer");
112 // A call lays files out in the older view after the cache last weighed it, and nothing asks for it again.
113 control.setWeight(growing, 16_000_000);
114 cache.trim();
115 assert.equal(get(cache, "newer"), newer);
116 assert.notEqual(get(cache, "growing"), growing);
117 });
118});
120test("an oversized view is returned but not retained", () => {
121 withControlledWeights((control) => {
122 const cache = new ViewCache(new Graph());
123 control.setIncomingWeights([VIEW_BUDGET + 1, 0]);
124 const oversized = get(cache, "large");
125 assert.ok(oversized instanceof View);
126 assert.notEqual(get(cache, "large"), oversized);
127 });
128});
130test("cache eviction preserves real Graph and View layouts", () => {
131 const graph = new Graph();
132 graph.add(
133 buildBase(
134 { "notes.txt": "one\ntwo\n", "folder/readme.md": "cached view\n" },
135 { author: "test", message: "view cache" },
136 ),
137 );
138 const set = new Set(["a0"]);
139 const cache = new ViewCache(graph);
140 const cached = cache.view(set);
141 const expected = new View(graph, set);
142 assert.deepEqual(cached.files(), expected.files());
143 assert.equal(cached.text("notes.txt"), expected.text("notes.txt"));
144 for (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);
145 assert.deepEqual(cache.view(set).files(), new View(graph, set).files());
146});
148test("trim lets go of the least recently used file layouts across views before whole views (abso Capacity)", () => {
149 const graph = new Graph();
150 const lines = (name: string) => Array.from({ length: 40 }, (_, i) => `${name} line ${i}\n`).join("");
151 graph.add(
152 buildBase({ "a.txt": lines("a"), "b.txt": lines("b"), "c.txt": lines("c") }, { author: "test", message: "base" }),
153 );
154 const set = new Set(["a0"]);
155 // a.txt in x first, then b.txt in y, then c.txt in x: a.txt is the least recently used.
156 const use = (cache: ViewCache) => {
157 const x = cache.keyed("x", () => set);
158 const y = cache.keyed("y", () => set);
159 x.text("a.txt");
160 y.text("b.txt");
161 x.text("c.txt");
162 return { x: x as View, y: y as View };
163 };
164 const probe = use(new ViewCache(graph));
165 const total = probe.x.weight() + probe.y.weight();
166 const cache = new ViewCache(graph, undefined, total - 1);
167 const { x, y } = use(cache);
168 cache.trim();
169 assert.deepEqual(
170 [...x.laidOut()].map((f) => f.path),
171 ["c.txt"],
172 );
173 assert.deepEqual(
174 [...y.laidOut()].map((f) => f.path),
175 ["b.txt"],
176 );
177 assert.equal(
178 cache.keyed("x", () => set),
179 x,
180 "both views are still cached",
181 );
182 assert.equal(
183 cache.keyed("y", () => set),
184 y,
185 );
186 assert.equal(x.text("a.txt"), lines("a"), "a.txt is laid out again, the same");
187});