"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(sandbox): preserve canonical run root after HOME isolation (#1012)
hhjmiro8b4ca6773025
diff --git a/agent/src/core/runner.py b/agent/src/core/runner.pyindex 421c0dfe4..80a7bce5b 100644--- a/agent/src/core/runner.py+++ b/agent/src/core/runner.py@@ -448,6 +448,24 @@ class Runner:})+ # ``execute()`` replaces HOME with an ephemeral sandbox directory. The+ # backtest entry point validates ``run_dir`` again in that child+ # process, so its HOME-derived default roots no longer include a run+ # created under the real ``~/.vibe-trading/runs``. Carry the exact+ # current run directory across the boundary as an explicit root. Using+ # the run itself (rather than its parent) keeps the sandbox grant as+ # narrow as possible while ensuring artifacts land in the canonical+ # directory indexed by the Reports API.+ allowed_run_roots = [+ item.strip()+ for item in env.get("VIBE_TRADING_ALLOWED_RUN_ROOTS", "").split(",")+ if item.strip()+ ]+ current_run_root = str(run_dir.resolve())+ if current_run_root not in allowed_run_roots:+ allowed_run_roots.append(current_run_root)+ env["VIBE_TRADING_ALLOWED_RUN_ROOTS"] = ",".join(allowed_run_roots)+if pythonpath_extra:existing = env.get("PYTHONPATH", "")env["PYTHONPATH"] = str(pythonpath_extra) + (os.pathsep + existing if existing else "")diff --git a/agent/tests/test_runner_env.py b/agent/tests/test_runner_env.pyindex af49acd5a..f616005cd 100644--- a/agent/tests/test_runner_env.py+++ b/agent/tests/test_runner_env.py@@ -99,6 +99,51 @@ def test_backtest_runtime_env_prepends_runtime_pythonpath(assert env["PYTHONPATH"] == f"{pythonpath_extra}{os.pathsep}existing-path"+def test_backtest_runtime_env_adds_exact_current_run_root(+ monkeypatch,+ tmp_path: Path,+) -> None:+ configured_root = tmp_path / "configured-runs"+ run_dir = tmp_path / "state-root" / "runs" / "run-1"+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(configured_root))++ env = Runner(timeout=1)._build_runtime_env(run_dir)++ assert env["VIBE_TRADING_ALLOWED_RUN_ROOTS"].split(",") == [+ str(configured_root),+ str(run_dir.resolve()),+ ]+++def test_execute_keeps_real_home_run_valid_after_home_is_sandboxed(+ monkeypatch,+ tmp_path: Path,+) -> None:+ real_home = tmp_path / "real-home"+ run_dir = real_home / ".vibe-trading" / "runs" / "run-1"+ run_dir.mkdir(parents=True)+ monkeypatch.setenv("HOME", str(real_home))+ monkeypatch.delenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", raising=False)++ entry = _probe_entry(+ tmp_path,+ "import sys\n"+ "from src.tools.path_utils import safe_run_dir\n"+ "print(safe_run_dir(sys.argv[1]))\n",+ )+ agent_root = Path(runner_mod.__file__).resolve().parents[2]++ result = Runner(timeout=60).execute(+ entry,+ run_dir,+ cwd=agent_root,+ cli_args=[str(run_dir)],+ )++ assert result.success, result.stderr+ assert str(run_dir.resolve()) in result.stdout++# --------------------------------------------------------------------------- ## VT-001 runtime defense-in-depth: ephemeral HOME, UID-drop fallback, rlimits.# --------------------------------------------------------------------------- #