diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c8611886..2f9dfa6c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,6 +24,24 @@ on: description: 'Rehearsal mode: build + publish to TestPyPI only, then STOP (no PyPI, no git tag, no notebook commits, no Colab bumps). Used by the Heart/Brain release-validation gate.' required: false default: 'false' + # The commits a run builds (M2.4, PyAutoHands#337). Human ruling + # 2026-11-12: a release publishes exactly the commits that passed + # rehearsal, even if `main` has moved since — on 2026-11-09 the release + # leg re-checked-out `main` and published PyAutoArray four commits past + # what the rehearsal built. A live run therefore REFUSES unless it can + # name a rehearsal record: the `testpypi-rehearsal-version` artifact of a + # successful rehearsal run, which now records the commit of every library + # it built. See the resolve_commits job. The exact caller contract (input + # names, where rehearsal.json lives in the artifact, its schema, and what + # to feed Heart) is docs/internals.md "Caller contract". + rehearsal_run_id: + description: 'Live release: the run id of the successful rehearsal (rehearsal=true run of this workflow) whose recorded commits to release. Ignored in rehearsal mode.' + required: false + default: '' + commit_shas: + description: 'JSON {"PyAutoNerves": "", ...} for all six libraries. Rehearsal: build exactly these (default: main HEADs, pinned once at run start). Live: must equal the rehearsal record (with rehearsal_run_id), or must match some recent rehearsal record (without it).' + required: false + default: '' jobs: # Single source of truth for rehearsal-vs-live: the `rehearsal` dispatch @@ -44,6 +62,98 @@ jobs: echo "rehearsal=${REHEARSAL}" >> "$GITHUB_OUTPUT" echo "event=\`${{ github.event_name }}\` → rehearsal=\`${REHEARSAL}\`" >> "$GITHUB_STEP_SUMMARY" + # Single source of truth for WHICH library commits this run builds (M2.4, + # PyAutoHands#337). Every library checkout below reads its commit from this + # job's `commit_shas` output (`{bare repo name: full sha}`), never `ref: main`. + # + # - Rehearsal: pins each library's `main` HEAD once, here, before any leg + # starts (or takes the `commit_shas` input verbatim), so all legs build the + # same set and `rehearsal_version` records exactly what was built. + # - Live: reads the rehearsal record(s) and resolves through + # autohands/rehearsed_commits.py, which REFUSES — before anything is built, + # tagged or published — when no rehearsal is named, the named run has no + # record (failed, not a rehearsal, artifact expired, or a rehearsal from + # before this job existed), or the record disagrees with `commit_shas`. + # `main` having moved since the rehearsal is NOT a refusal: the rehearsed + # commits are released and later merges wait for the next release. + resolve_commits: + runs-on: ubuntu-latest + needs: resolve_mode + permissions: + actions: read # read the rehearsal run + download its artifact + contents: read + outputs: + commit_shas: ${{ steps.resolve.outputs.commit_shas }} + env: + GH_TOKEN: ${{ github.token }} + REHEARSAL: ${{ needs.resolve_mode.outputs.rehearsal }} + REHEARSAL_RUN_ID: ${{ github.event.inputs.rehearsal_run_id }} + COMMIT_SHAS: ${{ github.event.inputs.commit_shas }} + steps: + - uses: actions/checkout@v4 + with: + path: PyAutoHands + # The helper reads the library set from autohands/config/workspaces.yaml. + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + - name: Install PyYAML + run: python3 -m pip install pyyaml + - name: Fetch rehearsal records (live release only) + if: "${{ needs.resolve_mode.outputs.rehearsal != 'true' }}" + run: | + set -euo pipefail + mkdir -p records + # A record is the testpypi-rehearsal-version artifact of a SUCCESSFUL + # run of this workflow; it is saved under records//, and the + # directory name is the record's identity. + fetch() { + local id="$1" path conclusion + path="$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$id" --jq '.path' 2>/dev/null || true)" + conclusion="$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$id" --jq '.conclusion' 2>/dev/null || true)" + if [ "$path" != ".github/workflows/release.yml" ] || [ "$conclusion" != "success" ]; then + echo "run $id is not a successful run of release.yml (path=${path:-?} conclusion=${conclusion:-?}) — not a rehearsal record" + return 0 + fi + gh run download "$id" -R "$GITHUB_REPOSITORY" -n testpypi-rehearsal-version \ + -D "records/$id" >/dev/null 2>&1 \ + && echo "run $id: rehearsal record found" \ + || echo "run $id: no testpypi-rehearsal-version artifact (a live run, or expired)" + } + if [ -n "$REHEARSAL_RUN_ID" ]; then + if ! [[ "$REHEARSAL_RUN_ID" =~ ^[0-9]+$ ]]; then + echo "::error::REFUSED — rehearsal_run_id must be a numeric run id, got '$REHEARSAL_RUN_ID'" + exit 1 + fi + fetch "$REHEARSAL_RUN_ID" + elif [ -n "$COMMIT_SHAS" ]; then + # No run named: look for a recent rehearsal that built these commits. + for id in $(gh run list -R "$GITHUB_REPOSITORY" --workflow release.yml \ + --status success -L 30 --json databaseId --jq '.[].databaseId'); do + fetch "$id" + done + fi + - name: Resolve the commits this run builds + id: resolve + run: | + set -euo pipefail + if [ "$REHEARSAL" = "true" ]; then + python3 PyAutoHands/autohands/rehearsed_commits.py rehearsal \ + --commit-shas "$COMMIT_SHAS" --out commit_shas.json + else + python3 PyAutoHands/autohands/rehearsed_commits.py release \ + --records-dir records \ + --rehearsal-run-id "$REHEARSAL_RUN_ID" \ + --commit-shas "$COMMIT_SHAS" \ + --out commit_shas.json + fi + { + echo "### Library commits this run builds" + echo '```json' + cat commit_shas.json + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + version_number: runs-on: ubuntu-latest needs: resolve_mode @@ -83,7 +193,11 @@ jobs: release_test_pypi: runs-on: ubuntu-latest - needs: version_number + # resolve_commits pins the commit each library leg builds — in a + # rehearsal the commits it records, in a live run the rehearsed ones. + needs: + - version_number + - resolve_commits env: TWINE_REPOSITORY: testpypi TWINE_USERNAME: __token__ @@ -93,33 +207,40 @@ jobs: python-version: [3.12] project: - repository: PyAutoLabs/PyAutoNerves - ref: main path: PyAutoNerves - repository: PyAutoLabs/PyAutoFit - ref: main path: PyAutoFit - repository: PyAutoLabs/PyAutoArray - ref: main path: PyAutoArray - repository: PyAutoLabs/PyAutoGalaxy - ref: main path: PyAutoGalaxy - repository: PyAutoLabs/PyAutoLens - ref: main path: PyAutoLens - repository: PyAutoLabs/PyAutoCTI - ref: main path: PyAutoCTI steps: - uses: actions/checkout@v4 with: path: PyAutoHands - - name: Checkout + - name: Checkout (the commit resolve_commits pinned, never `main`) uses: actions/checkout@v4 with: repository: "${{ matrix.project.repository }}" - ref: "${{ matrix.project.ref }}" + ref: "${{ fromJSON(needs.resolve_commits.outputs.commit_shas)[matrix.project.path] }}" path: "${{ matrix.project.path }}" + # Full history: the next step proves the commit is on origin/main. + fetch-depth: 0 + persist-credentials: false + - name: Confirm the pinned commit is on main + # Both modes. A `commit_shas` input can name any fetchable commit (a + # PR head, a side branch); refusing here means such a commit never + # becomes a rehearsal record, so it can never be released either. + env: + PINNED_SHA: "${{ fromJSON(needs.resolve_commits.outputs.commit_shas)[matrix.project.path] }}" + run: | + set -euo pipefail + python3 PyAutoHands/autohands/rehearsed_commits.py verify \ + --repo-dir "${{ matrix.project.path }}" --expected "$PINNED_SHA" - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: @@ -454,51 +575,65 @@ jobs: TWINE_REPOSITORY: pypi TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI }} + # The rehearsed commit of this row's library (M2.4): checked out, verified, + # tagged and built — `main` is never read for what to publish. + REHEARSED_SHA: ${{ fromJSON(needs.resolve_commits.outputs.commit_shas)[matrix.project.path] }} needs: # Workspace-integration validation moved to PyAutoHeart # (workspace-validation.yml). Build is a pure executor: it gates only on # the TestPyPI build + version. Release readiness is enforced upstream by # the release agent via `pyauto-heart readiness`. - resolve_mode + - resolve_commits - release_test_pypi - version_number strategy: matrix: project: - repository: PyAutoLabs/PyAutoNerves - ref: main path: PyAutoNerves - repository: PyAutoLabs/PyAutoFit - ref: main path: PyAutoFit - repository: PyAutoLabs/PyAutoArray - ref: main path: PyAutoArray - repository: PyAutoLabs/PyAutoGalaxy - ref: main path: PyAutoGalaxy - repository: PyAutoLabs/PyAutoLens - ref: main path: PyAutoLens - repository: PyAutoLabs/PyAutoCTI - ref: main path: PyAutoCTI steps: - uses: actions/checkout@v4 with: path: PyAutoHands - - name: Checkout + - name: Checkout (the rehearsed commit) # The libraries are public, so the read needs no App token. Full history - # (tags included) like the former `git clone`. persist-credentials: false + # (tags and origin/main included) like the former `git clone`, so the next + # step can report how far `main` has moved. persist-credentials: false # keeps checkout's GITHUB_TOKEN extraheader out of the clone's config, # where it would shadow the App credential the Tag step pushes with. uses: actions/checkout@v4 with: repository: ${{ matrix.project.repository }} - ref: ${{ matrix.project.ref }} + ref: ${{ env.REHEARSED_SHA }} path: ${{ matrix.project.path }} fetch-depth: 0 persist-credentials: false + - name: Confirm the checkout is the rehearsed commit + # Refuses if HEAD is not the rehearsed commit or is not on origin/main + # (merge-base --is-ancestor); reports (does not refuse) `main` having + # moved since — the ruling is to release what was rehearsed. + run: | + set -euo pipefail + python3 PyAutoHands/autohands/rehearsed_commits.py verify \ + --repo-dir "${{ matrix.project.path }}" --expected "$REHEARSED_SHA" \ + | tee verify.txt + { + echo "### ${{ matrix.project.path }}" + echo '```' + cat verify.txt + echo '```' + } >> "$GITHUB_STEP_SUMMARY" - name: Set up Python 3.12 uses: actions/setup-python@v5 with: @@ -581,6 +716,11 @@ jobs: VERSION="${{ needs.version_number.outputs.version_number }}" gh auth setup-git HEAD_SHA="$(git rev-parse HEAD)" + # Never tag anything but the rehearsed commit (M2.4). + if [ "$HEAD_SHA" != "$REHEARSED_SHA" ]; then + echo "::error::REFUSED — HEAD $HEAD_SHA is not the rehearsed commit $REHEARSED_SHA; not tagging." + exit 1 + fi # For an annotated tag `refs/tags/` is the tag object and # `refs/tags/^{}` the commit it peels to; a lightweight tag has no # peeled line, so fall back to the ref's own object id. @@ -602,6 +742,15 @@ jobs: echo "never silently re-point a published tag." exit 1 fi + # The safeguard line (decision sheet M2.4): the published tag peels to + # the rehearsed commit, read back from the remote, or the job fails. + REMOTE_REFS="$(git ls-remote origin "refs/tags/$VERSION" "refs/tags/$VERSION^{}")" + PUBLISHED="$(printf '%s\n' "$REMOTE_REFS" | awk -v r="refs/tags/$VERSION^{}" '$2 == r {print $1}')" + if [ "$PUBLISHED" != "$REHEARSED_SHA" ]; then + echo "::error::tag $VERSION on origin peels to ${PUBLISHED:-nothing}, not the rehearsed commit $REHEARSED_SHA." + exit 1 + fi + echo "rehearsed $REHEARSED_SHA released $VERSION -> $PUBLISHED identical" - name: Build run: | pushd "${{ matrix.project.path }}" @@ -1084,10 +1233,27 @@ jobs: fi + # Library Colab URL bump — on a release branch, never on `main` (M2.4, + # PyAutoHands#337). This job used to commit and push the bump straight to + # each library's `main` the moment the release published: a bot commit that + # moved `main` past the rehearsed commit during every release, so the next + # readiness check saw "source moved since rehearsal". + # + # Mechanism: check out the RELEASED commit (the rehearsed one, which the tag + # peels to), apply the bump there, and push it as `release/`. The + # tag stays exactly the rehearsed commit (`compare/...` is + # `identical`), and `main` takes the bump only through a normal reviewed PR + # from that branch. The bumped links point at the WORKSPACE tags + # (`...//blob//...`), which release_workspaces creates; + # until the branch merges, a library README on `main` still links the + # previous workspace tag, which keeps resolving — nothing breaks, it just + # lags one release. Workspace bumps (release_workspaces) are unchanged: their + # tag must carry their own notebooks, so they still commit and tag there. bump_library_colab_urls: runs-on: ubuntu-latest needs: - resolve_mode + - resolve_commits - release - version_number if: "${{ needs.resolve_mode.outputs.rehearsal != 'true' }}" @@ -1104,9 +1270,9 @@ jobs: - uses: actions/checkout@v4 with: path: PyAutoHands - - name: Mint App token (push library main) + - name: Mint App token (push the library release branch) # Short job: minted at the top. checkout's `token:` persists it for the - # final `git push origin main`. + # final `git push` of the release branch. id: app_token uses: actions/create-github-app-token@v3 with: @@ -1115,11 +1281,13 @@ jobs: owner: PyAutoLabs repositories: ${{ matrix.library }} permission-contents: write - - name: Checkout library + # Open/update the PR that merges release/ back into main. + permission-pull-requests: write + - name: Checkout library (the released commit, not main) uses: actions/checkout@v4 with: repository: PyAutoLabs/${{ matrix.library }} - ref: main + ref: ${{ fromJSON(needs.resolve_commits.outputs.commit_shas)[matrix.library] }} path: library token: ${{ steps.app_token.outputs.token }} - name: Configure Git @@ -1130,18 +1298,73 @@ jobs: # is what GitHub attributes on; it differs from the App id. git config --global user.email "346909844+pyautolabs-bot[bot]@users.noreply.github.com" git config --global user.name "pyautolabs-bot[bot]" - - name: Bump Colab URL tag refs + - name: Bump Colab URL tag refs onto release/ + id: bump run: | + set -euo pipefail VERSION="${{ needs.version_number.outputs.version_number }}" + BRANCH="release/$VERSION" + echo "branch=$BRANCH" >> "$GITHUB_OUTPUT" pushd library bash "$GITHUB_WORKSPACE/PyAutoHands/autohands/bump_colab_urls.sh" "$VERSION" - if ! git diff --quiet; then - # Tracked modifications only — the bumper rewrites existing files in - # place; `git add -A` would sweep any unrelated leftovers (#156 step 2). - git add -u - git commit -m "Release $VERSION: bump Colab URL tag refs" - git push origin main + if git diff --quiet; then + echo "No Colab URLs to bump in ${{ matrix.library }}." + echo "pushed=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + # Idempotent re-run: the branch already carries this release's bump + # (the PR step below still runs, so a PR lost on the first try is made). + if git ls-remote --exit-code origin "refs/heads/$BRANCH" >/dev/null; then + echo "$BRANCH already on origin — re-run, skipping the push." + echo "pushed=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + # Tracked modifications only — the bumper rewrites existing files in + # place; `git add -A` would sweep any unrelated leftovers (#156 step 2). + git add -u + git commit -m "Release $VERSION: bump Colab URL tag refs" + # The only ref this job writes. Never `main`. + git push origin "HEAD:refs/heads/$BRANCH" + echo "pushed=true" >> "$GITHUB_OUTPUT" + { + echo "### ${{ matrix.library }} Colab bump" + echo "On \`$BRANCH\` (tag \`$VERSION\` stays the rehearsed commit; \`main\` untouched)." + } >> "$GITHUB_STEP_SUMMARY" + - name: Open or update the Colab-bump PR into main + # The bump reaches `main` only through this PR (normal review and merge + # gates), never by a push from the release. One PR per library per + # release: an open PR from the branch is updated, otherwise one is + # opened. Best-effort: the release has already published, so a failure + # here is a warning and the branch stays for a human to PR by hand. + if: "${{ steps.bump.outputs.pushed == 'true' }}" + env: + GH_TOKEN: ${{ steps.app_token.outputs.token }} + REPO: PyAutoLabs/${{ matrix.library }} + BRANCH: ${{ steps.bump.outputs.branch }} + VERSION: ${{ needs.version_number.outputs.version_number }} + run: | + set -uo pipefail + TITLE="release $VERSION: Colab URL bump" + BODY="Merges \`$BRANCH\` into \`main\`: the Colab URL bump for release $VERSION. + + The release tag \`$VERSION\` is the rehearsed commit and does not include this bump; the release no longer pushes to \`main\` (PyAutoHands#337). Docs-only: Colab links now point at the $VERSION workspace tags." + warn() { + echo "::warning::Colab-bump PR for $REPO not opened/updated ($1) — open it by hand: https://github.com/$REPO/compare/main...$BRANCH" + exit 0 + } + EXISTING="$(gh pr list --repo "$REPO" --head "$BRANCH" --base main --state open \ + --json number --jq '.[0].number // empty')" || warn "gh pr list failed" + if [ -n "$EXISTING" ]; then + gh pr edit "$EXISTING" --repo "$REPO" --title "$TITLE" --body "$BODY" \ + || warn "gh pr edit #$EXISTING failed" + URL="https://github.com/$REPO/pull/$EXISTING" + echo "Updated $URL" + else + URL="$(gh pr create --repo "$REPO" --base main --head "$BRANCH" \ + --title "$TITLE" --body "$BODY")" || warn "gh pr create failed" + echo "Opened $URL" fi + echo "Merge to main by PR: $URL" >> "$GITHUB_STEP_SUMMARY" # --------------------------------------------------------------------------- # Wait for the released stack to be installable from production PyPI. @@ -1469,12 +1692,19 @@ jobs: runs-on: ubuntu-latest needs: - resolve_mode + - resolve_commits - version_number - release_test_pypi if: "${{ needs.resolve_mode.outputs.rehearsal == 'true' }}" steps: - - name: Record resolved TestPyPI version + - name: Record resolved TestPyPI version and the commits built + # `commit_shas` is the rehearsal record a live release resolves through + # (M2.4): the exact library commits every leg of THIS run built, pinned + # once by resolve_commits. A live run releases these and nothing else. + env: + COMMIT_SHAS: ${{ needs.resolve_commits.outputs.commit_shas }} run: | + set -euo pipefail VERSION="${{ needs.version_number.outputs.version_number }}" echo "Rehearsal published to TestPyPI at version: $VERSION" mkdir -p rehearsal @@ -1487,10 +1717,12 @@ jobs: "packages": ["autonerves", "autoarray", "autofit", "autogalaxy", "autolens", "autocti"], "run_id": "${{ github.run_id }}", "run_attempt": "${{ github.run_attempt }}", - "build_sha": "${{ github.sha }}" + "build_sha": "${{ github.sha }}", + "commit_shas": $COMMIT_SHAS } JSON - cat rehearsal/rehearsal.json + # Fail here, not in the release that later reads it. + python3 -m json.tool rehearsal/rehearsal.json - name: Write rehearsal summary run: | VERSION="${{ needs.version_number.outputs.version_number }}" diff --git a/autohands/rehearsed_commits.py b/autohands/rehearsed_commits.py new file mode 100644 index 00000000..d159a4d3 --- /dev/null +++ b/autohands/rehearsed_commits.py @@ -0,0 +1,387 @@ +"""Resolve the library commits a release.yml run builds (M2.4, PyAutoHands#337). + +Human ruling, 2026-11-12: a release publishes exactly the commits that passed +rehearsal, even if ``main`` has moved since. On 2026-11-09 the rehearsal built +one library at ``e2b05f004`` and the release leg, checking out ``main`` again +100 minutes later, published ``6ae33d433`` (four commits the rehearsal never +saw). + +``release.yml``'s ``resolve_commits`` job runs this module once per run, before +anything is built, and every library checkout in the run uses its output: + +- **rehearsal** (``rehearsal=true``): pin each library's ``main`` HEAD once, at + the start of the run (or take an explicit ``commit_shas`` manifest), so every + matrix leg builds the same set and the ``testpypi-rehearsal-version`` + artifact records exactly what was built (``rehearsal.json`` ``commit_shas``). +- **release** (live): take the commits from a rehearsal record (the + ``testpypi-rehearsal-version`` artifact of a successful rehearsal run) and, + when the caller also passes a ``commit_shas`` manifest, require the two to + agree. No record, a record without ``commit_shas``, or a mismatch is a + refusal: nothing is built, tagged or published. + +``verify`` runs after each live checkout and prints the safeguard line the +decision sheet asked for: the rehearsed commit, the commit checked out, how far +``main`` has moved since, and ``identical``. + +The library set is ``config/workspaces.yaml`` ``libraries`` (the released set, +release.yml's matrix order) and the owner is ``$GITHUB_REPOSITORY_OWNER`` or +``--owner``: organ code names no instance repo (tenant firewall). The GitHub +calls (run lookup, artifact download) stay in the workflow, so everything here +is testable offline; PyYAML is needed only to read the library set. +""" + +from __future__ import annotations + +import argparse +import json +import os +import re +import subprocess +import sys +from pathlib import Path +from typing import Callable, Iterable + +CONFIG_PATH = Path(__file__).resolve().parent / "config" / "workspaces.yaml" + + +def libraries() -> tuple[str, ...]: + """The published library set, as bare repo names, from Build config. + + These are the keys of commit_shas.json (Brain's nightly capture), of + Heart's validation report and of release.yml's matrix ``path``, so one + manifest addresses all three. + """ + import yaml # lazy: `verify` runs on a bare runner python without it + + cfg = yaml.safe_load(CONFIG_PATH.read_text()) or {} + names = tuple(str(lib["name"]) for lib in cfg.get("libraries") or []) + if not names: + raise Refused(f"no libraries declared in {CONFIG_PATH}") + return names + +RECORD_FILENAME = "rehearsal.json" + +_FULL_SHA = re.compile(r"^[0-9a-f]{40}$") + + +class Refused(Exception): + """The release must not proceed; the message says why.""" + + +# -------------------------------------------------------------------------- +# Manifest + record parsing +# -------------------------------------------------------------------------- + + +def _normalise(shas: object, where: str, libs: Iterable[str] | None = None) -> dict[str, str]: + """Validate a {library: sha} mapping covering every library. + + Extra keys (e.g. ``PyAutoHands``, which Heart folds into its report) are + ignored: only the published libraries are pinned. Full 40-hex SHAs only — + a prefix cannot be checked out reliably and cannot be compared for + ``identical`` without ambiguity. + """ + if not isinstance(shas, dict): + raise Refused(f"{where}: expected a JSON object of {{library: sha}}, got {type(shas).__name__}") + out: dict[str, str] = {} + missing = [] + for lib in libs if libs is not None else libraries(): + sha = shas.get(lib) + if not sha: + missing.append(lib) + continue + sha = str(sha).strip().lower() + if not _FULL_SHA.match(sha): + raise Refused(f"{where}: {lib} sha {sha!r} is not a full 40-character commit sha") + out[lib] = sha + if missing: + raise Refused(f"{where}: no commit for {', '.join(missing)}") + return out + + +def parse_manifest(text: str | None) -> dict[str, str] | None: + """Parse a ``commit_shas`` manifest; empty/blank means "none given". + + Accepts the bare ``{library: sha}`` form (Brain's ``commit_shas.json``) or + the wrapped ``{"commit_shas": {...}}`` form Heart also reads. + """ + if text is None or not text.strip(): + return None + try: + data = json.loads(text) + except json.JSONDecodeError as exc: + raise Refused(f"commit_shas manifest is not valid JSON: {exc}") from exc + if isinstance(data, dict) and isinstance(data.get("commit_shas"), dict): + data = data["commit_shas"] + return _normalise(data, "commit_shas manifest") + + +def load_records(records_dir: Path) -> list[dict]: + """Load every rehearsal record under ``records_dir``. + + The workflow downloads each candidate run's ``testpypi-rehearsal-version`` + artifact into ``//``. The directory name is the run the + artifact was downloaded FROM, so it — not the body's own ``run_id`` field — + is the record's identity (``source_run_id``). + """ + records = [] + if not records_dir.is_dir(): + return records + for path in sorted(records_dir.rglob(RECORD_FILENAME)): + try: + data = json.loads(path.read_text()) + except (OSError, json.JSONDecodeError): + continue + if not isinstance(data, dict) or data.get("mode") != "rehearsal": + continue + rel = path.relative_to(records_dir) + data["source_run_id"] = rel.parts[0] if len(rel.parts) > 1 else str(data.get("run_id", "")) + records.append(data) + return records + + +# -------------------------------------------------------------------------- +# Resolution +# -------------------------------------------------------------------------- + + +def _diff(manifest: dict[str, str], recorded: dict[str, str]) -> list[str]: + return [ + f" {lib}: manifest {manifest[lib][:12]} != rehearsed {recorded[lib][:12]}" + for lib in manifest + if manifest[lib] != recorded.get(lib) + ] + + +def resolve_release( + manifest: dict[str, str] | None, + records: list[dict], + rehearsal_run_id: str | None = None, +) -> tuple[dict[str, str], dict]: + """Return ``(commits, record)`` for a live release, or raise ``Refused``. + + - ``rehearsal_run_id`` given: that run's record is the authority; a + manifest, if also given, must match it exactly. + - only a manifest given: some record must carry exactly those commits. + - neither: refuse — there is nothing to say what was rehearsed. + """ + run_id = (rehearsal_run_id or "").strip() or None + if manifest is None and run_id is None: + raise Refused( + "no rehearsal named: pass rehearsal_run_id (the rehearsal run of release.yml) " + "and/or commit_shas (the rehearsed commits). A live release builds only rehearsed commits." + ) + + candidates = records + if run_id is not None: + candidates = [r for r in records if str(r.get("source_run_id")) == run_id] + if not candidates: + raise Refused( + f"no rehearsal record for run {run_id}: its testpypi-rehearsal-version artifact " + "is missing (not a successful rehearsal run, or the artifact expired)." + ) + + usable = [] + unusable = [] + for record in candidates: + try: + usable.append((_normalise(record.get("commit_shas"), f"rehearsal run {record.get('source_run_id')}"), record)) + except Refused as exc: + unusable.append(str(exc)) + if not usable: + detail = "; ".join(unusable) or "no rehearsal records found" + raise Refused( + "no rehearsal record carries the commits it built (a rehearsal from before " + f"PyAutoHands#337 does not record them — rehearse again): {detail}" + ) + + if manifest is None: + # run_id is set here; one run has one artifact. + commits, record = usable[0] + return commits, record + + for commits, record in usable: + if commits == manifest: + return commits, record + + if run_id is not None: + commits, _ = usable[0] + raise Refused( + f"the recorded commits of rehearsal run {run_id} do not match the commit_shas manifest:\n" + + "\n".join(_diff(manifest, commits)) + ) + raise Refused( + f"no rehearsal record matches the commit_shas manifest ({len(usable)} recent rehearsal " + "record(s) checked): these exact commits were never rehearsed." + ) + + +def main_heads( + owner: str = "", + libs: Iterable[str] | None = None, + ls_remote: Callable[[str], str] | None = None, +) -> dict[str, str]: + """Each library's current ``main`` HEAD, read once (public repos, no token).""" + libs = tuple(libs if libs is not None else libraries()) + + def _default(lib: str) -> str: + if not owner: + raise Refused("no GitHub owner to read main HEADs from ($GITHUB_REPOSITORY_OWNER or --owner)") + out = subprocess.run( + ["git", "ls-remote", f"https://github.com/{owner}/{lib}.git", "refs/heads/main"], + capture_output=True, text=True, check=True, + ).stdout + return out.split()[0] if out.strip() else "" + + fetch = ls_remote or _default + heads = {lib: fetch(lib).strip().lower() for lib in libs} + return _normalise(heads, "main HEADs", libs) + + +def resolve_rehearsal( + manifest: dict[str, str] | None, + heads: Callable[[], dict[str, str]], +) -> dict[str, str]: + """The commits a rehearsal builds: the manifest if given, else main HEADs now.""" + return manifest if manifest is not None else heads() + + +# -------------------------------------------------------------------------- +# Post-checkout safeguard +# -------------------------------------------------------------------------- + + +def _git(repo_dir: Path, *args: str) -> str: + return subprocess.run( + ["git", "-C", str(repo_dir), *args], capture_output=True, text=True, check=True + ).stdout.strip() + + +def verify_checkout(repo_dir: Path, expected: str, main_ref: str = "origin/main") -> list[str]: + """Assert HEAD is the pinned commit AND is on ``main``; return log lines. + + Runs in both modes: in each rehearsal leg (so a ``commit_shas`` input naming + a PR head, a side branch or any other fetchable commit never becomes a + rehearsal record) and in each live release leg (so it is never published). + "On main" means ``git merge-base --is-ancestor HEAD ``; a clone + without ```` cannot prove it and is refused. + + ``main`` having moved past the commit is reported, not refused: the ruling + is to release the rehearsed commit; later merges wait for the next release. + Because the commit is an ancestor of ``main``, ``HEAD..main`` counts exactly + the commits merged since, so the number reported is the real lag. + """ + expected = expected.strip().lower() + head = _git(repo_dir, "rev-parse", "HEAD").lower() + if head != expected: + raise Refused(f"checked out {head} but the pinned commit is {expected}") + try: + main = _git(repo_dir, "rev-parse", "--verify", f"{main_ref}^{{commit}}").lower() + except subprocess.CalledProcessError as exc: + raise Refused( + f"{main_ref} is not in this clone, so {head} cannot be shown to be on main " + "(check out with full history)" + ) from exc + on_main = subprocess.run( + ["git", "-C", str(repo_dir), "merge-base", "--is-ancestor", head, main], + capture_output=True, text=True, + ) + if on_main.returncode == 1: + raise Refused( + f"{head} is not on main ({main_ref} = {main}): only commits merged to main " + "are rehearsed or released (a PR head or side-branch commit is refused)" + ) + if on_main.returncode != 0: + raise Refused(f"could not check whether {head} is on main: {on_main.stderr.strip()}") + lines = [f"pinned commit: {expected}", f"checked out: {head} (identical, on main)"] + if main == head: + lines.append(f"{main_ref}: {main} (main has not moved past it)") + else: + ahead = _git(repo_dir, "rev-list", "--count", f"{head}..{main}") + lines.append( + f"{main_ref}: {main} (main has {ahead} commit(s) merged after this one; " + "they are not in this build and wait for the next release)" + ) + return lines + + +# -------------------------------------------------------------------------- +# CLI +# -------------------------------------------------------------------------- + + +def _emit(commits: dict[str, str], out: Path | None) -> None: + compact = json.dumps(commits, sort_keys=True, separators=(",", ":")) + if out is not None: + out.write_text(json.dumps(commits, indent=2, sort_keys=True) + "\n") + gh_out = os.environ.get("GITHUB_OUTPUT") + if gh_out: + with open(gh_out, "a") as fh: + fh.write(f"commit_shas={compact}\n") + for lib, sha in commits.items(): + print(f" {lib:<13} {sha}") + + +def _cmd_rehearsal(args: argparse.Namespace) -> int: + manifest = parse_manifest(args.commit_shas) + commits = resolve_rehearsal(manifest, heads=lambda: main_heads(args.owner)) + print("Rehearsal builds " + ("the commit_shas manifest:" if manifest else "main HEADs pinned now:")) + _emit(commits, args.out) + return 0 + + +def _cmd_release(args: argparse.Namespace) -> int: + manifest = parse_manifest(args.commit_shas) + records = load_records(args.records_dir) + commits, record = resolve_release(manifest, records, args.rehearsal_run_id) + print( + f"Release builds the commits rehearsed in run {record.get('source_run_id')} " + f"(TestPyPI {record.get('version', '?')}):" + ) + _emit(commits, args.out) + return 0 + + +def _cmd_verify(args: argparse.Namespace) -> int: + for line in verify_checkout(args.repo_dir, args.expected, args.main_ref): + print(line) + return 0 + + +def build_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + sub = parser.add_subparsers(dest="cmd", required=True) + + p = sub.add_parser("rehearsal", help="pin the commits a rehearsal builds") + p.add_argument("--commit-shas", default="", help="optional JSON manifest {library: sha}") + p.add_argument("--owner", default=os.environ.get("GITHUB_REPOSITORY_OWNER", ""), + help="GitHub owner of the libraries (default: $GITHUB_REPOSITORY_OWNER)") + p.add_argument("--out", type=Path) + p.set_defaults(func=_cmd_rehearsal) + + p = sub.add_parser("release", help="resolve a live release's commits from a rehearsal record") + p.add_argument("--records-dir", type=Path, required=True) + p.add_argument("--rehearsal-run-id", default="") + p.add_argument("--commit-shas", default="", help="optional JSON manifest {library: sha}") + p.add_argument("--out", type=Path) + p.set_defaults(func=_cmd_release) + + p = sub.add_parser("verify", help="assert a checkout is the rehearsed commit; report main drift") + p.add_argument("--repo-dir", type=Path, required=True) + p.add_argument("--expected", required=True) + p.add_argument("--main-ref", default="origin/main") + p.set_defaults(func=_cmd_verify) + return parser + + +def main(argv: list[str] | None = None) -> int: + args = build_parser().parse_args(argv) + try: + return args.func(args) + except Refused as exc: + print(f"::error::REFUSED — {exc}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/bin/autohands b/bin/autohands index 845aa7d0..6a27fae0 100755 --- a/bin/autohands +++ b/bin/autohands @@ -117,6 +117,7 @@ INTERNAL_MODULES=( env_config # env-profile resolution library result_collector # per-run JSON result library generate_autofit # generate.py's autofit-specific branch + rehearsed_commits # release.yml helper: pins/resolves the rehearsed library commits (#337) ) # _python_in_autohands