"""Run or independently collect exact-revision release tests with bound evidence.""" from __future__ import annotations import argparse import json import os from pathlib import Path import re import subprocess import sys import tempfile from scripts.release_checkout_identity import verify_checkout from scripts.release_bytecode_policy import bytecode_identity from scripts.release_test_environment import release_extras, tomllib from scripts.release_source_identity import checkout_only_sources, digest, source_identity from scripts.shard_pr_tests import load_durations, shard_tests CPU_MARKER = ( "not slow and not gpu and not mps and not mlx and not network and not download " "and not remote and not operator_ui" ) SHARDS = {"core": 6, "gloo": 8} def target_metadata(root: Path, candidate: str, revision: str, target: str) -> dict: for value in (candidate, revision): if not re.fullmatch(r"[0-9a-f]{40}", value): raise ValueError("candidate and target must be exact commit SHAs") actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root, text=True).strip() if actual != revision or target == "head" and actual != candidate: raise ValueError("checked-out target does not match requested revision") return { "schema_version": 1, "candidate_sha": candidate, "target_sha": actual, "target": target, "python_version": f"{sys.version_info.major}.{sys.version_info.minor}", "extras": release_extras(root / "pyproject.toml"), "checkout_tree_digest": verify_checkout(root, revision), **source_identity(root), **bytecode_identity(), } def coverage_config(root: Path, output: Path, revision: str, source_roots: list[str]) -> Path: text = (root / "pyproject.toml").read_text(encoding="utf-8") config = tomllib.loads(text).get("tool", {}).get("coverage", {}).get("run", {}) if "context" in config: raise ValueError("target coverage context requires explicit release identity composition") if config.keys() & {"include", "source", "source_dirs", "source_pkgs"}: raise ValueError("target coverage source selection requires explicit release identity composition") header = "[tool.coverage.run]" include = [str(Path(origin) / "**" / "*.py") for origin in source_roots] include += [str(path) for path in checkout_only_sources(root)] bindings = f'context = "release:{revision}"\ninclude = {json.dumps(include)}\n' text = text.replace(header, header + "\n" + bindings, 1) if header in text else text + "\n" + header + "\n" + bindings path = output / "coverage-config.toml" path.write_text(text, encoding="utf-8") return path def pytest_command(root: Path, output: Path, *, scratch: Path, collect: bool, kind: str | None) -> list[str]: marker = CPU_MARKER if kind is None else CPU_MARKER + (" and gloo" if kind == "gloo" else " and not gloo") command = [ sys.executable, "-m", "pytest", "-p", "release_partition_plugin", "--release-nodeids", str(output / "collection.json"), "-o", "addopts=--strict-config --strict-markers", "-m", marker, "--basetemp", str(scratch / "pytest-tmp"), ] if collect: # Pytest's per-file counts keep large parameter IDs out of the runner log. # The release plugin records the full IDs in collection.json. command += ["--collect-only", "-qq", "-p", "no:cacheprovider"] else: command += ["--cov", "--cov-branch", "--cov-fail-under=0", "--cov-report=", "--junitxml", str(output / "junit.xml")] return command def execute(args) -> int: root, output = args.root.resolve(), args.output.resolve() metadata = target_metadata(root, args.candidate_sha, args.target_sha, args.target) output.mkdir(parents=True, exist_ok=True) if any(output.glob("*.json")) or any(output.glob("coverage.data*")): raise ValueError("evidence destination contains stale outputs") collect = args.command == "collect" selected = [] if not collect: if type(args.shard) is not int or args.shard not in range(SHARDS[args.kind]): raise ValueError("invalid release shard index") files = sorted(path.relative_to(root).as_posix() for path in (root / "tests").rglob("test_*.py")) default, durations = load_durations(args.durations) selected = shard_tests(files, default, durations, root=root, shards=SHARDS[args.kind], kind=args.kind)[args.shard] metadata.update(kind=args.kind, shard=args.shard, shard_count=SHARDS[args.kind]) env = {**os.environ, "COVERAGE_FILE": str(output / "coverage.data"), "PYTHONDONTWRITEBYTECODE": "1"} env["PYTHONPATH"] = os.pathsep.join([str(Path(__file__).resolve().parent), str(root), env.get("PYTHONPATH", "")]) if not collect and not selected: (output / "collection.json").write_text(json.dumps({"schema_version": 1, "nodes": {"core": [], "gloo": []}, "collection_skips": []})) (output / "junit.xml").write_text("\n") code = 5 else: # Scratch model/checkpoint/RSS trees are never children of the uploaded evidence. with tempfile.TemporaryDirectory(prefix="release-pytest-", dir=os.environ.get("RUNNER_TEMP")) as scratch: if Path(scratch).resolve().is_relative_to(output): raise ValueError("pytest scratch directory must be outside evidence") command = pytest_command(root, output, scratch=Path(scratch), collect=collect, kind=None if collect else args.kind) if not collect: config = coverage_config(root, output, args.target_sha, metadata["source_roots"]) command += ["--cov-config", str(config), *selected] with (output / "pytest.log").open("w", encoding="utf-8") as log: with subprocess.Popen(command, cwd=root, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as process: for line in process.stdout: log.write(line) print(line, end="", flush=True) code = process.wait() current = target_metadata(root, args.candidate_sha, args.target_sha, args.target) if any(metadata[key] != value for key, value in current.items()): raise ValueError("target identity changed during test execution") metadata["exit_code"] = code metadata["files"] = { path.name: digest(path) for path in sorted(output.iterdir()) if path.name in {"collection.json", "junit.xml"} or path.name.startswith("coverage.data") } (output / "status.json").write_text(json.dumps(metadata, indent=2, sort_keys=True), encoding="utf-8") # The validator separately proves that exit 5 really was empty. return 0 if code in {0, 5} else code def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("command", choices=("run", "collect")) parser.add_argument("--root", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--candidate-sha", required=True) parser.add_argument("--target-sha", required=True) parser.add_argument("--target", choices=("head", "base"), required=True) parser.add_argument("--kind", choices=tuple(SHARDS)) parser.add_argument("--shard", type=int) parser.add_argument("--durations", type=Path) args = parser.parse_args() if args.command == "run" and (args.kind is None or args.shard is None or args.durations is None): parser.error("run requires --kind, --shard and --durations") try: return execute(args) except (OSError, ValueError, subprocess.SubprocessError) as exc: print(f"Release partition execution failed: {exc}", file=sys.stderr) return 1 if __name__ == "__main__": raise SystemExit(main())