Skip to content

fix(deps): include a non-default port in the dependency identity key - #3932

Merged
Daniel Meppiel (danielmeppiel) merged 9 commits into
mainfrom
danielmeppiel-fix-3927-port-identity-key
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 9 commits into
mainfrom
danielmeppiel-fix-3927-port-identity-key

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

Root cause 1 of #3927: build_dependency_unique_key ignored the port, so https://github.com:8443/acme/tool.git and acme/tool (and gitlab.example.com:8443/... vs gitlab.example.com/...) shared one key. Lock entries, dedup, the #3696 slot check and approval keys treated two servers as one package; the second declaration was silently dropped.

  • models/dependency/identity.py (the identity owner): a non-default port qualifies the host segment, host:port/owner/repo, on github.com too. Port-less keys and the bare default-host form are unchanged, so existing lockfiles keep their keys. Scheme-default ports are already dropped at parse, and host case-folding is unchanged.
  • DependencyReference.get_unique_key, LockedDependency.get_unique_key (port is already persisted in the lockfile) and the install/plan.py fallback pass port.
  • Consumers now agree: MaterializationSlots ([BUG] The same owner/repo from two hosts collides in apm_modules with "Path already has a replacement in progress" #3696) sees two identities on one apm_modules directory and rejects the pair before fetch, naming both (github.com/acme/tool and github.com:8443/acme/tool are different packages ...); alias: installs both. Approval grants follow the ported key (fail closed). canonical_exec_identity (deny matching) drops the port, like dependency-policy host matching, so a deny cannot be bypassed by naming another port. Uninstall/audit/deps matching already route through get_unique_key / get_identity.
  • Architecture: extended the contracts-tooling-dependency-identity guard so the builder must handle port and both identity models must pass port=self.port; three mutation tests prove its teeth.
  • Port-blind deny key is owned by identity.py (build_port_blind_dependency_key); the guard pins it and its canonical_exec_identity caller.
  • Docs: lockfile-spec key section, apm-guide dependencies.md (it stated the old "one lockfile key" behavior), policy-schema and apm-guide governance.md (deny ignores the port; allow/recommend/require name the ported key).

Behavior note for review: two tests encoded the old "port is a transport detail" contract and are updated. One consequence: a Bitbucket DC repo reached as ssh://host:7999/... and https://host/... in the same graph is now two packages (rejected on the shared directory unless aliased), and its lockfile key gains :7999 on the next write (keys are recomputed on load; a v3 lock that recorded a ported parent port-blind is re-pointed at its one ported entry on read). Executable approvals for ported deps must be re-granted (apm approve); this is in the CHANGELOG upgrade notes. This matches the approved scope and req-rs-016 (identity includes explicit port).

Issue and approved scope

Issue: Fixes #3927

Human scope-approval comment: #3927 (comment)

Does this PR complete the issue, or what remains? Yes. It delivers root cause 1 (port in identity key); root cause 2 (MCP conflict hint) merged in #3930, so this PR completes #3927.

Type of change

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

Testing

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

Validation evidence (HEAD merged with main bcd020f; re-merged with be0e43a, lint chain clean):

  • Repro on main 0e19cce: DependencyReference.parse('https://github.com:8443/acme/tool.git').get_unique_key() -> acme/tool (same as acme/tool). After: github.com:8443/acme/tool.
  • Real CLI, isolated HOME/APM_HOME, apm.yml with acme/tool + https://github.com:8443/acme/tool.git: .venv/bin/apm install -> exit 1, [x] github.com/acme/tool and github.com:8443/acme/tool are different packages that would both install to apm_modules/acme/tool ... (no fetch).
  • New traps run against an unfixed 0e19cce worktree: pytest tests/unit/test_generic_git_urls.py::TestCustomPortParsing tests/unit/security/test_executables.py::TestCanonicalExecIdentityHostCase tests/unit/deps/test_replacement_resolver.py tests/integration/test_same_repo_two_hosts_lifecycle.py::test_same_repo_on_another_port_is_a_different_package -> 8 failed, 37 passed (all new port traps fail; port-less cases pass).
  • APM_BINARY_PATH=.venv/bin/apm .venv/bin/python -m pytest -p no:xdist tests/integration/test_same_repo_two_hosts_lifecycle.py -> 4 passed (includes installed-CLI port case + alias install + lockfile replay stability).
  • .venv/bin/python -m pytest -p no:xdist tests/unit/deps tests/unit/models tests/unit/security tests/unit/test_generic_git_urls.py tests/unit/test_canonicalization.py tests/unit/test_apm_package.py tests/unit/test_ssh_user_threading.py tests/unit/test_generic_host_error_port.py tests/unit/test_lockfile*.py -> 2917 passed (pre-merge); post-merge targeted set tests/unit/test_generic_git_urls.py tests/unit/security/test_executables.py tests/unit/deps/test_replacement_resolver.py tests/unit/install/test_plan.py tests/unit/test_lockfile*.py -> 520 passed.
  • .venv/bin/python -m pytest -p no:xdist tests/integration/test_architecture_owner_rule_mutations.py -k "cannot_drop_the_port or dependency-identity or covers_every or case_order" -> 8 passed.
  • Lint: uvx [email protected] check / format --check (CI paths) -> clean; pylint R0801 -> 10.00/10; bash scripts/lint-auth-signals.sh -> clean; bash scripts/lint-architecture-boundaries.sh -> exit 0; file-length guard: reference.py 2100 (max 2100, NR > max).

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)

Implements existing req-rs-016 (repository identity includes explicit port); no normative text changes. Spec gate waived in a commit message.

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

apm-spec-waiver: implements existing req-rs-016 port identity; no spec text change

build_dependency_unique_key now qualifies the host with a non-default
port (host:port/owner/repo, github.com included), and both
DependencyReference and LockedDependency pass it. Port-less keys and the
bare default-host form are unchanged. Two ports of one repository are
now two packages, so MaterializationSlots rejects them on their shared
apm_modules directory (#3696) unless one sets alias:. Exec deny matching
stays port-blind, like dependency-policy host matching, so a deny cannot
be bypassed by naming another port. Aligns with req-rs-016 (repository
identity includes explicit port).

Refs #3927

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 14:17
apm-spec-waiver: implements existing req-rs-016 port identity; no spec text change

Co-authored-by: Copilot App <[email protected]>

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 changelog omits the breaking approval-key migration for existing non-default-port dependencies and the required reapproval step using apm approve.

1 open issue
What changed in this PR

Fixes the dependency-identity portion of #3927 by treating non-default ports as distinct authorities. This prevents lockfile entries and dependency deduplication from silently merging different packages; the MCP conflict-hint fix remains separate in #3930.

Changes:

  • Threads ports through the canonical identity builder while preserving port-less keys.
  • Keeps executable approvals port-specific and deny matching port-blind.
  • Adds identity, collision, lockfile, and architecture regression coverage, plus documentation updates.
File Description
tests/​unit/​test_generic_git_urls.py Tests port-qualified keys and lockfile round trips.
tests/​unit/​security/​test_executables.py Tests that alternate ports cannot bypass denials.
tests/​unit/​deps/​test_replacement_resolver.py Distinguishes identity ambiguity from directory collisions.
tests/​integration/​test_same_repo_two_hosts_lifecycle.py Covers port collisions, aliases, and stable lockfile replay.
tests/​integration/​test_architecture_owner_rule_mutations.py Adds mutations proving port-forwarding guards.
src/​apm_cli/​security/​executables.py Removes ports when canonicalizing deny identities.
src/​apm_cli/​models/​dependency/​reference.py Passes the reference port to the identity builder.
src/​apm_cli/​models/​dependency/​identity.py Includes non-default ports in dependency keys.
src/​apm_cli/​install/​plan.py Preserves ports in fallback key construction.
src/​apm_cli/​deps/​lockfile.py Includes persisted ports when reconstructing keys.
scripts/​architecture_linter/​checks/​contracts_test_taxonomy.py Guards port handling across identity owners.
packages/​apm-guide/​.apm/​skills/​apm-usage/​dependencies.md Explains port-sensitive identity and alias requirements.
docs/​src/​content/​docs/​reference/​lockfile-spec.md Documents port-qualified lockfile keys.
CHANGELOG.md Records the user-visible identity correction.

Review effort: Balanced

Comment thread CHANGELOG.md Resolved
The deny identity dropped the port inside executables.py. identity.py
now owns it as build_port_blind_dependency_key, the guard pins both the
helper and its deny caller, and two mutation cases prove it. Tests pin
that a grant names one port while project and org denies cover all.
Addresses python-architect FU-1/FU-2, test-coverage FU-2 and
supply-chain FU-1.

Co-authored-by: Copilot App <[email protected]>
A v3 lock written before #3927 recorded a ported parent as its
port-blind key, so reading it failed with 'Replacement graph parent ...
is absent from the lockfile'. The lockfile read path now points such a
parent at its one ported entry. Addresses test-coverage FU-1.

Co-authored-by: Copilot App <[email protected]>
Deny ignores the port; allow, recommend and require name the ported
key. The installed-CLI test now uninstalls the default-port package
and keeps the ported one. Addresses supply-chain FU-2 and
test-coverage FU-3.

Co-authored-by: Copilot App <[email protected]>
Existing ported dependencies change approval key, so previously
approved hooks and MCP servers become gated until re-approved.
Addresses Copilot inline on CHANGELOG.md.

apm-spec-waiver: implements existing req-rs-016 port identity; no spec text change

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_with_followups

Fixes a silent merge of two repos that differ only by port: a non-default port now becomes part of the dependency identity key, while deny rules ignore the port and approvals must match it exactly.

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 fixes a correctness bug that also touched the trust boundary. On main, github.com:8443/acme/tool and acme/tool got the same identity key, so the second declaration was silently dropped in dedup, lockfile writes and approval-key lookups. The fix keeps one owner for the key (build_dependency_unique_key), adds a non-default port only when present, and leaves every port-less key unchanged, matching OpenAPM req-rs-016. No persona found a bypass and every finding was recommended. Every new port trap was shown to fail on unfixed main (8 failed).

The high-signal findings are already folded at d1fe877. The port-blind deny key now lives in identity.py (build_port_blind_dependency_key), and the architecture guard pins both the key side and the deny side (10 mutation tests pass). A legacy v3 lockfile with a ported parent failed to load at the old head; LockFile._rekey_port_blind_parents fixes it, and its test fails if the call is removed. test_deny_ignores_the_port_and_grants_do_not pins the two-sided trust contract. A real-binary uninstall test covers the ported key. Policy-schema and apm-guide governance document the trust-key rules, and the CHANGELOG entry moved to upgrade notes with the apm approve re-approval step, as the Copilot reviewer asked.

Two items are deferred, both inside the scope record's boundaries: the pre-existing hand-copied kwargs in the install/plan.py fallback, and default-port normalization for hand-edited lock rows (the lockfile has no transport field; the writer never emits default ports; a mismatch fails closed).

Aligned with: Portable by manifest (port-distinct entries survive write and reload; old v3 locks still load); secure by default (deny is port-blind, grants are port-exact, both pinned under mutation); governed by policy (deny follows normalize_policy_host, documented); multi-host (self-hosted GitLab/Bitbucket DC ports get distinct identities, with an alias: escape hatch); pragmatic (port-less keys unchanged).

Panel summary

Persona B R N Takeaway
Supply Chain Security Expert 0 3 0 No bypass found. Deny ignores the port, grants must match it exactly, and failures stay closed. Asked for grant non-leakage tests, trust-key docs, and default-port lock normalization.
Python Architect 0 3 0 One owner for the key holds. The deny port-drop should move into identity.py and the guard should pin it.
Test Coverage Expert 0 3 0 Key, dedup and #3696 traps are solid. Gaps: legacy v3 lock with a ported parent failed to read (reproduced), grant fail-closed unpinned, ported uninstall untested.

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

Top 2 follow-ups

  1. [Python Architect] Replace the hand-copied kwargs in the install/plan.py _dep_ref_key fallback with a call into the identity.py owner, and add the anchored_local_path/is_marketplace fields it is already missing. -- It is the one remaining key path outside the single owner, and it drifted before [BUG] Dependency identity key ignores the port; MCP conflict hint misreads ${CLAUDE_PROJECT_DIR:-.} #3927; the guard could pin it once it routes through identity.py.
  2. [Supply Chain Security Expert] At the next lockfile schema revision, record the transport (or normalize scheme-default ports) so a lock row carrying :443/:22 normalizes to the manifest key. -- It fails closed today and the writer never emits default ports, but a hand-edited row causes a confusing re-resolve or re-approval.

Recommendation

Merge once CI is green on d1fe877. CI was green on the prior head 2262d6c, and the folds passed locally (2581 unit tests, 10 architecture mutation tests, lint chain clean). Every finding on the trust boundary and lockfile back-compat is folded and pinned by tests that fail under mutation. Track the _dep_ref_key single-owner cleanup as the highest-signal follow-up, and take the default-port lock normalization with the next lockfile schema revision.


Full per-persona findings

Python Architect

  • [recommended] The port-blind deny identity was a second policy-identity rule computed in security/executables.py, outside identity.py at src/apm_cli/security/executables.py:328
    Folded: build_port_blind_dependency_key in identity.py; canonical_exec_identity calls it.
  • [recommended] The extended guard pinned the port on the key side but not on the deny side at scripts/architecture_linter/checks/contracts_test_taxonomy.py
    Folded: guard pins the helper body and its deny caller; mutation cases deny-keeps-port and port-blind-key-keeps-port.
  • [recommended] install/plan.py _dep_ref_key fallback is a hand-copied list of builder arguments at src/apm_cli/install/plan.py:104
    Deferred: pre-existing drift (anchored_local_path, is_marketplace) in a test-fake fallback predates [BUG] Dependency identity key ignores the port; MCP conflict hint misreads ${CLAUDE_PROJECT_DIR:-.} #3927; this PR only added port.

Supply Chain Security Expert

  • [recommended] Add a test that a port-less grant does not approve the ported key, plus project deny and ported org deny cases at tests/unit/security/test_executables.py
    Folded: TestDenyIdentitySpellings::test_deny_ignores_the_port_and_grants_do_not (fails if the port drop is reverted).
  • [recommended] Document the executable-trust key rules and the re-approval upgrade at docs/src/content/docs/enterprise/policy-reference.md
    Folded: Ports note in reference/policy-schema.md and apm-guide governance.md; CHANGELOG upgrade note with apm approve.
  • [recommended] A lock row with a default port (443/22) gets a key that never matches the manifest at src/apm_cli/deps/lockfile.py:715
    Deferred: the lockfile records no transport, so a per-row default port needs a schema change (out of scope); fails closed.

Test Coverage Expert

  • [recommended] A legacy v3 lockfile whose replacement-graph parent is a ported dep failed to load at src/apm_cli/deps/lockfile.py:1403
    Folded: LockFile._rekey_port_blind_parents; test_legacy_v3_lock_with_port_blind_parent_still_loads (fails if the call is removed).
  • [recommended] Approval grants stay port-exact, but no test asserted it at tests/unit/security/test_executables.py
    Folded with the supply-chain test above.
  • [recommended] No real-binary lifecycle test of uninstall with the ported key at tests/integration/test_same_repo_two_hosts_lifecycle.py
    Folded: test_same_repo_on_another_port_is_a_different_package now uninstalls test/tool and keeps the github.com:8443 package and lock entry.

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 d46ce76 into main Nov 19, 2026
35 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fix-3927-port-identity-key branch November 19, 2026 15:18
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
- default client is copilot; vscode is its deprecated alias (#3875)
- non-default port is part of the lock identity key (#3932); the roundtrip
  still writes and reads port: 7990
- a partial `apm marketplace update` fails closed with exit 1 and names
  the failed catalog (#3910)
- Claude's skill-command rewrite is an instance method that needs the
  project's .claude/skills copy (#3875)

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] Dependency identity key ignores the port; MCP conflict hint misreads ${CLAUDE_PROJECT_DIR:-.}

2 participants