Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): keep polling macOS grants after the permissions gate times out (#6739)

injaneityf54add4681c0
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/main.rs b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
index 245c079274..4bcdc4be6e 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/main.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
@@ -711,6 +711,7 @@ fn main() {
// already active. Honors --no-permissions-gate and
// CUA_DRIVER_RS_PERMISSIONS_GATE=0 for CI / headless.
//
+ let slow_opts = gate_opts.after_timeout();
let gate_result = platform_macos::permissions::run_if_needed_with_observer(
gate_opts,
|progress, context| match progress {
@@ -729,9 +730,9 @@ fn main() {
}
},
);
- if gate_result.is_ok() {
- serve::set_permission_gate_pending(false);
- }
+ serve::settle_permission_gate(gate_result.is_ok(), move || {
+ platform_macos::permissions::gate::wait_for_grants(&slow_opts)
+ });
let gate_context = platform_macos::permissions::gate::telemetry_context();
if gate_context.engaged {
telemetry::capture_permissions_gate_completed(
@@ -749,8 +750,8 @@ fn main() {
if let Err(e) = gate_result {
eprintln!("[cua-driver] permissions gate: {e}");
eprintln!(
- "[cua-driver] desktop tool calls remain gated; grant Accessibility and \
- Screen Recording permissions, then restart the daemon."
+ "[cua-driver] desktop tool calls remain gated until Accessibility and \
+ Screen Recording are granted; still checking in the background."
);
}
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/serve.rs b/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
index eee4e326ae..b91940fbef 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
@@ -694,6 +694,38 @@ pub fn set_permission_gate_pending(pending: bool) {
PERMISSION_GATE_PENDING.store(pending, std::sync::atomic::Ordering::Release);
}
+#[cfg(target_os = "macos")]
+/// Settle the gate after its interactive phase. A pass opens tool calls now.
+/// A timeout keeps them gated while `wait_after_timeout` keeps polling on its
+/// own thread, so a later grant opens them without a restart.
+pub fn settle_permission_gate(
+ gate_passed: bool,
+ wait_after_timeout: impl FnOnce() -> anyhow::Result<()> + Send + 'static,
+) {
+ settle_gate_flag(&PERMISSION_GATE_PENDING, gate_passed, wait_after_timeout);
+}
+
+#[cfg(target_os = "macos")]
+fn settle_gate_flag(
+ pending: &'static std::sync::atomic::AtomicBool,
+ gate_passed: bool,
+ wait_after_timeout: impl FnOnce() -> anyhow::Result<()> + Send + 'static,
+) -> Option<std::thread::JoinHandle<()>> {
+ if gate_passed {
+ pending.store(false, std::sync::atomic::Ordering::Release);
+ return None;
+ }
+ let poll = std::thread::Builder::new()
+ .name("cua-permissions-poll".into())
+ .spawn(move || {
+ if wait_after_timeout().is_ok() {
+ pending.store(false, std::sync::atomic::Ordering::Release);
+ }
+ })
+ .expect("spawn permissions poll thread");
+ Some(poll)
+}
+
fn permission_gate_pending_response(request: &DaemonRequest) -> Option<DaemonResponse> {
permission_gate_response_for_state(
request,
@@ -3093,6 +3125,38 @@ mod permission_gate_routing_tests {
assert_eq!(response.exit_code, Some(75));
}
+ #[cfg(target_os = "macos")]
+ #[test]
+ fn a_late_grant_reopens_tool_calls_without_a_restart() {
+ use std::sync::atomic::{AtomicBool, Ordering};
+ // Private flags: the daemon tests in this binary read the real one.
+ fn gated(pending: &AtomicBool) -> bool {
+ permission_gate_response_for_state(
+ &call("list_windows"),
+ pending.load(Ordering::Acquire),
+ )
+ .is_some()
+ }
+
+ static PASSED: AtomicBool = AtomicBool::new(true);
+ assert!(super::settle_gate_flag(&PASSED, true, || unreachable!()).is_none());
+ assert!(!gated(&PASSED));
+
+ static NEVER_GRANTED: AtomicBool = AtomicBool::new(true);
+ super::settle_gate_flag(&NEVER_GRANTED, false, || anyhow::bail!("not granted"))
+ .expect("a timed-out gate keeps polling")
+ .join()
+ .unwrap();
+ assert!(gated(&NEVER_GRANTED));
+
+ static LATE_GRANT: AtomicBool = AtomicBool::new(true);
+ super::settle_gate_flag(&LATE_GRANT, false, || Ok(()))
+ .expect("a timed-out gate keeps polling")
+ .join()
+ .unwrap();
+ assert!(!gated(&LATE_GRANT));
+ }
+
#[test]
fn calls_resume_only_after_the_gate_completes() {
assert!(permission_gate_response_for_state(&call("check_permissions"), true).is_some());
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/permissions/gate.rs b/libs/cua-driver/rust/crates/platform-macos/src/permissions/gate.rs
index 1d25a708ab..d5f799c2ba 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/permissions/gate.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/permissions/gate.rs
@@ -237,6 +237,18 @@ impl Default for GateOpts {
}
impl GateOpts {
+ /// Options for the wait that continues after the interactive gate timed
+ /// out: no deadline, so a later grant still unblocks the running daemon,
+ /// at a slower cadence than the interactive poll.
+ pub fn after_timeout(&self) -> Self {
+ Self {
+ deadline: None,
+ poll_interval: Duration::from_secs(5),
+ status_interval: Duration::from_secs(5 * 60),
+ ..self.clone()
+ }
+ }
+
/// Construct from the standard env-var
/// (`CUA_DRIVER_RS_PERMISSIONS_GATE` set to `0` / `false` / `no` /
/// `off`, case-insensitive, disables the gate), an explicit
@@ -550,9 +562,16 @@ fn present_panel_if_available(initial: PermissionsStatus) -> PanelPresentation {
/// short-lived copy of the signed executable, so the long-lived daemon never
/// caches a negative preflight and never needs to replace its process image.
pub fn wait_for_grants(opts: &GateOpts) -> Result<()> {
+ wait_for_grants_with(opts, check_required_permissions)
+}
+
+fn wait_for_grants_with(
+ opts: &GateOpts,
+ mut probe: impl FnMut() -> Vec<MissingPermission>,
+) -> Result<()> {
let start = Instant::now();
let mut last_status_print = start;
- let mut last_missing: Vec<MissingPermission> = check_required_permissions();
+ let mut last_missing: Vec<MissingPermission> = probe();
loop {
if last_missing.is_empty() {
@@ -573,7 +592,7 @@ pub fn wait_for_grants(opts: &GateOpts) -> Result<()> {
std::thread::sleep(opts.poll_interval);
- let new_missing = check_required_permissions();
+ let new_missing = probe();
if new_missing != last_missing {
// State changed (red→green, or order-flipped). Re-emit so the
// user sees progress without waiting for the next status tick.
@@ -684,6 +703,34 @@ mod tests {
clear_telemetry_env();
}
+ #[test]
+ fn wait_after_timeout_picks_up_a_late_grant() {
+ let interactive = GateOpts {
+ deadline: Some(Duration::from_nanos(1)),
+ poll_interval: Duration::from_millis(1),
+ ..GateOpts::default()
+ };
+ let still_missing = || vec![MissingPermission::Accessibility];
+ assert!(wait_for_grants_with(&interactive, still_missing).is_err());
+
+ let slow = GateOpts {
+ poll_interval: Duration::from_millis(1),
+ ..interactive.after_timeout()
+ };
+ assert_eq!(slow.deadline, None);
+ let mut polls = 0;
+ let late_grant = || {
+ polls += 1;
+ if polls < 50 {
+ vec![MissingPermission::Accessibility]
+ } else {
+ Vec::new()
+ }
+ };
+ assert!(wait_for_grants_with(&slow, late_grant).is_ok());
+ assert_eq!(polls, 50);
+ }
+
#[test]
fn opt_out_short_circuits_run_if_needed() {
// With opt_out=true the gate must return Ok(()) without touching