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

Test deterministic replay and account view cache weight

of shared·codex-capacity-policy··Not checked

Version 102 · Files at version 103

3 files, 251 lines added, 13 removed.

--- /dev/null
+++ b/src/engine/test/record-query-contract.test.ts
@@ -0,0 +1,127 @@
1+// A small live graph and a graph rebuilt from its serialized history must
2+// answer the same one-file record query.
3+
4+import assert from "node:assert/strict";
5+import { describe, it } from "node:test";
6+import { buildAtom, Graph, View } from "../index.ts";
7+import { lines, Repo } from "./helpers.ts";
8+
9+describe("record-query replay contract", () => {
10+ it("matches a one-file record query after repeated writers, readers, and delete/recreate history", () => {
11+ const repo = Repo.init({ "src.txt": lines("top", "bottom"), "recreated.txt": lines("old") });
12+ const base = ["a0"];
13+ const insertAtTopGap = (text: string) =>
14+ repo.edit({ edits: [{ file: "src.txt", start: 1, end: 1, lines: [text], mode: "after" }] }, base);
15+ const writerOne = insertAtTopGap("writer one");
16+ const writerTwo = insertAtTopGap("writer two");
17+ const readerOne = insertAtTopGap("reader one");
18+ const readerTwo = insertAtTopGap("reader two");
19+ const attrOne = repo.edit({ attrs: [{ file: "src.txt", name: "exec", value: "1" }] }, base);
20+ const attrTwo = repo.edit({ attrs: [{ file: "src.txt", name: "exec", value: null }] }, base);
21+ const deletion = repo.edit({ edits: [{ file: "recreated.txt", old: lines("old"), new: "" }] });
22+ const recreation = repo.edit({ edits: [{ file: "recreated.txt", old: "", new: lines("fresh") }] });
24+ // Nine existing atoms make a finite, repeatable history. The last query
25+ // both replaces a line and inserts at the repeatedly written gap.
26+ assert.equal(repo.graph.size, 9);
27+ assert.deepEqual(recreation.deps, ["a0", deletion.id]);
28+ assert.deepEqual(repo.view().attrs("src.txt"), {
29+ exists: false,
30+ exec: false,
31+ link: null,
32+ blob: null,
33+ });
35+ const input = {
36+ author: "record-query-test",
37+ message: "record one file",
38+ time: 100,
39+ edits: [
40+ { file: "src.txt", start: 0, end: 1, lines: ["TOP"] },
41+ { file: "src.txt", start: 1, end: 1, lines: ["recorded"] },
42+ ],
43+ attrs: [{ file: "src.txt", name: "exec" as const, value: "1" }],
44+ };
45+ const liveAtom = buildAtom(repo.view(), input);
46+ const expectedDeps = ["a0", writerOne.id, writerTwo.id, readerOne.id, readerTwo.id, attrOne.id, attrTwo.id];
47+ assert.deepEqual(liveAtom.deps, expectedDeps);
48+ assert.equal(liveAtom.deps.length, 7);
49+ assert.equal(liveAtom.writes.length, 3);
50+ assert.equal(liveAtom.reads.length, 2);
51+ assert.equal(liveAtom.holds.length, 0);
52+ assert.equal(liveAtom.seq, 9);
53+ assert.deepEqual(liveAtom.deletes, ["a0.1"]);
54+ assert.deepEqual(liveAtom.inserts, [
55+ { file: "src.txt", parent: "a0.1", lines: ["TOP"] },
56+ { file: "src.txt", parent: "a0.1", lines: ["recorded"] },
57+ ]);
58+ assert.deepEqual(liveAtom.attrs, [{ file: "src.txt", name: "exec", value: "1" }]);
60+ const coldGraph = Graph.fromJSON(JSON.parse(JSON.stringify(repo.graph.toJSON())));
61+ const coldSet = new Set(repo.shared);
62+ const coldAtom = buildAtom(new View(coldGraph, coldSet), input);
63+ assert.deepEqual(coldAtom, liveAtom);
64+ assert.equal(JSON.stringify(coldAtom), JSON.stringify(liveAtom));
65+ const perturbedInput = {
66+ ...input,
67+ edits: input.edits.map((edit, index) => (index === 1 ? { ...edit, lines: ["different"] } : edit)),
68+ };
69+ const perturbedAtom = buildAtom(new View(coldGraph, coldSet), perturbedInput);
70+ assert.notDeepEqual(perturbedAtom, liveAtom);
71+ assert.notEqual(JSON.stringify(perturbedAtom), JSON.stringify(liveAtom));
73+ repo.graph.add(liveAtom);
74+ repo.shared.add(liveAtom.id);
75+ coldGraph.add(coldAtom);
76+ coldSet.add(coldAtom.id);
77+ const liveView = new View(repo.graph, repo.shared);
78+ const coldView = new View(coldGraph, coldSet);
79+ const visible = (view: View, path: string) => view.lines(path).map((line) => [line.id, line.text, line.atom]);
80+ assert.deepEqual(visible(liveView, "src.txt"), visible(coldView, "src.txt"));
81+ assert.deepEqual(
82+ visible(liveView, "src.txt").map((line) => line[1]),
83+ ["TOP", "recorded", "reader two", "reader one", "writer two", "writer one", "bottom"],
84+ );
85+ assert.deepEqual(liveView.attrs("src.txt"), coldView.attrs("src.txt"));
86+ assert.equal(liveView.attrs("src.txt").exec, true);
87+ assert.deepEqual(visible(liveView, "recreated.txt"), visible(coldView, "recreated.txt"));
88+ assert.deepEqual(
89+ visible(liveView, "recreated.txt").map((line) => line[1]),
90+ ["fresh"],
91+ );
92+ assert.deepEqual(
93+ liveView.sequence("recreated.txt").map((line) => [line.id, line.text, line.visible, line.deletedBy]),
94+ [
95+ [`${recreation.id}.0`, "fresh", true, []],
96+ ["a0.0", "old", false, [deletion.id]],
97+ ],
98+ );
100+ const recreatedFileInput = {
101+ author: "record-query-test",
102+ message: "record the recreated file",
103+ time: 101,
104+ edits: [{ file: "recreated.txt", old: lines("fresh"), new: lines("FRESH") }],
105+ };
106+ const liveRecreatedFileAtom = buildAtom(liveView, recreatedFileInput);
107+ const coldRecreatedFileAtom = buildAtom(coldView, recreatedFileInput);
108+ assert.deepEqual(liveRecreatedFileAtom.deps, [recreation.id]);
109+ assert.deepEqual(coldRecreatedFileAtom, liveRecreatedFileAtom);
110+ assert.equal(JSON.stringify(coldRecreatedFileAtom), JSON.stringify(liveRecreatedFileAtom));
111+ repo.graph.add(liveRecreatedFileAtom);
112+ repo.shared.add(liveRecreatedFileAtom.id);
113+ coldGraph.add(coldRecreatedFileAtom);
114+ coldSet.add(coldRecreatedFileAtom.id);
115+ assert.deepEqual(
116+ visible(new View(repo.graph, repo.shared), "recreated.txt"),
117+ visible(new View(coldGraph, coldSet), "recreated.txt"),
118+ );
119+ assert.deepEqual(
120+ visible(new View(repo.graph, repo.shared), "recreated.txt").map((line) => line[1]),
121+ ["FRESH"],
122+ );
123+ assert.equal(repo.graph.size, 11);
124+ assert.equal(coldGraph.size, 11);
125+ assert.equal(JSON.stringify(coldGraph.toJSON()), JSON.stringify(repo.graph.toJSON()));
126+ });
127+});
--- a/src/engine/test/view.test.ts
+++ b/src/engine/test/view.test.ts
@@ -91,4 +91,82 @@
9191 assert.deepEqual(view.knownPaths(), ["a.txt", "b.txt"]);
9292 assert.deepEqual(view.files(), { "a.txt": "a\n" });
9393 });
95+ it("accounts for the membership bitmap before layout and stays observational", () => {
96+ const small = Repo.init({ "f.txt": lines("same") });
97+ const larger = Repo.init({ "f.txt": lines("same") });
98+ for (let i = 0; i < 8; i++)
99+ larger.edit({ attrs: [{ file: "f.txt", name: "exec", value: i % 2 === 0 ? "1" : null }] });
101+ const smallView = new View(small.graph, small.shared);
102+ const largerView = new View(larger.graph, larger.shared);
103+ const smallBeforeLayout = smallView.weight();
104+ const largerBeforeLayout = largerView.weight();
105+ assert.ok(smallBeforeLayout >= small.graph.size);
106+ assert.ok(largerBeforeLayout >= larger.graph.size);
107+ assert.equal(largerBeforeLayout - smallBeforeLayout, larger.graph.size - small.graph.size);
108+ assert.equal(largerView.weight(), largerBeforeLayout);
110+ assert.deepEqual(smallView.files(), largerView.files());
111+ const afterLayout = largerView.weight();
112+ assert.equal(largerView.weight(), afterLayout);
113+ assert.deepEqual(largerView.files(), { "f.txt": "same\n" });
114+ });
116+ it("accounts for lazy view-owned containers and relocation/index growth", () => {
117+ const repo = Repo.init({ "a.txt": lines("one", "two"), "b.txt": lines("base") });
118+ repo.edit({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 1 } });
119+ const deletion = repo.edit({ edits: [{ file: "b.txt", old: "base\n", new: "" }] });
120+ const view = repo.view();
121+ let weight = view.weight();
122+ assert.equal(view.weight(), weight);
124+ assert.equal(view.set.size, 3);
125+ assert.ok(view.weight() > weight);
126+ weight = view.weight();
127+ assert.deepEqual(view.knownPaths(), ["a.txt", "b.txt"]);
128+ assert.ok(view.weight() > weight);
129+ weight = view.weight();
130+ assert.equal(view.fileOf("a0.0"), "b.txt");
131+ assert.ok(view.weight() > weight);
133+ const expectedFiles = { "a.txt": "two\n", "b.txt": "one\n" };
134+ assert.deepEqual(view.files(), expectedFiles);
135+ weight = view.weight();
136+ assert.deepEqual(
137+ view.sequence("b.txt").map((line) => [line.text, line.visible, line.deletedBy]),
138+ [
139+ ["base", false, [deletion.id]],
140+ ["one", true, []],
141+ ],
142+ );
143+ assert.ok(view.weight() > weight);
144+ weight = view.weight();
145+ assert.equal(view.sequenceIndex("b.txt", "a0.0"), 1);
146+ assert.ok(view.weight() > weight);
147+ weight = view.weight();
148+ assert.equal(view.lineNumber("b.txt", "a0.0"), 0);
149+ assert.ok(view.weight() > weight);
150+ weight = view.weight();
151+ assert.deepEqual(
152+ view.lines("b.txt").map((line) => line.text),
153+ ["one"],
154+ );
155+ assert.ok(view.weight() > weight);
156+ const finalWeight = view.weight();
157+ assert.equal(view.weight(), finalWeight);
158+ assert.deepEqual(view.files(), expectedFiles);
159+ });
161+ it("counts line-cache references without charging Graph-owned text per view", () => {
162+ const short = Repo.init({ "f.txt": lines("x") });
163+ const long = Repo.init({ "f.txt": lines("x".repeat(10_000)) });
164+ const shortView = short.view();
165+ const longView = long.view();
166+ shortView.lines("f.txt");
167+ longView.lines("f.txt");
168+ assert.equal(shortView.weight(), longView.weight());
169+ const longWeight = longView.weight();
170+ assert.equal(longView.weight(), longWeight);
171+ });
94172});
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -71,6 +71,16 @@
7171const EMPTY_INTS = new Int32Array(0);
7272const NO_FILE: FileState = { attrs: NO_ATTRS, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
7373
74+// Approximate retained-container costs for View.weight(): reference slots,
75+// object headers, and map/set entries. These are cache-budget units, not V8
76+// heap measurements. Referenced Graph strings and records are not charged.
77+const ARRAY_HEADER_BYTES = 24;
78+const OBJECT_BYTES = 32;
79+const MAP_HEADER_BYTES = 32;
80+const MAP_ENTRY_BYTES = 40;
81+const SET_ENTRY_BYTES = 32;
82+const REFERENCE_BYTES = 8;
7484/**
7585 * The projection of a closed set. Construct once, then ask for files, lines
7686 * and sequences. Throws `not-closed` if some atom's dependency is missing.
@@ -448,21 +458,44 @@
448458 }
449459
450460 /**
451− * About how many bytes keeping this view costs (#46): its laid-out files
452− * (4 bytes a line, and more for a deleted line and for positions once asked
453− * for), and the records it keeps for the file it was last asked about.
461+ * About how many bytes of retained state this view costs (#46). This uses
462+ * approximate ownership costs for containers and wrappers, not V8 heap
463+ * measurements. Graph-owned strings and records count only as references.
464+ * This method only inspects already-created state: it does not lay out files,
465+ * populate lazy caches, enumerate the graph, or count temporary traversal
466+ * containers and shared empty singleton payloads as retained view state.
454467 */
455468 weight(): number {
456− let n = 0;
457− for (const f of this.#files.values())
458− n +=
459− 4 * (f.seq.length + f.vis.length) +
460− 40 * f.deletedBy.size +
461− 24 * ((f.seqIndex?.size ?? 0) + (f.visIndex?.size ?? 0));
462− n +=
463− 100 * (this.#linesCache?.lines.length ?? 0) +
464− 120 * (this.#seqCache?.seq.length ?? 0) +
465− 2 * (this.#textCache?.text.length ?? 0);
469+ let n = this.#in.byteLength;
470+ n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
471+ n += MAP_HEADER_BYTES; // #files exists from construction.
472+ n += MAP_HEADER_BYTES; // #fileOf exists from construction.
473+ if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
474+ if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;
475+ if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
476+ if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
477+ n += MAP_ENTRY_BYTES * this.#fileOf.size;
478+ for (const f of this.#files.values()) {
479+ n += MAP_ENTRY_BYTES;
480+ if (f !== NO_FILE) {
481+ // FileState and FileAttrs are per laid-out file; typed-array
482+ // headers are separate from their payload byte lengths below.
483+ n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
484+ n += f.seq.byteLength + f.vis.byteLength;
485+ n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
486+ for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;
487+ if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;
488+ if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;
489+ }
490+ }
491+ if (this.#linesCache)
492+ n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 100 * this.#linesCache.lines.length;
493+ if (this.#seqCache) {
494+ n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 120 * this.#seqCache.seq.length;
495+ for (const line of this.#seqCache.seq)
496+ if (line.deletedBy !== NONE) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * line.deletedBy.length;
497+ }
498+ if (this.#textCache) n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 2 * this.#textCache.text.length;
466499 return n;
467500 }
468501