fix(agent): make tool error guidance neutral (#6227)

* fix(agent): make tool error guidance neutral

* refactor(tools): share tool error guidance
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-15 16:58:39 +08:00
1 parent c24153db86
commit 841d2b880c
5 files changed
+55 -33

No files matched your search

+19 -10
View File
@@ -56,16 +56,22 @@ async def test_runner_returns_tool_exception_to_model_for_recovery():
{"name": "list_dir", "status": "error", "detail": "boom"}
]
tool_message = next(message for message in result.messages if message.get("role") == "tool")
retry_hint = "[Analyze the error above and try a different approach.]"
error_hint = (
"[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
)
assert "Error: RuntimeError: boom" in tool_message["content"]
assert tool_message["content"].count(retry_hint) == 1
assert tool_message["content"].count(error_hint) == 1
@pytest.mark.asyncio
async def test_tool_execution_does_not_duplicate_existing_retry_hint():
retry_hint = "\n\n[Analyze the error above and try a different approach.]"
async def test_tool_execution_does_not_duplicate_existing_error_hint():
error_hint = (
"\n\n[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
)
tools = SimpleNamespace(
execute=AsyncMock(return_value=ToolResult.error("Error: boom" + retry_hint)),
execute=AsyncMock(return_value=ToolResult.error("Error: boom" + error_hint)),
)
results, events = await execute_tool_calls(
@@ -78,8 +84,8 @@ async def test_tool_execution_does_not_duplicate_existing_retry_hint():
context=AgentHookContext(iteration=0, messages=[]),
)
assert results == ["Error: boom" + retry_hint]
assert results[0].count(retry_hint) == 1
assert results == ["Error: boom" + error_hint]
assert results[0].count(error_hint) == 1
assert events[0]["status"] == "error"
@@ -300,15 +306,18 @@ async def test_runner_recognizes_web_fetch_errors(monkeypatch, url, ssrf):
content = tool_message["content"]
assert content.startswith(str(raw_error) + "\n\n")
assert json.JSONDecoder().raw_decode(content)[0] == raw_payload
retry_hint = "[Analyze the error above and try a different approach.]"
error_hint = (
"[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
)
if ssrf:
assert result.tool_events[0]["detail"].startswith("ssrf_violation:")
assert "non-bypassable security boundary" in content
assert "Do not retry" in content
assert "tools.ssrfWhitelist" in content
assert retry_hint not in content
assert error_hint not in content
else:
assert content.count(retry_hint) == 1
assert content.count(error_hint) == 1
@pytest.mark.asyncio
+5 -2
View File
@@ -626,7 +626,7 @@ async def test_execute_contains_malformed_success_result() -> None:
@pytest.mark.asyncio
async def test_registry_adds_retry_hint_to_malformed_mcp_result() -> None:
async def test_registry_adds_error_hint_to_malformed_mcp_result() -> None:
async def call_tool(_name: str, arguments: dict) -> object:
return SimpleNamespace(content=None)
@@ -638,7 +638,10 @@ async def test_registry_adds_retry_hint_to_malformed_mcp_result() -> None:
assert is_tool_error_result(result)
assert "MCP tool returned malformed content" in result
assert "Analyze the error above and try a different approach" in result
assert (
"[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
) in result
@pytest.mark.asyncio
+11 -4
View File
@@ -3,6 +3,8 @@ from __future__ import annotations
from typing import Any
from unittest.mock import AsyncMock
import pytest
from nanobot.agent.tools.base import Tool, ToolResult
from nanobot.agent.tools.filesystem import ReadFileTool
from nanobot.agent.tools.registry import ToolRegistry
@@ -269,7 +271,8 @@ async def test_registry_preserves_successful_exec_output_that_starts_with_error(
assert result == output
async def test_registry_uses_structured_tool_result_for_errors() -> None:
@pytest.mark.parametrize("already_hinted", [False, True])
async def test_registry_uses_structured_tool_result_for_errors(already_hinted: bool) -> None:
registry = ToolRegistry()
output = "Error: plain tool output, not a structured failure"
raw_tool = _FakeTool("raw_output")
@@ -280,16 +283,20 @@ async def test_registry_uses_structured_tool_result_for_errors() -> None:
assert raw_result == output
error_hint = (
"\n\n[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
)
error_content = "Error: real failure" + (error_hint if already_hinted else "")
failing_tool = _FakeTool("failing_tool")
failing_tool.execute = AsyncMock(return_value=ToolResult.error("Error: real failure"))
failing_tool.execute = AsyncMock(return_value=ToolResult.error(error_content))
registry.register(failing_tool)
error_result = await registry.execute("failing_tool", {})
assert isinstance(error_result, ToolResult)
assert error_result.is_error
assert error_result.startswith("Error: real failure")
assert "[Analyze the error above" in error_result
assert error_result == "Error: real failure" + error_hint
def test_get_definitions_returns_cached_result() -> None: