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