- Lab Notebook: deterministic reproducibility gaps (antibody dilution, vendor/catalog, controls, sample size, missing readout) - Trial Matcher: 'why this rank' one-line eligibility summary from per-criterion assessments - Verify: copyable citation + full reviewer memo, source quotes verbatim - /console/demo reviewer walkthrough linking the 3 named-user journeys, wired into console nav - All additive, deterministic (no new LLM calls), tsc + build green
118 lines
4.7 KiB
TypeScript
118 lines
4.7 KiB
TypeScript
import type { ExtractedFinding } from "@/lib/schemas";
|
|
import type { GroundedSpan } from "@/lib/grounding";
|
|
import type { VerifySource, VerifyVerification } from "./types";
|
|
import { VERDICT_STYLES, trustQualifier } from "./types";
|
|
|
|
// Pure, side-effect-free builders that turn a verdict into copy-pasteable provenance text
|
|
// a medical-affairs reviewer can drop into a memo or a citation footnote. No React, no I/O —
|
|
// so the "show WHY" block stays deterministic and testable, and the grounded source quotes
|
|
// are reproduced VERBATIM (never re-summarized) to preserve PaperTrail's grounding moat.
|
|
|
|
// How much of the source we quote as the anchor when there are no flagged spans (an accurate
|
|
// claim still deserves a grounded citation). Kept short so a memo footnote stays readable.
|
|
const ANCHOR_QUOTE_CHARS = 200;
|
|
|
|
export interface ProvenanceInput {
|
|
claim: string;
|
|
verification: VerifyVerification;
|
|
finding: ExtractedFinding;
|
|
source: VerifySource;
|
|
}
|
|
|
|
// A short, footnote-style citation: the claim, the verdict + trust, and one grounded anchor
|
|
// quote. This is the "paste into a sentence" form.
|
|
export function buildCitation(input: ProvenanceInput): string {
|
|
const { claim, verification, source } = input;
|
|
const verdictLabel = VERDICT_STYLES[verification.discrepancy_type].label;
|
|
const anchor = anchorQuote(verification.flagged_spans, source.raw_text);
|
|
const sourceLabel = sourceCitationLabel(source);
|
|
return [
|
|
`"${claim}"`,
|
|
`Verdict: ${verdictLabel} — PaperTrail trust score ${verification.trust_score}/100 (${trustQualifier(
|
|
verification.trust_score
|
|
)}).`,
|
|
`Grounded on: "${anchor}"`,
|
|
`Source: ${sourceLabel}.`,
|
|
].join("\n");
|
|
}
|
|
|
|
// A full reviewer memo: everything a reader needs to audit the call without re-running the
|
|
// tool — the extracted finding, and every flagged claim-vs-source quote pair verbatim.
|
|
export function buildSummary(input: ProvenanceInput): string {
|
|
const { claim, verification, finding, source } = input;
|
|
const verdictLabel = VERDICT_STYLES[verification.discrepancy_type].label;
|
|
const lines: string[] = [];
|
|
|
|
lines.push("PAPERTRAIL — CLAIM VERIFICATION MEMO");
|
|
lines.push("");
|
|
lines.push(`Claim audited:`);
|
|
lines.push(` "${claim}"`);
|
|
lines.push("");
|
|
lines.push(
|
|
`Verdict: ${verdictLabel} — trust score ${verification.trust_score}/100 (${trustQualifier(
|
|
verification.trust_score
|
|
)}).`
|
|
);
|
|
lines.push(`Assessment: ${verification.explanation}`);
|
|
lines.push("");
|
|
|
|
lines.push("Extracted source finding:");
|
|
lines.push(` Effect size: ${finding.effect_size}`);
|
|
lines.push(` Population: ${finding.population}`);
|
|
lines.push(` Condition: ${finding.condition}`);
|
|
lines.push(` Endpoint: ${finding.endpoint}`);
|
|
lines.push(
|
|
` Caveats: ${finding.caveats.length > 0 ? finding.caveats.join("; ") : "None reported"}`
|
|
);
|
|
lines.push("");
|
|
|
|
if (verification.flagged_spans.length > 0) {
|
|
lines.push(
|
|
`Flagged claim-vs-source spans (${verification.flagged_spans.length}) — source quotes are verbatim:`
|
|
);
|
|
verification.flagged_spans.forEach((span, i) => {
|
|
lines.push(` ${i + 1}. Claim says: "${span.claim_span}"`);
|
|
lines.push(
|
|
` Source says: "${span.source_span}" (chars ${span.grounding.start}-${span.grounding.end}${
|
|
span.grounding.status === "approximate" ? ", normalized match" : ""
|
|
})`
|
|
);
|
|
lines.push(` Issue: ${span.issue}`);
|
|
});
|
|
} else {
|
|
lines.push("Flagged spans: none — the claim matches the source finding.");
|
|
}
|
|
lines.push("");
|
|
|
|
if (verification.grounding_dropped_count > 0) {
|
|
lines.push(
|
|
`Note: ${verification.grounding_dropped_count} model-proposed span${
|
|
verification.grounding_dropped_count === 1 ? " was" : "s were"
|
|
} dropped for not matching the source verbatim (PaperTrail grounding invariant).`
|
|
);
|
|
lines.push("");
|
|
}
|
|
|
|
lines.push(`Source: ${sourceCitationLabel(source)}.`);
|
|
lines.push(
|
|
"Generated by PaperTrail — every flag is tied to an exact quote from the cited source."
|
|
);
|
|
|
|
return lines.join("\n");
|
|
}
|
|
|
|
// Prefer the first flagged span (the most load-bearing quote); otherwise anchor on the head
|
|
// of the source so an accurate verdict is still grounded. Never invents text.
|
|
function anchorQuote(spans: readonly GroundedSpan[], rawText: string): string {
|
|
if (spans.length > 0) return spans[0].source_span;
|
|
const head = rawText.slice(0, ANCHOR_QUOTE_CHARS).trim();
|
|
return rawText.length > ANCHOR_QUOTE_CHARS ? `${head}…` : head;
|
|
}
|
|
|
|
function sourceCitationLabel(source: VerifySource): string {
|
|
const title = source.title.trim().length > 0 ? source.title.trim() : "Pasted source";
|
|
const parts = [title, `${source.source_type}`];
|
|
if (source.url.trim().length > 0) parts.push(source.url.trim());
|
|
return parts.join(" · ");
|
|
}
|