Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): persist exact fill position evidence

Miranda2b2fcbbaf37e
diff --git a/agent/src/live/pending_action.py b/agent/src/live/pending_action.py
index 696f3139b..d43c9d546 100644
--- a/agent/src/live/pending_action.py
+++ b/agent/src/live/pending_action.py
@@ -113,6 +113,31 @@ class RecoveredOrderEvidence(BaseModel):
return self
+class RecoveredPositionEvidence(BaseModel):
+ model_config = ConfigDict(extra="forbid", frozen=True, strict=True)
+
+ pre_position_qty: str
+ current_position_qty: str
+ attributed_filled_qty: str
+
+ @model_validator(mode="after")
+ def validate_quantities(self) -> Self:
+ try:
+ values = [
+ Decimal(value)
+ for value in (
+ self.pre_position_qty,
+ self.current_position_qty,
+ self.attributed_filled_qty,
+ )
+ ]
+ except InvalidOperation as exc:
+ raise ValueError("position evidence must be exact decimals") from exc
+ if any(not value.is_finite() for value in values) or values[2] <= 0:
+ raise ValueError("position evidence must be finite with a positive fill")
+ return self
+
+
class PendingAction(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True)
@@ -127,20 +152,26 @@ class PendingAction(BaseModel):
client_order_id: str = Field(min_length=1, max_length=48)
broker_order_id: str | None = None
request: PendingOrderRequest
- pre_position_qty: float | int | None
+ pre_position_qty: str | float | int | None
remediation_attempts: Literal[0]
resolution: RecoveredOrderEvidence | None = None
+ position_resolution: RecoveredPositionEvidence | None = None
@model_validator(mode="after")
def validate_evidence(self) -> Self:
if self.created_at.tzinfo is None:
raise ValueError("created_at requires a timezone")
- if self.pre_position_qty is not None and not math.isfinite(
- self.pre_position_qty
- ):
- raise ValueError("pre-position quantity must be finite")
+ if self.pre_position_qty is not None:
+ try:
+ pre_position = Decimal(str(self.pre_position_qty))
+ except InvalidOperation as exc:
+ raise ValueError("pre-position quantity must be exact") from exc
+ if not pre_position.is_finite():
+ raise ValueError("pre-position quantity must be finite")
if self.phase == "pending_write" and (
- self.broker_order_id is not None or self.resolution is not None
+ self.broker_order_id is not None
+ or self.resolution is not None
+ or self.position_resolution is not None
):
raise ValueError("pending write cannot contain resolved evidence")
if self.phase != "pending_write" and (
@@ -171,6 +202,31 @@ class PendingAction(BaseModel):
)
):
raise ValueError("resolved evidence contradicts the owned request")
+ if (
+ self.phase == "resolved_fill_pending_audit"
+ and self.position_resolution is None
+ ):
+ raise ValueError("fill resolution requires exact position evidence")
+ if self.position_resolution is not None:
+ if self.resolution is None or self.pre_position_qty is None:
+ raise ValueError(
+ "position resolution requires order and pre-position evidence"
+ )
+ before = Decimal(self.position_resolution.pre_position_qty)
+ after = Decimal(self.position_resolution.current_position_qty)
+ attributed = Decimal(self.position_resolution.attributed_filled_qty)
+ filled = Decimal(str(self.resolution.filled_qty))
+ expected = (
+ before + attributed
+ if self.request.side == "buy"
+ else before - attributed
+ )
+ if (
+ before != Decimal(str(self.pre_position_qty))
+ or attributed != filled
+ or after != expected
+ ):
+ raise ValueError("position resolution contradicts the owned order")
return self
@@ -178,7 +234,7 @@ def new_pending_order(
broker: str,
request: Mapping[str, object],
*,
- pre_position_qty: float | None,
+ pre_position_qty: str | float | int | None,
) -> PendingAction:
"""Build the redaction-safe marker written immediately before submission."""
key = _broker_key(broker)
@@ -240,24 +296,39 @@ def transition_to_revalidation(
def transition_to_fill_resolution(
- action: PendingAction, evidence: Mapping[str, object]
+ action: PendingAction,
+ evidence: Mapping[str, object],
+ position_evidence: Mapping[str, object],
) -> PendingAction:
"""Durably retain an attributed fill before its audit/terminal transition."""
- return _transition(action, evidence, "resolved_fill_pending_audit")
+ return _transition(
+ action,
+ evidence,
+ "resolved_fill_pending_audit",
+ position_evidence=position_evidence,
+ )
def _transition(
action: PendingAction,
evidence: Mapping[str, object],
phase: Literal["resolved_fill_pending_audit", "resolved_needs_revalidation"],
+ *,
+ position_evidence: Mapping[str, object] | None = None,
) -> PendingAction:
resolution = RecoveredOrderEvidence.model_validate(evidence)
+ position_resolution = (
+ RecoveredPositionEvidence.model_validate(position_evidence)
+ if position_evidence is not None
+ else action.position_resolution
+ )
updated = PendingAction.model_validate(
{
**action.model_dump(mode="python"),
"phase": phase,
"broker_order_id": resolution.broker_order_id,
"resolution": resolution,
+ "position_resolution": position_resolution,
}
)
current = load_pending_action(action.broker)
diff --git a/agent/src/live/sdk_order_gate.py b/agent/src/live/sdk_order_gate.py
index 8f987f8bd..11e8b258c 100644
--- a/agent/src/live/sdk_order_gate.py
+++ b/agent/src/live/sdk_order_gate.py
@@ -435,10 +435,18 @@ def _allow(broker, session_id, connector_module, config, intent, place_kwargs, m
"""Execute the order; consume a count + audit only on a non-error result."""
pending = None
if broker == "alpaca":
+ pre_position_qty = _pre_position_qty(positions, intent.symbol)
+ if place_kwargs.get("quantity") is not None and pre_position_qty is None:
+ return _deny(
+ broker, session_id, "exact pre-position evidence is unavailable",
+ ["mandate", "expiry", "halt_flag", "pending_action", "position"],
+ mandate, intent=intent,
+ reason_code="pending_position_evidence_unavailable",
+ )
try:
pending = pending_action.new_pending_order(
broker, place_kwargs,
- pre_position_qty=_pre_position_qty(positions, intent.symbol),
+ pre_position_qty=pre_position_qty,
)
pending_action.save_pending_action(pending)
except Exception as exc: # noqa: BLE001 - missing evidence forbids the write
@@ -613,12 +621,25 @@ 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"]
+ persisted = action.position_resolution
+ same_resolution = (
+ action.phase == "resolved_fill_pending_audit"
+ and action.resolution
+ == pending_action.RecoveredOrderEvidence.model_validate(evidence)
+ and persisted is not None
+ )
+ if same_resolution:
+ positions = None
+ before = _exact_decimal(persisted.pre_position_qty)
+ after = _exact_decimal(persisted.current_position_qty)
+ filled = _exact_decimal(persisted.attributed_filled_qty)
+ else:
+ positions = _safe_read(connector, "get_positions", config)
+ before = _exact_decimal(action.pre_position_qty)
+ after = _exact_position_qty(positions, action.request.symbol)
+ filled = _exact_decimal(evidence["filled_qty"])
coherent_size = (
request_qty is not None
and filled is not None
@@ -637,20 +658,21 @@ def _recover_correlated_fill(
"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),
- },
+ position_evidence = {
+ "pre_position_qty": format(before, "f"),
+ "current_position_qty": format(after, "f"),
+ "attributed_filled_qty": format(filled, "f"),
}
+ if not same_resolution:
+ try:
+ action = pending_action.transition_to_fill_resolution(
+ action, evidence, position_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": position_evidence}
record = _audit(
broker, session_id, kind="order_placed", outcome="filled", mandate=mandate,
intent=None, broker_request=action.request.model_dump(mode="json"),
@@ -713,13 +735,20 @@ def _exact_position_qty(positions, symbol) -> Decimal | None:
if len(matches) != 1:
return None
row = matches[0]
+ exact_value = row.get("exact_quantity")
try:
- quantity = _exact_decimal(row.get("quantity", row.get("qty")))
+ quantity = _exact_decimal(
+ exact_value if exact_value is not None else 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:
+ if (
+ exact_value is None
+ and str(row.get("side") or "").strip().lower() in {"short", "sell"}
+ and quantity > 0
+ ):
quantity = -quantity
return quantity
@@ -975,22 +1004,9 @@ def _safe_read(connector_module: Any, fn_name: str, config: Any) -> object:
return result
-def _pre_position_qty(positions: object, symbol: str) -> float | None:
- rows = positions.get("positions", positions.get("data")) if isinstance(positions, dict) else positions
- if not isinstance(rows, list):
- return None
- target = str(symbol or "").strip().upper()
- for row in rows:
- if not isinstance(row, dict) or str(row.get("symbol") or "").strip().upper() != target:
- continue
- try:
- quantity = float(row.get("quantity", row.get("qty")))
- except (TypeError, ValueError):
- return None
- if str(row.get("side") or "").strip().lower() in {"short", "sell"} and quantity > 0:
- quantity = -quantity
- return quantity
- return 0.0
+def _pre_position_qty(positions: object, symbol: str) -> str | None:
+ quantity = _exact_position_qty(positions, symbol)
+ return format(quantity, "f") if quantity is not None else None
# --------------------------------------------------------------------------- #
diff --git a/agent/src/trading/connectors/alpaca/sdk.py b/agent/src/trading/connectors/alpaca/sdk.py
index 5f59601cc..f40b80b62 100644
--- a/agent/src/trading/connectors/alpaca/sdk.py
+++ b/agent/src/trading/connectors/alpaca/sdk.py
@@ -25,6 +25,7 @@ import hashlib
import json
import os
from dataclasses import asdict, dataclass
+from decimal import Decimal
from pathlib import Path
from types import ModuleType
from typing import Any, Mapping
@@ -896,15 +897,18 @@ def _position_to_dict(item: Any) -> dict[str, Any]:
side_raw = _obj_get(item, "side", "")
side = str(getattr(side_raw, "value", side_raw)).strip().lower()
quantity = _obj_get(item, "qty")
+ exact_quantity = Decimal(str(quantity)) if quantity is not None else None
if side == "short" and quantity is not None:
# Alpaca reports qty as an absolute magnitude and carries direction in
# side. The mandate gate (like the tiger connector) reads quantity as
# signed, so an unsigned short would book as positive exposure.
quantity = -float(quantity)
+ exact_quantity = -abs(exact_quantity)
return {
"symbol": _obj_get(item, "symbol"),
"side": side,
"quantity": quantity,
+ "exact_quantity": format(exact_quantity, "f") if exact_quantity is not None else None,
"average_cost": _obj_get(item, "avg_entry_price"),
"market_value": _obj_get(item, "market_value"),
"current_price": _obj_get(item, "current_price"),
diff --git a/agent/tests/test_alpaca_position_sign.py b/agent/tests/test_alpaca_position_sign.py
index c4e813a0f..5744e97cb 100644
--- a/agent/tests/test_alpaca_position_sign.py
+++ b/agent/tests/test_alpaca_position_sign.py
@@ -23,12 +23,21 @@ pytestmark = pytest.mark.unit
def test_short_position_gets_negative_quantity() -> None:
row = _position_to_dict({"symbol": "AAPL", "side": "short", "qty": "400", "market_value": "-50000"})
assert row["quantity"] == -400.0
+ assert row["exact_quantity"] == "-400"
assert row["side"] == "short"
def test_long_position_quantity_passes_through() -> None:
row = _position_to_dict({"symbol": "AAPL", "side": "long", "qty": "400", "market_value": "50000"})
assert row["quantity"] == "400"
+ assert row["exact_quantity"] == "400"
+
+
+def test_fractional_position_keeps_an_exact_recovery_quantity() -> None:
+ row = _position_to_dict(
+ {"symbol": "AAPL", "side": "short", "qty": "0.123456789123456789"}
+ )
+ assert row["exact_quantity"] == "-0.123456789123456789"
def test_short_position_without_qty_stays_none() -> None:
diff --git a/agent/tests/test_sdk_order_gate.py b/agent/tests/test_sdk_order_gate.py
index 4ce21fe79..102fd9208 100644
--- a/agent/tests/test_sdk_order_gate.py
+++ b/agent/tests/test_sdk_order_gate.py
@@ -482,6 +482,54 @@ def test_exact_quantity_fill_requires_matching_signed_position(
assert len(connector.placed) == 1 and live_halt.halt_flag_set("alpaca") is False
+def test_fractional_fill_preserves_exact_position_decimals(monkeypatch) -> None:
+ _patch_gate(monkeypatch, mandate=_mandate())
+ connector = _LostResponseConnector(
+ positions={
+ "status": "ok",
+ "positions": [{
+ "symbol": "AAPL",
+ "quantity": "0.123456789123456789",
+ "market_value": "12.3456789123456789",
+ "side": "long",
+ }],
+ }
+ )
+ _place(connector, quantity=0.1, notional=None)
+ action = load_pending_action("alpaca")
+ connector.lookup_result = _exact_order(action, status="filled", filled_qty="0.1")
+ connector._positions = {
+ "status": "ok",
+ "positions": [{"symbol": "AAPL", "quantity": "0.223456789123456789"}],
+ }
+
+ result = _place(connector)
+
+ assert result["reason_code"] == "pending_action_resolved_fill"
+ assert load_pending_action("alpaca") is None
+ assert live_halt.halt_flag_set("alpaca") is False
+
+
+def test_quantity_submit_requires_unambiguous_pre_position(monkeypatch) -> None:
+ _patch_gate(monkeypatch, mandate=_mandate())
+ connector = _LostResponseConnector(
+ positions={
+ "status": "ok",
+ "positions": [
+ {"symbol": "AAPL", "quantity": 1, "market_value": 100},
+ {"symbol": "AAPL", "quantity": 2, "market_value": 200},
+ ],
+ }
+ )
+
+ result = _place(connector, quantity=10, notional=None)
+
+ assert result["status"] == "blocked"
+ assert result["reason_code"] == "pending_position_evidence_unavailable"
+ assert connector.placed == []
+ assert load_pending_action("alpaca") is None
+
+
@pytest.mark.parametrize(
("quantity", "filled", "after"),
[(100, 101, 126), (100, 30, 54), (100, 0, 25), (None, 5, 5)],
@@ -540,11 +588,15 @@ def test_attributed_fill_survives_audit_failure_and_replays_without_submit(monke
interrupted = _place(connector)
persisted = load_pending_action("alpaca")
+ connector._positions = {
+ "status": "ok", "positions": [{"symbol": "AAPL", "quantity": 999}]
+ }
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 persisted.position_resolution is not None
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