Skip to content

[0.38] fix(registry): lockfile replay tolerates a mirror URL when the hash matches (#4013) - #4022

Merged
Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
danielmeppiel-registry-mirror-4013
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
danielmeppiel-registry-mirror-4013

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

Registry lockfile replay now accepts a moved or mirrored registry endpoint when the bytes match the locked hash, as OpenAPM req-rs-009 requires. Previously, download_from_lockfile failed with "lockfile registry URL does not match the configured registry endpoint" before it checked the hash. Pointing a registry name at a mirror or a new URL therefore broke every replay of that registry's dependencies.

In deps/registry/resolver.py download_from_lockfile:

  • Bytes always come from the archive URL of the dependency's configured registry. APM never fetches the locked resolved_url, so credentials only go to a host the user configured. fetch_from_url still enforces the configured origin and base path.
  • When the configured archive URL differs from the locked one, the bytes are checked against resolved_hash before the install target is touched.
    • Match: the install succeeds and prints one [i] line naming both URLs. resolved_url and resolved_hash in apm.lock.yaml are not rewritten.
    • Mismatch: the install fails closed per req-lk-013. The error names the package, both URLs, and the expected and actual hashes. Nothing is extracted.
  • When the URLs are equal, replay is unchanged.

--frozen fix (needed for "--frozen behaves the same"). In a frozen install, a package that failed to download was masked by the later hydrated-MCP drift check ("restored MCP declarations differ ... package manifest not found"). The hash-mismatch cause never reached the user. The new InstallService.enforce_frozen_restored reports restore failures with their real cause before the MCP check runs. It lives in InstallService, the owner of frozen decisions (architecture guard install-deployment-frozen-mutation-eligibility), and is called from install/phases/integrate.py. This touches neither install/phases/resolve.py, install/sources.py nor deps/registry/auth.py (#4009's seam).

Issue and approved scope

Issue: Fixes #4013

Human scope-approval comment: #4013 (comment)

This PR completes the issue. Out of scope per the record: policy-declared mirrors, fetching from a lockfile URL that no configured registry owns, registry-routed marketplace replay (#4009), lockfile schema changes, and apm update re-resolution.

Rebase onto 0.37.0: composition with #4278 (decision log)

While this PR was waiting, main landed #4278 (280e7d23d). Replay now accepts the locked resolved_url when any registry declared in apm.yml serves it, and refuses otherwise. #4013's scenario still failed on main: the same registry name rebound to endpoint B, so A is no longer declared. It failed with "lockfile registry URL does not match any registry endpoint declared in apm.yml".

Decision (S15 default, logged here): compose the two behaviours rather than replace either.

Reasoning: req-rs-009 says a declared registry may satisfy the fetch and that a URL mismatch "MUST NOT fail the install when the hash matches". The #4013 record requires exactly this outcome.

One test outcome changed. Main's test_rejects_lockfile_registry_origin_override (attacker.example locked URL) becomes test_foreign_locked_url_is_never_fetched. Its security assertion is kept: the attacker URL is never fetched, and only the configured registry is contacted with its own credentials. The expected outcome changes from "refused before any fetch" to "configured bytes fetched, then fail closed on the hash". If Daniel prefers main's refuse-before-fetch posture for an undeclared locked URL, revert that branch in _lockfile_replay_client and this PR reduces to enforce_frozen_restored.

Validation on 6cafcf82:

  • ruff, pylint R0801, lint-auth-signals.sh, lint_architecture_boundaries.py --root ., and the file guards are clean.
  • The full test_architecture_owner_rule_mutations.py run: 1246 passed.
  • orphan_check is OK (149).
  • Targeted tests (registry, install, resolution conformance, both mirror lifecycle tests): no failure beyond the environment. 249 failures (test_unix_install_ownership, gpg signing, two install unit tests) fail identically on pristine main in this sandbox.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass (see the note below)
  • Added tests for new functionality (if applicable)

Lifecycle test tests/integration/test_registry_mirror_replay_lifecycle.py runs the installed CLI against two local registry endpoints. The steps:

  1. Install from A.
  2. Replay from A with an equal URL: no mirror line.
  3. Reconfigure the same registry name to B and remove apm_modules. Plain install and install --frozen both succeed with identical deployed bytes, an unchanged apm.lock.yaml, and the [i] line, with zero requests to A.
  4. B serves different bytes. Plain and --frozen both fail closed with both URLs and both hashes, no request to A, no file written, and no extraction.

The test was red on main at step 3, then at step 6 (the --frozen mismatch) until the frozen fix.

Conformance (req-rs-009, req-lk-013) in tests/spec_conformance/test_resolution_reqs.py: a mismatched locked URL is satisfied by the configured registry when the hash matches, and fails closed before extraction when it does not. Unit tests in tests/unit/registry/test_resolver.py: the old "reject origin override" test becomes "a foreign locked URL is never fetched", plus moved-endpoint success that keeps the locked URL and hash.

Command Result
APM_BINARY_PATH=.venv/bin/apm uv run --extra dev pytest -q tests/integration/test_registry_mirror_replay_lifecycle.py 1 passed
APM_BINARY_PATH=.venv/bin/apm uv run --extra dev pytest -q -n 4 tests/integration/test_frozen_remote_plugin_lifecycle.py tests/integration/test_frozen_cold_cache_apm_package_e2e.py tests/integration/test_auth_upgrade_lock_replay_lifecycle.py tests/integration/test_registry_redirect_location_hardening_lifecycle.py tests/integration/test_deps_registry_coverage.py tests/integration/test_registry_client_integration.py tests/unit/integration/test_frozen_mcp_evidence.py tests/unit/registry tests/unit/install/test_resolve_registry.py 495 passed, 39 skipped, 2 xfailed
uv run --extra dev pytest -q tests/spec_conformance/test_resolution_reqs.py tests/spec_conformance/test_lockfile_reqs.py all pass except test_repository_identity_survives_full_install_and_persistent_cache, which fails identically on main in this sandbox (commit signing)
uv run --extra dev pytest -q -n auto tests/unit/registry tests/unit/install tests/unit/deps Only sandbox-environment failures remain (test_unix_install_ownership, git bare-cache tests, test_cache_pin, test_persistent_cache_ssh_transport errors). They are identical on main here; CI is authoritative.
uv run --extra dev python -m tests.spec_conformance.orphan_check OK (148 aligned)
ruff check / ruff format --check / pylint R0801 / lint-auth-signals.sh / lint-architecture-boundaries.sh clean

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)

This PR brings the implementation into line with the existing req-rs-009 and req-lk-013 (Mode A: code fixed, spec unchanged). There is no spec edit, so the manifest and anchors are unchanged. New @pytest.mark.req("req-rs-009") and req-lk-013 tests cite them, and CONFORMANCE.{md,json} is regenerated.

  • 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.

Docs re-home after #3996

All docs/** and packages/apm-guide/** changes are in the last commit (docs: ...), written against the current main layout:

Current page (this PR) Change Target in #3996 layout
guides/registries.md, section 4 "What gets recorded in the lockfile" Replaces the stale "resolved_url is the trust anchor" paragraph: resolved_hash is the anchor, replay fetches from the configured registry, and a new "Moved registries and mirrors" paragraph reference/registries.md, same section 4 (the page moves unchanged)
reference/lockfile-spec.md resolved_url field row now says advisory on replay Same page (kept)
packages/apm-guide/.apm/skills/apm-usage/dependencies.md New replay paragraph after the registry lockfile fields Same file (kept)

…sh matches (#4013)

Builds on the declared-mirror replay from #4278. When a registry declared
in apm.yml serves the locked resolved_url, replay fetches it there,
unchanged. When none does (the same registry name now points at a moved
endpoint), the locked URL is never contacted: replay fetches the locked
version from the dependency's configured registry and verifies the bytes
against resolved_hash before the install target is touched (req-rs-009,
req-lk-013). A match prints one [i] line naming both URLs and keeps the
lockfile unchanged; a mismatch fails closed naming the package, both URLs
and both hashes. Credentials still reach only each registry's own URL.

A frozen install whose locked dependencies could not be restored now
reports their cause through InstallService.enforce_frozen_restored
instead of the MCP drift their missing manifests implied.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-registry-mirror-4013 branch from 6cafcf8 to fa08b53 Compare November 21, 2026 08:26
@danielmeppiel
Daniel Meppiel (danielmeppiel) marked this pull request as ready for review November 21, 2026 08:39
Copilot AI balanced review requested due to automatic review settings November 21, 2026 08:39
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit e37b0e7 into main Nov 21, 2026
45 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-registry-mirror-4013 branch November 21, 2026 08:40

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

Moved-registry replay can break cold audits, rewrite lockfiles, fail on unrelated registry configuration, and expose URL credentials, while acquisition failures can be masked during MCP validation.

6 open issues
What changed in this PR

This PR fixes registry lockfile replay after an endpoint moves, using the locked content hash to verify archives from the dependency's configured registry while preserving existing declared-mirror support.

Changes:

  • Adds a hash-verified fallback for moved endpoints without fetching an unconfigured locked URL.
  • Adds a frozen-install restore-failure guard before hydrated MCP checks.
  • Adds regression coverage and updates registry documentation, conformance records, and the changelog.
File Description
tests/​unit/​registry/​test_resolver.py Tests moved-endpoint replay and avoidance of foreign locked URLs.
tests/​spec_conformance/​test_resolution_reqs.py Adds hash-match and mismatch cases for registry replay requirements.
tests/​integration/​test_registry_mirror_replay_lifecycle.py Exercises plain and frozen replay against two local endpoints.
src/​apm_cli/​install/​service.py Adds the frozen restore-failure guard.
src/​apm_cli/​install/​phases/​integrate.py Calls the restore guard before frozen MCP checks.
src/​apm_cli/​deps/​registry/​resolver.py Adds configured-endpoint fallback, pre-extraction verification, and diagnostics.
packages/​apm-guide/​.apm/​skills/​apm-usage/​dependencies.md Documents moved-endpoint and mirror replay behavior.
docs/​src/​content/​docs/​reference/​registries.md Describes hash-based trust and endpoint migration.
docs/​src/​content/​docs/​reference/​lockfile-spec.md Clarifies the advisory role of resolved_url.
CONFORMANCE.md Updates registry replay and integrity coverage counts.
CONFORMANCE.json Records the added conformance tests.
CHANGELOG.md Adds the user-visible registry replay fix.

Review effort: Balanced

Runs before the install target is touched, so a mismatch leaves nothing
extracted (req-lk-013).
"""
try:
Comment on lines +414 to +415
client = self._build_client(locked_name, locked_url)
return client, client.archive_url(owner, repo, version)
Comment on lines +414 to +415
client = self._build_client(locked_name, locked_url)
return client, client.archive_url(owner, repo, version)
Comment on lines +446 to +449
if fetch_url != resolved_url:
_verify_moved_archive(
dep_ref, version, archive_bytes, resolved_hash, resolved_url, fetch_url
)
from apm_cli.install.template import effective_allow_map
from apm_cli.security.trust import McpAdmission

InstallService.enforce_frozen_restored(ctx.transitive_failures)
`resolved_hash` is the trust anchor for re-installs. On `apm install` and `apm install --frozen`, APM fetches the locked version through a registry declared in `apm.yml`, sending each registry's credentials only to its own URL, and verifies the bytes against `resolved_hash`. A hash mismatch aborts the install before extraction, and `apm update` re-resolves and records a new URL.

**Moved registries and mirrors.** If another declared registry serves the locked `resolved_url` (a mirror), APM fetches from it. If no declared registry serves it (you pointed the registry name at a new URL), APM never contacts the old URL: it fetches the locked version from the configured registry, prints one `[i]` line naming both URLs, and keeps the lockfile unchanged when the hash matches. A mismatch fails the install and names the package, both URLs and both hashes.

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.

Registry replay rejects a moved or mirrored endpoint before checking the hash (req-rs-009)

2 participants