Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

54.6 KB
Raw Blame
import Foundation
import Observation
import PeelCore

enum AppSort: String, CaseIterable, Identifiable {
    case name
    case size
    case lastOpened
    case dateAdded

    var id: String { rawValue }

    var title: LocalizedStringResource {
        switch self {
        case .name: "Name"
        case .size: "Size"
        case .lastOpened: "Last Opened"
        case .dateAdded: "Date Added"
        }
    }
}

extension AppSource {
    var title: LocalizedStringResource {
        switch self {
        case .appStore: "App Store"
        case .homebrew: "Homebrew"
        case .setapp: LocalizedStringResource("Setapp", comment: "A product's name, never translated.")
        case .direct: "Downloaded"
        }
    }
}

/// The apps the Applications list shows: those with an update waiting, drawn first, and the rest.
struct VisibleApps {
    let waiting: [InstalledApp]
    let rest: [InstalledApp]

    var isEmpty: Bool { waiting.isEmpty && rest.isEmpty }
}

private enum UpdateMemoryStore {
    static func load(from defaults: UserDefaults) -> UpdateMemories {
        defaults.data(forKey: SettingsKey.updateMemory).flatMap(UpdateMemories.init(data:)) ?? UpdateMemories()
    }

    static func save(_ memory: UpdateMemories, to defaults: UserDefaults) {
        guard let data = memory.data else { return }
        defaults.set(data, forKey: SettingsKey.updateMemory)
    }
}

@Observable
final class AppLibrary {
    private static let concurrentUpdateChecks = 6
    private static let concurrentSizeReads = 4
    /// The number of months without being opened after which an app counts as unused.
    static let unusedMonths = 6

    private(set) var apps: [InstalledApp] = []
    /// Incremented whenever the apps listed change: one installed, removed, moved, or replaced by another build.
    /// Work is keyed to this rather than to `apps.count`, because an upgrade replaces a bundle without changing the
    /// count. When an app was last opened is not part of it, since opening an app changes nothing that is installed.
    private(set) var revision = 0
    /// Incremented when only the dates the apps were last opened changed, which the list shows, sorts by, and
    /// filters on.
    private(set) var lastOpenedRevision = 0
    private(set) var isLoading = false
    private(set) var hasLoaded = false
    /// The folders the person chose for apps, beside the Applications folders, or nil when their list cannot be read.
    /// `PeelApp` watches them and reads the apps again when they change.
    private(set) var folders: [URL]?
    /// The folders on the list that are no longer read, which Settings names.
    private(set) var foldersLeftOut: [AppFolders.LeftOut] = []
    /// Counts the changes the person makes to that list, so a reading that began before one never shows its older list.
    @ObservationIgnored private var folderChanges = 0
    private(set) var couldNotSaveFolders = false
    /// Why the last change of that list couldn't be saved, when Peel knows.
    private(set) var whyFoldersNotSaved: FileProblem?
    /// What the last reading of the folders could not read, so the apps in it are missing from `apps`
    /// (`AppScan.unreadable`).
    private(set) var unreadable: [URL] = []
    /// Whether Full Disk Access would open what the last reading could not read.
    private(set) var unreadableNeedsFullDiskAccess = false
    /// Whether the last reading could not read the list of chosen folders (`AppScan.listOfFoldersUnreadable`).
    private(set) var listOfFoldersUnreadable = false
    private(set) var whyListOfFoldersUnreadable: FileProblem?

    /// Whether the last reading read every app, which Orphaned Files waits for.
    var readEverything: Bool { listing.readEverything }
    /// True while a reading the person asked for runs, the first or Rescan's: the list tells VoiceOver only those, and
    /// the readings Peel starts on its own change it quietly.
    var isReadingAsAsked: Bool { scanRun.isRunning && askedForReading }
    private var askedForReading = false
    private(set) var updateStatuses: [InstalledApp.ID: UpdateStatus] = [:] {
        didSet { updatesRevision += 1 }
    }

    /// Incremented by anything that changes which apps have an update waiting, so the list redraws.
    private(set) var updatesRevision = 0
    private var updateRounds = UpdateRounds<InstalledApp.ID>() {
        didSet {
            if updateRounds.checking.isEmpty {
                heldUpdates = nil
            } else if oldValue.checking.isEmpty {
                heldUpdates = Set(apps.filter(hasUpdate).map(\.id))
            }
        }
    }
    var appsCheckingForUpdates: Set<InstalledApp.ID> { updateRounds.checking }
    /// Whether a round has asked about apps since Peel opened. The first one also asks about every app whose answer is
    /// a day old, whatever its schedule says, so opening Peel shows what came out meanwhile.
    private var hasAskedSinceLaunch = false
    /// The apps with an update when the current round of checks began. The menu bar shows them until the round
    /// ends, rather than a count that climbs as answers arrive seconds apart, and the list draws only them first.
    private var heldUpdates: Set<InstalledApp.ID>? {
        didSet { updatesRevision += 1 }
    }
    private(set) var sizes: [InstalledApp.ID: Int64] = [:] {
        didSet { sizesRevision += 1 }
    }
    /// The apps whose bundle could not be measured, by time or by macOS, so their size is not known rather than
    /// still to come. The list reads "Unknown" for them and sorts them first.
    private(set) var unmeasured: Set<InstalledApp.ID> = [] {
        didSet { sizesRevision += 1 }
    }

    /// The apps whose bundle shares most of its storage with another copy (`FolderContents.sharesMostOfItsStorage`),
    /// so its size is far below the space it takes.
    private(set) var sharingStorage: Set<InstalledApp.ID> = []

    /// Incremented when a size arrives. Only the size sort depends on it, so other sorts keep their cached list.
    private(set) var sizesRevision = 0
    private(set) var lastUpdateChecks: [InstalledApp.ID: Date] = [:]
    /// Why the last check of each app's own source failed, when it did, which the note behind Update check failed says.
    private(set) var updateFailures: [InstalledApp.ID: UpdateFailure] = [:]
    /// How the last upgrade run from each app's page went. Kept here rather than on the page: a successful upgrade
    /// is another build, so the page is built again, and the new one shows the result.
    private var upgrades: [InstalledApp.ID: Upgrade] = [:]
    /// Apps whose signing team changed without the user acknowledging it yet, by bundle identifier.
    private(set) var teamChanges: [String: TeamRegistry.Change] = [:]
    /// Why the record of who signs each app is not being kept, or nil while it is.
    private(set) var teamRecordProblem: TeamRegistry.Problem?
    /// The casks Homebrew knows, installed or not, for telling where an app came from and comparing versions.
    private(set) var casks: [HomebrewPackage] = []
    /// The cask each Homebrew-installed app came from. Matched with the same evidence everything else uses,
    /// rather than by guessing a token from the app's name.
    private(set) var homebrewApps: [InstalledApp.ID: HomebrewPackage] = [:]
    /// The apps Homebrew didn't install whose versions its catalog lists (`UpdateChecker.catalogCask(for:in:)`).
    private var appsInTheCatalog: Set<InstalledApp.ID> = []
    /// The same, by cask, since the Homebrew page asks about every outdated cask each time it is drawn.
    private var appsOfCasks: [HomebrewPackage.ID: [InstalledApp]] = [:]
    /// The rows chosen in the list, which the page beside it shows.
    var selection: Set<InstalledApp.ID> = []
    /// An app something asked to show that Peel couldn't read, and why, which the window says.
    var couldNotShow: CouldNotShow?
    /// The apps selected with their checkboxes, to be removed together. Kept apart from `selection`, so
    /// choosing a row to look at never clears the batch.
    var picked: Set<InstalledApp.ID> = []
    private(set) var ignoredApps: Set<String> {
        didSet { updatesRevision += 1 }
    }

    private(set) var skippedVersions: [String: String] {
        didSet { updatesRevision += 1 }
    }
    var sort = AppSort.name
    var sources: Set<AppSource> = []
    /// The developer whose apps the list shows, or nil for all developers.
    var selectedDeveloper: String?
    var showsOnlyUnused = false
    /// True when any of the list's filters is on, the same three `visible(matching:)` applies.
    var isFiltering: Bool { !sources.isEmpty || selectedDeveloper != nil || showsOnlyUnused }

    private var memory: UpdateMemories
    @ObservationIgnored private var announced: AnnouncedUpdates
    /// What is new in each waiting update, read at launch, so a page shows it with no connection too.
    private(set) var releaseNotes = ReleaseNotesMemory()
    private let releaseNotesStore: ReleaseNotesStore
    private let defaults: UserDefaults
    private let appFolders: AppFolders
    private let appMemory: AppMemory
    /// Apps shown from outside the scanned folders, such as one in Downloads or in the Trash. Kept apart,
    /// because the next reading of the folders doesn't find them and would drop their row and their page.
    private var revealed: [InstalledApp] = []
    /// The listed apps that are in a Trash, which only a revealed one can be: Watch the Trash leads to them so what
    /// they left behind can go. They are on their way out, so no round checks them, and no update is counted.
    private var appsInATrash: Set<InstalledApp.ID> = []
    /// Runs the reading of the folders. A new reading stops one still running, since the older one could
    /// finish last with a list that lacks an app installed in between.
    let scanRun = ScanRun()
    private var lastRead: ContinuousClock.Instant?
    @ObservationIgnored private var firstReading: Task<Void, Never>?
    /// The revision `followChanges` last ran its work for.
    @ObservationIgnored private var followedRevision = 0
    /// The apps readings found at another build since that work last ran.
    @ObservationIgnored private var changedBuilds: [InstalledApp.ID: InstalledApp] = [:]
    private let updateChecker: UpdateChecker
    private let teamRegistry = TeamRegistry()

    init(
        defaults: UserDefaults = .standard,
        updateChecker: UpdateChecker = UpdateChecker(),
        releaseNotesStore: ReleaseNotesStore = ReleaseNotesStore(),
        appFolders: AppFolders = AppFolders(),
        appMemory: AppMemory = AppMemory()
    ) {
        self.defaults = defaults
        self.updateChecker = updateChecker
        self.releaseNotesStore = releaseNotesStore
        self.appFolders = appFolders
        self.appMemory = appMemory
        folders = appFolders.load()
        ignoredApps = Set(defaults.stringArray(forKey: SettingsKey.ignoredUpdateApps) ?? [])
        skippedVersions = defaults.dictionary(forKey: SettingsKey.skippedUpdateVersions) as? [String: String] ?? [:]
        announced = AnnouncedUpdates(
            versions: defaults.dictionary(forKey: SettingsKey.announcedUpdates) as? [String: String] ?? [:]
        )
        memory = UpdateMemoryStore.load(from: defaults)
    }

    /// The apps the page beside the list shows: the chosen rows, or the selected batch when no row is chosen.
    private var shown: Set<InstalledApp.ID> {
        selection.isEmpty ? picked : selection
    }

    var selectedApp: InstalledApp? {
        shown.count == 1 ? apps.first { shown.contains($0.id) } : nil
    }

    var selectedApps: [InstalledApp] {
        apps.filter { shown.contains($0.id) }
    }

    var isShowingSeveral: Bool {
        shown.count > 1
    }

    /// The apps listed, and what could not be read of them, which Orphaned Files checks its files against.
    struct Listing: Equatable {
        let revision: Int
        let unreadable: [URL]
        let listOfFoldersUnreadable: Bool

        var readEverything: Bool { unreadable.isEmpty && !listOfFoldersUnreadable }
    }

    var listing: Listing {
        Listing(revision: revision, unreadable: unreadable, listOfFoldersUnreadable: listOfFoldersUnreadable)
    }

    /// The first reading of the folders, at launch. A reading asked for meanwhile, as when the folder watch hears of a
    /// change, waits for this one, which alone puts back what earlier runs learned.
    func load() async {
        let reading = Task { await readFirst() }
        firstReading = reading
        await reading.value
        firstReading = nil
    }

    private func readFirst() async {
        isLoading = true
        lastRead = .now
        if !hasLoaded {
            releaseNotes = await releaseNotesStore.load()
        }
        let changes = folderChanges
        askedForReading = true
        let found = await scanRun.run { [appFolders] in await AppCatalog.scan(choices: appFolders) }
        if !scanRun.isRunning { askedForReading = false }
        if let found {
            showFolders(of: found, readSince: changes)
            keepUnreadable(of: found)
            adopt(found.apps)
            // Launch does for this reading what `followChanges` does for the others.
            followedRevision = revision
        }
        recall()
        isLoading = false
        hasLoaded = true
        if let found {
            await appMemory.remember(found.apps)
        }
    }

    /// Calls `refresh()` unless a reading of the folders started in the last 30 seconds, even one still running. For
    /// Peel coming forward: the folder watcher sees every real change, so a recent read is enough.
    func refreshUnlessRecent() async {
        if let lastRead, ContinuousClock.now - lastRead < .seconds(30) { return }
        await refresh()
    }

    func addFolders(_ urls: [URL]) {
        changeFolders { $0.add(urls) }
    }

    func removeFolders(_ urls: [URL]) {
        changeFolders { $0.remove(urls) }
    }

    /// Sets the list of folders aside when it cannot be read, so the folders can be chosen again.
    @discardableResult
    func startFoldersOver() -> StartOver {
        let answer = appFolders.startOver()
        if answer == .started {
            couldNotSaveFolders = false
            whyFoldersNotSaved = nil
        }
        folderChanges += 1
        folders = appFolders.load()
        foldersLeftOut = appFolders.leftOut()
        return answer
    }

    var foldersFile: URL { appFolders.url }

    private func changeFolders(_ change: (AppFolders) -> ListChange) {
        switch change(appFolders) {
        case .saved:
            couldNotSaveFolders = false
            whyFoldersNotSaved = nil
        case .notSaved(let why):
            couldNotSaveFolders = true
            whyFoldersNotSaved = why
        }
        folderChanges += 1
        folders = appFolders.load()
        foldersLeftOut = appFolders.leftOut()
    }

    /// Shows the list of chosen folders the reading looked in, so Settings and the pages say the same of it, unless
    /// the person changed the list since the reading began: that change brings a reading of its own.
    private func showFolders(of scan: AppScan, readSince changes: Int) {
        guard changes == folderChanges else { return }
        if scan.chosenFolders != folders { folders = scan.chosenFolders }
        if scan.foldersLeftOut != foldersLeftOut { foldersLeftOut = scan.foldersLeftOut }
    }

    /// Reads the folders again without emptying the list first, so an app installed while Peel is open
    /// appears in its place instead of the whole list blinking. An upgrade keeps the path, and with it the app's
    /// identity, so what was cached about the old build (its size, its update status, when it was last checked)
    /// is dropped rather than shown for a version it doesn't describe.
    func refresh() async {
        await firstReading?.value
        lastRead = .now
        let changes = folderChanges
        let read = await scanRun.run { [appFolders] in await AppCatalog.scan(choices: appFolders) }
        if !scanRun.isRunning { askedForReading = false }
        guard let scan = read else { return }
        showFolders(of: scan, readSince: changes)
        keepUnreadable(of: scan)
        let found = scan.apps
        let listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))
        guard listed != apps else { return }
        guard !AppCatalog.listsTheSameApps(listed, as: apps) else {
            apps = listed
            groupHomebrewApps()
            lastOpenedRevision += 1
            return
        }
        let gone = Set(apps.compactMap(\.bundleIdentifier)).subtracting(listed.compactMap(\.bundleIdentifier))
        for app in adopt(found) { changedBuilds[app.id] = app }
        // Remembered whenever the list changes, so an app installed and removed between two visits to Orphaned
        // Files is still known. Done last, so nothing above is left half done while this waits.
        await appMemory.remember(found, gone: gone)
    }

    /// Runs `work` for each change of the list, with the apps found at another build. Changes that arrive while it
    /// runs are one more run. Whichever reading comes first, the folder watch, Rescan, or Peel coming forward, the
    /// others find nothing new, so what depends on the list follows it here and not after a reading.
    func followChanges(_ work: ([InstalledApp]) async -> Void) async {
        for await revision in Observations({ self.revision }) where revision != followedRevision {
            followedRevision = revision
            let changed = Array(changedBuilds.values)
            changedBuilds = [:]
            await work(changed)
        }
    }

    /// Checks again what a reading found at another build: whether it has an update and who signs it, since what
    /// Peel learned was about the old build.
    func checkAgain(_ changed: [InstalledApp]) async {
        guard !changed.isEmpty else { return }
        await checkForUpdates(changed, force: true)
        await checkSigningTeams()
    }

    /// Reads the folders again and asks about every app's updates, in a round of its own so the list isn't held.
    func rescan() async {
        askedForReading = true
        await refresh()
        checkEveryAppAgain()
    }

    /// The round of checks Rescan started, which the next Rescan stops before it starts its own.
    @ObservationIgnored private(set) var recheck: Task<Void, Never>?
    @ObservationIgnored private var rechecks = 0
    /// True while the round Rescan started runs, so the list can tell VoiceOver what that round found.
    private(set) var isCheckingEveryApp = false

    func checkEveryAppAgain() {
        recheck?.cancel()
        rechecks += 1
        let round = rechecks
        isCheckingEveryApp = true
        recheck = Task {
            await checkForUpdates(apps, force: true)
            if round == rechecks { isCheckingEveryApp = false }
        }
    }

    /// Stops the round Rescan started, which `recheck` runs.
    func stopCheckingEveryApp() {
        recheck?.cancel()
        rechecks += 1
        isCheckingEveryApp = false
    }

    private func keepUnreadable(of scan: AppScan) {
        if unreadable != scan.unreadable { unreadable = scan.unreadable }
        if listOfFoldersUnreadable != scan.listOfFoldersUnreadable {
            listOfFoldersUnreadable = scan.listOfFoldersUnreadable
        }
        if whyListOfFoldersUnreadable != scan.whyListOfFoldersUnreadable {
            whyListOfFoldersUnreadable = scan.whyListOfFoldersUnreadable
        }
        if unreadableNeedsFullDiskAccess != scan.needsFullDiskAccess {
            unreadableNeedsFullDiskAccess = scan.needsFullDiskAccess
        }
    }

    /// Takes a new reading of the folders and returns the apps whose build changed. Drops whatever was cached
    /// about an app that is gone or a build that was replaced, so an app removed and installed again never
    /// shows the old copy's size or update status. Both `load()` and `refresh()` come through here.
    @discardableResult
    private func adopt(_ found: [InstalledApp]) -> [InstalledApp] {
        revealed = stillThere(revealed, beside: found)
        appsInATrash = Set(revealed.filter { TrashService.isInsideATrash($0.url) }.map(\.id))
        let listed = AppCatalog.sorted(found + revealed)
        let previous = Dictionary(apps.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
        let changed = listed.filter { app in previous[app.id].map { !$0.isTheSameBuild(as: app) } ?? false }
        let present = Set(listed.map(\.id))
        IconCache.forget(previous.keys.filter { !present.contains($0) } + changed.map(\.url))
        apps = listed
        revision += 1
        matchHomebrewApps()

        let stale = Set(changed.map(\.id))
        let keeps = { (id: InstalledApp.ID) in present.contains(id) && !stale.contains(id) }
        sizes = sizes.filter { keeps($0.key) }
        unmeasured = unmeasured.filter(keeps)
        sharingStorage = sharingStorage.filter(keeps)
        updateStatuses = updateStatuses.filter { keeps($0.key) }
        lastUpdateChecks = lastUpdateChecks.filter { keeps($0.key) }
        updateFailures = updateFailures.filter { keeps($0.key) }
        selection.formIntersection(present)
        picked.formIntersection(present)

        // Forgets what was learned about a build that was replaced, including when to check it again.
        let recalled = memory.recalled(for: listed)
        if recalled != memory {
            memory = recalled
            UpdateMemoryStore.save(memory, to: defaults)
        }
        return changed
    }

    /// The revealed apps that are still on disk and that the new reading of the folders did not find.
    private func stillThere(_ revealed: [InstalledApp], beside found: [InstalledApp]) -> [InstalledApp] {
        let listed = Set(found.map(\.id))
        return revealed.filter {
            !listed.contains($0.id) && FileManager.default.fileExists(atPath: $0.url.path(percentEncoded: false))
        }
    }

    /// Restores the update answers saved by earlier runs, for apps still at the build they describe.
    private func recall() {
        for app in apps {
            guard let remembered = remembered(app) else { continue }
            updateStatuses[app.id] = remembered.status
            if let checked = remembered.checked {
                lastUpdateChecks[app.id] = checked
            }
            if let failure = remembered.failure {
                updateFailures[app.id] = failure
            }
        }
        // Before Peel noted what it told of, the updates found earlier were told of as they were found.
        if !apps.isEmpty, defaults.object(forKey: SettingsKey.announcedUpdates) == nil {
            announced.note(appsWithUpdates, version: { updateStatuses[$0.id]?.version })
            defaults.set(announced.versions, forKey: SettingsKey.announcedUpdates)
        }
    }

    /// Measures the apps with no size yet. Each bundle needs a walk of every file, so sizes arrive after the list.
    /// A bundle that could not be measured is asked once more after `remeasureDelay`: a walk that ran out of time
    /// goes on, and its answer is kept for the next question.
    func loadSizes() async {
        await measure(apps.filter { sizes[$0.id] == nil })
        guard !unmeasured.isEmpty, (try? await Task.sleep(for: Self.remeasureDelay)) != nil else { return }
        await measure(apps.filter { unmeasured.contains($0.id) })
    }

    private static let remeasureDelay = Duration.seconds(30)

    private func measure(_ missing: [InstalledApp]) async {
        guard !missing.isEmpty else { return }
        let asked = Dictionary(missing.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
        await withTaskGroup(of: (InstalledApp.ID, FolderContents?).self) { group in
            var pending = missing.makeIterator()
            for _ in 0..<Self.concurrentSizeReads {
                guard let app = pending.next() else { break }
                _ = group.addTaskUnlessCancelled { (app.id, await FileSize.contents(of: app.url)) }
            }
            while let (id, contents) = await group.next() {
                let measured = contents.flatMap { $0.couldNotBeRead ? nil : $0 }
                // A walk given up because the list changed says nothing about the bundle, and a bundle replaced
                // while it was walked is another build, so neither answer is kept.
                if !Task.isCancelled, let app = asked[id],
                   apps.contains(where: { $0.id == id && $0.isTheSameBuild(as: app) }) {
                    if let measured {
                        sizes[id] = measured.size
                        unmeasured.remove(id)
                        if measured.sharesMostOfItsStorage {
                            sharingStorage.insert(id)
                        } else {
                            sharingStorage.remove(id)
                        }
                    } else {
                        unmeasured.insert(id)
                    }
                }
                if !Task.isCancelled, let app = pending.next() {
                    _ = group.addTaskUnlessCancelled { (app.id, await FileSize.contents(of: app.url)) }
                }
            }
        }
    }

    func source(of app: InstalledApp) -> AppSource {
        AppSource.of(app, installedByHomebrew: homebrewApps[app.id] != nil)
    }

    func developer(of app: InstalledApp) -> String? {
        appDevelopers.developer(of: app, remembered: app.bundleIdentifier.flatMap(memory.developer(of:)))
    }

    var developers: [String] {
        AppDevelopers.offered(apps.compactMap(developer(of:)), chosen: selectedDeveloper)
    }

    private var appDevelopers: AppDevelopers {
        if let lastTeams, lastTeams.revision == revision { return lastTeams.developers }
        let developers = AppDevelopers(apps)
        lastTeams = (revision, developers)
        return developers
    }

    /// The cask this app was installed from, which is the name `brew upgrade` has to be given.
    func cask(for app: InstalledApp) -> HomebrewPackage? {
        homebrewApps[app.id]
    }

    func apps(of cask: HomebrewPackage) -> [InstalledApp] {
        appsOfCasks[cask.id] ?? []
    }

    func whyItCannotCheck(_ app: InstalledApp) -> CannotCheck {
        updateChecker.whyItCannotCheck(app, preference: updateSource, casks: casks)
    }

    func isUnused(_ app: InstalledApp) -> Bool {
        AppOrder.isUnused(app, forMonths: Self.unusedMonths)
    }

    /// The last answer of `visibleApps(matching:)`. This and the caches below are written while a view's
    /// body is evaluated, so they are kept out of Observation: a change there would trigger another update.
    @ObservationIgnored private var lastVisible: (key: VisibleKey, apps: VisibleApps)?
    @ObservationIgnored private var lastExcluded: (revision: Int, exclusions: Exclusions, ids: Set<InstalledApp.ID>)?
    @ObservationIgnored private var lastNames: (revision: Int, names: [String: String])?
    @ObservationIgnored private var lastTeams: (revision: Int, developers: AppDevelopers)?

    /// The apps the user excluded. Worked out again only when the apps or the exclusions change, since each
    /// check resolves the app's path on disk.
    func excludedIDs(by exclusions: Exclusions) -> Set<InstalledApp.ID> {
        if let lastExcluded, lastExcluded.revision == revision, lastExcluded.exclusions == exclusions {
            return lastExcluded.ids
        }
        let ids = Set(apps.filter(exclusions.excludes).map(\.id))
        lastExcluded = (revision, exclusions, ids)
        return ids
    }

    private struct VisibleKey: Hashable {
        let revision: Int
        let lastOpened: Int
        let updates: Int
        let sizes: Int
        let sort: AppSort
        let sources: Set<AppSource>
        let developer: String?
        let showsOnlyUnused: Bool
        let searchText: String
    }

    /// The apps the list shows. Those with an update waiting are split out and drawn first, since a badge
    /// alone is easy to miss in a long list. Both parts keep the chosen sort.
    ///
    /// The answer is cached until one of its inputs changes, so a size or an update status arriving doesn't
    /// filter and sort the whole list again.
    func visibleApps(matching searchText: String) -> VisibleApps {
        let key = VisibleKey(
            revision: revision,
            lastOpened: lastOpenedRevision,
            updates: updatesRevision,
            sizes: sort == .size ? sizesRevision : 0,
            sort: sort,
            sources: sources,
            developer: selectedDeveloper,
            showsOnlyUnused: showsOnlyUnused,
            searchText: searchText
        )
        if let lastVisible, lastVisible.key == key { return lastVisible.apps }
        let answer = visible(matching: searchText)
        lastVisible = (key, answer)
        return answer
    }

    private func visible(matching searchText: String) -> VisibleApps {
        let filter = AppListFilter(
            text: searchText, sources: sources, developer: selectedDeveloper, onlyUnused: showsOnlyUnused
        )
        let result = apps.filter {
            filter.keeps($0, source: source(of: $0), developer: developer(of: $0), isUnused: isUnused($0))
        }

        var waiting: [InstalledApp] = []
        var rest: [InstalledApp] = []
        for app in sorted(result) {
            // An update found while a round runs moves its row up once the round ends, not under the pointer.
            if hasUpdate(app), heldUpdates?.contains(app.id) ?? true {
                waiting.append(app)
            } else {
                rest.append(app)
            }
        }
        return VisibleApps(waiting: waiting, rest: rest)
    }

    private func sorted(_ apps: [InstalledApp]) -> [InstalledApp] {
        switch sort {
        case .name:
            apps.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
        case .size:
            AppOrder.sortedBySize(apps, sizes: sizes, unmeasured: unmeasured)
        case .lastOpened:
            AppOrder.sorted(apps) { $0.lastUsedDate ?? .distantPast }
        case .dateAdded:
            AppOrder.sorted(apps) { $0.dateAdded ?? .distantPast }
        }
    }

    /// Checks the apps in `targets` that are due, and schedules when each is due next.
    ///
    /// `force` checks every target that isn't ignored, whatever the schedule says. It is for checks the user
    /// asked for, and for bundles that changed on disk, which are not the build the last answer described.
    func checkForUpdates(_ targets: [InstalledApp], force: Bool = false) async {
        // Read here, where every caller passes. Settings promises that with this off, Peel contacts nothing on
        // its own, so even Rescan on an app's page only rescans files.
        guard defaults.isOn(SettingsKey.checksForAppUpdates, whenNeverSet: true) else { return }
        // What Homebrew says lasts only a day after its last check, so its casks decide again in every round.
        applyHomebrewStatuses()
        let now = Date.now
        let isFirstRound = !force && !hasAskedSinceLaunch && !targets.isEmpty
        if isFirstRound { hasAskedSinceLaunch = true }
        // An app is checked on a schedule kept by its identifier, so an app with none is left to Homebrew's answer.
        let wanted = targets.filter { app in
            guard app.bundleIdentifier != nil, isAskedAboutUpdates(app) else { return false }
            guard !force else { return true }
            // Already being checked by another round: a second answer could double the wait for the next check.
            guard !appsCheckingForUpdates.contains(app.id) else { return false }
            // An update whose notes are still to be asked for is checked now rather than on its next turn.
            let notesWait = releaseNotes.asks(about: updateStatuses[app.id], of: app, with: updatePreferences)
            let saved = remembered(app)
            let isDue = saved?.schedule.isDue(at: now) ?? true
            return isDue || notesWait || isFirstRound && saved?.isADayOld(at: now) == true
        }
        // The list's own entry, not the caller's: a page that was open through an upgrade still holds the old build.
        .map { app in apps.first { $0.id == app.id } ?? app }
        guard !wanted.isEmpty else { return }
        // With no network at all, each request would wait out its whole time, and nothing is learned of any app, so the
        // first round that asks about answers a day old is still to come.
        guard await updateChecker.canReachTheNetwork() else {
            if isFirstRound { hasAskedSinceLaunch = false }
            return
        }
        let asked = Dictionary(wanted.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
        let round = updateRounds.begin(wanted.map(\.id))
        // However the round ends, no app is left marked as being checked by it.
        defer { updateRounds.end(round) }
        let checker = updateChecker
        let preference = updateSource
        let casks = casks
        let notesBefore = releaseNotes
        var notesToAsk: [ReleaseNotesQuestion] = []
        let repliesBefore = checker.serverReplies

        await withTaskGroup(of: (InstalledApp.ID, UpdateAnswer).self) { group in
            var pending = wanted.makeIterator()
            var unreached: [(id: InstalledApp.ID, answer: UpdateAnswer)] = []
            @MainActor func askNext() {
                guard let app = pending.next() else { return }
                let earlier = updateStatuses[app.id]
                let notListedBefore = remembered(app)?.isNotListed == true
                _ = group.addTaskUnlessCancelled {
                    let answer = await checker.answer(
                        for: app, preference: preference, casks: casks,
                        earlier: earlier, notListedBefore: notListedBefore
                    )
                    return (app.id, answer)
                }
            }
            for _ in 0..<Self.concurrentUpdateChecks {
                askNext()
            }
            while case let (id, answer)? = await group.next() {
                // The update source changed since this round began, and the round that change started answers now.
                guard updateRounds.answered(id, in: round) else {
                    group.cancelAll()
                    continue
                }
                // Whatever comes of this answer, the next app is asked, so one that left the list costs no place.
                askNext()
                // In a canceled round every pending request ends as a failure. Recording those would erase
                // what was known and put off the next check for hours, and so would recording a check made while
                // this Mac wasn't online, which leaves the app due at the next round.
                guard !Task.isCancelled, !answer.wasOffline else { continue }
                if answer.reachedNoServer {
                    unreached.append((id, answer))
                } else {
                    take(answer, of: id)
                }
            }
            // With no server answering at all the fault is this Mac's network, and nothing was learned of any app.
            guard !Task.isCancelled, checker.serverReplies > repliesBefore else { return }
            for (id, answer) in unreached {
                take(answer, of: id)
            }

            @MainActor func take(_ answer: UpdateAnswer, of id: InstalledApp.ID) {
                // The answer describes the build that was asked about, which may have been replaced since.
                guard let app = asked[id], apps.contains(where: { $0.id == id && $0.isTheSameBuild(as: app) }) else {
                    return
                }
                let kept = answer.following(updateStatuses[id])
                // Homebrew's answer stands in for the app's own only where it is the status kept.
                let standsInFor = kept == answer.status ? answer.standsInFor : nil
                // Homebrew may have been read again while this answer was on its way: its casks decide where they can.
                let shown = casks == self.casks
                    ? kept
                    : checker.statusWithoutAsking(
                        for: app, preference: preference, casks: self.casks, known: standsInFor ?? kept
                    ) ?? kept
                // Any write redraws the whole Applications list, so an answer that changes nothing is not written.
                if updateStatuses[id] != shown {
                    updateStatuses[id] = shown
                }
                if answer.status != .failed {
                    lastUpdateChecks[id] = .now
                }
                if updateFailures[id] != answer.failure {
                    updateFailures[id] = answer.failure
                }
                guard let identifier = app.bundleIdentifier else { return }
                let before = remembered(app)
                memory.keep(UpdateMemory(
                    status: kept,
                    // A feed that failed is asked again on its own count, even where Homebrew answered in its place.
                    schedule: UpdateSchedule.next(
                        after: answer.standsInFor ?? answer.status, following: before?.schedule
                    ),
                    checked: answer.status == .failed ? before?.checked : .now,
                    describing: app,
                    developer: answer.developer ?? memory.developer(of: identifier),
                    standsInFor: standsInFor,
                    isNotListed: answer.isNotListed,
                    failure: answer.failure
                ), for: app)
                // Only an answer that found this update can say what is new in it; a failed one keeps what was known.
                if case .updateAvailable(let version, _, _, _) = kept, answer.status == kept {
                    if let notes = answer.notes {
                        releaseNotes.record(.found(notes), of: identifier, version: version)
                    } else if releaseNotes.asks(about: kept, of: app, with: updatePreferences, force: force) {
                        notesToAsk.append(ReleaseNotesQuestion(app: app, answer: answer, version: version))
                    }
                }
            }
        }
        guard !Task.isCancelled, updateRounds.isCurrent(round) else { return }
        UpdateMemoryStore.save(memory, to: defaults)
        await ask(notesToAsk, checker: checker)
        var waiting: [String: String] = [:]
        for app in apps {
            if let version = updateStatuses[app.id]?.version, let identifier = app.bundleIdentifier {
                waiting[identifier] = version
            }
        }
        releaseNotes.keep(only: waiting)
        if releaseNotes != notesBefore {
            await releaseNotesStore.save(releaseNotes)
        }
    }

    /// What is new in the update `app` is waiting for, when it is known.
    func releaseNotes(of app: InstalledApp) -> ReleaseNotes? {
        guard let identifier = app.bundleIdentifier else { return nil }
        return updateStatuses[app.id]?.version.flatMap { releaseNotes.notes(of: identifier, version: $0) }
    }

    /// An update whose answer did not say what is new in it.
    private struct ReleaseNotesQuestion: Sendable {
        let app: InstalledApp
        let answer: UpdateAnswer
        let version: String
    }

    /// Asks the app's own addresses for the notes the round's answers did not carry, a few at a time.
    private func ask(_ questions: [ReleaseNotesQuestion], checker: UpdateChecker) async {
        await withTaskGroup(of: (ReleaseNotesQuestion, ReleaseNotesLookup).self) { group in
            var pending = questions.makeIterator()
            func askNext() {
                guard let question = pending.next() else { return }
                group.addTask { (question, await checker.releaseNotes(for: question.app, answer: question.answer)) }
            }
            for _ in 0..<Self.concurrentUpdateChecks {
                askNext()
            }
            while let (question, lookup) = await group.next() {
                // A stopped round's requests end without an answer, which would put the notes off for hours.
                guard !Task.isCancelled else { continue }
                if let identifier = question.app.bundleIdentifier {
                    releaseNotes.record(lookup, of: identifier, version: question.version)
                }
                askNext()
            }
        }
    }

    /// The user's update settings as one value. The command line reads the same settings from the same keys,
    /// so the app and `peel` can't disagree about what is waiting.
    var updatePreferences: UpdatePreferences {
        UpdatePreferences(
            source: updateSource,
            ignoredApps: ignoredApps,
            skippedVersions: skippedVersions
        )
    }

    /// Whether an update is waiting for `app`. An ignored app has none, and neither has one whose waiting
    /// version was skipped. The list, the menu bar, and the notification all ask this, so they always agree.
    /// Peel's own updates are Sparkle's (`PeelUpdater`).
    func hasUpdate(_ app: InstalledApp) -> Bool {
        !app.isPeelItself && updatePreferences.isWaiting(updateStatuses[app.id], for: app)
    }

    /// The answer about `app`'s updates that its page shows: an update the user muted is not announced again.
    func shownUpdateStatus(of app: InstalledApp) -> UpdateStatus? {
        // A web app updates with its website, so there is nothing to check, and nothing is said.
        guard !app.isPeelItself, app.webApp == nil else { return nil }
        return updatePreferences.shownStatus(updateStatuses[app.id], for: app)
    }

    /// The day the Up to date shown for `app` was found, once nothing renews it: the checks are off, the answer is past
    /// the longest wait, and Homebrew doesn't say it now. The badge then says that day.
    func keptUpToDate(of app: InstalledApp) -> Date? {
        guard shownUpdateStatus(of: app) == .upToDate,
              !defaults.isOn(SettingsKey.checksForAppUpdates, whenNeverSet: true),
              let checked = lastUpdateChecks[app.id], !updateChecker.stillHolds(answeredAt: checked),
              updateChecker.homebrewStatus(for: app, casks: casks) != .upToDate
        else { return nil }
        return checked
    }

    /// The apps the reading of the folders found, without those shown on the list from elsewhere: what is installed,
    /// as `peel inventory` lists it.
    var installedApps: [InstalledApp] {
        apps.filter { app in !revealed.contains { $0.id == app.id } }
    }

    /// The installed apps with an update waiting, which the menu bar, its notification and the list's summary count.
    /// An app shown from elsewhere keeps its update on its row and its page.
    var appsWithUpdates: [InstalledApp] { installedApps.filter(hasUpdate) }

    /// The apps the menu bar item and its panel count. They follow `hasUpdate(_:)`, but while a round of checks runs
    /// they are the ones from when the round began.
    var menuBarUpdates: [InstalledApp] {
        guard let heldUpdates else { return appsWithUpdates }
        return installedApps.filter { heldUpdates.contains($0.id) }
    }

    var menuBarUpdateCount: Int { menuBarUpdates.count }

    /// The installed apps whose waiting update no notification has named, which then count as named
    /// (`AnnouncedUpdates`), kept across launches. None while Check for app updates is off.
    func updatesToAnnounce() -> [InstalledApp] {
        let new = announced.takeNew(
            appsWithUpdates,
            version: { updateStatuses[$0.id]?.version },
            whileChecking: !appsCheckingForUpdates.isEmpty,
            checksAreOn: defaults.isOn(SettingsKey.checksForAppUpdates, whenNeverSet: true)
        )
        if !new.isEmpty {
            defaults.set(announced.versions, forKey: SettingsKey.announcedUpdates)
        }
        return new
    }

    var updateSource: UpdateSource {
        UpdateSource(rawValue: defaults.string(forKey: SettingsKey.updateSource) ?? "") ?? .automatic
    }

    /// Forgets every update answer and schedule, then checks every app again. The answers shown were found
    /// under the old source, and the badge would explain them in terms of the new one.
    func updateSourceChanged() async {
        updateRounds.sourceChanged()
        updateStatuses = [:]
        lastUpdateChecks = [:]
        updateFailures = [:]
        memory = UpdateMemories()
        UpdateMemoryStore.save(memory, to: defaults)
        await checkForUpdates(apps, force: true)
    }

    func isIgnored(_ app: InstalledApp) -> Bool {
        updatePreferences.isIgnored(app)
    }

    func isInATrash(_ app: InstalledApp) -> Bool {
        appsInATrash.contains(app.id)
    }

    func setIgnored(_ isIgnored: Bool, for app: InstalledApp) {
        var references = ignoredApps
        if isIgnored {
            references.insert(app.reference)
        } else {
            references.remove(app.reference)
        }
        ignoredApps = references
        defaults.set(Array(references).sorted(), forKey: SettingsKey.ignoredUpdateApps)
    }

    func skippedVersion(for app: InstalledApp) -> String? {
        skippedVersions[app.reference]
    }

    func skip(version: String, for app: InstalledApp) {
        skippedVersions[app.reference] = version
        defaults.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
    }

    /// Undoes both a skipped version and "never check this app", by `InstalledApp.reference`: what was muted may not
    /// be installed anymore, and then there is no `InstalledApp` to name it with.
    func unmute(_ reference: String) {
        ignoredApps.remove(reference)
        skippedVersions.removeValue(forKey: reference)
        defaults.set(ignoredApps.sorted(), forKey: SettingsKey.ignoredUpdateApps)
        defaults.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
    }

    struct Upgrade {
        /// The app as the upgrade left it.
        let app: InstalledApp
        let succeeded: Bool
    }

    /// How the last upgrade of `app` went, while `app` is still the build it left.
    func upgrade(of app: InstalledApp) -> Upgrade? {
        upgrades[app.id].flatMap { $0.app.isTheSameBuild(as: app) ? $0 : nil }
    }

    func record(_ upgrade: Upgrade?, of app: InstalledApp) {
        upgrades[app.id] = upgrade
    }

    /// True once casks came from a reading of Homebrew that says of every app whether it installed it.
    private(set) var hasMatchedHomebrew = false
    /// Whether `cask(for:)` answers for every app listed: the apps were read, and matched to such a reading. Until
    /// then an app Homebrew installed may still look like one it didn't.
    var knowsHomebrewsApps: Bool { hasLoaded && hasMatchedHomebrew }

    /// Called whenever Homebrew is read again. Apps are matched to casks again only when the casks changed, since
    /// `adopt` already does it when the apps change, and matching holds the main thread. `knowsItsOwnApps` is true
    /// when `packages` says of every app whether Homebrew installed it, and not while Homebrew could not be read.
    func loadHomebrewCasks(_ packages: [HomebrewPackage], knowsItsOwnApps: Bool) {
        let casks = packages.filter { $0.kind == .cask }
        if casks != self.casks {
            self.casks = casks
            matchHomebrewApps()
        }
        if knowsItsOwnApps {
            forgetUpdatesHomebrewNoLongerOffers()
            hasMatchedHomebrew = true
        }
        applyHomebrewStatuses()
    }

    /// Forgets an update Homebrew reported and no longer reports, for an app it no longer counts as its own or lists in
    /// its catalog, or whose cask is no longer behind, saved answer included, so the app is due and its own feed
    /// answers at the next round. One that stands in for the app's own source is decided again from that source's
    /// answer instead.
    private func forgetUpdatesHomebrewNoLongerOffers() {
        let outlived = Set(apps.filter { app in
            guard let status = updateStatuses[app.id], remembered(app)?.standsInFor == nil else { return false }
            let homebrew = homebrewApps[app.id] == nil && !appsInTheCatalog.contains(app.id)
                ? nil : updateChecker.homebrewStatus(for: app, casks: casks)
            return !status.holds(whileHomebrewSays: homebrew)
        }.map(\.id))
        guard !outlived.isEmpty else { return }
        updateStatuses = updateStatuses.filter { !outlived.contains($0.key) }
        lastUpdateChecks = lastUpdateChecks.filter { !outlived.contains($0.key) }
        updateFailures = updateFailures.filter { !outlived.contains($0.key) }
        for app in apps where outlived.contains(app.id) {
            memory.forget(app)
        }
        UpdateMemoryStore.save(memory, to: defaults)
    }

    /// Shows what a check would answer without asking a server, for each app Homebrew installed or lists in its
    /// catalog, or answers for in place of its own source. Reading Homebrew costs no network request, so this doesn't
    /// wait for the update schedule, and Applications agrees with the Homebrew page.
    private func applyHomebrewStatuses() {
        let preference = updateSource
        for app in apps where isAskedAboutUpdates(app) {
            let saved = remembered(app)
            guard homebrewApps[app.id] != nil || appsInTheCatalog.contains(app.id) || saved?.standsInFor != nil
            else { continue }
            let decided = updateChecker.statusWithoutAsking(
                for: app, preference: preference, casks: casks, known: saved.map { $0.standsInFor ?? $0.status }
            )
            guard let decided, decided != updateStatuses[app.id] else { continue }
            updateStatuses[app.id] = decided
        }
    }

    /// The answer saved for `app`, while it is about the build installed.
    private func remembered(_ app: InstalledApp) -> UpdateMemory? {
        memory.entry(for: app)
    }

    /// Whether update checks ask about `app`. Never about Peel's own copies, which Sparkle keeps current, checking
    /// the one running, nor about a web app, nor an app the user told Peel never to check or a copy in a Trash.
    private func isAskedAboutUpdates(_ app: InstalledApp) -> Bool {
        !app.isPeelItself && app.webApp == nil && !isIgnored(app) && !appsInATrash.contains(app.id)
    }

    private func matchHomebrewApps() {
        let casks = casks
        homebrewApps = Dictionary(uniqueKeysWithValues: apps.compactMap { app in
            CaskEvidence.installedCask(for: app, in: casks).map { (app.id, $0) }
        })
        appsInTheCatalog = Set(apps.filter { app in
            homebrewApps[app.id] == nil && UpdateChecker.catalogCask(for: app, in: casks) != nil
        }.map(\.id))
        groupHomebrewApps()
    }

    private func groupHomebrewApps() {
        appsOfCasks = Dictionary(grouping: apps.filter { homebrewApps[$0.id] != nil }) { homebrewApps[$0.id]?.id ?? "" }
    }

    /// Records the team that signs each app, and keeps the apps whose team changed. The registry reports each
    /// change again on every launch until the user acknowledges it.
    func checkSigningTeams() async {
        let outcome = await teamRegistry.check(apps)
        teamChanges = Dictionary(
            outcome.changes.map { ($0.bundleIdentifier, $0) },
            uniquingKeysWith: { first, _ in first }
        )
        teamRecordProblem = outcome.problem
    }

    func acknowledgeTeamChange(for app: InstalledApp) async {
        guard let identifier = app.bundleIdentifier else { return }
        teamChanges.removeValue(forKey: identifier)
        if let problem = await teamRegistry.acknowledge(identifier) {
            teamRecordProblem = problem
        }
    }

    /// Keeps a record of signers that cannot be read beside a new one, and records who signs each app now.
    @discardableResult
    func startTeamRecordOver() async -> StartOver {
        let answer = await teamRegistry.startOver()
        if case .couldNotSetAside = answer {} else { await checkSigningTeams() }
        return answer
    }

    var teamRecordFile: URL { teamRegistry.url }

    /// Selects the apps together, adding each first when it lives outside the scanned folders, for example in
    /// the Trash. The first Peel can't read is left for the window to say (`couldNotShow`), and false means none
    /// could be read.
    @discardableResult
    func reveal(_ urls: [URL]) async -> Bool {
        var chosen: Set<InstalledApp.ID> = []
        var unread: [URL] = []
        for url in urls {
            if let id = await listed(url) {
                chosen.insert(id)
            } else {
                unread.append(url)
            }
        }
        if let first = unread.first {
            couldNotShow = CouldNotShow(url: first, reason: AppInspector.whyUnreadable(first))
        }
        guard !chosen.isEmpty else { return false }
        selection = chosen
        return true
    }

    private func listed(_ url: URL) async -> InstalledApp.ID? {
        let bundleURL = url.standardizedFileURL
        // Looked up on disk, since a trailing slash, another case, or a link can name a bundle already listed.
        // Listed twice, the app would be its own rival and share every file with itself.
        if let existing = await Self.find(bundleURL, among: apps) {
            return existing.id
        }
        guard AppInspector.isNamedAsAnApp(bundleURL), let app = await Self.inspect(bundleURL) else { return nil }
        // Checked again, since two drops of the same bundle can both get this far before either is listed.
        if !apps.contains(where: { $0.id == app.id }) {
            revealed.append(app)
            adopt(apps.filter { listed in !revealed.contains { $0.id == listed.id } })
        }
        return app.id
    }

    @concurrent
    nonisolated static func inspect(_ url: URL) async -> InstalledApp? {
        AppInspector.inspect(url)
    }

    @concurrent
    private nonisolated static func find(_ url: URL, among apps: [InstalledApp]) async -> InstalledApp? {
        AppCatalog.app(at: url, among: apps)
    }

    /// The name of the app `reference` names (`InstalledApp.reference`): the listed app's, or the one Finder shows
    /// for an app macOS knows elsewhere by that identifier, such as a helper or a Nightly build on another disk. The
    /// reference itself when macOS knows none.
    func name(forReference reference: String) -> String {
        if lastNames?.revision != revision {
            lastNames = (
                revision,
                Dictionary(apps.map { ($0.reference, $0.name) }, uniquingKeysWith: { first, _ in first })
            )
        }
        if let name = lastNames?.names[reference] { return name }
        let name = AppInspector.knownName(forBundleIdentifier: reference)
        lastNames?.names[reference] = name
        return name
    }
}

/// Rescan's work: the reading of the folders, and then the round of checks it starts.
extension AppLibrary: StoppableWork {
    var isRunning: Bool { scanRun.isRunning || isCheckingEveryApp }

    func stop() {
        scanRun.stop()
        stopCheckingEveryApp()
    }
}

/// What `AppLibrary.reveal` couldn't show, and why.
struct CouldNotShow: Equatable {
    let url: URL
    let reason: AppInspector.Unreadable
}