- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/publish.ts?format=json
217 linesFirst import 89% · codex-capacity-chair 5% · speed 2% · autosettle 2% · rename 1% · infra-split 1%Last change autosettle in version 211, 7 versions changed it
1// Add the agent's draft to its space. Writes go through PublishHost; today
2// the host is Project. Ingest calls publishDraft, the inner step.
4import { type AtomId, buildAtom, type Hunk, joinLines, type ReadView } from "../../src/engine/index.ts";
5import { differenceSet, unionOverlay } from "../../src/engine/state-source.ts";
6import { autoSettledOf } from "./autosettle.ts";
7import type { Pair } from "./collisions.ts";
8import type { DefinitionCollision } from "./definition-rule.ts";
9import { heldMessage, keptOf, lostLines, settleLines } from "./markers.ts";
10import { ProjectError } from "./project-error.ts";
11import type { AgentState, Collision, Entry, Meta, Mode, PublishAnswer, Settled } from "./project-types.ts";
12import { last, SHARED, type Space } from "./spaces.ts";
14/** What publishing needs from a project. */
15export interface PublishHost {
16 readonly agents: ReadonlyMap<string, AgentState>;
17 nextAt(): number;
18 checkClash(changes: readonly AtomId[], ahead?: ReadonlySet<AtomId>): void;
19 queueOn(): boolean;
20 queuedAtoms(except?: string): Set<AtomId>;
21 agent(name: string, entries: Entry[]): AgentState;
22 draft(agent: AgentState): AtomId[];
23 spaceOf(agent: AgentState): Space;
24 strandedFiles(draft: readonly AtomId[], set: ReadonlySet<AtomId>): string[];
25 setOf(space: Space): ReadonlySet<AtomId>;
26 markedFiles(agent: AgentState): string[];
27 agentView(agent: AgentState): ReadView;
28 recordFile(
29 agent: AgentState,
30 view: ReadView,
31 path: string,
32 text: string | null | undefined,
33 time: number,
34 meta: Meta,
35 entries: Entry[],
36 mode?: Mode,
37 blob?: string,
38 hunks?: readonly Hunk[],
39 ): boolean;
40 expressMoves(agent: AgentState, time: number, entries: Entry[]): void;
41 pairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];
42 collision(agent: AgentState, pair: Pair): Collision;
43 definitionCollisions(agent: AgentState, newer: ReadonlySet<AtomId>): DefinitionCollision[];
44 duplicates(agent: AgentState): Collision[];
45 refuse(
46 agent: AgentState,
47 space: Space,
48 via: "publish" | "land",
49 time: number,
50 collisions: Collision[],
51 entries: Entry[],
52 message?: string,
53 ): { ok: false; collisions: Collision[] };
54 markerCollisions(agent: AgentState): Collision[];
55 enqueue(
56 agent: AgentState,
57 message: string,
58 time: number,
59 draft: readonly AtomId[],
60 resolves: readonly AtomId[],
61 entries: Entry[],
62 ): PublishAnswer;
63 view(set: ReadonlySet<AtomId>): ReadView;
64 spaceOverlay?(space: Space, atoms: readonly AtomId[]): ReadView;
65 requireClosed(tie: { deps: readonly AtomId[] }, union: ReadonlySet<AtomId>): void;
66 newerFor(agent: AgentState): ReadonlySet<AtomId>;
67 currentChange(agent: AgentState, time: number): number | undefined;
68 apply(entry: Entry): void;
69 newsForOthers(publisher: AgentState, space: Space, version: number, entries: Entry[]): void;
70 settledNews(agent: AgentState, version: number, settled: readonly Settled[], entries: Entry[]): void;
71 addressedNews(agent: AgentState, version: number, entries: Entry[]): void;
72 reviewNews(agent: AgentState, version: number, entries: Entry[]): void;
73 flush(entries: Entry[]): void;
74}
76/**
77 * Add the agent's draft to its space (shared, or its named draft). Refused
78 * only when a draft change collides with work published to that space
79 * after the agent's base.
80 */
81export function publish(
82 host: PublishHost,
83 agentName: string,
84 message: string,
85 time: number,
86 options: { asIs?: boolean } = {},
87): PublishAnswer {
88 if (typeof message !== "string") throw new ProjectError("bad-request", "push needs a message");
89 const known = host.agents.get(agentName);
90 if (known && known.space === SHARED)
91 host.checkClash(host.draft(known), host.queueOn() ? host.queuedAtoms(known.name) : undefined);
92 const entries: Entry[] = [];
93 const answer = publishDraft(host, host.agent(agentName, entries), message, time, entries, options.asIs === true);
94 host.flush(entries);
95 return answer;
96}
98/** The inner publish, also used when ingesting a git push. */
99export function publishDraft(
100 host: PublishHost,
101 agent: AgentState,
102 message: string,
103 time: number,
104 entries: Entry[],
105 asIs = false,
106 direct = false,
107): PublishAnswer {
108 const space = host.spaceOf(agent);
109 const stranded = host.strandedFiles(host.draft(agent), host.setOf(space));
110 if (stranded.length > 0) {
111 throw new ProjectError(
112 "conflict",
113 `your unpushed changes to ${stranded.join(", ")} build on lines a drop took out of shared; ` +
114 `restore those files (abso restore ${stranded.join(" ")}), pull, and make the change again`,
115 );
116 }
117 // asIs (#11): every block of markers keeps both sides, and the publish says which files it settled that way.
118 if (asIs) {
119 for (const file of host.markedFiles(agent)) {
120 const view = host.agentView(agent);
121 if (!view.hasFile(file)) continue;
122 const s = settleLines(
123 view.lines(file).map((l) => l.text),
124 "both",
125 );
126 if (s.blocks > 0) host.recordFile(agent, view, file, joinLines(s.lines), time, {}, entries);
127 }
128 }
129 host.expressMoves(agent, time, entries);
130 const draft = host.draft(agent);
131 if (draft.length === 0) return { ok: true, version: last(space), changes: 0 };
132 const now = host.setOf(space);
133 const union = unionOverlay(now, draft);
134 const newer = host.newerFor(agent);
135 const collisions = [
136 ...host.pairs(draft, newer, union).map((p) => host.collision(agent, p)),
137 ...host.definitionCollisions(agent, newer).map((d) => d.collision),
138 ...host.duplicates(agent),
139 ];
140 if (collisions.length > 0) return host.refuse(agent, space, "publish", time, collisions, entries, message);
141 // Settled means the markers are gone (#11): refused while the draft adds any.
142 const marked = host.markerCollisions(agent);
143 if (marked.length > 0) return host.refuse(agent, space, "publish", time, marked, entries);
144 // What the agent has seen collide (it synced past both sides) and not
145 // settled by an edit. Lines both sides changed, still untouched since the
146 // sync, are both in the file, and publishing that doubles them in shared
147 // (#37: twenty goals twice, shared red for three versions). So
148 // that is refused unless the agent says to publish as it is (asIs); then,
149 // like any other seen collision, publishing settles it.
150 const seen = host.pairs(draft, differenceSet(now, newer), union);
151 if (!asIs) {
152 const untouched = seen
153 .map((p) => host.collision(agent, p))
154 .filter((c) => c.kind === "lines")
155 .map((c) => ({ ...c, untouched: true as const }));
156 if (untouched.length > 0) return host.refuse(agent, space, "publish", time, untouched, entries);
157 }
158 const resolves = [...new Set(seen.flatMap((p) => [p.yours, p.theirs]))];
159 if (space.name === SHARED && !direct && host.queueOn())
160 return host.enqueue(agent, message, time, draft, resolves, entries);
161 const candidate = host.spaceOverlay ? host.spaceOverlay(space, draft) : host.view(union);
162 const tie = buildAtom(candidate, { author: agent.name, message, time, needs: draft, resolves });
163 host.requireClosed(tie, union);
164 const version = last(space) + 1;
165 const othersPublished = space.versions.slice(agent.base + 1).some((p) => p.agent !== agent.name);
166 // What this publish went on top of that the agent's folder doesn't have yet (#37).
167 const mergedWith = space.versions
168 .slice(agent.base + 1)
169 .filter((p) => p.agent !== agent.name && (p.atoms.length > 0 || p.dropped !== undefined))
170 .map((p) => ({ version: p.version, agent: p.agent, message: p.message }));
171 // What each marked collision kept, judged from the file as published (#11).
172 const view = host.agentView(agent);
173 const auto = autoSettledOf(message);
174 const settled: Settled[] = agent.marked
175 .filter((r) => r.space === space.name)
176 .map((r) => {
177 const lines = view.hasFile(r.file) ? view.lines(r.file).map((l) => l.text) : [];
178 const lost = lostLines(lines, r);
179 const held = heldMessage(agent, r.file);
180 const by = auto.get(r.file);
181 return {
182 file: r.file,
183 kept: keptOf(lines, r),
184 theirs: { agent: r.agent, version: r.version },
185 ...(lost.length > 0 ? { lost } : {}),
186 ...(held ? { held } : {}),
187 ...(by ? { auto: by } : {}),
188 };
189 });
190 entries.push({
191 type: "publish",
192 agent: agent.name,
193 ...(space.name !== SHARED ? { space: space.name } : {}),
194 version,
195 message,
196 time,
197 changes: draft,
198 tie,
199 rebase: !othersPublished,
200 ...(settled.length > 0 ? { settled } : {}),
201 change: host.currentChange(agent, time) ?? host.nextAt(),
202 });
203 host.apply(entries[entries.length - 1]!);
204 host.newsForOthers(agent, space, version, entries);
205 host.settledNews(agent, version, settled, entries);
206 if (space.name === SHARED) {
207 host.addressedNews(agent, version, entries);
208 host.reviewNews(agent, version, entries);
209 }
210 return {
211 ok: true,
212 version,
213 changes: draft.length,
214 ...(mergedWith.length > 0 ? { mergedWith } : {}),
215 ...(settled.length > 0 ? { settled } : {}),
216 };
217}