- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/scripts/autosettle/score.ts?version=192&format=json
127 linesautosettle 100%Last change autosettle in version 191, 2 versions changed it
You're reading version 192. The newest is 318, and 1 version since then changed this file.
1// Score a model against how agents really settled past holds (#19).
2//
3// node scripts/autosettle/score.ts <cases.json> [--via workers-ai|jev] [--model <id>] [--out <report.json>]
4//
5// --via workers-ai (the default) needs the Worker in workers-ai/ running under wrangler dev.
6//
7// A case is one settled collision: base, yours, theirsLines, context, both
8// pushes' messages, and `kept` from the server's settled record (yours, theirs,
9// both, or rewrote, which counts as "agent": a hand edit). --via jev needs JEV_API_KEY.
11import { readFileSync, writeFileSync } from "node:fs";
12import { ask, dollars, type Hold, PICKS, type Pick } from "./jev.ts";
13import { askWorkersAi } from "./workers-ai.ts";
15/** What score needs from either model. */
16interface Verdict {
17 pick: Pick;
18 confidence: number;
19 model: string;
20 ms: number;
21 inputTokens: number;
22 reason?: string;
23 probabilities?: Record<Pick, number>;
24 orders?: readonly { pick: Pick }[];
25}
27interface Case {
28 id: string;
29 file: string;
30 kind?: string;
31 settler: { agent: string; heldMessage?: string; settleMessage?: string };
32 theirs: { agent: string; version: number; message?: string };
33 base: string[] | null;
34 yours: string[] | null;
35 theirsLines: string[] | null;
36 context?: { before?: string[]; after?: string[] };
37 kept: "yours" | "theirs" | "both" | "rewrote";
38 issue?: string | null;
39}
41const truth = (kept: Case["kept"]): Pick => (kept === "rewrote" ? "agent" : kept);
43function holdOf(c: Case): Hold {
44 return {
45 file: c.file,
46 base: c.base ?? [],
47 yours: c.yours ?? [],
48 theirs: c.theirsLines ?? [],
49 before: c.context?.before ?? [],
50 after: c.context?.after ?? [],
51 yourAgent: c.settler.agent,
52 ...(c.settler.heldMessage ? { yourMessage: c.settler.heldMessage } : {}),
53 theirAgent: c.theirs.agent,
54 ...(c.theirs.message ? { theirMessage: c.theirs.message } : {}),
55 ...(c.issue ? { issue: c.issue } : {}),
56 };
57}
59function flag(args: string[], name: string): string | undefined {
60 const i = args.indexOf(name);
61 return i === -1 ? undefined : args[i + 1];
62}
64const args = process.argv.slice(2);
65const via = flag(args, "--via") ?? "workers-ai";
66const cases = (JSON.parse(readFileSync(args[0]!, "utf8")) as Case[]).filter(
67 (c) => c.yours !== null && c.theirsLines !== null,
68);
69const rows: { id: string; truth: Pick; verdict: Verdict; right: boolean }[] = [];
70for (const c of cases) {
71 try {
72 const model = flag(args, "--model") ? { model: flag(args, "--model")! } : {};
73 const verdict: Verdict = via === "jev" ? await ask(holdOf(c), model) : await askWorkersAi(holdOf(c), model);
74 const t = truth(c.kept);
75 rows.push({ id: c.id, truth: t, verdict, right: verdict.pick === t });
76 console.log(
77 `${verdict.pick === t ? "agree" : "DIFFER"} ${c.id}: agent ${t}, model ${verdict.pick} ${verdict.confidence.toFixed(2)} (${verdict.ms.toFixed(0)} ms)`,
78 );
79 } catch (e) {
80 console.log(`error ${c.id}: ${(e as Error).message}`);
81 }
82}
84const mean = (xs: number[]) => (xs.length === 0 ? Number.NaN : xs.reduce((a, b) => a + b, 0) / xs.length);
85const right = rows.filter((r) => r.right);
86const wrong = rows.filter((r) => !r.right);
87const ms = rows.map((r) => r.verdict.ms).sort((a, b) => a - b);
88const tokens = rows.map((r) => r.verdict.inputTokens);
89const at = (threshold: number) => {
90 const sure = rows.filter((r) => r.verdict.confidence >= threshold);
91 return { threshold, settled: sure.length, agreed: sure.filter((r) => r.right).length };
92};
93const report = {
94 via,
95 model: rows[0]?.verdict.model,
96 cases: rows.length,
97 skipped: cases.length - rows.length,
98 agreed: right.length,
99 confidenceWhenRight: mean(right.map((r) => r.verdict.confidence)),
100 confidenceWhenWrong: mean(wrong.map((r) => r.verdict.confidence)),
101 thresholds: [0.3, 0.5, 0.6, 0.7, 0.8, 0.9].map(at),
102 confusion: Object.fromEntries(
103 PICKS.map((t) => [
104 t,
105 Object.fromEntries(PICKS.map((p) => [p, rows.filter((r) => r.truth === t && r.verdict.pick === p).length])),
106 ]),
107 ),
108 ordersDisagreed: rows.filter((r) => r.verdict.orders && r.verdict.orders[0]!.pick !== r.verdict.orders[1]!.pick)
109 .length,
110 latencyMs: { median: ms[Math.floor(ms.length / 2)], max: ms[ms.length - 1], mean: mean(ms) },
111 inputTokensMean: mean(tokens),
112 ...(via === "jev" ? { dollarsPerCall: dollars(mean(tokens)) } : {}),
113 rows: rows.map((r) => ({
114 id: r.id,
115 agent: r.truth,
116 model: r.verdict.pick,
117 confidence: r.verdict.confidence,
118 probabilities: r.verdict.probabilities,
119 orders: r.verdict.orders?.map((o) => o.pick),
120 reason: r.verdict.reason,
121 ms: Math.round(r.verdict.ms),
122 })),
123};
124const out = flag(args, "--out");
125if (out) writeFileSync(out, JSON.stringify(report, null, "\t"));
126const { rows: _, ...summary } = report;
127console.log(JSON.stringify(summary, null, "\t"));