{"version":314,"agent":"capacity","message":"Definition rule: lay out only the touched files the draft's uses reach (abso 738). The rule found changed definitions by laying out every file the newer work touched, in two views; for codex-prose-authorship-610 that was 494 files a view where its uses reach 7. It now works out which files the uses mean first and lays out only the touched ones among them. Same answers: the rule already ignored untouched files. Draft collisions keep each file's definitions, and the touched files, under the newer work's key.","time":1792223392055,"timeIso":"2026-10-17T07:49:52.055Z","changes":2,"change":5514,"files":["worker/project/definition-rule.ts","worker/project/kept-parts.ts"],"draft":null,"diff":[{"path":"worker/project/definition-rule.ts","added":61,"removed":35,"diff":"--- a/worker/project/definition-rule.ts\n+++ b/worker/project/definition-rule.ts\n@@ -7,7 +7,7 @@\n import { type AtomId, type Graph, joinLines, type LineId, type ReadView } from \"../../src/engine/index.ts\";\n import { type Origin, touchedFiles } from \"./collisions.ts\";\n import { type Definition, definitionsIn, importsOf, usedNames } from \"./definitions.ts\";\n-import { definitionsBytes, type KeptParts, usesBytes } from \"./kept-parts.ts\";\n+import { definitionsBytes, filesBytes, type KeptParts, usesBytes } from \"./kept-parts.ts\";\n import { codeOfLines, declaredIn, holdsNoCode } from \"./names.ts\";\n import { definitionText } from \"./news.ts\";\n import type { Collision } from \"./project.ts\";\n@@ -37,7 +37,7 @@\n \treadonly parent: string;\n }\n \n-/** Definitions in the files newer work touched, before it and with it. */\n+/** Definitions in one file newer work touched, before it and with it. */\n interface Changed {\n \treadonly baseDefs: ReturnType<typeof definitionsIn>;\n \treadonly nowDefs: ReturnType<typeof definitionsIn>;\n@@ -52,8 +52,11 @@\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+\t/** The files the newer work touched, kept while it is asked about with the same views. */\n+\treadonly #touchedCache = new WeakMap<\n+\t\tReadonlySet<AtomId>,\n+\t\t{ base: ReadView; now: ReadView; files: ReadonlySet<string> }\n+\t>();\n \n \tconstructor(graph: Graph, origin: (atom: AtomId, spaceName: string) => Origin) {\n \t\tthis.#graph = graph;\n@@ -131,31 +134,54 @@\n \t\t// Only these names can collide (a publish's news, news.ts): a draft that uses none of them has none, and the\n \t\t// definitions changed since its base needn't be worked out.\n \t\tif (names && ![...uses.keys()].some((name) => names.has(name))) return [];\n-\t\tconst nowOf = () => (typeof nowView === \"function\" ? nowView() : nowView);\n-\t\tconst { baseDefs, nowDefs } = kept\n-\t\t\t? kept.parts.get(\n-\t\t\t\t\t`changed ${kept.changed}`,\n-\t\t\t\t\t() => this.#changed(newer, baseView, nowOf()),\n-\t\t\t\t\t(c) => definitionsBytes(c.baseDefs) + definitionsBytes(c.nowDefs),\n-\t\t\t\t)\n-\t\t\t: this.#changedFor(newer, baseView, nowOf());\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-\t\t\t// from, or its own file when it defines the name at the top level. A\n-\t\t\t// use with neither (a local, a parameter, a global, a name another\n-\t\t\t// language brings in some other way) means none: the rule only speaks\n-\t\t\t// when it knows which definition a use means.\n+\t\t// Which definitions the uses mean: the file each use imports the name\n+\t\t// from, or its own file when it defines the name at the top level. A\n+\t\t// use with neither (a local, a parameter, a global, a name another\n+\t\t// language brings in some other way) means none: the rule only speaks\n+\t\t// when it knows which definition a use means.\n+\t\tconst scopes = new Map<string, ReadonlySet<string>>();\n+\t\tfor (const [name, where] of uses) {\n \t\t\tconst scope = new Set<string>();\n \t\t\tfor (const w of new Set(where.map((x) => x.file))) {\n \t\t\t\tconst from = importsOf(view, w).get(name) ?? (definitionsIn(view, [w]).has(name) ? w : undefined);\n \t\t\t\tif (from !== undefined) scope.add(from);\n \t\t\t}\n-\t\t\tif (scope.size === 0) continue;\n-\t\t\tconst inScope = (d: Definition): boolean => scope.has(d.file);\n-\t\t\tconst before = baseDefs.get(name)?.filter(inScope);\n-\t\t\tif (!before?.length) continue; // not defined in what the drafter saw: nothing it relies on\n-\t\t\tconst after = (nowDefs.get(name) ?? []).filter(inScope);\n+\t\t\tif (scope.size > 0) scopes.set(name, scope);\n+\t\t}\n+\t\tif (scopes.size === 0) return [];\n+\t\tconst nowOf = () => (typeof nowView === \"function\" ? nowView() : nowView);\n+\t\t// Only a file the newer work touched can hold a changed definition, and only one a use means can matter: just those\n+\t\t// files are laid out, before and with the newer work (abso 738). Laying out every file the newer work touched cost\n+\t\t// codex-prose-authorship-610's draft-state 494 files a view where its uses reach 7.\n+\t\tconst touched = kept\n+\t\t\t? kept.parts.get(`touched ${kept.changed}`, () => this.#touched(newer, baseView, nowOf()), filesBytes)\n+\t\t\t: this.#touchedFor(newer, baseView, nowOf());\n+\t\tconst changed = new Map<string, Changed>();\n+\t\tconst changedIn = (file: string): Changed => {\n+\t\t\tlet c = changed.get(file);\n+\t\t\tif (!c) {\n+\t\t\t\tconst make = (): Changed => ({\n+\t\t\t\t\tbaseDefs: definitionsIn(baseView, [file]),\n+\t\t\t\t\tnowDefs: definitionsIn(nowOf(), [file]),\n+\t\t\t\t});\n+\t\t\t\tc = kept\n+\t\t\t\t\t? kept.parts.get(\n+\t\t\t\t\t\t\t`changed ${kept.changed} ${file}`,\n+\t\t\t\t\t\t\tmake,\n+\t\t\t\t\t\t\t(d) => definitionsBytes(d.baseDefs) + definitionsBytes(d.nowDefs),\n+\t\t\t\t\t\t)\n+\t\t\t\t\t: make();\n+\t\t\t\tchanged.set(file, c);\n+\t\t\t}\n+\t\t\treturn c;\n+\t\t};\n+\t\tconst out: DefinitionCollision[] = [];\n+\t\tfor (const [name, scope] of [...scopes].sort(([a], [b]) => (a < b ? -1 : 1))) {\n+\t\t\t// In the order the newer work touched its files, as when every touched file was laid out.\n+\t\t\tconst files = [...touched].filter((f) => scope.has(f));\n+\t\t\tconst before = files.flatMap((f) => changedIn(f).baseDefs.get(name) ?? []);\n+\t\t\tif (!before.length) continue; // not defined in what the drafter saw: nothing it relies on\n+\t\t\tconst after = files.flatMap((f) => changedIn(f).nowDefs.get(name) ?? []);\n \t\t\tif (signature(before) === signature(after)) continue;\n \t\t\t// Who changed it: newer writers of the new definition's lines, or newer deleters of the old one's.\n \t\t\tconst by = new Set<AtomId>();\n@@ -166,6 +192,7 @@\n \t\t\tconst first = [...by].sort((p, q) => this.#graph.atom(p).seq - this.#graph.atom(q).seq)[0];\n \t\t\tif (first === undefined) continue;\n \t\t\tconst origin = this.#origin(first, spaceName);\n+\t\t\tconst where = uses.get(name)!;\n \t\t\tconst file = where[0]!.file;\n \t\t\tconst indexes = where.filter((w) => w.file === file).map((w) => w.index);\n \t\t\tconst lo = Math.min(...indexes);\n@@ -205,20 +232,19 @@\n \t\treturn kept.uses;\n \t}\n \n-\t/** The newer work's definitions before and after, kept while it is asked about with the same views. */\n-\t#changedFor(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed {\n-\t\tlet changed = this.#changedCache.get(newer);\n-\t\tif (!changed || changed.base !== baseView || changed.now !== now) {\n-\t\t\tchanged = { base: baseView, now, defs: this.#changed(newer, baseView, now) };\n-\t\t\tthis.#changedCache.set(newer, changed);\n+\t/** The files the newer work touched, kept while it is asked about with the same views. */\n+\t#touchedFor(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): ReadonlySet<string> {\n+\t\tlet kept = this.#touchedCache.get(newer);\n+\t\tif (!kept || kept.base !== baseView || kept.now !== now) {\n+\t\t\tkept = { base: baseView, now, files: this.#touched(newer, baseView, now) };\n+\t\t\tthis.#touchedCache.set(newer, kept);\n \t\t}\n-\t\treturn changed.defs;\n+\t\treturn kept.files;\n \t}\n \n-\t/** Definitions in the files the newer work touched, before it and with it: only there can one have changed. */\n-\t#changed(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed {\n-\t\tconst touched = touchedFiles(this.#graph, newer, [baseView, now]);\n-\t\treturn { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) };\n+\t/** The files the newer work touched, before it and with it: only there can a definition have changed. */\n+\t#touched(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): ReadonlySet<string> {\n+\t\treturn touchedFiles(this.#graph, newer, [baseView, now]);\n \t}\n \n \t/** The names the draft's lines use and don't declare, less those the lines they replaced already used. */"},{"path":"worker/project/kept-parts.ts","added":11,"removed":4,"diff":"--- a/worker/project/kept-parts.ts\n+++ b/worker/project/kept-parts.ts\n@@ -1,8 +1,8 @@\n // What draft collisions work out about each draft and each pair of drafts, kept between calls under a hard byte budget\n-// (abso Capacity, issue 664): the names a draft's new lines use, the definitions a draft's changes touch before and\n-// after, and the names a pair of drafts both define in a file. Keys name everything an entry depends on (shared's\n-// version, a draft's base and atoms), and a call drops every entry it didn't use, so a draft that changes, an agent that\n-// leaves and shared moving on all drop theirs at once.\n+// (abso Capacity, issue 664): the names a draft's new lines use, the files a draft's changes touch and the definitions\n+// in each before and after, and the names a pair of drafts both define in a file. Keys name everything an entry depends\n+// on (shared's version, a draft's base and atoms), and a call drops every entry it didn't use, so a draft that changes,\n+// an agent that leaves and shared moving on all drop theirs at once.\n \n import { stringBytes } from \"../../src/engine/view.ts\";\n import type { Definition } from \"./definitions.ts\";\n@@ -100,3 +100,10 @@\n \t\tn += MAP_ENTRY_BYTES + stringBytes(name) + ARRAY_HEADER_BYTES + where.length * (REFERENCE_BYTES + OBJECT_BYTES);\n \treturn n;\n }\n+\n+/** A set of paths (the files some work touched). */\n+export function filesBytes(files: ReadonlySet<string>): number {\n+\tlet n = MAP_HEADER_BYTES;\n+\tfor (const file of files) n += MAP_ENTRY_BYTES + stringBytes(file);\n+\treturn n;\n+}"}],"ci":null}