Lines 1–12 at 318 (were 1–10 at 116)
11// Whether agents' unpublished changes would collide (#41, F22), read through DraftCollisionHost; today the host 22// is the project in memory. 4−import { type AtomId, type Graph, parseKey, type View } from "../../src/engine/index.ts";
5−import { describeSide, fileOfKey, type Pair } from "./collisions.ts";
4+import { type AtomId, type Graph, parseKey, type ReadView } from "../../src/engine/index.ts"; 5+import { StableSet } from "../../src/engine/state-source.ts"; 6+import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from "./collisions.ts"; 67import type { DefinitionRule } from "./definition-rule.ts"; 78import { type Definition, definedTwice } from "./definitions.ts"; 9+import { definitionsBytes, type KeptParts } from "./kept-parts.ts"; 810import { pathKey } from "./pathkey.ts"; 911import type { CollisionSide, DraftCollision } from "./project-types.ts"; 1012import { last, SHARED, type Space } from "./spaces.ts"; Lines 22–38 at 318 (were 20–32 at 116)
2022 readonly agents: ReadonlyMap<string, Agent>; 23− sharedView(): View;
25+ sharedView(): ReadView; 2426 definitionRule(): DefinitionRule; 2527 draft(agent: Agent): AtomId[]; 26− agentView(agent: Agent): View;
27− view(set: ReadonlySet<AtomId>): View;
28− pairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];
29− touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;
28+ agentView(agent: Agent): ReadView; 29+ /** Shared now with these agents' drafts on top. */ 30+ draftsView(agents: readonly Agent[]): ReadView; 31+ touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>; 32+ /** What collisions keep between calls (kept-parts.ts), when the project keeps any. */ 33+ keptParts?(): KeptParts | undefined; 34+ /** A key naming everything a draft's part of the answer depends on: shared's version, its base and its atoms. */ 35+ draftKey(agent: Agent): string; Lines 44–72 at 318 (were 38–43 at 116)
3844 * top of a file (refused once the second syncs onto the first's work). Exact for shared at `version`; as shared moves on, so can 3945 * the answer, as a real publish's would. `file` or `folder` narrows it. 48+ * A set and some drafts' atoms, by lookup (abso Capacity): copying shared's atoms into a new set read every one of them 49+ * from disk on a stored writer, once per pair of agents. 51+class WithDrafts extends StableSet<AtomId> { 52+ readonly retainedBytes: number; 53+ readonly #base: ReadonlySet<AtomId>; 54+ readonly #drafts: ReadonlySet<AtomId>; 55+ constructor(base: ReadonlySet<AtomId>, drafts: Iterable<AtomId>) { 58+ this.#drafts = new Set(drafts); 59+ this.retainedBytes = 64 + this.#drafts.size * 32; 61+ has(atom: AtomId): boolean { 62+ return this.#drafts.has(atom) || this.#base.has(atom); 64+ *values(): SetIterator<AtomId> { 66+ for (const atom of this.#drafts) if (!this.#base.has(atom)) yield atom; 4170export function draftCollisions( 4271 host: DraftCollisionHost, 4372 options: { file?: string; folder?: string } = {}, Lines 83–126 at 318 (were 54–59 at 116)
5483 const sharedView = host.sharedView(); 5584 const out: DraftCollision[] = []; 5685 const side = (agent: string, file: string, s: CollisionSide) => ({ agent, file, lines: s.lines, text: s.text }); 86+ // Every draft change's claimants among all the drafts, found once (abso Capacity): a stored atom's keys are read 87+ // from disk, and a draft deleting a big file has thousands, read again for every other agent. 88+ let claimants: ReadonlyMap<AtomId, readonly AtomId[]> | undefined; 89+ const claimantsOf = (agent: Agent): ReadonlyMap<AtomId, readonly AtomId[]> => { 90+ claimants ??= claimantsAmong( 92+ working.flatMap((a) => host.draft(a)), 94+ const all = claimants; 95+ return new Map(host.draft(agent).map((id) => [id, all.get(id) ?? []])); 97+ // One set per draft, so the definition rule keeps what it worked out about it (abso Capacity). 98+ const draftSets = new Map<Agent, ReadonlySet<AtomId>>(); 99+ const draftSet = (agent: Agent) => { 100+ let set = draftSets.get(agent); 101+ if (!set) draftSets.set(agent, (set = new Set(host.draft(agent)))); 104+ // Shared now with each agent's draft on top (project.ts, draftsView): kept between calls while shared and the 105+ // draft stay the same, and laid out as shared is for every file the draft doesn't touch (abso Capacity). 106+ const nowViewOf = (agent: Agent) => host.draftsView([agent]); 107+ // Each draft's uses and changed definitions, and each pair's names defined twice, kept between calls (kept-parts.ts, 108+ // abso Capacity): what this call doesn't use is dropped at its end. 109+ const parts = host.keptParts?.(); 110+ const keys = new Map<Agent, string>(); 111+ const keyOf = (agent: Agent) => { 112+ let key = keys.get(agent); 113+ if (key === undefined) keys.set(agent, (key = host.draftKey(agent))); 116+ parts?.startRound(); 117+ // Each agent's touched files once, not once per pair (abso Capacity). 118+ const touched = new Map<Agent, Set<string>>(); 119+ const touchedBy = (agent: Agent) => { 120+ let files = touched.get(agent); 121+ if (!files) touched.set(agent, (files = host.touchedFiles(host.draft(agent), [host.agentView(agent)]))); 57124 for (let i = 0; i < working.length; i++) { 58125 for (let j = 0; j < working.length; j++) { 59126 if (i === j) continue; Lines 128–141 at 318 (were 61–74 at 116)
61128 const b = working[j]!; 62129 const draftA = host.draft(a); 63130 const draftB = host.draft(b); 64− const newer = new Set(draftA);
65− const union = new Set([...shared, ...draftA, ...draftB]);
131+ const newer = draftSet(a); 132+ const union = new WithDrafts(shared, [...draftA, ...draftB]); 66133 const viewA = host.agentView(a); 67134 const viewB = host.agentView(b); 69136 // Lines and attributes: the same either way round, so once per pair. 70137 const paired = new Set<string>(); 71− for (const pair of host.pairs(draftB, newer, union)) {
138+ for (const pair of pairsFrom(host.graph, claimantsOf(b), newer, union)) { 72139 paired.add(`${pair.theirs} ${pair.yours}`); 73140 const file = fileOfKey(pair.keys[0]!.key, viewB); 74141 const fileKeys = pair.keys.filter((k) => fileOfKey(k.key, viewB) === file).map((k) => k.key); Lines 163–189 at 318 (were 96–114 at 116)
97164 // A name both define at the top level of one file: what the second meets once it syncs onto the 98165 // first's work, as the publish rule checks it then. Not when the two definitions are one lines collision. 99− const both = host.view(union);
166+ // The union's view only when the two share a file (abso Capacity): laying out shared plus both drafts 167+ // cost a few seconds a pair on a stored writer. 168+ let both: ReadView | undefined; 100169 const mineA = new Set(draftA); 101170 const mineB = new Set(draftB); 102− const filesA = host.touchedFiles(draftA, [viewA]);
103− for (const file of [...host.touchedFiles(draftB, [viewB])].filter((f) => filesA.has(f)).toSorted()) {
104− if (!both.hasFile(file)) continue;
105− for (const [name, defs] of definedTwice(both, file)) {
106− const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.line(l)!.atom)));
171+ const filesA = touchedBy(a); 172+ const twiceIn = (file: string): ReadonlyMap<string, Definition[]> => { 173+ const make = () => { 174+ both ??= host.draftsView([a, b]); 175+ return both.hasFile(file) ? definedTwice(both, file) : new Map<string, Definition[]>(); 177+ return parts ? parts.get(`twice ${keyOf(a)} | ${keyOf(b)} ${file}`, make, definitionsBytes) : make(); 179+ for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) { 180+ for (const [name, defs] of twiceIn(file)) { 181+ const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!))); 107182 const dA = by(mineA); 108183 const dB = by(mineB); 109184 if (!dA || !dB || dA === dB) continue; 110185 const atomsOf = (d: Definition, mine: Set<AtomId>) => 111− d.lines.map((l) => host.graph.line(l)!.atom).filter((x) => mine.has(x));
186+ d.lines.map((l) => host.graph.lineAtom(l)!).filter((x) => mine.has(x)); 112187 if (atomsOf(dA, mineA).some((x) => atomsOf(dB, mineB).some((y) => paired.has(`${x} ${y}`)))) continue; 114189 kind: "duplicate", Lines 196–204 at 318 (were 121–128 at 116)
123198 // Definitions: b's new lines use a name whose definition a's changes change. 124− const nowView = host.view(new Set([...shared, ...draftA]));
125− for (const d of host.definitionRule().collisions(new Set(draftB), viewB, sharedView, nowView, newer, SHARED)) {
199+ const nowView = () => nowViewOf(a); 200+ const kept = parts ? { parts, uses: keyOf(b), changed: keyOf(a) } : undefined; 201+ for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED, kept)) { 126202 const c = d.collision; 128204 kind: "definition", Lines 210–216 at 318 (were 134–139 at 116)
137214 const fileKey = options.file !== undefined ? pathKey(options.file) : undefined; 138215 const folderParts = 139216 options.folder !== undefined ? options.folder.split("/").filter((s) => s !== "" && s !== ".") : undefined;