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 @@ -75,7 +75,7 @@ async def call(self, parameters: str, **kwargs) -> str:
output_mode = params.get("output_mode")
before_context = params.get("-B")
after_context = params.get("-A")
context = params.get("-C", 3)
context = params.get("-C")
line_number = params.get("-n", True)
ignore_case = params.get("-i", False)
type = params.get("type")
Expand Down
28 changes: 28 additions & 0 deletions tests/test_grep_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import asyncio
import json
import tempfile
from pathlib import Path

from fastcontext.agent.tool.grep import GrepTool


def _search(grep, cwd, params):
return asyncio.run(grep.call(json.dumps(params), cwd=cwd))


def test_grep_no_context_unless_requested():
"""content mode must not inject surrounding context by default; -C only
applies when the model explicitly provides it."""
grep = GrepTool()
with tempfile.TemporaryDirectory() as cwd:
(Path(cwd) / "a.txt").write_text("before\nMATCH here\nafter\n", encoding="utf-8")

# No -C: only the matching line, no neighbors.
out = _search(grep, cwd, {"pattern": "MATCH", "output_mode": "content"})
assert "MATCH here" in out
assert "before" not in out, f"unexpected default context: {out!r}"
assert "after" not in out, f"unexpected default context: {out!r}"

# Explicit -C: neighbors are included.
out = _search(grep, cwd, {"pattern": "MATCH", "output_mode": "content", "-C": 1})
assert "before" in out and "after" in out, f"expected context with -C=1: {out!r}"