Files
Jyotisha/scripts/research/varga_resolution_persisted_replay.py
T
2026-10-17 21:16:32 +08:00

104 lines
5.1 KiB
Python

"""Public holdout replay through live API and production PostgreSQL answer paths.
Explicit fictional Case seeds are not successful product opening. No turn,
evidence, focus or answer is seeded: a refused production RPC stops that row.
Off is the default; on changes only segment ordering. Truth is oracle-only.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import platform
import subprocess
import threading
from http.server import ThreadingHTTPServer
from pathlib import Path
from scripts.jyotish_api_server import DEFAULT_ALLOWED_HOSTS, DEFAULT_ALLOWED_ORIGINS, JyotishAPIHandler
from scripts.research import varga_resolution_lib as vr
from scripts.research.minute_resolution_sweep import scoring_request_for
ROOT = Path(__file__).resolve().parents[2]
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", default=str(vr.HOLDOUT_V5))
parser.add_argument("--output", required=True)
parser.add_argument("--limit", type=int, default=10)
parser.add_argument("--radii", default="10,30,60")
parser.add_argument("--study", action="store_true")
parser.add_argument("--modes", default="all_correct,flip_1,flip_3,flip_5")
parser.add_argument("--orderings", default="off")
parser.add_argument("--code-sha", default="")
args = parser.parse_args()
target = Path(args.output)
if target.exists():
parser.error("--output already exists; retain prior evidence and choose a unique label")
cases = vr.load_cases(Path(args.dataset))
if args.limit:
cases = cases[:args.limit]
rows = []
for case in cases:
birth = case["birth"]
for radius in map(int, args.radii.split(",")):
request = scoring_request_for(case, radius)
rows.append({"case_key": case["case_id"], "radius": radius,
"snapshot": {"birth_date": birth["date"], "reported_birth_time": str(birth["time"])[:5],
"birth_time_source": "approximate", "active_birth_time": None,
"uncertainty_before_minutes": radius, "uncertainty_after_minutes": radius,
"latitude": request["lat"], "longitude": request["lon"],
"timezone_offset": request["tz"], "timezone_id": birth.get("timezone_id")},
"range": {"start_time": request["start_time"], "end_time": request["end_time"]},
"events": request["events"], "oracle_truth_minute": str(birth["time"])[:5]})
server = ThreadingHTTPServer(("127.0.0.1", 0), JyotishAPIHandler)
server.daemon_threads = True
origin = f"http://127.0.0.1:{server.server_port}"
server.allowed_hosts = set(DEFAULT_ALLOWED_HOSTS)
server.allowed_origins = set(DEFAULT_ALLOWED_ORIGINS) | {origin}
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
env = {**os.environ, "JYOTISH_API_BASE": origin}
body: dict = {"rows": rows}
if args.study:
body["study"] = {
"modes": [item for item in args.modes.split(",") if item],
"orderings": [item for item in args.orderings.split(",") if item],
"code_sha": args.code_sha,
}
result = subprocess.run(["node", "--import", "tsx", "tests/research/rectification-segment-persisted-replay.ts"],
cwd=ROOT / "frontend", env=env, input=json.dumps(body),
text=True, encoding="utf-8", stdout=subprocess.PIPE, check=False)
payload = json.loads(result.stdout)
payload.update({"python_version": platform.python_version(),
"dataset_sha256": hashlib.sha256(Path(args.dataset).read_bytes()).hexdigest(),
"runner_exit": result.returncode})
if args.study:
for row in payload.get("records") or []:
if isinstance(row, dict):
for key in ("asked", "gates", "ledger_before", "ledger_after", "metrics", "targets", "domain"):
row.pop(key, None)
payload["rerun"] = {
"command": "python scripts/research/varga_resolution_persisted_replay.py --study "
f"--modes {args.modes} --orderings {args.orderings} --radii {args.radii} "
f"--limit {args.limit} --code-sha {args.code_sha or '<HEAD>'} --output <new-file>",
"max_rounds": "DEFAULT_MAX_DISCRIMINATION_ROUNDS",
"truth_clock": "omitted; real-case calibration stores containment booleans only",
}
target.parent.mkdir(parents=True, exist_ok=True)
with target.open("x", encoding="utf-8") as stream:
stream.write(json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2) + "\n")
print(f"persisted replay rows={len(payload.get('records', []))}; runner_exit={result.returncode}", flush=True)
return result.returncode
finally:
server.shutdown()
thread.join(timeout=10)
server.server_close()
if __name__ == "__main__":
raise SystemExit(main())