where lays out an agent's view only when it reads it Version 83 of shared· speed· Oct 10 (2026-10-10 06:54 UTC) · Not checked
draftOf's view is now built on first use. where asks every agent for its draft, most agents have no changes, and a big project has more agents than the view cache holds (32), so each call built every view again. On a 17,000-entry log with 52 agents, where went from about 730 ms a call to about 10 ms. Test: worker/project/test/where-many-agents.test.ts.
Version 82 · Files at version 83
2 files, 43 lines added, 3 removed.
--- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -391,10 +391,21 @@
391 391 return [...this.#agents.keys()].sort(); 394 − /** An agent's unpublished changes, oldest first, and its view (its space at its base plus its changes). Read only. */
395 − draftOf(agentName: string): { atoms: AtomId[]; view: View } | undefined {
395 + * An agent's unpublished changes, oldest first, and its view (its space at its base plus its changes). Read only. 396 + * The view is laid out on first use: where asks every agent, most have no changes, and a big project has more 397 + * agents than the view cache holds, so building every view cost each call about 0.7 s. 399 + draftOf(agentName: string): { atoms: AtomId[]; readonly view: View } | undefined { 396 400 const agent = this.#agents.get(agentName); 397 − return agent && { atoms: this.#draft(agent), view: this.#agentView(agent) };
401 + if (!agent) return undefined; 402 + const view = () => this.#agentView(agent); 404 + atoms: this.#draft(agent), 400 411 /** The shared version an atom was published in, or undefined while it's only in a draft. */ --- /dev/null
+++ b/worker/project/test/where-many-agents.test.ts
@@ -0,0 +1,29 @@
1 + // Where asks every agent for its draft, and a big project has more agents than 2 + // the view cache holds (32). An agent's view is laid out only when it's read, 3 + // so agents with no changes cost nothing, and the answer stays what it was. 5 + import assert from "node:assert/strict"; 6 + import { test } from "node:test"; 7 + import { MemoryStore, Project } from "../project.ts"; 8 + import { where } from "../where.ts"; 10 + test("where answers right with more agents than the view cache holds, and lays out no view for an empty draft", () => { 11 + const store = new MemoryStore(); 12 + const project = new Project(store); 13 + project.init({ "a.ts": "one\ntwo\nthree\n" }, 0); 14 + for (let i = 0; i < 40; i++) project.sync(`idle-${i}`); 15 + project.edit("busy", { file: "a.ts", old: "two\n", new: "two!\n" }, 1); 17 + const answer = where(project); 18 + assert.deepEqual(Object.keys(answer.files), ["a.ts"]); 20 + answer.files["a.ts"]!.map((s) => [s.agent, s.lines, s.changes]), 21 + [["busy", [[2, 2]], 1]], 23 + assert.deepEqual(where(Project.load(store.entries, new MemoryStore())), answer); 25 + const idle = project.draftOf("idle-0")!; 26 + assert.deepEqual(idle.atoms, []); 27 + assert.equal(typeof Object.getOwnPropertyDescriptor(idle, "view")?.get, "function"); 28 + assert.equal(idle.view.text("a.ts"), "one\ntwo\nthree\n");