Coverage for retraining/rollback_hardening.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""V5.4.1 - auto-rollback hardening: dry-run mode, continuous degradation 

2score, and integration tests with fakeredis + mock Postgres. 

3 

4The dry-run mode returns the same decision dict but adds a 

5`dry_run: True` key so the caller can log what would have happened without 

6executing. The degradation score is a continuous 0-1 weighted composite of 

7all trigger severities, giving more signal than the binary flags alone.""" 

8 

9from __future__ import annotations 

10 

11from typing import Any 

12 

13 

14def compute_degradation_score(degradation_signals: dict[str, Any], *, weights: dict[str, Any] | None = None) -> float: 

15 """Continuous 0-1 composite of all trigger severities. 

16 

17 Default weights: kill_switch_tripped=0.40 (most severe), net_sharpe_decay=0.25, 

18 rank_ic_decay=0.20, slippage_divergence=0.15. Each signal contributes its 

19 weight if True, 0 if False/missing. Result is capped at 1.0.""" 

20 w = weights or { 

21 "kill_switch_tripped": 0.40, 

22 "net_sharpe_decay": 0.25, 

23 "rank_ic_decay": 0.20, 

24 "slippage_divergence": 0.15, 

25 } 

26 score = sum(float(wt) for name, wt in w.items() if bool(degradation_signals.get(name, False))) 

27 # V6.6.7 (#377): custom negative weights gave a negative score; the composite stays in [0, 1] 

28 return max(0.0, min(round(score, 4), 1.0)) 

29 

30 

31def select_rollback_target_dry_run( 

32 active_version: dict[str, Any], 

33 version_history: list[dict], 

34 degradation_signals: dict[str, Any], 

35 config: dict[str, Any], 

36) -> dict[str, Any]: 

37 """Dry-run wrapper around select_rollback_target(): identical return 

38 shape plus `dry_run: True` so callers can audit rollback decisions 

39 without executing them.""" 

40 from retraining.auto_rollback import select_rollback_target 

41 

42 result = select_rollback_target(active_version, version_history, degradation_signals, config) 

43 result["dry_run"] = True 

44 result["degradation_score"] = compute_degradation_score(degradation_signals) 

45 return result