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): bound browser_dialog Page.enable retry by the dialog subscription deadline (#6643)

injaneityc69668087c07
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 d9aea2e2a0..776d17f7a7 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
@@ -76,8 +76,10 @@ pub const MAX_REFS_PER_SNAPSHOT: usize = 300;
/// compromised or malformed endpoint before its response reaches consumers.
const MAX_BROWSER_SCREENSHOT_BYTES: usize = 16 * 1024 * 1024;
-/// Reply deadline for the best-effort `Page.enable` sent on a tab's first attach.
-const DIALOG_SUBSCRIPTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
+/// Reply deadline for the `Page.enable` dialog subscription, sent on a tab's
+/// first attach and retried by `browser_dialog`.
+pub(crate) const DIALOG_SUBSCRIPTION_TIMEOUT: std::time::Duration =
+ std::time::Duration::from_secs(2);
pub struct BrowserEngine {
pub(crate) platform: Arc<dyn BrowserPlatform>,
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 d8de6c17b8..daaba01b54 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
@@ -25,7 +25,7 @@ use crate::tool_args::ArgsExt;
use super::cdp_ws::CdpConnection;
use super::download::BrowserDownloadTool;
-use super::engine::{BrowserEngine, BrowserTabScreenshot};
+use super::engine::{BrowserEngine, BrowserTabScreenshot, DIALOG_SUBSCRIPTION_TIMEOUT};
use super::platform::{
BrowserVisualActionKind, PrepareAction, PrepareProfile, PrepareProfileMode, PrepareRequest,
PrepareStrategy,
@@ -2847,9 +2847,19 @@ impl Tool for BrowserDialogTool {
let cdp_target_id = validated.tab.cdp_target_id.as_str();
if conn.dialog_state(cdp_target_id).is_none() && !conn.has_dialog_session(cdp_target_id) {
// Register before Page.enable so an opening event delivered before
- // the command reply is still attributed to the exact target.
+ // the command reply is still attributed to the exact target. A
+ // renderer blocked by an unseen dialog never answers, so use the
+ // short subscription deadline rather than the full call timeout.
conn.register_dialog_session(cdp_session, cdp_target_id);
- if let Err(error) = conn.call(Some(cdp_session), "Page.enable", json!({})).await {
+ if let Err(error) = conn
+ .call_with_timeout(
+ Some(cdp_session),
+ "Page.enable",
+ json!({}),
+ DIALOG_SUBSCRIPTION_TIMEOUT,
+ )
+ .await
+ {
if conn.dialog_state(cdp_target_id).is_none() {
conn.unregister_dialog_session(cdp_session, cdp_target_id);
return BrowserRefusal::new(
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 116c89407f..6162018555 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
@@ -96,6 +96,9 @@ pub(super) struct FixtureState {
/// A page-owned JavaScript dialog is open in T1: `Page.enable` on a tab
/// session announces it, and the renderer answers nothing else.
dialog_open: bool,
+ /// `Page.enable` on a tab session never answers (a renderer blocked by a
+ /// dialog it has not yet announced).
+ page_enable_hangs: bool,
dom_click_changes_readback: bool,
dom_click_readback_available: bool,
dom_click_stall_after: Option<usize>,
@@ -122,6 +125,7 @@ impl Default for FixtureState {
omit_primary_target: false,
primary_window_error: None,
dialog_open: false,
+ page_enable_hangs: false,
main_loader: "L_MAIN_1".into(),
iframe_loader: "L_IFRAME_1".into(),
oopif_loader: "L_OOPIF_1".into(),
@@ -531,6 +535,7 @@ fn fixture_handler(state: SharedState) -> MockHandler {
st.tab_sessions += 1;
MockReply::ok(json!({ "sessionId": format!("tab-sess-{}", st.tab_sessions) }))
}
+ "Page.enable" if is_tab && st.page_enable_hangs => MockReply::hang(),
"Page.enable" if is_tab => {
st.page_sessions.push(sess.clone());
let mut reply = MockReply::ok(json!({}));
@@ -2881,6 +2886,34 @@ async fn an_open_dialog_moves_the_origin_proof_off_the_blocked_renderer() {
);
}
+#[tokio::test]
+async fn browser_dialog_resubscribe_uses_the_short_dialog_deadline() {
+ let f = fixture_with(|st| st.page_enable_hangs = true).await;
+ let (target, tab) = bind(&f).await;
+
+ let started = std::time::Instant::now();
+ let inspected = BrowserDialogTool::new(f.engine.clone())
+ .invoke(json!({
+ "target_id": target, "tab_id": tab, "action": "inspect", "session": SESSION
+ }))
+ .await;
+ // Revalidation's attach spends one short deadline on its best-effort
+ // subscribe and the tool's retry spends one more; the 20 s CDP call
+ // timeout must never be reached.
+ let elapsed = started.elapsed();
+ assert!(
+ elapsed
+ < 2 * super::engine::DIALOG_SUBSCRIPTION_TIMEOUT + std::time::Duration::from_secs(2),
+ "browser_dialog must not wait out the CDP call timeout: {elapsed:?}"
+ );
+ assert!(
+ recorded_calls(&f, "Page.enable").len() >= 2,
+ "browser_dialog retries the subscription the attach could not complete"
+ );
+ let s = structured(&inspected);
+ assert_eq!(s["refusal"]["code"], "browser_action_unavailable", "{s}");
+}
+
#[tokio::test]
async fn a_stale_binding_keeps_its_refusal_code_through_scope_attestation() {
use crate::authorization::PermissionMode;