{"draft":null,"from":175,"to":318,"files":[{"path":"worker/project/collisions.ts","added":119,"removed":8,"diff":"--- a/worker/project/collisions.ts\n+++ b/worker/project/collisions.ts\n@@ -9,8 +9,10 @@\n \tconflictKeys,\n \ttype Graph,\n \tjoinLines,\n+\ttype Key,\n \tparseKey,\n \ttype ReadView,\n+\tseqOf,\n } from \"../../src/engine/index.ts\";\n import { differenceSet, StableOverlaySet } from \"../../src/engine/state-source.ts\";\n import { definedTwice } from \"./definitions.ts\";\n@@ -44,7 +46,20 @@\n \tagainst: ReadonlySet<AtomId>,\n \tsettled: ReadonlySet<AtomId>,\n ): Pair[] {\n-\tconst out: Pair[] = [];\n+\treturn pairsFrom(g, claimantsIn(g, draft, against), against, settled);\n+}\n+\n+/**\n+ * Each draft change's claimants in `against`, in seq order: the atoms whose claims meet its keys, before the\n+ * engine's rule is applied. A stored atom's keys are read from disk, so a caller checking one draft against many\n+ * (draft-collisions.ts) gathers once against all of them and filters with pairsFrom.\n+ */\n+export function claimantsIn(\n+\tg: Graph,\n+\tdraft: readonly AtomId[],\n+\tagainst: ReadonlySet<AtomId>,\n+): ReadonlyMap<AtomId, readonly AtomId[]> {\n+\tconst out = new Map<AtomId, AtomId[]>();\n \tfor (const yours of draft) {\n \t\tconst atom = g.atom(yours);\n \t\tconst streams: IterableIterator<AtomId>[] = [];\n@@ -52,7 +67,62 @@\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)) streams.push(g.eachWriterOf(key));\n-\t\tfor (const theirs of orderedClaimants(g, streams)) {\n+\t\tconst theirs: AtomId[] = [];\n+\t\tfor (const id of orderedClaimants(streams)) if (against.has(id)) theirs.push(id);\n+\t\tout.set(yours, theirs);\n+\t}\n+\treturn out;\n+}\n+\n+/**\n+ * claimantsIn for a few atoms against each other: each atom's claimants among `atoms`, in seq order, from their own\n+ * keys held in memory. Walking each key's history of claimants read four lists from disk per key, and a draft deleting\n+ * a big file has thousands of keys (abso Capacity).\n+ */\n+export function claimantsAmong(g: Graph, atoms: readonly AtomId[]): ReadonlyMap<AtomId, readonly AtomId[]> {\n+\tconst claimers = new Map<Key, AtomId[]>();\n+\tconst writers = new Map<Key, AtomId[]>();\n+\tconst keys = new Map<AtomId, { writes: readonly Key[]; claims: readonly Key[] }>();\n+\tconst add = (index: Map<Key, AtomId[]>, key: Key, id: AtomId) => {\n+\t\tconst list = index.get(key);\n+\t\tif (list) list.push(id);\n+\t\telse index.set(key, [id]);\n+\t};\n+\tfor (const id of atoms) {\n+\t\tconst atom = g.atom(id);\n+\t\tconst writes = [...atom.writes];\n+\t\t// claimsOf, with the writes already read.\n+\t\tconst claims = [...new Set([...writes, ...atom.reads, ...atom.holds])].sort();\n+\t\tkeys.set(id, { writes, claims });\n+\t\tfor (const key of writes) add(writers, key, id);\n+\t\tfor (const key of claims) add(claimers, key, id);\n+\t}\n+\tconst out = new Map<AtomId, AtomId[]>();\n+\tfor (const id of atoms) {\n+\t\tconst own = keys.get(id)!;\n+\t\tconst found = new Set<AtomId>();\n+\t\t// As claimantsIn's streams: what claims a key it writes, and what writes a key it claims.\n+\t\tfor (const key of own.writes) for (const other of claimers.get(key) ?? []) found.add(other);\n+\t\tfor (const key of own.claims) for (const other of writers.get(key) ?? []) found.add(other);\n+\t\tfound.delete(id);\n+\t\tout.set(\n+\t\t\tid,\n+\t\t\t[...found].sort((a, b) => seqOf(a) - seqOf(b)),\n+\t\t);\n+\t}\n+\treturn out;\n+}\n+\n+/** The claimants in `against` that collide by the engine's rule and that no atom of `settled` resolves. */\n+export function pairsFrom(\n+\tg: Graph,\n+\tclaimants: ReadonlyMap<AtomId, readonly AtomId[]>,\n+\tagainst: ReadonlySet<AtomId>,\n+\tsettled: ReadonlySet<AtomId>,\n+): Pair[] {\n+\tconst out: Pair[] = [];\n+\tfor (const [yours, theirsList] of claimants) {\n+\t\tfor (const theirs of theirsList) {\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@@ -66,7 +136,7 @@\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+function* orderedClaimants(streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> {\n \ttry {\n \t\ttype Head = { id: AtomId; seq: number; stream: number };\n \t\tconst heap: Head[] = [];\n@@ -82,9 +152,11 @@\n \t\t\t}\n \t\t\theap[at] = head;\n \t\t};\n+\t\t// Each claimant's seq from its id, as the graph reads it (abso 738): loading the stored atom for it read a row a\n+\t\t// claimant, most of a warm draft-state's pairing on abso.\n \t\tconst advance = (stream: number) => {\n \t\t\tconst next = streams[stream]!.next();\n-\t\t\tif (!next.done) push({ id: next.value, seq: g.atom(next.value).seq, stream });\n+\t\t\tif (!next.done) push({ id: next.value, seq: seqOf(next.value), stream });\n \t\t};\n \t\tfor (let stream = 0; stream < streams.length; stream++) advance(stream);\n \t\tlet previous = -1;\n@@ -120,8 +192,8 @@\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\tconst x = seqOf(a.value);\n+\t\t\tconst y = seqOf(b.value);\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@@ -212,17 +284,56 @@\n \t\tfor (const file of g.filesOf(id)) out.add(file);\n \t\tif (moved.length === 0) continue;\n \t\tconst atom = g.atom(id);\n-\t\tconst lines: string[] = [...g.deletedLines(id)];\n+\t\tconst lines: string[] = [];\n \t\tfor (const run of atom.inserts) if (!run.parent.startsWith(\"^\")) lines.push(run.parent);\n \t\tfor (const m of atom.moves ?? []) {\n \t\t\tlines.push(m.line);\n \t\t\tif (!m.parent.startsWith(\"^\")) lines.push(m.parent);\n \t\t}\n-\t\tfor (const v of moved) for (const line of lines) out.add(v.fileOf(line));\n+\t\tfor (const v of moved) {\n+\t\t\tfor (const line of lines) out.add(v.fileOf(line));\n+\t\t\t// Deleted lines by number where the view can (abso Capacity): a draft deleting a big file has thousands, and\n+\t\t\t// making each one's id only to look its number up again was most of collisions on website.\n+\t\t\tconst byNumber = (v as { fileOfNumber?: (n: number) => string }).fileOfNumber;\n+\t\t\tif (byNumber) for (const n of g.deletedNumbers(id)) out.add(byNumber.call(v, n));\n+\t\t\telse for (const line of g.deletedLines(id)) out.add(v.fileOf(line));\n+\t\t}\n \t}\n \treturn out;\n }\n \n+/**\n+ * The files a view over `below` plus `mine` can lay out differently from `below` (abso Capacity, View.shareFiles), or\n+ * undefined when any of it moves lines and `placedDifferently` can't say where those land differently. A view's set is closed under dependencies, so it holds `mine` and whatever\n+ * `mine` depends on that `below` lacks; each of those can change a file, so all of them count. Counting `mine` alone\n+ * missed the files of work the draft depends on (Testing's typescript-rs diff for cloud-ts-01, core 235).\n+ */\n+export function touchedBeyond(\n+\tg: Graph,\n+\tbelow: ReadonlySet<AtomId>,\n+\tmine: Iterable<AtomId>,\n+\tviews: readonly ReadView[],\n+\tplacedDifferently?: () => ReadonlySet<string> | undefined,\n+): Set<string> | undefined {\n+\tconst beyond = new Set<AtomId>();\n+\tconst stack = [...mine];\n+\twhile (stack.length) {\n+\t\tconst id = stack.pop()!;\n+\t\tif (beyond.has(id) || below.has(id)) continue;\n+\t\tbeyond.add(id);\n+\t\tfor (const dep of g.atom(id).deps) if (!beyond.has(dep) && !below.has(dep)) stack.push(dep);\n+\t}\n+\tconst files = touchedFiles(g, beyond, views);\n+\t// Moves change more than the files their atoms name only where a moved line lands differently (View.placedDifferently,\n+\t// abso Capacity); without that comparison, a draft that moves lines shares nothing.\n+\tif ([...beyond].some((id) => (g.atom(id).moves?.length ?? 0) > 0)) {\n+\t\tconst placed = placedDifferently?.();\n+\t\tif (!placed) return undefined;\n+\t\tfor (const file of placed) files.add(file);\n+\t}\n+\treturn files;\n+}\n+\n /** The atom's visible lines in a file of a view, or where it acted when none are left. */\n export function describeSide(view: ReadView, atom: AtomId, file: string, keys: readonly string[]): CollisionSide {\n \tconst visible = view.lines(file);"}],"unmatched":[]}