// THE VERDICT BENCH'S SCORE (release 28, M7) — pure. // // Per model, over the items it was asked: // - agreement: the model's verdict equals the operator's, over every item // asked (a failed answer — a parse failure or an error — counts as a miss); // - agreementAdjacent: the same, with PARTLY and CORROBORATED counted as // agreeing with each other ("PARTLY adjacent to CORROBORATED"); // - the confusion matrix, 5×5 in VERDICTS order (rows: the operator's // verdict, columns: the model's), with each row's failed answers beside it // (`unanswered`); // - recall for CONTRADICTED and NOT_FOUND (of the items the operator ruled // so, the share the model ruled so), and for PARTLY strictly and adjacent; // - cost (USD from the Meter), latency (ms per call), parse failures, errors. // - (V1) "not CORROBORATED" as the positive class — the claims a proposal // would put in front of the operator: precision (of the model's // not-CORROBORATED answers, the share the operator ruled so too) and // recall (of the operator's not-CORROBORATED rulings, the share the model // found); a failed answer is a miss, never a positive. // The BASELINE is a model that always answers CORROBORATED, scored the same way // over the same items. A pass against the operator's bar is `passesBar`. // // V1 adds, also pure: `compareBenchRuns` (two runs on the same items: each // model's agreement in each, and the claims whose answers changed) and // `markedScore` (the operator's Right/Wrong marks on disagreements — a // disagreement marked Right counts as the model agreeing — tallied by the // triage marks' `markTally`). import { VERDICTS, type Verdict } from "../../verdicts"; import { markTally, type MarkTally } from "../triage/types"; export type BenchResult = { key: string; model: string; // The model's verdict, or null when it gave none. verdict: Verdict | null; reason?: string; // v3: the claim's parts as the model found them (the verdict follows from // them, ./prompt.ts verdictOfParts), and `checkable: false` when it judged // the claim untestable. parts?: { part: string; finding: "shown" | "shown wrong" | "not shown" }[]; checkable?: false; // `parse`: no structured answer, or one outside the schema; `error`: the // call failed (the API, the network, a prompt over the local cap). failure?: "parse" | "error"; error?: string; ms: number; usd: number; inputTokens: number; outputTokens: number; at: string; // The local prompt's window trim, when one was needed. cuesTrimmed?: number; }; export type Recall = { ruled: number; matched: number; recall: number | null }; // "Not CORROBORATED" as the positive class. export type FlagScore = { tp: number; fp: number; fn: number; precision: number | null; recall: number | null }; export type ModelScore = { model: string; asked: number; answered: number; agreed: number; agreement: number | null; agreedAdjacent: number; agreementAdjacent: number | null; // matrix[operator][model], VERDICTS order. matrix: number[][]; unanswered: number[]; recall: { CONTRADICTED: Recall; NOT_FOUND: Recall; PARTLY: Recall; PARTLY_ADJACENT: Recall }; notCorroborated: FlagScore; parseFailures: number; errors: number; usd: number; latency: { meanMs: number | null; medianMs: number | null; p90Ms: number | null }; }; export type BenchScore = { items: number; // How many items the operator ruled each verdict. ruled: Record; models: ModelScore[]; baseline: ModelScore; }; export type VerdictBar = { agreement: number; contradictedRecall: number; notFoundRecall: number }; export const BASELINE_MODEL = "always-CORROBORATED"; const idx = (v: Verdict) => VERDICTS.indexOf(v); const ratio = (a: number, b: number) => (b > 0 ? Math.round((a / b) * 10_000) / 10_000 : null); const adjacent = (a: Verdict, b: Verdict) => a === b || ((a === "PARTLY" || a === "CORROBORATED") && (b === "PARTLY" || b === "CORROBORATED")); function percentile(sorted: number[], p: number): number | null { if (!sorted.length) return null; const i = Math.min(sorted.length - 1, Math.max(0, Math.ceil(p * sorted.length) - 1)); return sorted[i]!; } function recallOf(matrix: number[][], unanswered: number[], v: Verdict, also?: Verdict): Recall { const row = matrix[idx(v)]!; const ruled = row.reduce((s, n) => s + n, 0) + unanswered[idx(v)]!; const matched = row[idx(v)]! + (also ? row[idx(also)]! : 0); return { ruled, matched, recall: ratio(matched, ruled) }; } // One model's score over `truth` (key → the operator's verdict). Only the // results whose key is in `truth` count; the last result per key wins. export function scoreModel(model: string, truth: ReadonlyMap, results: readonly BenchResult[]): ModelScore { const last = new Map(); for (const r of results) if (r.model === model && truth.has(r.key)) last.set(r.key, r); const matrix = VERDICTS.map(() => VERDICTS.map(() => 0)); const unanswered = VERDICTS.map(() => 0); let agreed = 0; let agreedAdjacent = 0; let parseFailures = 0; let errors = 0; let usd = 0; const ms: number[] = []; const flag = { tp: 0, fp: 0, fn: 0 }; for (const [key, r] of last) { const op = truth.get(key)!; usd += r.usd; if (r.ms > 0) ms.push(r.ms); const pos = r.verdict !== null && r.verdict !== "CORROBORATED"; if (pos && op !== "CORROBORATED") flag.tp++; else if (pos) flag.fp++; else if (op !== "CORROBORATED") flag.fn++; if (r.verdict === null) { unanswered[idx(op)]! += 1; if (r.failure === "parse") parseFailures++; else errors++; continue; } matrix[idx(op)]![idx(r.verdict)]! += 1; if (r.verdict === op) agreed++; if (adjacent(op, r.verdict)) agreedAdjacent++; } const asked = last.size; ms.sort((a, b) => a - b); return { model, asked, answered: asked - parseFailures - errors, agreed, agreement: ratio(agreed, asked), agreedAdjacent, agreementAdjacent: ratio(agreedAdjacent, asked), matrix, unanswered, recall: { CONTRADICTED: recallOf(matrix, unanswered, "CONTRADICTED"), NOT_FOUND: recallOf(matrix, unanswered, "NOT_FOUND"), PARTLY: recallOf(matrix, unanswered, "PARTLY"), PARTLY_ADJACENT: recallOf(matrix, unanswered, "PARTLY", "CORROBORATED"), }, notCorroborated: { ...flag, precision: ratio(flag.tp, flag.tp + flag.fp), recall: ratio(flag.tp, flag.tp + flag.fn) }, parseFailures, errors, usd: Math.round(usd * 1e6) / 1e6, latency: { meanMs: ms.length ? Math.round(ms.reduce((s, n) => s + n, 0) / ms.length) : null, medianMs: percentile(ms, 0.5), p90Ms: percentile(ms, 0.9), }, }; } // The whole run's score: each model in `models` order, and the baseline over // every item. export function scoreBench( items: readonly { key: string; verdict: Verdict }[], results: readonly BenchResult[], models: readonly string[], ): BenchScore { const truth = new Map(items.map((i) => [i.key, i.verdict] as const)); const ruled = Object.fromEntries(VERDICTS.map((v) => [v, 0])) as Record; for (const i of items) ruled[i.verdict]++; const baselineResults: BenchResult[] = items.map((i) => ({ key: i.key, model: BASELINE_MODEL, verdict: "CORROBORATED", ms: 0, usd: 0, inputTokens: 0, outputTokens: 0, at: "", })); return { items: items.length, ruled, models: models.map((m) => scoreModel(m, truth, results)), baseline: scoreModel(BASELINE_MODEL, truth, baselineResults), }; } export type BarCheck = { pass: boolean; misses: string[] }; // A model against the operator's bar (fractions 0..1): strict agreement, and // the CONTRADICTED and NOT_FOUND recalls. A recall with nothing ruled that way // is not a miss (there was nothing to find). export function passesBar(s: ModelScore, bar: VerdictBar): BarCheck { const misses: string[] = []; const pct = (v: number) => `${Math.round(v * 1000) / 10}%`; if ((s.agreement ?? 0) < bar.agreement) misses.push(`agreement ${pct(s.agreement ?? 0)} < ${pct(bar.agreement)}`); const c = s.recall.CONTRADICTED.recall; if (c !== null && c < bar.contradictedRecall) misses.push(`CONTRADICTED recall ${pct(c)} < ${pct(bar.contradictedRecall)}`); const nf = s.recall.NOT_FOUND.recall; if (nf !== null && nf < bar.notFoundRecall) misses.push(`NOT_FOUND recall ${pct(nf)} < ${pct(bar.notFoundRecall)}`); if (s.asked === 0) misses.push("no items asked"); return { pass: misses.length === 0, misses }; } // ─── V1: two runs compared, and the operator's marks ─── type RunLike = { items: readonly { key: string; verdict: Verdict }[]; results: readonly BenchResult[]; models: readonly string[] }; export type AnswerChange = { key: string; ruled: Verdict; a: Verdict | null; b: Verdict | null }; export type ModelCompare = { model: string; a: { agreed: number; asked: number; agreement: number | null }; b: { agreed: number; asked: number; agreement: number | null }; // The claims whose answer changed, the ones b got right that a did not first. changed: AnswerChange[]; // Of the changed: b agrees where a did not (fixed), and the other way (broke). fixed: number; broke: number; }; export type RunCompare = { // The item keys both runs have (a comparison reads only these). items: number; onlyA: number; onlyB: number; models: ModelCompare[]; }; const lastOf = (results: readonly BenchResult[], model: string) => { const m = new Map(); for (const r of results) if (r.model === model) m.set(r.key, r); return m; }; /** * Two runs on the same items: for each model asked in both, its agreement in * each (over the shared items) and every claim whose answer changed. The * operator's verdict is A's (the same items carry the same rulings). Pure. */ export function compareBenchRuns(a: RunLike, b: RunLike): RunCompare { const bKeys = new Map(b.items.map((i) => [i.key, i.verdict] as const)); const shared = a.items.filter((i) => bKeys.has(i.key)); const truth = new Map(shared.map((i) => [i.key, i.verdict] as const)); const models: ModelCompare[] = []; for (const model of a.models.filter((m) => b.models.includes(m))) { const ra = lastOf(a.results, model); const rb = lastOf(b.results, model); const changed: AnswerChange[] = []; let fixed = 0; let broke = 0; const tally = (r: Map) => { let agreed = 0; let asked = 0; for (const [k, v] of truth) { const x = r.get(k); if (!x) continue; asked++; if (x.verdict === v) agreed++; } return { agreed, asked, agreement: ratio(agreed, asked) }; }; for (const [k, ruled] of truth) { const x = ra.get(k); const y = rb.get(k); if (!x || !y || x.verdict === y.verdict) continue; changed.push({ key: k, ruled, a: x.verdict, b: y.verdict }); if (y.verdict === ruled) fixed++; else if (x.verdict === ruled) broke++; } const rank = (c: AnswerChange) => (c.b === c.ruled ? 0 : c.a === c.ruled ? 1 : 2); changed.sort((p, q) => rank(p) - rank(q)); models.push({ model, a: tally(ra), b: tally(rb), changed, fixed, broke }); } return { items: shared.length, onlyA: a.items.length - shared.length, onlyB: b.items.length - shared.length, models }; } // An operator's mark on one disagreement: was the MODEL right? `answer` is the // answer it marked — a mark whose answer has since changed (a resumed run asked // again) no longer applies. export type BenchMark = { verdict: "right" | "wrong"; answer: Verdict | null; at: string }; // model → item key → mark. export type BenchMarks = Record>; export type MarkedScore = { model: string; // The marks that apply (on a disagreement, on the answer it still has). marks: MarkTally; // Agreement and the not-CORROBORATED scores with each disagreement marked // Right counted as the model agreeing. agreed: number; agreement: number | null; notCorroborated: FlagScore; }; /** One model's score with the operator's marks applied. Pure. */ export function markedScore(items: readonly { key: string; verdict: Verdict }[], results: readonly BenchResult[], model: string, marks: BenchMarks): MarkedScore { const mine = marks[model] ?? {}; const last = lastOf(results, model); const applied: BenchMark[] = []; const truth = new Map(); for (const it of items) { const r = last.get(it.key); const m = mine[it.key]; let v = it.verdict; if (r && m && r.verdict !== it.verdict && m.answer === r.verdict) { applied.push(m); if (m.verdict === "right" && r.verdict !== null) v = r.verdict; } truth.set(it.key, v); } const s = scoreModel(model, truth, results); return { model, marks: markTally(applied), agreed: s.agreed, agreement: s.agreement, notCorroborated: s.notCorroborated }; }