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/amend.ts?format=json
59 linesFirst import 98% · rename 2%Last change rename in version 100, 2 versions changed it
1// Correct the message of the agent's newest publish in its space. Writes go
2// through AmendHost; today the host is Project.
3
4import { ProjectError } from "./project-error.ts";
5import type { AgentState, Entry } from "./project-types.ts";
6import { last, SHARED, type Space } from "./spaces.ts";
7
8/** What amending a publish message needs from a project. */
9export interface AmendHost {
10 agent(name: string, entries: Entry[]): AgentState;
11 spaceOf(agent: AgentState): Space;
12 apply(entry: Entry): void;
13 flush(entries: Entry[]): void;
16/**
17 * Correct the message of the agent's newest publish in its space (#27).
18 * Only the message changes: shared only grows, so a version's changes are
19 * fixed by publishing again (or dropping it). The log keeps the original.
20 */
21export function amend(
22 host: AmendHost,
23 agentName: string,
24 message: string,
25 time: number,
26): { version: number; draft: string | null; message: string; was: string } {
27 if (typeof message !== "string" || message.trim() === "")
28 throw new ProjectError("bad-request", "amend needs the new message");
29 const entries: Entry[] = [];
30 const agent = host.agent(agentName, entries);
31 const space = host.spaceOf(agent);
32 // Its newest version that published something (not a draft's bookkeeping versions).
33 const mineAt = (v: number): boolean =>
34 space.versions[v]!.agent === agent.name &&
35 (space.versions[v]!.atoms.length > 0 || space.versions[v]!.dropped !== undefined);
36 let version = last(space);
37 while (version > 0 && !mineAt(version)) version--;
38 const p = space.versions[version]!;
39 if (version === 0) {
40 host.flush(entries);
41 throw new ProjectError(
42 "bad-request",
43 `${agent.name} hasn't pushed in ${space.name === SHARED ? "shared" : `draft ${space.name}`}, so there is nothing to amend`,
44 );
45 }
46 const was = p.message;
47 const entry: Entry = {
48 type: "amend",
49 agent: agent.name,
50 ...(space.name !== SHARED ? { space: space.name } : {}),
51 version,
52 message,
53 time,
54 };
55 entries.push(entry);
56 host.apply(entry);
57 host.flush(entries);
58 return { version, draft: space.name === SHARED ? null : space.name, message, was };