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

124 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""OpenAlex Works search — zero-dependency stdin/stdout bridge.
Bridge: lib/engines/openalex.ts (searchOpenAlex). Reads a JSON job on stdin
{"query": str, "limit"?: int, "email"?: str} and emits exactly one JSON object
on stdout:
success: {"ok": true, "works": [
{"openalex_id", "title", "abstract", "doi", "year",
"cited_by_count", "is_retracted"}, ...]}
handled error: {"ok": false, "error": "..."} (exit 1)
Uses only the Python standard library (urllib) — no pip dependencies, works out
of the box. Queries the public OpenAlex REST API. The caller's query text is read
from stdin only and is never logged.
"""
import json
import sys
import urllib.parse
import urllib.request
OPENALEX_ENDPOINT = "https://api.openalex.org/works"
DEFAULT_LIMIT = 10
MAX_LIMIT = 200 # OpenAlex per-page cap
HTTP_TIMEOUT_S = 25
def _short_id(full_id):
"""Turn 'https://openalex.org/W123' into 'W123' (leave other shapes as-is)."""
if not isinstance(full_id, str) or not full_id:
return None
return full_id.rstrip("/").rsplit("/", 1)[-1] or None
def _bare_doi(doi):
"""Strip the 'https://doi.org/' prefix so the caller gets a bare DOI."""
if not isinstance(doi, str) or not doi:
return None
for prefix in ("https://doi.org/", "http://doi.org/", "doi.org/"):
if doi.startswith(prefix):
return doi[len(prefix):]
return doi
def _reconstruct_abstract(inverted_index):
"""Rebuild abstract text from OpenAlex's inverted-index representation."""
if not isinstance(inverted_index, dict) or not inverted_index:
return None
positioned = []
for word, positions in inverted_index.items():
if not isinstance(positions, list):
continue
for pos in positions:
if isinstance(pos, int):
positioned.append((pos, word))
if not positioned:
return None
positioned.sort(key=lambda pair: pair[0])
return " ".join(word for _pos, word in positioned)
def _digest(work):
idx = work.get("abstract_inverted_index")
year = work.get("publication_year")
cited = work.get("cited_by_count")
return {
"openalex_id": _short_id(work.get("id")),
"title": work.get("title"),
"abstract": _reconstruct_abstract(idx),
"doi": _bare_doi(work.get("doi")),
"year": int(year) if isinstance(year, int) else None,
"cited_by_count": int(cited) if isinstance(cited, int) else None,
"is_retracted": bool(work.get("is_retracted", False)),
}
def search(job):
query = job.get("query")
if not isinstance(query, str) or not query.strip():
return {"ok": False, "error": "openalex: a non-empty 'query' is required"}
limit = job.get("limit")
if not isinstance(limit, int) or limit <= 0:
limit = DEFAULT_LIMIT
limit = min(limit, MAX_LIMIT)
params = {
"search": query,
"per-page": str(limit),
"select": "id,title,abstract_inverted_index,doi,publication_year,"
"cited_by_count,is_retracted",
}
email = job.get("email")
if isinstance(email, str) and email.strip():
params["mailto"] = email.strip() # OpenAlex polite pool
url = OPENALEX_ENDPOINT + "?" + urllib.parse.urlencode(params)
req = urllib.request.Request(url, headers={"User-Agent": "PaperTrail/1.0 (openalex bridge)"})
with urllib.request.urlopen(req, timeout=HTTP_TIMEOUT_S) as resp: # noqa: S310 (fixed https host)
payload = json.loads(resp.read().decode("utf-8"))
results = payload.get("results")
if not isinstance(results, list):
return {"ok": False, "error": "openalex: unexpected response shape (no results array)"}
works = [_digest(w) for w in results if isinstance(w, dict)]
return {"ok": True, "works": works}
def main():
try:
raw = sys.stdin.read()
job = json.loads(raw) if raw.strip() else {}
out = search(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())