Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): halt on recorded-only positions

xkam7ar0940c15b3372
diff --git a/agent/src/live/runtime/reconcile.py b/agent/src/live/runtime/reconcile.py
index 0bc341246..b77ea1769 100644
--- a/agent/src/live/runtime/reconcile.py
+++ b/agent/src/live/runtime/reconcile.py
@@ -463,10 +463,12 @@ def _diff_positions(
}
deltas: list[ReconcileDelta] = []
+ broker_symbols: set[str] = set()
for position in broker_positions:
symbol = _position_symbol(position)
if symbol is None:
continue
+ broker_symbols.add(symbol)
broker_qty = _position_qty(position)
recorded_qty = recorded_by_symbol.get(symbol)
if recorded_qty is not None and recorded_qty == broker_qty:
@@ -501,6 +503,25 @@ def _diff_positions(
broker=dict(position),
)
)
+
+ for symbol, recorded_qty in recorded_by_symbol.items():
+ if symbol in broker_symbols or recorded_qty == 0:
+ continue
+ deltas.append(
+ ReconcileDelta(
+ kind=DeltaKind.UNKNOWN_FILL,
+ subject="position",
+ identity=symbol,
+ client_order_id=None,
+ detail=(
+ "recorded nonzero position is absent from broker truth "
+ f"(recorded={recorded_qty}, broker=absent); real money "
+ "moved without a matching audit record"
+ ),
+ recorded={"symbol": symbol, "qty": recorded_qty},
+ broker=None,
+ )
+ )
return deltas
diff --git a/agent/tests/test_runtime_reconcile.py b/agent/tests/test_runtime_reconcile.py
index b402fbb63..5e05e017f 100644
--- a/agent/tests/test_runtime_reconcile.py
+++ b/agent/tests/test_runtime_reconcile.py
@@ -145,6 +145,27 @@ def test_unknown_fill_on_quantity_drift(live_runtime: Path) -> None:
assert report.requires_halt is True
[email protected]("quantity", [3, -3])
+def test_recorded_position_missing_at_broker_forces_halt(
+ live_runtime: Path,
+ quantity: int,
+) -> None:
+ """A recorded long or short cannot disappear without a halting delta."""
+ state_path = _seed_state(
+ "robinhood",
+ positions=[{"symbol": "NVDA", "qty": quantity}],
+ )
+ before = state_path.read_text(encoding="utf-8")
+ rp, rb, ro = _readers(positions=[])
+
+ report = reconcile("robinhood", rp, rb, ro)
+
+ assert DeltaKind.UNKNOWN_FILL in _kinds(report)
+ assert report.requires_halt is True
+ assert report.state_persisted is False
+ assert state_path.read_text(encoding="utf-8") == before
+
+
# ---------------------------------------------------------------------------
# orphan_order (we recorded a CONFIRMED order the broker no longer shows)
# ---------------------------------------------------------------------------