Bring PaperTrail's flagship distortion detector into the mixture as the high-weight `discrepancy` agent (lib/verify/discrepancy.ts extract -> audit -> ground -> deterministic reconcile; catches magnitude/population/caveat/no-support distortions plain entailment misses). One clean healthy-API run: MoA 100% (20/20) vs Claude-alone 80% vs single-engine PaperTrail 75%. Harden the composition with 64 audit-driven fixes (24-agent audit -> 22-agent fix workflow, one file each, moat preserved): - aggregate.ts: fix qualityMult overwrite bug (only paper-qa's producer sets the global multiplier; verifiers no longer double-count or clobber it) - discrepancy: reconcile on the SAME truncated window the LLM saw; severity- and grounding-scaled refute confidence (no flat 0.7 floor); drop trust_score fallback - magnitude: borderline-tolerance guard, log-scaled confidence, support vote on an unambiguous significant source effect, ungrounded-effect audit trail - minicheck: calibrated gate, per-source try/catch, deterministic NEI tie-break - quant-extractor: only emit grounded effects; multivers: directional confidence; plus scispacy/indra/pytrials/storm/autogather/autoloop robustness - lib/claude.ts: exponential backoff on 429/529/503 (transient only) Resilience finding (honest): when the app key hit its usage cap mid-campaign (hard 400, regains 2026-08-01), every LLM call failed — Claude-alone and pure-LLM PaperTrail collapsed to 10%, but the MoA held 80-85% on its deterministic agents. docs/benchmark-clinical.md records the full multi-run story + the cap caveat (post-fix accuracy must be re-confirmed once the key resets).
225 lines
8.7 KiB
TypeScript
225 lines
8.7 KiB
TypeScript
// PaperTrail-native source-quality tiering — the TypeScript twin of
|
|
// backend/engines/paper-qa/papertrail_source_quality.py. paper-qa's synthesis treats
|
|
// every retrieved passage as equally trustworthy once it clears retrieval; PaperTrail
|
|
// cannot. A claim "confirmed" by a retracted paper or an unreviewed preprint is not
|
|
// confirmed. This module assigns each source a QUALITY TIER (A/B/C/D) and a QUALITY
|
|
// WEIGHT in [0, 1] from its metadata alone, so lib/paperqa/ask.ts synthesis can
|
|
// DOWN-WEIGHT low-tier evidence instead of counting it at face value.
|
|
//
|
|
// NO LLM anywhere in this path: tier and weight are a pure, documented function of the
|
|
// metadata. The same metadata always yields the same result — the determinism a
|
|
// provenance tool must guarantee. The numeric rubric mirrors the Python constants
|
|
// field-for-field (WELL_CITED_THRESHOLD, PREPRINT_CITED_THRESHOLD, BASE_WEIGHT_BY_TIER,
|
|
// OPEN_ACCESS_BONUS) so the CLI and the app agree on every score.
|
|
|
|
export type SourceQualityTier = "A" | "B" | "C" | "D";
|
|
|
|
// Raw source metadata as accepted at the boundary. Every field beyond `id` is
|
|
// optional and defensively narrowed — external data is never trusted.
|
|
export interface SourceQualityMeta {
|
|
id: string;
|
|
journal?: string | null;
|
|
year?: number | null;
|
|
citations?: number | null;
|
|
is_preprint?: boolean | null;
|
|
is_open_access?: boolean | null;
|
|
retracted?: boolean | null;
|
|
/** A Retraction Watch id present here hard-caps the source to untrusted (Tier D). */
|
|
retraction_watch_id?: string | null;
|
|
}
|
|
|
|
export interface SourceQualityResult {
|
|
id: string;
|
|
tier: SourceQualityTier;
|
|
tierLabel: string;
|
|
/** Multiplier synthesis applies to this source's evidence. In [0, 1]. */
|
|
weight: number;
|
|
retracted: boolean;
|
|
rationale: string;
|
|
}
|
|
|
|
// --- Rubric constants (mirror papertrail_source_quality.py exactly) ---
|
|
|
|
/** Journal-article citations at/above this promote a peer-reviewed source B -> A. */
|
|
export const WELL_CITED_THRESHOLD = 100;
|
|
/** Preprint/unknown citations at/above this promote C -> B (capped at B — never reviewed). */
|
|
export const PREPRINT_CITED_THRESHOLD = 50;
|
|
|
|
const BASE_WEIGHT_BY_TIER: Record<SourceQualityTier, number> = {
|
|
A: 1.0,
|
|
B: 0.8,
|
|
C: 0.5,
|
|
D: 0.0,
|
|
};
|
|
|
|
/** Additive weight bonus for open access, on non-D tiers only. */
|
|
export const OPEN_ACCESS_BONUS = 0.05;
|
|
|
|
const TIER_LABEL: Record<SourceQualityTier, string> = {
|
|
A: "peer-reviewed, well-cited",
|
|
B: "peer-reviewed",
|
|
C: "preprint or unreviewed",
|
|
D: "untrusted",
|
|
};
|
|
|
|
// Normalized, fully-narrowed metadata used internally by the scorer.
|
|
interface NormalizedMeta {
|
|
id: string;
|
|
journal: string | null;
|
|
// Validated publication year, or null when missing/unknown/out-of-range. Not a scoring
|
|
// input today; carried through so any consumer sees a real year or null, never garbage.
|
|
year: number | null;
|
|
citations: number;
|
|
isPreprint: boolean;
|
|
isOpenAccess: boolean;
|
|
retracted: boolean;
|
|
retractionWatchId: string | null;
|
|
}
|
|
|
|
function asOptionalString(value: unknown): string | null {
|
|
if (typeof value !== "string") return null;
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
|
|
// Non-negative integer citation count; anything invalid (NaN, negative, bool, etc.)
|
|
// deterministically becomes 0 rather than throwing — a missing count is not a failure.
|
|
function asNonNegInt(value: unknown): number {
|
|
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
const n = Math.trunc(value);
|
|
return n < 0 ? 0 : n;
|
|
}
|
|
|
|
// Earliest plausible publication year for a modern indexed source. Anything below this
|
|
// (e.g. a negative year, or `0` from a defaulted field) is corrupt metadata, not a real
|
|
// date, and is treated as unknown rather than silently accepted as an ancient paper.
|
|
export const MIN_PLAUSIBLE_YEAR = 1800;
|
|
|
|
// A validated publication year, or null when the field is missing/unknown/out-of-range.
|
|
// A source with `year: 999999` (or a negative year) is NOT a modern paper — it is corrupt
|
|
// metadata, so we treat it as unknown (null) instead of accepting it at face value. The
|
|
// upper bound is currentYear+1 to allow legitimate "in press"/next-issue dates without
|
|
// admitting far-future garbage. This narrows the optional interface field defensively;
|
|
// year is not itself a scoring input, so this changes no tier/weight math — it only
|
|
// guarantees any future consumer of the normalized year sees a real year or null.
|
|
function asPlausibleYear(value: unknown): number | null {
|
|
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
|
const n = Math.trunc(value);
|
|
const maxYear = new Date().getUTCFullYear() + 1;
|
|
if (n < MIN_PLAUSIBLE_YEAR || n > maxYear) return null;
|
|
return n;
|
|
}
|
|
|
|
function asBool(value: unknown): boolean {
|
|
return value === true;
|
|
}
|
|
|
|
// Round to 4 decimals and clamp to [0, 1], mirroring the Python _clamp01.
|
|
function clamp01(value: number): number {
|
|
const rounded = Math.round(value * 10000) / 10000;
|
|
if (rounded < 0) return 0;
|
|
if (rounded > 1) return 1;
|
|
return rounded;
|
|
}
|
|
|
|
function normalizeMeta(meta: SourceQualityMeta): NormalizedMeta {
|
|
const retractionWatchId = asOptionalString(meta.retraction_watch_id);
|
|
return {
|
|
id: meta.id,
|
|
journal: asOptionalString(meta.journal),
|
|
year: asPlausibleYear(meta.year),
|
|
citations: asNonNegInt(meta.citations),
|
|
// DELIBERATE CONSERVATISM CONTRACT: a missing or null `is_preprint` is treated as
|
|
// `false` (i.e. "not explicitly a preprint"). This does NOT auto-promote such a source
|
|
// to a reviewed tier — tiering also requires `journal` metadata to be present (see
|
|
// `isPeerReviewed` below). A source with no journal AND no explicit preprint flag still
|
|
// falls to Tier C ("Unknown venue"), which is the conservative, unreviewed outcome. We
|
|
// do not add an "unknown" tier: SourceQualityTier is a public A/B/C/D union consumed by
|
|
// the MoA layer, and widening it would break that contract. Tier C already encodes the
|
|
// "assume unreviewed" stance for absent metadata, so the conservative intent is met.
|
|
isPreprint: asBool(meta.is_preprint),
|
|
isOpenAccess: asBool(meta.is_open_access),
|
|
// A Retraction Watch id is itself proof of retraction, so it OR's into the flag.
|
|
retracted: asBool(meta.retracted) || retractionWatchId !== null,
|
|
retractionWatchId,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Deterministically tier one source and compute its quality weight.
|
|
*
|
|
* Pure function of `meta` — no LLM, no I/O. A retracted source (explicit flag or a
|
|
* Retraction Watch id) hard-caps to Tier D / weight 0 regardless of any other signal.
|
|
* Returns a NEW result object; the input is not mutated. Mirrors `score_source` in
|
|
* papertrail_source_quality.py field-for-field.
|
|
*/
|
|
export function scoreSourceQuality(meta: SourceQualityMeta): SourceQualityResult {
|
|
const n = normalizeMeta(meta);
|
|
|
|
// HARD CAP: retracted -> Tier D, weight 0.0, regardless of any other signal.
|
|
if (n.retracted) {
|
|
const why =
|
|
n.retractionWatchId !== null
|
|
? `Retraction Watch id ${n.retractionWatchId}`
|
|
: "flagged retracted";
|
|
return {
|
|
id: n.id,
|
|
tier: "D",
|
|
tierLabel: TIER_LABEL.D,
|
|
weight: 0.0,
|
|
retracted: true,
|
|
rationale: `Untrusted (Tier D): ${why}; a retracted source cannot support a claim.`,
|
|
};
|
|
}
|
|
|
|
const isPeerReviewed = n.journal !== null && !n.isPreprint;
|
|
|
|
let tier: SourceQualityTier;
|
|
let rationale: string;
|
|
|
|
if (isPeerReviewed) {
|
|
if (n.citations >= WELL_CITED_THRESHOLD) {
|
|
tier = "A";
|
|
rationale = `Peer-reviewed journal (${n.journal}) with ${n.citations} citations (>= ${WELL_CITED_THRESHOLD}): promoted to Tier A.`;
|
|
} else {
|
|
tier = "B";
|
|
rationale = `Peer-reviewed journal (${n.journal}) with ${n.citations} citations: Tier B.`;
|
|
}
|
|
} else {
|
|
const venue = n.isPreprint ? "Preprint" : "Unknown venue (no journal metadata)";
|
|
if (n.citations >= PREPRINT_CITED_THRESHOLD) {
|
|
tier = "B";
|
|
rationale = `${venue} but well cited (${n.citations} >= ${PREPRINT_CITED_THRESHOLD}): promoted to Tier B; capped there — never formally peer reviewed.`;
|
|
} else {
|
|
tier = "C";
|
|
rationale = `${venue} with ${n.citations} citations: Tier C (down-weighted, unreviewed evidence).`;
|
|
}
|
|
}
|
|
|
|
let weight = BASE_WEIGHT_BY_TIER[tier];
|
|
if (n.isOpenAccess) {
|
|
weight = clamp01(weight + OPEN_ACCESS_BONUS);
|
|
rationale += " Open-access bonus applied to weight.";
|
|
}
|
|
|
|
return {
|
|
id: n.id,
|
|
tier,
|
|
tierLabel: TIER_LABEL[tier],
|
|
weight: clamp01(weight),
|
|
retracted: false,
|
|
rationale,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Tier a batch of sources. Returns a NEW array in input order; no source is dropped
|
|
* (a low-quality source is DOWN-WEIGHTED, not discarded — its tier tells the caller
|
|
* how far to trust it).
|
|
*/
|
|
export function scoreSourceQualityBatch(
|
|
metas: readonly SourceQualityMeta[]
|
|
): SourceQualityResult[] {
|
|
return metas.map((meta) => scoreSourceQuality(meta));
|
|
}
|