Open source CapCut alternative.
TypeScript
feat(app): request macOS permissions in-conversation instead of assuming them
David Le5a0b0b4e9c82
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swiftindex 7771d725..17ca88f3 100644--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift@@ -375,7 +375,8 @@ public struct GenericHarnessRuntime: Sendable {guard let stoppedTask = await coordinator.waitForPermission(agentID: agentID,missingPermissions: result.missingPermissions,- pendingToolCall: call+ pendingToolCall: call,+ reason: HarnessPermission.permissionRequestSummary(for: result.missingPermissions)) else {return nil}diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swiftindex 367e46e3..688051e0 100644--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift@@ -139,7 +139,7 @@ public actor HarnessToolRegistry {callID: call.id,toolName: call.name,status: .permissionDenied,- summary: "Tool requires permission before it can run.",+ summary: HarnessPermission.permissionRequestSummary(for: missingPermissions),missingPermissions: missingPermissions,metadata: ["reason": "missingPermission",diff --git a/apps/Donkey/Sources/DonkeyHarness/HarnessPermissionDisplay.swift b/apps/Donkey/Sources/DonkeyHarness/HarnessPermissionDisplay.swiftnew file mode 100644index 00000000..4bacf7e9--- /dev/null+++ b/apps/Donkey/Sources/DonkeyHarness/HarnessPermissionDisplay.swift@@ -0,0 +1,28 @@+import Foundation++extension HarnessPermission {+ /// A short user-facing name for capabilities backed by a macOS permission the user grants, or nil+ /// for internal capabilities the user never sees in a prompt. The macOS mapping itself lives in+ /// `HarnessSystemPermissionBridge`; this is only the wording the notch shows.+ public var systemAccessName: String? {+ switch self {+ case .screenCapture:+ return "Screen Recording"+ case .accessibility, .input:+ return "Accessibility"+ case .conversation, .memory, .appLookup, .appControl,+ .verification, .lifecycle, .userPrompt, .skillLookup:+ return nil+ }+ }++ /// The notch banner text for a tool stopped on missing permissions, naming the macOS access the+ /// user needs to grant. Falls back to a generic line when nothing maps to a nameable permission.+ public static func permissionRequestSummary(for permissions: [HarnessPermission]) -> String {+ let names = permissions.compactMap(\.systemAccessName).reduce(into: [String]()) { unique, name in+ if !unique.contains(name) { unique.append(name) }+ }+ guard !names.isEmpty else { return "Donkey needs your permission to continue." }+ return "Donkey needs \(ListFormatter.localizedString(byJoining: names)) access to continue."+ }+}diff --git a/apps/Donkey/Sources/DonkeyRuntime/AppHarnessGenericLifecycle.swift b/apps/Donkey/Sources/DonkeyRuntime/AppHarnessGenericLifecycle.swiftindex fbbceee2..756c1c69 100644--- a/apps/Donkey/Sources/DonkeyRuntime/AppHarnessGenericLifecycle.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/AppHarnessGenericLifecycle.swift@@ -176,16 +176,24 @@ public struct AppHarnessGenericLifecycle: Sendable {return await coordinator.resume(agentID: agentID, reason: reason)}- // System (TCC) permission gate: the user approved in the notch, so NOW trigger the macOS- // permission request. Only on a successful grant do we resume the loop to re-run the tool.+ // System (TCC) permission gate raised by an executor that named the permission directly (e.g.+ // Automation for a target app): the user approved in the notch, so NOW trigger the macOS+ // request. Only once it's granted do we resume the loop to re-run the tool.if continuation.metadata["gate"] == "systemPermission" {guard let permission = Self.systemPermission(from: continuation.metadata) else { return nil }- let granted = await SystemPermissionCoordinator.request(permission)- guard granted else { return nil }+ guard await HarnessSystemPermissionBridge.grantBlocking(permission) else { return nil }return await coordinator.resume(agentID: agentID, reason: reason)}guard !continuation.missingPermissions.isEmpty else { return nil }+ // Capability gate raised by the registry because a tool needs a capability whose macOS+ // permission isn't held. The user approved, so request each backing system permission now;+ // only mark the capability granted (and resume) once macOS actually grants it, so the re-run+ // succeeds instead of hitting the same denial at the syscall.+ for permission in continuation.missingPermissions {+ guard let system = HarnessSystemPermissionBridge.systemPermission(for: permission) else { continue }+ guard await HarnessSystemPermissionBridge.grantBlocking(system) else { return nil }+ }return await coordinator.grantPermissions(agentID: agentID,permissions: Set(continuation.missingPermissions),diff --git a/apps/Donkey/Sources/DonkeyRuntime/HarnessSystemPermissionBridge.swift b/apps/Donkey/Sources/DonkeyRuntime/HarnessSystemPermissionBridge.swiftnew file mode 100644index 00000000..a531cc50--- /dev/null+++ b/apps/Donkey/Sources/DonkeyRuntime/HarnessSystemPermissionBridge.swift@@ -0,0 +1,49 @@+import DonkeyHarness+import Foundation++/// Bridges the harness's capability permissions (`HarnessPermission`) to the macOS TCC permissions+/// (`SystemPermission`) that actually back them. The harness is platform-agnostic and only reasons+/// about capabilities; this is the single place that says which of those need a real macOS grant and+/// that turns the user's notch approval into a system request.+public enum HarnessSystemPermissionBridge {+ /// The macOS permission a harness capability requires, or nil for capabilities that are purely+ /// internal (conversation, lookups, lifecycle) and never touch TCC.+ public static func systemPermission(for permission: HarnessPermission) -> SystemPermission? {+ switch permission {+ case .screenCapture:+ return .screenRecording+ case .accessibility, .input:+ return .accessibility+ case .conversation, .memory, .appLookup, .appControl,+ .verification, .lifecycle, .userPrompt, .skillLookup:+ return nil+ }+ }++ /// Narrows a desired capability set to what's actually available now: internal capabilities pass+ /// through untouched, and a TCC-backed one is kept only when macOS currently grants it. A+ /// capability that drops out trips the registry's permission gate the first time a tool needs it,+ /// which is what surfaces the in-notch request — so onboarding can be skipped and the permission+ /// asked for in the conversation instead of being assumed.+ public static func granting(_ desired: Set<HarnessPermission>) -> Set<HarnessPermission> {+ desired.filter { permission in+ guard let system = systemPermission(for: permission) else { return true }+ return SystemPermissionCoordinator.isGranted(system)+ }+ }++ /// Requests a macOS permission after the user approved the notch gate, and reports whether it+ /// ended up granted. Microphone and Automation resolve synchronously. Accessibility flips live+ /// once the user toggles the Settings pane this opens, so a short poll catches it without a second+ /// Approve. Screen Recording can need an app relaunch — the poll then times out and the caller+ /// keeps the gate open rather than resuming into the same denial.+ public static func grantBlocking(_ permission: SystemPermission) async -> Bool {+ if SystemPermissionCoordinator.isGranted(permission) { return true }+ if await SystemPermissionCoordinator.request(permission) { return true }+ for _ in 0..<16 {+ try? await Task.sleep(nanoseconds: 500_000_000)+ if SystemPermissionCoordinator.isGranted(permission) { return true }+ }+ return false+ }+}diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessSystemPermissionBridgeTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessSystemPermissionBridgeTests.swiftnew file mode 100644index 00000000..cd507712--- /dev/null+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessSystemPermissionBridgeTests.swift@@ -0,0 +1,59 @@+@testable import DonkeyHarness+@testable import DonkeyRuntime+import Testing++@Suite+struct HarnessSystemPermissionBridgeTests {+ @Test+ func tccBackedCapabilitiesMapToSystemPermissions() {+ #expect(HarnessSystemPermissionBridge.systemPermission(for: .screenCapture) == .screenRecording)+ #expect(HarnessSystemPermissionBridge.systemPermission(for: .accessibility) == .accessibility)+ // Synthetic input needs Accessibility, so it rides the same macOS grant.+ #expect(HarnessSystemPermissionBridge.systemPermission(for: .input) == .accessibility)+ }++ @Test+ func internalCapabilitiesHaveNoSystemPermission() {+ for capability in [HarnessPermission.conversation, .memory, .appLookup, .appControl,+ .verification, .lifecycle, .userPrompt, .skillLookup] {+ #expect(HarnessSystemPermissionBridge.systemPermission(for: capability) == nil)+ }+ }++ @Test+ func grantingKeepsInternalCapabilitiesRegardlessOfSystemState() {+ // Internal capabilities never consult macOS, so they always survive narrowing.+ let internalOnly: Set<HarnessPermission> = [.conversation, .lifecycle, .appLookup]+ #expect(HarnessSystemPermissionBridge.granting(internalOnly) == internalOnly)+ }++ @Test+ func grantingKeepsTccCapabilityIffSystemGrantsIt() {+ // A TCC-backed capability survives narrowing exactly when macOS currently grants its backing+ // permission; otherwise it drops so the first tool needing it trips the in-conversation gate.+ let narrowed = HarnessSystemPermissionBridge.granting([.lifecycle, .screenCapture, .accessibility, .input])+ #expect(narrowed.contains(.lifecycle))+ for capability in [HarnessPermission.screenCapture, .accessibility, .input] {+ let system = HarnessSystemPermissionBridge.systemPermission(for: capability)!+ #expect(narrowed.contains(capability) == SystemPermissionCoordinator.isGranted(system))+ }+ }++ @Test+ func permissionRequestSummaryNamesTheMacOSAccess() {+ #expect(+ HarnessPermission.permissionRequestSummary(for: [.accessibility])+ == "Donkey needs Accessibility access to continue."+ )+ // Duplicate-backed capabilities (input -> Accessibility) don't repeat the name.+ #expect(+ HarnessPermission.permissionRequestSummary(for: [.accessibility, .input])+ == "Donkey needs Accessibility access to continue."+ )+ // Internal-only capabilities fall back to the generic line.+ #expect(+ HarnessPermission.permissionRequestSummary(for: [.lifecycle])+ == "Donkey needs your permission to continue."+ )+ }+}diff --git a/docs/guides/permission-gate.md b/docs/guides/permission-gate.mdindex d071d9c6..6fada50f 100644--- a/docs/guides/permission-gate.md+++ b/docs/guides/permission-gate.md@@ -4,6 +4,8 @@ Every macOS permission Donkey needs while a task is running is asked for in the**The one rule:** Donkey never asks macOS for a permission it doesn't already hold until the user has approved it in the notch. A task that needs one stops and shows an Approve / Deny banner; only on Approve does the system dialog appear. The microphone is the lone exception — clicking the mic button is itself the approval, and macOS's dialog follows from there.+First-run setup is a convenience, not a prerequisite: every permission is optional there and can be skipped. Whatever wasn't granted is asked for in the conversation, through this same gate, the first time a tool actually needs it.+## How It WorksTwo decisions stack. First the user decides whether Donkey may ask macOS at all; only then does macOS decide the actual grant.@@ -55,8 +57,8 @@ Each permission Donkey can need while working, and how it is kept behind the gat| --- | --- | --- || Controlling another app | running an app-automation step | Stops at the notch gate; the system is asked only after Approve || Window housekeeping | un-minimizing a window before a screenshot | Done only if already granted, otherwise skipped — never asked (the app is already in front) |-| Screen recording | capturing the screen to see it | Checked but never asked mid-task; granted during first-run setup |-| Accessibility | reading or driving an app's controls | Checked but never asked mid-task; granted during first-run setup |+| Screen recording | capturing the screen to see it | Stops at the notch gate when not held; the system is asked only after Approve |+| Accessibility | reading or driving an app's controls | Stops at the notch gate when not held; the system is asked only after Approve || Microphone | turning on voice from the notch | The mic button is the approval; macOS's dialog follows |## Invariants