{"draft":null,"from":227,"to":318,"files":[{"path":"worker/project/draft-collisions.ts","added":57,"removed":14,"diff":"--- a/worker/project/draft-collisions.ts\n+++ b/worker/project/draft-collisions.ts\n@@ -2,9 +2,11 @@\n // is the project in memory.\n \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@@ -24,8 +26,13 @@\n \tdefinitionRule(): DefinitionRule;\n \tdraft(agent: Agent): AtomId[];\n \tagentView(agent: Agent): ReadView;\n-\tview(set: ReadonlySet<AtomId>): 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@@ -37,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@@ -71,13 +101,19 @@\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, once per agent: the key of a view of a set lists every atom in it.\n-\tconst nowViews = new Map<Agent, ReadView>();\n-\tconst nowViewOf = (agent: Agent) => {\n-\t\tlet view = nowViews.get(agent);\n-\t\tif (!view) nowViews.set(agent, (view = host.view(new Set([...shared, ...host.draft(agent)]))));\n-\t\treturn view;\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@@ -93,7 +129,7 @@\n \t\t\tconst draftA = host.draft(a);\n \t\t\tconst draftB = host.draft(b);\n \t\t\tconst newer = draftSet(a);\n-\t\t\tconst union = new Set([...shared, ...draftA, ...draftB]);\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@@ -133,16 +169,21 @@\n \t\t\t\tconst mineA = new Set(draftA);\n \t\t\t\tconst mineB = new Set(draftB);\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\tboth ??= host.view(union);\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\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@@ -156,7 +197,8 @@\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 = () => nowViewOf(a);\n-\t\t\tfor (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED)) {\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@@ -168,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":[]}