Skip to content

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

Merged
Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-4851-sync-failure-claims
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-4851-sync-failure-claims

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

apm uninstall deleted the removed package's lock row in Step 9, before Step 10 hook sync. If hook sync then could not remove a hook file, uninstall exited 1 and named the file. But apm.lock.yaml was persisted without the row, or removed entirely when it was the last package, so the lock stopped tracking a file still on disk.

Step 9 now keeps the removed rows aside. After reconciliation, retain_failed_claims (commands/uninstall/lockfile_state.py) returns each removed row narrowed to the files sync failed on, with their recorded hashes, through DeploymentLedgerCodec.replace_legacy_owners. The lock keeps tracking what is still on disk, and the next run can retry.

Issue and approved scope

Issue: Fixes #4851

Human scope-approval comment: #4851 (comment)

Does this PR complete the issue, or what remains? It completes it.

Type of change

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

Testing

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

Red-first command-level cell: tests/unit/test_uninstall_hook_sync_failure_claims.py::test_uninstall_keeps_the_claim_of_a_hook_sync_could_not_remove. It runs a real apm install then apm uninstall in process. Only the Path.unlink I/O boundary is faked: selected cleanup's unlink is undone, and the later hook-sync unlink of the same file is denied. It asserts exit 1, the hook still on disk, and its claim still in apm.lock.yaml.

  • On main d0a48e1: fails, because apm.lock.yaml is gone (the claim is lost).
  • Here: passes. pytest tests/unit/test_uninstall_hook_sync_failure_claims.py tests/unit/test_uninstall*.py tests/unit/commands/test_uninstall*.py: 186 passed.

Bug-ledger row: tests/fixtures/lifecycle_bug_ledger/bugs/4851.json. The lint mirror (ruff 0.15.12, R0801, architecture boundaries, auth signals, complexity ratchet, assertion ratchet, taxonomy) is 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.

Step 9 deleted the removed package's lock row before Step 10 hook sync,
so a hook file sync then failed to remove stayed on disk while
apm.lock.yaml stopped tracking it. The removed rows are kept aside, and
after reconciliation each returns, narrowed through the ledger owner to
the files sync failed on.

Fixes #4851

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:22

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 claim restoration can invalidate the lockfile graph, lose retained hooks’ canonical integrity hashes, and incorrectly preserve an uninstalled package’s MCP server.

3 open issues
What changed in this PR

This PR addresses #4851 by preserving lockfile ownership when apm uninstall cannot remove a hook during late synchronization.

Changes:

  • Saves removed dependency rows and restores claims narrowed to failed cleanup paths.
  • Adds a command-level regression for late hook-removal failure.
  • Records the regression in the lifecycle bug ledger and adds a changelog entry.
File Description
tests/​unit/​test_uninstall_hook_sync_failure_claims.py Tests claim persistence after hook removal fails.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4851.json Records the bug and links its regression test.
src/​apm_cli/​commands/​uninstall/​lockfile_state.py Restores removed owners with failed-path claims and hashes.
src/​apm_cli/​commands/​uninstall/​cli.py Saves removed rows and invokes claim restoration after reconciliation.
changelog.d/​4866.fixed.md Documents retained ownership after hook cleanup failure.

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved

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

Failed-hook claim restoration loses canonical ownership and provenance and can retain invalid parent references that block lockfile saves, while required command-level symlink-race coverage is missing.

3 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Failed-claim restore collapses distinct owners for shared paths

src/​apm_cli/​commands/​uninstall/​lockfile_state.py:113

When a failed hook claim is retained, this call rebuilds unrelated surviving deployments from legacy path lists and collapses distinct target records for a shared path, such as codex and cursor claims for .agents/skills/shared/SKILL.md. This loses ownership information that the preceding reconciliation preserves and can make later target-specific cleanup overlook the dropped target. Restore failed claims through a ledger-owner operation that preserves unrelated canonical records, and add a regression with multiple surviving target records for one path.

Medium severity Test omits parent-symlink race and reported-path assertions

tests/​unit/​test_uninstall_hook_sync_failure_claims.py:68

The approved scope for #4851 requires a command-level parent-symlink race as well as an I/O failure, but this test only injects PermissionError and never checks the reported path. Add the late parent-symlink case and assert exit 1, the named path, the surviving hook, and its persisted lock claim for both failure modes.

Review effort: Balanced

…onical hashes and a valid graph

Addresses Copilot review on #4866: a claim-only row is restored after MCP,
LSP and native survivor derivation (and drops its native_plugin record), its
hash baseline comes from the pre-removal canonical snapshot, and parent edges
to rows removed in the same uninstall re-anchor at the root so the lock still
serializes.

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

Retained file-only claims can leave a shared MCP server configured after both packages are uninstalled, and the required parent-directory symlink-swap regression is missing.

3 open issues
3 resolved since last review

Review effort: Balanced

Addresses Copilot review on #4866: an aborted hook sync (a path-safety
refusal through a parent swapped for a symlink) reports no per-path result,
so every removed path still on disk keeps its claim, and the refusal is
named without --verbose. Root anchoring of a retained claim's replacement
requests moves to ReplacementBinding.retain_parents_or_root, guarded by
contracts-tooling-source-replacements.

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 sync-abort handling can mistake path-safety or inspection failures for hook absence, dropping a claim and potentially deleting the last-package lockfile while the original hook remains.

No open issues 🎉

3 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

Two unresolved issues can redirect retained local dependencies to incorrect sources and discard lock claims when symlink destinations are empty, dangling, or inaccessible.

2 open issues

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
…im sources

Addresses Copilot review on #4866: after an aborted sync, a removed path
loses its claim only when its absence is proven through real, inspectable
directories (a symlinked parent, an empty or dangling destination, or a
denied lstat keeps it). A local claim whose declaring parent goes is
re-spelled against the project root by the path-anchoring owner
(root_anchored_local_path), so it still names the same source directory.

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

Legacy local-claim restoration can leave invalid parent references or colliding serialized identities, preventing lockfile persistence or overwriting a surviving root dependency on reload.

2 open issues
2 resolved since last review

Review effort: Balanced

…entity collisions

Addresses Copilot review on #4866: every retained claim's final key is
computed before restoration and parent edges are remapped onto it, so two
retained legacy local generations stay a valid graph; a claim whose
re-spelled identity matches a row the lock keeps joins that row through the
ledger owner instead of shadowing it on reload.

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 change preserving lockfile ownership claims after failed hook cleanup.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit c22ccbe into main Nov 23, 2026
74 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-4851-sync-failure-claims branch November 23, 2026 08:27
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
…1-sync-failure-claims

revert: #4866 (main red c22ccbe: test_a_retained_hook_claim_does_not_keep_its_mcp_server)
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
Picks up the #4950 revert of #4866, whose test failed on this PR's merge commit.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
fix(uninstall): re-land #4866, keep the lock claim on a hook file sync cannot remove
Gregory (gdelpu) pushed a commit to gdelpu/apm that referenced this pull request Nov 23, 2026
… file sync cannot remove

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

Co-authored-by: copilot-swe-agent[bot] <[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] apm uninstall drops a removed package's lock claim when hook sync fails after the row is deleted

2 participants