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. Version 192 of shared· autosettle· Oct 14 (2026-10-14 23:07 UTC) · 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. 5 + import type { Hold, Pick } from "./jev.ts"; 7 + const solid = (lines: readonly string[]) => lines.map((l) => l.trim()).filter((l) => /[A-Za-z0-9]/.test(l)); 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" }; 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) 33 + .find((k) => k && theirKeys.has(k)); 34 + return clash ? `keeping both would set ${clash} twice` : undefined; --- a/scripts/autosettle/workers-ai.ts
+++ b/scripts/autosettle/workers-ai.ts
@@ -1,6 +1,7 @@
1 1 // Ask the local auto-settle Worker (workers-ai/, run with wrangler dev) how a hold should be settled (#19). 3 3 import type { Hold, Pick } from "./jev.ts"; 4 + import { refused, ruled } from "./rules.ts"; 5 6 export const WORKER = process.env.AUTOSETTLE_WORKER ?? "http://127.0.0.1:8845"; @@ -14,14 +15,34 @@
14 15 /** The model's own time, as the Worker measured it. */ 15 16 readonly modelMs: number; 16 17 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; 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( 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; 36 + async function askModel(h: Hold, opts: { model?: string; url?: string; samples?: number }): Promise<ModelVerdict> { 20 37 const started = performance.now(); 21 38 const res = await fetch(opts.url ?? WORKER, { 23 40 headers: { "content-type": "application/json" }, 24 − body: JSON.stringify({ hold: h, ...(opts.model ? { model: opts.model } : {}) }),
41 + body: JSON.stringify({ 43 + ...(opts.model ? { model: opts.model } : {}), 44 + samples: opts.samples ?? Number(process.env.AUTOSETTLE_SAMPLES ?? 1), 26 47 const out = (await res.json()) as { @@ -41,5 +62,6 @@
41 62 ms: performance.now() - started, 43 64 inputTokens: out.usage?.prompt_tokens ?? 0, --- a/scripts/autosettle/workers-ai/index.ts
+++ b/scripts/autosettle/workers-ai/index.ts
@@ -74,16 +74,22 @@
75 75 async fetch(request: Request, env: Env): Promise<Response> { 76 76 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 };
79 + model = DEFAULT_MODEL, 81 + } = (await request.json()) as { hold: Hold; model?: string; samples?: number }; 78 82 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 () => { 80 86 const out = (await env.AI.run(model, { 82 88 { role: "system", content: SYSTEM }, 83 89 { role: "user", content: prompt(hold) }, 85 91 response_format: { type: "json_schema", json_schema: SCHEMA }, 86 − temperature: 0,
92 + temperature: samples > 1 ? 0.7 : 0, 88 94 })) as { response: unknown; usage?: { prompt_tokens?: number; completion_tokens?: number } }; 89 95 const answer = (typeof out.response === "string" ? JSON.parse(out.response) : out.response) as { @@ -91,9 +97,24 @@
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 }; 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({ 110 + confidence: answers.length > 1 ? votes[pick]! / answers.length : first.confidence, 111 + selfReported: answers.map((a) => a.confidence), 113 + reason: first.reason, 115 + ms: Date.now() - started, 98 119 return Response.json({ error: (e as Error).message, model, ms: Date.now() - started }, { status: 502 });