File size: 1,960 Bytes
2e658e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
"""Enforce explicit branch-coverage floors for execution-critical modules."""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

THRESHOLDS = {
    "hermes_overlay/trading/config_validation.py": 85.0,
    "hermes_overlay/trading/plan_integrity.py": 70.0,
    "hermes_overlay/trading/risk.py": 80.0,
    "hermes_overlay/trading/domain/execution_service.py": 50.0,
    "hermes_overlay/trading/futures_execution.py": 50.0,
    "hermes_overlay/trading/provider_contracts.py": 50.0,
}


def validate(report: dict) -> list[str]:
    errors: list[str] = []
    files = report.get("files") or {}
    for path, threshold in THRESHOLDS.items():
        entry = files.get(path)
        if not isinstance(entry, dict):
            errors.append(f"critical coverage module missing: {path}")
            continue
        summary = entry.get("summary") or {}
        percent = summary.get("percent_covered")
        if not isinstance(percent, (int, float)):
            errors.append(f"critical coverage percentage missing: {path}")
            continue
        if float(percent) + 1e-9 < threshold:
            errors.append(f"critical coverage below threshold: {path} {percent:.2f}% < {threshold:.2f}%")
    return errors


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("report", nargs="?", default="coverage.json")
    args = parser.parse_args(argv)
    try:
        report = json.loads(Path(args.report).read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError) as exc:
        print(f"ERROR: unable to read coverage report: {exc}")
        return 1
    errors = validate(report)
    for error in errors:
        print(f"ERROR: {error}")
    print(f"critical_coverage={'PASS' if not errors else 'FAIL'} modules={len(THRESHOLDS)}")
    return 1 if errors else 0


if __name__ == "__main__":
    raise SystemExit(main())