The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(computer-use): recreate CUA session suspect after MCP timeout (#74799)
BlackishGreen33c73d721b1d65
diff --git a/tests/tools/test_computer_use.py b/tests/tools/test_computer_use.pyindex 68ca6b7da49..58385bad050 100644--- a/tests/tools/test_computer_use.py+++ b/tests/tools/test_computer_use.py@@ -1256,6 +1256,89 @@ class TestCuaDriverSessionReconnect:assert session._reconnect_log == ["stop", "start"]assert len(bridge.calls) == 1+ def test_timeout_marks_session_suspect_without_replaying(self):+ """An MCP timeout fails closed: outcome unknown, no silent replay (#74799)."""+ import concurrent.futures++ class FakeBridge:+ def __init__(self):+ self.calls = []++ def run(self, value, timeout=None):+ self.calls.append((value, timeout))+ raise concurrent.futures.TimeoutError()++ bridge = FakeBridge()+ session = self._make_session(bridge)++ result = session.call_tool("click", {"x": 20, "y": 30})++ # Uncertainty surfaced in the error shape: the action MAY have taken+ # effect on the remote screen before the deadline hit.+ assert result["isError"] is True+ assert result["structuredContent"]["code"] == "timeout_outcome_unknown"+ assert result["structuredContent"]["next_step"] == "fresh_state"+ assert "unknown" in result["data"]+ # The timed-out call was NOT replayed and the session was not+ # eagerly torn down — recreation is deferred to the next call.+ assert len(bridge.calls) == 1+ assert session._reconnect_log == []+ assert session._timeout_suspect is True++ def test_suspect_session_is_recreated_before_next_call(self):+ """The next call_tool tears down + recreates the suspect session first."""+ import concurrent.futures++ class FakeBridge:+ def __init__(self):+ self.calls = []+ self.timeout = True++ def run(self, value, timeout=None):+ self.calls.append(value)+ if self.timeout:+ raise concurrent.futures.TimeoutError()+ return {"ok": True}++ bridge = FakeBridge()+ session = self._make_session(bridge)++ session.call_tool("get_window_state", {"window_id": 42})+ assert session._timeout_suspect is True++ bridge.timeout = False+ assert session.call_tool("list_apps", {}) == {"ok": True}++ # Recreate-before-call: stop/start happened ahead of the second call,+ # and only one call per call_tool — no replay of either operation.+ assert session._reconnect_log == ["stop", "start"]+ assert [c for c in bridge.calls] == [+ ("call", "get_window_state", {"window_id": 42}),+ ("call", "list_apps", {}),+ ]+ # Flag cleared: subsequent calls do not trigger another restart.+ assert session._timeout_suspect is False+ session.call_tool("list_windows", {})+ assert session._reconnect_log == ["stop", "start"]++ def test_healthy_session_is_never_restarted(self):+ """Negative probe: a clean call must not touch the session lifecycle."""++ class FakeBridge:+ def __init__(self):+ self.calls = []++ def run(self, value, timeout=None):+ self.calls.append(value)+ return {"ok": True}++ bridge = FakeBridge()+ session = self._make_session(bridge)++ assert session.call_tool("list_apps", {}) == {"ok": True}+ assert session._reconnect_log == []+ assert session._timeout_suspect is False+def test_mutation_does_not_cross_to_cli_on_transient_proxy_error(self):class FakeBridge:def run(self, value, timeout=None):diff --git a/tools/computer_use/cua_backend.py b/tools/computer_use/cua_backend.pyindex bd93ea2cea5..d3a6d92a7b5 100644--- a/tools/computer_use/cua_backend.py+++ b/tools/computer_use/cua_backend.py@@ -2207,6 +2207,12 @@ class _CuaDriverSession:"list_windows",})+ # Set when an MCP call timed out (#74799): a timed-out session is+ # wedged for all later calls, so it is torn down and recreated before+ # the next non-lifecycle call_tool. Class-level default so tests that+ # bypass __init__ see a healthy (non-suspect) session.+ _timeout_suspect = False+@classmethoddef _transport_replay_is_safe(cls, name: str) -> bool:return name in cls._TRANSPORT_REPLAY_SAFE_TOOLS@@ -2233,7 +2239,53 @@ class _CuaDriverSession:"isError": True,}+ @staticmethod+ def _timeout_outcome(name: str, exc: Exception) -> Dict[str, Any]:+ """Fail-closed result for an MCP call that hit its deadline (#74799).++ The action MAY have taken effect on the remote screen before the+ response was lost — the same effect_disposition=unknown principle as+ ``_unknown_transport_outcome`` — so the timed-out call is never+ silently replayed here; the caller decides after taking fresh state.+ """+ message = (+ f"cua-driver MCP call {name} timed out; the action outcome is "+ "unknown and may still have taken effect on the remote screen. "+ "The session has been marked suspect and will be recreated before "+ "the next computer-use call. Take fresh state before deciding "+ "whether to act again."+ )+ return {+ "data": message,+ "images": [],+ "image_mime_types": [],+ "structuredContent": {+ "ok": False,+ "code": "timeout_outcome_unknown",+ "message": message,+ "operation": name,+ "next_step": "fresh_state",+ "detail": str(exc),+ },+ "isError": True,+ }+def call_tool(self, name: str, args: Dict[str, Any], timeout: float = 30.0) -> Dict[str, Any]:+ # A prior MCP timeout (#74799) marks the session suspect: it may be+ # wedged for every later call. Recreate it before this call so a+ # single timeout never poisons the rest of the computer-use session.+ # Healthy sessions (flag clear) are never restarted here.+ if self._timeout_suspect and name not in self._LIFECYCLE_CALLS:+ logger.warning(+ "cua-driver session suspect after earlier MCP timeout; "+ "recreating before %s",+ name,+ )+ with self._lock:+ self._restart_session_locked()+ self._timeout_suspect = False+ self._restore_declared_session_after_transport_reset(timeout)+# A prior session may have died (MCP drop / driver crash): its# lifecycle coro reset _started to False in its finally (#55048).if not self._started and name not in self._LIFECYCLE_CALLS:@@ -2250,6 +2302,18 @@ class _CuaDriverSession:timeout=timeout,)except Exception as e:+ if isinstance(e, concurrent.futures.TimeoutError):+ # MCP deadline hit (#74799): the session is suspect and must+ # be recreated before the next call. Fail closed — the action+ # may have taken effect on the remote screen, so never replay+ # it here; surface the uncertainty instead (#74799).+ self._timeout_suspect = True+ logger.warning(+ "cua-driver MCP timed out on %s; marking session suspect "+ "for recreation before the next call",+ name,+ )+ return self._timeout_outcome(name, e)if self._is_transient_daemon_error(e):if not self._transport_replay_is_safe(name):self._notify_transport_reset()