Skip to content

Commit f2b246e

Browse files
ChuckBuildsclaude
andauthored
fix(version): make the core version have exactly one answer (#428)
* fix(version): make the core version have exactly one answer Plugin compatibility floors compare against src.__version__, so that string has to be trustworthy. It has not been. v3.1.0 was tagged 2026-05-31 while src/__init__.py still said "1.0.0"; the bump did not land until 2026-07-12. Every device installed from that release reports 1.0.0, which is below the (2, 0, 0) floor in PluginLoader._warn_if_incompatible -- so those users are silently exempt from every plugin compatibility warning. web_interface carried a third answer, a hardcoded "3.0.0" that nothing read and that had drifted two majors from the core. It now re-exports the canonical value, so it cannot disagree again. Adds: - test/test_version_consistency.py (enrolled in the core unit CI job): src.__version__ is parseable semver, matches the newest CHANGELOG heading, the CHANGELOG's headings are unique and descending, and web_interface tracks the core. src.plugin_system.__version__ is deliberately excluded -- it versions the plugin API and moves independently. - scripts/check_release_version.py + a release-version-check workflow that asserts the tag, the CHANGELOG and src.__version__ agree. Runs on pushed v* tags and published releases, and via workflow_dispatch so a tag can be checked *before* it is created: python scripts/check_release_version.py v3.2.0 Verified: 757 core unit tests pass including the four new ones; the script exits 0 for v3.2.0 and non-zero for both a mismatched tag (v3.1.0) and a non-semver one (v2.5); web_interface and web_interface.app still import. Prerequisite for cutting v3.2.0 -- phase B4 in docs/SPORTS_UNIFICATION.md. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5 * fix(version): address review — regex strictness, OSError, stale doc claim From CodeRabbit on #428, all three valid: - The module docstring claimed the tag check "runs at release time in .github/workflows/release-version-check.yml". That workflow is held back to a follow-up PR (the pushing token lacks the `workflow` scope), so the claim was false as written. Both files now describe the script as a manual pre-flight and say the CI wiring is still to come. - `\d` also matches non-ASCII decimal digits, which int() happily parses, and `\s` matches newlines -- so "##\n3.2.0" read as a version heading. Patterns now use [0-9] and [ \t], kept in step across the test and the script, with a regression test pinning both behaviours. - A missing or unreadable CHANGELOG.md raised OSError out of read_text() and printed a traceback. In a release gate that reads as "the tooling is broken"; it now reports the path and a recovery action and exits 1. Verified: v3.2.0 passes, a mismatched tag exits 1, and a missing CHANGELOG exits 1 with the new message instead of a traceback. 5 tests pass. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5 --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 963ab82 commit f2b246e

3 files changed

Lines changed: 241 additions & 1 deletion

File tree

scripts/check_release_version.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python3
2+
"""Assert that a release tag, the CHANGELOG, and `src.__version__` all agree.
3+
4+
Run it *before* creating a tag to check yourself:
5+
6+
python scripts/check_release_version.py v3.2.0
7+
8+
Wiring it into CI (on pushed `v*` tags and published releases) is a follow-up
9+
PR, so for now it is a manual pre-flight: run it before creating the tag and a
10+
mismatch shows up here rather than as a silent wrong answer on user devices.
11+
12+
Why this exists: `v3.1.0` was tagged 2026-05-31 while `src/__init__.py` still
13+
said `"1.0.0"`; the bump to `"3.1.0"` did not land until 2026-07-12. Devices
14+
installed from that release report `1.0.0`, which is below the `(2, 0, 0)` floor
15+
in `PluginLoader._warn_if_incompatible`, so they are silently exempt from every
16+
plugin compatibility warning. Plugin `ledmatrix_min_version` floors are only as
17+
trustworthy as this agreement. See `docs/SPORTS_UNIFICATION.md`, phase B4.
18+
"""
19+
20+
from __future__ import annotations
21+
22+
import argparse
23+
import re
24+
import sys
25+
from pathlib import Path
26+
27+
REPO_ROOT = Path(__file__).resolve().parents[1]
28+
sys.path.insert(0, str(REPO_ROOT))
29+
30+
# [0-9] rather than \d, and [ \t] rather than \s: \d also matches non-ASCII
31+
# decimal digits (which int() parses), and \s matches newlines, so "##\n3.2.0"
32+
# would otherwise read as a version heading. Keep these in step with
33+
# test/test_version_consistency.py.
34+
SEMVER = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+$")
35+
HEADING = re.compile(
36+
r"^##[ \t]+(?P<version>[0-9]+\.[0-9]+\.[0-9]+)[ \t]*$", re.MULTILINE)
37+
38+
39+
def normalize(tag: str) -> str:
40+
"""`v3.2.0` and `3.2.0` are the same release; tags here carry the `v`."""
41+
return tag[1:] if tag.startswith("v") else tag
42+
43+
44+
def newest_changelog_version(changelog: Path) -> str | None:
45+
"""Newest version heading, or None when there is none.
46+
47+
Raises OSError if the file cannot be read; main() turns that into a clear
48+
message rather than a traceback, because this runs as a release gate and a
49+
traceback there reads as "the tooling is broken", not "your CHANGELOG is
50+
missing".
51+
"""
52+
headings = HEADING.findall(changelog.read_text(encoding="utf-8"))
53+
return headings[0] if headings else None
54+
55+
56+
def main() -> int:
57+
parser = argparse.ArgumentParser(description=__doc__)
58+
parser.add_argument(
59+
"tag",
60+
help="Release tag to check, with or without the leading 'v' (e.g. v3.2.0)",
61+
)
62+
args = parser.parse_args()
63+
64+
from src import __version__ as core_version
65+
66+
tag_version = normalize(args.tag)
67+
changelog_path = REPO_ROOT / "CHANGELOG.md"
68+
69+
problems: list[str] = []
70+
71+
try:
72+
changelog_version = newest_changelog_version(changelog_path)
73+
except OSError as e:
74+
print(
75+
f"Release version check FAILED for tag {args.tag}:\n"
76+
f" - could not read {changelog_path}: {e}\n"
77+
f" Restore the file (git checkout -- CHANGELOG.md) and re-run.",
78+
file=sys.stderr,
79+
)
80+
return 1
81+
82+
if not SEMVER.match(tag_version):
83+
problems.append(
84+
f"tag {args.tag!r} is not vX.Y.Z. Older tags (v2.5) predate this "
85+
"check; new releases must be full semver so floors can parse them."
86+
)
87+
88+
if not SEMVER.match(core_version):
89+
problems.append(f"src.__version__ is {core_version!r}, which is not X.Y.Z")
90+
91+
if tag_version != core_version:
92+
problems.append(
93+
f"tag says {tag_version} but src.__version__ says {core_version}. "
94+
"Bump src/__init__.py to match the tag before releasing — devices "
95+
"report __version__, not the tag, and plugin floors compare "
96+
"against it."
97+
)
98+
99+
if changelog_version is None:
100+
problems.append("CHANGELOG.md has no '## X.Y.Z' version heading")
101+
elif changelog_version != core_version:
102+
problems.append(
103+
f"CHANGELOG.md's newest heading is {changelog_version} but "
104+
f"src.__version__ is {core_version}. Plugin authors read the "
105+
"CHANGELOG to pick a ledmatrix_min_version floor."
106+
)
107+
108+
if problems:
109+
print(f"Release version check FAILED for tag {args.tag}:", file=sys.stderr)
110+
for problem in problems:
111+
print(f" - {problem}", file=sys.stderr)
112+
return 1
113+
114+
print(
115+
f"OK: tag {args.tag}, src.__version__ {core_version}, and the CHANGELOG "
116+
"all agree."
117+
)
118+
return 0
119+
120+
121+
if __name__ == "__main__":
122+
sys.exit(main())

test/test_version_consistency.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Version reporting must have exactly one answer.
2+
3+
`src.__version__` is the canonical core version. The plugin loader compares
4+
plugin `ledmatrix_min_version` floors against it, and the plugin ecosystem
5+
floors on the number recorded in `CHANGELOG.md` — so if those two disagree, a
6+
plugin can declare a floor that is satisfied by a core which does not actually
7+
ship the module it needs.
8+
9+
This has already gone wrong once. The `v3.1.0` tag was cut 2026-05-31, but
10+
`src/__init__.py` was not bumped from `"1.0.0"` to `"3.1.0"` until 2026-07-12,
11+
six weeks later. Every device installed from that release reports `1.0.0`,
12+
which is below the `(2, 0, 0)` floor in `PluginLoader._warn_if_incompatible` —
13+
so those users get no compatibility warning at all. See
14+
`docs/SPORTS_UNIFICATION.md` (phase B4).
15+
16+
A tag is not available here, so the tag half of the check lives in
17+
`scripts/check_release_version.py`. Wiring that script into CI (on pushed `v*`
18+
tags and published releases) is a follow-up PR; until it lands, run it by hand
19+
before tagging:
20+
21+
python scripts/check_release_version.py v3.2.0
22+
23+
Note: `src.plugin_system.__version__` is deliberately NOT checked. That module
24+
versions the *plugin API* (it sits beside `__api_version__` and is documented as
25+
such), which moves independently of the core version.
26+
"""
27+
28+
import re
29+
from pathlib import Path
30+
31+
import pytest
32+
33+
import src
34+
35+
REPO_ROOT = Path(__file__).resolve().parents[1]
36+
CHANGELOG = REPO_ROOT / "CHANGELOG.md"
37+
38+
# [0-9] rather than \d: \d also matches non-ASCII decimal digits, which int()
39+
# happily parses, so a heading in Arabic-Indic numerals would pass the pattern
40+
# and then mismatch confusingly. [ \t] rather than \s for the same class of
41+
# reason -- \s matches newlines, so "##\n3.2.0" would read as a heading.
42+
SEMVER = re.compile(r"^([0-9]+)\.([0-9]+)\.([0-9]+)$")
43+
# Version headings look like "## 3.2.0". A leading "## Unreleased" section is
44+
# allowed and skipped -- it is where module additions are staged before a bump.
45+
HEADING = re.compile(
46+
r"^##[ \t]+(?P<version>[0-9]+\.[0-9]+\.[0-9]+)[ \t]*$", re.MULTILINE)
47+
48+
49+
def test_core_version_is_semver():
50+
"""A floor comparison parses this string; it has to be parseable."""
51+
assert SEMVER.match(src.__version__), (
52+
f"src.__version__ is {src.__version__!r}, which is not X.Y.Z. "
53+
"The loader's floor comparison cannot parse it."
54+
)
55+
56+
57+
def test_changelog_documents_the_current_version():
58+
"""The newest versioned CHANGELOG heading is the version we claim to be.
59+
60+
Plugins floor on the version recorded in the CHANGELOG as first shipping a
61+
module. If the code says 3.2.0 and the CHANGELOG's newest entry is 3.1.0,
62+
that record points at the wrong release.
63+
"""
64+
text = CHANGELOG.read_text(encoding="utf-8")
65+
headings = HEADING.findall(text)
66+
assert headings, "CHANGELOG.md has no '## X.Y.Z' version headings"
67+
68+
newest = headings[0]
69+
assert newest == src.__version__, (
70+
f"src.__version__ is {src.__version__!r} but the newest CHANGELOG "
71+
f"heading is {newest!r}. Bump one to match the other: the CHANGELOG is "
72+
"what plugin authors read to pick a ledmatrix_min_version floor."
73+
)
74+
75+
76+
def test_changelog_versions_are_ordered_and_unique():
77+
"""A duplicated or out-of-order heading makes 'first release shipping X'
78+
ambiguous, which is exactly the question the sunset rule asks."""
79+
text = CHANGELOG.read_text(encoding="utf-8")
80+
versions = [tuple(int(p) for p in v.split(".")) for v in HEADING.findall(text)]
81+
82+
duplicates = {v for v in versions if versions.count(v) > 1}
83+
assert not duplicates, f"CHANGELOG.md has duplicate version headings: {duplicates}"
84+
85+
assert versions == sorted(versions, reverse=True), (
86+
"CHANGELOG.md version headings are not in descending order; "
87+
f"got {['.'.join(map(str, v)) for v in versions]}"
88+
)
89+
90+
91+
def test_web_interface_version_tracks_the_core():
92+
"""web_interface used to carry its own hardcoded "3.0.0", a third answer to
93+
'what version is this'. It now re-exports the canonical one."""
94+
web_interface = pytest.importorskip(
95+
"web_interface", reason="web_interface needs Flask, which is optional here"
96+
)
97+
assert getattr(web_interface, "__version__", None) == src.__version__, (
98+
"web_interface.__version__ has drifted from src.__version__; it should "
99+
"re-export the canonical value rather than hardcode its own."
100+
)
101+
102+
103+
def test_heading_pattern_is_strict_about_digits_and_whitespace():
104+
"""`\\d` also matches non-ASCII decimal digits and `\\s` matches newlines,
105+
either of which would let a malformed heading through and then fail the
106+
comparison with a confusing message. Pin the tightened patterns."""
107+
assert HEADING.findall("## 3.2.0\n") == ["3.2.0"]
108+
assert HEADING.findall("##\t3.2.0 \n") == ["3.2.0"]
109+
# A bare "##" whose version sits on the next line is not a heading.
110+
assert HEADING.findall("##\n3.2.0\n") == []
111+
# Arabic-Indic digits parse via int() but are not our version format.
112+
assert HEADING.findall("## ٣.٢.٠\n") == []
113+
assert SEMVER.match("٣.٢.٠") is None

web_interface/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
LED Matrix Web Interface V3
33
Modern web interface for controlling the LED Matrix display
44
"""
5-
__version__ = "3.0.0"
5+
6+
# Re-exported, never hardcoded. This used to carry its own "3.0.0", a third
7+
# answer to "what version is this" alongside the tag and src.__version__ —
8+
# and disagreeing version numbers are what made plugin compatibility floors
9+
# untrustworthy (see docs/SPORTS_UNIFICATION.md, phase B4).
10+
from src import __version__ # noqa: F401
611

0 commit comments

Comments
 (0)