caelum0x 91fe8978d8 feat: enterprise + evidence depth — living evidence, verdict fragility, gated audit export, DSAR + legal hold
Evidence:
- Living Evidence Monitoring: deterministic cumulative meta-analysis (re-pool in time order, first-significant
  + would-a-new-study-flip); monitors + events (0069); backend/engines/pyalex citation-velocity (in-place);
  /api/evidence/living(+/assess) + /console/living-evidence
- Verdict Fragility: Walsh Fragility Index (exhaustive Fisher-exact reassignment) + meta leave-one-out
  robustness + required-information-size; /api/evidence/fragility + /console/fragility

Enterprise:
- Immutable audit export (Enterprise-tier gated): verifyChain + per-entry integrity proofs + export_hash;
  FIRST real requireFeature enforcement -> UpgradeRequired 402 with upgrade CTA; /api/enterprise/audit-export
- Data governance: legal holds (block retention purge) + DSAR export (no secrets/hashes) (0070);
  /api/governance/legal-hold + /api/governance/dsar + /console/governance/data

All numeric/verdict paths deterministic (no LLM); org-scoped; tsc + build green; 0069/0070 applied to Neon.
2026-07-11 03:25:07 +03:00

65 lines
2.4 KiB
TypeScript

import { NextRequest } from "next/server";
import { ok, fail } from "@/lib/api/response";
import { checkRateLimit } from "@/lib/rateLimit";
import { logEvent } from "@/lib/logger";
import { assessLivingEvidence, assessRequestSchema } from "@/lib/livingEvidence/monitor";
// Public POST endpoint for LIVING-EVIDENCE ASSESSMENT. Given a time-ordered body of
// { studies[] } plus a single { candidate } new study, it deterministically
// re-pools the accumulating evidence (cumulative meta-analysis) and returns:
// - the running pooled estimate at each accrual step (for a cumulative timeline),
// - when the pooled effect first reached significance,
// - a FLIP verdict: would_flip / strengthens / weakens / no_change /
// insufficient_evidence — decided purely by inverse-variance pooling math.
//
// No LLM is in the numeric loop. An unpoolable body (fewer than two usable studies
// even after the candidate) returns an honest insufficient_evidence rather than a
// forced answer.
export const runtime = "nodejs";
export async function POST(req: NextRequest) {
const start = Date.now();
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
const rate = checkRateLimit(ip);
if (!rate.allowed) {
logEvent("evidence.living.assess.rate_limited", { ip });
return fail("Rate limit reached. Please try again shortly.", 429);
}
let raw: unknown;
try {
raw = await req.json();
} catch {
return fail("Request body must be valid JSON.", 400);
}
const parsed = assessRequestSchema.safeParse(raw);
if (!parsed.success) {
const issue = parsed.error.issues[0];
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
return fail(
`Invalid living-evidence request — ${where}${issue?.message ?? "provide studies[] and a candidate."}`,
400
);
}
try {
const result = assessLivingEvidence(parsed.data);
logEvent("evidence.living.assess.success", {
latencyMs: Date.now() - start,
verdict: result.verdict,
studyCount: parsed.data.studies.length,
usableCount: result.cumulative.usableCount,
});
return ok(result);
} catch (err) {
logEvent("evidence.living.assess.error", { latencyMs: Date.now() - start, error: String(err) });
console.error("[/api/evidence/living/assess] failed:", err);
return fail(
"Something went wrong while assessing the living evidence. This has been logged — please check your inputs and try again.",
500
);
}
}