{"version":191,"agent":"autosettle","message":"Auto-settle (#19): a Workers AI settler. scripts/autosettle/workers-ai is a Worker (wrangler dev --port 8845) that asks Llama 3.3 70B, in JSON mode, for one of keep yours, keep theirs, keep both or needs an agent, plus a confidence and a one-line reason. settle-one and score use it by default (--via jev for Jev). On the commonmark hold (nesting 64 vs 1000) it picks needs an agent, 0.90, in 2.5 s.","time":1792017924660,"timeIso":"2026-10-14T22:45:24.660Z","changes":10,"change":3195,"files":["scripts/autosettle/extract.ts","scripts/autosettle/score.ts","scripts/autosettle/settle-one.ts","scripts/autosettle/workers-ai.ts","scripts/autosettle/workers-ai/index.ts","scripts/autosettle/workers-ai/wrangler.jsonc"],"draft":null,"diff":[{"path":"scripts/autosettle/extract.ts","added":1,"removed":1,"diff":"--- a/scripts/autosettle/extract.ts\n+++ b/scripts/autosettle/extract.ts\n@@ -85,7 +85,7 @@\n \treturn to < 0 ? null : r.slice(from, to);\n }\n \n-const cases = [];\n+const cases: { kept: string; yours: string[] | null }[] = [];\n for (const c of review.changes) {\n \tfor (const s of c.settled) {\n \t\tconst before = fileAt(s.theirs.version - 1, s.file);"},{"path":"scripts/autosettle/score.ts","added":30,"removed":11,"diff":"--- a/scripts/autosettle/score.ts\n+++ b/scripts/autosettle/score.ts\n@@ -1,14 +1,29 @@\n-// Score Jev against how agents really settled past holds (#19).\n+// Score a model against how agents really settled past holds (#19).\n //\n-//   node scripts/autosettle/score.ts <cases.json> [--out <report.json>] [--model jev-latest]\n+//   node scripts/autosettle/score.ts <cases.json> [--via workers-ai|jev] [--model <id>] [--out <report.json>]\n //\n+// --via workers-ai (the default) needs the Worker in workers-ai/ running under wrangler dev.\n+//\n // A case is one settled collision: base, yours, theirsLines, context, both\n // pushes' messages, and `kept` from the server's settled record (yours, theirs,\n-// both, or rewrote, which counts as \"agent\": a hand edit). Needs JEV_API_KEY.\n+// both, or rewrote, which counts as \"agent\": a hand edit). --via jev needs JEV_API_KEY.\n \n import { readFileSync, writeFileSync } from \"node:fs\";\n-import { ask, dollars, type Hold, PICKS, type Pick, type Verdict } from \"./jev.ts\";\n+import { ask, dollars, type Hold, PICKS, type Pick } from \"./jev.ts\";\n+import { askWorkersAi } from \"./workers-ai.ts\";\n \n+/** What score needs from either model. */\n+interface Verdict {\n+\tpick: Pick;\n+\tconfidence: number;\n+\tmodel: string;\n+\tms: number;\n+\tinputTokens: number;\n+\treason?: string;\n+\tprobabilities?: Record<Pick, number>;\n+\torders?: readonly { pick: Pick }[];\n+}\n+\n interface Case {\n \tid: string;\n \tfile: string;\n@@ -47,17 +62,19 @@\n }\n \n const args = process.argv.slice(2);\n+const via = flag(args, \"--via\") ?? \"workers-ai\";\n const cases = (JSON.parse(readFileSync(args[0]!, \"utf8\")) as Case[]).filter(\n \t(c) => c.yours !== null && c.theirsLines !== null,\n );\n const rows: { id: string; truth: Pick; verdict: Verdict; right: boolean }[] = [];\n for (const c of cases) {\n \ttry {\n-\t\tconst verdict = await ask(holdOf(c), { ...(flag(args, \"--model\") ? { model: flag(args, \"--model\") } : {}) });\n+\t\tconst model = flag(args, \"--model\") ? { model: flag(args, \"--model\")! } : {};\n+\t\tconst verdict: Verdict = via === \"jev\" ? await ask(holdOf(c), model) : await askWorkersAi(holdOf(c), model);\n \t\tconst t = truth(c.kept);\n \t\trows.push({ id: c.id, truth: t, verdict, right: verdict.pick === t });\n \t\tconsole.log(\n-\t\t\t`${verdict.pick === t ? \"agree\" : \"DIFFER\"}  ${c.id}: agent ${t}, Jev ${verdict.pick} ${verdict.confidence.toFixed(2)} (${verdict.ms.toFixed(0)} ms)`,\n+\t\t\t`${verdict.pick === t ? \"agree\" : \"DIFFER\"}  ${c.id}: agent ${t}, model ${verdict.pick} ${verdict.confidence.toFixed(2)} (${verdict.ms.toFixed(0)} ms)`,\n \t\t);\n \t} catch (e) {\n \t\tconsole.log(`error   ${c.id}: ${(e as Error).message}`);\n@@ -74,6 +91,7 @@\n \treturn { threshold, settled: sure.length, agreed: sure.filter((r) => r.right).length };\n };\n const report = {\n+\tvia,\n \tmodel: rows[0]?.verdict.model,\n \tcases: rows.length,\n \tskipped: cases.length - rows.length,\n@@ -87,18 +105,19 @@\n \t\t\tObject.fromEntries(PICKS.map((p) => [p, rows.filter((r) => r.truth === t && r.verdict.pick === p).length])),\n \t\t]),\n \t),\n-\tordersDisagreed: rows.filter((r) => r.verdict.orders[0]!.pick !== r.verdict.orders[1]!.pick).length,\n+\tordersDisagreed: rows.filter((r) => r.verdict.orders && r.verdict.orders[0]!.pick !== r.verdict.orders[1]!.pick)\n+\t\t.length,\n \tlatencyMs: { median: ms[Math.floor(ms.length / 2)], max: ms[ms.length - 1], mean: mean(ms) },\n \tinputTokensMean: mean(tokens),\n-\tdollarsPerCall: dollars(mean(tokens)),\n+\t...(via === \"jev\" ? { dollarsPerCall: dollars(mean(tokens)) } : {}),\n \trows: rows.map((r) => ({\n \t\tid: r.id,\n \t\tagent: r.truth,\n-\t\tjev: r.verdict.pick,\n+\t\tmodel: r.verdict.pick,\n \t\tconfidence: r.verdict.confidence,\n \t\tprobabilities: r.verdict.probabilities,\n-\t\torders: r.verdict.orders.map((o) => o.pick),\n-\t\tsigns: r.verdict.signs,\n+\t\torders: r.verdict.orders?.map((o) => o.pick),\n+\t\treason: r.verdict.reason,\n \t\tms: Math.round(r.verdict.ms),\n \t})),\n };"},{"path":"scripts/autosettle/settle-one.ts","added":16,"removed":5,"diff":"--- a/scripts/autosettle/settle-one.ts\n+++ b/scripts/autosettle/settle-one.ts\n@@ -1,13 +1,14 @@\n-// Ask Jev how to settle each marker block in one file (#19), the way abso pull\n-// leaves a held push's file. Prints Jev's pick and its probabilities; changes nothing.\n+// Ask a model how to settle each marker block in one file (#19), the way abso\n+// pull leaves a held push's file. Prints its pick; changes nothing.\n //\n-//   node scripts/autosettle/settle-one.ts <file> [--message \"<your held push's message>\"] [--issue \"...\"] [--name <path in the project>] [--json]\n+//   node scripts/autosettle/settle-one.ts <file> [--via workers-ai|jev] [--model <id>] [--message \"<your held push's message>\"] [--issue \"...\"] [--name <path in the project>] [--json]\n //\n-// Needs JEV_API_KEY in the environment.\n+// --via workers-ai (the default) needs workers-ai/ running under wrangler dev; --via jev needs JEV_API_KEY.\n \n import { readFileSync } from \"node:fs\";\n import { findBlocks } from \"../../client/src/settle.ts\";\n import { ask, dollars, type Hold, PICKS } from \"./jev.ts\";\n+import { askWorkersAi } from \"./workers-ai.ts\";\n \n const CONTEXT = 15;\n const CLOSE = /^>>>>>>> theirs: (.*?), version (\\d+) \"(.*)\"$/;\n@@ -57,8 +58,18 @@\n \t\tprocess.exit(1);\n \t}\n \tconst results = [];\n+\tconst model = flag(args, \"--model\") ? { model: flag(args, \"--model\")! } : {};\n \tfor (const [i, h] of holds.entries()) {\n-\t\tconst v = await ask(h);\n+\t\tif (flag(args, \"--via\") !== \"jev\") {\n+\t\t\tconst w = await askWorkersAi(h, model);\n+\t\t\tresults.push({ block: i + 1, ...w });\n+\t\t\tif (!args.includes(\"--json\"))\n+\t\t\t\tconsole.log(\n+\t\t\t\t\t`Block ${i + 1} of ${holds.length} in ${h.file}: ${w.pick}, confidence ${w.confidence.toFixed(2)}\\n  ${w.reason}\\n  ${w.model}, ${w.ms.toFixed(0)} ms (model ${w.modelMs} ms), ${w.inputTokens} input tokens`,\n+\t\t\t\t);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tconst v = await ask(h, model);\n \t\tresults.push({ block: i + 1, ...v });\n \t\tif (args.includes(\"--json\")) continue;\n \t\tconst pct = (x: number) => `${(x * 100).toFixed(0)}%`;"},{"path":"scripts/autosettle/workers-ai.ts","added":45,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/workers-ai.ts\n@@ -0,0 +1,45 @@\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+\n+export const WORKER = process.env.AUTOSETTLE_WORKER ?? \"http://127.0.0.1:8845\";\n+\n+export interface ModelVerdict {\n+\treadonly pick: Pick;\n+\treadonly confidence: number;\n+\treadonly reason: string;\n+\treadonly model: string;\n+\t/** Round trip from here, through wrangler dev, to Workers AI and back. */\n+\treadonly ms: number;\n+\t/** The model's own time, as the Worker measured it. */\n+\treadonly modelMs: number;\n+\treadonly inputTokens: number;\n+}\n+\n+export async function askWorkersAi(h: Hold, opts: { model?: string; url?: string } = {}): 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});\n+\tconst out = (await res.json()) as {\n+\t\tpick?: Pick;\n+\t\tconfidence?: number;\n+\t\treason?: string;\n+\t\tmodel: string;\n+\t\tms: number;\n+\t\terror?: string;\n+\t\tusage?: { prompt_tokens?: number } | null;\n+\t};\n+\tif (!res.ok || out.error || !out.pick) throw new Error(`Workers AI: ${out.error ?? res.status}`);\n+\treturn {\n+\t\tpick: out.pick,\n+\t\tconfidence: Math.min(1, Math.max(0, out.confidence ?? 0)),\n+\t\treason: out.reason ?? \"\",\n+\t\tmodel: out.model,\n+\t\tms: performance.now() - started,\n+\t\tmodelMs: out.ms,\n+\t\tinputTokens: out.usage?.prompt_tokens ?? 0,\n+\t};\n+}"},{"path":"scripts/autosettle/workers-ai/index.ts","added":101,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/workers-ai/index.ts\n@@ -0,0 +1,101 @@\n+// A Worker that asks a Workers AI text model how a held push's collision should\n+// be settled (#19). JSON mode limits the answer to abso's settle choices, a\n+// confidence and a one-line reason: the model picks, it never writes code.\n+//\n+//   cd scripts/autosettle/workers-ai && npx wrangler dev --port 8845   (the AI binding always runs on Cloudflare)\n+//   POST / with {\"hold\": <Hold>, \"model\"?: \"<@cf/... model>\"}\n+\n+interface Env {\n+\treadonly AI: { run(model: string, input: unknown): Promise<unknown> };\n+}\n+\n+interface Hold {\n+\treadonly file: string;\n+\treadonly base: readonly string[];\n+\treadonly yours: readonly string[];\n+\treadonly theirs: readonly string[];\n+\treadonly before: readonly string[];\n+\treadonly after: readonly string[];\n+\treadonly yourMessage?: string;\n+\treadonly yourAgent?: string;\n+\treadonly theirMessage?: string;\n+\treadonly theirAgent?: string;\n+\treadonly issue?: string;\n+}\n+\n+const PICKS = [\"yours\", \"theirs\", \"both\", \"agent\"] as const;\n+const DEFAULT_MODEL = \"@cf/meta/llama-3.3-70b-instruct-fp8-fast\";\n+\n+const SYSTEM = `You settle merge collisions in a version control system for coding agents.\n+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.\n+Pick exactly one:\n+- \"yours\": keep YOURS only. YOURS already does what THEIRS meant, or THEIRS is wrong or out of date.\n+- \"theirs\": keep THEIRS only. THEIRS already does what YOURS meant, or YOURS is wrong or out of date.\n+- \"both\": keep both unchanged, YOURS then THEIRS. They add separate things that work side by side, with nothing duplicated or contradictory.\n+- \"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.\n+Give \"confidence\" from 0 to 1: how likely your pick is the correct settle. Give \"reason\" as one plain sentence. Do not write code.`;\n+\n+const block = (lines: readonly string[]) => (lines.length === 0 ? \"(no lines)\" : lines.join(\"\\n\"));\n+\n+function prompt(h: Hold): string {\n+\treturn [\n+\t\t`FILE: ${h.file}`,\n+\t\t`YOUR PUSH (${h.yourAgent ?? \"you\"}): ${h.yourMessage ?? \"(no message)\"}`,\n+\t\t`THEIR PUSH (${h.theirAgent ?? \"the other agent\"}): ${h.theirMessage ?? \"(no message)\"}`,\n+\t\t...(h.issue ? [`ISSUE: ${h.issue}`] : []),\n+\t\t\"\",\n+\t\t\"LINES ABOVE:\",\n+\t\tblock(h.before),\n+\t\t\"\",\n+\t\t\"BEFORE (what both started from):\",\n+\t\tblock(h.base),\n+\t\t\"\",\n+\t\t\"YOURS:\",\n+\t\tblock(h.yours),\n+\t\t\"\",\n+\t\t\"THEIRS:\",\n+\t\tblock(h.theirs),\n+\t\t\"\",\n+\t\t\"LINES BELOW:\",\n+\t\tblock(h.after),\n+\t].join(\"\\n\");\n+}\n+\n+const SCHEMA = {\n+\ttype: \"object\",\n+\tproperties: {\n+\t\tpick: { type: \"string\", enum: PICKS },\n+\t\tconfidence: { type: \"number\", minimum: 0, maximum: 1 },\n+\t\treason: { type: \"string\" },\n+\t},\n+\trequired: [\"pick\", \"confidence\", \"reason\"],\n+};\n+\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 started = Date.now();\n+\t\ttry {\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\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+\t\t\t\tpick: string;\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} catch (e) {\n+\t\t\treturn Response.json({ error: (e as Error).message, model, ms: Date.now() - started }, { status: 502 });\n+\t\t}\n+\t},\n+};"},{"path":"scripts/autosettle/workers-ai/wrangler.jsonc","added":10,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/workers-ai/wrangler.jsonc\n@@ -0,0 +1,10 @@\n+// The auto-settle prototype's Workers AI caller (#19). Local only: npx wrangler dev from this folder.\n+// The AI binding always runs on Cloudflare, under the account wrangler is signed in to.\n+{\n+\t\"$schema\": \"../../../server/node_modules/wrangler/config-schema.json\",\n+\t\"name\": \"abso-autosettle\",\n+\t\"main\": \"index.ts\",\n+\t\"compatibility_date\": \"2026-10-01\",\n+\t\"workers_dev\": false,\n+\t\"ai\": { \"binding\": \"AI\", \"remote\": true }\n+}"}],"ci":null}