- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/pluck.ts?version=316&format=json
109 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 316. The newest is 318, and no version since then changed this file.
1// Take part of a named draft into shared (#27): some of its versions as one new version of shared.
2// Writes go through PluckHost; today the host is Project.
4import type { AtomId, Graph } from "../../src/engine/index.ts";
5import { ProjectError } from "./project-error.ts";
6import { checkDraftName } from "./project-helpers.ts";
7import type { AgentState, Entry, LandAnswer, PluckAnswer } from "./project-types.ts";
8import { last, SHARED, type Space } from "./spaces.ts";
10/** What plucking versions of a named draft needs from a project. */
11export interface PluckHost {
12 readonly graph: Graph;
13 readonly spaces: ReadonlyMap<string, Space>;
14 agent(name: string, entries: Entry[]): AgentState;
15 shared(): Space;
16 notQueuedYet(what: string): void;
17 strandedFiles(draft: readonly AtomId[], set: ReadonlySet<AtomId>): string[];
18 checkClash(changes: readonly AtomId[]): void;
19 land(
20 agent: AgentState,
21 message: string,
22 time: number,
23 entries: Entry[],
24 pick?: { space: Space; changes: AtomId[] },
25 ): LandAnswer;
26 flush(entries: Entry[]): void;
27}
29/**
30 * Take part of a named draft into shared (#27): the changes of some of its
31 * versions, as one new version of shared, checked for collisions like a
32 * land. When they build on other versions of the draft, those are listed
33 * and it is refused, unless `withDependencies` takes them too. The draft
34 * stays as it is; landing it later brings in only what's left.
35 */
36export function pluck(
37 host: PluckHost,
38 agentName: string,
39 draftName: string,
40 versions: readonly number[],
41 message: string,
42 time: number,
43 options: { withDependencies?: boolean } = {},
44): PluckAnswer {
45 if (typeof message !== "string" || message.trim() === "")
46 throw new ProjectError("bad-request", "pluck needs a message saying why");
47 if (!Array.isArray(versions) || versions.length === 0)
48 throw new ProjectError("bad-request", "pluck needs {versions: [number]}");
49 host.notQueuedYet("pluck");
50 const space = host.spaces.get(checkDraftName(draftName));
51 if (!space || space.name === SHARED)
52 throw new ProjectError("bad-request", `there is no named draft ${JSON.stringify(draftName)}`);
53 const entries: Entry[] = [];
54 const agent = host.agent(agentName, entries);
55 const shared = host.shared();
56 const g = host.graph;
57 const pending = (id: AtomId): boolean => !shared.own.has(id) && !shared.droppedIn.has(id);
58 const chosen = new Set<AtomId>();
59 for (const v of versions) {
60 if (!Number.isInteger(v) || v < 1 || v > last(space))
61 throw new ProjectError("bad-request", `draft ${space.name} has no version ${String(v)} to pluck`);
62 const atoms = space.versions[v]!.atoms.filter(pending);
63 if (atoms.length === 0)
64 throw new ProjectError("bad-request", `draft ${space.name} version ${v} has nothing that shared doesn't have`);
65 for (const id of atoms) chosen.add(id);
66 }
67 // What they build on in the draft that shared doesn't have yet.
68 const stack = [...chosen];
69 const needed = new Set<AtomId>();
70 while (stack.length > 0) {
71 for (const d of g.atom(stack.pop()!).deps) {
72 if (chosen.has(d) || needed.has(d) || !space.own.has(d) || !pending(d)) continue;
73 needed.add(d);
74 stack.push(d);
75 }
76 }
77 const asked = new Set(versions);
78 const neededVersions = [...new Set([...needed].map((id) => space.publishedIn.get(id)!))]
79 .filter((v) => !asked.has(v))
80 .toSorted((a, b) => a - b);
81 const describe = (v: number) => ({
82 version: v,
83 agent: space.versions[v]!.agent,
84 message: space.versions[v]!.message,
85 });
86 if (neededVersions.length > 0 && options.withDependencies !== true) {
87 host.flush(entries);
88 return { ok: false, needs: neededVersions.map(describe), collisions: [] };
89 }
90 const stranded = host.strandedFiles([...chosen, ...needed], shared.own);
91 if (stranded.length > 0) {
92 host.flush(entries);
93 throw new ProjectError(
94 "conflict",
95 `those changes to ${stranded.join(", ")} build on lines a drop took out of shared`,
96 );
97 }
98 const all = [...versions, ...neededVersions].toSorted((a, b) => a - b);
99 try {
100 host.checkClash([...chosen, ...needed]);
101 } catch (e) {
102 host.flush(entries);
103 throw e;
104 }
105 const answer = host.land(agent, message, time, entries, { space, changes: [...chosen, ...needed] });
106 host.flush(entries);
107 if (!answer.ok) return { ok: false, needs: [], collisions: answer.collisions };
108 return { ok: true, version: answer.version, changes: answer.changes ?? 0, plucked: all.map(describe) };
109}