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/views.ts?draft=login-wait&format=json
59 linesFirst import 100%0 versions changed it
1// The views a project lays out from sets of atoms, cached under a byte budget (#46, disk-first 1). Published
2// versions never change, so their views are kept by name; any other set is kept by its exact contents.
3
4import { type AtomId, type Graph, View } from "../../src/engine/index.ts";
5
6/**
7 * About how many bytes the cached views may hold together (#46). A laid-out
8 * line costs about 10 bytes (view.ts), so this holds over 2 million of them;
9 * the records a view keeps for the file it was last asked about count too.
10 */
11const VIEW_BUDGET = 24_000_000;
13/** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */
14export class ViewCache {
15 readonly #graph: Graph;
16 readonly #cache = new Map<string, View>();
18 constructor(graph: Graph) {
19 this.#graph = graph;
20 }
22 /**
23 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,
24 * #46): each view holds every line its files ever had, so a few views of a
25 * large generated file add up. At most 32.
26 */
27 #trim(): void {
28 let total = 0;
29 for (const v of this.#cache.values()) total += v.weight();
30 for (const [key, v] of this.#cache) {
31 if (total <= VIEW_BUDGET && this.#cache.size < 32) break;
32 total -= v.weight();
33 this.#cache.delete(key);
34 }
35 }
37 /** A view of a set, cached by the set's exact contents. */
38 view(set: ReadonlySet<AtomId>): View {
39 const key = [...set]
40 .map((id) => this.#graph.atom(id).seq)
41 .sort((a, b) => a - b)
42 .join(",");
43 return this.keyed(key, () => set);
44 }
46 /** A view cached under `key`, made from `set()` when it isn't there. */
47 keyed(key: string, set: () => ReadonlySet<AtomId>): View {
48 let view = this.#cache.get(key);
49 if (view) {
50 // Most recently used last, so eviction takes the oldest.
51 this.#cache.delete(key);
52 } else {
53 view = new View(this.#graph, set());
54 this.#trim();
55 }
56 this.#cache.set(key, view);
57 return view;
58 }