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/sync.ts?format=json
97 linesFirst import 81% · codex-capacity-chair 12% · rename 5% · trials 1%Last change codex-capacity-chair in version 163, 6 versions changed it
1import { View } from "../../src/engine/index.ts";
2// Move the agent's base to its space's newest version. Writes go through
3// SyncHost; today the host is Project.
4
5import type { AtomId, Graph, ReadView } from "../../src/engine/index.ts";
6import { ProjectError } from "./project-error.ts";
7import type { AgentState, Entry, MergedLine, SyncAnswer } from "./project-types.ts";
8import { last, SHARED, type Space } from "./spaces.ts";
9
10/** What a sync needs from a project. */
11export interface SyncHost {
12 readonly graph: Graph;
13 readonly labels: ReadonlyMap<string, readonly number[]>;
14 agent(name: string, entries: Entry[]): AgentState;
15 spaceOf(agent: AgentState): Space;
16 agentView(agent: AgentState): ReadView;
17 captureAgentView?(agent: AgentState): () => ReadView;
18 at(space: Space, version: number): ReadonlySet<AtomId>;
19 draft(agent: AgentState): AtomId[];
20 strandedFiles(draft: readonly AtomId[], set: ReadonlySet<AtomId>): string[];
21 apply(entry: Entry): void;
22 flush(entries: Entry[]): void;
23 mergeIdentical(
24 agent: AgentState,
25 before: ReadView,
26 oldBase: ReadonlySet<AtomId>,
27 oldVersion: number,
28 time: number,
29 entries: Entry[],
30 ): MergedLine[];
31 changesSince(agent: AgentState, before: ReadView): SyncAnswer;
34/**
35 * Move the agent's base to its space's newest version. Returns every file
36 * whose text in its view changes (null when it's gone), and the collisions
37 * it now has to settle. A first sync returns every file (that's how a
38 * folder joins).
39 */
40export function sync(host: SyncHost, agentName: string, options: { label?: string; full?: boolean } = {}): SyncAnswer {
41 const entries: Entry[] = [];
42 const agent = host.agent(agentName, entries);
43 const space = host.spaceOf(agent);
44 let target = last(space);
45 if (options.label !== undefined) {
46 // Follow a label (#10): the newest version of shared that carries it.
47 if (space.name !== SHARED)
48 throw new ProjectError("bad-request", "labels mark versions of shared; switch to shared to follow one");
49 const versions = host.labels.get(options.label);
50 if (!versions)
51 throw new ProjectError("bad-request", `no version of shared has the label ${JSON.stringify(options.label)}`);
52 target = versions[versions.length - 1]!;
53 }
54 const reopenBefore = agent.synced ? host.captureAgentView?.(agent) : undefined;
55 const before = agent.synced ? host.agentView(agent) : new View(host.graph, []);
56 const set = host.at(space, target);
57 const stranded = host.strandedFiles(host.draft(agent), set);
58 if (stranded.length > 0) {
59 host.flush(entries);
60 throw new ProjectError(
61 "conflict",
62 target === last(space)
63 ? `your unpushed changes to ${stranded.join(", ")} build on lines a drop took out of shared; ` +
64 `restore those files (abso restore ${stranded.join(" ")}) and pull again`
65 : `your unpushed changes to ${stranded.join(", ")} build on lines version ${target} doesn't have yet; ` +
66 `push or restore them before following label ${options.label}`,
67 );
68 }
69 // Moving back to a labeled version: the agent's own newer published work must not build on what that version lacks.
70 const mineAfter = agent.mine.filter((id) => !set.has(id));
71 const lacking = host.strandedFiles(mineAfter, set);
72 if (lacking.length > 0) {
73 host.flush(entries);
74 throw new ProjectError(
75 "conflict",
76 `your pushed changes to ${lacking.join(", ")} build on versions after ${target}, so you can't move back to it`,
77 );
78 }
79 const oldBase = host.at(space, agent.base);
80 const oldVersion = agent.base;
81 entries.push({ type: "sync", agent: agent.name, version: target });
82 host.apply(entries[entries.length - 1]!);
83 // News about versions the agent has now synced past is moot: the sync answer lists what's still open (#37).
84 if (agent.news.some((t) => !t.startsWith("Announcement from "))) {
85 const heard: Entry = { type: "heard", agent: agent.name, stale: true };
86 entries.push(heard);
87 host.apply(heard);
88 }
89 const merged = host.mergeIdentical(agent, before, oldBase, oldVersion, Date.now(), entries);
90 // full (#37, a partial join): every file in the view, as for a first sync, whatever the folder had.
91 const answer = host.changesSince(
92 agent,
93 options.full ? new View(host.graph, []) : reopenBefore ? reopenBefore() : before,
94 );
95 host.flush(entries);
96 return merged.length > 0 ? { ...answer, merged } : answer;