fix(agent): add tool-call loop guardrails

This commit is contained in:
Mind-Dragon 2026-04-27 16:29:19 +02:00 committed by Teknium
parent 8d7500d80d
commit 58b89965c8
5 changed files with 944 additions and 108 deletions

View file

@ -14,6 +14,7 @@ from difflib import unified_diff
from pathlib import Path
from utils import safe_json_loads
from agent.tool_guardrails import classify_tool_failure
# ANSI escape codes for coloring tool failure indicators
_RED = "\033[31m"
@ -808,30 +809,7 @@ def _detect_tool_failure(tool_name: str, result: str | None) -> tuple[bool, str]
like ``" [exit 1]"`` for terminal failures, or ``" [error]"`` for generic
failures. On success, returns ``(False, "")``.
"""
if result is None:
return False, ""
if tool_name == "terminal":
data = safe_json_loads(result)
if isinstance(data, dict):
exit_code = data.get("exit_code")
if exit_code is not None and exit_code != 0:
return True, f" [exit {exit_code}]"
return False, ""
# Memory-specific: distinguish "full" from real errors
if tool_name == "memory":
data = safe_json_loads(result)
if isinstance(data, dict):
if data.get("success") is False and "exceed the limit" in data.get("error", ""):
return True, " [full]"
# Generic heuristic for non-terminal tools
lower = result[:500].lower()
if '"error"' in lower or '"failed"' in lower or result.startswith("Error"):
return True, " [error]"
return False, ""
return classify_tool_failure(tool_name, result)
def get_cute_tool_message(