Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(portfolio): a positions read with no positions list is an error, not an empty source

Haozhe Wu8e6a369088c5
diff --git a/agent/src/portfolio/compatibility.py b/agent/src/portfolio/compatibility.py
index a71d1c9af..534bdd5d9 100644
--- a/agent/src/portfolio/compatibility.py
+++ b/agent/src/portfolio/compatibility.py
@@ -134,7 +134,10 @@ def adapt_and_validate_payloads(
account = dict(account_payload)
positions = dict(positions_payload)
- raw_rows = positions.get("positions", [])
+ # No default: a read that never produced a positions list (an unmapped MCP
+ # envelope, a text-only reply) is not an empty portfolio. Reading it as one
+ # would store a complete snapshot of a source that holds nothing.
+ raw_rows = positions.get("positions")
if not isinstance(raw_rows, list):
raise PortfolioContractError("positions payload must contain a list")
rows = [dict(row) if isinstance(row, dict) else row for row in raw_rows]
diff --git a/agent/tests/test_portfolio_compatibility.py b/agent/tests/test_portfolio_compatibility.py
index 17092b98a..cde1ec455 100644
--- a/agent/tests/test_portfolio_compatibility.py
+++ b/agent/tests/test_portfolio_compatibility.py
@@ -60,6 +60,16 @@ def test_contract_rejects_position_rows_without_symbol_or_quantity():
adapt_and_validate_payloads("sample", {"account": {}}, {"positions": [{"symbol": "DEMO"}]})
+def test_contract_rejects_a_positions_read_without_a_positions_list():
+ unmapped = {"status": "ok", "structured_content": {"holdings": [{"symbol": "DEMO", "quantity": 1}]}}
+ for payload in ({}, {"status": "ok"}, unmapped):
+ with pytest.raises(PortfolioContractError, match="must contain a list"):
+ adapt_and_validate_payloads("sample", {"account": {}}, payload)
+
+ _, positions = adapt_and_validate_payloads("sample", {"account": {}}, {"positions": []})
+ assert positions["positions"] == []
+
+
def test_okx_account_details_are_adapted_to_spot_positions():
account, positions = adapt_and_validate_payloads(
"okx",
diff --git a/agent/tests/test_portfolio_service.py b/agent/tests/test_portfolio_service.py
index 8ef71b667..753acf85c 100644
--- a/agent/tests/test_portfolio_service.py
+++ b/agent/tests/test_portfolio_service.py
@@ -156,6 +156,38 @@ def test_partial_refresh_is_saved_and_marked_incomplete(tmp_path):
assert any("longbridge" in warning.lower() for warning in snapshot["warnings"])
+def test_a_positions_read_without_a_positions_list_is_an_error_not_an_empty_source(tmp_path):
+ def get_positions(profile_id):
+ if profile_id.startswith("ibkr"):
+ return {"status": "ok", "structured_content": "positions table rendered as text"}
+ return {"positions": []}
+
+ service = PortfolioService(
+ PortfolioStore(tmp_path / "portfolio.sqlite3"),
+ settings_store=_settings_store(tmp_path),
+ get_account=lambda profile_id: {"summary": []},
+ get_positions=get_positions,
+ get_quote=lambda *args, **kwargs: {},
+ fx_fetcher=lambda: (
+ Decimal("7.2"),
+ Decimal("7.8"),
+ "2026-08-09T00:00:00+00:00",
+ ),
+ )
+
+ snapshot = service.refresh()
+
+ assert snapshot["complete"] is False
+ statuses = {row["broker"]: row["status"] for row in snapshot["accounts"]}
+ assert statuses["ibkr"] == "error"
+ assert {broker: status for broker, status in statuses.items() if broker != "ibkr"} == {
+ "longbridge": "ok",
+ "binance": "ok",
+ }
+ ibkr = next(row for row in snapshot["accounts"] if row["broker"] == "ibkr")
+ assert "must contain a list" in ibkr["error"]
+
+
def test_failed_source_is_excluded_from_totals_and_reports_its_last_success(tmp_path):
"""A source that fails contributes nothing; only its last-healthy time survives."""
offline = False