- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/switch-to.ts?version=100&format=json
78 linesFirst import 99% · rename 1%Last change rename in version 100, 2 versions changed it
You're reading version 100. The newest is 318, and 1 version since then changed this file.
1// Move an agent to a named draft, or back to shared. Writes go through
2// SwitchHost; today the host is Project.
4import { type AtomId, type Graph, View } from "../../src/engine/index.ts";
5import { ProjectError } from "./project-error.ts";
6import { checkDraftName } from "./project-helpers.ts";
7import type { AgentState, Entry, SwitchAnswer, SyncAnswer } from "./project-types.ts";
8import { last, SHARED, type Space } from "./spaces.ts";
10/** What switching an agent's draft needs from a project. */
11export interface SwitchHost {
12 agent(name: string, entries: Entry[]): AgentState;
13 flush(entries: Entry[]): void;
14 readonly spaces: ReadonlyMap<string, Space>;
15 agentView(agent: AgentState): View;
16 readonly graph: Graph;
17 version(): number;
18 apply(entry: Entry): void;
19 draft(agent: AgentState): readonly AtomId[];
20 setOf(space: Space): ReadonlySet<AtomId>;
21 changesSince(agent: AgentState, before: View): SyncAnswer;
22}
24/**
25 * Move the agent to a named draft, or back to shared with null. Its
26 * unpublished changes stay parked on the server in the space it leaves,
27 * and what it parked in the space it moves to comes back. Nothing is lost
28 * and nothing has to be published or stashed first. A new named draft
29 * starts on shared's newest version; with `carry`, the agent's current
30 * unpublished changes move into it instead of being parked. Answers like a
31 * sync: the files that change in its folder.
32 */
33export function switchTo(
34 host: SwitchHost,
35 agentName: string,
36 draftName: string | null,
37 time: number,
38 options: { carry?: boolean } = {},
39): SwitchAnswer {
40 const entries: Entry[] = [];
41 const agent = host.agent(agentName, entries);
42 const target = draftName === null ? SHARED : checkDraftName(draftName);
43 const carry = options.carry === true;
44 const name = target === SHARED ? null : target;
45 if (target === agent.space && !carry) {
46 host.flush(entries);
47 return { version: agent.base, files: {}, collisions: [], draft: name };
48 }
49 if (carry && host.spaces.has(target)) {
50 throw new ProjectError(
51 "bad-request",
52 `carrying changes starts a new named draft, and ${name ?? "shared"} already exists`,
53 );
54 }
55 const before = agent.synced ? host.agentView(agent) : new View(host.graph, []);
56 if (!host.spaces.has(target)) {
57 entries.push({ type: "space", name: target, agent: agent.name, time, sharedBase: host.version() });
58 host.apply(entries[entries.length - 1]!);
59 }
60 const space = host.spaces.get(target)!;
61 if (carry) {
62 const draft = host.draft(agent);
63 const into = new Set([...host.setOf(space), ...draft]);
64 for (const id of draft) {
65 if (host.graph.atom(id).deps.some((d) => !into.has(d))) {
66 throw new ProjectError(
67 "bad-request",
68 `your unpushed changes build on changes that shared doesn't have yet (they're in draft ${agent.space}); push them there first, or switch without carrying`,
69 );
70 }
71 }
72 }
73 entries.push({ type: "switch", agent: agent.name, space: target, version: last(space), carry });
74 host.apply(entries[entries.length - 1]!);
75 const answer = host.changesSince(agent, before);
76 host.flush(entries);
77 return { ...answer, version: agent.base, draft: name };
78}