Skip to content

fix(semver): parse exact versions with the SemVer 2.0.0 grammar - #3894

Merged
Daniel Meppiel (danielmeppiel) merged 3 commits into
mainfrom
danielmeppiel-0-37-r5-deps-hardening
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 3 commits into
mainfrom
danielmeppiel-0-37-r5-deps-hardening

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

TL;DR

parse_semver now follows the SemVer 2.0.0 grammar for numeric identifiers: ASCII digits only, no leading zeros. The policy literal-tag check (v1.2.3) drops its own regex and calls parse_semver, so require_pinned_constraint no longer treats v01.2.3, v1.2.3-01 or [email protected] as exact releases.

Problem (WHY)

Approach (WHAT)

Before After
\d+ core parts and prerelease ids 0|[1-9][0-9]* core parts; prerelease ids are numeric without leading zeros or alphanumeric
_LITERAL_TAG_RE regex in policy _is_literal_version_tag: v prefix plus parse_semver(spec[1:])
Build metadata [0-9A-Za-z-]+ Unchanged (SemVer allows leading zeros there)

Implementation (HOW)

File Change
src/apm_cli/marketplace/semver.py _SEMVER_RE is built from _NUMERIC_ID / _PRERELEASE_ID fragments; docstring names it the one exact-version parser.
src/apm_cli/policy/_constraint_pinning.py _LITERAL_TAG_RE removed; classification step 5 calls _is_literal_version_tag, which routes through parse_semver (lazy import, as the neighbouring helpers do).
tests/unit/marketplace/test_semver.py Rejects the issue repros plus non-ASCII digits; keeps 1.2.3-0, 1.2.3-0a, 1.2.3+001 valid.
tests/unit/policy/test_pinned_constraint.py v01.2.3, v1.02.3, v1.2.3-01, v1.2.3-rc..1, v\u0661.2.3, [email protected] classify as BARE_BRANCH.
tests/unit/deps/test_git_semver_resolver.py iter_semver_tags over v01.9.0, v1.9.0-01, v1.0.0 keeps only v1.0.0, so ^1.0.0 cannot select a leading-zero tag (panel fold; fails on main).

_PACKAGE_AT_TAG_RE and the {name}-v... layouts already delegate the version part to parse_semver, so they pick up the stricter grammar with no edit.

Trade-offs

  • No new architecture guard. The record's Done-when lists one, but the captain pruned it for this follow-up ("No new guard is needed unless trivial"). A new rule needs a registry record, a MutationCase and a frozen-inventory entry, so it is not trivial. The two exact-version paths named in the record now share one owner.
  • Prefix detectors left alone. commands/outdated.TAG_RE, marketplace/version_resolver._BARE_SEMVER and bundle/formats._VERSION_RE are ^\d+\.\d+\.\d+ prefix probes, not exact-version parsers. Range syntax and tag-naming conventions are out of scope in the record.
  • Behavior change, scoped to non-SemVer inputs. Git tags such as v1.02.0 stop counting as semver candidates during range resolution, and the same refs stop passing require_pinned_constraint. Both are the issue's cases. $ keeps its current trailing-newline behavior; no ref reaches the parser with one.

Benefits

  1. One exact-version grammar for parse_semver and the policy literal-tag check (2 owners become 1).
  2. require_pinned_constraint judges v01.2.3 / [email protected] as unbounded, matching the documented "exact release" intent.
  3. Version ordering can no longer compare 01.2.3 and 1.2.3 as equal versions.

Validation

Fail-before on main (tests added first): 15 failed, 6 passed for the new cases. After the fix:

Commands and results
$ .venv/bin/python -m pytest tests/unit/marketplace/test_semver.py tests/unit/policy/test_pinned_constraint.py tests/unit/policy/test_pinned_constraint_declared.py -p no:xdist -q
174 passed, 1 xfailed

$ .venv/bin/python -m pytest tests/unit/marketplace tests/unit/deps tests/unit/policy \
    tests/unit/test_outdated_command.py tests/unit/test_outdated_marketplace.py \
    tests/unit/test_outdated_marketplace_detection.py tests/unit/test_outdated_phase3w5.py \
    tests/unit/test_drift_detection.py tests/unit/test_drift_registry.py \
    tests/unit/test_git_reference_resolver_resolution.py tests/unit/test_git_reference_resolver_phase3w4.py \
    tests/unit/install/test_git_semver_wiring.py tests/unit/install/test_cached_semver_rebuild.py \
    tests/unit/install/test_resolve_registry.py tests/unit/install/test_resolve_resolution_paths.py \
    tests/unit/install/test_resolve_spec_drift.py tests/unit/install/test_resolve_strategy_selection.py \
    tests/unit/install/test_cache_pin.py tests/spec_conformance/test_resolution_reqs.py -p no:xdist -q
5807 passed, 4 xfailed, 68 subtests passed

# real binary, local git fixtures (cd tests/integration)
$ APM_E2E_TESTS=1 APM_BINARY_PATH=.venv/bin/apm python -m pytest test_git_semver_install_e2e.py \
    test_git_semver_prerelease_chain_lifecycle.py test_git_semver_subdirectory_frozen_replay_lifecycle.py \
    test_policy_pinned_constraint_e2e.py test_revision_pin_lock_consistency_lifecycle.py \
    test_revision_pin_update_lifecycle.py test_marketplace_sha_pin_install.py test_lifecycle_workspace_lock.py \
    test_architecture_lifecycle_lock.py test_unquoted_lockfile_version_lifecycle.py test_outdated_coverage.py \
    test_wave6_outdated_view_coverage.py test_ssh_semver_transport_contract.py test_deps_resolver_resolution.py \
    test_architecture_marketplace_tag_pattern.py test_architecture_revision_pins.py -p no:xdist -q
5 failed, 359 passed
# The 5 failures are test_ssh_semver_transport_contract::test_git_semver_resolution_honors_consume_transport_contract[*],
# which asserts a PyInstaller-packaged binary ("Expected a native packaged apm binary, got prefix=b'#!/U'");
# the local .venv/bin/apm is a script shim. Environment precondition, not this change; CI runs it on the built binary.

$ uvx [email protected] check src/ tests/ scripts/lint_architecture_boundaries.py scripts/architecture_linter/ scripts/windows_native_symlink_probe_entry.py
All checks passed!
$ uvx [email protected] format --check (same paths)
2411 files already formatted
$ python -m pylint --disable=all --enable=R0801 --min-similarity-lines=10 --fail-on=R0801 src/apm_cli/ scripts/...
rated 10.00/10
$ bash scripts/lint-auth-signals.sh
[+] auth-signal lint clean
$ bash scripts/lint-architecture-boundaries.sh
rc=0

Scenario evidence

Scenario Principle Test
apm.yml pins acme/lib#v01.2.3 under require_pinned_constraint: true: reported as a bare branch, not an exact release Governance tests/unit/policy/test_pinned_constraint.py::test_classify_non_semver_literal_tag_returns_bare_branch
Valid pins (v1.2.3, v1.0.0-rc.1, v1.2.3+build.42, [email protected]) stay pinned Governance tests/unit/policy/test_pinned_constraint.py::test_classify_pinned_specs_return_none, test_pinned_constraint_declared.py
A git dependency on ^1.0.0 in a repo tagged v01.9.0 and v1.0.0 resolves to v1.0.0 Secure by default tests/unit/deps/test_git_semver_resolver.py::TestIterSemverTags::test_skips_tags_outside_semver_grammar
Semver range resolution over git tags and registry versions is unchanged for valid versions Reproducibility tests/integration/test_git_semver_install_e2e.py, test_git_semver_prerelease_chain_lifecycle.py, tests/unit/deps

How to test

  • python -c "from apm_cli.marketplace.semver import parse_semver as p; print(p('01.2.3'), p('1.2.3-01'), p('1.2.3-0a'))" prints None None SemVer(...).
  • Run tests/unit/marketplace/test_semver.py and tests/unit/policy/test_pinned_constraint.py.

Issue and approved scope

Issue: Closes #3795

Human scope-approval comment: #3795 (comment) (restated record for the pruned scope; earlier records 7000601744 and 7000058184)

Does this PR complete the issue, or what remains? It completes it. Both call paths named in the record use parse_semver, and the fail-before tests, resolution and pinning suites and the CHANGELOG line (under [Unreleased] Fixed) are done. The guard item was dropped by the 0.37 captain's scope prune (see Trade-offs).

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour. OpenAPM already requires SemVer 2.0.0; tests/spec_conformance/test_resolution_reqs.py (which already expects v01.2.3 to be rejected) stays green.

Co-authored-by: Copilot [email protected]

parse_semver now rejects leading zeros in numeric identifiers and
non-ASCII digits. The policy literal-tag check (v1.2.3) routes through
parse_semver instead of its own regex, so v01.2.3 and [email protected] are
no longer treated as exact releases by require_pinned_constraint.

Closes #3795

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 05:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the stricter SemVer parsing and shared policy validation changes.

No open issues 🎉

What changed in this PR

Fixes #3795 by tightening APM’s shared exact-version parser to reject leading zeros in numeric identifiers and non-ASCII digits, and routing policy literal-tag checks through that parser.

Changes:

  • Enforces SemVer numeric-identifier rules while preserving valid alphanumeric prerelease identifiers and build metadata.
  • Replaces the policy’s separate literal-tag regex with parse_semver.
  • Adds regression tests and documents the behavior change.
File Description
tests/​unit/​policy/​test_pinned_constraint.py Verifies malformed release tags classify as bare branches.
tests/​unit/​marketplace/​test_semver.py Tests invalid numeric identifiers and valid zero-containing forms.
src/​apm_cli/​policy/​_constraint_pinning.py Delegates v-prefixed release validation to parse_semver.
src/​apm_cli/​marketplace/​semver.py Tightens numeric and prerelease identifier grammar.
CHANGELOG.md Records stricter release recognition in resolution and pinning policy.

Review effort: Balanced

@danielmeppiel

Daniel Meppiel (danielmeppiel) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_with_followups

Exact SemVer now follows the 2.0.0 grammar everywhere: leading-zero and non-ASCII versions stop counting as pinned releases in resolution and require_pinned_constraint.

panel-mode=lean; personas=python-architect,test-coverage-expert,apm-ceo

cc Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

Both panelists agree on the core change. parse_semver is now the single owner of the exact-version grammar, and the policy check _LITERAL_TAG_RE delegates to it instead of keeping its own looser regex. That closes the gap in #3795, where v01.2.3 or [email protected] could pass require_pinned_constraint as an exact release. Fail-before evidence is solid: 15 new unit tests failed on main and pass on head. The 5807 unit tests and 359 real-binary lifecycle tests stay green. The 5 ssh transport tests that need a PyInstaller binary are an environment precondition, not a regression.

The highest-weight gap is the missing resolver trap. tag_pattern._VERSION_RX still accepts any digits (\d+), which makes parse_semver in git_semver_resolver the only filter. On head, iter_semver_tags([v01.9.0, v1.9.0-01, v1.0.0]) returns ['v1.0.0'], but nothing in the suite would notice if that filter regressed and ^1.0.0 started picking up a leading-zero tag. One parametrized case in TestIterSemverTags closes it and should land before merge.

python-architect is right that this is a user-visible behavior change, not just a fix. Tags like v1.02.0 drop out of candidate selection, outdated and drift checks, and malformed prereleases (v1.2.3-01, v1.2.3-rc..1) are now reported as bare branches. Users with such tags must be able to see this in the CHANGELOG. Reword the existing single Fixed line rather than add a second one.

Dissent. No real dissent. python-architect suggests tracking the dropped guard as a follow-up. The CEO sides with the maintainer prune ("no new guard unless trivial") and treats the guard as optional. The binding record's Done-when still lists a guard, so the deviation should be written down on the record.

Aligned with: Portable by manifest: a manifest pin now means the same strict SemVer 2.0.0 version in every code path. Secure by default: lookalike tags such as v01.2.3 can no longer pass as exact pins. Governed by policy: require_pinned_constraint uses the same parser as resolution. Pragmatic as npm: matches how node-semver treats leading zeros.

Panel summary

Persona B R N Takeaway
Python Architect 0 2 0 Owner routing is correct: _LITERAL_TAG_RE now delegates to parse_semver and the grammar matches SemVer 2.0.0. Remaining gaps: the dropped guard and the behavior-change wording.
Test Coverage Expert 0 1 0 Grammar and pinning repros are trapped at unit tier and pass. Resolver tag selection has no trap for leading-zero tags. No durable-state paths changed, so no lifecycle test needed.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.

Top 3 follow-ups

  1. [Test Coverage Expert] In-PR: add one parametrized case to TestIterSemverTags in tests/unit/deps/test_git_semver_resolver.py asserting that only v1.0.0 survives from [v01.9.0, v1.9.0-01, v1.0.0]. -- _VERSION_RX still accepts any digits, so parse_semver is the only thing stopping a leading-zero tag from being selected, and no test watches it.
  2. [Python Architect] In-PR: reword the single Fixed line as a behavior change (leading-zero, non-ASCII or malformed-prerelease tags are skipped by resolution and reported as bare branches by require_pinned_constraint). -- Users with tags like v1.02.0 will see different resolution results; that must be visible in the CHANGELOG.
  3. [Python Architect] Record the guard deviation on the [BUG] parse_semver accepts leading zeros and non-ASCII digits #3795 binding record (captain prune, 2026-11-19), not only in the PR body. Open a guard follow-up only if the maintainer wants one. -- The record's Done-when still lists a guard; writing the decision down keeps governance honest without adding scope.

Recommendation

Merge once the author adds the resolver regression test and the reworded single CHANGELOG line, both already planned for this PR. Neither needs a new review round. After that, the only open item is noting the guard deviation on the #3795 record, which is the maintainer's call.


Full per-persona findings

Python Architect

  • [recommended] Skipping the guard is defensible under the captain prune, but the record and the architecture rule both expect one. Record the deviation. at src/apm_cli/marketplace/semver.py:125
    Routing is correct and complete within scope: _is_literal_version_tag, _is_package_literal_tag and _is_package_at_release_tag all route through parse_semver with consistent lazy imports, and _PARTIAL_WILDCARD_RE is range syntax, correctly left alone. architecture.instructions.md says a split-authority fix is complete only with a regression test and a registered static guard; today the only protection is the parse_semver docstring line.
    Suggested: Do not add the guard in this PR. State the deviation and track it if the maintainer wants it.
  • [recommended] The stricter parser also changes resolution and policy results for real-world tags. Make sure the one CHANGELOG line says so. at src/apm_cli/policy/_constraint_pinning.py:277
    The grammar is correct against SemVer 2.0.0 (alphanumeric prerelease ids such as 0a stay valid; build metadata still allows +001; ASCII digits keep int() and _cmp_tuple safe). Tags such as v1.02.0 now drop out of git-semver selection, outdated and drift, and refs the old regex accepted (v1.2.3-01, v1.2.3--, v1.2.3-rc..1, v1.2.3+a+b) now classify as BARE_BRANCH.
    Suggested: Word the CHANGELOG entry as a behavior change.

Test Coverage Expert

  • [recommended] No resolver-level regression trap shows that ^1.0.0 ignores a v01.9.0 tag. at tests/unit/deps/test_git_semver_resolver.py:288
    The pinning promise is trapped through test_classify_non_semver_literal_tag_returns_bare_branch. The resolution promise is only trapped indirectly through parse_semver unit tests; tests/spec_conformance/test_resolution_reqs.py:714 checks a private regex copy, not the resolver. TestIterSemverTags::test_skips_non_semver_tags only covers release-candidate.
    Suggested: Add RemoteRef tags refs/tags/v01.9.0 and refs/tags/v1.9.0-01 next to refs/tags/v1.0.0 and assert only v1.0.0 comes back.
    Proof (missing at): tests/unit/deps/test_git_semver_resolver.py::TestIterSemverTags::test_skips_leading_zero_semver_tags -- proves: a range like ^1.0.0 never installs from a tag such as v01.9.0 or v1.9.0-01. [secure-by-default,portability-by-manifest]

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.

Folded in this run

  • (panel) Resolver regression trap TestIterSemverTags::test_skips_tags_outside_semver_grammar keeps only v1.0.0 from [v01.9.0, v1.9.0-01, v1.0.0]; fails with main's parser -- resolved in 647c2a6.
  • (panel) CHANGELOG line reworded as a behavior change (resolution skips non-SemVer tags; require_pinned_constraint reports them as bare branches) -- resolved in 647c2a6.

Copilot signals reviewed

  • Copilot review on b86e306: approval recommended, no inline findings.

Deferred (out-of-scope follow-ups)

Lint

ruff 0.15.12 check + format, pylint R0801, auth-signal lint and scripts/lint-architecture-boundaries.sh all clean at 647c2a6.

CI

gh pr checks 3894 at 647c2a6: 30 pass, 2 skipping, 0 failing (CI, Merge Gate, Spec conformance, CodeQL).


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

Fold panel findings on #3894: iter_semver_tags must skip v01.9.0 and
v1.9.0-01, and the CHANGELOG line names the resolution and policy
behavior change.

Co-authored-by: Copilot App <[email protected]>
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.

[BUG] parse_semver accepts leading zeros and non-ASCII digits

2 participants