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): don't open a background web <select>; steer to set_value (CUA-1795) (#5353)

Francesco Bonacciadd1383b0c60
diff --git a/docs/content/docs/cua-driver/reference/mcp-tools/click.mdx b/docs/content/docs/cua-driver/reference/mcp-tools/click.mdx
index f350851836..be41898bd0 100644
--- a/docs/content/docs/cua-driver/reference/mcp-tools/click.mdx
+++ b/docs/content/docs/cua-driver/reference/mcp-tools/click.mdx
@@ -26,7 +26,7 @@ Click a target pid. Prefer `element_token` (from get_window_state): it identifie
- element_token: AX action path. Tokens are scoped to (pid, window_id) and stale once the window is snapshotted again.
- x, y: window-local pixels of the get_window_state screenshot, sent as CGEvent mouse events. Needs a visible window. `modifier` takes cmd/shift/option/ctrl.
-`button` defaults to left; on the AX path "right" maps to AXShowMenu and "middle" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`.
+`button` defaults to left; on the AX path "right" maps to AXShowMenu and "middle" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`. In the background a web `<select>` (Chrome, Electron) is not opened, because its menu would raise the browser window: the click is refused with `select_menu_would_raise_window`; use `set_value`.
**macOS parameters**
diff --git a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
index 53daadced3..5f4a2655c8 100644
--- a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
+++ b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
@@ -320,6 +320,14 @@ chosen with `click` on the `AXMenuItem` (or in one call with
`set_value` on the popup). Keyboard navigation of such a menu with
`press_key` is not the route; pick the item by element.
+Opening a menu also brings the app's window above the user's window for a
+moment, and no client can stop that. So in the background, a `click` on a
+web `<select>` in Chrome or an Electron app is refused before anything is
+sent (`select_menu_would_raise_window`): choose the option with
+`set_value` on the same element, which types the option's text into the
+closed control and never opens the menu. Native popups and context menus
+still open and flash the window briefly.
+
### Apps that activate themselves
Some apps activate themselves in answer to input, even in the background:
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 67bfae23e4..3bfa28773a 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
@@ -319,7 +319,7 @@ fn def() -> &'static ToolDef {
- element_token: AX action path. Tokens are scoped to (pid, window_id) and stale once the window is snapshotted again.\n\
- x, y: window-local pixels of the get_window_state screenshot, sent as CGEvent mouse events. Needs a visible window. `modifier` takes cmd/shift/option/ctrl.\n\
\n\
- `button` defaults to left; on the AX path \"right\" maps to AXShowMenu and \"middle\" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`.".into(),
+ `button` defaults to left; on the AX path \"right\" maps to AXShowMenu and \"middle\" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`. In the background a web `<select>` (Chrome, Electron) is not opened, because its menu would raise the browser window: the click is refused with `select_menu_would_raise_window`; use `set_value`.".into(),
input_schema: serde_json::json!({
"type": "object",
// `pid` is conditionally required — needed for window/element clicks
@@ -642,6 +642,27 @@ impl Tool for ClickTool {
action.clone()
};
+ // A background press or show-menu on a Chromium <select> would
+ // open its menu and raise the browser window (CUA-1795): steer to
+ // set_value before anything is sent.
+ if !delivery_mode.is_foreground()
+ && matches!(effective_action.as_str(), "press" | "show_menu")
+ {
+ let select_guard = element_guard.clone();
+ let refusal = tokio::task::spawn_blocking(move || {
+ let element = select_guard.as_ptr() as AXUIElementRef;
+ super::set_value::is_chromium_web_select(pid, wid, element).then(|| {
+ super::set_value::chromium_select_open_refusal(pid, wid, Some(idx), element)
+ })
+ })
+ .await
+ .ok()
+ .flatten();
+ if let Some(refusal) = refusal {
+ return refusal;
+ }
+ }
+
// Animate cursor to element center BEFORE firing AX action,
// mirroring Swift's `performElementClick` → `animateAndWait(to:)`.
let center_guard = element_guard.clone();
@@ -1118,6 +1139,29 @@ impl Tool for ClickTool {
}
// Animate the visual cursor to the click point and wait for it to
// arrive — mirrors Swift's `AgentCursor.shared.animateAndWait(to:)`.
+ // A background pixel click on a Chromium <select> would open its
+ // menu and raise the browser window (CUA-1795).
+ if let Some(select_wid) = window_id.filter(|_| !delivery_mode.is_foreground()) {
+ let refusal = tokio::task::spawn_blocking(move || unsafe {
+ let element = element_at_screen_position(pid, screen_x, screen_y)?;
+ let refusal = (crate::ax::exact_target::element_window_id(element)
+ == Some(select_wid)
+ && super::set_value::is_chromium_web_select(pid, select_wid, element))
+ .then(|| {
+ super::set_value::chromium_select_open_refusal(
+ pid, select_wid, None, element,
+ )
+ });
+ CFRelease(element as _);
+ refusal
+ })
+ .await
+ .ok()
+ .flatten();
+ if let Some(refusal) = refusal {
+ return refusal;
+ }
+ }
crate::cursor::overlay::animate_cursor_to(cursor_key.clone(), screen_x, screen_y).await;
// Keep the registry in sync with the overlay (see AX path above).
self.state
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
index df1b6b1aa9..3b786748c2 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
@@ -873,6 +873,56 @@ fn is_chromium_popup_owner(pid: i32) -> bool {
|| crate::browser::electron_js::ElectronJs::is_electron(pid)
}
+/// Whether pressing `element` would open a Chromium or Electron `<select>`
+/// menu: an `AXPopUpButton` inside web content of a Chromium renderer. The
+/// browser's own toolbar popups have no web-area ancestor and do not match.
+pub(super) fn is_chromium_web_select(pid: i32, window_id: u32, element: AXUIElementRef) -> bool {
+ unsafe { copy_string_attr(element, "AXRole") }.as_deref() == Some("AXPopUpButton")
+ && is_chromium_popup_owner(pid)
+ && super::type_text::target_in_web_area(
+ pid,
+ Some((element as usize, None)),
+ Some(window_id),
+ )
+}
+
+/// Refusal for a background press that would open a Chromium `<select>` menu
+/// (CUA-1795). Opening it orders the browser window above the user's window
+/// for at least a frame, which undoing afterwards cannot hide; `set_value`
+/// selects through the page's type-ahead and never opens the menu.
+pub(super) fn chromium_select_open_refusal(
+ pid: i32,
+ window_id: u32,
+ element_index: Option<usize>,
+ element: AXUIElementRef,
+) -> cua_driver_core::protocol::ToolResult {
+ let title = unsafe { copy_string_attr(element, "AXTitle") }.unwrap_or_default();
+ let current = unsafe { copy_string_attr(element, "AXValue") }.unwrap_or_default();
+ let target = match element_index {
+ Some(index) => format!("[{index}] AXPopUpButton \"{title}\""),
+ None => format!("the AXPopUpButton \"{title}\" at that point"),
+ };
+ cua_driver_core::protocol::ToolResult::error(format!(
+ "Not opened: {target} is a web dropdown (<select>). In the background, opening its \
+ menu brings the browser window above the user's window, so nothing was sent. Its \
+ current value is \"{current}\". Choose an option in one call with \
+ set_value(element_token, \"<option text>\"): it types the option's text into the \
+ closed control and the menu never opens. Open the menu with \
+ delivery_mode:\"foreground\" only if the user agreed to the browser coming forward."
+ ))
+ .with_structured(serde_json::json!({
+ "code": "select_menu_would_raise_window",
+ "effect": "refused",
+ "pid": pid,
+ "window_id": window_id,
+ "current_value": current,
+ "escalation": {
+ "recommended": "set_value",
+ "reason": "a background <select> is chosen with set_value; opening its menu would raise the browser window",
+ },
+ }))
+}
+
/// How long the page may take to apply a type-ahead selection.
const TYPEAHEAD_SETTLE: std::time::Duration = std::time::Duration::from_millis(800);
diff --git a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
index f8a25b66f4..6e4e8eb389 100644
--- a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
+++ b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
@@ -486,7 +486,7 @@
"read_only": false
},
{
- "description": "Click a target pid. Prefer `element_token` (from get_window_state): it identifies one snapshot element, works on backgrounded, minimized or off-Space windows, and moves no cursor or focus. Use `x, y` only for canvas / video / WebGL / custom-drawn surfaces missing from the AX tree.\n\n- element_token: AX action path. Tokens are scoped to (pid, window_id) and stale once the window is snapshotted again.\n- x, y: window-local pixels of the get_window_state screenshot, sent as CGEvent mouse events. Needs a visible window. `modifier` takes cmd/shift/option/ctrl.\n\n`button` defaults to left; on the AX path \"right\" maps to AXShowMenu and \"middle\" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`.",
+ "description": "Click a target pid. Prefer `element_token` (from get_window_state): it identifies one snapshot element, works on backgrounded, minimized or off-Space windows, and moves no cursor or focus. Use `x, y` only for canvas / video / WebGL / custom-drawn surfaces missing from the AX tree.\n\n- element_token: AX action path. Tokens are scoped to (pid, window_id) and stale once the window is snapshotted again.\n- x, y: window-local pixels of the get_window_state screenshot, sent as CGEvent mouse events. Needs a visible window. `modifier` takes cmd/shift/option/ctrl.\n\n`button` defaults to left; on the AX path \"right\" maps to AXShowMenu and \"middle\" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`. In the background a web `<select>` (Chrome, Electron) is not opened, because its menu would raise the browser window: the click is refused with `select_menu_would_raise_window`; use `set_value`.",
"destructive": true,
"idempotent": false,
"input_schema": {