Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): record timeout failures in the backtest tool's state.json

shadowinlife9acae9c8fe31
diff --git a/agent/src/tools/backtest_tool.py b/agent/src/tools/backtest_tool.py
index 7984f79da..59681937a 100644
--- a/agent/src/tools/backtest_tool.py
+++ b/agent/src/tools/backtest_tool.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import subprocess
from pathlib import Path
from backtest.loaders.registry import VALID_SOURCES
@@ -56,12 +57,24 @@ def run_backtest(run_dir: str) -> str:
message=f"running backtest engine (source={source})",
)
runner = Runner(timeout=300)
- result = runner.execute(
- entry_script,
- run_path,
- cwd=agent_root,
- cli_args=[str(run_path)],
- )
+ try:
+ result = runner.execute(
+ entry_script,
+ run_path,
+ cwd=agent_root,
+ cli_args=[str(run_path)],
+ )
+ except subprocess.TimeoutExpired:
+ # The lifecycle block below is unreachable on a timeout, so record the
+ # failure here — otherwise the run is indistinguishable from never-run
+ # (the evidence gate fail-closes either way, but the reason is lost).
+ reason = f"backtest engine timed out after {runner.timeout}s"
+ RunStateStore().mark_failure(run_path, reason)
+ return json.dumps({
+ "status": "error",
+ "error": reason,
+ "run_dir": run_dir,
+ }, ensure_ascii=False)
# Record lifecycle status so tool-driven runs are ingestible by the
# evidence pipeline: refresh_strategy_evidence fail-closes without
diff --git a/agent/tests/test_backtest_tool_state.py b/agent/tests/test_backtest_tool_state.py
index 430fe1c81..49c2c2456 100644
--- a/agent/tests/test_backtest_tool_state.py
+++ b/agent/tests/test_backtest_tool_state.py
@@ -10,6 +10,7 @@ and no state file when validation rejects the run before the engine starts.
from __future__ import annotations
import json
+import subprocess
from dataclasses import dataclass, field
from pathlib import Path
from unittest.mock import patch
@@ -67,3 +68,15 @@ def test_validation_error_records_no_state(tool_run_dir):
assert envelope["status"] == "error"
assert not (tool_run_dir / "state.json").exists()
+
+
+def test_timeout_records_state_failed_and_returns_error_envelope(tool_run_dir):
+ with patch("src.tools.backtest_tool.emit_progress"), patch("src.tools.backtest_tool.Runner") as runner_cls:
+ runner_cls.return_value.timeout = 300
+ runner_cls.return_value.execute.side_effect = subprocess.TimeoutExpired(cmd="runner.py", timeout=300)
+ envelope = json.loads(run_backtest(str(tool_run_dir)))
+
+ assert envelope["status"] == "error"
+ assert envelope["error"] == "backtest engine timed out after 300s"
+ state = json.loads((tool_run_dir / "state.json").read_text(encoding="utf-8"))
+ assert state == {"status": "failed", "reason": "backtest engine timed out after 300s"}