diff --git a/tools/code_execution_tool.py b/tools/code_execution_tool.py index ff5c7f7fedd..0c1d4ea650b 100644 --- a/tools/code_execution_tool.py +++ b/tools/code_execution_tool.py @@ -1344,4 +1344,5 @@ registry.register( enabled_tools=kw.get("enabled_tools")), check_fn=check_sandbox_requirements, emoji="🐍", + max_result_size_chars=30_000, ) diff --git a/tools/file_tools.py b/tools/file_tools.py index 45add116b96..3ad174612d1 100644 --- a/tools/file_tools.py +++ b/tools/file_tools.py @@ -695,6 +695,15 @@ def search_tool(pattern: str, target: str = "content", path: str = ".", ) result_json = json.dumps(result_dict, ensure_ascii=False) + # Layer 1: Cap search output to prevent context overflow + MAX_SEARCH_OUTPUT_CHARS = 50_000 + if len(result_json) > MAX_SEARCH_OUTPUT_CHARS: + result_json = ( + result_json[:MAX_SEARCH_OUTPUT_CHARS] + + f"\n\n[Search output truncated: {len(result_json):,} chars exceeded " + f"{MAX_SEARCH_OUTPUT_CHARS:,} char limit. Use a more specific pattern, " + f"file_glob, or reduce limit/context to see complete results.]" + ) # Hint when results were truncated — explicit next offset is clearer # than relying on the model to infer it from total_count vs match count. if result_dict.get("truncated"): @@ -783,7 +792,7 @@ SEARCH_FILES_SCHEMA = { "target": {"type": "string", "enum": ["content", "files"], "description": "'content' searches inside file contents, 'files' searches for files by name", "default": "content"}, "path": {"type": "string", "description": "Directory or file to search in (default: current working directory)", "default": "."}, "file_glob": {"type": "string", "description": "Filter files by pattern in grep mode (e.g., '*.py' to only search Python files)"}, - "limit": {"type": "integer", "description": "Maximum number of results to return (default: 50)", "default": 50}, + "limit": {"type": "integer", "description": "Maximum number of results to return (default: 50)", "default": 50, "maximum": 10000}, "offset": {"type": "integer", "description": "Skip first N results for pagination (default: 0)", "default": 0}, "output_mode": {"type": "string", "enum": ["content", "files_only", "count"], "description": "Output format for grep mode: 'content' shows matching lines with line numbers, 'files_only' lists file paths, 'count' shows match counts per file", "default": "content"}, "context": {"type": "integer", "description": "Number of context lines before and after each match (grep mode only)", "default": 0} @@ -822,7 +831,7 @@ def _handle_search_files(args, **kw): output_mode=args.get("output_mode", "content"), context=args.get("context", 0), task_id=tid) -registry.register(name="read_file", toolset="file", schema=READ_FILE_SCHEMA, handler=_handle_read_file, check_fn=_check_file_reqs, emoji="📖") -registry.register(name="write_file", toolset="file", schema=WRITE_FILE_SCHEMA, handler=_handle_write_file, check_fn=_check_file_reqs, emoji="✍️") -registry.register(name="patch", toolset="file", schema=PATCH_SCHEMA, handler=_handle_patch, check_fn=_check_file_reqs, emoji="🔧") -registry.register(name="search_files", toolset="file", schema=SEARCH_FILES_SCHEMA, handler=_handle_search_files, check_fn=_check_file_reqs, emoji="🔎") +registry.register(name="read_file", toolset="file", schema=READ_FILE_SCHEMA, handler=_handle_read_file, check_fn=_check_file_reqs, emoji="📖", max_result_size_chars=float('inf')) +registry.register(name="write_file", toolset="file", schema=WRITE_FILE_SCHEMA, handler=_handle_write_file, check_fn=_check_file_reqs, emoji="✍️", max_result_size_chars=100_000) +registry.register(name="patch", toolset="file", schema=PATCH_SCHEMA, handler=_handle_patch, check_fn=_check_file_reqs, emoji="🔧", max_result_size_chars=100_000) +registry.register(name="search_files", toolset="file", schema=SEARCH_FILES_SCHEMA, handler=_handle_search_files, check_fn=_check_file_reqs, emoji="🔎", max_result_size_chars=20_000) diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py index bcffe079502..c68867c394b 100644 --- a/tools/terminal_tool.py +++ b/tools/terminal_tool.py @@ -1594,4 +1594,5 @@ registry.register( handler=_handle_terminal, check_fn=check_terminal_requirements, emoji="💻", + max_result_size_chars=30_000, ) diff --git a/tools/web_tools.py b/tools/web_tools.py index 8571c2a26f6..b3f3effb4ff 100644 --- a/tools/web_tools.py +++ b/tools/web_tools.py @@ -2085,6 +2085,7 @@ registry.register( check_fn=check_web_api_key, requires_env=_web_requires_env(), emoji="🔍", + max_result_size_chars=100_000, ) registry.register( name="web_extract", @@ -2096,4 +2097,5 @@ registry.register( requires_env=_web_requires_env(), is_async=True, emoji="📄", + max_result_size_chars=100_000, )