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

67 lines
2.5 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 { analyzeFragility, FragilityRequestSchema } from "@/lib/evidenceFragility";
// Public POST endpoint for VERDICT-FRAGILITY ANALYSIS. Two modes, dispatched by
// `mode`:
// { mode: "table", a, b, c, d } — the Walsh Fragility Index of a single 2x2
// table: the minimum number of event reassignments that flips the two-sided
// Fisher exact significance at p = 0.05.
// { mode: "meta", studies[], informationSize? } — leave-one-out robustness of a
// pooled random-effects verdict, plus (optionally) whether enough information
// has accrued relative to the required information size.
//
// The verdict is decided entirely by deterministic math — Fisher's exact test,
// meta-analytic pooling and the required information size formula. NO LLM is in
// the loop, and nothing is fabricated: an unpoolable or non-significant input
// yields an honest verdict rather than a forced one.
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.fragility.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 = FragilityRequestSchema.safeParse(raw);
if (!parsed.success) {
const issue = parsed.error.issues[0];
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
return fail(
`Invalid fragility request — ${where}${issue?.message ?? "provide a 2x2 { a, b, c, d } or { studies[] }."}`,
400
);
}
try {
const result = analyzeFragility(parsed.data);
logEvent("evidence.fragility.success", {
latencyMs: Date.now() - start,
mode: parsed.data.mode,
verdict: result.verdict,
studyCount: parsed.data.mode === "meta" ? parsed.data.studies.length : undefined,
});
return ok(result);
} catch (err) {
logEvent("evidence.fragility.error", { latencyMs: Date.now() - start, error: String(err) });
console.error("[/api/evidence/fragility] failed:", err);
return fail(
"Something went wrong while running the fragility analysis. This has been logged — please check your inputs and try again.",
500
);
}
}