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?version=162&format=json
93 linesFirst import 86% · codex-capacity-chair 5% · rename 5% · speed 2% · trials 1%Last change speed in version 161, 5 versions changed it

You're reading version 162. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

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 { outlastsSync } from "./news.ts";
7import { ProjectError } from "./project-error.ts";
8import type { AgentState, Entry, MergedLine, SyncAnswer } from "./project-types.ts";
9import { last, SHARED, type Space } from "./spaces.ts";
11/** What a sync needs from a project. */
12export interface SyncHost {
13 readonly graph: Graph;
14 readonly labels: ReadonlyMap<string, readonly number[]>;
15 agent(name: string, entries: Entry[]): AgentState;
16 spaceOf(agent: AgentState): Space;
17 agentView(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 before = agent.synced ? host.agentView(agent) : new View(host.graph, []);
55 const set = host.at(space, target);
56 const stranded = host.strandedFiles(host.draft(agent), set);
57 if (stranded.length > 0) {
58 host.flush(entries);
59 throw new ProjectError(
60 "conflict",
61 target === last(space)
62 ? `your unpushed changes to ${stranded.join(", ")} build on lines a drop took out of shared; ` +
63 `restore those files (abso restore ${stranded.join(" ")}) and pull again`
64 : `your unpushed changes to ${stranded.join(", ")} build on lines version ${target} doesn't have yet; ` +
65 `push or restore them before following label ${options.label}`,
66 );
67 }
68 // Moving back to a labeled version: the agent's own newer published work must not build on what that version lacks.
69 const mineAfter = agent.mine.filter((id) => !set.has(id));
70 const lacking = host.strandedFiles(mineAfter, set);
71 if (lacking.length > 0) {
72 host.flush(entries);
73 throw new ProjectError(
74 "conflict",
75 `your pushed changes to ${lacking.join(", ")} build on versions after ${target}, so you can't move back to it`,
76 );
77 }
78 const oldBase = host.at(space, agent.base);
79 const oldVersion = agent.base;
80 entries.push({ type: "sync", agent: agent.name, version: target });
81 host.apply(entries[entries.length - 1]!);
82 // News about versions the agent has now synced past is moot: the sync answer lists what's still open (#37).
83 if (agent.news.some((t) => !outlastsSync(t))) {
84 const heard: Entry = { type: "heard", agent: agent.name, stale: true };
85 entries.push(heard);
86 host.apply(heard);
87 }
88 const merged = host.mergeIdentical(agent, before, oldBase, oldVersion, Date.now(), entries);
89 // full (#37, a partial join): every file in the view, as for a first sync, whatever the folder had.
90 const answer = host.changesSince(agent, options.full ? new View(host.graph, []) : before);
91 host.flush(entries);
92 return merged.length > 0 ? { ...answer, merged } : answer;