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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,4 @@ __marimo__/

# Streamlit
.streamlit/secrets.toml
.fastcontext/
4 changes: 2 additions & 2 deletions src/fastcontext/agent/tool/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from pathlib import Path

from .tool import Tool
from .tool import Tool, resolve_path


def run(directory: str, pattern: str, cwd: str) -> str:
Expand Down Expand Up @@ -39,7 +39,7 @@ class GlobTool(Tool):
async def call(self, parameters: str, **kwargs) -> str:
cwd = kwargs.get("cwd", Path.cwd().as_posix())
params: dict = json.loads(parameters)
directory = params.get("directory", cwd)
directory = resolve_path(params.get("directory", cwd), cwd)
pattern = params.get("pattern")

p = Path(directory)
Expand Down
8 changes: 5 additions & 3 deletions src/fastcontext/agent/tool/grep.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import shutil
from pathlib import Path

from .tool import Tool
from .tool import Tool, resolve_path


class GrepTool(Tool):
Expand Down Expand Up @@ -64,8 +65,8 @@ class GrepTool(Tool):
"required": ["pattern"],
}

# Adjust this path if ripgrep is not in your system PATH
_rg_path = "/usr/bin/rg"
# Locate ripgrep on PATH (falls back to /usr/bin/rg)
_rg_path = shutil.which("rg") or "/usr/bin/rg"

async def call(self, parameters: str, **kwargs) -> str:
params: dict = json.loads(parameters)
Expand All @@ -84,6 +85,7 @@ async def call(self, parameters: str, **kwargs) -> str:
head_limit = params.get("head_limit")
multiline = params.get("multiline")

path = resolve_path(path, cwd)
if not Path(path).resolve().is_relative_to(Path(cwd).resolve()):
return f"Permission error: `{path}` is not within the working directory `{cwd}`."

Expand Down
9 changes: 8 additions & 1 deletion src/fastcontext/agent/tool/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import aiofiles

from .tool import Tool
from .tool import Tool, resolve_path

MAX_LINE = 2000
MAX_LINE_LENGTH = 2000
Expand Down Expand Up @@ -40,6 +40,13 @@ async def call(self, parameters: str, **kwargs) -> str:
if not file_path:
return "Read Tool: file path is required."

cwd = kwargs.get("cwd", Path.cwd().as_posix())
file_path = resolve_path(file_path, cwd)

if Path(file_path).is_dir():
entries = sorted(p.name + ("/" if p.is_dir() else "") for p in Path(file_path).iterdir())
return f"Read Tool: {file_path} is a directory. Contents:\n" + "\n".join(entries)

if not Path(file_path).exists():
return f"Read Tool: file {file_path} does not exist."

Expand Down
28 changes: 28 additions & 0 deletions src/fastcontext/agent/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@
MAX_TOOLRUN_TIMEOUT = 10


def resolve_path(path: str, cwd: str) -> str:
"""Resolve model-emitted paths against the working directory.

The model was trained on SWE-bench instances with repos mounted at
/<repo-name>/, so it often emits paths like /myrepo/src/x.py that do
not exist locally. Remap those onto cwd when the literal path is absent.
"""
if not path:
return cwd
p = Path(path)
base = Path(cwd)
if not p.is_absolute():
return (base / p).as_posix()
if p.exists():
return path
parts = p.parts # ('/', 'repo', 'src', ...)
if len(parts) >= 2:
# /<repo-name>[/rest] -> cwd[/rest]
candidate = base.joinpath(*parts[2:]) if len(parts) > 2 else base
if parts[1] == base.name or candidate.exists():
return candidate.as_posix()
# /src/... -> cwd/src/...
candidate = base.joinpath(*parts[1:])
if candidate.exists():
return candidate.as_posix()
return path


class ToolResult(BaseModel):
tool_call_id: str
output: str
Expand Down