Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

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