- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/views.ts?version=190&format=json
82 linesFirst import 61% · codex-capacity-chair 34% · codex-capacity-policy 5%Last change codex-capacity-chair in version 143, 4 versions changed it
You're reading version 190. The newest is 318, and 4 versions since then changed this file.
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. Immutable indexed sets use their identity;
3// ordinary in-memory sets use their exact contents.
5import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
6import { StableSet } from "../../src/engine/state-source.ts";
8/**
9 * About how many bytes the cached views may hold together (#46). A laid-out
10 * line costs about 10 bytes (view.ts), so this holds over 2 million of them;
11 * the records a view keeps for the file it was last asked about count too.
12 */
13const VIEW_BUDGET = 24_000_000;
15/** Internal opt-in. The owner validates identity and lifetime on every returned reader. */
16export interface ReadViewProvider {
17 keyed(key: string): ReadView;
18}
20/** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */
21export class ViewCache {
22 readonly #graph: Graph;
23 readonly #provider?: ReadViewProvider;
24 readonly #cache = new Map<string | StableSet<AtomId>, View>();
26 constructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider) {
27 this.#graph = graph;
28 this.#provider = provider?.(graph);
29 }
30 assertUnkeyedAllowed(): void {
31 if (this.#provider) throw Error("unsupported unkeyed projection membership");
32 }
34 /**
35 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,
36 * #46): each view holds every line its files ever had, so a few views of a
37 * large generated file add up. At most 32.
38 */
39 #trim(): void {
40 let total = 0;
41 for (const v of this.#cache.values()) total += v.weight();
42 for (const [key, v] of this.#cache) {
43 if (total <= VIEW_BUDGET && this.#cache.size <= 32) break;
44 total -= v.weight();
45 this.#cache.delete(key);
46 }
47 }
49 /** Indexed snapshots use identity; ordinary sets use their exact contents. */
50 view(set: ReadonlySet<AtomId>): ReadView {
51 this.assertUnkeyedAllowed();
52 // Immutable indexed membership is already a stable identity. Building
53 // a sorted history-sized key defeats its external-storage contract.
54 if (set instanceof StableSet) return this.#get(set, () => set);
55 const key = [...set]
56 .map((id) => this.#graph.atom(id).seq)
57 .sort((a, b) => a - b)
58 .join(",");
59 return this.keyed(key, () => set);
60 }
62 /** A view cached under `key`, made from `set()` when it isn't there. */
63 keyed(key: string, set: () => ReadonlySet<AtomId>): ReadView {
64 if (this.#provider) return this.#provider.keyed(key);
65 return this.#get(key, set);
66 }
68 #get(key: string | StableSet<AtomId>, set: () => ReadonlySet<AtomId>): View {
69 let view = this.#cache.get(key);
70 if (view) {
71 // Most recently used last, so eviction takes the oldest.
72 this.#cache.delete(key);
73 } else {
74 view = new View(this.#graph, set());
75 }
76 // Account the selected view, including growth since its last access,
77 // before evicting older entries. An oversized view may evict itself.
78 this.#cache.set(key, view);
79 this.#trim();
80 return view;
81 }
82}