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

+5 -13
View File
@@ -13,14 +13,13 @@ from loguru import logger
from nanobot.agent.hook import AgentHook, AgentHookContext
from nanobot.agent.tools.context import tool_log_content_allowed
from nanobot.agent.tools.file_state import file_read_context
from nanobot.agent.tools.registry import ToolRegistry, is_tool_error_result
from nanobot.agent.tools.registry import ToolRegistry, is_tool_error_result, with_tool_error_hint
from nanobot.providers.base import ToolCallRequest
from nanobot.utils.runtime import (
repeated_external_lookup_error,
repeated_workspace_violation_error,
)
_RETRY_HINT = "\n\n[Analyze the error above and try a different approach.]"
# SSRF is a hard security block at the tool boundary, but the agent turn
# should recover conversationally instead of aborting the runtime.
_SSRF_MARKERS: tuple[str, ...] = (
@@ -47,13 +46,6 @@ _WORKSPACE_VIOLATION_MARKERS: tuple[str, ...] = (
)
def _with_retry_hint(payload: str) -> str:
"""Append the recovery hint exactly once."""
if payload.endswith(_RETRY_HINT):
return payload
return payload + _RETRY_HINT
async def execute_tool_calls(
tools: ToolRegistry,
tool_calls: list[ToolCallRequest],
@@ -132,7 +124,7 @@ async def _execute_tool_call(
"status": "error",
"detail": "repeated external lookup blocked",
}
return _with_retry_hint(lookup_error), event
return with_tool_error_hint(lookup_error), event
prepare_call = cast(
Callable[[str, Any], object] | None,
@@ -146,7 +138,7 @@ async def _execute_tool_call(
if len(prepared_tuple) == 3:
tool, params, prep_error = cast(tuple[Any, Any, str | None], prepared_tuple)
if prep_error:
payload = _with_retry_hint(prep_error)
payload = with_tool_error_hint(prep_error)
event = {
"name": tool_call.name,
"status": "error",
@@ -182,7 +174,7 @@ async def _execute_tool_call(
"status": "error",
"detail": str(exc),
}
payload = _with_retry_hint(f"Error: {type(exc).__name__}: {exc}")
payload = with_tool_error_hint(f"Error: {type(exc).__name__}: {exc}")
handled = _classify_violation(
raw_text=str(exc),
soft_payload=payload,
@@ -196,7 +188,7 @@ async def _execute_tool_call(
if is_tool_error_result(result):
await hook.on_execute_tool_error(context, tool_call, tool, params, result)
payload = _with_retry_hint(result)
payload = with_tool_error_hint(result)
event = {
"name": tool_call.name,
"status": "error",
+15 -4
View File
@@ -11,6 +11,18 @@ from nanobot.agent.tools.context import ContextAware, current_request_context
if TYPE_CHECKING:
from nanobot.runtime_context import RuntimeContextProvider
_ERROR_HINT = (
"\n\n[Consider this result in the context of the user's goal "
"and decide whether and how to proceed.]"
)
def with_tool_error_hint(payload: str) -> str:
"""Append tool error guidance exactly once."""
if payload.endswith(_ERROR_HINT):
return payload
return payload + _ERROR_HINT
def is_tool_error_result(result: Any) -> bool:
return isinstance(result, ToolResult) and result.is_error
@@ -186,19 +198,18 @@ class ToolRegistry:
async def execute(self, name: str, params: Any) -> Any:
"""Execute a tool by name with given parameters."""
hint = "\n\n[Analyze the error above and try a different approach.]"
tool, params, error = self.prepare_call(name, params)
if error:
return ToolResult.error(str(error) + hint)
return ToolResult.error(with_tool_error_hint(str(error)))
try:
assert tool is not None # guarded by prepare_call()
result = await tool.execute(**params)
if is_tool_error_result(result):
return ToolResult.error(str(result) + hint)
return ToolResult.error(with_tool_error_hint(str(result)))
return result
except Exception as e:
return ToolResult.error(f"Error executing {name}: {str(e)}" + hint)
return ToolResult.error(with_tool_error_hint(f"Error executing {name}: {str(e)}"))
@property
def tool_names(self) -> list[str]: