Repository navigation
Feat/generic orphan service detection - #838
Conversation
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.
There was a problem hiding this comment.
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_serviceswith plistProgram/ProgramArguments[0]binary-existence detection plus a force-protect pattern list. - Add
clean_orphaned_container_stubsto remove allowlisted~/Library/Containersstub directories when the backing app is not installed. - Wire the new container-stub cleanup into the main
perform_cleanupflow 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. |
| # 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 |
There was a problem hiding this comment.
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.
| # 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. |
| # If the canonical app path exists, the app is installed — skip. | ||
| [[ -d "$app_path" ]] && continue |
| mkdir -p "/Applications/CleanMyMac X.app" 2>/dev/null || true | ||
|
|
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.
* 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 ---------
* 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]>
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_patternswith a newknown_protect_patternslist, 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_orphanedto 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
clean_orphaned_container_stubs, a new function that removes stub-only~/Library/Containersdirectories 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
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)