Skip to content

fix(uninstall): re-anchor a still-needed local dependency instead of refusing (#4889) - #4964

Merged
Daniel Meppiel (danielmeppiel) merged 23 commits into
mainfrom
p2-4889-local-reanchor
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 23 commits into
mainfrom
p2-4889-local-reanchor

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

apm uninstall ./vendor/e used to refuse when a surviving package still declares e (#4882): a local lock key embeds where the dependency was declared, and uninstall could not re-key it. Uninstall now keeps e and gives it the same lockfile identity that apm install --no-frozen records:

  • Lock row: local_path as the new parent declares it, plus declaring_parent and anchored_local_path. The key changes from ./vendor/e to local:vendor/e.
  • References to the old key: descendants' declaring_parent and resolved_by, and replacement request parents (req-rpl-003), all in the same transition.
  • Lock ledger owners are renamed to the new key.
  • Deployment receipts:

Byte-identity narrowing (accepted): cell 1's byte identity with a real apm install --no-frozen (lock and receipts) is narrowed. Two cases are deferred to #4999, which lands today:

  • the declaring-edge order across production and dev roots;
  • the replacement request order.

In those cases --frozen still passes and no file is lost; only the serialized order differs. #4999 also moves the apm_modules materialization dir, with rollback.

Refusals before any write:

  • a graph that cannot be read offline, including apm_modules/ missing while a surviving local source reaches the package;
  • a deployment receipt APM cannot prove, including a file claim whose lock hash is null;
  • an alias spelling or alias that installs elsewhere;
  • a narrower surviving selection, or any survivor refresh;
  • native plugin ownership;
  • a combined removal of other packages.

Missing apm_modules/: a local removal is checked offline against the surviving local sources (deps/reachability.local_sources_reach, which reads each node the way the main walk does).

  • It refuses if a survivor reaches the package by inode or by normcase+NFC path.
  • It also refuses if a survivor source is missing or unreadable and is not a known manifest-less leaf.
  • A --dry-run shows that package as kept, with the reason, and makes no changes.

test_exact_uninstall_fails_when_multiple_shared_slot_survivors_remain now creates apm_modules/. The survivor count is unchanged. Without apm_modules/, its survivors' sources are missing, so this offline check refuses first. It refuses earlier and is stricter, not looser: nothing is written either way. With apm_modules/ present, the specific shared-slot refusal runs as before.

R8 cell mapping:

  • symlinked survivor dir: test_without_apm_modules_a_symlinked_survivor_directory_refuses
  • .. survivor: test_without_apm_modules_a_survivor_outside_the_project_refuses
  • plugin-format survivor: test_without_apm_modules_a_plugin_format_survivor_refuses
  • native-owned re-anchor: the unit cell test_a_reanchor_that_cannot_be_one_transaction_refuses[native]

R7 paired cell: test_a_dry_run_previews_the_offline_refusal_as_kept.

Rollback: a lock save or receipt rename that fails restores apm.yml and the lock, byte for byte (CRLF too), and exits nonzero.

How resolution is computed: the new identity comes from the offline reachability walk, deps/reachability.compute_forward_reachable_keys. It runs the install resolver's own read-only expander (APMDependencyResolver(read_only=True)) and transitive_local_identity, without any download. The full install resolve phase is bound to InstallContext and its download callbacks, so it can't run offline. The walk is the same resolution logic without download side effects (P2 lead and QL accepted this). Two guarantees follow:

  • An incomplete or unreadable walk refuses before any write; a re-anchor never fetches.
  • Cell 1 compares the result against a real apm install --no-frozen run: lock and receipts must be byte-identical.

The walk also records each local node's anchored directory (local_dirs). It now gives a surviving local node the same reachable and dev status as a remote one; otherwise a descendant that became dev-only kept is_dev unset. A symlink loop now makes the walk incomplete instead of crashing it.

User scope keeps the #4882 refusal.

Issue and approved scope

Issue: Fixes #4889

Human scope-approval comment: the regent's ruling on the #4889 design brief (docs/design/4889-local-reanchoring-brief.md at 9b2af88, branch p2-4889-design-brief): spec option (b), no amendment; Done-when cells 1-8. The P2 lead and QL accepted using the reachability walk.

Does this PR complete the issue, or what remains? This is step 1 (rule 22). Remaining: none from the brief cells; a re-anchor combined with another removal refuses (rollback covers apm.yml and the lock only). Earlier list:

  • Cell 5: a lifecycle cell for replacement parents. The code already re-keys requests[].parent.
  • Cell 8d: case and NFC/NFD variants.
  • Full rollback of the manifest and lock if the receipt rename fails. Today it exits 1 and keeps the files.

Type of change

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

Testing

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

Under the burst rule, CI is the test run: locally I only ran ruff on the changed files. A manual CLI probe of the cell 1 graph showed:

  • --frozen passes after the uninstall;
  • the lock and receipts are byte-identical after a real --no-frozen;
  • the receipt hash history is carried;
  • apm audit reports an edit made before re-anchoring;
  • a later apm uninstall ./vendor/d removes the now-transitive e.

New cells in tests/integration/test_uninstall_local_reanchor_lifecycle.py. They fail on main because uninstall refuses (or crashes on a symlink loop):

  1. test_uninstall_reanchors_a_still_needed_local_dependency
  2. test_later_uninstall_and_prune_of_the_reanchored_package_keep_deletion_evidence
    If a file was edited before a later removal, that removal refuses: it names the file and changes nothing until the file is moved aside. This is the required outcome, because deletion needs proof that APM wrote the bytes (req-lk-024, [BUG] Stale cleanup still trusts a lockfile hash that matches a file APM never deployed #3701), and a lost or forged binding keeps files and never deletes them. It is not a weaker cell.
  3. test_a_forged_receipt_binding_refuses_the_reanchor
  4. test_a_reanchor_that_would_need_the_network_refuses
  5. test_an_edit_made_before_reanchoring_is_still_reported
  6. test_reanchoring_carries_the_receipt_hash_history
    8a/b/c/e. whitespace and non-ASCII paths, missing directory, symlinked directory, symlink loop, outside-project path

The two #4882 refusal tests are removed, because that behavior is what this PR changes. Ledger rows: 4889 added, 4882 repointed.

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.

apm-spec-waiver: re-keying a kept local lock row is an implementation detail covered by req-lk-006, req-lk-017 and req-lk-024 (ruled option b, no amendment); the reachability walk only exposes data it already computes. Per the ruling (option b), it is an implementation detail covered by req-lk-006, req-lk-017 and req-lk-024; no amendment.

…refusing

apm uninstall of a direct local dependency that a surviving package still
declares now keeps it under the identity apm install --no-frozen records:
the lock row (local_path, declaring_parent, anchored_local_path), every
reference to the old key (descendants, replacement request parents), the
lock's ledger owners, and the deployment receipts with their hash history.

Resolution is the offline reachability walk (the install resolver's own
read-only expander plus transitive_local_identity). An incomplete walk, or a
receipt binding APM cannot prove, refuses before any write.

Fixes #4889

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 09:43
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…n receipt failure

A re-anchored parent's replacement requests take its new dev status, as
install records the edge. A receipt rename that fails after the lock commits
restores apm.yml and apm.lock.yaml, so the re-anchor is one transaction.
Adds cells 5 (replacement parents), 8d (case and NFD spellings) and the
rollback cell.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.38.x milestone Nov 23, 2026

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

Re-anchoring can persist parent and local identities that differ from a fresh install, while incorrectly rejecting or invalidating valid shared-deployment receipts.

5 open issues
What changed in this PR

This PR updates project-scope uninstall to retain still-needed local dependencies under their transitive lock identity instead of refusing removal of the direct declaration. It extends the existing reachability and deployment-receipt mechanisms; user-scope refusal remains unchanged.

Changes:

  • Re-keys local lock entries, parent references, ledger owners, and receipt bindings while preserving hash history.
  • Adds offline-readability and receipt-validation checks, plus local reachability and dev-status repairs.
  • Adds lifecycle coverage and updates documentation and bug-ledger records.
File Description
tests/​integration/​test_uninstall_shared_direct_dependency_lifecycle.py Removes superseded local-refusal tests.
tests/​integration/​test_uninstall_local_reanchor_lifecycle.py Adds re-anchoring, receipt-history, refusal, and path coverage.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4889.json Records the fix and regression cases.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4882.json Redirects regression links to the new behavior.
src/​apm_cli/​deps/​reachability.py Records local directories and extends local-node matching and error handling.
src/​apm_cli/​core/​deployment_reanchor.py Validates and renames receipt ownership while retaining history.
src/​apm_cli/​commands/​uninstall/​local_reanchor.py Plans and applies lock identity and ownership repairs.
src/​apm_cli/​commands/​uninstall/​cli.py Integrates preflight planning and postcommit receipt updates.
packages/​apm-guide/​.apm/​skills/​apm-usage/​commands.md Updates uninstall usage guidance.
docs/​src/​content/​docs/​reference/​cli/​uninstall.md Documents retention and refusal behavior.
changelog.d/​4964.fixed.md Adds the user-facing fix entry.

Review effort: Balanced

Comment thread src/apm_cli/core/deployment_reanchor.py Resolved
Comment thread src/apm_cli/core/deployment_reanchor.py Resolved
…wners

The re-keyed identity comes from transitive_local_identity; the receipt owner
rename is registered with the deployment provenance owner, which now also
rejects forgetting receipt history in a re-anchor (with a mutation case).
Both new modules join their canonical owners; remediation text comes from the
catalog.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Each guard has exactly one mutation case, so the no-history-forget rule for
core/deployment_reanchor.py is its own guard,
install-deployment-reanchor-receipt-history, registered with the deployment
provenance owner.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…le; UTF-8 fixtures

A later removal of the re-anchored package refuses while a deployed file is
edited, naming it and changing nothing; once moved aside the proven files go.
Non-ASCII path fixtures are written as UTF-8.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…writer-bound receipts

The re-anchored identity uses the first shallowest declaring edge install
keeps; nearest-parent ties rank by the post-rename keys; an alias spelling
that installs elsewhere refuses before writes. Receipts are authenticated by
writer and claim signature, and every claim the rename touches is re-signed,
shared co-owner claims included.

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 failed receipt rename during mixed removals can restore manifest and lockfile entries without restoring already-deleted dependency files, leaving an inconsistent installation despite reporting that all files were kept.

4 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Match install’s breadth-first order when selecting declaring parents

src/​apm_cli/​commands/​uninstall/​local_reanchor.py:73

Reachability drains all production paths before dev roots, so its edge order can differ from install's breadth-first order. With surviving production p -> x -> s -> e and dev roots ordered b -> t -> e, then d -> s -> e, this chooses s as e's declaring parent while install chooses t, causing --no-frozen to rewrite the lock. Track install's combined breadth-first discovery order separately from production-status propagation, and use it for both root identities and descendant declaring-parent repairs.

Medium severity Repair replaced bindings when request parents are dev-only

src/​apm_cli/​commands/​uninstall/​local_reanchor.py:169

For e -> f -> replaced r, removing direct production e while dev d -> e survives makes f dev-only without changing its key. This guard skips r's binding because local:vendor/f is not in renames, leaving requests[].dev false and causing install --no-frozen to rewrite the lock. Process bindings whose request parents appear in either renames or the dev-status repair map.

5 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
…ck, re-anchor alone

A case variant that names the same package directory carries the repo_url a
fresh install records instead of refusing. The rollback snapshot keeps raw
line endings. A re-anchor combined with another removal refuses before any
write, since rollback restores apm.yml and the lock, not deleted content.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
apm-spec-waiver: ruled option b, no spec amendment for local re-anchoring

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

Critical re-anchor error paths can bypass rollback after lock-save failures or leave deployed files changed after receipt failures, breaking frozen installs or file-preservation guarantees.

No open issues 🎉

4 previously missed issues

In code that hasn't changed since last review

High severity Prevent survivor refresh from changing files during re-anchor

src/​apm_cli/​commands/​uninstall/​cli.py:219

A standalone re-anchor can still change deployed files: when e's source changed since install, gate_retired_hook_survivors() refreshes its still-depth-1 row and schedules its deployed files for cleanup. If receipt retirement or renaming then fails because the receipt directory is unwritable, restoring only the manifest and lock leaves those files deleted or rewritten despite reporting that every file was kept. Refuse survivor refresh/gate-retirement work before writes during a re-anchor, or include its file and receipt changes in the rollback.

High severity Roll back re-anchor when lock persistence fails

src/​apm_cli/​commands/​uninstall/​cli.py:503

A failed or withheld lock save returns at lines 500-502 before this call, bypassing the re-anchor rollback after apm.yml has already lost the direct declaration. An atomic lock-write failure can therefore leave the old direct identity on disk, breaking frozen installs while still reaching Uninstall complete. Restore the re-anchor snapshot and exit nonzero when required lock persistence fails or is withheld, with explicit handling for restoration failures.

Medium severity Match install declaration order when repairing identities

src/​apm_cli/​commands/​uninstall/​local_reanchor.py:76

The production-first walk can record a different first shallowest edge than install: with surviving prod p -> q -> z, dev d -> [a, z], and both a and z declaring e, it selects z while install's breadth-first traversal selects a. Removing direct e then records identity fields that install --no-frozen rewrites; descendant selection at lines 180-181 has the same issue. Track install-equivalent declaration order separately from production/dev propagation and use it for both identity repairs.

Medium severity Update dev status for unchanged descendant lock keys

src/​apm_cli/​commands/​uninstall/​local_reanchor.py:192

With e -> f -> replaced r and only dev d -> e surviving, f becomes dev-only without changing its lock key. This guard skips r's request from local:vendor/f, leaving dev: false where a fresh install --no-frozen records true and rewrites the lock. Update request dev status for affected parents even when their keys are unchanged, and cover a replacement below a descendant.

4 resolved since last review

Review effort: Balanced

…observable output

Prune of a transitive local orphan is tracked in #4989, so cell 2 asserts
that prune removes only the proven package and keeps the re-anchored one's
files, row and receipts. Cell 7 also asserts the command output and lock.

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

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
Comment thread src/apm_cli/core/deployment_reanchor.py Resolved
… strict

A local removal refuses when apm_modules/ is missing and a surviving local
package could need it; a re-anchor refuses when the surviving selection is
narrower or its alias differs. An unsaved lock rolls the re-anchor back and
exits nonzero. Receipt checks no longer skip a file whose lock hash is
missing, and replacement requests from any re-parented row take its dev
status. Path spelling cells are one parametrized cell.

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 omitted critical findings indicate risks of trusting unauthenticated receipts, removing still-needed deployments, and losing receipt history.

No open issues 🎉

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

Unresolved critical re-anchoring bugs can remove live hooks that manifest/lock rollback cannot restore and retain the wrong shared-file writer, causing deployed content to change on reinstall.

4 open issues

Review effort: Balanced

Comment thread src/apm_cli/commands/uninstall/cli.py Resolved
…every same-directory declaration

A re-anchored package that ships hooks, or that shares a deployed file with
another owner, refuses before any write: its merged hooks or shared writer
cannot move with the re-key in one transaction yet. Offline, every requested
declaration of one directory is kept in the preview. The rollback unit cells
write fixtures as bytes so CRLF platforms compare exactly.

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 incomplete reachability checks and staged lockfile rollback failures can cause unsafe local removals and inconsistent dependency identities.

1 open issue
3 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

Local re-anchoring leaves stale LSP provenance that causes later lockfile rewrites, and repeated rollback failures can overwrite existing recovery backups.

2 open issues
1 resolved since last review

Review effort: Balanced

…llback backup

LSP provenance names the owning package by its lock key, which a re-anchor
would leave stale, so it refuses before any write. A repeated failed
rollback writes a fresh backup name with create_only instead of overwriting
the earlier recovery content.

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 local re-anchoring interacts with ownership journals and path identity in ways that can leave removed Codex plugins registered.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit e8799a5 into main Nov 23, 2026
75 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the p2-4889-local-reanchor branch November 23, 2026 16:35
Gregory (gdelpu) pushed a commit to gdelpu/apm that referenced this pull request Nov 23, 2026
On 2026-11-23 the job took 252 s at p50, 296 s at p90 and 299 s at most
across 30 green runs, against a 300 s timeout, and about 9 runs were
cancelled at the cap (microsoft#4975, microsoft#4964, microsoft#5001, microsoft#4982). The cap moves to 10
minutes as headroom only: the scaling assertion, selection and the job's
non-required status are unchanged. The ratchet topology pin follows.

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.

uninstall: keep a still-needed LOCAL dependency by re-anchoring its lock row, ledger owners and receipts

2 participants