- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/pathindex.ts?version=261&format=json
289 linesFirst import 64% · capacity 32% · codex-capacity-chair 4%Last change capacity in version 252, 7 versions changed it
You're reading version 261. The newest is 318, and 1 version since then changed this file.
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.
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"];
24}
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 /** keptPaths as a set, kept for the newest version asked: never changed once made, so a view can hold it. */
94 keptPathSet(space: Space, version: number): ReadonlySet<string> | undefined {
95 let fold = this.#keptPaths.get(space);
96 if (fold?.version === version) return fold.files;
97 if (fold && fold.version > version) return this.#olderPathSet(space, fold, version);
98 if (!fold) fold = { version: -1, files: new Set() };
99 const files = new Set(fold.files);
100 for (let v = fold.version + 1; v <= version; v++) {
101 if (!this.#keptFiles(space, v)) return undefined;
102 // Not yet when the version is still being kept (keep asks its views which files exist).
103 const shape = this.#shapeCache.get(space)?.get(v);
104 if (!shape) return undefined;
105 for (const f of shape.gone) files.delete(f);
106 for (const f of shape.made) files.add(f);
107 }
108 this.#keptPaths.set(space, { version, files });
109 return files;
110 }
112 /** Each space's path index (#41): built the first time it's asked for, then kept up as versions land. */
113 readonly #pathIndex = new WeakMap<Space, { upTo: number; paths: Map<string, PathLife> }>();
115 /** The files a version changed. `views` holds views by version, so a run of versions builds each once. */
116 versionFiles(space: Space, version: number, views = new Map<number, ReadView>()): string[] {
117 let cache = this.#filesCache.get(space);
118 if (!cache) this.#filesCache.set(space, (cache = new Map()));
119 const known = cache.get(version);
120 if (known) return known;
121 const kept = this.#keptFiles(space, version);
122 if (kept) return kept;
123 const shapes = this.#shapeCache.get(space)!;
124 const p = space.versions[version]!;
125 const viewAt = (v: number): ReadView => {
126 let view = views.get(v);
127 if (!view) views.set(v, (view = v < 0 ? new View(this.#deps.graph, []) : this.#deps.viewAt(space, v)));
128 return view;
129 };
130 const before = viewAt(version - 1);
131 const after = viewAt(version);
132 // Net changes only, as show has them: a file made and removed again before the publish isn't one (#37).
133 const touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]);
134 const files = [...touched].filter((path) => differs(before, after, path)).sort();
135 this.keep(space, version, files, before, after);
136 return files;
137 }
139 /**
140 * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the
141 * version landed (version-files.ts, abso Capacity), which says which files existed before and after.
142 */
143 keep(
144 space: Space,
145 version: number,
146 files: string[],
147 before: ReadView,
148 after: ReadView,
149 exists?: ReadonlyMap<string, readonly [boolean, boolean]>,
150 ): void {
151 let cache = this.#filesCache.get(space);
152 if (!cache) this.#filesCache.set(space, (cache = new Map()));
153 let shapes = this.#shapeCache.get(space);
154 if (!shapes) this.#shapeCache.set(space, (shapes = new Map()));
155 const p = space.versions[version]!;
156 cache.set(version, files);
157 // Files made and taken out, and which of those are one file moved (#41): an atom that moves lines
158 // names both files, and a binary or empty file moved keeps its blob.
159 const had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f);
160 const has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f);
161 const made = files.filter((f) => !had(f) && has(f));
162 const gone = files.filter((f) => had(f) && !has(f));
163 const moves: [string, string][] = [];
164 if (made.length > 0 && gone.length > 0) {
165 const movers = new Set(this.#deps.graph.movers());
166 const moving = p.atoms.filter((id) => movers.has(id)).map((id) => this.#deps.graph.filesOf(id));
167 const taken = new Set<string>();
168 for (const to of made) {
169 const blob = after.attrs(to).blob;
170 const from = gone.find(
171 (g) =>
172 !taken.has(g) &&
173 (moving.some((fs) => fs.includes(g) && fs.includes(to)) ||
174 (blob !== null && before.attrs(g).blob === blob)),
175 );
176 if (from !== undefined) taken.add(from), moves.push([from, to]);
177 }
178 }
179 shapes.set(version, { made, gone, moves });
180 this.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves }));
181 }
183 /** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */
184 #keptFiles(space: Space, version: number): string[] | undefined {
185 let cache = this.#filesCache.get(space);
186 if (!cache) this.#filesCache.set(space, (cache = new Map()));
187 let shapes = this.#shapeCache.get(space);
188 if (!shapes) this.#shapeCache.set(space, (shapes = new Map()));
189 const known = cache.get(version);
190 if (known) return known;
191 const row = this.#deps.shapes?.get(space.name, version);
192 if (row === undefined) return undefined;
193 const kept = JSON.parse(row) as { stamp: string; files: string[] } & VersionShape;
194 if (kept.stamp !== shapeStamp(space.versions[version]!)) return undefined;
195 cache.set(version, kept.files);
196 shapes.set(version, { made: kept.made, gone: kept.gone, moves: kept.moves });
197 return kept.files;
198 }
200 /** A version's files made and taken out, and moves (#41). */
201 versionShape(space: Space, version: number, views?: Map<number, ReadView>): VersionShape {
202 const known = this.#shapeCache.get(space)?.get(version);
203 if (known) return known;
204 this.#filesCache.get(space)?.delete(version);
205 this.versionFiles(space, version, views);
206 return this.#shapeCache.get(space)!.get(version)!;
207 }
209 /**
210 * The space's path index (#41): for each path ever in it, the versions that
211 * changed it, and the versions that made it and took it out. The first ask
212 * after a restart walks every version, as a cold log over all of them does;
213 * later asks only walk the versions since.
214 */
215 pathsOf(space: Space): Map<string, PathLife> {
216 let index = this.#pathIndex.get(space);
217 if (!index) this.#pathIndex.set(space, (index = { upTo: -1, paths: new Map() }));
218 const views = new Map<number, ReadView>();
219 // Shared's sets grow version by version, so walk them forward rather than build each from the
220 // start; the views stay out of the cache, which they would only push other views from. A version
221 // whose files are kept (#41) needs no views at all.
222 let running: Set<AtomId> | undefined;
223 let runningAt = -1;
224 const setAt = (u: number): Set<AtomId> => {
225 if (!running || runningAt > u) (running = new Set(u < 0 ? [] : this.#deps.at(space, u))), (runningAt = u);
226 while (runningAt < u) {
227 const q = space.versions[++runningAt]!;
228 for (const id of q.atoms) running.add(id);
229 for (const id of q.dropped ?? []) running.delete(id);
230 }
231 return running;
232 };
233 for (let v = index.upTo + 1; v <= last(space); v++) {
234 // Persistent graphs use the indexed version reader; a raw View would
235 // rebuild move scratch state and copy historical membership during a read.
236 if (space.name === SHARED && !this.#deps.graph.hasOrderedChildren && !this.#keptFiles(space, v)) {
237 if (v > 0 && !views.has(v - 1)) views.set(v - 1, new View(this.#deps.graph, new Set(setAt(v - 1))));
238 views.set(v, new View(this.#deps.graph, new Set(setAt(v))));
239 }
240 const files = this.versionFiles(space, v, views);
241 const shape = this.versionShape(space, v, views);
242 views.delete(v - 1);
243 const life = (path: string): PathLife => {
244 let l = index!.paths.get(path);
245 if (!l) index!.paths.set(path, (l = { changed: [], events: [] }));
246 return l;
247 };
248 for (const f of files) life(f).changed.push(v);
249 const from = new Map(shape.moves.map(([a, b]) => [b, a]));
250 const to = new Map(shape.moves);
251 for (const f of shape.made)
252 life(f).events.push({ version: v, made: true, ...(from.has(f) ? { other: from.get(f)! } : {}) });
253 for (const f of shape.gone)
254 life(f).events.push({ version: v, made: false, ...(to.has(f) ? { other: to.get(f)! } : {}) });
255 index.upTo = v;
256 }
257 return index.paths;
258 }
260 lastChange(space: Space, version: number): LastChange {
261 const p = space.versions[version]!;
262 return { version, agent: p.agent, time: p.time, message: p.message };
263 }
265 /** The newest version in a sorted list no later than `version`, or undefined. */
266 static atOrBefore(list: readonly number[], version: number): number | undefined {
267 let lo = 0;
268 let hi = list.length;
269 while (lo < hi) {
270 const mid = (lo + hi) >> 1;
271 if (list[mid]! <= version) lo = mid + 1;
272 else hi = mid;
273 }
274 return lo > 0 ? list[lo - 1] : undefined;
275 }
277 /** The newest version in (from, to] that took a line out: one deleting it, or a drop of the change that wrote it. */
278 removedIn(space: Space, from: number, to: number, line: LineId): { removed?: number } {
279 const g = this.#deps.graph;
280 const deleters = new Set(g.deletersOf(line));
281 const writer = g.line(line)?.atom;
282 for (let v = to; v > from; v--) {
283 const p = space.versions[v]!;
284 if (p.atoms.some((id) => deleters.has(id)) || (writer !== undefined && p.dropped?.includes(writer)))
285 return { removed: v };
286 }
287 return {};
288 }
289}