verify_review_package.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python3
  2. """Unzip-directory self-verify for an hth-platform review package.
  3. --mode offline: SHA, MANIFEST, permissions, L0 offline (buf missing = PARTIAL).
  4. --mode release: same plus buf lint+breaking required. Missing tools/logs FAIL.
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import hashlib
  9. import json
  10. import os
  11. import subprocess
  12. import sys
  13. import tempfile
  14. from pathlib import Path
  15. REQUIRED_REVIEWS = (
  16. "01-scope-architecture.md",
  17. "02-watchdog-protocol-truth.md",
  18. "03-actor-action-lifecycle.md",
  19. "04-sdk-replay-deployment.md",
  20. "05-evidence-artifact-release.md",
  21. )
  22. def main() -> int:
  23. ap = argparse.ArgumentParser()
  24. ap.add_argument("root", nargs="?", default=".")
  25. ap.add_argument("--mode", choices=("offline", "release"), default="offline")
  26. ap.add_argument("--full", action="store_true", help="also go build and pip wheel --no-deps")
  27. args = ap.parse_args()
  28. root = Path(args.root).resolve()
  29. sums = root / "SHA256SUMS"
  30. manifest = root / "MANIFEST"
  31. if not sums.is_file() or not manifest.is_file():
  32. print("FAIL missing SHA256SUMS or MANIFEST", file=sys.stderr)
  33. return 1
  34. rc = 0
  35. man_set: set[str] = set()
  36. for line in manifest.read_text(encoding="utf-8").splitlines():
  37. if not line.strip():
  38. continue
  39. if line.startswith("/") or ".." in Path(line).parts:
  40. print("FAIL path", line, file=sys.stderr)
  41. rc = 1
  42. man_set.add(line.strip())
  43. sum_set: set[str] = set()
  44. for line in sums.read_text(encoding="utf-8").splitlines():
  45. if not line.strip():
  46. continue
  47. digest, name = line.split(None, 1)
  48. name = name.strip()
  49. sum_set.add(name)
  50. path = root / name
  51. if not path.is_file():
  52. print("FAIL missing", name, file=sys.stderr)
  53. rc = 1
  54. continue
  55. got = hashlib.sha256(path.read_bytes()).hexdigest()
  56. if got != digest:
  57. print("FAIL sha256", name, file=sys.stderr)
  58. rc = 1
  59. mode = path.stat().st_mode & 0o777
  60. if mode == 0o777:
  61. print("FAIL 0777", name, file=sys.stderr)
  62. rc = 1
  63. if path.suffix == ".sh" or (path.suffix == ".py" and path.read_bytes()[:2] == b"#!" and path.parent.name == "tools"):
  64. if mode != 0o755:
  65. print("FAIL expected 0755", name, oct(mode), file=sys.stderr)
  66. rc = 1
  67. if man_set != sum_set:
  68. print("FAIL MANIFEST and SHA256SUMS file sets differ", file=sys.stderr)
  69. print("only_manifest", sorted(man_set - sum_set)[:20], file=sys.stderr)
  70. print("only_sums", sorted(sum_set - man_set)[:20], file=sys.stderr)
  71. rc = 1
  72. golden = root / "evidence" / "golden-vectors.json"
  73. if not golden.is_file():
  74. print("FAIL missing evidence/golden-vectors.json", file=sys.stderr)
  75. rc = 1
  76. host = root / "host"
  77. if not (host / "go.mod").is_file():
  78. print("FAIL host/go.mod missing", file=sys.stderr)
  79. return 1
  80. for name in REQUIRED_REVIEWS:
  81. if (root / "reviews" / name).is_file():
  82. continue
  83. if (host / "reviews" / name).is_file():
  84. continue
  85. print("FAIL missing review", name, file=sys.stderr)
  86. rc = 1
  87. run_dir = root / "run"
  88. man_json = run_dir / "run-manifest.json"
  89. if man_json.is_file():
  90. payload = json.loads(man_json.read_text(encoding="utf-8"))
  91. commits = {payload.get("git_commit")}
  92. trees = {payload.get("git_tree")}
  93. dirties = {payload.get("git_dirty_state")}
  94. times: set[str] = set()
  95. for cmd in payload.get("commands") or []:
  96. log = cmd.get("log") or ""
  97. if log:
  98. lp = run_dir / Path(log).name
  99. if not lp.is_file():
  100. print("FAIL missing run log", log, file=sys.stderr)
  101. rc = 1
  102. elif cmd.get("log_sha256"):
  103. got = hashlib.sha256(lp.read_bytes()).hexdigest()
  104. if got != cmd["log_sha256"]:
  105. print("FAIL log sha", log, file=sys.stderr)
  106. rc = 1
  107. elif args.mode == "release":
  108. print("FAIL missing log_sha256", cmd.get("name"), file=sys.stderr)
  109. rc = 1
  110. st = (cmd.get("status") or "").upper()
  111. name = cmd.get("name") or ""
  112. if st in {"NOT RUN", "NOT_RUN"}:
  113. if not cmd.get("not_run_reason_sha256") and not cmd.get("command") and not cmd.get("not_run_reason"):
  114. print("FAIL NOT RUN missing reason", name, file=sys.stderr)
  115. rc = 1
  116. continue
  117. if st == "PASS" and (not log or not cmd.get("log_sha256")):
  118. print("FAIL PASS without raw log/sha", name, file=sys.stderr)
  119. rc = 1
  120. if "NOT RUN" in st and "PASS" in (cmd.get("matrix") or ""):
  121. print("FAIL NOT RUN marked PASS", name, file=sys.stderr)
  122. rc = 1
  123. times.add(cmd.get("start_utc") or "")
  124. times.add(cmd.get("end_utc") or "")
  125. if payload.get("git_dirty") and payload.get("git_dirty_state") == "clean":
  126. print("FAIL dirty flag vs dirty_state", file=sys.stderr)
  127. rc = 1
  128. if len([t for t in times if t]) == 1 and len(payload.get("commands") or []) > 3:
  129. print("FAIL all commands share one timestamp (forged window)", file=sys.stderr)
  130. rc = 1
  131. _ = commits, trees, dirties
  132. elif args.mode == "release":
  133. print("FAIL missing run/run-manifest.json", file=sys.stderr)
  134. rc = 1
  135. env = os.environ.copy()
  136. l0 = subprocess.run(
  137. [
  138. sys.executable,
  139. str(host / "tools" / "l0_contract_check.py"),
  140. "--repo-root",
  141. str(root),
  142. "--evidence-root",
  143. str(root / "evidence"),
  144. "--mode",
  145. args.mode,
  146. ],
  147. cwd=str(host),
  148. env=env,
  149. )
  150. if l0.returncode != 0:
  151. print("FAIL l0", args.mode, file=sys.stderr)
  152. rc = 1
  153. oracle = host / "tools" / "protocol_oracle.py"
  154. if oracle.is_file() and golden.is_file():
  155. por = subprocess.run(
  156. [
  157. sys.executable,
  158. str(oracle),
  159. "--golden",
  160. str(golden),
  161. "--min-positive",
  162. "8",
  163. "--min-negative",
  164. "2",
  165. "--min-frames",
  166. "6",
  167. ],
  168. cwd=str(root),
  169. env=env,
  170. )
  171. if por.returncode != 0:
  172. print("FAIL protocol_oracle", file=sys.stderr)
  173. rc = 1
  174. if args.full:
  175. with tempfile.TemporaryDirectory(prefix="hth-verify-hthd-") as td:
  176. out_bin = Path(td) / "hthd"
  177. build = subprocess.run(["go", "build", "-o", str(out_bin), "./cmd/hthd"], cwd=str(host), env=env)
  178. if build.returncode != 0:
  179. print("FAIL go build", file=sys.stderr)
  180. rc = 1
  181. sdk = host / "sdk" / "python"
  182. if (sdk / "pyproject.toml").is_file():
  183. with tempfile.TemporaryDirectory(prefix="hth-verify-wheels-") as td:
  184. wheel_dir = Path(td)
  185. whl = subprocess.run(
  186. [sys.executable, "-m", "pip", "wheel", "--no-deps", "-q", "-w", str(wheel_dir), str(sdk)],
  187. env=env,
  188. )
  189. if whl.returncode != 0:
  190. print("FAIL python wheel --no-deps", file=sys.stderr)
  191. rc = 1
  192. else:
  193. print("python_wheel_ok", list(wheel_dir.glob("hth_sdk-*.whl")))
  194. if rc == 0:
  195. if args.mode == "release":
  196. print("RELEASE_CONTRACT_GATE_PASS")
  197. else:
  198. print("OFFLINE_EVIDENCE_CHECK_PASS")
  199. print("PACKAGE_SELF_VERIFY_PASS")
  200. return rc
  201. if __name__ == "__main__":
  202. raise SystemExit(main())