"""Generate public v5 engine goldens; expected values come from accepted research.""" from __future__ import annotations import json import hashlib import platform from pathlib import Path from scripts.research import varga_resolution_lib as vr ROOT = Path(__file__).resolve().parents[2] def main(): cache = ROOT / "artifacts/varga-impl-validation-20260930/replay-cache" accepted_path = ROOT / "docs/research/varga_resolution_research_2026_09_30.json" accepted = json.loads(accepted_path.read_text(encoding="utf-8")) records = [] for case in vr.load_cases()[:12]: for radius in (10, 30): replay = vr.CaseReplay(case, radius, cache_dir=cache) state = vr.replay_scores(replay.public, replay.probes, replay.true_time) for chart in ("D1", "D9", "D10"): segments = replay.segments((chart,)) ordered = vr.select_probes_by_segment(replay.public, replay.probes, segments, replay.true_time) summary = vr.segment_shares(vr.minute_weights(state, "raw"), segments, replay.true_time) accepted_cell = next(row for row in accepted["m1"]["by_radius"][str(radius)][chart]["raw"]["per_case"] if row["case_id"] == replay.case_id) actual_cell = {key: summary[key] for key in ("top_share", "top_is_truth", "truth_retained", "alive_segments")} actual_cell["segments_in_window"] = len(segments) if any(value != accepted_cell[key] for key, value in actual_cell.items()): raise ValueError(f"accepted_raw_parity_mismatch:{replay.case_id}:{radius}:{chart}; use the calibrated Python runtime") records.append({ "case_id": replay.case_id, "radius": radius, "chart": chart, "rows": replay.public, "posterior_rows": state["public"], "eliminated": sorted(state["eliminated"]), "probes": replay.probes, "segments": segments, "offsets": {minute: vr.offset_of(minute, replay.true_time) for minute in replay.times}, "truth_offset": 0, "truth_time": replay.true_time, "expected_summary": summary, "accepted_cell": actual_cell, "expected_order": [probe["semantic_key"] for probe in ordered["asked"]], "answers": {probe["semantic_key"]: vr.optimal_answer(probe, replay.true_time) for probe in replay.probes}, }) path = ROOT / "frontend/tests/fixtures/varga-segment-v5-golden.json" path.write_text(vr.stable_json({ "source": "v5 public AA engine responses; raw cells strictly verified against accepted research", "python_version": platform.python_version(), "accepted_sha256": hashlib.sha256(accepted_path.read_bytes()).hexdigest(), "order_source": "sequential accepted research function; m2 JSON does not store semantic-key lists", "records": records, }), encoding="utf-8") print(f"generated {len(records)} chart goldens across 12 public cases") if __name__ == "__main__": main()