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/src/engine/view.ts?version=120&format=json
574 linesFirst import 80% · codex-capacity-chair 12% · codex-capacity-policy 8%Last change codex-capacity-chair in version 119, 7 versions changed it

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

1// Projection: the files a closed set of atoms produces. A pure function of the
2// set. Each file is a tree of lines hanging off its start node; linearizing it
3// depth-first, children in descending priority, gives the file's sequence. A
4// line hangs off the node it was written after, unless a move in the set gave
5// it another parent (#28): then the newest such move decides.
6//
7// A laid-out file is kept as arrays of line numbers (the graph's n), not as a
8// record per line (#46): a record, its id and two index entries cost about
9// 370 bytes a line, and the view cache holds up to 200,000 lines. Records are
10// made when a caller asks for them, and the latest asked for is kept.
12import type { Graph, RelocationInfo } from "./graph.ts";
13import { isStartNode } from "./keys.ts";
14import { StableSet } from "./state-source.ts";
15import { joinLines } from "./text.ts";
16import { type AtomId, type AtomSet, EngineError, type LineId, type NodeId } from "./types.ts";
18/** A line in a file's full sequence, tombstones included. */
19export interface SeqLine {
20 readonly id: LineId;
21 readonly text: string;
22 /** The atom that wrote it. */
23 readonly atom: AtomId;
24 /** Atoms in the set that delete it, by seq. Empty when it is visible. */
25 readonly deletedBy: readonly AtomId[];
26 readonly visible: boolean;
29/** A visible line of a projected file. */
30export interface VisibleLine {
31 readonly id: LineId;
32 readonly text: string;
33 readonly atom: AtomId;
36/** A file's attributes in a view. */
37export interface FileAttrs {
38 /** Kept with no lines (an empty file). */
39 readonly exists: boolean;
40 readonly exec: boolean;
41 /** The target, when the file is a symbolic link. */
42 readonly link: string | null;
43 /** The SHA-256 of its bytes, when the file is binary. */
44 readonly blob: string | null;
47const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
48const NONE: readonly never[] = Object.freeze([]);
50interface FileState {
51 readonly attrs: FileAttrs;
52 /** Full history is retained only when a sequence caller explicitly requests it. */
53 readonly complete: boolean;
54 /** The file's sequence, tombstones included: each line's n. */
55 readonly seq: Int32Array;
56 /** Its visible lines' n, in order. */
57 readonly vis: Int32Array;
58 /** For each deleted position in seq: the seqs of the atoms in the set that delete it, by priority. */
59 readonly deletedBy: ReadonlyMap<number, readonly number[]>;
60 /** Positions by n, built the first time they're asked for. */
61 seqIndex?: Map<number, number>;
62 visIndex?: Map<number, number>;
65interface Child {
66 readonly n: number;
67 /** 1 when the child's atom deleted the parent (the child replaces it), else 0. A moved line is 0. */
68 readonly replaces: number;
69 readonly lamport: number;
70 readonly seq: number;
71 readonly k: number;
74const EMPTY_INTS = new Int32Array(0);
75const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
77// Approximate retained-container costs for View.weight(): reference slots,
78// object headers, and map/set entries. These are cache-budget units, not V8
79// heap measurements. Referenced Graph strings and records are not charged.
80const ARRAY_HEADER_BYTES = 24;
81const OBJECT_BYTES = 32;
82const MAP_HEADER_BYTES = 32;
83const MAP_ENTRY_BYTES = 40;
84const SET_ENTRY_BYTES = 32;
85const REFERENCE_BYTES = 8;
87/**
88 * The projection of a closed set. Construct once, then ask for files, lines
89 * and sequences. Throws `not-closed` if some atom's dependency is missing.
90 * Each file is laid out the first time it is asked for, so a view of a large
91 * set costs little until files are read.
92 */
93export class View {
94 readonly graph: Graph;
95 /** The set, made from #in the first time it's asked for: a view keeps a byte per atom, not a copy of the set (#46). */
96 #set: AtomSet | undefined;
97 readonly #stable: StableSet<AtomId> | undefined;
98 /** Fixed-size memo for immutable indexed membership, independent of history size. */
99 #membershipSeq = new Float64Array(256).fill(-1);
100 #membershipValue = new Uint8Array(256);
101 #files = new Map<string, FileState>();
102 /** Every file the set wrote in, sorted; worked out the first time it's asked for. */
103 #knownList: string[] | undefined;
104 /** 1 for each atom (by seq) in the set. */
105 #in: Uint8Array;
106 /** Atoms in the set that move lines. */
107 #movers: AtomId[];
108 /** Each moved line's relocation that counts in this set; built on first use. */
109 #relocs: Map<LineId, RelocationInfo> | undefined;
110 /** The same, by n, with the new parent's code. */
111 #relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined;
112 #fileOf = new Map<number, string>();
113 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */
114 #linesCache: { path: string; lines: readonly VisibleLine[] } | undefined;
115 #seqCache: { path: string; seq: readonly SeqLine[] } | undefined;
116 #textCache: { path: string; text: string } | undefined;
118 constructor(graph: Graph, set: Iterable<AtomId>) {
119 this.graph = graph;
120 if (set instanceof StableSet) {
121 this.#stable = set;
122 this.#set = set;
123 this.#in = new Uint8Array(0);
124 for (const id of set) {
125 const atom = graph.atom(id);
126 for (const dependency of atom.deps) {
127 graph.atom(dependency);
128 if (!set.has(dependency))
129 throw new EngineError("not-closed", `set is not closed: ${id} depends on ${dependency}`);
130 }
131 }
132 this.#movers = graph.movers().filter((id) => set.has(id));
133 this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
134 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
135 return;
136 }
137 const inSet = new Uint8Array(graph.size);
138 for (const id of set) {
139 const atom = graph.atom(id);
140 inSet[atom.seq] = 1;
141 }
142 // Validate one atom at a time. Keeping all decoded atoms here defeats
143 // storage-backed Graph reads even though the view only needs membership.
144 for (let seq = 0; seq < inSet.length; seq++) {
145 if (inSet[seq] !== 1) continue;
146 const atom = graph.atom(graph.idOfSeq(seq));
147 for (const d of atom.deps) {
148 if (inSet[graph.atom(d).seq] !== 1)
149 throw new EngineError("not-closed", `set is not closed: ${atom.id} depends on ${d}`);
150 }
151 }
152 this.graph = graph;
153 this.#in = inSet;
154 this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);
155 this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
156 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
157 }
159 /** The set of atoms this view projects. */
160 get set(): AtomSet {
161 if (!this.#set) {
162 const out = new Set<AtomId>();
163 for (let seq = 0; seq < this.#in.length; seq++) if (this.#in[seq] === 1) out.add(this.graph.idOfSeq(seq));
164 this.#set = out;
165 }
166 return this.#set;
167 }
169 /** Whether the atom with this seq is in the set. */
170 #has(seq: number): boolean {
171 if (this.#stable) {
172 const slot = seq % this.#membershipSeq.length;
173 if (this.#membershipSeq[slot] === seq) return this.#membershipValue[slot] === 1;
174 const present = this.#stable.has(this.graph.idOfSeq(seq));
175 this.#membershipSeq[slot] = seq;
176 this.#membershipValue[slot] = present ? 1 : 0;
177 return present;
178 }
179 return seq < this.#in.length && this.#in[seq] === 1;
180 }
182 /**
183 * The relocation that counts for each moved line: the newest (by priority)
184 * in the set. Two moves made apart can loop lines into each other, so no
185 * start node reaches them; then the oldest relocation on the loop is
186 * ignored, and the next newest of that line counts, until none loop.
187 */
188 #relocations(): Map<LineId, RelocationInfo> {
189 if (this.#relocs) return this.#relocs;
190 const graph = this.graph;
191 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0;
192 const ignored = new Set<RelocationInfo>();
193 const pick = (line: LineId): RelocationInfo | undefined => {
194 let best: RelocationInfo | undefined;
195 for (const r of graph.relocationsOf(line)) {
196 if (this.#has(graph.atom(r.atom).seq) && !ignored.has(r) && (!best || stronger(r, best))) best = r;
197 }
198 return best;
199 };
200 const relocs = new Map<LineId, RelocationInfo>();
201 for (const id of this.#movers) {
202 for (const m of graph.atom(id).moves!) {
203 if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);
204 }
205 }
206 const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;
207 const reaches = new Set<LineId>();
208 for (const line of [...relocs.keys()]) {
209 for (;;) {
210 const path: LineId[] = [];
211 const onPath = new Map<LineId, number>();
212 let cur: NodeId = line;
213 let loopAt = -1;
214 while (!isStartNode(cur) && !reaches.has(cur)) {
215 const at = onPath.get(cur);
216 if (at !== undefined) {
217 loopAt = at;
218 break;
219 }
220 onPath.set(cur, path.length);
221 path.push(cur);
222 cur = parentOf(cur);
223 }
224 if (loopAt < 0) {
225 for (const l of path) reaches.add(l);
226 break;
227 }
228 let weakest: RelocationInfo | undefined;
229 for (const l of path.slice(loopAt)) {
230 const r = relocs.get(l);
231 if (r && (!weakest || stronger(weakest, r))) weakest = r;
232 }
233 ignored.add(weakest!);
234 const next = pick(weakest!.line);
235 if (next) relocs.set(weakest!.line, next);
236 else relocs.delete(weakest!.line);
237 }
238 }
239 this.#relocs = relocs;
240 return relocs;
241 }
243 /** The relocations by n, with each new parent's code. */
244 #relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> {
245 if (this.#relocsN) return this.#relocsN;
246 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();
247 for (const [line, r] of this.#relocations())
248 out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
249 this.#relocsN = out;
250 return out;
251 }
253 /** The node a line hangs off in this set: where it was written, or where a move put it. */
254 parentOf(line: LineId): NodeId {
255 const r = this.#movers.length === 0 ? undefined : this.#relocations().get(line);
256 if (r) return r.parent;
257 const parent = this.graph.lineParent(line);
258 if (parent === undefined) throw new EngineError("unknown-line", `no line ${line}`);
259 return parent;
260 }
262 /** Whether any atom in the set moves lines. */
263 hasMoves(): boolean {
264 return this.#movers.length > 0;
265 }
267 /** The file a line is in, in this set: the one its chain of parents reaches. */
268 fileOf(line: LineId): string {
269 const n = this.graph.lineN(line);
270 if (n < 0) throw new EngineError("unknown-line", `no line ${line}`);
271 return this.#fileOfN(n);
272 }
274 #fileOfN(n: number): string {
275 const graph = this.graph;
276 if (this.#movers.length === 0) return graph.writtenIn(n);
277 const relocs = this.#relocationsN();
278 const path: number[] = [];
279 let cur = n;
280 let file: string | undefined;
281 while (file === undefined) {
282 if (cur < 0) file = graph.nodeOf(cur).slice(1);
283 else {
284 file = this.#fileOf.get(cur);
285 if (file === undefined) {
286 path.push(cur);
287 cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
288 }
289 }
290 }
291 for (const l of path) this.#fileOf.set(l, file);
292 return file;
293 }
295 /** The lines hanging off a node in this set, in layout order. */
296 #children(code: number): Child[] {
297 const graph = this.graph;
298 const relocs = this.#movers.length === 0 ? undefined : this.#relocationsN();
299 const ns: number[] = [];
300 graph.childrenN(code, ns);
301 const out: Child[] = [];
302 for (const n of ns) {
303 const seq = graph.writerSeqOf(n);
304 if (!this.#has(seq) || relocs?.has(n)) continue;
305 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
306 }
307 if (relocs) {
308 for (const r of graph.relocatedToCode(code)) {
309 const n = graph.lineN(r.line);
310 const selected = relocs.get(n)?.r;
311 if (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;
312 const seq = graph.atom(r.atom).seq;
313 out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
314 }
315 }
316 // A line's replacements (runs of atoms that deleted it) first, then the
317 // rest; each group in descending priority (lamport, then seq); lines of
318 // one atom keep k order. A replacement already outranks every insertion
319 // after the line that reads it (it depends on them, or conflicts with
320 // them); the grouping also puts it above a `before` insertion, which
321 // doesn't read the line above, so the new text lands in the replaced
322 // line's place. A moved line ranks like an insertion by the moving atom.
323 if (out.length > 1)
324 out.sort((a, b) => b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k);
325 return out;
326 }
328 /** Lay out one file: its line tree from the set's atoms, linearized. */
329 #layout(path: string, complete: boolean): FileState {
330 const graph = this.graph;
331 // For each attribute, the highest-priority setter in the set wins.
332 const setters = new Map<string, { id: AtomId; value: string | null }>();
333 for (const id of graph.eachAtomIn(path)) {
334 if (!this.#has(graph.atom(id).seq)) continue;
335 for (const a of graph.atom(id).attrs ?? []) {
336 if (a.file !== path) continue;
337 const best = setters.get(a.name);
338 if (!best || graph.comparePriority(best.id, id) < 0) setters.set(a.name, { id, value: a.value });
339 }
340 }
342 const seq: number[] = [];
343 const vis: number[] = [];
344 const deletedBy = new Map<number, readonly number[]>();
345 // Depth-first, iteratively (a file of n lines can be a chain n deep).
346 const stack: number[] = [];
347 const pushChildren = (code: number): void => {
348 const list = this.#children(code);
349 for (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!.n);
350 };
351 const start = graph.startCodeOf(path);
352 if (start !== undefined) pushChildren(start);
353 const priority = (a: number, b: number) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;
354 while (stack.length > 0) {
355 const n = stack.pop()!;
356 const deleters = graph.deleterSeqsOf(n);
357 const by = deleters.length === 0 ? NONE : deleters.filter((d) => this.#has(d));
358 if (by.length > 0) {
359 if (complete) deletedBy.set(seq.length, by.length > 1 ? [...by].sort(priority) : by);
360 } else vis.push(n);
361 if (complete) seq.push(n);
362 pushChildren(n);
363 }
364 const attrs: FileAttrs = {
365 exists: setters.get("exists")?.value === "1",
366 exec: setters.get("exec")?.value === "1",
367 link: setters.get("link")?.value ?? null,
368 blob: setters.get("blob")?.value ?? null,
369 };
370 return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
371 }
373 get #known(): string[] {
374 if (!this.#knownList) {
375 const known = new Set<string>();
376 if (this.#stable) {
377 for (const id of this.#stable) for (const file of this.graph.filesOf(id)) known.add(file);
378 } else {
379 for (let seq = 0; seq < this.#in.length; seq++)
380 if (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);
381 }
382 this.#knownList = [...known].sort();
383 }
384 return this.#knownList;
385 }
387 /** Files with at least one visible line, sorted. */
388 paths(): string[] {
389 if (this.#stable) {
390 const paths: string[] = [];
391 for (const path of this.graph.knownFiles()) {
392 if (this.hasFile(path)) paths.push(path);
393 else this.#files.delete(path);
394 }
395 return paths.sort();
396 }
397 return this.#known.filter((p) => this.hasFile(p));
398 }
400 /** Every file the set has ever written a line in, including fully deleted ones. */
401 knownPaths(): string[] {
402 return [...this.#known];
403 }
405 /** Whether the file exists in this projection (has a visible line). */
406 hasFile(path: string): boolean {
407 const f = this.#file(path);
408 return f.vis.length > 0 || f.attrs.exists || f.attrs.link !== null || f.attrs.blob !== null;
409 }
411 /** A file's attributes: kept while empty, executable, a link's target. */
412 attrs(path: string): FileAttrs {
413 return this.#file(path).attrs;
414 }
416 /** The projected text of a file ("" if it has no visible lines). */
417 text(path: string): string {
418 if (this.#textCache?.path === path) return this.#textCache.text;
419 const vis = this.#file(path).vis;
420 const lines: string[] = new Array(vis.length);
421 for (let i = 0; i < vis.length; i++) lines[i] = this.graph.textOf(vis[i]!);
422 const text = joinLines(lines);
423 this.#textCache = { path, text };
424 return text;
425 }
427 /** Whether a file reads the same here and in `other` (of the same graph), compared line by line without joining either (#46). */
428 sameText(path: string, other: View): boolean {
429 const a = this.#file(path).vis;
430 const b = other.#file(path).vis;
431 if (a.length !== b.length) return false;
432 for (let i = 0; i < a.length; i++)
433 if (a[i] !== b[i] && this.graph.textOf(a[i]!) !== this.graph.textOf(b[i]!)) return false;
434 return true;
435 }
437 /** Visible lines with their ids. */
438 lines(path: string): readonly VisibleLine[] {
439 if (this.#linesCache?.path === path) return this.#linesCache.lines;
440 const graph = this.graph;
441 const vis = this.#file(path).vis;
442 const lines: VisibleLine[] = new Array(vis.length);
443 for (let i = 0; i < vis.length; i++) {
444 const n = vis[i]!;
445 lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
446 }
447 this.#linesCache = { path, lines };
448 return lines;
449 }
451 /** The full linearized sequence of a file, tombstones included. */
452 sequence(path: string): readonly SeqLine[] {
453 if (this.#seqCache?.path === path) return this.#seqCache.seq;
454 const graph = this.graph;
455 const f = this.#file(path, true);
456 const out: SeqLine[] = new Array(f.seq.length);
457 for (let i = 0; i < f.seq.length; i++) {
458 const n = f.seq[i]!;
459 const by = f.deletedBy.get(i);
460 out[i] = {
461 id: graph.lineIdOf(n),
462 text: graph.textOf(n),
463 atom: graph.idOfSeq(graph.writerSeqOf(n)),
464 deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
465 visible: by === undefined,
466 };
467 }
468 this.#seqCache = { path, seq: out };
469 return out;
470 }
472 /** Index of a line in `sequence(path)`, or -1. */
473 sequenceIndex(path: string, line: LineId): number {
474 const f = this.#file(path, true);
475 if (!f.seqIndex) {
476 f.seqIndex = new Map();
477 for (let i = 0; i < f.seq.length; i++) f.seqIndex.set(f.seq[i]!, i);
478 }
479 return f.seqIndex.get(this.graph.lineN(line)) ?? -1;
480 }
482 /** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */
483 lineNumber(path: string, line: LineId): number {
484 const f = this.#file(path);
485 if (!f.visIndex) {
486 f.visIndex = new Map();
487 for (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);
488 }
489 return f.visIndex.get(this.graph.lineN(line)) ?? -1;
490 }
492 /** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */
493 files(): Record<string, string> {
494 const out: Record<string, string> = {};
495 for (const path of this.paths()) if (this.#file(path).attrs.blob === null) out[path] = this.text(path);
496 return out;
497 }
499 /** Binary files, path -> the SHA-256 of their bytes, in path order. */
500 blobs(): Record<string, string> {
501 const out: Record<string, string> = {};
502 for (const path of this.paths()) {
503 const blob = this.#file(path).attrs.blob;
504 if (blob !== null) out[path] = blob;
505 }
506 return out;
507 }
509 /**
510 * About how many bytes of retained state this view costs (#46). This uses
511 * approximate ownership costs for containers and wrappers, not V8 heap
512 * measurements. Graph-owned strings and records count only as references.
513 * This method only inspects already-created state: it does not lay out files,
514 * populate lazy caches, enumerate the graph, or count temporary traversal
515 * containers and shared empty singleton payloads as retained view state.
516 */
517 weight(): number {
518 let n = this.#in.byteLength;
519 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength;
520 n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
521 n += MAP_HEADER_BYTES; // #files exists from construction.
522 n += MAP_HEADER_BYTES; // #fileOf exists from construction.
523 if (this.#stable) n += this.#stable.retainedBytes;
524 else if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
525 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;
526 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
527 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
528 n += MAP_ENTRY_BYTES * this.#fileOf.size;
529 for (const f of this.#files.values()) {
530 n += MAP_ENTRY_BYTES;
531 if (f !== NO_FILE) {
532 // FileState and FileAttrs are per laid-out file; typed-array
533 // headers are separate from their payload byte lengths below.
534 n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
535 n += f.seq.byteLength + f.vis.byteLength;
536 n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
537 for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;
538 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;
539 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;
540 }
541 }
542 if (this.#linesCache)
543 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 100 * this.#linesCache.lines.length;
544 if (this.#seqCache) {
545 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 120 * this.#seqCache.seq.length;
546 for (const line of this.#seqCache.seq)
547 if (line.deletedBy !== NONE) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * line.deletedBy.length;
548 }
549 if (this.#textCache) n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 2 * this.#textCache.text.length;
550 return n;
551 }
553 #file(path: string, complete = false): FileState {
554 let state = this.#files.get(path);
555 if (!state || (complete && !state.complete)) {
556 state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;
557 this.#files.set(path, state);
558 }
559 return state;
560 }
563/** The files of a closed set, path -> text. */
564export function project(graph: Graph, set: Iterable<AtomId>): Record<string, string> {
565 return new View(graph, set).files();
568/** Where a node sits in a file's sequence: the index just after it (0 for a start node). */
569export function indexAfter(view: View, path: string, node: NodeId): number {
570 if (isStartNode(node)) return 0;
571 const i = view.sequenceIndex(path, node);
572 if (i < 0) throw new EngineError("not-in-view", `line ${node} is not in ${path} in this view`);
573 return i + 1;