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). Version 251 of shared· capacity· Oct 15 (2026-10-15 18:14 UTC) · 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 @@
6 6 import { pathKey } from "./pathkey.ts"; 7 7 import { checkReadPath, differs, type LastChange, ProjectError, utf8Bytes } from "./project.ts"; 8 8 import { last, SHARED, type Space } from "./spaces.ts"; 9 + import type { FileTotals } from "./version-files.ts"; 10 11 /** What the history reads need from a project. */ 11 12 export interface HistoryHost { @@ -18,6 +19,15 @@
18 19 viewAt(space: Space, version: number): ReadView; 19 20 /** A version's file list from kept version shapes, when every version up to it has one (pathindex.ts). */ 20 21 keptPaths?(space: Space, version: number): string[] | undefined; 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. 29 + agent?: { readonly name: string; readonly space: string; readonly base: number }, 30 + ): ((path: string) => FileTotals | undefined) | undefined; 21 31 agentView(agent: { readonly name: string; readonly space: string; readonly base: number }): ReadView; 22 32 spellingIn(view: ReadView, asked: string): string; 23 33 pathsOf(space: Space): Map<string, PathLifeRecord>; @@ -131,6 +141,8 @@
131 141 authors.set(id, author); 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; 134 146 for (const path of paths) { 135 147 const e = entryOf(path); @@ -140,13 +152,19 @@
140 152 if (agent && differs(base, view, path)) e.changed = true; 141 153 if (options.authors) { 142 154 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 − }
147 155 const blob = view.attrs(path).blob; 156 + const totals = totalsOf?.(path); 158 + for (const [author, n] of totals.authors) counts.set(author, (counts.get(author) ?? 0) + n); 159 + if (blob === null) e.bytes += totals.bytes; 161 + for (const l of view.lines(path)) { 162 + const author = authorOf(l.atom); 163 + counts.set(author, (counts.get(author) ?? 0) + 1); 165 + if (blob === null) e.bytes += utf8Bytes(view.text(path)); 148 167 if (blob !== null) (e.blobs ??= []).push(blob); 149 − else e.bytes += utf8Bytes(view.text(path));
152 170 // 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 @@
200 200 import { switchTo as hostedSwitchTo, type SwitchHost } from "./switch-to.ts"; 201 201 import { sync as hostedSync, type SyncHost } from "./sync.ts"; 202 202 import { treeHash } from "./tree.ts"; 203 − import { versionFiles as keptVersionFiles, type VersionFilesHost } from "./version-files.ts";
207 + versionFiles as keptVersionFiles, 208 + type VersionFilesHost, 209 + } from "./version-files.ts"; 206 212 compare as versionCompare, 207 213 compareSummary as versionCompareSummary, @@ -1787,6 +1793,7 @@
1787 1793 checkVersion: (space, version) => this.#checkVersion(space, version), 1788 1794 viewAt: (space, version) => this.#viewAt(space, version), 1789 1795 keptPaths: (space, version) => this.#keptPaths(space, version), 1796 + authorTotals: (space, version, agent) => this.#authorTotals(space, version, agent as AgentState | undefined), 1790 1797 agentView: (agent) => this.#agentView(agent as AgentState), 1791 1798 spellingIn: (view, asked) => this.spellingIn(view, asked), 1792 1799 pathsOf: (space) => this.#paths.pathsOf(space), @@ -1802,6 +1809,25 @@
1802 1809 return this.#writerStorage && space.name === SHARED ? this.#paths.keptPaths(space, version) : undefined; 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. 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)); 1805 1831 /** One folder at a version (history.ts). */ 1806 1832 tree(...args: Tail<Parameters<typeof historyTree>>): ReturnType<typeof historyTree> { 1807 1833 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. 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"; 16 + const storage = new SqlState(); 17 + const entries = storedArray<Entry>(storage, "canonical-log"); 18 + const store: ProjectStore = { 20 + for (const entry of batch) entries.push(entry); 23 + return { storage, project: new Project(store, { writerStorage: storage, hasBlob: () => true }) }; 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) { 34 + "src/shapes.ts": `// shapes\n${AREA}`, 35 + "src/geometry.ts": "// geometry\n", 36 + "notes.md": "a\nb\n", 41 + { "logo.png": BLOB }, 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); 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"]) 76 + out[`${v} ${folder}`] = project.tree(folder, { version: v, authors: true }); 78 + out[`${v} ${folder}`] = String(e); 80 + for (const agent of AGENTS) 81 + for (const folder of ["", "src"]) out[`${agent} ${folder}`] = project.tree(folder, { agent, authors: true }); 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(); 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, 99 + assert.deepEqual(at(1, "notes.md")!.authors, [ 100 + { agent: "init", lines: 2 }, 101 + { agent: "bob", lines: 1 }, 103 + assert.ok(JSON.stringify(expected).includes('"agent":"cy"')); 105 + (expected["0 "] as { entries: { name: string; blobs?: string[] }[] }).entries.find((e) => e.name === "logo.png")! --- a/worker/project/version-files.ts
+++ b/worker/project/version-files.ts
@@ -10,12 +10,96 @@
10 10 import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts"; 11 11 import type { GraphStateStorage } from "../../src/engine/state-source.ts"; 12 12 import { touchedFiles } from "./collisions.ts"; 13 + import { utf8Bytes } from "./project-helpers.ts"; 13 14 import type { Published } from "./spaces.ts"; 14 15 import { differs } from "./view-changes.ts"; 16 17 const COUNTS = "head-visible"; 17 18 const META = "head-visible:meta"; 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; 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; 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. 53 + let hi = log.length - 1; 56 + const mid = (lo + hi) >> 1; 57 + if (log[mid]![0] <= v) { 62 + const totals = found < 0 ? null : log[found]![1]; 63 + return totals ? { authors: new Map(totals.a), bytes: totals.b } : NO_TOTALS; 66 + /** Each atom's author, read once. */ 67 + function authorsOf(graph: Graph): (id: AtomId) => string { 68 + const known = new Map<AtomId, string>(); 70 + let author = known.get(id); 71 + if (author === undefined) known.set(id, (author = graph.atom(id).author)); 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 }; 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); 90 + return normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path))); 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]]); 19 103 /** The shared version the running counts describe, or undefined when this writer keeps none. */ 20 104 export function countedVersion(storage: GraphStateStorage): number | undefined { 21 105 return storage.read(META, "version") as number | undefined; @@ -58,6 +142,10 @@
58 142 // The same moves as the version before: share them, and where lines sit, instead of resolving them again. 59 143 if (fast && before instanceof View && after instanceof View) after.shareMoves(before); 60 144 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); 61 149 let files: string[]; 63 151 files = [...touched].filter((path) => differs(before, after, path)).sort(); @@ -66,6 +154,12 @@
66 154 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path)); 67 155 storage.write(META, "version", v); 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. 160 + for (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf)); 161 + storage.write(AUTHORS_META, "version", v); 71 165 const beforeSet = host.at(v - 1); @@ -76,11 +170,15 @@
77 171 const fileOf = (view: ReadView, line: string, n: number) => 78 172 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< 177 + { net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number } 81 179 const change = (path: string) => { 82 180 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 })); 86 184 for (const id of p.atoms) { @@ -92,17 +190,38 @@
94 192 c.texts.set(text, (c.texts.get(text) ?? 0) + 1); 194 + const author = authorOf(id); 195 + c.authors.set(author, (c.authors.get(author) ?? 0) + 1); 196 + c.bytes += utf8Bytes(text) + 1; 96 199 for (const line of graph.deletedLines(id)) { 97 200 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; 99 203 const c = change(fileOf(before, line, n)); 100 204 const text = graph.textOf(n); 103 207 c.texts.set(text, (c.texts.get(text) ?? 0) - 1); 209 + const author = authorOf(writer); 210 + c.authors.set(author, (c.authors.get(author) ?? 0) - 1); 211 + c.bytes -= utf8Bytes(text) + 1; 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)); 223 + storage.write(AUTHORS_META, "version", v); 107 226 const exists = new Map<string, readonly [boolean, boolean]>(); 108 227 for (const path of touched) {