Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): recover exact Alpaca fills

Miranda16f5fb767ee8
diff --git a/agent/src/live/pending_action.py b/agent/src/live/pending_action.py
index a15952079..696f3139b 100644
--- a/agent/src/live/pending_action.py
+++ b/agent/src/live/pending_action.py
@@ -119,7 +119,9 @@ class PendingAction(BaseModel):
schema_version: Literal[1]
broker: Literal["alpaca"]
action_id: str = Field(pattern=r"^act_[0-9a-f]{32}$")
- phase: Literal["pending_write", "resolved_needs_revalidation"]
+ phase: Literal[
+ "pending_write", "resolved_fill_pending_audit", "resolved_needs_revalidation"
+ ]
kind: Literal["place_order"]
created_at: datetime
client_order_id: str = Field(min_length=1, max_length=48)
@@ -141,7 +143,7 @@ class PendingAction(BaseModel):
self.broker_order_id is not None or self.resolution is not None
):
raise ValueError("pending write cannot contain resolved evidence")
- if self.phase == "resolved_needs_revalidation" and (
+ if self.phase != "pending_write" and (
self.resolution is None
or self.broker_order_id != self.resolution.broker_order_id
or self.client_order_id != self.resolution.client_order_id
@@ -234,11 +236,26 @@ def transition_to_revalidation(
action: PendingAction, evidence: Mapping[str, object]
) -> PendingAction:
"""Durably retain exact working-order truth for later policy revalidation."""
+ return _transition(action, evidence, "resolved_needs_revalidation")
+
+
+def transition_to_fill_resolution(
+ action: PendingAction, evidence: Mapping[str, object]
+) -> PendingAction:
+ """Durably retain an attributed fill before its audit/terminal transition."""
+ return _transition(action, evidence, "resolved_fill_pending_audit")
+
+
+def _transition(
+ action: PendingAction,
+ evidence: Mapping[str, object],
+ phase: Literal["resolved_fill_pending_audit", "resolved_needs_revalidation"],
+) -> PendingAction:
resolution = RecoveredOrderEvidence.model_validate(evidence)
updated = PendingAction.model_validate(
{
**action.model_dump(mode="python"),
- "phase": "resolved_needs_revalidation",
+ "phase": phase,
"broker_order_id": resolution.broker_order_id,
"resolution": resolution,
}
diff --git a/agent/src/live/sdk_order_gate.py b/agent/src/live/sdk_order_gate.py
index a63cfc700..8f987f8bd 100644
--- a/agent/src/live/sdk_order_gate.py
+++ b/agent/src/live/sdk_order_gate.py
@@ -45,7 +45,7 @@ from src.live.enforcement import (
instrument_asset_class,
last_price_usd,
)
-from src.live.halt import halt_flag_set
+from src.live.halt import halt_flag_set, trip_halt
from src.live.mandate.model import MANDATE_SCHEMA_VERSION, Mandate
from src.live.mandate.store import load_mandate
@@ -556,15 +556,23 @@ def _recover_pending_order(broker, session_id, connector, config, mandate, actio
filled = Decimal(str(evidence["filled_qty"]))
working = {"accepted", "new", "open", "pending_new", "accepted_for_bidding"}
terminal = {"rejected", "canceled", "expired"}
- if ((status in working or status in terminal) and filled != 0) or (
- status in {"partially_filled", "filled"} and filled <= 0
+ fill_statuses = {"partially_filled", "filled"}
+ if (filled == 0 and status in fill_statuses) or (
+ filled > 0 and status not in fill_statuses | {"canceled", "expired"}
):
- return unresolved("broker status contradicts filled quantity")
+ return _halt_fill_recovery(
+ broker, session_id, mandate, action, evidence, None,
+ "broker status contradicts filled quantity", checked,
+ )
if status != "rejected":
try:
increment_daily_count(broker, action.action_id)
except DailyCountError:
return unresolved("recovered order could not be durably accounted")
+ if filled > 0:
+ return _recover_correlated_fill(
+ broker, session_id, connector, config, mandate, action, evidence, checked
+ )
record = _audit(
broker, session_id,
@@ -601,6 +609,121 @@ def _recover_pending_order(broker, session_id, connector, config, mandate, actio
return _recovery_refusal(broker, "exact fill requires position attribution", record)
+def _recover_correlated_fill(
+ broker, session_id, connector, config, mandate, action, evidence, checked
+):
+ """Attribute an exact quantity fill to the signed broker position delta."""
+ positions = _safe_read(connector, "get_positions", config)
+ request_qty = _exact_decimal(action.request.quantity)
+ filled = _exact_decimal(evidence["filled_qty"])
+ before = _exact_decimal(action.pre_position_qty)
+ after = _exact_position_qty(positions, action.request.symbol)
+ status = evidence["order_status"]
+ coherent_size = (
+ request_qty is not None
+ and filled is not None
+ and 0 < filled <= request_qty
+ and before is not None
+ and after is not None
+ and (status != "filled" or filled == request_qty)
+ and (status not in {"partially_filled", "canceled", "expired"} or filled < request_qty)
+ )
+ expected_after = None
+ if coherent_size:
+ expected_after = before + filled if action.request.side == "buy" else before - filled
+ if not coherent_size or after != expected_after:
+ return _halt_fill_recovery(
+ broker, session_id, mandate, action, evidence, positions,
+ "exact fill does not match durable quantity and signed position evidence",
+ checked,
+ )
+ try:
+ action = pending_action.transition_to_fill_resolution(action, evidence)
+ except Exception as exc: # noqa: BLE001 - original marker remains fail-closed
+ logger.warning("fill resolution persistence failed for %s: %s", broker, exc)
+ return _recovery_refusal(broker, "fill resolution was not durable")
+
+ broker_response = {
+ "order": evidence,
+ "position_evidence": {
+ "pre_position_qty": float(before),
+ "current_position_qty": float(after),
+ "attributed_filled_qty": float(filled),
+ },
+ }
+ record = _audit(
+ broker, session_id, kind="order_placed", outcome="filled", mandate=mandate,
+ intent=None, broker_request=action.request.model_dump(mode="json"),
+ broker_response=broker_response,
+ gate_decision={"allowed": False, "decision": _DECISION_DENY,
+ "checked_limits": checked + ["signed_position_delta"],
+ "reason_code": "exact_fill_attributed", "action_id": action.action_id},
+ )
+ if record is None:
+ return _recovery_refusal(broker, "fill attribution audit was not durable")
+ if status == "partially_filled":
+ try:
+ pending_action.transition_to_revalidation(action, evidence)
+ except Exception as exc: # noqa: BLE001 - retain exact evidence
+ logger.warning("fill recovery transition failed for %s: %s", broker, exc)
+ return _recovery_refusal(broker, "fill transition was not durable", record)
+ return _recovery_refusal(
+ broker, "working partial fill requires policy revalidation", record,
+ reason_code="pending_action_needs_revalidation",
+ )
+ try:
+ pending_action.clear_pending_action(broker, action.action_id)
+ except Exception as exc: # noqa: BLE001 - retain exact evidence
+ logger.warning("fill recovery clear failed for %s: %s", broker, exc)
+ return _recovery_refusal(broker, "resolved fill could not be cleared", record)
+ return _recovery_refusal(
+ broker, "exact terminal fill recovered", record,
+ reason_code="pending_action_resolved_fill", recovery_pending=False,
+ )
+
+
+def _halt_fill_recovery(broker, session_id, mandate, action, evidence, positions, reason, checked):
+ try:
+ trip_halt(by="file", reason=reason, broker=broker)
+ except Exception as exc: # noqa: BLE001 - marker remains the fail-closed gate
+ logger.error("fill recovery could not persist broker halt for %s: %s", broker, exc)
+ record = _audit(
+ broker, session_id, kind="halt_tripped", outcome="blocked", mandate=mandate,
+ intent=None, broker_request=action.request.model_dump(mode="json"),
+ broker_response={"order": evidence, "positions": positions},
+ gate_decision={"allowed": False, "decision": _DECISION_DENY,
+ "checked_limits": checked + ["signed_position_delta"],
+ "reason_code": "pending_action_fill_inconsistent",
+ "action_id": action.action_id}, error=reason,
+ )
+ return _recovery_refusal(
+ broker, reason, record, reason_code="pending_action_fill_inconsistent"
+ )
+
+
+def _exact_position_qty(positions, symbol) -> Decimal | None:
+ rows = positions.get("positions", positions.get("data")) if isinstance(positions, dict) else positions
+ if not isinstance(rows, list):
+ return None
+ target = str(symbol).strip().upper()
+ matches = [row for row in rows if isinstance(row, dict)
+ and str(row.get("symbol") or "").strip().upper() == target]
+ if not matches:
+ return Decimal(0)
+ if len(matches) != 1:
+ return None
+ row = matches[0]
+ try:
+ quantity = _exact_decimal(row.get("quantity", row.get("qty")))
+ except InvalidOperation:
+ return None
+ if quantity is None:
+ return None
+ if str(row.get("side") or "").strip().lower() in {"short", "sell"} and quantity > 0:
+ quantity = -quantity
+ return quantity
+
+
def _validate_recovery_evidence(action, response) -> dict[str, Any] | None:
order = response.get("order") if isinstance(response, dict) and response.get("status") == "ok" else None
if (
diff --git a/agent/tests/test_sdk_order_gate.py b/agent/tests/test_sdk_order_gate.py
index 3cc649245..4ce21fe79 100644
--- a/agent/tests/test_sdk_order_gate.py
+++ b/agent/tests/test_sdk_order_gate.py
@@ -18,6 +18,7 @@ import pytest
import src.live.paths as live_paths
from src.config.accessor import reset_env_config
from src.live import audit as live_audit
+from src.live import halt as live_halt
from src.live import pending_action as pending_state
from src.live import sdk_order_gate as gate
from src.live.daily_count import read_daily_count
@@ -439,9 +440,115 @@ def test_exact_fill_is_counted_but_retained_for_position_attribution(monkeypatch
result = _place(connector)
- assert result["reason_code"] == "pending_action_unresolved"
+ assert result["reason_code"] == "pending_action_fill_inconsistent"
assert load_pending_action("alpaca").phase == "pending_write"
assert counted == [action.action_id] and len(connector.placed) == 1
+ assert live_halt.halt_flag_set("alpaca") is True
+
+
+ ("side", "before", "filled", "after", "status", "phase"),
+ [
+ ("buy", 25, 30, 55, "partially_filled", "resolved_needs_revalidation"),
+ ("buy", 25, 100, 125, "filled", None),
+ ("sell", 25, 100, -75, "filled", None),
+ ("buy", 25, 30, 55, "canceled", None),
+ ("sell", 100, 100, None, "filled", None),
+ ],
+)
+def test_exact_quantity_fill_requires_matching_signed_position(
+ monkeypatch, side, before, filled, after, status, phase,
+) -> None:
+ _patch_gate(monkeypatch, mandate=_mandate())
+ connector = _LostResponseConnector(
+ positions={"status": "ok", "positions": [{"symbol": "AAPL", "quantity": before,
+ "market_value": before * 100, "side": "long"}]}
+ )
+ submitted = _place(connector, side=side, quantity=100, notional=None)
+ action = load_pending_action("alpaca")
+ assert action is not None, submitted
+ connector.lookup_result = _exact_order(action, status=status, filled_qty=str(filled))
+ connector._positions = {
+ "status": "ok",
+ "positions": ([] if after is None else [{"symbol": "AAPL", "quantity": after}]),
+ }
+
+ result = _place(connector)
+ persisted = load_pending_action("alpaca")
+
+ expected_code = "pending_action_needs_revalidation" if phase else "pending_action_resolved_fill"
+ assert result["reason_code"] == expected_code
+ assert (persisted.phase if persisted else None) == phase
+ assert len(connector.placed) == 1 and live_halt.halt_flag_set("alpaca") is False
+
+
+ ("quantity", "filled", "after"),
+ [(100, 101, 126), (100, 30, 54), (100, 0, 25), (None, 5, 5)],
+)
+def test_unattributable_fill_halts_and_retains_exact_evidence(
+ monkeypatch, quantity, filled, after,
+) -> None:
+ _patch_gate(monkeypatch, mandate=_mandate())
+ connector = _LostResponseConnector(
+ positions={"status": "ok", "positions": [{"symbol": "AAPL", "quantity": 25,
+ "market_value": 2500, "side": "long"}]}
+ )
+ submitted = _place(connector, quantity=quantity, notional=500.0 if quantity is None else None)
+ action = load_pending_action("alpaca")
+ assert action is not None, submitted
+ connector.lookup_result = _exact_order(action, status="filled", filled_qty=str(filled))
+ connector._positions = {
+ "status": "ok", "positions": [{"symbol": "AAPL", "quantity": after}]
+ }
+
+ result = _place(connector)
+
+ assert result["reason_code"] == "pending_action_fill_inconsistent"
+ assert load_pending_action("alpaca").action_id == action.action_id
+ assert len(connector.placed) == 1 and live_halt.halt_flag_set("alpaca") is True
+
+
+def test_fill_position_read_failure_halts_without_clearing(monkeypatch) -> None:
+ _patch_gate(monkeypatch, mandate=_mandate())
+ connector = _LostResponseConnector(positions={"status": "ok", "positions": []})
+ _place(connector, quantity=10, notional=None)
+ action = load_pending_action("alpaca")
+ connector.lookup_result = _exact_order(action, status="filled", filled_qty="10")
+ connector._positions = {"status": "error", "error": "unavailable"}
+
+ result = _place(connector)
+
+ assert result["reason_code"] == "pending_action_fill_inconsistent"
+ assert load_pending_action("alpaca") is not None
+ assert len(connector.placed) == 1 and live_halt.halt_flag_set("alpaca") is True
+
+
+def test_attributed_fill_survives_audit_failure_and_replays_without_submit(monkeypatch) -> None:
+ counted: set[str] = set()
+ _patch_gate(monkeypatch, mandate=_mandate())
+ monkeypatch.setattr(gate, "increment_daily_count",
+ lambda broker, action_id=None: counted.add(action_id) or 1)
+ connector = _LostResponseConnector(positions={"status": "ok", "positions": []})
+ _place(connector, quantity=10, notional=None)
+ action = load_pending_action("alpaca")
+ connector.lookup_result = _exact_order(action, status="filled", filled_qty="10")
+ connector._positions = {
+ "status": "ok", "positions": [{"symbol": "AAPL", "quantity": 10}]
+ }
+ monkeypatch.setattr(gate, "write_live_action", lambda *args, **kwargs: None)
+
+ interrupted = _place(connector)
+ persisted = load_pending_action("alpaca")
+ monkeypatch.setattr(gate, "write_live_action", lambda *args, **kwargs: {"audited": True})
+ replay = _place(connector)
+
+ assert interrupted["reason_code"] == "pending_action_unresolved"
+ assert persisted.phase == "resolved_fill_pending_audit"
+ assert replay["reason_code"] == "pending_action_resolved_fill"
+ assert load_pending_action("alpaca") is None and counted == {action.action_id}
+ assert len(connector.placed) == 1
+ assert connector.lookups == [action.client_order_id] * 2
def test_corrupt_pending_marker_and_failed_audit_both_fail_closed(monkeypatch) -> None: