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): list bound tabs in the order the browser reports them (#5527)

injaneity59eace173488
diff --git a/libs/cua-driver/rust/Cargo.lock b/libs/cua-driver/rust/Cargo.lock
index 41b6777b88..cda6c08a3e 100644
--- a/libs/cua-driver/rust/Cargo.lock
+++ b/libs/cua-driver/rust/Cargo.lock
@@ -1275,6 +1275,7 @@ dependencies = [
"hkdf",
"hmac",
"image",
+ "indexmap",
"jsonschema",
"keyring",
"libc",
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/Cargo.toml b/libs/cua-driver/rust/crates/cua-driver-core/Cargo.toml
index 1d25b9267a..cba09886d2 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/Cargo.toml
+++ b/libs/cua-driver/rust/crates/cua-driver-core/Cargo.toml
@@ -46,6 +46,7 @@ futures-util = "0.3"
uuid = { workspace = true }
base64 = { workspace = true }
sha2 = "0.10"
+indexmap = "2"
url = "2.5"
chacha20poly1305 = { workspace = true }
hkdf = { workspace = true }
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/engine.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/engine.rs
index 3b3e0d2ba4..73f9e92ef7 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/engine.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/engine.rs
@@ -27,6 +27,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, Weak};
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
+use indexmap::IndexMap;
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use uuid::Uuid;
@@ -1383,7 +1384,7 @@ impl BrowserEngine {
// target is only a window handle and must never be reported as active.
let selected_cdp_target_id =
selected_tab_target_id(&native.title, &candidates, candidate.cdp_window_id);
- let mut tabs = HashMap::new();
+ let mut tabs = IndexMap::new();
for c in candidates.iter().filter(|c| match candidate.cdp_window_id {
Some(window_id) => c.cdp_window_id == Some(window_id),
None => c.cdp_target_id == candidate.cdp_target_id,
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/store.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/store.rs
index 68f2099f61..c950b26a09 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/store.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/store.rs
@@ -19,6 +19,7 @@ use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
+use indexmap::IndexMap;
use serde::Serialize;
use uuid::Uuid;
@@ -228,7 +229,7 @@ pub struct TargetRecord {
/// CDP browser window id, or None for the exact single-page embedded route.
pub cdp_window_id: Option<i64>,
pub quality: BindingQuality,
- pub tabs: HashMap<String, TabRecord>,
+ pub tabs: IndexMap<String, TabRecord>,
}
#[derive(Default)]
@@ -488,7 +489,7 @@ mod tests {
cdp_target_id: "CDP1".into(),
cdp_window_id: Some(11),
quality: BindingQuality::Exact,
- tabs: HashMap::new(),
+ tabs: IndexMap::new(),
}
}
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 65be2ed3db..da318a3339 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
@@ -3797,11 +3797,12 @@ mod tests {
#[tokio::test]
async fn heuristic_bindings_refuse_mutation() {
use crate::browser::types::{BindingQuality, Rect};
+ use indexmap::IndexMap;
use std::collections::HashMap;
let e = engine();
// Seed a heuristic binding directly into the store.
- let mut tabs = HashMap::new();
+ let mut tabs = IndexMap::new();
let tab_id = e.store.mint_tab_id();
tabs.insert(
tab_id.clone(),
@@ -3858,10 +3859,11 @@ mod tests {
#[tokio::test]
async fn stale_fingerprint_refuses_before_any_cdp_traffic() {
use crate::browser::types::{BindingQuality, Rect};
+ use indexmap::IndexMap;
use std::collections::HashMap;
let e = engine();
- let mut tabs = HashMap::new();
+ let mut tabs = IndexMap::new();
let tab_id = e.store.mint_tab_id();
tabs.insert(
tab_id.clone(),
@@ -3918,7 +3920,6 @@ mod tests {
#[tokio::test]
async fn session_end_hook_drops_the_capability_namespace() {
use crate::browser::types::{BindingQuality, Rect};
- use std::collections::HashMap;
let e = engine();
let sid = "browser-store-cleanup-session-771";
@@ -3945,7 +3946,7 @@ mod tests {
cdp_target_id: "C".into(),
cdp_window_id: Some(1),
quality: BindingQuality::Exact,
- tabs: HashMap::new(),
+ tabs: indexmap::IndexMap::new(),
},
);
assert_eq!(e.store.target_count(sid), 1);
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 7d4b86e3cf..67410543f9 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
@@ -72,6 +72,9 @@ pub(super) struct FixtureState {
/// window (`Browser.getWindowForTarget` → `-32000 Browser window not
/// found`): an extension side panel, an offscreen document.
windowless_targets: Vec<Value>,
+ /// Extra page targets living in the bound window, after T1 in the
+ /// browser's `Target.getTargets` order.
+ sibling_tabs: Vec<Value>,
/// Drop the primary tab target T1 from `Target.getTargets`.
omit_primary_target: bool,
/// Make `Browser.getWindowForTarget` for T1 answer this CDP error.
@@ -96,6 +99,7 @@ impl Default for FixtureState {
emit_rogue_attach: false,
main_url: "https://fixture.test/".into(),
windowless_targets: Vec::new(),
+ sibling_tabs: Vec::new(),
omit_primary_target: false,
primary_window_error: None,
main_loader: "L_MAIN_1".into(),
@@ -468,6 +472,7 @@ fn fixture_handler(state: SharedState) -> MockHandler {
"attached": false,
}));
}
+ infos.extend(st.sibling_tabs.iter().cloned());
infos.extend(st.windowless_targets.iter().cloned());
MockReply::ok(json!({ "targetInfos": infos }))
}
@@ -3625,6 +3630,37 @@ async fn windowless_side_panel_target_is_skipped_and_bind_stays_exact() {
.any(|(_, m, p)| { m == "Target.attachToTarget" && p["targetId"] == "T_SIDEPANEL" }));
}
+#[tokio::test]
+async fn bound_tabs_keep_the_browser_order_across_binds() {
+ let urls: Vec<String> = (1..=5).map(|n| format!("https://tab{n}.test/")).collect();
+ let f = fixture_with(|st| {
+ for (n, url) in urls.iter().enumerate().skip(1) {
+ st.sibling_tabs.push(json!({
+ "targetId": format!("T_SIBLING_{n}"),
+ "type": "page",
+ "title": format!("Sibling {n}"),
+ "url": url,
+ "attached": false,
+ }));
+ }
+ })
+ .await;
+ let mut expected = vec!["https://fixture.test/".to_owned()];
+ expected.extend(urls.into_iter().skip(1));
+ for _ in 0..2 {
+ let s = bind_result(&f).await;
+ assert_eq!(s["status"], "ok", "{s}");
+ let listed: Vec<&str> = s["tabs"]
+ .as_array()
+ .expect("tabs")
+ .iter()
+ .map(|t| t["url"].as_str().unwrap())
+ .collect();
+ assert_eq!(listed, expected, "{s}");
+ assert_eq!(s["tabs"][0]["active"], true, "{s}");
+ }
+}
+
#[tokio::test]
async fn only_windowless_targets_refuse_wrong_target_never_route_unavailable() {
let f = fixture_with(|st| {
diff --git a/libs/cua-spacesd/Cargo.lock b/libs/cua-spacesd/Cargo.lock
index bb52d7838a..ce9648bd88 100644
--- a/libs/cua-spacesd/Cargo.lock
+++ b/libs/cua-spacesd/Cargo.lock
@@ -1893,6 +1893,7 @@ dependencies = [
"hkdf 0.12.4",
"hmac 0.12.1",
"image",
+ "indexmap",
"jsonschema",
"keyring 4.2.0",
"libc",
diff --git a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
index 1c2dd51aec..f7edadc859 100644
--- a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
+++ b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
@@ -2320,6 +2320,7 @@ dependencies = [
"hkdf 0.12.4",
"hmac 0.12.1",
"image",
+ "indexmap",
"jsonschema",
"keyring 4.2.0",
"libc",
diff --git a/libs/cua-spacesd/tests/teleport-e2e/Cargo.lock b/libs/cua-spacesd/tests/teleport-e2e/Cargo.lock
index cb1c71a8f6..c3214a8eb1 100644
--- a/libs/cua-spacesd/tests/teleport-e2e/Cargo.lock
+++ b/libs/cua-spacesd/tests/teleport-e2e/Cargo.lock
@@ -1427,6 +1427,7 @@ dependencies = [
"hkdf 0.12.4",
"hmac 0.12.1",
"image",
+ "indexmap",
"jsonschema",
"keyring",
"libc",