Skip to content

Build and run agents you can see, understand and trust.

fix(tool): sort ripgrep output by path so Grep pagination is stable (#2726)

DavdGao226daf0193a9
diff --git a/src/agentscope/tool/_builtin/_grep.py b/src/agentscope/tool/_builtin/_grep.py
index e0c798790..c70005944 100644
--- a/src/agentscope/tool/_builtin/_grep.py
+++ b/src/agentscope/tool/_builtin/_grep.py
@@ -375,7 +375,8 @@ class Grep(ToolBase):
is_last=True,
)
- args: list[str] = ["--hidden"]
+ # Sort by path: offset/head_limit paginate over a stable order
+ args: list[str] = ["--hidden", "--sort", "path"]
# Exclude VCS directories
for vcs_dir in VCS_DIRECTORIES_TO_EXCLUDE:
diff --git a/tests/builtin_grep_test.py b/tests/builtin_grep_test.py
index 17593f145..4718344b1 100644
--- a/tests/builtin_grep_test.py
+++ b/tests/builtin_grep_test.py
@@ -109,6 +109,31 @@ class GrepToolTest(IsolatedAsyncioTestCase):
self.assertIn("def hello", content)
self.assertIn("def goodbye", content)
+ async def test_pagination_is_stable(self) -> None:
+ """Pages taken with offset/head_limit follow the path order."""
+ pages = []
+ for offset in range(3):
+ chunk = await self.grep_tool(
+ pattern="def",
+ path=self.temp_dir,
+ output_mode="files_with_matches",
+ head_limit=1,
+ offset=offset,
+ )
+ pages.append(chunk.content[0].text)
+
+ self.assertListEqual(
+ pages,
+ [
+ os.path.join(self.temp_dir, "subdir", "nested.py")
+ + "\n\n[Showing results with pagination = limit: 1]",
+ os.path.join(self.temp_dir, "test1.py")
+ + "\n\n[Showing results with pagination = limit: 1, "
+ "offset: 1]",
+ os.path.join(self.temp_dir, "test2.py"),
+ ],
+ )
+
async def test_case_insensitive(self) -> None:
"""Test case-insensitive search."""
chunk = await self.grep_tool(