Skip to content

fix(lockfile): refuse lockfiles written by a newer APM (#2334) - #3916

Merged
Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-cli-version-gate-2334
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-cli-version-gate-2334

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

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.0 in apm.lock.yaml and a dependency content change, apm install succeeded, restamped the file as 0.36.1, and dropped an unknown top-level field a newer APM had recorded.

Now an APM older than the lockfile's apm_version refuses before changing anything:

[x] Refusing to run 'apm install' on apm.lock.yaml: it was written by APM 0.37.0, newer than this APM 0.36.1.
An older APM can drop or misread what a newer one recorded. Nothing was changed.
Install APM 0.37.0 or newer. Upgrade, then retry:
  apm self-update                            (installer script)
  brew upgrade apm                           (Homebrew)
  python3 -m pip install --upgrade apm-cli   (pip)
Behaviour Commands
Refuse before any write install (incl. --frozen, --dry-run, -g), update, uninstall, prune, lock, audit (lockfile-backed modes), pack, deprecated deps update
Refuse at write time (backstop) every other LockFile.write path (MCP/LSP lock writers, bundles), via the existing ensure_lockfile_writable owner
Warn once, continue read-only reads: deps list/tree, outdated, view, compile, ...

Version rules:

  • Comparison uses APM's release grammar through utils/version_checker.release_precedence. aN < bN < rcN < release, numerically, so a9 < a10. +build is ignored. is_newer_version now uses it too, which fixes its old lexicographic a10 < a9.
  • A missing apm_version or the APM-written unknown is accepted.
  • A duplicate or invalid apm_version fails closed, with a restore-from-VCS remedy.
  • A running CLI that reports an uncomparable version refuses.
  • There is no override flag or env var. That is Daniel Meppiel (@danielmeppiel)'s decision, consistent with the no-automatic-CLI-version-management decision.

Canonical owner: deps/lockfile_generator.py, registered as owner record lockfile-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

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

Testing

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

tests/integration/test_lockfile_generator_gate_lifecycle.py uses the installed CLI and covers:

  • the newer prerelease generator refuses all 8 commands, and apm.yml, the lockfile and deployed files stay byte-identical
  • read-only commands warn
  • equal, +build, own-prerelease, older and unknown generators continue and are restamped
  • duplicate and invalid values fail closed
  • -g is guarded

With the fix removed, 4 of the 9 cases fail; the 5 continue cases are preservation controls.

Command Result
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) 146 passed
pytest tests/unit tests/test_console.py tests/spec_conformance -n 8 28820 passed. The remaining failures are shell-installer subprocess timeouts under host load average about 100 and an environment-dependent test_is_tool_available. All other failures pass when rerun serially. The diff does not touch install.sh
uvx [email protected] check and format --check on the CI paths; pylint R0801; bash scripts/lint-auth-signals.sh; bash scripts/lint-architecture-boundaries.sh clean
bash tests/spec_conformance/mode_b_detector.sh waived (commit trailer): APM CLI policy, OpenAPM schema and req-lk-005 unchanged

Test fixtures that stamped non-APM versions (0.0.0-test) now use valid old versions (0.0.0).

Changelog

  • One line under ## [Unreleased] in CHANGELOG.md: user-visible
    effect first, 25 words or fewer, ending with (#PR); see
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

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. apm_version generator compatibility is APM CLI policy; the lockfile schema and req-lk-005 equivalence are unchanged.

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]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 19, 2026
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:09

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.

🟡 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

Comment thread src/apm_cli/deps/lockfile_generator.py Resolved
Comment thread src/apm_cli/deps/lockfile_generator.py Resolved
Comment thread src/apm_cli/deps/lockfile_generator.py Resolved
Comment thread src/apm_cli/commands/pack.py Resolved
… 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
…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]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_with_followups

An older APM now refuses to rewrite, replay, audit or pack a lockfile written by a newer APM. This closes a silent data-loss path, reproduced on main, where an unknown lockfile field was dropped.

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

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

This PR delivers the #2334 apm-scope:v1 contract as written. These commands stop before any change, naming both versions and the upgrade routes:

  • install (including --frozen, --dry-run and -g)
  • update, uninstall, prune and lock
  • audit
  • pack (including its --json envelope)
  • deps update

LockFile.write is the structural backstop for any path the entry gates miss. Read-only commands warn. A missing or unknown apm_version is accepted, and a duplicate, invalid or null value fails closed. There is no override flag, by the maintainer's explicit decision.

The panel agreed on the shape. All five Copilot threads and seven of the eight panelist recommendations are folded with mutation-proven traps. The highest-signal folds:

  • Supply chain, backstop wording: the backstop no longer claims "Nothing was changed" after earlier writes may have happened.
  • Supply chain, fail-open route closed: the backstop refuses to overwrite unreadable YAML, or a top-level << merge key that could hide apm_version.
  • Architect, one version owner: the stamp (installed_apm_version) now delegates to version.get_version, so the CLI never refuses its own stamp.
  • Architect, consistent rendering: a LockfileRefusal marker makes install, MCP, LSP and apm mcp show the refusal verbatim, not as a dependency failure.
  • Test coverage: the refusal matrix gained --dry-run, plain audit and deps update, a dependency-failure control case, and an unprefixed-message assertion.

Dissent. None on severity. I accept the one defer, an inverse route guard: every write is already backstopped by LockFile.write, and the certify/replay taxonomy that guard needs does not exist yet. The residual read-side gap is tracked below.

Aligned with: Portable by manifest: an older APM can no longer silently drop fields from or downgrade a newer lockfile. Secure by default: there is no override, the gate runs before any change, invalid values are refused, and the backstop refuses unreadable files and merge-key hiding. Governed by policy: audit and --frozen replay refuse to certify a lockfile they cannot fully understand. Pragmatic as npm: like npm's lockfileVersion, the refusal names both versions and one-command upgrade routes.

Panel summary

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

  1. [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_version delegates to get_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: the LockfileRefusal marker 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 update gate had no test, at tests/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.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit cb6ec19 into main Nov 19, 2026
38 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-cli-version-gate-2334 branch November 19, 2026 12:21
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 19, 2026
* 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]>
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.

Fail closed when CLI is older than lockfile apm_version

2 participants