{"version":246,"agent":"capacity","message":"Collisions step 2: shared with drafts on top is a kept view that borrows shared's layouts (abso Capacity, issue 664).\n\nDraft collisions asked for shared now with one agent's draft on top (and with two agents' for a name both define) as a fresh view each call, laid out from scratch. Project's draftsView keeps it under shared's version and the drafts' atoms, so it changes with any draft change, a dropped draft or shared moving. It is laid out as shared is for every file the drafts don't touch, as an agent's view is (shareFiles, touchedBeyond). One agent based on shared's newest version is just its own view.\n\nOn the abso export (my machine): a warm collisions call takes 6.3 s instead of 7.7 s, with 45 layouts instead of 80. Same answer as replay. A full warm run's heap is 76.9 MB against 76.5 MB on 244, so borrowing adds nothing.\n\ntest/collisions-kept-views.test.ts: a writer against the in-memory project, each answer asked twice, through drafts with definition and duplicate collisions, after a draft changes, after shared moves on and after a draft is dropped. The collision, path and move tests pass (35 of 35).","time":1792080534526,"timeIso":"2026-10-15T16:08:54.526Z","changes":8,"change":3791,"files":["worker/project/draft-collisions.ts","worker/project/project.ts","worker/project/test/collisions-kept-views.test.ts"],"draft":null,"diff":[{"path":"worker/project/draft-collisions.ts","added":7,"removed":13,"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 { StableSet, unionOverlay } from \"../../src/engine/state-source.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@@ -25,7 +25,8 @@\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 }\n \n@@ -95,16 +96,9 @@\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. 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 = withDraftsView(host.draft(agent))));\n-\t\treturn view;\n-\t};\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 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@@ -161,7 +155,7 @@\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 ??= withDraftsView(draftA, draftB);\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\t\tconst by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!)));"},{"path":"worker/project/project.ts","added":24,"removed":1,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -1900,12 +1900,35 @@\n \t\t\tdefinitionRule: () => this.#definitionRule,\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\tdraftsView: (agents) => this.#draftsView(agents as readonly AgentState[]),\n \t\t\ttouchedFiles: (ids, views) => this.#touchedFiles(ids, views),\n \t\t};\n \t\treturn this.#draftCollisionHost;\n \t}\n \n+\t/**\n+\t * Shared now with some agents' drafts on top, for draft collisions (abso Capacity). Kept under the drafts' atoms and\n+\t * shared's version, so a second ask with nothing changed lays nothing out again, and laid out as shared is for every\n+\t * file the drafts don't touch, as an agent's view is. One agent based on shared's newest version is its own view.\n+\t */\n+\t#draftsView(agents: readonly AgentState[]): ReadView {\n+\t\tconst shared = this.#shared;\n+\t\tconst version = last(shared);\n+\t\tconst only = agents.length === 1 ? agents[0]! : undefined;\n+\t\tif (only && only.space === SHARED && only.base === version) return this.#agentView(only);\n+\t\tconst drafts = agents.map((agent) => this.#draft(agent));\n+\t\tconst mine = drafts.flat();\n+\t\tconst base = this.#sharedView();\n+\t\tconst view = this.#viewKeyed(`d ${SHARED}@${version}+${drafts.map((d) => d.join(\",\")).join(\"+\")}`, () =>\n+\t\t\tunionOverlay(base.set, mine),\n+\t\t);\n+\t\tif (this.#writerStorage && mine.length > 0 && view instanceof View && base instanceof View) {\n+\t\t\tview.shareMoves(base);\n+\t\t\tview.shareFiles(base, () => touchedBeyond(this.#graph, base.set, mine, [base, view]));\n+\t\t}\n+\t\treturn view;\n+\t}\n+\n \t/** Whether agents' unpublished changes would collide, by the publish rule (draft-collisions.ts). */\n \tcollisions(...args: Tail<Parameters<typeof draftCollisions>>): ReturnType<typeof draftCollisions> {\n \t\treturn draftCollisions(this.#draftCollisions, ...args);"},{"path":"worker/project/test/collisions-kept-views.test.ts","added":66,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/collisions-kept-views.test.ts\n@@ -0,0 +1,66 @@\n+// Draft collisions read shared with drafts on top through kept views (abso Capacity, Project's draftsView): kept under\n+// shared's version and the drafts' atoms, and laid out as shared is for the files the drafts don't touch. On a writer\n+// every answer must match the in-memory project's, asked twice, and again after a draft changes, after shared moves on\n+// and after a draft is dropped.\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+\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 SHAPES = \"export function area(r: number) {\\n  return 3.14 * r * r;\\n}\\n\";\n+const IMPORT = 'import { area } from \"./shapes\";\\n';\n+const MAIN = `${IMPORT}export const a = area(2);\\n`;\n+\n+let t = 0;\n+const steps: [string, (project: Project) => void][] = [\n+\t[\n+\t\t\"drafts\",\n+\t\t(project) => {\n+\t\t\tproject.init({ \"src/shapes.ts\": SHAPES, \"src/main.ts\": IMPORT, \"src/notes.md\": \"notes\\n\", \"e.ts\": \"// e\\n\" }, t);\n+\t\t\tproject.sync(\"old\");\n+\t\t\tproject.record(\"old\", { \"src/notes.md\": \"notes\\nmore\\n\" }, ++t);\n+\t\t\tproject.record(\"mover\", { \"src/notes.md\": null, \"docs/notes.md\": \"notes\\n\" }, ++t);\n+\t\t\tassert.equal(project.publish(\"mover\", \"Move notes\", ++t).ok, true);\n+\t\t\t// ann changes area(); bob's new line uses it; cy and dee define f in e.ts.\n+\t\t\tproject.edit(\"ann\", { file: \"src/shapes.ts\", old: \"area(r: number)\", new: \"area(r: number, scale = 1)\" }, ++t);\n+\t\t\tproject.record(\"bob\", { \"src/main.ts\": MAIN }, ++t);\n+\t\t\tproject.record(\"cy\", { \"e.ts\": \"// e\\nexport function f() {}\\n\" }, ++t);\n+\t\t\tproject.record(\"dee\", { \"e.ts\": \"export function f() { return 1; }\\n// e\\n\" }, ++t);\n+\t\t},\n+\t],\n+\t[\"a draft changes\", (project) => project.record(\"bob\", { \"src/main.ts\": `${MAIN}export const b = area(3);\\n` }, ++t)],\n+\t[\"shared moves on\", (project) => assert.equal(project.publish(\"ann\", \"Add a scale\", ++t).ok, true)],\n+\t[\"a draft is dropped\", (project) => project.restore(\"cy\", null, ++t)],\n+];\n+\n+test(\"draft collisions through kept views match the in-memory project's as drafts and shared change\", () => {\n+\tconst oracle = new Project(new MemoryStore());\n+\tconst project = writer();\n+\tconst seen = new Set<string>();\n+\tfor (const [name, step] of steps) {\n+\t\tconst at = t;\n+\t\tstep(oracle);\n+\t\tt = at;\n+\t\tstep(project);\n+\t\tconst expected = oracle.collisions();\n+\t\tfor (const c of expected.collisions) seen.add(c.kind);\n+\t\tassert.deepEqual(project.collisions(), expected, name);\n+\t\tassert.deepEqual(project.collisions(), expected, `${name}, again`);\n+\t}\n+\t// The kinds that read through the kept views, so a history change can't empty the test.\n+\tassert.ok(seen.has(\"definition\"), [...seen].join(\", \"));\n+});"}],"ci":null}