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): Windows list_windows omits the driver's own overlay windows

injaneity9370532e8841
diff --git a/docs/content/docs/cua-driver/reference/mcp-tools/apps-windows-and-screen.mdx b/docs/content/docs/cua-driver/reference/mcp-tools/apps-windows-and-screen.mdx
index 8bc33dfc1d..4204f5615f 100644
--- a/docs/content/docs/cua-driver/reference/mcp-tools/apps-windows-and-screen.mdx
+++ b/docs/content/docs/cua-driver/reference/mcp-tools/apps-windows-and-screen.mdx
@@ -146,7 +146,7 @@ Use this, not list_apps, for any window-level reasoning: "does this app have a v
Per-record fields: window_id (HWND), pid + app_name, title, bounds {x, y, width, height}, layer (always 0), z_index (integer or null; higher values are closer to the front; null means stacking order is unavailable and callers must not infer one), is_on_screen, minimized. To select a frontmost candidate, take the maximum integer z_index; if every value is null, use an explicit fallback instead of relying on array order. The macOS-specific on_current_space / space_ids fields are omitted on Windows; current_space_id is null.
-Inputs: pid (optional pid filter), app (app-name filter), on_screen_only (bool, default false). Filter with app or pid: an unfiltered call lists every window.
+Inputs: pid (optional pid filter), app (app-name filter), on_screen_only (bool, default false). Filter with app or pid: an unfiltered call lists every window. The driver's own overlay is omitted.
**Windows parameters**
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
index 07d22a84b4..e29e1f6bed 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
@@ -947,7 +947,8 @@ impl Tool for ListWindowsTool {
on_current_space / space_ids fields are \
omitted on Windows; current_space_id is null.\n\n\
Inputs: pid (optional pid filter), app (app-name filter), on_screen_only (bool, \
- default false). Filter with app or pid: an unfiltered call lists every window.".into(),
+ default false). Filter with app or pid: an unfiltered call lists every window. \
+ The driver's own overlay is omitted.".into(),
input_schema: json!({"type":"object","properties":{
"pid":{"type":"integer","description":"Optional pid filter. When set, only this pid's windows are returned."},
"app":{"type":"string","description":cua_driver_core::tool_args::LIST_WINDOWS_APP_FILTER_DESCRIPTION},
@@ -973,6 +974,7 @@ impl Tool for ListWindowsTool {
if on_screen_only {
windows.retain(|w| w.is_on_screen);
}
+ drop_own_windows(&mut windows, std::process::id());
if let Some(filter) = args.opt_str("app") {
windows.retain(|w| {
let app_name = pid_to_name.get(&w.pid).map(String::as_str).unwrap_or("");
@@ -1096,6 +1098,10 @@ impl Tool for ListWindowsTool {
use cua_driver_core::window_target::z_index_from_front_to_back;
+fn drop_own_windows(windows: &mut Vec<crate::win32::WindowInfo>, own_pid: u32) {
+ windows.retain(|w| w.pid != own_pid);
+}
+
#[cfg(test)]
mod list_windows_z_index_tests;
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/list_windows_z_index_tests.rs b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/list_windows_z_index_tests.rs
index 7c6737a7e9..6a0bd94876 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/list_windows_z_index_tests.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/list_windows_z_index_tests.rs
@@ -1,4 +1,5 @@
-use super::exact_window_ownership_result;
+use super::{drop_own_windows, exact_window_ownership_result, z_index_from_front_to_back};
+use crate::win32::WindowInfo;
#[test]
fn explicit_pid_hwnd_guard_refuses_wrong_or_stale_owners() {
@@ -18,3 +19,28 @@ fn explicit_pid_hwnd_guard_refuses_wrong_or_stale_owners() {
"window_target_not_found"
);
}
+
+fn window(hwnd: u64, pid: u32) -> WindowInfo {
+ WindowInfo {
+ hwnd,
+ pid,
+ title: format!("window {hwnd}"),
+ x: 0,
+ y: 0,
+ width: 1280,
+ height: 800,
+ is_on_screen: true,
+ minimized: false,
+ }
+}
+
+#[test]
+fn the_drivers_own_windows_are_dropped_and_the_rest_keep_contiguous_z() {
+ let mut windows = vec![window(1, 7), window(2, 42), window(3, 7), window(4, 9)];
+ drop_own_windows(&mut windows, 7);
+ assert_eq!(windows.iter().map(|w| w.hwnd).collect::<Vec<_>>(), [2, 4]);
+ let z: Vec<_> = (0..windows.len())
+ .map(|i| z_index_from_front_to_back(windows.len(), i))
+ .collect();
+ assert_eq!(z, [1, 0]);
+}
diff --git a/scripts/docs-generators/cli-specs/cua-driver-mcp-windows.json b/scripts/docs-generators/cli-specs/cua-driver-mcp-windows.json
index 744913c198..354a65854d 100644
--- a/scripts/docs-generators/cli-specs/cua-driver-mcp-windows.json
+++ b/scripts/docs-generators/cli-specs/cua-driver-mcp-windows.json
@@ -18,7 +18,7 @@
"read_only": true
},
{
- "description": "List every top-level window currently known to the window manager. Each record self-contains its owning app identity so the caller never has to join back against list_apps.\n\nUse this — not list_apps — for any window-level reasoning: \"does this app have a visible window right now?\", \"which of this pid's windows is the main one?\".\n\nPer-record fields: window_id (HWND), pid + app_name, title, bounds {x, y, width, height}, layer (always 0), z_index (integer or null; higher values are closer to the front; null means stacking order is unavailable and callers must not infer one), is_on_screen, minimized. To select a frontmost candidate, take the maximum integer z_index; if every value is null, use an explicit fallback instead of relying on array order. The macOS-specific on_current_space / space_ids fields are omitted on Windows; current_space_id is null.\n\nInputs: pid (optional pid filter), app (app-name filter), on_screen_only (bool, default false). Filter with app or pid: an unfiltered call lists every window.",
+ "description": "List every top-level window currently known to the window manager. Each record self-contains its owning app identity so the caller never has to join back against list_apps.\n\nUse this — not list_apps — for any window-level reasoning: \"does this app have a visible window right now?\", \"which of this pid's windows is the main one?\".\n\nPer-record fields: window_id (HWND), pid + app_name, title, bounds {x, y, width, height}, layer (always 0), z_index (integer or null; higher values are closer to the front; null means stacking order is unavailable and callers must not infer one), is_on_screen, minimized. To select a frontmost candidate, take the maximum integer z_index; if every value is null, use an explicit fallback instead of relying on array order. The macOS-specific on_current_space / space_ids fields are omitted on Windows; current_space_id is null.\n\nInputs: pid (optional pid filter), app (app-name filter), on_screen_only (bool, default false). Filter with app or pid: an unfiltered call lists every window. The driver's own overlay is omitted.",
"destructive": false,
"idempotent": true,
"input_schema": {