Skip to content

The agent that grows with you

refactor(computer_use): inline single-caller restore helper; compact session/parse docstrings and comments (WHY sentences kept)

Teknium2803e6c1efdb
diff --git a/tools/computer_use/cua_backend_parse.py b/tools/computer_use/cua_backend_parse.py
index f419821a1a0..f3374726666 100644
--- a/tools/computer_use/cua_backend_parse.py
+++ b/tools/computer_use/cua_backend_parse.py
@@ -1,7 +1,6 @@
-"""Pure parsing helpers for the cua-driver backend: MCP result flattening, ``list_windows``
-/ ``get_window_state`` payload normalisation, key combos. No I/O, no module state — every
-function depends only on its inputs, so the MCP and CLI transports share them safely.
-"""
+"""Pure parsing helpers for the cua-driver backend: MCP result flattening, ``list_windows`` /
+``get_window_state`` payload normalisation, key combos. No I/O, no module state — every function
+depends only on its inputs, so the MCP and CLI transports share them safely."""
from __future__ import annotations
@@ -12,8 +11,8 @@ from typing import Any, Dict, List, Optional, Tuple
from tools.computer_use.backend import ActionResult, UIElement, image_dimensions_from_bytes
-# Linux/X11 surfaces GNOME Shell / desktop backdrop windows ahead of real app windows with
-# no useful z-order; they are targetable but capture as empty, so default capture skips them.
+# Linux/X11 surfaces GNOME Shell / desktop backdrop windows ahead of real app windows with no useful z-order; they
+# are targetable but capture as empty, so default capture skips them.
_NON_APP_WINDOW_TITLE_PREFIXES = ("@!", "Desktop", "gnome-shell", "GNOME Shell") # "@!" = GNOME helpers
_ELEMENT_LINE_RE = re.compile(
@@ -26,27 +25,25 @@ _ELEMENT_LINE_RE = re.compile(
r'(?:\s+(?:\(\d+\)\s+)?id=([^\s\[\]]+))?', # optional id=value (after an optional (order))
re.MULTILINE,
)
-"""get_window_state AX-tree markdown line: ``[N] AXRole`` + label in one of four forms
-(``= "value"``, ``"quoted"``, ``(paren)``, ``id=Label`` optionally after an ``(order)`` number).
-A parenthesised pure-digit group is an ORDER index, not a label, and is excluded so the id=
-label wins. Group 1 index, group 2 role, groups 3-6 the label in whichever form matched."""
+"""get_window_state AX-tree markdown line: ``[N] AXRole`` + label in one of four forms (``= "value"``, ``"quoted"``,
+``(paren)``, ``id=Label`` optionally after an ``(order)`` number). A parenthesised pure-digit group is an ORDER
+index, not a label, and is excluded so the id= label wins. Group 1 index, group 2 role, groups 3-6 the label."""
def _mcp_field(obj, snake: str, camel: str, default=None):
- """Read an MCP model field across the 1.x -> 2.x rename: mcp 2.0 exposes snake_case
- attributes and keeps camelCase only as a serialization alias, so ``getattr(result,
- "isError", False)`` is False for every 2.x result and a denied call looks like success.
- Deliberately duplicated from ``tools.mcp_tool.mcp_field`` so computer_use never loads
- the much larger config-driven MCP client module."""
+ """Read an MCP model field across the 1.x -> 2.x rename: mcp 2.0 exposes snake_case attributes and keeps
+ camelCase only as a serialization alias, so ``getattr(result, "isError", False)`` is False for every 2.x result
+ and a denied call looks like success. Deliberately duplicated from ``tools.mcp_tool.mcp_field`` so computer_use
+ never loads the much larger config-driven MCP client module."""
return getattr(obj, snake, getattr(obj, camel, default))
def _action_result_from(name: str, ok: bool, message: str, meta: Dict[str, Any],
structured: Dict[str, Any], *, requested_delivery: Optional[str] = None) -> ActionResult:
- """Build an ActionResult, lifting cua-driver's structured verdict. structuredContent is
- canonical, the flattened ``meta`` copy the fallback. Every structured field is additive:
- a driver that omits one leaves the attribute ``None`` so old drivers see unchanged behavior."""
+ """Build an ActionResult, lifting cua-driver's structured verdict. structuredContent is canonical, the flattened
+ ``meta`` copy the fallback. Every structured field is additive: a driver that omits one leaves the attribute
+ ``None`` so old drivers see unchanged behavior."""
sc = structured if isinstance(structured, dict) else {}
- def _pick(key: str) -> Any:
+ def _raw(key: str) -> Any:
return sc.get(key) if key in sc else meta.get(key)
def _typed(value: Any, typ) -> Any:
@@ -54,15 +51,13 @@ def _action_result_from(name: str, ok: bool, message: str, meta: Dict[str, Any],
return ActionResult(
ok=ok, action=name, message=message, meta=meta,
- verified=_typed(_pick("verified"), bool),
- effect=_typed(_pick("effect"), str),
- escalation=_typed(_pick("escalation"), dict),
- path=_typed(_pick("path"), str),
- degraded=_typed(_pick("degraded"), bool),
+ verified=_typed(_raw("verified"), bool), effect=_typed(_raw("effect"), str),
+ escalation=_typed(_raw("escalation"), dict), path=_typed(_raw("path"), str),
+ degraded=_typed(_raw("degraded"), bool),
# What we asked for; the driver's `path` records the rung that ran.
delivery_mode=_typed(requested_delivery, str),
# Refusal/limitation code — drivers spell it "code" or "reason_code".
- code=_typed(_pick("code") or _pick("reason_code"), str),
+ code=_typed(_raw("code") or _raw("reason_code"), str),
)
def _z_index_uninformative(windows: List[Dict[str, Any]]) -> bool:
@@ -70,8 +65,8 @@ def _z_index_uninformative(windows: List[Dict[str, Any]]) -> bool:
return len({w.get("z_index", 0) for w in windows}) <= 1
def _parse_xprop_net_active_window(stdout: str) -> Optional[int]:
- """Parse ``xprop -root _NET_ACTIVE_WINDOW`` stdout into a window id: the ``window id #
- 0x...`` form, falling back to the first hex token."""
+ """Parse ``xprop -root _NET_ACTIVE_WINDOW`` stdout into a window id: the ``window id # 0x...`` form, falling
+ back to the first hex token."""
text = stdout or ""
match = re.search(r"window id # (0x[0-9a-fA-F]+)", text) or re.search(r"(0x[0-9a-fA-F]+)", text)
return int(match.group(1), 16) if match else None
@@ -79,30 +74,23 @@ def _parse_xprop_net_active_window(stdout: str) -> Optional[int]:
def _is_real_app_window(w: Dict[str, Any]) -> bool:
"""Return False for desktop/shell helper windows that capture as empty."""
title = w.get("title", "")
- return not any(
- title.startswith(p) or title.lower().startswith(p.lower())
- for p in _NON_APP_WINDOW_TITLE_PREFIXES
- )
+ return not any(title.startswith(p) or title.lower().startswith(p.lower()) for p in _NON_APP_WINDOW_TITLE_PREFIXES)
def _parse_elements_from_tree(markdown: str) -> List[UIElement]:
- """Parse UIElements from get_window_state AX-tree markdown — last-resort fallback for
- drivers without ``structuredContent.elements``. Bounds are always ``(0, 0, 0, 0)`` (the
- markdown carries none), fine for element-index clicks since the driver resolves the frame."""
+ """Parse UIElements from get_window_state AX-tree markdown — last-resort fallback for drivers without
+ ``structuredContent.elements``. Bounds are always ``(0, 0, 0, 0)`` (the markdown carries none), fine for
+ element-index clicks since the driver resolves the frame."""
return [
- UIElement(
- index=int(m.group(1)),
- role=m.group(2),
- # groups 3-6: value / quoted / paren / id= label (first non-None wins)
- label=m.group(3) or m.group(4) or m.group(5) or m.group(6) or "",
- bounds=(0, 0, 0, 0),
- )
+ # groups 3-6: value / quoted / paren / id= label (first non-None wins)
+ UIElement(index=int(m.group(1)), role=m.group(2),
+ label=m.group(3) or m.group(4) or m.group(5) or m.group(6) or "", bounds=(0, 0, 0, 0))
for m in _ELEMENT_LINE_RE.finditer(markdown)
]
def _parse_elements_from_structured(raw_elements: List[Dict[str, Any]]) -> List[UIElement]:
- """Read the canonical ``structuredContent.elements`` array: ``element_index``, ``role``,
- ``label`` and, when AT-SPI / AXFrame returned usable bounds, ``frame`` ``{x, y, w, h}`` —
- so real pixel bounds survive (the markdown path loses them). Malformed entries are skipped."""
+ """Read the canonical ``structuredContent.elements`` array: ``element_index``, ``role``, ``label`` and, when
+ AT-SPI / AXFrame returned usable bounds, ``frame`` ``{x, y, w, h}`` — so real pixel bounds survive (the
+ markdown path loses them). Malformed entries are skipped."""
elements: List[UIElement] = []
for raw in raw_elements:
idx = raw.get("element_index") if isinstance(raw, dict) else None
@@ -114,9 +102,7 @@ def _parse_elements_from_structured(raw_elements: List[Dict[str, Any]]) -> List[
if isinstance(frame, dict) and frame:
bounds = tuple(int(frame.get(k, 0)) for k in ("x", "y", "w", "h")) # type: ignore[assignment]
elements.append(UIElement(
- index=idx,
- role=role if isinstance(role, str) else "",
- label=label if isinstance(label, str) else "",
+ index=idx, role=role if isinstance(role, str) else "", label=label if isinstance(label, str) else "",
bounds=bounds,
# Opaque `s{snapshot_hex}:{index}` token — the driver owns parse + LRU semantics.
element_token=token if isinstance(token, str) and token else None,
@@ -150,9 +136,8 @@ def _parse_key_combo(keys: str) -> Tuple[Optional[str], List[str]]:
def _tool_envelope(data: Any, images: List[str], structured: Any, is_error: bool,
image_mime_types: Optional[List[str]] = None) -> Dict[str, Any]:
- """The normalised tool-result dict every transport emits: ``{data, images,
- [image_mime_types,] structuredContent, isError}``. ``image_mime_types`` is only present
- when the transport can report it (MCP image parts); the CLI remap omits the key."""
+ """The normalised tool-result dict every transport emits: ``{data, images, [image_mime_types,] structuredContent,
+ isError}``. ``image_mime_types`` is only present when the transport can report it (MCP image parts)."""
out: Dict[str, Any] = {"data": data, "images": images}
if image_mime_types is not None:
out["image_mime_types"] = image_mime_types
@@ -160,10 +145,9 @@ def _tool_envelope(data: Any, images: List[str], structured: Any, is_error: bool
return out
def _extract_tool_result(mcp_result: Any) -> Dict[str, Any]:
- """Flatten an mcp CallToolResult into ``{data, images, image_mime_types, structuredContent,
- isError}``. ``data`` is the joined text parts (parsed as JSON when it looks like JSON);
- ``image_mime_types`` is parallel to ``images`` with ``""`` where the part carried no
- mimeType (older drivers — callers then sniff the base64 prefix)."""
+ """Flatten an mcp CallToolResult into ``{data, images, image_mime_types, structuredContent, isError}``. ``data``
+ is the joined text parts (parsed as JSON when it looks like JSON); ``image_mime_types`` is parallel to
+ ``images`` with ``""`` where the part carried no mimeType (older drivers — callers then sniff the base64 prefix)."""
data: Any = None
images: List[str] = []
image_mime_types: List[str] = []
@@ -187,9 +171,9 @@ def _extract_tool_result(mcp_result: Any) -> Dict[str, Any]:
_mcp_field(mcp_result, "is_error", "isError", False) is True, image_mime_types)
def _image_from_tool_result(out: Dict[str, Any]) -> tuple[Optional[str], Optional[str]]:
- """Pull ``(b64, mime_type)`` out of a flattened tool result. cua-driver delivers screenshots
- as an MCP ``image`` part (``out["images"]``) or as ``screenshot_png_b64`` in structuredContent
- (newer builds, CLI transport); checking both keeps capture() robust to the driver moving it."""
+ """Pull ``(b64, mime_type)`` out of a flattened tool result. cua-driver delivers screenshots as an MCP ``image``
+ part (``out["images"]``) or as ``screenshot_png_b64`` in structuredContent (newer builds, CLI transport);
+ checking both keeps capture() robust to the driver moving it."""
images = out.get("images") or []
if images and images[0]:
mimes = out.get("image_mime_types") or []
@@ -215,18 +199,17 @@ def _positive_int(value: Any) -> Optional[int]:
return parsed if parsed is not None and parsed > 0 else None
def _is_placeholder_id(value: Any) -> bool:
- """True when *value* is a schema-filler id (``0`` / negative) rather than a target: some
- providers zero-fill every optional integer, and treating that as targeting would drop the
- caller's ``app=``. Non-numeric values are NOT placeholders — they still reach validation."""
+ """True when *value* is a schema-filler id (``0`` / negative) rather than a target: some providers zero-fill
+ every optional integer, and treating that as targeting would drop the caller's ``app=``. Non-numeric values
+ are NOT placeholders — they still reach validation."""
parsed = _int_or_none(value)
return parsed is not None and parsed <= 0
def _ingest_windows(raw_windows: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
- """Normalise cua-driver ``list_windows`` entries, dropping unusable ones. Every downstream
- call needs integer ``pid`` and ``window_id``; on X11 the PID comes from the optional
- ``_NET_WM_PID`` property, so root/panel/popup windows report ``pid: null`` — skip those
- instead of aborting the enumeration. ``z_index``: higher = closer to front; Wayland's null
- (undefined stacking) sorts lowest so real windows stay above the desktop."""
+ """Normalise cua-driver ``list_windows`` entries, dropping unusable ones. Every downstream call needs integer
+ ``pid`` and ``window_id``; on X11 the PID comes from the optional ``_NET_WM_PID`` property, so root/panel/popup
+ windows report ``pid: null`` — skip those instead of aborting the enumeration. ``z_index``: higher = closer to
+ front; Wayland's null (undefined stacking) sorts lowest so real windows stay above the desktop."""
windows: List[Dict[str, Any]] = []
for w in raw_windows:
if not isinstance(w, dict): # untrusted compatibility envelopes
@@ -247,8 +230,8 @@ def _ingest_windows(raw_windows: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
return windows
def _windows_from_tool_result(out: Dict[str, Any]) -> List[Dict[str, Any]]:
- """Return list_windows payloads across cua-driver result shapes: structuredContent.windows,
- then ``windows`` / ``_legacy_windows`` in the text payload, then on the envelope itself."""
+ """Return list_windows payloads across cua-driver result shapes: structuredContent.windows, then ``windows`` /
+ ``_legacy_windows`` in the text payload, then on the envelope itself."""
candidates = ((out.get("structuredContent"), ("windows",)),
(out.get("data"), ("windows", "_legacy_windows")),
(out, ("windows", "_legacy_windows")))
diff --git a/tools/computer_use/cua_backend_session.py b/tools/computer_use/cua_backend_session.py
index 07028dcdb28..d34cef4f2c1 100644
--- a/tools/computer_use/cua_backend_session.py
+++ b/tools/computer_use/cua_backend_session.py
@@ -1,8 +1,7 @@
-"""cua-driver MCP session plumbing: the asyncio bridge thread and the lazily-started,
-self-healing ``_CuaDriverSession`` (MCP transport with a ``cua-driver call`` CLI fallback).
-Driver resolution / policy helpers are looked up lazily through
-``tools.computer_use.cua_backend`` so tests that patch them there keep working.
-"""
+"""cua-driver MCP session plumbing: the asyncio bridge thread and the lazily-started, self-healing
+``_CuaDriverSession`` (MCP transport with a ``cua-driver call`` CLI fallback). Driver resolution /
+policy helpers are looked up lazily through ``tools.computer_use.cua_backend`` so tests that patch
+them there keep working."""
from __future__ import annotations
@@ -67,8 +66,8 @@ class _AsyncBridge:
self._thread.join(timeout=2.0)
self._thread = self._loop = None
-# Fail-closed messages for calls whose effect on the remote screen is unknown. The action
-# MAY have landed, so it is never replayed; the caller decides after taking fresh state.
+# Fail-closed messages for calls whose effect on the remote screen is unknown. The action MAY have landed, so it
+# is never replayed; the caller decides after taking fresh state.
_UNKNOWN_OUTCOME_MESSAGES = {
"transport_outcome_unknown": (
"cua-driver transport failed during {name}; the action outcome is unknown, so Hermes "
@@ -83,50 +82,42 @@ _UNKNOWN_OUTCOME_MESSAGES = {
def _outcome_unknown(name: str, exc: Exception, code: str) -> Dict[str, Any]:
"""Fail-closed ``isError`` result for *code* (see ``_UNKNOWN_OUTCOME_MESSAGES``)."""
message = _UNKNOWN_OUTCOME_MESSAGES[code].format(name=name)
- structured = {"ok": False, "code": code, "message": message, "operation": name,
- "next_step": "fresh_state", "detail": str(exc)}
- return _tool_envelope(message, [], structured, True, [])
+ return _tool_envelope(message, [], {"ok": False, "code": code, "message": message, "operation": name,
+ "next_step": "fresh_state", "detail": str(exc)}, True, [])
def _tool_field(obj: Any, *names: str) -> Any:
"""``_mcp_field`` plus the ``model_extra`` fallback some MCP SDKs (Pydantic v2) forward custom fields via."""
value = _mcp_field(obj, names[0], names[-1])
- if value is None:
- value = (getattr(obj, "model_extra", None) or {}).get(names[-1])
- return value
+ return (getattr(obj, "model_extra", None) or {}).get(names[-1]) if value is None else value
_CLI_ATTEMPTS = 4 # CLI fallback transport retries (backoff 0.5s doubling)
def _cli_run_json(cmd: List[str], env: Dict[str, str], name: str, timeout: float) -> Any:
- """Run ``cua-driver call`` with backoff until it prints JSON; return the parsed value.
- "daemon is not running" is PERMANENT for this invocation (the CLI needs the machine-wide
- daemon socket, which Linux installs typically never start) -> fail fast, no ~3.5s backoff."""
+ """Run ``cua-driver call`` with backoff until it prints JSON; return the parsed value. "daemon is not running"
+ is PERMANENT for this invocation (the CLI needs the machine-wide daemon socket, which Linux installs typically
+ never start) -> fail fast, no ~3.5s backoff."""
import subprocess as _subprocess
import time as _time
from tools.computer_use import cua_backend as _cb
- backoff = 0.5
- last_err = ""
+ backoff, last_err = 0.5, ""
for attempt in range(_CLI_ATTEMPTS):
try:
- proc = _subprocess.run(
- cmd, capture_output=True, text=True, encoding="utf-8", errors="replace",
- timeout=max(15.0, timeout), creationflags=_cb.windows_hide_flags(), env=env)
+ proc = _subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace",
+ timeout=max(15.0, timeout), creationflags=_cb.windows_hide_flags(), env=env)
except Exception as e: # pragma: no cover - subprocess spawn failure
raise RuntimeError(f"cua-driver CLI fallback for {name} failed to spawn: {e}") from e
-
out, err = (proc.stdout or "").strip(), proc.stderr or ""
last_err = out[:200] or err[:200]
if "daemon is not running" in out or "daemon is not running" in err:
- raise RuntimeError(
- f"cua-driver CLI fallback for {name} unavailable: the "
- "machine-wide cua-driver daemon is not running (the "
- "CLI transport requires it; the MCP runtime does not).")
+ raise RuntimeError(f"cua-driver CLI fallback for {name} unavailable: the "
+ "machine-wide cua-driver daemon is not running (the "
+ "CLI transport requires it; the MCP runtime does not).")
start = min((i for i in (out.find("{"), out.find("[")) if i != -1), default=-1)
with contextlib.suppress(json.JSONDecodeError):
if start != -1:
return json.loads(out[start:])
- # No JSON (EAGAIN warning / empty) — retry with backoff.
- if attempt < _CLI_ATTEMPTS - 1:
+ if attempt < _CLI_ATTEMPTS - 1: # no JSON (EAGAIN warning / empty) — retry with backoff
logger.warning("cua-driver CLI fallback for %s got no JSON (attempt %d/%d); "
"retrying in %.1fs", name, attempt + 1, _CLI_ATTEMPTS, backoff)
_time.sleep(backoff)
@@ -135,7 +126,7 @@ def _cli_run_json(cmd: List[str], env: Dict[str, str], name: str, timeout: float
f"{_CLI_ATTEMPTS} attempts: {last_err}")
def _cli_result(parsed: Any, shot_file: Optional[str]) -> Dict[str, Any]:
- """Remap a ``cua-driver call`` JSON body into the ``_extract_tool_result`` shape."""
+ """Remap a ``cua-driver call`` JSON body into the ``_extract_tool_result`` shape (no ``image_mime_types`` key)."""
if not isinstance(parsed, dict):
return _tool_envelope(None, [], None, False)
# In-band logical failures with exit 0 must still fail closed.
@@ -156,40 +147,31 @@ def _cli_result(parsed: Any, shot_file: Optional[str]) -> Dict[str, Any]:
class _CuaDriverSession:
- """Holds the mcp ClientSession. Spawned lazily; re-entered on drop.
-
- Lifecycle ownership: one long-running coroutine (`_lifecycle_coro`) opens the
- stdio_client + ClientSession contexts, populates capabilities, sets `_ready_event`,
- waits on `_shutdown_event`, then closes the contexts — enter and exit in the SAME
- task, as anyio's cancel-scope invariant requires (each `bridge.run(coro)` is a NEW
- task). Tool calls run in short-lived tasks touching only the session object.
- """
+ """Holds the mcp ClientSession. Spawned lazily; re-entered on drop. Lifecycle ownership: one long-running
+ coroutine (`_lifecycle_coro`) opens the stdio_client + ClientSession contexts, populates capabilities, sets
+ `_ready_event`, waits on `_shutdown_event`, then closes the contexts — enter and exit in the SAME task, as
+ anyio's cancel-scope invariant requires (each `bridge.run(coro)` is a NEW task). Tool calls run in short-lived
+ tasks touching only the session object."""
- # Handshake calls issued BY start()/stop() — exempt from call_tool's auto-restart
- # guard, or start() would recurse.
+ # Handshake calls issued BY start()/stop() — exempt from call_tool's auto-restart guard, or start() would recurse.
_LIFECYCLE_CALLS = frozenset({"start_session", "end_session"})
- # Idempotent reads, safe to replay after a broken transport. Mutations stay out:
- # a lost response does not prove they failed.
- _TRANSPORT_REPLAY_SAFE_TOOLS = frozenset({
- "get_cursor_position", "get_displays", "get_screen_size",
- "get_window_state", "list_apps", "list_windows",
- })
- # A timed-out MCP session is wedged for later calls, so it is recreated before the
- # next non-lifecycle call_tool. Class-level default: tests that bypass __init__ see healthy.
+ # Idempotent reads, safe to replay after a broken transport. Mutations stay out: a lost response does not
+ # prove they failed.
+ _TRANSPORT_REPLAY_SAFE_TOOLS = frozenset({"get_cursor_position", "get_displays", "get_screen_size",
+ "get_window_state", "list_apps", "list_windows"})
+ # A timed-out MCP session is wedged for later calls, so it is recreated before the next non-lifecycle
+ # call_tool. Class-level default: tests that bypass __init__ see healthy.
_timeout_suspect = False
def __init__(self, bridge: _AsyncBridge, embedded_daemon: Optional[Any] = None) -> None:
- self._bridge, self._embedded_daemon = bridge, embedded_daemon
- self._session = None
- self._lock = threading.Lock()
- self._started = False
- # Per-tool capability-token sets from `tools/list` (read via supports_capability).
+ self._bridge, self._embedded_daemon, self._session = bridge, embedded_daemon, None
+ self._lock, self._started = threading.Lock(), False
+ # Per-tool capability-token sets from `tools/list` (read via supports_capability). Raw input schemas are
+ # the source of truth for action properties: 0.9-era drivers advertise delivery_mode in inputSchema
+ # without the ``input.delivery_mode`` token.
self._capabilities: Dict[str, set] = {}
- # Raw input schemas are the source of truth for action properties: 0.9-era drivers
- # advertise delivery_mode in inputSchema without the ``input.delivery_mode`` token.
self._tool_schemas: Dict[str, Dict[str, Any]] = {}
- self._capability_version: str = ""
- self._ready_event = threading.Event()
+ self._capability_version, self._ready_event = "", threading.Event()
self._shutdown_event: Optional[asyncio.Event] = None # created on bridge loop
self._lifecycle_future = None # concurrent.futures.Future
self._setup_error: Optional[BaseException] = None
@@ -205,8 +187,7 @@ class _CuaDriverSession:
self._capabilities, self._tool_schemas, self._capability_version = {}, {}, ""
async def _lifecycle_coro(self) -> None:
- """Owns the stdio MCP contexts: open, signal ready, block on shutdown, clean up —
- all in one task (see class docstring)."""
+ """Owns the stdio MCP contexts: open, signal ready, block on shutdown, clean up — all in one task."""
import time as _time
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
@@ -228,8 +209,7 @@ class _CuaDriverSession:
else (_cb._resolve_mcp_invocation(driver_cmd), _cb.cua_driver_child_env()))
_t_manifest = _time.monotonic()
# Telemetry policy first (default: disabled), then strip Hermes secrets.
- params = StdioServerParameters(command=command, args=args,
- env=_sanitize_subprocess_env(child_env))
+ params = StdioServerParameters(command=command, args=args, env=_sanitize_subprocess_env(child_env))
async with stdio_client(params) as (read, write):
self._startup_phase = "mcp-initialize"
async with ClientSession(read, write) as session:
@@ -238,8 +218,7 @@ class _CuaDriverSession:
# Capabilities BEFORE exposing the session: the first call sees them.
self._startup_phase = "capability-discovery"
await self._populate_capabilities(session)
- self._session = session
- self._startup_phase = "ready"
+ self._session, self._startup_phase = session, "ready"
self._ready_event.set()
logger.info("cua-driver session ready in %.1fs (manifest=%.1fs, mcp_init=%.1fs)",
_time.monotonic() - _t0, _t_manifest - _t0, _t_init - _t_manifest)
@@ -250,14 +229,13 @@ class _CuaDriverSession:
self._ready_event.set()
raise
finally:
- self._session = None
- # A session that dies for ANY reason must be re-enterable: the next call
- # sees _started False and rebuilds. Atomic bool write — stop() may hold _lock.
- self._started = False
+ # A session that dies for ANY reason must be re-enterable: the next call sees _started False and
+ # rebuilds. Atomic bool write — stop() may hold _lock.
+ self._session, self._started = None, False
async def _populate_capabilities(self, session: Any) -> None:
- """Cache per-tool capability sets, input schemas and capability_version from
- tools/list. Soft prerequisite: on failure the map stays empty (capability False)."""
+ """Cache per-tool capability sets, input schemas and capability_version from tools/list. Soft
+ prerequisite: on failure the map stays empty (capability False)."""
self._reset_capability_state()
try:
tools_list = await session.list_tools()
@@ -265,9 +243,9 @@ class _CuaDriverSession:
tool_name = getattr(tool, "name", None)
if not isinstance(tool_name, str):
continue
- caps = _tool_field(tool, "capabilities")
- self._capabilities[tool_name] = {c for c in caps if isinstance(c, str)} if isinstance(caps, list) else set()
- schema = _tool_field(tool, "input_schema", "inputSchema")
+ caps, schema = _tool_field(tool, "capabilities"), _tool_field(tool, "input_schema", "inputSchema")
+ self._capabilities[tool_name] = (
+ {c for c in caps if isinstance(c, str)} if isinstance(caps, list) else set())
self._tool_schemas[tool_name] = dict(schema) if isinstance(schema, dict) else {}
# capability_version is a sibling of `tools` in tools/list (NOT in initialize).
cv = _tool_field(tools_list, "capability_version")
@@ -278,11 +256,10 @@ class _CuaDriverSession:
def start(self) -> None:
with self._lock:
- if self._started:
- return
- self._bridge.start()
- self._start_lifecycle_locked()
- self._started = True
+ if not self._started:
+ self._bridge.start()
+ self._start_lifecycle_locked()
+ self._started = True
def _start_lifecycle_locked(self) -> None:
"""Spawn the lifecycle owner and wait for ready. Caller holds self._lock."""
@@ -295,11 +272,10 @@ class _CuaDriverSession:
self._lifecycle_future = asyncio.run_coroutine_threadsafe(self._lifecycle_coro(), loop)
if not self._ready_event.wait(timeout=30.0):
self._signal_shutdown_locked()
- phase = getattr(self, "_startup_phase", "unknown")
from hermes_constants import display_hermes_home
raise RuntimeError(
- f"cua-driver session never reached ready (timeout 30s; stuck in phase: {phase}). "
- "Run `hermes computer-use doctor` and check "
+ f"cua-driver session never reached ready (timeout 30s; stuck in phase: "
+ f"{getattr(self, '_startup_phase', 'unknown')}). Run `hermes computer-use doctor` and check "
f"{display_hermes_home()}/logs/agent.log for the phase timings.")
if self._setup_error is not None:
raise RuntimeError(f"cua-driver session setup failed: {self._setup_error}") from self._setup_error
@@ -309,10 +285,9 @@ class _CuaDriverSession:
def stop(self) -> None:
with self._lock:
- if not self._started:
- return
- self._started = False
- self._stop_lifecycle_locked()
+ if self._started:
+ self._started = False
+ self._stop_lifecycle_locked()
def set_transport_reset_callback(self, callback: Any) -> None:
"""Register a synchronous cache invalidation hook for transport swaps."""
@@ -357,8 +332,8 @@ class _CuaDriverSession:
return any(capability in c for c in caps)
def _has_tool(self, name: str) -> bool:
- """``tools/list`` advertised *name*. Routes capture() (PNG capture moved into
- ``get_window_state``). False before discovery — callers treat that as "unknown"."""
+ """``tools/list`` advertised *name*. Routes capture() (PNG capture moved into ``get_window_state``).
+ False before discovery — callers treat that as "unknown"."""
return name in self._capabilities
def supports_input_property(self, tool: str, property_name: str) -> bool:
@@ -410,10 +385,9 @@ class _CuaDriverSession:
@staticmethod
def _is_transient_daemon_error(exc: Exception) -> bool:
- """Daemon-proxy EAGAIN congestion: on macOS the ``cua-driver mcp`` bridge uses a
- non-blocking unix socket and heavy ops (``get_window_state``) fail with ``os error
- 35`` when its buffer is full. A retry succeeds, so back off / fall back instead
- of surfacing an empty 0x0 capture."""
+ """Daemon-proxy EAGAIN congestion: on macOS the ``cua-driver mcp`` bridge uses a non-blocking unix socket
+ and heavy ops (``get_window_state``) fail with ``os error 35`` when its buffer is full. A retry succeeds,
+ so back off / fall back instead of surfacing an empty 0x0 capture."""
msg = str(exc)
return any(needle in msg for needle in ("Resource temporarily unavailable", "os error 35",
"daemon transport error", "daemon proxy"))
@@ -436,19 +410,13 @@ class _CuaDriverSession:
result = self._run_call("start_session", {"session": session_id}, timeout)
if result.get("isError") is True:
logger.warning(failure_msg, session_id, self._logical_error_text(result))
- return False
- return True
-
- def _restore_declared_session_after_transport_reset(self, timeout: float) -> None:
- """Re-attach the public label inside a replacement private lifecycle."""
- if getattr(self, "_declared_session_id", None):
- self._redeclare_session(timeout, "cua-driver public session label %s could not be restored: %s")
+ return result.get("isError") is not True
def _recreate_session(self, name: str, timeout: float, log_msg: str, *, restart: bool = True,
clear_timeout_suspect: bool = False) -> None:
- """Log *log_msg* (``%s`` = *name*), then either start() a dead session or (``restart``)
- tear down and rebuild the MCP lifecycle under ``_lock`` with capabilities repopulated
- from scratch; finally re-attach the declared public label."""
+ """Log *log_msg* (``%s`` = *name*), then either start() a dead session or (``restart``) tear
+ down and rebuild the MCP lifecycle under ``_lock`` with capabilities repopulated from scratch;
+ finally re-attach the declared public label inside the replacement private lifecycle."""
logger.warning(log_msg, name)
if not restart:
self.start()
@@ -465,21 +433,20 @@ class _CuaDriverSession:
self._started = True
if clear_timeout_suspect:
self._timeout_suspect = False
- self._restore_declared_session_after_transport_reset(timeout)
+ if getattr(self, "_declared_session_id", None):
+ self._redeclare_session(timeout, "cua-driver public session label %s could not be restored: %s")
def _call_tool_via_cli(self, name: str, args: Dict[str, Any], timeout: float) -> Dict[str, Any]:
- """Fallback transport: ``cua-driver call <tool> <json>`` subprocess. The MCP stdio bridge
- can persistently fail heavy calls (``get_window_state``) with EAGAIN while the plain CLI,
- on its own daemon socket, keeps working. Output is remapped to the ``_extract_tool_result``
- shape. ``get_window_state`` routes its screenshot to a temp file (``screenshot_out_file``)
- so the daemon returns a tiny JSON body, not the multi-megabyte base64 blob that congests
- the socket; ``_cli_result`` reads it back."""
+ """Fallback transport: ``cua-driver call <tool> <json>`` subprocess. The MCP stdio bridge can persistently
+ fail heavy calls (``get_window_state``) with EAGAIN while the plain CLI, on its own daemon socket, keeps
+ working. Output is remapped to the ``_extract_tool_result`` shape. ``get_window_state`` routes its
+ screenshot to a temp file (``screenshot_out_file``) so the daemon returns a tiny JSON body, not the
+ multi-megabyte base64 blob that congests the socket; ``_cli_result`` reads it back."""
import tempfile as _tempfile
from tools.computer_use import cua_backend as _cb
from tools.environments.local import _sanitize_subprocess_env
- call_args = dict(args)
- shot_file: Optional[str] = None
+ call_args, shot_file = dict(args), None
if name == "get_window_state" and "screenshot_out_file" not in call_args:
fd, shot_file = _tempfile.mkstemp(prefix="cua_shot_", suffix=".png")
os.close(fd)
@@ -501,9 +468,9 @@ class _CuaDriverSession:
os.remove(shot_file)
def call_tool(self, name: str, args: Dict[str, Any], timeout: float = 30.0) -> Dict[str, Any]:
- # A prior MCP timeout marks the session suspect (possibly wedged): recreate it
- # so one timeout never poisons the run. Healthy sessions are never restarted here.
if name not in self._LIFECYCLE_CALLS:
+ # A prior MCP timeout marks the session suspect (possibly wedged): recreate it so one timeout never
+ # poisons the run. Healthy sessions are never restarted here.
if self._timeout_suspect:
self._recreate_session(
name, timeout, "cua-driver session suspect after earlier MCP timeout; recreating before %s",