- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/drop.ts?format=json
143 linesFirst import 99% · rename 1%Last change rename in version 100, 2 versions changed it
1// Drop published versions of shared (#10): a new shared version that leaves their changes out.
2// Writes go through DropHost; today the host is Project.
4import type { AtomId, Graph } from "../../src/engine/index.ts";
5import type { NewsItem } from "./news.ts";
6import { ProjectError } from "./project-error.ts";
7import type { AgentState, DropAnswer, Entry } from "./project-types.ts";
8import { last, SHARED, type Space } from "./spaces.ts";
10/** What dropping versions needs from a project. */
11export interface DropHost {
12 readonly graph: Graph;
13 readonly agents: ReadonlyMap<string, AgentState>;
14 readonly spaces: ReadonlyMap<string, Space>;
15 agent(name: string, entries: Entry[]): AgentState;
16 shared(): Space;
17 flush(entries: Entry[]): void;
18 apply(entry: Entry): void;
19 queueNews(agent: AgentState, items: NewsItem[], entries: Entry[]): void;
20 addressedNews(agent: AgentState, version: number, entries: Entry[]): void;
21}
23/**
24 * Drop published versions of shared (#10): a new shared version whose set
25 * leaves out their changes. Later work that doesn't depend on them stays;
26 * history keeps them. When later published work built on them, they are
27 * refused with that work listed, unless `withDependents`, which drops it
28 * too. Unpublished work that builds on them (an agent's draft, a named
29 * draft not yet landed) always refuses it: drop never touches it.
30 */
31export function drop(
32 host: DropHost,
33 agentName: string,
34 versions: readonly number[],
35 message: string,
36 time: number,
37 options: { withDependents?: boolean } = {},
38): DropAnswer {
39 if (typeof message !== "string" || message.trim() === "")
40 throw new ProjectError("bad-request", "drop needs a message saying why");
41 if (!Array.isArray(versions) || versions.length === 0)
42 throw new ProjectError("bad-request", "drop needs {versions: [number]}");
43 const entries: Entry[] = [];
44 const agent = host.agent(agentName, entries);
45 const shared = host.shared();
46 const g = host.graph;
47 const targets = new Set<AtomId>();
48 for (const v of versions) {
49 if (!Number.isInteger(v) || v < 1 || v > last(shared))
50 throw new ProjectError(
51 "bad-request",
52 `there is no version ${String(v)} of shared to drop (version 0 is the start)`,
53 );
54 const p = shared.versions[v]!;
55 if (p.atoms.length === 0)
56 throw new ProjectError("bad-request", `version ${v} added nothing (it is a drop), so there is nothing to drop`);
57 const left = p.atoms.filter((id) => shared.own.has(id));
58 if (left.length === 0)
59 throw new ProjectError(
60 "bad-request",
61 `version ${v} was already dropped in version ${shared.droppedIn.get(p.atoms[0]!)}`,
62 );
63 for (const id of left) targets.add(id);
64 }
65 // Everything in shared that builds on them.
66 const gone = new Set(targets);
67 const stack = [...targets];
68 while (stack.length > 0) {
69 for (const d of g.dependentsOf(stack.pop()!)) {
70 if (shared.own.has(d) && !gone.has(d)) {
71 gone.add(d);
72 stack.push(d);
73 }
74 }
75 }
76 const asked = new Set(versions);
77 const dependents = [...new Set([...gone].map((id) => shared.publishedIn.get(id)!))]
78 .filter((v) => !asked.has(v))
79 .toSorted((a, b) => a - b);
80 const describe = (v: number) => ({
81 version: v,
82 agent: shared.versions[v]!.agent,
83 message: shared.versions[v]!.message,
84 });
85 if (dependents.length > 0 && options.withDependents !== true) {
86 host.flush(entries);
87 return { ok: false, dependents: dependents.map(describe), blocked: [] };
88 }
89 // Unpublished work that builds on them.
90 const blocked: { agent?: string; draft?: string; files: string[] }[] = [];
91 const buildsOn = (ids: Iterable<AtomId>): string[] => {
92 const files = new Set<string>();
93 for (const id of ids) {
94 if (gone.has(id) || shared.own.has(id)) continue;
95 if (g.atom(id).deps.some((d) => gone.has(d))) for (const f of g.filesOf(id)) files.add(f);
96 }
97 return [...files].toSorted();
98 };
99 for (const a of [...host.agents.values()].toSorted((x, y) => (x.name < y.name ? -1 : 1))) {
100 const files = buildsOn([...a.mine, ...[...a.parked.values()].flatMap((p) => p.mine)]);
101 if (files.length > 0) blocked.push({ agent: a.name, files });
102 }
103 for (const space of [...host.spaces.values()]
104 .filter((s) => s.name !== SHARED)
105 .toSorted((x, y) => (x.name < y.name ? -1 : 1))) {
106 const files = buildsOn(space.own);
107 if (files.length > 0) blocked.push({ draft: space.name, files });
108 }
109 if (blocked.length > 0) {
110 host.flush(entries);
111 return { ok: false, dependents: dependents.map(describe), blocked };
112 }
113 const version = last(shared) + 1;
114 const all = [...versions, ...dependents].toSorted((a, b) => a - b);
115 const entry: Entry = {
116 type: "drop",
117 agent: agent.name,
118 version,
119 message,
120 time,
121 versions: all,
122 atoms: [...gone].toSorted((p, q) => g.atom(p).seq - g.atom(q).seq),
123 };
124 entries.push(entry);
125 host.apply(entry);
126 const names = all.map((v) => `version ${v}`).join(", ");
127 for (const other of [...host.agents.values()].toSorted((x, y) => (x.name < y.name ? -1 : 1))) {
128 if (other === agent || other.space !== SHARED) continue;
129 host.queueNews(
130 other,
131 [
132 {
133 key: `drop ${version}`,
134 text: `${agent.name} dropped ${names} from shared (version ${version}): "${message.trim()}". Run abso pull to take it out of your folder.`,
135 },
136 ],
137 entries,
138 );
139 }
140 host.addressedNews(agent, version, entries);
141 host.flush(entries);
142 return { ok: true, version, dropped: all.map(describe) };
143}