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

Versions 116 to 318

Compare

+94 −13 in 3 places
Lines 1–9 at 318 (were 1–7 at 116)
11// 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.
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.
34
4−import { type AtomId, type Graph, View } from "../../src/engine/index.ts";
5+import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
6+import { StableSet } from "../../src/engine/state-source.ts";
57
68/**
79 * About how many bytes the cached views may hold together (#46). A laid-out
Lines 12–117 at 318 (were 10–41 at 116)
1012 */
1113const VIEW_BUDGET = 24_000_000;
1214
13−/** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */
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+ */
20+export const VIEW_COUNT = 128;
22+/** Internal opt-in. The owner validates identity and lifetime on every returned reader. */
23+export 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. */
1428export class ViewCache {
1529 readonly #graph: Graph;
16− readonly #cache = new Map<string, View>();
30+ readonly #provider?: ReadViewProvider;
31+ readonly #cache = new Map<string | StableSet<AtomId>, View>();
1732
18− constructor(graph: Graph) {
33+ /** VIEW_BUDGET unless a test asks for less. */
34+ readonly #budget: number;
35+ constructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider, budget = VIEW_BUDGET) {
1936 this.#graph = graph;
37+ this.#provider = provider?.(graph);
38+ this.#budget = budget;
2039 }
40+ assertUnkeyedAllowed(): void {
41+ if (this.#provider) throw Error("unsupported unkeyed projection membership");
42+ }
2143
2244 /**
45+ * Weigh every cached view again and evict until they fit (abso Capacity): views grow while a call lays files out,
46+ * and #get weighs a view only when it is asked for again. A kept reader trims after each call.
47+ */
48+ trim(): void {
49+ const seen = new Set<object>();
50+ let total = 0;
51+ for (const v of this.#cache.values()) total += v.weight(seen);
52+ if (total > this.#budget) this.#shed(total - this.#budget);
53+ this.#trim();
54+ }
56+ /**
57+ * Let go of the least recently used file layouts across every view, until about `over` bytes are freed (abso
58+ * Capacity). Evicting whole views dropped layouts just made along with old ones: compare 612 to 1224 on abso needs
59+ * both versions' layouts, about 25 MB, and evicting one whole after the call meant asking again laid it all out again.
60+ * A layout several views share goes from all of them.
61+ */
62+ #shed(over: number): void {
63+ // One entry per layout: a layout several views hold (a draft's view borrows its base's) is used as recently as
64+ // any of them used it, and goes from all of them at once, since only that frees it.
65+ const layouts = new Map<object, { used: number; bytes: number; holders: { view: View; path: string }[] }>();
66+ for (const view of this.#cache.values())
67+ for (const f of view.laidOut()) {
68+ const entry = layouts.get(f.layout);
69+ if (entry) {
70+ entry.used = Math.max(entry.used, f.used);
71+ entry.holders.push({ view, path: f.path });
72+ } else layouts.set(f.layout, { used: f.used, bytes: f.bytes, holders: [{ view, path: f.path }] });
73+ }
74+ let freed = 0;
75+ for (const entry of [...layouts.values()].sort((a, b) => a.used - b.used)) {
76+ if (freed >= over) break;
77+ for (const { view, path } of entry.holders) view.forget(path);
78+ freed += entry.bytes;
79+ }
80+ }
82+ /**
2383 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,
2484 * #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.
85+ * large generated file add up. At most VIEW_COUNT.
2686 */
2787 #trim(): void {
88+ // The newest views that fit, as evicting oldest first leaves them. A file layout two views share (a draft's view
89+ // takes its base's, View.shareFiles) counts once (abso Capacity): counted per view, they filled the budget twice
90+ // over, and a kept writer laid abso's drafts out again on every collisions call.
91+ const seen = new Set<object>();
2892 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();
93+ let kept = 0;
94+ let full = false;
95+ for (const [key, v] of [...this.#cache].reverse()) {
96+ if (!full) {
97+ const w = v.weight(seen);
98+ if (total + w <= this.#budget && kept < VIEW_COUNT) {
99+ total += w;
100+ kept++;
101+ continue;
102+ }
103+ full = true;
104+ }
33105 this.#cache.delete(key);
34106 }
35107 }
36108
37− /** A view of a set, cached by the set's exact contents. */
38− view(set: ReadonlySet<AtomId>): View {
109+ /** Indexed snapshots use identity; ordinary sets use their exact contents. */
110+ view(set: ReadonlySet<AtomId>): ReadView {
111+ this.assertUnkeyedAllowed();
112+ // Immutable indexed membership is already a stable identity. Building
113+ // a sorted history-sized key defeats its external-storage contract.
114+ if (set instanceof StableSet) return this.#get(set, () => set);
39115 const key = [...set]
40116 .map((id) => this.#graph.atom(id).seq)
41117 .sort((a, b) => a - b)
Lines 120–131 at 318 (were 44–50 at 116)
44120 }
45121
46122 /** A view cached under `key`, made from `set()` when it isn't there. */
47− keyed(key: string, set: () => ReadonlySet<AtomId>): View {
123+ keyed(key: string, set: () => ReadonlySet<AtomId>): ReadView {
124+ if (this.#provider) return this.#provider.keyed(key);
125+ return this.#get(key, set);
126+ }
128+ #get(key: string | StableSet<AtomId>, set: () => ReadonlySet<AtomId>): View {
48129 let view = this.#cache.get(key);
49130 if (view) {
50131 // Most recently used last, so eviction takes the oldest.
MCP
compare on this site's /mcp
CLI
abso diff 116 318 worker/project/views.ts --json
JSON
/christopher/abso-core/compare/116..318?file=worker%2Fproject%2Fviews.ts&format=json