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