|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Execute a real generated export and write a provenance receipt for #267.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +from datetime import datetime, timezone |
| 8 | +import hashlib |
| 9 | +import json |
| 10 | +import os |
| 11 | +from pathlib import Path |
| 12 | +import platform |
| 13 | +import tempfile |
| 14 | + |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +from tether.runtime.verify_inference import load_verification_inference |
| 18 | +from tether.smoke import create_smoke_export |
| 19 | + |
| 20 | + |
| 21 | +def _sha256(path: Path) -> str: |
| 22 | + digest = hashlib.sha256() |
| 23 | + with path.open("rb") as stream: |
| 24 | + for chunk in iter(lambda: stream.read(1024 * 1024), b""): |
| 25 | + digest.update(chunk) |
| 26 | + return digest.hexdigest() |
| 27 | + |
| 28 | + |
| 29 | +def _validate_smoke_actions(actions: np.ndarray, noise: np.ndarray) -> None: |
| 30 | + """Fail the workflow unless the generated Constant export truly executed.""" |
| 31 | + expected = np.zeros_like(noise) |
| 32 | + if not np.array_equal(actions, expected): |
| 33 | + raise RuntimeError("smoke export actions do not match its declared constant graph") |
| 34 | + |
| 35 | + |
| 36 | +def build_receipt(output: Path) -> dict[str, object]: |
| 37 | + event_name = os.environ.get("GITHUB_EVENT_NAME") |
| 38 | + ref = os.environ.get("GITHUB_REF") |
| 39 | + if os.environ.get("GITHUB_ACTIONS") == "true": |
| 40 | + if event_name not in {"push", "workflow_dispatch"}: |
| 41 | + raise RuntimeError(f"receipt event is not allowlisted: {event_name!r}") |
| 42 | + if ref != "refs/heads/main": |
| 43 | + raise RuntimeError(f"receipt must execute from protected main, got {ref!r}") |
| 44 | + if os.environ.get("GITHUB_REF_PROTECTED") != "true": |
| 45 | + raise RuntimeError("main is not reported as a protected ref") |
| 46 | + with tempfile.TemporaryDirectory(prefix="tether-verify-artifact-") as temporary: |
| 47 | + export_dir = create_smoke_export(Path(temporary) / "export") |
| 48 | + inference = load_verification_inference(export_dir, device="cpu") |
| 49 | + noise = np.arange(50 * 32, dtype=np.float32).reshape(1, 50, 32) |
| 50 | + image = np.zeros((1, 3, 512, 512), dtype=np.float32) |
| 51 | + mask = np.ones((1,), dtype=np.bool_) |
| 52 | + actions = inference.predict_action_chunk( |
| 53 | + img_base=image, |
| 54 | + img_wrist_l=image, |
| 55 | + img_wrist_r=image, |
| 56 | + mask_base=mask, |
| 57 | + mask_wrist_l=mask, |
| 58 | + mask_wrist_r=mask, |
| 59 | + lang_tokens=np.zeros((1, 16), dtype=np.int64), |
| 60 | + lang_masks=np.ones((1, 16), dtype=np.bool_), |
| 61 | + noise=noise, |
| 62 | + state=np.zeros((1, 32), dtype=np.float32), |
| 63 | + episode_id="receipt", |
| 64 | + ) |
| 65 | + if actions.shape != noise.shape: |
| 66 | + raise RuntimeError(f"unexpected action shape {actions.shape}; expected {noise.shape}") |
| 67 | + _validate_smoke_actions(actions, noise) |
| 68 | + receipt: dict[str, object] = { |
| 69 | + "schema_version": 1, |
| 70 | + "kind": "tether.verify_artifact_execution", |
| 71 | + "source_sha": os.environ.get("GITHUB_SHA", "local"), |
| 72 | + "workflow": os.environ.get("GITHUB_WORKFLOW", "local"), |
| 73 | + "workflow_ref": os.environ.get("GITHUB_WORKFLOW_REF", "local"), |
| 74 | + "repository": os.environ.get("GITHUB_REPOSITORY", "local"), |
| 75 | + "event_name": event_name or "local", |
| 76 | + "ref": ref or "local", |
| 77 | + "run_id": os.environ.get("GITHUB_RUN_ID", "local"), |
| 78 | + "run_attempt": os.environ.get("GITHUB_RUN_ATTEMPT", "local"), |
| 79 | + "generated_at": datetime.now(timezone.utc).isoformat(), |
| 80 | + "model_type": "smolvla", |
| 81 | + "export_kind": "monolithic_onnx", |
| 82 | + "backend": inference.get_stats().get("backend"), |
| 83 | + "model_sha256": _sha256(export_dir / "model.onnx"), |
| 84 | + "config_sha256": _sha256(export_dir / "tether_config.json"), |
| 85 | + "actions_sha256": hashlib.sha256(actions.tobytes()).hexdigest(), |
| 86 | + "python": platform.python_version(), |
| 87 | + "passed": True, |
| 88 | + } |
| 89 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 90 | + output.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n") |
| 91 | + return receipt |
| 92 | + |
| 93 | + |
| 94 | +def main() -> int: |
| 95 | + parser = argparse.ArgumentParser() |
| 96 | + parser.add_argument("--output", type=Path, default=Path("verify-artifact-receipt.json")) |
| 97 | + args = parser.parse_args() |
| 98 | + print(json.dumps(build_receipt(args.output), indent=2, sort_keys=True)) |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + raise SystemExit(main()) |
0 commit comments