Skip to content

fix(mcp): delete the emptied config file APM created when its last entry leaves (#4852) - #4862

Merged
Daniel Meppiel (danielmeppiel) merged 11 commits into
mainfrom
cloud-4852-empty-config-residue
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 11 commits into
mainfrom
cloud-4852-empty-config-residue

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

Retiring APM's last entry from a config file APM created (approval revoked, org policy executables.deny_all, a target dropped) left the file as an empty skeleton -- .mcp.json as {"mcpServers": {}}, .claude/settings.json as {} -- and the lock kept mcp_target_servers: {}. A fresh deploy of the same inputs writes none of them, so the result depended on history.

Following the #4524 / LSP pattern:

  • MCP cleanup of the project .mcp.json (copilot and claude) deletes the file once this cleanup removed every server it held and only empty scaffolding remains (_write_cleaned_json_config in integration/mcp_integrator.py).
  • Merged-hook cleanup (_clean_apm_entries_from_json, _clean_apm_source_from_json) deletes a hook JSON once what remains equals that target's own injected defaults (Cursor's version: 1), through write_seam.unlink.
  • LockFile omits an empty mcp_target_servers once no MCP server is left. Serialization and is_semantically_equivalent share one predicate (records_mcp_target_servers in deps/lockfile_container.py), so the lock does not churn. lockfile.py stays under its 2100-line cap.
  • A file holding the user's own servers or keys, even an empty-valued key, stays with only APM's entries removed.

Known limit: APM keeps no record of file creation. As with the LSP config and #4524, it treats "every entry was APM's and nothing else remains" as proof that it created the file. A file the user created empty and APM then wrote into is deleted; it held no user data. Follow-up: #4867.

Imports W4's lane C differential (tests/lifecycle/security/test_incremental_deploy_differential.py and kit tests/lifecycle/scenarios/_deploy_diff.py, from danielmeppiel-w4-t7-lane-c-differential @ b050939) and flips its three strict Known(#4852) cells.

Issue and approved scope

Issue: Fixes #4852

Human scope-approval comment: #4852 (comment)

Completes the issue.

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: on main + the imported lane C module, pytest tests/lifecycle/security/test_incremental_deploy_differential.py -> 7 passed, 3 xfailed (strict Known #4852: approval-revoked, policy-denies-executables, target-removed). With the fix and the markers kept -> 3 failed (XPASS strict); with the markers removed -> 10 passed.

Guard cells (new): test_retiring_apm_entries_keeps_a_config_file_with_user_content[mcp-user-server|mcp-user-key|claude-settings-user-key] -> 3 passed (a user server or user key keeps .mcp.json / .claude/settings.json, without APM's entry).

Updated anti-oracles that pinned the empty skeleton (unit: hook integrator, dropped-target reconcile, cursor contraction, claude MCP, MCP ownership, lock determinism; integration: test_copilot_root_mcp_json_lifecycle.py, test_lock_derived_expected_state_lifecycle.py) -> 462 passed / 46 passed. Added test_empty_mcp_target_servers_is_omitted_without_mcp_servers, test_constructed_target_servers_equal_their_read_back and test_sync_keeps_settings_with_user_keys[permissions|cursor-default-is-claude-user-key]. After the review folds, the targeted lifecycle and integration run (lane C, lock-derived, copilot root MCP, hooks execution, wave6, hook contraction, reinstall gates, MCP write receipts, gate removal) gives 549 passed, 6 xfailed, and the touched unit files give 602 passed.

Lint mirror: ruff check, ruff format --check, pylint R0801, scripts/lint-auth-signals.sh, scripts/lint-architecture-boundaries.sh, check_complexity_ratchet.py --base-ref origin/main, python -m tests.quality.taxonomy --changed -> all clean. python -m tests.lifecycle.generator --check -> up to date; known_cell_issues --write -> no drift. Ledger row tests/fixtures/lifecycle_bug_ledger/bugs/4852.json -> test_lifecycle_bug_ledger.py 2 passed.

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
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

… fresh deploy

Imported from danielmeppiel-w4-t7-lane-c-differential @ b050939 (W4).
Three cells are strict known failures of #4852 until the fix lands.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…try leaves (#4852)

Retiring APM's last entry left .mcp.json as {"mcpServers": {}} or
.claude/settings.json as {}, and the lock kept an empty
mcp_target_servers; a fresh deploy of the same inputs writes none of them.

- MCP cleanup (copilot and claude project .mcp.json) deletes the file
  once this cleanup removed every server in it and only empty
  scaffolding remains, as the LSP config and #4524 do.
- Merged-hook cleanup deletes a hook JSON left with only the native
  defaults APM injects (Cursor's version: 1).
- The lock omits an empty mcp_target_servers once no MCP server is left.
- A file holding the user's own entries or keys stays.

Flips the three strict Known(#4852) lane C cells and adds user-file
guard cells.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.38.0 milestone Nov 23, 2026
Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:14
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

Cleanup can delete pre-existing configuration files and user-owned keys without proven APM ownership, while lockfile serialization also breaks equality-based no-change detection.

5 open issues
What changed in this PR

This PR addresses #4852 by cleaning up empty MCP and hook configuration files after APM retires their last managed entry, aiming to make incremental installs match fresh deployments.

Changes:

  • Adds deletion paths for emptied MCP and merged-hook JSON configurations.
  • Omits empty MCP ownership metadata when no servers remain and adjusts lockfile equivalence.
  • Adds differential lifecycle coverage, preservation controls, documentation, and a changelog entry.
File Description
tests/​unit/​test_claude_mcp.py Expects emptied project MCP files to disappear.
tests/​unit/​integration/​test_hook_integrator.py Tests empty hook-file deletion and user-key preservation.
tests/​unit/​integration/​test_hook_integrator_reconcile_dropped_targets.py Updates dropped-target cleanup expectations.
tests/​unit/​integration/​test_cursor_hook_native_contract.py Expects emptied hook files to disappear after target contraction.
tests/​unit/​install/​test_mcp_ownership.py Checks that cleanup preserves user servers.
tests/​unit/​install/​test_mcp_lockfile_determinism.py Tests omission and equivalence of empty ownership metadata.
tests/​lifecycle/​security/​test_incremental_deploy_differential.py Compares incremental and fresh deployments; checks user-content preservation.
tests/​lifecycle/​scenarios/​_deploy_diff.py Supplies shared deployment scenarios and state-comparison helpers.
tests/​integration/​test_lock_derived_expected_state_lifecycle.py Allows deletion of emptied native configurations.
tests/​integration/​test_copilot_root_mcp_json_lifecycle.py Updates root MCP retirement expectations.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4852.json Records the bug and regression coverage.
src/​apm_cli/​integration/​mcp_integrator.py Adds deletion logic for emptied project MCP JSON.
src/​apm_cli/​integration/​hook_integrator.py Adds deletion logic for cleaned hook JSON.
src/​apm_cli/​deps/​lockfile.py Adjusts ownership-map serialization and semantic comparison.
docs/​src/​content/​docs/​reference/​lockfile-spec.md Documents empty ownership-map omission.
changelog.d/​4862.fixed.md Describes the user-visible cleanup change.

Review effort: Balanced

Comment thread src/apm_cli/deps/lockfile.py Resolved
…ts (#4852)

Review folds:
- A merged hook file is deleted only when what remains equals that
  target's own injected defaults (Cursor's version: 1 is a user key in
  Claude settings).
- .mcp.json is deleted only when no key other than mcpServers remains,
  even an empty-valued user key keeps it.
- LockFile serialization and equivalence share one predicate
  (records_mcp_target_servers), so a non-empty map compares equal
  whatever its presence flag.

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

Cleanup lacks path-containment checks and can delete an external settings.json through a symlinked .claude directory, while the MCP retirement test can pass without deploying the managed entry.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Assert MCP retirement removes only the managed server

tests/​lifecycle/​security/​test_incremental_deploy_differential.py:216

In the mcp-user-server case, deployed.get(container) is already truthy because the fixture contains my-server. All subsequent config assertions also pass if the file remains unchanged and p-exec-mcp was never deployed, so this guard can succeed without exercising MCP retirement at all. Assert that the specific managed entry is present before revoking approval so the test actually proves cleanup preserves the user's server.

5 resolved since last review

Review effort: Balanced

…state (#4852)

- _clean_apm_entries_from_json keeps its write_seam.open_for_write write
  (the raw-open-for-write owner-rule mutation targets it); deletion goes
  through write_seam.unlink behind _only_target_defaults.
- Lock unit tests assert on the written lock text (AQ003).
- test_ownership_siblings reads .mcp.json only while it exists.

apm-spec-waiver: config cleanup residue only; no OpenAPM-observable requirement changes

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Package-target hook reconcile (executables gate, target restriction)
now returns deleted_paths, and install names them under
"Cleaned N emptied hook config file(s) for <pkg>:" so no deletion is
silent. Updates the two lifecycle tests that read the now-deleted
.mcp.json / .claude/settings.json to expect absence.

apm-spec-waiver: config cleanup residue only; no OpenAPM-observable requirement changes

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
hook_file_emptied (hook_ownership) refuses deletion when the target
directory or the file is a symlink; the cleanup rewrites it in place as
before. The MCP user-file guard cell now asserts p-exec-mcp itself was
deployed before the change.

apm-spec-waiver: config cleanup residue only; no OpenAPM-observable requirement changes

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

Cleanup can still delete an external hooks.json through a symlinked ancestor and discard a user-modified Cursor config because boolean true is treated as version 1.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

High severity Check symlinked ancestors of user-scoped hooks

src/​apm_cli/​integration/​hook_ownership.py:672

For user-scoped Windsurf hooks at ~/.codeium/windsurf/hooks.json, this check uses .codeium as its base, which has_symlink_component does not inspect. If .codeium is a symlink, retiring the last hook can therefore delete the external hooks.json despite the new guard. Pass the actual project or user containment root through both cleanup paths to this helper, and cover a symlinked ancestor of a nested user target.

Medium severity Require matching types when removing Cursor skeleton values

src/​apm_cli/​integration/​hook_ownership.py:674

Python considers True == 1, so a cleaned Cursor config containing {"version": true} is treated as the injected {"version": 1} skeleton. If a user changes that value and then removes Cursor or uninstalls, both cleanup paths delete the file instead of preserving the user value. Require matching types as well as values before treating a key as disposable.

1 resolved since last review

Review effort: Balanced

…efaults (#4852)

hook_file_emptied takes the project or home containment root from every
caller, so a symlinked ancestor of a nested user target (for example
~/.codeium) blocks deletion; with no root it never deletes. A key matches
an injected default only with the same type, so a user's
{"version": true} keeps a Cursor hooks.json.

apm-spec-waiver: config cleanup residue only; no OpenAPM-observable requirement changes

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 empty-configuration cleanup change.

No open issues 🎉

Review effort: Balanced

…reated to be gone (#4852)

Integration anti-oracles read the file after APM's last entry left;
they now assert absence (or read an absent file as empty).

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 hook cleanup can delete user-owned configuration, and missing static guardrails leave cleanup and lock-field decisions vulnerable to divergence.

No open issues 🎉

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

Cleanup incorrectly rejects legitimate CLAUDE_CONFIG_DIR paths outside HOME, leaving an empty settings file when retiring the last hook instead of deleting it.

1 open issue

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 25f5321 into main Nov 23, 2026
74 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the cloud-4852-empty-config-residue branch November 23, 2026 08:20
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
Gregory (gdelpu) pushed a commit to gdelpu/apm that referenced this pull request Nov 23, 2026
…last server

microsoft#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]>
Gregory (gdelpu) pushed a commit to gdelpu/apm that referenced this pull request Nov 23, 2026
…s empty-config removal

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] Retiring the last APM entry leaves an empty .mcp.json / .claude/settings.json and an empty mcp_target_servers lock field

2 participants