Collisions step 2: shared with drafts on top is a kept view that borrows shared's layouts (abso Capacity, issue 664). Version 246 of shared· capacity· Oct 15 (2026-10-15 16:08 UTC) · Not checked
Draft 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.
On 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.
test/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).
Version 245 · Files at version 246
3 files, 97 lines added, 14 removed.
--- a/worker/project/draft-collisions.ts
+++ b/worker/project/draft-collisions.ts
@@ -2,7 +2,7 @@
2 2 // is the project in memory. 4 4 import { type AtomId, type Graph, parseKey, type ReadView } from "../../src/engine/index.ts"; 5 − import { StableSet, unionOverlay } from "../../src/engine/state-source.ts";
5 + import { StableSet } from "../../src/engine/state-source.ts"; 6 6 import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from "./collisions.ts"; 7 7 import type { DefinitionRule } from "./definition-rule.ts"; 8 8 import { type Definition, definedTwice } from "./definitions.ts"; @@ -25,7 +25,8 @@
25 25 definitionRule(): DefinitionRule; 26 26 draft(agent: Agent): AtomId[]; 27 27 agentView(agent: Agent): ReadView; 28 − view(set: ReadonlySet<AtomId>): ReadView;
28 + /** Shared now with these agents' drafts on top. */ 29 + draftsView(agents: readonly Agent[]): ReadView; 29 30 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>; @@ -95,16 +96,9 @@
95 96 if (!set) draftSets.set(agent, (set = new Set(host.draft(agent)))); 98 − // 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
99 − // is closed under dependencies anyway, so shared's closed set and the drafts lay out what shared's atoms and the drafts
100 − // do, and only the drafts' atoms are walked to close it.
101 − const withDraftsView = (...drafts: readonly AtomId[][]) => host.view(unionOverlay(sharedView.set, drafts.flat()));
102 − const nowViews = new Map<Agent, ReadView>();
103 − const nowViewOf = (agent: Agent) => {
104 − let view = nowViews.get(agent);
105 − if (!view) nowViews.set(agent, (view = withDraftsView(host.draft(agent))));
106 − return view;
107 − };
99 + // Shared now with each agent's draft on top (project.ts, draftsView): kept between calls while shared and the 100 + // draft stay the same, and laid out as shared is for every file the draft doesn't touch (abso Capacity). 101 + const nowViewOf = (agent: Agent) => host.draftsView([agent]); 108 102 // Each agent's touched files once, not once per pair (abso Capacity). 109 103 const touched = new Map<Agent, Set<string>>(); 110 104 const touchedBy = (agent: Agent) => { @@ -161,7 +155,7 @@
161 155 const mineB = new Set(draftB); 162 156 const filesA = touchedBy(a); 163 157 for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) { 164 − both ??= withDraftsView(draftA, draftB);
158 + both ??= host.draftsView([a, b]); 165 159 if (!both.hasFile(file)) continue; 166 160 for (const [name, defs] of definedTwice(both, file)) { 167 161 const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!))); --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -1900,12 +1900,35 @@
1900 1900 definitionRule: () => this.#definitionRule, 1901 1901 draft: (agent) => this.#draft(agent as AgentState), 1902 1902 agentView: (agent) => this.#agentView(agent as AgentState), 1903 − view: (set) => this.#view(set),
1903 + draftsView: (agents) => this.#draftsView(agents as readonly AgentState[]), 1904 1904 touchedFiles: (ids, views) => this.#touchedFiles(ids, views), 1906 1906 return this.#draftCollisionHost; 1910 + * Shared now with some agents' drafts on top, for draft collisions (abso Capacity). Kept under the drafts' atoms and 1911 + * shared's version, so a second ask with nothing changed lays nothing out again, and laid out as shared is for every 1912 + * file the drafts don't touch, as an agent's view is. One agent based on shared's newest version is its own view. 1914 + #draftsView(agents: readonly AgentState[]): ReadView { 1915 + const shared = this.#shared; 1916 + const version = last(shared); 1917 + const only = agents.length === 1 ? agents[0]! : undefined; 1918 + if (only && only.space === SHARED && only.base === version) return this.#agentView(only); 1919 + const drafts = agents.map((agent) => this.#draft(agent)); 1920 + const mine = drafts.flat(); 1921 + const base = this.#sharedView(); 1922 + const view = this.#viewKeyed(`d ${SHARED}@${version}+${drafts.map((d) => d.join(",")).join("+")}`, () => 1923 + unionOverlay(base.set, mine), 1925 + if (this.#writerStorage && mine.length > 0 && view instanceof View && base instanceof View) { 1926 + view.shareMoves(base); 1927 + view.shareFiles(base, () => touchedBeyond(this.#graph, base.set, mine, [base, view])); 1909 1932 /** Whether agents' unpublished changes would collide, by the publish rule (draft-collisions.ts). */ 1910 1933 collisions(...args: Tail<Parameters<typeof draftCollisions>>): ReturnType<typeof draftCollisions> { 1911 1934 return draftCollisions(this.#draftCollisions, ...args); --- /dev/null
+++ b/worker/project/test/collisions-kept-views.test.ts
@@ -0,0 +1,66 @@
1 + // Draft collisions read shared with drafts on top through kept views (abso Capacity, Project's draftsView): kept under 2 + // shared's version and the drafts' atoms, and laid out as shared is for the files the drafts don't touch. On a writer 3 + // every answer must match the in-memory project's, asked twice, and again after a draft changes, after shared moves on 4 + // and after a draft is dropped. 6 + import assert from "node:assert/strict"; 7 + import { test } from "node:test"; 8 + import { storedArray } from "../../../src/engine/state-source.ts"; 9 + import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 10 + import { MemoryStore, Project } from "../project.ts"; 11 + import type { Entry, ProjectStore } from "../project-types.ts"; 14 + const storage = new SqlState(); 15 + const entries = storedArray<Entry>(storage, "canonical-log"); 16 + const store: ProjectStore = { 18 + for (const entry of batch) entries.push(entry); 21 + return new Project(store, { writerStorage: storage }); 24 + const SHAPES = "export function area(r: number) {\n return 3.14 * r * r;\n}\n"; 25 + const IMPORT = 'import { area } from "./shapes";\n'; 26 + const MAIN = `${IMPORT}export const a = area(2);\n`; 29 + const steps: [string, (project: Project) => void][] = [ 33 + project.init({ "src/shapes.ts": SHAPES, "src/main.ts": IMPORT, "src/notes.md": "notes\n", "e.ts": "// e\n" }, t); 35 + project.record("old", { "src/notes.md": "notes\nmore\n" }, ++t); 36 + project.record("mover", { "src/notes.md": null, "docs/notes.md": "notes\n" }, ++t); 37 + assert.equal(project.publish("mover", "Move notes", ++t).ok, true); 38 + // ann changes area(); bob's new line uses it; cy and dee define f in e.ts. 39 + project.edit("ann", { file: "src/shapes.ts", old: "area(r: number)", new: "area(r: number, scale = 1)" }, ++t); 40 + project.record("bob", { "src/main.ts": MAIN }, ++t); 41 + project.record("cy", { "e.ts": "// e\nexport function f() {}\n" }, ++t); 42 + project.record("dee", { "e.ts": "export function f() { return 1; }\n// e\n" }, ++t); 45 + ["a draft changes", (project) => project.record("bob", { "src/main.ts": `${MAIN}export const b = area(3);\n` }, ++t)], 46 + ["shared moves on", (project) => assert.equal(project.publish("ann", "Add a scale", ++t).ok, true)], 47 + ["a draft is dropped", (project) => project.restore("cy", null, ++t)], 50 + test("draft collisions through kept views match the in-memory project's as drafts and shared change", () => { 51 + const oracle = new Project(new MemoryStore()); 52 + const project = writer(); 53 + const seen = new Set<string>(); 54 + for (const [name, step] of steps) { 59 + const expected = oracle.collisions(); 60 + for (const c of expected.collisions) seen.add(c.kind); 61 + assert.deepEqual(project.collisions(), expected, name); 62 + assert.deepEqual(project.collisions(), expected, `${name}, again`); 64 + // The kinds that read through the kept views, so a history change can't empty the test. 65 + assert.ok(seen.has("definition"), [...seen].join(", "));