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/history.ts?version=258&format=json
351 linesFirst import 85% · capacity 14% · codex-capacity-chair 1%Last change capacity in version 258, 8 versions changed it

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

1// History reads for the Files pages (#41; #46, disk-first 1): a folder at a version (tree), a path's life
2// (pathLife), and where lines went (follow). They read through HistoryHost, the one seam the derived tables
3// (abso issues 48 and 79) plug into: today the host is the project in memory.
4
5import type { AtomId, Graph, LineId, ReadView } from "../../src/engine/index.ts";
6import { pathKey } from "./pathkey.ts";
7import { checkReadPath, differs, type LastChange, ProjectError, utf8Bytes } from "./project.ts";
8import { last, SHARED, type Space } from "./spaces.ts";
9import { authorsInView, type FileTotals } from "./version-files.ts";
11/** What the history reads need from a project. */
12export interface HistoryHost {
13 readonly graph: Graph;
14 readonly agents: ReadonlyMap<string, { readonly name: string; readonly space: string; readonly base: number }>;
15 readonly spaces: ReadonlyMap<string, Space>;
16 needInit(): void;
17 spaceNamed(name: string | null): Space;
18 checkVersion(space: Space, version: number): void;
19 viewAt(space: Space, version: number): ReadView;
20 /** A version's file list from kept version shapes, when every version up to it has one (pathindex.ts). */
21 keptPaths?(space: Space, version: number): string[] | undefined;
22 /**
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.
25 */
26 authorTotals?(
27 space: Space,
28 version: number,
29 agent?: { readonly name: string; readonly space: string; readonly base: number },
30 ): ((path: string) => FileTotals | undefined) | undefined;
31 /**
32 * The files an agent's draft can make differ from its base (collisions.ts, touchedBeyond): every other file is the
33 * same in both. Undefined when that isn't known (a draft that moves lines): compare every file.
34 */
35 agentTouched?(agent: {
36 readonly name: string;
37 readonly space: string;
38 readonly base: number;
39 }): ReadonlySet<string> | undefined;
40 agentView(agent: { readonly name: string; readonly space: string; readonly base: number }): ReadView;
41 spellingIn(view: ReadView, asked: string): string;
42 pathsOf(space: Space): Map<string, PathLifeRecord>;
43 lastChange(space: Space, version: number): LastChange;
44 atOrBefore(list: readonly number[], version: number): number | undefined;
45 removedIn(space: Space, from: number, to: number, line: LineId): { removed?: number };
48/** A path's history in a space, as the path index keeps it. */
49export interface PathLifeRecord {
50 readonly changed: number[];
51 readonly events: { version: number; made: boolean; other?: string }[];
54/**
55 * One folder of shared (or a named draft) at a version (#41, F1, F2, F34):
56 * the folders and files directly in it, each with the last version that
57 * changed it (for a folder, any file under it), who made it, when and why.
58 * The root is folder "". A folder is matched by any spelling (#49).
59 *
60 * `agent` (#41, F25): the folder as that agent has it, unpublished changes
61 * and all; `last` is as of the version it last synced to, and `changed`
62 * marks an entry its unpublished work touches. `authors` (F33): each
63 * entry's lines by who wrote them, as blame ?folder= gives a whole folder,
64 * and its text's size in UTF-8 bytes (`bytes`, Sizes in a folder) with the
65 * blobs of its binary files (`blobs`), which the Worker turns into bytes.
66 */
67export function tree(
68 h: HistoryHost,
69 folder = "",
70 options: { version?: number; draft?: string | null; agent?: string; authors?: boolean } = {},
71): {
72 version: number;
73 draft: string | null;
74 agent?: string;
75 folder: string;
76 entries: {
77 name: string;
78 path: string;
79 kind: "folder" | "file";
80 files?: number;
81 last: LastChange | null;
82 changed?: true;
83 authors?: { agent: string; lines: number }[];
84 bytes?: number;
85 blobs?: string[];
86 }[];
87} {
88 h.needInit();
89 const agent = options.agent !== undefined ? h.agents.get(options.agent) : undefined;
90 if (options.agent !== undefined) {
91 if (!agent) throw new ProjectError("bad-request", `there is no agent ${JSON.stringify(options.agent)}`);
92 if (options.version !== undefined || (options.draft ?? null) !== null)
93 throw new ProjectError("bad-request", "an agent's view is its current one: give agent alone");
94 }
95 const space = agent ? h.spaces.get(agent.space)! : h.spaceNamed(options.draft ?? null);
96 const version = agent ? agent.base : (options.version ?? last(space));
97 h.checkVersion(space, version);
98 const want = folder.split("/").filter((s) => s !== "" && s !== ".");
99 const wantKey = pathKey(want.join("/"));
100 const base = h.viewAt(space, version);
101 const view = agent ? h.agentView(agent) : base;
102 const graph = h.graph;
103 const index = h.pathsOf(space);
104 const lastOf = (path: string): number | undefined => h.atOrBefore(index.get(path)?.changed ?? [], version);
105 type Entry = {
106 name: string;
107 path: string;
108 kind: "folder" | "file";
109 files: number;
110 last: number | undefined;
111 changed: boolean;
112 authors?: Map<string, number>;
113 bytes: number;
114 blobs?: string[];
115 };
116 const entries = new Map<string, Entry>();
117 let stored: string | undefined;
118 const entryOf = (path: string): Entry | undefined => {
119 const parts = path.split("/");
120 if (parts.length <= want.length || (want.length > 0 && pathKey(parts.slice(0, want.length).join("/")) !== wantKey))
121 return undefined;
122 stored ??= parts.slice(0, want.length).join("/");
123 const name = parts[want.length]!;
124 const kind = parts.length === want.length + 1 ? "file" : "folder";
125 const key = `${kind} ${kind === "folder" ? pathKey(name) : name}`;
126 let e = entries.get(key);
127 if (!e)
128 entries.set(
129 key,
130 (e = {
131 name,
132 path: parts.slice(0, want.length + 1).join("/"),
133 kind,
134 files: 0,
135 last: undefined,
136 changed: false,
137 bytes: 0,
138 }),
139 );
140 return e;
141 };
142 const paths = (agent ? undefined : h.keptPaths?.(space, version)) ?? view.paths();
143 // Each atom's author once (abso Capacity): on a stored writer, graph.atom reads and parses the whole atom, and
144 // most lines share a few big atoms.
145 const authors = new Map<AtomId, string>();
146 const authorOf = (id: AtomId): string => {
147 const known = authors.get(id);
148 if (known !== undefined) return known;
149 const author = graph.atom(id).author;
150 authors.set(id, author);
151 return author;
152 };
153 // Each file's lines by author and size kept as versions land (abso Capacity), for the files it can answer.
154 const totalsOf = options.authors ? h.authorTotals?.(space, version, agent) : undefined;
155 // Only a file the draft touches can differ from its base (abso Capacity): comparing every file laid each out in both
156 // views, 13 s of an agent's tree on abso.
157 const touchedByDraft = agent ? h.agentTouched?.(agent) : undefined;
158 for (const path of paths) {
159 const e = entryOf(path);
160 if (!e) continue;
161 e.files++;
162 const v = lastOf(path);
163 if (v !== undefined && (e.last === undefined || v > e.last)) e.last = v;
164 if (agent && (touchedByDraft === undefined || touchedByDraft.has(path)) && differs(base, view, path))
165 e.changed = true;
166 if (options.authors) {
167 const counts = (e.authors ??= new Map());
168 const blob = view.attrs(path).blob;
169 const totals = totalsOf?.(path);
170 if (totals) {
171 for (const [author, n] of totals.authors) counts.set(author, (counts.get(author) ?? 0) + n);
172 if (blob === null) e.bytes += totals.bytes;
173 } else {
174 for (const [author, n] of authorsInView(view, path, graph, authorOf))
175 counts.set(author, (counts.get(author) ?? 0) + n);
176 if (blob === null) e.bytes += utf8Bytes(view.text(path));
177 }
178 if (blob !== null) (e.blobs ??= []).push(blob);
179 }
180 }
181 // A file the agent took out still marks the folder it was in, when that folder is still listed.
182 if (agent)
183 for (const path of base.paths())
184 if (!view.hasFile(path)) {
185 const parts = path.split("/");
186 if (parts.length <= want.length + 1) continue;
187 const key = `folder ${pathKey(parts[want.length]!)}`;
188 const e = entries.get(key);
189 if (e && (want.length === 0 || pathKey(parts.slice(0, want.length).join("/")) === wantKey)) e.changed = true;
190 }
191 if (want.length > 0 && stored === undefined) {
192 throw new ProjectError(
193 "not-found",
194 `there is no folder ${want.join("/")} in ${space.name === SHARED ? "shared" : `draft ${space.name}`} at version ${version}`,
195 );
196 }
197 const list = [...entries.values()].sort((a, b) =>
198 a.kind !== b.kind ? (a.kind === "folder" ? -1 : 1) : a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
199 );
200 return {
201 version,
202 draft: space.name === SHARED ? null : space.name,
203 ...(agent ? { agent: agent.name } : {}),
204 folder: stored ?? "",
205 entries: list.map((e) => ({
206 name: e.name,
207 path: e.path,
208 kind: e.kind,
209 ...(e.kind === "folder" ? { files: e.files } : {}),
210 last: e.last === undefined ? null : h.lastChange(space, e.last),
211 ...(e.changed ? { changed: true as const } : {}),
212 ...(e.authors
213 ? {
214 authors: [...e.authors]
215 .map(([agent, lines]) => ({ agent, lines }))
216 .sort((a, b) => b.lines - a.lines || (a.agent < b.agent ? -1 : 1)),
217 }
218 : {}),
219 ...(options.authors ? { bytes: e.bytes } : {}),
220 ...(e.blobs ? { blobs: e.blobs } : {}),
221 })),
222 };
225/**
226 * A path's life at a version (#41, F10, F30): whether it exists, how many
227 * versions changed it, its last change, and the version that made it (and
228 * the name it was moved from). When it doesn't exist: the version that
229 * took it out (and where it was moved to), and the next version that makes
230 * it again, if any.
231 */
232export function pathLife(
233 h: HistoryHost,
234 path: string,
235 options: { version?: number; draft?: string | null } = {},
236): {
237 path: string;
238 version: number;
239 exists: boolean;
240 versions: number;
241 last: LastChange | null;
242 made?: LastChange & { from?: string };
243 removed?: LastChange & { to?: string };
244 next?: LastChange & { from?: string };
245} {
246 h.needInit();
247 checkReadPath(path);
248 const space = h.spaceNamed(options.draft ?? null);
249 const version = options.version ?? last(space);
250 h.checkVersion(space, version);
251 const index = h.pathsOf(space);
252 let stored = path;
253 if (!index.has(path)) {
254 const twins = [...index.keys()].filter((p) => pathKey(p) === pathKey(path));
255 if (twins.length === 1) stored = twins[0]!;
256 }
257 const life = index.get(stored);
258 const exists = h.viewAt(space, version).hasFile(stored);
259 const count = life ? life.changed.filter((v) => v <= version).length : 0;
260 const lastV = life ? h.atOrBefore(life.changed, version) : undefined;
261 const event = (e: { version: number; other?: string }, key: "from" | "to") => ({
262 ...h.lastChange(space, e.version),
263 ...(e.other !== undefined ? { [key]: e.other } : {}),
264 });
265 const before = (life?.events ?? []).filter((e) => e.version <= version);
266 const made = exists ? before.filter((e) => e.made).pop() : undefined;
267 const removed = !exists ? before.filter((e) => !e.made).pop() : undefined;
268 const next = !exists ? (life?.events ?? []).find((e) => e.made && e.version > version) : undefined;
269 return {
270 path: stored,
271 version,
272 exists,
273 versions: count,
274 last: lastV === undefined ? null : h.lastChange(space, lastV),
275 ...(made ? { made: event(made, "from") } : {}),
276 ...(removed ? { removed: event(removed, "to") } : {}),
277 ...(next ? { next: event(next, "from") } : {}),
278 };
281/**
282 * Lines followed from one version to another (#41, F15): lines from..to of
283 * `path` at `version`, found by their ids at version `at` (the newest by
284 * default), in whatever file they are in now. A line that's gone says which
285 * version took it out.
286 */
287export function follow(
288 h: HistoryHost,
289 path: string,
290 version: number,
291 from: number,
292 to: number,
293 options: { at?: number; draft?: string | null } = {},
294): {
295 path: string;
296 version: number;
297 from: number;
298 to: number;
299 at: number;
300 lines: { was: number; file?: string; now: number | null; removed?: number }[];
301 ranges: { file: string; from: number; to: number }[];
302 removed: { from: number; to: number; version: number }[];
303} {
304 h.needInit();
305 checkReadPath(path);
306 const space = h.spaceNamed(options.draft ?? null);
307 h.checkVersion(space, version);
308 const at = options.at ?? last(space);
309 h.checkVersion(space, at);
310 if (at < version)
311 throw new ProjectError("bad-request", `follow goes forward: at (${at}) is before version ${version}`);
312 const then = h.viewAt(space, version);
313 const file = h.spellingIn(then, path);
314 if (!then.hasFile(file))
315 throw new ProjectError(
316 "not-found",
317 `${path} isn't in ${space.name === SHARED ? "shared" : `draft ${space.name}`} at version ${version}`,
318 );
319 const lines = then.lines(file);
320 if (lines.length === 0)
321 throw new ProjectError("bad-request", `${file} has no lines to follow (a binary file, or one kept whole)`);
322 if (!Number.isInteger(from) || !Number.isInteger(to) || from < 1 || to < from || to > lines.length) {
323 throw new ProjectError(
324 "bad-request",
325 `${file} has ${lines.length} lines at version ${version}; ask for from..to within 1-${lines.length}`,
326 );
327 }
328 const now = h.viewAt(space, at);
329 const out: { was: number; file?: string; now: number | null; removed?: number }[] = [];
330 for (let n = from; n <= to; n++) {
331 const id = lines[n - 1]!.id;
332 const where = now.fileOf(id);
333 const i = now.lineNumber(where, id);
334 if (i >= 0) out.push({ was: n, file: where, now: i + 1 });
335 else out.push({ was: n, now: null, ...h.removedIn(space, version, at, id) });
336 }
337 const ranges: { file: string; from: number; to: number }[] = [];
338 const removed: { from: number; to: number; version: number }[] = [];
339 for (const l of out) {
340 if (l.now !== null) {
341 const r = ranges[ranges.length - 1];
342 if (r && r.file === l.file && r.to + 1 === l.now) r.to = l.now;
343 else ranges.push({ file: l.file!, from: l.now, to: l.now });
344 } else if (l.removed !== undefined) {
345 const r = removed[removed.length - 1];
346 if (r && r.version === l.removed && r.to + 1 === l.was) r.to = l.was;
347 else removed.push({ from: l.was, to: l.was, version: l.removed });
348 }
349 }
350 return { path: file, version, from, to, at, lines: out, ranges, removed };