Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/land.ts?version=316&format=json
168 linesFirst import 72% · codex-capacity-chair 18% · capacity 10%Last change capacity in version 316, 4 versions changed it

You're reading version 316. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// Land a named draft in shared as one unit. Writes go through LandHost; today
2// the host is Project. Ingest and pluck call landDraft, the inner step.
3
4import { type AtomId, buildAtom, type Graph, type ReadView } from "../../src/engine/index.ts";
5import { differenceSet, StableOverlaySet, unionOverlay } from "../../src/engine/state-source.ts";
6import type { Pair } from "./collisions.ts";
7import type { DefinitionRule } from "./definition-rule.ts";
8import { ProjectError } from "./project-error.ts";
9import type { AgentState, Collision, Entry, LandAnswer } from "./project-types.ts";
10import { SHARED, SharedAdditions, type Space, sharedBaseOf } from "./spaces.ts";
12/** What landing a named draft needs from a project. */
13export interface LandHost {
14 readonly agents: ReadonlyMap<string, AgentState>;
15 readonly graph: Graph;
16 readonly spaces: ReadonlyMap<string, Space>;
17 version(): number;
18 nextAt(): number;
19 notQueuedYet(what: string): void;
20 checkClash(changes: readonly AtomId[]): void;
21 agent(name: string, entries: Entry[]): AgentState;
22 namedSpaceOf(agent: AgentState): Space;
23 shared(): Space;
24 setOf(space: Space): ReadonlySet<AtomId>;
25 at(space: Space, version: number): ReadonlySet<AtomId>;
26 view(set: ReadonlySet<AtomId>): ReadView;
27 viewAt(space: Space, version: number, changes?: readonly AtomId[]): ReadView;
28 sharedView(): ReadView;
29 pairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];
30 describe(pair: Pair, yoursView: ReadView, theirsView: ReadView, spaceName: string): Collision;
31 definitionRule(): DefinitionRule;
32 /** The files a space's versions after `from`, up to `to`, changed, when each one's are kept; else undefined. */
33 changedSince(space: Space, from: number, to: number): ReadonlySet<string> | undefined;
34 refuse(
35 agent: AgentState,
36 space: Space,
37 via: "publish" | "land",
38 time: number,
39 collisions: Collision[],
40 entries: Entry[],
41 ): { ok: false; collisions: Collision[] };
42 requireClosed(tie: { deps: readonly AtomId[] }, union: ReadonlySet<AtomId>): void;
43 apply(entry: Entry): void;
44 newsForOthers(publisher: AgentState, space: Space, version: number, entries: Entry[]): void;
45 addressedNews(agent: AgentState, version: number, entries: Entry[]): void;
46 reviewNews(agent: AgentState, version: number, entries: Entry[]): void;
47 flush(entries: Entry[]): void;
50/**
51 * Land the agent's named draft in shared as one unit: every change
52 * published into it that shared doesn't have yet. Refused, like a publish,
53 * when those changes collide with work that landed in shared after the
54 * draft's base; sync the draft, settle, and land again.
55 */
56export function land(host: LandHost, agentName: string, message: string, time: number): LandAnswer {
57 if (typeof message !== "string") throw new ProjectError("bad-request", "land needs a message");
58 host.notQueuedYet("land");
59 const known = host.agents.get(agentName);
60 const landing = known && known.space !== SHARED ? host.spaces.get(known.space) : undefined;
61 if (landing) host.checkClash([...landing.own].filter((id) => !host.shared().own.has(id)));
62 const entries: Entry[] = [];
63 const answer = landDraft(host, host.agent(agentName, entries), message, time, entries);
64 host.flush(entries);
65 return answer;
68/**
69 * What landing `changes` of a named draft would collide with in shared now (#41, the Drafts page reads it
70 * too): the same pairs and definitions land refuses on.
71 */
72export function landCollisions(
73 host: LandHost,
74 space: Space,
75 changes: AtomId[],
76 pick: boolean,
77): { collisions: Collision[]; newer: ReadonlySet<AtomId>; union: ReadonlySet<AtomId> } {
78 const shared = host.shared();
79 const base = host.at(shared, sharedBaseOf(space));
80 const own = new StableOverlaySet(changes);
81 const current = host.at(shared, host.version());
82 const union = unionOverlay(current, changes);
83 // The draft's own changes (some already plucked into shared) are never newer work to it.
84 const newer = differenceSet(
85 differenceSet(
86 shared.membershipVersion
87 ? new SharedAdditions(shared, sharedBaseOf(space), base, current)
88 : differenceSet(current, base),
89 own,
90 ),
91 space.own,
92 );
93 // A pluck is checked as the draft's base plus only the plucked changes.
94 const draftView = pick
95 ? host.viewAt(shared, sharedBaseOf(space), changes)
96 : host.viewAt(space, space.versions.length - 1);
97 const describeLand = (p: Pair): Collision => host.describe(p, draftView, host.sharedView(), SHARED);
98 // Where shared changed since the draft's base, from the files each version since kept as it landed (abso 738): working
99 // it out from the newer atoms placed every line they wrote or deleted, about 30% of a warm draft-state read.
100 const changed = () => host.changedSince(shared, sharedBaseOf(space), host.version());
101 const collisions = [
102 ...host.pairs(changes, newer, union).map(describeLand),
103 ...host
104 .definitionRule()
105 .collisions(
106 own,
107 draftView,
108 host.viewAt(shared, sharedBaseOf(space)),
109 host.sharedView(),
110 newer,
111 SHARED,
112 undefined,
113 undefined,
114 changed,
115 )
116 .map((d) => d.collision),
117 ];
118 return { collisions, newer, union };
121/** The inner land, also used when ingesting a git push and when plucking. */
122export function landDraft(
123 host: LandHost,
124 agent: AgentState,
125 message: string,
126 time: number,
127 entries: Entry[],
128 pick?: { space: Space; changes: AtomId[] },
129): LandAnswer {
130 const space = pick?.space ?? host.namedSpaceOf(agent);
131 const shared = host.shared();
132 const changes = (pick?.changes ?? [...space.own])
133 .filter((id) => !shared.own.has(id) && !shared.droppedIn.has(id))
134 .sort((p, q) => host.graph.atom(p).seq - host.graph.atom(q).seq);
135 if (changes.length === 0) return { ok: true, version: host.version(), changes: 0 };
136 const { collisions, newer, union } = landCollisions(host, space, changes, !!pick);
137 if (collisions.length > 0) return host.refuse(agent, space, "land", time, collisions, entries);
138 const seen = host.pairs(changes, differenceSet(host.at(shared, host.version()), newer), union);
139 const resolves = [...new Set(seen.flatMap((p) => [p.yours, p.theirs]))];
140 const tie = buildAtom(host.viewAt(shared, host.version(), changes), {
141 author: agent.name,
142 message,
143 time,
144 needs: changes,
145 resolves,
146 });
147 host.requireClosed(tie, union);
148 const version = host.version() + 1;
149 // A pluck leaves the draft where it is: the rest of it lands later.
150 const rebase = !pick && shared.versions.length - 1 === sharedBaseOf(space);
151 entries.push({
152 type: "land",
153 space: space.name,
154 agent: agent.name,
155 version,
156 message,
157 time,
158 changes,
159 tie,
160 rebase,
161 change: space.startedAt ?? host.nextAt(),
162 });
163 host.apply(entries[entries.length - 1]!);
164 host.newsForOthers(agent, shared, version, entries);
165 host.addressedNews(agent, version, entries);
166 host.reviewNews(agent, version, entries);
167 return { ok: true, version, changes: changes.length };