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): activate browsers fully and keep the field focus before foreground typing (#4835)
Francesco Bonaccic90e94662e99
diff --git a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md b/libs/cua-driver/rust/Skills/cua-driver/MACOS.mdindex 2fd332b2df..6ed6177aee 100644--- a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md+++ b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md@@ -113,6 +113,7 @@ is safe even for apps that normally foreground on media-load| Screenshot | `get_window_state` (window) or authorized `get_desktop_state` (desktop) | `screencapture` || Quit an app | ask the user first, then `hotkey({pid, keys:["cmd","q"]})` | `kill`, `killall`, `pkill` || Hand a file/URL to an app | `launch_app({bundle_id, urls:[<path>]})` | `open -a <App> <path>`, `open <url>` |+| View a local file in the GUI | `launch_app({bundle_id, urls:["file:///…"]})` | typing `file://` into the omnibox |### The narrow carve-outdiff --git a/libs/cua-driver/rust/Skills/cua-driver/SKILL.md b/libs/cua-driver/rust/Skills/cua-driver/SKILL.mdindex b6571346a9..eeea1110cb 100644--- a/libs/cua-driver/rust/Skills/cua-driver/SKILL.md+++ b/libs/cua-driver/rust/Skills/cua-driver/SKILL.md@@ -49,6 +49,7 @@ The MCP server instructions carry the same rules, so they apply even when this f| ----------------------------------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------- || Check installation and capabilities | `cua-driver --version`, `status`, `doctor`, `describe <tool>`; MCP `tools/list` | [Runtime](RUNTIME.md) || Find or open the requested app | `list_apps`, `list_windows`, `launch_app` | Current platform guide below |+| View a local file in the GUI | `launch_app({bundle_id, urls:["file:///…"]})` opens it in that app | Current platform guide below || Observe one window | `get_window_state({pid, window_id, query})` | [Workflow](WORKFLOW.md) || Act on a control | `click` / `type_text` with a fresh `element_token` and exact window target | [Workflow](WORKFLOW.md) || Act and see what changed in one call | `run_actions({steps:[{tool,args},...], observe:true})`; stops at the first failure | [Workflow](WORKFLOW.md#batch-known-actions) |diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/bring_to_front.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/bring_to_front.rsindex 9b28bdce19..db0770bcd4 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/bring_to_front.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/bring_to_front.rs@@ -235,6 +235,39 @@ fn wait_for_exact_window(pid: i32, window_id: u32) -> ExactWindowObservation {}}+/// Activate `pid` and make `window_id` its key window exactly as+/// `bring_to_front` does (SkyLight process front, exact make-key, Cocoa+/// activation, AX raise), then wait for the exact postcondition. Blocking.+///+/// Used by the foreground `type_text` rung: a SkyLight front alone does not+/// make the app active for AppKit, so a window the typing opens (Chrome's+/// omnibox suggestions) hands the front back to the previous app mid-string.+pub(crate) fn activate_exact_window_blocking(pid: i32, window_id: u32) -> bool {+ let Some(app) = (unsafe { NSRunningApplication::runningApplicationWithProcessIdentifier(pid) })+ else {+ return false;+ };+ crate::input::skylight::set_front_process_persistently(pid, window_id);+ crate::input::skylight::make_exact_window_key(pid, window_id);+ unsafe {+ app.activateWithOptions(NSApplicationActivationOptions::NSApplicationActivateAllWindows)+ };+ raise_exact_ax_window(pid, window_id);+ wait_for_exact_window(pid, window_id).exact_postcondition(pid, window_id)+}++/// Ask `pid` to take activation back through Cocoa after a foreground action+/// that activated another app with [`activate_exact_window_blocking`]. Only+/// the app's own key window comes forward. Best effort: macOS may refuse an+/// activation requested by a background process.+pub(crate) fn reactivate_application(pid: i32) -> bool {+ let Some(app) = (unsafe { NSRunningApplication::runningApplicationWithProcessIdentifier(pid) })+ else {+ return false;+ };+ unsafe { app.activateWithOptions(NSApplicationActivationOptions::empty()) }+}+/// Best-effort completion of the one exact-window request. This addresses only/// the requested AX window; it never orders every window owned by the process.fn raise_exact_ax_window(pid: i32, window_id: u32) -> bool {diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rsindex 9040f1c094..3ac8d59627 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs@@ -1048,6 +1048,92 @@ fn classify_target_web_area(}}+/// Whether `pid` is a web browser whose omnibox and pages the foreground+/// typing rung must activate fully (see the browser branch of the rung).+fn is_browser_pid(pid: i32) -> bool {+ let bundle_id = apps::bundle_id_for_pid(pid).unwrap_or_default();+ crate::browser::browser_js::BrowserJs::supports(&bundle_id) || is_chromium_identity(&bundle_id)+}++fn is_chromium_identity(bundle_id: &str) -> bool {+ bundle_id+ .to_ascii_lowercase()+ .split(|ch: char| !ch.is_ascii_alphanumeric())+ .any(|token| {+ matches!(+ token,+ "chrome" | "chromium" | "brave" | "edgemac" | "vivaldi" | "opera" | "thorium"+ )+ })+}++const SELECTED_RANGE: &str = "AXSelectedTextRange";++/// Copy an attribute's raw value (+1 retained), whatever its type.+///+/// # Safety+///+/// `element` must be a valid, live `AXUIElementRef`; the caller releases the+/// returned value.+unsafe fn copy_raw_attr(+ element: AXUIElementRef,+ name: &str,+) -> Option<core_foundation::base::CFTypeRef> {+ use core_foundation::{base::TCFType, string::CFString};+ let attr = CFString::new(name);+ let mut value: core_foundation::base::CFTypeRef = std::ptr::null();+ let err = crate::ax::bindings::AXUIElementCopyAttributeValue(+ element,+ attr.as_concrete_TypeRef(),+ &mut value,+ );+ (err == kAXErrorSuccess && !value.is_null()).then_some(value)+}++/// Set an attribute to a raw value previously read with [`copy_raw_attr`].+///+/// # Safety+///+/// `element` and `value` must be valid for the duration of the call.+unsafe fn set_raw_attr(+ element: AXUIElementRef,+ name: &str,+ value: core_foundation::base::CFTypeRef,+) {+ use core_foundation::{base::TCFType, string::CFString};+ let attr = CFString::new(name);+ let _ = crate::ax::bindings::AXUIElementSetAttributeValue(+ element,+ attr.as_concrete_TypeRef(),+ value,+ );+}++/// After an activation, wait until WindowServer has kept `window_id`'s+/// process front for [`FRONT_SETTLE_STABLE`], bounded by+/// [`FRONT_SETTLE_TIMEOUT`]. Returns whether it settled.+fn await_front_settled(pid: i32, window_id: u32) -> bool {+ let deadline = std::time::Instant::now() + FRONT_SETTLE_TIMEOUT;+ let mut since: Option<std::time::Instant> = None;+ loop {+ let now = std::time::Instant::now();+ if crate::input::skylight::front_process_matches(pid, window_id) == Some(true) {+ if now.duration_since(*since.get_or_insert(now)) >= FRONT_SETTLE_STABLE {+ return true;+ }+ } else {+ since = None;+ }+ if now >= deadline {+ return false;+ }+ std::thread::sleep(std::time::Duration::from_millis(20));+ }+}++const FRONT_SETTLE_STABLE: std::time::Duration = std::time::Duration::from_millis(250);+const FRONT_SETTLE_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(1500);+/// Type via CGEvent keystrokes at the current insertion point, then verify by/// read-back. `type_text` is deliberately non-idempotent: it must never clear/// an existing value merely because AX cannot read that value back.@@ -1216,13 +1302,13 @@ fn type_text_blocking(// dropped. 200ms covers that re-grab without penalizing an already// armed interactive stream on every text chunk.let foreground_settle_ms = foreground_settle_ms(pid, apps::frontmost_pid());- let do_type = || {+ let do_type = |element: Option<(usize, Option<usize>)>| {cgevent_type_verified(pid,text,delay_ms,before.as_deref(),- element_ptr_and_idx,+ element,foreground_settle_ms,window_id,)@@ -1249,6 +1335,58 @@ fn type_text_blocking()?;((false, None), true)}+ Some(wid) if is_browser_pid(pid) => {+ // A browser needs a real activation, and its text focus kept.+ // A SkyLight front alone is not a Cocoa activation: the+ // omnibox suggestion window hands the front back to the+ // previous app and the rest of the string is lost. A Cocoa+ // activation re-installs the window's remembered first+ // responder (the page), so keys typed after it miss the+ // omnibox. "file:///…/archive-access.log" arrived as+ // "file://g" (bench CDB-G04, CUA-1223). So: remember the+ // focused field, activate exactly as bring_to_front does, wait+ // until the front holds, put focus back on the field, type,+ // then ask the previous app to take activation back. macOS may+ // refuse that from a background process and leave the browser+ // front, as it already did after a foreground omnibox Return.+ let previous_pid = apps::frontmost_pid().filter(|previous| *previous != pid);+ let remembered = match element_ptr_and_idx {+ Some(_) => None,+ None => unsafe { crate::ax::exact_target::focused_element_in_window(pid, wid) },+ };+ // The caret or selection (cmd+L selects the whole URL) is+ // part of what activation resets: keep it too.+ let selection = remembered+ .and_then(|element| unsafe { copy_raw_attr(element, SELECTED_RANGE) });+ super::bring_to_front::activate_exact_window_blocking(pid, wid);+ await_front_settled(pid, wid);+ let mut typed_delivery = (false, None);+ let fronted =+ crate::input::skylight::with_foreground_assist(pid as libc::pid_t, wid, || {+ if let Some(element) = remembered {+ let _ = crate::input::ax_actions::focus_element(element as usize);+ if let Some(selection) = selection {+ unsafe { set_raw_attr(element, SELECTED_RANGE, selection) };+ }+ }+ // The remembered field is focused again; the read-back+ // follows the focused element.+ typed_delivery = do_type(element_ptr_and_idx)?;+ Ok(())+ });+ unsafe {+ if let Some(selection) = selection {+ CFRelease(selection);+ }+ if let Some(element) = remembered {+ CFRelease(element as core_foundation::base::CFTypeRef);+ }+ }+ if let Some(previous_pid) = previous_pid {+ super::bring_to_front::reactivate_application(previous_pid);+ }+ (typed_delivery, fronted?)+ }Some(wid) => {// Front → type → restore. The closure returns the read-back// result; with_foreground_assist returns whether it actually@@ -1259,14 +1397,14 @@ fn type_text_blocking(pid as libc::pid_t,wid,|| {- typed_delivery = do_type()?;+ typed_delivery = do_type(element_ptr_and_idx)?;Ok(())},)?;(typed_delivery, fronted)}// No window to front — best-effort background keystrokes instead.- None => (do_type()?, false),+ None => (do_type(element_ptr_and_idx)?, false),};// Only claim the `_fg` path when a front actually happened; when no// foregrounding occurred (no window, or SPIs unavailable) these were@@ -1422,6 +1560,28 @@ fn type_text_blocking(#[cfg(test)]mod tests {+ #[test]+ fn browser_identities_take_the_activating_rung() {+ for id in [+ "com.google.Chrome",+ "com.google.Chrome.canary",+ "org.chromium.Chromium",+ "com.brave.Browser",+ "com.microsoft.edgemac",+ "com.vivaldi.Vivaldi",+ ] {+ assert!(super::is_chromium_identity(id), "{id}");+ }+ for id in [+ "org.libreoffice.script",+ "com.apple.TextEdit",+ "com.tinyspeck.slackmacgap",+ "",+ ] {+ assert!(!super::is_chromium_identity(id), "{id}");+ }+ }+use super::*;/// A semantic-only policy must refuse the terminal short-circuit before