Find reason-warning neighbors without materializing file history
of shared·codex-capacity-chair··Not checked
Version 137 · Files at version 138
3 files, 89 lines added, 17 removed.
--- a/src/engine/conflicts.ts
+++ b/src/engine/conflicts.ts
@@ -207,23 +207,7 @@
207207function visibleNeighbours(view: View, graph: Graph, line: LineId): LineId[] { 208208 const info = graph.line(line); 209209 if (!info) return []; 210− const sequence = view.sequence(info.file);
211− const at = view.sequenceIndex(info.file, line);
212− if (at < 0) return [];
213− const out: LineId[] = [];
214− for (let i = at - 1; i >= 0; i--) {
215− if (sequence[i]!.visible) {
216− out.push(sequence[i]!.id);
217− break;
218− }
219− }
220− for (let i = at + 1; i < sequence.length; i++) {
221− if (sequence[i]!.visible) {
222− out.push(sequence[i]!.id);
223− break;
224− }
225− }
226− return out;
210+ return view.visibleNeighbours(info.file, line); --- /dev/null
+++ b/src/engine/test/visible-neighbours.test.ts
@@ -0,0 +1,54 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { Graph } from "../graph.ts"; 4+import { CachedStateStorage, StableOverlaySet } from "../state-source.ts"; 5+import { View } from "../view.ts"; 6+import { Repo } from "./helpers.ts"; 7+import { SqlState } from "./storage-helper.ts"; 9+test("visible neighbours match historical sequence without retaining it, including moves and tombstones", (t) => { 10+ const source = new SqlState(); 12+ const original = Array.from({ length: 256 }, (_, n) => `line ${n}\n`).join(""); 13+ const repo = Repo.init({ "a.txt": original, "b.txt": "other\n" }); 14+ const sets = [new Set(repo.shared)]; 15+ const deletion = repo.draft({ edits: [{ file: "a.txt", old: original, new: "line 0\nline 255\n" }] }); 16+ repo.graph.add(deletion); 17+ repo.shared.add(deletion.id); 18+ sets.push(new Set(repo.shared)); 19+ const move = repo.draft({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 0 } }); 20+ repo.graph.add(move); 21+ repo.shared.add(move.id); 22+ sets.push(new Set(repo.shared)); 23+ const graph = new Graph({ storage: new CachedStateStorage(source) }); 24+ for (const id of repo.shared) graph.add(repo.graph.atom(id)); 25+ const rows = source.db.prepare("SELECT count(*) AS n FROM state").get()!.n; 26+ for (const set of sets) { 27+ const actual = new View(graph, new StableOverlaySet(set)); 28+ const expected = new View(repo.graph, set); 29+ const weight = actual.weight(); 30+ t.mock.method(actual, "sequence", () => { 31+ throw new Error("historical sequence forbidden"); 33+ t.mock.method(actual, "sequenceIndex", () => { 34+ throw new Error("historical position index forbidden"); 36+ for (const path of ["a.txt", "b.txt", "missing.txt"]) { 37+ const sequence = expected.sequence(path); 38+ for (const line of ["a0.0", "a0.127", "a0.255", "a0.256", "a999.0"]) { 39+ const at = sequence.findIndex((item) => item.id === line); 40+ const before = at < 0 ? undefined : sequence.slice(0, at).findLast((item) => item.visible)?.id; 41+ const after = at < 0 ? undefined : sequence.slice(at + 1).find((item) => item.visible)?.id; 43+ actual.visibleNeighbours(path, line), 44+ [before, after].filter((id) => id !== undefined), 48+ assert.equal(actual.weight(), weight, "neighbour lookup retains no file/history projection"); 49+ assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, rows); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -561,6 +561,40 @@
564+ /** Two visible neighbours without retaining the file's historical sequence. */ 565+ visibleNeighbours(path: string, line: LineId): LineId[] { 566+ const graph = this.graph; 567+ const target = graph.lineN(line); 568+ const start = graph.startCodeOf(path); 569+ if (target < 0 || start === undefined) return []; 570+ const scan = (nodes: Iterable<number>): LineId[] => { 571+ let previous: LineId | undefined; 573+ for (const n of nodes) { 578+ let deleted = false; 579+ for (const seq of graph.eachDeleterSeqOf(n)) 580+ if (this.#has(seq)) { 584+ if (deleted) continue; 585+ const id = graph.lineIdOf(n); 586+ if (found) return previous === undefined ? [id] : [previous, id]; 589+ return found && previous !== undefined ? [previous] : []; 591+ if (this.#hasMoves && graph.hasOrderedChildren) 592+ return graph.withProjectionScratch((scratch) => 593+ scan(scratchLayoutNodes(graph, (id) => this.#has(graph.atom(id).seq), scratch, graph.nodeOf(start))), 595+ return scan(this.#layoutNodes(start)); 564598 /** Index of a line in `sequence(path)`, or -1. */ 565599 sequenceIndex(path: string, line: LineId): number { 566600 const f = this.#file(path, true);