{"version":207,"agent":"capacity","message":"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.","time":1792033264009,"timeIso":"2026-10-15T03:01:04.009Z","changes":6,"change":3492,"files":["src/engine/view.ts","worker/project/pathindex.ts","worker/project/project.ts","worker/project/spaces.ts","worker/project/test/version-files.test.ts","worker/project/version-files.ts"],"draft":null,"diff":[{"path":"src/engine/view.ts","added":36,"removed":3,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -47,7 +47,7 @@\n }\n \n /** Structural read boundary shared by historical and transaction-bound projections. */\n-export type ReadView = Omit<Pick<View, keyof View>, \"sameText\"> & {\n+export type ReadView = Omit<Pick<View, keyof View>, \"sameText\" | \"visibleCount\" | \"shareMoves\"> & {\n \tsameText(path: string, other: ReadView): boolean;\n };\n \n@@ -100,6 +100,8 @@\n export const MOVED_LINES_KEPT = 20_000;\n class TooManyMoves extends Error {}\n const NO_LINES: readonly number[] = Object.freeze([]);\n+/** Lines whose place views sharing moves remember, at most: about 40 bytes each. */\n+const SHARED_FILE_MEMO = 200_000;\n \n /**\n  * The projection of a closed set. Construct once, then ask for files, lines\n@@ -274,6 +276,25 @@\n \t\treturn relocs;\n \t}\n \n+\t/** Where lines sit, shared with views that took this one's moves (shareMoves): unbounded up to SHARED_FILE_MEMO. */\n+\t#fileOfShared: Map<number, string> | undefined;\n+\n+\t/**\n+\t * Take another view's resolved moves (abso Capacity). Only for a set that differs from that view's by atoms that\n+\t * move nothing, drop nothing and change no closure: its moves resolve the same, and every line it shares with that\n+\t * view sits where it sat there.\n+\t */\n+\tshareMoves(from: View): void {\n+\t\tif (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves) return;\n+\t\tif (from.#scratchMoves()) return;\n+\t\tthis.#relocs = from.#relocations();\n+\t\tthis.#relocsN = from.#relocationsN();\n+\t\tthis.#movesOnDisk = false;\n+\t\tthis.#movedFrom = from.#movedInFrom();\n+\t\tfrom.#fileOfShared ??= new Map();\n+\t\tthis.#fileOfShared = from.#fileOfShared;\n+\t}\n+\n \t/** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */\n \t#scratchMoves(): boolean {\n \t\tif (!this.#hasMoves || !this.graph.hasOrderedChildren) return false;\n@@ -349,13 +370,20 @@\n \t\twhile (file === undefined) {\n \t\t\tif (cur < 0) file = graph.nodeOf(cur).slice(1);\n \t\t\telse {\n-\t\t\t\tfile = this.#fileOf.get(cur);\n+\t\t\t\tfile = (this.#fileOfShared ?? this.#fileOf).get(cur);\n \t\t\t\tif (file === undefined) {\n-\t\t\t\t\tpath[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;\n+\t\t\t\t\tif (this.#fileOfShared) path.push(cur);\n+\t\t\t\t\telse path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;\n \t\t\t\t\tcur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n+\t\tconst shared = this.#fileOfShared;\n+\t\tif (shared) {\n+\t\t\tif (shared.size > SHARED_FILE_MEMO) shared.clear();\n+\t\t\tfor (const l of path) shared.set(l, file);\n+\t\t\treturn file;\n+\t\t}\n \t\tfor (const l of path) {\n \t\t\tif (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES)\n \t\t\t\tthis.#fileOf.delete(this.#fileOf.keys().next().value!);\n@@ -668,6 +696,11 @@\n \t\treturn [...this.#known];\n \t}\n \n+\t/** How many visible lines a file has. */\n+\tvisibleCount(path: string): number {\n+\t\treturn this.#file(path).vis.length;\n+\t}\n+\n \t/** Whether the file exists in this projection (has a visible line). */\n \thasFile(path: string): boolean {\n \t\tconst attrs = this.attrs(path);"},{"path":"worker/project/pathindex.ts","added":25,"removed":3,"diff":"--- a/worker/project/pathindex.ts\n+++ b/worker/project/pathindex.ts\n@@ -64,11 +64,34 @@\n \t\t// Net changes only, as show has them: a file made and removed again before the publish isn't one (#37).\n \t\tconst touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]);\n \t\tconst files = [...touched].filter((path) => differs(before, after, path)).sort();\n+\t\tthis.keep(space, version, files, before, after);\n+\t\treturn files;\n+\t}\n+\n+\t/**\n+\t * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the\n+\t * version landed (version-files.ts, abso Capacity), which says which files existed before and after.\n+\t */\n+\tkeep(\n+\t\tspace: Space,\n+\t\tversion: number,\n+\t\tfiles: string[],\n+\t\tbefore: ReadView,\n+\t\tafter: ReadView,\n+\t\texists?: ReadonlyMap<string, readonly [boolean, boolean]>,\n+\t): void {\n+\t\tlet cache = this.#filesCache.get(space);\n+\t\tif (!cache) this.#filesCache.set(space, (cache = new Map()));\n+\t\tlet shapes = this.#shapeCache.get(space);\n+\t\tif (!shapes) this.#shapeCache.set(space, (shapes = new Map()));\n+\t\tconst p = space.versions[version]!;\n \t\tcache.set(version, files);\n \t\t// Files made and taken out, and which of those are one file moved (#41): an atom that moves lines\n \t\t// names both files, and a binary or empty file moved keeps its blob.\n-\t\tconst made = files.filter((f) => !before.hasFile(f) && after.hasFile(f));\n-\t\tconst gone = files.filter((f) => before.hasFile(f) && !after.hasFile(f));\n+\t\tconst had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f);\n+\t\tconst has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f);\n+\t\tconst made = files.filter((f) => !had(f) && has(f));\n+\t\tconst gone = files.filter((f) => had(f) && !has(f));\n \t\tconst moves: [string, string][] = [];\n \t\tif (made.length > 0 && gone.length > 0) {\n \t\t\tconst movers = new Set(this.#deps.graph.movers());\n@@ -87,7 +110,6 @@\n \t\t}\n \t\tshapes.set(version, { made, gone, moves });\n \t\tthis.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves }));\n-\t\treturn files;\n \t}\n \n \t/** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */"},{"path":"worker/project/project.ts","added":39,"removed":4,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -12,8 +12,16 @@\n // agents publish into under the same rules, and that lands in shared as one\n // unit. Every agent works in one space at a time.\n \n-import { type Atom, type AtomId, EngineError, Graph, type LineId, type ReadView } from \"../../src/engine/index.ts\";\n import {\n+\ttype Atom,\n+\ttype AtomId,\n+\tEngineError,\n+\tGraph,\n+\ttype LineId,\n+\ttype ReadView,\n+\tView,\n+} from \"../../src/engine/index.ts\";\n+import {\n \tCachedStateStorage,\n \tdifferenceSet,\n \ttype GraphStateStorage,\n@@ -176,11 +184,21 @@\n import { recordFile as hostedRecordFile, type RecordFileHost } from \"./record-file.ts\";\n import { refusals as hostedRefusals, refuse as hostedRefuse, type RefuseHost } from \"./refuse.ts\";\n import { restore as hostedRestore, type RestoreHost } from \"./restore.ts\";\n-import { indexClosure, last, Membership, SHARED, SharedAdditions, type Space, sharedBaseOf } from \"./spaces.ts\";\n+import {\n+\tclosureChangedAt,\n+\tindexClosure,\n+\tlast,\n+\tMembership,\n+\tSHARED,\n+\tSharedAdditions,\n+\ttype Space,\n+\tsharedBaseOf,\n+} from \"./spaces.ts\";\n import { starting as hostedStarting, type StartingHost } from \"./start.ts\";\n import { switchTo as hostedSwitchTo, type SwitchHost } from \"./switch-to.ts\";\n import { sync as hostedSync, type SyncHost } from \"./sync.ts\";\n import { treeHash } from \"./tree.ts\";\n+import { versionFiles as keptVersionFiles, type VersionFilesHost } from \"./version-files.ts\";\n import {\n \ttype LogHost,\n \tcompare as versionCompare,\n@@ -1588,8 +1606,25 @@\n \t\t\tthis.#applyEntry(entry, at, keptTime);\n \t\t\t// Shared's closure, kept per version on disk (abso Capacity): after the entry, since its atoms join the graph after\n \t\t\t// its membership.\n-\t\t\tif (this.#writerStorage && (this.#spaces.get(SHARED)?.versions.length ?? 0) > versionsBefore)\n-\t\t\t\tindexClosure(this.#writerStorage, this.#spaces.get(SHARED)!, this.#graph);\n+\t\t\tif (this.#writerStorage && (this.#spaces.get(SHARED)?.versions.length ?? 0) > versionsBefore) {\n+\t\t\t\tconst shared = this.#spaces.get(SHARED)!;\n+\t\t\t\tconst storage = this.#writerStorage;\n+\t\t\t\tindexClosure(storage, shared, this.#graph);\n+\t\t\t\t// Each new version's changed files, from what it touched (version-files.ts): kept in the shapes table when\n+\t\t\t\t// the host gives one, so the log, a path's history and the tree never lay versions out to find them.\n+\t\t\t\tconst host: VersionFilesHost = {\n+\t\t\t\t\tstorage,\n+\t\t\t\t\tgraph: this.#graph,\n+\t\t\t\t\tat: (v) => this.#at(shared, v),\n+\t\t\t\t\tviewAt: (v) => this.#viewAt(shared, v),\n+\t\t\t\t\tempty: () => new View(this.#graph, []),\n+\t\t\t\t\tclosureChangedAt: (v) => closureChangedAt(storage, v),\n+\t\t\t\t};\n+\t\t\t\tfor (let v = versionsBefore; v < shared.versions.length; v++) {\n+\t\t\t\t\tconst { files, exists } = keptVersionFiles(host, shared.versions[v]!);\n+\t\t\t\t\tthis.#paths.keep(shared, v, files, v === 0 ? host.empty() : host.viewAt(v - 1), host.viewAt(v), exists);\n+\t\t\t\t}\n+\t\t\t}\n \t\t} finally {\n \t\t\t// Positions by version: where each version of shared was made.\n \t\t\tconst versionsAfter = this.#spaces.get(SHARED)?.versions.length ?? 0;"},{"path":"worker/project/spaces.ts","added":7,"removed":0,"diff":"--- a/worker/project/spaces.ts\n+++ b/worker/project/spaces.ts\n@@ -133,6 +133,13 @@\n \treturn membershipEvent(storage, CLOSURE, atom, version);\n }\n \n+/** Whether shared's closure changed at this version: its newest closure event is from it. */\n+export function closureChangedAt(storage: GraphStateStorage, version: number): boolean {\n+\tconst order = storedArray<MembershipEvent>(storage, `${CLOSURE}:events`);\n+\tconst length = order.length;\n+\treturn length > 0 && order[length - 1]!.version === version;\n+}\n+\n /** The newest shared version whose closure is indexed, or undefined when this writer has none. */\n export function closureIndexed(storage: GraphStateStorage): number | undefined {\n \treturn storage.read(`${CLOSURE}:meta`, \"indexed\") as number | undefined;"},{"path":"worker/project/test/version-files.test.ts","added":55,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/version-files.test.ts\n@@ -0,0 +1,55 @@\n+// Each version's changed files kept as it lands (abso Capacity, version-files.ts): a writer-backed project's log,\n+// path histories and shows match the in-memory project's through edits, new and removed files, a line taken out and\n+// put back with the same text, moves and a drop.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { storedArray } from \"../../../src/engine/state-source.ts\";\n+import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n+import { MemoryStore, Project } from \"../project.ts\";\n+import type { Entry, ProjectStore } from \"../project-types.ts\";\n+import { countedVersion } from \"../version-files.ts\";\n+\n+function writer() {\n+\tconst storage = new SqlState();\n+\tconst entries = storedArray<Entry>(storage, \"canonical-log\");\n+\tconst store: ProjectStore = {\n+\t\tappend(batch) {\n+\t\t\tfor (const entry of batch) entries.push(entry);\n+\t\t},\n+\t};\n+\treturn { storage, project: new Project(store, { writerStorage: storage }) };\n+}\n+\n+function history(project: Project) {\n+\tlet t = 0;\n+\tconst publish = (agent: string, files: Record<string, string | null>, message: string) => {\n+\t\tproject.record(agent, files, ++t);\n+\t\tassert.equal(project.publish(agent, message, ++t).ok, true, message);\n+\t};\n+\tproject.init({ \"a.txt\": \"one\\ntwo\\nthree\\n\", \"b.txt\": \"bee\\n\", \"gone.txt\": \"soon\\n\" }, t);\n+\tpublish(\"ann\", { \"a.txt\": \"one\\nTWO\\nthree\\n\" }, \"Edit a\");\n+\tpublish(\"ann\", { \"c.txt\": \"new file\\n\" }, \"Make c\");\n+\tpublish(\"ann\", { \"gone.txt\": null }, \"Remove gone\");\n+\tpublish(\"ann\", { \"b.txt\": \"\" }, \"Empty b\");\n+\tpublish(\"ann\", { \"b.txt\": \"bee\\n\" }, \"Same text back in b\");\n+\tproject.move(\"ann\", \"c.txt\", \"d.txt\", ++t);\n+\tassert.equal(project.publish(\"ann\", \"Move c to d\", ++t).ok, true);\n+\tpublish(\"bob\", { \"d.txt\": \"new file\\nmore\\n\" }, \"Edit d\");\n+\tpublish(\"cy\", { \"e.txt\": \"e\\n\" }, \"Make e\");\n+\tconst dropped = project.drop(\"cy\", [project.version], \"Drop e\", ++t);\n+\tassert.equal(dropped.ok, true);\n+\tpublish(\"bob\", { \"a.txt\": \"one\\nTWO\\nthree\\nfour\\n\" }, \"Last\");\n+}\n+\n+test(\"kept version files match the in-memory project's log, paths and shows\", () => {\n+\tconst oracle = new Project(new MemoryStore());\n+\tconst { storage, project } = writer();\n+\thistory(oracle);\n+\thistory(project);\n+\tassert.equal(countedVersion(storage), oracle.version);\n+\tassert.deepEqual(project.log({ limit: 100 }), oracle.log({ limit: 100 }));\n+\tfor (const path of [\"a.txt\", \"b.txt\", \"c.txt\", \"d.txt\", \"e.txt\", \"gone.txt\"])\n+\t\tassert.deepEqual(project.log({ limit: 100, path }), oracle.log({ limit: 100, path }), path);\n+\tfor (let v = 0; v <= oracle.version; v++) assert.deepEqual(project.show(v), oracle.show(v), `version ${v}`);\n+});"},{"path":"worker/project/version-files.ts","added":129,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/version-files.ts\n@@ -0,0 +1,129 @@\n+// Each version of shared's changed files, worked out as the version lands from what it touched (disk-first 2;\n+// abso Capacity). The log, a path's history and the tree read these lists; working them out from two layouts of every\n+// touched file per version cost game's whole history 460 s to adopt. Here a version costs about its own size:\n+// - a file it touched differs when its visible lines' count or texts changed, or its mode or blob did;\n+// - whether a file exists before and after comes from a running count of each file's visible lines;\n+// - only a version whose added and removed lines have the very same texts, or one with moves, drops or a change to\n+//   shared's closure, is laid out as before.\n+// The answers are exactly differs() in view-changes.ts.\n+\n+import { type AtomId, type Graph, type ReadView, View } from \"../../src/engine/index.ts\";\n+import type { GraphStateStorage } from \"../../src/engine/state-source.ts\";\n+import { touchedFiles } from \"./collisions.ts\";\n+import type { Published } from \"./spaces.ts\";\n+import { differs } from \"./view-changes.ts\";\n+\n+const COUNTS = \"head-visible\";\n+const META = \"head-visible:meta\";\n+\n+/** The shared version the running counts describe, or undefined when this writer keeps none. */\n+export function countedVersion(storage: GraphStateStorage): number | undefined {\n+\treturn storage.read(META, \"version\") as number | undefined;\n+}\n+\n+export interface VersionFilesHost {\n+\treadonly storage: GraphStateStorage;\n+\treadonly graph: Graph;\n+\t/** Shared's set at a version (closed) and its view. */\n+\tat(version: number): ReadonlySet<AtomId>;\n+\tviewAt(version: number): ReadView;\n+\tempty(): ReadView;\n+\t/** Whether shared's closure changed at this version (spaces.ts). */\n+\tclosureChangedAt(version: number): boolean;\n+}\n+\n+/** A file's visible lines in a view: a laid-out count, for the slow path. */\n+function visibleCount(view: ReadView, path: string): number {\n+\tconst counted = (view as { visibleCount?(path: string): number }).visibleCount;\n+\treturn counted ? counted.call(view, path) : view.lines(path).length;\n+}\n+\n+/** The changed files of shared's version `v`, sorted, and which existed before and after; the running counts moved to `v`. */\n+export function versionFiles(\n+\thost: VersionFilesHost,\n+\tp: Published,\n+): { files: string[]; exists?: Map<string, readonly [boolean, boolean]> } {\n+\tconst { storage, graph } = host;\n+\tconst v = p.version;\n+\tconst counted = countedVersion(storage);\n+\tconst atoms = [...p.atoms, ...(p.dropped ?? [])];\n+\tconst before = v === 0 ? host.empty() : host.viewAt(v - 1);\n+\tconst after = host.viewAt(v);\n+\tconst fast =\n+\t\tv > 0 &&\n+\t\tcounted === v - 1 &&\n+\t\t!p.dropped?.length &&\n+\t\t!p.atoms.some((id) => graph.atom(id).moves?.length) &&\n+\t\t!host.closureChangedAt(v);\n+\t// The same moves as the version before: share them, and where lines sit, instead of resolving them again.\n+\tif (fast && before instanceof View && after instanceof View) after.shareMoves(before);\n+\tconst touched = touchedFiles(graph, atoms, [before, after]);\n+\tlet files: string[];\n+\tif (!fast) {\n+\t\tfiles = [...touched].filter((path) => differs(before, after, path)).sort();\n+\t\t// Counts start with the first version; a writer adopted before has none and keeps none.\n+\t\tif (counted === v - 1 || (v === 0 && counted === undefined)) {\n+\t\t\tfor (const path of files) storage.write(COUNTS, path, visibleCount(after, path));\n+\t\t\tstorage.write(META, \"version\", v);\n+\t\t}\n+\t\treturn { files };\n+\t}\n+\tconst beforeSet = host.at(v - 1);\n+\tconst afterSet = host.at(v);\n+\tconst visibleIn = (set: ReadonlySet<AtomId>, n: number) => {\n+\t\tfor (const seq of graph.eachDeleterSeqOf(n)) if (set.has(graph.idOfSeq(seq))) return false;\n+\t\treturn true;\n+\t};\n+\tconst fileOf = (view: ReadView, line: string, n: number) =>\n+\t\tview.hasMoves() ? view.fileOf(line) : graph.writtenIn(n);\n+\t// Per file: visible lines added minus removed, and the texts' balance.\n+\tconst changes = new Map<string, { net: number; texts: Map<string, number>; lines: number }>();\n+\tconst change = (path: string) => {\n+\t\tlet c = changes.get(path);\n+\t\tif (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0 }));\n+\t\treturn c;\n+\t};\n+\tfor (const id of p.atoms) {\n+\t\tfor (const line of graph.eachInsertedLine(id)) {\n+\t\t\tconst n = graph.lineN(line);\n+\t\t\tif (!visibleIn(afterSet, n)) continue;\n+\t\t\tconst c = change(fileOf(after, line, n));\n+\t\t\tconst text = graph.textOf(n);\n+\t\t\tc.net++;\n+\t\t\tc.lines++;\n+\t\t\tc.texts.set(text, (c.texts.get(text) ?? 0) + 1);\n+\t\t}\n+\t\tfor (const line of graph.deletedLines(id)) {\n+\t\t\tconst n = graph.lineN(line);\n+\t\t\tif (!beforeSet.has(graph.idOfSeq(graph.writerSeqOf(n))) || !visibleIn(beforeSet, n)) continue;\n+\t\t\tconst c = change(fileOf(before, line, n));\n+\t\t\tconst text = graph.textOf(n);\n+\t\t\tc.net--;\n+\t\t\tc.lines++;\n+\t\t\tc.texts.set(text, (c.texts.get(text) ?? 0) - 1);\n+\t\t}\n+\t}\n+\tfiles = [];\n+\tconst exists = new Map<string, readonly [boolean, boolean]>();\n+\tfor (const path of touched) {\n+\t\tconst was = (storage.read(COUNTS, path) as number | undefined) ?? 0;\n+\t\tconst c = changes.get(path);\n+\t\tconst now = was + (c?.net ?? 0);\n+\t\tconst b = before.attrs(path);\n+\t\tconst a = after.attrs(path);\n+\t\tconst had = b.exists || b.link !== null || b.blob !== null || was > 0;\n+\t\tconst has = a.exists || a.link !== null || a.blob !== null || now > 0;\n+\t\tlet differ = had !== has || b.exec !== a.exec || b.link !== a.link || b.blob !== a.blob;\n+\t\tif (!differ && had && c && c.lines > 0) {\n+\t\t\t// Different texts in or out: the file's lines changed. The same texts in and out: compare the files.\n+\t\t\tdiffer = [...c.texts.values()].some((count) => count !== 0) || !before.sameText(path, after);\n+\t\t}\n+\t\tif (differ) {\n+\t\t\tfiles.push(path);\n+\t\t\texists.set(path, [had, has]);\n+\t\t}\n+\t\tif (now !== was) storage.write(COUNTS, path, now);\n+\t}\n+\tstorage.write(META, \"version\", v);\n+\treturn { files: files.sort(), exists };\n+}"}],"ci":null}