| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #!/usr/bin/env python3
- """Unzip-directory self-verify for an hth-platform review package.
- --mode offline: SHA, MANIFEST, permissions, L0 offline (buf missing = PARTIAL).
- --mode release: same plus buf lint+breaking required. Missing tools/logs FAIL.
- """
- from __future__ import annotations
- import argparse
- import hashlib
- import json
- import os
- import subprocess
- import sys
- import tempfile
- from pathlib import Path
- REQUIRED_REVIEWS = (
- "01-scope-architecture.md",
- "02-protocol-event-truth.md",
- "03-serial-safety-lifecycle.md",
- "04-api-replay-deployment.md",
- "05-evidence-release.md",
- )
- def main() -> int:
- ap = argparse.ArgumentParser()
- ap.add_argument("root", nargs="?", default=".")
- ap.add_argument("--mode", choices=("offline", "release"), default="offline")
- ap.add_argument("--full", action="store_true", help="also go build and pip wheel --no-deps")
- args = ap.parse_args()
- root = Path(args.root).resolve()
- sums = root / "SHA256SUMS"
- manifest = root / "MANIFEST"
- if not sums.is_file() or not manifest.is_file():
- print("FAIL missing SHA256SUMS or MANIFEST", file=sys.stderr)
- return 1
- rc = 0
- man_set: set[str] = set()
- for line in manifest.read_text(encoding="utf-8").splitlines():
- if not line.strip():
- continue
- if line.startswith("/") or ".." in Path(line).parts:
- print("FAIL path", line, file=sys.stderr)
- rc = 1
- man_set.add(line.strip())
- sum_set: set[str] = set()
- for line in sums.read_text(encoding="utf-8").splitlines():
- if not line.strip():
- continue
- digest, name = line.split(None, 1)
- name = name.strip()
- sum_set.add(name)
- path = root / name
- if not path.is_file():
- print("FAIL missing", name, file=sys.stderr)
- rc = 1
- continue
- got = hashlib.sha256(path.read_bytes()).hexdigest()
- if got != digest:
- print("FAIL sha256", name, file=sys.stderr)
- rc = 1
- mode = path.stat().st_mode & 0o777
- if mode == 0o777:
- print("FAIL 0777", name, file=sys.stderr)
- rc = 1
- if man_set != sum_set:
- print("FAIL MANIFEST and SHA256SUMS file sets differ", file=sys.stderr)
- print("only_manifest", sorted(man_set - sum_set)[:20], file=sys.stderr)
- print("only_sums", sorted(sum_set - man_set)[:20], file=sys.stderr)
- rc = 1
- golden = root / "evidence" / "golden-vectors.json"
- if not golden.is_file():
- print("FAIL missing evidence/golden-vectors.json", file=sys.stderr)
- rc = 1
- host = root / "host"
- if not (host / "go.mod").is_file():
- print("FAIL host/go.mod missing", file=sys.stderr)
- return 1
- for name in REQUIRED_REVIEWS:
- if not (host / "reviews" / name).is_file():
- print("FAIL missing review", name, file=sys.stderr)
- rc = 1
- run_dir = root / "run"
- man_json = run_dir / "run-manifest.json"
- if man_json.is_file():
- payload = json.loads(man_json.read_text(encoding="utf-8"))
- commits = {payload.get("git_commit")}
- trees = {payload.get("git_tree")}
- dirties = {payload.get("git_dirty_state")}
- times: set[str] = set()
- for cmd in payload.get("commands") or []:
- log = cmd.get("log") or ""
- if log:
- lp = run_dir / Path(log).name
- if not lp.is_file():
- print("FAIL missing run log", log, file=sys.stderr)
- rc = 1
- elif cmd.get("log_sha256"):
- got = hashlib.sha256(lp.read_bytes()).hexdigest()
- if got != cmd["log_sha256"]:
- print("FAIL log sha", log, file=sys.stderr)
- rc = 1
- elif args.mode == "release":
- print("FAIL missing log_sha256", cmd.get("name"), file=sys.stderr)
- rc = 1
- st = (cmd.get("status") or "").upper()
- name = cmd.get("name") or ""
- if st == "PASS" and (not log or not cmd.get("log_sha256")):
- print("FAIL PASS without raw log/sha", name, file=sys.stderr)
- rc = 1
- if "NOT RUN" in st and "PASS" in (cmd.get("matrix") or ""):
- print("FAIL NOT RUN marked PASS", name, file=sys.stderr)
- rc = 1
- times.add(cmd.get("start_utc") or "")
- times.add(cmd.get("end_utc") or "")
- if payload.get("git_dirty") and payload.get("git_dirty_state") == "clean":
- print("FAIL dirty flag vs dirty_state", file=sys.stderr)
- rc = 1
- if len([t for t in times if t]) == 1 and len(payload.get("commands") or []) > 3:
- print("FAIL all commands share one timestamp (forged window)", file=sys.stderr)
- rc = 1
- _ = commits, trees, dirties
- elif args.mode == "release":
- print("FAIL missing run/run-manifest.json", file=sys.stderr)
- rc = 1
- env = os.environ.copy()
- l0 = subprocess.run(
- [
- sys.executable,
- str(host / "tools" / "l0_contract_check.py"),
- "--repo-root",
- str(root),
- "--evidence-root",
- str(root / "evidence"),
- "--mode",
- args.mode,
- ],
- cwd=str(host),
- env=env,
- )
- if l0.returncode != 0:
- print("FAIL l0", args.mode, file=sys.stderr)
- rc = 1
- oracle = host / "tools" / "protocol_oracle.py"
- if oracle.is_file() and golden.is_file():
- por = subprocess.run(
- [
- sys.executable,
- str(oracle),
- "--golden",
- str(golden),
- "--min-positive",
- "8",
- "--min-negative",
- "2",
- "--min-frames",
- "6",
- ],
- cwd=str(root),
- env=env,
- )
- if por.returncode != 0:
- print("FAIL protocol_oracle", file=sys.stderr)
- rc = 1
- if args.full:
- with tempfile.TemporaryDirectory(prefix="hth-verify-hthd-") as td:
- out_bin = Path(td) / "hthd"
- build = subprocess.run(["go", "build", "-o", str(out_bin), "./cmd/hthd"], cwd=str(host), env=env)
- if build.returncode != 0:
- print("FAIL go build", file=sys.stderr)
- rc = 1
- sdk = host / "sdk" / "python"
- if (sdk / "pyproject.toml").is_file():
- with tempfile.TemporaryDirectory(prefix="hth-verify-wheels-") as td:
- wheel_dir = Path(td)
- whl = subprocess.run(
- [sys.executable, "-m", "pip", "wheel", "--no-deps", "-q", "-w", str(wheel_dir), str(sdk)],
- env=env,
- )
- if whl.returncode != 0:
- print("FAIL python wheel --no-deps", file=sys.stderr)
- rc = 1
- else:
- print("python_wheel_ok", list(wheel_dir.glob("hth_sdk-*.whl")))
- if rc == 0:
- if args.mode == "release":
- print("RELEASE_CONTRACT_GATE_PASS")
- else:
- print("OFFLINE_EVIDENCE_CHECK_PASS")
- print("PACKAGE_SELF_VERIFY_PASS")
- return rc
- if __name__ == "__main__":
- raise SystemExit(main())
|