diff --git a/src/retraining_engine.py b/src/retraining_engine.py index 88becd7..0918319 100644 --- a/src/retraining_engine.py +++ b/src/retraining_engine.py @@ -191,6 +191,10 @@ def calculate_health(): root_causes = root_cause_report.get("high_risk_root_causes", {}) if not isinstance(root_causes, dict): raise ValueError("Root-cause contributions must be an object") + if high_risk_rate > 0 and not root_causes: + raise ValueError("Missing high-risk root-cause contributions for production observations") + if high_risk_rate == 0 and root_causes: + raise ValueError("Stale high-risk root-cause contributions without high-risk observations") for contribution in root_causes.values(): if isinstance(contribution, bool) or not isinstance(contribution, Real): raise ValueError("Root-cause contributions must be finite percentages between 0 and 100") diff --git a/tests/test_health_missing_drivers.py b/tests/test_health_missing_drivers.py new file mode 100644 index 0000000..89b0bfe --- /dev/null +++ b/tests/test_health_missing_drivers.py @@ -0,0 +1,43 @@ +import json + +import pytest + +from src import retraining_engine as engine +from test_monitoring_contracts import setup_health + + +def test_health_rejects_missing_high_risk_driver_analysis(tmp_path, monkeypatch): + setup_health(tmp_path, monkeypatch, { + "is_anomaly": 1, "high_uncertainty": 1, "prediction_risk": "HIGH", + }) + (tmp_path / "causes.json").write_text(json.dumps({ + "high_risk_observations": 1, "high_risk_root_causes": {}, + }), encoding="utf-8") + output = tmp_path / "out.json" + output.write_text('{"previous": "report"}', encoding="utf-8") + with pytest.raises(ValueError, match="high-risk.*root-cause"): + engine.calculate_health() + assert json.loads(output.read_text()) == {"previous": "report"} + + +def test_health_allows_empty_drivers_when_no_high_risk_observations(tmp_path, monkeypatch): + setup_health(tmp_path, monkeypatch, { + "is_anomaly": 0, "high_uncertainty": 0, "prediction_risk": "LOW", + }) + (tmp_path / "causes.json").write_text(json.dumps({ + "high_risk_observations": 0, "high_risk_root_causes": {}, + }), encoding="utf-8") + engine.calculate_health() + assert json.loads((tmp_path / "out.json").read_text())["top_high_risk_drivers"] == {} + + +def test_health_rejects_stale_high_risk_drivers_when_no_observations_are_high_risk(tmp_path, monkeypatch): + setup_health(tmp_path, monkeypatch, { + "is_anomaly": 0, "high_uncertainty": 0, "prediction_risk": "LOW", + }) + (tmp_path / "causes.json").write_text(json.dumps({ + "high_risk_observations": 0, "high_risk_root_causes": {"signal": 100.0}, + }), encoding="utf-8") + with pytest.raises(ValueError, match="high-risk.*root-cause"): + engine.calculate_health() + assert not (tmp_path / "out.json").exists()