The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(display): a null "error" field no longer tags a successful tool as failed
Tekniumb5b4918771c0
diff --git a/agent/display.py b/agent/display.pyindex b509bb49642..01d58ab3087 100644--- a/agent/display.py+++ b/agent/display.py@@ -18,7 +18,7 @@ from urllib.parse import urlsplitfrom utils import safe_json_loadsfrom agent.i18n import tfrom agent.redact import redact_sensitive_text-from agent.tool_result_classification import file_mutation_result_landed, is_guardrail_refusal+from agent.tool_result_classification import file_mutation_result_landed, has_failure_marker, is_guardrail_refusallogger = logging.getLogger(__name__)@@ -1049,9 +1049,7 @@ def _detect_tool_failure(tool_name: str, result: Any) -> tuple[bool, str]:if err and (failed or "error" in data):return True, f" [{_trim_error(str(err))}]"# Multimodal results (dicts) are successes; failures arrive as JSON-encoded strings.- if isinstance(result, str) and (- '"error"' in result[:500].lower() or '"failed"' in result[:500].lower() or result.startswith("Error")- ):+ if isinstance(result, str) and has_failure_marker(result):return True, t("display.failure.generic")return False, ""diff --git a/agent/tool_guardrails.py b/agent/tool_guardrails.pyindex 258ca3ab929..1f99155424b 100644--- a/agent/tool_guardrails.py+++ b/agent/tool_guardrails.py@@ -14,7 +14,7 @@ from dataclasses import asdict, dataclass, field, fieldsfrom typing import Any, Mappingfrom utils import safe_json_loads-from agent.tool_result_classification import file_mutation_result_landed, is_guardrail_refusal+from agent.tool_result_classification import file_mutation_result_landed, has_failure_marker, is_guardrail_refusalIDEMPOTENT_TOOL_NAMES = frozenset({@@ -237,8 +237,7 @@ def classify_tool_failure(tool_name: str, result: str | None) -> tuple[bool, strdata = safe_json_loads(result)if isinstance(data, dict) and data.get("success") is False and "exceed the limit" in data.get("error", ""):return True, " [full]"- lower = result[:500].lower()- return (True, " [error]") if '"error"' in lower or '"failed"' in lower or result.startswith("Error") else (False, "")+ return (True, " [error]") if has_failure_marker(result) else (False, "")# Guardrail verdict text injected into the conversation, keyed by decision code.diff --git a/agent/tool_result_classification.py b/agent/tool_result_classification.pyindex 1e11b47f6de..2fb807226ca 100644--- a/agent/tool_result_classification.py+++ b/agent/tool_result_classification.py@@ -3,6 +3,7 @@from __future__ import annotationsimport json+import refrom typing import Any@@ -41,6 +42,18 @@ def is_guardrail_refusal(result: Any) -> bool:return isinstance(data, dict) and data.get(GUARDRAIL_REFUSAL_KEY) is True+# A quoted "error"/"failed" in a result's head marks it failed UNLESS it is a key whose value is+# empty: success payloads routinely carry ``"error": null`` per item (web_extract) or a+# ``"failed": 0`` count, and a bare substring test flagged every one of them.+_FAILURE_MARKER_RE = re.compile(r'"(?:error|failed)"(?!\s*:\s*(?:null|false|0|""|\[\]|\{\})\s*(?:[,}\]]|$))')+++def has_failure_marker(result: str) -> bool:+ """Last-resort text test for a failed tool result: a non-empty ``"error"``/``"failed"``+ in the first 500 chars, or a body starting with ``Error``."""+ return bool(_FAILURE_MARKER_RE.search(result[:500].lower())) or result.startswith("Error")++def file_mutation_result_landed(tool_name: str, result: Any) -> bool:"""Return True when a file mutation result proves the write landed."""if tool_name not in FILE_MUTATING_TOOL_NAMES or not isinstance(result, str):diff --git a/tests/agent/test_display_tool_failure.py b/tests/agent/test_display_tool_failure.pyindex e960d722047..38cbc726a92 100644--- a/tests/agent/test_display_tool_failure.py+++ b/tests/agent/test_display_tool_failure.py@@ -8,6 +8,7 @@ not a generic "[error]".import json+from agent.tool_guardrails import classify_tool_failurefrom agent.display import (_detect_tool_failure,_trim_error,@@ -127,6 +128,28 @@ class TestDetectToolFailureStructured:result = json.dumps({"success": True, "data": "hello"})assert _detect_tool_failure("web_search", result) == (False, "")+ def test_empty_error_and_failed_values_are_not_failures(self):+ """A successful web_extract carries ``"error": null`` per URL; batch results carry+ ``"failed": 0``. Neither is a failure, in the CLI tag or the guardrail counter."""+ ok = [+ json.dumps({"results": [{"url": "https://example.com/", "content": "hi", "error": None}]}, indent=2),+ json.dumps({"done": 3, "failed": 0}),+ json.dumps({"items": [], "errors": [], "error": ""}),+ ]+ for result in ok:+ assert _detect_tool_failure("web_extract", result) == (False, ""), result+ assert classify_tool_failure("web_extract", result) == (False, ""), result++ def test_real_error_and_failed_markers_still_flag(self):+ bad = [+ json.dumps({"results": [{"url": "https://x.test/", "content": "", "error": "HTTP 403"}]}),+ json.dumps({"status": "failed"}),+ "Error: connection refused",+ ]+ for result in bad:+ assert _detect_tool_failure("web_extract", result)[0] is True, result+ assert classify_tool_failure("web_extract", result)[0] is True, result+class TestGetCuteToolMessageFailureSuffix: