- Contradiction Atlas: deterministic conflict explainer (population/dose/tissue/follow-up) over Valsci mixed verdicts + INDRA belief; backend/engines/Valsci/papertrail_conflict.py + backend/engines/indra/grounding_hook.py (in-place); /api/verify/contradiction-resolve + /console/contradiction - Verification hardening: subgroup-cited-as-primary check wired into structuredVerification; publication-bias (Egger + trim-and-fill) auto-downgrades GRADE one step; /api/verify/subgroup-check + /api/evidence-report/meta-bias-analysis - RAG-fusion + evidence-sufficiency: backend/engines/R2R/papertrail_rag_fusion.py (in-place); ragFusionRetrieve + evidenceSufficiency; /api/research/expand-query - Immutable chain of custody: migration 0067 evidence_source_versions; lib/provenance/chainOfCustody.ts (sha256 over provenance tuple, re-grounded spans); anchorChainOfCustody into WORM chain; /api/audit-chain/verification/[id] + /console/audit/custody (in-browser hash verify) All edits to existing core files (structuredVerification/grade/hybrid/evidencePipeline/chain) surgical + additive; tsc + build green; 0067 applied to Neon.
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { z } from "zod";
|
|
import { withOrg, type Ctx } from "@/lib/api/handler";
|
|
import { ok, fail } from "@/lib/api/response";
|
|
import { requireRole } from "@/lib/authz/rbac";
|
|
import { getPool } from "@/lib/db";
|
|
import { buildChainOfCustody, type ChainOfCustody } from "@/lib/provenance/chainOfCustody";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
// GET /api/audit-chain/verification/[id] — reconstruct the EXACT chain-of-custody
|
|
// state for one stored verification at export time: every grounded span with its
|
|
// { source_id, doi/pmid, source_version, snapshot_date, verification_id,
|
|
// chain_of_custody_hash } tuple, plus a deterministic aggregate hash. Viewer+.
|
|
//
|
|
// No LLM, no scoring — pure deterministic assembly from the cached source + the
|
|
// snapshot version ledger. Spans that no longer ground against the current source
|
|
// text are dropped and counted (honest "we can't point to this anymore").
|
|
const idSchema = z.string().uuid();
|
|
|
|
function rbacStatus(err: unknown): number | null {
|
|
if (
|
|
err instanceof Error &&
|
|
typeof (err as unknown as { status?: unknown }).status === "number"
|
|
) {
|
|
return (err as unknown as { status: number }).status;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export const GET = withOrg(async (_req: NextRequest, ctx: Ctx, params) => {
|
|
try {
|
|
requireRole(ctx, "viewer");
|
|
|
|
const parsed = idSchema.safeParse(params?.id);
|
|
if (!parsed.success) {
|
|
return fail("Invalid verification id.", 400);
|
|
}
|
|
|
|
const custody = await buildChainOfCustody(getPool(), parsed.data);
|
|
if (!custody) {
|
|
return fail("Verification not found.", 404);
|
|
}
|
|
|
|
return ok<ChainOfCustody>(custody);
|
|
} catch (err: unknown) {
|
|
const status = rbacStatus(err);
|
|
if (status !== null) {
|
|
return fail((err as Error).message, status);
|
|
}
|
|
return fail("Couldn't reconstruct the chain of custody. Please try again.", 500);
|
|
}
|
|
});
|