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 192 --json
JSON
/christopher/abso-core/versions/192?format=json

Auto-settle (#19): code checks around the Workers AI pick. rules.ts settles in code when one side's lines are all in the other's, and turns down a keep-both that would repeat a line or set the same key twice (needs an agent instead). The Worker can take several samples and use their agreement as the confidence, since the model's own confidence swung 0.00 to 0.90 between identical runs.

of shared·autosettle··Not checked

Version 191 · Files at version 192

3 files, 86 lines added, 8 removed.

--- /dev/null
+++ b/scripts/autosettle/rules.ts
@@ -0,0 +1,35 @@
1+// Checks in code around the model's pick (#19). What code can decide, code
2+// decides; what the model picks is turned down when it would plainly break
3+// the file. Both run before anything is settled automatically.
4+
5+import type { Hold, Pick } from "./jev.ts";
6+
7+const solid = (lines: readonly string[]) => lines.map((l) => l.trim()).filter((l) => /[A-Za-z0-9]/.test(l));
8+
9+/** A pick code can make without the model, with why; undefined when it takes judgment. */
10+export function ruled(h: Hold): { pick: Pick; reason: string } | undefined {
11+ const yours = solid(h.yours);
12+ const theirs = solid(h.theirs);
13+ if (yours.join("\n") === theirs.join("\n")) return { pick: "theirs", reason: "both sides wrote the same lines" };
14+ if (yours.length > 0 && yours.every((l) => theirs.includes(l)))
15+ return { pick: "theirs", reason: "every line of yours is already in theirs" };
16+ if (theirs.length > 0 && theirs.every((l) => yours.includes(l)))
17+ return { pick: "yours", reason: "every line of theirs is already in yours" };
18+ return undefined;
19+}
21+/** Why a model's pick can't be applied as it is, or undefined when it can. */
22+export function refused(h: Hold, pick: Pick): string | undefined {
23+ if (pick !== "both") return undefined;
24+ const theirs = new Set(solid(h.theirs));
25+ const twice = solid(h.yours).filter((l) => theirs.has(l));
26+ if (twice.length > 0)
27+ return `keeping both would repeat ${twice.length === 1 ? "a line" : `${twice.length} lines`}: ${twice[0]}`;
28+ // The same key or name set on both sides: `timeoutMs:`, `limit =`.
29+ const key = (l: string) => /^([A-Za-z_$"'][\w$"'-]*)\s*[:=]/.exec(l)?.[1];
30+ const theirKeys = new Set(solid(h.theirs).map(key).filter(Boolean));
31+ const clash = solid(h.yours)
32+ .map(key)
33+ .find((k) => k && theirKeys.has(k));
34+ return clash ? `keeping both would set ${clash} twice` : undefined;
35+}
--- a/scripts/autosettle/workers-ai.ts
+++ b/scripts/autosettle/workers-ai.ts
@@ -1,6 +1,7 @@
11// Ask the local auto-settle Worker (workers-ai/, run with wrangler dev) how a hold should be settled (#19).
22
33import type { Hold, Pick } from "./jev.ts";
4+import { refused, ruled } from "./rules.ts";
45
56export const WORKER = process.env.AUTOSETTLE_WORKER ?? "http://127.0.0.1:8845";
67
@@ -14,14 +15,34 @@
1415 /** The model's own time, as the Worker measured it. */
1516 readonly modelMs: number;
1617 readonly inputTokens: number;
18+ /** rule: code decided without the model. turned down: the model's pick would break the file, so an agent settles. */
19+ readonly by: "rule" | "model" | "turned down";
20+ /** The model's own pick, when it was turned down. */
21+ readonly modelPick?: Pick;
1722}
1823
19−export async function askWorkersAi(h: Hold, opts: { model?: string; url?: string } = {}): Promise<ModelVerdict> {
24+/** Code's rules first, then the model, then code's check on what the model picked. */
25+export async function askWorkersAi(
26+ h: Hold,
27+ opts: { model?: string; url?: string; samples?: number } = {},
28+): Promise<ModelVerdict> {
29+ const rule = ruled(h);
30+ if (rule) return { ...rule, confidence: 1, model: "rule", ms: 0, modelMs: 0, inputTokens: 0, by: "rule" };
31+ const v = await askModel(h, opts);
32+ const no = refused(h, v.pick);
33+ return no ? { ...v, pick: "agent", reason: no, by: "turned down", modelPick: v.pick } : v;
34+}
36+async function askModel(h: Hold, opts: { model?: string; url?: string; samples?: number }): Promise<ModelVerdict> {
2037 const started = performance.now();
2138 const res = await fetch(opts.url ?? WORKER, {
2239 method: "POST",
2340 headers: { "content-type": "application/json" },
24− body: JSON.stringify({ hold: h, ...(opts.model ? { model: opts.model } : {}) }),
41+ body: JSON.stringify({
42+ hold: h,
43+ ...(opts.model ? { model: opts.model } : {}),
44+ samples: opts.samples ?? Number(process.env.AUTOSETTLE_SAMPLES ?? 1),
45+ }),
2546 });
2647 const out = (await res.json()) as {
2748 pick?: Pick;
@@ -41,5 +62,6 @@
4162 ms: performance.now() - started,
4263 modelMs: out.ms,
4364 inputTokens: out.usage?.prompt_tokens ?? 0,
65+ by: "model",
4466 };
4567}
--- a/scripts/autosettle/workers-ai/index.ts
+++ b/scripts/autosettle/workers-ai/index.ts
@@ -74,16 +74,22 @@
7474export default {
7575 async fetch(request: Request, env: Env): Promise<Response> {
7676 if (request.method !== "POST") return new Response("POST a hold", { status: 405 });
77− const { hold, model = DEFAULT_MODEL } = (await request.json()) as { hold: Hold; model?: string };
77+ const {
78+ hold,
79+ model = DEFAULT_MODEL,
80+ samples = 1,
81+ } = (await request.json()) as { hold: Hold; model?: string; samples?: number };
7882 const started = Date.now();
79− try {
83+ // One sample answers at temperature 0 with the model's own confidence. Several sample at 0.7 in parallel,
84+ // and the confidence is the share that agree with the most common pick: steadier than a self-report.
85+ const one = async () => {
8086 const out = (await env.AI.run(model, {
8187 messages: [
8288 { role: "system", content: SYSTEM },
8389 { role: "user", content: prompt(hold) },
8490 ],
8591 response_format: { type: "json_schema", json_schema: SCHEMA },
86− temperature: 0,
92+ temperature: samples > 1 ? 0.7 : 0,
8793 max_tokens: 200,
8894 })) as { response: unknown; usage?: { prompt_tokens?: number; completion_tokens?: number } };
8995 const answer = (typeof out.response === "string" ? JSON.parse(out.response) : out.response) as {
@@ -91,9 +97,24 @@
9197 confidence: number;
9298 reason: string;
9399 };
94− if (!(PICKS as readonly string[]).includes(answer.pick))
95− return Response.json({ error: `not a settle choice: ${answer.pick}` }, { status: 502 });
96− return Response.json({ ...answer, model, ms: Date.now() - started, usage: out.usage ?? null });
100+ if (!(PICKS as readonly string[]).includes(answer?.pick)) throw new Error(`not a settle choice: ${answer?.pick}`);
101+ return { ...answer, usage: out.usage ?? null };
102+ };
103+ try {
104+ const answers = await Promise.all(Array.from({ length: Math.min(9, Math.max(1, samples)) }, one));
105+ const votes = Object.fromEntries(PICKS.map((p) => [p, answers.filter((a) => a.pick === p).length]));
106+ const pick = PICKS.reduce((a, b) => (votes[b]! > votes[a]! ? b : a));
107+ const first = answers.find((a) => a.pick === pick)!;
108+ return Response.json({
109+ pick,
110+ confidence: answers.length > 1 ? votes[pick]! / answers.length : first.confidence,
111+ selfReported: answers.map((a) => a.confidence),
112+ votes,
113+ reason: first.reason,
114+ model,
115+ ms: Date.now() - started,
116+ usage: first.usage,
117+ });
97118 } catch (e) {
98119 return Response.json({ error: (e as Error).message, model, ms: Date.now() - started }, { status: 502 });
99120 }