Skip to content

[0.37] fix(audit): required-package matching follows source replacement (#4038) - #4069

Merged
Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-require-replace-4038
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-require-replace-4038

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

apm install and apm audit --ci disagreed on which package a dependencies.require entry identifies when the project has a root replace: rule.

Take a consumer that declares example-org/app, with a root replace: that selects fork-org/app-fork:

  • The install gate matched entries against the selected package (fork-org/app-fork), as req-rpl-002 requires.
  • apm audit --ci matched them against the original package (example-org/app).

Root cause: run_policy_checks (src/apm_cli/policy/policy_checks.py, the apm audit --ci path) passed the raw manifest.get_apm_dependencies() to the required-package checks.

  • The install gate passes ctx.deps_to_install, whose declarations are already projected through replace:.
  • The audit's allow/deny checks already received the projected set through _audit_source_dependencies. Only the required-package checks used the raw declarations.

Fix: run_policy_checks passes the production declarations through the same projection, project_manifest_dependencies. That function gains an include_dev switch, so the audit keeps its production-only scope.

  • As a result, required-packages, required-packages-deployed, required-package-version and required-executable-untrusted identify the selected package (req-rpl-002).
  • They read the lock entry under the original's lockfile identity (req-pl-018 clause (g)), through the existing RequiredPackageMatch key. For a bound reference, DependencyReference.get_unique_key() already returns the original's key, so there is no new key logic.
  • If the replacement table cannot bind, the existing dependency-sources failure path is used.
  • registry-source reads the same declaration list, so apm audit --ci now also judges registry_source against the selected source, as apm install does.

Unchanged: lockfile keys and format, replacement binding, install-gate behaviour, the production-only scope of the audit's require checks, allow/deny matching, and projects without replace:.

Issue and approved scope

Issue: Fixes #4038

Human scope-approval comment: #4038 (comment)

Stacked on #4008 (base danielmeppiel-scaling-potato at 3560d7f); it must merge after #4008. Because the base is stacked, CI does not run on this PR; local evidence is below. It does not overlap #4067 (#4037), which changes RequiredPackageMatch.locked_in and deps/lockfile.py.

Type of change

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

Testing

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

The new installed-CLI lifecycle test, tests/integration/policy/test_required_packages_source_replacement_lifecycle.py, sets up the following:

  • packages come from local bare repositories behind git URL rewrites;
  • the org policy (enforcement: block) is discovered through the project's origin remote and served on api.github.com by local_scripted_https_proxy;
  • the consumer declares example-org/app#v1.0.0 with a root replace: to fork-org/app-fork.

Each scenario runs apm install (the gate), then apm audit --ci:

  1. Original entry (example-org/app): fails required-packages in apm install, and fails required-packages and required-packages-deployed in apm audit --ci. This holds for a github.com fork, for a GHES fork, and for a GHES fork with the qualified github.com/example-org/app entry.
  2. Selected entry (fork-org/app-fork, or ghes.example.test/fork-org/app-fork): apm install passes, and apm audit --ci exits 0 with both presence checks passing. The lock entry stays keyed example-org/app and records the fork.
  3. Version pin on the selected entry (fork-org/app-fork#v9.9.9, require_resolution: block): a fresh apm install passes the gate, since there is no locked ref to compare yet. required-package-version in the audit then reports expected ref 'v9.9.9', got 'v1.0.0'.
  4. GHES fork: behaves the same with the host-qualified entry (covered by the GHES cases in 1 and 2).
  5. Control: without replace:, a require on the declared package passes both commands, and apm audit --ci exits 0.

The conformance test in tests/spec_conformance/test_policy_reqs.py, marked req("req-rpl-002", "req-pl-018"), runs run_policy_checks on disk: the selected entry passes both presence checks and the original entry fails both. CONFORMANCE.{md,json} are regenerated.

Red/green: with the src/ change reverted at 3560d7f, the 6 replacement lifecycle cases fail and the control passes. The conformance test fails with (False, False) == (True, True).

Command Result
APM_BINARY_PATH=.venv/bin/apm pytest -q -n 4 tests/integration/policy/test_required_packages_source_replacement_lifecycle.py 7 passed
Same, with the src/ change reverted (3560d7f) 6 failed, 1 passed (control)
pytest -q -p no:randomly tests/spec_conformance/test_policy_reqs.py -k follows_source_replacement passed (fails at 3560d7f)
pytest -q -n 8 tests/unit/policy tests/unit/models tests/unit/install tests/spec_conformance tests/integration/policy No failures beyond the base. The 256 failures are sandbox-only (test_unix_install_ownership, plus git signing in test_mode_b_detector, test_cache_pin and test_resolution_reqs), identical with the fix reverted, apart from the new conformance test
pytest -q -n 8 tests/unit -k "doc or guide or changelog or governance" 479 passed
ruff check / ruff format --check / pylint R0801 / lint-auth-signals.sh / lint_architecture_boundaries.py --root . clean
python -m tests.spec_conformance.orphan_check OK, 148 requirements aligned

Changelog

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

Spec conformance (OpenAPM v0.1)

  • Spec edit: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

There is no spec text change. req-rpl-002 already says policy matching "evaluates the selected package, not the original package". req-pl-018 clause (g), as revised by #4008 (0.1.67), already requires reading the lock entry by the dependency's own lockfile identity.

Docs re-home after #3996

The docs are in the last commit, by themselves.

Topic Page
dependencies.require and source replacement docs/src/content/docs/reference/policy-schema.md
Same, for agents packages/apm-guide/.apm/skills/apm-usage/governance.md

`apm audit --ci` (run_policy_checks) passed the raw manifest declarations
to the required-package checks, while the install gate passes the
declarations projected through the manifest `replace:` rules. The two
commands therefore matched `dependencies.require` entries against
different packages when a root replacement was present.

run_policy_checks now passes the production declarations through the
same projection (project_manifest_dependencies, which gains an
include_dev switch), so required-packages, required-packages-deployed,
required-package-version and required-executable-untrusted identify the
selected package (req-rpl-002) and read the lock entry under the
original's lockfile identity (req-pl-018 clause (g)) through the existing
RequiredPackageMatch key. Projects without `replace:` are unchanged.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Base automatically changed from danielmeppiel-scaling-potato to main November 20, 2026 06:40
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit caf117c into main Nov 20, 2026
5 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-require-replace-4038 branch November 20, 2026 06:41
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
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.

dependencies.require identity ignores source replacement (req-rpl-002)

1 participant