|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Smoke-test the progress-review sentinel differential from recorded answers. |
| 3 | +
|
| 4 | +Replays the committed comparison matrix against the committed provider |
| 5 | +recordings, so it needs no credential and no network. It asserts the harness |
| 6 | +shape and that the replay reproduces the committed summary: the typed repeat |
| 7 | +fuse stays quiet on every self-declared `advanced` round, while each receipt |
| 8 | +signal's first-flag rounds match what the live run recorded. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | +from pathlib import Path |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | + |
| 18 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 19 | +sys.path.insert(0, str(REPO_ROOT)) |
| 20 | +sys.path.insert(1, str(REPO_ROOT / "packages" / "loopx-jev" / "src")) |
| 21 | + |
| 22 | +from loopx_jev.sentinel_compare import COMPARISON_SCHEMA, compare # noqa: E402 |
| 23 | +from loopx_jev.sentinel_matrix import load_sentinel_matrix # noqa: E402 |
| 24 | + |
| 25 | +FIXTURES = REPO_ROOT / "packages" / "loopx-jev" / "tests" / "fixtures" / "sentinel" |
| 26 | + |
| 27 | + |
| 28 | +def deterministic_view(comparison: dict) -> dict: |
| 29 | + """Project the fields that must reproduce from recordings alone.""" |
| 30 | + |
| 31 | + return { |
| 32 | + case["case_id"]: { |
| 33 | + "first_flag_round": case["first_flag_round"], |
| 34 | + "first_obligation_round": case["first_obligation_round"], |
| 35 | + "typed_repeat_first_round": case["baseline"]["typed_repeat_first_round"], |
| 36 | + "statuses": [row["status"] for row in case["rounds"]], |
| 37 | + } |
| 38 | + for case in comparison["cases"] |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def main() -> int: |
| 43 | + matrix = load_sentinel_matrix(FIXTURES / "matrix.json") |
| 44 | + expected_path = FIXTURES / "expected_summary.json" |
| 45 | + if not expected_path.is_file(): |
| 46 | + print("expected_summary.json is missing; record it with `loopx-jev sentinel compare --live`") |
| 47 | + return 1 |
| 48 | + expected = json.loads(expected_path.read_text(encoding="utf-8")) |
| 49 | + with tempfile.TemporaryDirectory(prefix="loopx-sentinel-smoke-") as temporary: |
| 50 | + comparison = compare( |
| 51 | + matrix, |
| 52 | + responses=FIXTURES / "responses", |
| 53 | + live=False, |
| 54 | + model=expected["model"], |
| 55 | + deadline_ms=5000, |
| 56 | + drift_threshold=2, |
| 57 | + ) |
| 58 | + Path(temporary, "comparison.json").write_text(json.dumps(comparison), encoding="utf-8") |
| 59 | + assert comparison["schema_version"] == COMPARISON_SCHEMA |
| 60 | + assert comparison["execution"] == "recorded_replay" |
| 61 | + assert comparison["aggregate"]["cases"] == 16 |
| 62 | + assert comparison["aggregate"]["baseline"]["typed_repeat_fired_cases"] == 0 |
| 63 | + assert comparison["aggregate"]["signals"] == expected["live_aggregate"]["signals"] |
| 64 | + for case in comparison["cases"]: |
| 65 | + assert all(row["status"] != "not_captured" for row in case["rounds"]), case["case_id"] |
| 66 | + assert all( |
| 67 | + row["execution_kind"] == "recorded_replay" |
| 68 | + for row in case["rounds"] |
| 69 | + if row["status"] in {"completed", "abstained", "failed"} |
| 70 | + ), case["case_id"] |
| 71 | + actual = deterministic_view(comparison) |
| 72 | + if actual != expected["deterministic_view"]: |
| 73 | + for case_id, view in actual.items(): |
| 74 | + if view != expected["deterministic_view"].get(case_id): |
| 75 | + print("mismatch", case_id, json.dumps(view), json.dumps(expected["deterministic_view"].get(case_id))) |
| 76 | + return 1 |
| 77 | + print( |
| 78 | + json.dumps( |
| 79 | + { |
| 80 | + "status": "ok", |
| 81 | + "cases": comparison["aggregate"]["cases"], |
| 82 | + "signals": comparison["aggregate"]["signals"], |
| 83 | + "baseline": comparison["aggregate"]["baseline"]["typed_repeat_fired_cases"], |
| 84 | + }, |
| 85 | + indent=2, |
| 86 | + sort_keys=True, |
| 87 | + ) |
| 88 | + ) |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + raise SystemExit(main()) |
0 commit comments