Skip to content

The agent that grows with you

fix(agent): a JSON-object tool result is judged by its top-level shape, not by a substring scan

riesvriend32fe390d6dc2
diff --git a/agent/display.py b/agent/display.py
index 01d58ab3087..191470cac0a 100644
--- a/agent/display.py
+++ b/agent/display.py
@@ -1048,6 +1048,10 @@ def _detect_tool_failure(tool_name: str, result: Any) -> tuple[bool, str]:
err = data.get("error") or data.get("message")
if err and (failed or "error" in data):
return True, f" [{_trim_error(str(err))}]"
+ # A JSON object is judged by its top-level shape only. An ``error`` key nested
+ # inside a success payload is data the tool read (a page's own validation
+ # message inside browser_cdp's ``result``), not a failure of the call.
+ return False, ""
# Multimodal results (dicts) are successes; failures arrive as JSON-encoded strings.
if isinstance(result, str) and has_failure_marker(result):
return True, t("display.failure.generic")
diff --git a/agent/tool_guardrails.py b/agent/tool_guardrails.py
index 1f99155424b..0328cb84dd7 100644
--- a/agent/tool_guardrails.py
+++ b/agent/tool_guardrails.py
@@ -233,10 +233,15 @@ def classify_tool_failure(tool_name: str, result: str | None) -> tuple[bool, str
exit_code = data.get("exit_code") if isinstance(data, dict) else None
return (True, f" [exit {exit_code}]") if exit_code is not None and exit_code != 0 else (False, "")
- if tool_name == "memory":
- data = safe_json_loads(result)
- if isinstance(data, dict) and data.get("success") is False and "exceed the limit" in data.get("error", ""):
+ data = safe_json_loads(result)
+ if isinstance(data, dict):
+ failed = data.get("success") is False
+ if tool_name == "memory" and failed and "exceed the limit" in data.get("error", ""):
return True, " [full]"
+ # Same rule as _detect_tool_failure: a JSON object is judged by its top-level
+ # shape, never by a substring scan of its nested content.
+ err = data.get("error") or data.get("message")
+ return (True, " [error]") if err and (failed or "error" in data) else (False, "")
return (True, " [error]") if has_failure_marker(result) else (False, "")
diff --git a/tests/agent/test_display_tool_failure.py b/tests/agent/test_display_tool_failure.py
index 5c4e71346aa..b2fbd57dd5d 100644
--- a/tests/agent/test_display_tool_failure.py
+++ b/tests/agent/test_display_tool_failure.py
@@ -154,6 +154,35 @@ class TestDetectToolFailureStructured:
+ def test_nested_error_inside_a_success_object_is_not_a_failure(self):
+ # browser_cdp wraps a raw CDP result as {"success": True, "method", "result"}.
+ # A Runtime.evaluate that reads a page's own validation object puts that
+ # object's "error" key inside "result": data the tool read, not a failure.
+ result = json.dumps({
+ "success": True,
+ "method": "Runtime.evaluate",
+ "result": {"result": {"type": "object",
+ "value": {"error": ["Vul een geldig telefoonnummer in."]}}},
+ })
+ assert _detect_tool_failure("browser_cdp", result) == (False, "")
+
+ def test_success_object_with_error_word_in_a_nested_string_is_not_a_failure(self):
+ result = json.dumps({"success": True, "rows": [{"ok": False, "error": "matched 0 elements"}]})
+ assert _detect_tool_failure("browser_fill_form", result) == (False, "")
+
+ def test_top_level_error_without_success_flag_is_still_a_failure(self):
+ result = json.dumps({"error": "CDP call timed out after 30.0s", "method": "Runtime.evaluate"})
+ is_failure, suffix = _detect_tool_failure("browser_cdp", result)
+ assert is_failure is True
+ assert "timed out" in suffix
+
+ def test_plain_text_failures_still_trip_the_substring_scan(self):
+ # The substring scan is for results that are not a JSON object.
+ assert _detect_tool_failure("web_search", "Error executing tool 'web_search': boom") == (True, " [error]")
+ assert _detect_tool_failure("web_search", 'request "failed" upstream') == (True, " [error]")
+ assert _detect_tool_failure("web_search", "all good") == (False, "")
+
+
class TestGetCuteToolMessageFailureSuffix:
"""End-to-end: failure suffix is appended by get_cute_tool_message."""
diff --git a/tests/agent/test_tool_guardrails.py b/tests/agent/test_tool_guardrails.py
index 430fdb5e935..f6153c777ce 100644
--- a/tests/agent/test_tool_guardrails.py
+++ b/tests/agent/test_tool_guardrails.py
@@ -478,3 +478,26 @@ def test_execute_code_replay_streak_notice_fires_on_warn_only_desktop_config():
assert notices[:2] == [None, None]
assert all(n is not None and "consecutive identical call to execute_code" in n for n in notices[2:]), notices
assert controller.halt_decision is None, "warn-only surfaces must not halt"
+
+
+def test_classify_tool_failure_judges_a_json_object_by_its_top_level_shape():
+ # Mirrors agent.display._detect_tool_failure: an "error" key nested inside a
+ # success payload (browser_cdp's wrapped Runtime.evaluate result) is not a failure.
+ nested = json.dumps({
+ "success": True,
+ "method": "Runtime.evaluate",
+ "result": {"result": {"type": "object", "value": {"error": ["Vul een geldig telefoonnummer in."]}}},
+ })
+ assert classify_tool_failure("browser_cdp", nested) == (False, "")
+ assert classify_tool_failure("browser_cdp", json.dumps({"error": "CDP call timed out"})) == (True, " [error]")
+ assert classify_tool_failure("read_file", json.dumps({"success": False, "error": "File not found"})) == (True, " [error]")
+ assert classify_tool_failure("memory", json.dumps({"success": False, "error": "would exceed the limit"})) == (True, " [full]")
+ assert classify_tool_failure("web_search", json.dumps({"success": True, "data": "hello"})) == (False, "")
+
+
+def test_classify_tool_failure_keeps_the_substring_scan_for_plain_text():
+ assert classify_tool_failure("web_search", "Error executing tool 'web_search': boom") == (True, " [error]")
+ assert classify_tool_failure("web_search", 'request "failed" upstream') == (True, " [error]")
+ assert classify_tool_failure("web_search", "all good") == (False, "")
+ assert classify_tool_failure("terminal", json.dumps({"output": "x", "exit_code": 2})) == (True, " [exit 2]")
+ assert classify_tool_failure("terminal", json.dumps({"output": "x", "exit_code": 0})) == (False, "")