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): treat Windows elements read while minimized as stale on resolver-routed clicks (#6049)

injaneity89b34d85fe80
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 9964932134..7e8d483e5d 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
@@ -148,10 +148,13 @@ fn resolve_onscreen_point_with_scroll(
// authoritative for that case; without it a foreground tap can land on the
// visible control covering the stale point (for example a bottom toolbar).
if let Some(retained) = admitted.as_ref() {
+ // Geometry read while minimized is stale even when it happens to land
+ // inside the restored window rect.
+ let trust_cached = cached_center_in_window && !retained.read_while_minimized;
if retained.is_uia() {
let is_offscreen =
unsafe { crate::uia::scroll::element_is_offscreen(retained.as_ptr()) };
- if cached_center_in_window && is_offscreen != Some(true) {
+ if trust_cached && is_offscreen != Some(true) {
return Ok((cx, cy));
}
if let Some((nx, ny)) = unsafe {
@@ -161,7 +164,7 @@ fn resolve_onscreen_point_with_scroll(
return Ok((nx, ny));
}
}
- } else if cached_center_in_window {
+ } else if trust_cached {
return Ok((cx, cy));
}
// `retained` drops here → COM Release.
@@ -1440,6 +1443,7 @@ impl Tool for GetWindowStateTool {
let tree_task = tokio::task::spawn_blocking(move || {
let mut budget =
cua_driver_core::walk_budget::WalkBudget::new(timeout_ms, max_elements);
+ let iconic_before = crate::input::window_is_iconic(hwnd);
let tree =
crate::uia::walk_tree_budgeted(hwnd, q.as_deref(), max_depth, &mut budget);
let kind = if tree.nodes.iter().any(|node| node.msaa_role.is_some()) {
@@ -1448,7 +1452,9 @@ impl Tool for GetWindowStateTool {
crate::uia::snapshot::SnapshotKind::Uia
};
let payload = crate::uia::snapshot::UiaSnapshot::from_nodes(&tree.nodes, kind)
- .with_read_while_minimized(crate::input::window_is_iconic(hwnd));
+ .with_read_while_minimized(
+ iconic_before || crate::input::window_is_iconic(hwnd),
+ );
(tree, payload, budget.outcome())
});
let started = std::time::Instant::now();
@@ -3695,6 +3701,16 @@ impl Tool for ClickTool {
}
}
};
+ if admitted
+ .as_ref()
+ .is_some_and(|element| element.read_while_minimized)
+ {
+ if let Err(refusal) =
+ resolve_onscreen_point_with_scroll(&admitted, hwnd, idx, tx, ty, "clicking")
+ {
+ return refusal;
+ }
+ }
pin_overlay_above(&cursor_key, hwnd);
overlay_glide_to_target(
&cursor_key,
@@ -4117,6 +4133,12 @@ impl Tool for ClickTool {
.map_err(|result| anyhow::anyhow!(tool_result_text(result)))?;
return Ok(BackgroundElementClick::Inject { x: cx, y: cy, failed_calls });
}
+ if admitted.as_ref().is_some_and(|element| element.read_while_minimized) {
+ let (x, y) =
+ resolve_onscreen_point_with_scroll(&admitted, hwnd, idx, cx, cy, "clicking")
+ .map_err(|result| anyhow::anyhow!(tool_result_text(result)))?;
+ return Ok(BackgroundElementClick::Post { x, y, failed_calls });
+ }
Ok(BackgroundElementClick::Post { x: cx, y: cy, failed_calls })
} }).await;
let plan = match result {
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/snapshot_coordinate_tests.rs b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/snapshot_coordinate_tests.rs
index e5b6cf4b81..820553509a 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/snapshot_coordinate_tests.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_/snapshot_coordinate_tests.rs
@@ -39,3 +39,39 @@ fn press_key_native_element_focus_skips_second_screenshot_scaling() {
);
assert!(screenshot.get("_native_coordinates").is_none());
}
+
+#[test]
+fn resolver_refuses_cached_center_read_while_minimized() {
+ use crate::uia::snapshot::{RetainedElement, SnapshotKind};
+ use windows::Win32::UI::WindowsAndMessaging::{
+ GetSystemMetrics, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN,
+ SM_YVIRTUALSCREEN,
+ };
+ let (x, y, w, h) = unsafe {
+ (
+ GetSystemMetrics(SM_XVIRTUALSCREEN),
+ GetSystemMetrics(SM_YVIRTUALSCREEN),
+ GetSystemMetrics(SM_CXVIRTUALSCREEN),
+ GetSystemMetrics(SM_CYVIRTUALSCREEN),
+ )
+ };
+ assert!(w > 0 && h > 0, "runner has no virtual-screen metrics");
+ assert!(crate::input::point_on_virtual_desktop(x, y));
+
+ let none = super::resolve_onscreen_point_with_scroll(&None, 0, 1, x, y, "clicking");
+ assert_eq!(none.ok(), Some((x, y)));
+
+ for kind in [SnapshotKind::Uia, SnapshotKind::Msaa] {
+ let fresh = Some(RetainedElement::detached(kind, (x, y), false));
+ let ok = super::resolve_onscreen_point_with_scroll(&fresh, 0, 1, x, y, "clicking");
+ assert_eq!(ok.ok(), Some((x, y)));
+
+ let stale = Some(RetainedElement::detached(kind, (x, y), true));
+ let err = super::resolve_onscreen_point_with_scroll(&stale, 0, 1, x, y, "clicking")
+ .expect_err("flagged element must be refused");
+ let sc = err.structured_content.expect("structured refusal");
+ assert_eq!(sc["status"], "refused");
+ assert_eq!(sc["code"], "element_not_visible");
+ assert_eq!(sc["refusal"]["code"], "element_not_visible");
+ }
+}
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot.rs b/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot.rs
index d58c62ab5c..03dad69550 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot.rs
@@ -15,6 +15,24 @@ pub struct RetainedElement {
pub read_while_minimized: bool,
}
+#[cfg(test)]
+impl RetainedElement {
+ pub(crate) fn detached(
+ kind: SnapshotKind,
+ center: (i32, i32),
+ read_while_minimized: bool,
+ ) -> Self {
+ Self {
+ ptr: 0,
+ kind,
+ center,
+ rect: None,
+ msaa_role: None,
+ read_while_minimized,
+ }
+ }
+}
+
impl RetainedElement {
pub fn as_ptr(&self) -> usize {
self.ptr
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot_uaf_repro.rs b/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot_uaf_repro.rs
index 68c4c542b7..bd68b05591 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot_uaf_repro.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/uia/snapshot_uaf_repro.rs
@@ -255,6 +255,7 @@ fn exact_snapshot_retains_matching_identity_and_geometry() {
payload.elements[0].center = (31, 47);
payload.elements[0].rect = Some((11, 27, 51, 67));
payload.elements[0].msaa_role = Some(0x38);
+ let payload = payload.with_read_while_minimized(true);
let first = cache.publish(PID as i32, HWND, payload);
let guard = acquire(&cache, first, 0).unwrap();
let second = cache.publish(PID as i32, HWND, snapshot_with(vec![make_fake(hits)]));
@@ -266,8 +267,12 @@ fn exact_snapshot_retains_matching_identity_and_geometry() {
assert_eq!(guard.msaa_role, Some(0x38));
assert!(guard.focus_element().is_err());
assert_eq!(guard.element_has_keyboard_focus(), None);
- assert_eq!(acquire(&cache, second, 0).unwrap().center, (0, 0));
+ assert!(guard.read_while_minimized);
+ let replacement = acquire(&cache, second, 0).unwrap();
+ assert_eq!(replacement.center, (0, 0));
+ assert!(!replacement.read_while_minimized);
let cloned = guard.clone();
+ assert!(cloned.read_while_minimized);
drop(guard);
unsafe { touch_vtable(cloned.as_ptr()) };
drop(cloned);