Stream project conflict candidates and resolver intersections
of shared·codex-capacity-chair··Not checked
Version 133 · Files at version 134
2 files, 130 lines added, 8 removed.
--- a/worker/project/collisions.ts
+++ b/worker/project/collisions.ts
@@ -46,25 +46,74 @@
4646 const out: Pair[] = []; 4747 for (const yours of draft) { 4848 const atom = g.atom(yours); 49− const candidates = new Set<AtomId>();
49+ const streams: IterableIterator<AtomId>[] = []; 5050 for (const key of atom.writes) { 51− for (const list of [g.writersOf(key), g.readersOf(key), g.holdersOf(key)]) {
52− for (const id of list) if (against.has(id)) candidates.add(id);
53− }
51+ streams.push(g.eachWriterOf(key), g.eachReaderOf(key), g.eachHolderOf(key)); 55− for (const key of claimsOf(atom)) for (const id of g.writersOf(key)) if (against.has(id)) candidates.add(id);
56− for (const theirs of [...candidates].sort((p, q) => g.atom(p).seq - g.atom(q).seq)) {
53+ for (const key of claimsOf(atom)) streams.push(g.eachWriterOf(key)); 54+ for (const theirs of orderedClaimants(g, streams)) { 55+ if (!against.has(theirs)) continue; 5756 const [lo, hi] = g.atom(yours).seq < g.atom(theirs).seq ? [yours, theirs] : [theirs, yours]; 5857 const keys = conflictKeys(g.atom(lo), g.atom(hi), g.dependsOn(hi, lo)); 5958 if (keys.length === 0 || sideBySide(g, lo, hi, keys)) continue; 60− const fromHi = new Set(g.resolversOf(hi));
61− if (g.resolversOf(lo).some((r) => fromHi.has(r) && settled.has(r))) continue;
59+ if (hasCommonResolver(g, lo, hi, settled)) continue; 6260 out.push({ yours, theirs, keys }); 66+/** Claim indexes append in canonical seq order. Merge one cursor per current 67+ * atom key, retaining no candidate set proportional to historical claims. */ 68+function* orderedClaimants(g: Graph, streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> { 70+ const heads = streams.map((stream) => stream.next()); 75+ for (let i = 0; i < heads.length; i++) { 76+ const head = heads[i]!; 77+ if (head.done) continue; 78+ const current = g.atom(head.value).seq; 84+ if (best < 0) return; 85+ const id = heads[best]!.value!; 86+ heads[best] = streams[best]!.next(); 87+ if (seq === previous) continue; 92+ for (const stream of streams) stream.return?.(); 96+/** Resolver indexes share the same append order, so intersection needs two cursors. */ 97+function hasCommonResolver(g: Graph, lo: AtomId, hi: AtomId, settled: ReadonlySet<AtomId>): boolean { 98+ const left = g.eachResolverOf(lo); 99+ const right = g.eachResolverOf(hi); 101+ let a = left.next(); 102+ let b = right.next(); 103+ while (!a.done && !b.done) { 104+ const x = g.atom(a.value).seq; 105+ const y = g.atom(b.value).seq; 106+ if (x === y && settled.has(a.value)) return true; 107+ if (x <= y) a = left.next(); 108+ if (y <= x) b = right.next(); 69118 * Two pure insertions at the same point, and nothing else between them 70119 * (#37, the game trial): both only write the gap after one line. Neither --- /dev/null
+++ b/worker/project/test/collision-streaming.test.ts
@@ -0,0 +1,73 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { buildBase, Graph } from "../../../src/engine/index.ts"; 4+import { CachedStateStorage } from "../../../src/engine/state-source.ts"; 5+import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 6+import { pairsIn } from "../collisions.ts"; 8+test("persistent conflict candidates and resolver intersections stream in exact canonical order", (t) => { 9+ const source = new SqlState(); 11+ const graph = new Graph({ storage: new CachedStateStorage(source, 8192, 32) }); 12+ graph.add(buildBase({ "a.txt": "one\n" }, { author: "fixture", message: "base", time: 0 })); 14+ for (let seq = 1; seq <= count + 1; seq++) 31+ const draft = `a${count + 1}`; 32+ const against = new Set(Array.from({ length: count }, (_, n) => `a${n + 1}`)); 33+ const settled = new Set<string>(); 34+ for (let n = 1; n <= count; n += 2) { 35+ const seq = graph.size; 36+ const id = `a${seq}`; 49+ resolves: [`a${n}`, draft], 51+ deps: [`a${n}`, draft], 55+ const reopened = new Graph({ storage: new CachedStateStorage(source, 8192, 32) }); 56+ for (const method of ["writersOf", "readersOf", "holdersOf", "resolversOf"] as const) 57+ t.mock.method(reopened, method, () => { 58+ throw new Error(`materialized ${method} forbidden`); 61+ pairsIn(reopened, [draft], against, new Set()).map((pair) => pair.theirs), 65+ pairsIn(reopened, [draft], against, settled).map((pair) => pair.theirs), 66+ [...against].filter((id) => Number(id.slice(1)) % 2 === 0), 68+ const selected = new Set(["a1", "a257", "a513"]); 69+ assert.deepEqual(pairsIn(reopened, [draft], selected, settled), []);