Each shared version's changed files kept as it lands (disk-first 2; abso Capacity, version-files.ts). A version's files are decided from what it touched: visible lines in and out per file, their texts' balance, a running count of each file's visible lines for made and taken out, and mode and blob; a version with moves, drops or a closure change, or whose lines in and out have the same texts, is laid out as before. Views of consecutive versions share resolved moves and where lines sit (View.shareMoves). PathIndex.keep records them, into the shapes table when the host gives one. version-files.test.ts checks log, path logs and every show against the in-memory project through edits, new and removed files, same text put back, a move and a drop. On game's export all 27 harness answers now match Project.load: the log 70 ms (was over 30 s), a path's history 0.56 s (was over 30 s), the tree 3.1-3.4 s (was over 30 s); adoption about 85 s; heap 14.4 MB at most. Version 207 of shared· capacity· Oct 15 (2026-10-15 03:01 UTC) · Not checked
Each shared version's changed files kept as it lands (disk-first 2; abso Capacity, version-files.ts). A version's files are decided from what it touched: visible lines in and out per file, their texts' balance, a running count of each file's visible lines for made and taken out, and mode and blob; a version with moves, drops or a closure change, or whose lines in and out have the same texts, is laid out as before.
Views of consecutive versions share resolved moves and where lines sit (View.shareMoves). PathIndex.keep records them, into the shapes table when the host gives one. version-files.test.ts checks log, path logs and every show against the in-memory project through edits, new and removed files, same text put back, a move and a drop.
On game's export all 27 harness answers now match Project.load: the log 70 ms (was over 30 s), a path's history 0.56 s (was over 30 s), the tree 3.1-3.4 s (was over 30 s); adoption about 85 s; heap 14.4 MB at most.
Version 206 · Files at version 207
6 files, 291 lines added, 10 removed.
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -47,7 +47,7 @@
49 49 /** Structural read boundary shared by historical and transaction-bound projections. */ 50 − export type ReadView = Omit<Pick<View, keyof View>, "sameText"> & {
50 + export type ReadView = Omit<Pick<View, keyof View>, "sameText" | "visibleCount" | "shareMoves"> & { 51 51 sameText(path: string, other: ReadView): boolean; @@ -100,6 +100,8 @@
100 100 export const MOVED_LINES_KEPT = 20_000; 101 101 class TooManyMoves extends Error {} 102 102 const NO_LINES: readonly number[] = Object.freeze([]); 103 + /** Lines whose place views sharing moves remember, at most: about 40 bytes each. */ 104 + const SHARED_FILE_MEMO = 200_000; 105 107 * The projection of a closed set. Construct once, then ask for files, lines @@ -274,6 +276,25 @@
279 + /** Where lines sit, shared with views that took this one's moves (shareMoves): unbounded up to SHARED_FILE_MEMO. */ 280 + #fileOfShared: Map<number, string> | undefined; 283 + * Take another view's resolved moves (abso Capacity). Only for a set that differs from that view's by atoms that 284 + * move nothing, drop nothing and change no closure: its moves resolve the same, and every line it shares with that 285 + * view sits where it sat there. 287 + shareMoves(from: View): void { 288 + if (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves) return; 289 + if (from.#scratchMoves()) return; 290 + this.#relocs = from.#relocations(); 291 + this.#relocsN = from.#relocationsN(); 292 + this.#movesOnDisk = false; 293 + this.#movedFrom = from.#movedInFrom(); 294 + from.#fileOfShared ??= new Map(); 295 + this.#fileOfShared = from.#fileOfShared; 277 298 /** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */ 278 299 #scratchMoves(): boolean { 279 300 if (!this.#hasMoves || !this.graph.hasOrderedChildren) return false; @@ -349,13 +370,20 @@
349 370 while (file === undefined) { 350 371 if (cur < 0) file = graph.nodeOf(cur).slice(1); 352 − file = this.#fileOf.get(cur);
373 + file = (this.#fileOfShared ?? this.#fileOf).get(cur); 353 374 if (file === undefined) { 354 − path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;
375 + if (this.#fileOfShared) path.push(cur); 376 + else path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur; 355 377 cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur); 381 + const shared = this.#fileOfShared; 383 + if (shared.size > SHARED_FILE_MEMO) shared.clear(); 384 + for (const l of path) shared.set(l, file); 359 387 for (const l of path) { 360 388 if (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES) 361 389 this.#fileOf.delete(this.#fileOf.keys().next().value!); @@ -668,6 +696,11 @@
668 696 return [...this.#known]; 699 + /** How many visible lines a file has. */ 700 + visibleCount(path: string): number { 701 + return this.#file(path).vis.length; 671 704 /** Whether the file exists in this projection (has a visible line). */ 672 705 hasFile(path: string): boolean { 673 706 const attrs = this.attrs(path); --- a/worker/project/pathindex.ts
+++ b/worker/project/pathindex.ts
@@ -64,11 +64,34 @@
64 64 // Net changes only, as show has them: a file made and removed again before the publish isn't one (#37). 65 65 const touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]); 66 66 const files = [...touched].filter((path) => differs(before, after, path)).sort(); 67 + this.keep(space, version, files, before, after); 72 + * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the 73 + * version landed (version-files.ts, abso Capacity), which says which files existed before and after. 81 + exists?: ReadonlyMap<string, readonly [boolean, boolean]>, 83 + let cache = this.#filesCache.get(space); 84 + if (!cache) this.#filesCache.set(space, (cache = new Map())); 85 + let shapes = this.#shapeCache.get(space); 86 + if (!shapes) this.#shapeCache.set(space, (shapes = new Map())); 87 + const p = space.versions[version]!; 67 88 cache.set(version, files); 68 89 // Files made and taken out, and which of those are one file moved (#41): an atom that moves lines 69 90 // names both files, and a binary or empty file moved keeps its blob. 70 − const made = files.filter((f) => !before.hasFile(f) && after.hasFile(f));
71 − const gone = files.filter((f) => before.hasFile(f) && !after.hasFile(f));
91 + const had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f); 92 + const has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f); 93 + const made = files.filter((f) => !had(f) && has(f)); 94 + const gone = files.filter((f) => had(f) && !has(f)); 72 95 const moves: [string, string][] = []; 73 96 if (made.length > 0 && gone.length > 0) { 74 97 const movers = new Set(this.#deps.graph.movers()); @@ -87,7 +110,6 @@
88 111 shapes.set(version, { made, gone, moves }); 89 112 this.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves })); 90 − return files;
93 115 /** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */ --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -12,8 +12,16 @@
12 12 // agents publish into under the same rules, and that lands in shared as one 13 13 // unit. Every agent works in one space at a time. 15 − import { type Atom, type AtomId, EngineError, Graph, type LineId, type ReadView } from "../../src/engine/index.ts";
23 + } from "../../src/engine/index.ts"; 19 27 type GraphStateStorage, @@ -176,11 +184,21 @@
176 184 import { recordFile as hostedRecordFile, type RecordFileHost } from "./record-file.ts"; 177 185 import { refusals as hostedRefusals, refuse as hostedRefuse, type RefuseHost } from "./refuse.ts"; 178 186 import { restore as hostedRestore, type RestoreHost } from "./restore.ts"; 179 − import { indexClosure, last, Membership, SHARED, SharedAdditions, type Space, sharedBaseOf } from "./spaces.ts";
196 + } from "./spaces.ts"; 180 197 import { starting as hostedStarting, type StartingHost } from "./start.ts"; 181 198 import { switchTo as hostedSwitchTo, type SwitchHost } from "./switch-to.ts"; 182 199 import { sync as hostedSync, type SyncHost } from "./sync.ts"; 183 200 import { treeHash } from "./tree.ts"; 201 + import { versionFiles as keptVersionFiles, type VersionFilesHost } from "./version-files.ts"; 186 204 compare as versionCompare, @@ -1588,8 +1606,25 @@
1588 1606 this.#applyEntry(entry, at, keptTime); 1589 1607 // Shared's closure, kept per version on disk (abso Capacity): after the entry, since its atoms join the graph after 1590 1608 // its membership. 1591 − if (this.#writerStorage && (this.#spaces.get(SHARED)?.versions.length ?? 0) > versionsBefore)
1592 − indexClosure(this.#writerStorage, this.#spaces.get(SHARED)!, this.#graph);
1609 + if (this.#writerStorage && (this.#spaces.get(SHARED)?.versions.length ?? 0) > versionsBefore) { 1610 + const shared = this.#spaces.get(SHARED)!; 1611 + const storage = this.#writerStorage; 1612 + indexClosure(storage, shared, this.#graph); 1613 + // Each new version's changed files, from what it touched (version-files.ts): kept in the shapes table when 1614 + // the host gives one, so the log, a path's history and the tree never lay versions out to find them. 1615 + const host: VersionFilesHost = { 1617 + graph: this.#graph, 1618 + at: (v) => this.#at(shared, v), 1619 + viewAt: (v) => this.#viewAt(shared, v), 1620 + empty: () => new View(this.#graph, []), 1621 + closureChangedAt: (v) => closureChangedAt(storage, v), 1623 + for (let v = versionsBefore; v < shared.versions.length; v++) { 1624 + const { files, exists } = keptVersionFiles(host, shared.versions[v]!); 1625 + this.#paths.keep(shared, v, files, v === 0 ? host.empty() : host.viewAt(v - 1), host.viewAt(v), exists); 1594 1629 // Positions by version: where each version of shared was made. 1595 1630 const versionsAfter = this.#spaces.get(SHARED)?.versions.length ?? 0; --- a/worker/project/spaces.ts
+++ b/worker/project/spaces.ts
@@ -133,6 +133,13 @@
133 133 return membershipEvent(storage, CLOSURE, atom, version); 136 + /** Whether shared's closure changed at this version: its newest closure event is from it. */ 137 + export function closureChangedAt(storage: GraphStateStorage, version: number): boolean { 138 + const order = storedArray<MembershipEvent>(storage, `${CLOSURE}:events`); 139 + const length = order.length; 140 + return length > 0 && order[length - 1]!.version === version; 136 143 /** The newest shared version whose closure is indexed, or undefined when this writer has none. */ 137 144 export function closureIndexed(storage: GraphStateStorage): number | undefined { 138 145 return storage.read(`${CLOSURE}:meta`, "indexed") as number | undefined; --- /dev/null
+++ b/worker/project/test/version-files.test.ts
@@ -0,0 +1,55 @@
1 + // Each version's changed files kept as it lands (abso Capacity, version-files.ts): a writer-backed project's log, 2 + // path histories and shows match the in-memory project's through edits, new and removed files, a line taken out and 3 + // put back with the same text, moves and a drop. 5 + import assert from "node:assert/strict"; 6 + import { test } from "node:test"; 7 + import { storedArray } from "../../../src/engine/state-source.ts"; 8 + import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 9 + import { MemoryStore, Project } from "../project.ts"; 10 + import type { Entry, ProjectStore } from "../project-types.ts"; 11 + import { countedVersion } from "../version-files.ts"; 14 + const storage = new SqlState(); 15 + const entries = storedArray<Entry>(storage, "canonical-log"); 16 + const store: ProjectStore = { 18 + for (const entry of batch) entries.push(entry); 21 + return { storage, project: new Project(store, { writerStorage: storage }) }; 24 + function history(project: Project) { 26 + const publish = (agent: string, files: Record<string, string | null>, message: string) => { 27 + project.record(agent, files, ++t); 28 + assert.equal(project.publish(agent, message, ++t).ok, true, message); 30 + project.init({ "a.txt": "one\ntwo\nthree\n", "b.txt": "bee\n", "gone.txt": "soon\n" }, t); 31 + publish("ann", { "a.txt": "one\nTWO\nthree\n" }, "Edit a"); 32 + publish("ann", { "c.txt": "new file\n" }, "Make c"); 33 + publish("ann", { "gone.txt": null }, "Remove gone"); 34 + publish("ann", { "b.txt": "" }, "Empty b"); 35 + publish("ann", { "b.txt": "bee\n" }, "Same text back in b"); 36 + project.move("ann", "c.txt", "d.txt", ++t); 37 + assert.equal(project.publish("ann", "Move c to d", ++t).ok, true); 38 + publish("bob", { "d.txt": "new file\nmore\n" }, "Edit d"); 39 + publish("cy", { "e.txt": "e\n" }, "Make e"); 40 + const dropped = project.drop("cy", [project.version], "Drop e", ++t); 41 + assert.equal(dropped.ok, true); 42 + publish("bob", { "a.txt": "one\nTWO\nthree\nfour\n" }, "Last"); 45 + test("kept version files match the in-memory project's log, paths and shows", () => { 46 + const oracle = new Project(new MemoryStore()); 47 + const { storage, project } = writer(); 50 + assert.equal(countedVersion(storage), oracle.version); 51 + assert.deepEqual(project.log({ limit: 100 }), oracle.log({ limit: 100 })); 52 + for (const path of ["a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "gone.txt"]) 53 + assert.deepEqual(project.log({ limit: 100, path }), oracle.log({ limit: 100, path }), path); 54 + for (let v = 0; v <= oracle.version; v++) assert.deepEqual(project.show(v), oracle.show(v), `version ${v}`); --- /dev/null
+++ b/worker/project/version-files.ts
@@ -0,0 +1,129 @@
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. 10 + import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts"; 11 + import type { GraphStateStorage } from "../../src/engine/state-source.ts"; 12 + import { touchedFiles } from "./collisions.ts"; 13 + import type { Published } from "./spaces.ts"; 14 + import { differs } from "./view-changes.ts"; 16 + const COUNTS = "head-visible"; 17 + const META = "head-visible:meta"; 19 + /** The shared version the running counts describe, or undefined when this writer keeps none. */ 20 + export function countedVersion(storage: GraphStateStorage): number | undefined { 21 + return storage.read(META, "version") as number | undefined; 24 + export 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; 31 + /** Whether shared's closure changed at this version (spaces.ts). */ 32 + closureChangedAt(version: number): boolean; 35 + /** A file's visible lines in a view: a laid-out count, for the slow path. */ 36 + function 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; 41 + /** The changed files of shared's version `v`, sorted, and which existed before and after; the running counts moved to `v`. */ 42 + export function versionFiles( 43 + host: VersionFilesHost, 45 + ): { files: string[]; exists?: Map<string, readonly [boolean, boolean]> } { 46 + const { storage, graph } = host; 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); 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]); 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); 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; 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 })); 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); 94 + c.texts.set(text, (c.texts.get(text) ?? 0) + 1); 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); 103 + c.texts.set(text, (c.texts.get(text) ?? 0) - 1); 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); 123 + exists.set(path, [had, has]); 125 + if (now !== was) storage.write(COUNTS, path, now); 127 + storage.write(META, "version", v); 128 + return { files: files.sort(), exists };