Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: finish optional Robinhood buying power validation and CSV regressions

Haozhe Wu46892136da6b
diff --git a/agent/src/trading/connectors/robinhood/mcp.py b/agent/src/trading/connectors/robinhood/mcp.py
index beaca2fa8..27a19f817 100644
--- a/agent/src/trading/connectors/robinhood/mcp.py
+++ b/agent/src/trading/connectors/robinhood/mcp.py
@@ -180,8 +180,9 @@ def portfolio_summary(envelope: Any) -> dict[str, Any]:
reply omitted it. Values stay decimal strings.
Raises:
- RobinhoodShapeError: If ``total_value`` or ``currency`` is missing, or a
- value that is present is not a finite number.
+ RobinhoodShapeError: If ``total_value`` or ``currency`` is missing,
+ non-null ``buying_power`` is not an object, or a value that is
+ present is not a finite number.
"""
tool = "get_portfolio"
data = broker_data(envelope, tool)
@@ -189,13 +190,17 @@ def portfolio_summary(envelope: Any) -> dict[str, Any]:
if not isinstance(currency, str) or not currency.strip():
raise RobinhoodShapeError(f"{tool} currency is missing")
buying_power = data.get("buying_power")
- if not isinstance(buying_power, dict):
+ if buying_power is not None and not isinstance(buying_power, dict):
raise RobinhoodShapeError(f"{tool} buying_power is not an object")
- raw_buying_power = buying_power.get("buying_power")
+ raw_buying_power = buying_power.get("buying_power") if isinstance(buying_power, dict) else None
return {
"portfolio_value": _decimal_text(data.get("total_value"), "total_value", tool),
"cash": _decimal_text(data["cash"], "cash", tool) if data.get("cash") is not None else None,
- "buying_power": _decimal_text(raw_buying_power, "buying_power.buying_power", tool),
+ "buying_power": (
+ _decimal_text(raw_buying_power, "buying_power.buying_power", tool)
+ if raw_buying_power is not None
+ else None
+ ),
"currency": currency.strip().upper(),
"non_equity_values": {
field: _decimal_text(data[field], field, tool) if data.get(field) is not None else None
diff --git a/agent/tests/test_loop_helpers.py b/agent/tests/test_loop_helpers.py
index 72dc9d04e..69c64da6a 100644
--- a/agent/tests/test_loop_helpers.py
+++ b/agent/tests/test_loop_helpers.py
@@ -20,7 +20,6 @@ from src.agent.loop import (
_archive_backtest_result,
_llm_timeout_seconds,
_stall_timeout_seconds,
- _verification_ledger,
_cleared_text,
)
diff --git a/agent/tests/test_robinhood_mcp_mapping.py b/agent/tests/test_robinhood_mcp_mapping.py
index 140a7c61e..009e74628 100644
--- a/agent/tests/test_robinhood_mcp_mapping.py
+++ b/agent/tests/test_robinhood_mcp_mapping.py
@@ -55,14 +55,50 @@ def test_an_omitted_non_equity_value_is_unknown_not_zero() -> None:
assert mcp.portfolio_summary(envelope)["non_equity_values"]["crypto_value"] is None
-def test_portfolio_summary_rejects_an_unmapped_buying_power_shape() -> None:
[email protected]("buying_power", [[], ["5000.00"], "5000.00", 5000, False])
+def test_portfolio_summary_rejects_an_unmapped_buying_power_shape(buying_power) -> None:
envelope = rh.portfolio()
- envelope["structured_content"]["data"]["buying_power"] = ["5000.00"]
+ envelope["structured_content"]["data"]["buying_power"] = buying_power
with pytest.raises(mcp.RobinhoodShapeError, match="buying_power is not an object"):
mcp.portfolio_summary(envelope)
[email protected]("buying_power", [None, {}, {"buying_power": None}])
+def test_portfolio_summary_preserves_unknown_buying_power(buying_power) -> None:
+ envelope = rh.portfolio(total_value="1234.50")
+ envelope["structured_content"]["data"]["buying_power"] = buying_power
+
+ summary = mcp.portfolio_summary(envelope)
+
+ assert summary["buying_power"] is None
+ assert summary["portfolio_value"] == "1234.50"
+
+
+def test_portfolio_summary_allows_omitted_buying_power() -> None:
+ envelope = rh.portfolio()
+ del envelope["structured_content"]["data"]["buying_power"]
+
+ assert mcp.portfolio_summary(envelope)["buying_power"] is None
+
+
[email protected]("value", ["NaN", "Infinity", "-Infinity", "invalid", [], {}])
+def test_portfolio_summary_rejects_invalid_nested_buying_power(value) -> None:
+ envelope = rh.portfolio()
+ envelope["structured_content"]["data"]["buying_power"] = {"buying_power": value}
+
+ with pytest.raises(mcp.RobinhoodShapeError, match="buying_power.buying_power"):
+ mcp.portfolio_summary(envelope)
+
+
[email protected]("value", ["0", 0, "180.25"])
+def test_portfolio_summary_keeps_finite_buying_power(value) -> None:
+ envelope = rh.portfolio()
+ envelope["structured_content"]["data"]["buying_power"] = {"buying_power": value}
+
+ assert mcp.portfolio_summary(envelope)["buying_power"] == str(value)
+
+
def test_position_rows_map_symbol_quantity_and_cost() -> None:
rows = mcp.position_rows(
rh.positions(
diff --git a/agent/tests/test_session_load_metrics.py b/agent/tests/test_session_load_metrics.py
index 06528f45d..7b54d2ce0 100644
--- a/agent/tests/test_session_load_metrics.py
+++ b/agent/tests/test_session_load_metrics.py
@@ -6,6 +6,8 @@ from __future__ import annotations
from pathlib import Path
+import pytest
+
from src.session.service import SessionService
@@ -52,3 +54,18 @@ def test_empty_field_values_are_skipped(tmp_path: Path) -> None:
metrics = SessionService._load_metrics(tmp_path)
assert metrics == {"total_return": 0.15}
+
+
[email protected]("contents", [b"", b"total_return,sharpe\n", b"total_return\n\xff\n"])
+def test_empty_or_unreadable_csv_returns_none(tmp_path: Path, contents: bytes) -> None:
+ artifacts = tmp_path / "artifacts"
+ artifacts.mkdir()
+ (artifacts / "metrics.csv").write_bytes(contents)
+
+ assert SessionService._load_metrics(tmp_path) is None
+
+
+def test_extra_csv_cells_do_not_discard_named_numeric_metrics(tmp_path: Path) -> None:
+ _write_metrics_csv(tmp_path, "total_return,benchmark_ticker", "0,SPY,extra")
+
+ assert SessionService._load_metrics(tmp_path) == {"total_return": 0.0}