apm icon indicating copy to clipboard operation
apm copied to clipboard

[BUG] uninstall orphans tracked files when removed package target differs from surviving manifest targets

Open benizzio-opencode opened this issue 3 months ago • 1 comments

Describe the bug

When a global manifest already persists one target, a later package can be installed to another target with --target. APM deploys that package correctly, but apm uninstall --global resolves cleanup targets only from the surviving manifest after removing the package.

If the removed package's deployment target is absent from that surviving target set, its files are not visited. Uninstall still exits successfully and deletes the package's manifest, module, lockfile, and deployment-ledger entries. The files become untracked orphans with no warning and cannot be cleaned by rerunning uninstall because the package is no longer declared.

I reproduced this with a surviving agent-skills package and a removed OpenCode agent. The issue is not OpenCode-specific in the observed logic; OpenCode makes the mismatch easy to demonstrate because its user-scope agent path is distinct from .agents/skills/.

To Reproduce

Create two minimal local packages under an isolated home:

ROOT="$(mktemp -d)"
export HOME="$ROOT/home"
mkdir -p "$HOME" "$ROOT/work"
mkdir -p "$ROOT/survivor/.apm/skills/survivor"
mkdir -p "$ROOT/removed/.apm/agents"

cat > "$ROOT/survivor/apm.yml" <<'EOF'
name: survivor
version: "1.0.0"
description: Keeps agent-skills as the persisted global target.
author: repro
license: MIT
dependencies:
  apm: []
  mcp: []
includes:
  - .apm/skills/
scripts: {}
EOF

cat > "$ROOT/survivor/.apm/skills/survivor/SKILL.md" <<'EOF'
---
name: survivor
description: Remains installed during the reproduction.
---

# Survivor
EOF

cat > "$ROOT/removed/apm.yml" <<'EOF'
name: removed
version: "1.0.0"
description: Deploys one agent to a different global target.
author: repro
license: MIT
dependencies:
  apm: []
  mcp: []
includes:
  - .apm/agents/
scripts: {}
EOF

cat > "$ROOT/removed/.apm/agents/orphan.agent.md" <<'EOF'
---
name: orphan-agent
description: Must be removed when its package is uninstalled.
---

This agent should not survive package removal.
EOF

cd "$ROOT/work"
apm install "$ROOT/survivor" --target agent-skills --global
apm install "$ROOT/removed" --target opencode --global

At this point ~/.apm/apm.yml still declares only:

targets:
  - agent-skills

The second install correctly created ~/.config/opencode/agents/orphan.md, and the removed dependency's lock entry and deployment ledger recorded that path.

Uninstall the second package:

apm uninstall "$ROOT/removed" --global --verbose

The command exits successfully:

[i] Uninstalling from user scope (~/.apm/)
[>] Uninstalling 1 package(s)...
[+] _local/removed - found in apm.yml
[i] Removed _local/removed from dependencies.apm in apm.yml
[*] Updated ~/.apm/apm.yml (removed 1 package(s))
[i] Removed _local/removed from apm_modules/
    Path: _local/removed
[*] Uninstall complete: Removed 1 package(s) from apm.yml, Removed 1 package(s) from apm_modules/

Afterward:

test -f "$HOME/.config/opencode/agents/orphan.md"  # still succeeds

The survivor skill remains as expected. The removed package is absent from apm.yml, apm.lock.yaml, and apm_modules, and the lockfile no longer mentions .config/opencode/agents/orphan.md.

Expected behavior

Uninstall should remove every tracked file owned only by the removed package, including files deployed to a target that is not present in the post-removal manifest target set. If cleanup cannot be completed, uninstall should retain recoverable ownership state and return a non-zero result rather than reporting success.

Environment (please complete the following information):

  • OS: CachyOS Linux, x86_64
  • Python Version: 3.14.7
  • APM Version: 0.28.0 (e041462)
  • VSCode Version: not relevant

Logs

The complete relevant uninstall log is included in the reproduction above. No cleanup warning is emitted and no integrated-agent cleanup count is reported.

Additional context

In current src/apm_cli/commands/uninstall/engine.py, _sync_integrations_after_uninstall() resolves one _resolved_targets set from the post-removal apm_package.canonical_targets. all_deployed_files is partitioned across known target prefixes, but Phase 1 file cleanup iterates only _resolved_targets. Removed-dependency deployment-ledger targets do not augment that loop. Phase 2 survivor reintegration appropriately uses the surviving target set, but Phase 1 cleanup needs the removed deployment target provenance as well.

A fix could separate the Phase 1 cleanup target set, derived from tracked removed-package deployments, from the Phase 2 survivor target set, derived from the surviving manifest. This report concerns per-file tracked cleanup. It is related to target-scope issues such as #2250 and the orphan symptom in #2558, but has a different reproduction and root cause.

benizzio-opencode avatar Aug 22 '26 10:08 benizzio-opencode

Triage Panel Verdict

Issue: [BUG] uninstall orphans tracked files when removed package target differs from surviving manifest targets Author: @benizzio-opencode -- thank you for the detailed, well-reproduced report with root-cause analysis.


Panel synthesis

Verdict: ACCEPT -- confirmed bug, high priority

DevX UX Expert: This is a high-signal correctness failure. apm uninstall exits 0 while silently leaving deployed files on disk -- a direct violation of the package manager contract. The user cannot self-heal because the package entry has already been removed from apm.yml, apm.lock.yaml, and apm_modules. The orphaned files are invisible to any subsequent APM command.

Supply Chain Security Expert: The orphaned files include deployed agent/skill/MCP configuration files. From a security standpoint, a "removed" package's configuration continues to execute in the user's agent toolchain. This elevates the urgency: the user believes they have removed something, but the deployed artifacts remain active. This is a meaningful trust and integrity risk.

Python Architect: Root cause is precisely identified: _sync_integrations_after_uninstall() in src/apm_cli/commands/uninstall/engine.py resolves Phase 1 file cleanup from canonical_targets of the post-removal manifest. Targets present only in the removed package's deployment ledger are never visited. Fix: Phase 1 cleanup must union the removed package's deployment-ledger targets into its cleanup target set, independent of the Phase 2 survivor target set. This is a bounded, well-scoped change. Related to #2250 (target-scope issues) and #2558 (orphan symptom), but distinct root cause and distinct reproduction.

APM CEO Arbiter (synthesis): Accept with priority/high. Silent data loss + orphaned active agent configurations is a correctness and security issue. The report is complete, the reproduction is minimal and verifiable, the root cause is located, and the fix scope is bounded.


Triage decision

Dimension Decision
Type type/bug
Area area/cli
Priority priority/high
Theme theme/security
Status status/triaged
Milestone next patch release (maintainer to confirm)

Suggested next action: Assign to a contributor or maintainer to implement the Phase 1 / Phase 2 target-set separation in _sync_integrations_after_uninstall(). Add a regression test covering the multi-target install + uninstall scenario described in the reproduction. Consider whether a warning or non-zero exit should be emitted when cleanup targets cannot be fully resolved.


Triage status: agentic proposal pending human ratification. Silence is approval. Maintainers can:

  • Override any label or milestone above by editing it directly -- human edits are authoritative and will not be reverted on subsequent runs.
  • Re-trigger triage by applying the status/needs-triage label, or by removing status/triaged to enroll the issue in the next daily sweep.

Posted by the Triage Panel workflow. See .apm/skills/apm-triage-panel for the panel skill.

Generated by Triage Panel · 46 AIC · ⌖ 7.55 AIC · ⊞ 9.2K · ◷

github-actions[bot] avatar Aug 22 '26 12:08 github-actions[bot]