// THE `verdicts` TASK (release 29 M8): proposed verdicts for a fact-check's // UNRULED claims, asked of a model that cleared the verdict bench's bar. // // It reads the article as the bench does — `unruledItemsOfReport` (the claims // with no verdict; each one's quotes and transcript window) — and asks each // claim with the bench's own prompt (`benchPrompt`, PROMPT_VERSION recorded on // every proposal), so a proposal is made exactly the way the model was // measured. One claim per call, sequential. // // What it writes: proposals, through the sidecar store // (../../verdictProposals.ts recordVerdictProposals — each as it lands). // What it NEVER writes: a claim's `verdict` (only the operator's review does, // through patchArticle). It REFUSES a model not in // `settings.generation.verdicts.models` (`allowed`: empty by default, so // nothing runs until the operator sets the bar and ticks a model). A claim the // operator has ruled is never asked; neither is one whose proposal the // operator has already approved or rejected. // // An API model is held to `maxUsd` (checked before each call against the // spend so far plus that call's estimate: past it the run ends `capped`). import { Meter, providerOf, StructuredAnswerError, structuredAnswer, type LlmConfig } from "../../../llm"; import type { GenerationSettings } from "../../../generationSettings"; import type { Paths } from "../../../paths"; import type { CuesOf } from "../../convertShared"; import { claimFingerprint, markVerdictsJob, proposalIsCurrent, readVerdictProposals, recordVerdictProposals, VerdictProposalsError, type NewProposal } from "../../verdictProposals"; import { benchPrompt, benchReportsOf, DEFAULT_WINDOW_S, estimateCall, LOCAL_PROMPT_CAP, PROMPT_VERSION, readVerdictAnswer, unruledItemsOfReport, VERDICT_ANSWER_SCHEMA, type BenchAsk, type BenchDropped, type BenchSkipped, type DateOf, type TurnsOf, type WindowMode, } from "../bench/index"; export const VERDICTS_TASK = "verdicts" as const; const MAX_ANSWER_TOKENS = 1024; export class VerdictsRefusal extends Error {} /** * The model proposals run on: the asked one, which must be in * `verdicts.models`; else the first of them on the task's route (`api` or * `local`). Refused when none is — nothing runs on a model the operator has * not ticked. */ export function verdictsModelOf(gen: Pick, asked?: string): string { const allowed = gen.verdicts.models; if (!allowed.length) throw new VerdictsRefusal("no model has cleared the verdict bench's bar: settings.generation.verdicts.models is empty (tick a passing model on umtool's /tools/bench)"); if (asked) { if (!allowed.includes(asked)) throw new VerdictsRefusal(`${asked} has not cleared the verdict bench's bar: settings.generation.verdicts.models is ${allowed.join(", ")}`); return asked; } const route = gen.routes.verdicts; if (route === "code") throw new VerdictsRefusal(`verdicts is routed to Claude Code (settings.generation.routes): name a model (${allowed.join(", ")})`); const pick = allowed.find((m) => providerOf(m) === route); if (!pick) throw new VerdictsRefusal(`no model in settings.generation.verdicts.models (${allowed.join(", ")}) runs on the ${route} route: name one, or route verdicts to ${route === "api" ? "local" : "api"}`); return pick; } export type VerdictsInput = { article: string; model: string; // Only these claims (default: every unruled one). claims?: string[]; maxUsd?: number; // The window a proposal reads (settings.generation.verdicts.window: the // bench run the bar was set on). Default the bench's: seconds, ±60. windowS?: number; windowMode?: WindowMode; // The job proposing (marked on the sidecar while it runs: deleteArticle // refuses then). jobId?: string; }; export type VerdictsDeps = { paths: Pick; cuesOf: CuesOf; turnsOf?: TurnsOf; dateOf?: DateOf; llm: LlmConfig; // settings.generation.verdicts.models: the only models a proposal may run on. allowed: readonly string[]; // The model, stubbed (tests; the bench's shape). ask?: BenchAsk; log?: (line: string) => void; progress?: (done: number, total: number) => void; signal?: AbortSignal; now?: () => Date; localCapTokens?: number; }; export type VerdictsOutcome = { status: "done" | "capped" | "cancelled"; article: string; model: string; promptVersion: string; // The claims proposed for, in order. proposed: string[]; failed: { claim: string; error: string }[]; skipped: BenchSkipped[]; dropped: BenchDropped[]; usd: number; byModel: ReturnType["byModel"]; summary: string; }; export async function proposeVerdicts(input: VerdictsInput, deps: VerdictsDeps): Promise { const { article, model } = input; if (!deps.allowed.includes(model)) { throw new VerdictsRefusal( deps.allowed.length ? `${model} has not cleared the verdict bench's bar: settings.generation.verdicts.models is ${deps.allowed.join(", ")}` : "no model has cleared the verdict bench's bar: settings.generation.verdicts.models is empty", ); } const log = deps.log ?? (() => {}); const now = () => deps.now?.() ?? new Date(); const local = providerOf(model) === "local"; const localCap = deps.localCapTokens ?? LOCAL_PROMPT_CAP; const [report] = await benchReportsOf(deps.paths, [article]); // Each claim as this run reads it (review fix 1: a proposal carries it). const citations = (report!.report.citations ?? {}) as Record; const fingerprints = new Map(); for (const sec of report!.report.sections) for (const c of sec.claims ?? []) fingerprints.set(c.id, claimFingerprint(c, citations)); const { items, skipped, dropped } = await unruledItemsOfReport(report!, { cuesOf: deps.cuesOf, ...(input.windowS !== undefined ? { windowS: input.windowS } : {}), ...(input.windowMode ? { windowMode: input.windowMode } : {}), ...(deps.turnsOf ? { turnsOf: deps.turnsOf } : {}), ...(deps.dateOf ? { dateOf: deps.dateOf } : {}), ...(input.claims?.length ? { claims: input.claims } : {}), }); // A proposal the operator has reviewed is not asked again — on the same // claim (a reviewed proposal on a claim since changed is as good as none). const prior = await readVerdictProposals(article, deps.paths); const todo = items.filter((it) => { const p = prior?.proposals[it.claim]; if (p && p.status !== "proposed" && proposalIsCurrent(p, { fingerprint: fingerprints.get(it.claim) ?? "" })) { skipped.push({ key: it.key, report: article, claim: it.claim, reason: `its proposal was ${p.status} by the operator` }); return false; } return true; }); const win = `${input.windowMode === "turn" ? "the speaker's turn within " : "±"}${input.windowS ?? DEFAULT_WINDOW_S} s`; log(`verdicts ${article} on ${model} (prompt version ${PROMPT_VERSION}, window ${win}): ${todo.length} unruled claim${todo.length === 1 ? "" : "s"} to propose for, ${skipped.length} skipped, ${dropped.length} citations dropped`); const meter = new Meter(); const ask: BenchAsk = deps.ask ?? ((system: string, user: string, schema: object, m: string) => structuredAnswer({ llm: deps.llm, model: m, system, user, schema, meter, ...(deps.signal ? { signal: deps.signal } : {}), maxTokens: MAX_ANSWER_TOKENS })); const proposed: string[] = []; const failed: VerdictsOutcome["failed"] = []; let status: VerdictsOutcome["status"] = "done"; let done = 0; deps.progress?.(0, todo.length); if (input.jobId) await markVerdictsJob(article, { jobId: input.jobId }, deps.paths, now()); try { for (const item of todo) { if (deps.signal?.aborted) { status = "cancelled"; break; } if (!local && input.maxUsd !== undefined) { const next = estimateCall({ ...item, verdict: "CORROBORATED" }, model, localCap).usd; if (meter.usd + next > input.maxUsd) { status = "capped"; log(`stopped at the cap: $${meter.usd.toFixed(4)} spent, the next call (~$${next.toFixed(4)}) would pass $${input.maxUsd}`); break; } } const prompt = benchPrompt(item, local ? { capTokens: localCap } : {}); try { const ans = await ask(prompt.system, prompt.user, VERDICT_ANSWER_SCHEMA, model); // The bench's reading (bench v3: the verdict follows from the parts). const read = readVerdictAnswer(ans); if (!read.ok) throw new StructuredAnswerError(`not a verdict and a reason (${read.error}): ${JSON.stringify(ans ?? null).slice(0, 120)}`); const { verdict, reason } = read; const p: NewProposal = { verdict, reason, model, promptVersion: PROMPT_VERSION, at: now().toISOString(), fingerprint: fingerprints.get(item.claim) ?? "" }; const { written, stale } = await recordVerdictProposals(article, { [item.claim]: p }, deps.paths, now()); if (written.length) proposed.push(item.claim); log(`${item.claim}: ${verdict} — ${reason}${stale.length ? " (not kept: the claim was ruled or changed meanwhile)" : ""}`); } catch (err) { // The article gone (deleted mid-run): nothing more is written for it. if (err instanceof VerdictProposalsError && err.status === 404) throw err; if (deps.signal?.aborted) { status = "cancelled"; break; } const error = (err as Error).message.slice(0, 300); failed.push({ claim: item.claim, error }); log(`${item.claim}: no proposal (${error})`); } deps.progress?.(++done, todo.length); } } finally { if (input.jobId) await markVerdictsJob(article, null, deps.paths, now()).catch((e: Error) => log(`the job mark was not cleared: ${e.message}`)); } const usd = Math.round(meter.usd * 1e6) / 1e6; const summary = `${proposed.length} verdict${proposed.length === 1 ? "" : "s"} proposed on ${model}` + (failed.length ? `, ${failed.length} failed` : "") + (status === "capped" ? ", stopped at the cap" : status === "cancelled" ? ", cancelled" : "") + (usd ? ` · $${usd.toFixed(4)}` : ""); return { status, article, model, promptVersion: PROMPT_VERSION, proposed, failed, skipped, dropped, usd, byModel: meter.summary().byModel, summary }; }