The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): keep truncated search_files output pure JSON
liuhao1024478e66475b89
diff --git a/tests/tools/test_file_tools.py b/tests/tools/test_file_tools.pyindex 48f22f381df..22bac533741 100644--- a/tests/tools/test_file_tools.py+++ b/tests/tools/test_file_tools.py@@ -420,8 +420,10 @@ class TestSearchHints:from tools.file_tools import search_toolraw = search_tool(pattern="foo", offset=0, limit=50)- assert "[Hint:" in raw- assert "offset=50" in raw+ # The hint rides inside the payload as a structured field — the tool+ # result must stay pure JSON (#90322).+ parsed = json.loads(raw)+ assert "offset=50" in parsed["_hint"]@patch("tools.file_tools._get_file_ops")@@ -438,8 +440,8 @@ class TestSearchHints:from tools.file_tools import search_toolraw = search_tool(pattern="foo", offset=50, limit=50)- assert "[Hint:" in raw- assert "offset=100" in raw+ parsed = json.loads(raw)+ assert "offset=100" in parsed["_hint"]# ---------------------------------------------------------------------------diff --git a/tests/tools/test_search_truncation_json.py b/tests/tools/test_search_truncation_json.pynew file mode 100644index 00000000000..7b0aa68c877--- /dev/null+++ b/tests/tools/test_search_truncation_json.py@@ -0,0 +1,64 @@+"""Regression tests for search_files truncation output staying pure JSON (#90322).++The truncated-results hint used to be appended as plain text after the+serialized JSON payload (``{...}\\n\\n[Hint: ...]``), so the tool result was+no longer parseable JSON — downstream tool-message handling on providers+strict about tool-content formatting could reject or mishandle it. The hint+now rides inside the payload as a structured ``_hint`` field, matching the+existing ``_omitted``/``_warning`` side-channel convention in the same+function.+"""++import json+from unittest.mock import MagicMock, patch++import pytest+++class _FakeSearchResult:+ """Minimal stand-in for FileOperations.search return value."""++ def __init__(self, truncated=False):+ self.matches = []+ self._truncated = truncated++ def to_dict(self, densify=False):+ payload = {"matches": [{"file": "test.py", "line": 1, "text": "match"}]}+ if self._truncated:+ payload["truncated"] = True+ payload["total_count"] = 20+ return payload+++def _make_fake_file_ops(truncated):+ fake = MagicMock()+ fake.search = lambda **kw: _FakeSearchResult(truncated=truncated)+ return fake+++class TestSearchTruncationStaysJson:+ @patch("tools.file_tools._get_file_ops", return_value=_make_fake_file_ops(True))+ def test_truncated_result_is_parseable_json(self, _mock_ops):+ """The whole tool output must round-trip through json.loads — no+ text appended after the serialized payload."""+ from tools.file_tools import search_tool++ raw = search_tool("def main", offset=0, limit=20, task_id="t1")+ parsed = json.loads(raw) # raises on main: trailing "[Hint: ...]" text+ assert parsed["truncated"] is True++ @patch("tools.file_tools._get_file_ops", return_value=_make_fake_file_ops(True))+ def test_truncation_hint_is_a_structured_field(self, _mock_ops):+ from tools.file_tools import search_tool++ parsed = json.loads(search_tool("def main", offset=0, limit=20, task_id="t1"))+ assert "_hint" in parsed+ assert "offset=20" in parsed["_hint"]++ @patch("tools.file_tools._get_file_ops", return_value=_make_fake_file_ops(False))+ def test_untruncated_result_has_no_hint(self, _mock_ops):+ from tools.file_tools import search_tool++ parsed = json.loads(search_tool("def main", task_id="t1"))+ assert "truncated" not in parsed+ assert "_hint" not in parseddiff --git a/tools/file_tools.py b/tools/file_tools.pyindex 38d210b8b87..938d83fa28a 100644--- a/tools/file_tools.py+++ b/tools/file_tools.py@@ -944,11 +944,15 @@ def search_tool(pattern: str, target: str = "content", path: str = ".",f"You have run this exact search {count} times consecutively. ""The results have not changed. Use the information you already have.")- result_json = json.dumps(result_dict, ensure_ascii=False)+ # Structured like ``_warning`` above: text appended after the JSON+ # breaks every json.loads consumer (execute_code RPC, strict tool-message+ # providers) — #90322.if result_dict.get("truncated"):- next_offset = offset + limit- result_json += f"\n\n[Hint: Results truncated. Use offset={next_offset} to see more, or narrow with a more specific pattern or file_glob.]"- return result_json+ result_dict["_hint"] = (+ f"Results truncated. Use offset={offset + limit} to see more, "+ "or narrow with a more specific pattern or file_glob."+ )+ return json.dumps(result_dict, ensure_ascii=False)except Exception as e:return tool_error(str(e))