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=124&format=json
585 linesFirst import 77% · codex-capacity-chair 15% · codex-capacity-policy 8%Last change codex-capacity-chair in version 123, 8 versions changed it

You're reading version 124. The newest is 318, and 37 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[] | undefined;
108 #hasMoves = false;
109 /** Each moved line's relocation that counts in this set; built on first use. */
110 #relocs: Map<LineId, RelocationInfo> | undefined;
111 /** The same, by n, with the new parent's code. */
112 #relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined;
113 #fileOf = new Map<number, string>();
114 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */
115 #linesCache: { path: string; lines: readonly VisibleLine[] } | undefined;
116 #seqCache: { path: string; seq: readonly SeqLine[] } | undefined;
117 #textCache: { path: string; text: string } | undefined;
119 constructor(graph: Graph, set: Iterable<AtomId>) {
120 this.graph = graph;
121 if (set instanceof StableSet) {
122 this.#stable = set;
123 this.#set = set;
124 this.#in = new Uint8Array(0);
125 for (const id of set) {
126 const atom = graph.atom(id);
127 for (const dependency of atom.deps) {
128 graph.atom(dependency);
129 if (!set.has(dependency))
130 throw new EngineError("not-closed", `set is not closed: ${id} depends on ${dependency}`);
131 }
132 }
133 for (const id of graph.movers()) {
134 if (set.has(id)) {
135 this.#hasMoves = true;
136 break;
137 }
138 }
139 this.#relocs = this.#hasMoves ? undefined : new Map();
140 this.#relocsN = this.#hasMoves ? undefined : new Map();
141 return;
142 }
143 const inSet = new Uint8Array(graph.size);
144 for (const id of set) {
145 const atom = graph.atom(id);
146 inSet[atom.seq] = 1;
147 }
148 // Validate one atom at a time. Keeping all decoded atoms here defeats
149 // storage-backed Graph reads even though the view only needs membership.
150 for (let seq = 0; seq < inSet.length; seq++) {
151 if (inSet[seq] !== 1) continue;
152 const atom = graph.atom(graph.idOfSeq(seq));
153 for (const d of atom.deps) {
154 if (inSet[graph.atom(d).seq] !== 1)
155 throw new EngineError("not-closed", `set is not closed: ${atom.id} depends on ${d}`);
156 }
157 }
158 this.graph = graph;
159 this.#in = inSet;
160 this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);
161 this.#hasMoves = this.#movers.length > 0;
162 this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
163 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
164 }
166 /** The set of atoms this view projects. */
167 get set(): AtomSet {
168 if (!this.#set) {
169 const out = new Set<AtomId>();
170 for (let seq = 0; seq < this.#in.length; seq++) if (this.#in[seq] === 1) out.add(this.graph.idOfSeq(seq));
171 this.#set = out;
172 }
173 return this.#set;
174 }
176 /** Whether the atom with this seq is in the set. */
177 #has(seq: number): boolean {
178 if (this.#stable) {
179 const slot = seq % this.#membershipSeq.length;
180 if (this.#membershipSeq[slot] === seq) return this.#membershipValue[slot] === 1;
181 const present = this.#stable.has(this.graph.idOfSeq(seq));
182 this.#membershipSeq[slot] = seq;
183 this.#membershipValue[slot] = present ? 1 : 0;
184 return present;
185 }
186 return seq < this.#in.length && this.#in[seq] === 1;
187 }
189 /**
190 * The relocation that counts for each moved line: the newest (by priority)
191 * in the set. Two moves made apart can loop lines into each other, so no
192 * start node reaches them; then the oldest relocation on the loop is
193 * ignored, and the next newest of that line counts, until none loop.
194 */
195 #relocations(): Map<LineId, RelocationInfo> {
196 if (this.#relocs) return this.#relocs;
197 const graph = this.graph;
198 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0;
199 const ignored = new Set<RelocationInfo>();
200 const pick = (line: LineId): RelocationInfo | undefined => {
201 let best: RelocationInfo | undefined;
202 for (const r of graph.relocationsOf(line)) {
203 if (this.#has(graph.atom(r.atom).seq) && !ignored.has(r) && (!best || stronger(r, best))) best = r;
204 }
205 return best;
206 };
207 const relocs = new Map<LineId, RelocationInfo>();
208 for (const id of this.#eachMover()) {
209 for (const m of graph.atom(id).moves!) {
210 if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);
211 }
212 }
213 const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;
214 const reaches = new Set<LineId>();
215 for (const line of [...relocs.keys()]) {
216 for (;;) {
217 const path: LineId[] = [];
218 const onPath = new Map<LineId, number>();
219 let cur: NodeId = line;
220 let loopAt = -1;
221 while (!isStartNode(cur) && !reaches.has(cur)) {
222 const at = onPath.get(cur);
223 if (at !== undefined) {
224 loopAt = at;
225 break;
226 }
227 onPath.set(cur, path.length);
228 path.push(cur);
229 cur = parentOf(cur);
230 }
231 if (loopAt < 0) {
232 for (const l of path) reaches.add(l);
233 break;
234 }
235 let weakest: RelocationInfo | undefined;
236 for (const l of path.slice(loopAt)) {
237 const r = relocs.get(l);
238 if (r && (!weakest || stronger(weakest, r))) weakest = r;
239 }
240 ignored.add(weakest!);
241 const next = pick(weakest!.line);
242 if (next) relocs.set(weakest!.line, next);
243 else relocs.delete(weakest!.line);
244 }
245 }
246 this.#relocs = relocs;
247 return relocs;
248 }
250 /** The relocations by n, with each new parent's code. */
251 #relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> {
252 if (this.#relocsN) return this.#relocsN;
253 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();
254 for (const [line, r] of this.#relocations())
255 out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
256 this.#relocsN = out;
257 return out;
258 }
260 /** The node a line hangs off in this set: where it was written, or where a move put it. */
261 parentOf(line: LineId): NodeId {
262 const r = this.#hasMoves ? this.#relocations().get(line) : undefined;
263 if (r) return r.parent;
264 const parent = this.graph.lineParent(line);
265 if (parent === undefined) throw new EngineError("unknown-line", `no line ${line}`);
266 return parent;
267 }
269 /** Whether any atom in the set moves lines. */
270 hasMoves(): boolean {
271 return this.#hasMoves;
272 }
273 *#eachMover(): IterableIterator<AtomId> {
274 if (this.#movers) yield* this.#movers;
275 else for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id;
276 }
278 /** The file a line is in, in this set: the one its chain of parents reaches. */
279 fileOf(line: LineId): string {
280 const n = this.graph.lineN(line);
281 if (n < 0) throw new EngineError("unknown-line", `no line ${line}`);
282 return this.#fileOfN(n);
283 }
285 #fileOfN(n: number): string {
286 const graph = this.graph;
287 if (!this.#hasMoves) return graph.writtenIn(n);
288 const relocs = this.#relocationsN();
289 const path: number[] = [];
290 let cur = n;
291 let file: string | undefined;
292 while (file === undefined) {
293 if (cur < 0) file = graph.nodeOf(cur).slice(1);
294 else {
295 file = this.#fileOf.get(cur);
296 if (file === undefined) {
297 path.push(cur);
298 cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
299 }
300 }
301 }
302 for (const l of path) this.#fileOf.set(l, file);
303 return file;
304 }
306 /** The lines hanging off a node in this set, in layout order. */
307 #children(code: number): Child[] {
308 const graph = this.graph;
309 const relocs = this.#hasMoves ? this.#relocationsN() : undefined;
310 const ns: number[] = [];
311 graph.childrenN(code, ns);
312 const out: Child[] = [];
313 for (const n of ns) {
314 const seq = graph.writerSeqOf(n);
315 if (!this.#has(seq) || relocs?.has(n)) continue;
316 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
317 }
318 if (relocs) {
319 for (const r of graph.relocatedToCode(code)) {
320 const n = graph.lineN(r.line);
321 const selected = relocs.get(n)?.r;
322 if (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;
323 const seq = graph.atom(r.atom).seq;
324 out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
325 }
326 }
327 // A line's replacements (runs of atoms that deleted it) first, then the
328 // rest; each group in descending priority (lamport, then seq); lines of
329 // one atom keep k order. A replacement already outranks every insertion
330 // after the line that reads it (it depends on them, or conflicts with
331 // them); the grouping also puts it above a `before` insertion, which
332 // doesn't read the line above, so the new text lands in the replaced
333 // line's place. A moved line ranks like an insertion by the moving atom.
334 if (out.length > 1)
335 out.sort((a, b) => b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k);
336 return out;
337 }
339 /** Lay out one file: its line tree from the set's atoms, linearized. */
340 #layout(path: string, complete: boolean): FileState {
341 const graph = this.graph;
342 // For each attribute, the highest-priority setter in the set wins.
343 const setters = new Map<string, { id: AtomId; value: string | null }>();
344 for (const id of graph.eachAtomIn(path)) {
345 if (!this.#has(graph.atom(id).seq)) continue;
346 for (const a of graph.atom(id).attrs ?? []) {
347 if (a.file !== path) continue;
348 const best = setters.get(a.name);
349 if (!best || graph.comparePriority(best.id, id) < 0) setters.set(a.name, { id, value: a.value });
350 }
351 }
353 const seq: number[] = [];
354 const vis: number[] = [];
355 const deletedBy = new Map<number, readonly number[]>();
356 // Depth-first, iteratively (a file of n lines can be a chain n deep).
357 const stack: number[] = [];
358 const pushChildren = (code: number): void => {
359 const list = this.#children(code);
360 for (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!.n);
361 };
362 const start = graph.startCodeOf(path);
363 if (start !== undefined) pushChildren(start);
364 const priority = (a: number, b: number) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;
365 while (stack.length > 0) {
366 const n = stack.pop()!;
367 const deleters = graph.deleterSeqsOf(n);
368 const by = deleters.length === 0 ? NONE : deleters.filter((d) => this.#has(d));
369 if (by.length > 0) {
370 if (complete) deletedBy.set(seq.length, by.length > 1 ? [...by].sort(priority) : by);
371 } else vis.push(n);
372 if (complete) seq.push(n);
373 pushChildren(n);
374 }
375 const attrs: FileAttrs = {
376 exists: setters.get("exists")?.value === "1",
377 exec: setters.get("exec")?.value === "1",
378 link: setters.get("link")?.value ?? null,
379 blob: setters.get("blob")?.value ?? null,
380 };
381 return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
382 }
384 get #known(): string[] {
385 if (!this.#knownList) {
386 const known = new Set<string>();
387 if (this.#stable) {
388 for (const id of this.#stable) for (const file of this.graph.filesOf(id)) known.add(file);
389 } else {
390 for (let seq = 0; seq < this.#in.length; seq++)
391 if (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);
392 }
393 this.#knownList = [...known].sort();
394 }
395 return this.#knownList;
396 }
398 /** Files with at least one visible line, sorted. */
399 paths(): string[] {
400 if (this.#stable) {
401 const paths: string[] = [];
402 for (const path of this.graph.knownFiles()) {
403 if (this.hasFile(path)) paths.push(path);
404 else this.#files.delete(path);
405 }
406 return paths.sort();
407 }
408 return this.#known.filter((p) => this.hasFile(p));
409 }
411 /** Every file the set has ever written a line in, including fully deleted ones. */
412 knownPaths(): string[] {
413 return [...this.#known];
414 }
416 /** Whether the file exists in this projection (has a visible line). */
417 hasFile(path: string): boolean {
418 const f = this.#file(path);
419 return f.vis.length > 0 || f.attrs.exists || f.attrs.link !== null || f.attrs.blob !== null;
420 }
422 /** A file's attributes: kept while empty, executable, a link's target. */
423 attrs(path: string): FileAttrs {
424 return this.#file(path).attrs;
425 }
427 /** The projected text of a file ("" if it has no visible lines). */
428 text(path: string): string {
429 if (this.#textCache?.path === path) return this.#textCache.text;
430 const vis = this.#file(path).vis;
431 const lines: string[] = new Array(vis.length);
432 for (let i = 0; i < vis.length; i++) lines[i] = this.graph.textOf(vis[i]!);
433 const text = joinLines(lines);
434 this.#textCache = { path, text };
435 return text;
436 }
438 /** Whether a file reads the same here and in `other` (of the same graph), compared line by line without joining either (#46). */
439 sameText(path: string, other: View): boolean {
440 const a = this.#file(path).vis;
441 const b = other.#file(path).vis;
442 if (a.length !== b.length) return false;
443 for (let i = 0; i < a.length; i++)
444 if (a[i] !== b[i] && this.graph.textOf(a[i]!) !== this.graph.textOf(b[i]!)) return false;
445 return true;
446 }
448 /** Visible lines with their ids. */
449 lines(path: string): readonly VisibleLine[] {
450 if (this.#linesCache?.path === path) return this.#linesCache.lines;
451 const graph = this.graph;
452 const vis = this.#file(path).vis;
453 const lines: VisibleLine[] = new Array(vis.length);
454 for (let i = 0; i < vis.length; i++) {
455 const n = vis[i]!;
456 lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
457 }
458 this.#linesCache = { path, lines };
459 return lines;
460 }
462 /** The full linearized sequence of a file, tombstones included. */
463 sequence(path: string): readonly SeqLine[] {
464 if (this.#seqCache?.path === path) return this.#seqCache.seq;
465 const graph = this.graph;
466 const f = this.#file(path, true);
467 const out: SeqLine[] = new Array(f.seq.length);
468 for (let i = 0; i < f.seq.length; i++) {
469 const n = f.seq[i]!;
470 const by = f.deletedBy.get(i);
471 out[i] = {
472 id: graph.lineIdOf(n),
473 text: graph.textOf(n),
474 atom: graph.idOfSeq(graph.writerSeqOf(n)),
475 deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
476 visible: by === undefined,
477 };
478 }
479 this.#seqCache = { path, seq: out };
480 return out;
481 }
483 /** Index of a line in `sequence(path)`, or -1. */
484 sequenceIndex(path: string, line: LineId): number {
485 const f = this.#file(path, true);
486 if (!f.seqIndex) {
487 f.seqIndex = new Map();
488 for (let i = 0; i < f.seq.length; i++) f.seqIndex.set(f.seq[i]!, i);
489 }
490 return f.seqIndex.get(this.graph.lineN(line)) ?? -1;
491 }
493 /** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */
494 lineNumber(path: string, line: LineId): number {
495 const f = this.#file(path);
496 if (!f.visIndex) {
497 f.visIndex = new Map();
498 for (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);
499 }
500 return f.visIndex.get(this.graph.lineN(line)) ?? -1;
501 }
503 /** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */
504 files(): Record<string, string> {
505 const out: Record<string, string> = {};
506 for (const path of this.paths()) if (this.#file(path).attrs.blob === null) out[path] = this.text(path);
507 return out;
508 }
510 /** Binary files, path -> the SHA-256 of their bytes, in path order. */
511 blobs(): Record<string, string> {
512 const out: Record<string, string> = {};
513 for (const path of this.paths()) {
514 const blob = this.#file(path).attrs.blob;
515 if (blob !== null) out[path] = blob;
516 }
517 return out;
518 }
520 /**
521 * About how many bytes of retained state this view costs (#46). This uses
522 * approximate ownership costs for containers and wrappers, not V8 heap
523 * measurements. Graph-owned strings and records count only as references.
524 * This method only inspects already-created state: it does not lay out files,
525 * populate lazy caches, enumerate the graph, or count temporary traversal
526 * containers and shared empty singleton payloads as retained view state.
527 */
528 weight(): number {
529 let n = this.#in.byteLength;
530 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength;
531 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
532 n += MAP_HEADER_BYTES; // #files exists from construction.
533 n += MAP_HEADER_BYTES; // #fileOf exists from construction.
534 if (this.#stable) n += this.#stable.retainedBytes;
535 else if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
536 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;
537 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
538 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
539 n += MAP_ENTRY_BYTES * this.#fileOf.size;
540 for (const f of this.#files.values()) {
541 n += MAP_ENTRY_BYTES;
542 if (f !== NO_FILE) {
543 // FileState and FileAttrs are per laid-out file; typed-array
544 // headers are separate from their payload byte lengths below.
545 n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
546 n += f.seq.byteLength + f.vis.byteLength;
547 n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
548 for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;
549 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;
550 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;
551 }
552 }
553 if (this.#linesCache)
554 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 100 * this.#linesCache.lines.length;
555 if (this.#seqCache) {
556 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 120 * this.#seqCache.seq.length;
557 for (const line of this.#seqCache.seq)
558 if (line.deletedBy !== NONE) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * line.deletedBy.length;
559 }
560 if (this.#textCache) n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 2 * this.#textCache.text.length;
561 return n;
562 }
564 #file(path: string, complete = false): FileState {
565 let state = this.#files.get(path);
566 if (!state || (complete && !state.complete)) {
567 state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;
568 this.#files.set(path, state);
569 }
570 return state;
571 }
574/** The files of a closed set, path -> text. */
575export function project(graph: Graph, set: Iterable<AtomId>): Record<string, string> {
576 return new View(graph, set).files();
579/** Where a node sits in a file's sequence: the index just after it (0 for a start node). */
580export function indexAfter(view: View, path: string, node: NodeId): number {
581 if (isStartNode(node)) return 0;
582 const i = view.sequenceIndex(path, node);
583 if (i < 0) throw new EngineError("not-in-view", `line ${node} is not in ${path} in this view`);
584 return i + 1;