#!/usr/bin/env python3 """Build the director-facing algorithm kit. No Go, proto, RTL, or review docs.""" from __future__ import annotations import hashlib import json import os import sys import zipfile from datetime import datetime, timezone from pathlib import Path from package_hth_review import write_zip_file ROOT_NAME = "hth-algorithm-kit" def _sha(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def main() -> int: host = Path(__file__).resolve().parents[1] sdk = host / "sdk" / "python" sys.path.insert(0, str(sdk)) commit = os.environ.get("HTH_GIT_COMMIT", "unknown") short = commit[:7] if commit != "unknown" else "unknown" version = "0.2.0" out_dir = Path(os.environ.get("HTH_PACKAGE_DIR") or os.environ.get("TEMP") or "/tmp") out_dir.mkdir(parents=True, exist_ok=True) wheel = Path(os.environ.get("HTH_SDK_WHEEL") or "") if not wheel.is_file(): dist = list((sdk / "dist").glob("hth_sdk-*.whl")) if (sdk / "dist").is_dir() else [] if not dist: raise SystemExit("set HTH_SDK_WHEEL to a built wheel") wheel = sorted(dist)[-1] epoch = int(os.environ.get("SOURCE_DATE_EPOCH") or 0) items: list[tuple[str, bytes, int]] = [] def add(name: str, data: bytes, mode: int = 0o644) -> None: items.append((f"{ROOT_NAME}/{name}", data, mode)) add("README_10_MINUTES.md", (host / "docs" / "simple" / "README_10_MINUTES.md").read_bytes()) add("ALGORITHM_API.md", (host / "docs" / "simple" / "ALGORITHM_API.md").read_bytes()) add(wheel.name, wheel.read_bytes()) add("quickstart.py", (sdk / "examples" / "quickstart.py").read_bytes()) add("policy_template.py", (sdk / "examples" / "policy_template.py").read_bytes()) add("closed_loop_demo.py", (sdk / "examples" / "closed_loop_demo.py").read_bytes()) add("offline_replay_demo.py", (sdk / "examples" / "offline_replay_demo.py").read_bytes()) add("algorithm_quickstart.ipynb", (sdk / "examples" / "algorithm_quickstart.ipynb").read_bytes()) add( "scenarios/README.md", b"SIMULATION ONLY\nNOT IDENTIFIED FROM REAL HARDWARE\nNOT A PHYSICAL VALIDATION MODEL\n", ) from hth_sdk.replay import write_sample_replay replay = Path(os.environ.get("TMPDIR") or "/tmp") / "hth-sample-replay.jsonl" write_sample_replay(replay) add("sample_replay/sample.jsonl", replay.read_bytes()) manifest = { "name": ROOT_NAME, "version": version, "git_commit": commit, "created_utc": datetime.fromtimestamp(epoch or 0, timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") if epoch else datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), "files": [n.split("/", 1)[1] for n, _, _ in items], "note": "Director kit. Mock/Replay only. Hardware validation NOT RUN.", } add("MANIFEST.json", (json.dumps(manifest, indent=2, sort_keys=True) + "\n").encode()) items.sort(key=lambda x: x[0]) sums = [] for name, data, _ in items: sums.append(f"{_sha(data)} {name.split('/', 1)[1]}") items.append((f"{ROOT_NAME}/SHA256SUMS", ("\n".join(sums) + "\n").encode(), 0o644)) items.sort(key=lambda x: x[0]) zip_path = out_dir / f"hth-algorithm-kit-{version}-{short}.zip" with zipfile.ZipFile(zip_path, "w") as zf: for name, data, mode in items: write_zip_file(zf, name, data, mode) digest = _sha(zip_path.read_bytes()) (out_dir / (zip_path.name + ".sha256")).write_text(f"{digest} {zip_path.name}\n", encoding="utf-8") att = { "filename": zip_path.name, "bytes": zip_path.stat().st_size, "sha256": digest, "git_commit": commit, "packager": "host/tools/package_hth_algorithm_kit.py", "kind": "algorithm-kit", } (out_dir / (zip_path.name + ".attestation.json")).write_text(json.dumps(att, indent=2) + "\n", encoding="utf-8") print(zip_path) return 0 if __name__ == "__main__": raise SystemExit(main())