diff --git a/src/fastcontext/agent/tool/grep.py b/src/fastcontext/agent/tool/grep.py index 4ee161f..02ea18c 100644 --- a/src/fastcontext/agent/tool/grep.py +++ b/src/fastcontext/agent/tool/grep.py @@ -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 diff --git a/tests/test_grep_count.py b/tests/test_grep_count.py new file mode 100644 index 0000000..41d1a9b --- /dev/null +++ b/tests/test_grep_count.py @@ -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 ":"; 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")