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/pathindex.ts?format=json
304 linesFirst import 61% · capacity 35% · codex-capacity-chair 4%Last change capacity in version 316, 8 versions changed it
1import { View } from "../../src/engine/index.ts";
2// Each version's files and shape, and each path's history in a space (#41; #46, disk-first 1). Built the first
3// time it's asked for and kept up as versions land; a version's files are also kept in the shapes table, so a
4// load needn't lay them out again.
5
6import type { AtomId, Graph, LineId, ReadView } from "../../src/engine/index.ts";
7import {
8 differs,
9 type LastChange,
10 type PathLife,
11 type ProjectOptions,
12 shapeStamp,
13 type VersionShape,
14} from "./project.ts";
15import { last, SHARED, type Space } from "./spaces.ts";
17/** What the path index needs from a project. */
18export interface PathIndexDeps {
19 readonly graph: Graph;
20 at(space: Space, version: number): ReadonlySet<AtomId>;
21 viewAt(space: Space, version: number): ReadView;
22 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;
23 readonly shapes: ProjectOptions["shapes"];
26/** Older versions' file lists kept per space (a few agents' bases): about 1,000 paths each on abso. */
27const OLDER_PATH_SETS = 8;
29export class PathIndex {
30 readonly #deps: PathIndexDeps;
32 constructor(deps: PathIndexDeps) {
33 this.#deps = deps;
34 }
36 /** Whether a version's files are already worked out (a file's log skips versions that can't name it, #43). */
37 knowsFiles(space: Space, version: number): boolean {
38 return this.#filesCache.get(space)?.has(version) ?? false;
39 }
41 /** Each version's changed files (#43): a published version never changes, so they're worked out once. */
42 readonly #filesCache = new WeakMap<Space, Map<number, string[]>>();
44 /** Each version's files made and taken out, and moves (#41), worked out with its files. */
45 readonly #shapeCache = new WeakMap<Space, Map<number, VersionShape>>();
47 /** The files at the newest version folded so far from kept shapes, per space (keptPaths). */
48 readonly #keptPaths = new WeakMap<Space, { version: number; files: Set<string> }>();
49 /** The files at a few older versions, worked back from the newest (agents' bases, mostly), most recent last. */
50 readonly #olderPaths = new WeakMap<Space, Map<number, Set<string>>>();
52 /**
53 * The files at a version older than the newest folded (abso Capacity): the newest's files with each version since
54 * undone, its made files taken out and its gone ones put back. Folding from version 0 again for an agent's base
55 * read every version's kept row on each call.
56 */
57 #olderPathSet(
58 space: Space,
59 fold: { version: number; files: Set<string> },
60 version: number,
61 ): ReadonlySet<string> | undefined {
62 let older = this.#olderPaths.get(space);
63 if (!older) this.#olderPaths.set(space, (older = new Map()));
64 const known = older.get(version);
65 if (known) {
66 older.delete(version);
67 older.set(version, known);
68 return known;
69 }
70 const files = new Set(fold.files);
71 for (let v = fold.version; v > version; v--) {
72 if (!this.#keptFiles(space, v)) return undefined;
73 const shape = this.#shapeCache.get(space)?.get(v);
74 if (!shape) return undefined;
75 for (const f of shape.made) files.delete(f);
76 for (const f of shape.gone) files.add(f);
77 }
78 older.set(version, files);
79 if (older.size > OLDER_PATH_SETS) older.delete(older.keys().next().value!);
80 return files;
81 }
83 /**
84 * The files a space has at a version, from the kept rows of every version up to it: each one's files made and
85 * taken out, folded from version 0 (abso Capacity). Undefined when a version has no kept row that describes it;
86 * then the caller lays the version out.
87 */
88 keptPaths(space: Space, version: number): string[] | undefined {
89 const files = this.keptPathSet(space, version);
90 return files && [...files].sort();
91 }
93 /**
94 * The files the versions after `from`, up to `to`, changed, from each version's kept row (abso 738): undefined when one
95 * has none, and the caller works them out from the atoms. Every file whose text differs between the two versions is
96 * in it, since some version between changed it.
97 */
98 keptChangesSince(space: Space, from: number, to: number): ReadonlySet<string> | undefined {
99 const out = new Set<string>();
100 for (let v = from + 1; v <= to; v++) {
101 const files = this.#keptFiles(space, v);
102 if (!files) return undefined;
103 for (const f of files) out.add(f);
104 }
105 return out;
106 }
108 /** keptPaths as a set, kept for the newest version asked: never changed once made, so a view can hold it. */
109 keptPathSet(space: Space, version: number): ReadonlySet<string> | undefined {
110 let fold = this.#keptPaths.get(space);
111 if (fold?.version === version) return fold.files;
112 if (fold && fold.version > version) return this.#olderPathSet(space, fold, version);
113 if (!fold) fold = { version: -1, files: new Set() };
114 const files = new Set(fold.files);
115 for (let v = fold.version + 1; v <= version; v++) {
116 if (!this.#keptFiles(space, v)) return undefined;
117 // Not yet when the version is still being kept (keep asks its views which files exist).
118 const shape = this.#shapeCache.get(space)?.get(v);
119 if (!shape) return undefined;
120 for (const f of shape.gone) files.delete(f);
121 for (const f of shape.made) files.add(f);
122 }
123 this.#keptPaths.set(space, { version, files });
124 return files;
125 }
127 /** Each space's path index (#41): built the first time it's asked for, then kept up as versions land. */
128 readonly #pathIndex = new WeakMap<Space, { upTo: number; paths: Map<string, PathLife> }>();
130 /** The files a version changed. `views` holds views by version, so a run of versions builds each once. */
131 versionFiles(space: Space, version: number, views = new Map<number, ReadView>()): string[] {
132 let cache = this.#filesCache.get(space);
133 if (!cache) this.#filesCache.set(space, (cache = new Map()));
134 const known = cache.get(version);
135 if (known) return known;
136 const kept = this.#keptFiles(space, version);
137 if (kept) return kept;
138 const shapes = this.#shapeCache.get(space)!;
139 const p = space.versions[version]!;
140 const viewAt = (v: number): ReadView => {
141 let view = views.get(v);
142 if (!view) views.set(v, (view = v < 0 ? new View(this.#deps.graph, []) : this.#deps.viewAt(space, v)));
143 return view;
144 };
145 const before = viewAt(version - 1);
146 const after = viewAt(version);
147 // Net changes only, as show has them: a file made and removed again before the publish isn't one (#37).
148 const touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]);
149 const files = [...touched].filter((path) => differs(before, after, path)).sort();
150 this.keep(space, version, files, before, after);
151 return files;
152 }
154 /**
155 * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the
156 * version landed (version-files.ts, abso Capacity), which says which files existed before and after.
157 */
158 keep(
159 space: Space,
160 version: number,
161 files: string[],
162 before: ReadView,
163 after: ReadView,
164 exists?: ReadonlyMap<string, readonly [boolean, boolean]>,
165 ): void {
166 let cache = this.#filesCache.get(space);
167 if (!cache) this.#filesCache.set(space, (cache = new Map()));
168 let shapes = this.#shapeCache.get(space);
169 if (!shapes) this.#shapeCache.set(space, (shapes = new Map()));
170 const p = space.versions[version]!;
171 cache.set(version, files);
172 // Files made and taken out, and which of those are one file moved (#41): an atom that moves lines
173 // names both files, and a binary or empty file moved keeps its blob.
174 const had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f);
175 const has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f);
176 const made = files.filter((f) => !had(f) && has(f));
177 const gone = files.filter((f) => had(f) && !has(f));
178 const moves: [string, string][] = [];
179 if (made.length > 0 && gone.length > 0) {
180 const movers = new Set(this.#deps.graph.movers());
181 const moving = p.atoms.filter((id) => movers.has(id)).map((id) => this.#deps.graph.filesOf(id));
182 const taken = new Set<string>();
183 for (const to of made) {
184 const blob = after.attrs(to).blob;
185 const from = gone.find(
186 (g) =>
187 !taken.has(g) &&
188 (moving.some((fs) => fs.includes(g) && fs.includes(to)) ||
189 (blob !== null && before.attrs(g).blob === blob)),
190 );
191 if (from !== undefined) taken.add(from), moves.push([from, to]);
192 }
193 }
194 shapes.set(version, { made, gone, moves });
195 this.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves }));
196 }
198 /** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */
199 #keptFiles(space: Space, version: number): string[] | undefined {
200 let cache = this.#filesCache.get(space);
201 if (!cache) this.#filesCache.set(space, (cache = new Map()));
202 let shapes = this.#shapeCache.get(space);
203 if (!shapes) this.#shapeCache.set(space, (shapes = new Map()));
204 const known = cache.get(version);
205 if (known) return known;
206 const row = this.#deps.shapes?.get(space.name, version);
207 if (row === undefined) return undefined;
208 const kept = JSON.parse(row) as { stamp: string; files: string[] } & VersionShape;
209 if (kept.stamp !== shapeStamp(space.versions[version]!)) return undefined;
210 cache.set(version, kept.files);
211 shapes.set(version, { made: kept.made, gone: kept.gone, moves: kept.moves });
212 return kept.files;
213 }
215 /** A version's files made and taken out, and moves (#41). */
216 versionShape(space: Space, version: number, views?: Map<number, ReadView>): VersionShape {
217 const known = this.#shapeCache.get(space)?.get(version);
218 if (known) return known;
219 this.#filesCache.get(space)?.delete(version);
220 this.versionFiles(space, version, views);
221 return this.#shapeCache.get(space)!.get(version)!;
222 }
224 /**
225 * The space's path index (#41): for each path ever in it, the versions that
226 * changed it, and the versions that made it and took it out. The first ask
227 * after a restart walks every version, as a cold log over all of them does;
228 * later asks only walk the versions since.
229 */
230 pathsOf(space: Space): Map<string, PathLife> {
231 let index = this.#pathIndex.get(space);
232 if (!index) this.#pathIndex.set(space, (index = { upTo: -1, paths: new Map() }));
233 const views = new Map<number, ReadView>();
234 // Shared's sets grow version by version, so walk them forward rather than build each from the
235 // start; the views stay out of the cache, which they would only push other views from. A version
236 // whose files are kept (#41) needs no views at all.
237 let running: Set<AtomId> | undefined;
238 let runningAt = -1;
239 const setAt = (u: number): Set<AtomId> => {
240 if (!running || runningAt > u) (running = new Set(u < 0 ? [] : this.#deps.at(space, u))), (runningAt = u);
241 while (runningAt < u) {
242 const q = space.versions[++runningAt]!;
243 for (const id of q.atoms) running.add(id);
244 for (const id of q.dropped ?? []) running.delete(id);
245 }
246 return running;
247 };
248 for (let v = index.upTo + 1; v <= last(space); v++) {
249 // Persistent graphs use the indexed version reader; a raw View would
250 // rebuild move scratch state and copy historical membership during a read.
251 if (space.name === SHARED && !this.#deps.graph.hasOrderedChildren && !this.#keptFiles(space, v)) {
252 if (v > 0 && !views.has(v - 1)) views.set(v - 1, new View(this.#deps.graph, new Set(setAt(v - 1))));
253 views.set(v, new View(this.#deps.graph, new Set(setAt(v))));
254 }
255 const files = this.versionFiles(space, v, views);
256 const shape = this.versionShape(space, v, views);
257 views.delete(v - 1);
258 const life = (path: string): PathLife => {
259 let l = index!.paths.get(path);
260 if (!l) index!.paths.set(path, (l = { changed: [], events: [] }));
261 return l;
262 };
263 for (const f of files) life(f).changed.push(v);
264 const from = new Map(shape.moves.map(([a, b]) => [b, a]));
265 const to = new Map(shape.moves);
266 for (const f of shape.made)
267 life(f).events.push({ version: v, made: true, ...(from.has(f) ? { other: from.get(f)! } : {}) });
268 for (const f of shape.gone)
269 life(f).events.push({ version: v, made: false, ...(to.has(f) ? { other: to.get(f)! } : {}) });
270 index.upTo = v;
271 }
272 return index.paths;
273 }
275 lastChange(space: Space, version: number): LastChange {
276 const p = space.versions[version]!;
277 return { version, agent: p.agent, time: p.time, message: p.message };
278 }
280 /** The newest version in a sorted list no later than `version`, or undefined. */
281 static atOrBefore(list: readonly number[], version: number): number | undefined {
282 let lo = 0;
283 let hi = list.length;
284 while (lo < hi) {
285 const mid = (lo + hi) >> 1;
286 if (list[mid]! <= version) lo = mid + 1;
287 else hi = mid;
288 }
289 return lo > 0 ? list[lo - 1] : undefined;
290 }
292 /** The newest version in (from, to] that took a line out: one deleting it, or a drop of the change that wrote it. */
293 removedIn(space: Space, from: number, to: number, line: LineId): { removed?: number } {
294 const g = this.#deps.graph;
295 const deleters = new Set(g.deletersOf(line));
296 const writer = g.line(line)?.atom;
297 for (let v = to; v > from; v--) {
298 const p = space.versions[v]!;
299 if (p.atoms.some((id) => deleters.has(id)) || (writer !== undefined && p.dropped?.includes(writer)))
300 return { removed: v };
301 }
302 return {};
303 }