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
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/version-log.ts?version=201&format=json
282 linesFirst import 93% · codex-capacity-chair 7%Last change codex-capacity-chair in version 163, 3 versions changed it

You're reading version 201. The newest is 318, and 5 versions since then changed this file. Go to the newestCompare with 318

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.
5
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 } 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>;
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 const entry = logEntry(host, space, v, views);
70 // The walk goes down: a view above this version won't be needed again (#46).
71 views.delete(v + 1);
72 // With a path: only the versions that changed that file (its history), under its name then.
73 if (path === undefined) versions.push(entry);
74 else if (entry.files.includes(nameAt(v))) versions.push(nameAt(v) === path ? entry : { ...entry, path: nameAt(v) });
75 }
76 const next = v >= 0 && versions.length > 0 ? versions[versions.length - 1]!.version : undefined;
77 const movedFrom = names.slice(0, -1).map((n, i) => ({ path: names[i + 1]!.path, version: n.since }));
78 return {
79 draft: space.name === SHARED ? null : space.name,
80 version: last(space),
81 versions,
82 ...(next !== undefined ? { next } : {}),
83 ...(path !== undefined ? { path } : {}),
84 ...(movedFrom.length > 0 ? { movedFrom } : {}),
85 };
88/**
89 * A file's names back through moves (#41, F27), newest first: each with the
90 * version from which it had that name. Found from the atoms that moved lines
91 * into it (abso mv), so it never guesses from how alike two files are.
92 */
93function namesBefore(host: LogHost, space: Space, path: string): { path: string; since: number }[] {
94 const g = host.graph;
95 const out: { path: string; since: number }[] = [];
96 let cur = path;
97 let below = last(space) + 1;
98 const movers = g.movers();
99 for (;;) {
100 // The newest version below `below` that moved another file into `cur`.
101 const into = movers
102 .filter((id) => g.filesOf(id).includes(cur))
103 .map((id) => space.publishedIn.get(id))
104 .filter((v): v is number => v !== undefined && v < below)
105 .toSorted((a, b) => b - a);
106 let found: { from: string; version: number } | undefined;
107 for (const version of into) {
108 const move = host.paths.versionShape(space, version).moves.find(([, to]) => to === cur);
109 if (move) {
110 found = { from: move[0], version };
111 break;
112 }
113 }
114 out.push({ path: cur, since: found?.version ?? 0 });
115 if (!found) break;
116 cur = found.from;
117 below = found.version;
118 }
119 return out;
122/**
123 * The files whose lines can be in `path` (#43): itself, and any file a move
124 * shares with one already found. A line is in a file other than the one it
125 * was written in only when a move carried it, or a line before it, there;
126 * that move names both files.
127 */
128function moveSources(g: Graph, path: string): Set<string> {
129 const out = new Set([path]);
130 const movers = g.movers().map((id) => g.filesOf(id));
131 for (let grew = true; grew; ) {
132 grew = false;
133 for (const files of movers) {
134 if (!files.some((f) => out.has(f))) continue;
135 for (const f of files) if (!out.has(f)) out.add(f), (grew = true);
136 }
137 }
138 return out;
141/**
142 * Shared's versions in one small answer, for the website's version ribbon
143 * (#43): `authors[v]` indexes `agents`, which are in order of each agent's
144 * first version. No files, so it costs nothing on a long history.
145 */
146export function ribbon(host: LogHost): { version: number; agents: string[]; authors: number[] } {
147 host.needInit();
148 const shared = host.shared();
149 const agents: string[] = [];
150 const index = new Map<string, number>();
151 const authors = shared.versions.map((p) => {
152 let i = index.get(p.agent);
153 if (i === undefined) index.set(p.agent, (i = agents.push(p.agent) - 1));
154 return i;
155 });
156 return { version: last(shared), agents, authors };
159/** One version of shared (or of a named draft): who, why, and each file it changed, before and after. */
160export function show(
161 host: LogHost,
162 version: number,
163 options: { draft?: string | null; agent?: string } = {},
164): ShowAnswer {
165 host.needInit();
166 const space = host.spaceFor(options);
167 if (!Number.isInteger(version) || version < 0 || version > last(space)) {
168 throw new ProjectError(
169 "bad-request",
170 `there is no version ${String(version)}${space.name === SHARED ? "" : ` of draft ${space.name}`}`,
171 );
172 }
173 const entry = logEntry(host, space, version);
174 const before = version === 0 ? new View(host.graph, []) : host.viewAt(space, version - 1);
175 const after = host.viewAt(space, version);
176 return {
177 ...entry,
178 draft: space.name === SHARED ? null : space.name,
179 diff: changesBetween(before, after, entry.files),
180 };
183function logEntry(host: LogHost, space: Space, version: number, views?: Map<number, ReadView>): LogEntry {
184 const p = space.versions[version]!;
185 let changes = 0;
186 for (const id of p.atoms) {
187 const atom = host.graph.atom(id);
188 const ownLines = atom.inserts.length > 0 || host.graph.deleteCount(id) > 0;
189 if (ownLines) changes++;
190 }
191 const files = host.paths.versionFiles(space, version, views);
192 const change = host.versionChange(space, version);
193 const droppedIn = p.atoms.length > 0 ? space.droppedIn.get(p.atoms[p.atoms.length - 1]!) : undefined;
194 return {
195 version,
196 agent: p.agent,
197 message: p.message,
198 time: p.time,
199 changes,
200 ...(change !== undefined ? { change } : {}),
201 files: [...files],
202 ...(p.droppedVersions ? { drops: [...p.droppedVersions] } : {}),
203 ...(droppedIn !== undefined ? { droppedIn } : {}),
204 ...labelsOn(host, space, version),
205 ...(p.originalMessage !== undefined ? { originalMessage: p.originalMessage } : {}),
206 };
209function labelsOn(host: LogHost, space: Space, version: number): { labels?: string[] } {
210 if (space.name !== SHARED) return {};
211 const names = [...host.labels]
212 .filter(([, vs]) => vs.includes(version))
213 .map(([n]) => n)
214 .toSorted();
215 return names.length > 0 ? { labels: names } : {};
218/** A file as it was at a version of shared (or a named draft): its text, or null when it didn't exist. */
219export function fileAt(
220 host: LogHost,
221 path: string,
222 version: number,
223 options: { draft?: string | null; agent?: string } = {},
224): { path: string; version: number; text: string | null; blob?: string } {
225 host.needInit();
226 checkReadPath(path);
227 const space = host.spaceFor(options);
228 host.checkVersion(space, version);
229 const view = host.viewAt(space, version);
230 // Any spelling reads the stored one (#41, F7).
231 const file = host.spellingIn(view, path);
232 const blob = view.attrs(file).blob;
233 return {
234 path: file,
235 version,
236 text: view.hasFile(file) ? view.text(file) : null,
237 ...(blob !== null ? { blob } : {}),
238 };
241/** Every file that differs between two versions of shared (or a named draft), before (at `from`) and after (at `to`). */
242export function compare(
243 host: LogHost,
244 from: number,
245 to: number,
246 options: { draft?: string | null; agent?: string } = {},
247): { from: number; to: number; files: FileChange[] } {
248 host.needInit();
249 const space = host.spaceFor(options);
250 host.checkVersion(space, from);
251 host.checkVersion(space, to);
252 const a = host.at(space, from);
253 const b = host.at(space, to);
254 const before = host.viewAt(space, from);
255 const after = host.viewAt(space, to);
256 const files = new Set<string>();
257 for (const [x, y] of [
258 [a, b],
259 [b, a],
260 ] as const) {
261 for (const file of host.touchedFiles(
262 (function* () {
263 for (const id of x) if (!y.has(id)) yield id;
264 })(),
265 [before, after],
266 ))
267 files.add(file);
268 }
269 return { from, to, files: changesBetween(before, after, files) };
272/** A version of shared (or of a named draft) as a ReadView, for reading lines (blame at a version). */
273export function viewAt(
274 host: LogHost,
275 version: number,
276 options: { draft?: string | null; agent?: string } = {},
277): ReadView {
278 host.needInit();
279 const space = host.spaceFor(options);
280 host.checkVersion(space, version);
281 return host.viewAt(space, version);