Skip to content

An AI Agent that interacts with Windows OS at GUI level.

Python

Add typed UIA exception hierarchy and dead-window fast-exit

jeomoncdd81b57b266
diff --git a/windows_use/agent/tree/service.py b/windows_use/agent/tree/service.py
index faed0f0..ee8d2a5 100644
--- a/windows_use/agent/tree/service.py
+++ b/windows_use/agent/tree/service.py
@@ -1,4 +1,5 @@
-from windows_use.uia import Control, ComboBoxControl, CheckBoxControl, EditControl, ButtonControl, SliderControl, ScrollPattern, WindowControl, Rect, ExpandCollapseState, ToggleState, PatternId, PropertyId, AccessibleRoleNames, TreeScope, ControlFromHandle
+from windows_use.uia import Control, ComboBoxControl, CheckBoxControl, EditControl, ButtonControl, SliderControl, ScrollPattern, WindowControl, Rect, ExpandCollapseState, ToggleState, PatternId, PropertyId, AccessibleRoleNames, TreeScope, ControlFromHandle, UIAException, UIADeadElementError, UIARetryableError, from_com_error
+from _ctypes import COMError
from windows_use.agent.tree.config import INTERACTIVE_CONTROL_TYPE_NAMES, DOCUMENT_CONTROL_TYPE_NAMES, INFORMATIVE_CONTROL_TYPE_NAMES, DEFAULT_ACTIONS, INTERACTIVE_ROLES, THREAD_MAX_RETRIES
from windows_use.agent.tree.views import TreeElementNode, ScrollElementNode, TextElementNode, Center, BoundingBox, TreeState
from windows_use.agent.tree.cache_utils import CacheRequestFactory, CachedControlHelper
@@ -115,7 +116,10 @@ class Tree:
scrollable_nodes.extend(scroll_nodes)
dom_informative_nodes.extend(info_nodes)
break
- except Exception as e:
+ except UIADeadElementError as e:
+ logger.debug(f"Skipping destroyed window (handle {handle}): {e}")
+ break
+ except (UIAException, Exception) as e:
retry_counts[handle] = attempt + 1
try:
window_name = ControlFromHandle(handle).Name
@@ -564,9 +568,10 @@ class Tree:
else:
# normal non-dialog children
self.tree_traversal(child, window_bounding_box, window_name, is_browser, interactive_nodes, scrollable_nodes, dom_interactive_nodes, dom_informative_nodes, is_dom=is_dom, is_dialog=is_dialog, element_cache_req=element_cache_req, children_cache_req=children_cache_req, hwnd=hwnd)
- except Exception as e:
- logger.error(f"Error in tree_traversal: {e}", exc_info=True)
+ except UIAException:
raise
+ except COMError as e:
+ raise from_com_error(e) from e
def window_name_correction(self,window_name:str)->str:
match window_name:
@@ -610,9 +615,10 @@ class Tree:
interactive_nodes.extend(dom_interactive_nodes)
return (interactive_nodes,scrollable_nodes,dom_informative_nodes)
- except Exception as e:
- logger.error(f"Error getting nodes for handle {handle}: {e}")
+ except UIAException:
raise
+ except COMError as e:
+ raise from_com_error(e) from e
def on_focus_change(self, sender:ctypes.POINTER('IUIAutomationElement')):
"""Handle focus change events."""
diff --git a/windows_use/uia/__init__.py b/windows_use/uia/__init__.py
index 394ce0f..169ef2c 100644
--- a/windows_use/uia/__init__.py
+++ b/windows_use/uia/__init__.py
@@ -1,4 +1,5 @@
from .enums import *
+from .exceptions import *
from .core import *
from .patterns import *
from .controls import *
diff --git a/windows_use/uia/enums.py b/windows_use/uia/enums.py
index 5f87d21..491695b 100644
--- a/windows_use/uia/enums.py
+++ b/windows_use/uia/enums.py
@@ -1953,3 +1953,93 @@ class VisualEffects(IntEnum):
VisualEffects_SoftEdges = 4
VisualEffects_Bevel = 5
+
+class UIAError(IntEnum):
+ """
+ COM/UIA HRESULT error codes as signed 32-bit integers (the form Python ctypes.COMError reports them).
+
+ Groups:
+ UIA_E_* — UI Automation–specific errors (UIAutomationCoreApi.h)
+ EVENT_E_* — COM+ event system errors (Winerror.h) — share hex range with UIA_E
+ RO_E_* — Windows Runtime object lifecycle errors (Winerror.h)
+ RPC_E_* — COM RPC transport/threading errors (Winerror.h)
+ CO_E_* — COM object state errors (Winerror.h)
+ E_* — Generic COM errors (Winerror.h)
+ """
+
+ # --- UIA-specific (UIAutomationCoreApi.h) ---
+ UIA_E_ELEMENTNOTENABLED = -2147220992 # 0x80040200 — method called on disabled element
+ UIA_E_ELEMENTNOTAVAILABLE = -2147220991 # 0x80040201 — element destroyed or virtualized
+ UIA_E_NOCLICKABLEPOINT = -2147220990 # 0x80040202 — element has no clickable point
+ UIA_E_PROXYASSEMBLYNOTLOADED= -2147220989 # 0x80040203 — client-side proxy provider failed to load
+ UIA_E_NOTSUPPORTED = -2147220988 # 0x80040204 — property/pattern not supported by provider
+ UIA_E_INVALIDOPERATION = -2146233079 # 0x80131509 — operation not valid in current state
+ UIA_E_TIMEOUT = -2146233083 # 0x80131505 — UIA operation timed out
+
+ # --- COM+ Event system (Winerror.h) — same hex range as UIA_E, easy to confuse ---
+ EVENT_E_ALL_SUBSCRIBERS_FAILED = -2147220991 # 0x80040201 — same code as UIA_E_ELEMENTNOTAVAILABLE; "unable to invoke any subscribers"
+ EVENT_E_INTERNALEXCEPTION = -2147220987 # 0x80040205 — unexpected exception in event system
+ EVENT_E_USER_EXCEPTION = -2147220984 # 0x80040208 — a subscriber raised an exception
+
+ # --- Windows Runtime object lifecycle (Winerror.h) ---
+ RO_E_CLOSED = -2147483629 # 0x80000013 — object has been closed/disposed
+
+ # --- COM RPC transport & threading (Winerror.h) ---
+ RPC_E_CALL_REJECTED = -2147418111 # 0x80010001 — callee rejected the call (app busy, may retry)
+ RPC_E_CALL_CANCELED = -2147418110 # 0x80010002 — call canceled by message filter
+ RPC_E_CONNECTION_TERMINATED = -2147418106 # 0x80010006 — connection terminated or in bogus state
+ RPC_E_SERVER_DIED = -2147418105 # 0x80010007 — server gone, call may have executed
+ RPC_E_SERVER_DIED_DNE = -2147418094 # 0x80010012 — server gone, call did NOT execute
+ RPC_E_SYS_CALL_FAILED = -2147417854 # 0x80010100 — underlying system call failed
+ RPC_E_OUT_OF_RESOURCES = -2147417855 # 0x80010101 — could not allocate required resource
+ RPC_E_ATTEMPTED_MULTITHREAD = -2147417854 # 0x80010102 — multiple threads in STA mode
+ RPC_E_SERVERFAULT = -2147417851 # 0x80010105 — server threw an exception
+ RPC_E_CHANGED_MODE = -2147417850 # 0x80010106 — STA/MTA mode conflict on thread
+ RPC_E_DISCONNECTED = -2147418360 # 0x80010108 — object invoked has disconnected from clients
+ RPC_E_SERVERCALL_RETRYLATER = -2147417846 # 0x8001010A — app busy, retry later
+ RPC_E_SERVERCALL_REJECTED = -2147417845 # 0x8001010B — message filter rejected the call
+ RPC_E_WRONG_THREAD = -2147417842 # 0x8001010E — interface marshalled for a different thread
+ RPC_E_THREAD_NOT_INIT = -2147417841 # 0x8001010F — CoInitialize not called on current thread
+ RPC_E_TIMEOUT = -2147417825 # 0x8001011F — RPC-level operation timed out
+ RPC_E_UNEXPECTED = -2147352577 # 0x8001FFFF — internal RPC error
+
+ # --- COM object state (Winerror.h) ---
+ CO_E_OBJNOTCONNECTED = -2147220995 # 0x800401FD — object not connected to server
+ CO_E_RELEASED = -2147220993 # 0x800401FF — object has been released
+ CO_E_NOTINITIALIZED = -2147220496 # 0x800401F0 — CoInitialize not called
+
+ # --- Generic COM (Winerror.h) ---
+ E_NOTIMPL = -2147467263 # 0x80004001 — not implemented
+ E_NOINTERFACE = -2147467262 # 0x80004002 — interface not supported
+ E_ABORT = -2147467260 # 0x80004004 — operation aborted
+ E_FAIL = -2147467259 # 0x80004005 — unspecified failure
+ RPC_E_ACCESS_DENIED = -2147417829 # 0x8001011B — RPC-level access denied
+ E_ACCESSDENIED = -2147024891 # 0x80070005 — access denied
+ E_OUTOFMEMORY = -2147024882 # 0x8007000E — out of memory
+ E_INVALIDARG = -2147024809 # 0x80070057 — invalid argument
+ E_UNEXPECTED = -2147418113 # 0x8000FFFF — catastrophic/unexpected failure
+ E_POINTER = -2147467261 # 0x80004003 — invalid pointer
+
+
+def is_dead_element_error(code: int) -> bool:
+ """Return True if the COM error code means the UIA element/window no longer exists."""
+ return code in {
+ UIAError.UIA_E_ELEMENTNOTAVAILABLE, # == EVENT_E_ALL_SUBSCRIBERS_FAILED
+ UIAError.RO_E_CLOSED,
+ UIAError.RPC_E_DISCONNECTED,
+ UIAError.RPC_E_SERVER_DIED,
+ UIAError.RPC_E_SERVER_DIED_DNE,
+ UIAError.RPC_E_CONNECTION_TERMINATED,
+ UIAError.CO_E_OBJNOTCONNECTED,
+ UIAError.CO_E_RELEASED,
+ }
+
+
+def is_retryable_error(code: int) -> bool:
+ """Return True if the COM error code means the app is busy but alive — retrying may succeed."""
+ return code in {
+ UIAError.RPC_E_CALL_REJECTED,
+ UIAError.RPC_E_SERVERCALL_RETRYLATER,
+ UIAError.RPC_E_SERVERCALL_REJECTED,
+ }
+
diff --git a/windows_use/uia/exceptions.py b/windows_use/uia/exceptions.py
new file mode 100644
index 0000000..ca5ecde
--- /dev/null
+++ b/windows_use/uia/exceptions.py
@@ -0,0 +1,121 @@
+from _ctypes import COMError # noqa: F401 — re-exported for callers
+from .enums import UIAError, is_dead_element_error, is_retryable_error
+
+
+class UIAException(Exception):
+ """Base exception for all UIA/COM errors. Wraps ctypes.COMError with a typed error code."""
+
+ def __init__(self, code: int, original: Exception | None = None):
+ self.code = code
+ self.original = original
+ name = UIAError(code).name if code in UIAError._value2member_map_ else hex(code & 0xFFFFFFFF)
+ super().__init__(f"{name} ({code})")
+
+ @property
+ def uia_error(self) -> UIAError | None:
+ return UIAError(self.code) if self.code in UIAError._value2member_map_ else None
+
+
+class UIADeadElementError(UIAException):
+ """Element or window no longer exists — retrying will not help.
+
+ Covers: UIA_E_ELEMENTNOTAVAILABLE, RO_E_CLOSED, RPC_E_DISCONNECTED,
+ RPC_E_SERVER_DIED, RPC_E_SERVER_DIED_DNE, RPC_E_CONNECTION_TERMINATED,
+ CO_E_OBJNOTCONNECTED, CO_E_RELEASED.
+ """
+
+
+class UIARetryableError(UIAException):
+ """App is busy but alive — the call may succeed on retry.
+
+ Covers: RPC_E_CALL_REJECTED, RPC_E_SERVERCALL_RETRYLATER, RPC_E_SERVERCALL_REJECTED.
+ """
+
+
+class UIANotEnabledError(UIAException):
+ """Method called on a disabled element (UIA_E_ELEMENTNOTENABLED)."""
+
+
+class UIANotSupportedError(UIAException):
+ """Property or pattern not supported by the provider (UIA_E_NOTSUPPORTED)."""
+
+
+class UIANoClickablePointError(UIAException):
+ """Element has no clickable point (UIA_E_NOCLICKABLEPOINT)."""
+
+
+class UIATimeoutError(UIAException):
+ """UIA or RPC operation timed out (UIA_E_TIMEOUT, RPC_E_TIMEOUT)."""
+
+
+class UIAThreadError(UIAException):
+ """Threading violation — wrong thread or CoInitialize not called.
+
+ Covers: RPC_E_WRONG_THREAD, RPC_E_THREAD_NOT_INIT, RPC_E_CHANGED_MODE.
+ """
+
+
+class UIAInvalidOperationError(UIAException):
+ """Operation is not valid in the current state (UIA_E_INVALIDOPERATION)."""
+
+
+class UIAAccessDeniedError(UIAException):
+ """Access denied to the element or operation (E_ACCESSDENIED, RPC_E_ACCESS_DENIED)."""
+
+
+class UIAUnknownError(UIAException):
+ """COM error with no specific UIA mapping."""
+
+
+_DEAD_ELEMENT_CODES = {
+ UIAError.UIA_E_ELEMENTNOTAVAILABLE,
+ UIAError.RO_E_CLOSED,
+ UIAError.RPC_E_DISCONNECTED,
+ UIAError.RPC_E_SERVER_DIED,
+ UIAError.RPC_E_SERVER_DIED_DNE,
+ UIAError.RPC_E_CONNECTION_TERMINATED,
+ UIAError.CO_E_OBJNOTCONNECTED,
+ UIAError.CO_E_RELEASED,
+}
+
+_RETRYABLE_CODES = {
+ UIAError.RPC_E_CALL_REJECTED,
+ UIAError.RPC_E_SERVERCALL_RETRYLATER,
+ UIAError.RPC_E_SERVERCALL_REJECTED,
+}
+
+_CODE_TO_EXCEPTION: dict[int, type[UIAException]] = {
+ UIAError.UIA_E_ELEMENTNOTAVAILABLE: UIADeadElementError,
+ UIAError.RO_E_CLOSED: UIADeadElementError,
+ UIAError.RPC_E_DISCONNECTED: UIADeadElementError,
+ UIAError.RPC_E_SERVER_DIED: UIADeadElementError,
+ UIAError.RPC_E_SERVER_DIED_DNE: UIADeadElementError,
+ UIAError.RPC_E_CONNECTION_TERMINATED: UIADeadElementError,
+ UIAError.CO_E_OBJNOTCONNECTED: UIADeadElementError,
+ UIAError.CO_E_RELEASED: UIADeadElementError,
+
+ UIAError.RPC_E_CALL_REJECTED: UIARetryableError,
+ UIAError.RPC_E_SERVERCALL_RETRYLATER: UIARetryableError,
+ UIAError.RPC_E_SERVERCALL_REJECTED: UIARetryableError,
+
+ UIAError.UIA_E_ELEMENTNOTENABLED: UIANotEnabledError,
+ UIAError.UIA_E_NOTSUPPORTED: UIANotSupportedError,
+ UIAError.UIA_E_NOCLICKABLEPOINT: UIANoClickablePointError,
+ UIAError.UIA_E_TIMEOUT: UIATimeoutError,
+ UIAError.RPC_E_TIMEOUT: UIATimeoutError,
+ UIAError.UIA_E_INVALIDOPERATION: UIAInvalidOperationError,
+
+ UIAError.RPC_E_WRONG_THREAD: UIAThreadError,
+ UIAError.RPC_E_THREAD_NOT_INIT: UIAThreadError,
+ UIAError.RPC_E_CHANGED_MODE: UIAThreadError,
+
+ UIAError.E_ACCESSDENIED: UIAAccessDeniedError,
+ UIAError.RPC_E_ACCESS_DENIED: UIAAccessDeniedError,
+}
+
+
+def from_com_error(e: COMError) -> UIAException:
+ """Convert a ctypes.COMError into a typed UIAException. Only call with COMError instances."""
+ code: int = e.args[0] if e.args else 0
+ exc_class = _CODE_TO_EXCEPTION.get(code, UIAUnknownError)
+ return exc_class(code, e)