Lines 2–12 at 318 (were 2–10 at 223)
22// is the project in memory. 44import { type AtomId, type Graph, parseKey, type ReadView } from "../../src/engine/index.ts"; 5+import { StableSet } from "../../src/engine/state-source.ts"; 56import { 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 26–38 at 318 (were 24–31 at 223)
2426 definitionRule(): DefinitionRule; 2527 draft(agent: Agent): AtomId[]; 2628 agentView(agent: Agent): ReadView; 27− view(set: ReadonlySet<AtomId>): ReadView;
29+ /** Shared now with these agents' drafts on top. */ 30+ draftsView(agents: readonly Agent[]): ReadView; 2831 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 37–42 at 223)
3744 * 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 3845 * 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; 4070export function draftCollisions( 4171 host: DraftCollisionHost, 4272 options: { file?: string; folder?: string } = {}, Lines 101–119 at 318 (were 71–83 at 223)
71101 if (!set) draftSets.set(agent, (set = new Set(host.draft(agent)))); 74− // Shared now with each agent's draft on top, once per agent: the key of a view of a set lists every atom in it.
75− const nowViews = new Map<Agent, ReadView>();
76− const nowViewOf = (agent: Agent) => {
77− let view = nowViews.get(agent);
78− if (!view) nowViews.set(agent, (view = host.view(new Set([...shared, ...host.draft(agent)]))));
79− return view;
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(); 81117 // Each agent's touched files once, not once per pair (abso Capacity). 82118 const touched = new Map<Agent, Set<string>>(); 83119 const touchedBy = (agent: Agent) => { Lines 129–135 at 318 (were 93–99 at 223)
93129 const draftA = host.draft(a); 94130 const draftB = host.draft(b); 95131 const newer = draftSet(a); 96− const union = new Set([...shared, ...draftA, ...draftB]);
132+ const union = new WithDrafts(shared, [...draftA, ...draftB]); 97133 const viewA = host.agentView(a); 98134 const viewB = host.agentView(b); Lines 169–189 at 318 (were 133–148 at 223)
133169 const mineA = new Set(draftA); 134170 const mineB = new Set(draftB); 135171 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(); 136179 for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) { 137− both ??= host.view(union);
138− if (!both.hasFile(file)) continue;
139− for (const [name, defs] of definedTwice(both, file)) {
140− const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.line(l)!.atom)));
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)!))); 141182 const dA = by(mineA); 142183 const dB = by(mineB); 143184 if (!dA || !dB || dA === dB) continue; 144185 const atomsOf = (d: Definition, mine: Set<AtomId>) => 145− 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)); 146187 if (atomsOf(dA, mineA).some((x) => atomsOf(dB, mineB).some((y) => paired.has(`${x} ${y}`)))) continue; 148189 kind: "duplicate", Lines 197–204 at 318 (were 156–162 at 223)
157198 // Definitions: b's new lines use a name whose definition a's changes change. 158199 const nowView = () => nowViewOf(a); 159− for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED)) {
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)) { 160202 const c = d.collision; 162204 kind: "definition", Lines 210–216 at 318 (were 168–173 at 223)
171214 const fileKey = options.file !== undefined ? pathKey(options.file) : undefined; 172215 const folderParts = 173216 options.folder !== undefined ? options.folder.split("/").filter((s) => s !== "" && s !== ".") : undefined;