"""Start the v10 HTTP server and verify the public API paths.""" from __future__ import annotations import json from pathlib import Path import subprocess import sys import time from urllib.request import Request, urlopen ROOT = Path(__file__).resolve().parent OUT = ROOT / "logs" / "eval" / "ts_reasoner_v10_server_smoke.json" HOST = "127.0.0.1" PORT = 7863 BASE = f"http://{HOST}:{PORT}" def fetch_json(path: str, payload: dict | None = None) -> dict | list: if payload is None: with urlopen(f"{BASE}{path}", timeout=5) as res: return json.loads(res.read().decode("utf-8")) body = json.dumps(payload).encode("utf-8") req = Request(f"{BASE}{path}", data=body, headers={"Content-Type": "application/json"}, method="POST") with urlopen(req, timeout=5) as res: return json.loads(res.read().decode("utf-8")) def wait_ready(proc: subprocess.Popen) -> None: deadline = time.time() + 10 while time.time() < deadline: if proc.poll() is not None: raise RuntimeError(f"server exited early with {proc.returncode}") try: fetch_json("/healthz") return except Exception: time.sleep(0.1) raise TimeoutError("server did not become ready") def main() -> None: proc = subprocess.Popen( [sys.executable, "ts_reasoner_v10.py", "serve", "--host", HOST, "--port", str(PORT)], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) try: wait_ready(proc) health = fetch_json("/healthz") examples = fetch_json("/examples") solve = fetch_json("/solve", {"prompt": "Set ledger: A={a,b}; B={b,c}. Compute A union B:", "category": "set_reasoning"}) finally: proc.terminate() try: proc.wait(timeout=5) except subprocess.TimeoutExpired: proc.kill() proc.wait(timeout=5) ok = isinstance(health, dict) and health.get("ok") is True and isinstance(examples, list) and len(examples) >= 30 and solve.get("display_answer") == "{a,b,c}" payload = {"engine": "TensionLM-117M-TS-Reasoner-v10", "ok": ok, "health": health, "examples_count": len(examples) if isinstance(examples, list) else None, "solve": solve} OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text(json.dumps(payload, indent=2)) print(f"server_smoke={'ok' if ok else 'failed'}") print(f"Wrote {OUT}") if not ok: raise SystemExit(1) if __name__ == "__main__": main()