- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/news.ts?format=json
506 linesFirst import 52% · capacity 37% · df-routing 4% · speed 2% · autosettle 2% · rename 1% · Last change capacity in version 309, 16 versions changed it
1// How news reads: one sentence for each thing an agent should hear, with the key that makes it heard
2// once, and who hears it after an edit, publish or land. Writes (the news entry) stay on Project.
4import { type AtomId, type Graph, isStartNode, parseKey, type ReadView } from "../../src/engine/index.ts";
5import { unionOverlay } from "../../src/engine/state-source.ts";
6import { type Origin, type Pair, pairsIn, touchedFiles } from "./collisions.ts";
7import { type DefinitionCollision, signature } from "./definition-rule.ts";
8import { definitionsIn } from "./definitions.ts";
9import type { Kept } from "./markers.ts";
10import { pairKey } from "./project-helpers.ts";
11import type { AgentState, Collision, Entry, NoteKind, Overlap, PersonSender, Settled } from "./project-types.ts";
12import { last, type Space } from "./spaces.ts";
14/** One piece of news: `key` is told to an agent once, `text` is what it reads. */
15export interface NewsItem {
16 readonly key: string;
17 readonly text: string;
18}
20/** A line quoted in news: trimmed, and cut to 80 characters. */
21export function clip(line: string): string {
22 const t = line.trim();
23 return t.length > 80 ? `${t.slice(0, 79)}…` : t;
24}
26function versionName(o: { version: number; draft?: string }): string {
27 return o.draft === undefined ? `version ${o.version}` : `draft ${o.draft}, version ${o.version}`;
28}
30export function agentAnnouncement(sender: string, mark: string, text: string): string {
31 return `Announcement from ${sender} (${mark}): ${text.trim()}`;
32}
34export function personAnnouncement(name: string, id: string, text: string): string {
35 return `Announcement from ${name} (a person, signed in on the website, id ${id}): ${JSON.stringify(text.trim())}`;
36}
38/** A reviewer hears of a change that touches its review area (#44). */
39export function reviewItem(
40 by: string,
41 change: number,
42 title: string,
43 files: readonly string[],
44 version: number,
45): NewsItem {
46 const shown = files.slice(0, 3).join(", ") + (files.length > 3 ? ` and ${files.length - 3} more` : "");
47 return {
48 key: `review ${change}`,
49 text: `${by}'s change "${clip(title)}" touches ${shown} (version ${version}), in your review area. Read it: abso review ${change}.`,
50 };
51}
53/** An objection's writer hears that a version changed every line it was on (#44). */
54export function addressedItem(
55 by: string,
56 version: number,
57 note: { readonly id: number; readonly file: string; readonly text: string },
58): NewsItem {
59 return {
60 key: `addressed ${note.id}`,
61 text: `${by}'s version ${version} changed every line your objection ${note.id} on ${note.file} was on ("${clip(note.text)}"). Look, then close it: abso note --close ${note.id} --resolved.`,
62 };
63}
65/**
66 * A note's lines' authors, its change's agent and its thread hear it (#44).
67 * A person's text is JSON-quoted, as in their announcements, so a line break
68 * can't pass for another item.
69 */
70export function noteSaid(note: {
71 readonly id: number;
72 readonly writer: string;
73 readonly person?: PersonSender;
74 readonly kind: NoteKind;
75 readonly reply: boolean;
76 readonly file: string;
77 readonly where: readonly [number, number] | null;
78 readonly change?: string;
79 readonly text: string;
80}): string {
81 const { id, file, where } = note;
82 const span = where
83 ? where[0] === where[1]
84 ? `line ${where[0]}`
85 : `lines ${where[0]}-${where[1]}`
86 : "lines now gone";
87 const about = note.change !== undefined ? ` (change "${clip(note.change)}")` : "";
88 const verb = note.reply ? "replies on" : note.kind === "objection" ? "objects on" : "comments on";
89 const ask =
90 note.kind === "objection" ? ` Fix it, or answer: abso note --reply ${id}` : ` Answer: abso note --reply ${id}`;
91 return note.person
92 ? `${note.person.person.name} (a person, signed in on the website, id ${note.person.person.id}) ${verb} ${file} ${span}${about}: ${JSON.stringify(clip(note.text))}.${ask}`
93 : `${note.writer} ${verb} ${file} ${span}${about}: "${clip(note.text)}".${ask}`;
94}
96/** A change's agent hears that an objection on it was closed (#44). */
97export function noteClosedItem(
98 by: string,
99 how: "resolved" | "withdrawn",
100 id: number,
101 note: { readonly file: string; readonly text: string },
102): NewsItem {
103 return {
104 key: `note closed ${id}`,
105 text: `${by} ${how === "resolved" ? "resolved" : "withdrew"} its objection on ${note.file}: "${clip(note.text)}".`,
106 };
107}
109/** An agent with work in files another's starting note names (#37). */
110export function startingItem(by: string, time: number, to: string, text: string, files: readonly string[]): NewsItem {
111 return {
112 key: `starting ${by} ${time} ${to}`,
113 text: `${by} says it is starting on "${text.trim()}", naming ${files.join(", ")}, where you have work too; run abso where to see who is where.`,
114 };
115}
117/** A file an edit left holding collision markers (#11): again whenever the count changes. */
118export function markedItem(draftId: string, file: string, blocks: number): NewsItem {
119 return {
120 key: `marked ${draftId} ${file} ${blocks}`,
121 text: `${file} still has ${blocks === 1 ? "1 collision block" : `${blocks} collision blocks`} to settle: abso settle ${file} --yours|--theirs|--both`,
122 };
123}
125/**
126 * How many unheard news lines an agent keeps (abso Capacity). An agent that never hears its news, most often one that
127 * stopped, kept every line: 7 MB of prod project abso's memory. Past this many (and NEWS_TRIM more), the oldest go,
128 * and one line in front says how many.
129 */
130export const NEWS_KEPT = 200;
131/** How far past NEWS_KEPT the news grows before the oldest are trimmed off. */
132export const NEWS_TRIM = 50;
134const DROPPED_NEWS = /^(\d+) older news items? went unheard and (?:was|were) dropped: an agent keeps its newest \d+\.$/;
136/** The line in front of an agent's news that counts what was dropped. */
137export function droppedNewsLine(count: number): string {
138 return `${count} older news ${count === 1 ? "item" : "items"} went unheard and ${count === 1 ? "was" : "were"} dropped: an agent keeps its newest ${NEWS_KEPT}.`;
139}
141/**
142 * Keeps an agent's unheard news to at most NEWS_KEPT + NEWS_TRIM lines (and their times, in step): the oldest go, and the dropped-count
143 * line in front adds them up. Deterministic, so a replay of the log keeps the same lines.
144 */
145export function capNews(agent: { news: string[]; newsAt: (number | undefined)[] }): void {
146 const counted = DROPPED_NEWS.exec(agent.news[0] ?? "");
147 const before = counted ? Number(counted[1]) : 0;
148 const from = counted ? 1 : 0;
149 // Trimmed in batches of NEWS_TRIM, so a busy agent isn't copied on every line of news.
150 if (agent.news.length - from <= NEWS_KEPT + NEWS_TRIM) return;
151 const over = agent.news.length - from - NEWS_KEPT;
152 const at = agent.newsAt[from + over];
153 agent.news = [droppedNewsLine(before + over), ...agent.news.slice(from + over)];
154 agent.newsAt = [at, ...agent.newsAt.slice(from + over)];
155}
157/**
158 * News a sync doesn't make moot, so it is kept through one: announcements,
159 * and what a settle kept (the pull brings the file, not whose side survived).
160 */
161export function outlastsSync(text: string): boolean {
162 return text.startsWith("Announcement from ") || / settled its collision with your version \d+ in /.test(text);
163}
165const SETTLED_WHAT: Record<Kept, string> = {
166 theirs: "it kept your lines",
167 yours: "it kept its own lines, not yours",
168 both: "it kept both its lines and yours",
169 rewrote: "it kept part of each side or rewrote them",
170};
172/** The other author of a marked collision a publish settled (#11): what was kept, any lines lost, and the settler's held reason. */
173/** " automatically, as abso's model advised (confidence 0.81)" for a settle the settler's abso made by itself (abso-core 23). */
174function autoWords(s: Settled): string {
175 if (!s.auto) return "";
176 return s.auto.by === "rule"
177 ? " automatically, by a check in code"
178 : ` automatically, as abso's model advised (confidence ${s.auto.confidence.toFixed(2)})`;
179}
181export function settledItem(by: string, version: number, s: Settled): NewsItem {
182 return {
183 key: `settled ${by} ${version} ${s.file} ${s.theirs.version}`,
184 text:
185 `${by} settled its collision with your version ${s.theirs.version} in ${s.file}${autoWords(s)}, pushing version ${version}: ${SETTLED_WHAT[s.kept]}.` +
186 (s.lost
187 ? ` It also lost ${s.lost.length === 1 ? "a line" : `${s.lost.length} lines`} you both still had: ${s.lost.map((l) => `"${clip(l)}"`).join(", ")}.`
188 : "") +
189 (s.held ? ` Its held push said: "${clip(s.held)}".` : "") +
190 ` abso show ${version}:${s.file} shows the file.`,
191 };
192}
194/** One line per other agent whose work or starting note meets files the draft just started changing (#37). */
195export function overlapItems(draftId: string, overlaps: readonly Overlap[]): NewsItem[] {
196 const byAgent = new Map<string, Overlap[]>();
197 for (const o of overlaps) byAgent.set(o.agent, [...(byAgent.get(o.agent) ?? []), o]);
198 const items: NewsItem[] = [];
199 for (const [name, list] of byAgent) {
200 const files = list.map((o) => o.file);
201 const what = list.some((o) => o.changes) ? "has unpushed changes in" : "says it is starting on";
202 const note = list.find((o) => o.starting)?.starting;
203 items.push({
204 key: `overlap ${draftId} ${name} ${files.join(" ")}`,
205 text: `${name} ${what} ${files.join(", ")}${note ? ` ("${note}")` : ""}, which you just started changing. If it's the same work, talk to ${name} before going further; abso where shows the lines.`,
206 });
207 }
208 return items;
209}
211/** A move that took lines the draft changes somewhere else (#28): the changes went with them. */
212export function moveItem(atom: string, origin: Origin, from: string, to: string): NewsItem {
213 const where = from === to ? `within ${to}` : `from ${from} to ${to}`;
214 return {
215 key: `move ${atom}`,
216 text:
217 `${origin.agent} pushed "${origin.message}" (${versionName(origin)}), which moved lines your draft changes ${where}; ` +
218 `your changes moved with them. Run abso pull to see them there.`,
219 };
220}
222/** A lines collision, as the agent whose draft it is reads it. */
223export function linesText(c: Collision): string {
224 const [a, b] = c.theirs.lines;
225 const where = b >= a ? `lines ${a}-${b}` : `line ${a}`;
226 return (
227 `${c.theirs.agent} pushed "${c.theirs.message}" (${versionName(c.theirs)}), ` +
228 `which changes ${c.file} ${where}, where your draft also changes lines; run abso pull to see both versions.`
229 );
230}
232/** A changed or removed definition the draft uses (#11). */
233export function definitionText(origin: Origin, change: string, name: string, file: string, line: number): string {
234 return (
235 `${origin.agent} pushed "${origin.message}" (${versionName(origin)}), which ${change}, ` +
236 `and your draft uses ${name} in ${file} line ${line}; run abso pull to see the new definition.`
237 );
238}
240/** What news assembly needs from a project. */
241export interface NewsHost {
242 readonly graph: Graph;
243 readonly agents: ReadonlyMap<string, AgentState>;
244 draft(agent: AgentState): AtomId[];
245 newerFor(agent: AgentState): ReadonlySet<AtomId>;
246 spaceOf(agent: AgentState): Space;
247 setOf(space: Space): ReadonlySet<AtomId>;
248 viewAt(space: Space, version: number): ReadView;
249 origin(atom: AtomId, spaceName: string): Origin;
250 collision(agent: AgentState, pair: Pair): Collision;
251 /** `names`: only these can collide; a draft that uses none of them has none (newsForOthers). */
252 definitionCollisions(
253 agent: AgentState,
254 newer: ReadonlySet<AtomId>,
255 names?: ReadonlySet<string>,
256 ): DefinitionCollision[];
257 queueNews(agent: AgentState, items: readonly NewsItem[], entries: Entry[]): void;
258 /** A line for the host's logs (ProjectOptions.logLine). */
259 logLine?(line: Record<string, string | number>): void;
260 /**
261 * Owed news (abso Capacity, 2026-10-15): when this answers true, a publish leaves its definition news for other
262 * agents to be worked out after its write (definitionNewsOwed) and hands it to owe instead.
263 */
264 owesDefinitionNews(): boolean;
265 owe(owed: OwedDefinitionNews): void;
266 spaceNamed(name: string): Space | undefined;
267 /** newerFor as it was at `version` of the agent's space. */
268 newerAt(agent: AgentState, version: number): ReadonlySet<AtomId>;
269 /** definitionCollisions against the agent's space at `version`, not its newest. */
270 definitionCollisionsAt(
271 agent: AgentState,
272 newer: ReadonlySet<AtomId>,
273 names: ReadonlySet<string>,
274 version: number,
275 ): DefinitionCollision[];
276 /** The space's atoms at `version`: setOf as it was then (owed news). */
277 setAt(space: Space, version: number): ReadonlySet<AtomId>;
278 /** collision, with theirs described from the agent's space at `version`, not its newest (owed news). */
279 collisionAt(agent: AgentState, pair: Pair, version: number): Collision;
280}
282/**
283 * A publish's news for other agents, not yet worked out: the version it made, and who made it. `all`: everything the
284 * publish would have told them (same lines, definitions, moves; abso 714). Without it, as owed before 714, only the
285 * definition news. `names`: the names its definitions changed, once worked out (owedNewsNames).
286 */
287export interface OwedNews {
288 readonly version: number;
289 readonly space: string;
290 readonly publisher: string;
291 readonly names?: readonly string[];
292 readonly all?: boolean;
293}
294export type OwedDefinitionNews = OwedNews;
296function lineNews(host: NewsHost, agent: AgentState, pairs: readonly Pair[]): NewsItem[] {
297 return pairs.map((p) => ({ key: pairKey(p), text: linesText(host.collision(agent, p)) }));
298}
300function definitionNews(defs: readonly DefinitionCollision[]): NewsItem[] {
301 return defs.map((d) => ({ key: d.key, text: d.news }));
302}
304/**
305 * One sentence per move in `just` that took lines the draft changes
306 * (deletes, or writes right after) somewhere else (#28). The draft's
307 * changes went with them, so this is news, not a collision.
308 */
309function moveNews(
310 host: NewsHost,
311 draft: readonly AtomId[],
312 just: ReadonlySet<AtomId>,
313 space: Space,
314 version: number,
315 /** The space's version the moved lines' files are read at: its newest, or an owed publish's own. */
316 now = last(space),
317): NewsItem[] {
318 const g = host.graph;
319 const movers = [...just].filter((id) => g.atom(id).moves);
320 if (movers.length === 0) return [];
321 const claimed = new Set<string>();
322 for (const id of draft) {
323 for (const key of g.atom(id).writes) {
324 const { kind, node } = parseKey(key);
325 if ((kind === "line" || kind === "gap") && !isStartNode(node)) claimed.add(node);
326 }
327 }
328 const was = host.viewAt(space, version - 1);
329 const after = host.viewAt(space, now);
330 const out: NewsItem[] = [];
331 for (const id of movers) {
332 const moved = g
333 .atom(id)
334 .writes.map((k) => parseKey(k))
335 .filter((k) => k.kind === "pos" && claimed.has(k.node))
336 .map((k) => k.node);
337 if (moved.length === 0) continue;
338 out.push(moveItem(id, host.origin(id, space.name), was.fileOf(moved[0]!), after.fileOf(moved[0]!)));
339 }
340 return out;
341}
343/** After the agent's own change: news when its draft now collides with work published after its base. */
344export function newsAboutOwnDraft(host: NewsHost, agent: AgentState, entries: Entry[]): void {
345 const draft = host.draft(agent);
346 if (draft.length === 0) return;
347 const newer = host.newerFor(agent);
348 if (newer.size === 0) return;
349 const lines = pairsIn(host.graph, draft, newer, unionOverlay(host.setOf(host.spaceOf(agent)), draft));
350 host.queueNews(
351 agent,
352 [...lineNews(host, agent, lines), ...definitionNews(host.definitionCollisions(agent, newer))],
353 entries,
354 );
355}
357/** After a publish or a landing: news for every other agent in that space whose draft collides with what just arrived. */
358export function newsForOthers(
359 host: NewsHost,
360 publisher: AgentState,
361 space: Space,
362 version: number,
363 entries: Entry[],
364): void {
365 const just = new Set(space.versions[version]!.atoms);
366 // The names whose definitions this publish changed, worked out once. Most publishes change none; then only the
367 // same-lines rule can bring news. A definition collision this publish brings (its `by` in `just`) is one of
368 // these, so a draft that uses none of them has none, and the rule isn't run for it (abso Capacity, prod abso
369 // 2026-10-15): it compared definitions in every file changed since each agent's base, for every agent with a draft,
370 // and a publish touching abso's worker/src/index.ts took 48 s of it.
371 let changedNames: ReadonlySet<string> | undefined;
372 // For the host's logs (abso 702): a line as the work starts, written before it, and one as it ends, so work that
373 // outlives its request still shows. No time in them: a Durable Object's clock stands still in synchronous code.
374 let drafts = 0;
375 let news = 0;
376 // Owed (abso 714, Christopher's base fix): on stored tables this news for other agents was most of a publish's one
377 // write transaction (the same-lines pairs, the definitions, the moves, for every draft: about 10 s for a 30-line
378 // change to abso's 12k-line index.ts), long enough to time storage out and reset the object (prod abso, 23:32Z
379 // 2026-10-16). Owing, the publish only notes what it owes, here, once some other agent has a draft; it's worked out
380 // after the commit, one agent at a time, against this version (newsOwed). Asked only then (news-lazy.test.ts: no
381 // news work at all otherwise).
382 let owing: boolean | undefined;
383 for (const agent of [...host.agents.values()].toSorted((a, b) => (a.name < b.name ? -1 : 1))) {
384 if (agent === publisher || agent.space !== space.name) continue;
385 const draft = host.draft(agent);
386 if (draft.length === 0) continue;
387 owing ??= host.owesDefinitionNews();
388 if (owing) {
389 host.owe({ version, space: space.name, publisher: publisher.name, all: true });
390 host.logLine?.({ work: "publishNews", step: "owed", space: space.name, version, publisher: publisher.name });
391 return;
392 }
393 if (drafts++ === 0)
394 host.logLine?.({ work: "publishNews", step: "start", space: space.name, version, publisher: publisher.name });
395 const lines = pairsIn(host.graph, draft, just, unionOverlay(host.setOf(space), draft));
396 changedNames ??= changedNamesAt(host, space, version);
397 const defs =
398 changedNames.size > 0
399 ? host.definitionCollisions(agent, host.newerFor(agent), changedNames).filter((d) => just.has(d.by))
400 : [];
401 const items = [
402 ...lineNews(host, agent, lines),
403 ...definitionNews(defs),
404 ...moveNews(host, draft, just, space, version),
405 ];
406 news += items.length;
407 host.queueNews(agent, items, entries);
408 }
409 if (drafts > 0)
410 host.logLine?.({
411 work: "publishNews",
412 step: "end",
413 space: space.name,
414 version,
415 publisher: publisher.name,
416 drafts,
417 changedNames: changedNames?.size ?? 0,
418 news,
419 });
420}
422/**
423 * The names whose definitions the publish at `version` changed, in the files it touched: before it against its own
424 * version. Most publishes change none; then only the same-lines rule can bring news. A definition collision the publish
425 * brings is one of these, so a draft that uses none of them has none (abso Capacity, prod abso 2026-10-15).
426 */
427function changedNamesAt(host: NewsHost, space: Space, version: number): ReadonlySet<string> {
428 const just = new Set(space.versions[version]!.atoms);
429 const touched = touchedFiles(host.graph, just, [host.viewAt(space, version - 1), host.viewAt(space, version)]);
430 const before = definitionsIn(host.viewAt(space, version - 1), touched);
431 const after = definitionsIn(host.viewAt(space, version), touched);
432 return new Set(
433 [...new Set([...before.keys(), ...after.keys()])].filter(
434 (name) => !before.has(name) || signature(before.get(name)!) !== signature(after.get(name) ?? []),
435 ),
436 );
437}
439/** owedNews's names, worked out once for a publish (the host keeps them on its row): changedNamesAt its version. */
440export function owedNewsNames(host: NewsHost, owed: OwedNews): string[] {
441 const space = host.spaceNamed(owed.space);
442 if (!space || owed.version > last(space) || !space.versions[owed.version]) return [];
443 return [...changedNamesAt(host, space, owed.version)].sort();
444}
446/**
447 * The definition news a publish owes, worked out as newsForOthers would have at that version: the same agents, the
448 * same rule against the space at `owed.version` and the work published up to it, the same words and keys. Reads only.
449 * An agent that has synced past the version since is left out (the version is no longer newer for it), and an item
450 * an agent was already told (from its own edit since, or this run before) is dropped, as queueNews would.
451 */
452export function newsOwed(
453 host: NewsHost,
454 owed: OwedNews,
455 /** Only these agents (owedAgents' names), so a host can work the news out one agent at a time (abso 714). */
456 only?: readonly string[],
457): { agent: string; items: NewsItem[] }[] {
458 const space = host.spaceNamed(owed.space);
459 if (!space || owed.version > last(space) || !space.versions[owed.version]) return [];
460 const just = new Set(space.versions[owed.version]!.atoms);
461 let names: ReadonlySet<string> | undefined = owed.names ? new Set(owed.names) : undefined;
462 const out: { agent: string; items: NewsItem[] }[] = [];
463 for (const agent of [...host.agents.values()].toSorted((a, b) => (a.name < b.name ? -1 : 1))) {
464 if (only && !only.includes(agent.name)) continue;
465 if (!owedTo(host, owed, space, agent)) continue;
466 const draft = host.draft(agent);
467 names ??= changedNamesAt(host, space, owed.version);
468 const lines = owed.all
469 ? pairsIn(host.graph, draft, just, unionOverlay(host.setAt(space, owed.version), draft)).map((p) => ({
470 key: pairKey(p),
471 text: linesText(host.collisionAt(agent, p, owed.version)),
472 }))
473 : [];
474 const defs =
475 names.size > 0
476 ? host
477 .definitionCollisionsAt(agent, host.newerAt(agent, owed.version), names, owed.version)
478 .filter((d) => just.has(d.by))
479 : [];
480 const moves = owed.all ? moveNews(host, draft, just, space, owed.version, owed.version) : [];
481 const items = [...lines, ...definitionNews(defs), ...moves].filter((item) => !agent.told.has(item.key));
482 if (items.length > 0) out.push({ agent: agent.name, items });
483 }
484 return out;
485}
486/** newsOwed, under the name tonight's owed definition news had (abso 2341e805). */
487export const definitionNewsOwed = newsOwed;
489/**
490 * The agents a publish's owed news may be for, in the order definitionNewsOwed takes them: every other agent in the
491 * space, behind the version, with a draft. Cheap (no definition rule), so a host can list them once and then work the
492 * news out a few at a time, each in its own read (abso 714: one read over every draft reset prod abso's object).
493 */
494export function owedAgents(host: NewsHost, owed: OwedDefinitionNews): string[] {
495 const space = host.spaceNamed(owed.space);
496 if (!space || owed.version > last(space) || !space.versions[owed.version]) return [];
497 return [...host.agents.values()]
498 .toSorted((a, b) => (a.name < b.name ? -1 : 1))
499 .filter((agent) => owedTo(host, owed, space, agent))
500 .map((agent) => agent.name);
501}
503function owedTo(host: NewsHost, owed: OwedDefinitionNews, space: Space, agent: AgentState): boolean {
504 if (agent.name === owed.publisher || agent.space !== space.name || agent.base >= owed.version) return false;
505 return host.draft(agent).length > 0;
506}