{"draft":null,"from":228,"to":229,"files":[{"path":"worker/project/draft-collisions.ts","added":33,"removed":6,"diff":"--- a/worker/project/draft-collisions.ts\n+++ b/worker/project/draft-collisions.ts\n@@ -2,6 +2,7 @@\n // is the project in memory.\n \n import { type AtomId, type Graph, parseKey, type ReadView } from \"../../src/engine/index.ts\";\n+import { StableSet, unionOverlay } 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@@ -37,6 +38,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,11 +95,14 @@\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+\t// Shared now with each agent's draft on top, once per agent. Over shared's view's own set (abso Capacity): a view's set\n+\t// is closed under dependencies anyway, so shared's closed set and the drafts lay out what shared's atoms and the drafts\n+\t// do, and only the drafts' atoms are walked to close it.\n+\tconst withDraftsView = (...drafts: readonly AtomId[][]) => host.view(unionOverlay(sharedView.set, drafts.flat()));\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\tif (!view) nowViews.set(agent, (view = withDraftsView(host.draft(agent))));\n \t\treturn view;\n \t};\n \t// Each agent's touched files once, not once per pair (abso Capacity).\n@@ -93,7 +120,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@@ -134,15 +161,15 @@\n \t\t\t\tconst mineB = new Set(draftB);\n \t\t\t\tconst filesA = touchedBy(a);\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\tboth ??= withDraftsView(draftA, draftB);\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\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\","}],"unmatched":[]}