Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/retraining_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 43 additions & 0 deletions tests/test_health_missing_drivers.py
Original file line number Diff line number Diff line change
@@ -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()
Loading