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=136&format=json
131 linescodex-capacity-policy 82% · codex-capacity-chair 18%Last change codex-capacity-chair in version 117, 2 versions changed it

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

1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { buildBase, Graph, View } from "../../../src/engine/index.ts";
4import { StableOverlaySet } from "../../../src/engine/state-source.ts";
5import type { AtomId } from "../../../src/engine/types.ts";
6import { ViewCache } from "../views.ts";
7
8const VIEW_BUDGET = 24_000_000;
9
10test("indexed view cache keys never enumerate membership and remain bounded by eviction", () => {
11 class Counted extends StableOverlaySet<AtomId> {
12 iterations = 0;
13 override values(): SetIterator<AtomId> {
14 this.iterations++;
15 return super.values();
16 }
17 }
18 const graph = new Graph();
19 graph.add(buildBase({ "a.txt": "one\n" }, { author: "test", message: "base" }));
20 const cache = new ViewCache(graph);
21 const set = new Counted(["a0"]);
22 const view = cache.view(set);
23 assert.equal(set.iterations, 1, "only View closure validation enumerates membership");
24 assert.equal(cache.view(set), view);
25 assert.equal(set.iterations, 1, "cache hit must not construct a full-history key");
26 assert.equal(view.text("a.txt"), "one\n");
27 for (let i = 0; i < 33; i++) cache.view(new Counted(["a0"]));
28 assert.notEqual(cache.view(set), view, "indexed identities obey the same cache eviction limit");
29 assert.equal(set.iterations, 2);
30});
32type WeightControls = {
33 setIncomingWeights(values: number[]): void;
34 setWeight(view: View, weight: number): void;
35};
37function withControlledWeights(run: (controls: WeightControls) => void): void {
38 const original = Object.getOwnPropertyDescriptor(View.prototype, "weight")!;
39 const weights = new WeakMap<View, number>();
40 let newWeights: number[] = [];
41 Object.defineProperty(View.prototype, "weight", {
42 configurable: true,
43 value(this: View) {
44 if (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0);
45 return weights.get(this)!;
46 },
47 });
48 const controls: WeightControls = {
49 setWeight: (view, weight) => {
50 weights.set(view, weight);
51 },
52 setIncomingWeights: (values) => {
53 newWeights = [...values];
54 },
55 };
56 try {
57 run(controls);
58 } finally {
59 Object.defineProperty(View.prototype, "weight", original);
60 }
63function get(cache: ViewCache, key: string): View {
64 return cache.keyed(key, () => new Set());
67test("the incoming view counts toward the retained byte budget", () => {
68 withControlledWeights((control) => {
69 const cache = new ViewCache(new Graph());
70 control.setIncomingWeights([10_000_000, 15_000_001]);
71 const old = get(cache, "old");
72 const incoming = get(cache, "incoming");
73 assert.equal(get(cache, "incoming"), incoming);
74 assert.notEqual(get(cache, "old"), old);
75 });
76});
78test("the cache retains exactly 32 views and evicts the least recently used one", () => {
79 withControlledWeights((control) => {
80 const cache = new ViewCache(new Graph());
81 control.setIncomingWeights(Array(33).fill(0));
82 const first = get(cache, "key-0");
83 const retained = Array.from({ length: 31 }, (_, i) => get(cache, `key-${i + 1}`));
84 assert.equal(get(cache, "key-0"), first);
85 const last = get(cache, "key-32");
86 assert.equal(get(cache, "key-0"), first);
87 assert.equal(get(cache, "key-32"), last);
88 for (let i = 1; i < retained.length; i++) assert.equal(get(cache, `key-${i + 1}`), retained[i]);
89 assert.notEqual(get(cache, "key-1"), retained[0]);
90 });
91});
93test("a repeated hit reweighs lazy growth and evicts older entries", () => {
94 withControlledWeights((control) => {
95 const cache = new ViewCache(new Graph());
96 control.setIncomingWeights([10_000_000, 10_000_000]);
97 const growing = get(cache, "growing");
98 const older = get(cache, "older");
99 control.setWeight(growing, 16_000_000);
100 assert.equal(get(cache, "growing"), growing);
101 assert.notEqual(get(cache, "older"), older);
102 });
103});
105test("an oversized view is returned but not retained", () => {
106 withControlledWeights((control) => {
107 const cache = new ViewCache(new Graph());
108 control.setIncomingWeights([VIEW_BUDGET + 1, 0]);
109 const oversized = get(cache, "large");
110 assert.ok(oversized instanceof View);
111 assert.notEqual(get(cache, "large"), oversized);
112 });
113});
115test("cache eviction preserves real Graph and View layouts", () => {
116 const graph = new Graph();
117 graph.add(
118 buildBase(
119 { "notes.txt": "one\ntwo\n", "folder/readme.md": "cached view\n" },
120 { author: "test", message: "view cache" },
121 ),
122 );
123 const set = new Set(["a0"]);
124 const cache = new ViewCache(graph);
125 const cached = cache.view(set);
126 const expected = new View(graph, set);
127 assert.deepEqual(cached.files(), expected.files());
128 assert.equal(cached.text("notes.txt"), expected.text("notes.txt"));
129 for (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);
130 assert.deepEqual(cache.view(set).files(), new View(graph, set).files());
131});