diff --git a/docs/product/release-readiness.md b/docs/product/release-readiness.md index a2f8186cc3..0adc9b4db3 100644 --- a/docs/product/release-readiness.md +++ b/docs/product/release-readiness.md @@ -156,6 +156,14 @@ passes, PyPI; each stable promotion still needs one package version name. The version source is `loopx.__version__`, mirrored by `pyproject.toml`; the expected public tag is `vX.Y.Z` for that version. +Exact-source qualification reads both version declarations from the selected +checkout without executing its package. Missing, malformed or conflicting build +metadata refuses qualification even when the commit is clean and all supplied +receipts agree. Release artifact tooling loads the shared static reader without +bootstrapping the candidate package, including when the tool and candidate use +the same checkout. Invalid metadata retains the artifact CLI's normal error +output; inspection does not execute package initialization. + Before moving `stable`, maintainers should: - before freezing the candidate, fetch `stable` from the verified target diff --git a/examples/release-artifacts-smoke.py b/examples/release-artifacts-smoke.py index 937fe4a11d..0cda949886 100644 --- a/examples/release-artifacts-smoke.py +++ b/examples/release-artifacts-smoke.py @@ -121,6 +121,9 @@ def commit(message: str) -> str: shutil.copyfile(ROOT / "pyproject.toml", root / "pyproject.toml") (root / "loopx").mkdir() shutil.copyfile(ROOT / "loopx/__init__.py", root / "loopx/__init__.py") + shutil.copyfile(ROOT / "loopx/release_metadata.py", root / "loopx/release_metadata.py") + with (root / "loopx/__init__.py").open("a") as initializer: + initializer.write('\nraise RuntimeError("artifact inspection executed the candidate")\n') artifacts = [*packages.iterdir(), root / "dist/SHA256SUMS"] before = {path.name: digest(path) for path in artifacts} env = {**os.environ, "RELEASE_TAG": identity.tag, "GITHUB_SHA": correction, diff --git a/loopx/control_plane/testing/release_commit_qualification.py b/loopx/control_plane/testing/release_commit_qualification.py index d1af3fa1d6..6096f91be6 100644 --- a/loopx/control_plane/testing/release_commit_qualification.py +++ b/loopx/control_plane/testing/release_commit_qualification.py @@ -1,12 +1,12 @@ from __future__ import annotations -import ast import re import subprocess from datetime import datetime from pathlib import Path from typing import Any, Callable, Mapping +from ...release_metadata import checkout_release_metadata from ..content_digest import ENVELOPED_SHA256_PATTERN from ..runtime.public_safety import public_safe_compact_text from .actual_default_model_behavior_portfolio import ( @@ -512,25 +512,9 @@ def _git(repo_root: Path, *args: str) -> str: def _checkout_package_version(repo_root: Path) -> str: try: - init_module = ast.parse( - (repo_root / "loopx" / "__init__.py").read_text(encoding="utf-8") - ) - except (OSError, SyntaxError) as exc: + module_version = checkout_release_metadata(repo_root)["version"] + except (OSError, SyntaxError, ValueError) as exc: raise ValueError("release package version is unavailable from the checkout") from exc - - module_version = None - for statement in init_module.body: - if not isinstance(statement, ast.Assign) or len(statement.targets) != 1: - continue - target = statement.targets[0] - if ( - isinstance(target, ast.Name) - and target.id == "__version__" - and isinstance(statement.value, ast.Constant) - and isinstance(statement.value.value, str) - ): - module_version = statement.value.value - break if not isinstance(module_version, str) or not _VERSION_RE.fullmatch(module_version): raise ValueError("release package version is invalid in the checkout") return module_version diff --git a/loopx/release_metadata.py b/loopx/release_metadata.py new file mode 100644 index 0000000000..1f179b5cfc --- /dev/null +++ b/loopx/release_metadata.py @@ -0,0 +1,38 @@ +"""Static source identity without bootstrapping the candidate package.""" + +from __future__ import annotations + +import ast +from pathlib import Path +import tomllib + + +def checkout_release_metadata(project_root: Path) -> dict[str, str]: + """Read build identity without importing or executing the candidate package.""" + pyproject_path = project_root / "pyproject.toml" + payload = tomllib.loads(pyproject_path.read_text(encoding="utf-8")) + project = payload.get("project") + if not isinstance(project, dict): + raise ValueError(f"missing [project] table in {pyproject_path}") + name = project.get("name") + version = project.get("version") + if not isinstance(name, str) or not name: + raise ValueError("project.name must be a non-empty string") + if not isinstance(version, str) or not version: + raise ValueError("project.version must be a non-empty string") + module_path = project_root / "loopx" / "__init__.py" + tree = ast.parse(module_path.read_text(encoding="utf-8"), filename=str(module_path)) + for statement in tree.body: + if not isinstance(statement, (ast.Assign, ast.AnnAssign)): + continue + targets = statement.targets if isinstance(statement, ast.Assign) else [statement.target] + if not any(isinstance(target, ast.Name) and target.id == "__version__" for target in targets): + continue + value = statement.value + if isinstance(value, ast.Constant) and isinstance(value.value, str): + if value.value != version: + raise ValueError( + f"version mismatch: pyproject.toml={version!r}, loopx.__version__={value.value!r}" + ) + return {"name": name, "version": version} + raise ValueError(f"missing literal __version__ in {module_path}") diff --git a/scripts/release_artifacts.py b/scripts/release_artifacts.py index c6b6712e69..cb058c06a8 100644 --- a/scripts/release_artifacts.py +++ b/scripts/release_artifacts.py @@ -4,18 +4,17 @@ from __future__ import annotations import argparse -import ast import copy import gzip import hashlib import io import json import re +import runpy import subprocess import sys import tarfile import tempfile -import tomllib from dataclasses import dataclass from pathlib import Path @@ -38,38 +37,14 @@ def distribution_prefix(self) -> str: return self.name.replace("-", "_") -def _module_version(path: Path) -> str: - tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path)) - for statement in tree.body: - if not isinstance(statement, (ast.Assign, ast.AnnAssign)): - continue - targets = statement.targets if isinstance(statement, ast.Assign) else [statement.target] - if not any(isinstance(target, ast.Name) and target.id == "__version__" for target in targets): - continue - value = statement.value - if isinstance(value, ast.Constant) and isinstance(value.value, str): - return value.value - raise ReleaseArtifactError(f"missing literal __version__ in {path}") - - def load_identity(project_root: Path) -> ReleaseIdentity: - pyproject_path = project_root / "pyproject.toml" - payload = tomllib.loads(pyproject_path.read_text(encoding="utf-8")) - project = payload.get("project") - if not isinstance(project, dict): - raise ReleaseArtifactError(f"missing [project] table in {pyproject_path}") - name = project.get("name") - version = project.get("version") - if not isinstance(name, str) or not name: - raise ReleaseArtifactError("project.name must be a non-empty string") - if not isinstance(version, str) or not version: - raise ReleaseArtifactError("project.version must be a non-empty string") - module_version = _module_version(project_root / "loopx" / "__init__.py") - if module_version != version: - raise ReleaseArtifactError( - f"version mismatch: pyproject.toml={version!r}, loopx.__version__={module_version!r}" - ) - return ReleaseIdentity(name=name, version=version) + try: + # The tool can inspect its own checkout: loading its package would run + # the very initializer whose version must remain inert. + reader = runpy.run_path(str(Path(__file__).resolve().parents[1] / "loopx/release_metadata.py")) + return ReleaseIdentity(**reader["checkout_release_metadata"](project_root)) + except (OSError, SyntaxError, ValueError) as exc: + raise ReleaseArtifactError(str(exc)) from exc def distribution_files(dist_dir: Path, identity: ReleaseIdentity) -> tuple[Path, ...]: diff --git a/tests/control_plane/test_release_commit_qualification.py b/tests/control_plane/test_release_commit_qualification.py index a89bfaf140..8476486314 100644 --- a/tests/control_plane/test_release_commit_qualification.py +++ b/tests/control_plane/test_release_commit_qualification.py @@ -228,9 +228,8 @@ def _git(repo: Path, *args: str) -> str: return completed.stdout.strip() -def test_release_qualification_cli_matches_clean_git_source_and_is_read_only( - tmp_path: Path, -) -> None: +@pytest.fixture +def release_source_repo(tmp_path: Path) -> Path: source_repo = tmp_path / "source" (source_repo / "loopx").mkdir(parents=True) _git(source_repo, "init") @@ -247,6 +246,21 @@ def test_release_qualification_cli_matches_clean_git_source_and_is_read_only( ) _git(source_repo, "add", "source.txt", "pyproject.toml", "loopx/__init__.py") _git(source_repo, "commit", "-m", "release source") + return source_repo + + +@pytest.mark.parametrize("module", [ + '__version__ = "9.8.7"\n', + '__version__: str = "9.8.7"\n', + '__version__ = "9.8.7"\nraise RuntimeError("candidate must not execute")\n', +]) +def test_release_qualification_cli_matches_clean_git_source_and_is_read_only( + tmp_path: Path, release_source_repo: Path, module: str, +) -> None: + source_repo = release_source_repo + (source_repo / "loopx/__init__.py").write_text(module, encoding="utf-8") + _git(source_repo, "add", "loopx/__init__.py") + _git(source_repo, "commit", "--allow-empty", "-m", "literal source identity") source = collect_release_source_identity(source_repo) manifest = _manifest(source=source) manifest_path = tmp_path / "qualification.json" @@ -305,6 +319,44 @@ def test_release_qualification_cli_matches_clean_git_source_and_is_read_only( assert payload["source_mismatches"] == ["observed_source:git_dirty"] +@pytest.mark.parametrize("metadata", [ + '[project]\nname = "loopx"\nversion = "9.8.6"\n', + '[project]\nname = "loopx"\n', + '[project]\nname = "loopx"\nversion = true\n', + '[project\n', +]) +def test_release_qualification_refuses_invalid_build_version( + tmp_path: Path, release_source_repo: Path, metadata: str, +) -> None: + """A clean commit and matching receipts cannot excuse invalid wheel metadata.""" + repo = release_source_repo + (repo / "pyproject.toml").write_text(metadata, encoding="utf-8") + _git(repo, "add", "pyproject.toml") + _git(repo, "commit", "-m", "inconsistent build metadata") + source = { + **_source(commit=_git(repo, "rev-parse", "HEAD"), tree=_git(repo, "rev-parse", "HEAD^{tree}")), + "package_version": "9.8.7", "version_tag": "v9.8.7", + } + manifest = tmp_path / "qualification.json" + manifest.write_text(json.dumps(_manifest(source=source)), encoding="utf-8") + before = {path.name: path.read_bytes() for path in (repo / "pyproject.toml", manifest)} + result = subprocess.run([ + sys.executable, "-m", "loopx.cli", "--format", "json", "canary", + "release-qualification", "--manifest-json", str(manifest), "--repo-root", str(repo), + ], cwd=REPO_ROOT, capture_output=True, text=True, check=False) + + assert result.returncode == 1, result.stdout + payload = json.loads(result.stdout) + assert payload["ready_for_release"] is False + assert payload["decision"] == "invalid_input" + assert payload["error"] == "release package version is unavailable from the checkout" + assert str(tmp_path) not in result.stdout + assert payload["read_boundary"]["model_api_invoked"] is False + assert payload["read_boundary"]["release_mutation_invoked"] is False + assert _git(repo, "status", "--porcelain") == "" + assert before == {path.name: path.read_bytes() for path in (repo / "pyproject.toml", manifest)} + + def test_release_qualification_cli_redacts_manifest_path_errors(tmp_path: Path) -> None: missing = tmp_path / "private-release-name.json" result = subprocess.run( diff --git a/tests/test_release_artifact_publication.py b/tests/test_release_artifact_publication.py index 621f0c1c04..31e68d1411 100644 --- a/tests/test_release_artifact_publication.py +++ b/tests/test_release_artifact_publication.py @@ -2,6 +2,7 @@ import importlib.util import json +import shutil import subprocess import sys from pathlib import Path @@ -18,6 +19,38 @@ SPEC.loader.exec_module(owner) +@pytest.mark.parametrize("isolated", [False, True]) +@pytest.mark.parametrize("metadata", ["valid", "mismatch", "malformed"]) +def test_colocated_artifact_cli_never_initializes_the_candidate(tmp_path, isolated, metadata): + root = Path(__file__).resolve().parents[1] + (tmp_path / "loopx").mkdir() + shutil.copyfile(root / "loopx/release_metadata.py", tmp_path / "loopx/release_metadata.py") + (tmp_path / "scripts").mkdir() + script = tmp_path / "scripts/release_artifacts.py" + shutil.copyfile(root / "scripts/release_artifacts.py", script) + sentinel = tmp_path / "loopx/initialized" + (tmp_path / "loopx/__init__.py").write_text( + '__version__: str = "1.2.3"\nfrom pathlib import Path\n' + 'Path(__file__).with_name("initialized").write_text("executed")\n' + 'raise RuntimeError("candidate package executed")\n', encoding="utf-8") + version = "9.9.9" if metadata == "mismatch" else "1.2.3" + project = f'[project]\nname = "loopx"\nversion = "{version}"\n' + if metadata == "malformed": + project = "[project\n" + (tmp_path / "pyproject.toml").write_text(project, encoding="utf-8") + result = subprocess.run([sys.executable, *(["-I", "-S"] if isolated else []), + str(script), "expected-tag"], cwd=tmp_path, + capture_output=True, text=True, timeout=30) + assert not sentinel.exists(), (result.stdout, result.stderr) + assert result.returncode == (0 if metadata == "valid" else 2), result.stderr + if metadata == "valid": + assert result.stdout.strip() == "v1.2.3" + else: + assert result.stderr and "Traceback" not in result.stderr + if metadata == "mismatch": + assert "version mismatch" in result.stderr + + @pytest.fixture def bundle(tmp_path): identity = owner.ReleaseIdentity("loopx", "1.2.3")