The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): bound the truncation log so it stops re-dumping the full body
Erosika84bc43007309
diff --git a/tests/tools/test_registry.py b/tests/tools/test_registry.pyindex bc1ea0f424f..6cefab0fc7b 100644--- a/tests/tools/test_registry.py+++ b/tests/tools/test_registry.py@@ -8,6 +8,7 @@ from unittest.mock import patchfrom tools.registry import (ToolRegistry,+ _MAX_LOGGED_ERROR_CHARS,_MAX_TOOL_ERROR_CHARS,_module_registers_tools,discover_builtin_tools,@@ -164,12 +165,23 @@ class TestToolErrorBounding:result = json.loads(tool_error(msg))assert result["error"] == msg- def test_full_body_preserved_in_logs_when_truncated(self, caplog):+ def test_longer_prefix_reaches_logs_than_context(self, caplog):import loggingbody = "boom: " + "Z" * 5000+ with caplog.at_level(logging.DEBUG, logger="tools.registry"):+ result = json.loads(tool_error(body))+ logged = "\n".join(rec.getMessage() for rec in caplog.records)+ assert body[:5000] in logged+ assert len(result["error"]) < 5000++ def test_log_line_is_bounded_for_huge_bodies(self, caplog):+ import logging+ body = "boom: " + "Z" * 500_000with caplog.at_level(logging.DEBUG, logger="tools.registry"):json.loads(tool_error(body))- assert any(body in rec.getMessage() for rec in caplog.records)+ for record in caplog.records:+ assert len(record.getMessage()) < _MAX_LOGGED_ERROR_CHARS + 200+ assert body not in "\n".join(r.getMessage() for r in caplog.records)class TestDispatchBoundsDirectErrorResults:diff --git a/tools/registry.py b/tools/registry.pyindex 0684cb4da0b..333922d2d0f 100644--- a/tools/registry.py+++ b/tools/registry.py@@ -29,13 +29,19 @@ logger = logging.getLogger(__name__)# Cap on a tool error body; only trims runaway interpolated exceptions (static msgs are ~115 chars)._MAX_TOOL_ERROR_CHARS = 2048_TOOL_ERROR_TRUNCATION_MARKER = "… [truncated]"+# Logs keep more of the body than the model sees, but still a bounded amount.+_MAX_LOGGED_ERROR_CHARS = 8192def _bound_error_text(text: str) -> str:- """Bound an error body destined for model context; full body stays in logs."""+ """Bound an error body destined for model context; logs keep a longer prefix."""if len(text) <= _MAX_TOOL_ERROR_CHARS:return text- logger.debug("tool error body truncated for context (%d chars): %s", len(text), text)+ logger.debug(+ "tool error body truncated for context (%d chars): %s",+ len(text),+ text[:_MAX_LOGGED_ERROR_CHARS],+ )return text[:_MAX_TOOL_ERROR_CHARS] + _TOOL_ERROR_TRUNCATION_MARKER