{"draft":null,"from":73,"to":318,"files":[{"path":"src/engine/build.ts","added":84,"removed":28,"diff":"--- a/src/engine/build.ts\n+++ b/src/engine/build.ts\n@@ -4,7 +4,7 @@\n \n import { Graph } from \"./graph.ts\";\n import { attrKey, gapKey, isStartNode, lineKey, parseKey, posKey, startNode, writerOf } from \"./keys.ts\";\n-import { diffLines, joinLines, normalizeText, splitLines } from \"./text.ts\";\n+import { diffChunkedLines, joinLines, normalizeText, splitLines } from \"./text.ts\";\n import {\n \tATTR_NAMES,\n \ttype Atom,\n@@ -23,6 +23,36 @@\n } from \"./types.ts\";\n import { View } from \"./view.ts\";\n \n+/** Exact read surface used to build atoms; membership requires point lookup only. */\n+export type AtomBuildView = Pick<View, \"graph\" | \"lines\" | \"sequence\" | \"sequenceIndex\" | \"parentOf\"> & {\n+\treadonly set: Pick<ReadonlySet<AtomId>, \"has\">;\n+\tbuildLines?: View[\"buildLines\"];\n+};\n+\n+function buildingLines(view: AtomBuildView, path: string) {\n+\treturn view.buildLines ? view.buildLines(path) : view.lines(path);\n+}\n+\n+/** Cache only within one immutable build; never retain handles across writes. */\n+function cachedBuildView(view: AtomBuildView): AtomBuildView {\n+\tconst files = new Map<string, ReturnType<View[\"lines\"]>>();\n+\treturn {\n+\t\tgraph: view.graph,\n+\t\tset: view.set,\n+\t\tlines(path) {\n+\t\t\tlet lines = files.get(path);\n+\t\t\tif (!lines) {\n+\t\t\t\tlines = buildingLines(view, path);\n+\t\t\t\tfiles.set(path, lines);\n+\t\t\t}\n+\t\t\treturn lines;\n+\t\t},\n+\t\tsequence: (path) => view.sequence(path),\n+\t\tsequenceIndex: (path, line) => view.sequenceIndex(path, line),\n+\t\tparentOf: (line) => view.parentOf(line),\n+\t};\n+}\n+\n /** Replace the one exact occurrence of `old` in `file` with `new`. */\n export interface TextEdit {\n \treadonly file: string;\n@@ -108,7 +138,7 @@\n  * the graph's next id; accept it with `graph.add(atom)` and put it in a set.\n  * Its dependencies are derived from what it touched in the view (see deriveDeps).\n  */\n-export function buildAtom(view: View, input: AtomInput): Atom {\n+export function buildAtom(view: AtomBuildView, input: AtomInput): Atom {\n \treturn build(view, input, false);\n }\n \n@@ -126,7 +156,8 @@\n \treturn build(view, { ...meta, edits, attrs }, true);\n }\n \n-function build(view: View, input: AtomInput, allowEmpty: boolean): Atom {\n+function build(view: AtomBuildView, input: AtomInput, allowEmpty: boolean): Atom {\n+\tview = cachedBuildView(view);\n \tconst graph = view.graph;\n \tconst id = graph.nextId();\n \tif (typeof input.author !== \"string\" || typeof input.message !== \"string\") {\n@@ -147,9 +178,14 @@\n \tconst emptiedBy = new Set<AtomId>();\n \n \tfor (const region of regions) {\n-\t\tconst visible = view.lines(region.file);\n-\t\tconst oldLines = visible.slice(region.start, region.end).map((l) => l.text);\n-\t\tfor (const hunk of diffLines(oldLines, region.lines)) {\n+\t\tconst visible = buildingLines(view, region.file);\n+\t\tfunction* oldLines() {\n+\t\t\tfor (let i = region.start; i < region.end; i++) {\n+\t\t\t\tconst line = visible[i]!;\n+\t\t\t\tyield () => (line.textChunks ? line.textChunks() : [line.text]);\n+\t\t\t}\n+\t\t}\n+\t\tfor (const hunk of diffChunkedLines(oldLines(), region.lines)) {\n \t\t\tconst start = region.start + hunk.start;\n \t\t\tconst end = region.start + hunk.end;\n \t\t\tlet parent: NodeId;\n@@ -275,12 +311,12 @@\n  * lines into each other collide in all but the rarest cases (the view breaks\n  * any loop that is left).\n  */\n-function planMove(view: View, move: MoveInput, writes: Set<Key>, reads: Set<Key>): Relocation[] {\n+function planMove(view: AtomBuildView, move: MoveInput, writes: Set<Key>, reads: Set<Key>): Relocation[] {\n \tfor (const path of [move.file, move.to]) {\n \t\tif (typeof path !== \"string\" || path === \"\" || /[\\n\\r]/.test(path))\n \t\t\tthrow new EngineError(\"bad-edit\", `bad file path ${JSON.stringify(path)}`);\n \t}\n-\tconst visible = view.lines(move.file);\n+\tconst visible = buildingLines(view, move.file);\n \tconst { start, end, at } = move;\n \tif (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > visible.length || start >= end) {\n \t\tthrow new EngineError(\n@@ -288,7 +324,7 @@\n \t\t\t`${move.file} lines [${start}, ${end}) are not a non-empty range of its ${visible.length} visible lines`,\n \t\t);\n \t}\n-\tconst dest = view.lines(move.to);\n+\tconst dest = buildingLines(view, move.to);\n \tif (!Number.isInteger(at) || at < 0 || at > dest.length) {\n \t\tthrow new EngineError(\"bad-range\", `${move.to} has no place ${at} (it has ${dest.length} visible lines)`);\n \t}\n@@ -333,10 +369,15 @@\n  * - creating a file over a deleted one: the atoms that deleted its lines;\n  * - needs and resolves.\n  * Nothing else from the view: not the line below an insertion, not the\n- * tombstones around it.\n+ * tombstones around it. New atoms encode implicit dependencies as maximal\n+ * tips: ancestors reachable through another tip are omitted from direct deps.\n+ * Explicit needs/resolves always remain direct. Existing atoms are not rewritten.\n+ * Direct-edge diagnostics (including why/Deps trailers and missingDeps) therefore\n+ * show fewer redundant edges; transitive closure and dependency ordering remain\n+ * the semantic contract. A missing tip still implies its required ancestors.\n  */\n function deriveDeps(\n-\tview: View,\n+\tview: AtomBuildView,\n \tparts: {\n \t\twrites: readonly Key[];\n \t\treads: readonly Key[];\n@@ -347,16 +388,30 @@\n \t},\n ): AtomId[] {\n \tconst graph = view.graph;\n-\tconst deps = new Set<AtomId>(parts.emptiedBy);\n+\t// New atoms retain maximal implicit dependencies. Their transitive closure is\n+\t// unchanged; explicit needs/resolves remain direct edges below.\n+\tconst deps = new Set<AtomId>();\n+\tconst addImplicit = (candidate: AtomId): void => {\n+\t\tif (deps.has(candidate)) return;\n+\t\tconst candidateClock = graph.lamport(candidate);\n+\t\tfor (const previous of deps) {\n+\t\t\tconst previousClock = graph.lamport(previous);\n+\t\t\t// Equal Lamport clocks cannot reach each other; check only the possible direction.\n+\t\t\tif (previousClock > candidateClock && graph.dependsOn(previous, candidate)) return;\n+\t\t\tif (candidateClock > previousClock && graph.dependsOn(candidate, previous)) deps.delete(previous);\n+\t\t}\n+\t\tdeps.add(candidate);\n+\t};\n+\tfor (const id of parts.emptiedBy) addImplicit(id);\n \tfor (const key of [...parts.writes, ...parts.reads, ...parts.holds]) {\n \t\tconst { kind, node } = parseKey(key);\n \t\t// An attribute builds on whoever set it last in the view, the way an insertion builds on a gap's writers;\n \t\t// a move builds on the earlier moves of its lines.\n-\t\tif (kind !== \"attr\" && !isStartNode(node)) deps.add(writerOf(node));\n-\t\tif (kind !== \"line\") for (const writer of graph.writersOf(key)) if (view.set.has(writer)) deps.add(writer);\n+\t\tif (kind !== \"attr\" && !isStartNode(node)) addImplicit(writerOf(node));\n+\t\tif (kind !== \"line\") for (const writer of graph.eachWriterOf(key)) if (view.set.has(writer)) addImplicit(writer);\n \t}\n \tfor (const key of parts.writes) {\n-\t\tfor (const reader of graph.readersOf(key)) if (view.set.has(reader)) deps.add(reader);\n+\t\tfor (const reader of graph.eachReaderOf(key)) if (view.set.has(reader)) addImplicit(reader);\n \t}\n \tfor (const id of parts.needs) deps.add(id);\n \tfor (const id of parts.resolves) deps.add(id);\n@@ -371,8 +426,8 @@\n \t});\n }\n \n-function rangeLines(view: View, range: LineRange): LineId[] {\n-\tconst visible = view.lines(range.file);\n+function rangeLines(view: AtomBuildView, range: LineRange): LineId[] {\n+\tconst visible = buildingLines(view, range.file);\n \tif (\n \t\t!Number.isInteger(range.start) ||\n \t\t!Number.isInteger(range.end) ||\n@@ -388,7 +443,7 @@\n \treturn visible.slice(range.start, range.end).map((l) => l.id);\n }\n \n-function uniqueAtoms(view: View, ids: readonly AtomId[], what: string): AtomId[] {\n+function uniqueAtoms(view: AtomBuildView, ids: readonly AtomId[], what: string): AtomId[] {\n \tconst out: AtomId[] = [];\n \tfor (const id of ids) {\n \t\tif (!view.graph.has(id)) throw new EngineError(\"unknown-atom\", `${what} names unknown atom ${id}`);\n@@ -400,7 +455,7 @@\n \n // Matching edits ---------------------------------------------------------------\n \n-function resolveRegions(view: View, edits: readonly EditInput[]): Region[] {\n+function resolveRegions(view: AtomBuildView, edits: readonly EditInput[]): Region[] {\n \tconst regions = edits.map((edit, order) => {\n \t\tconst mode = edit.mode ?? \"both\";\n \t\tif (!MODES.includes(mode)) throw new EngineError(\"bad-edit\", `mode must be one of ${MODES.join(\", \")}`);\n@@ -436,12 +491,12 @@\n  * whole lines. If the replacement would leave a line without its newline, the\n  * next line joins the region (that is what the plain-text edit does).\n  */\n-function matchText(view: View, edit: TextEdit, mode: Mode, order: number): Region {\n+function matchText(view: AtomBuildView, edit: TextEdit, mode: Mode, order: number): Region {\n \tif (typeof edit.old !== \"string\" || typeof edit.new !== \"string\") {\n \t\tthrow new EngineError(\"bad-edit\", \"a text edit needs old and new strings\");\n \t}\n \tconst file = edit.file;\n-\tconst visible = view.lines(file);\n+\tconst visible = buildingLines(view, file);\n \tconst oldText = normalizeText(edit.old);\n \tconst newText = normalizeText(edit.new);\n \tif (oldText === \"\") {\n@@ -453,7 +508,8 @@\n \t\t}\n \t\treturn { file, start: 0, end: 0, lines: splitLines(newText), mode, order };\n \t}\n-\tconst text = joinLines(visible.map((l) => l.text));\n+\tconst texts = visible.map((line) => line.text);\n+\tconst text = joinLines(texts);\n \tconst at = text.indexOf(oldText);\n \tif (at < 0) throw new EngineError(\"no-match\", `old text not found in ${file}`);\n \tif (text.indexOf(oldText, at + 1) >= 0) {\n@@ -464,9 +520,9 @@\n \t}\n \tconst starts: number[] = [];\n \tlet pos = 0;\n-\tfor (const line of visible) {\n+\tfor (const text of texts) {\n \t\tstarts.push(pos);\n-\t\tpos += line.text.length + 1;\n+\t\tpos += text.length + 1;\n \t}\n \tconst lineAt = (offset: number): number => {\n \t\tlet lo = 0;\n@@ -482,12 +538,12 @@\n \tconst startLine = lineAt(at);\n \tlet endLine = lineAt(end - 1);\n \tconst lineStart = starts[startLine]!;\n-\tconst lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;\n+\tconst lineEnd = starts[endLine]! + texts[endLine]!.length + 1;\n \tlet replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd);\n \tif (replacement !== \"\" && !replacement.endsWith(\"\\n\")) {\n \t\tif (endLine + 1 < visible.length) {\n \t\t\tendLine++;\n-\t\t\treplacement += visible[endLine]!.text + \"\\n\";\n+\t\t\treplacement += `${texts[endLine]}\\n`;\n \t\t} else {\n \t\t\treplacement += \"\\n\";\n \t\t}\n@@ -495,8 +551,8 @@\n \treturn { file, start: startLine, end: endLine + 1, lines: splitLines(replacement), mode, order };\n }\n \n-function matchSplice(view: View, edit: SpliceEdit, mode: Mode, order: number): Region {\n-\tconst count = view.lines(edit.file).length;\n+function matchSplice(view: AtomBuildView, edit: SpliceEdit, mode: Mode, order: number): Region {\n+\tconst count = buildingLines(view, edit.file).length;\n \tif (\n \t\t!Number.isInteger(edit.start) ||\n \t\t!Number.isInteger(edit.end) ||"}],"unmatched":[]}