{"draft":null,"from":204,"to":205,"files":[{"path":"src/engine/view.ts","added":46,"removed":10,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -12,7 +12,7 @@\n import type { Graph, RelocationInfo } from \"./graph.ts\";\n import { isStartNode } from \"./keys.ts\";\n import { resolveScratchMoves, scratchLayoutNodes } from \"./relocation-scratch.ts\";\n-import { StableSet } from \"./state-source.ts\";\n+import { isClosedSet, StableSet } from \"./state-source.ts\";\n import { joinLines } from \"./text.ts\";\n import { type AtomId, type AtomSet, EngineError, type LineId, type NodeId } from \"./types.ts\";\n \n@@ -91,6 +91,13 @@\n const SET_ENTRY_BYTES = 32;\n const REFERENCE_BYTES = 8;\n const FILE_LOCATION_MEMO_ENTRIES = 256;\n+/**\n+ * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on\n+ * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that.\n+ */\n+export const MOVED_LINES_KEPT = 20_000;\n+class TooManyMoves extends Error {}\n+const NO_LINES: readonly number[] = Object.freeze([]);\n \n /**\n  * The projection of a closed set. Construct once, then ask for files, lines\n@@ -118,6 +125,10 @@\n \t#relocs: Map<LineId, RelocationInfo> | undefined;\n \t/** The same, by n, with the new parent's code. */\n \t#relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined;\n+\t/** The moved lines under each node, by its code, made from the relocations on first use. */\n+\t#movedInto: Map<number, number[]> | undefined;\n+\t/** Whether this view's moves are too many to keep, so they're resolved on disk per layout; undefined until asked. */\n+\t#movesOnDisk: boolean | undefined;\n \t/** Bounded memo: ancestry can span the entire history even for one visible line. */\n \t#fileOf = new Map<number, string>();\n \t/** The latest records asked for: one file's lines, one file's sequence, one file's text. */\n@@ -131,7 +142,8 @@\n \t\t\tthis.#stable = set;\n \t\t\tthis.#set = set;\n \t\t\tthis.#in = new Uint8Array(0);\n-\t\t\tfor (const id of set) {\n+\t\t\t// A set kept closed when it was written needs no proof here (abso Capacity).\n+\t\t\tfor (const id of isClosedSet(set) ? [] : set) {\n \t\t\t\tconst atom = graph.atom(id);\n \t\t\t\tfor (const dependency of atom.deps) {\n \t\t\t\t\tgraph.atom(dependency);\n@@ -220,6 +232,7 @@\n \t\tfor (const id of this.#eachMover()) {\n \t\t\tfor (const m of graph.atom(id).moves!) {\n \t\t\t\tif (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);\n+\t\t\t\tif (graph.hasOrderedChildren && relocs.size > MOVED_LINES_KEPT) throw new TooManyMoves();\n \t\t\t}\n \t\t}\n \t\tconst parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;\n@@ -259,6 +272,21 @@\n \t\treturn relocs;\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+\t\tif (this.#movesOnDisk === undefined) {\n+\t\t\ttry {\n+\t\t\t\tthis.#relocations();\n+\t\t\t\tthis.#movesOnDisk = false;\n+\t\t\t} catch (error) {\n+\t\t\t\tif (!(error instanceof TooManyMoves)) throw error;\n+\t\t\t\tthis.#movesOnDisk = true;\n+\t\t\t}\n+\t\t}\n+\t\treturn this.#movesOnDisk;\n+\t}\n+\n \t/** The relocations by n, with each new parent's code. */\n \t#relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> {\n \t\tif (this.#relocsN) return this.#relocsN;\n@@ -272,7 +300,7 @@\n \t/** The node a line hangs off in this set: where it was written, or where a move put it. */\n \tparentOf(line: LineId): NodeId {\n \t\tif (this.graph.lineParent(line) === undefined) throw new EngineError(\"unknown-line\", `no line ${line}`);\n-\t\tif (this.#hasMoves && this.graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn this.graph.withProjectionScratch((scratch) =>\n \t\t\t\tresolveScratchMoves(this.graph, (id) => this.#has(this.graph.atom(id).seq), scratch).parent(line),\n \t\t\t);\n@@ -302,7 +330,7 @@\n \t#fileOfN(n: number): string {\n \t\tconst graph = this.graph;\n \t\tif (!this.#hasMoves) return graph.writtenIn(n);\n-\t\tif (graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) => {\n \t\t\t\tconst moves = resolveScratchMoves(graph, (id) => this.#has(graph.atom(id).seq), scratch);\n \t\t\t\tlet current = graph.nodeOf(n);\n@@ -345,10 +373,18 @@\n \t\t\tout.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });\n \t\t}\n \t\tif (relocs) {\n-\t\t\tfor (const r of graph.eachRelocatedToCode(code)) {\n-\t\t\t\tconst n = graph.lineN(r.line);\n-\t\t\t\tconst selected = relocs.get(n)?.r;\n-\t\t\t\tif (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;\n+\t\t\t// The lines this set's moves put under each node, from the moves it already resolved: no row read per node\n+\t\t\t// (abso Capacity).\n+\t\t\tif (!this.#movedInto) {\n+\t\t\t\tthis.#movedInto = new Map();\n+\t\t\t\tfor (const [n, { parent }] of relocs) {\n+\t\t\t\t\tconst into = this.#movedInto.get(parent);\n+\t\t\t\t\tif (into) into.push(n);\n+\t\t\t\t\telse this.#movedInto.set(parent, [n]);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tfor (const n of this.#movedInto.get(code) ?? NO_LINES) {\n+\t\t\t\tconst r = relocs.get(n)!.r;\n \t\t\t\tconst seq = graph.atom(r.atom).seq;\n \t\t\t\tout.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });\n \t\t\t}\n@@ -415,7 +451,7 @@\n \t/** Lay out one file: its line tree from the set's atoms, linearized. */\n \t#layout(path: string, complete: boolean): FileState {\n \t\tconst graph = this.graph;\n-\t\tif (this.#hasMoves && graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) =>\n \t\t\t\tthis.#layoutResolved(\n \t\t\t\t\tpath,\n@@ -635,7 +671,7 @@\n \t\t\t}\n \t\t\treturn found && previous !== undefined ? [previous] : [];\n \t\t};\n-\t\tif (this.#hasMoves && graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) =>\n \t\t\t\tscan(scratchLayoutNodes(graph, (id) => this.#has(graph.atom(id).seq), scratch, graph.nodeOf(start))),\n \t\t\t);"}],"unmatched":[]}