{"version":212,"agent":"capacity","message":"Draft reads on a stored writer (disk-first 2; abso Capacity): where, drafts, status and collisions on website's export.\n\n- where looks deleted lines up by number (graph.deletedNumbers, View.fileOfNumber, View.lineNumberOf). It takes the agent's view once instead of once per line, since each read weighed every cached view. It finds the draft's lines by writer (View.visibleWriters), with no text or id read per line.\n- Draft files (overlaps.ts) go by number the same way.\n- An agent's view takes its base's resolved moves when both hold the same atoms that move lines (View.shareMoves checks this).\n- Draft collisions:\n  - build the union view only when two drafts share a file;\n  - work out each agent's touched files and its \"shared now plus draft\" view once;\n  - find claimants among the drafts from the drafts' own keys (collisions.ts claimantsAmong) instead of walking each key's history;\n  - have the definition rule keep a draft's uses, and the newer work's definitions, across partners.\n- A stored list read in order reads its length once, not before every element.\n\ndraft-reads-stored.test.ts checks where and collisions against the in-memory project. It covers edits, a whole file deleted, a line collision, one name defined twice, and bases before and after a move. 135 of 136 related tests pass; none fail.\n\nOn website's export (1,387 entries) the answers match Project.load:\n- status: 17 s to 4.5 s\n- where: over 60 s to 8.8 s\n- drafts: 14 s to 5.3 s\n- collisions: over 60 s to 54 s, measured under the profiler before the last two changes","time":1792036932387,"timeIso":"2026-10-15T04:02:12.387Z","changes":10,"change":3537,"files":["src/engine/graph.ts","src/engine/state-source.ts","src/engine/view.ts","worker/project/collisions.ts","worker/project/definition-rule.ts","worker/project/draft-collisions.ts","worker/project/overlaps.ts","worker/project/project.ts","worker/project/test/draft-reads-stored.test.ts","worker/project/where.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":11,"removed":0,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -262,6 +262,7 @@\n export type PackedDelete = [number, number];\n /** An attribute set on a file: [seq, name, value]. */\n export type PackedAttr = [number, string, string | null];\n+const EMPTY_NUMBERS: readonly number[] = Object.freeze([]);\n /** Lines longer than this aren't packed with their file; their text is read on its own. */\n const PACKED_TEXT = 4096;\n \n@@ -879,6 +880,16 @@\n \t\treturn out;\n \t}\n \n+\t/** The numbers of the lines an atom deletes, from the graph's packed copy: no lookup per line id. */\n+\t*deletedNumbers(id: AtomId): IterableIterator<number> {\n+\t\tconst seq = this.#seq(id);\n+\t\tif (this.#storage?.read(\"atom-delete-counts\", String(seq)) !== undefined) {\n+\t\t\tyield* storedLists<number>(this.#storage!, \"atom-deletes\").values(String(seq));\n+\t\t\treturn;\n+\t\t}\n+\t\tyield* this.#atomDeletes[seq] ?? EMPTY_NUMBERS;\n+\t}\n+\n \t/** How many lines an atom deletes, without reading its lists. */\n \tdeleteCount(id: AtomId): number {\n \t\tconst seq = this.#seq(id);"},{"path":"src/engine/state-source.ts","added":14,"removed":7,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -594,16 +594,23 @@\n \t\treturn count as number;\n \t};\n \tlength();\n+\tconst at = (index: number): T => {\n+\t\tconst raw = storage.read(name, String(index));\n+\t\tconst value = raw === undefined ? undefined : decode ? decode(raw, index) : (raw as T);\n+\t\tif (value === undefined) throw Error(\"missing stored list element\");\n+\t\treturn value && typeof value === \"object\" ? (Object.freeze(value) as T) : value;\n+\t};\n+\t// In order, with the length read once (abso Capacity): the array iterator reads it again before every element, a\n+\t// second disk read per element of a list too long for the cache.\n+\tfunction* inOrder(): IterableIterator<T> {\n+\t\tconst count = length();\n+\t\tfor (let index = 0; index < count; index++) yield at(index);\n+\t}\n \treturn new Proxy([] as T[], {\n \t\tget(target, key, receiver) {\n \t\t\tif (key === \"length\") return length();\n-\t\t\tif (indexKey(key)) {\n-\t\t\t\tif (Number(key) >= length()) return undefined;\n-\t\t\t\tconst raw = storage.read(name, key);\n-\t\t\t\tconst value = raw === undefined ? undefined : decode ? decode(raw, Number(key)) : (raw as T);\n-\t\t\t\tif (value === undefined) throw Error(\"missing stored list element\");\n-\t\t\t\treturn value && typeof value === \"object\" ? Object.freeze(value) : value;\n-\t\t\t}\n+\t\t\tif (indexKey(key)) return Number(key) >= length() ? undefined : at(Number(key));\n+\t\t\tif (key === Symbol.iterator || key === \"values\") return inOrder;\n \t\t\treturn Reflect.get(target, key, receiver);\n \t\t},\n \t\thas(target, key) {"},{"path":"src/engine/view.ts","added":29,"removed":7,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -10,7 +10,7 @@\n // made when a caller asks for them, and the latest asked for is kept.\n \n import type { Graph, RelocationInfo } from \"./graph.ts\";\n-import { isStartNode } from \"./keys.ts\";\n+import { isStartNode, seqOf } from \"./keys.ts\";\n import { resolveScratchMoves, scratchLayoutNodes } from \"./relocation-scratch.ts\";\n import { isClosedSet, StableSet } from \"./state-source.ts\";\n import { joinLines } from \"./text.ts\";\n@@ -47,7 +47,10 @@\n }\n \n /** Structural read boundary shared by historical and transaction-bound projections. */\n-export type ReadView = Omit<Pick<View, keyof View>, \"sameText\" | \"visibleCount\" | \"shareMoves\"> & {\n+export type ReadView = Omit<\n+\tPick<View, keyof View>,\n+\t\"sameText\" | \"visibleCount\" | \"shareMoves\" | \"fileOfNumber\" | \"lineNumberOf\" | \"visibleWriters\"\n+> & {\n \tsameText(path: string, other: ReadView): boolean;\n };\n \n@@ -280,13 +283,16 @@\n \t#fileOfShared: Map<number, string> | undefined;\n \n \t/**\n-\t * Take another view's resolved moves (abso Capacity). Only for a set that differs from that view's by atoms that\n-\t * move nothing, drop nothing and change no closure: its moves resolve the same, and every line it shares with that\n-\t * view sits where it sat there.\n+\t * Take another view's resolved moves (abso Capacity), when both sets hold the very same atoms that move lines: the\n+\t * moves then resolve the same, and every line sits where it sits there. Checked here, so any caller may ask.\n \t */\n \tshareMoves(from: View): void {\n \t\tif (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves) return;\n-\t\tif (from.#scratchMoves()) return;\n+\t\tif (this.#relocs || from.#scratchMoves()) return;\n+\t\tfor (const id of this.graph.movers()) {\n+\t\t\tconst seq = seqOf(id);\n+\t\t\tif (this.#has(seq) !== from.#has(seq)) return;\n+\t\t}\n \t\tthis.#relocs = from.#relocations();\n \t\tthis.#relocsN = from.#relocationsN();\n \t\tthis.#movesOnDisk = false;\n@@ -696,6 +702,11 @@\n \t\treturn [...this.#known];\n \t}\n \n+\t/** The file line n sits in, in this set: fileOf by the line's number, with no lookup of its id. */\n+\tfileOfNumber(n: number): string {\n+\t\treturn this.#fileOfN(n);\n+\t}\n+\n \t/** How many visible lines a file has. */\n \tvisibleCount(path: string): number {\n \t\treturn this.#file(path).vis.length;\n@@ -849,14 +860,25 @@\n \n \t/** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */\n \tlineNumber(path: string, line: LineId): number {\n+\t\treturn this.lineNumberOf(path, this.graph.lineN(line));\n+\t}\n+\n+\t/** lineNumber by the line's number, with no lookup of its id. */\n+\tlineNumberOf(path: string, n: number): number {\n \t\tconst f = this.#file(path);\n \t\tif (!f.visIndex) {\n \t\t\tf.visIndex = new Map();\n \t\t\tfor (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);\n \t\t}\n-\t\treturn f.visIndex.get(this.graph.lineN(line)) ?? -1;\n+\t\treturn f.visIndex.get(n) ?? -1;\n \t}\n \n+\t/** The seq of the atom that wrote each visible line, in order: lines() without ids or text. */\n+\tvisibleWriters(path: string): number[] {\n+\t\tconst graph = this.graph;\n+\t\treturn Array.from(this.#file(path).vis, (n) => graph.writerSeqOf(n));\n+\t}\n+\n \t/** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */\n \tfiles(): Record<string, string> {\n \t\tconst out: Record<string, string> = {};"},{"path":"worker/project/collisions.ts","added":72,"removed":2,"diff":"--- a/worker/project/collisions.ts\n+++ b/worker/project/collisions.ts\n@@ -9,8 +9,10 @@\n \tconflictKeys,\n \ttype Graph,\n \tjoinLines,\n+\ttype Key,\n \tparseKey,\n \ttype ReadView,\n+\tseqOf,\n } from \"../../src/engine/index.ts\";\n import { differenceSet, StableOverlaySet } from \"../../src/engine/state-source.ts\";\n import { definedTwice } from \"./definitions.ts\";\n@@ -44,7 +46,20 @@\n \tagainst: ReadonlySet<AtomId>,\n \tsettled: ReadonlySet<AtomId>,\n ): Pair[] {\n-\tconst out: Pair[] = [];\n+\treturn pairsFrom(g, claimantsIn(g, draft, against), against, settled);\n+}\n+\n+/**\n+ * Each draft change's claimants in `against`, in seq order: the atoms whose claims meet its keys, before the\n+ * engine's rule is applied. A stored atom's keys are read from disk, so a caller checking one draft against many\n+ * (draft-collisions.ts) gathers once against all of them and filters with pairsFrom.\n+ */\n+export function claimantsIn(\n+\tg: Graph,\n+\tdraft: readonly AtomId[],\n+\tagainst: ReadonlySet<AtomId>,\n+): ReadonlyMap<AtomId, readonly AtomId[]> {\n+\tconst out = new Map<AtomId, AtomId[]>();\n \tfor (const yours of draft) {\n \t\tconst atom = g.atom(yours);\n \t\tconst streams: IterableIterator<AtomId>[] = [];\n@@ -52,7 +67,62 @@\n \t\t\tstreams.push(g.eachWriterOf(key), g.eachReaderOf(key), g.eachHolderOf(key));\n \t\t}\n \t\tfor (const key of claimsOf(atom)) streams.push(g.eachWriterOf(key));\n-\t\tfor (const theirs of orderedClaimants(g, streams)) {\n+\t\tconst theirs: AtomId[] = [];\n+\t\tfor (const id of orderedClaimants(g, streams)) if (against.has(id)) theirs.push(id);\n+\t\tout.set(yours, theirs);\n+\t}\n+\treturn out;\n+}\n+\n+/**\n+ * claimantsIn for a few atoms against each other: each atom's claimants among `atoms`, in seq order, from their own\n+ * keys held in memory. Walking each key's history of claimants read four lists from disk per key, and a draft deleting\n+ * a big file has thousands of keys (abso Capacity).\n+ */\n+export function claimantsAmong(g: Graph, atoms: readonly AtomId[]): ReadonlyMap<AtomId, readonly AtomId[]> {\n+\tconst claimers = new Map<Key, AtomId[]>();\n+\tconst writers = new Map<Key, AtomId[]>();\n+\tconst keys = new Map<AtomId, { writes: readonly Key[]; claims: readonly Key[] }>();\n+\tconst add = (index: Map<Key, AtomId[]>, key: Key, id: AtomId) => {\n+\t\tconst list = index.get(key);\n+\t\tif (list) list.push(id);\n+\t\telse index.set(key, [id]);\n+\t};\n+\tfor (const id of atoms) {\n+\t\tconst atom = g.atom(id);\n+\t\tconst writes = [...atom.writes];\n+\t\t// claimsOf, with the writes already read.\n+\t\tconst claims = [...new Set([...writes, ...atom.reads, ...atom.holds])].sort();\n+\t\tkeys.set(id, { writes, claims });\n+\t\tfor (const key of writes) add(writers, key, id);\n+\t\tfor (const key of claims) add(claimers, key, id);\n+\t}\n+\tconst out = new Map<AtomId, AtomId[]>();\n+\tfor (const id of atoms) {\n+\t\tconst own = keys.get(id)!;\n+\t\tconst found = new Set<AtomId>();\n+\t\t// As claimantsIn's streams: what claims a key it writes, and what writes a key it claims.\n+\t\tfor (const key of own.writes) for (const other of claimers.get(key) ?? []) found.add(other);\n+\t\tfor (const key of own.claims) for (const other of writers.get(key) ?? []) found.add(other);\n+\t\tfound.delete(id);\n+\t\tout.set(\n+\t\t\tid,\n+\t\t\t[...found].sort((a, b) => seqOf(a) - seqOf(b)),\n+\t\t);\n+\t}\n+\treturn out;\n+}\n+\n+/** The claimants in `against` that collide by the engine's rule and that no atom of `settled` resolves. */\n+export function pairsFrom(\n+\tg: Graph,\n+\tclaimants: ReadonlyMap<AtomId, readonly AtomId[]>,\n+\tagainst: ReadonlySet<AtomId>,\n+\tsettled: ReadonlySet<AtomId>,\n+): Pair[] {\n+\tconst out: Pair[] = [];\n+\tfor (const [yours, theirsList] of claimants) {\n+\t\tfor (const theirs of theirsList) {\n \t\t\tif (!against.has(theirs)) continue;\n \t\t\tconst [lo, hi] = g.atom(yours).seq < g.atom(theirs).seq ? [yours, theirs] : [theirs, yours];\n \t\t\tconst keys = conflictKeys(g.atom(lo), g.atom(hi), g.dependsOn(hi, lo));"},{"path":"worker/project/definition-rule.ts","added":58,"removed":28,"diff":"--- a/worker/project/definition-rule.ts\n+++ b/worker/project/definition-rule.ts\n@@ -36,10 +36,23 @@\n \treadonly parent: string;\n }\n \n+/** Definitions in the files newer work touched, before it and with it. */\n+interface Changed {\n+\treadonly baseDefs: ReturnType<typeof definitionsIn>;\n+\treadonly nowDefs: ReturnType<typeof definitionsIn>;\n+}\n+\n+/** Names a draft's lines use that they don't declare, with where. */\n+type Uses = ReadonlyMap<string, readonly { file: string; index: number }[]>;\n+\n export class DefinitionRule {\n \treadonly #graph: Graph;\n \treadonly #origin: (atom: AtomId, spaceName: string) => Origin;\n \treadonly #replacedCache = new Map<string, ReadonlySet<string>>();\n+\t/** A draft's uses, kept while it is asked about with the same views: draft collisions ask once per other agent. */\n+\treadonly #usesCache = new WeakMap<ReadonlySet<AtomId>, { view: ReadView; base: ReadView; uses: Uses }>();\n+\t/** The newer work's definitions before and after, kept while it is asked about with the same views. */\n+\treadonly #changedCache = new WeakMap<ReadonlySet<AtomId>, { base: ReadView; now: ReadView; defs: Changed }>();\n \n \tconstructor(graph: Graph, origin: (atom: AtomId, spaceName: string) => Origin) {\n \t\tthis.#graph = graph;\n@@ -93,47 +106,35 @@\n \t * draft declares itself is its own. A changed definition is any difference\n \t * in the text of the name's definitions (see definitions.ts), including one\n \t * that disappeared; a use that imports the name, or sits in a file that\n-\t * defines it, only counts that definition.\n+\t * defines it, only counts that definition. `nowView` may be a function, called only when the draft uses a name.\n \t */\n \tcollisions(\n \t\tdraft: ReadonlySet<AtomId>,\n \t\tview: ReadView,\n \t\tbaseView: ReadView,\n-\t\tnowView: ReadView,\n+\t\tnowView: ReadView | (() => ReadView),\n \t\tnewer: ReadonlySet<AtomId>,\n \t\tspaceName: string,\n \t): DefinitionCollision[] {\n \t\tif (draft.size === 0 || newer.size === 0) return [];\n-\t\tconst uses = new Map<string, { file: string; index: number }[]>();\n-\t\tconst declared = new Set<string>();\n-\t\tconst runsOf = new Map<AtomId, readonly RunEnd[]>();\n-\t\t// Only files the draft wrote lines in can hold its lines.\n-\t\tconst draftFiles = touchedFiles(this.#graph, draft, [view]);\n-\t\tfor (const file of [...draftFiles].sort()) {\n-\t\t\tif (holdsNoCode(file) || !view.hasFile(file)) continue;\n-\t\t\tconst visible = view.lines(file);\n-\t\t\tif (!visible.some((l) => draft.has(l.atom))) continue;\n-\t\t\tconst code = codeOfLines(visible.map((l) => l.text));\n-\t\t\tvisible.forEach((line, i) => {\n-\t\t\t\tif (!draft.has(line.atom)) return;\n-\t\t\t\tfor (const name of declaredIn(code[i]!)) declared.add(name);\n-\t\t\t\t// Only names the change added: a use already on the lines it replaced is not the draft's to answer for.\n-\t\t\t\tconst before = this.#replacedNames(line.id, baseView, runsOf);\n-\t\t\t\tfor (const name of usedNames(code[i]!)) {\n-\t\t\t\t\tif (before.has(name)) continue;\n-\t\t\t\t\tconst list = uses.get(name);\n-\t\t\t\t\tif (list) list.push({ file, index: i });\n-\t\t\t\t\telse uses.set(name, [{ file, index: i }]);\n-\t\t\t\t}\n-\t\t\t});\n+\t\tlet kept = this.#usesCache.get(draft);\n+\t\tif (!kept || kept.view !== view || kept.base !== baseView) {\n+\t\t\tkept = { view, base: baseView, uses: this.#usesOf(draft, view, baseView) };\n+\t\t\tthis.#usesCache.set(draft, kept);\n \t\t}\n-\t\tfor (const name of declared) uses.delete(name);\n+\t\tconst uses = kept.uses;\n \t\tif (uses.size === 0) return [];\n+\t\tconst now = typeof nowView === \"function\" ? nowView() : nowView;\n \n \t\t// A definition can only have changed in a file the newer work touched.\n-\t\tconst touched = touchedFiles(this.#graph, newer, [baseView, nowView]);\n-\t\tconst baseDefs = definitionsIn(baseView, touched);\n-\t\tconst nowDefs = definitionsIn(nowView, touched);\n+\t\tlet changed = this.#changedCache.get(newer);\n+\t\tif (!changed || changed.base !== baseView || changed.now !== now) {\n+\t\t\tconst touched = touchedFiles(this.#graph, newer, [baseView, now]);\n+\t\t\tconst defs = { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) };\n+\t\t\tchanged = { base: baseView, now, defs };\n+\t\t\tthis.#changedCache.set(newer, changed);\n+\t\t}\n+\t\tconst { baseDefs, nowDefs } = changed.defs;\n \t\tconst out: DefinitionCollision[] = [];\n \t\tfor (const [name, where] of [...uses].sort(([a], [b]) => (a < b ? -1 : 1))) {\n \t\t\t// Which definitions the uses mean: the file each use imports the name\n@@ -189,4 +190,33 @@\n \t\t}\n \t\treturn out;\n \t}\n+\n+\t/** The names the draft's lines use and don't declare, less those the lines they replaced already used. */\n+\t#usesOf(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses {\n+\t\tconst uses = new Map<string, { file: string; index: number }[]>();\n+\t\tconst declared = new Set<string>();\n+\t\tconst runsOf = new Map<AtomId, readonly RunEnd[]>();\n+\t\t// Only files the draft wrote lines in can hold its lines.\n+\t\tconst draftFiles = touchedFiles(this.#graph, draft, [view]);\n+\t\tfor (const file of [...draftFiles].sort()) {\n+\t\t\tif (holdsNoCode(file) || !view.hasFile(file)) continue;\n+\t\t\tconst visible = view.lines(file);\n+\t\t\tif (!visible.some((l) => draft.has(l.atom))) continue;\n+\t\t\tconst code = codeOfLines(visible.map((l) => l.text));\n+\t\t\tvisible.forEach((line, i) => {\n+\t\t\t\tif (!draft.has(line.atom)) return;\n+\t\t\t\tfor (const name of declaredIn(code[i]!)) declared.add(name);\n+\t\t\t\t// Only names the change added: a use already on the lines it replaced is not the draft's to answer for.\n+\t\t\t\tconst before = this.#replacedNames(line.id, baseView, runsOf);\n+\t\t\t\tfor (const name of usedNames(code[i]!)) {\n+\t\t\t\t\tif (before.has(name)) continue;\n+\t\t\t\t\tconst list = uses.get(name);\n+\t\t\t\t\tif (list) list.push({ file, index: i });\n+\t\t\t\t\telse uses.set(name, [{ file, index: i }]);\n+\t\t\t\t}\n+\t\t\t});\n+\t\t}\n+\t\tfor (const name of declared) uses.delete(name);\n+\t\treturn uses;\n+\t}\n }"},{"path":"worker/project/draft-collisions.ts","added":43,"removed":9,"diff":"--- a/worker/project/draft-collisions.ts\n+++ b/worker/project/draft-collisions.ts\n@@ -2,7 +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 { describeSide, fileOfKey, type Pair } from \"./collisions.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 { pathKey } from \"./pathkey.ts\";\n@@ -25,7 +25,6 @@\n \tdraft(agent: Agent): AtomId[];\n \tagentView(agent: Agent): ReadView;\n \tview(set: ReadonlySet<AtomId>): ReadView;\n-\tpairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];\n \ttouchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;\n }\n \n@@ -54,6 +53,38 @@\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, 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};\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 +92,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 newer = draftSet(a);\n \t\t\tconst union = new Set([...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,11 +127,14 @@\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\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\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@@ -121,8 +155,8 @@\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\tfor (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED)) {\n \t\t\t\tconst c = d.collision;\n \t\t\t\tout.push({\n \t\t\t\t\tkind: \"definition\","},{"path":"worker/project/overlaps.ts","added":4,"removed":1,"diff":"--- a/worker/project/overlaps.ts\n+++ b/worker/project/overlaps.ts\n@@ -70,10 +70,13 @@\n \t// No draft, no view to build: most agents in a big project have nothing unpublished.\n \tif (draft.length === 0) return files;\n \tconst view = host.agentView(agent);\n+\t// By number where the view can (abso Capacity): a draft deleting a big generated file looked up each line's id.\n+\tconst byNumber = (view as { fileOfNumber?(n: number): string }).fileOfNumber?.bind(view);\n \tfor (const id of draft) {\n \t\tconst atom = host.graph.atom(id);\n \t\tfor (const run of atom.inserts) files.add(isStartNode(run.parent) ? run.file : view.fileOf(run.parent));\n-\t\tfor (const line of atom.deletes) files.add(view.fileOf(line));\n+\t\tif (byNumber) for (const n of host.graph.deletedNumbers(id)) files.add(byNumber(n));\n+\t\telse for (const line of atom.deletes) files.add(view.fileOf(line));\n \t\tfor (const m of atom.moves ?? []) files.add(view.fileOf(m.line));\n \t}\n \treturn files;"},{"path":"worker/project/project.ts","added":7,"removed":2,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -1874,7 +1874,6 @@\n \t\t\tdraft: (agent) => this.#draft(agent as AgentState),\n \t\t\tagentView: (agent) => this.#agentView(agent as AgentState),\n \t\t\tview: (set) => this.#view(set),\n-\t\t\tpairs: (draft, against, settled) => this.#pairs(draft, against, settled),\n \t\t\ttouchedFiles: (ids, views) => this.#touchedFiles(ids, views),\n \t\t};\n \t\treturn this.#draftCollisionHost;\n@@ -2719,12 +2718,18 @@\n \t}\n \n \t#agentView(agent: AgentState): ReadView {\n-\t\treturn this.#viewKeyed(this.#agentViewKey(agent), () => {\n+\t\tconst view = this.#viewKeyed(this.#agentViewKey(agent), () => {\n \t\t\tconst base = this.#at(this.#spaceOf(agent), agent.base);\n \t\t\treturn base instanceof StableSet\n \t\t\t\t? new StableUnionSet(base, new StableOverlaySet(agent.mine))\n \t\t\t\t: new Set([...base, ...agent.mine]);\n \t\t});\n+\t\t// A draft that moves nothing resolves its moves as its base does (abso Capacity): take them from the base's view.\n+\t\tif (this.#writerStorage && agent.mine.length > 0 && view instanceof View) {\n+\t\t\tconst base = this.#viewAt(this.#spaceOf(agent), agent.base);\n+\t\t\tif (base instanceof View) view.shareMoves(base);\n+\t\t}\n+\t\treturn view;\n \t}\n \n \t/** An agent's view, keyed by what the set is made of, not by sorting every atom in it (#46): that cost more than the work. */"},{"path":"worker/project/test/draft-reads-stored.test.ts","added":54,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/draft-reads-stored.test.ts\n@@ -0,0 +1,54 @@\n+// Reads of agents' drafts on a writer-backed project (abso Capacity): where and draft collisions find files and\n+// lines by line number, take moves from the draft's base, and gather each draft's claimants once among all the\n+// drafts. They must answer as the in-memory project does: drafts that edit, delete a whole file, collide on a line,\n+// define the same name, and sit on bases before and after a move.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { storedArray } from \"../../../src/engine/state-source.ts\";\n+import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n+import { MemoryStore, Project } from \"../project.ts\";\n+import type { Entry, ProjectStore } from \"../project-types.ts\";\n+import { where } from \"../where.ts\";\n+\n+function writer() {\n+\tconst storage = new SqlState();\n+\tconst entries = storedArray<Entry>(storage, \"canonical-log\");\n+\tconst store: ProjectStore = {\n+\t\tappend(batch) {\n+\t\t\tfor (const entry of batch) entries.push(entry);\n+\t\t},\n+\t};\n+\treturn new Project(store, { writerStorage: storage });\n+}\n+\n+const big = Array.from({ length: 300 }, (_, i) => `generated ${i}\\n`).join(\"\");\n+\n+function history(project: Project) {\n+\tlet t = 0;\n+\tproject.init({ \"a.ts\": \"one\\ntwo\\nthree\\nfour\\n\", \"big.txt\": big, \"c.ts\": \"const x = 1;\\n\" }, t);\n+\t// One agent's base is before a move, the others' after it.\n+\tproject.sync(\"old\");\n+\tproject.record(\"old\", { \"a.ts\": \"one\\ntwo\\nthree\\nfour\\nfive\\n\" }, ++t);\n+\tproject.move(\"mover\", \"c.ts\", \"d.ts\", ++t);\n+\tassert.equal(project.publish(\"mover\", \"Move c to d\", ++t).ok, true);\n+\tproject.record(\"ann\", { \"a.ts\": \"one\\nTWO\\nthree\\nfour\\n\", \"d.ts\": \"const x = 2;\\n\" }, ++t);\n+\tproject.record(\"bob\", { \"a.ts\": \"one\\nTwo\\nthree\\nfour\\n\", \"big.txt\": null }, ++t);\n+\tproject.record(\"cy\", { \"e.ts\": \"export function f() {}\\n\" }, ++t);\n+\tproject.record(\"dee\", { \"e.ts\": \"export function f() { return 1; }\\n\" }, ++t);\n+\tproject.sync(\"idle\");\n+}\n+\n+test(\"where and draft collisions on a stored writer match the in-memory project's\", () => {\n+\tconst oracle = new Project(new MemoryStore());\n+\tconst project = writer();\n+\thistory(oracle);\n+\thistory(project);\n+\tconst expected = where(oracle);\n+\tassert.ok(expected.files[\"big.txt\"], \"bob's deleted file is listed\");\n+\tassert.deepEqual(where(project), expected);\n+\tconst collisions = oracle.collisions();\n+\tassert.ok(collisions.collisions.length > 0, \"ann and bob collide on a.ts\");\n+\tassert.deepEqual(project.collisions(), collisions);\n+\tassert.deepEqual(project.collisions({ file: \"a.ts\" }), oracle.collisions({ file: \"a.ts\" }));\n+});"},{"path":"worker/project/where.ts","added":48,"removed":11,"diff":"--- a/worker/project/where.ts\n+++ b/worker/project/where.ts\n@@ -3,11 +3,18 @@\n // two ways: in the agent's own view (what its folder shows), and in shared's\n // numbering (the shared lines it changes, or the shared line its new lines\n // follow), so agents can compare where they are against one common text.\n-import type { AtomId, LineId } from \"../../src/engine/index.ts\";\n+import { type AtomId, type LineId, type ReadView, seqOf } from \"../../src/engine/index.ts\";\n import type { Project } from \"./project.ts\";\n \n type Range = [number, number];\n \n+/** A view's reads by line number (View has them; another ReadView may not). */\n+type ByNumber = ReadView & {\n+\tfileOfNumber?(n: number): string;\n+\tlineNumberOf?(path: string, n: number): number;\n+\tvisibleWriters?(path: string): readonly number[];\n+};\n+\n function ranges(ns: number[]): Range[] {\n \tconst out: Range[] = [];\n \tfor (const n of [...new Set(ns)].sort((a, b) => a - b)) {\n@@ -30,11 +37,18 @@\n \n export function where(project: Project) {\n \tconst graph = project.graph;\n-\tlet shared: ReturnType<Project[\"sharedView\"]> | undefined;\n-\tconst sharedNumber = (file: string, id: LineId): number | undefined => {\n-\t\tshared ??= project.sharedView();\n+\tlet shared: ByNumber | undefined;\n+\t// By the file's index of visible lines (abso Capacity): finding it in lines() built every line of the file again\n+\t// whenever the file changed, per line asked.\n+\tconst sharedNumber = (file: string, line: LineId | number): number | undefined => {\n+\t\tshared ??= project.sharedView() as ByNumber;\n \t\tif (!shared.hasFile(file)) return undefined;\n-\t\tconst i = shared.lines(file).findIndex((l) => l.id === id);\n+\t\tconst i =\n+\t\t\ttypeof line === \"string\"\n+\t\t\t\t? shared.lineNumber(file, line)\n+\t\t\t\t: shared.lineNumberOf\n+\t\t\t\t\t? shared.lineNumberOf(file, line)\n+\t\t\t\t\t: shared.lineNumber(file, graph.lineIdOf(line));\n \t\treturn i >= 0 ? i + 1 : undefined;\n \t};\n \tconst files: Record<string, Spot[]> = {};\n@@ -42,6 +56,10 @@\n \t\tconst draft = project.draftOf(agent);\n \t\tif (!draft || draft.atoms.length === 0) continue;\n \t\tconst mine = new Set<AtomId>(draft.atoms);\n+\t\t// Once per agent (abso Capacity): each read of draft.view asks the project's view cache, which weighs every\n+\t\t// view it holds.\n+\t\tconst view = draft.view as ByNumber;\n+\t\tconst fileOfNumber = view.fileOfNumber?.bind(view);\n \t\tconst perFile = new Map<string, { view: number[]; shared: number[]; changes: Set<AtomId>; last: number }>();\n \t\tconst at = (file: string) => {\n \t\t\tlet s = perFile.get(file);\n@@ -52,7 +70,7 @@\n \t\t\tconst atom = graph.atom(id);\n \t\t\tfor (const run of atom.inserts) {\n \t\t\t\t// A line moved by someone (#28) is in the file the draft's view puts it in.\n-\t\t\t\tconst file = run.parent.startsWith(\"^\") ? run.file : draft.view.fileOf(run.parent);\n+\t\t\t\tconst file = run.parent.startsWith(\"^\") ? run.file : view.fileOf(run.parent);\n \t\t\t\tconst s = at(file);\n \t\t\t\ts.changes.add(id);\n \t\t\t\ts.last = Math.max(s.last, atom.time);\n@@ -60,10 +78,23 @@\n \t\t\t\tconst anchor = run.parent.startsWith(\"^\") ? 0 : sharedNumber(file, run.parent);\n \t\t\t\tif (anchor !== undefined) s.shared.push(Math.max(anchor, 1));\n \t\t\t}\n+\t\t\t// By number where the view can (abso Capacity): a draft deleting a big generated file read every line's text\n+\t\t\t// to check it exists, and looked up each line's id.\n+\t\t\tif (fileOfNumber) {\n+\t\t\t\tfor (const line of graph.deletedNumbers(id)) {\n+\t\t\t\t\tconst file = fileOfNumber(line);\n+\t\t\t\t\tconst s = at(file);\n+\t\t\t\t\ts.changes.add(id);\n+\t\t\t\t\ts.last = Math.max(s.last, atom.time);\n+\t\t\t\t\tconst n = sharedNumber(file, line);\n+\t\t\t\t\tif (n !== undefined) s.shared.push(n);\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tfor (const lineId of atom.deletes) {\n \t\t\t\tconst info = graph.line(lineId);\n \t\t\t\tif (!info) continue;\n-\t\t\t\tconst file = draft.view.fileOf(lineId);\n+\t\t\t\tconst file = view.fileOf(lineId);\n \t\t\t\tconst s = at(file);\n \t\t\t\ts.changes.add(id);\n \t\t\t\ts.last = Math.max(s.last, atom.time);\n@@ -72,10 +103,16 @@\n \t\t\t}\n \t\t}\n \t\tfor (const [file, s] of perFile) {\n-\t\t\tif (draft.view.hasFile(file)) {\n-\t\t\t\tdraft.view.lines(file).forEach((l, i) => {\n-\t\t\t\t\tif (mine.has(l.atom)) s.view.push(i + 1);\n-\t\t\t\t});\n+\t\t\tif (view.hasFile(file)) {\n+\t\t\t\tif (view.visibleWriters) {\n+\t\t\t\t\tconst seqs = new Set(draft.atoms.map(seqOf));\n+\t\t\t\t\tview.visibleWriters(file).forEach((seq, i) => {\n+\t\t\t\t\t\tif (seqs.has(seq)) s.view.push(i + 1);\n+\t\t\t\t\t});\n+\t\t\t\t} else\n+\t\t\t\t\tview.lines(file).forEach((l, i) => {\n+\t\t\t\t\t\tif (mine.has(l.atom)) s.view.push(i + 1);\n+\t\t\t\t\t});\n \t\t\t}\n \t\t\t(files[file] ??= []).push({\n \t\t\t\tagent,"}],"ci":null}