- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/version-log.ts?format=json
347 linesFirst import 74% · capacity 20% · codex-capacity-chair 6%Last change capacity in version 313, 8 versions changed it
1import { View } from "../../src/engine/index.ts";
2// The version list reads: log (versions newest first, or one file's history), ribbon (who published each version),
3// show (one version's changes), fileAt, compare and viewAt. They read through LogHost; today the host is the project
4// in memory.
6import type { AtomId, Graph, ReadView } from "../../src/engine/index.ts";
7import { checkReadPath } from "./path-rules.ts";
8import type { PathIndex } from "./pathindex.ts";
9import { ProjectError } from "./project-error.ts";
10import type { FileChange, LogAnswer, LogEntry, ShowAnswer } from "./project-types.ts";
11import { last, SHARED, type Space } from "./spaces.ts";
12import { changesBetween, summariesBetween } from "./view-changes.ts";
14/** What the version list reads need from a project. */
15export interface LogHost {
16 readonly graph: Graph;
17 readonly paths: Pick<PathIndex, "knowsFiles" | "versionFiles" | "versionShape">;
18 readonly labels: ReadonlyMap<string, readonly number[]>;
19 shared(): Space;
20 needInit(): void;
21 spaceFor(options: { draft?: string | null; agent?: string }): Space;
22 checkVersion(space: Space, version: number): void;
23 viewAt(space: Space, version: number): ReadView;
24 view(set: ReadonlySet<AtomId>): ReadView;
25 at(space: Space, version: number): ReadonlySet<AtomId>;
26 spellingIn(view: ReadView, path: string): string;
27 versionChange(space: Space, version: number): number | undefined;
28 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;
29}
31/**
32 * The versions of shared (or of a named draft), newest first: who
33 * published each, why, and which files it touched. `limit` defaults to 50.
34 */
35export function log(
36 host: LogHost,
37 options: {
38 draft?: string | null;
39 agent?: string;
40 limit?: number;
41 path?: string;
42 before?: number;
43 follow?: boolean;
44 } = {},
45): LogAnswer {
46 host.needInit();
47 const space = host.spaceFor(options);
48 const limit = Number.isInteger(options.limit) && options.limit! > 0 ? Math.min(options.limit!, 200) : 50;
49 if (options.path !== undefined) checkReadPath(options.path);
50 if (options.before !== undefined && (!Number.isInteger(options.before) || options.before < 0))
51 throw new ProjectError("bad-request", "before must be a version number");
52 const versions: LogEntry[] = [];
53 // Paging (#41, the website): `before` starts the page below that version; `next` is the `before` for the page after.
54 let v = Math.min(last(space), (options.before ?? last(space) + 1) - 1);
55 // Any spelling reads the stored one (#41, F7). With follow, the history follows moves (F27): before
56 // the version that moved the file in, it's the file it came from.
57 const path = options.path === undefined ? undefined : host.spellingIn(host.viewAt(space, last(space)), options.path);
58 const names = path === undefined ? [] : options.follow ? namesBefore(host, space, path) : [{ path, since: 0 }];
59 const nameAt = (version: number): string => names.find((n) => version >= n.since)?.path ?? path!;
60 // A file's history (#43): only versions whose changes name the file, or a file moves can carry lines from, can have changed it.
61 const sources = path === undefined ? undefined : new Set(names.flatMap((n) => [...moveSources(host.graph, n.path)]));
62 const g = host.graph;
63 const views = new Map<number, ReadView>();
64 for (; v >= 0 && versions.length < limit; v--) {
65 if (sources && !host.paths.knowsFiles(space, v)) {
66 const p = space.versions[v]!;
67 if (![...p.atoms, ...(p.dropped ?? [])].some((id) => g.filesOf(id).some((f) => sources.has(f)))) continue;
68 }
69 // With a path: only the versions that changed that file (its history), under its name then. Their files first, and
70 // the whole entry (every atom of the version read) only for those (abso Capacity): a file's history read every
71 // version's atoms down to its twentieth change.
72 if (path !== undefined && !host.paths.versionFiles(space, v, views).includes(nameAt(v))) {
73 views.delete(v + 1);
74 continue;
75 }
76 const entry = logEntry(host, space, v, views);
77 // The walk goes down: a view above this version won't be needed again (#46).
78 views.delete(v + 1);
79 if (path === undefined) versions.push(entry);
80 else versions.push(nameAt(v) === path ? entry : { ...entry, path: nameAt(v) });
81 }
82 const next = v >= 0 && versions.length > 0 ? versions[versions.length - 1]!.version : undefined;
83 const movedFrom = names.slice(0, -1).map((n, i) => ({ path: names[i + 1]!.path, version: n.since }));
84 return {
85 draft: space.name === SHARED ? null : space.name,
86 version: last(space),
87 versions,
88 ...(next !== undefined ? { next } : {}),
89 ...(path !== undefined ? { path } : {}),
90 ...(movedFrom.length > 0 ? { movedFrom } : {}),
91 };
92}
94/**
95 * A file's names back through moves (#41, F27), newest first: each with the
96 * version from which it had that name. Found from the atoms that moved lines
97 * into it (abso mv), so it never guesses from how alike two files are.
98 */
99function namesBefore(host: LogHost, space: Space, path: string): { path: string; since: number }[] {
100 const g = host.graph;
101 const out: { path: string; since: number }[] = [];
102 let cur = path;
103 let below = last(space) + 1;
104 const movers = g.movers();
105 for (;;) {
106 // The newest version below `below` that moved another file into `cur`.
107 const into = movers
108 .filter((id) => g.filesOf(id).includes(cur))
109 .map((id) => space.publishedIn.get(id))
110 .filter((v): v is number => v !== undefined && v < below)
111 .toSorted((a, b) => b - a);
112 let found: { from: string; version: number } | undefined;
113 for (const version of into) {
114 const move = host.paths.versionShape(space, version).moves.find(([, to]) => to === cur);
115 if (move) {
116 found = { from: move[0], version };
117 break;
118 }
119 }
120 out.push({ path: cur, since: found?.version ?? 0 });
121 if (!found) break;
122 cur = found.from;
123 below = found.version;
124 }
125 return out;
126}
128/**
129 * The files whose lines can be in `path` (#43): itself, and any file a move
130 * shares with one already found. A line is in a file other than the one it
131 * was written in only when a move carried it, or a line before it, there;
132 * that move names both files.
133 */
134function moveSources(g: Graph, path: string): Set<string> {
135 const out = new Set([path]);
136 const movers = g.movers().map((id) => g.filesOf(id));
137 for (let grew = true; grew; ) {
138 grew = false;
139 for (const files of movers) {
140 if (!files.some((f) => out.has(f))) continue;
141 for (const f of files) if (!out.has(f)) out.add(f), (grew = true);
142 }
143 }
144 return out;
145}
147/**
148 * Shared's versions in one small answer, for the website's version ribbon
149 * (#43): `authors[v]` indexes `agents`, which are in order of each agent's
150 * first version. No files, so it costs nothing on a long history.
151 */
152export function ribbon(host: LogHost): { version: number; agents: string[]; authors: number[] } {
153 host.needInit();
154 const shared = host.shared();
155 const agents: string[] = [];
156 const index = new Map<string, number>();
157 const authors = shared.versions.map((p) => {
158 let i = index.get(p.agent);
159 if (i === undefined) index.set(p.agent, (i = agents.push(p.agent) - 1));
160 return i;
161 });
162 return { version: last(shared), agents, authors };
163}
165/** One version of shared (or of a named draft): who, why, and each file it changed, before and after. */
166export function show(
167 host: LogHost,
168 version: number,
169 options: { draft?: string | null; agent?: string } = {},
170): ShowAnswer {
171 host.needInit();
172 const space = host.spaceFor(options);
173 if (!Number.isInteger(version) || version < 0 || version > last(space)) {
174 throw new ProjectError(
175 "bad-request",
176 `there is no version ${String(version)}${space.name === SHARED ? "" : ` of draft ${space.name}`}`,
177 );
178 }
179 const entry = logEntry(host, space, version);
180 const before = version === 0 ? new View(host.graph, []) : host.viewAt(space, version - 1);
181 const after = host.viewAt(space, version);
182 return {
183 ...entry,
184 draft: space.name === SHARED ? null : space.name,
185 diff: changesBetween(before, after, entry.files),
186 };
187}
189function logEntry(host: LogHost, space: Space, version: number, views?: Map<number, ReadView>): LogEntry {
190 const p = space.versions[version]!;
191 let changes = 0;
192 for (const id of p.atoms) {
193 const atom = host.graph.atom(id);
194 const ownLines = atom.inserts.length > 0 || host.graph.deleteCount(id) > 0;
195 if (ownLines) changes++;
196 }
197 const files = host.paths.versionFiles(space, version, views);
198 const change = host.versionChange(space, version);
199 const droppedIn = p.atoms.length > 0 ? space.droppedIn.get(p.atoms[p.atoms.length - 1]!) : undefined;
200 return {
201 version,
202 agent: p.agent,
203 message: p.message,
204 time: p.time,
205 changes,
206 ...(change !== undefined ? { change } : {}),
207 files: [...files],
208 ...(p.droppedVersions ? { drops: [...p.droppedVersions] } : {}),
209 ...(droppedIn !== undefined ? { droppedIn } : {}),
210 ...labelsOn(host, space, version),
211 ...(p.originalMessage !== undefined ? { originalMessage: p.originalMessage } : {}),
212 };
213}
215function labelsOn(host: LogHost, space: Space, version: number): { labels?: string[] } {
216 if (space.name !== SHARED) return {};
217 const names = [...host.labels]
218 .filter(([, vs]) => vs.includes(version))
219 .map(([n]) => n)
220 .toSorted();
221 return names.length > 0 ? { labels: names } : {};
222}
224/** A file as it was at a version of shared (or a named draft): its text, or null when it didn't exist. */
225export function fileAt(
226 host: LogHost,
227 path: string,
228 version: number,
229 options: { draft?: string | null; agent?: string } = {},
230): { path: string; version: number; text: string | null; blob?: string } {
231 host.needInit();
232 checkReadPath(path);
233 const space = host.spaceFor(options);
234 host.checkVersion(space, version);
235 const view = host.viewAt(space, version);
236 // Any spelling reads the stored one (#41, F7).
237 const file = host.spellingIn(view, path);
238 const blob = view.attrs(file).blob;
239 return {
240 path: file,
241 version,
242 text: view.hasFile(file) ? view.text(file) : null,
243 ...(blob !== null ? { blob } : {}),
244 };
245}
247/**
248 * fileAt for exactly this spelling, from the file's own lines (abso Capacity, 2026-10-17): no spelling lookup and no
249 * list of the version's files. fileAt's hasFile answers from the version's whole path list, folded from version 0
250 * when nothing's kept, and a spelling it doesn't find reads every known path. A name nothing ever wrote is one read.
251 * The pipeline's project files (abso.yml and the like) are read this way on every tick.
252 */
253export function fileExactAt(
254 host: LogHost,
255 path: string,
256 version: number,
257 options: { draft?: string | null; agent?: string } = {},
258): { path: string; version: number; text: string | null; blob?: string } {
259 host.needInit();
260 checkReadPath(path);
261 const space = host.spaceFor(options);
262 host.checkVersion(space, version);
263 if (!host.graph.hasAtomsIn(path)) return { path, version, text: null };
264 const view = host.viewAt(space, version);
265 const attrs = view.attrs(path);
266 // hasFile's own test, without its path list: kept while empty, a link, a binary, or a visible line.
267 const exists = attrs.exists || attrs.link !== null || attrs.blob !== null || view.lines(path).length > 0;
268 return {
269 path,
270 version,
271 text: exists ? view.text(path) : null,
272 ...(attrs.blob !== null ? { blob: attrs.blob } : {}),
273 };
274}
276/** The files that may differ between two versions, and both views: compare's and compareSummary's ground. */
277function compared(
278 host: LogHost,
279 from: number,
280 to: number,
281 options: { draft?: string | null; agent?: string },
282): { before: ReadView; after: ReadView; files: Set<string> } {
283 host.needInit();
284 const space = host.spaceFor(options);
285 host.checkVersion(space, from);
286 host.checkVersion(space, to);
287 const before = host.viewAt(space, from);
288 const after = host.viewAt(space, to);
289 const files = new Set<string>();
290 // A stored writer keeps each shared version's changed files (version-files.ts, abso Capacity): every file that
291 // differs between two versions changed in one between them, so those lists are the candidates, not every atom
292 // either set holds.
293 if (space.name === SHARED && host.graph.packedFiles) {
294 for (let v = Math.min(from, to) + 1; v <= Math.max(from, to); v++)
295 for (const file of host.paths.versionFiles(space, v)) files.add(file);
296 return { before, after, files };
297 }
298 const a = host.at(space, from);
299 const b = host.at(space, to);
300 for (const [x, y] of [
301 [a, b],
302 [b, a],
303 ] as const) {
304 for (const file of host.touchedFiles(
305 (function* () {
306 for (const id of x) if (!y.has(id)) yield id;
307 })(),
308 [before, after],
309 ))
310 files.add(file);
311 }
312 return { before, after, files };
313}
315/** Every file that differs between two versions of shared (or a named draft), before (at `from`) and after (at `to`). */
316export function compare(
317 host: LogHost,
318 from: number,
319 to: number,
320 options: { draft?: string | null; agent?: string } = {},
321): { from: number; to: number; files: FileChange[] } {
322 const { before, after, files } = compared(host, from, to, options);
323 return { from, to, files: changesBetween(before, after, files) };
324}
326/** compare's files as each one's size and SHA-256 before and after, not its texts (abso issue 680). */
327export function compareSummary(
328 host: LogHost,
329 from: number,
330 to: number,
331 options: { draft?: string | null; agent?: string } = {},
332): { from: number; to: number; summary: true; files: ReturnType<typeof summariesBetween> } {
333 const { before, after, files } = compared(host, from, to, options);
334 return { from, to, summary: true, files: summariesBetween(before, after, files) };
335}
337/** A version of shared (or of a named draft) as a ReadView, for reading lines (blame at a version). */
338export function viewAt(
339 host: LogHost,
340 version: number,
341 options: { draft?: string | null; agent?: string } = {},
342): ReadView {
343 host.needInit();
344 const space = host.spaceFor(options);
345 host.checkVersion(space, version);
346 return host.viewAt(space, version);
347}