"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(mcp): harden Alpha Bench report writes
cgycorey76038caa7840
diff --git a/agent/src/tools/alpha_bench_tool.py b/agent/src/tools/alpha_bench_tool.pyindex 1653db787..be38bda23 100644--- a/agent/src/tools/alpha_bench_tool.py+++ b/agent/src/tools/alpha_bench_tool.py@@ -909,7 +909,7 @@ def run_alpha_bench(**kwargs: Any) -> dict[str, Any]:output_dir.mkdir(parents=True, exist_ok=True)ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")- report_path = output_dir / f"alpha_bench_{ts}.html"+ report_path = output_dir / f"alpha_bench_{ts}_{secrets.token_hex(16)}.html"context = {"csp": _CSP,@@ -924,7 +924,11 @@ def run_alpha_bench(**kwargs: Any) -> dict[str, Any]:}try:- report_path.write_text(_render_html(context), encoding="utf-8")+ report_html = _render_html(context)+ flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)+ report_fd = os.open(report_path, flags, 0o666)+ with os.fdopen(report_fd, "w", encoding="utf-8") as report_file:+ report_file.write(report_html)except OSError as exc:return {"status": "error", "error": f"failed to write report: {exc}"}diff --git a/agent/tests/test_mcp_alpha_zoo_contract.py b/agent/tests/test_mcp_alpha_zoo_contract.pyindex b22bc47cf..711a2d77c 100644--- a/agent/tests/test_mcp_alpha_zoo_contract.py+++ b/agent/tests/test_mcp_alpha_zoo_contract.py@@ -1,6 +1,7 @@from __future__ import annotationsimport asyncio+import datetime as dtimport inspectimport jsonimport re@@ -9,6 +10,7 @@ from pathlib import Pathimport mcp_serverfrom src.tools.alpha_bench_tool import AlphaBenchToolfrom src.tools.alpha_zoo_tool import AlphaZooTool+from src.tools import alpha_bench_tool as alpha_bench_module_ALPHA_ZOO = getattr(mcp_server.alpha_zoo, "fn", None) or getattr(mcp_server.alpha_zoo, "__wrapped__", mcp_server.alpha_zoo@@ -185,3 +187,56 @@ def test_alpha_bench_rejects_output_outside_allowed_roots(monkeypatch) -> None:assert result["status"] == "error"assert "allowed" in result["error"]assert registry.calls == []+++def test_alpha_bench_does_not_follow_preexisting_report_symlink(monkeypatch, tmp_path: Path) -> None:+ class _BenchRegistry:+ def get(self, alpha_id: str) -> object:+ return object()++ class _FixedDateTime:+ @classmethod+ def now(cls, tz: dt.tzinfo | None = None) -> dt.datetime:+ return dt.datetime(2026, 1, 2, 3, 4, 5, tzinfo=tz)++ output_dir = tmp_path / "reports"+ output_dir.mkdir()+ external_target = tmp_path / "external.html"+ predictable_report_link = output_dir / "alpha_bench_20260102T030405Z.html"+ report_link = output_dir / "alpha_bench_20260102T030405Z_fixed.html"+ predictable_report_link.symlink_to(external_target)+ report_link.symlink_to(external_target)++ monkeypatch.setattr("src.factors.registry.get_default_registry", lambda: _BenchRegistry())+ monkeypatch.setattr(alpha_bench_module, "datetime", _FixedDateTime)+ monkeypatch.setattr(alpha_bench_module.secrets, "token_hex", lambda length: "fixed")+ monkeypatch.setattr(alpha_bench_module, "_load_universe_panel", lambda *args, **kwargs: {})+ monkeypatch.setattr(alpha_bench_module, "_compute_forward_returns", lambda panel: {})+ monkeypatch.setattr(+ alpha_bench_module,+ "_bench_one_alpha",+ lambda *args, **kwargs: {+ "id": "alpha101_001",+ "zoo": "alpha101",+ "theme": [],+ "formula_latex": "1",+ "ic_mean": 0.1,+ "ic_std": 0.2,+ "ir": 0.5,+ "ic_positive_ratio": 0.5,+ "ic_count": 1,+ },+ )++ result = alpha_bench_module.run_alpha_bench(+ alpha_id="alpha101_001",+ universe="sp500",+ period="2020-2021",+ output_dir=str(output_dir),+ )++ assert result["status"] == "error"+ assert "failed to write report" in result["error"]+ assert report_link.is_symlink()+ assert predictable_report_link.is_symlink()+ assert not external_target.exists()diff --git a/agent/tests/test_mcp_new_tools.py b/agent/tests/test_mcp_new_tools.pyindex 636eece48..412aa6433 100644--- a/agent/tests/test_mcp_new_tools.py+++ b/agent/tests/test_mcp_new_tools.py@@ -30,10 +30,9 @@ MIRRORED_TOOL_NAMES = ("research_papers",)-# Tools that mutate state (is_readonly is not True on the agent side) and were-# ALREADY exposed on the MCP surface before the institutional-research tools-# were added. They are sandbox/session writes — a backtest run, a session file-# write, a swarm launch, research-goal bookkeeping, and selecting which broker+# Tools that mutate state (is_readonly is not True on the agent side) but are+# sanctioned sandbox/session writes: Alpha Bench reports, backtest runs, session+# files, swarm launches, research-goal bookkeeping, and selecting which broker# profile subsequent READS use — not broker order flow.## This snapshot is the regression gate: adding an MCP tool that mutates@@ -41,6 +40,7 @@ MIRRORED_TOOL_NAMES = (KNOWN_MUTATING_MCP_TOOLS = frozenset({"add_goal_evidence",+ "alpha_bench","backtest","run_swarm","start_research_goal",