- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/version-files.ts?format=json
272 linescapacity 100% · First import 1%Last change capacity in version 258, 3 versions changed it
1// Each version of shared's changed files, worked out as the version lands from what it touched (disk-first 2;
2// abso Capacity). The log, a path's history and the tree read these lists; working them out from two layouts of every
3// touched file per version cost game's whole history 460 s to adopt. Here a version costs about its own size:
4// - a file it touched differs when its visible lines' count or texts changed, or its mode or blob did;
5// - whether a file exists before and after comes from a running count of each file's visible lines;
6// - only a version whose added and removed lines have the very same texts, or one with moves, drops or a change to
7// shared's closure, is laid out as before.
8// The answers are exactly differs() in view-changes.ts.
10import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
11import type { GraphStateStorage } from "../../src/engine/state-source.ts";
12import { touchedFiles } from "./collisions.ts";
13import { utf8Bytes } from "./project-helpers.ts";
14import type { Published } from "./spaces.ts";
15import { differs } from "./view-changes.ts";
17const COUNTS = "head-visible";
18const 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.
25const AUTHORS = "head-authors";
26const AUTHORS_LOG = "author-totals";
27const 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. */
30type StoredTotals = { a: [string, number][]; b: number };
32/** A shared file's lines by author and its text's size in UTF-8 bytes. */
33export interface FileTotals {
34 readonly authors: ReadonlyMap<string, number>;
35 readonly bytes: number;
36}
38const 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. */
41export 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. */
46export 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. */
67function 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. */
77function 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/**
83 * A laid-out file's lines by author, from its visible lines' writers (abso Capacity): each writer's author once, instead
84 * of making an atom id and a line id for every line (an agent's tree spent 0.4 s on that for its draft's files).
85 */
86export function authorsInView(
87 view: ReadView,
88 path: string,
89 graph: Graph,
90 authorOf: (id: AtomId) => string,
91): Map<string, number> {
92 const out = new Map<string, number>();
93 const writers = (view as { visibleWriters?(path: string): readonly number[] }).visibleWriters?.call(view, path);
94 if (!writers) {
95 for (const l of view.lines(path)) {
96 const author = authorOf(l.atom);
97 out.set(author, (out.get(author) ?? 0) + 1);
98 }
99 return out;
100 }
101 const bySeq = new Map<number, number>();
102 for (const seq of writers) bySeq.set(seq, (bySeq.get(seq) ?? 0) + 1);
103 for (const [seq, n] of bySeq) {
104 const author = authorOf(graph.idOfSeq(seq));
105 out.set(author, (out.get(author) ?? 0) + n);
106 }
107 return out;
108}
110/** A file's totals from its layout in a view, as tree with authors counts them. */
111function laidOutTotals(view: ReadView, path: string, authorOf: (id: AtomId) => string): StoredTotals | null {
112 if (!view.hasFile(path)) return null;
113 const authors = authorsInView(view, path, view.graph, authorOf);
114 return normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path)));
115}
117/** Keep a file's totals at version `v`, when they differ from what it had. */
118function keepTotals(storage: GraphStateStorage, v: number, path: string, totals: StoredTotals | null): void {
119 const was = (storage.read(AUTHORS, path) as StoredTotals | undefined) ?? null;
120 if (JSON.stringify(was) === JSON.stringify(totals)) return;
121 if (totals) storage.write(AUTHORS, path, totals);
122 else storage.remove(AUTHORS, path);
123 const log = (storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined) ?? [];
124 storage.write(AUTHORS_LOG, path, [...log, [v, totals]]);
125}
127/** The shared version the running counts describe, or undefined when this writer keeps none. */
128export function countedVersion(storage: GraphStateStorage): number | undefined {
129 return storage.read(META, "version") as number | undefined;
130}
132export interface VersionFilesHost {
133 readonly storage: GraphStateStorage;
134 readonly graph: Graph;
135 /** Shared's set at a version (closed) and its view. */
136 at(version: number): ReadonlySet<AtomId>;
137 viewAt(version: number): ReadView;
138 empty(): ReadView;
139 /** Whether shared's closure changed at this version (spaces.ts). */
140 closureChangedAt(version: number): boolean;
141}
143/** A file's visible lines in a view: a laid-out count, for the slow path. */
144function visibleCount(view: ReadView, path: string): number {
145 const counted = (view as { visibleCount?(path: string): number }).visibleCount;
146 return counted ? counted.call(view, path) : view.lines(path).length;
147}
149/** The changed files of shared's version `v`, sorted, and which existed before and after; the running counts moved to `v`. */
150export function versionFiles(
151 host: VersionFilesHost,
152 p: Published,
153): { files: string[]; exists?: Map<string, readonly [boolean, boolean]> } {
154 const { storage, graph } = host;
155 const v = p.version;
156 const counted = countedVersion(storage);
157 const atoms = [...p.atoms, ...(p.dropped ?? [])];
158 const before = v === 0 ? host.empty() : host.viewAt(v - 1);
159 const after = host.viewAt(v);
160 const fast =
161 v > 0 &&
162 counted === v - 1 &&
163 !p.dropped?.length &&
164 !p.atoms.some((id) => graph.atom(id).moves?.length) &&
165 !host.closureChangedAt(v);
166 // The same moves as the version before: share them, and where lines sit, instead of resolving them again.
167 if (fast && before instanceof View && after instanceof View) after.shareMoves(before);
168 const touched = touchedFiles(graph, atoms, [before, after]);
169 // Author totals start with the first version too, and are kept only while every version since has kept them.
170 const authored = authoredVersion(storage);
171 const authoring = authored === v - 1 || (v === 0 && authored === undefined);
172 const authorOf = authorsOf(graph);
173 let files: string[];
174 if (!fast) {
175 files = [...touched].filter((path) => differs(before, after, path)).sort();
176 // Counts start with the first version; a writer adopted before has none and keeps none.
177 if (counted === v - 1 || (v === 0 && counted === undefined)) {
178 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path));
179 storage.write(META, "version", v);
180 }
181 // Every touched file's totals from its layout: one whose lines are the same texts by someone else isn't among
182 // the changed files, but its authors changed.
183 if (authoring) {
184 for (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf));
185 storage.write(AUTHORS_META, "version", v);
186 }
187 return { files };
188 }
189 const beforeSet = host.at(v - 1);
190 const afterSet = host.at(v);
191 const visibleIn = (set: ReadonlySet<AtomId>, n: number) => {
192 for (const seq of graph.eachDeleterSeqOf(n)) if (set.has(graph.idOfSeq(seq))) return false;
193 return true;
194 };
195 const fileOf = (view: ReadView, line: string, n: number) =>
196 view.hasMoves() ? view.fileOf(line) : graph.writtenIn(n);
197 // Per file: visible lines added minus removed, and the texts' balance; and, while totals are kept, each author's lines
198 // and the bytes added minus removed (a line and its newline, as joinLines makes the file).
199 const changes = new Map<
200 string,
201 { net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number }
202 >();
203 const change = (path: string) => {
204 let c = changes.get(path);
205 if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0, authors: new Map(), bytes: 0 }));
206 return c;
207 };
208 for (const id of p.atoms) {
209 for (const line of graph.eachInsertedLine(id)) {
210 const n = graph.lineN(line);
211 if (!visibleIn(afterSet, n)) continue;
212 const c = change(fileOf(after, line, n));
213 const text = graph.textOf(n);
214 c.net++;
215 c.lines++;
216 c.texts.set(text, (c.texts.get(text) ?? 0) + 1);
217 if (authoring) {
218 const author = authorOf(id);
219 c.authors.set(author, (c.authors.get(author) ?? 0) + 1);
220 c.bytes += utf8Bytes(text) + 1;
221 }
222 }
223 for (const line of graph.deletedLines(id)) {
224 const n = graph.lineN(line);
225 const writer = graph.idOfSeq(graph.writerSeqOf(n));
226 if (!beforeSet.has(writer) || !visibleIn(beforeSet, n)) continue;
227 const c = change(fileOf(before, line, n));
228 const text = graph.textOf(n);
229 c.net--;
230 c.lines++;
231 c.texts.set(text, (c.texts.get(text) ?? 0) - 1);
232 if (authoring) {
233 const author = authorOf(writer);
234 c.authors.set(author, (c.authors.get(author) ?? 0) - 1);
235 c.bytes -= utf8Bytes(text) + 1;
236 }
237 }
238 }
239 if (authoring) {
240 for (const [path, c] of changes) {
241 if (c.lines === 0) continue;
242 const was = storage.read(AUTHORS, path) as StoredTotals | undefined;
243 const authors = new Map(was?.a ?? []);
244 for (const [author, d] of c.authors) authors.set(author, (authors.get(author) ?? 0) + d);
245 keepTotals(storage, v, path, normalTotals(authors, (was?.b ?? 0) + c.bytes));
246 }
247 storage.write(AUTHORS_META, "version", v);
248 }
249 files = [];
250 const exists = new Map<string, readonly [boolean, boolean]>();
251 for (const path of touched) {
252 const was = (storage.read(COUNTS, path) as number | undefined) ?? 0;
253 const c = changes.get(path);
254 const now = was + (c?.net ?? 0);
255 const b = before.attrs(path);
256 const a = after.attrs(path);
257 const had = b.exists || b.link !== null || b.blob !== null || was > 0;
258 const has = a.exists || a.link !== null || a.blob !== null || now > 0;
259 let differ = had !== has || b.exec !== a.exec || b.link !== a.link || b.blob !== a.blob;
260 if (!differ && had && c && c.lines > 0) {
261 // Different texts in or out: the file's lines changed. The same texts in and out: compare the files.
262 differ = [...c.texts.values()].some((count) => count !== 0) || !before.sameText(path, after);
263 }
264 if (differ) {
265 files.push(path);
266 exists.set(path, [had, has]);
267 }
268 if (now !== was) storage.write(COUNTS, path, now);
269 }
270 storage.write(META, "version", v);
271 return { files: files.sort(), exists };
272}