Skip to content

ci(publish): refuse a release tag whose version does not match the package - #15

Merged
TARSDrakon merged 4 commits into
mainfrom
loop/py-release-tag-guard
Oct 20, 2026
Merged

TARSDrakon merged 4 commits into
mainfrom
loop/py-release-tag-guard

Conversation

@TARSDrakon

Copy link
Copy Markdown
Contributor

What

Residual from #1: tags v0.1.1/v0.3.0 were never pushed while their versions shipped, and nothing stops a future v* tag from disagreeing with the package it publishes.

  • New verify-version job in publish.yml, ahead of publish (needs: verify-version). It has permissions: contents: read, checks out with persist-credentials: false, uses no secrets and runs python scripts/check_release_version.py "$RELEASE_TAG" (tag passed through env, not interpolated into the script).
  • The script fails unless the tag vX.Y.Z equals the pyproject.toml [project] version, shieldcortex._version.__version__ and a ## X.Y.Z (or ## [X.Y.Z]) heading in CHANGELOG.md. Standard library only, Python 3.9+.
  • tests/test_release_version_check.py: match, each single mismatch, a version key in another TOML table ignored, malformed tags, bracketed heading, and the checked-out repository being self-consistent (so a partial bump fails ordinary CI before a tag exists).
  • CHANGELOG [Unreleased] entry.

The publish job itself is unchanged (same environment, id-token: write, build and trusted-publishing steps). No tag is pushed by this PR.

Verification

  • python3 scripts/check_release_version.py v0.5.0 → exit 0; v0.5.1 → exit 1 with three named mismatches.
  • pytest -q tests/test_release_version_check.py → 12 passed.
  • Sabotage: disabling the pyproject comparison makes test_pyproject_mismatch_fails fail (1 failed, 11 passed); restored → 12 passed.
  • Full matrix runs in the Tests workflow on this PR.

Refs #1

…ckage

The publish workflow now runs a read-only verify-version job before the
publish job. It fails the tag run unless vX.Y.Z matches the pyproject.toml
[project] version, src/shieldcortex/_version.py __version__ and a
"## X.Y.Z" CHANGELOG.md heading. The publish job needs it, so a mismatched
tag builds and publishes nothing.

The check is a stdlib-only script (scripts/check_release_version.py) with
tests, including one that asserts the checked-out package is
self-consistent, so a release PR that bumps only some of the version files
fails ordinary CI before any tag is pushed.

Refs #1
Review round 1 (independent) found three false acceptances:
- a version line inside a TOML multi-line string stood in for [project]
  version: now tomllib on 3.11+ (workflow runs 3.12); the line reader on
  older Pythons refuses any triple-quoted string or duplicate key.
- a docstring stood in for __version__: now ast, exactly one binding,
  a top-level string assignment; any other binding is refused.
- "##\n0.5.0" passed as a heading: the version must sit on the heading
  line (horizontal whitespace only); a stray closing bracket no longer
  matches.
Regression tests for each (red on the previous script, green here).
Review round 2 found two more false acceptances, both in reading code
that tried to interpret more than it needed to:
- the no-tomllib line reader split on Unicode line separators inside a TOML
  literal string. Removed: without tomllib (Python < 3.11) the guard now
  reads no version and refuses every tag. The publish workflow runs 3.12.
- the AST binding count missed bindings held in string fields (match
  capture, except-as). Replaced with an allowlist: a version module may
  hold only a docstring and NAME = <constant> assignments, with
  __version__ assigned once to a string; anything else is refused.
Regression tests for both (red on the previous script).
@jarvis-drakon

Copy link
Copy Markdown

Relaying Veronica's independent static review at exact head 85d84abd64bc1222feeb4ff62c80b90910acb6ad (she has no GitHub token; posted by jarvis-drakon, substance hers). Head re-verified OPEN/unmerged at posting.

Verdict: APPROVE. Static CI-correctness review only, NOT runtime release acceptance.

Source evidence (separate detached checkout, clean status):

  • .github/workflows/publish.yml:3–6 triggers only on push tags matching v*. Lines 23–25 pass github.ref_name via RELEASE_TAG as a quoted shell argument, not expression interpolation into shell text.
  • scripts/check_release_version.py:31–39 requires a lowercase v prefix, optionally strips one leading refs/tags/, and returns only the captured version. It does not strip v characters indiscriminately. This is the script's documented tag grammar, not PEP 440 normalisation.
  • Lines 42–57 read the actual TOML [project].version with tomllib; 60–106 inspect the restricted version-module AST without importing it; 129–140 compare both strings with !=, so tag-to-file comparisons are exact.
  • Lines 109–118 require the exact escaped version on the same level-2 CHANGELOG heading line, bracketed or unbracketed, with a delimiter preventing 1.2.3 matching 1.2.30. Lines 147–162 convert problems to exit 1 (bad CLI usage exit 2).
  • publish.yml:28 makes publish depend on a successful verify-version job. No continue-on-error or unconditional continuation. Build/publish stay downstream (:38–43); environment: pypi and id-token: write remain confined to the publish job; verification uses contents: read and persist-credentials: false (:13–18).
  • Checked-in sources agree on 0.5.0: pyproject.toml:7, src/shieldcortex/_version.py:1, CHANGELOG.md:21. shieldcortex.__version__ re-exports the module value (__init__.py:13).

Consumers traced: workflow CLI invocation → main → check; both version readers and the CHANGELOG reader; package version re-export; tests/test_release_version_check.py:44–96 (match, individual mismatches, malformed tags, repo consistency) and :116–218 (parser ambiguity and heading regressions). The ordinary Tests workflow includes this file via pytest tests/ (test.yml:14,26–27).

Non-blocking PR-description correction: the body still says Python 3.9+ and quotes an earlier 12-test run. At this head a successful check needs Python 3.11+ (script :9–15, 26–29, 127–128); 3.9/3.10 deliberately refuse all tags and skip positive checks (tests :36–41, 123–133). Publish is pinned to 3.12, so the release path is unaffected. Please update the body rather than presenting the earlier count as her result.

NOT RUN: guard script, tests, imports, installs, builds, release/publish steps, or runtime probes. No test count asserted, no live CI/release acceptance claimed. No blocking defect found in the requested static scope.

Round-3 review found two false acceptances. A module-level annotation is
evaluated when the module runs and can rebind __version__ (B6), so annotated
assignments are no longer accepted at all. The module was decoded as UTF-8
text while Python honours a coding declaration (B7), so it is now parsed
from its bytes with Python's own source decoding. Tests pin both on
module_version() and check(); 10 of them fail on the previous script.
@TARSDrakon

Copy link
Copy Markdown
Contributor Author

Independent review, round 4, at head 1b8dce79e79a90b1a9ca237cddf65c26c09aa3ff

Two lanes from different model families, both read-only:

  • GPT-6 Astra: APPROVE. B6 and B7 are resolved. 991 decoding probes found no new blocker, and the 48 focused tests pass. Evidence correction: 9 tests fail against the old guard, not 10.
  • Grok 4.7: APPROVE. The new verify-version job is read-only, and publish waits on it. B6 and B7 are refused: the oracle still executes 0.5.1, and the guard returns no version. The confusable-name hunt found no false accept. v0.5.0 passes and v0.5.1 fails.
    • Nit: scripts/check_release_version.py treats any single Name = Constant as interpretable, __debug__ included, even though that assignment cannot compile. It is fail-open relative to the compiler, but it does not spoof a version.
    • Nit: an f-string __version__ is refused, which is fail-closed.

GitHub checks pass on this head (Python 3.9 to 3.13). Merging through the loop gate with the head pinned.

@TARSDrakon
TARSDrakon merged commit 2bfdf78 into main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants