Skip to content

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

fix(tool): handle current-directory segments in glob patterns (#2979)

little-spoon778b8de537ea39
diff --git a/src/agentscope/tool/_builtin/_scripts/_glob_helper.py b/src/agentscope/tool/_builtin/_scripts/_glob_helper.py
index d6025eca3..1384921b3 100644
--- a/src/agentscope/tool/_builtin/_scripts/_glob_helper.py
+++ b/src/agentscope/tool/_builtin/_scripts/_glob_helper.py
@@ -146,7 +146,8 @@ def glob_match(pattern: str, base_dir: str) -> list[str]:
Splits *pattern* on path separators (``/`` or ``\\``) and
delegates to :func:`_match_parts` for recursive directory
- traversal. Supports ``*`` (any characters within a segment),
+ traversal, ignoring standalone current-directory (``.``) segments.
+ Supports ``*`` (any characters within a segment),
``?`` (single character), and ``**`` (zero or more directories).
Args:
@@ -160,7 +161,7 @@ def glob_match(pattern: str, base_dir: str) -> list[str]:
modification time).
"""
results: list[str] = []
- parts = [p for p in re.split(r"[\\/]+", pattern) if p]
+ parts = [p for p in re.split(r"[\\/]+", pattern) if p and p != "."]
_match_parts(parts, 0, base_dir, results)
return results
diff --git a/tests/builtin_glob_test.py b/tests/builtin_glob_test.py
index 656fa7294..5c5ed0a51 100644
--- a/tests/builtin_glob_test.py
+++ b/tests/builtin_glob_test.py
@@ -120,6 +120,42 @@ class GlobToolTest(IsolatedAsyncioTestCase):
self.assertIn("test2.py", content)
self.assertIn("test3.py", content)
+ async def test_current_directory_segments(self) -> None:
+ """Standalone dot segments do not change the matched files."""
+ for pattern, expected in (
+ ("./*.py", ["test1.py", "test2.py"]),
+ ("./**/*.py", ["subdir/test3.py", "test1.py", "test2.py"]),
+ ("subdir/./*.py", ["subdir/test3.py"]),
+ (r".\subdir\.\*.py", ["subdir/test3.py"]),
+ ):
+ with self.subTest(pattern=pattern):
+ chunk = await self.glob_tool(
+ pattern=pattern,
+ path=self.temp_dir,
+ )
+ self.assertListEqual(
+ sorted(chunk.content[0].text.splitlines()),
+ [
+ os.path.join(self.temp_dir, *name.split("/"))
+ for name in expected
+ ],
+ )
+
+ async def test_dot_prefixed_names_are_preserved(self) -> None:
+ """Normalizing '.' must not strip dots from hidden names."""
+ directory = os.path.join(self.temp_dir, ".config")
+ os.makedirs(directory)
+ path = os.path.join(directory, ".env")
+ with open(path, "w", encoding="utf-8"):
+ pass
+
+ result = await self.glob_tool(
+ pattern="./.config/./.env",
+ path=self.temp_dir,
+ )
+
+ self.assertEqual(result.content[0].text, path)
+
async def test_default_head_limit_truncates_large_result(self) -> None:
"""Test the default head limit returns the newest 250 files."""
expected_paths = []