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): refuse disabled macOS AX actions (#2637)

Francesco Bonacci8f9c2dfd422a
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/input/ax_actions.rs b/libs/cua-driver/rust/crates/platform-macos/src/input/ax_actions.rs
index 6e5ca47190..1c8e428bac 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/input/ax_actions.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/input/ax_actions.rs
@@ -2,9 +2,30 @@
use crate::ax::bindings::*;
+fn ensure_ax_enabled(enabled: Option<bool>, action: &str) -> anyhow::Result<()> {
+ if enabled == Some(false) {
+ anyhow::bail!(
+ "refusing {action}: the target reports AXEnabled=false. \
+ Retry this action with delivery_mode:\"foreground\" or call bring_to_front first"
+ );
+ }
+ Ok(())
+}
+
+/// Refuse AX actions that macOS reports as disabled.
+///
+/// This must be checked immediately before dispatch rather than trusting the
+/// cached snapshot value: foreground delivery can make a menu item live after
+/// it was resolved, while backgrounding can disable it in the other direction.
+pub fn ensure_ax_action_enabled(element_ptr: usize, action: &str) -> anyhow::Result<()> {
+ let enabled = unsafe { copy_bool_attr(element_ptr as AXUIElementRef, "AXEnabled") };
+ ensure_ax_enabled(enabled, action)
+}
+
/// Perform an AX action on a cached element.
pub fn perform_ax_action(element_ptr: usize, action: &str) -> anyhow::Result<()> {
let ax_action = map_action(action);
+ ensure_ax_action_enabled(element_ptr, ax_action)?;
let err = unsafe { perform_action(element_ptr as AXUIElementRef, ax_action) };
if err == kAXErrorSuccess {
@@ -26,6 +47,26 @@ fn map_action(action: &str) -> &'static str {
}
}
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn disabled_elements_are_refused_before_dispatch() {
+ let error = ensure_ax_enabled(Some(false), "AXPick").unwrap_err();
+ let message = error.to_string();
+ assert!(message.contains("AXEnabled=false"));
+ assert!(message.contains("delivery_mode:\"foreground\""));
+ assert!(message.contains("bring_to_front"));
+ }
+
+ #[test]
+ fn enabled_or_unreported_state_is_allowed() {
+ assert!(ensure_ax_enabled(Some(true), "AXPress").is_ok());
+ assert!(ensure_ax_enabled(None, "AXPress").is_ok());
+ }
+}
+
/// Set AXFocused=true on an element (for pre-focusing before key press).
pub fn focus_element(element_ptr: usize) -> anyhow::Result<()> {
let err = unsafe { set_bool_attr_true(element_ptr as AXUIElementRef, "AXFocused") };
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
index 8c4cb1e1d8..d027093586 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
@@ -969,6 +969,12 @@ fn perform_ax_click(
let ax_action = map_action(action_str);
let element = element_ptr as AXUIElementRef;
+ // Check the live value immediately before dispatch. Foreground assist can
+ // enable menu items that were disabled in the cached snapshot, while a
+ // background transition can disable them after that snapshot. macOS may
+ // otherwise return success for a disabled action that did nothing.
+ crate::input::ax_actions::ensure_ax_action_enabled(element_ptr, ax_action)?;
+
// Capture advertised actions BEFORE dispatching so we can detect silent no-ops
// (AX returns success even when the element doesn't advertise the action).
let advertised = unsafe { copy_action_names(element) };