{"draft":null,"from":7,"to":318,"files":[{"path":"worker/project/draft-collisions.ts","added":98,"removed":21,"diff":"--- a/worker/project/draft-collisions.ts\n+++ b/worker/project/draft-collisions.ts\n@@ -1,10 +1,12 @@\n // Whether agents' unpublished changes would collide (#41, F22), read through DraftCollisionHost; today the host\n // is the project in memory.\n \n-import { type AtomId, type Graph, parseKey, type View } from \"../../src/engine/index.ts\";\n-import { describeSide, fileOfKey, type Pair } from \"./collisions.ts\";\n+import { type AtomId, type Graph, parseKey, type ReadView } from \"../../src/engine/index.ts\";\n+import { StableSet } from \"../../src/engine/state-source.ts\";\n+import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from \"./collisions.ts\";\n import type { DefinitionRule } from \"./definition-rule.ts\";\n import { type Definition, definedTwice } from \"./definitions.ts\";\n+import { definitionsBytes, type KeptParts } from \"./kept-parts.ts\";\n import { pathKey } from \"./pathkey.ts\";\n import type { CollisionSide, DraftCollision } from \"./project-types.ts\";\n import { last, SHARED, type Space } from \"./spaces.ts\";\n@@ -20,13 +22,17 @@\n \treadonly agents: ReadonlyMap<string, Agent>;\n \tneedInit(): void;\n \tshared(): Space;\n-\tsharedView(): View;\n+\tsharedView(): ReadView;\n \tdefinitionRule(): DefinitionRule;\n \tdraft(agent: Agent): AtomId[];\n-\tagentView(agent: Agent): View;\n-\tview(set: ReadonlySet<AtomId>): View;\n-\tpairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];\n-\ttouchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;\n+\tagentView(agent: Agent): ReadView;\n+\t/** Shared now with these agents' drafts on top. */\n+\tdraftsView(agents: readonly Agent[]): ReadView;\n+\ttouchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;\n+\t/** What collisions keep between calls (kept-parts.ts), when the project keeps any. */\n+\tkeptParts?(): KeptParts | undefined;\n+\t/** A key naming everything a draft's part of the answer depends on: shared's version, its base and its atoms. */\n+\tdraftKey(agent: Agent): string;\n }\n \n /**\n@@ -38,6 +44,29 @@\n  * 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\n  * the answer, as a real publish's would. `file` or `folder` narrows it.\n  */\n+/**\n+ * A set and some drafts' atoms, by lookup (abso Capacity): copying shared's atoms into a new set read every one of them\n+ * from disk on a stored writer, once per pair of agents.\n+ */\n+class WithDrafts extends StableSet<AtomId> {\n+\treadonly retainedBytes: number;\n+\treadonly #base: ReadonlySet<AtomId>;\n+\treadonly #drafts: ReadonlySet<AtomId>;\n+\tconstructor(base: ReadonlySet<AtomId>, drafts: Iterable<AtomId>) {\n+\t\tsuper();\n+\t\tthis.#base = base;\n+\t\tthis.#drafts = new Set(drafts);\n+\t\tthis.retainedBytes = 64 + this.#drafts.size * 32;\n+\t}\n+\thas(atom: AtomId): boolean {\n+\t\treturn this.#drafts.has(atom) || this.#base.has(atom);\n+\t}\n+\t*values(): SetIterator<AtomId> {\n+\t\tyield* this.#base;\n+\t\tfor (const atom of this.#drafts) if (!this.#base.has(atom)) yield atom;\n+\t}\n+}\n+\n export function draftCollisions(\n \thost: DraftCollisionHost,\n \toptions: { file?: string; folder?: string } = {},\n@@ -54,6 +83,44 @@\n \tconst sharedView = host.sharedView();\n \tconst out: DraftCollision[] = [];\n \tconst side = (agent: string, file: string, s: CollisionSide) => ({ agent, file, lines: s.lines, text: s.text });\n+\t// Every draft change's claimants among all the drafts, found once (abso Capacity): a stored atom's keys are read\n+\t// from disk, and a draft deleting a big file has thousands, read again for every other agent.\n+\tlet claimants: ReadonlyMap<AtomId, readonly AtomId[]> | undefined;\n+\tconst claimantsOf = (agent: Agent): ReadonlyMap<AtomId, readonly AtomId[]> => {\n+\t\tclaimants ??= claimantsAmong(\n+\t\t\thost.graph,\n+\t\t\tworking.flatMap((a) => host.draft(a)),\n+\t\t);\n+\t\tconst all = claimants;\n+\t\treturn new Map(host.draft(agent).map((id) => [id, all.get(id) ?? []]));\n+\t};\n+\t// One set per draft, so the definition rule keeps what it worked out about it (abso Capacity).\n+\tconst draftSets = new Map<Agent, ReadonlySet<AtomId>>();\n+\tconst draftSet = (agent: Agent) => {\n+\t\tlet set = draftSets.get(agent);\n+\t\tif (!set) draftSets.set(agent, (set = new Set(host.draft(agent))));\n+\t\treturn set;\n+\t};\n+\t// Shared now with each agent's draft on top (project.ts, draftsView): kept between calls while shared and the\n+\t// draft stay the same, and laid out as shared is for every file the draft doesn't touch (abso Capacity).\n+\tconst nowViewOf = (agent: Agent) => host.draftsView([agent]);\n+\t// Each draft's uses and changed definitions, and each pair's names defined twice, kept between calls (kept-parts.ts,\n+\t// abso Capacity): what this call doesn't use is dropped at its end.\n+\tconst parts = host.keptParts?.();\n+\tconst keys = new Map<Agent, string>();\n+\tconst keyOf = (agent: Agent) => {\n+\t\tlet key = keys.get(agent);\n+\t\tif (key === undefined) keys.set(agent, (key = host.draftKey(agent)));\n+\t\treturn key;\n+\t};\n+\tparts?.startRound();\n+\t// Each agent's touched files once, not once per pair (abso Capacity).\n+\tconst touched = new Map<Agent, Set<string>>();\n+\tconst touchedBy = (agent: Agent) => {\n+\t\tlet files = touched.get(agent);\n+\t\tif (!files) touched.set(agent, (files = host.touchedFiles(host.draft(agent), [host.agentView(agent)])));\n+\t\treturn files;\n+\t};\n \tfor (let i = 0; i < working.length; i++) {\n \t\tfor (let j = 0; j < working.length; j++) {\n \t\t\tif (i === j) continue;\n@@ -61,14 +128,14 @@\n \t\t\tconst b = working[j]!;\n \t\t\tconst draftA = host.draft(a);\n \t\t\tconst draftB = host.draft(b);\n-\t\t\tconst newer = new Set(draftA);\n-\t\t\tconst union = new Set([...shared, ...draftA, ...draftB]);\n+\t\t\tconst newer = draftSet(a);\n+\t\t\tconst union = new WithDrafts(shared, [...draftA, ...draftB]);\n \t\t\tconst viewA = host.agentView(a);\n \t\t\tconst viewB = host.agentView(b);\n \t\t\tif (i < j) {\n \t\t\t\t// Lines and attributes: the same either way round, so once per pair.\n \t\t\t\tconst paired = new Set<string>();\n-\t\t\t\tfor (const pair of host.pairs(draftB, newer, union)) {\n+\t\t\t\tfor (const pair of pairsFrom(host.graph, claimantsOf(b), newer, union)) {\n \t\t\t\t\tpaired.add(`${pair.theirs} ${pair.yours}`);\n \t\t\t\t\tconst file = fileOfKey(pair.keys[0]!.key, viewB);\n \t\t\t\t\tconst fileKeys = pair.keys.filter((k) => fileOfKey(k.key, viewB) === file).map((k) => k.key);\n@@ -96,33 +163,42 @@\n \t\t\t\t}\n \t\t\t\t// A name both define at the top level of one file: what the second meets once it syncs onto the\n \t\t\t\t// first's work, as the publish rule checks it then. Not when the two definitions are one lines collision.\n-\t\t\t\tconst both = host.view(union);\n+\t\t\t\t// The union's view only when the two share a file (abso Capacity): laying out shared plus both drafts\n+\t\t\t\t// cost a few seconds a pair on a stored writer.\n+\t\t\t\tlet both: ReadView | undefined;\n \t\t\t\tconst mineA = new Set(draftA);\n \t\t\t\tconst mineB = new Set(draftB);\n-\t\t\t\tconst filesA = host.touchedFiles(draftA, [viewA]);\n-\t\t\t\tfor (const file of [...host.touchedFiles(draftB, [viewB])].filter((f) => filesA.has(f)).toSorted()) {\n-\t\t\t\t\tif (!both.hasFile(file)) continue;\n-\t\t\t\t\tfor (const [name, defs] of definedTwice(both, file)) {\n-\t\t\t\t\t\tconst by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.line(l)!.atom)));\n+\t\t\t\tconst filesA = touchedBy(a);\n+\t\t\t\tconst twiceIn = (file: string): ReadonlyMap<string, Definition[]> => {\n+\t\t\t\t\tconst make = () => {\n+\t\t\t\t\t\tboth ??= host.draftsView([a, b]);\n+\t\t\t\t\t\treturn both.hasFile(file) ? definedTwice(both, file) : new Map<string, Definition[]>();\n+\t\t\t\t\t};\n+\t\t\t\t\treturn parts ? parts.get(`twice ${keyOf(a)} | ${keyOf(b)} ${file}`, make, definitionsBytes) : make();\n+\t\t\t\t};\n+\t\t\t\tfor (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) {\n+\t\t\t\t\tfor (const [name, defs] of twiceIn(file)) {\n+\t\t\t\t\t\tconst by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!)));\n \t\t\t\t\t\tconst dA = by(mineA);\n \t\t\t\t\t\tconst dB = by(mineB);\n \t\t\t\t\t\tif (!dA || !dB || dA === dB) continue;\n \t\t\t\t\t\tconst atomsOf = (d: Definition, mine: Set<AtomId>) =>\n-\t\t\t\t\t\t\td.lines.map((l) => host.graph.line(l)!.atom).filter((x) => mine.has(x));\n+\t\t\t\t\t\t\td.lines.map((l) => host.graph.lineAtom(l)!).filter((x) => mine.has(x));\n \t\t\t\t\t\tif (atomsOf(dA, mineA).some((x) => atomsOf(dB, mineB).some((y) => paired.has(`${x} ${y}`)))) continue;\n \t\t\t\t\t\tout.push({\n \t\t\t\t\t\t\tkind: \"duplicate\",\n \t\t\t\t\t\t\tname,\n \t\t\t\t\t\t\tfile,\n-\t\t\t\t\t\t\tfirst: { agent: a.name, file, lines: [dA.start + 1, dA.end], text: dA.text + \"\\n\" },\n-\t\t\t\t\t\t\tsecond: { agent: b.name, file, lines: [dB.start + 1, dB.end], text: dB.text + \"\\n\" },\n+\t\t\t\t\t\t\tfirst: { agent: a.name, file, lines: [dA.start + 1, dA.end], text: `${dA.text}\\n` },\n+\t\t\t\t\t\t\tsecond: { agent: b.name, file, lines: [dB.start + 1, dB.end], text: `${dB.text}\\n` },\n \t\t\t\t\t\t});\n \t\t\t\t\t}\n \t\t\t\t}\n \t\t\t}\n \t\t\t// Definitions: b's new lines use a name whose definition a's changes change.\n-\t\t\tconst nowView = host.view(new Set([...shared, ...draftA]));\n-\t\t\tfor (const d of host.definitionRule().collisions(new Set(draftB), viewB, sharedView, nowView, newer, SHARED)) {\n+\t\t\tconst nowView = () => nowViewOf(a);\n+\t\t\tconst kept = parts ? { parts, uses: keyOf(b), changed: keyOf(a) } : undefined;\n+\t\t\tfor (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED, kept)) {\n \t\t\t\tconst c = d.collision;\n \t\t\t\tout.push({\n \t\t\t\t\tkind: \"definition\",\n@@ -134,6 +210,7 @@\n \t\t\t}\n \t\t}\n \t}\n+\tparts?.endRound();\n \tconst fileKey = options.file !== undefined ? pathKey(options.file) : undefined;\n \tconst folderParts =\n \t\toptions.folder !== undefined ? options.folder.split(\"/\").filter((s) => s !== \"\" && s !== \".\") : undefined;"}],"unmatched":[]}