Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cua-driver): put the user's window back above a background target that opens a menu on macOS (#4955)
Francesco Bonacci984e5f9c5c5a
diff --git a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md b/libs/cua-driver/rust/Skills/cua-driver/MACOS.mdindex d05c093611..6e74564702 100644--- a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md+++ b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md@@ -83,8 +83,11 @@ frontmost is true'`). Mutating it is not.**Corollary: the AXMenuBar rule.** Do not manually drive a backgroundapplication's `AXMenuBarItem`: the visible macOS menu bar belongs to thefrontmost app, and command items may be disabled otherwise. Use `invoke_menu`-for a known application-menu path. It owns the necessary temporary activation,-resolves every AX level live, and restores the prior app on a best-effort basis. Prefer an in-window+for a known application-menu path. With the app in the background it resolves+the whole path without opening a menu and never activates the app; if macOS+keeps the item unavailable to a background app it refuses with+`background_unavailable` (then, if the user allows it, `bring_to_front` first).+With the app already in front it resolves every AX level live. Prefer an in-windowelement action when the same command has an ordinary control, and prefer`set_window_frame` for exact geometry. Full rationale is in “Navigating nativemenu bars” below.diff --git a/libs/cua-driver/rust/crates/platform-macos/src/key_focus_guard.rs b/libs/cua-driver/rust/crates/platform-macos/src/key_focus_guard.rsindex 15ef0df1e5..10082d8f2d 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/key_focus_guard.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/key_focus_guard.rs@@ -15,6 +15,15 @@//! `SLPSReleaseKeyFocusWithID` and drops the event. The user's app never sees//! the loss; the menu stays open and is still operable through accessibility.//!+//! Opening a menu also makes WindowServer order the menu's window group to the+//! front, which lifts the target's parent window above the user's window. No+//! client can stop that ordering or reorder another app's windows, so the+//! guard restores the order right after: once the menu window is on screen+//! (and again when the action ends), if a target window sits above the user's+//! front window, the already-active front app is re-activated with all its+//! windows. Key status does not move and the menu stays open; the raise is+//! visible for at most a frame or two.+//!//! Theft ids come from one session-wide counter that only the thief learns.//! [`protect`] therefore calibrates before each guarded action: it steals and//! releases key focus on the driver's own connection (the tap drops that@@ -40,17 +49,122 @@ const MENU_WAIT: Duration = Duration::from_millis(150);/// CGWindowLevel of popup menus (`kCGPopUpMenuWindowLevel`).const POPUP_MENU_LAYER: i32 = 101;+/// Whether a normal-level window of `target` sits above the frontmost+/// normal-level window of `front` in the on-screen order.+fn target_above_front(target: i32, front: i32) -> bool {+ // Every layer, without the Space queries of `visible_windows`: this runs+ // while the raise is on screen, so it has to be quick.+ let windows = crate::windows::all_windows_any_layer();+ target_above_front_in(&windows, target, front)+}++fn target_above_front_in(windows: &[crate::windows::WindowInfo], target: i32, front: i32) -> bool {+ let top = |pid: i32| {+ windows+ .iter()+ .filter(|w| w.pid == pid && w.layer == 0 && w.is_on_screen)+ .filter(|w| w.bounds.width > 1.0 && w.bounds.height > 1.0)+ .map(|w| w.z_index)+ .max()+ };+ match (top(target), top(front)) {+ (Some(t), Some(f)) => t > f,+ _ => false,+ }+}++/// Put the user's front app back above a background target that raised its+/// window (AppKit orders a popup menu's window group to the front, which+/// lifts its parent window). Re-activating the already-active front app with+/// all its windows restores the order without moving key status; the+/// target's menu stays open.+fn restore_front_if_raised(target: i32, front: i32) -> bool {+ if crate::apps::frontmost_pid() != Some(front) || !target_above_front(target, front) {+ return false;+ }+ use objc2_app_kit::{NSApplicationActivationOptions, NSRunningApplication};+ let restored = unsafe {+ NSRunningApplication::runningApplicationWithProcessIdentifier(front).is_some_and(|app| {+ app.activateWithOptions(NSApplicationActivationOptions::NSApplicationActivateAllWindows)+ })+ };+ RESTORED.fetch_add(1, Ordering::Relaxed);+ tracing::debug!(+ target,+ front,+ restored,+ "restored the user's window above a background target"+ );+ true+}++/// How long the order stays watched after a guarded action ends. A target+/// can raise a window a moment after the call that caused it returns (a+/// banner, a sheet, or a menu that a renderer process opens later).+const ORDER_WATCH: Duration = Duration::from_millis(2500);+const ORDER_POLL: Duration = Duration::from_millis(20);+const MAX_RESTORES_PER_WATCH: u32 = 3;++/// Keep restoring the user's window above `target` for a short while after+/// the action. One watcher per target; a later action extends it.+fn watch_order_after_action(target: i32, front: i32) {+ static WATCHING: OnceLock<Mutex<HashMap<i32, Instant>>> = OnceLock::new();+ let watching = WATCHING.get_or_init(|| Mutex::new(HashMap::new()));+ let until = Instant::now() + ORDER_WATCH;+ {+ let mut map = watching+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let running = map.contains_key(&target);+ map.insert(target, until);+ if running {+ return;+ }+ }+ let spawned = std::thread::Builder::new()+ .name("cua-order-watch".into())+ .spawn(move || {+ // Some apps re-raise right after each restore (a Chromium popup+ // that stays open); cap the restores so the user's app is not+ // re-activated in a loop.+ let mut restores = 0;+ loop {+ std::thread::sleep(ORDER_POLL);+ if restores < MAX_RESTORES_PER_WATCH && restore_front_if_raised(target, front) {+ restores += 1;+ }+ let mut map = watching+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if map+ .get(&target)+ .is_none_or(|until| Instant::now() >= *until)+ {+ map.remove(&target);+ return;+ }+ }+ });+ if spawned.is_err() {+ watching+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .remove(&target);+ }+}+/// Wait until `pid` shows a menu-level window, or `budget` runs out.fn wait_for_menu_window(pid: i32, budget: Duration) {let deadline = Instant::now() + budget;while Instant::now() < deadline {- if crate::windows::visible_windows()+ // `visible_windows` lists layer 0 only; a menu is at layer 101.+ if crate::windows::all_windows_any_layer().iter().any(|w| w.pid == pid && w.layer >= POPUP_MENU_LAYER && w.is_on_screen){return;}- std::thread::sleep(Duration::from_millis(3));+ std::thread::sleep(Duration::from_millis(1));}}@@ -137,6 +251,7 @@ static OWN_NOTICES: AtomicU64 = AtomicU64::new(0);static UNVERIFIED_FRONT: AtomicI32 = AtomicI32::new(0);static RELEASED: AtomicU64 = AtomicU64::new(0);static MISSED: AtomicU64 = AtomicU64::new(0);+static RESTORED: AtomicU64 = AtomicU64::new(0);/// Counters since the daemon started.pub fn stats() -> Stats {@@ -150,10 +265,18 @@ pub fn stats() -> Stats {/// user's frontmost app. Dropping it starts a short tail (see [`TAIL`]).pub struct Lease {pid: i32,+ front: i32,+ /// A target window already sat above the user's window when the action+ /// started (the user arranged it so); then nothing is restored.+ above_before: bool,}impl Drop for Lease {fn drop(&mut self) {+ if !self.above_before {+ restore_front_if_raised(self.pid, self.front);+ watch_order_after_action(self.pid, self.front);+ }let mut st = lock();if let Some((leases, until)) = st.guarded.get_mut(&self.pid) {*leases = leases.saturating_sub(1);@@ -196,7 +319,11 @@ pub fn protect(target_pid: i32) -> Option<Lease> {entry.0 += 1;entry.1 = Instant::now() + TAIL;}- let lease = Lease { pid: target_pid };+ let lease = Lease {+ pid: target_pid,+ front,+ above_before: target_above_front(target_pid, front),+ };if !ensure_tap(front) {return None;}@@ -331,6 +458,10 @@ extern "C" fn tap_callback(.name("cua-key-focus-release".into()).spawn(move || {wait_for_menu_window(thief_pid, MENU_WAIT);+ let front = lock().tap.as_ref().map(|tap| tap.front_pid);+ if let Some(front) = front {+ restore_front_if_raised(thief_pid, front);+ }if release_target_theft() {RELEASED.fetch_add(1, Ordering::Relaxed);tracing::debug!(thief_pid, "released a background key-focus theft");@@ -451,6 +582,41 @@ mod tests {assert_eq!(action, NoticeAction::ReleaseAndDrop);}+ fn win(pid: i32, layer: i32, z_index: usize) -> crate::windows::WindowInfo {+ crate::windows::WindowInfo {+ window_id: z_index as u32,+ pid,+ app_name: String::new(),+ title: String::new(),+ bounds: crate::windows::WindowBounds {+ x: 0.0,+ y: 0.0,+ width: 400.0,+ height: 300.0,+ },+ layer,+ z_index,+ is_on_screen: true,+ current_space_id: None,+ on_current_space: None,+ space_ids: None,+ }+ }++ #[test]+ fn a_target_window_above_the_users_window_is_detected() {+ // z_index: higher is closer to the front.+ let raised = [win(7, 0, 3), win(42, 0, 5), win(7, 0, 4)];+ assert!(target_above_front_in(&raised, 42, 7));+ let below = [win(7, 0, 5), win(42, 0, 3)];+ assert!(!target_above_front_in(&below, 42, 7));+ // A menu-level window does not count; only normal windows are raised.+ let menu_only = [win(7, 0, 4), win(42, 101, 9), win(42, 0, 2)];+ assert!(!target_above_front_in(&menu_only, 42, 7));+ // Without a window of either app there is nothing to restore.+ assert!(!target_above_front_in(&[win(42, 0, 9)], 42, 7));+ }+#[test]fn the_drivers_own_calibration_theft_is_swallowed() {assert_eq!(diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/invoke_menu.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/invoke_menu.rsindex 1b18cd8580..d0b73a98d8 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/invoke_menu.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/invoke_menu.rs@@ -534,6 +534,75 @@ fn exact_window_is_ready(frontmost_pid == Some(target_pid) && focused_window_id == Some(target_window_id)}+/// The item could not be invoked with its app in the background. Activating+/// the app would take the user's front window, so refuse and name the route.+fn background_unavailable(reason: String) -> ToolResult {+ let message = format!(+ "{reason}. invoke_menu does not activate a background app: macOS keeps this menu item \+ unavailable while the app is behind the user's window. If the user allows it, call \+ bring_to_front for this window first, then invoke_menu; or use the app's keyboard \+ shortcut with hotkey, or the control in the window."+ );+ ToolResult::error(message.clone()).with_structured(serde_json::json!({+ "status": "refused",+ "code": "background_unavailable",+ "refusal": { "code": "background_unavailable", "message": message }+ }))+}++/// Resolve the whole path from the app's live menu bar without opening any+/// level, then perform the final item's action. Used for a background app:+/// opening a menu-bar menu would take key focus and draw over the user's+/// front app.+unsafe fn invoke_path_in_background(+ pid: i32,+ window_id: u32,+ path: &[String],+) -> Result<(), String> {+ let app = AXUIElementCreateApplication(pid);+ if app.is_null() {+ return Err("invoke_menu: target application is unavailable".into());+ }+ set_messaging_timeout(app);+ let menu_bar = copy_element_attr(app, "AXMenuBar");+ CFRelease(app as CFTypeRef);+ let menu_bar = menu_bar.ok_or_else(|| "invoke_menu: target exposes no AXMenuBar".to_owned())?;+ set_messaging_timeout(menu_bar);+ let target = resolve_exact_prefix(menu_bar, path);+ CFRelease(menu_bar as CFTypeRef);+ let target = target?;+ set_messaging_timeout(target);+ let result = (|| {+ // AppKit validates menu items against the key window. Make the target+ // window AppKit-key without raising it or touching the user's window+ // (the focus record only), so items that need a key window enable.+ if copy_bool_attr(target, "AXEnabled") == Some(false)+ && crate::input::skylight::activate_without_raise_with(pid, window_id, false)+ {+ std::thread::sleep(Duration::from_millis(120));+ }+ if copy_bool_attr(target, "AXEnabled") == Some(false) {+ return Err(format!(+ "invoke_menu: path segment {} is disabled while the app is in the background",+ path.len().saturating_sub(1)+ ));+ }+ let actions = copy_action_names(target);+ let action = choose_action(&actions, true).ok_or_else(|| {+ "invoke_menu: the final menu item has no usable native menu action".to_owned()+ })?;+ let error = perform_action(target, action);+ if error != kAXErrorSuccess {+ return Err(format!(+ "invoke_menu: native action for the final menu item failed with AX error {error}"+ ));+ }+ Ok(())+ })();+ CFRelease(target as CFTypeRef);+ result+}+fn refusal(message: String) -> ToolResult {ToolResult::error(message.clone()).with_structured(serde_json::json!({"status": "refused",@@ -576,6 +645,49 @@ impl Tool for InvokeMenuTool {return refusal("invoke_menu: window_id does not belong to pid".into());}+ // A background target: invoke the item without activating the app or+ // raising its window. When macOS keeps the item unavailable to a+ // background app, refuse and say so instead of activating it.+ let target_in_front = tokio::task::spawn_blocking(move || {+ live_frontmost_app().or_else(crate::apps::frontmost_pid) == Some(pid)+ })+ .await+ .unwrap_or(false);+ if !target_in_front {+ let background_path = path.clone();+ let outcome = tokio::task::spawn_blocking(move || {+ let _key_focus = crate::key_focus_guard::protect(pid);+ unsafe { invoke_path_in_background(pid, window_id, &background_path) }+ })+ .await;+ return match outcome {+ Ok(Ok(())) => ToolResult::text(+ "Invoked the native menu item through accessibility with the app in the background (not activated); verify the command's semantic effect from fresh state.",+ )+ .with_action_record(+ ActionExecutionRecord::builder(+ ActionEffect::Unverifiable,+ ActionTransport::MacosAxAction,+ RequestedDelivery::Background,+ )+ .actual_delivery(ActualDelivery::Background)+ .evidence(ActionEvidence {+ kind: EvidenceKind::NativeApiResult,+ detail: "Every menu hop resolved uniquely without opening it and AX accepted the final action".into(),+ })+ .build()+ .expect("invoke_menu record is valid"),+ ),+ // Only a disabled item is a background limit; a missing or+ // ambiguous path is the same refusal as in the foreground.+ Ok(Err(error)) if error.contains("while the app is in the background") => {+ background_unavailable(error)+ }+ Ok(Err(error)) => refusal(error),+ Err(error) => refusal(format!("invoke_menu: blocking task failed: {error}")),+ };+ }+let outcome = tokio::task::spawn_blocking(move || {let prior_frontmost = live_frontmost_app().or_else(crate::apps::frontmost_pid);let prior_frontmost_window =@@ -632,6 +744,17 @@ impl Tool for InvokeMenuTool {mod tests {use super::*;+ #[test]+ fn a_background_menu_refusal_names_the_code_and_the_route() {+ let result = background_unavailable("invoke_menu: path segment 1 is disabled".into());+ assert_eq!(result.is_error, Some(true));+ let structured = result.structured_content.expect("structured refusal");+ assert_eq!(structured["code"], "background_unavailable");+ let message = structured["refusal"]["message"].as_str().unwrap();+ assert!(message.contains("does not activate a background app"));+ assert!(message.contains("bring_to_front"));+ }+#[test]fn path_normalization_rejects_empty_segments() {assert_eq!(diff --git a/libs/cua-driver/rust/crates/platform-macos/src/windows.rs b/libs/cua-driver/rust/crates/platform-macos/src/windows.rsindex 9acee4f7cb..461228dfc3 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/windows.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/windows.rs@@ -94,7 +94,7 @@ pub(crate) fn visible_windows_with_space_snapshot() -> WindowEnumeration {/// layer filter on enumeration and off identity lookup is what lets/// `get_window_state` tell "no such window" apart from "exists, but is not a/// layer-0 window" (issue #2237).-fn all_windows_any_layer() -> Vec<WindowInfo> {+pub(crate) fn all_windows_any_layer() -> Vec<WindowInfo> {enumerate_windows(kCGWindowListExcludeDesktopElements, LayerFilter::AnyLayer).windows}