caelum0x 869d44f1ec feat: Next tier — contradiction atlas, subgroup+pub-bias hardening, RAG-fusion+sufficiency, chain of custody
- 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.
2026-07-11 00:58:44 +03:00

61 lines
2.1 KiB
TypeScript

"use client";
// Client-side, browser-safe recomputation of a per-span chain-of-custody hash.
// This MUST byte-for-byte mirror the server's computeCustodyHash in
// lib/provenance/chainOfCustody.ts: sha256Hex(canonicalize(tuple)), where
// canonicalize() is order-independent JSON with recursively sorted object keys
// (see lib/compliance/hash.ts). We reimplement both here using Web Crypto so the
// "verify hash" button proves integrity entirely in the browser — no server round
// trip, no trust in a second endpoint.
// Canonical JSON: recursively sort object keys; preserve array order. Mirrors
// lib/compliance/hash.ts canonicalize(). Only the JSON-representable value types
// produced by the custody tuple appear here (string | number | null).
function sortValue(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(sortValue);
}
if (value !== null && typeof value === "object") {
const obj = value as Record<string, unknown>;
const sorted: Record<string, unknown> = {};
for (const key of Object.keys(obj).sort()) {
sorted[key] = sortValue(obj[key]);
}
return sorted;
}
return value;
}
function canonicalize(value: unknown): string {
return JSON.stringify(sortValue(value));
}
async function sha256Hex(input: string): Promise<string> {
const bytes = new TextEncoder().encode(input);
const digest = await crypto.subtle.digest("SHA-256", bytes);
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
// The exact tuple hashed on the server, in the same field set. canonicalize()
// makes field order here irrelevant, but we keep it aligned for readability.
export interface CustodyHashTuple {
verification_id: string;
source_id: string;
doi: string | null;
pmid: string | null;
source_version: string | null;
snapshot_date: string | null;
content_hash: string | null;
source_span: string;
span_start: number;
span_end: number;
}
export async function recomputeCustodyHash(
tuple: CustodyHashTuple
): Promise<string> {
return sha256Hex(canonicalize(tuple));
}