release_gate.py
python
sha256:982b4ce08b140ea3fa9057705ff1806a702c9203689c22b797a19f9db678fe5f
fix(release-gate): install gates select the opt-in install_…
Opus 5.5
patch
2 days ago
| 1 | #!/usr/bin/env python3 |
| 2 | """Release gate reports: run the required checks, bind the evidence to one exact artifact. |
| 3 | |
| 4 | A *gate report* is machine-readable evidence that one candidate (version, |
| 5 | SHA-256, source revision) passed every required gate. ``verify_report`` is the |
| 6 | only thing publication trusts, and it trusts nothing it can recompute: |
| 7 | |
| 8 | * the decision is recomputed from the gate results, never read; |
| 9 | * the report must name the artifact's real SHA-256 and the version in its filename; |
| 10 | * every required gate must be present, passing, with exit code 0; |
| 11 | * it must be fresh (``max_age``) and not from the future; |
| 12 | * its ``provenance`` must show the revision the operator claimed, the revision Muse observed |
| 13 | before the gates, and the revision observed after them to be one and the same, on a clean |
| 14 | tree, bound to the artifact's SHA-256 -- and the checkout *now* (observed again at |
| 15 | verification) must still be that revision on a clean tree (see ``release_provenance``). |
| 16 | |
| 17 | A missing, malformed, stale or mismatched report raises ``GateError``. Callers |
| 18 | must treat any exception as "do not publish". |
| 19 | |
| 20 | CLI:: |
| 21 | |
| 22 | release_gate.py run --artifact A --muse-repo R --source-revision REV --python /abs/python3.14 --out report.json |
| 23 | release_gate.py verify --artifact A --report report.json --muse-repo R |
| 24 | |
| 25 | ``run`` provisions its own hermetic environment (``deploy/gate_env.py``: an explicit ``--python``, |
| 26 | a fresh venv, pinned tooling, the exact artifact installed with its grammars, verified before |
| 27 | any gate runs, removed afterwards) and executes every gate in it against the exact artifact via |
| 28 | ``MUSE_CANDIDATE_SDIST``; the e2e harness additionally uses temp HOME/venvs, so the stable Muse |
| 29 | install is never touched. The report is written atomically only after all gates have run and the |
| 30 | environment is gone. An interrupted run writes nothing. |
| 31 | """ |
| 32 | from __future__ import annotations |
| 33 | |
| 34 | import argparse |
| 35 | import json |
| 36 | import os |
| 37 | import platform |
| 38 | import subprocess |
| 39 | import sys |
| 40 | import tempfile |
| 41 | from collections.abc import Callable, Mapping, Sequence |
| 42 | from dataclasses import dataclass |
| 43 | from datetime import datetime, timedelta, timezone |
| 44 | from pathlib import Path |
| 45 | from typing import Any |
| 46 | |
| 47 | from deploy.gate_env import GateEnv, GateEnvError, gate_environment, gate_process_env |
| 48 | from deploy.release_provenance import CheckoutState, ProvenanceError, observe_checkout, validate_revision |
| 49 | from musehub.release_pointer import ReleaseSelectionError, sha256_file, validate_sha256, validate_version |
| 50 | |
| 51 | # Schema 2 adds ``provenance`` (independently observed source revision + cleanliness). Schema 1 |
| 52 | # reports carried only an operator-typed revision, i.e. no evidence; they are refused, not upgraded. |
| 53 | # Reports live at most DEFAULT_MAX_AGE, so re-running the gate is the whole migration. |
| 54 | REPORT_SCHEMA = 2 |
| 55 | DEFAULT_MAX_AGE = timedelta(hours=24) |
| 56 | _CLOCK_SKEW = timedelta(minutes=5) |
| 57 | |
| 58 | REQUIRED_GATES: tuple[str, ...] = ( |
| 59 | # packaging & provenance |
| 60 | "packaging.sdist_reproducible", |
| 61 | "packaging.version", |
| 62 | "packaging.dependency_metadata", |
| 63 | "packaging.manifest_contract", |
| 64 | # installation |
| 65 | "install.full", |
| 66 | "install.slim", |
| 67 | "install.legacy_upgrade", |
| 68 | "install.profile_preservation", |
| 69 | "install.explicit_grammar", |
| 70 | # runtime |
| 71 | "runtime.doctor", |
| 72 | "runtime.grammar_availability", |
| 73 | "runtime.installed_parser_parity", |
| 74 | ) |
| 75 | |
| 76 | |
| 77 | class GateError(Exception): |
| 78 | """The release gate evidence is missing, invalid, stale, mismatched or failing.""" |
| 79 | |
| 80 | |
| 81 | @dataclass(frozen=True) |
| 82 | class Candidate: |
| 83 | version: str |
| 84 | sha256: str |
| 85 | source_revision: str |
| 86 | installer_revision: int |
| 87 | |
| 88 | |
| 89 | @dataclass(frozen=True) |
| 90 | class GateResult: |
| 91 | status: str # "pass" | "fail" |
| 92 | exit_code: int |
| 93 | detail: str |
| 94 | |
| 95 | |
| 96 | def _passed(result: GateResult) -> bool: |
| 97 | return result.status == "pass" and result.exit_code == 0 |
| 98 | |
| 99 | |
| 100 | NO_DATABASE_URL = "postgresql+asyncpg://gate-has-no-database.invalid:1/none" |
| 101 | |
| 102 | BUILD_RELATION = ( |
| 103 | "unattested: the build is not signed or recorded; packaging.sdist_reproducible shows a fresh sdist of the " |
| 104 | "observed clean checkout is content-identical to the artifact" |
| 105 | ) |
| 106 | |
| 107 | |
| 108 | @dataclass(frozen=True) |
| 109 | class Provenance: |
| 110 | claimed_revision: str |
| 111 | observed_before: str |
| 112 | observed_after: str |
| 113 | tree_clean: bool |
| 114 | artifact_sha256: str |
| 115 | |
| 116 | |
| 117 | def build_report( |
| 118 | candidate: Candidate, results: Mapping[str, GateResult], *, now: datetime, provenance: Provenance, environment: Mapping[str, Any] |
| 119 | ) -> dict[str, Any]: |
| 120 | names = [g for g in REQUIRED_GATES] + sorted(n for n in results if n not in REQUIRED_GATES) |
| 121 | gates: list[dict[str, Any]] = [] |
| 122 | for name in names: |
| 123 | r = results.get(name) |
| 124 | if r is None: |
| 125 | continue # absent => verify_report rejects; never fabricated here |
| 126 | gates.append({"name": name, "status": r.status, "exit_code": r.exit_code, "detail": r.detail}) |
| 127 | ok = all(g in results and _passed(results[g]) for g in REQUIRED_GATES) and all(_passed(r) for r in results.values()) |
| 128 | return { |
| 129 | "schema": REPORT_SCHEMA, |
| 130 | "candidate": { |
| 131 | "version": candidate.version, |
| 132 | "sha256": candidate.sha256, |
| 133 | "source_revision": candidate.source_revision, |
| 134 | "installer_revision": candidate.installer_revision, |
| 135 | }, |
| 136 | "provenance": { |
| 137 | "claimed_revision": provenance.claimed_revision, |
| 138 | "observed_revision_before": provenance.observed_before, |
| 139 | "observed_revision_after": provenance.observed_after, |
| 140 | "tree_clean": provenance.tree_clean, |
| 141 | "artifact_sha256": provenance.artifact_sha256, |
| 142 | "observer": "muse rev-parse HEAD --json; muse status --json", |
| 143 | "build_relation": BUILD_RELATION, |
| 144 | }, |
| 145 | "environment": dict(environment), |
| 146 | "platform": {"system": platform.system(), "machine": platform.machine(), "python": platform.python_version()}, |
| 147 | "created_at": now.astimezone(timezone.utc).isoformat(), |
| 148 | "required_gates": list(REQUIRED_GATES), |
| 149 | "gates": gates, |
| 150 | "decision": "pass" if ok else "fail", |
| 151 | } |
| 152 | |
| 153 | |
| 154 | def verify_report( |
| 155 | text: str | None, |
| 156 | artifact: Path, |
| 157 | *, |
| 158 | now: datetime, |
| 159 | checkout: CheckoutState, |
| 160 | max_age: timedelta = DEFAULT_MAX_AGE, |
| 161 | ) -> Candidate: |
| 162 | """Return the bound ``Candidate`` iff *text* authorizes exactly *artifact*; else raise ``GateError``. |
| 163 | |
| 164 | *checkout* is the state Muse reports for the source checkout **at verification time**; it is |
| 165 | required so provenance can never be skipped. |
| 166 | """ |
| 167 | if not text: |
| 168 | raise GateError("no gate report: missing test evidence is a failure") |
| 169 | try: |
| 170 | data = json.loads(text) |
| 171 | except ValueError as exc: |
| 172 | raise GateError(f"gate report is not valid JSON: {exc}") from exc |
| 173 | if not isinstance(data, dict) or data.get("schema") != REPORT_SCHEMA: |
| 174 | raise GateError(f"gate report must be an object with schema {REPORT_SCHEMA}") |
| 175 | |
| 176 | candidate = _parse_candidate(data.get("candidate")) |
| 177 | provenance = _parse_provenance(data.get("provenance")) |
| 178 | created = _parse_time(data.get("created_at")) |
| 179 | gates = _parse_gates(data.get("gates")) |
| 180 | |
| 181 | # -- evidence ---------------------------------------------------------- |
| 182 | for name in REQUIRED_GATES: |
| 183 | if name not in gates: |
| 184 | raise GateError(f"required gate {name} is missing from the report") |
| 185 | for name, result in gates.items(): |
| 186 | if not _passed(result): |
| 187 | raise GateError(f"gate {name} did not pass (status={result.status!r}, exit_code={result.exit_code})") |
| 188 | if data.get("decision") != "pass": |
| 189 | raise GateError(f"report decision is {data.get('decision')!r}, not 'pass'") |
| 190 | |
| 191 | # -- freshness --------------------------------------------------------- |
| 192 | if created - now > _CLOCK_SKEW: |
| 193 | raise GateError(f"gate report is dated in the future ({created.isoformat()})") |
| 194 | if now - created > max_age: |
| 195 | raise GateError(f"gate report is stale: created {created.isoformat()}, max age {max_age}") |
| 196 | |
| 197 | # -- binding to the exact artifact -------------------------------------- |
| 198 | if not artifact.is_file(): |
| 199 | raise GateError(f"artifact {artifact} not found") |
| 200 | expected_name = f"muse-{candidate.version}.tar.gz" |
| 201 | if artifact.name != expected_name: |
| 202 | raise GateError(f"artifact filename {artifact.name!r} does not match candidate version {candidate.version!r} (expected {expected_name!r})") |
| 203 | actual = sha256_file(artifact) |
| 204 | if actual != candidate.sha256: |
| 205 | raise GateError(f"artifact sha256 {actual} does not match the report's {candidate.sha256}") |
| 206 | _check_provenance(provenance, candidate, checkout) |
| 207 | _check_environment(data.get("environment"), candidate) |
| 208 | return candidate |
| 209 | |
| 210 | |
| 211 | def _check_environment(raw: object, candidate: Candidate) -> None: |
| 212 | """The gates must have run in a hermetic environment that held exactly this candidate.""" |
| 213 | if not isinstance(raw, dict): |
| 214 | raise GateError("report.environment is required: the gates must run in a provisioned, recorded environment") |
| 215 | interp = raw.get("interpreter") |
| 216 | if not isinstance(interp, dict) or not all(isinstance(interp.get(k), str) and interp[k] for k in ("path", "version", "implementation")): |
| 217 | raise GateError("report.environment.interpreter must record path, version and implementation") |
| 218 | if raw.get("isolated") is not True: |
| 219 | raise GateError("report.environment.isolated must be true") |
| 220 | packages = raw.get("packages") |
| 221 | if not isinstance(packages, dict) or not packages or not all(isinstance(k, str) and isinstance(v, str) for k, v in packages.items()): |
| 222 | raise GateError("report.environment.packages must list the installed distributions") |
| 223 | if packages.get("muse") != candidate.version: |
| 224 | raise GateError(f"report.environment installed muse {packages.get('muse')!r}, not the candidate {candidate.version!r}") |
| 225 | if raw.get("candidate_sha256") != candidate.sha256: |
| 226 | raise GateError("report.environment was built for a different artifact than the candidate") |
| 227 | if raw.get("cleanup") != "removed": |
| 228 | raise GateError("report.environment.cleanup must be 'removed': the environment was not shown to be cleaned up") |
| 229 | for key in ("requirements_sha256", "packages_sha256"): |
| 230 | try: |
| 231 | validate_sha256(raw.get(key)) |
| 232 | except ReleaseSelectionError as exc: |
| 233 | raise GateError(f"report.environment.{key}: {exc}") from exc |
| 234 | |
| 235 | |
| 236 | def _check_provenance(prov: Provenance, candidate: Candidate, checkout: CheckoutState) -> None: |
| 237 | if not prov.tree_clean: |
| 238 | raise GateError("provenance: the gate ran against a checkout with uncommitted changes") |
| 239 | if prov.claimed_revision != prov.observed_before: |
| 240 | raise GateError(f"provenance: operator claimed {prov.claimed_revision} but the checkout was at {prov.observed_before}") |
| 241 | if prov.observed_before != prov.observed_after: |
| 242 | raise GateError(f"provenance: checkout moved during the gate run ({prov.observed_before} -> {prov.observed_after})") |
| 243 | if candidate.source_revision != prov.observed_before: |
| 244 | raise GateError(f"provenance: candidate.source_revision {candidate.source_revision} is not the observed {prov.observed_before}") |
| 245 | if prov.artifact_sha256 != candidate.sha256: |
| 246 | raise GateError("provenance: report is bound to a different artifact sha256 than its candidate") |
| 247 | if checkout.revision != prov.observed_before: |
| 248 | raise GateError(f"provenance: report was gated at {prov.observed_before}, but the checkout is now at {checkout.revision}") |
| 249 | if not checkout.clean: |
| 250 | raise GateError(f"provenance: the checkout now has uncommitted changes ({', '.join(checkout.dirty[:5])})") |
| 251 | |
| 252 | |
| 253 | def _parse_provenance(raw: object) -> Provenance: |
| 254 | if not isinstance(raw, dict): |
| 255 | raise GateError("report.provenance is required (schema 2): source revision was never independently observed") |
| 256 | try: |
| 257 | revisions = [validate_revision(raw.get(k)) for k in ("claimed_revision", "observed_revision_before", "observed_revision_after")] |
| 258 | sha = validate_sha256(raw.get("artifact_sha256")) |
| 259 | except (ProvenanceError, ReleaseSelectionError) as exc: |
| 260 | raise GateError(f"report.provenance invalid: {exc}") from exc |
| 261 | if raw.get("tree_clean") is not True and raw.get("tree_clean") is not False: |
| 262 | raise GateError("report.provenance.tree_clean must be a boolean") |
| 263 | return Provenance(revisions[0], revisions[1], revisions[2], raw["tree_clean"], sha) |
| 264 | |
| 265 | |
| 266 | def _parse_candidate(raw: object) -> Candidate: |
| 267 | if not isinstance(raw, dict): |
| 268 | raise GateError("report.candidate must be an object") |
| 269 | try: |
| 270 | version = validate_version(raw.get("version")) |
| 271 | sha = validate_sha256(raw.get("sha256")) |
| 272 | except ReleaseSelectionError as exc: |
| 273 | raise GateError(f"report.candidate invalid: {exc}") from exc |
| 274 | try: |
| 275 | revision = validate_revision(raw.get("source_revision")) |
| 276 | except ProvenanceError as exc: |
| 277 | raise GateError(f"report.candidate.source_revision: {exc}") from exc |
| 278 | installer = raw.get("installer_revision") |
| 279 | if not isinstance(installer, int) or isinstance(installer, bool): |
| 280 | raise GateError("report.candidate.installer_revision must be an integer") |
| 281 | return Candidate(version, sha, revision, installer) |
| 282 | |
| 283 | |
| 284 | def _parse_time(raw: object) -> datetime: |
| 285 | if not isinstance(raw, str): |
| 286 | raise GateError("report.created_at must be an ISO-8601 string") |
| 287 | try: |
| 288 | parsed = datetime.fromisoformat(raw) |
| 289 | except ValueError as exc: |
| 290 | raise GateError(f"report.created_at is not ISO-8601: {raw!r}") from exc |
| 291 | if parsed.tzinfo is None: |
| 292 | raise GateError("report.created_at must carry a timezone") |
| 293 | return parsed |
| 294 | |
| 295 | |
| 296 | def _parse_gates(raw: object) -> dict[str, GateResult]: |
| 297 | if not isinstance(raw, list): |
| 298 | raise GateError("report.gates must be a list") |
| 299 | gates: dict[str, GateResult] = {} |
| 300 | for entry in raw: |
| 301 | if not isinstance(entry, dict): |
| 302 | raise GateError("every report.gates entry must be an object") |
| 303 | name, status, code, detail = entry.get("name"), entry.get("status"), entry.get("exit_code"), entry.get("detail", "") |
| 304 | if not isinstance(name, str) or not name: |
| 305 | raise GateError("gate entry without a name") |
| 306 | if status not in ("pass", "fail") or not isinstance(code, int) or isinstance(code, bool) or not isinstance(detail, str): |
| 307 | raise GateError(f"gate {name} has a malformed status/exit_code/detail") |
| 308 | if name in gates: |
| 309 | raise GateError(f"duplicate gate entry {name}") |
| 310 | gates[name] = GateResult(status, code, detail) |
| 311 | return gates |
| 312 | |
| 313 | |
| 314 | # ── running gates ───────────────────────────────────────────────────────────── |
| 315 | |
| 316 | Runner = Callable[..., "subprocess.CompletedProcess[str]"] |
| 317 | |
| 318 | |
| 319 | def run_gates( |
| 320 | commands: Mapping[str, Sequence[str]], |
| 321 | *, |
| 322 | env: Mapping[str, str] | None = None, |
| 323 | cwd: Path | str | None = None, |
| 324 | cwds: Mapping[str, Path | str] | None = None, |
| 325 | timeout: float = 3600, |
| 326 | runner: Runner = subprocess.run, |
| 327 | ) -> dict[str, GateResult]: |
| 328 | """Run every required gate (and any extra supplied). A nonzero exit, timeout, |
| 329 | launch failure or missing command is a *fail*, never a skip. ``KeyboardInterrupt`` |
| 330 | and other ``BaseException``s propagate so an interrupted run yields no report.""" |
| 331 | results: dict[str, GateResult] = {} |
| 332 | for name in [*REQUIRED_GATES, *[n for n in commands if n not in REQUIRED_GATES]]: |
| 333 | argv = commands.get(name) |
| 334 | if not argv: |
| 335 | results[name] = GateResult("fail", 127, "no command defined for this gate") |
| 336 | continue |
| 337 | try: |
| 338 | proc = runner(list(argv), capture_output=True, text=True, timeout=timeout, env=dict(env) if env else None, cwd=(cwds or {}).get(name, cwd), check=False) |
| 339 | except subprocess.TimeoutExpired: |
| 340 | results[name] = GateResult("fail", 124, f"timed out after {timeout}s") |
| 341 | continue |
| 342 | except OSError as exc: |
| 343 | results[name] = GateResult("fail", 127, f"could not launch: {exc}") |
| 344 | continue |
| 345 | output = ((proc.stdout or "") + (proc.stderr or ""))[-2000:] |
| 346 | results[name] = GateResult("pass" if proc.returncode == 0 else "fail", proc.returncode, output) |
| 347 | return results |
| 348 | |
| 349 | |
| 350 | def default_gate_commands(artifact: Path, muse_repo: Path, musehub_repo: Path, python: Path | str) -> dict[str, list[str]]: |
| 351 | """The concrete gate commands, all run by *python* (the hermetic gate venv's interpreter, never the caller's). |
| 352 | |
| 353 | Every one is handed the exact artifact via MUSE_CANDIDATE_SDIST.""" |
| 354 | py = str(python) |
| 355 | checks = [py, str(musehub_repo / "deploy" / "release_checks.py")] |
| 356 | e2e = [py, "-m", "pytest", "-q", "-p", "no:cacheprovider", "tests/install_e2e/test_installer_profiles.py"] |
| 357 | t = "tests/install_e2e/test_installer_profiles.py::" |
| 358 | return { |
| 359 | "packaging.sdist_reproducible": [*checks, "reproducible", str(artifact), str(muse_repo)], |
| 360 | "packaging.version": [*checks, "version", str(artifact), str(muse_repo)], |
| 361 | "packaging.dependency_metadata": [*checks, "dependency-metadata", str(artifact)], |
| 362 | "packaging.manifest_contract": [*checks, "manifest-contract", str(artifact), str(musehub_repo)], |
| 363 | "install.full": [*e2e[:-1], t + "TestFreshInstalls::test_default_profile_is_full", t + "TestFreshInstalls::test_full_installs_grammars_but_not_swift"], |
| 364 | "install.slim": [*e2e[:-1], t + "TestFreshInstalls::test_slim_installs_core_only", t + "TestFreshInstalls::test_slim_doctor_reports_slim_by_design_and_exits_zero"], |
| 365 | "install.legacy_upgrade": [*e2e[:-1], t + "TestUpgrades::test_upgrade_from_legacy_rc18_installs_full_and_writes_manifest"], |
| 366 | "install.profile_preservation": [*e2e[:-1], t + "TestReinstall::test_rerun_preserves_the_recorded_slim_profile", t + "TestUpgrades::test_upgrade_from_older_release_preserves_profile"], |
| 367 | "install.explicit_grammar": [*e2e[:-1], t + "TestFreshInstalls::test_swift_is_an_explicit_opt_in_layered_on_full", t + "TestUpgrades::test_upgrade_preserves_a_swift_enabled_install"], |
| 368 | "runtime.doctor": [*e2e[:-1], t + "TestFreshInstalls::test_full_doctor_is_verified_and_healthy"], |
| 369 | "runtime.grammar_availability": [*e2e[:-1], t + "TestParityStillHolds::test_full_install_has_every_guaranteed_grammar"], |
| 370 | # the suite is opt-in (-m installed); the dev side is the checkout, the candidate is installed from the artifact |
| 371 | "runtime.installed_parser_parity": ["env", f"PYTHONPATH={muse_repo}", py, "-m", "pytest", "-q", "-p", "no:cacheprovider", "-m", "installed", "tests/installed"], |
| 372 | } |
| 373 | |
| 374 | |
| 375 | def _cwd_for(gate: str, muse_repo: Path, musehub_repo: Path) -> Path: |
| 376 | return muse_repo if gate == "runtime.installed_parser_parity" else musehub_repo |
| 377 | |
| 378 | |
| 379 | def run_release_gates( |
| 380 | artifact: Path, |
| 381 | muse_repo: Path, |
| 382 | musehub_repo: Path, |
| 383 | claimed_revision: str, |
| 384 | installer_revision: int, |
| 385 | *, |
| 386 | python: Path | str | None, |
| 387 | now: datetime, |
| 388 | observe: Callable[[Path], CheckoutState] = observe_checkout, |
| 389 | run: Callable[..., dict[str, GateResult]] = run_gates, |
| 390 | environment: Callable[..., Any] = gate_environment, |
| 391 | ) -> dict[str, Any]: |
| 392 | """Run all gates against *artifact* and return the (unwritten) report. |
| 393 | |
| 394 | The source revision is observed from the checkout, not taken from the caller: the gates do |
| 395 | not start unless the claimed revision equals the observed one on a clean tree, and the |
| 396 | checkout is observed again afterwards -- if it moved, no report is produced. The gates run |
| 397 | inside a hermetic environment provisioned here (see ``deploy/gate_env.py``); any failure to |
| 398 | build or verify it, or to remove it afterwards, means no report. |
| 399 | """ |
| 400 | try: |
| 401 | claimed = validate_revision(claimed_revision) |
| 402 | before = observe(muse_repo) |
| 403 | if not before.clean: |
| 404 | raise ProvenanceError(f"checkout has uncommitted changes: {', '.join(before.dirty[:5])}") |
| 405 | if before.revision != claimed: |
| 406 | raise ProvenanceError(f"claimed revision {claimed} but the checkout is at {before.revision}") |
| 407 | except ProvenanceError as exc: |
| 408 | raise GateError(f"source provenance: {exc}") from exc |
| 409 | version = artifact.name.removeprefix("muse-").removesuffix(".tar.gz") |
| 410 | sha = sha256_file(artifact) |
| 411 | candidate = Candidate(version, sha, before.revision, installer_revision) |
| 412 | try: |
| 413 | with environment(artifact, python) as genv: |
| 414 | gate_env: dict[str, str] = { |
| 415 | **gate_process_env(genv), |
| 416 | "MUSE_CANDIDATE_SDIST": str(artifact.resolve()), |
| 417 | "MUSE_CHECKOUT": str(muse_repo), |
| 418 | "MUSE_GATE_PYTHON": str(genv.python), |
| 419 | # The gates need no database. If one ever does, it must fail loudly rather than reach |
| 420 | # whatever Postgres happens to be running on the operator's machine. |
| 421 | "TEST_DATABASE_URL": NO_DATABASE_URL, |
| 422 | } |
| 423 | commands = default_gate_commands(artifact, muse_repo, musehub_repo, genv.python) |
| 424 | cwds = {g: _cwd_for(g, muse_repo, musehub_repo) for g in commands} |
| 425 | results = run(commands, env=gate_env, cwds=cwds) |
| 426 | evidence = {**genv.evidence(), "cleanup": "pending"} |
| 427 | except GateEnvError as exc: |
| 428 | raise GateError(f"hermetic gate environment: {exc}") from exc |
| 429 | evidence["cleanup"] = "removed" # reached only if the context manager removed the environment without error |
| 430 | try: |
| 431 | after = observe(muse_repo) |
| 432 | except ProvenanceError as exc: |
| 433 | raise GateError(f"source provenance after the run: {exc}") from exc |
| 434 | if after.revision != before.revision or not after.clean: |
| 435 | raise GateError(f"source checkout changed while the gates ran ({before.revision} -> {after.revision}, clean={after.clean}); the evidence is void") |
| 436 | if sha256_file(artifact) != sha: |
| 437 | raise GateError("artifact changed while the gates ran; the evidence is void") |
| 438 | prov = Provenance(claimed, before.revision, after.revision, True, sha) |
| 439 | return build_report(candidate, results, now=now, provenance=prov, environment=evidence) |
| 440 | |
| 441 | |
| 442 | def write_atomic(path: Path, text: str) -> None: |
| 443 | fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.") |
| 444 | try: |
| 445 | with os.fdopen(fd, "w") as fh: |
| 446 | fh.write(text) |
| 447 | os.replace(tmp, path) |
| 448 | except BaseException: |
| 449 | Path(tmp).unlink(missing_ok=True) |
| 450 | raise |
| 451 | |
| 452 | |
| 453 | def main(argv: list[str] | None = None) -> int: |
| 454 | parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 455 | sub = parser.add_subparsers(dest="command", required=True) |
| 456 | run = sub.add_parser("run", help="run all gates against an artifact and write a report") |
| 457 | run.add_argument("--artifact", type=Path, required=True) |
| 458 | run.add_argument("--muse-repo", type=Path, default=Path.home() / "ecosystem" / "muse") |
| 459 | run.add_argument("--musehub-repo", type=Path, default=Path(__file__).resolve().parents[1]) |
| 460 | run.add_argument("--source-revision", required=True) |
| 461 | run.add_argument("--python", type=Path, required=True, help="absolute path of the CPython to build the hermetic gate venv from") |
| 462 | run.add_argument("--out", type=Path, required=True) |
| 463 | ver = sub.add_parser("verify", help="verify a report authorizes an artifact (exit 0 only if it does)") |
| 464 | ver.add_argument("--artifact", type=Path, required=True) |
| 465 | ver.add_argument("--report", type=Path, required=True) |
| 466 | ver.add_argument("--muse-repo", type=Path, required=True, help="the checkout the gates ran against; observed again now") |
| 467 | ver.add_argument("--max-age-hours", type=float, default=DEFAULT_MAX_AGE.total_seconds() / 3600) |
| 468 | args = parser.parse_args(argv) |
| 469 | |
| 470 | now = datetime.now(timezone.utc) |
| 471 | if args.command == "run": |
| 472 | from musehub.api.routes.musehub.install import INSTALLER_REVISION |
| 473 | |
| 474 | try: |
| 475 | report = run_release_gates(args.artifact, args.muse_repo, args.musehub_repo, args.source_revision, INSTALLER_REVISION, python=args.python, now=now) |
| 476 | except GateError as exc: |
| 477 | print(f"GATE FAILED: {exc}", file=sys.stderr) |
| 478 | return 1 |
| 479 | write_atomic(args.out, json.dumps(report, indent=2)) |
| 480 | print(f"decision: {report['decision']} ({args.out})", file=sys.stderr) |
| 481 | return 0 if report["decision"] == "pass" else 1 |
| 482 | try: |
| 483 | text = args.report.read_text() if args.report.is_file() else None |
| 484 | cand = verify_report(text, args.artifact, now=now, checkout=observe_checkout(args.muse_repo), max_age=timedelta(hours=args.max_age_hours)) |
| 485 | except (GateError, ProvenanceError) as exc: |
| 486 | print(f"GATE FAILED: {exc}", file=sys.stderr) |
| 487 | return 1 |
| 488 | print(f"gate report authorizes muse {cand.version} sha256={cand.sha256}") |
| 489 | return 0 |
| 490 | |
| 491 | |
| 492 | if __name__ == "__main__": |
| 493 | raise SystemExit(main()) |
File History
5 commits
sha256:982b4ce08b140ea3fa9057705ff1806a702c9203689c22b797a19f9db678fe5f
fix(release-gate): install gates select the opt-in install_…
Opus 5.5
patch
2 days ago
sha256:a977b39d08e15683210a33608e25712fe3ed9c8f05f0388fb69ef38acf835426
feat(release): hermetic release gates in a provisioned, pin…
Opus 5.5
minor
⚠
12 days ago
sha256:04db68e43d9007348dbd726b2a3e52f73e83c5646a4511c07e19cb9a66e28b5f
fix(release): gate provenance is observed from the checkout…
Opus 5.5
minor
⚠
12 days ago
sha256:28b9e16197029996ce556a27058a9faa4eb27e088440726c5d8aaa8a811a4a64
docs(release): runbook separating build/validate/publish/ac…
Opus 5.5
patch
12 days ago
sha256:5c8cb67657bbffc367d7e4b74e9fd7dea32dada487ae1ef893d2d5abfa0447e1
feat(release): gate report bound to the exact artifact, gat…
Opus 5.5
patch
13 days ago