// Verdict proposals (release 29 M8): the task never writes a claim's verdict, // refuses a model that has not cleared the bar, skips what the operator has // ruled or reviewed; the sidecar survives makeCurrent; the operator's approve // sets the verdict through patchArticle. Temp stores and the synthetic corpus. // // Run with: node_modules/.bin/tsx --test lib/report/draft/tasks/verdicts.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import os, { tmpdir } from "node:os"; import path from "node:path"; import { cuesOf } from "../../../../testing/draft/corpus.mjs"; import { startFakeAnthropic, toolTurn } from "../../../../testing/llm/fakeAnthropic.mjs"; import { articleFile, writeArticle } from "../../registry"; import { createIteration, makeCurrent, updateIteration, writeIterationFile } from "../../iterations"; import { currentClaims, markVerdictsJob, proposalIsCurrent, readVerdictProposals, recordVerdictProposals, reviewVerdictProposal, verdictProposalsFile, waitingProposals, } from "../../verdictProposals"; import { planArticleDelete } from "../../delete"; import { sanitizeGeneration } from "../../../generationSettings"; import { PROMPT_VERSION, type BenchAsk } from "../bench/index"; import { proposeVerdicts, verdictsModelOf, VerdictsRefusal } from "./verdicts"; const RULED = path.join(import.meta.dirname, "../../../../testing/draft/reports/ruled.report.json"); const ROOTS: string[] = []; after(() => { for (const r of ROOTS) rmSync(r, { recursive: true, force: true }); }); const NOW = new Date("2026-10-15T12:00:00.000Z"); const QWEN = "qwen3:8b"; // The synthetic fact-check with r02, r04 and r07 unruled (r08 unruled, no citation). async function store() { const root = mkdtempSync(path.join(tmpdir(), "verdict-proposals-")); ROOTS.push(root); const paths = { articlesDir: path.join(root, "reports"), exportIndexDir: path.join(root, ".export-index") }; const report = JSON.parse(readFileSync(RULED, "utf8")); for (const s of report.sections) for (const c of s.claims) if (["r02", "r04", "r07", "r08"].includes(c.id)) delete c.verdict; await writeArticle(report, { paths }); return { paths, id: report.id as string, file: articleFile(paths, report.id) }; } const claimOf = (user: string) => /^Claim: (.*)$/m.exec(user)?.[1] ?? ""; function stub(answer: (claimText: string) => unknown) { const asked: { model: string; user: string; system: string }[] = []; const ask: BenchAsk = async (system: string, user: string, _schema: object, model: string) => { asked.push({ model, user, system }); return answer(claimOf(user)) as T; }; return { ask, asked }; } // A bench v3 answer (the parts; the verdict follows in code) that makes `verdict`. function v3(verdict: string, reason: string) { const f: Record = { CORROBORATED: ["shown"], PARTLY: ["shown", "not shown"], CONTRADICTED: ["shown wrong"], NOT_FOUND: ["not shown"], UNTESTABLE: [] }; return { parts: f[verdict]!.map((finding, i) => ({ part: `part ${i}`, finding })), checkable: verdict !== "UNTESTABLE", reason }; } test("the model: only one in verdicts.models — the asked one, else the first on the route; none, or code, refused", () => { const gen = (models: string[], route: "api" | "local" | "code") => { const g = sanitizeGeneration({ verdicts: { bar: { agreement: 0.6, contradictedRecall: 0.5, notFoundRecall: 0.5 }, models } }); g.routes.verdicts = route; return g; }; assert.equal(sanitizeGeneration({}).verdicts.models.length, 0); assert.throws(() => verdictsModelOf(gen([], "local")), /models is empty/); assert.equal(verdictsModelOf(gen([QWEN, "claude-opus-5-5"], "local")), QWEN); assert.equal(verdictsModelOf(gen([QWEN, "claude-opus-5-5"], "api")), "claude-opus-5-5"); assert.throws(() => verdictsModelOf(gen([QWEN], "api")), /runs on the api route/); assert.throws(() => verdictsModelOf(gen([QWEN], "code")), /routed to Claude Code/); assert.equal(verdictsModelOf(gen([QWEN], "code"), QWEN), QWEN); assert.throws(() => verdictsModelOf(gen([QWEN], "local"), "claude-sonnet-5-5"), VerdictsRefusal); // Every preset routes verdicts. assert.equal(sanitizeGeneration({}).routes.verdicts, "local"); }); test("proposals: the unruled claims only, the bench's prompt, written to the sidecar — never a verdict on a claim; an unlisted model refused", async () => { const s = await store(); const before = readFileSync(s.file, "utf8"); const { ask, asked } = stub((text) => v3(text.includes("oven") ? "PARTLY" : "NOT_FOUND", `the window shows it: ${text.slice(0, 20)}`)); await assert.rejects(proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [], ask }), /models is empty/); await assert.rejects(proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: ["claude-opus-5-5"], ask }), /has not cleared the verdict bench's bar/); assert.equal(asked.length, 0); const out = await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); assert.equal(out.status, "done"); assert.deepEqual(out.proposed, ["r02", "r04", "r07"]); // The ruled claims are not asked; r08 has no citation. assert.equal(asked.length, 3); assert.ok(out.skipped.some((x) => x.claim === "r01" && x.reason === "ruled by the operator")); assert.ok(out.skipped.some((x) => x.claim === "r08" && x.reason === "no citation")); assert.ok(asked[0]!.user.startsWith("Report: ")); // report.json is untouched: not a byte. assert.equal(readFileSync(s.file, "utf8"), before); const f = (await readVerdictProposals(s.id, s.paths))!; assert.deepEqual(f.proposals.r02, { verdict: "PARTLY", reason: f.proposals.r02!.reason, model: QWEN, promptVersion: PROMPT_VERSION, at: NOW.toISOString(), fingerprint: f.proposals.r02!.fingerprint, status: "proposed" }); assert.match(f.proposals.r02!.fingerprint, /^[0-9a-f]{16}$/); assert.equal(waitingProposals(f).length, 3); assert.equal(path.basename(verdictProposalsFile(s.paths, s.id)), "verdicts.proposed.json"); }); // The proposal the operator is shown, for the compare-and-set review. async function shown(paths: Parameters[1], id: string, claim: string) { const p = (await readVerdictProposals(id, paths))!.proposals[claim]!; return { verdict: p.verdict, at: p.at }; } test("the sidecar survives makeCurrent; approve sets the verdict through patchArticle, reject leaves the claim; a reviewed proposal is never asked again", async () => { const s = await store(); const { ask, asked } = stub(() => v3("CONTRADICTED", "shown")); await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); // A new iteration made current replaces report.json whole: the proposals stay. const report = JSON.parse(readFileSync(s.file, "utf8")); await createIteration(s.id, { kind: "sweep", model: QWEN, question: "What does the article get right?" }, s.paths, NOW); await writeIterationFile(s.id, 1, "report.json", { ...report, title: "Retitled" }, s.paths); await updateIteration(s.id, 1, { status: "done" }, s.paths, NOW); await makeCurrent(s.id, 1, s.paths, NOW); assert.equal(JSON.parse(readFileSync(s.file, "utf8")).title, "Retitled"); assert.equal(waitingProposals(await readVerdictProposals(s.id, s.paths)).length, 3); // Approve r04: the claim's verdict is set, the proposal approved. const ok = await reviewVerdictProposal({ article: s.id, claim: "r04", decision: "approve", ...(await shown(s.paths, s.id, "r04")) }, s.paths, NOW); assert.equal(ok.status, "approved"); const claimVerdict = (id: string) => JSON.parse(readFileSync(s.file, "utf8")).sections.flatMap((x: any) => x.claims).find((c: any) => c.id === id)?.verdict; assert.equal(claimVerdict("r04"), "CONTRADICTED"); // Reject r07, with a note: the claim stays unruled. const no = await reviewVerdictProposal({ article: s.id, claim: "r07", decision: "reject", note: "the window is too short", ...(await shown(s.paths, s.id, "r07")) }, s.paths, NOW); assert.deepEqual([no.status, no.note], ["rejected", "the window is too short"]); assert.equal(claimVerdict("r07"), undefined); // Twice: refused. No proposal: 404. await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r04", decision: "approve", verdict: "CONTRADICTED", at: NOW.toISOString() }, s.paths, NOW), /approved already/); await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r01", decision: "approve", verdict: "CONTRADICTED", at: NOW.toISOString() }, s.paths, NOW), (e: any) => e.status === 404); // A rerun: r04 is ruled now and r07's proposal was rejected — only r02 is asked. asked.length = 0; const again = await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); assert.deepEqual(again.proposed, ["r02"]); assert.equal(asked.length, 1); assert.ok(again.skipped.some((x) => x.claim === "r07" && /rejected by the operator/.test(x.reason))); // A reviewed proposal on the same claim is never replaced by a record either. const fp = (await readVerdictProposals(s.id, s.paths))!.proposals.r07!.fingerprint; const rec = await recordVerdictProposals(s.id, { r07: { verdict: "PARTLY", reason: "x", model: QWEN, promptVersion: PROMPT_VERSION, at: NOW.toISOString(), fingerprint: fp } }, s.paths, NOW); assert.deepEqual(rec, { written: [], kept: ["r07"], stale: [] }); // A claim ruled by hand after its proposal: approving is refused, the verdict kept. const r = JSON.parse(readFileSync(s.file, "utf8")); for (const sec of r.sections) for (const c of sec.claims) if (c.id === "r02") c.verdict = "CORROBORATED"; await writeArticle(r, { paths: s.paths }); await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r02", decision: "approve", ...(await shown(s.paths, s.id, "r02")) }, s.paths, NOW), /ruled already/); assert.equal(claimVerdict("r02"), "CORROBORATED"); }); test("review fix 1: a revision that drops a claim shifts the ids — the shifted proposal is stale (409, not waiting), and the new claim under the id is asked again", async () => { const s = await store(); const { ask, asked } = stub((text) => v3("CONTRADICTED", `on ${text.slice(0, 30)}`)); await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); // r02 rejected by the operator. await reviewVerdictProposal({ article: s.id, claim: "r02", decision: "reject", ...(await shown(s.paths, s.id, "r02")) }, s.paths, NOW); // A revision drops r02: the old r03's text moves up under r02's id, unruled; r04 gets r05's claim. const r = JSON.parse(readFileSync(s.file, "utf8")); const sec = r.sections[0]; const r03 = sec.claims.find((c: any) => c.id === "r03"); const r05 = sec.claims.find((c: any) => c.id === "r05"); sec.claims = sec.claims.filter((c: any) => !["r02", "r03", "r04", "r05"].includes(c.id)); sec.claims.push({ ...r03, id: "r02", verdict: undefined }, { ...r05, id: "r04", verdict: undefined }); for (const c of sec.claims) if (c.verdict === undefined) delete c.verdict; await writeArticle(r, { paths: s.paths }); const claims = currentClaims(JSON.parse(readFileSync(s.file, "utf8"))); const f = (await readVerdictProposals(s.id, s.paths))!; // The r04 proposal judged another claim: stale, and its review refused. assert.equal(proposalIsCurrent(f.proposals.r04!, claims.get("r04")), false); await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r04", decision: "approve", ...(await shown(s.paths, s.id, "r04")) }, s.paths, NOW), (e: any) => e.status === 409 && /stale/.test(e.message)); assert.equal(claims.get("r04")!.ruled, false); // A rerun asks the new r02 (its old proposal's rejection was another claim's) and the new r04. asked.length = 0; const again = await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); assert.deepEqual(again.proposed.sort(), ["r02", "r04", "r07"]); const g = (await readVerdictProposals(s.id, s.paths))!; assert.equal(g.proposals.r02!.status, "proposed"); assert.equal(g.proposals.r02!.fingerprint, claims.get("r02")!.fingerprint); }); test("review fix 2: the review is a compare-and-set — a proposal rewritten between view and click is refused; concurrent approve and reject: one wins", async () => { const s = await store(); const { ask } = stub(() => v3("CORROBORATED", "shown")); await proposeVerdicts({ article: s.id, model: QWEN }, { paths: s.paths, cuesOf, llm: {}, allowed: [QWEN], ask, now: () => NOW }); const seen = await shown(s.paths, s.id, "r02"); // Another run rewrites r02 to PARTLY after the operator saw CORROBORATED. const later = new Date(NOW.getTime() + 60_000); const fp = (await readVerdictProposals(s.id, s.paths))!.proposals.r02!.fingerprint; await recordVerdictProposals(s.id, { r02: { verdict: "PARTLY", reason: "newer", model: "claude-opus-5-5", promptVersion: PROMPT_VERSION, at: later.toISOString(), fingerprint: fp } }, s.paths, later); await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r02", decision: "approve", ...seen }, s.paths, NOW), (e: any) => e.status === 409 && /changed since it was shown/.test(e.message)); const claimVerdict = (id: string) => JSON.parse(readFileSync(s.file, "utf8")).sections.flatMap((x: any) => x.claims).find((c: any) => c.id === id)?.verdict; assert.equal(claimVerdict("r02"), undefined); // A wrong verdict named: refused too. await assert.rejects(reviewVerdictProposal({ article: s.id, claim: "r02", decision: "approve", verdict: "CORROBORATED", at: later.toISOString() }, s.paths, NOW), /changed since it was shown/); // Approve and reject of r04 at once: exactly one wins, and the record agrees with the claim. const r04 = await shown(s.paths, s.id, "r04"); const both = await Promise.allSettled([ reviewVerdictProposal({ article: s.id, claim: "r04", decision: "approve", ...r04 }, s.paths, NOW), reviewVerdictProposal({ article: s.id, claim: "r04", decision: "reject", ...r04 }, s.paths, NOW), ]); assert.equal(both.filter((x) => x.status === "fulfilled").length, 1); assert.ok(both.some((x) => x.status === "rejected" && (x.reason as any).status === 409)); const status = (await readVerdictProposals(s.id, s.paths))!.proposals.r04!.status; assert.equal(claimVerdict("r04") === "CORROBORATED", status === "approved"); assert.equal(claimVerdict("r04") === undefined, status === "rejected"); }); test("review fix 3: no write without report.json (no orphan directory); deleteArticle refuses while a verdicts job is marked", async () => { const s = await store(); const sitesDir = path.join(path.dirname(s.paths.articlesDir), "sites"); const opts = { projectsRoot: path.join(path.dirname(s.paths.articlesDir), "projects"), paths: { ...s.paths, sitesDir } }; await markVerdictsJob(s.id, { jobId: "job-1" }, s.paths, NOW); const plan = await planArticleDelete(s.id, opts); assert.ok(plan.refusals.some((r) => /a verdicts job \(job job-1\)/.test(r.reason))); await markVerdictsJob(s.id, null, s.paths, NOW); assert.ok(!(await planArticleDelete(s.id, opts)).refusals.some((r) => /verdicts job/.test(r.reason))); // A mark whose process is gone is not a job. const f = JSON.parse(readFileSync(verdictProposalsFile(s.paths, s.id), "utf8")); writeFileSync(verdictProposalsFile(s.paths, s.id), JSON.stringify({ ...f, job: { jobId: "old", pid: 2 ** 22 + 7, host: os.hostname(), since: NOW.toISOString() } })); assert.ok(!(await planArticleDelete(s.id, opts)).refusals.some((r) => /verdicts job/.test(r.reason))); // The article deleted mid-run: the next record is refused and makes nothing. rmSync(path.dirname(s.file), { recursive: true, force: true }); await assert.rejects( recordVerdictProposals(s.id, { r02: { verdict: "PARTLY", reason: "x", model: QWEN, promptVersion: PROMPT_VERSION, at: NOW.toISOString(), fingerprint: "x" } }, s.paths, NOW), (e: any) => e.status === 404, ); assert.equal(existsSync(path.dirname(s.file)), false); await assert.rejects(markVerdictsJob(s.id, { jobId: "job-2" }, s.paths, NOW), /no report.json/); assert.equal(existsSync(path.dirname(s.file)), false); }); test("an API model: held to maxUsd before each call; the spend is the Meter's", async () => { const s = await store(); const api = await startFakeAnthropic(); try { for (let i = 0; i < 3; i++) api.push(toolTurn("answer", v3("PARTLY", "shown"))); const out = await proposeVerdicts( { article: s.id, model: "claude-opus-5-5", maxUsd: 0.015 }, { paths: s.paths, cuesOf, llm: { anthropic: { baseUrl: api.url, env: { ANTHROPIC_API_KEY: api.key } } }, allowed: ["claude-opus-5-5"] }, ); assert.equal(out.status, "capped"); assert.ok(out.proposed.length >= 1 && out.proposed.length < 3, `${out.proposed.length}`); assert.ok(out.usd > 0 && out.usd <= 0.015); assert.equal(api.requests.length, out.proposed.length); } finally { await api.close(); } });