File size: 2,383 Bytes
4db9dde
 
 
 
 
 
 
52b7bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db9dde
52b7bfd
7437974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db9dde
 
52b7bfd
 
4db9dde
7437974
 
 
 
 
4db9dde
 
 
7437974
 
4db9dde
 
 
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
57
58
59
60
61
#!/usr/bin/env python3
"""CLI: run Sunnyware diagnostics startup checks."""
import sys, json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))
from sunnyware.diagnostics import run_startup_checks, get_report

def _strip_volatile(record: dict) -> dict:
    """Remove fields that change every run (id, timestamp, session_id)."""
    return {k: v for k, v in record.items()
            if k not in ("id", "timestamp", "session_id")}


def _stable_report(report: dict) -> dict:
    """Produce a deterministic report for hashing/attestation."""
    stable = {"summary": dict(sorted(report.get("summary", {}).items())), "records": {}}
    for sev, items in sorted(report.get("records", {}).items()):
        stripped = [_strip_volatile(r) for r in items]
        stripped.sort(key=lambda r: (r.get("cls", ""), r.get("message", "")))
        stable["records"][sev] = stripped
    return stable


def main():
    attest = "--attest" in sys.argv

    if attest:
        # Suppress ALL import-time banners (tools/orchestrator) so stdout
        # contains only the deterministic JSON -- makes the SHA-256 stable
        # across every capture context (direct CLI, attest_all, CI).
        import contextlib, io
        _null = io.StringIO()
        with contextlib.redirect_stdout(_null), contextlib.redirect_stderr(_null):
            run_startup_checks()
            report = get_report()
        print(json.dumps(_stable_report(report), indent=2, sort_keys=True))
        for sev in ("error", "fatal"):
            if report["summary"].get(sev, 0) > 0:
                return 1
        return 0

    # Human-readable mode
    print("Running startup diagnostics...")
    summary = run_startup_checks()
    print("Startup summary:", {"status": summary.get("status"),
                                "records": summary.get("records")})
    report = get_report()
    print(json.dumps(report, indent=2))
    out = Path("data/diagnostics/report.json")
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps(report, indent=2), encoding="utf-8")
    print(f"Report written: {out}")
    for sev in ("error", "fatal"):
        if report["summary"].get(sev, 0) > 0:
            print(f"[FAIL] {sev} records present"); return 1
    print("[PASS] no errors or fatals")
    return 0

if __name__ == "__main__":
    sys.exit(main())