{"version":192,"agent":"autosettle","message":"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.","time":1792019245839,"timeIso":"2026-10-14T23:07:25.839Z","changes":4,"change":3212,"files":["scripts/autosettle/rules.ts","scripts/autosettle/workers-ai.ts","scripts/autosettle/workers-ai/index.ts"],"draft":null,"diff":[{"path":"scripts/autosettle/rules.ts","added":35,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/rules.ts\n@@ -0,0 +1,35 @@\n+// Checks in code around the model's pick (#19). What code can decide, code\n+// decides; what the model picks is turned down when it would plainly break\n+// the file. Both run before anything is settled automatically.\n+\n+import type { Hold, Pick } from \"./jev.ts\";\n+\n+const solid = (lines: readonly string[]) => lines.map((l) => l.trim()).filter((l) => /[A-Za-z0-9]/.test(l));\n+\n+/** A pick code can make without the model, with why; undefined when it takes judgment. */\n+export function ruled(h: Hold): { pick: Pick; reason: string } | undefined {\n+\tconst yours = solid(h.yours);\n+\tconst theirs = solid(h.theirs);\n+\tif (yours.join(\"\\n\") === theirs.join(\"\\n\")) return { pick: \"theirs\", reason: \"both sides wrote the same lines\" };\n+\tif (yours.length > 0 && yours.every((l) => theirs.includes(l)))\n+\t\treturn { pick: \"theirs\", reason: \"every line of yours is already in theirs\" };\n+\tif (theirs.length > 0 && theirs.every((l) => yours.includes(l)))\n+\t\treturn { pick: \"yours\", reason: \"every line of theirs is already in yours\" };\n+\treturn undefined;\n+}\n+\n+/** Why a model's pick can't be applied as it is, or undefined when it can. */\n+export function refused(h: Hold, pick: Pick): string | undefined {\n+\tif (pick !== \"both\") return undefined;\n+\tconst theirs = new Set(solid(h.theirs));\n+\tconst twice = solid(h.yours).filter((l) => theirs.has(l));\n+\tif (twice.length > 0)\n+\t\treturn `keeping both would repeat ${twice.length === 1 ? \"a line\" : `${twice.length} lines`}: ${twice[0]}`;\n+\t// The same key or name set on both sides: `timeoutMs:`, `limit =`.\n+\tconst key = (l: string) => /^([A-Za-z_$\"'][\\w$\"'-]*)\\s*[:=]/.exec(l)?.[1];\n+\tconst theirKeys = new Set(solid(h.theirs).map(key).filter(Boolean));\n+\tconst clash = solid(h.yours)\n+\t\t.map(key)\n+\t\t.find((k) => k && theirKeys.has(k));\n+\treturn clash ? `keeping both would set ${clash} twice` : undefined;\n+}"},{"path":"scripts/autosettle/workers-ai.ts","added":24,"removed":2,"diff":"--- a/scripts/autosettle/workers-ai.ts\n+++ b/scripts/autosettle/workers-ai.ts\n@@ -1,6 +1,7 @@\n // Ask the local auto-settle Worker (workers-ai/, run with wrangler dev) how a hold should be settled (#19).\n \n import type { Hold, Pick } from \"./jev.ts\";\n+import { refused, ruled } from \"./rules.ts\";\n \n export const WORKER = process.env.AUTOSETTLE_WORKER ?? \"http://127.0.0.1:8845\";\n \n@@ -14,14 +15,34 @@\n \t/** The model's own time, as the Worker measured it. */\n \treadonly modelMs: number;\n \treadonly inputTokens: number;\n+\t/** rule: code decided without the model. turned down: the model's pick would break the file, so an agent settles. */\n+\treadonly by: \"rule\" | \"model\" | \"turned down\";\n+\t/** The model's own pick, when it was turned down. */\n+\treadonly modelPick?: Pick;\n }\n \n-export async function askWorkersAi(h: Hold, opts: { model?: string; url?: string } = {}): Promise<ModelVerdict> {\n+/** Code's rules first, then the model, then code's check on what the model picked. */\n+export async function askWorkersAi(\n+\th: Hold,\n+\topts: { model?: string; url?: string; samples?: number } = {},\n+): Promise<ModelVerdict> {\n+\tconst rule = ruled(h);\n+\tif (rule) return { ...rule, confidence: 1, model: \"rule\", ms: 0, modelMs: 0, inputTokens: 0, by: \"rule\" };\n+\tconst v = await askModel(h, opts);\n+\tconst no = refused(h, v.pick);\n+\treturn no ? { ...v, pick: \"agent\", reason: no, by: \"turned down\", modelPick: v.pick } : v;\n+}\n+\n+async function askModel(h: Hold, opts: { model?: string; url?: string; samples?: number }): Promise<ModelVerdict> {\n \tconst started = performance.now();\n \tconst res = await fetch(opts.url ?? WORKER, {\n \t\tmethod: \"POST\",\n \t\theaders: { \"content-type\": \"application/json\" },\n-\t\tbody: JSON.stringify({ hold: h, ...(opts.model ? { model: opts.model } : {}) }),\n+\t\tbody: JSON.stringify({\n+\t\t\thold: h,\n+\t\t\t...(opts.model ? { model: opts.model } : {}),\n+\t\t\tsamples: opts.samples ?? Number(process.env.AUTOSETTLE_SAMPLES ?? 1),\n+\t\t}),\n \t});\n \tconst out = (await res.json()) as {\n \t\tpick?: Pick;\n@@ -41,5 +62,6 @@\n \t\tms: performance.now() - started,\n \t\tmodelMs: out.ms,\n \t\tinputTokens: out.usage?.prompt_tokens ?? 0,\n+\t\tby: \"model\",\n \t};\n }"},{"path":"scripts/autosettle/workers-ai/index.ts","added":27,"removed":6,"diff":"--- a/scripts/autosettle/workers-ai/index.ts\n+++ b/scripts/autosettle/workers-ai/index.ts\n@@ -74,16 +74,22 @@\n export default {\n \tasync fetch(request: Request, env: Env): Promise<Response> {\n \t\tif (request.method !== \"POST\") return new Response(\"POST a hold\", { status: 405 });\n-\t\tconst { hold, model = DEFAULT_MODEL } = (await request.json()) as { hold: Hold; model?: string };\n+\t\tconst {\n+\t\t\thold,\n+\t\t\tmodel = DEFAULT_MODEL,\n+\t\t\tsamples = 1,\n+\t\t} = (await request.json()) as { hold: Hold; model?: string; samples?: number };\n \t\tconst started = Date.now();\n-\t\ttry {\n+\t\t// One sample answers at temperature 0 with the model's own confidence. Several sample at 0.7 in parallel,\n+\t\t// and the confidence is the share that agree with the most common pick: steadier than a self-report.\n+\t\tconst one = async () => {\n \t\t\tconst out = (await env.AI.run(model, {\n \t\t\t\tmessages: [\n \t\t\t\t\t{ role: \"system\", content: SYSTEM },\n \t\t\t\t\t{ role: \"user\", content: prompt(hold) },\n \t\t\t\t],\n \t\t\t\tresponse_format: { type: \"json_schema\", json_schema: SCHEMA },\n-\t\t\t\ttemperature: 0,\n+\t\t\t\ttemperature: samples > 1 ? 0.7 : 0,\n \t\t\t\tmax_tokens: 200,\n \t\t\t})) as { response: unknown; usage?: { prompt_tokens?: number; completion_tokens?: number } };\n \t\t\tconst answer = (typeof out.response === \"string\" ? JSON.parse(out.response) : out.response) as {\n@@ -91,9 +97,24 @@\n \t\t\t\tconfidence: number;\n \t\t\t\treason: string;\n \t\t\t};\n-\t\t\tif (!(PICKS as readonly string[]).includes(answer.pick))\n-\t\t\t\treturn Response.json({ error: `not a settle choice: ${answer.pick}` }, { status: 502 });\n-\t\t\treturn Response.json({ ...answer, model, ms: Date.now() - started, usage: out.usage ?? null });\n+\t\t\tif (!(PICKS as readonly string[]).includes(answer?.pick)) throw new Error(`not a settle choice: ${answer?.pick}`);\n+\t\t\treturn { ...answer, usage: out.usage ?? null };\n+\t\t};\n+\t\ttry {\n+\t\t\tconst answers = await Promise.all(Array.from({ length: Math.min(9, Math.max(1, samples)) }, one));\n+\t\t\tconst votes = Object.fromEntries(PICKS.map((p) => [p, answers.filter((a) => a.pick === p).length]));\n+\t\t\tconst pick = PICKS.reduce((a, b) => (votes[b]! > votes[a]! ? b : a));\n+\t\t\tconst first = answers.find((a) => a.pick === pick)!;\n+\t\t\treturn Response.json({\n+\t\t\t\tpick,\n+\t\t\t\tconfidence: answers.length > 1 ? votes[pick]! / answers.length : first.confidence,\n+\t\t\t\tselfReported: answers.map((a) => a.confidence),\n+\t\t\t\tvotes,\n+\t\t\t\treason: first.reason,\n+\t\t\t\tmodel,\n+\t\t\t\tms: Date.now() - started,\n+\t\t\t\tusage: first.usage,\n+\t\t\t});\n \t\t} catch (e) {\n \t\t\treturn Response.json({ error: (e as Error).message, model, ms: Date.now() - started }, { status: 502 });\n \t\t}"}],"ci":null}