The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(computer_use): the backend reports the AX-walk bound it actually sent
kshitijk4poor76c3b0c7351a
diff --git a/tests/tools/test_computer_use_ax_walk_bound.py b/tests/tools/test_computer_use_ax_walk_bound.pyindex 70171d24553..cff22cc87b6 100644--- a/tests/tools/test_computer_use_ax_walk_bound.py+++ b/tests/tools/test_computer_use_ax_walk_bound.py@@ -42,7 +42,10 @@ class TestAxWalkBound:_write_config(tmp_path, "computer_use:\n ax_max_elements: 350\n")monkeypatch.setenv("HERMES_HOME", str(tmp_path))assert cua_backend._cua_configured_ax_max_elements() == 350- assert _StubCapture()._gws_args()["max_elements"] == 350+ stub = _StubCapture()+ assert stub._gws_args()["max_elements"] == 350+ # the backend records the bound it sent, so CaptureResult.ax_max_elements can report it+ assert stub._ax_max_elements_sent == 350def test_zero_disables_the_bound(self, tmp_path, monkeypatch):"""0 restores the driver default and must not leak a ``max_elements`` key into the payload."""@@ -59,9 +62,8 @@ class TestCappedWalkHint:def _summary(monkeypatch, n_elements: int, bound: int) -> str:from tools.computer_use import toolfrom tools.computer_use.backend import CaptureResult, UIElement- monkeypatch.setattr(cua_backend, "_cua_configured_ax_max_elements", lambda: bound)monkeypatch.setattr(tool, "_spill_elements_to_file", lambda cap: "/tmp/elements.json")- cap = CaptureResult(mode="ax", width=800, height=600,+ cap = CaptureResult(mode="ax", width=800, height=600, ax_max_elements=bound,elements=[UIElement(index=i + 1, role="AXButton", label=f"b{i}") for i in range(n_elements)])return "\n".join(tool._capture_summary_lines(tool._capture_view(cap, max_elements=5)))diff --git a/tools/computer_use/backend.py b/tools/computer_use/backend.pyindex 50be4d9c2aa..77666ce0e38 100644--- a/tools/computer_use/backend.py+++ b/tools/computer_use/backend.py@@ -80,6 +80,9 @@ class CaptureResult:# Guidance appended to the summary by capture lanes that intentionally return no elements (e.g.# full-screen composited grabs) to point the model at an interactive lane.note: str = ""+ # ``max_elements`` the backend asked the driver's AX walk to stop at (0 = unbounded / not applicable);+ # ``len(elements) >= ax_max_elements > 0`` means the tree may be truncated.+ ax_max_elements: int = 0@dataclassdiff --git a/tools/computer_use/cua_backend_capture.py b/tools/computer_use/cua_backend_capture.pyindex b027966d996..a68a2a9954c 100644--- a/tools/computer_use/cua_backend_capture.py+++ b/tools/computer_use/cua_backend_capture.py@@ -243,6 +243,7 @@ class _CaptureMixin:with contextlib.suppress(Exception): # lazy import: cua_backend imports this module at import timefrom tools.computer_use import cua_backend as _cbcapped = _cb._cua_configured_ax_max_elements()+ self._ax_max_elements_sent = cappedif capped:args["max_elements"] = cappedreturn args@@ -317,7 +318,8 @@ class _CaptureMixin:self._capture_vision() if mode == "vision" else self._capture_window_state())png_bytes_len, width, height = _png_metrics(png_b64, 0, 0) if png_b64 else (0, 0, 0)return CaptureResult(mode=mode, width=width, height=height, png_b64=png_b64, elements=elements, app=app_name,- window_title=window_title, png_bytes_len=png_bytes_len, image_mime_type=image_mime_type)+ window_title=window_title, png_bytes_len=png_bytes_len, image_mime_type=image_mime_type,+ ax_max_elements=0 if mode == "vision" else getattr(self, "_ax_max_elements_sent", 0))def _capture_full_screen(self, mode: str) -> CaptureResult:"""Composited PrtScn-style grab via `get_desktop_state` (the shell window would only show wallpaper + icons).diff --git a/tools/computer_use/tool.py b/tools/computer_use/tool.pyindex b9f9660a3af..1b395f0a5d9 100644--- a/tools/computer_use/tool.py+++ b/tools/computer_use/tool.py@@ -551,24 +551,16 @@ def _capture_view(cap: CaptureResult, max_elements: int) -> SimpleNamespace:lost_detail = len(cap.elements) > len(visible) or any(len(e.label) > _MAX_ELEMENT_LABEL_CHARS for e in visible)too_small = bool(dims) and min(dims) < _MIN_PROVIDER_IMAGE_DIMENSIONhas_image = bool(cap.png_b64) and cap.mode != "ax" and not too_small- # The driver's own AX walk may have stopped at ``computer_use.ax_max_elements``: then the spill file is NOT- # the full tree, and the hint must not promise one.- ax_bound = _ax_walk_bound()- ax_capped = ax_bound > 0 and len(cap.elements) >= ax_bound+ # The driver's own AX walk may have stopped at the ``max_elements`` the backend sent: then the spill file is+ # NOT the full tree, and the hint must not promise one.+ ax_capped = len(cap.elements) >= cap.ax_max_elements > 0return SimpleNamespace(cap=cap, visible=visible, total=len(cap.elements), width=width, height=height,truncated=len(cap.elements) - len(visible), bounds_scale=scale, bounds_note=note,- ax_capped=ax_bound if ax_capped else 0,+ ax_capped=cap.ax_max_elements if ax_capped else 0,elements_file=_spill_elements_to_file(cap) if lost_detail else None,screenshot_path=_persist_capture_image(cap) if has_image else None,dims_omitted=dims if too_small else None, has_image=has_image)-def _ax_walk_bound() -> int:- """``computer_use.ax_max_elements`` as the driver saw it (0 = unbounded / unknown)."""- with contextlib.suppress(Exception):- from tools.computer_use.cua_backend import _cua_configured_ax_max_elements- return int(_cua_configured_ax_max_elements())- return 0-def _capture_summary_lines(v: SimpleNamespace) -> List[str]:"""Human-readable capture summary; line ORDER is contract. Lists only what `elements` surfaces, otherwise thesummary names indices the model can't find."""@@ -577,10 +569,10 @@ def _capture_summary_lines(v: SimpleNamespace) -> List[str]:f"{v.bounds_scale} ≈ native coordinate)" if v.bounds_scale else ""),v.screenshot_path and f"shareable screenshot saved to {v.screenshot_path}",v.cap.note,- v.elements_file and (f"{'' if getattr(v, 'ax_capped', 0) else 'full '}element tree with untruncated labels "+ v.elements_file and (f"{'' if v.ax_capped else 'full '}element tree with untruncated labels "f"saved to {v.elements_file} — read_file/search_files it if you need dropped label ""text or elements beyond the cap"),- getattr(v, "ax_capped", 0) and (f"accessibility walk capped at {v.ax_capped} elements; pass app= to narrow"),+ v.ax_capped and (f"accessibility walk capped at {v.ax_capped} elements; pass app= to narrow"),)return [f"capture mode={v.cap.mode} {v.width}x{v.height}"