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

Author totals: each shared file's lines by author and its size, kept as versions land, so tree with authors reads them instead of laying every file out (abso Capacity, issue 664; docs/disk-first-2.md; Security yes on ask 2096).

of shared·capacity··Not checked

version-files.ts keeps, beside the head line counts, each file's totals at the newest version (head-authors) and at every version that changed them (author-totals), from version 0 on and only while every version since has kept them. A version on the fast path adds each inserted visible line to its author and takes each deleted one off its writer's, with its UTF-8 bytes and newline (joinLines).

A version with moves, drops or a closure change counts every touched file from its layout, so a line rewritten with the same text by someone else still moves its author. Shared only; a draft's view takes its base's totals for the files the draft doesn't touch (touchedBeyond) and lays out the rest, and a named draft never reads them.

Kept in the writer's state, so whatever replaces or clears it (a new adoption, a restore, a live drop) takes them too. A writer adopted before this keeps none and lays out as before.

On a fresh abso adoption (my machine): tree with authors at 1224 takes 333 ms first and 50 ms again (11 to 16 s and 1.8 to 2.4 s before); an agent's tree with authors 619 ms again (2 to 3.4 s). Adoption 7.2 min, the same as before. A full run matches 33 of 33 answers.

Test: author-totals.test.ts, tree with authors at every version and in every agent's view, the stored writer against the in-memory project: moved lines, a rename by record and by move, a drop, a binary file, text beyond ASCII, drafts that edit, move lines or have nothing. With the path, move, drop, draft, adoption and version-files tests: 47 of 47.

Version 250 · Files at version 251

4 files, 282 lines added, 10 removed.

--- a/worker/project/history.ts
+++ b/worker/project/history.ts
@@ -6,6 +6,7 @@
66import { pathKey } from "./pathkey.ts";
77import { checkReadPath, differs, type LastChange, ProjectError, utf8Bytes } from "./project.ts";
88import { last, SHARED, type Space } from "./spaces.ts";
9+import type { FileTotals } from "./version-files.ts";
1011/** What the history reads need from a project. */
1112export interface HistoryHost {
@@ -18,6 +19,15 @@
1819 viewAt(space: Space, version: number): ReadView;
1920 /** A version's file list from kept version shapes, when every version up to it has one (pathindex.ts). */
2021 keptPaths?(space: Space, version: number): string[] | undefined;
22+ /**
23+ * Each file's kept author totals (version-files.ts) for a version of shared, or an agent's view of it: a lookup that
24+ * answers undefined for a file it can't, which is then laid out. Undefined when none are kept.
25+ */
26+ authorTotals?(
27+ space: Space,
28+ version: number,
29+ agent?: { readonly name: string; readonly space: string; readonly base: number },
30+ ): ((path: string) => FileTotals | undefined) | undefined;
2131 agentView(agent: { readonly name: string; readonly space: string; readonly base: number }): ReadView;
2232 spellingIn(view: ReadView, asked: string): string;
2333 pathsOf(space: Space): Map<string, PathLifeRecord>;
@@ -131,6 +141,8 @@
131141 authors.set(id, author);
132142 return author;
133143 };
144+ // Each file's lines by author and size kept as versions land (abso Capacity), for the files it can answer.
145+ const totalsOf = options.authors ? h.authorTotals?.(space, version, agent) : undefined;
134146 for (const path of paths) {
135147 const e = entryOf(path);
136148 if (!e) continue;
@@ -140,13 +152,19 @@
140152 if (agent && differs(base, view, path)) e.changed = true;
141153 if (options.authors) {
142154 const counts = (e.authors ??= new Map());
143− for (const l of view.lines(path)) {
144− const author = authorOf(l.atom);
145− counts.set(author, (counts.get(author) ?? 0) + 1);
146− }
147155 const blob = view.attrs(path).blob;
156+ const totals = totalsOf?.(path);
157+ if (totals) {
158+ for (const [author, n] of totals.authors) counts.set(author, (counts.get(author) ?? 0) + n);
159+ if (blob === null) e.bytes += totals.bytes;
160+ } else {
161+ for (const l of view.lines(path)) {
162+ const author = authorOf(l.atom);
163+ counts.set(author, (counts.get(author) ?? 0) + 1);
164+ }
165+ if (blob === null) e.bytes += utf8Bytes(view.text(path));
166+ }
148167 if (blob !== null) (e.blobs ??= []).push(blob);
149− else e.bytes += utf8Bytes(view.text(path));
150168 }
151169 }
152170 // A file the agent took out still marks the folder it was in, when that folder is still listed.
--- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -200,8 +200,14 @@
200200import { switchTo as hostedSwitchTo, type SwitchHost } from "./switch-to.ts";
201201import { sync as hostedSync, type SyncHost } from "./sync.ts";
202202import { treeHash } from "./tree.ts";
203−import { versionFiles as keptVersionFiles, type VersionFilesHost } from "./version-files.ts";
204203import {
204+ authoredVersion,
205+ authorTotalsAt,
206+ type FileTotals,
207+ versionFiles as keptVersionFiles,
208+ type VersionFilesHost,
209+} from "./version-files.ts";
210+import {
205211 type LogHost,
206212 compare as versionCompare,
207213 compareSummary as versionCompareSummary,
@@ -1787,6 +1793,7 @@
17871793 checkVersion: (space, version) => this.#checkVersion(space, version),
17881794 viewAt: (space, version) => this.#viewAt(space, version),
17891795 keptPaths: (space, version) => this.#keptPaths(space, version),
1796+ authorTotals: (space, version, agent) => this.#authorTotals(space, version, agent as AgentState | undefined),
17901797 agentView: (agent) => this.#agentView(agent as AgentState),
17911798 spellingIn: (view, asked) => this.spellingIn(view, asked),
17921799 pathsOf: (space) => this.#paths.pathsOf(space),
@@ -1802,6 +1809,25 @@
18021809 return this.#writerStorage && space.name === SHARED ? this.#paths.keptPaths(space, version) : undefined;
18031810 }
18041811
1812+ /**
1813+ * Each file's kept author totals for tree with authors (version-files.ts), on a stored writer and for shared only. An
1814+ * agent's view takes its base's totals for every file its draft doesn't touch, as its layout does (touchedBeyond),
1815+ * and lays out the rest; a draft that moves lines lays out everything.
1816+ */
1817+ #authorTotals(
1818+ space: Space,
1819+ version: number,
1820+ agent?: AgentState,
1821+ ): ((path: string) => FileTotals | undefined) | undefined {
1822+ const storage = this.#writerStorage;
1823+ if (!storage || space.name !== SHARED || authoredVersion(storage) === undefined) return undefined;
1824+ if (!agent || agent.mine.length === 0) return (path) => authorTotalsAt(storage, path, version);
1825+ const base = this.#viewAt(space, version);
1826+ const except = touchedBeyond(this.#graph, base.set, agent.mine, [base, this.#agentView(agent)]);
1827+ if (!except) return undefined;
1828+ return (path) => (except.has(path) ? undefined : authorTotalsAt(storage, path, version));
1829+ }
18051831 /** One folder at a version (history.ts). */
18061832 tree(...args: Tail<Parameters<typeof historyTree>>): ReturnType<typeof historyTree> {
18071833 return historyTree(this.#history, ...args);
--- /dev/null
+++ b/worker/project/test/author-totals.test.ts
@@ -0,0 +1,109 @@
1+// Author totals (abso Capacity, issue 664; version-files.ts): each shared file's lines by author and its size, kept as
2+// versions land, answer tree with authors on a stored writer without laying files out. Every tree with authors must
3+// match the in-memory project's, at every version and in every agent's view: lines rewritten with the same text by
4+// someone else, lines moved between files, a file renamed by record and by move, a drop, a binary file, text beyond
5+// ASCII, and drafts that edit, move lines, or hold nothing.
6+
7+import assert from "node:assert/strict";
8+import { test } from "node:test";
9+import { storedArray } from "../../../src/engine/state-source.ts";
10+import { SqlState } from "../../../src/engine/test/storage-helper.ts";
11+import { MemoryStore, Project } from "../project.ts";
12+import type { Entry, ProjectStore } from "../project-types.ts";
13+import { authoredVersion } from "../version-files.ts";
15+function writer() {
16+ const storage = new SqlState();
17+ const entries = storedArray<Entry>(storage, "canonical-log");
18+ const store: ProjectStore = {
19+ append(batch) {
20+ for (const entry of batch) entries.push(entry);
21+ },
22+ };
23+ return { storage, project: new Project(store, { writerStorage: storage, hasBlob: () => true }) };
24+}
26+const AREA = "export function area(r: number) {\n return 3.14 * r * r;\n}\n";
27+const BLOB = "c".repeat(64);
28+const AGENTS = ["ann", "bob", "cy", "dee", "eve", "fay", "old"];
30+function history(project: Project) {
31+ let t = 0;
32+ project.init(
33+ {
34+ "src/shapes.ts": `// shapes\n${AREA}`,
35+ "src/geometry.ts": "// geometry\n",
36+ "notes.md": "a\nb\n",
37+ "src/x.ts": "x\n",
38+ },
39+ t,
40+ {},
41+ { "logo.png": BLOB },
42+ );
43+ project.sync("old");
44+ // bob adds a line beyond ASCII to a file init wrote.
45+ project.record("bob", { "notes.md": "a\nb\nmore ünïcode ✓\n" }, ++t);
46+ assert.equal(project.publish("bob", "Notes", ++t).ok, true);
47+ // ann moves area() into geometry.ts (a move once published), then renames x.ts by record.
48+ project.edit("ann", { file: "src/shapes.ts", old: AREA, new: "" }, ++t);
49+ project.edit("ann", { file: "src/geometry.ts", content: `// geometry\n${AREA}` }, ++t);
50+ assert.equal(project.publish("ann", "Move area", ++t).ok, true);
51+ project.record("ann", { "src/x.ts": null, "src/y.ts": "x\n" }, ++t);
52+ assert.equal(project.publish("ann", "Rename x", ++t).ok, true);
53+ // cy edits a moved line; the mover moves a file.
54+ project.edit("cy", { file: "src/geometry.ts", old: "3.14", new: "Math.PI" }, ++t);
55+ assert.equal(project.publish("cy", "Pi", ++t).ok, true);
56+ project.move("mover", "notes.md", "docs/notes.md", ++t);
57+ assert.equal(project.publish("mover", "Move notes", ++t).ok, true);
58+ // A version dropped again.
59+ project.record("dee", { "src/y.ts": "x\ny\n" }, ++t);
60+ const addY = project.publish("dee", "Add y", ++t) as { ok: boolean; version: number };
61+ assert.equal(addY.ok, true);
62+ assert.equal(project.drop("dee", [addY.version], "Not y", ++t).ok, true);
63+ // Drafts: eve edits, fay moves lines, ann has nothing, old sits on version 0.
64+ for (const agent of ["ann", "eve", "fay"]) project.sync(agent);
65+ project.record("eve", { "src/geometry.ts": "// geometry\nnew\n", "src/z.ts": "z\n" }, ++t);
66+ project.edit("fay", { file: "src/geometry.ts", old: "// geometry\n", new: "" }, ++t);
67+ project.edit("fay", { file: "src/moved.ts", content: "// geometry\n" }, ++t);
68+}
70+const trees = (project: Project) => {
71+ const out: Record<string, unknown> = {};
72+ const last = project.log().versions[0]!.version;
73+ for (let v = 0; v <= last; v++)
74+ for (const folder of ["", "src"])
75+ try {
76+ out[`${v} ${folder}`] = project.tree(folder, { version: v, authors: true });
77+ } catch (e) {
78+ out[`${v} ${folder}`] = String(e);
79+ }
80+ for (const agent of AGENTS)
81+ for (const folder of ["", "src"]) out[`${agent} ${folder}`] = project.tree(folder, { agent, authors: true });
82+ return out;
83+};
85+test("tree with authors from kept totals matches the in-memory project at every version and in every draft", () => {
86+ const oracle = new Project(new MemoryStore(), { hasBlob: () => true });
87+ const { storage, project } = writer();
88+ history(oracle);
89+ history(project);
90+ const expected = trees(oracle);
91+ assert.deepEqual(trees(project), expected);
92+ // The totals answered: kept up to shared's newest version.
93+ assert.equal(authoredVersion(storage), project.log().versions[0]!.version);
94+ // The cases the test is for, so a history change can't empty it.
95+ const at = (v: number, name: string) =>
96+ (expected[`${v} `] as { entries: { name: string; authors?: { agent: string }[] }[] }).entries.find(
97+ (e) => e.name === name,
98+ );
99+ assert.deepEqual(at(1, "notes.md")!.authors, [
100+ { agent: "init", lines: 2 },
101+ { agent: "bob", lines: 1 },
102+ ]);
103+ assert.ok(JSON.stringify(expected).includes('"agent":"cy"'));
104+ assert.equal(
105+ (expected["0 "] as { entries: { name: string; blobs?: string[] }[] }).entries.find((e) => e.name === "logo.png")!
106+ .blobs![0],
107+ BLOB,
108+ );
109+});
--- a/worker/project/version-files.ts
+++ b/worker/project/version-files.ts
@@ -10,12 +10,96 @@
1010import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
1111import type { GraphStateStorage } from "../../src/engine/state-source.ts";
1212import { touchedFiles } from "./collisions.ts";
13+import { utf8Bytes } from "./project-helpers.ts";
1314import type { Published } from "./spaces.ts";
1415import { differs } from "./view-changes.ts";
1516
1617const COUNTS = "head-visible";
1718const META = "head-visible:meta";
1819
20+// Author totals (docs/disk-first-2.md, abso Capacity, issue 664): each shared file's lines by author and its text's size
21+// in UTF-8 bytes, as tree with authors counts them, so it reads one value per file instead of laying every file out.
22+// AUTHORS holds each file's totals at the newest version kept; AUTHORS_LOG each file's totals at every version that
23+// changed them, oldest first. Shared only, never a draft (Security on ask 2096). They live in the writer's state, so
24+// whatever clears or replaces that state (a new adoption, a restore, a drop of the project) takes them with it.
25+const AUTHORS = "head-authors";
26+const AUTHORS_LOG = "author-totals";
27+const AUTHORS_META = "head-authors:meta";
29+/** A file's lines by author and its text's size, stored: authors by name, none at zero; null when it has no lines. */
30+type StoredTotals = { a: [string, number][]; b: number };
32+/** A shared file's lines by author and its text's size in UTF-8 bytes. */
33+export interface FileTotals {
34+ readonly authors: ReadonlyMap<string, number>;
35+ readonly bytes: number;
36+}
38+const NO_TOTALS: FileTotals = { authors: new Map(), bytes: 0 };
40+/** The newest shared version whose author totals are kept, or undefined when this writer keeps none. */
41+export function authoredVersion(storage: GraphStateStorage): number | undefined {
42+ return storage.read(AUTHORS_META, "version") as number | undefined;
43+}
45+/** A shared file's totals at version `v`, or undefined when they aren't kept that far: lay the file out instead. */
46+export function authorTotalsAt(storage: GraphStateStorage, path: string, v: number): FileTotals | undefined {
47+ const upTo = authoredVersion(storage);
48+ if (upTo === undefined || v > upTo) return undefined;
49+ const log = storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined;
50+ if (!log) return NO_TOTALS;
51+ // The newest entry at or before v.
52+ let lo = 0;
53+ let hi = log.length - 1;
54+ let found = -1;
55+ while (lo <= hi) {
56+ const mid = (lo + hi) >> 1;
57+ if (log[mid]![0] <= v) {
58+ found = mid;
59+ lo = mid + 1;
60+ } else hi = mid - 1;
61+ }
62+ const totals = found < 0 ? null : log[found]![1];
63+ return totals ? { authors: new Map(totals.a), bytes: totals.b } : NO_TOTALS;
64+}
66+/** Each atom's author, read once. */
67+function authorsOf(graph: Graph): (id: AtomId) => string {
68+ const known = new Map<AtomId, string>();
69+ return (id) => {
70+ let author = known.get(id);
71+ if (author === undefined) known.set(id, (author = graph.atom(id).author));
72+ return author;
73+ };
74+}
76+/** Totals in stored form: authors sorted, none at zero; null for a file with no lines. */
77+function normalTotals(authors: ReadonlyMap<string, number>, bytes: number): StoredTotals | null {
78+ const a = [...authors].filter(([, n]) => n !== 0).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0));
79+ return a.length === 0 && bytes === 0 ? null : { a, b: bytes };
80+}
82+/** A file's totals from its layout in a view, as tree with authors counts them. */
83+function laidOutTotals(view: ReadView, path: string, authorOf: (id: AtomId) => string): StoredTotals | null {
84+ if (!view.hasFile(path)) return null;
85+ const authors = new Map<string, number>();
86+ for (const l of view.lines(path)) {
87+ const author = authorOf(l.atom);
88+ authors.set(author, (authors.get(author) ?? 0) + 1);
89+ }
90+ return normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path)));
91+}
93+/** Keep a file's totals at version `v`, when they differ from what it had. */
94+function keepTotals(storage: GraphStateStorage, v: number, path: string, totals: StoredTotals | null): void {
95+ const was = (storage.read(AUTHORS, path) as StoredTotals | undefined) ?? null;
96+ if (JSON.stringify(was) === JSON.stringify(totals)) return;
97+ if (totals) storage.write(AUTHORS, path, totals);
98+ else storage.remove(AUTHORS, path);
99+ const log = (storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined) ?? [];
100+ storage.write(AUTHORS_LOG, path, [...log, [v, totals]]);
101+}
19103/** The shared version the running counts describe, or undefined when this writer keeps none. */
20104export function countedVersion(storage: GraphStateStorage): number | undefined {
21105 return storage.read(META, "version") as number | undefined;
@@ -58,6 +142,10 @@
58142 // The same moves as the version before: share them, and where lines sit, instead of resolving them again.
59143 if (fast && before instanceof View && after instanceof View) after.shareMoves(before);
60144 const touched = touchedFiles(graph, atoms, [before, after]);
145+ // Author totals start with the first version too, and are kept only while every version since has kept them.
146+ const authored = authoredVersion(storage);
147+ const authoring = authored === v - 1 || (v === 0 && authored === undefined);
148+ const authorOf = authorsOf(graph);
61149 let files: string[];
62150 if (!fast) {
63151 files = [...touched].filter((path) => differs(before, after, path)).sort();
@@ -66,6 +154,12 @@
66154 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path));
67155 storage.write(META, "version", v);
68156 }
157+ // Every touched file's totals from its layout: one whose lines are the same texts by someone else isn't among
158+ // the changed files, but its authors changed.
159+ if (authoring) {
160+ for (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf));
161+ storage.write(AUTHORS_META, "version", v);
162+ }
69163 return { files };
70164 }
71165 const beforeSet = host.at(v - 1);
@@ -76,11 +170,15 @@
76170 };
77171 const fileOf = (view: ReadView, line: string, n: number) =>
78172 view.hasMoves() ? view.fileOf(line) : graph.writtenIn(n);
79− // Per file: visible lines added minus removed, and the texts' balance.
80− const changes = new Map<string, { net: number; texts: Map<string, number>; lines: number }>();
173+ // Per file: visible lines added minus removed, and the texts' balance; and, while totals are kept, each author's lines
174+ // and the bytes added minus removed (a line and its newline, as joinLines makes the file).
175+ const changes = new Map<
176+ string,
177+ { net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number }
178+ >();
81179 const change = (path: string) => {
82180 let c = changes.get(path);
83− if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0 }));
181+ if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0, authors: new Map(), bytes: 0 }));
84182 return c;
85183 };
86184 for (const id of p.atoms) {
@@ -92,17 +190,38 @@
92190 c.net++;
93191 c.lines++;
94192 c.texts.set(text, (c.texts.get(text) ?? 0) + 1);
193+ if (authoring) {
194+ const author = authorOf(id);
195+ c.authors.set(author, (c.authors.get(author) ?? 0) + 1);
196+ c.bytes += utf8Bytes(text) + 1;
197+ }
95198 }
96199 for (const line of graph.deletedLines(id)) {
97200 const n = graph.lineN(line);
98− if (!beforeSet.has(graph.idOfSeq(graph.writerSeqOf(n))) || !visibleIn(beforeSet, n)) continue;
201+ const writer = graph.idOfSeq(graph.writerSeqOf(n));
202+ if (!beforeSet.has(writer) || !visibleIn(beforeSet, n)) continue;
99203 const c = change(fileOf(before, line, n));
100204 const text = graph.textOf(n);
101205 c.net--;
102206 c.lines++;
103207 c.texts.set(text, (c.texts.get(text) ?? 0) - 1);
208+ if (authoring) {
209+ const author = authorOf(writer);
210+ c.authors.set(author, (c.authors.get(author) ?? 0) - 1);
211+ c.bytes -= utf8Bytes(text) + 1;
212+ }
104213 }
105214 }
215+ if (authoring) {
216+ for (const [path, c] of changes) {
217+ if (c.lines === 0) continue;
218+ const was = storage.read(AUTHORS, path) as StoredTotals | undefined;
219+ const authors = new Map(was?.a ?? []);
220+ for (const [author, d] of c.authors) authors.set(author, (authors.get(author) ?? 0) + d);
221+ keepTotals(storage, v, path, normalTotals(authors, (was?.b ?? 0) + c.bytes));
222+ }
223+ storage.write(AUTHORS_META, "version", v);
224+ }
106225 files = [];
107226 const exists = new Map<string, readonly [boolean, boolean]>();
108227 for (const path of touched) {