{"version":134,"agent":"codex-capacity-chair","message":"Stream project conflict candidates and resolver intersections","time":1791843192862,"timeIso":"2026-10-12T22:13:12.862Z","changes":2,"change":1284,"files":["worker/project/collisions.ts","worker/project/test/collision-streaming.test.ts"],"draft":null,"diff":[{"path":"worker/project/collisions.ts","added":57,"removed":8,"diff":"--- a/worker/project/collisions.ts\n+++ b/worker/project/collisions.ts\n@@ -46,25 +46,74 @@\n \tconst out: Pair[] = [];\n \tfor (const yours of draft) {\n \t\tconst atom = g.atom(yours);\n-\t\tconst candidates = new Set<AtomId>();\n+\t\tconst streams: IterableIterator<AtomId>[] = [];\n \t\tfor (const key of atom.writes) {\n-\t\t\tfor (const list of [g.writersOf(key), g.readersOf(key), g.holdersOf(key)]) {\n-\t\t\t\tfor (const id of list) if (against.has(id)) candidates.add(id);\n-\t\t\t}\n+\t\t\tstreams.push(g.eachWriterOf(key), g.eachReaderOf(key), g.eachHolderOf(key));\n \t\t}\n-\t\tfor (const key of claimsOf(atom)) for (const id of g.writersOf(key)) if (against.has(id)) candidates.add(id);\n-\t\tfor (const theirs of [...candidates].sort((p, q) => g.atom(p).seq - g.atom(q).seq)) {\n+\t\tfor (const key of claimsOf(atom)) streams.push(g.eachWriterOf(key));\n+\t\tfor (const theirs of orderedClaimants(g, streams)) {\n+\t\t\tif (!against.has(theirs)) continue;\n \t\t\tconst [lo, hi] = g.atom(yours).seq < g.atom(theirs).seq ? [yours, theirs] : [theirs, yours];\n \t\t\tconst keys = conflictKeys(g.atom(lo), g.atom(hi), g.dependsOn(hi, lo));\n \t\t\tif (keys.length === 0 || sideBySide(g, lo, hi, keys)) continue;\n-\t\t\tconst fromHi = new Set(g.resolversOf(hi));\n-\t\t\tif (g.resolversOf(lo).some((r) => fromHi.has(r) && settled.has(r))) continue;\n+\t\t\tif (hasCommonResolver(g, lo, hi, settled)) continue;\n \t\t\tout.push({ yours, theirs, keys });\n \t\t}\n \t}\n \treturn out;\n }\n \n+/** Claim indexes append in canonical seq order. Merge one cursor per current\n+ * atom key, retaining no candidate set proportional to historical claims. */\n+function* orderedClaimants(g: Graph, streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> {\n+\ttry {\n+\t\tconst heads = streams.map((stream) => stream.next());\n+\t\tlet previous = -1;\n+\t\tfor (;;) {\n+\t\t\tlet best = -1;\n+\t\t\tlet seq = Infinity;\n+\t\t\tfor (let i = 0; i < heads.length; i++) {\n+\t\t\t\tconst head = heads[i]!;\n+\t\t\t\tif (head.done) continue;\n+\t\t\t\tconst current = g.atom(head.value).seq;\n+\t\t\t\tif (current < seq) {\n+\t\t\t\t\tbest = i;\n+\t\t\t\t\tseq = current;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (best < 0) return;\n+\t\t\tconst id = heads[best]!.value!;\n+\t\t\theads[best] = streams[best]!.next();\n+\t\t\tif (seq === previous) continue;\n+\t\t\tprevious = seq;\n+\t\t\tyield id;\n+\t\t}\n+\t} finally {\n+\t\tfor (const stream of streams) stream.return?.();\n+\t}\n+}\n+\n+/** Resolver indexes share the same append order, so intersection needs two cursors. */\n+function hasCommonResolver(g: Graph, lo: AtomId, hi: AtomId, settled: ReadonlySet<AtomId>): boolean {\n+\tconst left = g.eachResolverOf(lo);\n+\tconst right = g.eachResolverOf(hi);\n+\ttry {\n+\t\tlet a = left.next();\n+\t\tlet b = right.next();\n+\t\twhile (!a.done && !b.done) {\n+\t\t\tconst x = g.atom(a.value).seq;\n+\t\t\tconst y = g.atom(b.value).seq;\n+\t\t\tif (x === y && settled.has(a.value)) return true;\n+\t\t\tif (x <= y) a = left.next();\n+\t\t\tif (y <= x) b = right.next();\n+\t\t}\n+\t\treturn false;\n+\t} finally {\n+\t\tleft.return?.();\n+\t\tright.return?.();\n+\t}\n+}\n+\n /**\n  * Two pure insertions at the same point, and nothing else between them\n  * (#37, the game trial): both only write the gap after one line. Neither"},{"path":"worker/project/test/collision-streaming.test.ts","added":73,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/collision-streaming.test.ts\n@@ -0,0 +1,73 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase, Graph } from \"../../../src/engine/index.ts\";\n+import { CachedStateStorage } from \"../../../src/engine/state-source.ts\";\n+import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n+import { pairsIn } from \"../collisions.ts\";\n+\n+test(\"persistent conflict candidates and resolver intersections stream in exact canonical order\", (t) => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage: new CachedStateStorage(source, 8192, 32) });\n+\t\tgraph.add(buildBase({ \"a.txt\": \"one\\n\" }, { author: \"fixture\", message: \"base\", time: 0 }));\n+\t\tconst count = 513;\n+\t\tfor (let seq = 1; seq <= count + 1; seq++)\n+\t\t\tgraph.add({\n+\t\t\t\tid: `a${seq}`,\n+\t\t\t\tseq,\n+\t\t\t\tauthor: \"fixture\",\n+\t\t\t\tmessage: \"claim\",\n+\t\t\t\ttime: seq,\n+\t\t\t\tinserts: [],\n+\t\t\t\tdeletes: [],\n+\t\t\t\twrites: [\"L:a0.0\"],\n+\t\t\t\treads: [\"G:^a.txt\"],\n+\t\t\t\tholds: [],\n+\t\t\t\tneeds: [],\n+\t\t\t\tresolves: [],\n+\t\t\t\treasons: [],\n+\t\t\t\tdeps: [\"a0\"],\n+\t\t\t});\n+\t\tconst draft = `a${count + 1}`;\n+\t\tconst against = new Set(Array.from({ length: count }, (_, n) => `a${n + 1}`));\n+\t\tconst settled = new Set<string>();\n+\t\tfor (let n = 1; n <= count; n += 2) {\n+\t\t\tconst seq = graph.size;\n+\t\t\tconst id = `a${seq}`;\n+\t\t\tgraph.add({\n+\t\t\t\tid,\n+\t\t\t\tseq,\n+\t\t\t\tauthor: \"fixture\",\n+\t\t\t\tmessage: \"resolve\",\n+\t\t\t\ttime: seq,\n+\t\t\t\tinserts: [],\n+\t\t\t\tdeletes: [],\n+\t\t\t\twrites: [],\n+\t\t\t\treads: [],\n+\t\t\t\tholds: [],\n+\t\t\t\tneeds: [],\n+\t\t\t\tresolves: [`a${n}`, draft],\n+\t\t\t\treasons: [],\n+\t\t\t\tdeps: [`a${n}`, draft],\n+\t\t\t});\n+\t\t\tsettled.add(id);\n+\t\t}\n+\t\tconst reopened = new Graph({ storage: new CachedStateStorage(source, 8192, 32) });\n+\t\tfor (const method of [\"writersOf\", \"readersOf\", \"holdersOf\", \"resolversOf\"] as const)\n+\t\t\tt.mock.method(reopened, method, () => {\n+\t\t\t\tthrow new Error(`materialized ${method} forbidden`);\n+\t\t\t});\n+\t\tassert.deepEqual(\n+\t\t\tpairsIn(reopened, [draft], against, new Set()).map((pair) => pair.theirs),\n+\t\t\t[...against],\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\tpairsIn(reopened, [draft], against, settled).map((pair) => pair.theirs),\n+\t\t\t[...against].filter((id) => Number(id.slice(1)) % 2 === 0),\n+\t\t);\n+\t\tconst selected = new Set([\"a1\", \"a257\", \"a513\"]);\n+\t\tassert.deepEqual(pairsIn(reopened, [draft], selected, settled), []);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"}],"ci":null}