Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions MacCleaner/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,20 @@ enum CLI {

// MARK: Run

static func run(_ argv: [String]) -> Int32 {
/// The subcommand as `run` sees it: the first argument once `--json`, `--yes` and `-y` are set aside.
static func subcommand(_ argv: [String]) -> String? {
argv.first { !["--json", "--yes", "-y"].contains($0) }
}

@MainActor static func run(_ argv: [String]) -> Int32 {
var args = argv
let json = args.contains("--json"); args.removeAll { $0 == "--json" }
let yes = args.contains("--yes") || args.contains("-y"); args.removeAll { $0 == "--yes" || $0 == "-y" }
guard let name = args.first, !["help", "-h", "--help"].contains(name) else { printHelp(); return 0 }

switch name {
case "mcp": MCPServer.run(); return 0
case "bench": return MainActor.assumeIsolated { Bench.run(Array(args.dropFirst())) }
case "bench": return Bench.run(Array(args.dropFirst()))
case "install":
switch CLIInstaller.install() {
case .success(let u):
Expand Down Expand Up @@ -180,7 +185,7 @@ enum CLI {
}

/// A confirm reaching here came with --yes (`tool` refuses confirm without it): a non-interactive override.
private static func call(_ tool: String, _ a: [String: Any], json: Bool) -> Int32 {
@MainActor private static func call(_ tool: String, _ a: [String: Any], json: Bool) -> Int32 {
let approval: ToolApproval? = (a["confirm"] as? Bool) == true ? .yesFlag : nil
do { return output(try MCPServer.call(tool, a, approval: approval), json: json) } catch { err("\(error)"); return 1 }
}
Expand Down
2 changes: 1 addition & 1 deletion MacCleaner/MCPApproval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ApprovalRequest: Sendable {
/// itself. So before any MCP tool changes anything, Mac Cleaner asks the person in its own window.
enum HumanApproval {
/// How Mac Cleaner asks. Replaced only by the self-test.
nonisolated(unsafe) static var ask: (ApprovalRequest) -> Bool = askWithDialog
@MainActor static var ask: @MainActor (ApprovalRequest) -> Bool = askWithDialog
/// The dialog cancels itself after this long, so an unattended request never goes ahead.
/// MACCLEANER_APPROVAL_TIMEOUT can only shorten it (1–120 s), for testing the cancel path.
static var timeout: TimeInterval {
Expand Down
48 changes: 30 additions & 18 deletions MacCleaner/MCPServer.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import AppKit
import Foundation
import MacCleanerCore
import os

/// Model Context Protocol server over stdio. Start with: "Mac Cleaner" --mcp
/// Every tool that deletes or moves something returns a preview unless called with confirm: true.
enum MCPServer {
struct ToolError: Error, CustomStringConvertible { let description: String }

private static var cacheScan: [CleanTarget] = []
private static var appCache: [AppInfo] = []
private static var projectScan: [DevProject] = []
private static var sdkScan = SDKScan()
private static var sysReport = SysReport()
@MainActor private static var cacheScan: [CleanTarget] = []
@MainActor private static var appCache: [AppInfo] = []
@MainActor private static var projectScan: [DevProject] = []
@MainActor private static var sdkScan = SDKScan()
@MainActor private static var sysReport = SysReport()

static let instructions = """
Mac Cleaner frees disk space on this Mac. Rules for using it:
Expand All @@ -33,14 +34,28 @@ enum MCPServer {

// MARK: - Loop

static func run() {
@MainActor static func run() {
while let line = readLine() {
guard !line.isEmpty, let data = line.data(using: .utf8),
let msg = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue }
handle(msg)
}
}

/// Measures every scan location in parallel, one slot per location (nonisolated: no main-actor state involved).
/// The array is pre-sized (all nil) before any iteration starts. Each iteration measures outside the lock, then
/// writes only its own index under the lock. `concurrentPerform` is synchronous and returns only after every
/// iteration finished, so the final read sees all writes (the lock also gives happens-before). Same result as
/// the old unsafe-buffer version: slot i holds `Scanner.measure(plan[i])`.
private nonisolated static func measureAll(_ plan: [CleanTarget]) -> [CleanTarget?] {
let slots = OSAllocatedUnfairLock<[CleanTarget?]>(initialState: Array(repeating: nil, count: plan.count))
DispatchQueue.concurrentPerform(iterations: plan.count) { i in
let m = Scanner.measure(plan[i])
slots.withLock { $0[i] = m }
}
return slots.withLock { $0 }
}

private static func send(_ obj: [String: Any]) {
guard let d = try? JSONSerialization.data(withJSONObject: obj, options: [.withoutEscapingSlashes]) else { return }
FileHandle.standardOutput.write(d + Data("\n".utf8))
Expand All @@ -50,7 +65,7 @@ enum MCPServer {
send(["jsonrpc": "2.0", "id": id, "result": result])
}

private static func handle(_ msg: [String: Any]) {
@MainActor private static func handle(_ msg: [String: Any]) {
guard let id = msg["id"] else { return } // notifications need no answer
let method = msg["method"] as? String ?? ""
let params = msg["params"] as? [String: Any] ?? [:]
Expand Down Expand Up @@ -207,7 +222,7 @@ enum MCPServer {
/// `approval` is set only by the CLI: `.terminal` when a person typed "yes" at its prompt, `.yesFlag` for the
/// `--yes` override (no prompt, not a person's confirmation). Calls from the MCP connection pass nil, so every
/// confirm from an AI client also needs the person's yes in Mac Cleaner's own window.
static func call(_ name: String, _ a: [String: Any], approval: ToolApproval? = nil) throws -> Any {
@MainActor static func call(_ name: String, _ a: [String: Any], approval: ToolApproval? = nil) throws -> Any {
let confirm = a["confirm"] as? Bool ?? false
switch name {
case "disk_status":
Expand All @@ -218,10 +233,7 @@ enum MCPServer {
case "scan_caches":
let minBytes = Int64((a["min_size_mb"] as? Double ?? 1) * 1_000_000)
let plan = Scanner.plan()
var measured = [CleanTarget?](repeating: nil, count: plan.count)
measured.withUnsafeMutableBufferPointer { buf in
DispatchQueue.concurrentPerform(iterations: plan.count) { i in buf[i] = Scanner.measure(plan[i]) }
}
let measured = Self.measureAll(plan)
cacheScan = Scanner.finalize(measured.compactMap { $0 })
let shown = cacheScan.filter { $0.sizeBytes >= minBytes }.sorted { $0.sizeBytes > $1.sizeBytes }
return [
Expand Down Expand Up @@ -648,13 +660,13 @@ enum MCPServer {
/// A confirm only runs a plan the user was shown: each preview gets a single-use id, valid for
/// 10 minutes, tied to the tool and to a digest of the exact items. The confirm recomputes the
/// digest from the same arguments, so a different app, path or selection is refused.
private static var previews: [String: (tool: String, digest: String, expires: Date, request: ApprovalRequest,
@MainActor private static var previews: [String: (tool: String, digest: String, expires: Date, request: ApprovalRequest,
cleanupPlan: CleanupPlan?)] = [:]
static let previewLifetime: TimeInterval = 600

static func plan(_ parts: [String]) -> String { parts.sorted().joined(separator: "\n") }

static func issuePreview(_ tool: String, _ digest: String, now: Date = Date(), request: ApprovalRequest? = nil,
@MainActor static func issuePreview(_ tool: String, _ digest: String, now: Date = Date(), request: ApprovalRequest? = nil,
cleanupPlan: CleanupPlan? = nil) -> String {
previews = previews.filter { $0.value.expires > now }
let id = UUID().uuidString.lowercased()
Expand Down Expand Up @@ -683,7 +695,7 @@ enum MCPServer {
/// "yes", or the --yes override), Mac Cleaner asks the person in its own window, showing the preview's list. The preview is used up
/// either way, so a refused confirm can't be retried without a new preview.
@discardableResult
static func approve(_ tool: String, _ digest: String, _ a: [String: Any], _ approval: ToolApproval? = nil,
@MainActor static func approve(_ tool: String, _ digest: String, _ a: [String: Any], _ approval: ToolApproval? = nil,
now: Date = Date()) throws -> ConfirmedPreview {
guard let id = a["preview_id"] as? String, !id.isEmpty else {
throw ToolError(description: "confirm: true needs the preview_id from this tool's preview. Call it without confirm, show the user the list, and confirm only after they say yes.")
Expand Down Expand Up @@ -766,7 +778,7 @@ enum MCPServer {

/// `lines` replaces the window's list when the JSON result shows only part of it (e.g. a huge Trash):
/// the person always sees every item.
private static func withPreview(_ tool: String, _ digest: String, _ result: [String: Any],
@MainActor private static func withPreview(_ tool: String, _ digest: String, _ result: [String: Any],
lines: [String]? = nil, cleanupPlan: CleanupPlan? = nil) -> [String: Any] {
var r = result
let request = ApprovalRequest(tool: tool, verb: verb(for: tool), lines: lines ?? approvalLines(result))
Expand All @@ -777,7 +789,7 @@ enum MCPServer {

// MARK: - Helpers

private static func findApp(_ a: [String: Any]) throws -> AppInfo {
@MainActor private static func findApp(_ a: [String: Any]) throws -> AppInfo {
guard let q = a["app"] as? String, !q.isEmpty else { throw ToolError(description: "Pass app (name, bundle id or path).") }
if appCache.isEmpty { appCache = AppEngine.scanAll() }
if let hit = AppEngine.find(q, in: appCache) { return hit }
Expand All @@ -786,7 +798,7 @@ enum MCPServer {
throw ToolError(description: "No installed app matches \"\(q)\". Use list_apps to see names.")
}

private static func replace(_ app: AppInfo) {
@MainActor private static func replace(_ app: AppInfo) {
if let i = appCache.firstIndex(where: { $0.id == app.id }) { appCache[i] = app }
}

Expand Down
26 changes: 23 additions & 3 deletions MacCleaner/MacCleanerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,38 @@ enum Entry {
#endif
SettingsMigration.migrate()
if args.count > 1 && args[1] == "--cli" {
exit(CLI.run(Array(args.dropFirst(2))))
let cliArgs = Array(args.dropFirst(2))
if CLI.subcommand(cliArgs) == "bench" {
// Bench spins the main run loop itself (Bench.time); it would hang inside runOnMain's loop.
exit(MainActor.assumeIsolated { CLI.run(cliArgs) })
}
runOnMain { CLI.run(cliArgs) }
}
if CommandLine.arguments.contains("--selftest") {
SelfTest.installTestApprover() // never show a real confirmation window during the self-test
exit(SelfTest.run() ? 0 : 1)
}
if CommandLine.arguments.contains("--mcp") {
MCPServer.run()
exit(0)
runOnMain { MCPServer.run(); return 0 }
}
MacCleanerApp.main()
}

/// Runs `body` on the main actor inside a running main run loop, then exits with its code. Used by `--mcp` and
/// `--cli` only. The body starts from a main-queue task, so everything it does (including the approval dialog's
/// run-loop mode and any `@MainActor` hop) sees a live main run loop.
///
/// Never `dispatchMain()`: it parks the main thread in the dispatch main-queue drain, so the main CFRunLoop is not
/// run in its default/common modes (the probe reports mode=nil) and `RunLoop.main` timers and sources outside a
/// modal are not serviced. (`NSAlert.runModal` would still spin the loop itself.)
/// `CFRunLoopRun()` returns when no sources remain, hence the loop. Exit codes go through `exit(...)` unchanged.
///
/// Bench (`--cli bench`) and the self-test stay outside: both spin `RunLoop.main` themselves and wait for work
/// from the main thread, which would hang or re-enter if they ran inside a main-queue callout.
static func runOnMain(_ body: @escaping @MainActor () -> Int32) -> Never {
Task { @MainActor in exit(body()) }
while true { CFRunLoopRun() }
}
}

struct MacCleanerApp: App {
Expand Down
Loading
Loading