Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): preserve empty lines in grep results (#3842)
qkycir2963e72fb11c
diff --git a/src/agentscope/tool/_builtin/_grep.py b/src/agentscope/tool/_builtin/_grep.pyindex 8d59b9326..ca8e46d95 100644--- a/src/agentscope/tool/_builtin/_grep.py+++ b/src/agentscope/tool/_builtin/_grep.py@@ -312,8 +312,11 @@ class Grep(ToolBase):raw = result.stdout.decode("utf-8", errors="ignore")+ if not raw:+ return []+ # Only the terminal separator is framing; empty records are matches.lines = [- line.rstrip("\r") for line in raw.split("\n") if line.rstrip("\r")+ line.rstrip("\r") for line in raw.removesuffix("\n").split("\n")]return linesdiff --git a/tests/builtin_grep_test.py b/tests/builtin_grep_test.pyindex fcc52919f..7080b16c8 100644--- a/tests/builtin_grep_test.py+++ b/tests/builtin_grep_test.py@@ -7,7 +7,7 @@ from unittest.async_case import IsolatedAsyncioTestCasefrom unittest.mock import patchfrom agentscope.message import ToolResultState-from agentscope.tool import ExecResult, Grep, LocalBackend+from agentscope.tool import Edit, ExecResult, Grep, LocalBackendfrom agentscope.permission import (PermissionContext,PermissionBehavior,@@ -155,6 +155,72 @@ class GrepToolTest(IsolatedAsyncioTestCase):},)+ async def test_empty_matching_lines_and_pagination(self) -> None:+ """Keep empty records distinct from no output, including CRLF."""+ path = os.path.join(self.temp_dir, "empty-lines.txt")+ for newline in (b"\n", b"\r\n"):+ with self.subTest(newline=newline):+ with open(path, "wb") as stream:+ stream.write(+ newline.join([b"FIRST", b"", b"LAST", b"", b""]),+ )+ blank = await self.grep_tool.call(+ r"^\r?$",+ path,+ "content",+ n=False,+ )+ self.assertEqual(blank.state, ToolResultState.SUCCESS)+ self.assertEqual(blank.content[0].text, "\n")+ numbered = await self.grep_tool.call(+ r"^\r?$",+ path,+ "content",+ n=True,+ )+ self.assertEqual(numbered.content[0].text, "2:\n4:")+ missing = await self.grep_tool.call(+ "NEVER_PRESENT",+ path,+ "content",+ n=False,+ )+ self.assertIn("No matches found", missing.content[0].text)+ page = await self.grep_tool.call(+ ".*",+ path,+ "content",+ n=False,+ offset=1,+ head_limit=1,+ )+ self.assertEqual(+ page.content[0].text,+ "\n\n[Showing results with pagination = "+ "limit: 1, offset: 1]",+ )++ async def test_multiline_match_can_be_used_as_edit_input(self) -> None:+ """A match containing an empty line must still occur in the file."""+ path = os.path.join(self.temp_dir, "multiline.txt")+ with open(path, "wb") as stream:+ stream.write(b"BEGIN\n\nEND\n")+ match = await self.grep_tool.call(+ r"BEGIN\n\nEND",+ path,+ "content",+ n=False,+ multiline=True,+ )+ self.assertEqual(match.content[0].text, "BEGIN\n\nEND")+ await Edit().call(+ file_path=path,+ old_string=match.content[0].text,+ new_string="replaced",+ )+ with open(path, "rb") as stream:+ self.assertEqual(stream.read(), b"replaced\n")+async def test_content_mode(self) -> None:"""Test grep with content output mode."""chunk = await self.grep_tool(