{"draft":null,"from":154,"to":318,"files":[{"path":"scripts/autosettle/score.ts","added":133,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/score.ts\n@@ -0,0 +1,133 @@\n+// Score a model against how agents really settled past holds (#19).\n+//\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+// 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). --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+/** 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+\tkind?: string;\n+\tsettler: { agent: string; heldMessage?: string; settleMessage?: string };\n+\ttheirs: { agent: string; version: number; message?: string };\n+\tbase: string[] | null;\n+\tyours: string[] | null;\n+\ttheirsLines: string[] | null;\n+\tcontext?: { before?: string[]; after?: string[] };\n+\tkept: \"yours\" | \"theirs\" | \"both\" | \"rewrote\";\n+\tissue?: string | null;\n+}\n+\n+const truth = (kept: Case[\"kept\"]): Pick => (kept === \"rewrote\" ? \"agent\" : kept);\n+\n+function holdOf(c: Case): Hold {\n+\treturn {\n+\t\tfile: c.file,\n+\t\tbase: c.base ?? [],\n+\t\tyours: c.yours ?? [],\n+\t\ttheirs: c.theirsLines ?? [],\n+\t\tbefore: c.context?.before ?? [],\n+\t\tafter: c.context?.after ?? [],\n+\t\tyourAgent: c.settler.agent,\n+\t\t...(c.settler.heldMessage ? { yourMessage: c.settler.heldMessage } : {}),\n+\t\ttheirAgent: c.theirs.agent,\n+\t\t...(c.theirs.message ? { theirMessage: c.theirs.message } : {}),\n+\t\t...(c.issue ? { issue: c.issue } : {}),\n+\t};\n+}\n+\n+function flag(args: string[], name: string): string | undefined {\n+\tconst i = args.indexOf(name);\n+\treturn i === -1 ? undefined : args[i + 1];\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 model = flag(args, \"--model\") ? { model: flag(args, \"--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(\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+\t}\n+}\n+\n+const mean = (xs: number[]) => (xs.length === 0 ? Number.NaN : xs.reduce((a, b) => a + b, 0) / xs.length);\n+const right = rows.filter((r) => r.right);\n+const wrong = rows.filter((r) => !r.right);\n+const ms = rows.map((r) => r.verdict.ms).sort((a, b) => a - b);\n+const tokens = rows.map((r) => r.verdict.inputTokens);\n+const at = (threshold: number) => {\n+\tconst sure = rows.filter((r) => r.verdict.confidence >= threshold);\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+\tagreed: right.length,\n+\tconfidenceWhenRight: mean(right.map((r) => r.verdict.confidence)),\n+\tconfidenceWhenWrong: mean(wrong.map((r) => r.verdict.confidence)),\n+\tthresholds: [0.3, 0.5, 0.6, 0.7, 0.8, 0.9].map(at),\n+\tconfusion: Object.fromEntries(\n+\t\tPICKS.map((t) => [\n+\t\t\tt,\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 && 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+\t...(via === \"jev\" ? { dollarsPerCall: dollars(mean(tokens)) } : {}),\n+\trows: rows.map((r) => ({\n+\t\tid: r.id,\n+\t\tagent: r.truth,\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\treason: r.verdict.reason,\n+\t\tms: Math.round(r.verdict.ms),\n+\t})),\n+};\n+const out = flag(args, \"--out\");\n+if (out) writeFileSync(out, JSON.stringify(report, null, \"\\t\"));\n+const { rows: _, ...summary } = report;\n+console.log(JSON.stringify(summary, null, \"\\t\"));"}],"unmatched":[]}