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