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/change-records.ts?format=json
202 linesFirst import 93% · speed 5% · codex-capacity-chair 1% · infra-split 1%Last change speed in version 160, 4 versions changed it
1// Changes (#44): one start note, named draft or publish, its versions, and the list since you last
2// looked. Writes (the record a publish joins) and reads go through ChangeHost; today the host is Project.
3
4import type { Graph } from "../../src/engine/index.ts";
5import type { PathIndex } from "./pathindex.ts";
6import { ProjectError } from "./project-error.ts";
7import type {
8 AgentState,
9 ChangeAnswer,
10 ChangeRecord,
11 Entry,
12 NoteAnswer,
13 NoteRecord,
14 QueuedPublish,
15 Starting,
16} from "./project-types.ts";
17import { last, type Space } from "./spaces.ts";
19/** What the change reads and the record a publish joins need from a project. */
20export interface ChangeHost {
21 readonly changes: Map<number, ChangeRecord>;
22 readonly agents: ReadonlyMap<string, AgentState>;
23 readonly spaces: ReadonlyMap<string, Space>;
24 readonly notes: ReadonlyMap<number, NoteRecord>;
25 readonly versionChange: Map<string, number>;
26 readonly paths: Pick<PathIndex, "versionFiles">;
27 readonly graph: Graph;
28 stored(): number;
29 shared(): Space;
30 queue(): readonly QueuedPublish[];
31 active(agent: AgentState, time: number): void;
32 currentChange(agent: AgentState, time: number): number | undefined;
33 liveNote(agent: AgentState): Starting | undefined;
34 noteAnswer(n: NoteRecord): NoteAnswer;
35 reviewersOf(files: readonly string[], id: number): { agent: string; files: string[]; read: boolean }[];
38/** The files a version's changes name: cheap, for lists (the change page works out the net changes). */
39export function namedFiles(graph: Graph, space: Space, version: number): string[] {
40 const p = space.versions[version]!;
41 return [...new Set([...p.atoms, ...(p.dropped ?? [])].flatMap((id) => graph.filesOf(id)))].toSorted();
44export function changeRecord(
45 host: ChangeHost,
46 id: number,
47 agent: AgentState,
48 time: number,
49 queuedNote?: Starting,
50): ChangeRecord {
51 let rec = host.changes.get(id);
52 if (!rec) {
53 const draft = [...host.spaces.values()].find((s) => s.startedAt === id)?.name ?? null;
54 const noted =
55 draft === null ? (queuedNote ?? (id === agent.noteAt && agent.starting ? agent.starting : undefined)) : undefined;
56 rec = {
57 id,
58 agent: agent.name,
59 kind: draft !== null ? "draft" : noted ? "note" : "publish",
60 ...(noted ? { note: { text: noted.text, files: [...noted.files] } } : {}),
61 ...(noted?.issue !== undefined ? { issue: noted.issue } : {}),
62 draft,
63 time: noted?.time ?? time,
64 versions: [],
65 settled: [],
66 refusals: [],
67 last: id,
68 };
69 host.changes.set(id, rec);
70 }
71 // A start note made later inside a named draft gives the draft's change its text.
72 if (rec.kind === "draft" && !rec.note && agent.starting && agent.noteAt !== undefined && agent.noteAt > id) {
73 rec.note = { text: agent.starting.text, files: [...agent.starting.files] };
74 if (agent.starting.issue !== undefined) rec.issue = agent.starting.issue;
75 }
76 host.changes.set(id, rec);
77 return rec;
80/** A publish or land joins its change. Entries from before changes: the start note named it, and the publish ended the note. */
81export function joinChange(
82 host: ChangeHost,
83 agent: AgentState,
84 entry: Extract<Entry, { type: "publish" | "land" }>,
85 at: number,
86 draft: string | null,
87 landed?: string,
88): void {
89 // A queued publish lands when the checker says so, not when the agent acts (#39).
90 const queued =
91 entry.type === "publish" && entry.queued !== undefined
92 ? host.queue().find((q) => q.id === entry.queued)
93 : undefined;
94 if (!queued) host.active(agent, entry.time);
95 let id = entry.change;
96 const old = id === undefined;
97 if (id === undefined)
98 id =
99 landed !== undefined ? (host.spaces.get(landed)?.startedAt ?? at) : (host.currentChange(agent, entry.time) ?? at);
100 const rec = changeRecord(host, id, agent, entry.time, queued?.note);
101 rec.versions.push({ version: entry.version, draft, message: entry.message, time: entry.time, at });
102 host.versionChange.set(`${draft ?? ""} ${entry.version}`, id);
103 if (entry.type === "publish") for (const s of entry.settled ?? []) rec.settled.push({ ...s, version: entry.version });
104 // Pushes held on the way, when no change was going to put them on: they belong to the push that follows.
105 if (entry.type === "publish")
106 for (const { change: on, ...h } of agent.held ?? [])
107 if (on === undefined && !rec.refusals.some((r) => r.at === h.at))
108 rec.refusals.push({ ...h, kinds: [...h.kinds], files: [...h.files] });
109 rec.last = at;
110 host.changes.set(id, rec);
111 if (old) {
112 delete agent.starting;
113 delete agent.noteAt;
114 }
117export function changeAnswer(host: ChangeHost, rec: ChangeRecord, withFiles: boolean): ChangeAnswer {
118 const versions = rec.versions.map((v) => {
119 const space = v.draft === null ? host.shared() : host.spaces.get(v.draft);
120 const files =
121 !space || v.version > last(space)
122 ? []
123 : withFiles
124 ? host.paths.versionFiles(space, v.version)
125 : namedFiles(host.graph, space, v.version);
126 return { version: v.version, draft: v.draft, message: v.message, time: v.time, files };
127 });
128 const agent = host.agents.get(rec.agent);
129 const open =
130 rec.kind === "draft"
131 ? host.spaces.has(rec.draft!) && !rec.versions.some((v) => v.draft === null)
132 : rec.kind === "note" && !!agent && agent.noteAt === rec.id && !!host.liveNote(agent);
133 return {
134 id: rec.id,
135 agent: rec.agent,
136 title: rec.note?.text ?? (rec.kind === "draft" ? `Draft ${rec.draft}` : (rec.versions[0]?.message ?? "")),
137 kind: rec.kind,
138 note: rec.note ? { text: rec.note.text, files: [...rec.note.files] } : null,
139 issue: rec.issue ?? null,
140 draft: rec.draft,
141 time: rec.time,
142 open,
143 versions,
144 files: [...new Set(versions.flatMap((v) => v.files))].toSorted(),
145 settled: rec.settled.map((s) => ({ ...s })),
146 refusals: rec.refusals.map(({ time, kinds, files, message }) => ({
147 time,
148 kinds: [...kinds],
149 files: [...files],
150 ...(message ? { message } : {}),
151 })),
152 notes: [...host.notes.values()].filter((n) => n.change === rec.id).map((n) => host.noteAnswer(n)),
153 reviewers: host.reviewersOf(
154 versions.flatMap((v) => v.files),
155 rec.id,
156 ),
157 };
160/**
161 * One change (#44): the start note, named draft or publish that began it,
162 * each of its versions and the files they changed, what it settled, and its
163 * refusals. `id` is the log position of the entry that began it.
164 */
165export function change(host: ChangeHost, id: number): ChangeAnswer {
166 const rec = host.changes.get(id);
167 if (!rec) throw new ProjectError("not-found", `there is no change ${String(id)}`);
168 return changeAnswer(host, rec, true);
171/**
172 * "Since you last looked" (#44): the changes with anything new at or after
173 * log position `since`, newest first, at most `limit`. Their files are the
174 * ones their changes name; the change page has the net changes.
175 */
176export function changes(
177 host: ChangeHost,
178 since = 0,
179 limit = 50,
180 before?: number,
181 issue?: number,
182): { since: number; next: number; changes: ChangeAnswer[]; older?: number } {
183 const noted = new Set([...host.notes.values()].filter((n) => n.change !== undefined).map((n) => n.change!));
184 // `before` pages back (#44): changes whose newest entry is before that log position.
185 // `issue` (#54): only the changes whose start note named it, begun or not.
186 const found = [...host.changes.values()]
187 .filter(
188 (c) =>
189 c.last >= since &&
190 (before === undefined || c.last < before) &&
191 (issue === undefined ? c.versions.length > 0 || c.refusals.length > 0 || noted.has(c.id) : c.issue === issue),
192 )
193 .toSorted((a, b) => b.last - a.last);
194 const page = found.slice(0, Math.max(1, Math.min(limit, 200)));
195 const older = found.length > page.length ? page[page.length - 1]!.last : undefined;
196 return {
197 since,
198 next: host.stored(),
199 changes: page.map((c) => changeAnswer(host, c, false)),
200 ...(older !== undefined ? { older } : {}),
201 };