- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/apply-entry.ts?format=json
333 linesFirst import 78% · pinch 13% · speed 5% · codex-capacity-chair 4% · capacity 1% · infra-split 1%Last change capacity in version 200, 7 versions changed it
1// Apply one log entry to the project's in-memory state. Writes go through
2// ApplyEntryHost; today the host is Project. Membership (which atoms are in
3// which space) is applied first, in spaces.ts.
5import type { AtomId, Graph } from "../../src/engine/index.ts";
6import { capNews, outlastsSync } from "./news.ts";
7import { ProjectError } from "./project-error.ts";
8import type {
9 AgentState,
10 ChangeRecord,
11 Entry,
12 NoteRecord,
13 QueuedPublish,
14 Refusal,
15 TellRecord,
16} from "./project-types.ts";
17import { SHARED, type Space } from "./spaces.ts";
19/** What applying one log entry needs from a project. */
20export interface ApplyEntryHost {
21 readonly graph: Graph;
22 readonly agents: Map<string, AgentState>;
23 readonly atomAt: number[];
24 readonly mods: string[];
25 readonly notes: Map<number, NoteRecord>;
26 readonly changes: Map<number, ChangeRecord>;
27 readonly labels: Map<string, number[]>;
28 readonly refusals: Refusal[];
29 /** The newest tells, by id in the order sent, at most TELLS_KEPT. */
30 readonly tells: Map<number, TellRecord>;
31 uncheck(id: number): void;
32 stamp(time: number | undefined): void;
33 applyMembership(entry: Entry, at: number): void;
34 agentOf(name: string): AgentState;
35 active(agent: AgentState, time: number): void;
36 joinChange(
37 agent: AgentState,
38 entry: Extract<Entry, { type: "publish" | "land" }>,
39 at: number,
40 draft: string | null,
41 landed?: string,
42 ): void;
43 space(name: string): Space;
44 queued(): QueuedPublish[];
45 setQueued(items: QueuedPublish[]): void;
46 setQueuedId(id: number): void;
47 queuedId(): number;
48 setLastNote(id: number): void;
49 lastNote(): number;
50 setAnnounced(n: number): void;
51 announced(): number;
52 at(space: Space, version: number): ReadonlySet<AtomId>;
53 spaceOf(agent: AgentState): Space;
54 draft(agent: AgentState): AtomId[];
55 currentChange(agent: AgentState, time: number): number | undefined;
56 changeRecord(id: number, agent: AgentState, time: number): ChangeRecord;
57}
59/**
60 * Apply one log entry at position `at`. `time` is the entry's time when it
61 * has one, used for news that was logged before every item carried its own.
62 */
63export function applyEntry(host: ApplyEntryHost, entry: Entry, at: number, time: number | undefined): void {
64 const stamped = entry.type === "draft" ? entry.atom.time : "time" in entry ? entry.time : undefined;
65 host.stamp(stamped);
66 // Which atoms are in which space at which version: kept in spaces.ts, the same rules the tables use.
67 host.applyMembership(entry, at);
68 switch (entry.type) {
69 case "init": {
70 host.atomAt[entry.atom.seq] = at;
71 host.graph.add(entry.atom);
72 break;
73 }
74 case "join":
75 host.agents.set(entry.agent, {
76 name: entry.agent,
77 space: SHARED,
78 base: entry.version,
79 synced: false,
80 mine: [],
81 news: [],
82 newsAt: [],
83 told: new Set(),
84 parked: new Map(),
85 marked: [],
86 joined: at,
87 });
88 break;
89 case "draft": {
90 host.atomAt[entry.atom.seq] = at;
91 host.graph.add(entry.atom);
92 const agent = host.agentOf(entry.agent);
93 host.active(agent, entry.atom.time);
94 agent.mine.push(entry.atom.id);
95 agent.lastEdit = {
96 time: entry.atom.time,
97 ...(entry.session ? { session: entry.session } : {}),
98 ...(entry.client ? { client: entry.client } : {}),
99 };
100 if (entry.client?.startsWith("avcs-mod/") && !host.mods.includes(entry.client)) host.mods.push(entry.client);
101 break;
102 }
103 case "publish": {
104 host.atomAt[entry.tie.seq] = at;
105 host.graph.add(entry.tie);
106 const space = host.space(entry.space ?? SHARED);
107 const agent = host.agentOf(entry.agent);
108 if (entry.rebase) {
109 agent.base = entry.version;
110 agent.mine = [];
111 } else {
112 agent.mine.push(entry.tie.id);
113 }
114 host.joinChange(agent, entry, at, space.name === SHARED ? null : space.name);
115 agent.marked = agent.marked.filter((r) => r.space !== (entry.space ?? SHARED));
116 delete agent.held;
117 if (entry.queued !== undefined) host.setQueued(host.queued().filter((q) => q.id !== entry.queued));
118 break;
119 }
120 case "queued": {
121 const agent = host.agentOf(entry.agent);
122 host.active(agent, entry.time);
123 host.queued().push({
124 id: entry.id,
125 agent: entry.agent,
126 message: entry.message,
127 time: entry.time,
128 changes: [...entry.changes],
129 resolves: [...entry.resolves],
130 ...(entry.settled ? { settled: entry.settled } : {}),
131 ...(entry.change !== undefined ? { change: entry.change } : {}),
132 ...(entry.change !== undefined && entry.change === agent.noteAt && agent.starting
133 ? { note: agent.starting }
134 : {}),
135 });
136 host.setQueuedId(Math.max(host.queuedId(), entry.id));
137 // Its markers were settled when it was queued; what it kept goes out when it lands.
138 agent.marked = agent.marked.filter((r) => r.space !== SHARED);
139 break;
140 }
141 case "note": {
142 if (!entry.person) host.agentOf(entry.agent);
143 const { type: _, ...note } = entry;
144 host.notes.set(entry.id, { ...note, lines: [...entry.lines], at });
145 host.setLastNote(Math.max(host.lastNote(), entry.id));
146 if (entry.change !== undefined) {
147 const change = host.changes.get(entry.change);
148 if (change) {
149 change.last = at;
150 host.changes.set(entry.change, change);
151 }
152 }
153 break;
154 }
155 case "note-closed": {
156 const note = host.notes.get(entry.id);
157 if (note) {
158 note.closed = { agent: entry.agent, how: entry.how, time: entry.time };
159 host.notes.set(entry.id, note);
160 }
161 break;
162 }
163 case "pulled":
164 host.setQueued(host.queued().filter((q) => q.id !== entry.id));
165 host.uncheck(entry.id);
166 break;
167 case "starting": {
168 const agent = host.agentOf(entry.agent);
169 host.active(agent, entry.time);
170 if (entry.text === null) {
171 delete agent.starting;
172 delete agent.noteAt;
173 } else {
174 agent.starting = {
175 text: entry.text,
176 files: [...(entry.files ?? [])],
177 time: entry.time,
178 ...(entry.issue !== undefined ? { issue: entry.issue } : {}),
179 };
180 agent.noteAt = at;
181 }
182 break;
183 }
184 case "sync": {
185 const agent = host.agentOf(entry.agent);
186 agent.base = entry.version;
187 agent.synced = true;
188 // The version it moved to: the newest, or an older one with a label.
189 const set = host.at(host.spaceOf(agent), entry.version);
190 agent.mine = agent.mine.filter((id) => !set.has(id));
191 break;
192 }
193 case "news": {
194 const agent = host.agentOf(entry.agent);
195 agent.news.push(...entry.text);
196 agent.newsAt.push(...entry.text.map(() => time));
197 entry.pairs.forEach((p, i) => {
198 agent.told.add(p);
199 if (!p.startsWith("announce ")) return;
200 const id = Number(p.slice(9)) || 0;
201 host.setAnnounced(Math.max(host.announced(), id));
202 tellSent(host.tells, id, entry.text[i] ?? entry.text[0] ?? "", agent.name, time);
203 });
204 capNews(agent);
205 break;
206 }
207 case "discard": {
208 const agent = host.agentOf(entry.agent);
209 const gone = new Set(entry.atoms);
210 agent.mine = agent.mine.filter((id) => !gone.has(id));
211 if (entry.markers) agent.marked.push(...entry.markers.map((r) => ({ ...r, space: agent.space })));
212 break;
213 }
214 case "heard": {
215 const agent = host.agentOf(entry.agent);
216 // A sync's stale heard keeps announcements and settles' news (outlastsSync), so it hears no tell.
217 if (entry.stale !== true)
218 for (const tell of host.tells.values())
219 if (tell.heard.get(agent.name) === null) tell.heard.set(agent.name, { at: time });
220 const keep = agent.news.map((t) => entry.stale === true && outlastsSync(t));
221 agent.news = agent.news.filter((_, i) => keep[i]);
222 agent.newsAt = agent.newsAt.filter((_, i) => keep[i]);
223 break;
224 }
225 case "drop": {
226 const gone = new Set(entry.atoms);
227 // Agents keep their own published-but-unsynced atoms in `mine`; dropped ones leave.
228 for (const agent of host.agents.values()) {
229 agent.mine = agent.mine.filter((id) => !gone.has(id));
230 for (const p of agent.parked.values()) p.mine = p.mine.filter((id) => !gone.has(id));
231 }
232 break;
233 }
234 case "label": {
235 const list = (host.labels.get(entry.name) ?? []).filter((v) => v !== entry.version);
236 if (!entry.remove) list.push(entry.version);
237 list.sort((a, b) => a - b);
238 if (list.length > 0) host.labels.set(entry.name, list);
239 else host.labels.delete(entry.name);
240 break;
241 }
242 case "refused":
243 host.refusals.push({
244 agent: entry.agent,
245 draft: entry.space ?? null,
246 via: entry.via,
247 time: entry.time,
248 collisions: entry.collisions.map((c) => ({ ...c })),
249 ...(entry.message ? { message: entry.message } : {}),
250 });
251 {
252 // On the change the agent was working on, when there is one (#44).
253 const agent = host.agentOf(entry.agent);
254 host.active(agent, entry.time);
255 const id = host.currentChange(agent, entry.time);
256 const refusal = {
257 at,
258 time: entry.time,
259 kinds: [...new Set(entry.collisions.map((c) => c.kind))].sort(),
260 files: [...new Set(entry.collisions.map((c) => c.file))].sort(),
261 ...(entry.message ? { message: entry.message } : {}),
262 };
263 if (id !== undefined) {
264 const rec = host.changeRecord(id, agent, entry.time);
265 rec.refusals.push({ ...refusal, files: [...refusal.files], kinds: [...refusal.kinds] });
266 rec.last = at;
267 host.changes.set(id, rec);
268 }
269 // Kept until it pushes: the settle tells the held push's reason, and its change lists the refusal.
270 if (entry.via === "publish")
271 agent.held = [...(agent.held ?? []), { ...refusal, ...(id !== undefined ? { change: id } : {}) }];
272 }
273 break;
274 case "switch": {
275 const agent = host.agentOf(entry.agent);
276 const carry = entry.carry ?? true;
277 const draft = host.draft(agent);
278 const carried = new Set(carry ? draft : []);
279 agent.parked.set(agent.space, { base: agent.base, mine: agent.mine.filter((id) => !carried.has(id)) });
280 const back = carry ? undefined : agent.parked.get(entry.space);
281 agent.parked.delete(entry.space);
282 agent.space = entry.space;
283 agent.base = back ? back.base : entry.version;
284 agent.mine = back ? back.mine : [...carried];
285 agent.synced = true;
286 break;
287 }
288 case "land": {
289 host.atomAt[entry.tie.seq] = at;
290 host.graph.add(entry.tie);
291 host.joinChange(host.agentOf(entry.agent), entry, at, null, entry.space);
292 break;
293 }
294 case "amend":
295 case "space":
296 case "space-sync":
297 // Membership only: Membership.apply has done it.
298 break;
299 default: {
300 // A kind this engine doesn't know was written by a newer one. Skipping
301 // it would show, and then build on, a project without that entry, so
302 // the load stops instead.
303 const kind = (entry as { type?: unknown }).type;
304 throw new ProjectError(
305 "unknown-entry",
306 `log entry ${String(at)} is of kind ${JSON.stringify(kind)}, which this version of abso doesn't know: a newer version wrote it, so this one won't load the project`,
307 );
308 }
309 }
310}
312/** How many tells the project keeps who-heard-what for (status's tells). */
313export const TELLS_KEPT = 50;
315/** A tell's news reached one recipient's queue: the tell is kept, the oldest dropped past TELLS_KEPT. */
316function tellSent(
317 tells: Map<number, TellRecord>,
318 id: number,
319 text: string,
320 agent: string,
321 time: number | undefined,
322): void {
323 let tell = tells.get(id);
324 if (tell === undefined) {
325 tell = { id, text, at: time, heard: new Map() };
326 tells.set(id, tell);
327 for (const old of tells.keys()) {
328 if (tells.size <= TELLS_KEPT) break;
329 tells.delete(old);
330 }
331 }
332 if (!tell.heard.has(agent)) tell.heard.set(agent, null);
333}