- 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=261&format=json
109 linesFirst import 41% · capacity 30% · codex-capacity-chair 26% · codex-capacity-policy 3%Last change capacity in version 247, 7 versions changed it
You're reading version 261. The newest is 318, and 1 version 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/**
16 * At most this many views (abso Capacity). where visits every agent's draft view in turn: with more agents than views,
17 * each visit evicted the next one, and a warm where on abso laid every view out again (4.5 s against 0.33 s). The byte
18 * budget still bounds what they hold.
19 */
20export const VIEW_COUNT = 128;
22/** Internal opt-in. The owner validates identity and lifetime on every returned reader. */
23export interface ReadViewProvider {
24 keyed(key: string): ReadView;
25}
27/** Views by key, most recently used last, held under VIEW_BUDGET bytes and VIEW_COUNT views. */
28export class ViewCache {
29 readonly #graph: Graph;
30 readonly #provider?: ReadViewProvider;
31 readonly #cache = new Map<string | StableSet<AtomId>, View>();
33 constructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider) {
34 this.#graph = graph;
35 this.#provider = provider?.(graph);
36 }
37 assertUnkeyedAllowed(): void {
38 if (this.#provider) throw Error("unsupported unkeyed projection membership");
39 }
41 /**
42 * Weigh every cached view again and evict until they fit (abso Capacity): views grow while a call lays files out,
43 * and #get weighs a view only when it is asked for again. A kept reader trims after each call.
44 */
45 trim(): void {
46 this.#trim();
47 }
49 /**
50 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,
51 * #46): each view holds every line its files ever had, so a few views of a
52 * large generated file add up. At most VIEW_COUNT.
53 */
54 #trim(): void {
55 // The newest views that fit, as evicting oldest first leaves them. A file layout two views share (a draft's view
56 // takes its base's, View.shareFiles) counts once (abso Capacity): counted per view, they filled the budget twice
57 // over, and a kept writer laid abso's drafts out again on every collisions call.
58 const seen = new Set<object>();
59 let total = 0;
60 let kept = 0;
61 let full = false;
62 for (const [key, v] of [...this.#cache].reverse()) {
63 if (!full) {
64 const w = v.weight(seen);
65 if (total + w <= VIEW_BUDGET && kept < VIEW_COUNT) {
66 total += w;
67 kept++;
68 continue;
69 }
70 full = true;
71 }
72 this.#cache.delete(key);
73 }
74 }
76 /** Indexed snapshots use identity; ordinary sets use their exact contents. */
77 view(set: ReadonlySet<AtomId>): ReadView {
78 this.assertUnkeyedAllowed();
79 // Immutable indexed membership is already a stable identity. Building
80 // a sorted history-sized key defeats its external-storage contract.
81 if (set instanceof StableSet) return this.#get(set, () => set);
82 const key = [...set]
83 .map((id) => this.#graph.atom(id).seq)
84 .sort((a, b) => a - b)
85 .join(",");
86 return this.keyed(key, () => set);
87 }
89 /** A view cached under `key`, made from `set()` when it isn't there. */
90 keyed(key: string, set: () => ReadonlySet<AtomId>): ReadView {
91 if (this.#provider) return this.#provider.keyed(key);
92 return this.#get(key, set);
93 }
95 #get(key: string | StableSet<AtomId>, set: () => ReadonlySet<AtomId>): View {
96 let view = this.#cache.get(key);
97 if (view) {
98 // Most recently used last, so eviction takes the oldest.
99 this.#cache.delete(key);
100 } else {
101 view = new View(this.#graph, set());
102 }
103 // Account the selected view, including growth since its last access,
104 // before evicting older entries. An oversized view may evict itself.
105 this.#cache.set(key, view);
106 this.#trim();
107 return view;
108 }
109}