Skip to content

The agent that grows with you

fix(computer-use): skip driver self-windows in implicit capture

fangliquanflqe10fbf9bea1e
diff --git a/tests/tools/test_computer_use_capture_self_window.py b/tests/tools/test_computer_use_capture_self_window.py
new file mode 100644
index 00000000000..c7cb0d7e68a
--- /dev/null
+++ b/tests/tools/test_computer_use_capture_self_window.py
@@ -0,0 +1,58 @@
+"""Capture target selection must never implicitly target Cua Driver itself."""
+
+from tools.computer_use.cua_backend_capture import _select_capture_target
+
+
+def _window(app_name: str, pid: int, window_id: int, z_index: int):
+ return {
+ "app_name": app_name,
+ "pid": pid,
+ "window_id": window_id,
+ "title": "",
+ "off_screen": False,
+ "z_index": z_index,
+ }
+
+
+def test_implicit_capture_skips_frontmost_cua_driver_window():
+ driver = _window("Cua Driver", 55543, 101, 6)
+ app = _window("IB Gateway 10.50", 777, 202, 5)
+
+ target = _select_capture_target(
+ [driver, app], app_requested=False, exact_target=False
+ )
+
+ assert target == app
+
+
+def test_all_self_windows_keep_existing_failure_target():
+ frontmost = _window("Cua Driver", 55543, 101, 6)
+ other = _window("cua-driver", 55543, 102, 5)
+
+ target = _select_capture_target(
+ [frontmost, other], app_requested=False, exact_target=False
+ )
+
+ assert target == frontmost
+
+
+def test_exact_capture_keeps_explicit_cua_driver_target():
+ driver = _window("Cua_Driver", 55543, 101, 6)
+ app = _window("IB Gateway 10.50", 777, 202, 5)
+
+ target = _select_capture_target(
+ [driver, app], app_requested=False, exact_target=True
+ )
+
+ assert target == driver
+
+
+def test_similarly_named_user_app_is_not_treated_as_driver_self():
+ docs = _window("Cua Driver Docs", 888, 303, 7)
+ app = _window("IB Gateway 10.50", 777, 202, 5)
+
+ target = _select_capture_target(
+ [docs, app], app_requested=False, exact_target=False
+ )
+
+ assert target == docs
diff --git a/tools/computer_use/cua_backend_capture.py b/tools/computer_use/cua_backend_capture.py
index d60bce2b4fc..9f58d1197ab 100644
--- a/tools/computer_use/cua_backend_capture.py
+++ b/tools/computer_use/cua_backend_capture.py
@@ -55,6 +55,12 @@ def _linux_x11_active_window_id() -> Optional[int]:
return None
return _parse_xprop_net_active_window(proc.stdout or "") if proc.returncode == 0 else None
+def _is_cua_driver_self_window(w: Dict[str, Any]) -> bool:
+ """True for the authorization daemon's own native window (normalized app name)."""
+ app_name = str(w.get("app_name", "")).strip().lower()
+ return re.sub(r"[\s_-]+", "", app_name) == "cuadriver"
+
+
def _select_capture_target(windows: List[Dict[str, Any]], *, app_requested: bool,
exact_target: bool = False) -> Dict[str, Any]:
"""Best window from z-sorted (frontmost-first) list_windows output. Unqualified default captures on
@@ -66,6 +72,14 @@ def _select_capture_target(windows: List[Dict[str, Any]], *, app_requested: bool
informative, keep that frontmost contract. See #58026.
"""
pool = [w for w in windows if not w["off_screen"]]
+ # Implicit captures skip the authorization daemon's own windows: the driver refuses to
+ # operate on itself, so a frontmost self-window would fail the capture instead of falling
+ # through to the next real window (#94527). Keep the old target when only self-windows
+ # exist, so the driver still reports its normal refusal. Exact targets stay untouched.
+ if not exact_target:
+ external = [w for w in pool if not _is_cua_driver_self_window(w)]
+ if external:
+ pool = external
if not exact_target and not app_requested and sys.platform == "linux":
pool = [w for w in pool if _is_real_app_window(w)] or pool
if pool and _z_index_uninformative(pool):