Python
read_file, grep and git_log logic moves to standard-library-only modules
Devika Vermadd448ab617dc
diff --git a/coworker/sandbox/runner/tools_git.py b/coworker/sandbox/runner/tools_git.pynew file mode 100644index 00000000..6e745d3f--- /dev/null+++ b/coworker/sandbox/runner/tools_git.py@@ -0,0 +1,45 @@+"""The logic of the `git_log` tool. Standard library only (it also runs inside a sandbox)."""++from __future__ import annotations++import subprocess+from pathlib import Path+from typing import Any, Optional++_SEP = "\x1f"+++def git_log(workspace: str, path: Optional[str] = None, max_count: int = 20) -> dict[str, Any]:+ root = str(Path(workspace).resolve())+ n = max_count if isinstance(max_count, int) and max_count > 0 else 20+ n = min(n, 200)+ cmd = [+ "git",+ "-C",+ root,+ "log",+ f"-n{n}",+ f"--pretty=format:%h{_SEP}%an{_SEP}%ad{_SEP}%s",+ "--date=short",+ ]+ if path:+ cmd += ["--", path]+ try:+ out = subprocess.run(cmd, capture_output=True, text=True, timeout=15)+ except Exception as exc:+ return {"error": f"git log failed: {exc}"}+ if out.returncode != 0:+ return {"error": (out.stderr or "git log failed").strip()[:300]}+ commits = []+ for line in out.stdout.splitlines():+ parts = line.split(_SEP)+ if len(parts) == 4:+ commits.append(+ {+ "hash": parts[0],+ "author": parts[1],+ "date": parts[2],+ "subject": parts[3],+ }+ )+ return {"count": len(commits), "commits": commits}diff --git a/coworker/sandbox/runner/tools_grep.py b/coworker/sandbox/runner/tools_grep.pynew file mode 100644index 00000000..75ba8155--- /dev/null+++ b/coworker/sandbox/runner/tools_grep.py@@ -0,0 +1,161 @@+"""The logic of the `grep` tool. Standard library only (it also runs inside a sandbox).++ripgrep when available, a Python walk otherwise. ripgrep respects `.gitignore`, so it skips+`node_modules`/`target`/`dist` automatically; the fallback skips a hardcoded set of heavy+dirs. Read-only, workspace-scoped. Returns file:line:text.+"""++from __future__ import annotations++import fnmatch+import os+import re+import shutil+import subprocess+from pathlib import Path+from typing import Any, Optional++# Per-OS application data directories. These are not build noise: on macOS 14+ merely+# *descending* into ~/Library/Application Support (other apps' containers) trips the App+# Data TCC protection and macOS shows "would like to access data from other apps" — an+# alarming prompt the user never asked for, reachable whenever the workspace is a home+# directory. Never traversed; a workspace under one of these is still searched normally,+# because the guard matches directory NAMES encountered during a walk.+OS_DATA_DIRS = {+ "Library", # macOS+ "AppData", # Windows+ "Application Data", # Windows (legacy junction)+}++_IGNORE_DIRS = {+ ".git",+ "node_modules",+ "target",+ "dist",+ "build",+ ".venv",+ "venv",+ "__pycache__",+ ".next",+ ".mypy_cache",+ ".pytest_cache",+ ".ruff_cache",+ ".idea",+} | OS_DATA_DIRS+++def grep(+ workspace: str,+ pattern: str,+ path: str = ".",+ glob: Optional[str] = None,+ max_results: int = 100,+) -> dict[str, Any]:+ root = Path(workspace).resolve()+ n = max_results if isinstance(max_results, int) and max_results > 0 else 100+ n = min(n, 1000)+ base = (root / (path or ".")).resolve()+ try:+ base.relative_to(root) # keep searches inside the workspace+ except ValueError:+ return {"error": "path escapes the workspace"}++ rg = shutil.which("rg")+ if rg:+ cmd = [+ rg,+ "--line-number",+ "--no-heading",+ "--color=never",+ # Always print the filename (even for a single-file target) and+ # NUL-separate it from `line:text`, so parsing never has to guess+ # where the path ends — a Windows drive-letter colon (C:\ws\a.py),+ # or a colon inside the matched text, would otherwise be taken as a+ # field separator (issue #17). Without --with-filename a single-file+ # search emits no path and no NUL, and the parser would drop it.+ "--with-filename",+ "--null",+ "--max-count",+ str(n),+ "-e",+ pattern,+ ]+ if glob:+ cmd += ["--glob", glob]+ # Do not rely solely on a workspace's .gitignore: the Python fallback+ # always omits these generated/dependency directories too. Exclusions come+ # last because ripgrep resolves conflicting globs with the later one winning.+ for ignored in sorted(_IGNORE_DIRS):+ cmd += ["--glob", f"!**/{ignored}/**"]+ cmd.append(str(base))+ try:+ out = subprocess.run(cmd, capture_output=True, text=True, timeout=30)+ except Exception as exc:+ return {"error": f"grep failed: {exc}"}+ if out.returncode not in (0, 1): # 1 = no matches+ return {"error": (out.stderr or "ripgrep error").strip()[:300]}+ return {"engine": "ripgrep", **_parse_rg(out.stdout, root, n)}++ return {"engine": "python", **_py_grep(root, base, pattern, glob, n)}+++def _rel(path: str, root: Path) -> str:+ try:+ return str(Path(path).resolve().relative_to(root))+ except (ValueError, OSError):+ return path+++def _parse_rg(stdout: str, root: Path, n: int) -> dict[str, Any]:+ # `rg --null` emits each match as `<path>\0<line>:<text>`. Splitting the path off+ # on the NUL byte first means the colon inside a Windows drive letter (C:\...) is+ # never confused with the line/text separators — the old `split(":", 2)` parsed+ # `C:\ws\a.py:12:def f()` as file="C", line="\ws\a.py", text="12:def f()".+ matches: list[dict[str, Any]] = []+ for line in stdout.splitlines():+ path, sep, rest = line.partition("\0")+ if not sep:+ continue+ ln, _, txt = rest.partition(":")+ matches.append(+ {+ "file": _rel(path, root),+ "line": int(ln) if ln.isdigit() else 0,+ "text": txt[:300],+ }+ )+ if len(matches) >= n:+ break+ return {"count": len(matches), "matches": matches}+++def _py_grep(+ root: Path, base: Path, pattern: str, glob: Optional[str], n: int+) -> dict[str, Any]:+ try:+ rx = re.compile(pattern)+ except re.error as exc:+ return {"error": f"invalid regex: {exc}", "count": 0, "matches": []}+ matches: list[dict[str, Any]] = []+ for dirpath, dirs, files in os.walk(base):+ dirs[:] = [d for d in dirs if d not in _IGNORE_DIRS]+ for fn in files:+ if glob and not fnmatch.fnmatch(fn, glob):+ continue+ fp = Path(dirpath) / fn+ try:+ with open(fp, "r", encoding="utf-8", errors="ignore") as fh:+ for i, line in enumerate(fh, 1):+ if rx.search(line):+ matches.append(+ {+ "file": _rel(str(fp), root),+ "line": i,+ "text": line.rstrip()[:300],+ }+ )+ if len(matches) >= n:+ return {"count": len(matches), "matches": matches}+ except OSError:+ continue+ return {"count": len(matches), "matches": matches}diff --git a/coworker/sandbox/runner/tools_read.py b/coworker/sandbox/runner/tools_read.pynew file mode 100644index 00000000..660a47e2--- /dev/null+++ b/coworker/sandbox/runner/tools_read.py@@ -0,0 +1,79 @@+"""The logic of the `read_file` tool. Standard library only (it also runs inside a sandbox).++Returns `cat -n`-style numbered lines, windows big files instead of failing, and tells the+agent how to continue reading. Read-only, scoped to the session's folders.+"""++from __future__ import annotations++from pathlib import Path+from typing import Any, Optional, Sequence++_DEFAULT_MAX_LINES = 2000+_MAX_LINE_CHARS = 500+++def read_file(+ workspace: str,+ path: str,+ start_line: int = 1,+ max_lines: int = _DEFAULT_MAX_LINES,+ roots: Optional[Sequence[str]] = None,+) -> dict[str, Any]:+ """`roots`: the absolute paths of the session's other folders; a path inside any of+ them resolves too."""+ root = Path(workspace).resolve()+ extra_roots = [Path(str(r)).resolve() for r in (roots or [])]+ start = start_line if isinstance(start_line, int) and start_line > 0 else 1+ n = (+ max_lines+ if isinstance(max_lines, int) and max_lines > 0+ else _DEFAULT_MAX_LINES+ )+ n = min(n, _DEFAULT_MAX_LINES)+ target = (root / path).resolve()+ home = root+ try:+ target.relative_to(root) # keep reads inside the workspace+ except ValueError:+ for r in extra_roots:+ try:+ target.relative_to(r)+ home = r+ break+ except ValueError:+ continue+ else:+ return {"error": "path escapes the session's directories"}+ if not target.is_file():+ return {"error": f"not a file: {path}"}++ selected: list[str] = []+ total = 0+ try:+ with open(target, "r", encoding="utf-8", errors="replace") as fh:+ for i, line in enumerate(fh, 1):+ total = i+ if i < start or len(selected) >= n:+ continue+ text = line.rstrip("\n")+ if len(text) > _MAX_LINE_CHARS:+ text = text[:_MAX_LINE_CHARS] + "… (line truncated)"+ selected.append(f"{i:>6}\t{text}")+ except OSError as exc:+ return {"error": f"read failed: {exc}"}++ end = start + len(selected) - 1 if selected else start - 1+ result: dict[str, Any] = {+ "path": str(target.relative_to(home)) if home == root else str(target),+ "start_line": start,+ "end_line": end,+ "total_lines": total,+ "content": "\n".join(selected),+ }+ if end < total:+ result["note"] = (+ f"showing lines {start}-{end} of {total}; "+ f"call again with start_line={end + 1} to continue"+ )+ return resultdiff --git a/coworker/tools/files.py b/coworker/tools/files.pyindex 59716a0b..7cde1ce6 100644--- a/coworker/tools/files.py+++ b/coworker/tools/files.py@@ -8,13 +8,12 @@ the agent how to continue reading. Read-only, workspace-scoped.from __future__ import annotations-from pathlib import Pathfrom typing import Any, Optionalimport aisuite as ai-_DEFAULT_MAX_LINES = 2000-_MAX_LINE_CHARS = 500+from ..sandbox.runner import tools_read as _impl+from ..sandbox.runner.tools_read import _DEFAULT_MAX_LINES, _MAX_LINE_CHARS # noqa: F401_SCHEMA = {"type": "function",@@ -51,67 +50,15 @@ def file_tools(workspace: str, roots: Optional[list] = None) -> list:"""Windowed read_file rooted at `workspace`. With `roots` (RootDir list), absolutepaths inside ANY root also resolve — multi-root sessions (universal scratch) addresstheir scratch/extra dirs by the absolute paths the roots context advertises."""- root = Path(workspace).resolve()- extra_roots = [Path(str(r.path)).resolve() for r in (roots or [])]-def read_file(path: str,start_line: int = 1,max_lines: int = _DEFAULT_MAX_LINES,) -> dict[str, Any]:- start = start_line if isinstance(start_line, int) and start_line > 0 else 1- n = (- max_lines- if isinstance(max_lines, int) and max_lines > 0- else _DEFAULT_MAX_LINES+ # `roots` is the session's live list: read it on every call.+ return _impl.read_file(+ workspace, path, start_line, max_lines, roots=[str(r.path) for r in (roots or [])])- n = min(n, _DEFAULT_MAX_LINES)- target = (root / path).resolve()- home = root- try:- target.relative_to(root) # keep reads inside the workspace- except ValueError:- for r in extra_roots:- try:- target.relative_to(r)- home = r- break- except ValueError:- continue- else:- return {"error": "path escapes the session's directories"}- if not target.is_file():- return {"error": f"not a file: {path}"}-- selected: list[str] = []- total = 0- try:- with open(target, "r", encoding="utf-8", errors="replace") as fh:- for i, line in enumerate(fh, 1):- total = i- if i < start or len(selected) >= n:- continue- text = line.rstrip("\n")- if len(text) > _MAX_LINE_CHARS:- text = text[:_MAX_LINE_CHARS] + "… (line truncated)"- selected.append(f"{i:>6}\t{text}")- except OSError as exc:- return {"error": f"read failed: {exc}"}-- end = start + len(selected) - 1 if selected else start - 1- result: dict[str, Any] = {- "path": str(target.relative_to(home)) if home == root else str(target),- "start_line": start,- "end_line": end,- "total_lines": total,- "content": "\n".join(selected),- }- if end < total:- result["note"] = (- f"showing lines {start}-{end} of {total}; "- f"call again with start_line={end + 1} to continue"- )- return resultread_file.__name__ = "read_file"read_file.__doc__ = _SCHEMA["function"]["description"]diff --git a/coworker/tools/git.py b/coworker/tools/git.pyindex 169b8fd3..ce962c3b 100644--- a/coworker/tools/git.py+++ b/coworker/tools/git.py@@ -7,13 +7,11 @@ forbids those without explicit ask, and they'd go through run_shell anyway).from __future__ import annotations-import subprocess-from pathlib import Pathfrom typing import Any, Optionalimport aisuite as ai-_SEP = "\x1f"+from ..sandbox.runner import tools_git as _impl_SCHEMA = {"type": "function",@@ -41,41 +39,8 @@ _SCHEMA = {def git_tools(workspace: str) -> list:- root = str(Path(workspace).resolve())-def git_log(path: Optional[str] = None, max_count: int = 20) -> dict[str, Any]:- n = max_count if isinstance(max_count, int) and max_count > 0 else 20- n = min(n, 200)- cmd = [- "git",- "-C",- root,- "log",- f"-n{n}",- f"--pretty=format:%h{_SEP}%an{_SEP}%ad{_SEP}%s",- "--date=short",- ]- if path:- cmd += ["--", path]- try:- out = subprocess.run(cmd, capture_output=True, text=True, timeout=15)- except Exception as exc:- return {"error": f"git log failed: {exc}"}- if out.returncode != 0:- return {"error": (out.stderr or "git log failed").strip()[:300]}- commits = []- for line in out.stdout.splitlines():- parts = line.split(_SEP)- if len(parts) == 4:- commits.append(- {- "hash": parts[0],- "author": parts[1],- "date": parts[2],- "subject": parts[3],- }- )- return {"count": len(commits), "commits": commits}+ return _impl.git_log(workspace, path, max_count)git_log.__name__ = "git_log"git_log.__doc__ = _SCHEMA["function"]["description"]diff --git a/coworker/tools/search.py b/coworker/tools/search.pyindex a9d5c14f..14af656b 100644--- a/coworker/tools/search.py+++ b/coworker/tools/search.py@@ -6,43 +6,20 @@ fallback skips a hardcoded set of heavy dirs. Read-only, workspace-scoped. Returfrom __future__ import annotations-import fnmatch-import os-import re-import shutil-import subprocess-from pathlib import Path+import shutil # noqa: F401 (tests patch search.shutil / search.subprocess)+import subprocess # noqa: F401from typing import Any, Optionalimport aisuite as ai-# Per-OS application data directories. These are not build noise: on macOS 14+ merely-# *descending* into ~/Library/Application Support (other apps' containers) trips the App-# Data TCC protection and macOS shows "would like to access data from other apps" — an-# alarming prompt the user never asked for, reachable whenever the workspace is a home-# directory. Never traversed; a workspace under one of these is still searched normally,-# because the guard matches directory NAMES encountered during a walk.-OS_DATA_DIRS = {- "Library", # macOS- "AppData", # Windows- "Application Data", # Windows (legacy junction)-}--_IGNORE_DIRS = {- ".git",- "node_modules",- "target",- "dist",- "build",- ".venv",- "venv",- "__pycache__",- ".next",- ".mypy_cache",- ".pytest_cache",- ".ruff_cache",- ".idea",-} | OS_DATA_DIRS+from ..sandbox.runner import tools_grep as _impl+from ..sandbox.runner.tools_grep import ( # noqa: F401 (re-exported: the logic moved)+ _IGNORE_DIRS,+ OS_DATA_DIRS,+ _parse_rg,+ _py_grep,+ _rel,+)_SCHEMA = {"type": "function",@@ -80,59 +57,13 @@ _SCHEMA = {def search_tools(workspace: str) -> list:- root = Path(workspace).resolve()-def grep(pattern: str,path: str = ".",glob: Optional[str] = None,max_results: int = 100,) -> dict[str, Any]:- n = max_results if isinstance(max_results, int) and max_results > 0 else 100- n = min(n, 1000)- base = (root / (path or ".")).resolve()- try:- base.relative_to(root) # keep searches inside the workspace- except ValueError:- return {"error": "path escapes the workspace"}-- rg = shutil.which("rg")- if rg:- cmd = [- rg,- "--line-number",- "--no-heading",- "--color=never",- # Always print the filename (even for a single-file target) and- # NUL-separate it from `line:text`, so parsing never has to guess- # where the path ends — a Windows drive-letter colon (C:\ws\a.py),- # or a colon inside the matched text, would otherwise be taken as a- # field separator (issue #17). Without --with-filename a single-file- # search emits no path and no NUL, and the parser would drop it.- "--with-filename",- "--null",- "--max-count",- str(n),- "-e",- pattern,- ]- if glob:- cmd += ["--glob", glob]- # Do not rely solely on a workspace's .gitignore: the Python fallback- # always omits these generated/dependency directories too. Exclusions come- # last because ripgrep resolves conflicting globs with the later one winning.- for ignored in sorted(_IGNORE_DIRS):- cmd += ["--glob", f"!**/{ignored}/**"]- cmd.append(str(base))- try:- out = subprocess.run(cmd, capture_output=True, text=True, timeout=30)- except Exception as exc:- return {"error": f"grep failed: {exc}"}- if out.returncode not in (0, 1): # 1 = no matches- return {"error": (out.stderr or "ripgrep error").strip()[:300]}- return {"engine": "ripgrep", **_parse_rg(out.stdout, root, n)}-- return {"engine": "python", **_py_grep(root, base, pattern, glob, n)}+ return _impl.grep(workspace, pattern, path, glob, max_results)grep.__name__ = "grep"grep.__doc__ = _SCHEMA["function"]["description"]@@ -145,65 +76,3 @@ def search_tools(workspace: str) -> list:)grep.__coworker_schema__ = _SCHEMAreturn [grep]---def _rel(path: str, root: Path) -> str:- try:- return str(Path(path).resolve().relative_to(root))- except (ValueError, OSError):- return path---def _parse_rg(stdout: str, root: Path, n: int) -> dict[str, Any]:- # `rg --null` emits each match as `<path>\0<line>:<text>`. Splitting the path off- # on the NUL byte first means the colon inside a Windows drive letter (C:\...) is- # never confused with the line/text separators — the old `split(":", 2)` parsed- # `C:\ws\a.py:12:def f()` as file="C", line="\ws\a.py", text="12:def f()".- matches: list[dict[str, Any]] = []- for line in stdout.splitlines():- path, sep, rest = line.partition("\0")- if not sep:- continue- ln, _, txt = rest.partition(":")- matches.append(- {- "file": _rel(path, root),- "line": int(ln) if ln.isdigit() else 0,- "text": txt[:300],- }- )- if len(matches) >= n:- break- return {"count": len(matches), "matches": matches}---def _py_grep(- root: Path, base: Path, pattern: str, glob: Optional[str], n: int-) -> dict[str, Any]:- try:- rx = re.compile(pattern)- except re.error as exc:- return {"error": f"invalid regex: {exc}", "count": 0, "matches": []}- matches: list[dict[str, Any]] = []- for dirpath, dirs, files in os.walk(base):- dirs[:] = [d for d in dirs if d not in _IGNORE_DIRS]- for fn in files:- if glob and not fnmatch.fnmatch(fn, glob):- continue- fp = Path(dirpath) / fn- try:- with open(fp, "r", encoding="utf-8", errors="ignore") as fh:- for i, line in enumerate(fh, 1):- if rx.search(line):- matches.append(- {- "file": _rel(str(fp), root),- "line": i,- "text": line.rstrip()[:300],- }- )- if len(matches) >= n:- return {"count": len(matches), "matches": matches}- except OSError:- continue- return {"count": len(matches), "matches": matches}