caelum0x 1d4f80bb4c feat: expand backend/ — specialize 6 more OSS engines in place (MiniCheck, Loki, paper-qa, ASReview, BioCypher, STORM)
Each engine now carries a PaperTrail-native papertrail_*.py + PAPERTRAIL.md, mirrored by a native TS
module + public API (deterministic scoring; Claude only for grounded language steps):
- MiniCheck: negation-aware entailment + negative_supported verdict for absence claims
  (backend/engines/MiniCheck/papertrail_negation.py -> lib/grounding/negationEntailment.ts, /api/verify/absence-claim)
- Loki/OpenFactVerification: claim-frame on-topic reranker (cuts retrieval noise)
  (papertrail_rerank.py -> lib/agents/contextualRank.ts, /api/retrieval/rerank)
- paper-qa: source-quality tiers A/B/C/D (retracted -> untrusted, weight 0)
  (papertrail_source_quality.py -> lib/paperqa/sourceQuality.ts, /api/sources/quality-tier)
- ASReview: ensemble screening (inclusion + quality + risk-of-bias in one pass)
  (papertrail_ensemble.py -> lib/screening/ensemble.ts, /api/screening/ensemble)
- BioCypher: bring-your-own-KG CSV import with Biolink domain/range validation (0071 kg_import_batches)
  (papertrail_byokg.py -> lib/kg/byoKg.ts, /api/kg/import)
- STORM: structured Claim/Best-Case/Critique/Synthesis debate for mixed verdicts
  (papertrail_debate.py -> lib/synthesis/debate.ts, /api/synthesis/debate)

15 OSS engines now specialized in place. tsc + build green; 0071 applied to Neon.
2026-07-11 03:41:53 +03:00

91 lines
3.3 KiB
TypeScript

import { NextRequest } from "next/server";
import { z } from "zod";
import { ok, fail } from "@/lib/api/response";
import { checkRateLimit } from "@/lib/rateLimit";
import { logEvent } from "@/lib/logger";
import { verifyAbsenceClaim } from "@/lib/grounding/negationEntailment";
// Public POST endpoint for NEGATION-AWARE (ABSENCE) claim verification. Given
// { claim, source_text } it decides the claim's polarity DETERMINISTICALLY from a
// negation-cue lexicon, then asks Claude the polarity-NEUTRAL question "does the source
// assert PRESENCE / ABSENCE / NEITHER of the effect?" (returning a verbatim supporting
// sentence), grounds that sentence in the source, and maps (polarity x assertion) to a final
// label by a FIXED table: supported | negative_supported | refuted | nei.
//
// MOAT: no LLM decides the polarity, the score, or the final label — those are deterministic
// rules and a fixed table. The only model step is the neutral presence/absence judgement, and
// it counts only once its supporting sentence is grounded verbatim in the source; an
// ungroundable support is dropped and the verdict falls back honestly to `nei`. Native TS port
// of backend/engines/MiniCheck/papertrail_negation.py. Claim and source text are NEVER logged
// — only ids/counts/verdicts.
export const runtime = "nodejs";
const AbsenceClaimRequestSchema = z.object({
claim: z.string().trim().min(1, "claim must be a non-empty string").max(2000),
source_text: z
.string()
.trim()
.min(1, "source_text must be a non-empty string")
.max(100000),
effect: z.string().trim().min(1).max(2000).optional(),
});
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("verify.absence_claim.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 = AbsenceClaimRequestSchema.safeParse(raw);
if (!parsed.success) {
const issue = parsed.error.issues[0];
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
return fail(
`Invalid absence-claim request — ${where}${
issue?.message ?? "provide a claim and source_text."
}`,
400
);
}
try {
const result = await verifyAbsenceClaim({
claim: parsed.data.claim,
sourceText: parsed.data.source_text,
effect: parsed.data.effect,
});
logEvent("verify.absence_claim.success", {
latencyMs: Date.now() - start,
polarity: result.polarity,
negationCueCount: result.negation_cues.length,
sourceAssertion: result.source_assertion,
label: result.label,
grounded: result.supporting_span !== null,
groundingDropped: result.grounding_dropped,
});
return ok(result);
} catch (err) {
logEvent("verify.absence_claim.error", {
latencyMs: Date.now() - start,
error: String(err),
});
console.error("[/api/verify/absence-claim] failed:", err);
return fail(
"Something went wrong while verifying the absence claim. This has been logged — please check your inputs and try again.",
500
);
}
}