"""Capture real loopback score/scan API responses for the optional metadata contract. Uses the existing public AA request, never a private profile or a fabricated engine payload. Run in the calibrated Linux runtime for final evidence. """ from __future__ import annotations import hashlib import json import platform import threading import urllib.request from http.server import ThreadingHTTPServer from pathlib import Path from scripts.jyotish_api_server import DEFAULT_ALLOWED_HOSTS, DEFAULT_ALLOWED_ORIGINS, JyotishAPIHandler ROOT = Path(__file__).resolve().parents[2] def main(): source = ROOT / "frontend/tests/fixtures/rectification-grounding-aa-30min.golden.json" request = json.loads(source.read_text(encoding="utf-8"))["request"] server = ThreadingHTTPServer(("127.0.0.1", 0), JyotishAPIHandler) server.daemon_threads = True origin = f"http://127.0.0.1:{server.server_port}" server.allowed_origins = set(DEFAULT_ALLOWED_ORIGINS) | {origin} server.allowed_hosts = set(DEFAULT_ALLOWED_HOSTS) thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() def post(path, body): req = urllib.request.Request(origin + path, data=json.dumps(body).encode(), method="POST", headers={"Content-Type": "application/json", "Origin": origin}) with urllib.request.urlopen(req, timeout=180) as response: return json.load(response) try: response = post("/api/rectification/v5/score", request) intervals = response["decision_receipt"]["candidate_intervals"] assert len(intervals) == 1 scan_request = {"start_at": intervals[0]["start_at"], "end_at": intervals[0]["end_at"], "latitude": request["lat"], "longitude": request["lon"], "timezone_offset": request["tz"], "target_charts": ["D1", "D9", "D10"]} scan = post("/api/rectification/v5/varga-segments", scan_request) payload = {"source": "real loopback API; existing public AA calibration request", "python_version": platform.python_version(), "source_request_sha256": hashlib.sha256(source.read_bytes()).hexdigest(), "request": request, "response": response, "scan_request": scan_request, "scan": scan} target = ROOT / "frontend/tests/fixtures/varga-api-response.golden.json" target.write_text(json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8") print("captured real score and minute scan API responses") finally: server.shutdown() thread.join(timeout=10) server.server_close() if __name__ == "__main__": main()