Skip to content

Commit 3e2fa40

Browse files
committed
✨Please check .gitignore file existing or not in work_dir and only return ToolError Unsafe pattern if can not finding the .gitignore file.
1 parent 322607b commit 3e2fa40

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/kimi_cli/tools/file/glob.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ def __init__(self, builtin_args: BuiltinSystemPromptArgs) -> None:
4545
async def _validate_pattern(self, pattern: str) -> ToolError | None:
4646
"""Validate that the pattern is safe to use."""
4747
if pattern.startswith("**"):
48+
gitignore_path = self._work_dir / ".gitignore"
49+
if await gitignore_path.is_file():
50+
return None
51+
4852
ls_result = await list_directory(self._work_dir)
4953
return ToolError(
5054
output=ls_result,
5155
message=(
52-
f"Pattern `{pattern}` starts with '**' which is not allowed. "
56+
f"Pattern `{pattern}` starts with '**' which is not allowed because "
57+
"the working directory does not contain a .gitignore to constrain the search. "
5358
"This would recursively search all directories and may include large "
5459
"directories like `node_modules`. Use more specific patterns instead. "
5560
"For your convenience, a list of all files and directories in the "

tests/test_glob.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ async def test_glob_recursive_pattern_prohibited(glob_tool: Glob, test_files: Ka
6767
assert "Unsafe pattern" in result.brief
6868

6969

70+
@pytest.mark.asyncio
71+
async def test_glob_recursive_pattern_allowed_with_gitignore(glob_tool: Glob, test_files: KaosPath):
72+
"""Allow recursive ** pattern when a .gitignore exists in work dir."""
73+
await (test_files / ".gitignore").write_text("node_modules/\n")
74+
75+
result = await glob_tool(Params(pattern="**/*.py", directory=str(test_files)))
76+
77+
assert isinstance(result, ToolOk)
78+
assert isinstance(result.output, str)
79+
output = result.output.replace("\\", "/")
80+
assert "setup.py" in output
81+
assert "src/main/app.py" in output
82+
83+
7084
@pytest.mark.asyncio
7185
async def test_glob_safe_recursive_pattern(glob_tool: Glob, test_files: KaosPath):
7286
"""Test safe recursive glob pattern that doesn't start with **/."""

0 commit comments

Comments
 (0)