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 276 --json
JSON
/christopher/abso-core/versions/276?format=json

A stored graph keeps files' decoded line histories between layouts (abso Capacity, issue 664): Graph.packedHistory, under 4 MB.

of shared·capacity··Not checked

Every view of a file read its whole history from pages again: compare lays each file out once per side, collisions once per draft touching it, and a file whose lines moved in from another reads that one's history too (abso's worker files, split out of index.ts). The history (lines, deletes and crossings, no texts) is decoded once into flat 32-bit arrays, kept under its three lists' lengths, most recently used last, and add() lets every kept history go. abso's index.ts, 46,000 lines of history, takes about 1 MB.

Side by side on abso's export, same machine, two rounds, against 273: cold compare 612 to 1224 from 12.4 s to 5.0-6.5 s, cold collisions from 10.4 s to 5.0-6.9 s, cold where from 4.4 s to 1.5-2.6 s.

Tests: packed-files.test.ts adds a test that a kept history saves reads, add() lets it go, and the next layout matches the in-memory graph. tsc and Biome clean; view, state-cache, state-source, graph, graph-long-text, graph-line-chunks, graph-move-write-membership, graph-replacement-membership, graph-insertion-storage, packed-files, definition-code, definitions, changed-definition, collisions-kept-views, draft-reads-stored and collision-streaming pass.

Version 275 · Files at version 276

3 files, 258 lines added, 30 removed.

--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -266,6 +266,36 @@
266266export type PackedAttr = [number, string, string | null];
267267/** A run written in another file under one of a file's lines: [the parent's n, the run's file index]. */
268268export type PackedCrossing = [number, number];
270+/**
271+ * A file's packed history decoded, without texts (Graph.packedHistory): PackedLine's five numbers per line, then
272+ * PackedDelete's two per delete and PackedCrossing's two per crossing, each list flat.
273+ */
274+export interface PackedHistory {
275+ readonly lines: ArrayLike<number>;
276+ readonly deletes: ArrayLike<number>;
277+ readonly crossings: ArrayLike<number>;
278+}
280+/**
281+ * Bytes of decoded file histories a stored graph keeps between layouts (abso Capacity). Every view of a file read its
282+ * whole history from pages: compare lays each file out once per side, collisions once per draft touching it, and a
283+ * file whose lines moved in from another read that one's history too (abso's worker files, split out of index.ts).
284+ * abso's index.ts, 46,000 lines of history, takes about 1 MB.
285+ */
286+export const PACKED_HISTORY_BUDGET = 4_000_000;
288+/** Numbers packed flat: 4 bytes each while they fit in 32 bits, else 8. */
289+function packNumbers(rows: Iterable<readonly number[]>): Int32Array | Float64Array {
290+ const flat: number[] = [];
291+ let small = true;
292+ for (const row of rows)
293+ for (const value of row) {
294+ if ((value | 0) !== value) small = false;
295+ flat.push(value);
296+ }
297+ return small ? Int32Array.from(flat) : Float64Array.from(flat);
298+}
269299const EMPTY_NUMBERS: readonly number[] = Object.freeze([]);
270300/** At most this many path names kept in memory (Graph.#pathName). */
271301const PATH_NAMES = 4096;
@@ -440,7 +470,11 @@
440470 if (encoded.insertionFormat === 1) {
441471 const collection = `atom-inserts:${atom.seq}`;
442472 inserts = readonlyStoredArray(storage, collection, (raw, index) => {
443− const run = raw as { file: string; parent: string; lines: string };
473+ const run = raw as {
474+ file: string;
475+ parent: string;
476+ lines: string;
477+ };
444478 const lines = `${collection}:lines:${index}`;
445479 if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string")
446480 throw Error("invalid stored insertion run");
@@ -476,7 +510,11 @@
476510 const collection = `atom-inserts:${atom.seq}`;
477511 if (encoded.insertionRuns !== collection) throw Error("invalid stored insertion reference");
478512 const inserts = readonlyStoredArray<InsertRun>(storage, collection, (raw, index) => {
479− const run = raw as { file: string; parent: InsertRun["parent"]; lines: string };
513+ const run = raw as {
514+ file: string;
515+ parent: InsertRun["parent"];
516+ lines: string;
517+ };
480518 const lines = `${collection}:lines:${index}`;
481519 if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string")
482520 throw Error("invalid stored insertion run");
@@ -493,7 +531,10 @@
493531 }),
494532 });
495533 });
496− Object.defineProperty(result, "inserts", { value: inserts, enumerable: true });
534+ Object.defineProperty(result, "inserts", {
535+ value: inserts,
536+ enumerable: true,
537+ });
497538 }
498539 for (const field of listFields) {
499540 const collection = encoded.lists?.[field];
@@ -504,7 +545,10 @@
504545 encoded.listFormat === 2
505546 ? readonlyPagedArray(storage, collection)
506547 : readonlyStoredArray(storage, collection);
507− Object.defineProperty(result, field, { value: list, enumerable: true });
548+ Object.defineProperty(result, field, {
549+ value: list,
550+ enumerable: true,
551+ });
508552 }
509553 return streamed(Object.freeze(result));
510554 },
@@ -542,7 +586,11 @@
542586 lines.push({ text });
543587 } else lines.push(line);
544588 }
545− runs.push({ file: run.file, parent: run.parent, lines: lineCollection });
589+ runs.push({
590+ file: run.file,
591+ parent: run.parent,
592+ lines: lineCollection,
593+ });
546594 }
547595 return {
548596 insertionFormat: 1,
@@ -588,7 +636,10 @@
588636 "line-text",
589637 (value) => {
590638 if (typeof value === "string") return value;
591− const reference = value as unknown as { format: number; text: string };
639+ const reference = value as unknown as {
640+ format: number;
641+ text: string;
642+ };
592643 if (reference?.format !== 1 || typeof reference.text !== "string") throw Error("invalid long line reference");
593644 return longText.read(reference.text);
594645 },
@@ -708,6 +759,53 @@
708759 yield* this.#fileCrossings!.values(String(file));
709760 }
710761
762+ /** Decoded file histories by path index, most recently used last, under PACKED_HISTORY_BUDGET bytes. */
763+ readonly #histories = new Map<number, { counts: string; history: PackedHistory; bytes: number }>();
764+ #historyBytes = 0;
766+ /**
767+ * A file's line, delete and crossing history, decoded once and kept while it fits (abso Capacity). Kept under its
768+ * three lists' lengths: the lists only grow, and add() lets every kept history go, so a kept one is always whole.
769+ */
770+ packedHistory(file: number): PackedHistory {
771+ const key = String(file);
772+ const counts = `${this.#fileLines!.length(key)} ${this.#fileDeletes!.length(key)} ${this.#fileCrossings!.length(key)}`;
773+ const kept = this.#histories.get(file);
774+ if (kept) {
775+ this.#histories.delete(file);
776+ if (kept.counts === counts) {
777+ this.#histories.set(file, kept);
778+ return kept.history;
779+ }
780+ this.#historyBytes -= kept.bytes;
781+ }
782+ const history: PackedHistory = {
783+ lines: packNumbers(this.#fileLines!.values(key)),
784+ deletes: packNumbers(this.#fileDeletes!.values(key)),
785+ crossings: packNumbers(this.#fileCrossings!.values(key)),
786+ };
787+ const bytes =
788+ (history.lines as Int32Array).byteLength +
789+ (history.deletes as Int32Array).byteLength +
790+ (history.crossings as Int32Array).byteLength +
791+ 200;
792+ if (bytes <= PACKED_HISTORY_BUDGET) {
793+ this.#histories.set(file, { counts, history, bytes });
794+ this.#historyBytes += bytes;
795+ for (const [old, entry] of this.#histories) {
796+ if (this.#historyBytes <= PACKED_HISTORY_BUDGET) break;
797+ this.#histories.delete(old);
798+ this.#historyBytes -= entry.bytes;
799+ }
800+ }
801+ return history;
802+ }
804+ /** Bytes of decoded file histories kept (packedHistory). */
805+ get historyBytes(): number {
806+ return this.#historyBytes;
807+ }
711809 /** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */
712810 #lists: ((seq: number) => AtomLists) | undefined;
713811 /** Recently read atoms' lists, most recent last, up to LIST_CACHE atoms. */
@@ -1471,6 +1569,9 @@
14711569 * The atom is frozen. Adding to the graph does not add it to any set.
14721570 */
14731571 add(atom: Atom): void {
1572+ // A new atom appends to its files' histories; a failed one may leave a kept history longer than the lists.
1573+ this.#histories.clear();
1574+ this.#historyBytes = 0;
14741575 if (this.#storage)
14751576 this.#storage.transaction(() => {
14761577 this.#add(atom);
@@ -1484,7 +1585,10 @@
14841585 #validateAcceptedAtom(atom: Atom, scratch?: GraphStateStorage): void {
14851586 const membership = <T extends string>(name: string): { has(key: T): boolean; add(key: T): unknown } => {
14861587 if (!scratch) return new Set<T>();
1487− return { has: (key) => scratch.read(name, key) === true, add: (key) => scratch.write(name, key, true) };
1588+ return {
1589+ has: (key) => scratch.read(name, key) === true,
1590+ add: (key) => scratch.write(name, key, true),
1591+ };
14881592 };
14891593 validateShape(atom);
14901594 const seq = this.#atoms.length;
@@ -1698,7 +1802,12 @@
16981802 }
16991803 }
17001804 (frozen.moves ?? []).forEach((m, i) => {
1701− const info: RelocationInfo = Object.freeze({ atom: frozen.id, line: m.line, parent: m.parent, k: i });
1805+ const info: RelocationInfo = Object.freeze({
1806+ atom: frozen.id,
1807+ line: m.line,
1808+ parent: m.parent,
1809+ k: i,
1810+ });
17021811 if (this.#relocationPages) this.#relocationPages.append(m.line, info);
17031812 else pushIndex(this.#relocations, m.line, info);
17041813 if (this.#relocatedToPages) this.#relocatedToPages.append(m.parent, info);
@@ -1846,7 +1955,12 @@
18461955 // JSON -------------------------------------------------------------------
18471956
18481957 toJSON(): GraphJSON {
1849− return { format: GRAPH_FORMAT, version: 1, atoms: this.#atoms, labels: this.labels() };
1958+ return {
1959+ format: GRAPH_FORMAT,
1960+ version: 1,
1961+ atoms: this.#atoms,
1962+ labels: this.labels(),
1963+ };
18501964 }
18511965
18521966 /** Load a saved graph. Every atom is re-validated as it is added. */
@@ -1946,7 +2060,12 @@
19462060 // With text kept elsewhere (#46), a run's lines are read when asked for.
19472061 let k = 0;
19482062 const run = (r: InsertRun): InsertRun => {
1949− if (!text) return Object.freeze({ file: r.file, parent: r.parent, lines: Object.freeze([...r.lines]) });
2063+ if (!text)
2064+ return Object.freeze({
2065+ file: r.file,
2066+ parent: r.parent,
2067+ lines: Object.freeze([...r.lines]),
2068+ });
19502069 const start = k;
19512070 const length = r.lines.length;
19522071 k += length;
@@ -1961,16 +2080,25 @@
19612080 inserts: Object.freeze(atom.inserts.map(run)),
19622081 ...(lists
19632082 ? {}
1964− : { deletes: list(atom.deletes), writes: list(atom.writes), reads: list(atom.reads), holds: list(atom.holds) }),
2083+ : {
2084+ deletes: list(atom.deletes),
2085+ writes: list(atom.writes),
2086+ reads: list(atom.reads),
2087+ holds: list(atom.holds),
2088+ }),
19652089 needs: list(atom.needs, canon),
19662090 resolves: list(atom.resolves, canon),
19672091 reasons: list(atom.reasons, (r) => Object.freeze({ to: canon(r.to), kind: r.kind, text: r.text })),
19682092 deps: list(atom.deps, canon),
19692093 ...(atom.attrs && atom.attrs.length > 0
1970− ? { attrs: Object.freeze(atom.attrs.map((a) => Object.freeze({ file: a.file, name: a.name, value: a.value }))) }
2094+ ? {
2095+ attrs: Object.freeze(atom.attrs.map((a) => Object.freeze({ file: a.file, name: a.name, value: a.value }))),
2096+ }
19712097 : {}),
19722098 ...(atom.moves
1973− ? { moves: Object.freeze(atom.moves.map((m) => Object.freeze({ line: m.line, parent: m.parent }))) }
2099+ ? {
2100+ moves: Object.freeze(atom.moves.map((m) => Object.freeze({ line: m.line, parent: m.parent }))),
2101+ }
19742102 : {}),
19752103 } as Atom;
19762104 return Object.freeze(lists ? (new ListedAtom(copy, lists) as unknown as Atom) : copy);
--- a/src/engine/test/packed-files.test.ts
+++ b/src/engine/test/packed-files.test.ts
@@ -54,3 +54,31 @@
5454 storage.db.close();
5555 }
5656});
58+test("a file's decoded history is kept between views and let go when an atom is added", () => {
59+ const oracle = Repo.init({ "a.txt": lines("one", "two", "three"), "b.txt": lines("bee") });
60+ const storage = new SqlState();
61+ try {
62+ const graph = new Graph({ storage });
63+ for (const atom of oracle.graph.atoms()) graph.add(atom);
64+ const first = new View(graph, new StableOverlaySet(oracle.shared));
65+ assert.equal(first.text("a.txt"), "one\ntwo\nthree\n");
66+ assert.ok(graph.historyBytes > 0, "the first layout keeps a.txt's history");
67+ // Another view of the same file reads only what its membership needs, not the history's pages again.
68+ const before = storage.reads;
69+ assert.equal(new View(graph, new StableOverlaySet(oracle.shared)).sequence("a.txt").length, 3);
70+ const pages = storage.reads - before;
71+ const cold = new Graph({ storage });
72+ const coldBefore = storage.reads;
73+ new View(cold, new StableOverlaySet(oracle.shared)).sequence("a.txt");
74+ assert.ok(pages < storage.reads - coldBefore, "the kept history saved reads");
75+ // A new atom lets every kept history go; the next layout sees its lines.
76+ oracle.edit({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] });
77+ graph.add(oracle.graph.atom([...oracle.shared].at(-1)!));
78+ assert.equal(graph.historyBytes, 0);
79+ const after = new View(graph, new StableOverlaySet(oracle.shared));
80+ assert.equal(after.text("a.txt"), new View(oracle.graph, oracle.shared).text("a.txt"));
81+ } finally {
82+ storage.db.close();
83+ }
84+});
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -64,7 +64,12 @@
6464 sameText(path: string, other: ReadView): boolean;
6565};
6666
67−const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
67+const NO_ATTRS: FileAttrs = {
68+ exists: false,
69+ exec: false,
70+ link: null,
71+ blob: null,
72+};
6873
6974/**
7075 * A file layout's bytes, less its lookup indexes, worked out once per layout (abso Capacity): a layout never changes
@@ -123,7 +128,13 @@
123128}
124129
125130const EMPTY_INTS = new Int32Array(0);
126−const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
131+const NO_FILE: FileState = {
132+ attrs: NO_ATTRS,
133+ complete: true,
134+ seq: EMPTY_INTS,
135+ vis: EMPTY_INTS,
136+ deletedBy: new Map(),
137+};
127138
128139/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */
129140let fileUses = 0;
@@ -173,7 +184,10 @@
173184 readonly #graph: Graph;
174185 #seen = 0;
175186 /** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */
176− readonly movers: { readonly seq: number; readonly lines: readonly number[] }[] = [];
187+ readonly movers: {
188+ readonly seq: number;
189+ readonly lines: readonly number[];
190+ }[] = [];
177191 /** Every relocation of each moved line, by the line's number, in seq order. */
178192 readonly byLine = new Map<number, MoveEntry[]>();
179193 readonly #climbed = new Map<number, number>();
@@ -197,7 +211,12 @@
197211 const lines: number[] = [];
198212 graph.atom(id).moves!.forEach((m, k) => {
199213 const n = graph.lineN(m.line);
200− const r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k });
214+ const r: RelocationInfo = Object.freeze({
215+ atom: id,
216+ line: m.line,
217+ parent: m.parent,
218+ k,
219+ });
201220 const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! };
202221 const list = this.byLine.get(n);
203222 if (list) list.push(entry);
@@ -525,7 +544,10 @@
525544 if (this.#relocsN) return this.#relocsN;
526545 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();
527546 for (const [line, r] of this.#relocations())
528− out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
547+ out.set(this.graph.lineN(line), {
548+ r,
549+ parent: this.graph.codeOf(r.parent)!,
550+ });
529551 this.#relocsN = out;
530552 return out;
531553 }
@@ -627,7 +649,13 @@
627649 for (const n of graph.eachChildN(code)) {
628650 const seq = graph.writerSeqOf(n);
629651 if (!this.#has(seq) || relocs?.has(n)) continue;
630− out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
652+ out.push({
653+ n,
654+ replaces: graph.replacesN(n) ? 1 : 0,
655+ lamport: graph.lamportOfSeq(seq),
656+ seq,
657+ k: graph.kOf(n),
658+ });
631659 }
632660 if (relocs) {
633661 // The lines this set's moves put under each node, from the moves it already resolved: no row read per node
@@ -643,7 +671,13 @@
643671 for (const n of this.#movedInto.get(code) ?? NO_LINES) {
644672 const r = relocs.get(n)!.r;
645673 const seq = graph.atom(r.atom).seq;
646− out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
674+ out.push({
675+ n,
676+ replaces: 0,
677+ lamport: graph.lamportOfSeq(seq),
678+ seq,
679+ k: r.k,
680+ });
647681 }
648682 }
649683 // A line's replacements (runs of atoms that deleted it) first, then the
@@ -751,7 +785,14 @@
751785 const graph = this.graph;
752786 const attrs = this.#attributes(path);
753787 const start = graph.startCodeOf(path);
754− if (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
788+ if (start === undefined)
789+ return {
790+ attrs,
791+ complete,
792+ seq: EMPTY_INTS,
793+ vis: EMPTY_INTS,
794+ deletedBy: new Map(),
795+ };
755796 const relocs = this.#hasMoves ? this.#relocationsN() : undefined;
756797 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]);
757798 const children = new Map<number, Child[]>();
@@ -774,16 +815,28 @@
774815 return lamport;
775816 };
776817 for (const file of files) {
777− let place = -1;
778− for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {
779− place++;
818+ // The file's history decoded once and kept by the graph (abso Capacity): each view of it read every page again.
819+ const history = graph.packedHistory(file);
820+ const lines = history.lines;
821+ for (let i = 0, place = 0; i < lines.length; i += 5, place++) {
822+ const n = lines[i]!;
823+ const parent = lines[i + 1]!;
824+ const seq = lines[i + 2]!;
825+ const k = lines[i + 3]!;
826+ const replaces = lines[i + 4]!;
780827 if (!this.#has(seq)) continue;
781828 const moved = relocs?.get(n);
782829 let at = parent;
783830 let child: Child = { n, replaces, lamport: lamportOf(seq), seq, k };
784831 if (moved) {
785832 const mover = graph.atom(moved.r.atom).seq;
786− child = { n, replaces: 0, lamport: lamportOf(mover), seq: mover, k: moved.r.k };
833+ child = {
834+ n,
835+ replaces: 0,
836+ lamport: lamportOf(mover),
837+ seq: mover,
838+ k: moved.r.k,
839+ };
787840 at = moved.parent;
788841 }
789842 const list = children.get(at);
@@ -794,14 +847,18 @@
794847 writers.set(n, seq);
795848 places.set(n, k);
796849 }
797− for (const [n, deleter] of graph.packedDeletes(file)) {
850+ const dels = history.deletes;
851+ for (let at = 0; at < dels.length; at += 2) {
852+ const n = dels[at]!;
853+ const deleter = dels[at + 1]!;
798854 if (!this.#has(deleter)) continue;
799855 const by = deleters.get(n);
800856 if (by) by.push(deleter);
801857 else deleters.set(n, [deleter]);
802858 }
803859 // Runs written elsewhere under this file's lines follow them; the loop reads their files too.
804− for (const [parent, into] of graph.packedCrossings(file)) if (writers.has(parent)) files.add(into);
860+ const crossings = history.crossings;
861+ for (let at = 0; at < crossings.length; at += 2) if (writers.has(crossings[at]!)) files.add(crossings[at + 1]!);
805862 }
806863 const order = (a: Child, b: Child) =>
807864 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k;
@@ -925,7 +982,13 @@
925982 if (complete) seq.push(n);
926983 }
927984
928− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
985+ return {
986+ attrs,
987+ complete,
988+ seq: Int32Array.from(seq),
989+ vis: Int32Array.from(vis),
990+ deletedBy,
991+ };
929992 }
930993
931994 get #known(): string[] {
@@ -1039,7 +1102,11 @@
10391102 *visibleLines(path: string): IterableIterator<VisibleLine> {
10401103 const graph = this.graph;
10411104 for (const n of this.#file(path).vis)
1042− yield { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
1105+ yield {
1106+ id: graph.lineIdOf(n),
1107+ text: graph.textOf(n),
1108+ atom: graph.idOfSeq(graph.writerSeqOf(n)),
1109+ };
10431110 }
10441111
10451112 /** Atom-building line handles; text is loaded only when requested. */
@@ -1258,7 +1325,12 @@
12581325 * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a
12591326 * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity).
12601327 */
1261− *laidOut(): IterableIterator<{ path: string; layout: object; used: number; bytes: number }> {
1328+ *laidOut(): IterableIterator<{
1329+ path: string;
1330+ layout: object;
1331+ used: number;
1332+ bytes: number;
1333+ }> {
12621334 for (const [path, f] of this.#files) {
12631335 if (f === NO_FILE) continue;
12641336 let bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f);