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): render browser cursor on Linux (#3778)

Francesco Bonaccib75b0b08c947
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/browser_platform.rs b/libs/cua-driver/rust/crates/platform-linux/src/browser_platform.rs
index a0588380a7..f8ec238d8e 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/browser_platform.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/browser_platform.rs
@@ -3,13 +3,14 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
+use std::sync::{Arc, Mutex};
use std::time::Duration;
use async_trait::async_trait;
use cua_driver_core::browser::platform::{
select_isolated_browser_executable, BrowserConsentOutcome, BrowserConsentRequest,
- BrowserPlatform, ExistingProfileSetupOutcome, ExistingProfileSetupRequest, PrepareAction,
- PrepareOutcome, PrepareRequest,
+ BrowserPlatform, BrowserVisualAction, BrowserVisualActionKind, ExistingProfileSetupOutcome,
+ ExistingProfileSetupRequest, PrepareAction, PrepareOutcome, PrepareRequest,
};
use cua_driver_core::browser::refusal::{BrowserRefusal, BrowserRefusalCode};
use cua_driver_core::browser::types::{
@@ -19,12 +20,29 @@ use cua_driver_core::browser::types::{
};
use cua_driver_core::browser::{
existing_profile_setup_descriptor, is_firefox, loopback_websocket_port,
- parse_devtools_active_port,
+ parse_devtools_active_port, BrowserCursorTracker,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
-#[derive(Debug, Default)]
-pub struct LinuxBrowserPlatform;
+pub struct LinuxBrowserPlatform {
+ cursor_registry: Arc<cursor_overlay::CursorRegistry>,
+ browser_cursors: Mutex<BrowserCursorTracker>,
+}
+
+impl LinuxBrowserPlatform {
+ pub fn new(cursor_registry: Arc<cursor_overlay::CursorRegistry>) -> Self {
+ Self {
+ cursor_registry,
+ browser_cursors: Mutex::new(BrowserCursorTracker::default()),
+ }
+ }
+}
+
+impl Default for LinuxBrowserPlatform {
+ fn default() -> Self {
+ Self::new(Arc::new(cursor_overlay::CursorRegistry::new()))
+ }
+}
fn refusal(code: BrowserRefusalCode, message: impl Into<String>) -> BrowserRefusal {
BrowserRefusal::new(code, message)
@@ -466,6 +484,76 @@ impl BrowserPlatform for LinuxBrowserPlatform {
Some("Chromium's trusted CDP Input route activates its standalone browser window on Linux")
}
+ async fn visualize_browser_action(&self, action: BrowserVisualAction) {
+ if action.session.is_empty()
+ || action.cdp_target_id.is_empty()
+ || cua_driver_core::session::is_session_ended(&action.session)
+ {
+ return;
+ }
+
+ let visibility_updates = self.browser_cursors.lock().unwrap().update(
+ &action.session,
+ action.window_id,
+ &action.cdp_target_id,
+ action.tab_is_active,
+ );
+ let cursor_enabled = self
+ .cursor_registry
+ .get_or_create(&action.session)
+ .config
+ .enabled;
+ for (key, visible) in visibility_updates {
+ let enabled = if key == action.session {
+ visible && cursor_enabled
+ } else {
+ visible
+ && self
+ .cursor_registry
+ .get(&key)
+ .is_some_and(|state| state.config.enabled)
+ };
+ crate::overlay::send_command_for(
+ key,
+ cursor_overlay::OverlayCommand::SetEnabled(enabled),
+ );
+ }
+ if !action.tab_is_active || !cursor_enabled {
+ return;
+ }
+ let (Some(screen_x), Some(screen_y)) = (action.screen_x, action.screen_y) else {
+ return;
+ };
+ if !screen_x.is_finite() || !screen_y.is_finite() {
+ return;
+ }
+
+ crate::overlay::send_command_for(
+ action.session.clone(),
+ cursor_overlay::OverlayCommand::PinAbove(action.window_id),
+ );
+ crate::overlay::animate_cursor_to_for(action.session.clone(), screen_x, screen_y).await;
+ self.cursor_registry
+ .update_position(&action.session, screen_x, screen_y);
+
+ if matches!(
+ action.kind,
+ BrowserVisualActionKind::Click
+ | BrowserVisualActionKind::Type
+ | BrowserVisualActionKind::RightClick
+ | BrowserVisualActionKind::DoubleClick
+ | BrowserVisualActionKind::Drag
+ ) {
+ crate::overlay::send_command_for(
+ action.session,
+ cursor_overlay::OverlayCommand::ClickPulse {
+ x: screen_x,
+ y: screen_y,
+ },
+ );
+ }
+ }
+
async fn classify_browser(&self, pid: i64) -> Result<BrowserClassification, BrowserRefusal> {
let pid_u32 = u32::try_from(pid).map_err(|_| {
refusal(
@@ -1284,6 +1372,53 @@ mod tests {
assert!(cleaned);
}
+ #[tokio::test]
+ async fn browser_visual_feedback_updates_the_declared_session_cursor() {
+ let registry = Arc::new(cursor_overlay::CursorRegistry::new());
+ let platform = LinuxBrowserPlatform::new(registry.clone());
+ platform
+ .visualize_browser_action(BrowserVisualAction {
+ session: "browser-cursor-test".to_owned(),
+ window_id: 77,
+ cdp_target_id: "tab-A".to_owned(),
+ tab_is_active: true,
+ screen_x: Some(321.0),
+ screen_y: Some(456.0),
+ kind: BrowserVisualActionKind::Click,
+ })
+ .await;
+
+ let state = registry
+ .get("browser-cursor-test")
+ .expect("browser action should materialize its session cursor");
+ assert_eq!((state.x, state.y), (Some(321.0), Some(456.0)));
+ }
+
+ #[tokio::test]
+ async fn inactive_tab_feedback_materializes_but_does_not_move_its_cursor() {
+ let registry = Arc::new(cursor_overlay::CursorRegistry::new());
+ let platform = LinuxBrowserPlatform::new(registry.clone());
+ platform
+ .visualize_browser_action(BrowserVisualAction {
+ session: "browser-cursor-hidden".to_owned(),
+ window_id: 77,
+ cdp_target_id: "tab-hidden".to_owned(),
+ tab_is_active: false,
+ screen_x: Some(321.0),
+ screen_y: Some(456.0),
+ kind: BrowserVisualActionKind::Click,
+ })
+ .await;
+
+ let state = registry
+ .get("browser-cursor-hidden")
+ .expect("browser action should establish its session-to-tab binding");
+ assert!(
+ state.x.is_none() && state.y.is_none(),
+ "an inactive tab must not animate or move its visible cursor"
+ );
+ }
+
#[test]
fn isolated_browser_candidates_use_only_root_managed_payloads() {
let candidates = isolated_browser_candidates();
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
index 6fd6622c7b..268179d499 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
@@ -13266,7 +13266,7 @@ impl Tool for KillAppTool {
.and_then(Value::as_i64)
.filter(|pid| *pid > 0)
.ok_or_else(|| "kill_app requires a positive integer pid".to_owned())?;
- let fingerprint = crate::browser_platform::LinuxBrowserPlatform
+ let fingerprint = crate::browser_platform::LinuxBrowserPlatform::default()
.process_fingerprint(pid)
.await
.map_err(|error| error.message)?;
@@ -14141,7 +14141,9 @@ pub fn build_registry_with_provider(
super::page::LinuxPageBackend::new(),
))));
let browser_engine = cua_driver_core::browser::BrowserEngine::new_with_runtime_services(
- Arc::new(crate::browser_platform::LinuxBrowserPlatform),
+ Arc::new(crate::browser_platform::LinuxBrowserPlatform::new(
+ state.cursor_registry.clone(),
+ )),
r.approval_broker(),
r.protected_resource_ownership(),
);