{"version":249,"agent":"capacity","message":"Collisions step 3: each draft's definition work kept between calls, under a hard 4 MB budget (abso Capacity, issue 664).\n\nKeptParts (kept-parts.ts) holds what draft collisions work out about each draft and each pair of drafts: the names a draft's new lines use, the definitions a draft's changes touch before and after (the changed-definition rule), and the names two drafts both define in a file. Each entry is weighed as the view cache weighs views (line ids, texts, names: view.ts's stringBytes) and the whole stays under 4 MB, oldest out first; an entry over the budget isn't kept. Keys name everything an entry depends on: shared's version, the draft's base and its atoms. Each collisions call drops every entry it didn't use, so a draft that changes, an agent that leaves or shared moving on drops theirs at once. On a stored writer only; the publish check (land.ts) and the in-memory project work as before.\n\nOn the abso export (my machine): a warm collisions call takes 322 ms instead of 3.4 s, with the same answer as replay. A full warm run's held heap is at most 42.9 MB (41.7 MB without it), and all 33 answers match.\n\nTests: kept-parts (new: the budget, oldest out first, an oversized entry never kept, a round dropping what it didn't use) and collisions-kept-views, which now runs through the kept parts on the writer (a draft changing, shared moving on, a draft dropped, each asked twice against the in-memory project). With the collision, definition, path and view tests: 47 of 47.","time":1792081982267,"timeIso":"2026-10-15T16:33:02.267Z","changes":16,"change":3868,"files":["src/engine/view.ts","worker/project/definition-rule.ts","worker/project/draft-collisions.ts","worker/project/kept-parts.ts","worker/project/project.ts","worker/project/test/kept-parts.test.ts"],"draft":null,"diff":[{"path":"src/engine/view.ts","added":1,"removed":1,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -136,7 +136,7 @@\n  * What V8 holds for a string of its own (abso Capacity, measured on abso's layouts): a header, then one byte a\n  * character when every character fits in Latin-1 and two otherwise, rounded up to 8.\n  */\n-function stringBytes(text: string): number {\n+export function stringBytes(text: string): number {\n \tconst chars = /[^\\u0000-\\u00ff]/.test(text) ? 2 * text.length : text.length;\n \treturn (STRING_HEADER_BYTES + chars + 7) & ~7;\n }"},{"path":"worker/project/definition-rule.ts","added":41,"removed":17,"diff":"--- a/worker/project/definition-rule.ts\n+++ b/worker/project/definition-rule.ts\n@@ -7,6 +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 { codeOfLines, declaredIn, holdsNoCode } from \"./names.ts\";\n import { definitionText } from \"./news.ts\";\n import type { Collision } from \"./project.ts\";\n@@ -109,6 +110,8 @@\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. `nowView` may be a function, called only when the draft uses a name.\n+\t * With `kept` (draft collisions, abso Capacity), the draft's uses and the newer work's definitions are kept between\n+\t * calls under keys that name everything they depend on.\n \t */\n \tcollisions(\n \t\tdraft: ReadonlySet<AtomId>,\n@@ -117,26 +120,21 @@\n \t\tnowView: ReadView | (() => ReadView),\n \t\tnewer: ReadonlySet<AtomId>,\n \t\tspaceName: string,\n+\t\tkept?: { parts: KeptParts; uses: string; changed: string },\n \t): DefinitionCollision[] {\n \t\tif (draft.size === 0 || newer.size === 0) return [];\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\tconst uses = kept.uses;\n+\t\tconst uses = kept\n+\t\t\t? kept.parts.get(`uses ${kept.uses}`, () => this.#usesOf(draft, view, baseView), usesBytes)\n+\t\t\t: this.#usesFor(draft, view, baseView);\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\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 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@@ -193,6 +191,32 @@\n \t\treturn out;\n \t}\n \n+\t/** The draft's uses, kept while it is asked about with the same views. */\n+\t#usesFor(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses {\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\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\t}\n+\t\treturn changed.defs;\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}\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 }[]>();"},{"path":"worker/project/draft-collisions.ts","added":26,"removed":4,"diff":"--- a/worker/project/draft-collisions.ts\n+++ b/worker/project/draft-collisions.ts\n@@ -6,6 +6,7 @@\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@@ -28,6 +29,10 @@\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@@ -99,6 +104,16 @@\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@@ -154,10 +169,15 @@\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.draftsView([a, b]);\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\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@@ -177,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@@ -189,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;"},{"path":"worker/project/kept-parts.ts","added":102,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/kept-parts.ts\n@@ -0,0 +1,102 @@\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+\n+import { stringBytes } from \"../../src/engine/view.ts\";\n+import type { Definition } from \"./definitions.ts\";\n+\n+/** At most this many bytes, weighed as the view cache weighs views (view.ts). */\n+export const KEPT_PARTS_BUDGET = 4_000_000;\n+\n+const OBJECT_BYTES = 32;\n+const ARRAY_HEADER_BYTES = 24;\n+const MAP_HEADER_BYTES = 32;\n+const MAP_ENTRY_BYTES = 40;\n+const REFERENCE_BYTES = 8;\n+\n+/** Entries by key, most recently used last, held under KEPT_PARTS_BUDGET bytes. */\n+export class KeptParts {\n+\treadonly #budget: number;\n+\treadonly #entries = new Map<string, { value: unknown; bytes: number }>();\n+\t#bytes = 0;\n+\t/** The keys used since the round began, when one has. */\n+\t#used: Set<string> | undefined;\n+\n+\tconstructor(budget = KEPT_PARTS_BUDGET) {\n+\t\tthis.#budget = budget;\n+\t}\n+\n+\tget bytes(): number {\n+\t\treturn this.#bytes;\n+\t}\n+\n+\tget size(): number {\n+\t\treturn this.#entries.size;\n+\t}\n+\n+\t/** The value kept under `key`, or `make()`'s, kept when it fits: `weigh` says what it holds. */\n+\tget<T>(key: string, make: () => T, weigh: (value: T) => number): T {\n+\t\tthis.#used?.add(key);\n+\t\tconst kept = this.#entries.get(key);\n+\t\tif (kept) {\n+\t\t\tthis.#entries.delete(key);\n+\t\t\tthis.#entries.set(key, kept);\n+\t\t\treturn kept.value as T;\n+\t\t}\n+\t\tconst value = make();\n+\t\tconst bytes = MAP_ENTRY_BYTES + stringBytes(key) + weigh(value);\n+\t\tif (bytes > this.#budget) return value;\n+\t\tthis.#entries.set(key, { value, bytes });\n+\t\tthis.#bytes += bytes;\n+\t\tfor (const [old, entry] of this.#entries) {\n+\t\t\tif (this.#bytes <= this.#budget) break;\n+\t\t\tthis.#entries.delete(old);\n+\t\t\tthis.#bytes -= entry.bytes;\n+\t\t}\n+\t\treturn value;\n+\t}\n+\n+\t/** Start counting which entries are used; endRound drops the rest. */\n+\tstartRound(): void {\n+\t\tthis.#used = new Set();\n+\t}\n+\n+\t/** Drop every entry not used since startRound: what changed, left or moved on since. */\n+\tendRound(): void {\n+\t\tconst used = this.#used;\n+\t\tthis.#used = undefined;\n+\t\tif (!used) return;\n+\t\tfor (const [key, entry] of this.#entries) {\n+\t\t\tif (used.has(key)) continue;\n+\t\t\tthis.#entries.delete(key);\n+\t\t\tthis.#bytes -= entry.bytes;\n+\t\t}\n+\t}\n+}\n+\n+/** A definition's bytes: its record, name, text, and its lines' ids (its file's path is shared with the view's). */\n+export function definitionBytes(d: Definition): number {\n+\tlet n = OBJECT_BYTES + REFERENCE_BYTES + stringBytes(d.name) + stringBytes(d.text) + ARRAY_HEADER_BYTES;\n+\tfor (const line of d.lines) n += REFERENCE_BYTES + stringBytes(line);\n+\treturn n;\n+}\n+\n+/** Definitions by name. */\n+export function definitionsBytes(defs: ReadonlyMap<string, readonly Definition[]>): number {\n+\tlet n = MAP_HEADER_BYTES;\n+\tfor (const [name, list] of defs) {\n+\t\tn += MAP_ENTRY_BYTES + stringBytes(name) + ARRAY_HEADER_BYTES;\n+\t\tfor (const d of list) n += REFERENCE_BYTES + definitionBytes(d);\n+\t}\n+\treturn n;\n+}\n+\n+/** Names with where they're used: a file and a line index for each. */\n+export function usesBytes(uses: ReadonlyMap<string, readonly { file: string; index: number }[]>): number {\n+\tlet n = MAP_HEADER_BYTES;\n+\tfor (const [name, where] of uses)\n+\t\tn += MAP_ENTRY_BYTES + stringBytes(name) + ARRAY_HEADER_BYTES + where.length * (REFERENCE_BYTES + OBJECT_BYTES);\n+\treturn n;\n+}"},{"path":"worker/project/project.ts","added":9,"removed":0,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -76,6 +76,7 @@\n } from \"./history.ts\";\n import { ingest as hostedIngest, type IngestHost } from \"./ingest.ts\";\n import { init as hostedInit, type InitHost } from \"./init.ts\";\n+import { KeptParts } from \"./kept-parts.ts\";\n import { label as hostedLabel, type LabelHost } from \"./label.ts\";\n import {\n \tland as hostedLand,\n@@ -276,6 +277,8 @@\n \treadonly #graph: Graph;\n \t/** Views laid out from atom sets, cached under a byte budget (views.ts). */\n \treadonly #views: ViewCache;\n+\t/** What draft collisions keep between calls, under its own byte budget (kept-parts.ts). */\n+\t#keptParts: KeptParts | undefined;\n \t/** Each agent's draft files, under the key they were worked out for (#draftFiles). */\n \treadonly #draftFilesKept = new Map<string, { key: string; files: Set<string> }>();\n \t/** Each version's files and each path's history (pathindex.ts). */\n@@ -1902,6 +1905,12 @@\n \t\t\tagentView: (agent) => this.#agentView(agent as AgentState),\n \t\t\tdraftsView: (agents) => this.#draftsView(agents as readonly AgentState[]),\n \t\t\ttouchedFiles: (ids, views) => this.#touchedFiles(ids, views),\n+\t\t\t// On a stored writer only, as a reader kept between calls is (abso Capacity).\n+\t\t\tkeptParts: () => (this.#writerStorage ? (this.#keptParts ??= new KeptParts()) : undefined),\n+\t\t\tdraftKey: (agent) => {\n+\t\t\t\tconst a = agent as AgentState;\n+\t\t\t\treturn `${last(this.#shared)} ${a.space}@${a.base} ${this.#draft(a).join(\",\")}`;\n+\t\t\t},\n \t\t};\n \t\treturn this.#draftCollisionHost;\n \t}"},{"path":"worker/project/test/kept-parts.test.ts","added":55,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/kept-parts.test.ts\n@@ -0,0 +1,55 @@\n+// What draft collisions keep between calls (abso Capacity, kept-parts.ts): never more than its byte budget, nothing\n+// bigger than the budget kept at all, and a round drops every entry it didn't use.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { KEPT_PARTS_BUDGET, KeptParts } from \"../kept-parts.ts\";\n+\n+test(\"kept parts stay under the budget, oldest out first, and an entry over the budget isn't kept\", () => {\n+\tconst parts = new KeptParts(1_000);\n+\tlet made = 0;\n+\tconst get = (key: string, bytes: number) =>\n+\t\tparts.get(\n+\t\t\tkey,\n+\t\t\t() => ++made,\n+\t\t\t() => bytes,\n+\t\t);\n+\tassert.equal(get(\"a\", 300), 1);\n+\tassert.equal(get(\"b\", 300), 2);\n+\tassert.equal(get(\"a\", 300), 1, \"kept\");\n+\t// Each entry weighs its key and its slot too (364 bytes here), so a third one is over: b, the oldest, goes.\n+\tassert.equal(get(\"c\", 300), 3);\n+\tassert.ok(parts.bytes <= 1_000, `${parts.bytes} bytes`);\n+\tassert.equal(get(\"a\", 300), 1, \"a was used after b, so it stayed\");\n+\tassert.equal(get(\"c\", 300), 3);\n+\tassert.equal(get(\"b\", 300), 4, \"b went\");\n+\tassert.equal(get(\"big\", 2_000), 5);\n+\tassert.equal(get(\"big\", 2_000), 6, \"never kept\");\n+\tassert.ok(parts.bytes <= 1_000);\n+\tassert.equal(KEPT_PARTS_BUDGET, 4_000_000);\n+});\n+\n+test(\"a round drops what it didn't use: a draft that changed, an agent that left, shared moved on\", () => {\n+\tconst parts = new KeptParts();\n+\tlet made = 0;\n+\tconst get = (key: string) =>\n+\t\tparts.get(\n+\t\t\tkey,\n+\t\t\t() => ++made,\n+\t\t\t() => 10,\n+\t\t);\n+\tparts.startRound();\n+\tget(\"uses 7 ann\");\n+\tget(\"uses 7 bob\");\n+\tparts.endRound();\n+\tassert.equal(parts.size, 2);\n+\tparts.startRound();\n+\tassert.equal(get(\"uses 7 ann\"), 1, \"kept across rounds\");\n+\tget(\"uses 7 bob+1\");\n+\tparts.endRound();\n+\tassert.equal(parts.size, 2, \"bob's old draft dropped\");\n+\tparts.startRound();\n+\tparts.endRound();\n+\tassert.equal(parts.size, 0);\n+\tassert.equal(parts.bytes, 0);\n+});"}],"ci":null}