Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): return structured errors for directory listing failures (#1946)

Rudy Mizrahi Celeklibc7769aba94a
diff --git a/agent/src/tools/read_file_tool.py b/agent/src/tools/read_file_tool.py
index f4ff8134d..3a801f5af 100644
--- a/agent/src/tools/read_file_tool.py
+++ b/agent/src/tools/read_file_tool.py
@@ -95,7 +95,10 @@ class ReadFileTool(BaseTool):
"resolves against the skill that owns it."
),
},
- "limit": {"type": "integer", "description": "Max number of lines to return (default: all)"},
+ "limit": {
+ "type": "integer",
+ "description": "Max number of lines to return (default: all)",
+ },
},
"required": ["path"],
}
@@ -204,30 +207,34 @@ class ReadFileTool(BaseTool):
else:
error_code = "outside_readable_roots"
reason = f"{file_path} is outside what read_file may open."
- return json.dumps(
- {"status": "error", "error_code": error_code, "error": f"{reason} {_READABLE}"},
- ensure_ascii=False,
- )
-
- if resolved.is_dir():
- # A directory is a question about what is in it.
- entries = sorted(
- child.name + ("/" if child.is_dir() else "")
- for child in resolved.iterdir()
- if not child.name.startswith(".")
- )
return json.dumps(
{
- "status": "ok",
- "path": str(resolved),
- "kind": "directory",
- "entries": entries[:_LISTING_LIMIT],
- "truncated": len(entries) > _LISTING_LIMIT,
+ "status": "error",
+ "error_code": error_code,
+ "error": f"{reason} {_READABLE}",
},
ensure_ascii=False,
)
try:
+ if resolved.is_dir():
+ # A directory is a question about what is in it.
+ entries = sorted(
+ child.name + ("/" if child.is_dir() else "")
+ for child in resolved.iterdir()
+ if not child.name.startswith(".")
+ )
+ return json.dumps(
+ {
+ "status": "ok",
+ "path": str(resolved),
+ "kind": "directory",
+ "entries": entries[:_LISTING_LIMIT],
+ "truncated": len(entries) > _LISTING_LIMIT,
+ },
+ ensure_ascii=False,
+ )
+
text = resolved.read_text(encoding="utf-8")
if limit and limit > 0:
lines = text.splitlines(keepends=True)
diff --git a/agent/tests/test_read_file_directory_errors.py b/agent/tests/test_read_file_directory_errors.py
new file mode 100644
index 000000000..41da933c8
--- /dev/null
+++ b/agent/tests/test_read_file_directory_errors.py
@@ -0,0 +1,39 @@
+"""Directory read failures follow the same JSON error contract as file reads."""
+
+from __future__ import annotations
+
+import json
+from collections.abc import Iterator
+from pathlib import Path
+
+import pytest
+
+from src.tools.read_file_tool import ReadFileTool
+
+
+def test_unlistable_directory_returns_tool_error(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A supported directory path may fail during the actual listing operation."""
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(tmp_path))
+ run = tmp_path / "run"
+ directory = run / "artifacts"
+ directory.mkdir(parents=True)
+ (directory / "report.txt").write_text("report", encoding="utf-8")
+ real_iterdir = Path.iterdir
+
+ def denied_iterdir(path: Path) -> Iterator[Path]:
+ if path == directory:
+ raise PermissionError("fixture directory cannot be listed")
+ return real_iterdir(path)
+
+ monkeypatch.setattr(Path, "iterdir", denied_iterdir)
+ result = json.loads(ReadFileTool().execute(path="artifacts", run_dir=str(run)))
+ assert result["status"] == "error"
+ assert "fixture directory cannot be listed" in result["error"]
+ # A listing error does not broaden the allowed roots or prevent file reads.
+ control = json.loads(
+ ReadFileTool().execute(path="artifacts/report.txt", run_dir=str(run))
+ )
+ assert control["status"] == "ok"
+ assert control["content"] == "report"