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

Versions 196 to 318

Compare

+3 −233 in 1 place
Lines 1–4 at 318 (were 1–234 at 196)
1−// A first opinion on a held push (#19): for each collision, which of abso's
2−// settle choices fits, from Workers AI, with checks in code before and after.
3−// Off unless AUTOSETTLE is "on" and the Worker has an AI binding. The model
4−// picks one of four words; it never writes code.
1+// A first opinion on a held push (#19): the tool's own, in worker/project/autosettle.ts, so the platform's pin
2+// carries it too.
53
6−/** abso's settle choices, plus a hand edit by an agent. */
7−export type Pick = "yours" | "theirs" | "both" | "agent";
8−export const PICKS: readonly Pick[] = ["yours", "theirs", "both", "agent"];
9−export const MODEL = "@cf/meta/llama-3.3-70b-instruct-fp8-fast";
10−
11−/** One collision as the held agent sees it. */
12−export interface Hold {
13− readonly file: string;
14− readonly base: readonly string[];
15− readonly yours: readonly string[];
16− readonly theirs: readonly string[];
17− readonly before: readonly string[];
18− readonly after: readonly string[];
19− /** The held push's message: the settler's reason. */
20− readonly yourMessage?: string;
21− readonly yourAgent?: string;
22− /** The push that got there first: its reason. */
23− readonly theirMessage?: string;
24− readonly theirAgent?: string;
25− readonly issue?: string;
26−}
27−
28−export interface Advice {
29− readonly pick: Pick;
30− /** The share of samples that agree, or 1 for a pick code made. */
31− readonly confidence: number;
32− readonly reason: string;
33− /** rule: code decided without the model. turned down: the model's pick would break the file. */
34− readonly by: "rule" | "model" | "turned down";
35− readonly modelPick?: Pick;
36− readonly model?: string;
37− readonly ms: number;
38−}
39−
40−export interface Ai {
41− run(model: string, input: unknown): Promise<unknown>;
42−}
43−
44−const solid = (lines: readonly string[]) => lines.map((l) => l.trim()).filter((l) => /[A-Za-z0-9]/.test(l));
45−
46−/** A pick code can make without the model, with why; undefined when it takes judgment. */
47−export function ruled(h: Hold): { pick: Pick; reason: string } | undefined {
48− const yours = solid(h.yours);
49− const theirs = solid(h.theirs);
50− if (yours.join("\n") === theirs.join("\n")) return { pick: "theirs", reason: "both sides wrote the same lines" };
51− if (yours.length > 0 && yours.every((l) => theirs.includes(l)))
52− return { pick: "theirs", reason: "every line of yours is already in theirs" };
53− if (theirs.length > 0 && theirs.every((l) => yours.includes(l)))
54− return { pick: "yours", reason: "every line of theirs is already in yours" };
55− return undefined;
56−}
57−
58−/** Why a model's pick can't be applied as it is, or undefined when it can. */
59−export function refused(h: Hold, pick: Pick): string | undefined {
60− if (pick !== "both") return undefined;
61− const theirs = new Set(solid(h.theirs));
62− const twice = solid(h.yours).filter((l) => theirs.has(l));
63− if (twice.length > 0)
64− return `keeping both would repeat ${twice.length === 1 ? "a line" : `${twice.length} lines`}: ${twice[0]}`;
65− // The same key or name set on both sides: `timeoutMs:`, `limit =`.
66− const key = (l: string) => /^([A-Za-z_$"'][\w$"'-]*)\s*[:=]/.exec(l)?.[1];
67− const theirKeys = new Set(solid(h.theirs).map(key).filter(Boolean));
68− const clash = solid(h.yours)
69− .map(key)
70− .find((k) => k && theirKeys.has(k));
71− if (clash) return `keeping both would set ${clash} twice`;
72− // Two versions of one statement, such as an import from the same module with a different list of names:
73− // most of the names on one side are on the other too.
74− const names = (lines: readonly string[]) => new Set(lines.join(" ").match(/[A-Za-z_$][\w$]{2,}/g) ?? []);
75− const a = names(h.yours);
76− const b = names(h.theirs);
77− const shared = [...a].filter((n) => b.has(n)).length;
78− const share = shared / Math.max(1, Math.min(a.size, b.size));
79− return share >= 0.5
80− ? `keeping both would repeat one statement in two versions (${shared} names on both sides)`
81− : undefined;
82−}
83−
84−const SYSTEM = `You settle merge collisions in a version control system for coding agents.
85−Two agents changed the same lines of one file. Both started from BEFORE. YOURS is what the held agent wrote, for the reason in its push message; THEIRS was pushed first, for the reason in its push message.
86−Pick exactly one:
87−- "yours": keep YOURS only. YOURS already does what THEIRS meant, or THEIRS is wrong or out of date.
88−- "theirs": keep THEIRS only. THEIRS already does what YOURS meant, or YOURS is wrong or out of date.
89−- "both": keep both unchanged, YOURS then THEIRS. They add separate things that work side by side, with nothing duplicated or contradictory.
90−- "agent": none of those is correct. The sides change the same thing in different ways (such as two different values for one setting), or keeping both would duplicate or contradict, so a correct result needs a hand edit.
91−Give "confidence" from 0 to 1: how likely your pick is the correct settle. Give "reason" as one plain sentence. Do not write code.`;
92−
93−const block = (lines: readonly string[]) => (lines.length === 0 ? "(no lines)" : lines.join("\n"));
94−
95−function prompt(h: Hold): string {
96− return [
97− `FILE: ${h.file}`,
98− `YOUR PUSH (${h.yourAgent ?? "you"}): ${h.yourMessage ?? "(no message)"}`,
99− `THEIR PUSH (${h.theirAgent ?? "the other agent"}): ${h.theirMessage ?? "(no message)"}`,
100− ...(h.issue ? [`ISSUE: ${h.issue}`] : []),
101− "",
102− "LINES ABOVE:",
103− block(h.before),
104− "",
105− "BEFORE (what both started from):",
106− h.base.length === 0 && h.before.length === 0 ? "(not given)" : block(h.base),
107− "",
108− "YOURS:",
109− block(h.yours),
110− "",
111− "THEIRS:",
112− block(h.theirs),
113− "",
114− "LINES BELOW:",
115− block(h.after),
116− ].join("\n");
117−}
118−
119−const SCHEMA = {
120− type: "object",
121− properties: {
122− pick: { type: "string", enum: PICKS },
123− confidence: { type: "number", minimum: 0, maximum: 1 },
124− reason: { type: "string" },
125− },
126− required: ["pick", "confidence", "reason"],
127−};
128−
129−/** The model's pick: several samples at once, the confidence being the share that agree with the most common pick. */
130−export async function modelPick(
131− ai: Ai,
132− h: Hold,
133− opts: { model?: string; samples?: number } = {},
134−): Promise<{ pick: Pick; confidence: number; reason: string; selfReported: number[]; usage: unknown }> {
135− const model = opts.model ?? MODEL;
136− const samples = Math.min(9, Math.max(1, opts.samples ?? 5));
137− const one = async () => {
138− const out = (await ai.run(model, {
139− messages: [
140− { role: "system", content: SYSTEM },
141− { role: "user", content: prompt(h) },
142− ],
143− response_format: { type: "json_schema", json_schema: SCHEMA },
144− temperature: samples > 1 ? 0.7 : 0,
145− max_tokens: 200,
146− })) as { response: unknown; usage?: unknown };
147− const answer = (typeof out.response === "string" ? JSON.parse(out.response) : out.response) as {
148− pick: string;
149− confidence: number;
150− reason: string;
151− };
152− if (!(PICKS as readonly string[]).includes(answer?.pick)) throw new Error(`not a settle choice: ${answer?.pick}`);
153− return { ...answer, pick: answer.pick as Pick, usage: out.usage ?? null };
154− };
155− const answers = await Promise.all(Array.from({ length: samples }, one));
156− const votes = Object.fromEntries(PICKS.map((p) => [p, answers.filter((a) => a.pick === p).length])) as Record<
157− Pick,
158− number
159− >;
160− const pick = PICKS.reduce((a, b) => (votes[b] > votes[a] ? b : a));
161− const first = answers.find((a) => a.pick === pick)!;
162− return {
163− pick,
164− confidence: samples > 1 ? votes[pick] / samples : first.confidence,
165− reason: first.reason,
166− selfReported: answers.map((a) => a.confidence),
167− usage: first.usage,
168− };
169−}
170−
171−/** Code's rules first, then the model, then code's check on what the model picked. */
172−export async function advise(ai: Ai, h: Hold, opts: { model?: string; samples?: number } = {}): Promise<Advice> {
173− const started = Date.now();
174− const rule = ruled(h);
175− if (rule) return { ...rule, confidence: 1, by: "rule", ms: 0 };
176− const m = await modelPick(ai, h, opts);
177− const model = opts.model ?? MODEL;
178− const no = refused(h, m.pick);
179− return no
180− ? {
181− pick: "agent",
182− confidence: m.confidence,
183− reason: no,
184− by: "turned down",
185− modelPick: m.pick,
186− model,
187− ms: Date.now() - started,
188− }
189− : { pick: m.pick, confidence: m.confidence, reason: m.reason, by: "model", model, ms: Date.now() - started };
190−}
191−
192−interface HeldCollision {
193− readonly kind: string;
194− readonly file: string;
195− readonly untouched?: true;
196− readonly marked?: true;
197− readonly yours: { readonly lines: [number, number]; readonly text: string };
198− readonly theirs: { readonly agent: string; readonly message: string; readonly text: string; readonly file?: string };
199−}
200−
201−/**
202− * Advice for each lines collision of a held push, in the order of `collisions`; null where it doesn't apply
203− * (another kind, already in the agent's file) or the model failed. Collisions don't carry the lines both
204− * started from, so the model goes without them.
205− */
206−export async function adviseHeld(
207− ai: Ai,
208− collisions: readonly HeldCollision[],
209− held: { agent: string; message: string },
210−): Promise<(Advice | null)[]> {
211− const lines = (text: string) => (text === "" ? [] : text.replace(/\r?\n$/, "").split(/\r?\n/));
212− return Promise.all(
213− collisions.map(async (c) => {
214− if (c.kind !== "lines" || c.untouched || c.marked) return null;
215− try {
216− return await advise(ai, {
217− file: c.file,
218− base: [],
219− yours: lines(c.yours.text),
220− theirs: lines(c.theirs.text),
221− before: [],
222− after: [],
223− yourAgent: held.agent,
224− yourMessage: held.message,
225− theirAgent: c.theirs.agent,
226− theirMessage: c.theirs.message,
227− });
228− } catch (e) {
229− console.error(`autosettle: ${c.file}: ${e instanceof Error ? e.message : String(e)}`);
230− return null;
231− }
232− }),
233− );
234−}
4+export * from "../../worker/project/autosettle.ts";
MCP
compare on this site's /mcp
CLI
abso diff 196 318 server/src/autosettle.ts --json
JSON
/christopher/abso-core/compare/196..318?file=server%2Fsrc%2Fautosettle.ts&format=json