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): ignore daemon windows in change detection (#1784)

Mike Evdokimovdee3d5225cf8
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs b/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
index 3826a61e03..ad5164c130 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
@@ -150,6 +150,13 @@ impl Changes {
}
}
+/// Returns true when a window belongs to this cua-driver process, including
+/// transient UI such as the agent cursor overlay. Those windows are internal
+/// implementation details rather than action-triggered application windows.
+fn is_daemon_window(window: &WindowInfo) -> bool {
+ window.pid == std::process::id() as i32
+}
+
/// Default poll deadline — new windows triggered by a click typically
/// appear within ~200ms on macOS; 1.0s gives the wildcard suppressor
/// time to fire and settle.
@@ -324,17 +331,10 @@ impl Snapshot {
.into_iter()
.filter(|w| w.layer == 0)
.collect();
+ // Keep the live detector and the pure regression tests on the same
+ // diff path so daemon-window filtering cannot drift between them.
+ let (new_windows, _closed) = Self::diff(&self.window_ids, &current);
- let new_windows: Vec<WindowEvent> = current
- .iter()
- .filter(|w| !self.window_ids.contains(&w.window_id))
- .map(|w| WindowEvent {
- window_id: w.window_id,
- pid: w.pid,
- app_name: w.app_name.clone(),
- title: w.title.clone(),
- })
- .collect();
let current_front = apps::frontmost_pid();
let foreground_changed = match (self.front_pid, current_front) {
(Some(orig), Some(cur)) => orig != cur,
@@ -353,6 +353,38 @@ impl Snapshot {
}
}
}
+
+ // ── Internal helpers — also used by unit tests so the diff logic can be
+ // exercised without driving the live window enumerator. ──────────────
+
+ /// Pure-function diff: given the snapshot's window-id set + a
+ /// list of currently-visible windows, return the (opened, closed)
+ /// classification. Opened windows owned by this daemon are excluded so
+ /// transient UI such as the cursor overlay is not reported as an action
+ /// side effect.
+ pub(crate) fn diff(
+ snapshot_ids: &HashSet<u32>,
+ current: &[WindowInfo],
+ ) -> (Vec<WindowEvent>, Vec<u32>) {
+ let current_ids: HashSet<u32> = current.iter().map(|w| w.window_id).collect();
+ let opened: Vec<WindowEvent> = current
+ .iter()
+ .filter(|w| !snapshot_ids.contains(&w.window_id))
+ .filter(|w| !is_daemon_window(w))
+ .map(|w| WindowEvent {
+ window_id: w.window_id,
+ pid: w.pid,
+ app_name: w.app_name.clone(),
+ title: w.title.clone(),
+ })
+ .collect();
+ let closed: Vec<u32> = snapshot_ids
+ .iter()
+ .copied()
+ .filter(|id| !current_ids.contains(id))
+ .collect();
+ (opened, closed)
+ }
}
// ── Tests ────────────────────────────────────────────────────────────────────
@@ -360,6 +392,49 @@ impl Snapshot {
#[cfg(test)]
mod tests {
use super::*;
+ use crate::windows::WindowBounds;
+
+ fn win(window_id: u32, pid: i32, app_name: &str, title: &str) -> WindowInfo {
+ WindowInfo {
+ window_id,
+ pid,
+ app_name: app_name.to_owned(),
+ title: title.to_owned(),
+ bounds: WindowBounds {
+ x: 0.0,
+ y: 0.0,
+ width: 100.0,
+ height: 100.0,
+ },
+ layer: 0,
+ z_index: 0,
+ is_on_screen: true,
+ current_space_id: None,
+ on_current_space: None,
+ space_ids: None,
+ }
+ }
+
+ /// Regression for trycua/cua#1592 Bug 2. This exercises the same `diff`
+ /// path used by `detect_with`, rather than separately testing a predicate
+ /// that production could accidentally stop applying.
+ #[test]
+ fn diff_excludes_new_windows_owned_by_the_daemon() {
+ let snap: HashSet<u32> = [1].into_iter().collect();
+ let daemon_pid = std::process::id() as i32;
+ let cur = vec![
+ win(1, daemon_pid + 1, "Safari", "Home"),
+ win(2, daemon_pid, "Cua Driver", ""),
+ win(3, daemon_pid + 2, "Mail", "Inbox"),
+ ];
+
+ let (opened, closed) = Snapshot::diff(&snap, &cur);
+
+ assert_eq!(opened.len(), 1);
+ assert_eq!(opened[0].window_id, 3);
+ assert_eq!(opened[0].app_name, "Mail");
+ assert!(closed.is_empty());
+ }
#[test]
fn changes_result_suffix_no_change_is_empty() {