Remove list_files tool

This commit is contained in:
2026-01-31 13:18:48 -08:00
parent a0492ff7c0
commit 2a7ff66358
2 changed files with 54 additions and 32 deletions

View File

@@ -1,8 +1,6 @@
import os
import glob as globlib
from modaic import PrecompiledProgram, PrecompiledConfig
import dspy
import re
import subprocess
from dspy.utils.callback import BaseCallback
@@ -36,13 +34,14 @@ def read_file(path: str, offset: int = 0, limit: int = None) -> str:
Returns:
File contents with line numbers
"""
print(f"{MAGENTA}⏺ Reading file: {path}{RESET}")
lines = open(path).readlines()
if limit is None:
limit = len(lines)
selected = lines[offset : offset + limit]
return "".join(f"{offset + idx + 1:4}| {line}" for idx, line in enumerate(selected))
content = "".join(f"{offset + idx + 1:4}| {line}" for idx, line in enumerate(selected))
tokens = len(content) // 4 # ~4 chars per token estimate
print(f"{MAGENTA}⏺ Reading file: {path} (~{tokens:,} tokens){RESET}")
return content
def write_file(path: str, content: str) -> str:
@@ -53,13 +52,23 @@ def write_file(path: str, content: str) -> str:
content: Content to write to the file
Returns:
'ok' on success
Status message with file stats
"""
print(f"{MAGENTA}⏺ Creating file: {path}{RESET}")
is_new = not os.path.exists(path)
action = "Creating" if is_new else "Overwriting"
# Auto-create parent directories
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
with open(path, "w") as f:
f.write(content)
return "ok"
lines = content.count("\n") + (1 if content and not content.endswith("\n") else 0)
tokens = len(content) // 4
print(f"{MAGENTA}{action} file: {path} ({lines} lines, ~{tokens:,} tokens){RESET}")
return f"ok: wrote {lines} lines ({tokens:,} tokens) to {path}"
def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
@@ -89,7 +98,9 @@ def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
def glob_files(pattern: str, path: str = ".") -> str:
"""Find files matching a glob pattern, sorted by modification time.
"""Find files by filename matching a glob pattern, sorted by modification time.
Respects .gitignore files automatically via ripgrep.
Args:
pattern: Glob pattern to match (e.g., '**/*.py')
@@ -100,38 +111,49 @@ def glob_files(pattern: str, path: str = ".") -> str:
"""
print(f"{MAGENTA}⏺ Glob: {pattern}{RESET}")
full_pattern = (path + "/" + pattern).replace("//", "/")
files = globlib.glob(full_pattern, recursive=True)
files = sorted(
files,
key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0,
reverse=True,
)
return "\n".join(files) or "no files found"
cmd = ["rg", "--files", "-g", pattern, path]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
files = result.stdout.strip().split("\n") if result.stdout.strip() else []
files = sorted(
files,
key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0,
reverse=True,
)
return "\n".join(files) or "no files found"
except FileNotFoundError:
return "error: ripgrep (rg) not installed - install with 'brew install ripgrep'"
except subprocess.TimeoutExpired:
return "error: search timed out after 30s"
def grep_files(pattern: str, path: str = ".") -> str:
"""Search files for a regex pattern.
def grep_files(pattern: str, path: str = ".", glob: str = None, max_results: int = 50) -> str:
"""Search files for a regex pattern using ripgrep.
Args:
pattern: Regular expression pattern to search for
path: Base directory to search in
glob: Optional glob pattern to filter files (e.g., '*.py')
max_results: Maximum number of results to return
Returns:
Matching lines in format 'filepath:line_num:content'
"""
print(f"{MAGENTA}⏺ Grep: {pattern}{RESET}")
regex = re.compile(pattern)
hits = []
for filepath in globlib.glob(path + "/**", recursive=True):
try:
for line_num, line in enumerate(open(filepath), 1):
if regex.search(line):
hits.append(f"{filepath}:{line_num}:{line.rstrip()}")
except Exception:
pass
return "\n".join(hits[:50]) or "no matches found"
cmd = ["rg", "-n", "--no-heading", "--color=never", f"-m{max_results}"]
if glob:
cmd.extend(["-g", glob])
cmd.extend([pattern, path])
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
output = result.stdout.strip()
return output if output else "no matches found"
except FileNotFoundError:
return "error: ripgrep (rg) not installed - install with 'brew install ripgrep'"
except subprocess.TimeoutExpired:
return "error: search timed out after 30s"
# --- Shell operations ---
@@ -363,11 +385,11 @@ class RLMCodingProgram(PrecompiledProgram):
if __name__ == "__main__":
agent = RLMCodingProgram(RLMCodingConfig())
#agent(task="explicity call llm_query(who is the ceo of apple?) to get the answer to 'who is the ceo of apple?'")
branches = ["main", "dev", "prod"]
branches = ["dev", "main", "prod"]
for branch in branches:
agent.push_to_hub(
MODAIC_REPO_PATH,
commit_message="Fix config override bug by recreating LMs after load_state",
commit_message="Remove list_files tool",
branch=branch,
)