// THE VERDICT BENCH'S MARKS (release 29 V1): the operator's Right / Wrong on // a disagreement — was the MODEL right, where its answer is not the ruling? — // kept beside the run, so the bar can be set from marked precision // (common's `markedScore`, tallied by triage's `markTally`). // // //marks.json // { format: "archilyzer-bench-marks", version: 1, run, // marks: { "": { "/": { verdict: "right" | "wrong", // answer, at } } }, // updatedAt } // // `answer` is the model's verdict the mark was made on: a resumed run that // asked again and got another answer leaves the mark stale (not applied). // // R6's discipline (lib/research/triage-marks.mjs): ONE writer (umtool), // serialised in-process; the whole file to a temp name and a rename; a file // that does not parse is never overwritten (a write refuses with 409). The // run's other files are the editor's job's; this one is never touched by it. // // Plain ESM: the unit test reads it the way the app does. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const BENCH_MARKS_FORMAT = "archilyzer-bench-marks"; export const BENCH_MARKS_VERSION = 1; const RUN_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; const MODEL_RE = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/; const KEY_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}\/[A-Za-z0-9._:-]{1,64}$/; const VERDICTS = ["CORROBORATED", "PARTLY", "CONTRADICTED", "NOT_FOUND", "UNTESTABLE"]; export class BenchMarksError extends Error { constructor(message, status = 400) { super(message); this.status = status; } } export const benchMarksFile = (root, run) => { if (!RUN_RE.test(run)) throw new BenchMarksError(`not a run id: ${JSON.stringify(run)}`); return path.join(root, run, "marks.json"); }; function clean(raw, run) { const marks = {}; const given = raw?.marks && typeof raw.marks === "object" ? raw.marks : {}; for (const [model, byKey] of Object.entries(given)) { if (!MODEL_RE.test(model) || !byKey || typeof byKey !== "object") continue; for (const [key, m] of Object.entries(byKey)) { if (!KEY_RE.test(key) || !m || (m.verdict !== "right" && m.verdict !== "wrong")) continue; (marks[model] ??= {})[key] = { verdict: m.verdict, answer: VERDICTS.includes(m.answer) ? m.answer : null, at: typeof m.at === "string" ? m.at.slice(0, 40) : "", }; } } return { format: BENCH_MARKS_FORMAT, version: BENCH_MARKS_VERSION, run, marks, updatedAt: typeof raw?.updatedAt === "string" ? raw.updatedAt : "" }; } /** A run's marks (none: an empty set). A file that does not read is a 409. */ export async function readBenchMarksDoc(root, run) { let text; try { text = await readFile(/* turbopackIgnore: true */ benchMarksFile(root, run), "utf8"); } catch (err) { if (err?.code === "ENOENT") return clean(null, run); if (err instanceof BenchMarksError) throw err; throw new BenchMarksError(`the marks of ${run} cannot be read: ${err?.message ?? err}`, 409); } let raw; try { raw = JSON.parse(text); } catch (err) { throw new BenchMarksError(`${run}/marks.json is not JSON: ${err.message}`, 409); } if (raw?.format !== BENCH_MARKS_FORMAT) throw new BenchMarksError(`${run}/marks.json is not a ${BENCH_MARKS_FORMAT} file`, 409); return clean(raw, run); } let chain = Promise.resolve(); function serialised(fn) { const run = chain.then(fn); chain = run.catch(() => {}); return run; } /** * Mark one disagreement right or wrong (`verdict` null: unmark), on the * model's `answer`. Answers the marks as written. * * @param {string} root the bench runs' directory * @param {string} run * @param {{ model: string, key: string, verdict: "right" | "wrong" | null, answer: string | null, now?: Date }} m */ export function setBenchMark(root, run, m) { return serialised(async () => { if (!MODEL_RE.test(m.model ?? "")) throw new BenchMarksError(`not a model: ${JSON.stringify(m.model)}`); if (!KEY_RE.test(m.key ?? "")) throw new BenchMarksError(`not an item: ${JSON.stringify(m.key)}`); if (m.verdict !== null && m.verdict !== "right" && m.verdict !== "wrong") throw new BenchMarksError('"verdict" is "right", "wrong" or null'); const doc = await readBenchMarksDoc(root, run); const at = (m.now ?? new Date()).toISOString(); if (m.verdict === null) { if (doc.marks[m.model]) { delete doc.marks[m.model][m.key]; if (!Object.keys(doc.marks[m.model]).length) delete doc.marks[m.model]; } } else { (doc.marks[m.model] ??= {})[m.key] = { verdict: m.verdict, answer: VERDICTS.includes(m.answer) ? m.answer : null, at }; } doc.updatedAt = at; const file = benchMarksFile(root, run); await mkdir(/* turbopackIgnore: true */ path.dirname(file), { recursive: true }); const tmp = tmpPathFor(file); try { await writeFile(/* turbopackIgnore: true */ tmp, `${JSON.stringify(doc, null, 2)}\n`); await rename(/* turbopackIgnore: true */ tmp, file); } catch (err) { await rm(/* turbopackIgnore: true */ tmp, { force: true }).catch(() => {}); throw err; } return doc; }); }