Coverage for scripts/lean_smoke/live_plumbing_report.py: 100%

38 statements  

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

1"""What a `live_plumbing.sh` run proved, read from its logs (V6.6.4, B6). Pure functions over text, so a test can hold the readings to fixture logs. 

2 

3 python scripts/lean_smoke/live_plumbing_report.py --dir /tmp/aq_lean/live_plumbing --out ml/evaluation/v664/live_plumbing.json 

4""" 

5 

6from __future__ import annotations 

7 

8import argparse 

9import json 

10import re 

11from collections.abc import Mapping 

12from pathlib import Path 

13 

14CHECKS = { 

15 "engine_started_live": r"LEAN ALGORITHMIC TRADING ENGINE", 

16 "main_py_imported": r"main successfully imported", 

17 "paper_brokerage": r"creating brokerage 'PaperBrokerage'", 

18 "fake_data_queue": r"FakeDataQueue", 

19 "ticks_flowing": r"TICKS PER SECOND:: [1-9]", 

20 "warmed_up": r"Algorithm finished warming up", 

21} 

22FAILURES = (r"Traceback", r"Algorithm\.Initialize\(\) Error", r"Runtime Error", r"PythonException", r"Unhandled exception") 

23 

24 

25def scan_engine_log(text: str) -> dict: 

26 """Which startup stages the engine log shows, and any failure it shows.""" 

27 found = {name: bool(re.search(pattern, text)) for name, pattern in CHECKS.items()} 

28 found["failures"] = [line.strip()[:200] for line in text.splitlines() if any(re.search(pattern, line) for pattern in FAILURES)][:5] 

29 return found 

30 

31 

32def scan_algorithm_log(text: str) -> dict: 

33 """What main.py itself logged: warm-up, and any audit-queue trouble it reported while a service was down.""" 

34 lines = text.splitlines() 

35 return { 

36 "lines": len(lines), 

37 "warmup_finished": any("finished warming up" in line for line in lines), 

38 "audit_push_failures": sum("AuditQueue.push failed" in line for line in lines), 

39 "errors": [line.strip()[:200] for line in lines if re.search(r"\b(error|exception|traceback)\b", line, re.IGNORECASE)][:5], 

40 } 

41 

42 

43def build_report(run1: Mapping, run2: Mapping, facts: Mapping) -> dict: 

44 """The per-scenario verdicts. A scenario is `passed` only when its evidence is in the logs; anything not observed says so.""" 

45 engine1, engine2 = scan_engine_log(run1.get("engine", "")), scan_engine_log(run2.get("engine", "")) 

46 algo1 = scan_algorithm_log(run1.get("algorithm", "")) 

47 startup = all(engine1[name] for name in CHECKS) and not engine1["failures"] 

48 return { 

49 "scenarios": { 

50 "starts_in_live_mode_on_paper_brokerage": {"passed": startup, "engine": engine1}, 

51 "alive_through_redis_and_postgres_outages": { 

52 "passed": bool(facts.get("alive_after_outages")) and not engine1["failures"], 

53 "outage_seconds": facts.get("outage_seconds"), 

54 "audit_push_failures_logged": algo1["audit_push_failures"], 

55 "note": "the algorithm kept running while both services were stopped, and was still running after they came back", 

56 }, 

57 "restart_comes_back_up": {"passed": all(engine2[name] for name in CHECKS) and not engine2["failures"] and bool(facts.get("state_file_after_restart")), "engine": engine2}, 

58 "audit_chain_verifies": {"passed": bool(facts.get("audit_verify_ok")), "vacuous": "trivially valid" in facts.get("audit_verify_text", ""), "detail": facts.get("audit_verify_text", "")}, 

59 }, 

60 "not_proved": [ 

61 "an order reaching a broker: no rebalance fell inside the window, and PaperBrokerage fills against the fake ticks, not a market", 

62 "the kill switch under a forced drawdown: main.py is unchanged and FakeDataQueue prices are random, so no drawdown was forced", 

63 "reconciliation against a real broker's positions (needs the IB or Coinbase account)", 

64 ], 

65 "algorithm": algo1, 

66 } 

67 

68 

69def main() -> int: 

70 parser = argparse.ArgumentParser(description=__doc__) 

71 parser.add_argument("--dir", required=True) 

72 parser.add_argument("--out", required=True) 

73 args = parser.parse_args() 

74 base = Path(args.dir) 

75 

76 def read(name: str) -> str: 

77 path = base / name 

78 return path.read_text(encoding="utf-8", errors="replace") if path.exists() else "" 

79 

80 facts = json.loads(read("facts.json") or "{}") 

81 report = build_report({"engine": read("engine1.log"), "algorithm": read("results1/AetherQuantAlgorithm-log.txt")}, 

82 {"engine": read("engine2.log"), "algorithm": read("results2/AetherQuantAlgorithm-log.txt")}, facts) 

83 Path(args.out).parent.mkdir(parents=True, exist_ok=True) 

84 Path(args.out).write_text(json.dumps(report, indent=1), encoding="utf-8", newline="\n") 

85 passed = {name: value["passed"] for name, value in report["scenarios"].items()} 

86 print(json.dumps(passed)) 

87 return 0 if all(passed.values()) else 1 

88 

89 

90if __name__ == "__main__": 

91 raise SystemExit(main())