Skip to content

Feat/generic orphan service detection - #838

Merged
tw93 merged 5 commits into
tw93:mainfrom
AmanSikarwar:feat/generic-orphan-service-detection
May 5, 2026
Merged

tw93 merged 5 commits into
tw93:mainfrom
AmanSikarwar:feat/generic-orphan-service-detection

Conversation

@AmanSikarwar

@AmanSikarwar AmanSikarwar commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

This pull request significantly improves the orphaned system service and container cleanup logic in lib/clean/apps.sh, making detection more accurate and safer. It introduces a new function to remove stub-only app containers left behind by uninstalled apps, refines how orphaned system services are detected (now based on missing binaries rather than hardcoded patterns), and updates the test suite to cover these new behaviors.

Key changes:

Orphaned system service cleanup improvements

  • Replaces the old hardcoded known_orphan_patterns with a new known_protect_patterns list, which is now only used to prevent false positives for known apps. The detection of orphaned services is now performed generically by checking if the backing binary referenced in each plist actually exists, and whether it is managed by a package manager or system directory. (lib/clean/apps.sh) [1] [2] [3] [4] [5] [6] [7] [8]

  • Adds helper functions _plist_binary_path, _is_package_managed_binary, and _plist_is_orphaned to encapsulate the new detection logic for orphaned plists. (lib/clean/apps.sh)

  • Ensures that services confirmed as orphaned via missing binaries are not re-blocked by conservative path protection rules, reducing false negatives. (lib/clean/apps.sh)

Orphaned container stub cleanup

  • Introduces clean_orphaned_container_stubs, a new function that removes stub-only ~/Library/Containers directories left behind by uninstalled apps (currently targeting CleanMyMac X variants). These are only removed if the app is truly uninstalled and no user data is present. (lib/clean/apps.sh)

Test coverage

  • Adds comprehensive BATS tests for clean_orphaned_container_stubs, covering cases where a stub is removed, preserved due to the app being installed, and preserved when user data exists. (tests/clean_apps.bats)

Replace the hardcoded-list-only approach in clean_orphaned_system_services
with generic plist binary-existence checking — the same signal macOS uses
to show grey icons in System Settings > Login Items & Extensions.

Detection strategy (LaunchDaemons + LaunchAgents):
- Read Program/ProgramArguments[0] from each plist via PlistBuddy
- If the binary is missing AND not in a package-manager path → orphan
- Package-manager paths (/usr/local/bin, /opt/homebrew, /usr/bin, etc.)
  are always skipped to avoid false positives for Homebrew-managed daemons

Force-protect list (known_protect_patterns) replaces known_orphan_patterns:
- Now purely protective: if a matched app IS installed, skip removal
- Covers Docker, NetBird, Homebrew services, Zoom, Cindori, Wireshark,
  CleanMyMac, Sogou, ClashX/Mac, Nektony, i4tools, remot3.it, etc.

PrivilegedHelperTools: unchanged — continues to use bundle_has_installed_app
with the force-protect list now guarding known-good helpers first.

Binary-confirmed orphans bypass should_protect_path: the DATA_PROTECTED_BUNDLES
list is conservative (e.g. us.zoom.updater*) and would re-block removal of
plists whose backing app is already confirmed uninstalled.

Verified on real system:
- Detected: Wireshark ChmodBPF, Cindori TEHelper, remot3.it CLI,
  Zoom updater LaunchAgents (x2) — all confirmed orphans
- Not flagged: netbird (binary in /usr/local/bin → package-managed),
  Docker vmnetd (binary exists), Cloudflare WARP (app installed),
  homebrew.mxcl.httpd (Homebrew-managed)

Also adds clean_orphaned_container_stubs (stub-only ~/Library/Containers
left by uninstalled apps) and wires it into the App leftovers section.
Generic binary-existence detection already handles org.cindori.TEHelper
correctly: if any Cindori app (Lungo, Silenz, Turbo Boost Switcher) is
installed it keeps the PrivilegedHelperTool binary present, so the plist
is not flagged as orphaned. The multi-path entry was only needed under
the old pattern-match-only approach.
SC2206 is expected: the IFS='|' split on app_paths is deliberate,
allowing pipe-separated candidate app paths in _system_service_app_exists.
@AmanSikarwar
AmanSikarwar requested a review from tw93 as a code owner May 4, 2026 19:15
Copilot AI review requested due to automatic review settings May 4, 2026 19:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Mole’s “App leftovers” cleanup to detect orphaned system services generically by inspecting LaunchDaemon/LaunchAgent plists for missing executables, adds a protective allowlist to avoid known false positives, and introduces cleanup for stub-only sandbox container directories left behind after uninstall.

Changes:

  • Replace hardcoded orphan pattern matching in clean_orphaned_system_services with plist Program/ProgramArguments[0] binary-existence detection plus a force-protect pattern list.
  • Add clean_orphaned_container_stubs to remove allowlisted ~/Library/Containers stub directories when the backing app is not installed.
  • Wire the new container-stub cleanup into the main perform_cleanup flow and add Bats tests for the new behavior.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 5 comments.

File Description
lib/clean/apps.sh Implements generic orphan system-service detection and adds container-stub cleanup logic.
bin/clean.sh Runs clean_orphaned_container_stubs during the “App leftovers” section.
tests/clean_apps.bats Adds tests validating container-stub cleanup removal/preservation behavior.

Comment thread lib/clean/apps.sh Resolved
Comment thread lib/clean/apps.sh
Comment on lines +532 to +540
# Read the program binary from a plist (Program or ProgramArguments[0]).
# Prints the path; returns 1 if no Program key found.
_plist_binary_path() {
local plist="$1"
local binary=""
binary=$(/usr/libexec/PlistBuddy -c "Print :ProgramArguments:0" "$plist" 2>/dev/null || true)
if [[ -z "$binary" ]]; then
binary=$(/usr/libexec/PlistBuddy -c "Print :Program" "$plist" 2>/dev/null || true)
fi

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS requires system LaunchDaemons/LaunchAgents plists to be world-readable so launchd (which runs as root) can validate them, but also so that admin tools and diagnostic utilities can inspect them without privilege escalation. A plist that isn't world-readable would be rejected by launchd itself.

No code change needed.

Comment thread lib/clean/apps.sh
Comment on lines +787 to +789
# Remove stub-only ~/Library/Containers directories left by uninstalled apps.
# A stub container contains only .com.apple.containermanagerd.metadata.plist
# with no Data/ subdirectory — it holds no user data and is safe to remove.
Comment thread lib/clean/apps.sh Outdated
Comment on lines +815 to +816
# If the canonical app path exists, the app is installed — skip.
[[ -d "$app_path" ]] && continue
Comment thread tests/clean_apps.bats Outdated
Comment on lines +748 to +749
mkdir -p "/Applications/CleanMyMac X.app" 2>/dev/null || true

AmanSikarwar and others added 2 commits May 5, 2026 00:56
Previously, removed_count was incremented unconditionally after
safe_remove ... || true, so the success message would report stubs
as removed even if safe_remove returned non-zero (e.g. permission
denied, validation failure). Track failures separately and only
count a removal when safe_remove returns 0. Surface failed_count
in the output so the user knows if any stubs could not be cleaned.
@tw93
tw93 merged commit 3fe70f4 into tw93:main May 5, 2026
tw93 pushed a commit that referenced this pull request May 6, 2026
* clean: generic binary-existence detection for orphaned system services

Replace the hardcoded-list-only approach in clean_orphaned_system_services
with generic plist binary-existence checking — the same signal macOS uses
to show grey icons in System Settings > Login Items & Extensions.

Detection strategy (LaunchDaemons + LaunchAgents):
- Read Program/ProgramArguments[0] from each plist via PlistBuddy
- If the binary is missing AND not in a package-manager path → orphan
- Package-manager paths (/usr/local/bin, /opt/homebrew, /usr/bin, etc.)
  are always skipped to avoid false positives for Homebrew-managed daemons

Force-protect list (known_protect_patterns) replaces known_orphan_patterns:
- Now purely protective: if a matched app IS installed, skip removal
- Covers Docker, NetBird, Homebrew services, Zoom, Cindori, Wireshark,
  CleanMyMac, Sogou, ClashX/Mac, Nektony, i4tools, remot3.it, etc.

PrivilegedHelperTools: unchanged — continues to use bundle_has_installed_app
with the force-protect list now guarding known-good helpers first.

Binary-confirmed orphans bypass should_protect_path: the DATA_PROTECTED_BUNDLES
list is conservative (e.g. us.zoom.updater*) and would re-block removal of
plists whose backing app is already confirmed uninstalled.

Verified on real system:
- Detected: Wireshark ChmodBPF, Cindori TEHelper, remot3.it CLI,
  Zoom updater LaunchAgents (x2) — all confirmed orphans
- Not flagged: netbird (binary in /usr/local/bin → package-managed),
  Docker vmnetd (binary exists), Cloudflare WARP (app installed),
  homebrew.mxcl.httpd (Homebrew-managed)

Also adds clean_orphaned_container_stubs (stub-only ~/Library/Containers
left by uninstalled apps) and wires it into the App leftovers section.

* clean: remove hardcoded Cindori protect entry

Generic binary-existence detection already handles org.cindori.TEHelper
correctly: if any Cindori app (Lungo, Silenz, Turbo Boost Switcher) is
installed it keeps the PrivilegedHelperTool binary present, so the plist
is not flagged as orphaned. The multi-path entry was only needed under
the old pattern-match-only approach.

* clean: add shellcheck disable for intentional IFS word-split

SC2206 is expected: the IFS='|' split on app_paths is deliberate,
allowing pipe-separated candidate app paths in _system_service_app_exists.

* clean: only increment removed_count on successful safe_remove

Previously, removed_count was incremented unconditionally after
safe_remove ... || true, so the success message would report stubs
as removed even if safe_remove returned non-zero (e.g. permission
denied, validation failure). Track failures separately and only
count a removal when safe_remove returns 0. Surface failed_count
in the output so the user knows if any stubs could not be cleaned.

* fix: tighten orphan service cleanup safety

---------
tw93 pushed a commit that referenced this pull request May 7, 2026
* clean: generic binary-existence detection for orphaned system services

Replace the hardcoded-list-only approach in clean_orphaned_system_services
with generic plist binary-existence checking — the same signal macOS uses
to show grey icons in System Settings > Login Items & Extensions.

Detection strategy (LaunchDaemons + LaunchAgents):
- Read Program/ProgramArguments[0] from each plist via PlistBuddy
- If the binary is missing AND not in a package-manager path → orphan
- Package-manager paths (/usr/local/bin, /opt/homebrew, /usr/bin, etc.)
  are always skipped to avoid false positives for Homebrew-managed daemons

Force-protect list (known_protect_patterns) replaces known_orphan_patterns:
- Now purely protective: if a matched app IS installed, skip removal
- Covers Docker, NetBird, Homebrew services, Zoom, Cindori, Wireshark,
  CleanMyMac, Sogou, ClashX/Mac, Nektony, i4tools, remot3.it, etc.

PrivilegedHelperTools: unchanged — continues to use bundle_has_installed_app
with the force-protect list now guarding known-good helpers first.

Binary-confirmed orphans bypass should_protect_path: the DATA_PROTECTED_BUNDLES
list is conservative (e.g. us.zoom.updater*) and would re-block removal of
plists whose backing app is already confirmed uninstalled.

Verified on real system:
- Detected: Wireshark ChmodBPF, Cindori TEHelper, remot3.it CLI,
  Zoom updater LaunchAgents (x2) — all confirmed orphans
- Not flagged: netbird (binary in /usr/local/bin → package-managed),
  Docker vmnetd (binary exists), Cloudflare WARP (app installed),
  homebrew.mxcl.httpd (Homebrew-managed)

Also adds clean_orphaned_container_stubs (stub-only ~/Library/Containers
left by uninstalled apps) and wires it into the App leftovers section.

* clean: remove hardcoded Cindori protect entry

Generic binary-existence detection already handles org.cindori.TEHelper
correctly: if any Cindori app (Lungo, Silenz, Turbo Boost Switcher) is
installed it keeps the PrivilegedHelperTool binary present, so the plist
is not flagged as orphaned. The multi-path entry was only needed under
the old pattern-match-only approach.

* clean: add shellcheck disable for intentional IFS word-split

SC2206 is expected: the IFS='|' split on app_paths is deliberate,
allowing pipe-separated candidate app paths in _system_service_app_exists.

* clean: only increment removed_count on successful safe_remove

Previously, removed_count was incremented unconditionally after
safe_remove ... || true, so the success message would report stubs
as removed even if safe_remove returned non-zero (e.g. permission
denied, validation failure). Track failures separately and only
count a removal when safe_remove returns 0. Surface failed_count
in the output so the user knows if any stubs could not be cleaned.

* fix: tighten orphan service cleanup safety

---------

Co-authored-by: Codex <[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.

3 participants