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): report unknown browser tab selection

trycua-release[bot]6611622d6c6e
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/binding.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/binding.rs
index a445ab9c62..252818b903 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/browser/binding.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/browser/binding.rs
@@ -86,6 +86,35 @@ fn title_matches(native_title: &str, candidate_title: &str) -> bool {
!candidate_title.is_empty() && native_title.contains(candidate_title)
}
+/// Prove the selected page in one already-correlated browser window without
+/// activating a target. Chromium exposes the native window title but no
+/// browser-level selected-tab bit. A unique title match is therefore proof;
+/// duplicate, empty, absent, or substring-colliding titles remain unknown.
+/// Embedded single-page endpoints are selected by cardinality.
+pub fn selected_tab_target_id<'a>(
+ native_title: &str,
+ candidates: &'a [CdpWindowCandidate],
+ cdp_window_id: Option<i64>,
+) -> Option<&'a str> {
+ let tabs = candidates
+ .iter()
+ .filter(|candidate| match cdp_window_id {
+ Some(window_id) => candidate.cdp_window_id == Some(window_id),
+ None => candidate.cdp_window_id.is_none(),
+ })
+ .collect::<Vec<_>>();
+
+ if cdp_window_id.is_none() {
+ return (tabs.len() == 1).then(|| tabs[0].cdp_target_id.as_str());
+ }
+
+ let title_hits = tabs
+ .into_iter()
+ .filter(|candidate| title_matches(native_title, &candidate.title))
+ .collect::<Vec<_>>();
+ (title_hits.len() == 1).then(|| title_hits[0].cdp_target_id.as_str())
+}
+
/// Reduce page targets to one representative per proven CDP window.
/// The representative prefers a tab title displayed by the native
/// window, but its target id is only a handle for the selected window;
@@ -328,6 +357,45 @@ mod tests {
));
}
+ #[test]
+ fn selected_tab_requires_one_unique_native_title_match() {
+ let tabs = [cand("first", "Checkout", B), cand("second", "Cart", B)];
+ assert_eq!(
+ selected_tab_target_id("Cart - Chrome", &tabs, Some(1)),
+ Some("second")
+ );
+
+ let duplicate_titles = [cand("first", "Checkout", B), cand("second", "Checkout", B)];
+ assert_eq!(
+ selected_tab_target_id("Checkout - Chrome", &duplicate_titles, Some(1)),
+ None
+ );
+
+ let substring_collision = [cand("first", "Mail", B), cand("second", "Mail Inbox", B)];
+ assert_eq!(
+ selected_tab_target_id("Mail Inbox - Chrome", &substring_collision, Some(1)),
+ None
+ );
+
+ let empty_titles = [cand("first", "", B), cand("second", "", B)];
+ assert_eq!(
+ selected_tab_target_id("Chrome", &empty_titles, Some(1)),
+ None
+ );
+ }
+
+ #[test]
+ fn embedded_single_page_is_selected_by_cardinality_only() {
+ let one = [embedded_cand("only", "Fixture")];
+ assert_eq!(selected_tab_target_id("Fixture", &one, None), Some("only"));
+
+ let two = [
+ embedded_cand("first", "Fixture"),
+ embedded_cand("second", "Fixture"),
+ ];
+ assert_eq!(selected_tab_target_id("Fixture", &two, None), None);
+ }
+
#[test]
fn unique_title_disambiguates_distinct_maximized_windows_with_shared_bounds() {
let n = native("Docs - Chrome", B);
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 bad656150a..4cb7ac2ce5 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
@@ -31,7 +31,10 @@ use uuid::Uuid;
use crate::session::register_session_end_hook;
-use super::binding::{cardinality_exact_candidate, correlate, BindingOutcome, CdpWindowCandidate};
+use super::binding::{
+ cardinality_exact_candidate, correlate, selected_tab_target_id, BindingOutcome,
+ CdpWindowCandidate,
+};
use super::cdp_ws::{CdpConnection, CdpPool};
use super::grant::{ExistingProfileGrant, ExistingProfileGrants, GrantLookup};
use super::mutation::{MutationGates, MutationKey};
@@ -952,7 +955,11 @@ impl BrowserEngine {
}
};
- // Tabs = page targets living in the bound CDP window.
+ // Tabs = page targets living in the bound CDP window. Selection is a
+ // separate proof from native-window correlation: a representative CDP
+ // 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();
for c in candidates.iter().filter(|c| match candidate.cdp_window_id {
Some(window_id) => c.cdp_window_id == Some(window_id),
@@ -966,6 +973,7 @@ impl BrowserEngine {
cdp_target_id: c.cdp_target_id.clone(),
title: c.title.clone(),
url: c.url.clone(),
+ active: selected_cdp_target_id.map(|selected| selected == c.cdp_target_id),
generation: grant.as_ref().map_or(0, |grant| grant.generation),
snapshots: HashMap::new(),
},
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 0b47f26ddf..cb4939e4b6 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
@@ -187,6 +187,10 @@ pub struct TabRecord {
pub cdp_target_id: String,
pub title: String,
pub url: String,
+ /// Native-window selection proof captured at bind time. `None` means the
+ /// selected tab could not be proven without activating or foregrounding a
+ /// page, so the public `active` field must be JSON null.
+ pub active: Option<bool>,
pub generation: u64,
pub snapshots: HashMap<u64, SnapshotRecord>,
}
@@ -509,6 +513,7 @@ mod tests {
cdp_target_id: "CDP1".into(),
title: "Example".into(),
url: "https://example.test".into(),
+ active: Some(true),
generation: 0,
snapshots: HashMap::from([(
snap_id,
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 64b1eb7741..c0d2072ea0 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
@@ -342,7 +342,7 @@ impl Tool for GetBrowserStateTool {
"tab_id": t.tab_id,
"title": t.title,
"url": t.url,
- "active": t.cdp_target_id == record.cdp_target_id,
+ "active": t.active,
})
})
.collect();
@@ -1781,6 +1781,7 @@ mod tests {
cdp_target_id: "CDPX".into(),
title: "Mock".into(),
url: "https://example.test".into(),
+ active: Some(true),
generation: 0,
snapshots: HashMap::new(),
},
@@ -1837,6 +1838,7 @@ mod tests {
cdp_target_id: "CDPX".into(),
title: "Mock".into(),
url: "https://example.test".into(),
+ active: Some(true),
generation: 0,
snapshots: HashMap::new(),
},