#!/usr/bin/env python3 """Run one conditional pytest gate and reject empty or silently skipped evidence.""" from __future__ import annotations import argparse import importlib.util import ipaddress import json import os import platform import shutil import socket import subprocess import sys from datetime import datetime, timezone from pathlib import Path from xml.etree import ElementTree try: from scripts.conditional_gate_metadata import git_metadata except ModuleNotFoundError: # Direct execution adds scripts/, not the repository root. from conditional_gate_metadata import git_metadata GATES = { "model-download-runtime": "tests/conditional/test_model_download_runtime.py", "external-evaluation": "tests/conditional/test_external_evaluation_runtime.py", "network-services": "tests/conditional/test_network_services.py", "operator-ui": "tests", "cuda-runtime": "tests/conditional/test_cuda_runtime.py", "bitsandbytes-runtime": "tests/conditional/test_cuda_runtime.py", "llama-tp-local-gpu": "tests/conditional/test_distributed_llama_tp_cuda.py", "module-placement-gpu": "tests/conditional/test_gpu_qualification.py", "jetson-runtime": "tests/conditional/test_jetson_runtime.py", "mps-runtime": "tests/conditional/test_mps_runtime.py", "mlx-runtime": "tests/conditional/test_mlx_runtime.py", "remote-execution": "tests/conditional/test_remote_runtime.py", } JETSON_RELEASE = Path("/etc/nv_tegra_release") def _is_private_rfc1918_ipv4(address: object) -> bool: if not isinstance(address, str): return False try: candidate = ipaddress.ip_address(address) except ValueError: return False return candidate.version == 4 and any( candidate in network for network in ( ipaddress.ip_network("10.0.0.0/8"), ipaddress.ip_network("172.16.0.0/12"), ipaddress.ip_network("192.168.0.0/16"), ) ) def missing_prerequisites(gate: str) -> list[str]: missing: list[str] = [] if gate in { "cuda-runtime", "bitsandbytes-runtime", "llama-tp-local-gpu", "jetson-runtime", "mps-runtime", }: import torch if gate.startswith(("cuda", "bitsandbytes", "llama-tp", "jetson")): if not torch.cuda.is_available(): missing.append("a CUDA-capable PyTorch runtime") elif not (hasattr(torch.backends, "mps") and torch.backends.mps.is_available()): missing.append("an available Apple MPS backend") if gate == "bitsandbytes-runtime" and importlib.util.find_spec("bitsandbytes") is None: missing.append("bitsandbytes") if gate == "llama-tp-local-gpu": import torch if torch.cuda.device_count() != 2: missing.append("exactly two visible CUDA devices") if not torch.distributed.is_gloo_available(): missing.append("the Gloo distributed backend") if not torch.distributed.is_nccl_available(): missing.append("the NCCL distributed backend") if torch.cuda.is_available() and torch.cuda.device_count() == 2: identities = { ( torch.cuda.get_device_properties(index).name, torch.cuda.get_device_capability(index), torch.cuda.get_device_properties(index).total_memory, ) for index in range(torch.cuda.device_count()) } if len(identities) != 1: missing.append("a homogeneous CUDA device set") native_bf16 = [] for index in range(torch.cuda.device_count()): with torch.cuda.device(index): native_bf16.append( torch.cuda.is_bf16_supported(including_emulation=False), ) if not all(native_bf16): missing.append("native BF16 support on every CUDA device") unshare = shutil.which("unshare") if unshare is None or not Path("/proc/self/mountinfo").is_file(): missing.append("Linux user and mount namespaces") else: try: namespace = subprocess.run( [ unshare, "--user", "--map-root-user", "--mount", "--fork", "/bin/true", ], capture_output=True, timeout=10, check=False, ) except (OSError, subprocess.TimeoutExpired): namespace = None if namespace is None or namespace.returncode != 0: missing.append("an available unprivileged read-only mount namespace") import psutil # type: ignore[import-untyped] if not any( record.family == socket.AF_INET and _is_private_rfc1918_ipv4(record.address) for records in psutil.net_if_addrs().values() for record in records ): missing.append("a private numeric runner interface") for variable in ( "OBLITERATUS_TP_IMAGE_DIGEST", "OBLITERATUS_TP_TORCH_WHEEL_SHA256", "OBLITERATUS_TP_PACKAGE_DIGEST", "OBLITERATUS_TP_RUNTIME_DIGEST", "OBLITERATUS_TP_API_DIGEST", ): if not os.environ.get(variable): missing.append(variable) if gate == "jetson-runtime": if platform.machine().lower() not in {"aarch64", "arm64"}: missing.append("an ARM64 host") if not JETSON_RELEASE.is_file(): missing.append("a Jetson L4T runtime") if gate == "mlx-runtime": for module in ("mlx", "mlx_lm"): if importlib.util.find_spec(module) is None: missing.append(module) if gate == "remote-execution": for variable in ( "OBLITERATUS_REMOTE_HOST", "OBLITERATUS_REMOTE_USER", "OBLITERATUS_REMOTE_KEY", "OBLITERATUS_REMOTE_KNOWN_HOSTS", ): if not os.environ.get(variable): missing.append(variable) return missing def counts(junit_path: Path) -> dict[str, int]: root = ElementTree.parse(junit_path).getroot() suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) return { key: sum(int(suite.attrib.get(key, "0")) for suite in suites) for key in ("tests", "failures", "errors", "skipped") } def write_report(path: Path, gate: str, status: str, **extra: object) -> None: path.parent.mkdir(parents=True, exist_ok=True) payload = { "schema_version": 1, "gate": gate, "status": status, "generated_at": datetime.now(timezone.utc).isoformat(), **git_metadata(), **extra, } path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n") def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("gate", choices=sorted(GATES)) parser.add_argument("--evidence-dir", type=Path, default=Path("conditional-evidence")) parser.add_argument("--allow-missing", action="store_true") args = parser.parse_args() report = args.evidence_dir / f"{args.gate}.json" junit = args.evidence_dir / f"{args.gate}.xml" if args.gate == "llama-tp-local-gpu": artifact = Path(__file__).resolve().parents[1] / "obliteratus.egg-info" if artifact.exists() or artifact.is_symlink(): message = ( f"Stale build metadata at {artifact}: source-root egg-info can shadow " "installed distribution metadata in the qualification processes. " "Remove this build artifact after the non-editable install, then rerun " "llama-tp-local-gpu." ) write_report(report, args.gate, "not_run", reason=message) print(message, file=sys.stderr) return 2 missing = missing_prerequisites(args.gate) if missing: message = "Missing prerequisites: " + ", ".join(missing) write_report(report, args.gate, "not_run", reason=message) print(message, file=sys.stderr) return 0 if args.allow_missing else 2 command = [ sys.executable, "-m", "pytest", GATES[args.gate], "--no-cov", "-q", f"--junitxml={junit}", ] if args.gate == "cuda-runtime": command.append("tests/conditional/test_phase_admission_gpu.py") command.append("tests/conditional/test_operator_ui_cuda.py") command.append("tests/conditional/test_observation_cuda.py") command.append("tests/conditional/test_backend_qualification_cuda.py") command.append("tests/conditional/test_precision_reasoning_cuda.py") if args.gate == "operator-ui": command.extend(["-m", "operator_ui"]) if args.gate in {"model-download-runtime", "cuda-runtime", "bitsandbytes-runtime"}: command.append("tests/conditional/test_recovery_runtime.py") if args.gate != "model-download-runtime": command.append("tests/conditional/test_verify_checkpoint_cuda.py") marker = ("network and download and not gpu" if args.gate == "model-download-runtime" else "gpu and not network and not download") command.extend(["-m", marker]) result = subprocess.run(command, check=False) if not junit.is_file(): write_report(report, args.gate, "failed", exit_code=result.returncode, reason="no JUnit") return result.returncode or 1 result_counts = counts(junit) passed = ( result.returncode == 0 and result_counts["tests"] > 0 and result_counts["failures"] == 0 and result_counts["errors"] == 0 and result_counts["skipped"] == 0 ) write_report( report, args.gate, "passed" if passed else "failed", exit_code=result.returncode, counts=result_counts, ) if not passed: print(f"conditional gate did not produce unskipped green evidence: {result_counts}") return 0 if passed else 1 if __name__ == "__main__": raise SystemExit(main())