"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(session): stop one non-numeric metrics.csv field from dropping all run metrics
Lanre Shittu2c1c185b22b7
diff --git a/agent/src/session/service.py b/agent/src/session/service.pyindex a0254d73e..e80bc0fc1 100644--- a/agent/src/session/service.py+++ b/agent/src/session/service.py@@ -700,7 +700,13 @@ class SessionService:@staticmethoddef _load_metrics(run_dir: Path) -> Optional[Dict[str, Any]]:- """Load metrics.csv from a run directory."""+ """Load metrics.csv from a run directory.++ A run's metrics row can carry a non-numeric column (e.g.+ ``benchmark_ticker``) alongside the numeric metrics, so each field is+ converted independently: one unconvertible field is skipped rather+ than discarding every metric in the row.+ """import csvmetrics_path = run_dir / "artifacts" / "metrics.csv"if not metrics_path.exists():@@ -708,11 +714,19 @@ class SessionService:try:with open(metrics_path, "r", encoding="utf-8") as f:rows = list(csv.DictReader(f))- if rows:- return {k: float(v) for k, v in rows[0].items() if v}except Exception:- pass- return None+ return None+ if not rows:+ return None+ metrics: Dict[str, Any] = {}+ for key, value in rows[0].items():+ if not value:+ continue+ try:+ metrics[key] = float(value)+ except (TypeError, ValueError):+ continue+ return metrics or None@staticmethoddef _format_result_message(attempt: Attempt) -> str:diff --git a/agent/tests/test_session_load_metrics.py b/agent/tests/test_session_load_metrics.pynew file mode 100644index 000000000..06528f45d--- /dev/null+++ b/agent/tests/test_session_load_metrics.py@@ -0,0 +1,54 @@+"""``SessionService._load_metrics`` must not discard the whole row over one+non-numeric field (e.g. a benchmark ticker column written into metrics.csv).+"""++from __future__ import annotations++from pathlib import Path++from src.session.service import SessionService+++def _write_metrics_csv(run_dir: Path, header: str, row: str) -> None:+ artifacts = run_dir / "artifacts"+ artifacts.mkdir(parents=True, exist_ok=True)+ (artifacts / "metrics.csv").write_text(f"{header}\n{row}\n", encoding="utf-8")+++def test_non_numeric_field_is_skipped_not_fatal(tmp_path: Path) -> None:+ """A stray string column (e.g. benchmark_ticker) must not blank out real metrics."""+ _write_metrics_csv(+ tmp_path,+ "total_return,sharpe,benchmark_ticker",+ "0.15,1.2,SPY",+ )++ metrics = SessionService._load_metrics(tmp_path)++ assert metrics == {"total_return": 0.15, "sharpe": 1.2}+++def test_all_numeric_row_is_unaffected(tmp_path: Path) -> None:+ _write_metrics_csv(tmp_path, "total_return,sharpe", "0.15,1.2")++ metrics = SessionService._load_metrics(tmp_path)++ assert metrics == {"total_return": 0.15, "sharpe": 1.2}+++def test_missing_metrics_file_returns_none(tmp_path: Path) -> None:+ assert SessionService._load_metrics(tmp_path) is None+++def test_row_with_only_non_numeric_fields_returns_none(tmp_path: Path) -> None:+ _write_metrics_csv(tmp_path, "benchmark_ticker", "SPY")++ assert SessionService._load_metrics(tmp_path) is None+++def test_empty_field_values_are_skipped(tmp_path: Path) -> None:+ _write_metrics_csv(tmp_path, "total_return,sharpe", "0.15,")++ metrics = SessionService._load_metrics(tmp_path)++ assert metrics == {"total_return": 0.15}