Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
A background item's page asks nothing of macOS on the main thread
Țugui Dragoș5893b9439d7a
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swiftindex b94f8147..bdaf56cc 100644--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift@@ -33,7 +33,8 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {public let keepsAlive: Boolpublic let ownerBundleIdentifier: String?public let ownerName: String?- public let isOwnerInstalled: Bool+ /// Where the app that owns the job is, or nil when no app does or it is gone.+ public let ownerURL: URL?/// True when no installed app claims the job and its program is gone from disk, or it names none of its own./// The full rule is `BackgroundItemOwnership.isOrphan(label:program:owner:)`.public let isOrphan: Bool@@ -47,6 +48,8 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {/// What in the job's command is worth a second look, if anything.public var unusualCommand: UnusualCommand?+ public var isOwnerInstalled: Bool { ownerURL != nil }+/// Includes the file's path, because two files can declare one label (a per-user copy beside the/// system-wide one), and each file needs a row of its own.public var id: String { "\(kind.rawValue)/\(label)/\(plistURL?.path(percentEncoded: false) ?? "")" }diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swiftindex 4033a52a..2f0f309a 100644--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift@@ -1,4 +1,4 @@-import Foundation+public import Foundationimport PeelPrivileged/// Finds the app a launchd job belongs to. Every clue is tried before a job is called orphaned, because@@ -7,10 +7,13 @@ public struct BackgroundItemOwnership: Sendable {public struct Owner: Sendable, Hashable {public let bundleIdentifier: Stringpublic let name: String?- public let isInstalled: Bool+ /// Where the app is, or nil when it is gone.+ public let url: URL?/// True when macOS registered the job for the app, or its program is signed by the app's team or sits/// inside the app. False when only a name points to the app: a label, or a bundle anyone can make.public var isConfirmed = true++ public var isInstalled: Bool { url != nil }}private let apps: [InstalledApp]@@ -47,7 +50,7 @@ public struct BackgroundItemOwnership: Sendable {}let team = program.flatMap(teamOfProgram)if let team, let app = associated.lazy.compactMap({ byIdentifier[$0.lowercased()] }).first(where: { $0.teamIdentifier == team }) {- return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, isInstalled: true)+ return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, url: app.url)}if let identifier = Self.bundleIdentifier(inProgramPath: program) {var owner = owner(forIdentifier: identifier)@@ -55,15 +58,15 @@ public struct BackgroundItemOwnership: Sendable {return owner}if let app = appMatching(label: label) {- return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, isInstalled: true, isConfirmed: proves(program, app, team: team))+ return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, url: app.url, isConfirmed: proves(program, app, team: team))}if let team, let candidates = byTeam[team], candidates.count == 1 {- return Owner(bundleIdentifier: candidates[0].bundleIdentifier, name: candidates[0].name, isInstalled: true)+ return Owner(bundleIdentifier: candidates[0].bundleIdentifier, name: candidates[0].name, url: candidates[0].url)}// No installed app claims the job. An app the plist names that is gone is still returned, so the user// can see which app it was. That app is not installed, so it keeps nothing from being called orphaned.let gone = associated.first { byIdentifier[$0.lowercased()] == nil && AppInspector.applicationURL(forBundleIdentifier: $0) == nil }- return gone.map { Owner(bundleIdentifier: $0, name: nil, isInstalled: false, isConfirmed: false) }+ return gone.map { Owner(bundleIdentifier: $0, name: nil, url: nil, isConfirmed: false) }}/// Whether a job is orphaned: no installed app claims it, and its program is gone from disk or it names@@ -94,14 +97,10 @@ public struct BackgroundItemOwnership: Sendable {/// is asked where it is before the owner is called not installed.private func owner(forIdentifier identifier: String) -> Owner {if let app = byIdentifier[identifier.lowercased()] {- return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, isInstalled: true)+ return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, url: app.url)}let elsewhere = AppInspector.applicationURL(forBundleIdentifier: identifier)- return Owner(- bundleIdentifier: identifier,- name: elsewhere?.deletingPathExtension().lastPathComponent,- isInstalled: elsewhere != nil- )+ return Owner(bundleIdentifier: identifier, name: elsewhere?.deletingPathExtension().lastPathComponent, url: elsewhere)}/// Whether `program` is `app`'s own: signed by its team, or inside its bundle.diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swiftindex 61815775..12aceeca 100644--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift@@ -91,7 +91,7 @@ public enum BackgroundItems {keepsAlive: false,ownerBundleIdentifier: owner.bundleIdentifier,ownerName: owner.name,- isOwnerInstalled: true,+ ownerURL: owner.url,isOrphan: false,state: .notLoaded,isDisabled: true,@@ -164,7 +164,7 @@ public enum BackgroundItems {keepsAlive: job.keepsAlive,ownerBundleIdentifier: owner?.bundleIdentifier,ownerName: owner?.name,- isOwnerInstalled: owner?.isInstalled ?? false,+ ownerURL: owner?.url,isOrphan: ownership.isOrphan(label: job.label, program: job.program, owner: owner),state: loaded.state(of: job.label, kind),isDisabled: loaded.override(of: job.label, kind) ?? job.isDisabled,@@ -192,7 +192,7 @@ public enum BackgroundItems {keepsAlive: false,ownerBundleIdentifier: owner?.bundleIdentifier,ownerName: owner?.name,- isOwnerInstalled: owner?.isInstalled ?? false,+ ownerURL: owner?.url,isOrphan: ownership.isOrphan(label: name, program: nil, owner: owner),state: .notLoaded,isDisabled: false,@@ -249,7 +249,7 @@ public enum BackgroundItems {keepsAlive: job?.keepsAlive ?? false,ownerBundleIdentifier: owner?.bundleIdentifier,ownerName: owner?.name,- isOwnerInstalled: owner?.isInstalled ?? false,+ ownerURL: owner?.url,isOrphan: ownership.isOrphan(label: candidate.label, program: program, owner: owner),state: loaded.state(of: candidate.label, candidate.kind),isDisabled: loaded.override(of: candidate.label, candidate.kind) ?? false,diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swiftindex 708a8c4d..046b958b 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift@@ -9,7 +9,7 @@ struct BackgroundItemActionsTests {BackgroundItem(label: "com.example.agent", kind: .agent, source: .userLibrary, plistURL: plist, program: nil,runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil, ownerName: nil,- isOwnerInstalled: false, isOrphan: false, state: state, isDisabled: false+ ownerURL: nil, isOrphan: false, state: state, isDisabled: false)}@@ -35,7 +35,7 @@ struct BackgroundItemActionsTests {BackgroundItem(label: label, kind: .agent, source: .userLibrary, plistURL: nil, program: nil,runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil, ownerName: nil,- isOwnerInstalled: false, isOrphan: false, state: .loaded, isDisabled: false+ ownerURL: nil, isOrphan: false, state: .loaded, isDisabled: false)}let refused = BackgroundItemActions.Failure.launchctl(HelperRefusal.invalidRequest.rawValue)@@ -50,7 +50,7 @@ struct BackgroundItemActionsTests {BackgroundItem(label: label, kind: kind, source: .systemLibrary, plistURL: nil, program: nil,runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil, ownerName: nil,- isOwnerInstalled: false, isOrphan: false, state: .loaded, isDisabled: false+ ownerURL: nil, isOrphan: false, state: .loaded, isDisabled: false)}@@ -69,7 +69,7 @@ struct BackgroundItemActionsTests {BackgroundItem(label: "com.example.agent", kind: source == .systemLibrary ? .daemon : .agent, source: source,plistURL: plist, program: nil, runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil,- ownerName: nil, isOwnerInstalled: false, isOrphan: false, state: state, isDisabled: false+ ownerName: nil, ownerURL: nil, isOrphan: false, state: state, isDisabled: false)}@@ -137,7 +137,7 @@ struct BackgroundItemActionsTests {BackgroundItem(label: label, kind: kind, source: kind == .daemon ? .systemLibrary : .userLibrary,plistURL: nil, program: nil, runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil,- ownerName: nil, isOwnerInstalled: false, isOrphan: false, state: .notLoaded, isDisabled: false+ ownerName: nil, ownerURL: nil, isOrphan: false, state: .notLoaded, isDisabled: false)}@@ -311,10 +311,23 @@ struct DeclaredBackgroundItemsTests {#expect(item.state == .notLoaded)#expect(item.source == .app)#expect(item.ownerBundleIdentifier == "org.example.app")+ #expect(item.ownerURL == app.url)}#expect(items.first { $0.label == "org.example.app.daemon" }?.kind == .daemon)}+ @Test func knowsWhereEachJobsOwnerIs() throws {+ let directory = try TemporaryDirectory()+ try directory.file("home/Library/LaunchAgents/org.example.app.helper.plist", contents: job("org.example.app.helper"))+ try directory.file("home/Library/LaunchAgents/org.example.gone.helper.plist", contents: job("org.example.gone.helper"))+ let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "org.example.app", name: "Example")++ let items = BackgroundItems.declared(in: environment(directory), ownership: BackgroundItemOwnership(installedApps: [app]))++ #expect(items.first { $0.label == "org.example.app.helper" }?.ownerURL == app.url)+ #expect(items.first { $0.label == "org.example.gone.helper" }?.ownerURL == nil)+ }+/// One label declared in two folders is two files, and each row opens its own.@Test func keepsTwoFilesThatDeclareOneLabelApart() throws {let directory = try TemporaryDirectory()diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swiftindex fbb43f83..5dc5222b 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift@@ -23,7 +23,7 @@ struct BackgroundItemOwnershipTests {func item(_ label: String, _ kind: BackgroundItem.Kind) -> BackgroundItem {BackgroundItem(label: label, kind: kind, source: .app, plistURL: nil, program: nil, runsAtLoad: false, keepsAlive: false,- ownerBundleIdentifier: nil, ownerName: nil, isOwnerInstalled: false, isOrphan: false, state: .loaded, isDisabled: false+ ownerBundleIdentifier: nil, ownerName: nil, ownerURL: nil, isOrphan: false, state: .loaded, isDisabled: false)}let helper = item(HelperIdentity.helperIdentifier, .daemon)@@ -99,6 +99,16 @@ struct BackgroundItemOwnershipTests {#expect(!planted.isConfirmed)}+ @Test func knowsWhereTheOwnerIs() {+ #expect(ownership.owner(label: "com.other.tool.helper", associated: [], program: nil)?.url == URL(filePath: "/Applications/Tool.app"))+ let associated = ownership.owner(label: "org.unrelated.label", associated: ["com.example.app"], program: "/usr/local/bin/example-helper")+ #expect(associated?.url == URL(filePath: "/Applications/Example.app"))+ let elsewhere = ownership.owner(label: "org.unrelated.label", registeredBy: "com.apple.CharacterPaletteIM", associated: [], program: nil)+ #expect(elsewhere?.url?.lastPathComponent == "CharacterPalette.app")+ #expect(elsewhere?.isInstalled == true)+ #expect(ownership.owner(label: "com.missing.app.agent", registeredBy: "com.missing.app", associated: [], program: nil)?.url == nil)+ }+@Test func fallsBackToTheLabelWhenNothingElseMatches() {let owner = ownership.owner(label: "com.other.tool.helper", associated: [], program: nil)#expect(owner?.bundleIdentifier == "com.other.tool")diff --git a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swiftindex 5e9dda27..38034318 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift@@ -252,7 +252,7 @@ struct IntelInspectorTests {BackgroundItem(label: label, kind: .agent, source: .userLibrary, plistURL: nil,program: program.path(percentEncoded: false), runsAtLoad: false, keepsAlive: false,- ownerBundleIdentifier: nil, ownerName: nil, isOwnerInstalled: false, isOrphan: false,+ ownerBundleIdentifier: nil, ownerName: nil, ownerURL: nil, isOrphan: false,state: .loaded, isDisabled: false)}diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swiftindex 116e41f2..a2d70d9e 100644--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift@@ -11,10 +11,8 @@ struct BackgroundItemDetailView: View {@Environment(ExclusionsStore.self) private var exclusions@Environment(\.openSettings) private var openSettings@State private var isConfirmingTrash = false- /// Reading this and `ownerIconURL` asks macOS, so both are read once per item rather than on every- /// evaluation of `body`.+ /// Reading this asks macOS, so it is read once per item rather than on every evaluation of `body`.@State private var legacyStatus: LocalizedStringResource?- @State private var ownerIconURL: URL?let item: BackgroundItemvar body: some View {@@ -166,8 +164,7 @@ struct BackgroundItemDetailView: View {.motion(value: item.state).motion(value: item.isDisabled).task(id: item.id) {- legacyStatus = status(ofLegacyPlistOf: item)- ownerIconURL = iconURL(ofOwnerOf: item)+ legacyStatus = await Self.legacyStatus(of: item)}.navigationTitle(item.label).toolbar(removing: .title)@@ -208,19 +205,11 @@ struct BackgroundItemDetailView: View {backgroundItems.runningActionItemIDs.contains(item.id) || backgroundItems.isScanning}- /// Returns the URL of the app that owns `item`, for its icon. That app may sit outside the folders Peel scans,- /// so macOS is asked where it is when `library` doesn't list it.- private func iconURL(ofOwnerOf item: BackgroundItem) -> URL? {- if let app = library.apps.first(where: { $0.bundleIdentifier == item.ownerBundleIdentifier }) {- return app.url- }- guard let identifier = item.ownerBundleIdentifier else { return nil }- return AppInspector.applicationURL(forBundleIdentifier: identifier)- }-/// Returns the job's status in System Settings (Login Items & Extensions), or nil when it has no plist file./// Per `SMAppService.h`, `.requiresApproval` also means the user turned the job off, so the text says both.- private func status(ofLegacyPlistOf item: BackgroundItem) -> LocalizedStringResource? {+ /// Off the main thread: `SMAppService` asks Background Task Management over XPC.+ @concurrent+ private static func legacyStatus(of item: BackgroundItem) async -> LocalizedStringResource? {guard let plistURL = item.plistURL else { return nil }return switch SMAppService.statusForLegacyPlist(at: plistURL) {case .enabled: "Allowed"@@ -233,8 +222,8 @@ struct BackgroundItemDetailView: View {private var header: some View {HStack(spacing: 18) {- if let ownerIconURL {- AppIcon(url: ownerIconURL)+ if let ownerURL = item.ownerURL {+ AppIcon(url: ownerURL).frame(width: 56, height: 56)} else {Image(systemName: "gearshape.2")