Lines 9–20 at 318 (were 9–17 at 2)
13− type View,
1416} from "../../src/engine/index.ts"; 17+import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts"; 1518import { definedTwice } from "./definitions.ts"; 1619import type { AgentState, Collision, CollisionSide } from "./project-types.ts"; 1720import { SHARED, type Space } from "./spaces.ts"; Lines 46–210 at 318 (were 43–70 at 2)
4346 against: ReadonlySet<AtomId>, 4447 settled: ReadonlySet<AtomId>, 46− const out: Pair[] = [];
49+ return pairsFrom(g, claimantsIn(g, draft, against), against, settled); 53+ * Each draft change's claimants in `against`, in seq order: the atoms whose claims meet its keys, before the 54+ * engine's rule is applied. A stored atom's keys are read from disk, so a caller checking one draft against many 55+ * (draft-collisions.ts) gathers once against all of them and filters with pairsFrom. 57+export function claimantsIn( 59+ draft: readonly AtomId[], 60+ against: ReadonlySet<AtomId>, 61+): ReadonlyMap<AtomId, readonly AtomId[]> { 62+ const out = new Map<AtomId, AtomId[]>(); 4763 for (const yours of draft) { 4864 const atom = g.atom(yours); 49− const candidates = new Set<AtomId>();
65+ const streams: IterableIterator<AtomId>[] = []; 5066 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− }
67+ 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)) {
69+ for (const key of claimsOf(atom)) streams.push(g.eachWriterOf(key)); 70+ const theirs: AtomId[] = []; 71+ for (const id of orderedClaimants(streams)) if (against.has(id)) theirs.push(id); 72+ out.set(yours, theirs); 78+ * claimantsIn for a few atoms against each other: each atom's claimants among `atoms`, in seq order, from their own 79+ * keys held in memory. Walking each key's history of claimants read four lists from disk per key, and a draft deleting 80+ * a big file has thousands of keys (abso Capacity). 82+export function claimantsAmong(g: Graph, atoms: readonly AtomId[]): ReadonlyMap<AtomId, readonly AtomId[]> { 83+ const claimers = new Map<Key, AtomId[]>(); 84+ const writers = new Map<Key, AtomId[]>(); 85+ const keys = new Map<AtomId, { writes: readonly Key[]; claims: readonly Key[] }>(); 86+ const add = (index: Map<Key, AtomId[]>, key: Key, id: AtomId) => { 87+ const list = index.get(key); 88+ if (list) list.push(id); 89+ else index.set(key, [id]); 91+ for (const id of atoms) { 92+ const atom = g.atom(id); 93+ const writes = [...atom.writes]; 94+ // claimsOf, with the writes already read. 95+ const claims = [...new Set([...writes, ...atom.reads, ...atom.holds])].sort(); 96+ keys.set(id, { writes, claims }); 97+ for (const key of writes) add(writers, key, id); 98+ for (const key of claims) add(claimers, key, id); 100+ const out = new Map<AtomId, AtomId[]>(); 101+ for (const id of atoms) { 102+ const own = keys.get(id)!; 103+ const found = new Set<AtomId>(); 104+ // As claimantsIn's streams: what claims a key it writes, and what writes a key it claims. 105+ for (const key of own.writes) for (const other of claimers.get(key) ?? []) found.add(other); 106+ for (const key of own.claims) for (const other of writers.get(key) ?? []) found.add(other); 110+ [...found].sort((a, b) => seqOf(a) - seqOf(b)), 116+/** The claimants in `against` that collide by the engine's rule and that no atom of `settled` resolves. */ 117+export function pairsFrom( 119+ claimants: ReadonlyMap<AtomId, readonly AtomId[]>, 120+ against: ReadonlySet<AtomId>, 121+ settled: ReadonlySet<AtomId>, 123+ const out: Pair[] = []; 124+ for (const [yours, theirsList] of claimants) { 125+ for (const theirs of theirsList) { 126+ if (!against.has(theirs)) continue; 57127 const [lo, hi] = g.atom(yours).seq < g.atom(theirs).seq ? [yours, theirs] : [theirs, yours]; 58128 const keys = conflictKeys(g.atom(lo), g.atom(hi), g.dependsOn(hi, lo)); 59129 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;
130+ if (hasCommonResolver(g, lo, hi, settled)) continue; 62131 out.push({ yours, theirs, keys }); 137+/** Claim indexes append in canonical seq order. Merge one cursor per current 138+ * atom key, retaining no candidate set proportional to historical claims. */ 139+function* orderedClaimants(streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> { 141+ type Head = { id: AtomId; seq: number; stream: number }; 142+ const heap: Head[] = []; 143+ const less = (a: Head, b: Head) => a.seq < b.seq || (a.seq === b.seq && a.stream < b.stream); 144+ const push = (head: Head) => { 145+ let at = heap.length; 148+ const parent = (at - 1) >>> 1; 149+ if (!less(head, heap[parent]!)) break; 150+ heap[at] = heap[parent]!; 155+ // Each claimant's seq from its id, as the graph reads it (abso 738): loading the stored atom for it read a row a 156+ // claimant, most of a warm draft-state's pairing on abso. 157+ const advance = (stream: number) => { 158+ const next = streams[stream]!.next(); 159+ if (!next.done) push({ id: next.value, seq: seqOf(next.value), stream }); 161+ for (let stream = 0; stream < streams.length; stream++) advance(stream); 163+ while (heap.length) { 164+ const head = heap[0]!; 165+ const tail = heap.pop()!; 168+ while (at * 2 + 1 < heap.length) { 169+ let child = at * 2 + 1; 170+ if (child + 1 < heap.length && less(heap[child + 1]!, heap[child]!)) child++; 171+ if (!less(heap[child]!, tail)) break; 172+ heap[at] = heap[child]!; 177+ advance(head.stream); 178+ if (head.seq === previous) continue; 179+ previous = head.seq; 183+ for (const stream of streams) stream.return?.(); 187+/** Resolver indexes share the same append order, so intersection needs two cursors. */ 188+function hasCommonResolver(g: Graph, lo: AtomId, hi: AtomId, settled: ReadonlySet<AtomId>): boolean { 189+ const left = g.eachResolverOf(lo); 190+ const right = g.eachResolverOf(hi); 192+ let a = left.next(); 193+ let b = right.next(); 194+ while (!a.done && !b.done) { 195+ const x = seqOf(a.value); 196+ const y = seqOf(b.value); 197+ if (x === y && settled.has(a.value)) return true; 198+ if (x <= y) a = left.next(); 199+ if (y <= x) b = right.next(); 69209 * Two pure insertions at the same point, and nothing else between them 70210 * (#37, the game trial): both only write the gap after one line. Neither Lines 237–244 at 318 (were 97–104 at 2)
98238 origin: (atom: AtomId, spaceName: string) => Origin, 100− yoursView: View,
101− theirsView: View,
240+ yoursView: ReadView, 241+ theirsView: ReadView, 102242 spaceName: string, 104244 const file = fileOfKey(pair.keys[0]!.key, yoursView); Lines 247–253 at 318 (were 107–113 at 2)
107247 if (attrsOnly.length === fileKeys.length) { 108248 // Both set the same attribute of the file: show each side's value. 109249 const name = attrsOnly[0]!.attr!; 110− const valueIn = (view: View, atom: AtomId): string => {
250+ const valueIn = (view: ReadView, atom: AtomId): string => { 111251 const set = (g.atom(atom).attrs ?? []).find((a) => a.file === file && a.name === name); 112252 return set?.value ?? (name === "exec" ? "" : String(view.attrs(file)[name] ?? "")); Lines 267–273 at 318 (were 127–133 at 2)
130−export function fileOfKey(key: string, view: View): string {
270+export function fileOfKey(key: string, view: ReadView): string { 131271 const { kind, node } = parseKey(key); 132272 if (kind === "attr") return node; 133273 return node.startsWith("^") ? node.slice(1) : view.fileOf(node); Lines 277–341 at 318 (were 137–162 at 2)
137277 * The files some atoms change: the graph's files of each, and, in views 138278 * with moves (#28), wherever the lines they touch are in those views. 140−export function touchedFiles(g: Graph, ids: Iterable<AtomId>, views: readonly View[]): Set<string> {
280+export function touchedFiles(g: Graph, ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string> { 141281 const out = new Set<string>(); 142282 const moved = views.filter((v) => v.hasMoves()); 143283 for (const id of ids) { 144284 for (const file of g.filesOf(id)) out.add(file); 145285 if (moved.length === 0) continue; 146286 const atom = g.atom(id); 147− const lines: string[] = [...g.deletedLines(id)];
287+ const lines: string[] = []; 148288 for (const run of atom.inserts) if (!run.parent.startsWith("^")) lines.push(run.parent); 149289 for (const m of atom.moves ?? []) { 150290 lines.push(m.line); 151291 if (!m.parent.startsWith("^")) lines.push(m.parent); 153− for (const v of moved) for (const line of lines) out.add(v.fileOf(line));
293+ for (const v of moved) { 294+ for (const line of lines) out.add(v.fileOf(line)); 295+ // Deleted lines by number where the view can (abso Capacity): a draft deleting a big file has thousands, and 296+ // making each one's id only to look its number up again was most of collisions on website. 297+ const byNumber = (v as { fileOfNumber?: (n: number) => string }).fileOfNumber; 298+ if (byNumber) for (const n of g.deletedNumbers(id)) out.add(byNumber.call(v, n)); 299+ else for (const line of g.deletedLines(id)) out.add(v.fileOf(line)); 306+ * The files a view over `below` plus `mine` can lay out differently from `below` (abso Capacity, View.shareFiles), or 307+ * 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 308+ * `mine` depends on that `below` lacks; each of those can change a file, so all of them count. Counting `mine` alone 309+ * missed the files of work the draft depends on (Testing's typescript-rs diff for cloud-ts-01, core 235). 311+export function touchedBeyond( 313+ below: ReadonlySet<AtomId>, 314+ mine: Iterable<AtomId>, 315+ views: readonly ReadView[], 316+ placedDifferently?: () => ReadonlySet<string> | undefined, 317+): Set<string> | undefined { 318+ const beyond = new Set<AtomId>(); 319+ const stack = [...mine]; 320+ while (stack.length) { 321+ const id = stack.pop()!; 322+ if (beyond.has(id) || below.has(id)) continue; 324+ for (const dep of g.atom(id).deps) if (!beyond.has(dep) && !below.has(dep)) stack.push(dep); 326+ const files = touchedFiles(g, beyond, views); 327+ // Moves change more than the files their atoms name only where a moved line lands differently (View.placedDifferently, 328+ // abso Capacity); without that comparison, a draft that moves lines shares nothing. 329+ if ([...beyond].some((id) => (g.atom(id).moves?.length ?? 0) > 0)) { 330+ const placed = placedDifferently?.(); 331+ if (!placed) return undefined; 332+ for (const file of placed) files.add(file); 158337/** The atom's visible lines in a file of a view, or where it acted when none are left. */ 159−export function describeSide(view: View, atom: AtomId, file: string, keys: readonly string[]): CollisionSide {
338+export function describeSide(view: ReadView, atom: AtomId, file: string, keys: readonly string[]): CollisionSide { 160339 const visible = view.lines(file); 162341 let lastLine = -1; Lines 365–373 at 318 (were 186–194 at 2)
186365 readonly spaces: ReadonlyMap<string, Space>; 187366 version(): number; 188367 spaceOf(agent: AgentState): Space; 189− agentSet(agent: AgentState): Set<AtomId>;
368+ agentSet(agent: AgentState): ReadonlySet<AtomId>; 190369 draft(agent: AgentState): AtomId[]; 191− agentView(agent: AgentState): View;
370+ agentView(agent: AgentState): ReadView; 192371 at(space: Space, version: number): ReadonlySet<AtomId>; Lines 419–426 at 318 (were 240–247 at 2)
240419 kind: "duplicate", 243− yours: { lines: [yours.start + 1, yours.end], text: yours.text + "\n" },
244− theirs: { lines: [theirs.start + 1, theirs.end], text: theirs.text + "\n", ...originOf(host, by, space.name) },
422+ yours: { lines: [yours.start + 1, yours.end], text: `${yours.text}\n` }, 423+ theirs: { lines: [theirs.start + 1, theirs.end], text: `${theirs.text}\n`, ...originOf(host, by, space.name) }, Lines 435–445 at 318 (were 256–266 at 2)
256435export function openPairs(host: CollisionHost, agent: AgentState): Pair[] { 257436 const space = host.spaceOf(agent); 258437 const set = host.agentSet(agent); 259− const mine = new Set(agent.mine);
438+ const mine = new StableOverlaySet(agent.mine); 260439 const out: Pair[] = []; 261440 const draft = host.draft(agent); 262441 if (draft.length > 0) { 263− const seen = new Set([...host.at(space, agent.base)].filter((id) => !mine.has(id)));
442+ const seen = differenceSet(host.at(space, agent.base), mine); 264443 out.push(...pairsIn(host.graph, draft, seen, set)); 266445 if (space.name !== SHARED) {