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=2&format=json
273 linesFirst import 100%Last change First import in version 0, 1 version changed it

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

1// The version list reads: log (versions newest first, or one file's history), ribbon (who published each version),
2// show (one version's changes), fileAt, compare and viewAt. They read through LogHost; today the host is the project
3// in memory.
4
5import { type AtomId, type Graph, View } from "../../src/engine/index.ts";
6import { checkReadPath } from "./path-rules.ts";
7import type { PathIndex } from "./pathindex.ts";
8import { ProjectError } from "./project-error.ts";
9import type { FileChange, LogAnswer, LogEntry, ShowAnswer } from "./project-types.ts";
10import { last, SHARED, type Space } from "./spaces.ts";
11import { changesBetween } from "./view-changes.ts";
13/** What the version list reads need from a project. */
14export interface LogHost {
15 readonly graph: Graph;
16 readonly paths: Pick<PathIndex, "knowsFiles" | "versionFiles" | "versionShape">;
17 readonly labels: ReadonlyMap<string, readonly number[]>;
18 shared(): Space;
19 needInit(): void;
20 spaceFor(options: { draft?: string | null; agent?: string }): Space;
21 checkVersion(space: Space, version: number): void;
22 viewAt(space: Space, version: number): View;
23 view(set: ReadonlySet<AtomId>): View;
24 at(space: Space, version: number): ReadonlySet<AtomId>;
25 spellingIn(view: View, path: string): string;
26 versionChange(space: Space, version: number): number | undefined;
27 touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;
30/**
31 * The versions of shared (or of a named draft), newest first: who
32 * published each, why, and which files it touched. `limit` defaults to 50.
33 */
34export function log(
35 host: LogHost,
36 options: {
37 draft?: string | null;
38 agent?: string;
39 limit?: number;
40 path?: string;
41 before?: number;
42 follow?: boolean;
43 } = {},
44): LogAnswer {
45 host.needInit();
46 const space = host.spaceFor(options);
47 const limit = Number.isInteger(options.limit) && options.limit! > 0 ? Math.min(options.limit!, 200) : 50;
48 if (options.path !== undefined) checkReadPath(options.path);
49 if (options.before !== undefined && (!Number.isInteger(options.before) || options.before < 0))
50 throw new ProjectError("bad-request", "before must be a version number");
51 const versions: LogEntry[] = [];
52 // Paging (#41, the website): `before` starts the page below that version; `next` is the `before` for the page after.
53 let v = Math.min(last(space), (options.before ?? last(space) + 1) - 1);
54 // Any spelling reads the stored one (#41, F7). With follow, the history follows moves (F27): before
55 // the version that moved the file in, it's the file it came from.
56 const path = options.path === undefined ? undefined : host.spellingIn(host.viewAt(space, last(space)), options.path);
57 const names = path === undefined ? [] : options.follow ? namesBefore(host, space, path) : [{ path, since: 0 }];
58 const nameAt = (version: number): string => names.find((n) => version >= n.since)?.path ?? path!;
59 // A file's history (#43): only versions whose changes name the file, or a file moves can carry lines from, can have changed it.
60 const sources = path === undefined ? undefined : new Set(names.flatMap((n) => [...moveSources(host.graph, n.path)]));
61 const g = host.graph;
62 const views = new Map<number, View>();
63 for (; v >= 0 && versions.length < limit; v--) {
64 if (sources && !host.paths.knowsFiles(space, v)) {
65 const p = space.versions[v]!;
66 if (![...p.atoms, ...(p.dropped ?? [])].some((id) => g.filesOf(id).some((f) => sources.has(f)))) continue;
67 }
68 const entry = logEntry(host, space, v, views);
69 // The walk goes down: a view above this version won't be needed again (#46).
70 views.delete(v + 1);
71 // With a path: only the versions that changed that file (its history), under its name then.
72 if (path === undefined) versions.push(entry);
73 else if (entry.files.includes(nameAt(v))) versions.push(nameAt(v) === path ? entry : { ...entry, path: nameAt(v) });
74 }
75 const next = v >= 0 && versions.length > 0 ? versions[versions.length - 1]!.version : undefined;
76 const movedFrom = names.slice(0, -1).map((n, i) => ({ path: names[i + 1]!.path, version: n.since }));
77 return {
78 draft: space.name === SHARED ? null : space.name,
79 version: last(space),
80 versions,
81 ...(next !== undefined ? { next } : {}),
82 ...(path !== undefined ? { path } : {}),
83 ...(movedFrom.length > 0 ? { movedFrom } : {}),
84 };
87/**
88 * A file's names back through moves (#41, F27), newest first: each with the
89 * version from which it had that name. Found from the atoms that moved lines
90 * into it (abso mv), so it never guesses from how alike two files are.
91 */
92function namesBefore(host: LogHost, space: Space, path: string): { path: string; since: number }[] {
93 const g = host.graph;
94 const out: { path: string; since: number }[] = [];
95 let cur = path;
96 let below = last(space) + 1;
97 const movers = g.movers();
98 for (;;) {
99 // The newest version below `below` that moved another file into `cur`.
100 const into = movers
101 .filter((id) => g.filesOf(id).includes(cur))
102 .map((id) => space.publishedIn.get(id))
103 .filter((v): v is number => v !== undefined && v < below)
104 .toSorted((a, b) => b - a);
105 let found: { from: string; version: number } | undefined;
106 for (const version of into) {
107 const move = host.paths.versionShape(space, version).moves.find(([, to]) => to === cur);
108 if (move) {
109 found = { from: move[0], version };
110 break;
111 }
112 }
113 out.push({ path: cur, since: found?.version ?? 0 });
114 if (!found) break;
115 cur = found.from;
116 below = found.version;
117 }
118 return out;
121/**
122 * The files whose lines can be in `path` (#43): itself, and any file a move
123 * shares with one already found. A line is in a file other than the one it
124 * was written in only when a move carried it, or a line before it, there;
125 * that move names both files.
126 */
127function moveSources(g: Graph, path: string): Set<string> {
128 const out = new Set([path]);
129 const movers = g.movers().map((id) => g.filesOf(id));
130 for (let grew = true; grew; ) {
131 grew = false;
132 for (const files of movers) {
133 if (!files.some((f) => out.has(f))) continue;
134 for (const f of files) if (!out.has(f)) out.add(f), (grew = true);
135 }
136 }
137 return out;
140/**
141 * Shared's versions in one small answer, for the website's version ribbon
142 * (#43): `authors[v]` indexes `agents`, which are in order of each agent's
143 * first version. No files, so it costs nothing on a long history.
144 */
145export function ribbon(host: LogHost): { version: number; agents: string[]; authors: number[] } {
146 host.needInit();
147 const shared = host.shared();
148 const agents: string[] = [];
149 const index = new Map<string, number>();
150 const authors = shared.versions.map((p) => {
151 let i = index.get(p.agent);
152 if (i === undefined) index.set(p.agent, (i = agents.push(p.agent) - 1));
153 return i;
154 });
155 return { version: last(shared), agents, authors };
158/** One version of shared (or of a named draft): who, why, and each file it changed, before and after. */
159export function show(
160 host: LogHost,
161 version: number,
162 options: { draft?: string | null; agent?: string } = {},
163): ShowAnswer {
164 host.needInit();
165 const space = host.spaceFor(options);
166 if (!Number.isInteger(version) || version < 0 || version > last(space)) {
167 throw new ProjectError(
168 "bad-request",
169 `there is no version ${String(version)}${space.name === SHARED ? "" : ` of draft ${space.name}`}`,
170 );
171 }
172 const entry = logEntry(host, space, version);
173 const before = version === 0 ? new View(host.graph, []) : host.viewAt(space, version - 1);
174 const after = host.viewAt(space, version);
175 return {
176 ...entry,
177 draft: space.name === SHARED ? null : space.name,
178 diff: changesBetween(before, after, entry.files),
179 };
182function logEntry(host: LogHost, space: Space, version: number, views?: Map<number, View>): LogEntry {
183 const p = space.versions[version]!;
184 let changes = 0;
185 for (const id of p.atoms) {
186 const atom = host.graph.atom(id);
187 const ownLines = atom.inserts.length > 0 || host.graph.deleteCount(id) > 0;
188 if (ownLines) changes++;
189 }
190 const files = host.paths.versionFiles(space, version, views);
191 const change = host.versionChange(space, version);
192 const droppedIn = p.atoms.length > 0 ? space.droppedIn.get(p.atoms[p.atoms.length - 1]!) : undefined;
193 return {
194 version,
195 agent: p.agent,
196 message: p.message,
197 time: p.time,
198 changes,
199 ...(change !== undefined ? { change } : {}),
200 files: [...files],
201 ...(p.droppedVersions ? { drops: [...p.droppedVersions] } : {}),
202 ...(droppedIn !== undefined ? { droppedIn } : {}),
203 ...labelsOn(host, space, version),
204 ...(p.originalMessage !== undefined ? { originalMessage: p.originalMessage } : {}),
205 };
208function labelsOn(host: LogHost, space: Space, version: number): { labels?: string[] } {
209 if (space.name !== SHARED) return {};
210 const names = [...host.labels]
211 .filter(([, vs]) => vs.includes(version))
212 .map(([n]) => n)
213 .toSorted();
214 return names.length > 0 ? { labels: names } : {};
217/** A file as it was at a version of shared (or a named draft): its text, or null when it didn't exist. */
218export function fileAt(
219 host: LogHost,
220 path: string,
221 version: number,
222 options: { draft?: string | null; agent?: string } = {},
223): { path: string; version: number; text: string | null; blob?: string } {
224 host.needInit();
225 checkReadPath(path);
226 const space = host.spaceFor(options);
227 host.checkVersion(space, version);
228 const view = host.viewAt(space, version);
229 // Any spelling reads the stored one (#41, F7).
230 const file = host.spellingIn(view, path);
231 const blob = view.attrs(file).blob;
232 return {
233 path: file,
234 version,
235 text: view.hasFile(file) ? view.text(file) : null,
236 ...(blob !== null ? { blob } : {}),
237 };
240/** Every file that differs between two versions of shared (or a named draft), before (at `from`) and after (at `to`). */
241export function compare(
242 host: LogHost,
243 from: number,
244 to: number,
245 options: { draft?: string | null; agent?: string } = {},
246): { from: number; to: number; files: FileChange[] } {
247 host.needInit();
248 const space = host.spaceFor(options);
249 host.checkVersion(space, from);
250 host.checkVersion(space, to);
251 const a = host.at(space, from);
252 const b = host.at(space, to);
253 const files = new Set<string>();
254 for (const [x, y] of [
255 [a, b],
256 [b, a],
257 ] as const) {
258 for (const file of host.touchedFiles(
259 [...x].filter((id) => !y.has(id)),
260 [host.view(a), host.view(b)],
261 ))
262 files.add(file);
263 }
264 return { from, to, files: changesBetween(host.view(a), host.view(b), files) };
267/** A version of shared (or of a named draft) as a View, for reading lines (blame at a version). */
268export function viewAt(host: LogHost, version: number, options: { draft?: string | null; agent?: string } = {}): View {
269 host.needInit();
270 const space = host.spaceFor(options);
271 host.checkVersion(space, version);
272 return host.viewAt(space, version);