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): wait for Chrome web content on first read; point background browser_prepare refusals to the AX route (CUA-1766) (#5305)
Francesco Bonacci4f7084573e77
diff --git a/libs/cua-driver/rust/Skills/cua-driver/SKILL.md b/libs/cua-driver/rust/Skills/cua-driver/SKILL.mdindex b8ac29d63e..e1719d6970 100644--- a/libs/cua-driver/rust/Skills/cua-driver/SKILL.md+++ b/libs/cua-driver/rust/Skills/cua-driver/SKILL.md@@ -58,6 +58,7 @@ The MCP server instructions carry the same rules, so they apply even when this f| Verify the outcome | `verify_state({pid, window_id, expect})` or a fresh snapshot read by the agent | [Workflow](WORKFLOW.md) || Operate the authorized desktop | `get_desktop_state` → input with `target:{kind:"desktop",display_id:"primary"}` → `get_desktop_state` | [Workflow](WORKFLOW.md), [Linux](LINUX.md) on Wayland || Drive supported browser page content | `get_browser_state` → `browser_prepare` from `detail.next_call` if refused → typed browser action | [Browser](BROWSER.md) |+| Browser tools refuse in the background | Not blocked: fill the page through `get_window_state` + `set_value`/click; if the first read shows only browser chrome, read again. BLOCKED only after that fails | [Browser](BROWSER.md) || Record an explicitly requested run | `start_recording` → actions → `stop_recording`; verify artifacts | [Recording](RECORDING.md) || Finish | Stop after proof; `end_session` for this run, not `cua-driver stop` on a shared service | [Runtime](RUNTIME.md) |diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/prepare.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/prepare.rsindex c6faae7192..82f7dff412 100644--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/prepare.rs+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/prepare.rs@@ -1008,12 +1008,14 @@ impl BrowserEngine {BrowserRefusalCode::BackgroundUnavailable,format!("{limitation}, so attaching now would bring the browser in front of the app the \- user is working in. Nothing was changed. A browser window on another Space than \- the user's attaches in the background: keep or put it on another Space and \- retry. Retry with delivery_mode:\"foreground\" only when nobody is using this \- screen or the user agreed to the browser coming forward; the driver then brings \- the user's app back. Otherwise work from the window's accessibility tree and \- screenshot (get_window_state), which need no attachment."+ user is working in. Nothing was changed, and browser_prepare is optional: \+ use get_window_state on the browser window and act through its accessibility \+ tree (set_value, click by element token), which needs no attachment. If the \+ first read shows only browser chrome, read the window again. A browser window \+ on another Space than the user's also attaches in the background. Retry with \+ delivery_mode:\"foreground\" only when nobody is using this screen or the \+ user agreed to the browser coming forward; the driver then brings the user's \+ app back."),).with_detail(serde_json::json!({diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/tools.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/tools.rsindex dbb2fe6535..5a041f0d46 100644--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/tools.rs+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/tools.rs@@ -809,7 +809,7 @@ impl Tool for BrowserPrepareTool {.and_then(Value::as_str).unwrap_or("attaching would bring the browser to the front").to_owned();- crate::delivery::background_unavailable_result(+ crate::delivery::background_unavailable_result_with_suggestion(format!("browser_prepare refused: {}", refusal.message),refusal.code.as_str(),reason,@@ -817,6 +817,7 @@ impl Tool for BrowserPrepareTool {"status": "refused","refusal": refusal,}),+ BROWSER_PREPARE_BACKGROUND_SUGGESTION,)}Err(refusal) => refusal.to_tool_result(),@@ -824,6 +825,10 @@ impl Tool for BrowserPrepareTool {}}+/// `browser_prepare` is optional: the page's accessibility tree is a working+/// background route, so the refusal leads with it.+const BROWSER_PREPARE_BACKGROUND_SUGGESTION: &str = "Not blocked: use get_window_state on this window and act through the accessibility tree (set_value, click by element token). If the first read shows only browser chrome and no AXWebArea, read the window again after a moment. Use delivery_mode:\"foreground\" only if that fails and nobody is using the screen.";+fn isolated_session_lifecycle(session: &str,owner_transport: &str,diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/v2_tests.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/v2_tests.rsindex f0dab72300..a036c3a4d6 100644--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/v2_tests.rs+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/v2_tests.rs@@ -1519,6 +1519,13 @@ async fn background_existing_profile_attach_refuses_before_any_visible_effect()let refused = structured(&result);assert_eq!(refused["code"], "background_unavailable", "{refused}");assert_eq!(refused["escalation"]["recommended"], "foreground");+ let suggestion = refused["suggestion"].as_str().unwrap();+ assert!(+ suggestion.starts_with("Not blocked: use get_window_state"),+ "the refusal must lead with the working route: {suggestion}"+ );+ let message = refused["refusal"]["message"].as_str().unwrap();+ assert!(message.contains("use get_window_state"), "{message}");assert_eq!(refused["status"], "refused");assert_eq!(refused["refusal"]["code"], "background_unavailable");assert_eq!(refused["refusal"]["detail"]["frontmost_pid"], 99);diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/delivery.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/delivery.rsindex 36bb84833e..3046c0083c 100644--- a/libs/cua-driver/rust/crates/cua-driver-core/src/delivery.rs+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/delivery.rs@@ -64,13 +64,33 @@ pub fn background_unavailable_result(code: &str,escalation_reason: impl Into<String>,fields: Value,+) -> ToolResult {+ background_unavailable_result_with_suggestion(+ message,+ code,+ escalation_reason,+ fields,+ FOREGROUND_RETRY_SUGGESTION,+ )+}++/// Like [`background_unavailable_result`], but `suggestion` replaces the+/// foreground retry text. Use it when a working background route exists, so the+/// refusal leads the caller to that route instead of implying the task is+/// blocked. The foreground `escalation` is still included.+pub fn background_unavailable_result_with_suggestion(+ message: impl Into<String>,+ code: &str,+ escalation_reason: impl Into<String>,+ fields: Value,+ suggestion: &str,) -> ToolResult {let mut structured = match fields {Value::Object(fields) => fields,_ => serde_json::Map::new(),};structured.insert("code".to_owned(), json!(code));- structured.insert("suggestion".to_owned(), json!(FOREGROUND_RETRY_SUGGESTION));+ structured.insert("suggestion".to_owned(), json!(suggestion));structured.insert("escalation".to_owned(),json!({@@ -119,6 +139,21 @@ mod tests {}}+ #[test]+ fn a_custom_suggestion_replaces_only_the_foreground_retry_text() {+ let result = background_unavailable_result_with_suggestion(+ "refused",+ "background_unavailable",+ "why",+ json!({}),+ "use get_window_state",+ );+ let structured = result.structured_content.unwrap();+ assert_eq!(structured["suggestion"], "use get_window_state");+ assert_eq!(structured["code"], "background_unavailable");+ assert_eq!(structured["escalation"]["recommended"], "foreground");+ }+#[test]fn background_unavailable_result_carries_the_foreground_escalation() {let result = background_unavailable_result(diff --git a/libs/cua-driver/rust/crates/platform-macos/src/ax/enablement.rs b/libs/cua-driver/rust/crates/platform-macos/src/ax/enablement.rsindex 51e0bfd4f0..cc46e07de9 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/ax/enablement.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/ax/enablement.rs@@ -30,6 +30,7 @@ const MATERIALIZE_MAX_ATTEMPTS: u32 = 3;const MATERIALIZE_RETRY_BACKOFF_SECONDS: f64 = 30.0;const RUN_LOOP_PUMPS_PER_POLL: u32 = 4;const WEB_AREA_ROLE: &str = "AXWebArea";+const MENU_BAR_ROLE: &str = "AXMenuBar";const WEB_AREA_MAX_DEPTH: u32 = 10;const WEB_AREA_PROBE_NODES: u32 = 400;@@ -126,16 +127,29 @@ fn wait_outcome() -> Option<Wait> {match opt_in {AccessibilityOptIn::NotAccepted => None,- AccessibilityOptIn::EnhancedUserInterface => Some(Wait::Complete),- AccessibilityOptIn::ManualAccessibility => Some(if await_tree() {- Wait::Complete- } else {- Wait::TimedOut {- attempts: prior_timeouts + 1,- attempted_at,- }- }),+ // Chrome builds its web tree asynchronously after either opt-in, so+ // both wait for it. Returning at once for `AXEnhancedUserInterface`+ // let the first read return browser chrome only (CUA-1766).+ AccessibilityOptIn::EnhancedUserInterface | AccessibilityOptIn::ManualAccessibility => {+ Some(if await_tree() {+ Wait::Complete+ } else {+ Wait::TimedOut {+ attempts: prior_timeouts + 1,+ attempted_at,+ }+ })+ }+ }+}++unsafe fn web_area_is_populated(web_area: AXUIElementRef) -> bool {+ let children = copy_children(web_area);+ let populated = !children.is_empty();+ for child in children {+ CFRelease(child as CFTypeRef);}+ populated}unsafe fn has_web_area(element: AXUIElementRef, depth: u32, visits: &mut u32) -> bool {@@ -146,8 +160,15 @@ unsafe fn has_web_area(element: AXUIElementRef, depth: u32, visits: &mut u32) ->for child in copy_children(element) {if !found && *visits > 0 {*visits -= 1;- found = copy_string_attr(child, "AXRole").as_deref() == Some(WEB_AREA_ROLE)- || has_web_area(child, depth - 1, visits);+ let role = copy_string_attr(child, "AXRole");+ found = match role.as_deref() {+ // Menu items are numerous and never hold web content.+ Some(MENU_BAR_ROLE) => false,+ // An AXWebArea with no children is a stub: Chrome has not+ // built the page tree yet, so it does not count as content.+ Some(WEB_AREA_ROLE) => web_area_is_populated(child),+ _ => has_web_area(child, depth - 1, visits),+ };}CFRelease(child as CFTypeRef);}@@ -337,28 +358,37 @@ mod tests {}#[test]- fn only_the_manual_accessibility_path_polls_for_web_content() {+ fn both_chromium_opt_ins_wait_for_web_content_and_others_do_not() {let now = Instant::now();+ for opt_in in [+ AccessibilityOptIn::EnhancedUserInterface,+ AccessibilityOptIn::ManualAccessibility,+ ] {+ let polls = Cell::new(0u32);+ let ready = wait_outcome(opt_in, 0, now, || {+ polls.set(polls.get() + 1);+ true+ });+ assert_eq!(ready, Some(Wait::Complete));+ assert_eq!(polls.get(), 1, "{opt_in:?} must wait for the web tree");+ assert_eq!(+ wait_outcome(opt_in, 1, now, || false),+ Some(Wait::TimedOut {+ attempts: 2,+ attempted_at: now+ }),+ "{opt_in:?}: a tree that never appears is a timeout, not Complete"+ );+ }let polls = Cell::new(0u32);- let probe = || {- polls.set(polls.get() + 1);- false- };-assert_eq!(- wait_outcome(AccessibilityOptIn::EnhancedUserInterface, 0, now, probe),- Some(Wait::Complete),- "an app that only accepts AXEnhancedUserInterface has no web area to wait for"- );- assert_eq!(- wait_outcome(AccessibilityOptIn::NotAccepted, 0, now, probe),+ wait_outcome(AccessibilityOptIn::NotAccepted, 0, now, || {+ polls.set(polls.get() + 1);+ false+ }),None);- assert_eq!(- polls.get(),- 0,- "only the Chromium opt-in may pay the materialization wait"- );+ assert_eq!(polls.get(), 0, "native apps must not pay the wait");}#[test]