Project tells a host each write's news (newsWritten), and reads an agent's waiting news without taking it (peekNews) Version 292 of shared· readspeed· Oct 16 (2026-10-16 03:27 UTC) · Not checked
A host that wakes a listener the moment news lands, such as a WebSocket per agent, needs two things from the engine: which agents a write just gave news, and a way to show what's waiting without using it up.
ProjectOptions.newsWritten(items): after each write that appended "news" entries, it's called once with one item per text: {agent, text, at, seq}, where seq is the entry's place in the log. It runs inside the write, which can still roll back, so the host collects the items and acts only once the call has returned. Taking news (heard) and writes with no news call nothing, so a listener's own reads wake no one. It works on both the in-memory store and openWriter's. peekNews(agent): the agent's waiting news, read only. Nothing is taken, heard or written, and an agent the project hasn't got reads as none, with none made. A listener can catch up on connect while the news stays for the agent's own next read, so a listener that dies loses nothing. hasAgent(agent): whether the project has it, read only. Tests: worker/project/test/news-written.test.ts (3): one call per write, with each item's agent, text and place in the log; no call for writes without news or for taking news; peekNews takes and writes nothing, and the take still finds the news. All engine tests pass (362).
Version 291 · Files at version 292
3 files, 94 lines added, 0 removed.
--- a/worker/project/project-types.ts
+++ b/worker/project/project-types.ts
@@ -583,7 +583,22 @@
583 583 export type Blobs = Record<string, string | null>; 585 585 /** What the project needs from the store of binary files' bytes. */ 586 + /** One news item a write gave an agent: its text and time, and its entry's place in the log. */ 587 + export interface NewsWritten { 588 + readonly agent: string; 589 + readonly text: string; 590 + readonly at: number | null; 591 + /** The news entry's place in the log: later news has a higher one. */ 592 + readonly seq: number; 586 595 export interface ProjectOptions { 597 + * Told after each write that gave agents news, with each item as the write appended it. It runs inside the write, 598 + * which can still roll back, so a host collects the items and acts on them only once the call has returned. A host 599 + * can then wake a listener for exactly the agents that got news, without reading the log. 601 + newsWritten?: (items: readonly NewsWritten[]) => void; 587 602 /** Optional derived atom indexing within persistent graph acceptance. */ 588 603 indexAcceptedAtom?: (atom: Atom, files: readonly string[], storage: GraphStateStorage) => void; 589 604 /** Private experimental reader route; never falls back for unsupported membership. */ --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -157,6 +157,7 @@
@@ -330,6 +331,7 @@
331 332 checkerReady = false; 332 333 readonly #now: (() => number) | undefined; 334 + readonly #newsWritten: ((items: readonly NewsWritten[]) => void) | undefined; 333 335 readonly #whole: ProjectOptions["whole"]; 334 336 readonly #shapes: ProjectOptions["shapes"]; 335 337 readonly #logLine: ProjectOptions["logLine"]; @@ -452,6 +454,7 @@
453 455 this.#hasBlob = options.hasBlob ?? (() => false); 454 456 this.#now = options.now; 457 + this.#newsWritten = options.newsWritten; 455 458 this.#whole = options.whole; 456 459 this.#shapes = options.shapes; 457 460 this.#logLine = options.logLine; @@ -1440,6 +1443,17 @@
1440 1443 return hostedOverlapNews(this.#overlapHost, ...args); 1446 + /** The news waiting for an agent, read only: nothing is taken, heard or written. None for an agent it hasn't got. */ 1447 + peekNews(agentName: string): { text: string; at: number | null }[] { 1448 + const agent = typeof agentName === "string" ? this.#agents.get(agentName) : undefined; 1449 + return agent ? agent.news.map((text, i) => ({ text, at: agent.newsAt[i] ?? null })) : []; 1452 + /** Whether the project has this agent. Read only. */ 1453 + hasAgent(agentName: string): boolean { 1454 + return typeof agentName === "string" && this.#agents.get(agentName) !== undefined; 1443 1457 /** Hand over the news waiting for an agent (collisions, definitions, moves, announcements), once. */ 1444 1458 news(agentName: string): { news: string[]; items: { text: string; at: number | null }[] } { 1445 1459 const entries: Entry[] = []; @@ -1719,6 +1733,15 @@
1719 1733 if (this.#pending.length === 0) return; 1720 1734 const out = this.#pending; 1721 1735 this.#store.append(out); 1736 + if (this.#newsWritten) { 1737 + const items: NewsWritten[] = []; 1738 + out.forEach((entry, i) => { 1739 + if (entry.type !== "news") return; 1740 + for (const text of entry.text) 1741 + items.push({ agent: entry.agent, text, at: entry.time ?? null, seq: this.#stored + i }); 1743 + if (items.length > 0) this.#newsWritten(items); 1722 1745 this.#persistMutable(); 1723 1746 // A few more agents' old told lists into their buckets at each write, until every row has moved (writer-agents.ts). 1724 1747 if (this.#agents instanceof WriterAgents) this.#agents.moveOldTold(OLD_TOLD_PER_WRITE); --- /dev/null
+++ b/worker/project/test/news-written.test.ts
@@ -0,0 +1,56 @@
1 + // A host learns each write's news as the write appends it (newsWritten), and can read an agent's waiting news without 2 + // taking it (peekNews), so a listener can be woken for exactly the agents that got news and caught up on connect. 4 + import assert from "node:assert/strict"; 5 + import { test } from "node:test"; 6 + import { MemoryStore, type NewsWritten, Project } from "../project.ts"; 9 + const store = new MemoryStore(); 10 + const written: NewsWritten[][] = []; 11 + const project = new Project(store, { newsWritten: (items) => written.push([...items]) }); 12 + project.init({ "a.txt": "a\n" }, 0); 13 + for (const a of ["builder1", "builder2", "builder3"]) project.sync(a); 15 + return { project, store, written }; 18 + test("a write's news is told once, per item, with its agent, text and place in the log", () => { 19 + const { project, store, written } = setup(); 20 + project.announce("builder1", "Ship it", ["builder2", "builder3"], 1); 21 + assert.equal(written.length, 1, "one call for the write"); 22 + const items = written[0]!; 24 + items.map((i) => i.agent), 25 + ["builder2", "builder3"], 27 + for (const item of items) { 28 + assert.match(item.text, /Announcement from builder1.*Ship it/); 29 + assert.equal(store.entries[item.seq]?.type, "news", "seq is the entry's place in the log"); 31 + assert.ok(items[1]!.seq > items[0]!.seq); 34 + test("writes with no news, and taking news (heard), tell nothing", () => { 35 + const { project, written } = setup(); 36 + project.announce("builder1", "Ship it", ["builder2"], 1); 38 + project.news("builder2"); 39 + project.sync("builder3"); 40 + assert.deepEqual(written, []); 43 + test("peekNews reads waiting news and takes nothing; hasAgent reads only", () => { 44 + const { project, store } = setup(); 45 + project.announce("builder1", "Ship it", ["builder2"], 1); 46 + const length = store.entries.length; 47 + const peeked = project.peekNews("builder2"); 48 + assert.equal(peeked.length, 1); 49 + assert.match(peeked[0]!.text, /Ship it/); 50 + assert.deepEqual(project.peekNews("builder2"), peeked, "still there"); 51 + assert.deepEqual(project.peekNews("nobody"), [], "no agent: none, and none made"); 52 + assert.equal(project.hasAgent("builder2"), true); 53 + assert.equal(project.hasAgent("nobody"), false); 54 + assert.equal(store.entries.length, length, "nothing written"); 55 + assert.equal(project.news("builder2").news.length, 1, "the take still finds it");