Skip to content

fix(uninstall): re-land #4866, keep the lock claim on a hook file sync cannot remove - #4952

Merged
Daniel Meppiel (danielmeppiel) merged 17 commits into
mainfrom
danielmeppiel-4851-reland
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 17 commits into
mainfrom
danielmeppiel-4851-reland

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

Re-land of #4866 (reverted in #4950), plus a fix for its clash with #4862.

apm uninstall used to drop a removed package's lock row before hook sync. A hook file that sync then failed to remove stayed on disk while apm.lock.yaml stopped tracking it. Now the row comes back narrowed to the files sync could not remove: canonical hash baseline, valid replacement and local-source graph, and no service membership. The full design and the Copilot review history are on #4866.

Why #4866 was reverted. #4862 landed after #4866's CI base. It deletes a .mcp.json that APM created once its last entry leaves. test_a_retained_hook_claim_does_not_keep_its_mcp_server read that file unconditionally, so main went red on every OS. The behavior was correct; only the test was wrong.

This PR

  1. ceaa9e9e reverts the revert (970f2a6). The src/ and scripts/ trees are byte-identical to fix(uninstall): keep the lock claim on a hook file sync cannot remove #4866's merge (c22ccbe).
  2. 29491406 treats a missing .mcp.json as no servers in that test.
  3. 87f6369e adds a clash cell, test_a_retained_hook_claim_survives_the_removal_of_an_emptied_mcp_config. One uninstall both retires the package's only MCP server (fix(mcp): delete the emptied config file APM created when its last entry leaves (#4852) #4862 deletes the emptied .mcp.json) and keeps the hook claim sync could not remove ([BUG] apm uninstall drops a removed package's lock claim when hook sync fails after the row is deleted #4851). It asserts exit 1, .mcp.json gone, no mcp_servers in the lock, the hook on disk, and the lock still claiming it. Red on main (the lock is removed, so the claim is lost); green here.

Issue and approved scope

Issue: Fixes #4851. Refs #4896 (the follow-up MCP survivor fix is #4900).

Human scope-approval comment: #4851 (comment)

Does this PR complete the issue, or what remains?
It completes #4851. The rest of #4896 (LSP and native membership) follows after this lands.

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:

  • Clash cell red-first: with main's src/ (post-revert: #4866 (main red c22ccbe8: test_a_retained_hook_claim_does_not_keep_its_mcp_server) #4950), uv run --extra dev pytest -q -p no:xdist tests/unit/test_uninstall_hook_sync_failure_claims.py -k emptied_mcp_config gives 1 failed (FileNotFoundError: app/apm.lock.yaml). With this branch it passes.
  • uv run --extra dev pytest -q -p no:xdist tests/unit/test_uninstall_*.py tests/unit/commands/test_uninstall_*.py tests/unit/deps/test_replacement_binding.py tests/quality/test_lifecycle_bug_ledger.py: 228 passed.
  • Lint mirror (ruff check, ruff format --check, pylint R0801, lint-auth-signals, complexity ratchet --base-ref origin/main, taxonomy --changed, lint_architecture_boundaries.py --root .): all clean.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .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.

…c cannot remove

Reverts 970f2a6 (the #4950 revert), restoring #4866 (#4851).

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…last server

#4862 deletes an emptied .mcp.json that APM created, so after hooked-srv is
retired the file is gone. Treat a missing config as no servers.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…config removal

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 08:53

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

Uninstall can drop a lock claim while leaving the original hook behind on POSIX, and the changelog fragment’s mismatched PR reference blocks validation and release rendering.

2 open issues
What changed in this PR

Re-lands #4866 to preserve lockfile ownership when uninstall cannot remove a hook file, and updates regression coverage for #4862's deletion of emptied MCP configs.

Changes:

  • Restores failed file claims with their hash baselines and valid dependency graph links.
  • Adds regression coverage for cleanup failures, local-source anchoring, and simultaneous MCP config removal.
  • Adds an architecture guard, bug-ledger entry, and changelog fragment.
File Description
tests/​unit/​test_uninstall_hook_sync_failure_claims.py Tests retained claims and the empty MCP config interaction.
tests/​unit/​deps/​test_replacement_binding.py Tests parent pruning and root anchoring.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4851.json Records the bug and regression tests.
src/​apm_cli/​models/​dependency/​replacement.py Adds root anchoring when replacement parents are removed.
src/​apm_cli/​deps/​path_anchoring.py Preserves local source locations after reparenting.
src/​apm_cli/​commands/​uninstall/​lockfile_state.py Restores failed claims through the deployment ledger.
src/​apm_cli/​commands/​uninstall/​cli.py Saves removed rows and restores claims after sync failures.
scripts/​architecture_linter/​checks/​contracts_dependency_replacements.py Guards use of the canonical replacement owner.
changelog.d/​4952.fixed.md Documents the uninstall ownership fix.

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
Comment thread changelog.d/4952.fixed.md Resolved
…agment ends with #4952

Addresses Copilot review on #4952.

Co-authored-by: copilot-swe-agent[bot] <[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.

🟢 Approval recommended

No active review issues remain for the changes preserving lock claims when uninstall cleanup fails.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Co-authored-by: copilot-swe-agent[bot] <[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

A critical symlink race in hook inspection can falsely establish absence and drop the hook’s lock claim during an aborted sync, requiring a stable no-follow walk and a parent-swap regression test.

1 open issue

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
#4851)

A parent swapped for a symlink between its check and the final lookup could
redirect the lookup to an empty directory and drop a hook's lock claim.
Where the platform supports it, each parent is now opened as a no-follow
directory handle and the next lookup goes through it.

Co-authored-by: copilot-swe-agent[bot] <[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.

🟢 Approval recommended

No active review issues are reported for the uninstall claim-retention changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

… or a part is . or .. (#4851)

On Windows a junction lstats as a directory, so the lstat walk could still
prove a hook absent through a swapped parent. Without no-follow directory
handles nothing is proven and the claim stays; '.' and '..' parts are refused.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…rt durable state (#4851)

Co-authored-by: copilot-swe-agent[bot] <[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.

🔵 Needs a closer look

Human assessment is needed because a critical root-symlink race can falsely prove hook absence and prevent its claim from being restored after an aborted sync.

No open issues 🎉

Review effort: Balanced

…ed root no-follow (#4851)

A '//server/share' claim parsed to an absolute '//' part that os.open
resolved outside the deploy root. Absolute paths are now refused, and the
resolved deploy root is opened with O_NOFOLLOW so a root swapped for a
symlink after resolving proves nothing.

Co-authored-by: copilot-swe-agent[bot] <[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.

🔵 Needs a closer look

Human assessment is needed because unsafe root resolution and ancestor swaps can falsely report a hook absent, causing an aborted sync to drop its lock claim.

No open issues 🎉

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[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

A deploy-root symlink swap can make uninstall falsely conclude that a retained hook is absent and omit its owner claim during an aborted sync.

1 open issue

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
…ved (#4851)

A root replaced by a link to an empty directory was resolved and walked,
proving a hook absent while it remained in the moved root. The root is now
opened as given with O_NOFOLLOW, so a link root proves nothing.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[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.

🔵 Needs a closer look

Human assessment is needed because an ancestor-directory symlink swap can falsely prove hook absence, allowing uninstall to discard its tracking claim while the hook survives.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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

A critical ancestor-symlink vulnerability lets _absence_proven report absence while the original hook remains, causing sync aborts to drop its lock claim.

1 open issue

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
…e uninstall (#4851)

O_NOFOLLOW guards only the root's last component, so a root reached through
a swapped ancestor could prove a hook absent while it remained in the moved
tree. The root's identity is pinned before uninstall changes anything, and
absence is proven only when the opened root is that directory.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[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.

🟢 Approval recommended

There are no active review issues for the changes preserving lockfile tracking of hook files that cleanup cannot remove.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…ll/hook_absence.py (#4851)

Moves the no-follow absence proof and root pin out of uninstall/cli.py,
unchanged, so the merged cli.py stays under the file-length guardrail.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[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 new selector lacks a registered guard and mutation regression enforcing uninstall’s pinned-root/no-follow absence check, allowing a bypass that could reintroduce symlink-related claim loss.

1 open issue

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit b55e3d1 into main Nov 23, 2026
75 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-4851-reland branch November 23, 2026 19:13
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] apm uninstall drops a removed package's lock claim when hook sync fails after the row is deleted

2 participants