From 56eea9e4b0f5d125da16b75b551440b170aa8b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Sun, 22 Nov 2026 19:00:05 +0100 Subject: [PATCH] server : add offset pagination to grep_search tool (#32792) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- tools/server/server-tools.cpp | 29 +++++-- tools/server/tests/unit/test_tools_builtin.py | 80 +++++++++++++++++++ 2 files changed, 101 insertions(+), 8 deletions(-) diff --git a/tools/server/server-tools.cpp b/tools/server/server-tools.cpp index 63626ddcc..060361968 100644 --- a/tools/server/server-tools.cpp +++ b/tools/server/server-tools.cpp @@ -1121,6 +1121,7 @@ struct server_tool_grep_search : server_tool { {"literal", {{"type", "boolean"}, {"description", "Treat pattern as a literal string instead of a regular expression (default: false)"}}}, {"ignore_case", {{"type", "boolean"}, {"description", "Case-insensitive search (default: false)"}}}, {"context_lines", {{"type", "integer"}, {"description", "Number of lines of context to show before and after each match (default: 0)"}}}, + {"offset", {{"type", "integer"}, {"description", "Number of matching lines to skip (default: 0)"}}}, }}, {"required", json::array({"path", "pattern"})}, }}, @@ -1137,6 +1138,7 @@ struct server_tool_grep_search : server_tool { bool literal = json_value(params, "literal", false); bool ignore_case = json_value(params, "ignore_case", false); int ctx_lines = std::max(0, json_value(params, "context_lines", 0)); + uint64_t offset = std::max(0, json_value(params, "offset", int64_t{0})); std::string pattern_src = pat_str; if (literal) { @@ -1164,6 +1166,7 @@ struct server_tool_grep_search : server_tool { // collect (absolute_path, display_path) pairs to search std::vector> files; + bool listing_truncated = false; const std::string abs_path = io->resolve(path); if (io->is_regular_file(abs_path)) { files.emplace_back(abs_path, path); @@ -1172,6 +1175,7 @@ struct server_tool_grep_search : server_tool { if (!listing.err.empty()) { return {{"error", listing.err + ": " + path}}; } + listing_truncated = listing.truncated; for (const auto & entry : listing.entries) { if (!path_glob_match(include, entry.rel)) continue; if (!exclude.empty() && path_glob_match(exclude, entry.rel)) continue; @@ -1180,14 +1184,18 @@ struct server_tool_grep_search : server_tool { } else { return {{"error", "path does not exist: " + path}}; } + std::sort(files.begin(), files.end(), [](const auto & a, const auto & b) { + return a.second < b.second; + }); std::ostringstream output_text; + uint64_t skipped = 0; size_t total = 0; - bool limit_reached = false; + bool has_more = false; bool show_num = show_lineno || ctx_lines > 0; for (const auto & file_entry : files) { - if (limit_reached) break; + if (has_more) break; const std::string & fpath = file_entry.first; const std::string & display_path = file_entry.second; @@ -1201,11 +1209,15 @@ struct server_tool_grep_search : server_tool { } for (size_t i = 0; i < lines.size(); i++) { + if (!std::regex_search(lines[i], pattern)) continue; + if (skipped < offset) { + skipped++; + continue; + } if (total >= SERVER_TOOL_GREP_SEARCH_MAX_RESULTS) { - limit_reached = true; + has_more = true; break; } - if (!std::regex_search(lines[i], pattern)) continue; long ctx_start = ctx_lines > 0 ? std::max(0, (long) i - ctx_lines) : (long) i; long ctx_end = ctx_lines > 0 ? std::min((long) lines.size() - 1, (long) i + ctx_lines) : (long) i; @@ -1226,10 +1238,11 @@ struct server_tool_grep_search : server_tool { } output_text << "\n---\nTotal matches: " << total << "\n"; - if (limit_reached) { - output_text << string_format( - "[%zu matches limit reached. Narrow the path/pattern/include to see more.]\n", - SERVER_TOOL_GREP_SEARCH_MAX_RESULTS); + if (has_more) { + output_text << "Continue with offset=" << offset + total << " and the same search arguments.\n"; + } + if (listing_truncated) { + output_text << "[results truncated: time budget or unreadable directory]\n"; } return {{"plain_text_response", output_text.str()}}; diff --git a/tools/server/tests/unit/test_tools_builtin.py b/tools/server/tests/unit/test_tools_builtin.py index b6673a089..c830b0642 100755 --- a/tools/server/tests/unit/test_tools_builtin.py +++ b/tools/server/tests/unit/test_tools_builtin.py @@ -1,4 +1,5 @@ import os +import re import shutil import subprocess @@ -53,6 +54,85 @@ def test_tools_builtin_grep_search(): assert "Total matches: 1" in text +def _grep_matches(text: str) -> list[tuple[str, int, str]]: + matches = [] + for line in text.splitlines(): + match = re.match(r"^(.*):(\d+):(match-\d+)$", line) + if match: + path, line_number, identity = match.groups() + matches.append((path, int(line_number), identity)) + return matches + + +def test_tools_builtin_grep_search_pagination(tmp_path): + global server + server.start() + + path = tmp_path / "matches.txt" + expected = [f"match-{i:03d}" for i in range(231)] + path.write_text("\n".join(expected) + "\n") + params = {"path": str(path), "pattern": "match-", "return_line_numbers": True} + + pages = [] + next_offset = 0 + for page_index, expected_size in enumerate([100, 100, 31]): + page_params = params if not pages else {**params, "offset": next_offset} + text = call_tool("grep_search", page_params)["plain_text_response"] + matches = _grep_matches(text) + pages.append(matches) + assert len(matches) == expected_size + assert f"\n---\nTotal matches: {expected_size}\n" in text + + continuation = re.search(r"Continue with offset=(\d+) and the same search arguments\.", text) + if page_index < 2: + assert continuation is not None + next_offset = int(continuation.group(1)) + assert next_offset == (page_index + 1) * 100 + else: + assert continuation is None + + assert [identity for page in pages for _, _, identity in page] == expected + + +def test_tools_builtin_grep_search_exact_page_has_no_continuation(tmp_path): + global server + server.start() + + path = tmp_path / "matches.txt" + path.write_text("".join(f"match-{i:03d}\n" for i in range(100)) + "unrelated\n") + res = call_tool("grep_search", {"path": str(path), "pattern": "match-", "return_line_numbers": True}) + text = res["plain_text_response"] + + assert len(_grep_matches(text)) == 100 + assert "Total matches: 100" in text + assert "Continue with offset=" not in text + assert "matches limit reached" not in text + + +def test_tools_builtin_grep_search_page_order_with_context(tmp_path): + global server + server.start() + + (tmp_path / "z.txt").write_text("".join(f"match-{i:03d}\n" for i in range(50))) + (tmp_path / "a.txt").write_text("".join(f"match-{i:03d}\n" for i in range(50, 120))) + params = {"path": str(tmp_path), "pattern": "match-", "context_lines": 1} + + first = call_tool("grep_search", {**params, "offset": 0})["plain_text_response"] + second = call_tool("grep_search", {**params, "offset": 100})["plain_text_response"] + past_end = [call_tool("grep_search", {**params, "offset": offset})["plain_text_response"] for offset in (121, 1 << 32)] + + expected = [("a.txt", line, f"match-{line + 49:03d}") for line in range(1, 71)] + expected += [("z.txt", line, f"match-{line - 1:03d}") for line in range(1, 51)] + assert _grep_matches(first) == expected[:100] + assert _grep_matches(second) == expected[100:] + assert "Continue with offset=100 and the same search arguments." in first + assert "Continue with offset=" not in second + for text in past_end: + assert _grep_matches(text) == [] + assert "Total matches: 0" in text + assert "Continue with offset=" not in text + + def test_tools_builtin_read_file(tmp_path): global server server.start()