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): wait for background tab input readiness

trycua-release[bot]a19e909db8bf
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.rs
index 89bf52a122..64b1eb7741 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
@@ -1024,6 +1024,12 @@ const EDITABLE_AND_FOCUSED_CHECK: &str = "function() { \
.includes((this.type || 'text').toLowerCase()); \
}";
+const FOCUS_EMULATION_READY_CHECK: &str = "function() { \
+ const root = this.getRootNode(); \
+ const active = ('activeElement' in root) ? root.activeElement : null; \
+ return document.hasFocus() && active === this; \
+}";
+
pub struct BrowserTypeTool {
def: ToolDef,
engine: Arc<BrowserEngine>,
@@ -1231,6 +1237,64 @@ impl Tool for BrowserTypeTool {
)
.to_tool_result();
}
+ let mut focus_ready = false;
+ let mut focus_error = None;
+ for _ in 0..20 {
+ if let Err(error) = conn
+ .call(
+ Some(cdp),
+ "DOM.focus",
+ json!({ "backendNodeId": entry.backend_node_id }),
+ )
+ .await
+ {
+ focus_error = Some(error);
+ break;
+ }
+ let ready = conn
+ .call(
+ Some(cdp),
+ "Runtime.callFunctionOn",
+ json!({
+ "objectId": object_id,
+ "functionDeclaration": FOCUS_EMULATION_READY_CHECK,
+ "returnByValue": true,
+ }),
+ )
+ .await;
+ if matches!(
+ ready,
+ Ok(ref value) if value["result"]["value"].as_bool() == Some(true)
+ ) {
+ focus_ready = true;
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(25)).await;
+ }
+ if !focus_ready {
+ let _ = conn
+ .call(
+ Some(cdp),
+ "Emulation.setFocusEmulationEnabled",
+ json!({ "enabled": false }),
+ )
+ .await;
+ return BrowserRefusal::new(
+ BrowserRefusalCode::BrowserInputTrustUnavailable,
+ format!(
+ "the exact editable ref did not become focus-ready under CDP emulation{}",
+ focus_error
+ .as_ref()
+ .map(|error| format!(": {error}"))
+ .unwrap_or_default()
+ ),
+ )
+ .to_tool_result();
+ }
+ // Chromium acknowledges focus emulation before every renderer's
+ // trusted-input path is ready. Edge on Linux can otherwise drop
+ // the first one or two characters while still returning success.
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
if let Err(error) = conn
.call(
Some(cdp),
@@ -1248,13 +1312,10 @@ impl Tool for BrowserTypeTool {
.await;
return BrowserRefusal::new(
BrowserRefusalCode::BrowserRefStale,
- format!(
- "the ref's node could not be refocused after enabling CDP focus emulation: {error}"
- ),
+ format!("the exact editable ref became stale before trusted typing: {error}"),
)
.to_tool_result();
}
- tokio::time::sleep(std::time::Duration::from_millis(25)).await;
let mut result = Ok(());
let mut delivered = 0;
for ch in text.chars() {
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.rs
index 7e5c24f128..5d06256d13 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
@@ -1880,6 +1880,15 @@ async fn keystrokes_use_char_events_for_text_delivery() {
assert_eq!(focus_emulation.len(), 2);
assert_eq!(focus_emulation[0].1["enabled"], true);
assert_eq!(focus_emulation[1].1["enabled"], false);
+ let readiness_checks = recorded_calls(&f, "Runtime.callFunctionOn");
+ assert!(readiness_checks.iter().any(|(_, params)| {
+ params["functionDeclaration"]
+ .as_str()
+ .is_some_and(|declaration| {
+ declaration.contains("document.hasFocus()")
+ && declaration.contains("active === this")
+ })
+ }));
assert!(recorded_calls(&f, "Page.bringToFront").is_empty());
assert!(recorded_calls(&f, "Target.activateTarget").is_empty());
}