Repository navigation
fix(lockfile): refuse lockfiles written by a newer APM (#2334) - #3916
Conversation
An APM older than the lockfile's apm_version now refuses install (incl. --frozen, --dry-run, -g), update, uninstall, prune, lock, audit and pack before any change, naming both versions and the upgrade routes. Read-only commands warn once. Duplicate or invalid apm_version fails closed. The LockFile.write backstop (via ensure_lockfile_writable) leaves the bytes untouched on every other write path. Version precedence routes through version_checker.release_precedence, which now orders aN/bN/rcN numerically and ignores +build metadata. apm-spec-waiver: apm_version generator compatibility is APM CLI policy; OpenAPM lockfile schema and req-lk-005 equivalence are unchanged (#2334) Co-authored-by: Copilot App <[email protected]>
…nielmeppiel-cli-version-gate-2334
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The lockfile safety gate mishandles supported YAML layouts, legacy lockfiles, and explicit null metadata, allowing unsafe overwrites or mutation before refusal.
5 open issues
What changed in this PR
This PR addresses #2334 by adding a shared compatibility gate intended to prevent older APM versions from rewriting or relying on lockfiles generated by newer versions.
Changes:
- Adds command preflight checks, a lockfile-write backstop, and warnings for read-only access.
- Centralizes release ordering, including numeric prerelease comparison and ignored build metadata.
- Adds regression tests, an architecture guard, and upgrade guidance.
| File | Description |
|---|---|
tests/unit/test_version_checker.py |
Tests prerelease ordering and build metadata. |
tests/unit/test_self_entry_caller_guards.py |
Uses valid generator-version fixtures. |
tests/unit/test_lockfile_self_entry.py |
Uses valid generator-version fixtures. |
tests/unit/test_deps_list_tree_info.py |
Updates lockfile fixtures to valid versions. |
tests/unit/scripts/test_architecture_runner.py |
Registers the new guard in the expected inventory. |
tests/unit/install/test_plan.py |
Updates the generator-version fixture. |
tests/unit/install/test_mcp_only_lockfile_creation.py |
Uses valid versions in persistence fixtures. |
tests/unit/install/test_frozen.py |
Updates the frozen-install fixture version. |
tests/unit/deps/test_lockfile_generator.py |
Tests compatibility decisions and write protection. |
tests/spec_conformance/test_lockfile_reqs.py |
Updates a conformance fixture version. |
tests/integration/test_lockfile_generator_gate_lifecycle.py |
Exercises CLI refusal, warnings, and preservation. |
tests/integration/test_architecture_owner_rule_mutations.py |
Tests detection of a removed gate call. |
src/apm_cli/utils/version_checker.py |
Adds shared numeric release precedence. |
src/apm_cli/install/errors.py |
Preserves compatibility-error guidance. |
src/apm_cli/deps/lockfile.py |
Adds write protection and read warnings. |
src/apm_cli/deps/lockfile_generator.py |
Owns generator compatibility checks and diagnostics. |
src/apm_cli/commands/update.py |
Adds update preflight protection. |
src/apm_cli/commands/uninstall/cli.py |
Checks the selected scope before uninstall. |
src/apm_cli/commands/prune.py |
Adds prune preflight protection. |
src/apm_cli/commands/pack.py |
Checks compatibility before packing. |
src/apm_cli/commands/lock.py |
Checks compatibility before lock generation. |
src/apm_cli/commands/install.py |
Adds project and user-scope install checks. |
src/apm_cli/commands/deps/cli.py |
Guards deprecated dependency updates. |
src/apm_cli/commands/audit.py |
Guards lockfile-backed audits. |
scripts/architecture_linter/groups/contracts_tests.py |
Loads the compatibility routing guard. |
scripts/architecture_linter/checks/contracts_lockfile_generator.py |
Checks routing through the canonical owner. |
packages/apm-guide/.apm/skills/apm-usage/troubleshooting.md |
Adds refusal remedies and upgrade guidance. |
packages/apm-guide/.apm/skills/apm-usage/commands.md |
Documents affected commands and warning behavior. |
docs/src/content/docs/troubleshooting/install-failures.md |
Explains compatibility failures and recovery. |
docs/src/content/docs/reference/lockfile-spec.md |
Documents generator-version enforcement. |
CHANGELOG.md |
Records the user-visible compatibility protection. |
.apm/architecture/owners/contracts-tooling.json |
Registers the compatibility owner and guard. |
Review effort: Balanced
… pack --json Addresses the five Copilot inline threads on #3916: - an explicitly null or empty apm_version now fails closed instead of passing as absent; - the generator is read from the composed top-level mapping (yaml_io.top_level_scalar_values), so flow-style, indented and duplicate keys are all seen without object construction; - check_current_cli resolves a legacy apm.lock read-only, before install would rename it; - apm pack --json returns its JSON failure envelope for the refusal; - lifecycle controls derive equal/build/prerelease generators from the full running version so prerelease binaries validate. Co-authored-by: Copilot <[email protected]>
Addresses the lean-panel follow-ups on #3916: - python-architect: installed_apm_version (the stamp) delegates to version.get_version so stamp and gate share one owner, and a CLI always accepts its own stamp; LockfileRefusal marks canonical refusals so the install, MCP and LSP render sites show the upgrade guidance verbatim. - supply-chain-security-expert: the LockFile.write backstop no longer claims nothing changed, and refuses a lockfile whose generator cannot be read (unparseable YAML or a top-level merge key). - test-coverage-expert: lifecycle covers install --dry-run, bare audit and deps update, asserts no generic install prefix, and a dependency failure control; unit test for the canonical install message. Co-authored-by: Copilot <[email protected]>
…nielmeppiel-cli-version-gate-2334 # Conflicts: # src/apm_cli/commands/update.py
Co-authored-by: Copilot <[email protected]>
…3916) Addresses the apm-ceo note: older CLIs predate the gate, so the protection applies from this release on. Co-authored-by: Copilot <[email protected]>
APM Review Panel:
|
| Persona | B | R | N | Takeaway |
|---|---|---|---|---|
| Supply Chain Security Expert | 0 | 2 | 0 | The gate fails closed on the right axes; the backstop wording and the unparseable-overwrite path needed fixing. |
| Python Architect | 0 | 3 | 0 | The single-owner gate is well placed; the version source and refusal rendering needed one owner each. |
| Test Coverage Expert | 0 | 3 | 0 | Lifecycle and unit gates pass; dry-run, bare audit, deps update and the dependency-failure distinction needed tests. |
B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.
Top 1 follow-ups
- [Python Architect] Define a certify/replay primitive taxonomy for commands that consume the lockfile, then add an inverse static guard that flags any command using those primitives without the version gate. -- Writes are covered by
LockFile.write, but a future read-and-certify command could skip the gate without any guard flagging it.
Recommendation
Merge once CI is green at 2be5b70. The scope contract is met, every legitimate thread is folded with mutation-proven tests, and the CHANGELOG line now says the protection starts with this release (older CLIs predate it). File the inverse-guard taxonomy as a tracked follow-up linked from this PR.
Full per-persona findings
Supply Chain Security Expert
- [recommended] The LockFile.write backstop said "Nothing was changed." even when files might already have been written, at
src/apm_cli/deps/lockfile_generator.py
A false claim of no side effects hides a provenance gap. Folded: backstop-specific wording, unit-tested. - [recommended] The gate let writes through when the existing lockfile could not be parsed, and LockFile.write then overwrote it, at
src/apm_cli/deps/lockfile_generator.py
A silent fallback when a check cannot run. Folded: the backstop refuses unreadable YAML and top-level merge keys (mutation-proven).
Python Architect
- [recommended] Two functions answered "which APM is this", at
src/apm_cli/deps/lockfile_generator.py
A split between stamp and compare. Folded:installed_apm_versiondelegates toget_version, and a CLI always accepts its own stamp. - [recommended] ensure_lockfile_writable raises two error types, but render sites special-cased only the symlink error, at
src/apm_cli/deps/lockfile.py
Folded: theLockfileRefusalmarker is routed through all four render sites. - [recommended] The static guard cannot flag a new certifying command that skips the gate, at
scripts/architecture_linter/checks/contracts_lockfile_generator.py
Deferred: an inverse guard needs a certify/replay taxonomy that does not exist yet, and writes are structurally backstopped.
Test Coverage Expert
- [recommended] install --dry-run and bare audit were not in the refusal matrix, at
tests/integration/test_lockfile_generator_gate_lifecycle.py
Proof (passed):tests/integration/test_lockfile_generator_gate_lifecycle.py::test_older_cli_refuses_every_lockfile_command_without_mutation-- proves: previewing an install or a plain audit with an older APM refuses and leaves every file byte-identical. - [recommended] Nothing proved that dependency failures stay distinct from the refusal, at
tests/unit/install/test_errors.py
Proof (passed):tests/integration/test_lockfile_generator_gate_lifecycle.py::test_dependency_failure_is_not_a_version_refusal-- proves: a real dependency failure never reads as a version refusal. - [recommended] The deprecated
apm deps updategate had no test, attests/integration/test_lockfile_generator_gate_lifecycle.py
Folded into the refusal matrix.
This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.
Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.
* docs: reground pages freed by #3918 and leftover 0.37 drift Refs #3948. Wave 2 of the 0.37 docs sweep (docs-corpus-audit): the pages #3918 owned (concepts, install-packages, enterprise policy and security, mcp-as-primitive, reference update/manifest-schema, apm-usage dependencies/governance) plus reference/cli/deps.md (#3916 refusal). Pages owned by in-flight #3290 are not touched. Co-authored-by: Copilot App <[email protected]> * docs(security): scope dependency-confusion and rebound claims to code The new threat-model row credited #3895 with general dependency-confusion protection, but it only catches one install graph declaring the same owner/repo from two hosts; bare shorthand still resolves to the default host. The rebound sentence also omitted lock rows without a recorded origin, local marketplaces and in-repo moves, which the check skips. Addresses supply-chain-security-expert panel findings and Copilot inline on docs/src/content/docs/enterprise/security.md:323. Co-authored-by: Copilot <[email protected]> * docs(policy): fix skill CLI samples and allow-list wording The apm-usage governance CLI samples used header and footer lines the CLI never prints and a dry-run overflow that cannot follow two lines (the preview limit is 5); align them with policy-reference. Section 3 still described a repo override in the discovered chain, and the allowlist rows said 'non-empty', contradicting null vs [] semantics. Addresses doc-writer and supply-chain-security-expert panel findings. Co-authored-by: Copilot <[email protected]> * docs(install): qualify marketplace replay and default_host wording Locked marketplace entries replay offline except for a package whose locked MCP or LSP servers have no apm_modules copy; that case reads the marketplace even under --frozen (drift.py catalog-metadata). Also tie default_host to the shorthand rather than #ref and link the newer lockfile troubleshooting section. Addresses Copilot inline on reference/manifest-schema.md:701 and doc-writer panel nit on consumer/install-packages.md:64. Co-authored-by: Copilot <[email protected]> * test(docs): track host-qualified policy wording in security.md security.md no longer says policy patterns are host-blind or cannot restrict host identity: dependencies.allow/deny accept host-qualified patterns (policy-reference). The docs-contract test still pinned the old wording and failed Build & Test Shard 1. Keep its intent (no transport-aware policy claim) and assert the scheme-blind sentence and the host-qualified link instead. Addresses CI failure in tests/unit/test_tls_docs_scope.py:127. Co-authored-by: Copilot <[email protected]> * docs(security): name the manifest whose default_host applies Each parsed manifest, transitive ones included, resolves its own bare owner/repo dependencies against its own default_host, so a root default_host does not pin the whole graph. Say so in the dependency-confusion row and point at host-qualified allow patterns as the control. Addresses supply-chain-security-expert delta panel finding on docs/src/content/docs/enterprise/security.md:635. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: danielmeppiel <[email protected]> Co-authored-by: Copilot App <[email protected]>


Description
APM 0.24.0 could silently rewrite a lockfile generated by 0.25.0. I reproduced it on main (0.36.1) with the real CLI. With
apm_version: 0.99.0inapm.lock.yamland a dependency content change,apm installsucceeded, restamped the file as0.36.1, and dropped an unknown top-level field a newer APM had recorded.Now an APM older than the lockfile's
apm_versionrefuses before changing anything:install(incl.--frozen,--dry-run,-g),update,uninstall,prune,lock,audit(lockfile-backed modes),pack, deprecateddeps updateLockFile.writepath (MCP/LSP lock writers, bundles), via the existingensure_lockfile_writableownerdeps list/tree,outdated,view,compile, ...Version rules:
utils/version_checker.release_precedence.aN < bN < rcN < release, numerically, soa9 < a10.+buildis ignored.is_newer_versionnow uses it too, which fixes its old lexicographica10 < a9.apm_versionor the APM-writtenunknownis accepted.apm_versionfails closed, with a restore-from-VCS remedy.Canonical owner:
deps/lockfile_generator.py, registered as owner recordlockfile-generator-compatibility. Static guard:contracts-tooling-lockfile-generator-gate, which asserts every gate site and the write and read hooks route through the owner. It has a mutation case.Issue and approved scope
Issue: Fixes #2334
Human scope-approval comment: #2334 (comment) (apm-scope:v1). Daniel Meppiel (@danielmeppiel) approved the design, including no override, via the 0.37 issue-triage coordinator.
This PR completes the bounded scope. Out of scope: override flags, automatic CLI install or switching, project CLI pinning, lockfile schema changes.
Type of change
Testing
tests/integration/test_lockfile_generator_gate_lifecycle.pyuses the installed CLI and covers:apm.yml, the lockfile and deployed files stay byte-identical+build, own-prerelease, older andunknowngenerators continue and are restamped-gis guardedWith the fix removed, 4 of the 9 cases fail; the 5 continue cases are preservation controls.
APM_BINARY_PATH=.venv/bin/apm pytest tests/integration/test_lockfile_generator_gate_lifecycle.py tests/unit/deps/test_lockfile_generator.py tests/unit/test_version_checker.py tests/unit/scripts/test_architecture_runner.py tests/integration/test_architecture_owner_rule_mutations.py(after merging main)pytest tests/unit tests/test_console.py tests/spec_conformance -n 8test_is_tool_available. All other failures pass when rerun serially. The diff does not touchinstall.shuvx [email protected] checkandformat --checkon the CI paths; pylint R0801;bash scripts/lint-auth-signals.sh;bash scripts/lint-architecture-boundaries.shbash tests/spec_conformance/mode_b_detector.shTest fixtures that stamped non-APM versions (
0.0.0-test) now use valid old versions (0.0.0).Changelog
## [Unreleased]inCHANGELOG.md: user-visibleeffect first, 25 words or fewer, ending with
(#PR); see.github/instructions/changelog.instructions.md.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regeneratedapm_versiongenerator compatibility is APM CLI policy; the lockfile schema and req-lk-005 equivalence are unchanged.