caelum0x 4f7ba29383 feat: production hardening — real Python engines, no-stub verify guard, console UI kit
Backend (make everything real, no stubs):
- Implement all 10 Python engine scripts (scispacy, pymare, pytrials, minicheck,
  indra, openalex, paperqa, pykeen, storm, asreview) behind the existing lib/engines
  bridges. openalex + pytrials are zero-dep (stdlib urllib, work out of the box); the
  8 heavy engines run the real library when installed and emit an honest structured
  error envelope when absent — no phantom capability, no faked outputs.
- /api/verify: hard production guard so the offline fixture path can NEVER activate in
  production (MOCK_MODE gated to NODE_ENV!=production); prod always runs the real
  retrieve→extract→verify pipeline.
- /api/auth/google: wrap the OAuth entrypoint in try/catch — fail closed to /login.
- MoA: wire the previously-dangling design_priors artifact into the multivers
  cross-source verifier as a mean-preserving, clamped (+/-25%) design-credibility
  weight; exact no-op when absent, so the benchmark verdict mix is protected.

Frontend (enterprise-grade polish):
- New shared console UI kit (components/console/ui.tsx): Skeleton, Card, Badge,
  Button, StatCard, EmptyState — one source for the house tokens.
- Skeleton shimmer keyframe; LoadingBanner now renders shimmer instead of plain text
  (propagates to ~29 tool pages, signature unchanged).
- Real workspace overview landing page: live stats (fixes a phantom 'claims' field
  the old page always rendered as 0), recent verifications with permalinks, quick
  actions, skeleton loading + empty + error states.

Verified: tsc clean, 10/10 verify integration tests pass, production build compiles
all 417 pages.
2026-07-11 21:46:23 +03:00

94 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""ASReview active-learning re-ranking — stdin/stdout bridge.
Bridge: lib/engines/asreview.ts (rankRecords). Reads a JSON job on stdin
{"records": [{"id", "title", "abstract"}], "labeled": [{"id", "label": 0|1}]}
and emits exactly one JSON object on stdout:
success: {"ok": true, "ranking": [{"id", "relevance": float}, ...]} (unlabeled
records only, most-relevant-first)
handled error: {"ok": false, "error": "..."} (exit 1)
Dep-gated: requires `asreview` (pip install asreview). When absent the script emits
an honest error envelope and exits 1 — never fake rankings. Trains a real
TF-IDF + Naive Bayes learner on the human decisions and scores the remaining
records. Record text is read from stdin only and is never logged.
"""
import json
import sys
def rank(job):
records = job.get("records")
labeled = job.get("labeled")
if not isinstance(records, list) or not records:
return {"ok": False, "error": "asreview: a non-empty 'records' array is required"}
if not isinstance(labeled, list):
return {"ok": False, "error": "asreview: a 'labeled' array is required"}
label_by_id = {}
for entry in labeled:
if isinstance(entry, dict) and "id" in entry and entry.get("label") in (0, 1):
label_by_id[entry["id"]] = int(entry["label"])
if len(set(label_by_id.values())) < 2:
return {
"ok": False,
"error": "asreview: need at least one relevant (1) and one irrelevant (0) label to train",
}
try:
import numpy as np
from asreview.models.classifiers import NaiveBayesClassifier
from asreview.models.feature_extraction import Tfidf
except ImportError:
return {"ok": False, "error": "asreview not installed; install asreview to enable this engine"}
texts = []
for rec in records:
title = rec.get("title") if isinstance(rec, dict) else None
abstract = rec.get("abstract") if isinstance(rec, dict) else None
texts.append(" ".join(part for part in (title, abstract) if isinstance(part, str)))
feature_model = Tfidf()
features = feature_model.fit_transform(texts)
train_idx = [i for i, rec in enumerate(records)
if isinstance(rec, dict) and rec.get("id") in label_by_id]
y_train = np.array([label_by_id[records[i]["id"]] for i in train_idx])
classifier = NaiveBayesClassifier()
classifier.fit(features[train_idx], y_train)
unlabeled_idx = [i for i, rec in enumerate(records)
if not (isinstance(rec, dict) and rec.get("id") in label_by_id)]
ranking = []
if unlabeled_idx:
proba = classifier.predict_proba(features[unlabeled_idx])
# Column 1 is P(relevant); classes_ ordering is [0, 1] for the trained learner.
relevant_col = 1 if proba.shape[1] > 1 else 0
for pos, idx in enumerate(unlabeled_idx):
ranking.append({
"id": records[idx]["id"],
"relevance": float(proba[pos, relevant_col]),
})
ranking.sort(key=lambda row: row["relevance"], reverse=True)
return {"ok": True, "ranking": ranking}
def main():
try:
raw = sys.stdin.read()
job = json.loads(raw) if raw.strip() else {}
out = rank(job)
print(json.dumps(out))
return 0 if out.get("ok") else 1
except Exception as exc: # noqa: BLE001 — surface every failure as a JSON envelope
print(json.dumps({"ok": False, "error": f"{type(exc).__name__}: {exc}"}))
return 1
if __name__ == "__main__":
raise SystemExit(main())