evaluate_gates.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. """Evaluate run metas into honesty-preserving acceptance verdicts. Never invent PASS."""
  3. from __future__ import annotations
  4. import json
  5. import os
  6. from pathlib import Path
  7. def _st(rows: list[dict], *needles: str) -> str:
  8. hits = []
  9. for r in rows:
  10. name = (r.get("name") or "").lower()
  11. if all(n.lower() in name for n in needles):
  12. hits.append((r.get("status") or "UNKNOWN").upper())
  13. if not hits:
  14. return "NOT RUN"
  15. if any(h == "FAIL" for h in hits):
  16. return "FAIL"
  17. if any(h == "BLOCKED" for h in hits):
  18. return "BLOCKED"
  19. if all(h == "PASS" for h in hits):
  20. return "PASS"
  21. if any(h in {"NOT RUN", "NOT_RUN", "UNKNOWN"} for h in hits):
  22. return "NOT RUN"
  23. return "BLOCKED"
  24. def _all_named(rows: list[dict], names: tuple[str, ...]) -> str:
  25. sts = [_st(rows, n) for n in names]
  26. if any(s == "FAIL" for s in sts):
  27. return "FAIL"
  28. if any(s == "BLOCKED" for s in sts):
  29. return "BLOCKED"
  30. if all(s == "PASS" for s in sts):
  31. return "PASS"
  32. return "NOT RUN"
  33. def evaluate(rows: list[dict], *, soak_24h: str = "NOT RUN", hardware: str = "NOT RUN", expect_commit: str = "", expect_tree: str = "") -> dict[str, str]:
  34. go = _st(rows, "go-test")
  35. if go == "NOT RUN":
  36. go = _st(rows, "gotest")
  37. gofmt = _st(rows, "gofmt")
  38. race = _st(rows, "race")
  39. vet = _st(rows, "vet")
  40. static = _st(rows, "staticcheck")
  41. vuln = _st(rows, "govulncheck")
  42. buf_lint = _st(rows, "buf-lint")
  43. if buf_lint == "NOT RUN":
  44. buf_lint = _st(rows, "buf_lint")
  45. buf_break = _st(rows, "buf-breaking")
  46. if buf_break == "NOT RUN":
  47. buf_break = _st(rows, "buf_breaking")
  48. py = _st(rows, "python")
  49. pty = _st(rows, "pty")
  50. rtl = _st(rows, "rtl")
  51. systemd = _st(rows, "systemd")
  52. fuzz = _all_named(
  53. rows,
  54. (
  55. "fuzz-parser",
  56. "fuzz-profile",
  57. "fuzz-config",
  58. "fuzz-assemble",
  59. "fuzz-telemetry",
  60. "fuzz-capture",
  61. "fuzz-replay",
  62. "fuzz-sdk-descriptor",
  63. ),
  64. )
  65. if fuzz == "NOT RUN":
  66. fuzz = _st(rows, "fuzz-15m")
  67. soak2h = _st(rows, "soak-2h")
  68. if soak2h == "NOT RUN":
  69. soak2h = _st(rows, "soak_2h")
  70. pkg = _st(rows, "package")
  71. if pkg == "NOT RUN":
  72. pkg = _st(rows, "self-verify")
  73. bind = "PASS"
  74. for r in rows:
  75. st = (r.get("status") or "").upper()
  76. if st != "PASS":
  77. continue
  78. c = r.get("commit") or r.get("git_commit") or ""
  79. tr = r.get("tree") or r.get("git_tree") or ""
  80. dirty = (r.get("dirty") or r.get("git_dirty_state") or "").lower()
  81. if expect_commit and c and c != expect_commit:
  82. bind = "BLOCKED"
  83. if expect_tree and tr and tr != expect_tree:
  84. bind = "BLOCKED"
  85. if dirty and dirty not in {"clean", ""}:
  86. bind = "BLOCKED"
  87. p0 = all(s == "PASS" for s in (go, gofmt, race, vet, static, vuln, buf_lint, buf_break, py, pty, rtl, systemd, fuzz, soak2h, pkg)) and bind == "PASS"
  88. software = "PASS" if p0 else "BLOCKED"
  89. ready = "YES" if software == "PASS" else "NO"
  90. rc_ready = "YES" if software == "PASS" and soak_24h == "PASS" and hardware == "PASS" else "NO"
  91. prod = "NO"
  92. return {
  93. "architecture_direction": "PASS",
  94. "software_simulation_integration_acceptance": software,
  95. "software_safety_acceptance": software,
  96. "ready_for_controlled_hardware_validation": ready,
  97. "release_candidate_readiness": rc_ready,
  98. "production_readiness": prod,
  99. "hardware_validation": hardware,
  100. "real_actuator_validation": "NOT RUN",
  101. "gates": {
  102. "go_test": go,
  103. "gofmt": gofmt,
  104. "race": race,
  105. "vet": vet,
  106. "staticcheck": static,
  107. "govulncheck": vuln,
  108. "buf_lint": buf_lint,
  109. "buf_breaking": buf_break,
  110. "python": py,
  111. "pty": pty,
  112. "rtl": rtl,
  113. "systemd": systemd,
  114. "fuzz": fuzz,
  115. "soak_2h": soak2h,
  116. "soak_24h": soak_24h,
  117. "package": pkg,
  118. "commit_tree_bind": bind,
  119. },
  120. }
  121. def main() -> int:
  122. run = Path(os.environ.get("HTH_RUN_DIR") or "")
  123. if not run.is_dir():
  124. raise SystemExit("set HTH_RUN_DIR")
  125. man = run / "run-manifest.json"
  126. rows = []
  127. payload: dict = {}
  128. if man.is_file():
  129. payload = json.loads(man.read_text(encoding="utf-8"))
  130. rows = payload.get("commands") or []
  131. commit = payload.get("git_commit") if isinstance(payload, dict) else ""
  132. tree = payload.get("git_tree") if isinstance(payload, dict) else ""
  133. if not commit:
  134. commit = os.environ.get("HTH_GIT_COMMIT", "")
  135. if not tree:
  136. tree = os.environ.get("HTH_GIT_TREE", "")
  137. verdict = evaluate(rows, expect_commit=commit, expect_tree=tree)
  138. (run / "acceptance-verdict.json").write_text(json.dumps(verdict, indent=2) + "\n", encoding="utf-8")
  139. print(json.dumps(verdict, indent=2))
  140. return 0
  141. if __name__ == "__main__":
  142. raise SystemExit(main())