Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(read_file): say where the agent may read, and list a directory

Haozhe Wuc8c2dc3811b2
diff --git a/agent/src/tools/read_file_tool.py b/agent/src/tools/read_file_tool.py
index 933b4eb6c..f4ff8134d 100644
--- a/agent/src/tools/read_file_tool.py
+++ b/agent/src/tools/read_file_tool.py
@@ -23,6 +23,21 @@ _OUTPUT_LIMIT = 50_000
# arbitrary model-supplied relative path never starts reaching into skills/.
_SKILL_RELATIVE_PREFIXES = ("references/", "scripts/")
+# How many entries a directory listing returns.
+_LISTING_LIMIT = 200
+
+# What the refusal says, whichever root the path missed. A risk-parity run once
+# spent all eight of its no-progress rounds trying spellings of the engine's
+# source path (``../../agent/src/backtest``, ``skills/../backtest/engine.py``)
+# against one message that said neither where it may read nor what to do.
+_READABLE = (
+ "read_file opens files under the run directory (a relative path such as "
+ "artifacts/metrics.csv or config.json) and the bundled skill documents "
+ "(skills/<skill>/...). The engine's source code is not readable here; its "
+ "contracts and options are documented in the skills: load_skill(name) lists "
+ "a skill's sections, and load_skill(name, section=...) returns one."
+)
+
def _bundled_skills_dir() -> Path:
"""Return the bundled read-only skills root."""
@@ -123,6 +138,9 @@ class ReadFileTool(BaseTool):
resolved = None
namespaced = False
+ # Whether any root contains the path at all: a path inside a root that
+ # does not exist is "not found", one outside every root is refused.
+ contained = False
# `skills/` is a namespace bound to the bundled read-only skills root.
# Binding the prefix stops a same-named file in run_dir — which the agent
@@ -132,6 +150,7 @@ class ReadFileTool(BaseTool):
namespaced = True
try:
candidate = _safe_path(file_path[len("skills/") :], skills_dir)
+ contained = True
except ValueError:
candidate = None
if candidate is not None and candidate.exists():
@@ -142,6 +161,7 @@ class ReadFileTool(BaseTool):
for root in allowed_roots:
try:
candidate = _safe_path(file_path, root)
+ contained = True
if candidate.exists():
resolved = candidate
break
@@ -178,10 +198,31 @@ class ReadFileTool(BaseTool):
)
if resolved is None:
+ if contained:
+ error_code = "not_found"
+ reason = f"{file_path} was not found."
+ 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": "error",
- "error": f"File not found or path escapes workspace: {file_path}",
+ "status": "ok",
+ "path": str(resolved),
+ "kind": "directory",
+ "entries": entries[:_LISTING_LIMIT],
+ "truncated": len(entries) > _LISTING_LIMIT,
},
ensure_ascii=False,
)
diff --git a/agent/tests/test_read_file_refusals.py b/agent/tests/test_read_file_refusals.py
new file mode 100644
index 000000000..745e444a1
--- /dev/null
+++ b/agent/tests/test_read_file_refusals.py
@@ -0,0 +1,71 @@
+"""``read_file`` says where it may read, and lists a directory it is given.
+
+Regression (2026-09-29, a live risk-parity run on DeepSeek V4): the model went
+looking for the backtest engine's source — ``../../agent/src/backtest``, the
+checkout's absolute path, ``skills/../backtest/engine.py`` — and every attempt
+got "File not found or path escapes workspace". Each spelling was a new call,
+so none was blocked, and none was an observation: eight rounds later the run
+stopped with ``no_progress`` after 93 seconds, before any backtest ran. The
+message said neither which of the two had happened nor where reading is
+allowed, so the model kept guessing.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+from src.tools.read_file_tool import ReadFileTool
+
+
+def run_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(tmp_path))
+ run = tmp_path / "run"
+ (run / "artifacts").mkdir(parents=True)
+ (run / "artifacts" / "metrics.csv").write_text("sharpe\n0.69\n", encoding="utf-8")
+ (run / "artifacts" / ".archived_backtest.json").write_text("{}", encoding="utf-8")
+ return run
+
+
+def _read(path: str, run: Path) -> dict:
+ return json.loads(ReadFileTool().execute(path=path, run_dir=str(run)))
+
+
+ "path",
+ ["../../agent/src/backtest", "skills/../backtest/engine.py", "/etc/hosts"],
+)
+def test_a_path_outside_the_readable_roots_is_refused_as_such(run_dir: Path, path: str) -> None:
+ body = _read(path, run_dir)
+
+ assert body["status"] == "error"
+ assert body["error_code"] == "outside_readable_roots"
+ # The refusal carries the way forward, not just the verdict.
+ assert "run directory" in body["error"] and "skills/" in body["error"]
+ assert "load_skill" in body["error"]
+
+
+def test_a_missing_file_inside_a_root_is_not_found(run_dir: Path) -> None:
+ body = _read("artifacts/equity.csv", run_dir)
+
+ assert body["error_code"] == "not_found"
+ assert "load_skill" in body["error"]
+
+
+def test_a_directory_is_answered_with_its_entries(run_dir: Path) -> None:
+ listing = _read("artifacts", run_dir)
+ skill = _read("skills/strategy-generate", run_dir)
+
+ assert listing["status"] == "ok" and listing["kind"] == "directory"
+ assert listing["entries"] == ["metrics.csv"] # dotfiles stay out
+ assert "SKILL.md" in skill["entries"]
+
+
+def test_a_file_still_reads_as_before(run_dir: Path) -> None:
+ body = _read("artifacts/metrics.csv", run_dir)
+
+ assert body["status"] == "ok"
+ assert body["content"] == "sharpe\n0.69\n"