papertrail/lib/reportExport.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

87 lines
3.1 KiB
TypeScript

// Renders a verification result as a portable Markdown provenance block a researcher
// can paste straight into a manuscript, RPPR, or review. Every quoted source span is
// the code-grounded verbatim substring (see lib/grounding.ts), so the exported trail
// is as trustworthy as the on-screen one. Pure: no mutation, deterministic output.
import { GroundedSpan } from "./grounding";
export interface ReportInput {
claim: string;
source: {
title: string | null;
url: string;
source_type: string;
external_id?: string;
};
verification: {
discrepancy_type: string;
trust_score: number;
explanation: string;
flagged_spans: GroundedSpan[];
};
effectSizeCheck?: { verdict: string; rationale: string };
}
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 sourceCitation(source: ReportInput["source"]): string {
const registry = source.source_type === "pubmed" ? "PubMed" : "ClinicalTrials.gov";
const id = source.external_id ? ` (${registry} ${source.external_id})` : ` (${registry})`;
const title = source.title || source.url;
return `${title}${id} — ${source.url}`;
}
/**
* Build the Markdown provenance block. Sections: verdict, the checked claim, the
* primary source citation, the deterministic numeric check (if present), and each
* flagged discrepancy with its verbatim source quote.
*/
export function toMarkdownReport(input: ReportInput): string {
const { claim, source, verification, effectSizeCheck } = input;
const lines: string[] = [];
lines.push("## PaperTrail verification");
lines.push("");
lines.push(`**Verdict:** ${label(verification.discrepancy_type)} · **Trust score:** ${verification.trust_score}/100`);
lines.push("");
lines.push(verification.explanation);
lines.push("");
lines.push(`**Claim checked:** "${claim.trim()}"`);
lines.push("");
lines.push(`**Primary source:** ${sourceCitation(source)}`);
if (effectSizeCheck && effectSizeCheck.verdict !== "cannot_reconcile") {
lines.push("");
lines.push(`**Numeric check (${label(effectSizeCheck.verdict)}):** ${effectSizeCheck.rationale}`);
}
if (verification.flagged_spans.length > 0) {
lines.push("");
lines.push("**Flagged discrepancies:**");
for (const span of verification.flagged_spans) {
lines.push("");
lines.push(`- Claim: "${span.claim_span}"`);
lines.push(` - Source says: "${span.source_span}"`);
lines.push(` - Issue: ${span.issue}`);
}
} else if (verification.discrepancy_type === "accurate") {
lines.push("");
lines.push("No discrepancies flagged — the claim is consistent with the source.");
}
lines.push("");
lines.push("_Generated by PaperTrail. Every source quote is a verbatim substring of the cited source._");
return lines.join("\n");
}