- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/version-files.ts?version=227&format=json
129 linescapacity 100%Last change capacity in version 207, 1 version changed it
You're reading version 227. The newest is 318, and 2 versions since then changed this file.
1// Each version of shared's changed files, worked out as the version lands from what it touched (disk-first 2;
2// abso Capacity). The log, a path's history and the tree read these lists; working them out from two layouts of every
3// touched file per version cost game's whole history 460 s to adopt. Here a version costs about its own size:
4// - a file it touched differs when its visible lines' count or texts changed, or its mode or blob did;
5// - whether a file exists before and after comes from a running count of each file's visible lines;
6// - only a version whose added and removed lines have the very same texts, or one with moves, drops or a change to
7// shared's closure, is laid out as before.
8// The answers are exactly differs() in view-changes.ts.
10import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
11import type { GraphStateStorage } from "../../src/engine/state-source.ts";
12import { touchedFiles } from "./collisions.ts";
13import type { Published } from "./spaces.ts";
14import { differs } from "./view-changes.ts";
16const COUNTS = "head-visible";
17const META = "head-visible:meta";
19/** The shared version the running counts describe, or undefined when this writer keeps none. */
20export function countedVersion(storage: GraphStateStorage): number | undefined {
21 return storage.read(META, "version") as number | undefined;
22}
24export interface VersionFilesHost {
25 readonly storage: GraphStateStorage;
26 readonly graph: Graph;
27 /** Shared's set at a version (closed) and its view. */
28 at(version: number): ReadonlySet<AtomId>;
29 viewAt(version: number): ReadView;
30 empty(): ReadView;
31 /** Whether shared's closure changed at this version (spaces.ts). */
32 closureChangedAt(version: number): boolean;
33}
35/** A file's visible lines in a view: a laid-out count, for the slow path. */
36function visibleCount(view: ReadView, path: string): number {
37 const counted = (view as { visibleCount?(path: string): number }).visibleCount;
38 return counted ? counted.call(view, path) : view.lines(path).length;
39}
41/** The changed files of shared's version `v`, sorted, and which existed before and after; the running counts moved to `v`. */
42export function versionFiles(
43 host: VersionFilesHost,
44 p: Published,
45): { files: string[]; exists?: Map<string, readonly [boolean, boolean]> } {
46 const { storage, graph } = host;
47 const v = p.version;
48 const counted = countedVersion(storage);
49 const atoms = [...p.atoms, ...(p.dropped ?? [])];
50 const before = v === 0 ? host.empty() : host.viewAt(v - 1);
51 const after = host.viewAt(v);
52 const fast =
53 v > 0 &&
54 counted === v - 1 &&
55 !p.dropped?.length &&
56 !p.atoms.some((id) => graph.atom(id).moves?.length) &&
57 !host.closureChangedAt(v);
58 // The same moves as the version before: share them, and where lines sit, instead of resolving them again.
59 if (fast && before instanceof View && after instanceof View) after.shareMoves(before);
60 const touched = touchedFiles(graph, atoms, [before, after]);
61 let files: string[];
62 if (!fast) {
63 files = [...touched].filter((path) => differs(before, after, path)).sort();
64 // Counts start with the first version; a writer adopted before has none and keeps none.
65 if (counted === v - 1 || (v === 0 && counted === undefined)) {
66 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path));
67 storage.write(META, "version", v);
68 }
69 return { files };
70 }
71 const beforeSet = host.at(v - 1);
72 const afterSet = host.at(v);
73 const visibleIn = (set: ReadonlySet<AtomId>, n: number) => {
74 for (const seq of graph.eachDeleterSeqOf(n)) if (set.has(graph.idOfSeq(seq))) return false;
75 return true;
76 };
77 const fileOf = (view: ReadView, line: string, n: number) =>
78 view.hasMoves() ? view.fileOf(line) : graph.writtenIn(n);
79 // Per file: visible lines added minus removed, and the texts' balance.
80 const changes = new Map<string, { net: number; texts: Map<string, number>; lines: number }>();
81 const change = (path: string) => {
82 let c = changes.get(path);
83 if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0 }));
84 return c;
85 };
86 for (const id of p.atoms) {
87 for (const line of graph.eachInsertedLine(id)) {
88 const n = graph.lineN(line);
89 if (!visibleIn(afterSet, n)) continue;
90 const c = change(fileOf(after, line, n));
91 const text = graph.textOf(n);
92 c.net++;
93 c.lines++;
94 c.texts.set(text, (c.texts.get(text) ?? 0) + 1);
95 }
96 for (const line of graph.deletedLines(id)) {
97 const n = graph.lineN(line);
98 if (!beforeSet.has(graph.idOfSeq(graph.writerSeqOf(n))) || !visibleIn(beforeSet, n)) continue;
99 const c = change(fileOf(before, line, n));
100 const text = graph.textOf(n);
101 c.net--;
102 c.lines++;
103 c.texts.set(text, (c.texts.get(text) ?? 0) - 1);
104 }
105 }
106 files = [];
107 const exists = new Map<string, readonly [boolean, boolean]>();
108 for (const path of touched) {
109 const was = (storage.read(COUNTS, path) as number | undefined) ?? 0;
110 const c = changes.get(path);
111 const now = was + (c?.net ?? 0);
112 const b = before.attrs(path);
113 const a = after.attrs(path);
114 const had = b.exists || b.link !== null || b.blob !== null || was > 0;
115 const has = a.exists || a.link !== null || a.blob !== null || now > 0;
116 let differ = had !== has || b.exec !== a.exec || b.link !== a.link || b.blob !== a.blob;
117 if (!differ && had && c && c.lines > 0) {
118 // Different texts in or out: the file's lines changed. The same texts in and out: compare the files.
119 differ = [...c.texts.values()].some((count) => count !== 0) || !before.sameText(path, after);
120 }
121 if (differ) {
122 files.push(path);
123 exists.set(path, [had, has]);
124 }
125 if (now !== was) storage.write(COUNTS, path, now);
126 }
127 storage.write(META, "version", v);
128 return { files: files.sort(), exists };
129}