// WHERE A VERDICT BENCH RUN LIVES (release 28, M7): local-only, under the // checkout's gitignored `.generation/` (beside the usage ledger): // // .generation/bench/verdicts// // items.jsonl one item per line (./items.ts) — the operator's verdict // included: the answer key, never shown to a model // results.jsonl one answer per (item, model) (./score.ts BenchResult) // summary.json the run's parameters, status, spend, skipped claims and // its score; V1: the prompt version, the items hash, the // window mode, the dropped citations, `itemsFrom` // trace.jsonl (V1) every call as it was made — the prompt the model // saw, its raw answer, the verdict kept — appended, one // line each, plus a `start` line per invocation // marks.json (V1) the operator's Right/Wrong marks on disagreements, // umtool's own (one writer: umtool/lib/bench-marks.mjs) // // Never `transcripts/`, never a commit: the items quote ruled claims. Every // write is atomic (tmp + rename, the whole file); a re-run with the same id // resumes (./run.ts skips each pair already answered). import { appendFile, readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { writeFileAtomic, writeJsonAtomic } from "../../../jsonFile-server"; import type { Paths } from "../../../paths"; import type { BenchDropped, BenchItem, BenchSkipped, WindowMode } from "./items"; import type { BenchMarks, BenchResult, BenchScore } from "./score"; export const BENCH_FORMAT = "archilyzer-verdict-bench"; export const BENCH_VERSION = 1; export const BENCH_ITEMS_FILE = "items.jsonl"; export const BENCH_RESULTS_FILE = "results.jsonl"; export const BENCH_SUMMARY_FILE = "summary.json"; export const BENCH_TRACE_FILE = "trace.jsonl"; export const BENCH_MARKS_FILE = "marks.json"; export const BENCH_MARKS_FORMAT = "archilyzer-bench-marks"; export const BENCH_STATUSES = ["running", "done", "capped", "cancelled", "failed"] as const; export type BenchStatus = (typeof BENCH_STATUSES)[number]; export type BenchSummary = { format: typeof BENCH_FORMAT; version: typeof BENCH_VERSION; id: string; createdAt: string; updatedAt: string; status: BenchStatus; // Why the run stopped early (the cap, a cancel, a model's errors), when it did. stopped?: string[]; models: string[]; windowS: number; // V1: how each window was cut (absent: M7's, "seconds"). windowMode?: WindowMode; // V1: the prompt the run asked with (absent: M7's, "1"), and the identity of // its items (./items.ts benchItemsHash; absent: compute it from the items). promptVersion?: string; itemsHash?: string; // V1: the run whose items this one was made from (`--items-from`). itemsFrom?: string; maxUsd: number; // The reports asked for (empty: every fact-check in the registry). reports: string[]; limit?: number; // A run made with dedupe (a repeated claim asked once); absent: every claim asked. dedupe?: true; spentUsd: number; items: number; skipped: BenchSkipped[]; // V1: the citations of kept claims that gave no window, with their reasons. dropped?: BenchDropped[]; score: BenchScore; }; export type BenchRun = { summary: BenchSummary; items: BenchItem[]; results: BenchResult[] }; const RUN_ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; export function isBenchRunId(v: unknown): v is string { return typeof v === "string" && RUN_ID_RE.test(v); } // A new run's id: `vb-`. export function newBenchRunId(now: Date = new Date()): string { const s = now.toISOString().replace(/[-:]/g, "").replace("T", "-").slice(0, 15); return `vb-${s}`; } // The runs' directory: `.generation/bench/verdicts/`, beside the usage ledger. export function benchRootOf(paths: Pick): string { return path.join(path.dirname(paths.generationUsageFile), "bench", "verdicts"); } export function benchRunDir(root: string, id: string): string { if (!isBenchRunId(id)) throw new Error(`"${id}" is not a run id (lowercase letters, digits and dashes)`); return path.join(root, id); } function jsonl(rows: readonly unknown[]): string { return rows.map((r) => `${JSON.stringify(r)}\n`).join(""); } async function readJsonl(file: string): Promise { let text: string; try { text = await readFile(file, "utf8"); } catch { return []; } const out: T[] = []; for (const line of text.split("\n")) { if (!line.trim()) continue; try { out.push(JSON.parse(line) as T); } catch { // a torn line is skipped } } return out; } export async function writeBenchItems(dir: string, items: readonly BenchItem[]): Promise { await writeFileAtomic(path.join(dir, BENCH_ITEMS_FILE), jsonl(items), { mkdir: true }); } export async function writeBenchResults(dir: string, results: readonly BenchResult[]): Promise { await writeFileAtomic(path.join(dir, BENCH_RESULTS_FILE), jsonl(results), { mkdir: true }); } export async function writeBenchSummary(dir: string, summary: BenchSummary): Promise { await writeJsonAtomic(path.join(dir, BENCH_SUMMARY_FILE), summary, { mkdir: true }); } export async function readBenchSummary(dir: string): Promise { try { const v = JSON.parse(await readFile(path.join(dir, BENCH_SUMMARY_FILE), "utf8")) as BenchSummary; return v && v.format === BENCH_FORMAT ? v : null; } catch { return null; } } export async function readBenchRun(root: string, id: string): Promise { const dir = benchRunDir(root, id); const summary = await readBenchSummary(dir); if (!summary) return null; const [items, results] = await Promise.all([ readJsonl(path.join(dir, BENCH_ITEMS_FILE)), readJsonl(path.join(dir, BENCH_RESULTS_FILE)), ]); return { summary, items, results }; } // Every run's summary, newest first. export async function listBenchRuns(root: string): Promise { let names: string[]; try { names = (await readdir(root, { withFileTypes: true })).filter((e) => e.isDirectory() && isBenchRunId(e.name)).map((e) => e.name); } catch { return []; } const out: BenchSummary[] = []; for (const n of names) { const s = await readBenchSummary(path.join(root, n)); if (s) out.push(s); } return out.sort((a, b) => b.createdAt.localeCompare(a.createdAt) || b.id.localeCompare(a.id)); } export { readJsonl as readBenchJsonl }; // One line of a run's trace, appended (never rewritten). A trace write never // fails the run: the caller catches. export async function appendBenchTrace(dir: string, line: Record): Promise { await appendFile(path.join(dir, BENCH_TRACE_FILE), `${JSON.stringify(line)}\n`); } export async function readBenchTrace(root: string, id: string): Promise[]> { return readJsonl>(path.join(benchRunDir(root, id), BENCH_TRACE_FILE)); } // A run's marks, as umtool wrote them (none, or a file that does not read: empty). export async function readBenchMarks(root: string, id: string): Promise { try { const v = JSON.parse(await readFile(path.join(benchRunDir(root, id), BENCH_MARKS_FILE), "utf8")) as { format?: string; marks?: BenchMarks }; return v?.format === BENCH_MARKS_FORMAT && v.marks && typeof v.marks === "object" ? v.marks : {}; } catch { return {}; } }