Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fastcontext/agent/tool/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def run_rg(rg_path: str, pattern: str, path: str, **kwargs) -> str:
command.append("-n")
elif output_mode == "files_with_matches":
command.append("--files-with-matches")
elif output_mode == "count_matches":
elif output_mode == "count":
command.append("--count-matches")

# --heading and --color never
Expand Down
21 changes: 21 additions & 0 deletions tests/test_grep_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import asyncio
import json
import tempfile
from pathlib import Path

from fastcontext.agent.tool.grep import GrepTool


def test_grep_count_mode():
"""output_mode 'count' (the advertised enum value) must emit per-file
match counts via rg --count-matches, not fall through to content."""
grep = GrepTool()
with tempfile.TemporaryDirectory() as cwd:
(Path(cwd) / "haystack.txt").write_text("MATCH\nMATCH\nMATCH\nnope\n", encoding="utf-8")

out = asyncio.run(grep.call(json.dumps({"pattern": "MATCH", "output_mode": "count"}), cwd=cwd))

# rg --count-matches prints "<path>:<count>"; 3 matches in the file.
assert out.strip().endswith(":3"), f"expected a per-file count of 3, got: {out!r}"
# It must not be content mode (which would echo the matched line text).
assert "MATCH\n" not in out and not out.strip().endswith("MATCH")