An AI Agent that interacts with Windows OS at GUI level.
Python
fix(logging): report UIA watchdog callback errors through the logger
Hussain9ff50704bfe1
diff --git a/tests/test_watchdog_stdout_safety.py b/tests/test_watchdog_stdout_safety.pynew file mode 100644index 0000000..e194791--- /dev/null+++ b/tests/test_watchdog_stdout_safety.py@@ -0,0 +1,62 @@+"""The UIA watchdog handlers must never write to stdout.++stdout is the transport for newline delimited JSON-RPC. Any MCP server (or+other stdout based protocol harness) embedding windows-use breaks as soon as a+focus/structure/property callback raises, because the stray ``print`` lands in+the middle of the protocol stream.++The handlers must report failures through the logger (stderr) instead.+"""++import logging+import weakref++from windows_use.agent.watchdog import event_handlers+++class _Parent:+ """Minimal stand-in for the watchdog service."""++ def __init__(self):+ def _boom(*_args):+ raise RuntimeError("callback exploded")++ self._focus_callback = _boom+ self._structure_callback = _boom+ self._property_callback = _boom+++class _Stub:+ """Stands in for the COM object; the handlers only use ``_parent``."""++ def __init__(self, parent):+ self._parent = weakref.ref(parent)+++def test_focus_changed_handler_does_not_write_to_stdout(capsys):+ parent = _Parent()+ event_handlers.FocusChangedEventHandler.HandleFocusChangedEvent(_Stub(parent), None)+ assert capsys.readouterr().out == ""+++def test_structure_changed_handler_does_not_write_to_stdout(capsys):+ parent = _Parent()+ event_handlers.StructureChangedEventHandler.HandleStructureChangedEvent(+ _Stub(parent), None, None, None+ )+ assert capsys.readouterr().out == ""+++def test_property_changed_handler_does_not_write_to_stdout(capsys):+ parent = _Parent()+ event_handlers.PropertyChangedEventHandler.HandlePropertyChangedEvent(+ _Stub(parent), None, None, None+ )+ assert capsys.readouterr().out == ""+++def test_handlers_report_failures_through_the_logger(caplog):+ parent = _Parent()+ with caplog.at_level(logging.WARNING, logger=event_handlers.__name__):+ event_handlers.FocusChangedEventHandler.HandleFocusChangedEvent(_Stub(parent), None)+ assert any("focus callback" in record.message for record in caplog.records)diff --git a/windows_use/agent/watchdog/event_handlers.py b/windows_use/agent/watchdog/event_handlers.pyindex 4214704..554ad77 100644--- a/windows_use/agent/watchdog/event_handlers.py+++ b/windows_use/agent/watchdog/event_handlers.py@@ -1,9 +1,12 @@+import loggingimport weakrefimport comtypesfrom windows_use.uia.core import _AutomationClient+logger = logging.getLogger(__name__)+# Get UIA Interface for COM definitionsuia_client = _AutomationClient.instance()UIA = uia_client.UIAutomationCore@@ -22,7 +25,7 @@ class FocusChangedEventHandler(comtypes.COMObject):if parent and parent._focus_callback:parent._focus_callback(sender)except Exception as e:- print(f"Error in focus callback: {e}")+ logger.exception("Error in focus callback: %s", e)return 0 # S_OK@@ -39,7 +42,7 @@ class StructureChangedEventHandler(comtypes.COMObject):if parent and parent._structure_callback:parent._structure_callback(sender, changeType, runtimeId)except Exception as e:- print(f"Error in structure callback: {e}")+ logger.exception("Error in structure callback: %s", e)return 0 # S_OK@@ -56,5 +59,5 @@ class PropertyChangedEventHandler(comtypes.COMObject):if parent and parent._property_callback:parent._property_callback(sender, propertyId, newValue)except Exception as e:- print(f"Error in property callback: {e}")+ logger.exception("Error in property callback: %s", e)return 0 # S_OK