papertrail/lib/batchReport.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

130 lines
4.3 KiB
TypeScript

// Combines a whole batch run into one portable Markdown provenance report a researcher
// can attach to a manuscript, RPPR, or review. It reuses lib/reportExport.ts per verified
// claim so each verified section carries the same code-grounded verbatim source quotes,
// and lists no_support_found / error claims honestly with a one-line note rather than
// pretending they were checked. Pure and deterministic: no Date.now, no Math.random.
import { BatchResultItem } from "../components/BatchResults";
import { toMarkdownReport } from "./reportExport";
const DISCREPANCY_LABELS: Record<string, string> = {
accurate: "Accurate",
magnitude_overstated: "Magnitude overstated",
population_overgeneralized: "Population overgeneralized",
caveat_dropped: "Caveat dropped",
no_support_found: "No support found",
};
function label(discrepancyType: string): string {
return DISCREPANCY_LABELS[discrepancyType] ?? discrepancyType;
}
function statusCounts(items: BatchResultItem[]): {
verified: number;
no_support_found: number;
error: number;
} {
return items.reduce(
(acc, item) => ({ ...acc, [item.status]: acc[item.status] + 1 }),
{ verified: 0, no_support_found: 0, error: 0 }
);
}
/**
* Count verified claims by their discrepancy_type (e.g. accurate, magnitude_overstated),
* in the fixed label order so the summary is deterministic regardless of input order.
*/
function discrepancyCounts(items: BatchResultItem[]): Array<[string, number]> {
const tally = new Map<string, number>();
for (const item of items) {
if (item.status !== "verified" || !item.verification) continue;
const key = item.verification.discrepancy_type;
tally.set(key, (tally.get(key) ?? 0) + 1);
}
const ordered = Object.keys(DISCREPANCY_LABELS);
const known = ordered
.filter((k) => tally.has(k))
.map((k) => [k, tally.get(k) as number] as [string, number]);
const extra = [...tally.keys()]
.filter((k) => !ordered.includes(k))
.sort()
.map((k) => [k, tally.get(k) as number] as [string, number]);
return [...known, ...extra];
}
/**
* Build a combined Markdown provenance report for a batch run: a header summary
* (total claims, counts by status, and a breakdown of verified claims by
* discrepancy_type) followed by one section per claim. Verified claims reuse the
* single-claim report block; no_support_found / error claims get an honest one-liner.
*/
export function toBatchMarkdownReport(items: BatchResultItem[]): string {
const lines: string[] = [];
const counts = statusCounts(items);
const total = items.length;
lines.push("# PaperTrail batch verification");
lines.push("");
lines.push(`**Claims in this report:** ${total}`);
lines.push("");
lines.push(
`**By status:** ${counts.verified} verified · ` +
`${counts.no_support_found} no support found · ${counts.error} error`
);
const discrepancies = discrepancyCounts(items);
if (discrepancies.length > 0) {
lines.push("");
lines.push("**Verified claims by verdict:**");
for (const [type, count] of discrepancies) {
lines.push(`- ${label(type)}: ${count}`);
}
}
items.forEach((item, index) => {
lines.push("");
lines.push("---");
lines.push("");
lines.push(`### Claim ${index + 1}`);
lines.push("");
if (item.status === "verified" && item.verification && item.source) {
lines.push(
toMarkdownReport({
claim: item.claim,
source: {
title: item.source.title,
url: item.source.url,
source_type: item.source.source_type,
external_id: item.source.external_id,
},
verification: item.verification,
effectSizeCheck: item.effect_size_check,
})
);
return;
}
lines.push(`**Claim checked:** "${item.claim.trim()}"`);
lines.push("");
if (item.status === "no_support_found") {
lines.push(
"_No confident matching primary source was found. This is not a judgment on the " +
"claim — it could not be verified against a retrievable source._"
);
} else {
lines.push(
"_Verification failed for this claim; it was skipped so the rest of the batch " +
"could finish._"
);
}
});
lines.push("");
lines.push(
"_Generated by PaperTrail. Every source quote is a verbatim substring of the cited source._"
);
return lines.join("\n");
}