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
show on this site's /mcp
CLI
abso show 201 --json
JSON
/christopher/abso-core/versions/201?format=json

Auto-settle (#19): the open server's advice now comes from Clef, Cloudflare's decision model, by default. On project abso's 33 replayed past holds, Clef settled none wrong at confidence 0.5 or more, where the Llama text model was wrong once at 1.00 (it kept only theirs and would have dropped a 403 guard). A lean to one side under AUTOSETTLE_AT (0.5 unless set) is advice for an agent, marked unsure; AUTOSETTLE_MODEL picks clef-flash or a text model instead. Clef answers in about 0.3 s. The System One questions live in server/src/autosettle.ts, and scripts/autosettle/jev.ts asks Jev the same ones.

of shared·autosettle··Not checked

Version 200 · Files at version 201

7 files, 207 lines added, 106 removed.

--- a/client/src/api-answers.ts
+++ b/client/src/api-answers.ts
@@ -91,14 +91,14 @@
9191 | { ok: false; collisions: readonly Collision[]; reason?: string; advice?: readonly (Advice | null)[] };
9292/**
9393 * A server's first opinion on one collision of a held push (#19), when it runs AUTOSETTLE: which settle fits,
94− * how sure, and why. `by`: rule (code decided), model (Workers AI), or turned down (the model's pick would break
95− * the file, so an agent settles).
94+ * how sure, and why. `by`: rule (code decided), model (Workers AI), turned down (the model's pick would break
95+ * the file, so an agent settles), or unsure (the model leaned to a side under the server's threshold).
9696 */
9797export interface Advice {
9898 readonly pick: "yours" | "theirs" | "both" | "agent";
9999 readonly confidence: number;
100100 readonly reason: string;
101− readonly by: "rule" | "model" | "turned down";
101+ readonly by: "rule" | "model" | "turned down" | "unsure";
102102 readonly modelPick?: string;
103103 readonly model?: string;
104104 readonly ms: number;
--- a/client/src/publish-commands.ts
+++ b/client/src/publish-commands.ts
@@ -505,7 +505,9 @@
505505 ? "by a check in code"
506506 : a.by === "turned down"
507507 ? `the model said keep ${a.modelPick}, turned down`
508− : `${a.confidence.toFixed(2)} of samples agree`;
508+ : a.by === "unsure"
509+ ? `the model leaned to keep ${a.modelPick} at ${a.confidence.toFixed(2)}`
510+ : `confidence ${a.confidence.toFixed(2)}`;
509511 return `${what} (${how}): ${a.reason}`;
510512}
511513
--- a/scripts/autosettle/jev.ts
+++ b/scripts/autosettle/jev.ts
@@ -7,7 +7,11 @@
77// is never printed or written anywhere.
88
99import { execFileSync } from "node:child_process";
10+import { confidenceOf, SIGNS, systemOneBody } from "../../server/src/autosettle.ts";
1011
12+// The questions are the open server's own (server/src/autosettle.ts), so Jev and Clef are asked alike.
13+export { systemOneBody };
1115export const ENDPOINT = "https://api.typesafe.ai/v1/systemone";
1216
1317/** abso's settle choices, plus a hand edit by an agent. */
@@ -67,79 +71,10 @@
6771 }
6872}
6973
70−const text = (lines: readonly string[]): string => (lines.length === 0 ? "(no lines)" : lines.join("\n"));
71−
72−function stateOf(h: Hold): Record<string, unknown> {
73− return {
74− file: h.file,
75− lines_above: text(h.before),
76− before_either_change: text(h.base),
77− yours: text(h.yours),
78− theirs: text(h.theirs),
79− lines_below: text(h.after),
80− your_push: { agent: h.yourAgent ?? "you", message: h.yourMessage ?? "(no message)" },
81− their_push: { agent: h.theirAgent ?? "the other agent", message: h.theirMessage ?? "(no message)" },
82− ...(h.issue ? { issue: h.issue } : {}),
83− };
84−}
85−
86−const INSTRUCTIONS =
87− "Two agents changed the same lines of `file`. Both started from `before_either_change`. " +
88− "`yours` is what the held agent wrote, for the reason in `your_push`; `theirs` was pushed first, for the reason in `their_push`. " +
89− "The region sits between `lines_above` and `lines_below`. " +
90− "Which result is correct code that does what both pushes meant?";
91−
92−const CRITERIA: Record<Pick, string> = {
93− yours: "Keep `yours` only: `yours` already does what `theirs` meant, or `theirs` is wrong or out of date.",
94− theirs: "Keep `theirs` only: `theirs` already does what `yours` meant, or `yours` is wrong or out of date.",
95− both: "Keep both unchanged, `yours` then `theirs`: they add separate things that work side by side, and nothing is duplicated or contradicts.",
96− agent:
97− "Neither side alone and not both unchanged: 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.",
98−};
99−
100−function choice(order: readonly Pick[]) {
101− return {
102− type: "choice",
103− instructions: INSTRUCTIONS,
104− criteria: Object.fromEntries(order.map((p) => [p, CRITERIA[p]])),
105− };
106−}
107−
108−const SIGNS: Record<string, string> = {
109− same_setting_different_values:
110− "Do `yours` and `theirs` set the same setting, constant or value to two different values?",
111− theirs_covers_yours: "Does `theirs` already do everything `yours` was changed to do?",
112− yours_covers_theirs: "Does `yours` already do everything `theirs` was changed to do?",
113− both_unchanged_compiles:
114− "If `yours` were placed directly above `theirs`, unchanged, would the file still be correct code with nothing defined twice?",
115−};
116−
117−function confidenceOf(p: Record<Pick, number>): number {
118− const max = Math.max(...PICKS.map((k) => p[k]));
119− const even = 1 / PICKS.length;
120− return Math.max(0, (max - even) / (1 - even));
121−}
122−
12374function top(p: Record<Pick, number>): Pick {
12475 return PICKS.reduce((a, b) => (p[b] > p[a] ? b : a));
12576}
12677
127−/**
128− * One System One request (TypeSafe's API, which Cloudflare's Clef also takes): the choice in two option orders
129− * (Jev's docs warn order can sway a Choice) and the side questions.
130− */
131−export function systemOneBody(h: Hold, model: string) {
132− return {
133− model,
134− state: stateOf(h),
135− questions: {
136− settle: choice(PICKS),
137− settle_reversed: choice([...PICKS].reverse()),
138− ...Object.fromEntries(Object.entries(SIGNS).map(([id, q]) => [id, { type: "noul", instructions: q }])),
139− },
140− };
141−}
142−
14378export interface SystemOneAnswer {
14479 model: string;
14580 answers: Record<string, { type: string; probabilities?: Record<string, number>; noul?: number; choice?: string }>;
--- a/server/src/autosettle.ts
+++ b/server/src/autosettle.ts
@@ -30,8 +30,11 @@
3030 /** The share of samples that agree, or 1 for a pick code made. */
3131 readonly confidence: number;
3232 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";
33+ /**
34+ * rule: code decided without the model. turned down: the model's pick would break the file. unsure: the model
35+ * leaned to a side under the threshold, so an agent settles.
36+ */
37+ readonly by: "rule" | "model" | "turned down" | "unsure";
3538 readonly modelPick?: Pick;
3639 readonly model?: string;
3740 readonly ms: number;
@@ -168,13 +171,130 @@
168171 };
169172}
170173
174+const text = (lines: readonly string[]): string => (lines.length === 0 ? "(no lines)" : lines.join("\n"));
176+/** A hold as a System One state: the content the questions point at by name. */
177+export function stateOf(h: Hold): Record<string, unknown> {
178+ return {
179+ file: h.file,
180+ lines_above: text(h.before),
181+ before_either_change: h.base.length === 0 && h.before.length === 0 ? "(not given)" : text(h.base),
182+ yours: text(h.yours),
183+ theirs: text(h.theirs),
184+ lines_below: text(h.after),
185+ your_push: { agent: h.yourAgent ?? "you", message: h.yourMessage ?? "(no message)" },
186+ their_push: { agent: h.theirAgent ?? "the other agent", message: h.theirMessage ?? "(no message)" },
187+ ...(h.issue ? { issue: h.issue } : {}),
188+ };
189+}
191+const INSTRUCTIONS =
192+ "Two agents changed the same lines of `file`. Both started from `before_either_change`. " +
193+ "`yours` is what the held agent wrote, for the reason in `your_push`; `theirs` was pushed first, for the reason in `their_push`. " +
194+ "The region sits between `lines_above` and `lines_below`. " +
195+ "Which result is correct code that does what both pushes meant?";
197+const CRITERIA: Record<Pick, string> = {
198+ yours: "Keep `yours` only: `yours` already does what `theirs` meant, or `theirs` is wrong or out of date.",
199+ theirs: "Keep `theirs` only: `theirs` already does what `yours` meant, or `yours` is wrong or out of date.",
200+ both: "Keep both unchanged, `yours` then `theirs`: they add separate things that work side by side, and nothing is duplicated or contradicts.",
201+ agent:
202+ "Neither side alone and not both unchanged: 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.",
203+};
205+const choice = (order: readonly Pick[]) => ({
206+ type: "choice",
207+ instructions: INSTRUCTIONS,
208+ criteria: Object.fromEntries(order.map((p) => [p, CRITERIA[p]])),
209+});
211+/** Side questions asked with the choice, for the record (yes-probabilities). */
212+export const SIGNS: Record<string, string> = {
213+ same_setting_different_values:
214+ "Do `yours` and `theirs` set the same setting, constant or value to two different values?",
215+ theirs_covers_yours: "Does `theirs` already do everything `yours` was changed to do?",
216+ yours_covers_theirs: "Does `yours` already do everything `theirs` was changed to do?",
217+ both_unchanged_compiles:
218+ "If `yours` were placed directly above `theirs`, unchanged, would the file still be correct code with nothing defined twice?",
219+};
221+/**
222+ * A System One request (TypeSafe's Jev and Cloudflare's Clef both take it): the choice in two option orders, since
223+ * Jev's docs warn order can sway a Choice, and the side questions.
224+ */
225+export function systemOneBody(h: Hold, model: string) {
226+ return {
227+ model,
228+ state: stateOf(h),
229+ questions: {
230+ settle: choice(PICKS),
231+ settle_reversed: choice([...PICKS].reverse()),
232+ ...Object.fromEntries(Object.entries(SIGNS).map(([id, q]) => [id, { type: "noul", instructions: q }])),
233+ },
234+ };
235+}
237+/** Choice confidence: how far the top probability sits above an even split, 0 to 1 (TypeSafe's formula). */
238+export function confidenceOf(p: Record<Pick, number>): number {
239+ const max = Math.max(...PICKS.map((k) => p[k]));
240+ const even = 1 / PICKS.length;
241+ return Math.max(0, (max - even) / (1 - even));
242+}
244+/** Clef's pick: the two orders' probabilities averaged. */
245+export async function clefPick(
246+ ai: Ai,
247+ h: Hold,
248+ model: string,
249+): Promise<{ pick: Pick; confidence: number; reason: string; probabilities: Record<Pick, number> }> {
250+ const out = (await ai.run(model === "clef-flash" ? "@cf/cloudflare/clef-flash" : "@cf/cloudflare/clef", {
251+ ...systemOneBody(h, model === "clef-flash" ? "clef-flash" : "clef"),
252+ })) as { answers?: Record<string, { probabilities?: Record<string, number> }> };
253+ const orders = ["settle", "settle_reversed"].map((id) => out.answers?.[id]?.probabilities);
254+ if (orders.some((p) => !p)) throw new Error("Clef answered without the settle choice");
255+ const probabilities = Object.fromEntries(
256+ PICKS.map((k) => [k, ((orders[0]![k] ?? 0) + (orders[1]![k] ?? 0)) / 2]),
257+ ) as Record<Pick, number>;
258+ const pick = PICKS.reduce((a, b) => (probabilities[b] > probabilities[a] ? b : a));
259+ const words: Record<Pick, string> = {
260+ yours: "keep yours",
261+ theirs: "keep theirs",
262+ both: "keep both",
263+ agent: "an agent",
264+ };
265+ return {
266+ pick,
267+ confidence: confidenceOf(probabilities),
268+ probabilities,
269+ reason: `Clef's probabilities: ${PICKS.map((k) => `${words[k]} ${probabilities[k].toFixed(2)}`).join(", ")}`,
270+ };
271+}
171273/** 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> {
274+export async function advise(
275+ ai: Ai,
276+ h: Hold,
277+ opts: { model?: string; samples?: number; at?: number } = {},
278+): Promise<Advice> {
173279 const started = Date.now();
174280 const rule = ruled(h);
175281 if (rule) return { ...rule, confidence: 1, by: "rule", ms: 0 };
176− const m = await modelPick(ai, h, opts);
177− const model = opts.model ?? MODEL;
282+ // Clef unless a text model is named: on project abso's 33 past holds it settled nothing wrong at 0.5 or more,
283+ // where the text model was wrong once at 1.00 (#19).
284+ const clef = !opts.model || opts.model.startsWith("clef");
285+ const m = clef ? await clefPick(ai, h, opts.model ?? "clef") : await modelPick(ai, h, opts);
286+ const model = clef ? `@cf/cloudflare/${opts.model ?? "clef"}` : (opts.model ?? MODEL);
287+ const at = opts.at ?? 0.5;
288+ if (m.pick !== "agent" && m.confidence < at)
289+ return {
290+ pick: "agent",
291+ confidence: m.confidence,
292+ reason: `${m.reason}; under ${at}, so an agent settles`,
293+ by: "unsure",
294+ modelPick: m.pick,
295+ model,
296+ ms: Date.now() - started,
297+ };
178298 const no = refused(h, m.pick);
179299 return no
180300 ? {
@@ -207,24 +327,29 @@
207327 ai: Ai,
208328 collisions: readonly HeldCollision[],
209329 held: { agent: string; message: string },
330+ opts: { model?: string; at?: number } = {},
210331): Promise<(Advice | null)[]> {
211332 const lines = (text: string) => (text === "" ? [] : text.replace(/\r?\n$/, "").split(/\r?\n/));
212333 return Promise.all(
213334 collisions.map(async (c) => {
214335 if (c.kind !== "lines" || c.untouched || c.marked) return null;
215336 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− });
337+ return await advise(
338+ ai,
339+ {
340+ file: c.file,
341+ base: [],
342+ yours: lines(c.yours.text),
343+ theirs: lines(c.theirs.text),
344+ before: [],
345+ after: [],
346+ yourAgent: held.agent,
347+ yourMessage: held.message,
348+ theirAgent: c.theirs.agent,
349+ theirMessage: c.theirs.message,
350+ },
351+ opts,
352+ );
228353 } catch (e) {
229354 console.error(`autosettle: ${c.file}: ${e instanceof Error ? e.message : String(e)}`);
230355 return null;
--- a/server/src/env.ts
+++ b/server/src/env.ts
@@ -26,6 +26,10 @@
2626 KEY_HMAC_SECRET?: string;
2727 /** "on" gives a held push a first opinion on each collision from Workers AI (#19); needs the AI binding. */
2828 AUTOSETTLE?: string;
29+ /** Which model advises: clef (the default), clef-flash, or a Workers AI text model's id. */
30+ AUTOSETTLE_MODEL?: string;
31+ /** The confidence under which a lean to one side is left to an agent: 0.5 unless set. */
32+ AUTOSETTLE_AT?: string;
2933 /** Workers AI, for AUTOSETTLE. Unbound unless wrangler.jsonc names it. */
3034 AI?: { run(model: string, input: unknown): Promise<unknown> };
3135}
--- a/server/src/router.ts
+++ b/server/src/router.ts
@@ -274,7 +274,15 @@
274274 const held = a.body as { ok?: boolean; collisions?: Parameters<typeof adviseHeld>[1] };
275275 if (held.ok === false && held.collisions && held.collisions.length > 0) {
276276 const message = String((body as { message?: unknown } | undefined)?.message ?? "");
277− const advice = await adviseHeld(env.AI, held.collisions, { agent, message });
277+ const advice = await adviseHeld(
278+ env.AI,
279+ held.collisions,
280+ { agent, message },
281+ {
282+ ...(env.AUTOSETTLE_MODEL ? { model: env.AUTOSETTLE_MODEL } : {}),
283+ ...(Number(env.AUTOSETTLE_AT) > 0 ? { at: Number(env.AUTOSETTLE_AT) } : {}),
284+ },
285+ );
278286 if (advice.some((x) => x !== null)) return json(a.status, { ...held, advice });
279287 }
280288 }
--- a/server/test/autosettle.test.ts
+++ b/server/test/autosettle.test.ts
@@ -2,12 +2,26 @@
22import { test } from "node:test";
33import { type Ai, adviseHeld, refused, ruled } from "../src/autosettle.ts";
44
5−/** A stand-in for Workers AI that always answers `pick`, and counts its calls. */
6−function fakeAi(pick: string): Ai & { calls: number } {
5+/**
6+ * A stand-in for Workers AI that always leans to `pick`: a System One answer for Clef (`top` on `pick`, the rest
7+ * spread evenly) or a JSON-mode answer for a text model. It counts its calls.
8+ */
9+function fakeAi(pick: string, top = 0.85): Ai & { calls: number } {
710 const ai = {
811 calls: 0,
9− async run() {
12+ async run(_model: string, input: unknown) {
1013 ai.calls++;
14+ if ((input as { questions?: unknown }).questions) {
15+ const probabilities = Object.fromEntries(
16+ ["yours", "theirs", "both", "agent"].map((k) => [k, k === pick ? top : (1 - top) / 3]),
17+ );
18+ return {
19+ answers: {
20+ settle: { type: "choice", probabilities },
21+ settle_reversed: { type: "choice", probabilities },
22+ },
23+ };
24+ }
1125 return { response: { pick, confidence: 0.9, reason: "a fixed answer" }, usage: { prompt_tokens: 10 } };
1226 },
1327 };
@@ -20,13 +34,12 @@
2034 yours: { lines: [3, 3] as [number, number], text: yours },
2135 theirs: { agent: "other", message: "their reason", text: theirs },
2236});
37+const routes = held('\t"/blame": blame,\n', '\t"/search": search,\n');
38+const me = { agent: "me", message: "my reason" };
2339
2440test("code settles when one side's lines are all in the other's, without asking the model", async () => {
2541 const ai = fakeAi("both");
26− const [a] = await adviseHeld(ai, [held("\ttimeoutMs: 30000,\n", "\ttimeoutMs: 30000,\n\tretries: 2,\n")], {
27− agent: "me",
28− message: "my reason",
29− });
42+ const [a] = await adviseHeld(ai, [held("\ttimeoutMs: 30000,\n", "\ttimeoutMs: 30000,\n\tretries: 2,\n")], me);
3043 assert.equal(a?.pick, "theirs");
3144 assert.equal(a?.by, "rule");
3245 assert.equal(ai.calls, 0);
@@ -43,17 +56,31 @@
4356 assert.equal(ruled(hold(['\t"/blame": blame,'], ['\t"/search": search,'])), undefined);
4457});
4558
46−test("the model's pick comes back with how many samples agree, and other kinds get no advice", async () => {
59+test("Clef advises by default: one call, its confidence from the probabilities, and other kinds get no advice", async () => {
4760 const ai = fakeAi("both");
48− const advice = await adviseHeld(
49− ai,
50− [held('\t"/blame": blame,\n', '\t"/search": search,\n'), held("x", "y", "definition")],
51− { agent: "me", message: "my reason" },
52− );
61+ const advice = await adviseHeld(ai, [routes, held("x", "y", "definition")], me);
5362 assert.equal(advice[0]?.pick, "both");
5463 assert.equal(advice[0]?.by, "model");
55− assert.equal(advice[0]?.confidence, 1);
64+ assert.equal(advice[0]?.model, "@cf/cloudflare/clef");
65+ assert.equal(advice[0]?.confidence.toFixed(2), "0.80");
5666 assert.equal(advice[1], null);
67+ assert.equal(ai.calls, 1);
68+});
70+test("a lean to one side under the threshold goes to an agent, saying which side it leaned to", async () => {
71+ const [a] = await adviseHeld(fakeAi("both", 0.55), [routes], me);
72+ assert.equal(a?.pick, "agent");
73+ assert.equal(a?.by, "unsure");
74+ assert.equal(a?.modelPick, "both");
75+ const [b] = await adviseHeld(fakeAi("both", 0.55), [routes], me, { at: 0.3 });
76+ assert.equal(b?.pick, "both");
77+});
79+test("a text model named instead takes 5 samples, its confidence the share that agree", async () => {
80+ const ai = fakeAi("both");
81+ const [a] = await adviseHeld(ai, [routes], me, { model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast" });
82+ assert.equal(a?.pick, "both");
83+ assert.equal(a?.confidence, 1);
5784 assert.equal(ai.calls, 5);
5885});
5986
@@ -63,6 +90,6 @@
6390 throw new Error("Workers AI is down");
6491 },
6592 };
66− const [a] = await adviseHeld(ai, [held("a = 1", "b = 2")], { agent: "me", message: "m" });
93+ const [a] = await adviseHeld(ai, [held("a = 1", "b = 2")], me);
6794 assert.equal(a, null);
6895});