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

66 lines
2.5 KiB
TypeScript

import type { NextRequest } from "next/server";
import { withOrg, parsePagination, type Ctx } from "@/lib/api/handler";
import { ok, created, fail } from "@/lib/api/response";
import { requireRole } from "@/lib/authz/rbac";
import {
createMonitor,
listMonitors,
recordEvent,
createMonitorSchema,
} from "@/lib/livingEvidence/monitor";
// Org-scoped living-evidence monitor collection.
//
// GET /api/evidence/living — paginated list of the org's monitors, newest
// first. Any member (viewer+) may read.
// POST /api/evidence/living — create a monitor for a topic/claim with an
// optional baseline body of studies. Editor+.
//
// The monitor's flip verdict itself is computed by the public deterministic
// compute route at /api/evidence/living/assess — no LLM touches the numbers.
export const runtime = "nodejs";
export const GET = withOrg(async (req: NextRequest, ctx: Ctx) => {
try {
requireRole(ctx, "viewer");
const { limit, offset, page } = parsePagination(req);
const { items, total } = await listMonitors(ctx.org.id, limit, offset);
return ok(items, { total, page, limit });
} catch (err: unknown) {
if (err instanceof Error && typeof (err as { status?: unknown }).status === "number") {
return fail(err.message, (err as unknown as { status: number }).status);
}
console.error("[/api/evidence/living GET] failed:", err);
return fail("Failed to load living-evidence monitors.", 500);
}
});
export const POST = withOrg(async (req: NextRequest, ctx: Ctx) => {
try {
requireRole(ctx, "editor");
const raw = await req.json().catch(() => null);
const parsed = createMonitorSchema.safeParse(raw);
if (!parsed.success) {
const issue = parsed.error.issues[0];
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
return fail(`Invalid monitor — ${where}${issue?.message ?? "check your inputs."}`, 400);
}
const monitor = await createMonitor(ctx.org.id, ctx.user.id, parsed.data);
// Seed the timeline with a deterministic 'created' event (ids/counts only).
await recordEvent(ctx.org.id, monitor.id, "created", {
baselineCount: parsed.data.baseline?.length ?? 0,
});
return created(monitor);
} catch (err: unknown) {
if (err instanceof Error && typeof (err as { status?: unknown }).status === "number") {
return fail(err.message, (err as unknown as { status: number }).status);
}
console.error("[/api/evidence/living POST] failed:", err);
return fail("Failed to create living-evidence monitor.", 500);
}
});