Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): record state.json from the backtest tool so tool-driven runs are ingestible as evidence

shadowinlife5435a7ac268e
diff --git a/agent/src/tools/backtest_tool.py b/agent/src/tools/backtest_tool.py
index 8808c057a..7984f79da 100644
--- a/agent/src/tools/backtest_tool.py
+++ b/agent/src/tools/backtest_tool.py
@@ -9,6 +9,7 @@ from backtest.loaders.registry import VALID_SOURCES
from src.agent.progress import emit_progress
from src.agent.tools import BaseTool
from src.core.runner import Runner
+from src.core.state import RunStateStore
from src.tools.path_utils import safe_run_dir
@@ -62,6 +63,16 @@ def run_backtest(run_dir: str) -> str:
cli_args=[str(run_path)],
)
+ # Record lifecycle status so tool-driven runs are ingestible by the
+ # evidence pipeline: refresh_strategy_evidence fail-closes without
+ # state.json, which previously only the runtime loop wrote (#1412).
+ # Same contract as the runtime — success, or failure with a reason.
+ state_store = RunStateStore()
+ if result.success:
+ state_store.mark_success(run_path)
+ else:
+ state_store.mark_failure(run_path, f"backtest engine exited with code {result.exit_code}")
+
emit_progress("finalize", message="collecting artifacts")
artifacts_found = {name: str(path) for name, path in result.artifacts.items()}
return json.dumps({
diff --git a/agent/tests/test_backtest_tool_state.py b/agent/tests/test_backtest_tool_state.py
new file mode 100644
index 000000000..430fe1c81
--- /dev/null
+++ b/agent/tests/test_backtest_tool_state.py
@@ -0,0 +1,69 @@
+"""The backtest tool must record state.json so evidence ingestion can tell run status.
+
+refresh_strategy_evidence fail-closes on runs without state.json, which used to
+be written only by the runtime loop — so every tool-driven run was rejected as
+unknown-provenance despite complete artifacts (#1412). These tests pin the
+tool-side lifecycle recording: success and failure after an engine execution,
+and no state file when validation rejects the run before the engine starts.
+"""
+
+from __future__ import annotations
+
+import json
+from dataclasses import dataclass, field
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+
+from src.tools.backtest_tool import run_backtest
+
+
+@dataclass
+class _FakeRunResult:
+ success: bool
+ exit_code: int
+ stdout: str = ""
+ stderr: str = ""
+ artifacts: dict = field(default_factory=dict)
+
+
+def tool_run_dir(tmp_path, monkeypatch) -> Path:
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(tmp_path))
+ run_dir = tmp_path / "run"
+ (run_dir / "code").mkdir(parents=True)
+ (run_dir / "config.json").write_text(json.dumps({"source": "yfinance"}), encoding="utf-8")
+ (run_dir / "code" / "signal_engine.py").write_text("", encoding="utf-8")
+ return run_dir
+
+
+def _run_with_result(run_dir: Path, result: _FakeRunResult) -> dict:
+ with patch("src.tools.backtest_tool.emit_progress"), patch("src.tools.backtest_tool.Runner") as runner_cls:
+ runner_cls.return_value.execute.return_value = result
+ return json.loads(run_backtest(str(run_dir)))
+
+
+def test_successful_run_records_state_success(tool_run_dir):
+ envelope = _run_with_result(tool_run_dir, _FakeRunResult(success=True, exit_code=0))
+
+ assert envelope["status"] == "ok"
+ state = json.loads((tool_run_dir / "state.json").read_text(encoding="utf-8"))
+ assert state == {"status": "success"}
+
+
+def test_failed_run_records_state_failed_with_exit_code(tool_run_dir):
+ envelope = _run_with_result(tool_run_dir, _FakeRunResult(success=False, exit_code=3, stderr="boom"))
+
+ assert envelope["status"] == "error"
+ state = json.loads((tool_run_dir / "state.json").read_text(encoding="utf-8"))
+ assert state == {"status": "failed", "reason": "backtest engine exited with code 3"}
+
+
+def test_validation_error_records_no_state(tool_run_dir):
+ (tool_run_dir / "config.json").unlink()
+
+ envelope = json.loads(run_backtest(str(tool_run_dir)))
+
+ assert envelope["status"] == "error"
+ assert not (tool_run_dir / "state.json").exists()