Skip to content

fix(uninstall): LSP cleanup uses recorded targets (#4059) - #4068

Merged
Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-uninstall-lsp-4059
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-uninstall-lsp-4059

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

TL;DR

apm uninstall reconciled surviving LSP servers by re-choosing targets the way a fresh install does (saved apm config target, then harness autodetection), never reading the lsp_target_servers the lockfile records. It now keeps survivors on the recorded targets through one owner in core/target_detection.py. A project installed with --target copilot and no targets: no longer fails with "No harness detected", and a saved default no longer moves surviving servers to another harness.

Fixes #4059

Note

Follows #4057 (merged): that PR loads the manifest for LSP-only scopes; this one fixes which targets the reconciliation uses. Now based on main, so CI runs in full.

Problem (WHY)

  • Project installed with apm install --target copilot, no targets:, no harness marker: apm uninstall <pkg> exits 1 with LSP cleanup failed: No harness detected and cleans nothing (install/lsp/integration.py reconcile_lsp_after_uninstall called resolve_package_target_decision with no explicit target).
  • With a saved apm config target claude, uninstalling one package at project and user scope writes the surviving server into .claude/skills/apm-lsp/... and removes it from .github/lsp.json / ~/.copilot/lsp-config.json (lock: {claude: [keep-lsp]}).

Why these matter: the target owner already states the rule for existing deployments: resolve_audit_target_decision — "A saved install default is not a request to retarget an existing deployment." The lockfile reference promises that lsp_target_servers is the record "Reinstall and uninstall use … to remove stale executable entries" (docs/src/content/docs/reference/lockfile-spec.md).

Approach (WHAT)

# Fix
1 core/target_detection.py gains resolve_deployment_target_decision: manifest targets: → recorded targets → caller fallback only when nothing is recorded. resolve_audit_target_decision delegates to it (one owner, two consumers).
2 reconcile_lsp_after_uninstall passes recorded_lsp_targets(lockfile) and keeps today's selection (resolve_package_target_decision) as the fallback for a lock without lsp_target_servers.
3 Architecture guards: the LSP lifecycle rule requires the recorded-target call; the deployment resolver must have exactly one owner; a standalone *_cannot_bypass_owner test proves both guards fire (the registry matrix keeps one case per guard).

Implementation (HOW)

File Intent
src/apm_cli/core/target_detection.py New shared resolver; audit behavior unchanged (same precedence, default auto-detect fallback). Install precedence untouched.
src/apm_cli/install/lsp/integration.py recorded_lsp_targets() reads non-empty lsp_target_servers keys; reconcile routes through the shared resolver. Removal was already per recorded runtime; only the survivor target set changed.
scripts/architecture_linter/checks/{install_lsp_plugin,registry_owner_guards}.py Token guard on the reconcile call; single-owner check for the new resolver.
tests/integration/test_uninstall_lsp_recorded_targets_lifecycle.py Installed-CLI lifecycle, project + user scope (APM_HOME outside HOME): 2 regression tests, 2 controls, and a malformed-config fail-closed test that is a control at user scope and a regression at project scope.
src/apm_cli/core/target_detection.py (rebase) The shared resolver uses #3989's MANIFEST_TARGET_SOURCE; resolve_audit_target_decision delegates.
tests/unit/core/test_audit_target_decision.py, tests/integration/test_architecture_owner_rule_mutations.py Resolver precedence cases (fallback never runs when targets are recorded); test_uninstall_lsp_target_selection_cannot_bypass_owner (2 source overrides through run_selected_rules).

Diagrams

Legend: the uninstall-time target decision after this PR; the saved default and autodetection are reachable only from the right-most branch.

flowchart LR
    R[reconcile_lsp_after_uninstall] --> D{manifest targets declared}
    D -->|yes| M[EffectiveTargetDecision from apm.yml]
    D -->|no| L{lsp_target_servers recorded}
    L -->|yes| K[EffectiveTargetDecision from deployment ledger]
    L -->|no| F[resolve_package_target_decision fallback]
    M --> W[run_lsp_integration persist false]
    K --> W
    F --> W
Loading

Trade-offs

  • Manifest over recorded. Chose the audit precedent (declared targets: wins, as install does); rejected strictly-recorded-on-uninstall because it would diverge from what the next apm install does. Retargeting when they disagree is out of scope per the record.
  • A recorded target is reconciled even if its root was deleted. If .claude/ (or another recorded target root) was removed by hand, uninstall writes surviving servers back to it, because the lockfile says APM manages them there. This is intended: the record makes the lockfile's targets authoritative, and the next apm install would do the same.
  • Fallback kept for legacy locks. A lock without lsp_target_servers keeps today's selection instead of failing; covered by a control test.
  • No conformance test. OpenAPM v0.1 has no LSP requirement to cite, and the Mode B detector does not fire (17 substantive critical-path lines < 20).
  • fix(0.37): finish owner hardening for bundle MCP, audit copies, failed installs, MCP targets and plugin pins #3989 seam. That 0.37 PR renames the "apm.yml" source string inside resolve_audit_target_decision; the rebase is cosmetic (the literal moves into the shared resolver).

Benefits

  1. apm uninstall exits 0 for a --target copilot project with no targets: (was exit 1, nothing cleaned).
  2. A saved apm config target never moves a surviving LSP server during uninstall, at project and user scope.
  3. Uninstall and audit share one "existing deployment" target rule in the owner, guarded statically.

Docs re-home after #3996

Rebased onto 0.37.0 (main 2e01e13), which includes #3996.

Original edit After #3996 Done in this PR
reference/cli/uninstall.md (step 6, LSP cleanup targets) Kept as-is (reference layer) Applied unchanged
consumer/install-lsp-servers.md (Stale server cleanup) Page deleted; redirected to consumer/install-mcp-servers/ Sentence re-homed under install-mcp-servers.md ## LSP servers

Issue and approved scope

Issue:

Fixes #4059

Human scope-approval comment: #4059 (comment) (scripts/governance/eligibility.cjs → record-present, approver Daniel Meppiel (@danielmeppiel))

Completes the issue: installed-CLI lifecycle at project and user scope with controls, docs by topic, one CHANGELOG line.

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 on base, green on branch (APM_E2E_TESTS=1 APM_BINARY_PATH=.venv/bin/apm, base src = a1ef9f1):

Test Base (#4057) Branch
uninstall_without_targets_key_uses_the_recorded_target[project] ❌ No harness detected ✅
uninstall_without_targets_key_uses_the_recorded_target[user] ✅ (control) ✅
saved_default_target_never_moves_a_surviving_server[project] ❌ moved to claude ✅
saved_default_target_never_moves_a_surviving_server[user] ❌ moved to claude ✅
malformed_recorded_target_config_still_fails_closed[project] ❌ No harness detected ✅
malformed_recorded_target_config_still_fails_closed[user] ✅ (control) ✅
manifest_targets_still_win_over_the_recorded_target[project, user] (asserts the claude plugin contents) ✅ (control) ✅
lock_without_recorded_targets_keeps_the_fallback[project, user] ✅ (control) ✅
Local evidence (no CI on a stacked base)
$ pytest tests/integration/test_uninstall_lsp_recorded_targets_lifecycle.py      # branch
10 passed in 10.80s
$ (base src) same file
4 failed, 6 passed in 10.44s
$ pytest tests/unit/core/test_audit_target_decision.py
14 passed
$ pytest tests/integration/test_architecture_owner_rule_mutations.py tests/unit/scripts/test_architecture_runner.py -n 8
934 passed   (includes test_matrix_covers_every_registry_guard_exactly_once and the 2 new bypass cases)
$ ruff check / ruff format --check (src tests scripts/lint_architecture_boundaries.py scripts/architecture_linter)
All checks passed! / 2463 files already formatted
$ pylint R0801 (--min-similarity-lines=10)        10.00/10
$ bash scripts/lint-auth-signals.sh               [+] auth-signal lint clean
$ python scripts/lint_architecture_boundaries.py --root .   exit 0
$ BASE_REF=origin/danielmeppiel-uninstall-lsp-4045 bash tests/spec_conformance/mode_b_detector.sh
[+] mode_b: 17 substantive added lines (< 20); OK
$ pytest tests/unit/{core,install,commands,integration} + LSP lifecycle files -n 8
9173 passed, 257 failed: 251 fail identically on base (sandbox: installer ownership,
network guard, git transport); 6 in test_output_mode pass in isolation (7 passed, xdist ordering).

How to test:

  • apm install --target copilot in a project with an LSP package and no targets:, then apm uninstall <pkg> → exit 0, server gone from .github/lsp.json and the lock.
  • Add a second LSP package, apm config set target claude, uninstall one → survivor stays in .github/lsp.json; no .claude/skills/apm-lsp.
  • Repeat with --global and APM_HOME outside HOME.

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)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour. (OpenAPM v0.1 has no LSP requirement; Mode B detector: no fire.)

Co-authored-by: Copilot [email protected]

@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-uninstall-lsp-4059 branch 2 times, most recently from de24ba8 to 97ba1b3 Compare November 20, 2026 04:09
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
@danielmeppiel Daniel Meppiel (danielmeppiel) changed the title [0.38] fix(uninstall): LSP cleanup uses recorded targets (#4059) fix(uninstall): LSP cleanup uses recorded targets (#4059) Nov 20, 2026
Base automatically changed from danielmeppiel-uninstall-lsp-4045 to main November 20, 2026 08:55
Uninstall-time LSP reconciliation re-selected targets like a fresh
install (saved apm config target, then harness autodetection) and never
read the lsp_target_servers the lockfile records. A project installed with
--target copilot and no targets: failed with "No harness detected", and a
saved default moved a surviving package's server to another harness.

- core/target_detection.py: resolve_deployment_target_decision reconciles
  an existing deployment (manifest targets:, then recorded targets, then a
  caller fallback only when nothing is recorded); resolve_audit_target_decision
  delegates to it.
- install/lsp/integration.py: reconcile_lsp_after_uninstall passes the
  recorded LSP targets (recorded_lsp_targets) and keeps today's selection as
  the fallback for a lock without lsp_target_servers.
- Guards: lsp lifecycle requires the recorded-target call; the deployment
  resolver has one owner; two mutation cases.
- Tests: installed-CLI lifecycle at project and user scope (APM_HOME outside
  HOME) plus controls; resolver unit cases.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) marked this pull request as ready for review November 20, 2026 09:31
Copilot AI balanced review requested due to automatic review settings November 20, 2026 09:31
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit ae2c538 into main Nov 20, 2026
34 of 36 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-uninstall-lsp-4059 branch November 20, 2026 09:31

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

Uninstalling a package without manifest targets can incorrectly replay surviving package servers into bundle-only targets, requiring package/project-based target selection and a mixed-owner regression test.

1 open issue
What changed in this PR

This PR addresses #4059 by making uninstall-time LSP reconciliation use recorded deployment targets instead of saved defaults or harness detection. It builds on #4057’s fix for LSP cleanup without MCP state.

Changes:

  • Shares target precedence between audit and LSP uninstall: manifest targets, recorded targets, then fallback.
  • Adds project- and user-scope lifecycle coverage, resolver tests, and architecture guards.
  • Documents the cleanup behavior and records the regression in the bug ledger and changelog.
File Description
tests/​unit/​core/​test_audit_target_decision.py Tests target precedence and fallback invocation.
tests/​integration/​test_uninstall_lsp_recorded_targets_lifecycle.py Covers recorded targets, saved defaults, legacy locks, and malformed configs.
tests/​integration/​test_architecture_owner_rule_mutations.py Tests detection of target-selection bypasses and duplicate owners.
tests/​fixtures/​lifecycle_bug_ledger.json Records #4059 and its regression coverage.
src/​apm_cli/​install/​lsp/​integration.py Reads recorded LSP targets for uninstall reconciliation.
src/​apm_cli/​core/​target_detection.py Introduces the shared deployment-target resolver.
scripts/​architecture_linter/​checks/​registry_owner_guards.py Enforces a single deployment-target resolver owner.
scripts/​architecture_linter/​checks/​install_lsp_plugin.py Requires the recorded-target selection call.
docs/​src/​content/​docs/​reference/​cli/​uninstall.md Explains LSP cleanup target precedence.
docs/​src/​content/​docs/​consumer/​install-mcp-servers.md Describes recorded-target cleanup behavior.
CHANGELOG.md Announces the uninstall LSP target-selection fix.

Review effort: Balanced


def recorded_lsp_targets(lockfile: "LockFile") -> tuple[str, ...]:
"""Return the LSP targets *lockfile* records APM writing servers to."""
return tuple(sorted(name for name, servers in _target_server_sets(lockfile).items() if servers))
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.

LSP cleanup on uninstall ignores recorded LSP targets and fails with 'No harness detected'

2 participants