{"version":198,"agent":"autosettle","message":"Auto-settle (#19): Clef, Cloudflare's decision model on Workers AI, as a settler. Clef takes Jev's System One request, so clef.ts sends jev.ts's own questions through the local Worker's new /system-one route, with the same code checks before and after. score.ts and settle-one.ts take --via clef (--model clef or clef-flash). Six local holds: 6 of 6 right, 0.4 to 0.6 s each.","time":1792023876571,"timeIso":"2026-10-15T00:24:36.571Z","changes":6,"change":3367,"files":["scripts/autosettle/clef.ts","scripts/autosettle/jev.ts","scripts/autosettle/score.ts","scripts/autosettle/settle-one.ts","scripts/autosettle/workers-ai/index.ts"],"draft":null,"diff":[{"path":"scripts/autosettle/clef.ts","added":45,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/clef.ts\n@@ -0,0 +1,45 @@\n+// Ask Clef, Cloudflare's decision model on Workers AI, how a hold should be settled (#19). Clef takes Jev's\n+// System One request (state and typed questions) and answers with a probability per option, so the questions are\n+// jev.ts's own; it goes through the local Worker (workers-ai/, wrangler dev), which has the AI binding.\n+\n+import { type Hold, type SystemOneAnswer, systemOneBody, type Verdict, verdictOf } from \"./jev.ts\";\n+import { refused, ruled } from \"./rules.ts\";\n+import { WORKER } from \"./workers-ai.ts\";\n+\n+export interface ClefVerdict extends Verdict {\n+\treadonly reason?: string;\n+\treadonly by: \"rule\" | \"model\" | \"turned down\";\n+\treadonly modelPick?: Verdict[\"pick\"];\n+}\n+\n+/** Code's rules first, then Clef, then code's check on its pick, as for the Workers AI text model. */\n+export async function askClef(\n+\th: Hold,\n+\topts: { model?: \"clef\" | \"clef-flash\"; url?: string } = {},\n+): Promise<ClefVerdict> {\n+\tconst rule = ruled(h);\n+\tif (rule)\n+\t\treturn {\n+\t\t\tpick: rule.pick,\n+\t\t\treason: rule.reason,\n+\t\t\tconfidence: 1,\n+\t\t\tprobabilities: { yours: 0, theirs: 0, both: 0, agent: 0, [rule.pick]: 1 },\n+\t\t\torders: [],\n+\t\t\tsigns: {},\n+\t\t\tmodel: \"rule\",\n+\t\t\tms: 0,\n+\t\t\tinputTokens: 0,\n+\t\t\tby: \"rule\",\n+\t\t};\n+\tconst started = performance.now();\n+\tconst res = await fetch(`${opts.url ?? WORKER}/system-one`, {\n+\t\tmethod: \"POST\",\n+\t\theaders: { \"content-type\": \"application/json\" },\n+\t\tbody: JSON.stringify(systemOneBody(h, opts.model ?? \"clef\")),\n+\t});\n+\tconst out = (await res.json()) as SystemOneAnswer & { error?: string };\n+\tif (!res.ok || out.error || !out.answers) throw new Error(`Clef: ${out.error ?? res.status}`);\n+\tconst v = verdictOf({ ...out, model: out.model ?? opts.model ?? \"clef\" }, performance.now() - started);\n+\tconst no = refused(h, v.pick);\n+\treturn no ? { ...v, pick: \"agent\", reason: no, by: \"turned down\", modelPick: v.pick } : { ...v, by: \"model\" };\n+}"},{"path":"scripts/autosettle/jev.ts","added":26,"removed":15,"diff":"--- a/scripts/autosettle/jev.ts\n+++ b/scripts/autosettle/jev.ts\n@@ -124,32 +124,43 @@\n \treturn PICKS.reduce((a, b) => (p[b] > p[a] ? b : a));\n }\n \n-/** Ask Jev once, with the choice in two option orders (Jev's docs warn order can sway a Choice) and the side questions. */\n-export async function ask(h: Hold, opts: { model?: string; key?: string } = {}): Promise<Verdict> {\n-\tconst forward = PICKS;\n-\tconst backward = [...PICKS].reverse();\n-\tconst body = {\n-\t\tmodel: opts.model ?? \"jev-latest\",\n+/**\n+ * One System One request (TypeSafe's API, which Cloudflare's Clef also takes): the choice in two option orders\n+ * (Jev's docs warn order can sway a Choice) and the side questions.\n+ */\n+export function systemOneBody(h: Hold, model: string) {\n+\treturn {\n+\t\tmodel,\n \t\tstate: stateOf(h),\n \t\tquestions: {\n-\t\t\tsettle: choice(forward),\n-\t\t\tsettle_reversed: choice(backward),\n+\t\t\tsettle: choice(PICKS),\n+\t\t\tsettle_reversed: choice([...PICKS].reverse()),\n \t\t\t...Object.fromEntries(Object.entries(SIGNS).map(([id, q]) => [id, { type: \"noul\", instructions: q }])),\n \t\t},\n \t};\n+}\n+\n+export interface SystemOneAnswer {\n+\tmodel: string;\n+\tanswers: Record<string, { type: string; probabilities?: Record<string, number>; noul?: number; choice?: string }>;\n+\tusage?: { input_tokens?: number };\n+}\n+\n+/** Ask Jev once. */\n+export async function ask(h: Hold, opts: { model?: string; key?: string } = {}): Promise<Verdict> {\n \tconst started = performance.now();\n \tconst res = await fetch(ENDPOINT, {\n \t\tmethod: \"POST\",\n \t\theaders: { authorization: `Bearer ${opts.key ?? apiKey()}`, \"content-type\": \"application/json\" },\n-\t\tbody: JSON.stringify(body),\n+\t\tbody: JSON.stringify(systemOneBody(h, opts.model ?? \"jev-latest\")),\n \t});\n \tconst ms = performance.now() - started;\n \tif (!res.ok) throw new Error(`Jev answered ${res.status}: ${(await res.text()).slice(0, 500)}`);\n-\tconst out = (await res.json()) as {\n-\t\tmodel: string;\n-\t\tanswers: Record<string, { type: string; probabilities?: Record<string, number>; noul?: number; choice?: string }>;\n-\t\tusage: { input_tokens: number };\n-\t};\n+\treturn verdictOf((await res.json()) as SystemOneAnswer, ms);\n+}\n+\n+/** A System One answer to systemOneBody's questions, as a Verdict. */\n+export function verdictOf(out: SystemOneAnswer, ms: number): Verdict {\n \tconst orders = [\"settle\", \"settle_reversed\"].map((id) => {\n \t\tconst probabilities = Object.fromEntries(PICKS.map((k) => [k, out.answers[id]!.probabilities![k] ?? 0])) as Record<\n \t\t\tPick,\n@@ -168,7 +179,7 @@\n \t\tsigns: Object.fromEntries(Object.keys(SIGNS).map((id) => [id, out.answers[id]?.noul ?? Number.NaN])),\n \t\tmodel: out.model,\n \t\tms,\n-\t\tinputTokens: out.usage.input_tokens,\n+\t\tinputTokens: out.usage?.input_tokens ?? 0,\n \t};\n }\n "},{"path":"scripts/autosettle/score.ts","added":8,"removed":2,"diff":"--- a/scripts/autosettle/score.ts\n+++ b/scripts/autosettle/score.ts\n@@ -1,6 +1,6 @@\n // Score a model against how agents really settled past holds (#19).\n //\n-//   node scripts/autosettle/score.ts <cases.json> [--via workers-ai|jev] [--model <id>] [--out <report.json>]\n+//   node scripts/autosettle/score.ts <cases.json> [--via workers-ai|clef|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@@ -9,6 +9,7 @@\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 { askClef } from \"./clef.ts\";\n import { ask, dollars, type Hold, PICKS, type Pick } from \"./jev.ts\";\n import { askWorkersAi } from \"./workers-ai.ts\";\n \n@@ -70,7 +71,12 @@\n for (const c of cases) {\n \ttry {\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 verdict: Verdict =\n+\t\t\tvia === \"jev\"\n+\t\t\t\t? await ask(holdOf(c), model)\n+\t\t\t\t: via === \"clef\"\n+\t\t\t\t\t? await askClef(holdOf(c), { model: (model.model as \"clef\" | \"clef-flash\") ?? \"clef\" })\n+\t\t\t\t\t: 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("},{"path":"scripts/autosettle/settle-one.ts","added":11,"removed":1,"diff":"--- a/scripts/autosettle/settle-one.ts\n+++ b/scripts/autosettle/settle-one.ts\n@@ -1,12 +1,13 @@\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> [--via workers-ai|jev] [--model <id>] [--message \"<your held push's message>\"] [--issue \"...\"] [--name <path in the project>] [--json]\n+//   node scripts/autosettle/settle-one.ts <file> [--via workers-ai|clef|jev] [--model <id>] [--message \"<your held push's message>\"] [--issue \"...\"] [--name <path in the project>] [--json]\n //\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 { askClef } from \"./clef.ts\";\n import { ask, dollars, type Hold, PICKS } from \"./jev.ts\";\n import { askWorkersAi } from \"./workers-ai.ts\";\n \n@@ -60,6 +61,15 @@\n \tconst results = [];\n \tconst model = flag(args, \"--model\") ? { model: flag(args, \"--model\")! } : {};\n \tfor (const [i, h] of holds.entries()) {\n+\t\tif (flag(args, \"--via\") === \"clef\") {\n+\t\t\tconst c = await askClef(h, { model: (flag(args, \"--model\") as \"clef\" | \"clef-flash\" | undefined) ?? \"clef\" });\n+\t\t\tresults.push({ block: i + 1, ...c });\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}: ${c.pick}, confidence ${c.confidence.toFixed(2)} (${c.by}${c.modelPick ? `, Clef said ${c.modelPick}` : \"\"})\\n  probabilities: ${PICKS.map((k) => `${k} ${(c.probabilities[k] * 100).toFixed(0)}%`).join(\", \")}\\n  ${c.model}, ${c.ms.toFixed(0)} ms`,\n+\t\t\t\t);\n+\t\t\tcontinue;\n+\t\t}\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 });"},{"path":"scripts/autosettle/workers-ai/index.ts","added":12,"removed":0,"diff":"--- a/scripts/autosettle/workers-ai/index.ts\n+++ b/scripts/autosettle/workers-ai/index.ts\n@@ -74,6 +74,18 @@\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\t// POST /system-one: a System One request (state and typed questions) for a Clef decision model, passed through.\n+\t\tif (new URL(request.url).pathname === \"/system-one\") {\n+\t\t\tconst body = (await request.json()) as { model?: string };\n+\t\t\tconst id = body.model === \"clef-flash\" ? \"@cf/cloudflare/clef-flash\" : \"@cf/cloudflare/clef\";\n+\t\t\tconst started = Date.now();\n+\t\t\ttry {\n+\t\t\t\tconst out = await env.AI.run(id, { ...body, model: body.model ?? \"clef\" });\n+\t\t\t\treturn Response.json({ ...(out as object), ms: Date.now() - started });\n+\t\t\t} catch (e) {\n+\t\t\t\treturn Response.json({ error: (e as Error).message, model: id }, { status: 502 });\n+\t\t\t}\n+\t\t}\n \t\tconst {\n \t\t\thold,\n \t\t\tmodel = DEFAULT_MODEL,"}],"ci":null}