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
show on this site's /mcp
CLI
abso show 249 --json
JSON
/christopher/abso-core/versions/249?format=json

Collisions step 3: each draft's definition work kept between calls, under a hard 4 MB budget (abso Capacity, issue 664).

of shared·capacity··Not checked

KeptParts (kept-parts.ts) holds what draft collisions work out about each draft and each pair of drafts: the names a draft's new lines use, the definitions a draft's changes touch before and after (the changed-definition rule), and the names two drafts both define in a file. Each entry is weighed as the view cache weighs views (line ids, texts, names: view.ts's stringBytes) and the whole stays under 4 MB, oldest out first; an entry over the budget isn't kept.

Keys name everything an entry depends on: shared's version, the draft's base and its atoms. Each collisions call drops every entry it didn't use, so a draft that changes, an agent that leaves or shared moving on drops theirs at once. On a stored writer only; the publish check (land.ts) and the in-memory project work as before.

On the abso export (my machine): a warm collisions call takes 322 ms instead of 3.4 s, with the same answer as replay. A full warm run's held heap is at most 42.9 MB (41.7 MB without it), and all 33 answers match.

Tests: kept-parts (new: the budget, oldest out first, an oversized entry never kept, a round dropping what it didn't use) and collisions-kept-views, which now runs through the kept parts on the writer (a draft changing, shared moving on, a draft dropped, each asked twice against the in-memory project). With the collision, definition, path and view tests: 47 of 47.

Version 248 · Files at version 249

6 files, 234 lines added, 22 removed.

--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -136,7 +136,7 @@
136136 * What V8 holds for a string of its own (abso Capacity, measured on abso's layouts): a header, then one byte a
137137 * character when every character fits in Latin-1 and two otherwise, rounded up to 8.
138138 */
139−function stringBytes(text: string): number {
139+export function stringBytes(text: string): number {
140140 const chars = /[^\u0000-\u00ff]/.test(text) ? 2 * text.length : text.length;
141141 return (STRING_HEADER_BYTES + chars + 7) & ~7;
142142}
--- a/worker/project/definition-rule.ts
+++ b/worker/project/definition-rule.ts
@@ -7,6 +7,7 @@
77import { type AtomId, type Graph, joinLines, type LineId, type ReadView } from "../../src/engine/index.ts";
88import { type Origin, touchedFiles } from "./collisions.ts";
99import { type Definition, definitionsIn, importsOf, usedNames } from "./definitions.ts";
10+import { definitionsBytes, type KeptParts, usesBytes } from "./kept-parts.ts";
1011import { codeOfLines, declaredIn, holdsNoCode } from "./names.ts";
1112import { definitionText } from "./news.ts";
1213import type { Collision } from "./project.ts";
@@ -109,6 +110,8 @@
109110 * in the text of the name's definitions (see definitions.ts), including one
110111 * that disappeared; a use that imports the name, or sits in a file that
111112 * defines it, only counts that definition. `nowView` may be a function, called only when the draft uses a name.
113+ * With `kept` (draft collisions, abso Capacity), the draft's uses and the newer work's definitions are kept between
114+ * calls under keys that name everything they depend on.
112115 */
113116 collisions(
114117 draft: ReadonlySet<AtomId>,
@@ -117,26 +120,21 @@
117120 nowView: ReadView | (() => ReadView),
118121 newer: ReadonlySet<AtomId>,
119122 spaceName: string,
123+ kept?: { parts: KeptParts; uses: string; changed: string },
120124 ): DefinitionCollision[] {
121125 if (draft.size === 0 || newer.size === 0) return [];
122− let kept = this.#usesCache.get(draft);
123− if (!kept || kept.view !== view || kept.base !== baseView) {
124− kept = { view, base: baseView, uses: this.#usesOf(draft, view, baseView) };
125− this.#usesCache.set(draft, kept);
126− }
127− const uses = kept.uses;
126+ const uses = kept
127+ ? kept.parts.get(`uses ${kept.uses}`, () => this.#usesOf(draft, view, baseView), usesBytes)
128+ : this.#usesFor(draft, view, baseView);
128129 if (uses.size === 0) return [];
129− const now = typeof nowView === "function" ? nowView() : nowView;
130−
131− // A definition can only have changed in a file the newer work touched.
132− let changed = this.#changedCache.get(newer);
133− if (!changed || changed.base !== baseView || changed.now !== now) {
134− const touched = touchedFiles(this.#graph, newer, [baseView, now]);
135− const defs = { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) };
136− changed = { base: baseView, now, defs };
137− this.#changedCache.set(newer, changed);
138− }
139− const { baseDefs, nowDefs } = changed.defs;
130+ const nowOf = () => (typeof nowView === "function" ? nowView() : nowView);
131+ const { baseDefs, nowDefs } = kept
132+ ? kept.parts.get(
133+ `changed ${kept.changed}`,
134+ () => this.#changed(newer, baseView, nowOf()),
135+ (c) => definitionsBytes(c.baseDefs) + definitionsBytes(c.nowDefs),
136+ )
137+ : this.#changedFor(newer, baseView, nowOf());
140138 const out: DefinitionCollision[] = [];
141139 for (const [name, where] of [...uses].sort(([a], [b]) => (a < b ? -1 : 1))) {
142140 // Which definitions the uses mean: the file each use imports the name
@@ -193,6 +191,32 @@
193191 return out;
194192 }
195193
194+ /** The draft's uses, kept while it is asked about with the same views. */
195+ #usesFor(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses {
196+ let kept = this.#usesCache.get(draft);
197+ if (!kept || kept.view !== view || kept.base !== baseView) {
198+ kept = { view, base: baseView, uses: this.#usesOf(draft, view, baseView) };
199+ this.#usesCache.set(draft, kept);
200+ }
201+ return kept.uses;
202+ }
204+ /** The newer work's definitions before and after, kept while it is asked about with the same views. */
205+ #changedFor(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed {
206+ let changed = this.#changedCache.get(newer);
207+ if (!changed || changed.base !== baseView || changed.now !== now) {
208+ changed = { base: baseView, now, defs: this.#changed(newer, baseView, now) };
209+ this.#changedCache.set(newer, changed);
210+ }
211+ return changed.defs;
212+ }
214+ /** Definitions in the files the newer work touched, before it and with it: only there can one have changed. */
215+ #changed(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed {
216+ const touched = touchedFiles(this.#graph, newer, [baseView, now]);
217+ return { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) };
218+ }
196220 /** The names the draft's lines use and don't declare, less those the lines they replaced already used. */
197221 #usesOf(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses {
198222 const uses = new Map<string, { file: string; index: number }[]>();
--- a/worker/project/draft-collisions.ts
+++ b/worker/project/draft-collisions.ts
@@ -6,6 +6,7 @@
66import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from "./collisions.ts";
77import type { DefinitionRule } from "./definition-rule.ts";
88import { type Definition, definedTwice } from "./definitions.ts";
9+import { definitionsBytes, type KeptParts } from "./kept-parts.ts";
910import { pathKey } from "./pathkey.ts";
1011import type { CollisionSide, DraftCollision } from "./project-types.ts";
1112import { last, SHARED, type Space } from "./spaces.ts";
@@ -28,6 +29,10 @@
2829 /** Shared now with these agents' drafts on top. */
2930 draftsView(agents: readonly Agent[]): ReadView;
3031 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;
32+ /** What collisions keep between calls (kept-parts.ts), when the project keeps any. */
33+ keptParts?(): KeptParts | undefined;
34+ /** A key naming everything a draft's part of the answer depends on: shared's version, its base and its atoms. */
35+ draftKey(agent: Agent): string;
3136}
3237
3338/**
@@ -99,6 +104,16 @@
99104 // Shared now with each agent's draft on top (project.ts, draftsView): kept between calls while shared and the
100105 // draft stay the same, and laid out as shared is for every file the draft doesn't touch (abso Capacity).
101106 const nowViewOf = (agent: Agent) => host.draftsView([agent]);
107+ // Each draft's uses and changed definitions, and each pair's names defined twice, kept between calls (kept-parts.ts,
108+ // abso Capacity): what this call doesn't use is dropped at its end.
109+ const parts = host.keptParts?.();
110+ const keys = new Map<Agent, string>();
111+ const keyOf = (agent: Agent) => {
112+ let key = keys.get(agent);
113+ if (key === undefined) keys.set(agent, (key = host.draftKey(agent)));
114+ return key;
115+ };
116+ parts?.startRound();
102117 // Each agent's touched files once, not once per pair (abso Capacity).
103118 const touched = new Map<Agent, Set<string>>();
104119 const touchedBy = (agent: Agent) => {
@@ -154,10 +169,15 @@
154169 const mineA = new Set(draftA);
155170 const mineB = new Set(draftB);
156171 const filesA = touchedBy(a);
172+ const twiceIn = (file: string): ReadonlyMap<string, Definition[]> => {
173+ const make = () => {
174+ both ??= host.draftsView([a, b]);
175+ return both.hasFile(file) ? definedTwice(both, file) : new Map<string, Definition[]>();
176+ };
177+ return parts ? parts.get(`twice ${keyOf(a)} | ${keyOf(b)} ${file}`, make, definitionsBytes) : make();
178+ };
157179 for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) {
158− both ??= host.draftsView([a, b]);
159− if (!both.hasFile(file)) continue;
160− for (const [name, defs] of definedTwice(both, file)) {
180+ for (const [name, defs] of twiceIn(file)) {
161181 const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!)));
162182 const dA = by(mineA);
163183 const dB = by(mineB);
@@ -177,7 +197,8 @@
177197 }
178198 // Definitions: b's new lines use a name whose definition a's changes change.
179199 const nowView = () => nowViewOf(a);
180− for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED)) {
200+ const kept = parts ? { parts, uses: keyOf(b), changed: keyOf(a) } : undefined;
201+ for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED, kept)) {
181202 const c = d.collision;
182203 out.push({
183204 kind: "definition",
@@ -189,6 +210,7 @@
189210 }
190211 }
191212 }
213+ parts?.endRound();
192214 const fileKey = options.file !== undefined ? pathKey(options.file) : undefined;
193215 const folderParts =
194216 options.folder !== undefined ? options.folder.split("/").filter((s) => s !== "" && s !== ".") : undefined;
--- /dev/null
+++ b/worker/project/kept-parts.ts
@@ -0,0 +1,102 @@
1+// What draft collisions work out about each draft and each pair of drafts, kept between calls under a hard byte budget
2+// (abso Capacity, issue 664): the names a draft's new lines use, the definitions a draft's changes touch before and
3+// after, and the names a pair of drafts both define in a file. Keys name everything an entry depends on (shared's
4+// version, a draft's base and atoms), and a call drops every entry it didn't use, so a draft that changes, an agent that
5+// leaves and shared moving on all drop theirs at once.
6+
7+import { stringBytes } from "../../src/engine/view.ts";
8+import type { Definition } from "./definitions.ts";
9+
10+/** At most this many bytes, weighed as the view cache weighs views (view.ts). */
11+export const KEPT_PARTS_BUDGET = 4_000_000;
13+const OBJECT_BYTES = 32;
14+const ARRAY_HEADER_BYTES = 24;
15+const MAP_HEADER_BYTES = 32;
16+const MAP_ENTRY_BYTES = 40;
17+const REFERENCE_BYTES = 8;
19+/** Entries by key, most recently used last, held under KEPT_PARTS_BUDGET bytes. */
20+export class KeptParts {
21+ readonly #budget: number;
22+ readonly #entries = new Map<string, { value: unknown; bytes: number }>();
23+ #bytes = 0;
24+ /** The keys used since the round began, when one has. */
25+ #used: Set<string> | undefined;
27+ constructor(budget = KEPT_PARTS_BUDGET) {
28+ this.#budget = budget;
29+ }
31+ get bytes(): number {
32+ return this.#bytes;
33+ }
35+ get size(): number {
36+ return this.#entries.size;
37+ }
39+ /** The value kept under `key`, or `make()`'s, kept when it fits: `weigh` says what it holds. */
40+ get<T>(key: string, make: () => T, weigh: (value: T) => number): T {
41+ this.#used?.add(key);
42+ const kept = this.#entries.get(key);
43+ if (kept) {
44+ this.#entries.delete(key);
45+ this.#entries.set(key, kept);
46+ return kept.value as T;
47+ }
48+ const value = make();
49+ const bytes = MAP_ENTRY_BYTES + stringBytes(key) + weigh(value);
50+ if (bytes > this.#budget) return value;
51+ this.#entries.set(key, { value, bytes });
52+ this.#bytes += bytes;
53+ for (const [old, entry] of this.#entries) {
54+ if (this.#bytes <= this.#budget) break;
55+ this.#entries.delete(old);
56+ this.#bytes -= entry.bytes;
57+ }
58+ return value;
59+ }
61+ /** Start counting which entries are used; endRound drops the rest. */
62+ startRound(): void {
63+ this.#used = new Set();
64+ }
66+ /** Drop every entry not used since startRound: what changed, left or moved on since. */
67+ endRound(): void {
68+ const used = this.#used;
69+ this.#used = undefined;
70+ if (!used) return;
71+ for (const [key, entry] of this.#entries) {
72+ if (used.has(key)) continue;
73+ this.#entries.delete(key);
74+ this.#bytes -= entry.bytes;
75+ }
76+ }
77+}
79+/** A definition's bytes: its record, name, text, and its lines' ids (its file's path is shared with the view's). */
80+export function definitionBytes(d: Definition): number {
81+ let n = OBJECT_BYTES + REFERENCE_BYTES + stringBytes(d.name) + stringBytes(d.text) + ARRAY_HEADER_BYTES;
82+ for (const line of d.lines) n += REFERENCE_BYTES + stringBytes(line);
83+ return n;
84+}
86+/** Definitions by name. */
87+export function definitionsBytes(defs: ReadonlyMap<string, readonly Definition[]>): number {
88+ let n = MAP_HEADER_BYTES;
89+ for (const [name, list] of defs) {
90+ n += MAP_ENTRY_BYTES + stringBytes(name) + ARRAY_HEADER_BYTES;
91+ for (const d of list) n += REFERENCE_BYTES + definitionBytes(d);
92+ }
93+ return n;
94+}
96+/** Names with where they're used: a file and a line index for each. */
97+export function usesBytes(uses: ReadonlyMap<string, readonly { file: string; index: number }[]>): number {
98+ let n = MAP_HEADER_BYTES;
99+ for (const [name, where] of uses)
100+ n += MAP_ENTRY_BYTES + stringBytes(name) + ARRAY_HEADER_BYTES + where.length * (REFERENCE_BYTES + OBJECT_BYTES);
101+ return n;
102+}
--- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -76,6 +76,7 @@
7676} from "./history.ts";
7777import { ingest as hostedIngest, type IngestHost } from "./ingest.ts";
7878import { init as hostedInit, type InitHost } from "./init.ts";
79+import { KeptParts } from "./kept-parts.ts";
7980import { label as hostedLabel, type LabelHost } from "./label.ts";
8081import {
8182 land as hostedLand,
@@ -276,6 +277,8 @@
276277 readonly #graph: Graph;
277278 /** Views laid out from atom sets, cached under a byte budget (views.ts). */
278279 readonly #views: ViewCache;
280+ /** What draft collisions keep between calls, under its own byte budget (kept-parts.ts). */
281+ #keptParts: KeptParts | undefined;
279282 /** Each agent's draft files, under the key they were worked out for (#draftFiles). */
280283 readonly #draftFilesKept = new Map<string, { key: string; files: Set<string> }>();
281284 /** Each version's files and each path's history (pathindex.ts). */
@@ -1902,6 +1905,12 @@
19021905 agentView: (agent) => this.#agentView(agent as AgentState),
19031906 draftsView: (agents) => this.#draftsView(agents as readonly AgentState[]),
19041907 touchedFiles: (ids, views) => this.#touchedFiles(ids, views),
1908+ // On a stored writer only, as a reader kept between calls is (abso Capacity).
1909+ keptParts: () => (this.#writerStorage ? (this.#keptParts ??= new KeptParts()) : undefined),
1910+ draftKey: (agent) => {
1911+ const a = agent as AgentState;
1912+ return `${last(this.#shared)} ${a.space}@${a.base} ${this.#draft(a).join(",")}`;
1913+ },
19051914 };
19061915 return this.#draftCollisionHost;
19071916 }
--- /dev/null
+++ b/worker/project/test/kept-parts.test.ts
@@ -0,0 +1,55 @@
1+// What draft collisions keep between calls (abso Capacity, kept-parts.ts): never more than its byte budget, nothing
2+// bigger than the budget kept at all, and a round drops every entry it didn't use.
3+
4+import assert from "node:assert/strict";
5+import { test } from "node:test";
6+import { KEPT_PARTS_BUDGET, KeptParts } from "../kept-parts.ts";
7+
8+test("kept parts stay under the budget, oldest out first, and an entry over the budget isn't kept", () => {
9+ const parts = new KeptParts(1_000);
10+ let made = 0;
11+ const get = (key: string, bytes: number) =>
12+ parts.get(
13+ key,
14+ () => ++made,
15+ () => bytes,
16+ );
17+ assert.equal(get("a", 300), 1);
18+ assert.equal(get("b", 300), 2);
19+ assert.equal(get("a", 300), 1, "kept");
20+ // Each entry weighs its key and its slot too (364 bytes here), so a third one is over: b, the oldest, goes.
21+ assert.equal(get("c", 300), 3);
22+ assert.ok(parts.bytes <= 1_000, `${parts.bytes} bytes`);
23+ assert.equal(get("a", 300), 1, "a was used after b, so it stayed");
24+ assert.equal(get("c", 300), 3);
25+ assert.equal(get("b", 300), 4, "b went");
26+ assert.equal(get("big", 2_000), 5);
27+ assert.equal(get("big", 2_000), 6, "never kept");
28+ assert.ok(parts.bytes <= 1_000);
29+ assert.equal(KEPT_PARTS_BUDGET, 4_000_000);
30+});
32+test("a round drops what it didn't use: a draft that changed, an agent that left, shared moved on", () => {
33+ const parts = new KeptParts();
34+ let made = 0;
35+ const get = (key: string) =>
36+ parts.get(
37+ key,
38+ () => ++made,
39+ () => 10,
40+ );
41+ parts.startRound();
42+ get("uses 7 ann");
43+ get("uses 7 bob");
44+ parts.endRound();
45+ assert.equal(parts.size, 2);
46+ parts.startRound();
47+ assert.equal(get("uses 7 ann"), 1, "kept across rounds");
48+ get("uses 7 bob+1");
49+ parts.endRound();
50+ assert.equal(parts.size, 2, "bob's old draft dropped");
51+ parts.startRound();
52+ parts.endRound();
53+ assert.equal(parts.size, 0);
54+ assert.equal(parts.bytes, 0);
55+});