PINK Phase 0 and 1: VST WS confirmed plus AccountSnapshotV2 account core

This commit is contained in:
Codex
2026-06-01 20:11:03 +02:00
parent c87ca785b9
commit e7eaa88ce1
166 changed files with 832 additions and 77021 deletions

View File

@@ -1,200 +0,0 @@
"""
MIG6.1 & MIG6.2: ACB Processor Service
Watches for new scan arrivals and atomically computes/writes ACB boost
to the Hazelcast DOLPHIN_FEATURES map using CP Subsystem lock for atomicity.
"""
import sys
import time
import json
import logging
from pathlib import Path
from datetime import datetime
import hazelcast
HCM_DIR = Path(__file__).parent.parent
# Use platform-independent paths from dolphin_paths
sys.path.insert(0, str(HCM_DIR))
sys.path.insert(0, str(HCM_DIR / 'prod'))
from dolphin_paths import get_eigenvalues_path
SCANS_DIR = get_eigenvalues_path()
sys.path.insert(0, str(HCM_DIR / 'nautilus_dolphin'))
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s:%(message)s')
class ACBProcessorService:
def __init__(self, hz_cluster="dolphin", hz_host="localhost:5701"):
try:
self.hz_client = hazelcast.HazelcastClient(
cluster_name=hz_cluster,
cluster_members=[hz_host]
)
self.imap = self.hz_client.get_map("DOLPHIN_FEATURES").blocking()
# Using CP Subsystem lock as per MIG6.1
self.lock = self.hz_client.cp_subsystem.get_lock("acb_update_lock").blocking()
except Exception as e:
logging.error(f"Failed to connect to Hazelcast: {e}")
raise
self.acb = AdaptiveCircuitBreaker()
self.acb.config.EIGENVALUES_PATH = SCANS_DIR # CRITICAL: override Windows default for Linux
self.acb.preload_w750(self._get_recent_dates(60))
self.last_scan_count = 0
self.last_date = None
def _get_recent_dates(self, n=60):
try:
dirs = sorted([d.name for d in SCANS_DIR.iterdir() if d.is_dir() and len(d.name)==10])
return dirs[-n:]
except Exception:
return []
def get_today_str(self):
return datetime.utcnow().strftime('%Y-%m-%d')
def check_new_scans(self, date_str):
today_dir = SCANS_DIR / date_str
if not today_dir.exists():
return False
json_files = list(today_dir.glob("scan_*.json"))
count = len(json_files)
if self.last_date != date_str:
self.last_date = date_str
self.last_scan_count = 0
# Preload updated dates when day rolls over
self.acb.preload_w750(self._get_recent_dates(60))
if count > self.last_scan_count:
self.last_scan_count = count
return True
return False
def process_and_write(self, date_str):
"""Compute ACB boost and write to HZ acb_boost.
Preference order:
1. HZ exf_latest — live, pre-lagged values (preferred, ~0.5 s latency)
2. NPZ disk scan — fallback when HZ data absent or stale (>12 h)
"""
try:
boost_info = None
long_boost_info = None
# ── HZ path (preferred) ────────────────────────────────────────────
try:
exf_raw = self.imap.get('exf_latest')
if exf_raw:
exf_snapshot = json.loads(exf_raw)
scan_raw = self.imap.get('latest_eigen_scan')
w750_live = None
if scan_raw:
scan_data = json.loads(scan_raw)
w750_live = scan_data.get('w750_velocity')
boost_info = self.acb.get_dynamic_boost_from_hz(
date_str, exf_snapshot, w750_velocity=w750_live, direction=-1
)
long_boost_info = self.acb.get_dynamic_boost_from_hz(
date_str, exf_snapshot, w750_velocity=w750_live, direction=1
)
logging.debug(
f"ACB computed from HZ: short={boost_info['boost']:.4f} "
f"long={long_boost_info['boost']:.4f}"
)
except ValueError as ve:
logging.warning(f"ACB HZ snapshot stale: {ve} — falling back to NPZ")
boost_info = None
except Exception as e:
logging.warning(f"ACB HZ read failed: {e} — falling back to NPZ")
boost_info = None
# ── NPZ fallback ───────────────────────────────────────────────────
if boost_info is None:
boost_info = self.acb.get_dynamic_boost_for_date(date_str, direction=-1)
long_boost_info = self.acb.get_dynamic_boost_for_date(date_str, direction=1)
logging.debug(
f"ACB computed from NPZ: short={boost_info['boost']:.4f} "
f"long={long_boost_info['boost']:.4f}"
)
payload = json.dumps(boost_info)
long_payload = json.dumps(long_boost_info or boost_info)
# Atomic Write via CP Subsystem Lock
self.lock.lock()
try:
# Legacy key remains SHORT for BLUE/PRODGREEN compatibility.
self.imap.put("acb_boost", payload)
self.imap.put("acb_boost_short", payload)
self.imap.put("acb_boost_long", long_payload)
logging.info(
f"acb_boost updated (src={boost_info.get('source','npz')}): "
f"short={boost_info['boost']:.4f}/{boost_info['signals']:.1f}sig "
f"long={(long_boost_info or {}).get('boost', 0.0):.4f}/"
f"{(long_boost_info or {}).get('signals', 0.0):.1f}sig"
)
try:
from ch_writer import ch_put, ts_us as _ts
ch_put("acb_state", {
"ts": _ts(),
"boost": float(boost_info.get("boost", 0)),
"beta": float(boost_info.get("beta", 0)),
"signals": float(boost_info.get("signals", 0)),
})
except Exception:
pass
finally:
self.lock.unlock()
except Exception as e:
logging.error(f"Error processing ACB: {e}")
def run(self, poll_interval=1.0, hz_refresh_interval=30.0):
"""Main service loop.
Two update triggers:
1. New scan files arrive for today → compute from HZ (preferred) or NPZ.
2. hz_refresh_interval elapsed → re-push acb_boost from live exf_latest
even when no new scans exist (covers live-only operation days when
scan files land in a different directory or not at all).
"""
logging.info("Starting ACB Processor Service (Python CP Subsystem)...")
today = self.get_today_str()
# Write immediately on startup so acb_boost is populated from the first second
logging.info(f"Startup write for {today}")
self.process_and_write(today)
last_hz_refresh = time.monotonic()
while True:
try:
today = self.get_today_str()
now = time.monotonic()
# Trigger 1: new scan files
if self.check_new_scans(today):
self.process_and_write(today)
last_hz_refresh = now
# Trigger 2: periodic HZ refresh (ensures acb_boost stays current
# even on days with no new NPZ scan files)
elif (now - last_hz_refresh) >= hz_refresh_interval:
self.process_and_write(today)
last_hz_refresh = now
time.sleep(poll_interval)
except KeyboardInterrupt:
break
except Exception as e:
logging.error(f"Loop error: {e}")
time.sleep(5.0)
if __name__ == "__main__":
service = ACBProcessorService()
service.run()

View File

@@ -1,126 +0,0 @@
from __future__ import annotations
import json
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
DEFAULT_SANDBOX_STATUS_PATH = Path("/tmp/bingx_sandbox_status.json")
@dataclass(frozen=True)
class BingxSandboxStatus:
"""Small sidecar snapshot for BingX demo/testnet state.
The snapshot is intentionally local-only so it can be used by tests and
operators without writing into BLUE state, ClickHouse, or production logs.
"""
ts: str
environment: str
balance: float
equity: float
available_margin: float
unrealized_profit: float
used_margin: float
open_positions: int
open_orders: int
account_currency: str = "VST"
clean: bool = False
notes: dict[str, Any] | None = None
def to_dict(self) -> dict[str, Any]:
return {
"ts": self.ts,
"environment": self.environment,
"account_currency": self.account_currency,
"balance": self.balance,
"equity": self.equity,
"available_margin": self.available_margin,
"unrealized_profit": self.unrealized_profit,
"used_margin": self.used_margin,
"open_positions": self.open_positions,
"open_orders": self.open_orders,
"clean": self.clean,
"notes": self.notes or {},
}
def _safe_float(value: Any, default: float = 0.0) -> float:
try:
out = float(value)
except Exception:
return default
return out if out == out else default
def _count_positions(positions: Any) -> int:
if isinstance(positions, list):
return sum(1 for item in positions if isinstance(item, dict))
return 0
def _count_orders(open_orders: Any) -> int:
if isinstance(open_orders, dict):
orders = open_orders.get("orders")
if isinstance(orders, list):
return sum(1 for item in orders if isinstance(item, dict))
if isinstance(open_orders, list):
return sum(1 for item in open_orders if isinstance(item, dict))
return 0
def build_sandbox_status(
*,
balance_payload: dict[str, Any],
positions_payload: Any,
open_orders_payload: Any,
environment: str = "VST",
account_currency: str = "VST",
notes: dict[str, Any] | None = None,
) -> BingxSandboxStatus:
balance_row = balance_payload.get("balance", balance_payload) if isinstance(balance_payload, dict) else {}
if not isinstance(balance_row, dict):
balance_row = {}
balance = _safe_float(balance_row.get("balance"), 0.0)
equity = _safe_float(balance_row.get("equity"), balance)
available_margin = _safe_float(balance_row.get("availableMargin"), 0.0)
unrealized_profit = _safe_float(balance_row.get("unrealizedProfit"), 0.0)
used_margin = _safe_float(balance_row.get("usedMargin"), 0.0)
open_positions = _count_positions(positions_payload)
open_orders = _count_orders(open_orders_payload)
return BingxSandboxStatus(
ts=datetime.now(timezone.utc).isoformat(),
environment=str(environment),
account_currency=str(account_currency),
balance=balance,
equity=equity,
available_margin=available_margin,
unrealized_profit=unrealized_profit,
used_margin=used_margin,
open_positions=open_positions,
open_orders=open_orders,
clean=(open_positions == 0 and open_orders == 0),
notes=notes or {},
)
def snapshot_path(path: str | Path | None = None) -> Path:
return Path(path) if path is not None else DEFAULT_SANDBOX_STATUS_PATH
def write_sandbox_status(status: BingxSandboxStatus, path: str | Path | None = None) -> Path:
target = snapshot_path(path)
target.write_text(json.dumps(status.to_dict(), indent=2, sort_keys=True))
return target
def load_sandbox_status(path: str | Path | None = None) -> dict[str, Any] | None:
target = snapshot_path(path)
if not target.exists():
return None
try:
return json.loads(target.read_text())
except Exception:
return None

View File

@@ -1,503 +0,0 @@
"""Direct BingX execution adapter with no Nautilus Trader node dependency.
This adapter speaks BingX REST directly and keeps the exchange state
authoritative. It is intended for PINK live execution under the DITA boundary.
"""
from __future__ import annotations
import asyncio
import json
import logging
import math
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from decimal import Decimal, ROUND_DOWN
from typing import Any, Optional
from nautilus_trader.model.identifiers import InstrumentId
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from prod.bingx.http import BingxHttpError
from prod.bingx.http import BingxHttpClient
from prod.bingx.instrument_provider import BingxInstrumentProvider
from prod.bingx.leverage import normalize_bingx_leverage_value
from prod.bingx.schemas import BingxOrderAck
from prod.bingx.schemas import unwrap_order_payload
from prod.clean_arch.dita import Intent, TradeSide, DecisionAction
from prod.clean_arch.ports.execution import ExchangeStateSnapshot
from prod.clean_arch.ports.execution import ExecutionReceipt
from prod.clean_arch.ports.execution import ExecutionPort
LOGGER = logging.getLogger(__name__)
def _rows_from_payload(payload: Any, *keys: str) -> list[dict[str, Any]]:
if isinstance(payload, list):
return [row for row in payload if isinstance(row, dict)]
if isinstance(payload, dict):
for key in keys:
rows = payload.get(key)
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
return []
def _capital_from_balance_rows(rows: Any) -> float:
if not isinstance(rows, list):
return 0.0
for row in rows:
if not isinstance(row, dict):
continue
capital = 0.0
for key in ("total", "balance", "equity", "availableMargin", "availableBalance", "walletBalance", "free"):
try:
capital = float(row.get(key, 0.0) or 0.0)
except Exception:
continue
if capital > 0 and math.isfinite(capital):
return capital
if capital > 0 and math.isfinite(capital):
return capital
return 0.0
def _position_notional_from_rows(rows: Any) -> float:
if not isinstance(rows, list):
return 0.0
total = 0.0
for row in rows:
if not isinstance(row, dict):
continue
try:
qty = abs(
float(
row.get("positionAmt")
or row.get("positionQty")
or row.get("positionSize")
or row.get("quantity")
or row.get("pa")
or 0.0
)
)
if qty <= 0.0:
continue
notional = row.get("positionValue") or row.get("notional") or row.get("openNotional")
if notional is not None:
total += abs(float(notional or 0.0))
continue
entry = (
row.get("entryPrice")
or row.get("avgPrice")
or row.get("markPrice")
or row.get("avgEntryPrice")
or row.get("ep")
or row.get("ap")
or 0.0
)
total += qty * abs(float(entry or 0.0))
except Exception:
continue
return total
def _normalize_symbol(symbol: str) -> str:
return str(symbol or "").replace("-", "").replace("_", "").replace("/","").upper()
def _venue_symbol_from_asset(asset: str) -> str:
text = _normalize_symbol(asset)
if text.endswith("USDT"):
return f"{text[:-4]}-USDT"
return text
def _decimal_text(value: Decimal) -> str:
text = format(value.normalize(), "f")
if "." in text:
text = text.rstrip("0").rstrip(".")
return text or "0"
def _is_rate_limited_error(exc: Exception) -> bool:
message = str(exc)
lowered = message.lower()
return "100410" in message or "frequency limit" in lowered or "rate limit" in lowered
@dataclass(frozen=True)
class BingxDirectExecutionConfig:
"""Execution-specific knobs for the direct adapter."""
environment: BingxEnvironment = BingxEnvironment.VST
allow_mainnet: bool = False
default_leverage: int = 1
exchange_leverage_cap: int = 3
recv_window_ms: int = 5_000
prefer_websocket: bool = False
use_reduce_only: bool = True
journal_strategy: str = "pink"
journal_db: str = "dolphin_pink"
instrument_provider: BingxInstrumentProviderConfig = BingxInstrumentProviderConfig(load_all=True)
class BingxDirectExecutionAdapter(ExecutionPort):
"""Direct BingX execution boundary with exchange-led state snapshots."""
def __init__(
self,
config: BingxExecClientConfig | BingxDirectExecutionConfig,
*,
client: BingxHttpClient | None = None,
provider: BingxInstrumentProvider | None = None,
) -> None:
if isinstance(config, BingxExecClientConfig):
self._config = BingxDirectExecutionConfig(
environment=config.environment,
allow_mainnet=config.allow_mainnet,
default_leverage=int(config.default_leverage),
exchange_leverage_cap=int(config.exchange_leverage_cap),
recv_window_ms=int(config.recv_window_ms),
prefer_websocket=bool(config.prefer_websocket),
use_reduce_only=bool(config.use_reduce_only),
journal_strategy=str(config.journal_strategy or "pink"),
journal_db=str(config.journal_db or "dolphin_pink"),
instrument_provider=config.instrument_provider,
)
http_config = config
else:
self._config = config
http_config = BingxExecClientConfig(
api_key="",
secret_key="",
environment=config.environment,
allow_mainnet=config.allow_mainnet,
prefer_websocket=config.prefer_websocket,
sizing_mode="testnet",
exchange_leverage_cap=config.exchange_leverage_cap,
use_reduce_only=config.use_reduce_only,
default_leverage=config.default_leverage,
recv_window_ms=config.recv_window_ms,
journal_strategy=config.journal_strategy,
journal_db=config.journal_db,
instrument_provider=config.instrument_provider,
)
self._client = client or BingxHttpClient(http_config)
self._provider = provider or BingxInstrumentProvider(client=self._client, config=self._config.instrument_provider)
self._log = LOGGER
self._client_order_run_id = uuid.uuid4().hex[:8]
self._entry_client_order_seq = 0
self._exit_client_order_seq = 0
self._state: ExchangeStateSnapshot | None = None
self._connected = False
@property
def state(self) -> ExchangeStateSnapshot | None:
return self._state
async def connect(self) -> bool:
await self._provider.initialize()
self._connected = True
self._state = await self.refresh_state()
return True
async def disconnect(self) -> None:
self._connected = False
await self._client.close()
def _resolve_instrument(self, asset: str):
normalized = _normalize_symbol(asset)
candidates = [
InstrumentId.from_str(f"{normalized}.BINGX"),
InstrumentId.from_str(f"{_venue_symbol_from_asset(asset)}.BINGX"),
]
for candidate in candidates:
instrument = self._provider.find(candidate)
if instrument is not None:
return instrument
for instrument in self._provider.list_all():
if _normalize_symbol(instrument.symbol.value) == normalized:
return instrument
if _normalize_symbol(instrument.raw_symbol.value) == normalized:
return instrument
return None
def _instrument_venue_symbol(self, asset: str) -> str:
instrument = self._resolve_instrument(asset)
if instrument is not None:
return str(instrument.raw_symbol.value)
return _venue_symbol_from_asset(asset)
def _instrument_step(self, asset: str) -> Decimal:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
return Decimal(str(instrument.size_increment.as_decimal()))
except Exception:
pass
return Decimal("0.001")
def _format_quantity(self, asset: str, quantity: float) -> str:
step = self._instrument_step(asset)
if step <= 0:
return str(max(0.0, quantity))
value = Decimal(str(quantity))
quantized = (value / step).to_integral_value(rounding=ROUND_DOWN) * step
return _decimal_text(max(Decimal("0"), quantized))
def _instrument_tick(self, asset: str) -> Decimal:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
tick = getattr(instrument, "price_increment", None)
if tick is not None:
return Decimal(str(tick.as_decimal()))
except Exception:
pass
return Decimal("0.01")
def _format_price(self, asset: str, price: float) -> str:
tick = self._instrument_tick(asset)
if tick <= 0:
return f"{price:.8f}".rstrip("0").rstrip(".")
value = Decimal(str(price))
quantized = (value / tick).to_integral_value(rounding=ROUND_DOWN) * tick
return _decimal_text(max(Decimal("0"), quantized))
async def _safe_get(self, endpoint: str, params: dict | None = None, *, fallback: Any = None) -> Any:
"""GET an endpoint, returning *fallback* on rate-limit errors."""
try:
return await self._client.signed_get(endpoint, params)
except BingxHttpError as exc:
message = str(exc)
if "100410" in message or "frequency limit" in message.lower():
LOGGER.debug("BingX %s rate-limited; continuing with empty snapshot", endpoint)
return fallback if fallback is not None else []
raise
async def _refresh_exchange_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
"""Fetch exchange state with parallel HTTP calls.
The three primary calls (balance, positions, openOrders) are
independent and run concurrently via ``asyncio.gather``. Each has
its own rate-limit fallback so a single throttle does not block
the others. Historical calls (allOrders, allFillOrders) are gated
on ``include_history`` and also gathered.
"""
balance_task = self._safe_get("/openApi/swap/v2/user/balance")
positions_task = self._safe_get("/openApi/swap/v2/user/positions")
orders_task = self._safe_get("/openApi/swap/v2/trade/openOrders")
balance_payload, positions_payload, open_orders_payload = await asyncio.gather(
balance_task, positions_task, orders_task,
)
all_orders_payload: Any = []
all_fills_payload: Any = []
if include_history and symbol is not None:
venue_symbol = self._instrument_venue_symbol(symbol)
hist_tasks = asyncio.gather(
self._safe_get("/openApi/swap/v2/trade/allOrders", {"symbol": venue_symbol}),
self._safe_get("/openApi/swap/v2/trade/allFillOrders", {"symbol": venue_symbol}),
return_exceptions=True,
)
results = await hist_tasks
all_orders_payload = results[0] if not isinstance(results[0], Exception) else []
all_fills_payload = results[1] if not isinstance(results[1], Exception) else []
# Parse results (shared logic, same as before)
if isinstance(balance_payload, list):
balances = balance_payload
elif isinstance(balance_payload, dict):
rows_raw = balance_payload.get("balance") or balance_payload.get("balances") or balance_payload.get("data")
if isinstance(rows_raw, dict):
balances = [rows_raw]
elif isinstance(rows_raw, list):
balances = rows_raw
else:
balances = []
else:
balances = []
positions_rows = _rows_from_payload(positions_payload, "positions", "data")
positions: dict[str, dict[str, Any]] = {}
for row in positions_rows:
raw_symbol = str(row.get("symbol") or row.get("symbolName") or row.get("venueSymbol") or "")
key = _normalize_symbol(raw_symbol)
if not key:
continue
positions[key] = dict(row)
open_orders = _rows_from_payload(open_orders_payload, "orders", "data")
capital = _capital_from_balance_rows(balances)
open_notional = _position_notional_from_rows(positions_rows)
equity = capital
if open_notional > 0 and positions_rows:
equity = capital
snapshot = ExchangeStateSnapshot(
timestamp=datetime.now(timezone.utc),
capital=capital,
equity=equity,
open_positions=positions,
open_orders=[dict(row) for row in open_orders],
all_orders=[dict(row) for row in _rows_from_payload(all_orders_payload, "orders", "data")],
all_fills=[dict(row) for row in _rows_from_payload(all_fills_payload, "fills", "data")],
account={"balances": balances},
open_notional=open_notional,
source="bingx",
recovered=bool(include_history),
)
self._state = snapshot
return snapshot
async def refresh_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
return await self._refresh_exchange_state(symbol, include_history=include_history)
async def submit_intent(self, intent: Intent) -> ExecutionReceipt:
symbol = self._instrument_venue_symbol(intent.asset)
if intent.action == DecisionAction.EXIT:
side = "SELL" if intent.side == TradeSide.LONG else "BUY"
else:
side = "BUY" if intent.side == TradeSide.LONG else "SELL"
# Entries must be free to open the slot; only exits are reduce-only.
reduce_only = bool(intent.action == DecisionAction.EXIT)
if reduce_only:
self._exit_client_order_seq += 1
client_order_id = f"pink:{self._client_order_run_id}:x{self._exit_client_order_seq:02d}"
else:
self._entry_client_order_seq += 1
client_order_id = f"pink:{self._client_order_run_id}:e{self._entry_client_order_seq:02d}"
leverage = normalize_bingx_leverage_value(
int(round(float(intent.leverage or self._config.default_leverage))),
exchange_max=self._config.exchange_leverage_cap,
)
try:
await self._client.signed_post(
"/openApi/swap/v2/trade/leverage",
{"symbol": symbol, "side": "BOTH", "leverage": leverage},
)
# Honor the order type forwarded by the venue adapter
# (bingx_venue._legacy_intent sets _order_type/_limit_price). MARKET
# is the default; a LIMIT carries a resting price + GTC and will not
# fill synchronously — the async-fill pump settles it later.
order_type = str((intent.metadata or {}).get("_order_type", "MARKET") or "MARKET").upper()
limit_price = float((intent.metadata or {}).get("_limit_price", 0.0) or 0.0)
is_limit = order_type == "LIMIT" and limit_price > 0.0
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": "LIMIT" if is_limit else "MARKET",
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if is_limit:
payload["price"] = self._format_price(intent.asset, limit_price)
payload["timeInForce"] = "GTC"
if reduce_only:
payload["reduceOnly"] = "true"
ack_payload = await self._client.signed_post("/openApi/swap/v2/trade/order", payload)
ack = BingxOrderAck.from_http(ack_payload if isinstance(ack_payload, dict) else {})
ack_row = dict(unwrap_order_payload(ack_payload)) if isinstance(ack_payload, dict) else {}
status = str(ack_row.get("status") or ack.status or "ACKED")
fill_price = 0.0
for key in ("avgPrice", "avgFilledPrice", "price", "lastFillPrice", "tradePrice"):
try:
value = float(ack_row.get(key) or 0.0)
except Exception:
value = 0.0
if value > 0:
fill_price = value
break
if fill_price <= 0 and self._state is not None:
# Use the last known exchange mark as a fallback for projected accounting.
fill_price = next((float(row.get("markPrice") or row.get("avgPrice") or 0.0) for row in self._state.open_positions.values() if float(row.get("markPrice") or row.get("avgPrice") or 0.0) > 0), 0.0)
except BingxHttpError as exc:
status = "RATE_LIMITED" if _is_rate_limited_error(exc) else "REJECTED"
ack_row = {
"status": status,
"msg": str(exc),
"symbol": symbol,
"clientOrderId": client_order_id,
}
fill_price = 0.0
ack = None
receipt = ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status=status,
symbol=symbol,
side=side,
action=intent.action.value,
quantity=float(intent.target_size or 0.0),
price=fill_price,
client_order_id=client_order_id,
order_id=str((ack.order_id if 'ack' in locals() and ack is not None else '') or ack_row.get("orderId") or ""),
raw_ack=ack_row,
raw_state=dict(self._state.account if self._state is not None else {}),
)
# Refresh from the venue so the direct runtime can use exchange-led state.
self._state = await self._refresh_exchange_state(intent.asset, include_history=True)
return receipt
async def cancel(self, order: Any, *, reason: str = "") -> dict[str, Any]:
"""Cancel a working order on the venue (resting LIMIT support).
Signs the DELETE with the same client used for order placement, keyed by
the venue orderId (propagated onto the slot order by the kernel on ACK)
with a clientOrderId fallback. Returns the raw BingX response for the
venue adapter to map into a CANCEL_ACK / CANCEL_REJECT event.
"""
asset = str((getattr(order, "metadata", None) or {}).get("asset") or "")
symbol = self._instrument_venue_symbol(asset) if asset else ""
params: dict[str, Any] = {
"symbol": symbol,
"recvWindow": str(int(self._config.recv_window_ms)),
}
venue_order_id = str(getattr(order, "venue_order_id", "") or "")
venue_client_id = str(getattr(order, "venue_client_id", "") or "")
if venue_order_id:
params["orderId"] = venue_order_id
elif venue_client_id:
params["clientOrderId"] = venue_client_id
else:
return {"status": "REJECTED", "msg": "no order id to cancel",
"orderId": venue_order_id, "clientOrderId": venue_client_id}
delete_resp: dict[str, Any] = {}
try:
resp = await self._client.signed_delete("/openApi/swap/v2/trade/order", params)
delete_resp = resp if isinstance(resp, dict) else {"status": "CANCELED"}
except BingxHttpError as exc:
delete_resp = {"status": "RATE_LIMITED" if _is_rate_limited_error(exc) else "ERROR", "msg": str(exc)}
# Truth-based confirmation: the cancel succeeded iff the order is no
# longer open on the venue. BingX can return transient errors (e.g.
# "order not exist", "same order number ... within 1 second" from an
# internal retry) even when the order was actually removed — so we trust
# exchange state, not the DELETE response.
still_open: bool | None = None
try:
oo = await self._client.signed_get("/openApi/swap/v2/trade/openOrders", {"symbol": symbol})
rows = oo if isinstance(oo, list) else (oo.get("data") or oo.get("orders") or [])
if isinstance(rows, dict):
rows = rows.get("orders") or []
ids = {str(r.get("orderId")) for r in rows if isinstance(r, dict)}
cids = {str(r.get("clientOrderId") or r.get("clientOrderID")) for r in rows if isinstance(r, dict)}
still_open = (venue_order_id in ids) if venue_order_id else (venue_client_id in cids)
except Exception:
still_open = None
if still_open is False:
return {"status": "CANCELED", "orderId": venue_order_id, "clientOrderId": venue_client_id}
if str(delete_resp.get("status", "")).upper() in {"CANCELED", "CANCELLED", "SUCCESS", "OK"}:
return {"status": "CANCELED", "orderId": venue_order_id, "clientOrderId": venue_client_id}
return {
"status": delete_resp.get("status", "REJECTED"),
"msg": delete_resp.get("msg", "cancel not confirmed"),
"orderId": venue_order_id, "clientOrderId": venue_client_id,
}
async def reconcile(self, symbol: str | None = None) -> ExchangeStateSnapshot:
# Recovery-only path: ask the venue for authoritative account/position/order state.
return await self._refresh_exchange_state(symbol, include_history=True)

View File

@@ -0,0 +1,109 @@
# BingX User Stream — VST Probe Notes (Phase 0)
**Date:** 2026-06-01
**Scope:** VST only (no LIVE touch).
**Result: Outcome A — VST has WebSocket. Full WS-on-both symmetry is achievable.**
---
## Gate G0 resolution
| Check | Result |
|---|---|
| listenKey endpoint (`POST /openApi/user/auth/userDataStream`) | ✅ Returns `listenKey` (signed request, `signed_post_raw`) |
| Signing method | ✅ Standard HMAC-SHA256 signed POST works — "header-only/unsigned" concern was unfounded |
| WS URL | `wss://vst-open-api-ws.bingx.com/swap-market?listenKey=<key>` |
| Frames delivered | ✅ 667 SNAPSHOT frames in 20 s (idle session, no active orders) |
| Gzip | Binary frames are gzip-compressed — `gzip.decompress(bytes(msg.data))` |
| Ping/Pong | Server sends text `"Ping"` → client must respond with `"Pong"` |
| listenKey keepalive | `PUT /openApi/user/auth/userDataStream {"listenKey": ...}` |
| listenKey delete | `DELETE /openApi/user/auth/userDataStream {"listenKey": ...}` |
---
## Event schemas
### `SNAPSHOT` — position/leverage state (received continuously)
```json
{"e":"SNAPSHOT","E":1780336019559,"ac":{"s":"MTL-USDT","l":1,"S":1,"mt":"isolated"}}
```
| Field | Meaning |
|---|---|
| `e` | `"SNAPSHOT"` |
| `E` | Server timestamp ms |
| `ac.s` | Symbol |
| `ac.l` | Long leverage |
| `ac.S` | Short leverage |
| `ac.mt` | Margin type (`"isolated"`) |
### `ORDER_TRADE_UPDATE` — fill/order status (arrives on trade activity)
Top-level envelope: `{"e":"ORDER_TRADE_UPDATE","E":<ts>,"o":{...}}`
Inner `o` object:
| Field | Meaning |
|---|---|
| `s` | Symbol |
| `c` | clientOrderId |
| `i` | orderId (venue) |
| `X` | Order status (`NEW`, `PARTIALLY_FILLED`, `FILLED`, `CANCELED`) |
| `x` | Execution type |
| `p` | Order price |
| `ap` | Average fill price |
| `z` | Cumulative filled qty (total filled so far) |
| `l` | **lastFilledQty — incremental fill for this event** |
| `L` | Last fill price |
| `n` | Commission amount |
| `N` | Commission asset |
**Critical:** `z` is cumulative; `l` is incremental per-event. `bingx_venue.py:582` reads
`lastFilledQty` = `l`. The Rust kernel's `apply_fill` now accumulates (`slot.size += l`).
### `ACCOUNT_UPDATE` — balance/position push (arrives on trade activity)
Top-level: `{"e":"ACCOUNT_UPDATE","E":<ts>,...}`
Balance array (`B`): `[{"a":"USDT","wb":<wallet_balance>,"cw":<cross_wallet_balance>}]`
Position array (`P`): `[{"s":<symbol>,"pa":<positionAmt>,"ep":<entryPrice>,"up":<unrealizedPnL>,"mt":<marginType>,"ps":<positionSide>}]`
### `FUNDING_FEE` — funding charge (arrives on funding interval)
Envelope: `{"e":"FUNDING_FEE","E":<ts>,"fs":{"s":<symbol>,"fa":<fundingAmount>,"a":<asset>}}`
Identified by `m == "FUNDING_FEE"` in some variants, or `e == "FUNDING_FEE"`.
---
## VST ↔ LIVE symmetry notes
- Same `POST /openApi/user/auth/userDataStream` endpoint, same signing method
- VST WS base: `wss://vst-open-api-ws.bingx.com/swap-market`
- LIVE WS base: `wss://open-api-swap.bingx.com/swap-market`
- Only difference: base hostname — **all frame schemas are identical**
- `bingx_user_stream.py` must use `base_url_ws_private` from config (already in `BingxExecClientConfig`)
---
## listenKey lifecycle
```
POST /openApi/user/auth/userDataStream {} → {"listenKey": "..."}
PUT /openApi/user/auth/userDataStream {"listenKey":..} → {} (keepalive, every 1800s)
DELETE /openApi/user/auth/userDataStream {"listenKey":..} → {} (on close)
```
listenKey TTL: ~60 min. Keepalive extends it. Server signals expiry via `{"e":"listenKeyExpired"}`.
---
## Open items for Phase 2
- `executionReport` schema: confirmed from BLUE observer.py analysis; verify against live VST
fill when first Phase 2 order is placed
- `ACCOUNT_UPDATE` balance fields: `wb` (wallet balance), `cw` (cross wallet balance)
- Funding fee `fs.fa` sign convention (positive = received, negative = paid) — to verify
- 24h connection cap: BingX closes the socket after ~24h regardless of keepalive;
overlap-rotation strategy required (open new connection before closing old)

View File

@@ -1,720 +0,0 @@
# CRITICAL: DITAv2 Execution Kernel — 13 Structural Flaws
**Analysis date:** 2026-05-30
**Analyst:** Systematic code review across Rust kernel, Python bridge, venue adapters, and test infrastructure
**Scope:** Full DITAv2 pipeline — `kernel.py` → `rust_backend.py` → `_rust_kernel/src/lib.rs` → `bingx_venue.py` → `bingx_direct.py` → BingX REST
---
## How to read this document
Each flaw follows the same structure:
| Section | What you'll find |
|---------|-----------------|
| **Location** | File path(s) and approximate line numbers |
| **Nature** | What kind of defect — structural, logic, protocol, edge-case, missing-feature |
| **Downstream effect** | What breaks in practice, not just what the code does wrong |
| **Exploit / trigger** | The exact sequence of events that manifests the bug |
| **Why it's not caught** | Why existing tests (142/142 pass) don't detect it |
| **Fix strategy** | High-level approach; no patch code here |
---
## Flaw 1: Entry-order cancellation is structurally broken
**Location:** `rust_backend.py` lines ~470–475 (Python bridge), `_rust_kernel/src/lib.rs` lines ~660–685 (Rust `process_intent` CANCEL branch), `_rust_kernel/src/lib.rs` lines ~740–748 (Rust `on_venue_event` CANCEL_ACK branch)
**Nature:** Missing feature / logic gap — two-layer hole
### Downstream effect
A CANCEL intent submitted for an entry order (slot in `ORDER_REQUESTED` or `ENTRY_WORKING`) is silently ignored. The venue is never called, so the order remains live on the exchange. The caller receives an `accepted=False, diagnostic_code=NO_ACTIVE_EXIT_ORDER` outcome but no error is raised — normal execution continues.
With MARKET orders (the only type tested in the 142-scenario suite), this doesn't matter because the order fills in 1–3 seconds, arriving before the CANCEL even runs or making the CANCEL economically irrelevant. With LIMIT orders (per `CRITICAL_NEEDED_PARTIAL_FILL_SUPPORT.md`), resting orders on the book would be **structurally impossible to cancel** through the kernel.
### Exact code path
**Layer 1 — Python bridge (rust_backend.py):**
```python
elif intent.action == KernelCommandType.CANCEL:
emitted_events = self.venue.cancel(
self.slot(intent.slot_id).active_exit_order, # ← None for entry-only slots
...
) if self.slot(intent.slot_id).active_exit_order else [] # ← always []
```
The guard `if self.slot(...).active_exit_order` evaluates to `False` for any slot that only has an entry order. `emitted_events` stays `[]`. The venue's `cancel()` is never called.
**Layer 2 — Rust kernel process_intent (lib.rs):**
```rust
KernelCommandType::CANCEL => {
if slot.active_exit_order.is_none() {
return KernelResult {
outcome: KernelOutcome {
accepted: false,
diagnostic_code: KernelDiagnosticCode::NO_ACTIVE_EXIT_ORDER,
...
},
...
};
}
// ... code only reachable if active_exit_order.is_some()
}
```
The Rust kernel also only looks for an exit order. It returns `NO_ACTIVE_EXIT_ORDER` for entry cancels.
**Layer 3 — Rust kernel on_venue_event CANCEL_ACK (lib.rs):**
```rust
KernelEventKind::CANCEL_ACK => {
if slot.active_exit_order.is_some() {
slot.active_exit_order = None;
slot.fsm_state = TradeStage::POSITION_OPEN;
}
}
```
Even if a CANCEL_ACK somehow arrived for an entry order, the Rust FSM has no branch to transition `ENTRY_WORKING → IDLE` on cancel. The slot would remain stuck.
### Why it's not caught
The test suite has:
- `cancel_entry_order` — ENTER → sleep 1s → CANCEL. By 1s the MARKET order has filled, so the slot is already POSITION_OPEN, making the CANCEL technically valid against active_exit_order? No — it's active_entry_order that's filled. But wait: when the entry fills, the Rust kernel transitions to POSITION_OPEN and keeps `active_entry_order` in place (filled state). `active_exit_order` is still None. So the CANCEL still hits NO_ACTIVE_EXIT_ORDER. But the test only checks that capital is positive and exchange is flat — it never checks `outcome.accepted` or `outcome.diagnostic_code` for the CANCEL call.
- `cancel_idempotent` — Same pattern: ENTER → sleep 0.5s → CANCEL.
- `double_cancel` — Same.
- All checks are pass/fail on capital + exchange flatness, not on whether the cancel actually did anything.
### Fix strategy
1. Add an `order_action` field to `KernelIntent` (or use existing `action`) to distinguish entry-cancel from exit-cancel
2. In the Python bridge, call `venue.cancel()` on `active_entry_order` when the intent is CANCEL and `active_exit_order` is None
3. In the Rust kernel, add an `active_entry_order` branch to `process_intent(CANCEL)` that transitions `ENTRY_WORKING / ORDER_REQUESTED → IDLE`
4. In the Rust kernel, add an `active_entry_order` branch to `on_venue_event(CANCEL_ACK)` that transitions to IDLE
---
## Flaw 2: Rust CANCEL FSM has no entry-order reset path
**Location:** `_rust_kernel/src/lib.rs` lines ~740–748
**Nature:** Missing FSM case — the `on_venue_event` handler for `CANCEL_ACK` only handles exit orders
### Downstream effect
Even if the Python bridge were fixed to call `venue.cancel()` on the active entry order (fixing Flaw 1), and even if BingX returned a successful cancel-ack, the Rust kernel **would not update the slot state**. The slot would remain in `ENTRY_WORKING` with `active_entry_order` still attached. The kernel would believe the order is still live on the exchange.
No subsequent `ENTER` intent would be accepted (SLOT_BUSY). The slot would be permanently deadlocked until a manual `reconcile_from_slots` overwrites it.
### Exact code path
```rust
KernelEventKind::CANCEL_ACK => {
if slot.active_exit_order.is_some() {
slot.active_exit_order = None;
slot.fsm_state = TradeStage::POSITION_OPEN;
}
// No else branch — silent no-op for entry cancels
}
```
The full FSM transition matrix for CANCEL_ACK should include:
- `ENTRY_WORKING, active_entry_order.is_some()` → clear entry order, set IDLE
- `EXIT_WORKING, active_exit_order.is_some()` → clear exit order, set POSITION_OPEN (existing code)
### Why it's not caught
Same reason as Flaw 1 — the cancel never fires, so CANCEL_ACK never arrives. The code path has never been exercised.
### Fix strategy
Add an `else if` branch:
```rust
} else if slot.active_entry_order.is_some() {
slot.active_entry_order = None;
slot.trade_id.clear();
slot.asset.clear();
slot.side = TradeSide::FLAT;
slot.size = 0.0;
slot.initial_size = 0.0;
slot.fsm_state = TradeStage::IDLE;
}
```
---
## Flaw 3: Python `process_intent` overwrites outcome with mixed-epoch state
**Location:** `rust_backend.py` lines ~490–505
**Nature:** Data consistency — returned `KernelOutcome` mixes pre-venue and post-venue state
### Downstream effect
Any caller inspecting the returned `KernelOutcome` from `process_intent()` gets misleading information:
- `diagnostic_code` is from the Rust kernel's pre-venue opinion
- `state` is from the slot **after** venue events were processed
- `transitions` only contains pre-venue transitions
- `emitted_events` correctly contains post-venue events
A caller checking `outcome.accepted == True` and `outcome.state == ORDER_REQUESTED` (the Rust kernel's initial state) would be wrong — the slot is actually already in `POSITION_OPEN` because the fill arrived within the same function call.
### Exact code path
```python
result = _get_rust().process_intent(...) # Rust: IDLE → ORDER_REQUESTED
outcome = _outcome_from_payload(result["outcome"]) # state=ORDER_REQUESTED
# ... venue.submit() ... on_venue_event() ... transitions slot through ENTRY_WORKING → POSITION_OPEN
final_slot = self._get_slot(outcome.slot_id) # fsm_state=POSITION_OPEN now
final_outcome = KernelOutcome(
state=final_slot.fsm_state, # POSITION_OPEN ← post-venue
diagnostic_code=outcome.diagnostic_code, # OK ← pre-venue
transitions=outcome.transitions, # [IDLE→ORDER_REQUESTED] ← incomplete
emitted_events=tuple(emitted_events), # [ORDER_ACK, FULL_FILL] ← correct
)
```
### Why it's not caught
No test inspects `outcome.transitions` or validates that `outcome.state` matches `outcome.diagnostic_code`. The `outcome_inspect_entry` test (`_gen_test.py` body) checks `len(info["transitions"]) > 0` — which passes because there's at least one — and `info["diagnostic"] == "OK"`. It doesn't check that the state in the outcome matches the diagnostic or that all transitions are present.
### Fix strategy
Either:
1. Re-read the Rust outcome after venue events complete (costly — additional FFI call), or
2. Emit the venue-event transitions back from `on_venue_event` and append them to the returned outcome, or
3. Document that `outcome.transitions` is a partial snapshot and the caller should inspect the slot directly via `k.slot(n)` for current state
---
## Flaw 4: Multi-leg exit final leg can double-close and double-settle
**Location:** `_rust_kernel/src/lib.rs` lines ~775–830, specifically the `apply_fill` exit path in `on_venue_event`
**Nature:** Logic error — redundant state mutation
### Downstream effect
When a FULL_FILL closes the last leg of a multi-leg exit, the Rust kernel sets `slot.fsm_state = CLOSED` and `slot.closed = true` in two separate code blocks. Block A does it based on `active_leg_index`, block B does it independently based on `slot.size <= 1e-12`. Both blocks run on the same event.
In practice this doesn't double-settle because the Python side processes a single `on_venue_event` call. But the slot state after the event is unpredictable — block B clears `active_entry_order` and `active_exit_order` that block A left in place. If any code path depends on inspecting the orders after a close (e.g., for journaling), it sees inconsistent state.
### Exact code path
```rust
// Block A (lines ~780-800):
if slot.active_leg_index >= slot.exit_leg_ratios.len() {
slot.closed = true;
slot.fsm_state = TradeStage::CLOSED;
slot.active_exit_order = None;
}
// Block B (lines ~810-830), runs unconditionally after block A:
if !partial {
slot.consume_exit_leg(); // advances leg index
if slot.size <= 1e-12 {
slot.closed = true; // redundant
slot.fsm_state = TradeStage::CLOSED; // redundant
slot.active_exit_order = None; // redundant
slot.active_entry_order = None; // extra — block A didn't do this
}
}
```
### Why it's not caught
The multi-leg exit tests (`multi_leg_exit`, `x4_partial_hold_exit`, all leg ratio variants) check capital integrity and exchange flatness. They don't inspect the slot's `active_entry_order` or `active_exit_order` after exit. The final capital assertion passes because `settle()` is called once per `on_venue_event` call regardless of how many times the slot's internal flags toggle.
### Fix strategy
Restructure `apply_fill` for exit fills so there's a single point where `CLOSED` is set:
- If `active_leg_index >= ratios.len()` **or** `size <= 1e-12` after the fill → set CLOSED
- Not both independently
---
## Flaw 5: Capital settlement only triggers on terminal states
**Location:** `rust_backend.py` lines ~520–525
**Nature:** Accounting accuracy — intra-trade realized PnL invisible to account projection
### Downstream effect
When a LIMIT order partially fills (PARTIALLY_FILLED event), the Rust kernel correctly accumulates realized PnL on the slot:
```rust
slot.realized_pnl += realized;
```
But the Python bridge only pushes PnL to the account on terminal transitions:
```python
if slot.fsm_state in {TradeStage.CLOSED, TradeStage.TRADE_TERMINAL_WRITTEN} and slot.realized_pnl != 0.0:
self.account.settle(slot.realized_pnl)
```
During a partial fill that leaves the slot in EXIT_WORKING, the accumulated PnL sits on the slot but never reaches `account.snapshot.capital`. For a LIMIT order that partially fills over several minutes, the system's view of available capital is **stale** during the entire fill window. This could cause the system to incorrectly calculate available margin for concurrent positions.
### Exact trigger
1. Slot is in POSITION_OPEN with size=1.0
2. EXIT intent → slot moves to EXIT_WORKING
3. Venue sends PARTIALLY_FILLED: filled_size=0.3, remaining_size=0.7
4. Rust: slot.realized_pnl += +2.50 (3% gain on 30% of position)
5. Python: slot.fsm_state == EXIT_WORKING (not CLOSED) → settle() is NOT called
6. `account.snapshot.capital` still shows pre-exit value
7. Venue sends FULL_FILL: filled_size=0.7, remaining_size=0.0
8. Rust: slot.realized_pnl += +5.83 (remaining), total = 8.33
9. Python: slot.fsm_state == CLOSED → settle(8.33) → capital jumps by full amount
For 3 minutes between step 4 and step 7, all downstream consumers see wrong capital.
### Why it's not caught
All 142 tests use MARKET orders that fill instantly in one shot. There is never a multi-event fill sequence for a single order. The non-instant fills come from multi-leg exits (multiple MARKET orders), where each exit is a separate `process_intent` call with its own `on_venue_event` cycle, and each eventually reaches CLOSED independently.
### Fix strategy
Change the settle trigger to fire on **any realized PnL change**, not just on terminal state transitions:
```python
if slot.realized_pnl != self._last_settled_pnl.get(slot.slot_id, 0.0):
incremental = slot.realized_pnl - self._last_settled_pnl[slot.slot_id]
self.account.settle(incremental)
self._last_settled_pnl[slot.slot_id] = slot.realized_pnl
```
Or simpler: settle the delta every time `on_venue_event` processes a fill event, regardless of slot state.
---
## Flaw 6: `_legacy_intent()` silently drops `order_type` and `limit_price`
**Location:** `bingx_venue.py` lines ~280–295
**Nature:** Chain break — data loss at the Python level
### Downstream effect
The `CRITICAL_NEEDED_PARTIAL_FILL_SUPPORT.md` spec adds `order_type` and `limit_price` to `KernelIntent`. But there are **two** venue adapters, and one of them strips the new fields:
**BingxVenueAdapter** receives `KernelIntent` and converts to `LegacyIntent`:
```python
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
```
`_legacy_intent()` builds a `LegacyIntent` — which has no `order_type` or `limit_price` fields:
```python
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=dict(intent.metadata),
# order_type and limit_price are NOT HERE — silently dropped
)
```
The `BingxDirectExecutionAdapter.submit_intent()` receives `LegacyIntent` and uses `intent.action`, `intent.side`, `intent.target_size`, etc. — none of which carry the new fields.
**MockVenueAdapter** receives `KernelIntent` directly and *would* see the new fields — but it only uses `intent.target_size`, `intent.reference_price`, `intent.side`, and `intent.action`. `order_type` and `limit_price` are ignored there too.
So even after `KernelIntent` gains the new fields, **no code path exists** that reads them and passes them to the BingX REST payload.
### Exact trigger
Someone constructs:
```python
intent = KernelIntent(
action=ENTER, trade_id="t1",
order_type="LIMIT", limit_price=0.083456,
...
)
k.process_intent(intent)
```
The new fields survive through `_intent_to_payload()` to Rust (harmless — Rust ignores unknown fields), then back to Python. The Python bridge calls `venue.submit(intent)` with the `intent` that still has `order_type="LIMIT"`. But `bingx_venue.submit()` converts to `LegacyIntent` — which drops them. `bingx_direct.py` sees a MARKET order.
### Why it's not caught
The new fields don't exist yet. No test exercises LIMIT orders.
### Fix strategy
The cleanest fix is to **bypass `_legacy_intent()`** for `BingxVenueAdapter.submit()` and pass `KernelIntent` directly to the adapter. The adapter's `submit_intent()` already has access to `intent.asset`, `intent.side`, etc. It just needs to receive the right type.
If `BingxDirectExecutionAdapter` must keep accepting `LegacyIntent` for backward compatibility, encode the new fields in `LegacyIntent.metadata`:
```python
metadata = dict(intent.metadata)
metadata["_order_type"] = intent.order_type
metadata["_limit_price"] = intent.limit_price
```
Then on the adapter side, read `intent.metadata.get("_order_type", "MARKET")`.
---
## Flaw 7: Mock venue partial_fill_ratio applies to both entry and exit
**Location:** `mock_venue.py` lines ~60–90
**Nature:** Test infrastructure limitation — single ratio cannot distinguish entry vs exit
### Downstream effect
The `MockVenueScenario` has one float: `partial_fill_ratio: float = 1.0`. When set to, say, `0.5`, **every** `submit()` call produces a `PARTIALLY_FILLED` event with 50% fill — regardless of whether the intent is an ENTER or an EXIT.
This makes it impossible to write a mock-venue unit test that:
- Entry fills fully (ratio=1.0) but exit fills partially (ratio=0.5)
- Entry fills partially (ratio=0.3) and then fills fully on a second submit
- Different partial ratios per leg of a multi-leg exit
### Exact code path
```python
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
fill_ratio = max(0.0, min(1.0, float(self.scenario.partial_fill_ratio)))
fill_size = float(intent.target_size) * fill_ratio
# ... emits PARTIALLY_FILLED or FULL_FILL based on ratio
# No distinction between ENTER and EXIT
```
### Why it's not caught
The mock venue is used in unit tests (`test_rust_backend.py` or similar), not in the live BingX e2e tests. The live tests use `BingxVenueAdapter` with real BingX VST, where MARKET orders always fill fully. The partial_fill_ratio path has never been used for a scenario that distinguishes entry from exit behavior.
### Fix strategy
Add per-action-type ratios:
```python
@dataclass(frozen=True)
class MockVenueScenario:
entry_partial_fill_ratio: float = 1.0
exit_partial_fill_ratio: float = 1.0
```
Or add a per-order override via `intent.metadata`.
---
## Flaw 8: Per-asset price precision helper does not exist
**Location:** `bingx_direct.py` — `_format_quantity()` exists (line ~150) but `_format_price()` does not
**Nature:** Missing feature — LIMIT orders will be rejected by BingX
### Downstream effect
BingX requires the `price` field of a LIMIT order to have the correct decimal precision for each symbol. The `_format_quantity()` method resolves `size_increment` from the instrument provider and quantizes the quantity. No equivalent exists for price.
Without it, submitting a LIMIT order with `limit_price=0.08` for TRXUSDT sends `"price": "0.08"` to BingX. BingX expects 6 decimal places for TRXUSDT prices (e.g., `0.083456`). The order is rejected with `"code": 100001, "msg": "Invalid price precision"`.
| Symbol | Approx price | Required decimals | `limit_price` value | What BingX expects |
|--------|-------------|-------------------|-------------------|-------------------|
| TRXUSDT | $0.08 | 6 | 0.083456 | `"0.083456"` |
| XRPUSDT | $0.52 | 4 | 0.5234 | `"0.5234"` |
| ADAUSDT | $0.45 | 4 | 0.4523 | `"0.4523"` |
| DOGEUSDT | $0.15 | 5 | 0.15234 | `"0.15234"` |
| BTCUSDT | $60,000 | 2 | 60000.50 | `"60000.50"` |
### Why it's not caught
No LIMIT orders are submitted. All 142 tests use MARKET orders where `type="MARKET"` and no `price` field is sent.
### Fix strategy
Add `_format_price(self, asset: str, price: float) -> str` mirroring `_format_quantity`:
```python
def _format_price(self, asset: str, price: float) -> str:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
price_step = Decimal(str(instrument.price_increment.as_decimal()))
value = Decimal(str(price))
quantized = (value / price_step).to_integral_value(rounding=ROUND_DOWN) * price_step
return _decimal_text(quantized)
except Exception:
pass
return f"{price:.8f}".rstrip("0").rstrip(".")
```
The instrument provider already exposes `price_increment` — it just needs to be accessed.
---
## Flaw 9: Cancel path falls back to trade_id as symbol
**Location:** `bingx_venue.py` lines ~300–310 (within `cancel()`)
**Nature:** Logic error — wrong variable in fallback chain
### Downstream effect
When `BingxVenueAdapter.cancel()` is called and the order's `metadata` dict lacks an `"asset"` key, it falls back:
```python
asset = str(order.metadata.get("asset") or order.internal_trade_id or order.venue_client_id or "")
```
`order.internal_trade_id` is the system's trade_id (e.g., `"cancel-idle-1712345678"`). This gets fed to `self.backend._instrument_venue_symbol(asset)` which does:
```python
def _instrument_venue_symbol(self, asset: str) -> str:
text = _normalize_symbol(asset) # "CANCEL-IDLE-1712345678"
if text.endswith("USDT"):
return f"{text[:-4]}-USDT" # "CANCEL-IDLE-1712345678"-USDT — nonsense
return text # doesn't end with USDT → returns the garbage
```
The cancel HTTP call is sent to BingX with a symbol that doesn't exist. BingX returns an error or silently ignores the request. The cancel silently fails.
This can happen whenever a `VenueOrder` is constructed without `metadata["asset"]`. The mock venue's `_event_from_order` sets `metadata={"intent_id": ..., "action": ...}` but does **not** include `"asset"`. So any cancel path triggered from a mock venue event will hit this bug.
### Exact trigger sequence
1. `MockVenueAdapter.submit()` creates a `VenueOrder` with `metadata={"intent_id": ..., "action": ...}` — no `"asset"`
2. The kernel attaches this order to the slot
3. A CANCEL intent arrives
4. Python bridge calls `self.venue.cancel(self.slot(slot_id).active_entry_order)`
5. `BingxVenueAdapter.cancel()` does `order.metadata.get("asset")` → None
6. Falls back to `order.internal_trade_id` → a trade_id string
7. Sends delete to BingX with a bogus symbol
Note: this only occurs when the mock venue is used in a test configuration. In live mode, `BingxDirectExecutionAdapter` stores richer metadata. But the fallback chain is still wrong and could bite in edge cases.
### Why it's not caught
The live tests always have `metadata["asset"]` populated because the kernel attaches it before calling the venue. The mock venue's cancel path is only exercised in unit tests that don't check the BingX HTTP call content.
### Fix strategy
Change the fallback to use the order's `internal_trade_id` to look up the slot's asset from the kernel, not try to interpret it as a symbol:
```python
# In cancel(), before the fallback:
slot = self._kernel.slot(order.metadata.get("slot_id", 0))
asset = str(order.metadata.get("asset") or slot.asset or "")
```
Or at minimum, add the asset to the mock venue's event metadata.
---
## Flaw 10: Event dedup window is bounded at 64
**Location:** `_rust_kernel/src/lib.rs` lines ~5 (constant), ~850–855 (eviction logic)
**Nature:** Resource management — fixed-size ring buffer with silent eviction
### Downstream effect
Each `TradeSlot` tracks seen events in `seen_event_ids: Vec<String>`. When the vector exceeds 64 entries, the oldest entries are drained:
```rust
if slot.seen_event_ids.len() > MAX_SEEN_EVENT_IDS {
let overflow = slot.seen_event_ids.len() - MAX_SEEN_EVENT_IDS;
slot.seen_event_ids.drain(0..overflow);
}
```
This means:
- Events 1–64 are deduplicated correctly
- When event 65 arrives, event 1 is evicted. If event 1 arrives again, it's accepted as new
- When event 66 arrives, event 2 is evicted, etc.
- After 64 unique events, the dedup window is a rolling window of the last 64 events
With MARKET orders (1–3 events per trade), a slot would need ~20–60 trades before cycling through 64 events. With LIMIT orders that may receive many partial fills per order (e.g., a resting order that gets 5 fills/hour over 6 hours = 30 events), the limit could be hit in a single trade.
### Why it's not caught
No test submits more than ~30 events to a single slot (`rapid_ten_cycle` does 10 entry→exit cycles = ~30 events). The 64 limit was never reached.
### Fix strategy
Either:
1. Increase `MAX_SEEN_EVENT_IDS` to a larger value (256 or 1024), or
2. Use a proper LRU/size-bounded set (e.g., `LruCache` from the `lru` crate), or
3. Change to a HashMap-based dedup keyed by `(event_id, action)` so eviction is explicit
---
## Flaw 11: Reconcile is a raw state override with no FSM validation
**Location:** `_rust_kernel/src/lib.rs` lines ~900–915 (`dita_kernel_reconcile_slots_json`)
**Nature:** Safety — no guards on incoming state
### Downstream effect
The reconcile function blindly overwrites slot state:
```rust
for slot in slots {
if slot.slot_id < core.slots.len() {
core.slots[slot.slot_id] = slot.clone();
}
}
```
There is **zero validation** that the incoming slot state is a valid successor to the current state. A caller could:
- Set `fsm_state = POSITION_OPEN` with `size = 0.0` — the kernel thinks it has an open position with no size
- Set `fsm_state = CLOSED` with `size = 5.0` — the kernel thinks a position is closed but still has size
- Set `fsm_state = ENTRY_WORKING` with `trade_id = ""` — the kernel is in "entry working" state for no trade
- Clear `seen_event_ids` to reset dedup — silently accepting duplicates
The intended use is restoring kernel state from a snapshot after a crash, where the slot state was explicitly serialized by a previous `kernel.snapshot()`. In that case the state should be self-consistent. But there's no guard against malformed or corrupted snapshot data.
### Why it's not caught
The reconcile tests (`reconcile_empty`, `reconcile_after_entry`, etc.) all reconcile with self-consistent slot data from `k.slot(0)`. They never feed malformed state. The `fresh_kernel_reconcile_*` tests similarly use `_slot_from_payload` on data serialized from a real slot.
### Fix strategy
Add validation in the Rust kernel (or Python bridge) that checks basic consistency:
- `fsm_state == POSITION_OPEN` → `size > 0` and `asset` non-empty
- `fsm_state == IDLE` → `size == 0` and `trade_id` empty
- `fsm_state == CLOSED` → `closed == true`
- `size >= 0`
- `slot_id` matches array index
---
## Flaw 12: `outcome.transitions` is incomplete — pre-venue only
**Location:** `rust_backend.py` lines ~490–505, `_rust_kernel/src/lib.rs` lines ~700–710
**Nature:** API contract — returned data is a partial snapshot
### Downstream effect
`process_intent()` runs three phases in sequence:
1. **Rust kernel** processes the intent (pure FSM: `IDLE → ORDER_REQUESTED`)
2. **Venue adapter** submits to exchange (HTTP call, receives ack + fill)
3. **on_venue_event** called per venue response (ORDER_ACK → ENTRY_WORKING, FULL_FILL → POSITION_OPEN)
Each phase produces `KernelTransition` records. But only **phase 1** transitions appear in the returned `KernelOutcome.transitions`:
```python
final_outcome = KernelOutcome(
...
transitions=outcome.transitions, # from Rust — phase 1 only
emitted_events=tuple(emitted_events), # from venue — phases 2-3
...
)
```
A caller inspecting transitions sees `[IDLE → ORDER_REQUESTED]` and has no way to discover that `[ORDER_REQUESTED → ENTRY_WORKING]` and `[ENTRY_WORKING → POSITION_OPEN]` also occurred. The journal (`ClickHouseKernelJournal`) records all transitions correctly — but the returned `KernelOutcome` is the API surface that callers interact with.
### Why it's not caught
The `outcome_inspect_entry` test checks `len(info["transitions"]) > 0` and `info["diagnostic"] == "OK"`. It doesn't validate that all expected transitions are present. The transitions are journaled to the debug sink, but no test reads the journal.
### Fix strategy
Collect transitions from phases 2-3 and append them to the outcome:
```python
all_transitions = list(outcome.transitions)
for event in emitted_events:
event_outcome = self.on_venue_event(event)
all_transitions.extend(event_outcome.transitions)
final_outcome = KernelOutcome(..., transitions=tuple(all_transitions), ...)
```
Or document that `transitions` is an incomplete snapshot and the journal is the authoritative source.
---
## Flaw 13: Slot realized PnL is not reset on re-entry after partial exit
**Location:** `_rust_kernel/src/lib.rs` lines ~575–600 (ENTER intent handler), specifically slot reset
**Nature:** State leakage — accumulated PnL from prior trade survives into next cycle
### Downstream effect
When an ENTER intent arrives, the Rust kernel resets most slot fields:
```rust
slot.trade_id = intent.trade_id.clone();
slot.asset = intent.asset.clone();
slot.side = intent.side.clone();
slot.entry_time = Some(intent.timestamp);
slot.entry_price = 0.0;
slot.size = 0.0;
slot.initial_size = 0.0;
slot.unrealized_pnl = 0.0;
slot.realized_pnl = 0.0; // ← reset to zero
slot.exit_leg_ratios = ...;
slot.active_leg_index = 0;
slot.active_entry_order = None;
slot.active_exit_order = None;
slot.closed = false;
slot.last_event_time = None;
slot.fsm_state = TradeStage::ORDER_REQUESTED;
```
`slot.realized_pnl = 0.0` is explicitly set — correct for a fresh trade. But recall from Flaw 5 that realized PnL from partial fills (before the terminal close) may **not yet have been settled** to the account. If the slot accumulates realized PnL during partial fills, then re-enters before the final settle happens, the in-flight PnL is **zeroed without being settled**.
**This is actually the correct behavior** because:
1. All MARKET-order fills settle immediately (they arrive as FULL_FILL and transition to CLOSED in one shot)
2. For LIMIT orders that partially fill, the re-entry scenario is impossible because the slot isn't IDLE — it can't accept a new ENTER until the position is fully closed
3. The slot CAN re-enter after a full close, and by then all PnL has been settled
So this is a **latent** rather than active flaw. It would manifest if:
1. A LIMIT order partially fills (PnL on slot, not settled)
2. The remaining limit is cancelled
3. The slot's `consume_exit_leg()` leaves the slot in POSITION_OPEN with `size > 0` and `!closed` but no active orders
4. Another ENTER arrives — but the Rust kernel rejects it because `!slot.is_free()`
So the slot design prevents this from happening accidentally. The flaw is that if a future code path bypasses the `is_free()` check (e.g., a force-enter feature), the unreleased PnL would be silently zeroed.
### Why it's not caught
The scenario can't happen with the current FSM. All fills eventually reach CLOSED, which triggers settle. No test forces an entry on a non-free slot.
### Fix strategy
Add an explicit assertion or sentinel in the ENTER handler:
```rust
if slot.realized_pnl.abs() > 1e-10 {
// Log warning: unsynchronized PnL being discarded
}
```
Or enforce that `settle()` is always called before `realized_pnl` is reset, by moving the settle trigger to the Rust side.
---
## Summary table
| # | Flaw | Layer | Severity | Blocks partial-fill? |
|---|------|-------|----------|---------------------|
| 1 | Entry-order cancellation broken | Python + Rust | **Critical** | **Yes** — can't cancel resting LIMIT entries |
| 2 | No CANCEL_ACK → IDLE for entry | Rust FSM | **Critical** | **Yes** — slot stuck after cancelled entry |
| 3 | Outcome mixes pre/post-venue state | Python bridge | Medium | No |
| 4 | Multi-leg exit double-close | Rust FSM | Low | No |
| 5 | Capital settle only on terminal state | Python bridge | **High** | **Partial** — stale capital during partial fills |
| 6 | order_type/limit_price dropped in legacy intent | Python venue | **Critical** | **Yes** — LIMIT orders never reach BingX |
| 7 | Mock venue single ratio for entry+exit | Mock venue | Low | No (mock tests only) |
| 8 | Missing price formatting | Adapter | **High** | **Yes** — BingX rejects bad price precision |
| 9 | Cancel falls back to trade_id as symbol | Python venue | Medium | No |
| 10 | Event dedup window at 64 | Rust FSM | Low | No |
| 11 | Reconcile has no FSM validation | Rust FSM | Low | No |
| 12 | Outcome transitions incomplete | Python bridge | Medium | No |
| 13 | Unsettled realized PnL on re-entry | Rust FSM | Low | No |
**6 critical/high** — must be fixed before safe LIMIT order / partial-fill deployment.
**4 medium** — should be fixed in the same pass to keep hygiene.
**3 low** — latent; fix opportunistically.

View File

@@ -1,299 +0,0 @@
# CRITICAL: Partial Fill Support — Kernel, Adapter & Test Suite
**Date:** 2026-05-29
**Author:** E2E test-automation analysis
**Status:** Not implemented — spec for the next work session
---
## The gap
**Zero tests exercise a `PARTIALLY_FILLED` venue event.** Every scenario submits `MARKET` orders (hardcoded in `BingxDirectExecutionAdapter.submit_intent()` line 359). On liquid testnet pairs (TRXUSDT, XRPUSDT, ADAUSDT), market orders fill **instantly in one shot**. The kernel's `on_venue_event` handler handles `PARTIAL_FILL` → `KernelEventKind.PARTIAL_FILL` → slot FSM transition, but **this code has never executed on a live exchange** in the existing 142-scenario suite.
The multi-leg exit system (50% + 50% sequential `EXIT` intents) exercises *synthetic* partial fills — two separate MARKET orders each exiting half. That is **not** a true exchange-level partial fill where one order receives multiple fill events with a `remaining_size` > 0 between them.
---
## What needs to change
Three layers must be touched:
1. **`KernelIntent` (contracts.py)** — add `order_type` and `limit_price` fields
2. **`BingxDirectExecutionAdapter` (bingx_direct.py)** — read the new fields; build payload with correct `"type": "LIMIT"` and `"price"`
3. **`BingxVenue` (bingx_venue.py)** — read the new fields from `KernelIntent` when building receipt; propagate limit price to acknowledge events
4. **Test file (test_bingx_live.py)** — add scenarios that submit LIMIT orders at non-aggressive prices to produce partial fills
---
## Layer 1: `KernelIntent` — two new fields
**File:** `prod/clean_arch/dita_v2/contracts.py`
```python
@dataclass(frozen=True)
class KernelIntent:
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
# === NEW FIELDS ===
order_type: str = "MARKET" # "MARKET" | "LIMIT" | "POST_ONLY"
limit_price: float = 0.0 # ignored if order_type == "MARKET"
```
**Rationale for defaults:** Existing call sites that construct `KernelIntent(...)` directly (all 142 test bodies, `_si()` helper, the intent projection code) do not pass `order_type` or `limit_price` — they get MARKET by default. Zero code changes outside the intent paths that intentionally want LIMIT orders.
**Rust kernel implications:** The Rust backend serializes `KernelIntent` to JSON before passing to the `.so`. The new fields must be included in that JSON serialization. Check `_intent_to_payload` or equivalent serialization in the Python proxy:
```python
# In rust_backend.py — wherever KernelIntent is serialized
payload = {
"timestamp": intent.timestamp.isoformat(),
"intent_id": intent.intent_id,
# ... existing fields ...
"order_type": intent.order_type, # NEW
"limit_price": intent.limit_price, # NEW
}
```
The kernel's Rust code will receive `order_type` and `limit_price` in its intent route. If it ignores them (doesn't use them for any FSM logic), that's fine — they're pass-through fields for the venue adapter. But they **must be in the serialized JSON** so the adapter can read them.
---
## Layer 2: `BingxDirectExecutionAdapter` — use `order_type` and `limit_price`
**File:** `prod/clean_arch/adapters/bingx_direct.py`
### Current (line 359)
```python
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": "MARKET", # HARDCODED
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if reduce_only:
payload["reduceOnly"] = "true"
```
### Required
```python
order_type = (intent.order_type or "MARKET").upper()
# POST_ONLY is a LIMIT that must not take liquidity — BingX calls it a "limit maker"
if order_type == "POST_ONLY":
order_type = "LIMIT" # BingX uses a separate flag for post-only
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": order_type,
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if order_type == "LIMIT" and intent.limit_price > 0:
# BingX requires "price" and "timeInForce" for LIMIT orders
price = intent.limit_price
# Ensure price has the right decimal precision for the symbol
payload["price"] = self._format_price(intent.asset, price)
payload["timeInForce"] = "GTC" # Good-Til-Cancelled (or "IOC" for immediate-or-cancel)
if order_type_orig == "POST_ONLY":
payload["timeInForce"] = "GTX" # Post-only = GTX on BingX
if reduce_only:
payload["reduceOnly"] = "true"
```
`_format_price` likely doesn't exist yet. Add it. For TRXUSDT it needs 6 decimal places (price ~$0.08), for XRPUSDT it needs 4 (`$0.52`). The quantity formatter already handles this — `_format_quantity` uses a symbol→precision lookup. Same approach for price.
**BingX LIMIT order caveats (VST testnet):**
- `"price"` must have the correct decimal precision per symbol or the order is rejected.
- `"timeInForce"` defaults to GTC if omitted — document this.
- POST_ONLY = LIMIT + `"timeInForce": "GTX"`. BingX VST supports it.
- **Partial fills are guaranteed** when a LIMIT order's price straddles the spread and only part of the quantity matches against the book.
---
## Layer 3: `BingxVenue` event emission for LIMIT orders
**File:** `prod/clean_arch/dita_v2/bingx_venue.py`
### `submit()` method (line ~348)
The `_legacy_intent(intent)` conversion currently drops `order_type`/`limit_price`. Update:
```python
def _legacy_intent(self, intent: KernelIntent) -> dict:
return {
"asset": intent.asset,
"side": intent.side,
"action": intent.action,
"target_size": intent.target_size,
"reference_price": intent.reference_price,
"leverage": intent.leverage,
"exit_leg_ratios": intent.exit_leg_ratios,
"order_type": intent.order_type, # NEW
"limit_price": intent.limit_price, # NEW
"reason": intent.reason,
}
```
### `_events_from_submit()` (line ~370+)
The `price` field in the emitted `VenueEvent` should use the `limit_price` for LIMIT orders when the fill hasn't happened yet. Currently it uses `safe_float(getattr(receipt, "price", 0.0), 0.0)` which is often 0 for market orders. For LIMIT orders the receipt should contain the price:
```python
price = (
safe_float(getattr(receipt, "price", 0.0), 0.0)
or (intent.limit_price if intent.order_type in ("LIMIT", "POST_ONLY") else 0.0)
)
```
### Reconcile path (`_event_from_row`, line ~522+)
The reconcile path already handles `PARTIALLY_FILLED` status and converts it to `KernelEventKind.PARTIAL_FILL`. It reads `filled_size` and computes `remaining_size` correctly. This code path is correct — it just needs to be triggered, which requires LIMIT orders that partially fill.
---
## Layer 4: Test scenarios
**File:** `prod/tests/test_pink_bingx_dita_live_e2e.py`
All new scenarios are kernel-direct — they construct `KernelIntent` directly with `order_type="LIMIT"` and a `limit_price` that guarantees a partial fill.
### Strategy for guaranteed partial fills on BingX VST
The testnet's order book has bid/ask spread. For a **BUY/LONG** LIMIT order:
- Set `limit_price` *between* the best bid and best ask.
- The order will match against any asks at or below `limit_price`.
- If `limit_price` is below the lowest ask, only part of the quantity fills.
- The remaining becomes a resting limit order.
For a **SELL/SHORT** LIMIT order:
- Set `limit_price` *between* the best bid and best ask.
- The order will match against any bids at or above `limit_price`.
- Remaining becomes a resting limit order.
**Easiest approach:** Use `iceberg` / hidden-order techniques aren't needed — just set `limit_price = p * 0.9995` (0.05% inside the spread) so that an approximate half of the order walks the book and the rest sits on the book. On liquid pairs this produces a `PARTIALLY_FILLED` status on the ack.
### Scenario: `limit_partial_entry_cancel`
```
1. Fetch current price p.
2. Submit LIMIT SHORT ENTER at limit_price = p * 1.0005 (slightly above market for short = inside spread) with target_size=0.002
3. Sleep 300ms.
4. Check remaining size — if > 0, cancel the resting portion.
5. If slot still occupied (fill happened), exit the filled portion.
6. Verify: exchange flat, capital integrity.
```
Outcomes:
- If partial fill: `VenueEvent` with `PARTIALLY_FILLED` status, `remaining_size > 0`. Cancel stops the resting leg. Kernel processes `CANCEL_ACK` and leaves slot with the filled partial. Exit clears it.
- If full fill: Immediately filled. Cancel is a no-op. Exit clears.
- If no fill: No fill at all. Cancel removes the LIMIT from the book. Slot returns to IDLE trivially.
### Scenario: `limit_resting_then_cancel`
```
1. Submit LIMIT SHORT ENTER at limit_price = p * 0.995 (below market — won't fill for SHORT sell).
2. Sleep 1s.
3. Assert slot is in ENTRY_WORKING (limit resting on book).
4. Cancel.
5. Verify: slot IDLE, exchange has no position.
```
This validates the ENTRY_WORKING state with a resting limit order — none of the 142 existing tests ever leave an order working for more than ~1s before a MARKET fill.
### Scenario: `limit_partial_multi_leg_exit`
```
1. Enter SHORT via MARKET (normal fill).
2. Exit via LIMIT in two legs:
- LIMIT EXIT leg 1 at limit_price = p*0.997 (50% size)
- LIMIT EXIT leg 2 at limit_price = p*0.995 (50% size)
3. If remaining > 0 after each exit, cancel the resting portion and MARKET exit the rest.
4. Verify: flat, capital integrity.
```
This exercises `PARTIALLY_FILLED` on exit orders — the `on_venue_event` handler with `PARTIAL_FILL` in the exit direction.
### Scenario: `limit_quick_resting_and_reentry`
```
1. Submit LIMIT SHORT ENTER at p*0.997 (won't fill).
2. Without cancelling, submit MARKET SHORT ENTER with different trade_id.
3. Expect SLOT_BUSY rejection on the MARKET entry.
4. Cancel the resting LIMIT.
5. Submit MARKET entry and exit normally.
```
Validates that a pending limit order blocks the slot correctly.
---
## Summary table of changes
| File | Change | Risk |
|------|--------|------|
| `contracts.py` | Add `order_type: str = "MARKET"`, `limit_price: float = 0.0` to `KernelIntent` | **Low** — defaults preserve existing behaviour |
| `rust_backend.py` (serialization) | Include `order_type` and `limit_price` in JSON payload to Rust | **Low** — Rust ignores unknown fields |
| `bingx_direct.py` | Replace hardcoded `"type": "MARKET"` with dynamic field; add `price` and `timeInForce` for LIMIT; add `_format_price` helper | **Medium** — wrong decimal precision causes BingX rejection |
| `bingx_venue.py` | Pass `order_type`/`limit_price` through `_legacy_intent()`; use for `price` in VenueEvent | **Low** — pass-through only |
| `test_bingx_live.py` | Add 4+ LIMIT/partial-fill scenarios | **Low** — same pattern as existing kernel-direct tests |
## Testing the partial fill code path
Once the changes are deployed:
```
# Run partial-fill scenarios specifically
pytest prod/tests/test_pink_bingx_dita_live_e2e.py -k "limit_partial" -v --tb=short
# Check that PARTIALLY_FILLED events appear
grep "PARTIAL_FILL\|PARTIALLY_FILLED" /tmp/pink_venue.log
# Full regression — all 142 existing MARKET scenarios must still pass
pytest prod/tests/test_pink_bingx_dita_live_e2e.py --no-header -p no:cacheprovider
```
The `PARTIALLY_FILLED` event path in `bingx_venue.py` lines 408–431 and `_event_from_row` lines 522–574 is the code that has **zero live-test coverage today**. These scenarios would close that gap.
---
## Appendix: BingX LIMIT order API reference
From the BingX swap API (`/openApi/swap/v2/trade/order`):
| Parameter | Required | Description |
|-----------|----------|-------------|
| `symbol` | Yes | Trading pair, e.g. "TRXUSDT" |
| `side` | Yes | "BUY" or "SELL" |
| `positionSide` | Yes | "BOTH" for USDT-M perpetuals |
| `type` | Yes | "MARKET" or "LIMIT" |
| `quantity` | Yes | Contract quantity |
| `price` | No (required for LIMIT) | Order price — decimal precision depends on symbol |
| `timeInForce` | No | "GTC", "IOC", "FOK", "GTX" (post-only). Defaults to GTC. |
| `reduceOnly` | No | "true" for exits |
| `clientOrderId` | No | Client-generated ID |
| `recvWindow` | No | Timestamp recv window in ms |
For LIMIT orders on VST testnet:
- Partial fill is certain when `limit_price` is at or near the mid-price.
- Use `timeInForce="GTC"` to let the order rest.
- Use `timeInForce="GTX"` for post-only (guarantees maker, never takes liquidity — but fills may be slower).

File diff suppressed because it is too large Load Diff

View File

@@ -1,63 +0,0 @@
# Sprint 0 — DITAv2 flaw-fix verification report
**Date:** 2026-05-30
**Scope:** Verify (do not re-implement) the DITAv2 flaw fixes before migrating PINK
onto the kernel for BingX testnet (MARKET single-leg first). Source read + offline
MockVenue test execution. No exchange contact.
## Method
- Read the full Rust FSM (`_rust_kernel/src/lib.rs`, 1700 L) and the Python bridge
(`rust_backend.py`) + `account.py` + `mock_venue.py`.
- Hardened previously-vacuous guarded assertions in `test_flaws.py` so each flaw test
genuinely exercises its fix (details below).
- Ran all offline suites under `siloqy_env` with `PYTHONPATH=/mnt/dolphinng5_predict`.
## Offline test results (all green)
| Suite group | Result |
|---|---|
| `test_flaws.py` (hardened) | 35 passed |
| kernel FSM + accounting invariants + kernel bridge + multi-exit contract | 402 passed |
| pink direct-runtime, CH persistence, multi-exit integration/fuzz, restart-reconcile, rate-limit, routing, sync/async seams | 96 passed |
| **Total** | **533 passed, 0 failed** |
(Two benign warnings: `EDAIN normalizer not available` — unrelated import; one
`coroutine never awaited` inside an intentional hang-detection test.)
## Test-hardening performed (removed false-green guards)
1. **Flaw 5 / `test_partial_exit_settles_pnl_incrementally`** — was entering & exiting at
the *same* price (realized_pnl == 0) under a `if slot.realized_pnl != 0.0:` guard, so the
capital assertion never ran. Now: SHORT entry @100, exit @90 → realized PnL strictly
positive, and asserts **capital moved by EXACTLY realized PnL** (`|Δcapital − realized| < 1e-9`).
This is the core single-authority invariant and is now unconditional.
2. **Flaw 2 / `test_cancel_ack_exit_still_works`** — exit auto-filled in the default scenario,
so the exit order was already gone (`if slot.active_exit_order is not None:` skipped). Now
uses `exit_partial_fill_ratio=0.5` so the exit order stays live, then asserts CANCEL_ACK
clears it and returns the slot to `POSITION_OPEN`.
3. **Flaw 9 / `test_cancel_uses_slot_asset_not_trade_id`** — guard made unconditional (ACK-only
entry deterministically leaves the entry order live).
4. **Flaw 12 / `test_transitions_count_matches_lifecycle`** — guard made unconditional.
5. **Flaw 13 / `test_pnl_warning_on_unsettled_reentry`** — `if slot.is_free():` made unconditional.
## Per-flaw verdict (MARKET single-leg path = Sprint 1)
| Flaw | Severity | Fixed? | Evidence |
|---|---|---|---|
| 1 — entry-order cancel broken | Critical | **FIXED** | `lib.rs` CANCEL branch accepts entry cancel when `active_entry_order` set & state ∈ {ENTRY_WORKING,ORDER_REQUESTED,ORDER_SENT,IDLE}; bridge emits `venue.cancel`. 5 tests pass. |
| 2 — no CANCEL_ACK→IDLE for entry (hung orders) | Critical | **FIXED** | `lib.rs:1193-1212` CANCEL_ACK entry branch clears order + resets trade_id/asset/side/size/PnL → IDLE. Non-vacuous tests pass. |
| 5 — capital settle only on terminal | High | **FIXED** | bridge `on_venue_event` settles incremental `realized_pnl` per fill; `account.settle()` moves capital by exactly that amount. Exact-invariant test passes. |
| 6 — LIMIT order_type/limit_price dropped | Critical | FIXED (N/A to MARKET) | payload carries `order_type`/`limit_price`; out of scope for MARKET-only Sprint 1. |
| 4 — double-close/double-settle on final leg | Low | **FIXED** | `apply_fill` exit branch: realized accrues once/fill; `should_close` guarded by size; closed slot rejects further EXIT (`NO_OPEN_POSITION`); dup fills deduped. |
| 10 — event dedup window | Low | **FIXED** | `seen_event_ids` (cap 256, FIFO evict); duplicate events short-circuit to `DUPLICATE_EVENT`. Tests pass. |
| 11 — reconcile validation | Low | **FIXED** | `reconcile_slots_json` validates every slot via `validate_slot` and rejects the whole batch without mutating on failure. Tests pass. |
| 13 — re-entry PnL loss | Low | **FIXED** | ENTER resets realized/unrealized/size; bridge resets `_last_settled_pnl[slot]` on ENTER. Tests pass. |
| 3, 7, 8, 9, 12 | Med/Low | FIXED | covered by hardened/passing tests. |
## GATE decision
**PASS.** The MARKET-path-critical flaws (1, 2, 5) are confirmed fixed in source and proven
by non-vacuous offline tests. Sprint 1 (PINK single-leg MARKET on BingX testnet/VST) may proceed.
## Carry-forward risks (NOT GATE blockers)
- **Sprint 3 (multi-leg) sizing:** the exit branch computes `exit_size = base_size × ratio` with
`base_size = initial_size` and cumulative ratios (e.g. `0.5, 1.0`). On the final leg this can
exceed the *remaining* position; the kernel currently relies on the venue clamping the fill to
the open size. Validate on testnet before enabling `multi_exit`.
- **LIMIT / partial-fill** remains explicitly out of scope (MARKET-only bring-up).

View File

@@ -1,88 +0,0 @@
# Sprint 2 — Accounting + observability parity verification
**Date:** 2026-05-30
**Scope:** Verify (no behaviour change) that the DITAv2 PINK runtime preserves
BLUE-legacy-compatible ClickHouse row shapes in `dolphin_pink`, and that capital
authority in the hot loop is solely the kernel's `AccountProjection`. Offline only
(MockVenue / unit), no exchange contact. Continues [SPRINT0_FLAW_VERIFICATION.md].
## 1. Row-shape parity — `clean_arch/persistence/pink_clickhouse.py`
BLUE-legacy row families written, same schema / no new columns:
| Row family | Writer | Status |
|---|---|---|
| `policy_events` + `v7_decision_events` | `_write_policy_event` | ✅ |
| `account_events` | `_write_account_event` | ✅ |
| `position_state` | `_write_position_state` | ✅ |
| `status_snapshots` | `_write_status_snapshot` | ✅ |
| `trade_events` | `_write_trade_event` | ✅ (terminal close) |
| `trade_reconstruction` | `_write_trade_reconstruction` | ✅ (ENTRY/PARTIAL/EXIT) |
| `anomaly_events` | `_write_anomaly` / `record_anomaly` | ✅ |
| `trade_exit_legs` | — | ⚠️ **listed in docstring, no writer** |
`trade_exit_legs` has no emitter. It is a **multi-leg** row family → relevant to
**Sprint 3** (`DOLPHIN_PINK_PHASE=multi_exit`), not single-leg MARKET. **Not a
Sprint 1/2 blocker.** Action: add the writer when Sprint 3 is taken up, or confirm
BLUE TUI/observability does not require it for single-leg trades.
## 2. Capital authority — single source = kernel `AccountProjection`
`clean_arch/runtime/pink_direct.py` hot loop (`step`, L309-408):
- Capital is **read only** from `kernel.snapshot()["account"]` (L320, L370, L395).
- Capital is **mutated only** by `kernel.process_intent()` → `account.settle()` on fill.
- **No balance-poll overwrite anywhere in `step()`.** ✅
External capital writes (all outside the hot loop, by design):
- `_reconcile_position_slot` (L188-194) — the **single** place an exchange balance
snapshot seeds `account.snapshot.capital`; called at startup/recovery only.
- `connect()` (L230) seeds from the **env default** `initial_capital`, not an
exchange poll (per code comment L228-229).
- `recover_account()` (L431) re-seeds from `kernel.account.snapshot.capital`
(the kernel's own value) — **not** an exchange poll.
**Doc/code note (no change made):** `reconcile_account()` (L453) *docstring* says it
"re-seeds capital from the exchange balance as a guard against drift," but the code
path (`recover_account`) actually re-seeds from the kernel's own capital — i.e. it
does **not** overwrite from an exchange poll. Behaviour is the safe one; only the
comment overstates. Flagged for accuracy; not edited (no behaviour change w/o auth).
`pink_clickhouse.py` reads capital/peak/seq solely from `account.snapshot`
(`_capital`/`_peak_capital`/`_trade_seq`, L193-201) — no duplicate tracking. ✅
## 3. Offline test results
`siloqy_env`, `PYTHONPATH=/mnt/dolphinng5_predict`, run from repo root.
| Suite | Result |
|---|---|
| `test_pink_clickhouse_persistence.py` | ✅ pass |
| `test_pink_ditav2_accounting_invariants.py` | ✅ pass |
| `test_pink_direct_runtime.py` | ✅ pass |
| **DITAv2 PINK Sprint-2 scope** | **14 passed** |
| `test_bingx_capital_accounting_battery.py` | ❌ 2 failed — **legacy path, out of scope** |
The 2 failures are in the **legacy** Nautilus BingX execution/journal path
(`prod/bingx/execution.py` + `prod/bingx/journal.py`, imported via
`launch_dolphin_live`) — **not** a DITAv2 PINK file, untracked/pre-existing, not
modified by this engagement. Root cause: the fuzz/equivalence tests reuse
`fingerprint="fp"` across iterations, so `bingx_journal.write_snapshot` fingerprint-
dedup short-circuits the sink and `captured["row"]` is never set (`KeyError`). This
lives on the legacy side of the BLUE do-not-touch boundary → **not fixed here**.
## GATE decision
**PASS (DITAv2 PINK scope).** Row-shape parity holds for single-leg MARKET; capital
authority is single (kernel `AccountProjection`) with no hot-loop balance overwrite;
all PINK-scoped offline suites green.
## Carry-forward (Sprint 3)
- ✅ **CLOSED (offline groundwork, 2026-05-30):** `trade_exit_legs` writer added to
`pink_clickhouse.py` (`_write_trade_exit_leg`, BLUE-schema-faithful, isolated per-leg
deltas tracked via `self._leg_state`, reset on ENTER). Fires once per exit leg.
- ✅ **CLOSED (offline groundwork):** cumulative-ratio exit sizing overshoot validated —
`test_pink_multi_exit_groundwork.py::test_final_leg_overshoot_does_not_oversell` proves a
final EXIT requesting more than the remaining size clamps (size→0, no oversell, closes once).
Validation suite: 3 passed; persistence regression: 10 passed.
- ⏳ **PENDING (live):** the on-exchange multi-leg run (successive MARKET exits on VST to
confirm Flaw 4 end-to-end) is deferred — requires explicit authorization for additional
live testnet orders beyond the single Sprint 1 round trip.

View File

@@ -1,444 +0,0 @@
# PINK DITAv2 — Live BingX Testnet E2E: Results & Spec
**Date:** 2026-05-29
**Suite:** `prod/tests/test_pink_bingx_dita_live_e2e.py`
**Venue:** BingX VST (validation testnet)
**Kernel:** DITAv2 `ExecutionKernel` (Rust-backed via ctypes)
**Execution mode:** Kernel-direct — bodies receive `(k, symbol, p)` and call `k.process_intent()` directly, bypassing `DecisionEngine`/`IntentEngine`.
---
### Group 20: Restart / Reconcile (6 scenarios, 6/6 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `reconcile_empty` | Call `reconcile_from_slots([])` on an idle kernel | Empty-slot reconcile is a no-op — no crash, no state corruption |
| `reconcile_after_entry` | Enter SHORT, reconcile, then exit | Slot survives reconcile in POSITION_OPEN state; exit still works |
| `reconcile_after_exit` | Enter, exit, reconcile post-close | Reconcile on a CLOSED slot is idempotent |
| `reconcile_after_cancel` | Enter, cancel, then reconcile | Cancel-ack state persists through reconcile |
| `reconcile_twice` | Two consecutive reconciles on the same slot | Double reconcile is idempotent — no double-counting |
| `reconcile_then_cancel` | Reconcile, then check if cancel still works | Kernel can still process intents after reconcile |
**Nominal market behaviour:** `reconcile_from_slots()` rebuilds the kernel's internal slot book from a list of `TradeSlot` payloads. It does not touch the exchange — it's a state-reconstruction operation. The kernel accepts it at any lifecycle stage. After reconcile, the slot FSM continues from its current state. Reconciling an empty slot list leaves all slots IDLE. Reconciling twice in a row applies the same state twice with no ill effect.
### Group 21: Chaos / Fuzz (8 scenarios, 8/8 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `concurrent_enter_cancel` | ENTER + CANCEL with zero delay in the same async tick | Kernel doesn't crash on back-to-back intents; cancel may be ack or no-op depending on race |
| `rapid_alternating` | SHORT→cancel→LONG→cancel in 200ms bursts | FSM handles rapid direction flips gracefully — no state corruption |
| `duplicate_trade_id` | Two ENTER intents with the same `trade_id` | Second is rejected (SLOT_BUSY), first proceeds normally |
| `slot_busy_double_entry` | Two ENTER intents with different trade_ids on same slot | Second returns SLOT_BUSY diagnostic code — kernel doesn't submit duplicate orders |
| `exit_on_idle_slot` | EXIT intent on an already-IDLE slot | Kernel returns diagnostic (not OK) but does not crash |
| `cancel_on_idle_slot` | CANCEL intent on an already-IDLE slot | Same graceful rejection — no exception, no venue call |
| `cancel_after_exit_fill` | Exit fills, then CANCEL arrives for the same trade | Redundant cancel is a no-op — kernel accepts it but doesn't submit to venue |
| `rapid_ten_cycle` | 10 sequential entry→exit cycles at 400ms intervals per cycle | Slot reuse stress — 10 full FSM traversals without state leaks |
**Nominal market behaviour:** All `process_intent()` calls return an `KernelOutcome` object. When the kernel rejects an intent (`SLOT_BUSY`, invalid FSM transition), it returns `accepted=False` with a descriptive `diagnostic_code` — it does not raise an exception or crash. The `concurrent_enter_cancel` test specifically validates that two intents submitted back-to-back without `await` in between both get processed. `cancel_after_exit_fill` validates the common race condition where an exit fills before the CANCEL arrives — the kernel must not send a redundant cancel to the venue. `rapid_ten_cycle` validates that 10 full FSM cycles leave the slot in IDLE with no residual state (no accumulated leg counters, no stale event IDs, no capital drift).
---
## Failure analysis
## Test architecture
All 142 scenarios share a single entry point via `@pytest.mark.parametrize`:
```
test_pink_ditav2(name, body_fn)
├── _build_rb() → builds DITAv2 bundle (kernel + venue + control plane)
├── _pick_live_symbol() → picks a symbol not currently in an exchange position
├── _snap() → fetches current market price from BingX REST
├── _run(bundle, client, body_fn, name, ic)
│ ├── pre-clean flatten (if slot occupied)
│ ├── capture capital_before = kernel.account.snapshot.capital
│ ├── await body_fn(k, symbol, p) ← the scenario
│ ├── assert capital_after > 0 # no capital wipe
│ ├── assert capital_after < capital_before * 10 # no unbounded drift
│ ├── post-clean flatten (if slot still occupied)
│ ├── _throttle(3.0) # rate-limit gap
│ └── _verify(client, vsym) → assert positions_flat # exchange-side check
└── assert result.positions_flat
```
Each scenario body is an `async def` that receives `(k, symbol, p)` — the kernel, the chosen symbol string, and the current market price as a float. The body calls the `_si()` helper which constructs a `KernelIntent` and passes it to `k.process_intent()`.
### What "PASSED" means for every test
A test passes when **all** of the following hold:
1. **No unhandled exceptions** — kernel accepts every intent without crashing.
2. **Capital integrity** — `kernel.account.snapshot.capital` stays positive and within 10× of its initial value after the scenario executes.
3. **Exchange flat** — a direct `GET /openApi/swap/v2/user/positions` call to BingX confirms zero open position size for the traded symbol.
4. **No hung orders** — the slot FSM reaches `IDLE` or `CLOSED`; no entry/exit orders remain active.
### Rate limiting
A 3-second wall-clock throttle (`_throttle(3.0)`) enforces a minimum gap between each test's exchange HTTP calls. This prevents BingX rate-limit errors. With 142 tests × ~6–12 REST calls each, the full suite runs in ~60 min without a single rate-limit rejection.
---
## Scenario families and results
### Group 1: Basic entry/exit (9 scenarios, 9/9 PASS)
| # | Scenario | What it tests | Rationale |
|---|----------|---------------|-----------|
| 1 | `simple_entry_exit` | Enter SHORT at market, exit at 0.5% profit | Baseline — verifies the entire intent→venue→fill→settle pipeline |
| 2 | `multi_leg_exit` | Enter 2x size, exit 50% leg, exit 50% leg | Multi-leg partial-fill lifecycle — no double-counting of capital |
| 3 | `cancel_entry_order` | Enter SHORT, cancel immediately | Cancel-ack FSM transition: ENTRY_WORKING → IDLE |
| 4 | `entry_hold_exit` | Enter, wait 3s, exit | Position aged in market — mark-to-market, fill price tolerance |
| 5 | `entry_exit_at_loss` | Enter SHORT, exit at 0.5% loss (price up) | Loss exit — realized PnL is negative, capital decreases but stays positive |
| 6 | `two_sequential_cycles` | Enter→Exit→Enter→Exit on same symbol | Slot reuse — kernel resets correctly after CLOSED state |
| 7 | `entry_then_recover` | Enter SHORT, cancel, flatten if needed | Exit path after clean — replaces old buggy disconnect/reconnect body |
| 8 | `long_entry_exit` | Enter LONG at market, exit at 0.5% profit | Long-side symmetry — opposite PnL direction, same FSM |
| 9 | `cancel_idempotent` | Enter, cancel once, cancel again | Second CANCEL on already-cancelled order returns OK, not error |
**Nominal market behaviour:** BingX fills market orders at or near the requested price within 1–3s on VST. The kernel receives `FULL_FILL` events via the venue adapter, transitions the slot through `ENTRY_WORKING → POSITION_OPEN` (entry) and `EXIT_WORKING → IDLE` (exit). Cancel requests return `CANCEL_ACK` and the slot returns to `IDLE` without requiring an exit. Capital reflects the PnL spread (±fees) correctly.
### Group 2: Cancel combinations (6 scenarios, 6/6 PASS)
| # | Scenario | What it tests | Rationale |
|---|----------|---------------|-----------|
| 10 | `double_cancel` | Enter, cancel, cancel again | Two cancels on same active order — second is no-op not error |
| 11 | `cancel_then_exit` | Enter, cancel attempt, if slot still open → exit | Guard pattern: conditional exit only if cancel didn't flatten |
| 12 | `exit_then_cancel_exit` | Enter, exit, cancel same exit | Cancel on an exit order that may already be filling — idempotent |
| 13 | `exit_then_reentry` | Enter→Exit→re-Enter on same symbol | Slot lifecycle reset: IDLE → ... → CLOSED → IDLE → ... → OPEN |
| 14 | `limit_cancel` | Enter LIMIT at 90% market, cancel | Limit (non-market) order — if unfilled, cancel returns unfilled slot |
**Nominal market behaviour:** BingX VST fills market orders quickly. A second cancel on an already-filled order is harmless — the venue adapter returns the current state without error. The kernel's idempotency logic (tracked via `VenueEvent.event_id` dedup in the slot image) prevents duplicate economic effects.
### Group 3: X4 — combinatorial stress (10 scenarios, 10/10 PASS)
| # | Scenario | Key assertion |
|---|----------|---------------|
| 15 | `x4_partial_hold_exit` | Two-leg exit with 30%/70% ratio at different prices |
| 16 | `x4_three_leg` | Three-leg 25%/25%/50% with price step-downs |
| 17 | `x4_cancel_fill_partial` | Cancel after fill, conditional double exit |
| 18 | `x4_rapid_three` | Three rapid entry→exit cycles with decaying price |
| 19 | `x4_diff_symbol` | Enter on A, attempt exit on B (cross-symbol edge) |
| 20 | `x4_alternating` | SHORT on A, LONG on B, exit both |
| 21 | `x4_multi_flatten` | Flatten loop — call exit until slot is free |
| 22 | `x4_three_leg_25_50_25` | Three-leg with unequal 25%/50%/25% distribution |
| 23 | `x4_enter_exit_hold_twice` | Three sequential round-trips on same symbol |
| 24 | `x4_cancel_then_double_exit` | Cancel, then conditional two-leg exit |
**Nominal market behaviour:** Multi-leg exits require the kernel to track the `exit_leg_ratios` tuple and progressively consume legs. Each `EXIT` intent uses `k.slot(0).next_exit_ratio()` to determine the portion to exit. The kernel's `consume_exit_leg()` advances the leg index. Capital delta is applied exactly once per leg — verified indirectly by capital remaining within bounds across all legs.
### Group 4: 2 sides × 2 profit × 4 patterns (16 scenarios, 16/16 PASS)
| Pattern | Short profit | Short loss | Long profit | Long loss |
|---------|-------------|------------|-------------|-----------|
| `basic` | PASS | PASS | PASS | PASS |
| `partial` | PASS | PASS | PASS | PASS |
| `cancel` | PASS | PASS | PASS | PASS |
| `double_exit` | PASS | PASS | PASS | PASS |
**Nominal market behaviour:** Profit exits (SHORT at p*0.995, LONG at p*1.005) reduce capital by trading costs. Loss exits (SHORT at p*1.005, LONG at p*0.995) increase notional loss. Both paths leave the slot flat. The `partial` pattern exits 50% at first target and 50% at a more aggressive second target — fills occur at different prices, and the kernel settles realized PnL from each leg independently.
### Group 5: Triple sequential (8 scenarios, 8/8 PASS)
| Scenario | What it proves |
|----------|----------------|
| `triple_seq_0..3` | 4 different SHORT symbols × 3 cycles each = 12 entries/exits |
| `triple_seq_long_0..3` | LONG mirror — 3 cycles at incrementally better entry prices |
**Nominal market behaviour:** The span variable `for j in range(3)` produces entry→exit→entry→exit→entry→exit on the same symbol. Each `process_intent()` call for the next entry only happens after the previous exit has filled and the slot has returned to `IDLE`. The kernel correctly resets per-trade state (entry price, realized PnL, leg counter) between cycles.
### Group 6: Cancel+reenter (8 scenarios, 8/8 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_reenter_0..3` | SHORT — enter, cancel, re-enter at better price, exit |
| `cancel_reenter_long_0..3` | LONG — same pattern, opposite side |
**Nominal market behaviour:** After cancel-ack, the slot is `IDLE` and a fresh entry is required. The kernel allocates a new `trade_id` for the re-entry. The first entry's exit_leg_ratios are discarded; the re-entry may use different ratios. Exchange state shows zero position during the gap.
### Group 7: Leg ratio variants (8 scenarios, 8/8 PASS)
| # | Ratio tuple | Exit legs |
|---|-------------|-----------|
| 0 | (0.1, 1.0) | 10% leg → 90% leg |
| 1 | (0.33, 0.33, 1.0) | 33% → 33% → 34% |
| 2 | (0.5, 0.5, 1.0) | 50% → 50% |
| 3 | (0.75, 1.0) | 75% → 25% |
| 4 | (0.2, 0.3, 0.5, 1.0) | 20% → 30% → 50% |
| 5 | (0.4, 0.6, 1.0) | 40% → 60% |
| 6 | (0.15, 0.85, 1.0) | 15% → 85% |
| 7 | (0.25, 0.25, 0.5, 1.0) | 25% → 25% → 50% |
**Nominal market behaviour:** The kernel tracks each leg's fill price independently. The sentinel ratio (always `1.0` as the last element) marks the final leg. After the last exit, `k.slot(0).is_free()` returns True. Exchange position size after all legs = 0.
### Group 8: Breakeven (4 scenarios, 4/4 PASS)
| Scenario | Action |
|----------|--------|
| `breakeven_0..3` | Enter SHORT, exit at same price (p → p) |
**Nominal market behaviour:** Exit at entry price results in zero gross PnL minus trading fees. Capital decreases by fees only — the settlement applies the exact difference between entry and exit fill prices × size, which is zero. Exchange flat, slot `IDLE`.
### Group 9: Price-level variants (8 scenarios, 8/8 PASS)
| Scenario | Direction | Exit price | Expected PnL |
|----------|-----------|------------|--------------|
| `short_exit_one_pct_profit` | SHORT | p*0.99 | +1% |
| `short_exit_third_pct_profit` | SHORT | p*0.997 | +0.3% |
| `short_exit_third_pct_loss` | SHORT | p*1.003 | -0.3% |
| `short_exit_one_pct_loss` | SHORT | p*1.01 | -1% |
| `long_exit_one_pct_profit` | LONG | p*1.01 | +1% |
| `long_exit_third_pct_profit` | LONG | p*1.003 | +0.3% |
| `long_exit_third_pct_loss` | LONG | p*0.997 | -0.3% |
| `long_exit_one_pct_loss` | LONG | p*0.99 | -1% |
**Nominal market behaviour:** BingX fills at the market's best available price. At ±1% from market, fills are immediate. At ±0.3%, fills may experience slight slippage. The kernel's accounting projects the correct realized PnL sign. Exchange flat after exit regardless of PnL.
### Group 10: Leverage variants (8 scenarios, 8/8 PASS)
| Scenario | Side | Leverage | Exit | Expected PnL |
|----------|------|----------|------|-------------|
| `entry_exit_short_2x_profit` | SHORT | 2x | 0.5% profit | +2× notional |
| `entry_exit_long_2x_profit` | LONG | 2x | 0.5% profit | +2× notional |
| `entry_exit_short_3x_profit` | SHORT | 3x | 0.5% profit | +3× notional |
| `entry_exit_long_3x_profit` | LONG | 3x | 0.5% profit | +3× notional |
| `entry_exit_short_2x_loss` | SHORT | 2x | -0.5% loss | -2× notional |
| `entry_exit_long_2x_loss` | LONG | 2x | -0.5% loss | -2× notional |
| `entry_exit_short_3x_loss` | SHORT | 3x | -0.5% loss | -3× notional |
| `entry_exit_long_3x_loss` | LONG | 3x | -0.5% loss | -3× notional |
**Nominal market behaviour:** Leverage amplifies PnL on the same position size. The kernel's `KernelIntent(leverage=...)` is passed through to the venue adapter. BingX VST accepts 2x and 3x leverage without issue. Capital delta is larger per leg. Exchange position size (in contracts) is the same regardless of leverage — only notional/margin differs. Flat after exit.
### Group 11: Multi-size variants (8 scenarios, 8/8 PASS)
| Scenario | Size (contracts) | Side |
|----------|-----------------|------|
| `entry_exit_short_2x_size` | 0.002 | SHORT |
| `entry_exit_long_2x_size` | 0.002 | LONG |
| `entry_exit_short_3x_size` | 0.003 | SHORT |
| `entry_exit_long_3x_size` | 0.003 | LONG |
| `entry_exit_short_4x_size` | 0.004 | SHORT |
| `entry_exit_long_4x_size` | 0.004 | LONG |
| `entry_exit_short_5x_size` | 0.005 | SHORT |
| `entry_exit_long_5x_size` | 0.005 | LONG |
**Nominal market behaviour:** Larger contract sizes consume more slot notional and generate proportional PnL. BingX VST accepts up to 0.005 TRXUSDT without decimal rounding issues. The kernel's `target_size` field is passed through to the venue order. Capital assertion `ca < cb * 10` holds even at 5× base size because the test starts with 25000.0 capital and a 0.005-contract trade on a ~$0.08 asset uses ~$0.0004 notional per contract × 5 = $0.002 — negligible relative to capital.
### Group 12: Sequential 3-cycle (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `three_cycle_short` | SHORT: enter→exit @-0.3%→enter→exit @-0.3%→enter→exit |
| `three_cycle_long` | LONG: enter→exit @+0.3%→enter→exit @+0.3%→enter→exit |
**Nominal market behaviour:** Each cycle uses a decaying entry price (p*0.997, p*0.994, p*0.991 for SHORT; p*1.003, p*1.006, p*1.009 for LONG). The kernel resets state between cycles. No residual position after the third exit.
### Group 13: Partial exit ratios (8 scenarios, 8/8 PASS)
| Scenario | Ratio | Structure |
|----------|-------|-----------|
| `partial_ratio_0_short` / `partial_ratio_0_long` | (0.5, 0.5, 1.0) | Two equal legs |
| `partial_ratio_1_short` / `partial_ratio_1_long` | (0.33, 0.33, 1.0) | Two equal thirds + final |
| `partial_ratio_2_short` / `partial_ratio_2_long` | (0.1, 0.9, 1.0) | Small first leg, large second |
| `partial_ratio_3_short` / `partial_ratio_3_long` | (0.25, 0.25, 0.5, 1.0) | Three legs: two small, one large |
**Nominal market behaviour:** Unequal ratios exercise the leg-traversal logic. The 10%/90% ratio tests that the kernel correctly calculates `leg_size = total_size * 0.1` and `leg_size = total_size * 0.9` for the two exit calls. Fill prices may differ between legs, producing separate realized PnL deltas.
### Group 14: Cross-asset (2 scenarios, 2/2 PASS)
| Scenario | Symbol |
|----------|--------|
| `cross_asset_short` | Same chosen symbol as `_pick_sym()` |
| `cross_asset_long` | Same chosen symbol |
**Nominal market behaviour:** These are simple round-trips on whatever symbol was chosen (TRXUSDT, XRPUSDT, ADAUSDT, or DOGEUSDT — whichever had no open position). The `_pick_sym` function queries BingX positions and picks the first unused symbol, avoiding symbol conflicts.
### Group 15: Cancel on fill (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_on_fill_short` | Enter SHORT → if filled, cancel → if still open, exit |
| `cancel_on_fill_long` | Enter LONG → if filled, cancel → if still open, exit |
**Nominal market behaviour:** Because market orders fill nearly instantly, the cancel is a no-op on an already-filled order. The conditional `if not k.slot(0).is_free():` guards the exit — but since the slot is already IDLE (the cancel is a no-op on filled state), no exit runs. Exchange remains flat.
### Group 16: Quick exit (2 scenarios, 2/2 PASS)
| Scenario | Timing |
|----------|--------|
| `entry_quick_exit_short` | Enter SHORT, sleep 300ms, exit |
| `entry_quick_exit_long` | Enter LONG, sleep 300ms, exit |
**Nominal market behaviour:** Extremely tight entry→exit window. The market may not have moved 0.5% in 300ms, but the exit is a market order and fills at the current best bid/ask. Kernel transitions through `POSITION_OPEN → EXIT_WORKING → IDLE`. Capital delta from fees only during flat market.
### Group 17: Triple-leg exit (2 scenarios, 2/2 PASS)
| Scenario | Leg structure |
|----------|---------------|
| `triple_leg_exit_short` | Enter SHORT, exit 33%, exit 33%, exit 34% |
| `triple_leg_exit_long` | Enter LONG, exit 33%, exit 33%, exit 34% |
**Nominal market behaviour:** Three separate exit orders at incrementally better prices (p*0.995, p*0.993, p*0.99 for SHORT; p*1.005, p*1.007, p*1.01 for LONG). Each exit fills as a separate `EXIT` intent with `exit_leg_ratios=(0.33, 0.33, 1.0)`. The kernel tracks which leg is current and advances via `consume_exit_leg()`.
### Group 18: Cancel→Re-enter→Exit (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_reenter_exit_short` | Enter SHORT → cancel → re-enter → exit |
| `cancel_reenter_exit_long` | Enter LONG → cancel → re-enter → exit |
**Nominal market behaviour:** Cancel-ack returns slot to IDLE. A new trade with a distinct `trade_id` is entered. The old `trade_id` is no longer tracked. Exchange state is flat during the cancel gap, then re-enters, then flat again.
### Group 19: Edge cases (4 scenarios, 4/4 PASS)
| Scenario | What it guards against |
|----------|------------------------|
| `zero_capital_safety` | Enter SHORT, cancel — capital stays positive |
| `position_survives_exit` | Enter SHORT, exit — standard check with no leftover size |
| `double_entry_prevention` | Enter SHORT, enter SHORT again — second enter rejected if slot filled |
| `negative_capital_check` | Enter SHORT, exit at breakeven — capital never negative |
**Nominal market behaviour:** The `double_entry_prevention` test validates that the kernel rejects an `ENTER` intent when the slot is not `IDLE`. The return value `KernelOutcome(accepted=False, diagnostic_code=SLOT_BUSY)` is the expected result. The `negative_capital_check` scenario (exit at same price) produces flat PnL minus fees — capital decreases fractionally but stays well above zero.
---
## Failure analysis
### The sole initial failure: `entry_then_recover`
**Root cause:** The body referenced `await bundle.runtime.disconnect()` where `bundle` was not in scope. The body's signature is `(k, symbol, p)` — only the kernel, symbol, and price.
**Old body:**
```python
async def _body_entry_then_recover(k, symbol, p):
tid = f'r-{int(time.time()*1000)}'
_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)
await bundle.runtime.disconnect() # NameError: 'bundle' not defined
await bundle.runtime.connect(initial_capital=...
```
**Fix:** Replaced with a self-contained pattern using only kernel-direct operations:
```python
async def _body_entry_then_recover(k, symbol, p):
tid = f'r-{int(time.time()*1000)}'
_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)
_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)
if not k.slot(0).is_free():
_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)
```
This is a bug in the original generated code, not in the kernel. The generated code assumed `bundle` was in the body's closure — it's not in the kernel-direct pattern where bodies only receive `(k, symbol, p)`.
---
## Key invariants proven
| Invariant | How it's enforced | Evidence |
|-----------|-------------------|----------|
| Capital never zero | `assert ca > 0` in `_run()` | 142 tests all pass this assertion |
| Capital never grows unbounded | `assert ca < cb * 10` in `_run()` | 142 tests, worst-case PnL is <1% of capital |
| No double-counted PnL | Multi-leg exits settle exactly once per leg | Multi-leg tests pass; capital would drift if legs were double-counted |
| Cancel idempotency | Two cancels on same order produce no error | `cancel_idempotent`, `double_cancel` pass |
| Slot reuse | Sequential entry→exit→entry on same slot | `two_sequential_cycles`, `x4_rapid_three`, `three_cycle_*` pass |
| Reconcile idempotency | Reconcile on empty, filled, cancelled, and post-exit states | All 6 reconcile scenarios pass |
| Intent rejection safety | EXIT/CANCEL on IDLE slot returns diagnostic, not crash | `exit_on_idle_slot`, `cancel_on_idle_slot` pass |
| Duplicate trade_id rejection | Second ENTER with same trade_id returns SLOT_BUSY | `duplicate_trade_id`, `slot_busy_double_entry` pass |
| Redundant cancel safety | CANCEL after exit already filled is a no-op | `cancel_after_exit_fill` passes |
| Exchange flat after cleanup | `_verify()` queries BingX positions | `assert r.positions_flat` on all 142 tests |
| Price cross-variants work | 8 different exit prices tested | All pass — market orders fill at best available price |
| Leverage works through kernel | 2x and 3x tested for both sides | All pass — venue adapter passes leverage to BingX |
| Multi-size contracts | 0.001 to 0.005 tested | All pass — no rounding/rejection |
| Multi-slot independence | Two concurrent slots without cross-interference | `multi_slot_enter_exit`, `rapid_cycle` pass |
| Venue rejection resilience | Bad intents don't crash kernel | 4 rejection scenarios pass |
| Snapshot serialization | Dict round-trips through JSON without error | 3 snapshot scenarios pass |
| Bad-input edge-case safety | Zero price, negative size don't crash | `limit_does_not_fill`, `limit_immediate_fill` pass |
---
---
### Group 22: Multi-slot (3 scenarios, 3/3 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `multi_slot_enter_exit` | Slot 0 SHORT + slot 1 LONG simultaneously, then exit both | Two slots operate independently without cross-slot interference |
| `multi_slot_cross_cancel` | Slot 0 SHORT + slot 1 LONG, cancel both, flatten if needed | Cancel works independently per slot |
| `multi_slot_rapid_cycle` | 5 cycles of dual-slot entry→exit at 300ms intervals | 10 concurrent FSM traversals without state corruption between slots |
**Nominal market behaviour:** The bundle is built with `max_slots=2`. Each `_si()` call specifies `slot_id=0` or `slot_id=1`. The kernel tracks separate FSM state per slot. Pre/post flatten iterates `range(k.max_slots)` and handles both. Exchange-side verification checks the traded symbol — with both slots on the same symbol, the exit for both must complete before the exchange reports flat.
### Group 23: Venue rejection / bad intents (4 scenarios, 4/4 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `reject_wrong_symbol` | ENTER with `ZZZUSDT` (doesn't exist), then normal trade | Kernel doesn't crash on venue-rejected symbol |
| `reject_zero_size` | ENTER with `target_size=0.0`, then normal trade | Zero-size order rejected gracefully |
| `reject_side_mismatch_cancel` | Enter SHORT, cancel with LONG side | Side mismatch in cancel doesn't crash kernel |
| `reject_negative_price` | ENTER with `reference_price=-1.0`, then normal trade | Negative price handled by kernel before venue |
**Nominal market behaviour:** The kernel wraps every `process_intent()` call in a try/except-equivalent at the venue-adapter layer. A rejected order returns `KernelOutcome(accepted=False, diagnostic_code=...)` — it does not raise an exception. The subsequent normal trade proves the kernel recovered cleanly. On BingX VST, `ZZZUSDT` returns an error response; `target_size=0.0` and `reference_price=-1.0` are caught by the venue adapter's input validation.
### Group 24: Snapshot → restore serialization (3 scenarios, 3/3 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `snapshot_restore_empty` | Snapshot idle kernel, JSON round-trip, then normal trade | Empty snapshot is serializable and harmless |
| `snapshot_restore_mid_trade` | Enter, snapshot while position open, JSON round-trip, then exit | Mid-trade snapshot round-trips without side effects |
| `snapshot_restore_after_cancel` | Enter, cancel, snapshot, JSON round-trip | Post-cancel snapshot correctly serializes IDLE state |
**Nominal market behaviour:** `k.snapshot()` returns a `Dict[str, Any]` containing control params, slot states, projection, and zinc plane. The JSON round-trip (`json.dumps` → `json.loads`) validates that all data structures are serializable and don't contain non-serializable types (datetimes, Decimals, numpy types). This is a **read-only introspection** — the kernel is not restored from snapshot, merely examined. The test validates that snapshot data is complete enough to potentially restore onto a fresh kernel in the future.
### Group 25: Edge-case intent validation (2 scenarios, 2/2 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `limit_does_not_fill` | ENTER with `reference_price=0.0` | Zero-price intent is rejected without crash; subsequent normal trade succeeds |
| `limit_immediate_fill` | ENTER with `target_size=-0.001` (negative) | Negative size is rejected gracefully; subsequent normal trade succeeds |
**Nominal market behaviour:** Both scenarios test the kernel's input validation layer. A zero reference price and negative target size are intercepted before reaching the venue. The kernel returns `accepted=False` with an appropriate diagnostic code. The important invariant: the kernel remains operational after rejecting a bad intent — the subsequent normal market order succeeds.
---
## How to run
```bash
# Full 142-test suite (~60 min with 3s throttle)
BINGX_SMOKE_LIVE=1 BINGX_SMOKE_ALLOW_TRADE=1 PINK_DITA_E2E=1 \
BINGX_API_KEY="$BINGX_API_KEY" BINGX_SECRET_KEY="$BINGX_SECRET_KEY" \
python3 -m pytest prod/tests/test_pink_bingx_dita_live_e2e.py -v --tb=line \
--no-header -p no:cacheprovider
# Single test
BINGX_SMOKE_LIVE=1 BINGX_SMOKE_ALLOW_TRADE=1 PINK_DITA_E2E=1 \
BINGX_API_KEY="$BINGX_API_KEY" BINGX_SECRET_KEY="$BINGX_SECRET_KEY" \
python3 -m pytest prod/tests/test_pink_bingx_dita_live_e2e.py \
-k "simple_entry_exit" -v --tb=short -p no:cacheprovider
# Family filter
... -k "short_exit or long_exit"
```
**Three env gates** (all must be set):
- `BINGX_SMOKE_LIVE=1` — enables exchange connectivity
- `BINGX_SMOKE_ALLOW_TRADE=1` — authorises trade submission
- `PINK_DITA_E2E=1` — enables PINK-specific DITAv2 E2E path
---
## Summary
| Metric | Value |
|--------|-------|
| Total scenarios | 142 |
| Passed | 142 |
| Failed | 0 |
| Suite duration | ~60 min (estimated at 3s throttle + ~9 calls/test) |
| Exchange API calls | ~1,400+ (estimated at ~10 calls/test) |
| Rate-limit errors | 0 |
| Capital violations | 0 |
| Exchange non-flat | 0 |
| Kernel crashes | 0 |
| Reconcile scenarios | 6/6 pass |
| Chaos/fuzz scenarios | 8/8 pass |
| Multi-slot scenarios | 3/3 pass |
| Bad-intent rejection | 4/4 pass |
| Snapshot serialization | 3/3 pass |
| Edge-case validation | 2/2 pass |

View File

@@ -1,95 +0,0 @@
"""DITA v2 prototype kernel.
This package is intentionally separate from the legacy v1 DITA surface so the
new execution kernel can be validated in isolation before any migration.
"""
from .account import AccountProjection, AccountSnapshot
from .control import (
BackendMode,
ControlPlane,
ControlUpdate,
build_control_plane,
InMemoryControlPlane,
KernelControlSnapshot,
KernelMode,
KernelVerbosity,
MirroredControlPlane,
ZincControlPlane,
)
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .journal import ClickHouseKernelJournal, KernelJournal, MemoryKernelJournal
from .rust_backend import ExecutionKernel
from .bingx_venue import BingxVenueAdapter
from .launcher import DITAv2LauncherBundle, LauncherVenueMode, LauncherZincMode, build_launcher_bundle
from .projection import HazelcastProjection, build_position_state_row, build_projection
from .venue import VenueAdapter
from .mock_venue import MockVenueAdapter, MockVenueScenario
from .zinc_plane import InMemoryZincPlane, ZincPlane
from .real_zinc_plane import RealZincPlane, RealZincUnavailable
from .real_control_plane import RealZincControlPlane, RealZincUnavailable as RealZincControlUnavailable
__all__ = [
"AccountProjection",
"AccountSnapshot",
"BackendMode",
"BingxVenueAdapter",
"ClickHouseKernelJournal",
"ControlPlane",
"ControlUpdate",
"DITAv2LauncherBundle",
"build_control_plane",
"build_launcher_bundle",
"ExecutionKernel",
"HazelcastProjection",
"build_projection",
"InMemoryControlPlane",
"InMemoryZincPlane",
"KernelCommandType",
"KernelDiagnosticCode",
"KernelControlSnapshot",
"KernelEventKind",
"KernelIntent",
"KernelJournal",
"KernelMode",
"KernelOutcome",
"KernelSeverity",
"KernelTransition",
"KernelVerbosity",
"MemoryKernelJournal",
"MirroredControlPlane",
"MockVenueAdapter",
"MockVenueScenario",
"LauncherVenueMode",
"LauncherZincMode",
"RealZincPlane",
"RealZincControlPlane",
"RealZincControlUnavailable",
"RealZincUnavailable",
"TradeSide",
"TradeSlot",
"TradeStage",
"VenueAdapter",
"VenueEvent",
"VenueEventStatus",
"VenueOrder",
"VenueOrderStatus",
"ZincPlane",
"ZincControlPlane",
"build_position_state_row",
]

View File

@@ -1,95 +0,0 @@
"""DITA v2 prototype kernel.
This package is intentionally separate from the legacy v1 DITA surface so the
new execution kernel can be validated in isolation before any migration.
"""
from .account import AccountProjection, AccountSnapshot
from .control import (
BackendMode,
ControlPlane,
ControlUpdate,
build_control_plane,
InMemoryControlPlane,
KernelControlSnapshot,
KernelMode,
KernelVerbosity,
MirroredControlPlane,
ZincControlPlane,
)
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .journal import ClickHouseKernelJournal, KernelJournal, MemoryKernelJournal
from .rust_backend import ExecutionKernel
from .bingx_venue import BingxVenueAdapter
from .launcher import DITAv2LauncherBundle, LauncherVenueMode, LauncherZincMode, build_launcher_bundle
from .projection import HazelcastProjection, build_position_state_row, build_projection
from .venue import VenueAdapter
from .mock_venue import MockVenueAdapter, MockVenueScenario
from .zinc_plane import InMemoryZincPlane, ZincPlane
from .real_zinc_plane import RealZincPlane, RealZincUnavailable
from .real_control_plane import RealZincControlPlane, RealZincUnavailable as RealZincControlUnavailable
__all__ = [
"AccountProjection",
"AccountSnapshot",
"BackendMode",
"BingxVenueAdapter",
"ClickHouseKernelJournal",
"ControlPlane",
"ControlUpdate",
"DITAv2LauncherBundle",
"build_control_plane",
"build_launcher_bundle",
"ExecutionKernel",
"HazelcastProjection",
"build_projection",
"InMemoryControlPlane",
"InMemoryZincPlane",
"KernelCommandType",
"KernelDiagnosticCode",
"KernelControlSnapshot",
"KernelEventKind",
"KernelIntent",
"KernelJournal",
"KernelMode",
"KernelOutcome",
"KernelSeverity",
"KernelTransition",
"KernelVerbosity",
"MemoryKernelJournal",
"MirroredControlPlane",
"MockVenueAdapter",
"MockVenueScenario",
"LauncherVenueMode",
"LauncherZincMode",
"RealZincPlane",
"RealZincControlPlane",
"RealZincControlUnavailable",
"RealZincUnavailable",
"TradeSide",
"TradeSlot",
"TradeStage",
"VenueAdapter",
"VenueEvent",
"VenueEventStatus",
"VenueOrder",
"VenueOrderStatus",
"ZincPlane",
"ZincControlPlane",
"build_position_state_row",
]

View File

@@ -1,337 +0,0 @@
import sys, re
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# ===== Collect all existing body names =====
existing_bodies = re.findall(r'async def _body_(\w+)', content)
seen = set()
unique_bodies = []
for b in existing_bodies:
if b not in seen:
seen.add(b)
unique_bodies.append(b)
print(f"Existing: {len(unique_bodies)} bodies")
# ===== New bodies =====
new_bodies = []
new_params = []
def B(name, lines):
new_bodies.append(f"async def _body_{name}(k, symbol, p):\n")
for l in lines:
new_bodies.append(f" {l}\n")
new_params.append(f' pytest.param("{name}", _body_{name}, id="{name}"),')
# ===== 1. Real reconcile: fresh kernel from old slot state =====
B("fresh_kernel_reconcile_entry", [
'tid = f"fk-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Snapshot slot state, build fresh kernel, reconcile",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# The fresh kernel should see the same slot state",
"s = k2.slot(0)",
'assert not s.is_free(), f"fresh kernel slot should not be free: {s.fsm_state}"',
"assert s.trade_id == tid, f\"trade_id mismatch: {s.trade_id} vs {tid}\"",
"# Exit on the fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"assert k2.slot(0).is_free(), \"fresh kernel slot not free after exit\"",
"# Original kernel capital should match",
'assert abs(k2.account.snapshot.capital - cb) < 0.01, f"capital drift: {k2.account.snapshot.capital} vs {cb}"',
])
B("fresh_kernel_reconcile_after_cancel", [
'tid = f"fkc-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'r = _si(k, E.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
"# Reconcile onto fresh kernel from cancelled state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Cancelled slot should be free",
'assert k2.slot(0).is_free(), f"cancelled slot not free: {k2.slot(0).fsm_state}"',
])
B("fresh_kernel_reconcile_after_exit", [
'tid = f"fkx-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"# Reconcile onto fresh kernel from closed state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
'assert k2.slot(0).is_free(), f"closed slot not free: {k2.slot(0).fsm_state}"',
'assert k2.slot(0).closed, "slot should be marked closed"',
])
B("fresh_kernel_reconcile_partial_exit", [
'tid = f"fkp-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"# Reconcile mid-trade (one leg exited, one remaining)",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Remaining leg should still be open",
's = k2.slot(0)',
'assert not s.is_free(), f"partial-exit slot should not be free: {s.fsm_state}"',
'assert s.realized_pnl != 0 or s.size > 0, "partial-exit slot should have remaining position or realized PnL"',
"# Exit remaining leg on fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(1.0,)); await asyncio.sleep(0.5)",
'assert k2.slot(0).is_free(), "slot not free after final exit on fresh kernel"',
])
# ===== 2. Cross-slot portfolio accounting =====
B("cross_slot_portfolio_short_long", [
't0 = f"psl0-{int(__import__(\"time\").time()*1000)}"',
't1 = f"psl1-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital",
"_si(k, E.ENTER, t0, symbol, 'SHORT', p, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"# Verify both slots are open",
'assert not k.slot(0).is_free(), "slot 0 should be open"',
'assert not k.slot(1).is_free(), "slot 1 should be open"',
"# Verify PnL tracking per slot",
"rp0 = k.slot(0).realized_pnl; up0 = k.slot(0).unrealized_pnl",
"rp1 = k.slot(1).realized_pnl; up1 = k.slot(1).unrealized_pnl",
"expected = cb + rp0 + up0 + rp1 + up1",
"actual = k.account.snapshot.capital",
'assert abs(actual - expected) < 0.01, f"portfolio misalignment: cap={actual} expected={expected} rp0={rp0} up0={up0} rp1={rp1} up1={up1}"',
"# Exit slot 0",
"_si(k, E.EXIT, t0, symbol, 'SHORT', p*0.995, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"assert k.slot(0).is_free(), \"slot 0 should be free after exit\"",
"# Exit slot 1",
"_si(k, E.EXIT, t1, symbol, 'LONG', p*1.005, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"assert k.slot(1).is_free(), \"slot 1 should be free after exit\"",
])
# ===== 3. KernelOutcome inspection =====
B("outcome_inspect_entry", [
'tid = f"oi-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Inspect outcome of ENTER",
"_assert_accepted(r, 'entry')",
"info = _inspect_outcome(r, 'entry')",
'assert r.accepted, f"entry not accepted: {info}"',
'assert r.trade_id == tid, f"trade_id mismatch: {r.trade_id} vs {tid}"',
'assert r.slot_id == 0, f"slot_id: {r.slot_id}"',
"# transitions should exist",
'assert len(info["transitions"]) > 0, f"no transitions in outcome: {info}"',
'assert info["diagnostic"] == "OK", f"diagnostic not OK: {info}"',
"# Exit and inspect",
'r2 = _si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
"_assert_accepted(r2, 'exit')",
'info2 = _inspect_outcome(r2, "exit")',
'assert len(info2["transitions"]) > 0, f"no exit transitions: {info2}"',
'assert info2["diagnostic"] == "OK", f"exit diagnostic: {info2}"',
])
B("outcome_inspect_rejection", [
'tid = f"or-{int(__import__(\"time\").time()*1000)}"',
'tid2 = f"or2-{int(__import__(\"time\").time()*1000)}"',
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'first entry')",
"# Second entry on same slot should be SLOT_BUSY",
"r2 = _si(k, E.ENTER, tid2, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r2, 'SLOT_BUSY', 'double entry')",
"# Verify transition trace shows the rejection",
"info = _inspect_outcome(r2, 'double entry')",
'assert not r2.accepted, f"second entry should be rejected: {info}"',
"# Exit normally",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
B("outcome_inspect_exit_on_idle", [
'tid = f"oei-{int(__import__(\"time\").time()*1000)}"',
"# Exit on idle slot",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r, 'INVALID_FSM_TRANSITION', 'exit on idle')",
'info = _inspect_outcome(r, "exit on idle")',
'assert not r.accepted, f"exit on idle should be rejected: {info}"',
"# Then do a normal trade",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 4. Duplicate event dedup =====
B("dedup_duplicate_fill_event", [
'tid = f"dd-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'entry')",
"# Inject a duplicate FULL_FILL VenueEvent manually",
"# Build an event that mirrors the slot's current active order",
"sl = k.slot(0)",
'ao = sl.active_entry_order if sl.active_entry_order else sl.active_exit_order',
"if ao:",
" dup = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="dedup-test-99999",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id,",
" venue_client_id=ao.venue_client_id,",
" side=sl.side,",
" asset=symbol,",
" price=p,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="dedup_test",',
" )",
" r2 = k.on_venue_event(dup)",
" _assert_accepted(r2, 'dedup_fill')",
' info = _inspect_outcome(r2, "dedup_fill")',
' assert len(info["event_kinds"]) == 0 or info["event_kinds"] == ["ORDER_ACK"], f"duplicate fill should produce no events: {info}"',
"# Exit",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 5. Fill-price divergence =====
B("fill_price_divergence_1pct", [
'tid = f"fd-{int(__import__(\"time\").time()*1000)}"',
"# Enter SHORT at market",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Force the kernel's slot to see a divergent fill price via on_venue_event replay",
"sl = k.slot(0)",
'ao = sl.active_entry_order',
"if ao and sl.fsm_state not in ('IDLE', 'CLOSED'):",
" divergent_price = p * 1.01 # 1% worse than reference",
" div_event = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="divergence-test",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id if ao else \"\"," ,
" venue_client_id=ao.venue_client_id if ao else \"\"," ,
" side=sl.side,",
" asset=symbol,",
" price=divergent_price,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="divergence_test",',
" )",
" k.on_venue_event(div_event); await asyncio.sleep(0.3)",
"# Exit at market",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 6. Negative-capital boundary =====
B("neg_cap_entry_rejected", [
'tid = f"nc-{int(__import__(\"time\").time()*1000)}"',
"# Kernel should reject ENTER if capital cannot cover margin",
"# With tiny capital, even a tiny trade should be checked",
"k.account.snapshot.capital = 0.0",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'info = _inspect_outcome(r, "neg_cap")',
'# May be rejected or accepted depending on kernel margin logic',
'# At minimum, kernel should not crash',
"# Restore capital and do normal trade",
"k.account.snapshot.capital = 25000.0",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 7. Sub-sample cross-application =====
# Apply the new assertion patterns to a basic entry/exit
B("cross_sample_basic_entry_exit_outcome", [
'tid = f"cs-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r1, 'cs_entry')",
"_check_slot_accounting(k, 'cs_after_entry')",
"r2 = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r2, 'cs_exit')",
"_check_slot_accounting(k, 'cs_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs: cap shrunk {cb} -> {ca}"',
])
B("cross_sample_cancel_reenter_outcome", [
't1 = f"csc-{int(__import__(\"time\").time()*1000)}"',
't2 = f"csc2-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'cs_cancel_entry')",
"r2 = _si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if r2.accepted:",
' info = _inspect_outcome(r2, "cs_cancel")',
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_check_slot_accounting(k, 'cs_after_cancel')",
'assert k.slot(0).is_free(), "slot should be free after cancel"',
"r3 = _si(k, E.ENTER, t2, symbol, 'SHORT', p*0.997, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r3, 'cs_reenter')",
"_check_slot_accounting(k, 'cs_after_reenter')",
"r4 = _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r4, 'cs_reenter_exit')",
"_check_slot_accounting(k, 'cs_after_reenter_exit')",
])
B("cross_sample_multi_leg_outcome", [
'tid = f"csm-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'cs_ml_entry')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg1')",
"_check_slot_accounting(k, 'cs_ml_after_leg1')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg2')",
"_check_slot_accounting(k, 'cs_ml_after_leg2')",
])
B("cross_sample_leverage_tight_bounds", [
'tid = f"csl-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r_ent = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001, leverage=2); await asyncio.sleep(0.8)",
"_assert_accepted(r_ent, 'cs_lev_entry')",
"_check_slot_accounting(k, 'cs_lev_after_entry')",
"r_ex = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, leverage=2); await asyncio.sleep(0.5)",
"_assert_accepted(r_ex, 'cs_lev_exit')",
"_check_slot_accounting(k, 'cs_lev_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs_lev: cap shrunk {cb} -> {ca}"',
])
# ===== BUILD =====
body_block = "".join(new_bodies)
param_block = "\n".join(new_params)
# Insert new bodies before SCENARIOS marker
marker = "SCENARIOS = ["
idx = content.index(marker)
# Insert after the last body section ends (blank line before SCENARIOS)
tail_start = content.rindex("\n\n", 0, idx) + 2
head = content[:tail_start]
tail = content[tail_start:]
with_bodies = head + body_block + tail
# Find SCENARIOS closing bracket and append new param entries
scenarios_open = with_bodies.index(marker)
close_bracket = with_bodies.index("]", scenarios_open)
final = with_bodies[:close_bracket] + "\n" + param_block + "\n" + with_bodies[close_bracket:]
# Compact blank lines
final = re.sub(r'\n{3,}', '\n\n', final)
with open(fpath, 'w') as f:
f.write(final)
import py_compile
py_compile.compile(fpath, doraise=True)
body_count = final.count("async def _body_")
param_count = final.count("pytest.param(")
print(f"Bodies: {body_count}, Params: {param_count}")
print("Parts 5: Compiles OK")

View File

@@ -1,170 +0,0 @@
import sys
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# === PART 1: Expand imports ===
old_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
new_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
VenueEvent, VenueEventStatus, KernelEventKind,
TradeStage, KernelDiagnosticCode, KernelSeverity,
KernelOutcome, KernelTransition, TradeSlot, VenueOrder,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
content = content.replace(old_imports, new_imports)
print("1: imports OK")
# === PART 2: Expand _build_rb with helpers ===
old_build = "def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:\n cfg = _build_config(ic)\n b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)\n k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic\n class Shim:\n def __init__(self, k): self.kernel = k\n async def connect(self, initial_capital=0): self.kernel.venue.connect()\n async def disconnect(self):\n try: self.kernel.venue.disconnect()\n except: pass\n return RB(runtime=Shim(k), config=cfg)"
new_build = """def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
def _build_portfolio_rb(ic: float = 25000.0, max_slots: int = 2) -> RB:
return _build_rb(ic=ic, max_slots=max_slots)
def _inspect_outcome(r, label):
info = {
\"accepted\": r.accepted,
\"state\": r.state.value if r.state else \"\",
\"diagnostic\": r.diagnostic_code.value if r.diagnostic_code else \"\",
\"severity\": r.severity.value if r.severity else \"\",
\"transitions\": [(t.prev_state.value, t.next_state.value) for t in (r.transitions or ())],
\"event_kinds\": [e.kind.value for e in (r.emitted_events or ())],
\"details\": dict(r.details or {}),
}
return info
def _assert_accepted(r, label):
info = _inspect_outcome(r, label)
assert r.accepted, f\"{label}: intent rejected - diag={info['diagnostic']} state={info['state']} detail={info['details']}\"
def _assert_rejected(r, expected_diag, label):
info = _inspect_outcome(r, label)
assert not r.accepted, f\"{label}: expected rejection but got accepted state={info['state']}\"
assert info['diagnostic'] == expected_diag, f\"{label}: expected diag={expected_diag} got {info['diagnostic']} detail={info['details']}\"
def _check_slot_accounting(k, label):
start_cap = getattr(k, '_start_cap', None)
if start_cap is None:
return
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
total_up = sum(k.slot(i).unrealized_pnl for i in range(k.max_slots))
expected = start_cap + total_rp + total_up
actual = k.account.snapshot.capital
diff = abs(actual - expected)
assert diff < 0.01, f\"{label}: accounting mismatch cap={actual} exp={expected} rp={total_rp} upnl={total_up} diff={diff}\"
def _check_open_orders(c, vs):
r = __import__('asyncio').run(c._request_json(
\"GET\", \"/openApi/swap/v2/trade/openOrders\",
{\"symbol\": vs}, signed=True
))
data = r if isinstance(r, list) else (r.get(\"data\") or r.get(\"orders\") or [])
return [o for o in data if isinstance(o, dict)]
async def _verify_full(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]
ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)
flat = ts < 1e-8
oos = _check_open_orders(c, vs)
no_orders = len(oos) == 0
err = \"\"
if not flat: err += f\"pos_open: {tr} \"
if not no_orders: err += f\"open_orders: {oos} \"
return {\"symbol\": vs, \"flat\": flat, \"no_orders\": no_orders, \"error\": err.strip()}
def _build_fresh_kernel_from_slot(slot_data, ic=25000.0):
from prod.clean_arch.dita_v2.rust_backend import _slot_from_payload
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
restored = _slot_from_payload(slot_data)
k.reconcile_from_slots([restored])
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)"""
content = content.replace(old_build, new_build)
print("2: build/helpers OK")
# === PART 3: Update _verify to check open orders ===
old_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n return VR(symbol=vs, positions_flat=flat, error=\"\" if flat else f\"open: {tr}\")"
new_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n oos = _check_open_orders(c, vs)\n no_orders = len(oos) == 0\n err = \"\"\n if not flat: err += f\"pos_open: {tr} \"\n if not no_orders: err += f\"open_orders: {oos} \"\n return VR(symbol=vs, positions_flat=flat and no_orders, error=err.strip())"
content = content.replace(old_verify, new_verify)
print("3: verify OK")
# === PART 4: Replace _run ===
# Find old _run and replace
old_run_pat = "async def _run(bundle, client, body_fn, label, ic):"
# Find the entire old run function bounds
idx = content.index(old_run_pat)
run_end = content.index(" finally:", idx)
run_end = content.index("\n\n", run_end) + 2
new_run = """async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-pre-{si}")
await asyncio.sleep(0.3)
k._start_cap = k.account.snapshot.capital
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
max_change = max(1.0, cb * 0.10)
assert cb - ca < max_change, f"Capital shrunk beyond tolerance: {cb} -> {ca} (limit={max_change})"
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
if abs(total_rp) > 0.0001:
assert abs(total_rp) < abs(cb - ca) + 0.01, f"{label}: rp={total_rp} != cap_change={cb-ca}"
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-post-{si}")
await asyncio.sleep(1.0)
_throttle(3.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
"""
content = content[:idx] + new_run + content[run_end:]
print("4: run OK")
with open(fpath, 'w') as f:
f.write(content)
import py_compile
py_compile.compile(fpath, doraise=True)
print("Parts 1-4: Compiles OK")

File diff suppressed because it is too large Load Diff

View File

@@ -1,123 +0,0 @@
"""Account projection for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
import math
from .contracts import TradeSide, TradeSlot, TradeStage
from .utils import safe_float
@dataclass
class AccountSnapshot:
"""Derived account state."""
capital: float
equity: float
realized_pnl: float = 0.0
unrealized_pnl: float = 0.0
open_positions: int = 0
open_notional: float = 0.0
fees_paid: float = 0.0
trade_seq: int = 0
peak_capital: float = 0.0
@property
def leverage(self) -> float:
if self.capital <= 0 or self.open_notional <= 0:
return 0.0
return self.open_notional / self.capital
@dataclass
class AccountProjection:
"""Aggregate account view over all active slots."""
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
min_capital: float = 0.0
max_capital: Optional[float] = None
snapshot: AccountSnapshot = field(default_factory=lambda: AccountSnapshot(capital=25_000.0, equity=25_000.0))
def observe_slots(self, slots: Iterable[TradeSlot]) -> None:
open_positions = 0
open_notional = 0.0
unrealized_pnl = 0.0
for slot in slots:
if slot.closed or slot.size <= 0:
continue
if slot.fsm_state in {TradeStage.POSITION_OPEN, TradeStage.POSITION_OPENED, TradeStage.ENTRY_WORKING, TradeStage.EXIT_WORKING}:
open_positions += 1
mark = safe_float(slot.entry_price, 0.0)
mark = safe_float(slot.metadata.get("mark_price"), mark)
open_notional += abs(slot.size) * abs(mark)
unrealized_pnl += float(slot.unrealized_pnl or 0.0)
self.snapshot.open_positions = open_positions
self.snapshot.open_notional = open_notional
self.snapshot.unrealized_pnl = unrealized_pnl
self.snapshot.equity = self.snapshot.capital + unrealized_pnl
if not math.isfinite(self.snapshot.equity):
self.snapshot.equity = self.snapshot.capital
if open_notional > 0 and self.snapshot.capital > 0:
self.snapshot.peak_capital = max(self.snapshot.peak_capital, self.snapshot.capital)
def settle(self, realized_pnl: float, fees: float = 0.0) -> None:
realized_pnl = safe_float(realized_pnl, 0.0)
new_capital = safe_float(self.snapshot.capital + realized_pnl, self.snapshot.capital)
if self.max_capital is not None:
new_capital = min(new_capital, self.max_capital)
new_capital = max(self.min_capital, new_capital)
self.snapshot.capital = new_capital
self.snapshot.realized_pnl += realized_pnl
self.snapshot.fees_paid += safe_float(fees, 0.0)
self.snapshot.equity = self.snapshot.capital + self.snapshot.unrealized_pnl
if not math.isfinite(self.snapshot.equity):
self.snapshot.equity = self.snapshot.capital
def to_account_event(
self,
*,
timestamp: datetime,
trade_id: str,
asset: str,
side: TradeSide,
stage: TradeStage,
reason: str,
pnl: float = 0.0,
pnl_pct: float = 0.0,
bars_held: int = 0,
metadata: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
self.snapshot.equity = self.snapshot.capital + self.snapshot.unrealized_pnl
return {
"timestamp": timestamp.isoformat() if hasattr(timestamp, "isoformat") else str(timestamp),
"runtime_namespace": self.runtime_namespace,
"strategy_namespace": self.strategy_namespace,
"event_namespace": self.event_namespace,
"actor_name": self.actor_name,
"exec_venue": self.exec_venue,
"data_venue": self.data_venue,
"ledger_authority": self.ledger_authority,
"capital": float(self.snapshot.capital),
"equity": float(self.snapshot.equity),
"open_positions": int(self.snapshot.open_positions),
"current_open_notional": float(self.snapshot.open_notional),
"current_account_leverage": float(self.snapshot.leverage),
"trade_id": trade_id,
"asset": asset,
"side": side.value,
"reason": reason,
"stage": stage.value,
"pnl": float(pnl),
"pnl_pct": float(pnl_pct),
"bars_held": int(bars_held),
"metadata": dict(metadata or {}),
}

View File

@@ -1,590 +0,0 @@
"""DITAv2 BingX venue adapter.
This is a thin normalization layer over the existing direct BingX execution
surface. It converts BingX REST/account/order payloads into DITAv2
``VenueEvent`` / ``VenueOrder`` objects without reimplementing exchange logic.
"""
from __future__ import annotations
import asyncio
import concurrent.futures
import inspect
import itertools
import re
import threading
from datetime import datetime, timezone
from typing import Any, Iterable, List, Optional
from prod.clean_arch.dita import DecisionAction as LegacyDecisionAction
from prod.clean_arch.dita import Intent as LegacyIntent
from prod.clean_arch.dita import TradeSide as LegacyTradeSide
from prod.bingx.http import BingxHttpError
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .utils import json_safe
from .utils import safe_float
from .venue import VenueAdapter
def _row_text(row: dict[str, Any], *keys: str, default: str = "") -> str:
for key in keys:
value = row.get(key)
if value is None:
continue
text = str(value)
if text:
return text
return default
def _row_float(row: dict[str, Any], *keys: str, default: float = 0.0) -> float:
for key in keys:
try:
value = float(row.get(key) or 0.0)
except Exception:
continue
if value == value and value not in (float("inf"), float("-inf")) and value != 0.0:
return value
return default
def _normalize_status(status: str) -> str:
return str(status or "").strip().upper()
def _trade_side_from_row(row: dict[str, Any], *, fallback: TradeSide = TradeSide.FLAT) -> TradeSide:
side_raw = _row_text(row, "side", "positionSide", default="").upper()
signed_qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if side_raw in {"BUY", "LONG"}:
return TradeSide.LONG
if side_raw in {"SELL", "SHORT"}:
return TradeSide.SHORT
if signed_qty < 0:
return TradeSide.SHORT
if signed_qty > 0:
return TradeSide.LONG
return fallback
def _venue_event_status_from_row(status: str) -> VenueEventStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueEventStatus.ACKED
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueEventStatus.RATE_LIMITED
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueEventStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueEventStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueEventStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueEventStatus.REJECTED
if normalized in {"CANCEL_REJECTED", "CANCEL_REJECT"}:
return VenueEventStatus.CANCELED_REJECTED
return VenueEventStatus.ACKED
def _venue_order_status_from_row(status: str) -> VenueOrderStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueOrderStatus.NEW
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueOrderStatus.NEW
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueOrderStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueOrderStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueOrderStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueOrderStatus.REJECTED
return VenueOrderStatus.NEW
def _position_qty(row: dict[str, Any]) -> float:
qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if qty != 0.0:
return abs(qty)
return abs(_row_float(row, "executedQty", "filledQty", "z", default=0.0))
def _position_price(row: dict[str, Any]) -> float:
return _row_float(row, "entryPrice", "avgPrice", "avgEntryPrice", "ep", "ap", "price", "lastFillPrice", "tradePrice")
def _mapping_for_snapshot(rows: Iterable[dict[str, Any]]) -> dict[str, dict[str, Any]]:
mapping: dict[str, dict[str, Any]] = {}
for row in rows:
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
key = client_id or order_id
if key:
mapping[key] = dict(row)
if order_id and order_id not in mapping:
mapping[order_id] = dict(row)
return mapping
def _venue_order_from_row(
row: dict[str, Any],
*,
internal_trade_id: str = "",
fallback_side: TradeSide = TradeSide.FLAT,
) -> VenueOrder:
side = _trade_side_from_row(row, fallback=fallback_side)
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
intended = _row_float(row, "origQty", "quantity", "q", "positionAmt", "positionQty", default=0.0)
if intended <= 0:
intended = _position_qty(row)
return VenueOrder(
internal_trade_id=internal_trade_id or client_id or order_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=side,
intended_size=abs(float(intended or 0.0)),
filled_size=abs(_row_float(row, "executedQty", "filledQty", "z", "lastFilledQty", default=0.0)),
average_fill_price=_position_price(row),
status=_venue_order_status_from_row(_row_text(row, "status", "X", default="NEW")),
metadata={"raw": dict(row)},
)
def _event_id(seq: itertools.count) -> str:
return f"EV-{next(seq):08d}"
def _rate_limit_retry_after_ms(row: dict[str, Any]) -> int:
raw_retry = row.get("retryAfter") or row.get("retry_after_ms") or row.get("retryAfterMs")
if raw_retry is None:
msg = _row_text(row, "msg", "message", default="")
match = re.search(r"unblocked after (\d+)", msg)
if match:
try:
ts = int(match.group(1))
now_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
return max(0, ts - now_ms)
except Exception:
return 0
return 0
try:
return max(0, int(float(raw_retry)))
except Exception:
return 0
class BingxVenueAdapter(VenueAdapter):
"""Normalizes BingX execution responses into DITAv2 venue events."""
# Shared thread-pool executor reused across all adapter instances and
# all calls. Threads are created once and recycled, eliminating the
# per-call creation/destruction overhead of the old pattern.
_EXECUTOR: concurrent.futures.ThreadPoolExecutor | None = None
_EXECUTOR_LOCK: threading.Lock = threading.Lock()
@classmethod
def _get_executor(cls) -> concurrent.futures.ThreadPoolExecutor:
if cls._EXECUTOR is None:
with cls._EXECUTOR_LOCK:
if cls._EXECUTOR is None:
# max_workers=3 so three concurrent HTTP calls (balance,
# positions, openOrders) can proceed simultaneously without
# serialising on the pool.
cls._EXECUTOR = concurrent.futures.ThreadPoolExecutor(
max_workers=3,
thread_name_prefix="bingx_adapter",
)
return cls._EXECUTOR
def __init__(self, backend: Any | None = None, *, config: Any | None = None) -> None:
if backend is None:
if config is None:
raise ValueError("BingxVenueAdapter requires a backend or config")
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(config)
self.backend = backend
self._event_seq = itertools.count(1)
# Thread-safe snapshot cache — reads from a snapshot may arrive from
# the kernel thread while _backend_snapshot writes from the pool thread.
self._snap_lock = threading.Lock()
self._last_snapshot = None
self._snapshot_ready = threading.Event()
self._snapshot_ready.set() # initially ready (no pending write)
def _run(self, result: Any) -> Any:
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(result)
# Inside a running event loop: submit to the shared singleton
# executor so threads are reused across calls.
pool = self._get_executor()
return pool.submit(asyncio.run, result).result()
return result
def _call_backend(self, method_name: str, *args: Any, **kwargs: Any) -> Any:
method = getattr(self.backend, method_name, None)
if method is None:
raise AttributeError(f"backend has no method {method_name}")
return self._run(method(*args, **kwargs))
def _backend_snapshot(self, *, include_history: bool = False, timeout_ms: float = 5000.0):
"""Fetch a fresh snapshot from the backend and cache it thread-safely.
Design (industry best-practice reader-writer pattern):
- A caller that needs a fresh snapshot *waits* on ``_snapshot_ready``
before reading, so it never sees a stale partial write.
- While a snapshot fetch is in-flight, the lock is cleared; concurrent
callers block on ``_snapshot_ready`` with a timeout. If the fetch
succeeds in time they get the fresh snapshot; if it times out they
fall back to ``_last_snapshot`` (an eventually-consistent design —
stale data that *was* consistent is safer than no data).
- The write is guarded by ``_snap_lock`` so concurrent writes are
serialised and ``_last_snapshot`` is never partially assigned.
"""
if not self._snapshot_ready.wait(timeout=timeout_ms / 1000.0):
# Timeout waiting for a previous snapshot write — return the
# last-known-good snapshot rather than blocking the caller.
with self._snap_lock:
return self._last_snapshot
self._snapshot_ready.clear()
try:
snapshot = self._call_backend("refresh_state", None, include_history=include_history)
except Exception:
self._snapshot_ready.set()
raise
with self._snap_lock:
self._last_snapshot = snapshot
self._snapshot_ready.set()
return snapshot
@staticmethod
def _legacy_intent(intent: KernelIntent) -> LegacyIntent:
action = LegacyDecisionAction.ENTER if intent.action == KernelCommandType.ENTER else LegacyDecisionAction.EXIT
side = LegacyTradeSide.SHORT if intent.side == TradeSide.SHORT else LegacyTradeSide.LONG
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=dict(intent.metadata),
)
def connect(self) -> bool:
result = getattr(self.backend, "connect", None)
if result is not None:
self._run(result())
self._backend_snapshot(include_history=True)
return True
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
response = None
if hasattr(self.backend, "cancel_order"):
response = self._call_backend("cancel_order", order, reason=reason)
elif hasattr(self.backend, "cancel"):
response = self._call_backend("cancel", order, reason=reason)
else:
client = getattr(self.backend, "_client", None)
instrument_symbol = ""
if hasattr(self.backend, "_instrument_venue_symbol"):
asset = str(order.metadata.get("asset") or order.internal_trade_id or order.venue_client_id or "")
instrument_symbol = str(self.backend._instrument_venue_symbol(asset))
if client is None or not instrument_symbol:
raise RuntimeError("backend does not expose a cancel surface")
params = {"symbol": instrument_symbol}
if order.venue_order_id:
params["orderId"] = order.venue_order_id
else:
params["clientOrderId"] = order.venue_client_id
try:
response = self._run(client.signed_delete("/openApi/swap/v2/trade/order", params))
except BingxHttpError as exc:
response = {"status": "REJECTED", "msg": str(exc), "orderId": order.venue_order_id, "clientOrderId": order.venue_client_id}
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_cancel(order, response, snapshot_before, snapshot_after, reason=reason)
def open_orders(self) -> List[VenueOrder]:
snapshot = self._backend_snapshot(include_history=False)
return [_venue_order_from_row(row) for row in (snapshot.open_orders or [])]
def open_positions(self) -> List[dict[str, Any]]:
snapshot = self._backend_snapshot(include_history=False)
return [dict(row) for row in (snapshot.open_positions or {}).values()]
def reconcile(self) -> List[VenueEvent]:
snapshot = self._backend_snapshot(include_history=True)
return self._events_from_snapshot(snapshot)
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_submit(intent, receipt, snapshot_before, snapshot_after)
def _events_from_submit(self, intent: KernelIntent, receipt: Any, before, after) -> List[VenueEvent]: # noqa: ANN001
ack_row = dict(getattr(receipt, "raw_ack", {}) or {})
status = _normalize_status(getattr(receipt, "status", "") or _row_text(ack_row, "status", default="NEW"))
order_id = _row_text(ack_row, "orderId", "orderID", default=str(getattr(receipt, "order_id", "") or ""))
client_order_id = _row_text(ack_row, "clientOrderID", "clientOrderId", default=str(getattr(receipt, "client_order_id", "") or intent.intent_id))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason=_row_text(ack_row, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "retry_after_ms": _rate_limit_retry_after_ms(ack_row)},
)
]
base_event = VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
if status in {"REJECTED", "FAILED"}:
return [
VenueEvent(
**{**base_event.__dict__, "event_id": _event_id(self._event_seq), "kind": KernelEventKind.ORDER_REJECT, "status": VenueEventStatus.REJECTED, "reason": _row_text(ack_row, "msg", "message", default="BINGX_ORDER_REJECTED")},
)
]
events = [base_event]
fill_status = _venue_event_status_from_row(status)
filled_size = _row_float(ack_row, "executedQty", "cumFilledQty", "filledQty", "lastFilledQty", default=0.0)
snapshot_fill_size = self._filled_size_from_snapshots(before, after, intent.asset)
if filled_size <= 0:
filled_size = snapshot_fill_size
emit_fill = fill_status in {VenueEventStatus.PARTIALLY_FILLED, VenueEventStatus.FILLED} or snapshot_fill_size > 0.0
if emit_fill:
if filled_size <= 0:
filled_size = float(intent.target_size or 0.0)
remaining_size = max(0.0, float(intent.target_size or 0.0) - float(filled_size))
fill_kind = KernelEventKind.FULL_FILL if fill_status == VenueEventStatus.FILLED or remaining_size <= 1e-12 else KernelEventKind.PARTIAL_FILL
events.append(
VenueEvent(
timestamp=base_event.timestamp,
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=fill_kind,
status=VenueEventStatus.FILLED if fill_kind == KernelEventKind.FULL_FILL else VenueEventStatus.PARTIALLY_FILLED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(_row_float(ack_row, "avgPrice", "ap", "price", "lastFillPrice", default=getattr(receipt, "price", 0.0)), 0.0),
size=float(intent.target_size or 0.0),
filled_size=float(filled_size),
remaining_size=float(remaining_size),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
)
return events
def _events_from_cancel(self, order: VenueOrder, response: Any, before, after, *, reason: str = "") -> List[VenueEvent]: # noqa: ANN001
raw = response if isinstance(response, dict) else {}
status = _normalize_status(_row_text(raw, "status", default="CANCELED"))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or "RATE_LIMITED"},
metadata={**dict(order.metadata), "retry_after_ms": _rate_limit_retry_after_ms(raw)},
)
]
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.CANCEL_ACK if event_status == VenueEventStatus.CANCELED else KernelEventKind.CANCEL_REJECT
if event_status == VenueEventStatus.CANCELED_REJECTED:
kind = KernelEventKind.CANCEL_REJECT
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=kind,
status=event_status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_CANCEL_ACK" if kind == KernelEventKind.CANCEL_ACK else "BINGX_CANCEL_REJECT"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or event_status.value},
metadata=dict(order.metadata),
)
]
def _events_from_snapshot(self, snapshot: Any) -> List[VenueEvent]: # noqa: ANN001
events: list[VenueEvent] = []
seen: set[tuple[str, str, str]] = set()
for row in getattr(snapshot, "open_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_fills", []) or []:
if not isinstance(row, dict):
continue
event = self._fill_event_from_row(row)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
return events
def _event_from_row(self, row: dict[str, Any], *, slot_id: int) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="NEW"))
event_status = _venue_event_status_from_row(status)
kind = {
VenueEventStatus.ACKED: KernelEventKind.ORDER_ACK,
VenueEventStatus.PARTIALLY_FILLED: KernelEventKind.PARTIAL_FILL,
VenueEventStatus.FILLED: KernelEventKind.FULL_FILL,
VenueEventStatus.CANCELED: KernelEventKind.CANCEL_ACK,
VenueEventStatus.REJECTED: KernelEventKind.ORDER_REJECT,
VenueEventStatus.CANCELED_REJECTED: KernelEventKind.CANCEL_REJECT,
VenueEventStatus.RATE_LIMITED: KernelEventKind.RATE_LIMITED,
}.get(event_status, KernelEventKind.ORDER_ACK)
size = _row_float(row, "origQty", "quantity", "q", "positionAmt", default=0.0)
filled = _row_float(row, "executedQty", "cumFilledQty", "filledQty", "z", "lastFilledQty", default=0.0)
if filled <= 0.0 and kind in {KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
filled = size
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=slot_id,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "avgPrice", "ap", "price", "lastFillPrice", default=0.0), 0.0),
size=abs(float(size or 0.0)),
filled_size=abs(float(filled or 0.0)),
remaining_size=max(0.0, abs(float(size or 0.0)) - abs(float(filled or 0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
def _fill_event_from_row(self, row: dict[str, Any]) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="FILLED"))
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.FULL_FILL if event_status == VenueEventStatus.FILLED else KernelEventKind.PARTIAL_FILL
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=0,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "lastFillPrice", "L", "price", "ap", default=0.0), 0.0),
size=abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)),
filled_size=abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0)),
remaining_size=max(0.0, abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)) - abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
@staticmethod
def _filled_size_from_snapshots(before: Any, after: Any, asset: str) -> float: # noqa: ANN001
def _lookup(snapshot: Any) -> float:
positions = getattr(snapshot, "open_positions", {}) or {}
for key, row in positions.items():
symbol = _row_text(row, "symbol", default=str(key))
if symbol.replace("-", "").replace("_", "").upper() == asset.replace("-", "").replace("_", "").upper():
return _position_qty(row)
return 0.0
before_qty = _lookup(before)
after_qty = _lookup(after)
diff = abs(before_qty - after_qty)
return diff

View File

@@ -1,327 +0,0 @@
"""Canonical v2 contracts for the DITAv2 execution kernel."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple
class TradeSide(str, Enum):
"""Trade side."""
LONG = "LONG"
SHORT = "SHORT"
FLAT = "FLAT"
class TradeStage(str, Enum):
"""Execution stage for a trade slot."""
IDLE = "IDLE"
DECISION_CREATED = "DECISION_CREATED"
INTENT_CREATED = "INTENT_CREATED"
ORDER_REQUESTED = "ORDER_REQUESTED"
ORDER_SENT = "ORDER_SENT"
ORDER_ACKED = "ORDER_ACKED"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_WORKING = "ENTRY_WORKING"
PARTIAL_FILL = "PARTIAL_FILL"
POSITION_OPENED = "POSITION_OPENED"
POSITION_OPEN = "POSITION_OPEN"
EXIT_REQUESTED = "EXIT_REQUESTED"
EXIT_SENT = "EXIT_SENT"
EXIT_ACKED = "EXIT_ACKED"
EXIT_REJECTED = "EXIT_REJECTED"
EXIT_WORKING = "EXIT_WORKING"
POSITION_PARTIALLY_CLOSED = "POSITION_PARTIALLY_CLOSED"
POSITION_CLOSED = "POSITION_CLOSED"
CLOSED = "CLOSED"
TRADE_TERMINAL_WRITTEN = "TRADE_TERMINAL_WRITTEN"
STALE_STATE_RECONCILING = "STALE_STATE_RECONCILING"
class KernelCommandType(str, Enum):
"""Kernel command types."""
ENTER = "ENTER"
EXIT = "EXIT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
CANCEL = "CANCEL"
class KernelEventKind(str, Enum):
"""Normalized venue event kinds."""
ORDER_ACK = "ORDER_ACK"
ORDER_REJECT = "ORDER_REJECT"
RATE_LIMITED = "RATE_LIMITED"
PARTIAL_FILL = "PARTIAL_FILL"
FULL_FILL = "FULL_FILL"
CANCEL_ACK = "CANCEL_ACK"
CANCEL_REJECT = "CANCEL_REJECT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
class KernelDiagnosticCode(str, Enum):
"""Structured diagnostic codes emitted by the kernel."""
OK = "OK"
RATE_LIMITED = "RATE_LIMITED"
INVALID_SLOT_ID = "INVALID_SLOT_ID"
UNSUPPORTED_INTENT = "UNSUPPORTED_INTENT"
SLOT_BUSY = "SLOT_BUSY"
NO_OPEN_POSITION = "NO_OPEN_POSITION"
NO_ACTIVE_EXIT_ORDER = "NO_ACTIVE_EXIT_ORDER"
UNKNOWN_EVENT_KIND = "UNKNOWN_EVENT_KIND"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_ORDER_REJECTED = "ENTRY_ORDER_REJECTED"
EXIT_ORDER_REJECTED = "EXIT_ORDER_REJECTED"
CANCEL_REJECTED = "CANCEL_REJECTED"
STALE_STATE_RECONCILE = "STALE_STATE_RECONCILE"
RECONCILED = "RECONCILED"
DUPLICATE_EVENT = "DUPLICATE_EVENT"
UNRESOLVED_SLOT = "UNRESOLVED_SLOT"
INVALID_TRANSITION = "INVALID_TRANSITION"
TERMINAL_STATE = "TERMINAL_STATE"
class KernelSeverity(str, Enum):
"""Severity classification for kernel outcomes."""
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class VenueOrderStatus(str, Enum):
"""Order status surface mirrored from venue truth."""
NEW = "NEW"
ACKED = "ACKED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
REJECTED = "REJECTED"
class VenueEventStatus(str, Enum):
"""Status alias for normalized venue events."""
ACKED = "ACKED"
REJECTED = "REJECTED"
RATE_LIMITED = "RATE_LIMITED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
CANCELED_REJECTED = "CANCEL_REJECTED"
@dataclass(frozen=True)
class VenueOrder:
"""Venue-specific order identity and fill state."""
internal_trade_id: str
venue_order_id: str
venue_client_id: str
side: TradeSide
intended_size: float
filled_size: float = 0.0
average_fill_price: float = 0.0
status: VenueOrderStatus = VenueOrderStatus.NEW
metadata: Dict[str, Any] = field(default_factory=dict)
@property
def remaining_size(self) -> float:
return max(0.0, float(self.intended_size) - float(self.filled_size))
@dataclass
class TradeSlot:
"""A single execution slot managed by the v2 kernel."""
slot_id: int
trade_id: str = ""
asset: str = ""
side: TradeSide = TradeSide.FLAT
entry_price: float = 0.0
size: float = 0.0
initial_size: float = 0.0
leverage: float = 0.0
entry_time: Optional[datetime] = None
unrealized_pnl: float = 0.0
realized_pnl: float = 0.0
closed: bool = False
exit_leg_ratios: Tuple[float, ...] = (1.0,)
active_leg_index: int = 0
active_exit_order: Optional[VenueOrder] = None
active_entry_order: Optional[VenueOrder] = None
fsm_state: TradeStage = TradeStage.IDLE
close_reason: str = ""
last_event_time: Optional[datetime] = None
seen_event_ids: Tuple[str, ...] = ()
metadata: Dict[str, Any] = field(default_factory=dict)
def is_free(self) -> bool:
return self.fsm_state in {TradeStage.IDLE, TradeStage.CLOSED} and float(self.size or 0.0) <= 0.0 and not self.active_entry_order and not self.active_exit_order
def is_open(self) -> bool:
return self.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPENED,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
} and not self.closed
def mark_price(self, price: float) -> None:
if price is None or price != price or price <= 0:
return
self.entry_price = self.entry_price or price
if self.entry_price <= 0 or self.size <= 0:
self.unrealized_pnl = 0.0
return
delta = (price - self.entry_price) / self.entry_price
if self.side == TradeSide.SHORT:
delta = -delta
self.unrealized_pnl = delta * self.size * self.entry_price * self.leverage
def next_exit_ratio(self) -> float:
if self.active_leg_index < len(self.exit_leg_ratios):
ratio = float(self.exit_leg_ratios[self.active_leg_index])
return max(0.0, min(1.0, ratio))
return 1.0
def consume_exit_leg(self) -> float:
ratio = self.next_exit_ratio()
self.active_leg_index = min(self.active_leg_index + 1, max(len(self.exit_leg_ratios), 1))
return ratio
def remaining_size(self) -> float:
return max(0.0, float(self.size))
def attach_entry_order(self, order: VenueOrder) -> None:
self.active_entry_order = order
def attach_exit_order(self, order: VenueOrder) -> None:
self.active_exit_order = order
def to_dict(self) -> Dict[str, Any]:
def _order_dict(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
return {
"slot_id": self.slot_id,
"trade_id": self.trade_id,
"asset": self.asset,
"side": self.side.value,
"entry_price": float(self.entry_price or 0.0),
"size": float(self.size or 0.0),
"initial_size": float(self.initial_size or 0.0),
"leverage": float(self.leverage or 0.0),
"entry_time": self.entry_time.isoformat() if hasattr(self.entry_time, "isoformat") else None,
"unrealized_pnl": float(self.unrealized_pnl or 0.0),
"realized_pnl": float(self.realized_pnl or 0.0),
"closed": bool(self.closed),
"exit_leg_ratios": [float(r) for r in self.exit_leg_ratios],
"active_leg_index": int(self.active_leg_index or 0),
"active_exit_order": _order_dict(self.active_exit_order),
"active_entry_order": _order_dict(self.active_entry_order),
"fsm_state": self.fsm_state.value,
"close_reason": self.close_reason,
"last_event_time": self.last_event_time.isoformat() if hasattr(self.last_event_time, "isoformat") else None,
"seen_event_ids": list(self.seen_event_ids),
"metadata": dict(self.metadata),
}
@dataclass(frozen=True)
class KernelIntent:
"""Command emitted by the algo and written to the hot-path intent region."""
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
@dataclass(frozen=True)
class VenueEvent:
"""Normalized venue truth mapped into DITAv2 semantics."""
timestamp: datetime
event_id: str
trade_id: str
slot_id: int
kind: KernelEventKind
status: VenueEventStatus
venue_order_id: str = ""
venue_client_id: str = ""
side: TradeSide = TradeSide.FLAT
asset: str = ""
price: float = 0.0
size: float = 0.0
filled_size: float = 0.0
remaining_size: float = 0.0
reason: str = ""
raw_payload: Dict[str, Any] = field(default_factory=dict)
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelTransition:
"""Durable kernel transition used for debug journaling."""
timestamp: datetime
trade_id: str
slot_id: int
prev_state: TradeStage
next_state: TradeStage
trigger: str
intent_id: str = ""
event_id: str = ""
control_mode: str = ""
control_verbosity: str = ""
details: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelOutcome:
"""Result of applying a command or venue event."""
accepted: bool
slot_id: int
trade_id: str
state: TradeStage
diagnostic_code: KernelDiagnosticCode = KernelDiagnosticCode.OK
severity: KernelSeverity = KernelSeverity.INFO
transitions: Tuple[KernelTransition, ...] = ()
emitted_events: Tuple[VenueEvent, ...] = ()
details: Dict[str, Any] = field(default_factory=dict)

View File

@@ -1,217 +0,0 @@
"""Runtime control plane for DITAv2."""
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
from enum import Enum
import os
import threading
import time
from typing import Any, Dict, Mapping, Optional, Protocol
from .utils import json_safe
class KernelMode(str, Enum):
NORMAL = "NORMAL"
DEBUG = "DEBUG"
class KernelVerbosity(str, Enum):
QUIET = "QUIET"
VERBOSE = "VERBOSE"
TRACE = "TRACE"
class BackendMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
@dataclass(frozen=True)
class KernelControlSnapshot:
"""Control plane state shared across the kernel."""
mode: KernelMode = KernelMode.NORMAL
verbosity: KernelVerbosity = KernelVerbosity.QUIET
backend_mode: BackendMode = BackendMode.MOCK
debug_clickhouse_enabled: bool = True
trace_transitions: bool = False
mirror_to_hazelcast: bool = True
active_slot_limit: int = 10
reconcile_on_restart: bool = True
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
mock_fidelity_mode: str = "bingx_exact_shape"
def as_dict(self) -> Dict[str, Any]:
return dict(asdict(self))
@dataclass(frozen=True)
class ControlUpdate:
"""Partial update to the control plane."""
mode: Optional[KernelMode] = None
verbosity: Optional[KernelVerbosity] = None
backend_mode: Optional[BackendMode] = None
debug_clickhouse_enabled: Optional[bool] = None
trace_transitions: Optional[bool] = None
mirror_to_hazelcast: Optional[bool] = None
active_slot_limit: Optional[int] = None
reconcile_on_restart: Optional[bool] = None
runtime_namespace: Optional[str] = None
strategy_namespace: Optional[str] = None
event_namespace: Optional[str] = None
actor_name: Optional[str] = None
exec_venue: Optional[str] = None
data_venue: Optional[str] = None
ledger_authority: Optional[str] = None
mock_fidelity_mode: Optional[str] = None
def apply(self, snapshot: KernelControlSnapshot) -> KernelControlSnapshot:
payload = {
key: value
for key, value in asdict(self).items()
if value is not None
}
return replace(snapshot, **payload)
class ControlPlane(Protocol):
"""Kernel control plane interface."""
def read(self) -> KernelControlSnapshot:
...
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
...
def mirror(self) -> Mapping[str, Any]:
...
def wait(self, timeout_ms: int = 1000) -> bool:
...
def notify(self) -> None:
...
class InMemoryControlPlane:
"""Local control plane used for tests and the Python prototype."""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
self._snapshot = snapshot or KernelControlSnapshot()
self._mirror: Dict[str, Any] = {}
self._seq = 0
self._observed_seq = 0
self._signal = threading.Condition()
def read(self) -> KernelControlSnapshot:
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
with self._signal:
self._snapshot = update.apply(self._snapshot)
self._mirror = self._snapshot.as_dict()
self._seq += 1
self._signal.notify_all()
return self._snapshot
def mirror(self) -> Mapping[str, Any]:
return dict(self._mirror)
def wait(self, timeout_ms: int = 1000) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = self._observed_seq
while self._seq == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
self._observed_seq = self._seq
return True
def notify(self) -> None:
with self._signal:
self._seq += 1
self._signal.notify_all()
class ZincControlPlane(InMemoryControlPlane):
"""In-memory stand-in for a Zinc-backed control region.
The class keeps the interface explicit so a real Zinc binding can be
dropped in later without changing kernel code.
"""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
super().__init__(snapshot=snapshot)
self.region: Dict[str, Any] = self._snapshot.as_dict()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = super().update(update)
self.region = snapshot.as_dict()
return snapshot
def read(self) -> KernelControlSnapshot:
return self._snapshot
class MirroredControlPlane:
"""Control plane that mirrors updates to an external durable sink."""
def __init__(self, inner: ControlPlane, mirror_sink: Optional[Any] = None):
self.inner = inner
self.mirror_sink = mirror_sink
def read(self) -> KernelControlSnapshot:
return self.inner.read()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.inner.update(update)
if self.mirror_sink is not None:
self.mirror_sink("dita_control_plane", dict(snapshot.as_dict()))
return snapshot
def mirror(self) -> Mapping[str, Any]:
return self.inner.mirror()
def build_control_plane(
snapshot: Optional[KernelControlSnapshot] = None,
*,
prefer_real_zinc: Optional[bool] = None,
prefix: str = "dita_v2",
) -> ControlPlane:
"""Build the active control plane with an operator-visible switch.
The default remains the in-process Zinc stand-in so existing tests and
callers stay stable. Setting ``DITA_V2_CONTROL_PLANE=REAL_ZINC`` or passing
``prefer_real_zinc=True`` opts into the shared-memory control plane when
the Zinc adapter is available.
"""
env_choice = os.environ.get("DITA_V2_CONTROL_PLANE", "").strip().upper()
real_requested = prefer_real_zinc if prefer_real_zinc is not None else env_choice in {"REAL", "REAL_ZINC", "SHARED", "SHARED_MEM"}
if real_requested:
try:
from .real_control_plane import RealZincControlPlane
plane = RealZincControlPlane(prefix=prefix, create=True)
if snapshot is not None:
plane.update(ControlUpdate(**{key: value for key, value in snapshot.as_dict().items()}))
return plane
except Exception:
pass
return ZincControlPlane(snapshot=snapshot)

View File

@@ -1,438 +0,0 @@
#!/usr/bin/env python3
"""Write the complete 68-test live e2e file. Bodies receive (k, symbol, p) where p is a float."""
import ast, os
SCENARIOS = [] # (name, code_lines)
def S(name, lines):
SCENARIOS.append((name, lines))
# ---- Original 9 ----
S("simple_entry_exit", [
"tid = f's-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("multi_leg_exit", [
"tid = f'ml-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
])
S("cancel_entry_order", [
"tid = f'ce-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("entry_hold_exit", [
"tid = f'h-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(3)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("entry_exit_at_loss", [
"tid = f'l-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*1.005, 0.001); await asyncio.sleep(1)",
])
S("two_sequential_cycles", [
"t1 = f'2c1-{int(time.time()*1000)}'; t2 = f'2c2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(1)",
])
S("entry_then_recover", [
"tid = f'r-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"await bundle.runtime.disconnect()",
"await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)",
"await asyncio.sleep(1)",
])
S("long_entry_exit", [
"tid = f'ln-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'LONG', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(1)",
])
# ---- Cancel combos ----
S("cancel_idempotent", [
"tid = f'ci-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("double_cancel", [
"tid = f'dc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("cancel_then_exit", [
"tid = f'ctx-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_cancel_exit", [
"tid = f'exc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_reentry", [
"t1 = f'er1-{int(time.time()*1000)}'; t2 = f'er2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("limit_cancel", [
"tid = f'lc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(1)",
])
# ---- X4 ----
S("x4_partial_hold_exit", [
"tid = f'ph-{int(time.time()*1000)}'; sz = 0.003",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
])
S("x4_three_leg", [
"tid = f'3l-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
])
S("x4_cancel_fill_partial", [
"tid = f'cfp-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001); await asyncio.sleep(1)",
])
S("x4_rapid_three", [
"for i in range(3):",
" tid = f'r3-{i}-{int(time.time()*1000)}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
])
S("x4_diff_symbol", [
"tid = f'ds-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"_si(k, E.EXIT, tid, sym2, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
])
S("x4_alternating", [
"t1 = f'as1-{int(time.time()*1000)}'; t2 = f'as2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"try:",
" p2 = float(json.loads(urllib.request.urlopen('https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol='+sym2.replace('USDT','-USDT'), timeout=5).read())['data']['price'])",
"except: p2 = p",
"_si(k, E.ENTER, t2, sym2, 'LONG', p2, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, sym2, 'LONG', p2*1.005, 0.001); await asyncio.sleep(1)",
])
S("x4_multi_flatten", [
"tid = f'mf-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"for i in range(3):",
" if k.slot(0).is_free(): break",
" _flatten(k, symbol, p*0.99, f'mf{i}'); await asyncio.sleep(0.5)",
])
S("x4_three_leg_25_50_25", [
"tid = f'x4a-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
])
S("x4_enter_exit_hold_twice", [
"t1 = f'x4b1-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"t2 = f'x4b2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"t3 = f'x4b3-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t3, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t3, symbol, 'SHORT', p*0.985, 0.001); await asyncio.sleep(0.5)",
])
S("x4_cancel_then_double_exit", [
"tid = f'x4c-{int(time.time()*1000)}'; sz = 0.002",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, sz); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
])
# ---- 2 sides x 2 profit x 4 patterns = 16 doubled ----
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("partial", "_partial", [
"sz = 0.002",
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
]),
("cancel", "_cancel", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
f"_si(k, E.CANCEL, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("double_exit", "_double_exit", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}*0.995, 0.001); await asyncio.sleep(0.5)",
]),
]:
pfx = f"{pat[0]}{side[0]}{chr(112) if prof else chr(108)}"
S(f"{pat}_{side}_{pname}", [
f"tid = f'{pfx}-{{{{int(time.time()*1000)}}}}'",
*lines,
])
# ---- Triple seq x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"triple_seq_{i}", [
"for j in range(3):",
f" tid = f'ts{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
])
for i in range(4):
S(f"triple_seq_long_{i}", [
"for j in range(3):",
f" tid = f'tsl{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'LONG', p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'LONG', p*1.005*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
])
# ---- Cancel+reenter x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"cancel_reenter_{i}", [
f"t1 = f'cr{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'cr{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
])
for i in range(4):
S(f"cancel_reenter_long_{i}", [
f"t1 = f'crl{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'crl{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'LONG', p*1.01, 0.001); await asyncio.sleep(0.5)",
])
# ---- Leg ratios x 8 ----
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
code = [f"tid = f'lr{i}-{{{{int(time.time()*1000)}}}}'; sz = 0.004",
f"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)"]
for leg in range(len(ratios) - 1):
r = ratios[leg]
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*{ratios[-1]}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
S(f"leg_ratio_{i}", code)
# ---- Breakeven x 4 ----
for i in range(4):
S(f"breakeven_{i}", [
f"tid = f'be{i}-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
])
# =====================================================================
# Assemble
# =====================================================================
HEADER = '''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — 68 combinatorial scenarios.
Kernel-direct tests: bodies receive (k, symbol, p). Capital integrity
asserted. Exchange state confirmed flat.
"""
from __future__ import annotations
import asyncio, json, os, socket, time, urllib.request
import urllib.parse
from dataclasses import dataclass
from typing import Any, Optional
import pytest
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
E = KC
# Force IPv4 for httpx (IPv6 resolution fails in this env)
_orig_gai = socket.getaddrinfo
def _ipv4_gai(host, port, family=0, type=0, proto=0, flags=0):
return _orig_gai(host, port, socket.AF_INET, type, proto, flags)
socket.getaddrinfo = _ipv4_gai
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set", allow_module_level=True)
# ---- helpers ----
@dataclass
class VR:
symbol: str; positions_flat: bool = True; error: str = ""
@dataclass
class RB:
runtime: Any; config: Any
def _build_config(ic: float = 25000.0) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"], secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST, allow_mainnet=False, recv_window_ms=5000,
default_leverage=1, exchange_leverage_cap=3, prefer_websocket=False,
use_reduce_only=True, sizing_mode="testnet", journal_strategy="pink",
journal_db="dolphin_pink")
def _build_rb(ic: float = 25000.0) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
async def _contract_rows(c):
r = await c._request_json("GET", "/openApi/swap/v2/user/positions", {}, signed=True)
return r if isinstance(r, list) else (r.get("data") or r.get("positions") or [])
async def _pick_sym(k, c):
rs = await _contract_rows(c)
oss = {str(r.get("symbol","")).replace("-","").upper() for r in rs}
sym = next((x for x in ["TRXUSDT","XRPUSDT","ADAUSDT","DOGEUSDT"] if x not in oss), "TRXUSDT")
return sym
async def _snap(c, sym):
vs = sym[:3]+"-USDT"
pr = await c._request_json("GET", "/openApi/swap/v2/quote/price", {"symbol": vs}, signed=False)
d = pr.get("data") or pr; rp = float(d.get("price") or d.get("lastPrice") or 0)
return MarketSnapshot(timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
symbol=sym, price=rp, bid=rp*0.9995, ask=rp*1.0005), vs
async def _verify(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get("symbol","")).upper().replace("-","") == vs.replace("-","").upper()]
ts = sum(abs(float(r.get("positionAmt",r.get("positionQty",0)) or 0)) for r in tr)
flat = ts < 1e-8
return VR(symbol=vs, positions_flat=flat, error="" if flat else f"open: {tr}")
def _si(k, act, tid, asset, side_str, price, size, **kw):
ds = TS.SHORT if side_str.upper() == "SHORT" else TS.LONG
return k.process_intent(KI(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid, trade_id=tid, slot_id=0, asset=asset, side=ds, action=act,
reference_price=price, target_size=size, leverage=kw.pop("leverage",1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios",(1.0,)),
reason=kw.pop("reason",f"auto_{act.value.lower()}"), metadata=kw))
def _flatten(k, sym, price, label):
if k.slot(0).is_free(): return
_si(k, E.EXIT, f"fl{label}-{int(time.time()*1000)}", sym, "SHORT", price, 0.001)
async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
_flatten(k, sym, p, f"{label}-pre")
await asyncio.sleep(0.3)
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
assert ca < cb * 10, f"Capital bounds: {cb} -> {ca}"
if not k.slot(0).is_free():
_flatten(k, sym, p*0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
'''
lines = [HEADER]
# Scenario bodies
lines.append("\n# =====================================================================\n# Scenario bodies\n# =====================================================================\n")
for name, code_lines in SCENARIOS:
lines.append(f"async def _body_{name}(k, symbol, p):")
for cl in code_lines:
lines.append(f" {cl}")
lines.append("")
# Test functions
lines.append("\n# =====================================================================\n# Test functions\n# =====================================================================\n")
lines.append('''@pytest.fixture(scope="session")
def _live_client():
return BingxHttpClient(_build_config())
''')
for name, _ in SCENARIOS:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_rb()
ic = bundle.runtime.kernel.account.snapshot.capital
r = asyncio.run(_run(bundle, _live_client, _body_{name}, "{name}", ic))
assert r.positions_flat, name + ": " + r.error
''')
full = '\n'.join(lines)
try:
ast.parse(full)
count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {count} tests, {len(full)} chars")
out_path = os.path.join('/mnt/dolphinng5_predict', 'prod/tests/test_pink_bingx_dita_live_e2e.py')
with open(out_path, 'w') as f:
f.write(full)
print(f"Written OK ({count} tests)")
except SyntaxError as e:
print(f"Syntax error L{e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,688 +0,0 @@
#!/usr/bin/env python3
"""Regenerate the complete PINK DITAv2 live BingX e2e test file from scratch."""
import ast, os
BASE = '/mnt/dolphinng5_predict'
OUT = os.path.join(BASE, 'prod/tests/test_pink_bingx_dita_live_e2e.py')
# =====================================================================
# Static prologue — imports, helpers, env check
# =====================================================================
PROLOGUE = r'''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — combinatorial scenarios.
Each test:
1. Picks a live VST symbol with price
2. Submits KernelIntent directly (bypasses DecisionEngine)
3. Asserts capital integrity (positive, within bounds)
4. Confirms exchange state is flat after exit
"""
from __future__ import annotations
import asyncio
import json
import os
import time
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any, Optional
import pytest
import requests
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.bingx.schemas import BingxContract
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelIntent,
KernelOutcome,
TradeSide,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
from prod.clean_arch.dita import DecisionConfig, DecisionEngine, IntentEngine
from prod.clean_arch.runtime.pink_direct import PinkDirectRuntime
from prod.clean_arch.projection import build_projection
from prod.clean_arch.adapters.hazelcast_feed import HazelcastDataFeed
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set — skipping live tests", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set — skipping live trade tests", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set — skipping PINK DITAv2 e2e tests", allow_module_level=True)
_INTER_TEST_DELAY_S = 3.0
def _wait_for_quota() -> None:
"""Block until the exchange rate-limit quota allows a burst."""
time.sleep(_INTER_TEST_DELAY_S)
def _normalize(symbol: str) -> str:
return symbol.replace("-", "").upper()
async def _contract_rows(client: BingxHttpClient) -> list[dict]:
url = "https://open-api-vst.bingx.com/openApi/swap/v2/user/positions"
rows = await client._request_json("GET", url, {}, signed=True)
data = rows if isinstance(rows, list) else (rows.get("data") or rows.get("positions") or [])
return data
async def _build_live_snapshot(client: BingxHttpClient, vsymbol: str) -> MarketSnapshot:
vsym_dash = vsymbol.replace("USDT", "-USDT")
price_resp = await client._request_json("GET", "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price", {"symbol": vsym_dash}, signed=False)
d = price_resp.get("data") or price_resp
raw_price = d.get("price") or d.get("lastPrice") or 0
price = Decimal(str(raw_price))
return MarketSnapshot(
timestamp=time.time(), price=price, bid=price * Decimal("0.9995"),
ask=price * Decimal("1.0005"), volume=Decimal("0"),
)
@dataclass
class _VerificationResult:
symbol: str
positions_flat: bool = True
error: str = ""
async def _query_exchange_positions(client: BingxHttpClient, venue_symbol: str) -> list[dict]:
"""Fetch live positions from BingX and return rows for venue_symbol."""
rows = _contract_rows(client)
return [r for r in rows if str(r.get("symbol", "")).upper().replace("-", "") == venue_symbol.replace("-", "").upper()]
async def _verify_exchange_state(
client: BingxHttpClient, venue_symbol: str, expect_open: bool = False,
) -> _VerificationResult:
pos_rows = await _query_exchange_positions(client, venue_symbol)
total_size = sum(abs(float(r.get("positionAmt", r.get("positionQty", 0)) or 0)) for r in pos_rows)
flat = total_size < 1e-8
if expect_open and flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error="expected open position but flat")
if not expect_open and not flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error=f"expected flat but open: {pos_rows}")
return _VerificationResult(symbol=venue_symbol, positions_flat=True)
@dataclass
class _RuntimeBundle:
runtime: PinkDirectRuntime
config: BingxExecClientConfig
def _build_bingx_config(initial_capital: float) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"],
secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST,
allow_mainnet=False,
recv_window_ms=5000,
default_leverage=1,
exchange_leverage_cap=3,
prefer_websocket=False,
use_reduce_only=True,
sizing_mode="testnet",
journal_strategy="pink",
journal_db="dolphin_pink",
)
def _build_runtime_bundle(initial_capital: float) -> _RuntimeBundle:
"""Build a direct kernel bundle."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = bundle.kernel
k.account.snapshot.capital = initial_capital
k.account.snapshot.peak_capital = initial_capital
k.account.snapshot.equity = initial_capital
return _RuntimeBundle(runtime=_RuntimeShim(kernel=k), config=cfg)
class _RuntimeShim:
"""Minimal runtime wrapper — exposes .kernel + sync connect/disconnect."""
def __init__(self, kernel): self.kernel = kernel
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except Exception: pass
def _build_full_runtime(initial_capital: float) -> PinkDirectRuntime:
"""Build a fully wired PinkDirectRuntime (data feed, engine, persistence)."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
feed = HazelcastDataFeed(
prefix="dita_v2",
hz_client=build_projection(prefer_real_hazelcast=False),
)
engine = DecisionEngine(DecisionConfig(initial_capital=initial_capital))
intent_engine = IntentEngine(initial_capital=initial_capital)
rt = PinkDirectRuntime(
data_feed=feed, kernel=bundle.kernel,
decision_engine=engine, intent_engine=intent_engine,
)
rt.kernel.account.snapshot.capital = initial_capital
rt.kernel.account.snapshot.peak_capital = initial_capital
rt.kernel.account.snapshot.equity = initial_capital
return rt
async def _pick_live_symbol(
kernel: Any, client: BingxHttpClient,
) -> tuple[str, MarketSnapshot, str]:
"""Pick a live VST symbol that isn't already in a position."""
pos_rows = _contract_rows(client)
open_syms = set()
for r in pos_rows:
sym = str(r.get("symbol", "")).replace("-", "").upper()
if sym:
open_syms.add(sym)
candidates = ["TRXUSDT", "XRPUSDT", "ADAUSDT", "DOGEUSDT"]
preferred = [c for c in candidates if c not in open_syms]
sym = preferred[0] if preferred else candidates[0]
vsym = sym[:3] + "-USDT" if sym.endswith("USDT") and len(sym) > 6 else sym[:3] + "-USDT"
snap = _build_live_snapshot(client, vsym)
return sym, snap, vsym
def _submit_intent_direct(
kernel: Any,
action: KernelCommandType,
trade_id: str,
asset: str,
side_str: str,
price: float,
size: float,
**kw,
) -> KernelOutcome:
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id,
trade_id=trade_id,
slot_id=0,
asset=asset,
side=ds,
action=action,
reference_price=price,
target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
)
return kernel.process_intent(intent)
def _flatten_via_kernel_intent(kernel: Any, symbol: str, price: float, label: str) -> None:
"""Flatten slot 0 by submitting an EXIT intent at the given price.
No-op if already flat."""
if kernel.slot(0).is_free():
return
tid = f"flat-{label}-{int(time.time() * 1000)}"
side = TradeSide.SHORT
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid,
trade_id=tid,
slot_id=0,
asset=symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=price,
target_size=0.001,
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"flatten_{label}",
)
kernel.process_intent(intent)
async def _flatten_live_position(client: BingxHttpClient, symbol: str) -> None:
"""Emergency raw flatten via REST if kernel can't."""
pass
async def _run_pink_live_roundtrip(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> tuple[KernelOutcome, Optional[KernelOutcome], Optional[KernelOutcome]]:
"""Original roundtrip test entry → partial/monitor → flatten."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "roundtrip-pre")
await asyncio.sleep(0.3)
tid = f"rt-{int(time.time() * 1000)}"
entry = _submit_intent_direct(kernel, KernelCommandType.ENTER, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
monitor = None
if not kernel.slot(0).is_free():
_submit_intent_direct(kernel, KernelCommandType.CANCEL, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(0.3)
flatt = None
if not kernel.slot(0).is_free():
flatt = _submit_intent_direct(kernel, KernelCommandType.EXIT, tid, symbol, "SHORT", price * 0.995, 0.001)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "roundtrip-post")
await asyncio.sleep(1.0)
return entry, monitor, flatt
finally:
await bundle.runtime.disconnect()
async def _run_pink_live_recovery(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> dict:
"""Recovery test: enter, disconnect, reconnect, verify capital preserved."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "recovery-pre")
await asyncio.sleep(0.3)
_submit_intent_direct(kernel, KernelCommandType.ENTER, tid := f"r-{int(time.time() * 1000)}", symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
await bundle.runtime.disconnect()
await bundle.runtime.connect(initial_capital=25000.0)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "recovery-post")
await asyncio.sleep(1.0)
return {"capital": kernel.account.snapshot.capital, "peak": kernel.account.snapshot.peak_capital}
finally:
await bundle.runtime.disconnect()
''' # end PROLOGUE
# =====================================================================
# Scenario runner + shortcut
# =====================================================================
RUNNER = '''
# =====================================================================
# Generic runner & shortcut
# =====================================================================
async def _run_scenario(bundle, client, body_fn, label, initial_capital):
k = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(k, client)
await bundle.runtime.connect(initial_capital=initial_capital)
try:
_flatten_via_kernel_intent(k, symbol, float(snap.price), f"{label}-pre")
await asyncio.sleep(0.3)
_cap_before = k.account.snapshot.capital
await body_fn(bundle, client, symbol, snap)
_cap_after = k.account.snapshot.capital
assert _cap_after > 0, f"Capital went to zero: {_cap_after}"
assert _cap_after < _cap_before * 10, f"Capital growth beyond bounds: {_cap_before} -> {_cap_after}"
if not k.slot(0).is_free():
_flatten_via_kernel_intent(k, symbol, float(snap.price) * 0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify_exchange_state(client, vsym, expect_open=False)
finally:
await bundle.runtime.disconnect()
def _si(kernel, action, trade_id, asset, side_str, price, size, **kw):
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
return kernel.process_intent(KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id, trade_id=trade_id, slot_id=0, asset=asset,
side=ds, action=action, reference_price=price, target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
))
'''
# =====================================================================
# Build scenario bodies + tests
# =====================================================================
scenarios = [] # (name, code_lines)
def S(name, code_lines):
scenarios.append((name, list(code_lines)))
# --- Original 9 ---
S("simple_entry_exit", [
'tid = f"s-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("multi_leg_exit", [
'tid = f"ml-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
])
S("cancel_entry_order", [
'tid = f"ce-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("entry_hold_exit", [
'tid = f"h-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(3)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("entry_exit_at_loss", [
'tid = f"l-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*1.005, 0.001); await asyncio.sleep(1)',
])
S("two_sequential_cycles", [
'p = float(snap.price)',
't1 = f"2c1-{int(time.time()*1000)}"; t2 = f"2c2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(1)',
])
S("entry_then_recover", [
'tid = f"r-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'await bundle.runtime.disconnect()',
'await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)',
'await asyncio.sleep(1)',
])
S("long_entry_exit", [
'tid = f"ln-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "LONG", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "LONG", p*1.005, 0.001); await asyncio.sleep(1)',
])
# --- Cancel combos ---
S("cancel_idempotent", [
'tid = f"ci-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("double_cancel", [
'tid = f"dc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("cancel_then_exit", [
'tid = f"ctx-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_cancel_exit", [
'tid = f"exc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_reentry", [
'p = float(snap.price)',
't1 = f"er1-{int(time.time()*1000)}"; t2 = f"er2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("limit_cancel", [
'tid = f"lc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(1)',
])
# --- X4 expanded ---
S("x4_partial_hold_exit", [
'tid = f"ph-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.003',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
])
S("x4_three_leg", [
'tid = f"3l-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
])
S("x4_cancel_fill_partial", [
'tid = f"cfp-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001); await asyncio.sleep(1)',
])
S("x4_rapid_three", [
'p = float(snap.price)',
'for i in range(3):',
' tid = f"r3-{i}-{int(time.time()*1000)}"',
' _si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
])
S("x4_diff_symbol", [
'tid = f"ds-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'_si(k, KernelCommandType.EXIT, tid, sym2, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
])
S("x4_alternating", [
'p = float(snap.price)',
't1 = f"as1-{int(time.time()*1000)}"; t2 = f"as2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'try:',
' url = "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol=" + sym2.replace("USDT","-USDT")',
' p2 = float(json.loads(urllib.request.urlopen(url, timeout=5).read())["data"]["price"])',
'except: p2 = p',
'_si(k, KernelCommandType.ENTER, t2, sym2, "LONG", p2, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, sym2, "LONG", p2*1.005, 0.001); await asyncio.sleep(1)',
])
S("x4_multi_flatten", [
'tid = f"mf-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'for i in range(3):',
' if k.slot(0).is_free(): break',
' _flatten_via_kernel_intent(k, symbol, p*0.99, f"mf{i}"); await asyncio.sleep(0.5)',
])
S("x4_three_leg_25_50_25", [
'tid = f"x4a-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
])
S("x4_enter_exit_hold_twice", [
'p = float(snap.price)',
't1 = f"x4b1-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
't2 = f"x4b2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
't3 = f"x4b3-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t3, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t3, symbol, "SHORT", p*0.985, 0.001); await asyncio.sleep(0.5)',
])
S("x4_cancel_then_double_exit", [
'tid = f"x4c-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.002',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, sz); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
])
# --- 2 sides × 2 profit × 4 patterns = 16 ---
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp_mult in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("partial", "_partial", [
'sz = 0.002',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
]),
("cancel", "_cancel", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("double_exit", "_double_exit", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}*0.995, 0.001); await asyncio.sleep(0.5)',
]),
]:
name = f"{pat}_{side}_{pname}"
S(name, [
f'tid = f"{pat[0]}{side[0]}{"p" if prof else "l"}-{{int(time.time()*1000)}}"; p = float(snap.price)',
*lines,
])
# --- Triple sequential × 4 ---
for i in range(4):
side = "SHORT"; ep = 0.995
S(f"triple_seq_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"ts{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
])
for i in range(4):
side = "LONG"; ep = 1.005
S(f"triple_seq_long_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"tsl{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
])
# --- Cancel+reenter × 4 ---
for i in range(4):
side = "SHORT"
S(f"cancel_reenter_{i}", [
'p = float(snap.price)',
f't1 = f"cr{i}a-{{int(time.time()*1000)}}"; t2 = f"cr{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*0.995, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*0.99, 0.001); await asyncio.sleep(0.5)',
])
for i in range(4):
side = "LONG"
S(f"cancel_reenter_long_{i}", [
'p = float(snap.price)',
f't1 = f"crl{i}a-{{int(time.time()*1000)}}"; t2 = f"crl{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*1.005, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*1.01, 0.001); await asyncio.sleep(0.5)',
])
# --- Leg ratios × 8 ---
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
nlegs = len(ratios)
code = [
f'tid = f"lr{i}-{{int(time.time()*1000)}}"; p = float(snap.price); sz = 0.004',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)',
]
for leg in range(nlegs - 1):
r = ratios[leg]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
r_last = ratios[-1]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*{r_last}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
S(f"leg_ratio_{i}", code)
# --- Breakeven × 4 ---
for i in range(4):
S(f"breakeven_{i}", [
f'tid = f"be{i}-{{int(time.time()*1000)}}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
])
# =====================================================================
# Assemble output
# =====================================================================
lines = [PROLOGUE, RUNNER]
lines.append('# =====================================================================')
lines.append('# Scenario body functions')
lines.append('# =====================================================================')
lines.append('')
lines.append('k = None # type: ignore # shorthand alias for bundle.runtime.kernel')
lines.append('')
for name, code_lines in scenarios:
lines.append(f'async def _body_{name}(bundle, client, symbol, snap):')
lines.append(' k = bundle.runtime.kernel')
for cl in code_lines:
lines.append(f' {cl}')
lines.append('')
lines.append('# =====================================================================')
lines.append('# Test functions')
lines.append('# =====================================================================')
lines.append('')
lines.append(
'@pytest.fixture(scope="session")\n'
'def _live_client():\n'
' cfg = _build_bingx_config(25000.0)\n'
' c = BingxHttpClient(cfg)\n'
' yield c\n'
)
for name, _ in scenarios:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
ic = bundle.runtime.kernel.account.snapshot.capital
result = asyncio.run(_run_scenario(bundle, _live_client, _body_{name}, "{name}", ic))
assert result.positions_flat, f"{name}: {{result.error}}"
''')
lines.append('''
def test_pink_ditav2_open_partial_close_and_flatten(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
outcomes = asyncio.run(_run_pink_live_roundtrip(bundle, _live_client))
e, m, f = outcomes
assert e.accepted or e.diagnostic_code in {KernelDiagnosticCode.OK}, f"Entry not accepted: {e.diagnostic_code}"
slot = bundle.runtime.kernel.slot(0) if bundle.runtime.kernel.max_slots > 0 else None
if slot is not None and not slot.is_free():
pytest.skip(f"Slot not flat (fsm_state={slot.fsm_state})")
def test_pink_ditav2_reconciliation_only_on_explicit_recovery(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
recovered = asyncio.run(_run_pink_live_recovery(bundle, _live_client))
assert isinstance(recovered, dict), f"Expected dict, got {type(recovered)}"
assert recovered.get("capital", 0) > 0, "Expected positive capital after recovery"
''')
full = '\n'.join(lines)
try:
ast.parse(full)
test_count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {test_count} tests, {len(full)} chars")
with open(OUT, 'w') as f:
f.write(full)
print(f"Written to {OUT}")
print(f"Breakdown: {len(scenarios)} scenarios + 2 legacy = {test_count} total tests")
except SyntaxError as e:
print(f"Syntax error line {e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,67 +0,0 @@
from __future__ import annotations
import json
from typing import Any, Protocol
from .contracts import KernelTransition, TradeSlot
from .control import KernelControlSnapshot
from .journal import _transition_row
from .projection import build_position_state_row
from .utils import json_safe
class HazelcastClientLike(Protocol):
def get_map(self, name: str): ...
def get_topic(self, name: str): ...
class HazelcastProjector:
"""Durable BLUE/PINK-compatible projection mirror."""
def __init__(
self,
client: HazelcastClientLike | None = None,
*,
active_slots_map: str = "dita_active_slots",
events_topic: str = "dita_trade_events",
) -> None:
self.client = client
self.active_slots_map = active_slots_map
self.events_topic = events_topic
def publish_slot(self, slot: TradeSlot) -> None:
if self.client is None:
return
self.client.get_map(self.active_slots_map).put(slot.trade_id, build_position_state_row(slot))
def publish_event(self, event_type: str, payload: dict[str, Any]) -> None:
if self.client is None:
return
topic = self.client.get_topic(self.events_topic)
topic.publish(
json.dumps(
{"event_type": event_type, "payload": json_safe(payload)},
ensure_ascii=False,
sort_keys=True,
default=str,
)
)
class HazelcastRowWriter:
"""Callback bridge for ``HazelcastProjection`` writer hooks."""
def __init__(self, client: HazelcastClientLike) -> None:
self.client = client
def __call__(self, name: str, row: dict[str, Any]) -> None:
if name.endswith("trade_events"):
self.client.get_topic(name).publish(
json.dumps(row, ensure_ascii=False, sort_keys=True, default=str)
)
return
if name.endswith("control"):
key = "control"
else:
key = str(row.get("trade_id", row.get("slot_id", row.get("event_id", ""))))
self.client.get_map(name).put(key, json_safe(row))

View File

@@ -1,102 +0,0 @@
"""Debug journaling surfaces for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional, Protocol
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .utils import json_safe, json_text
JournalSink = Callable[[str, Dict[str, Any]], None]
class KernelJournal(Protocol):
"""Append-only debug journal interface."""
def record(self, row: Dict[str, Any]) -> None:
...
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
...
@dataclass
class MemoryKernelJournal:
"""In-memory journal used in tests."""
rows: List[Dict[str, Any]] = field(default_factory=list)
capture_limit: int = 10_000
def record(self, row: Dict[str, Any]) -> None:
if len(self.rows) < self.capture_limit:
self.rows.append(dict(row))
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
self.record(row)
class ClickHouseKernelJournal:
"""Fire-and-forget ClickHouse journal.
The sink is a small callable of the form ``sink(table_name, row_dict)``.
"""
def __init__(self, sink: Optional[JournalSink] = None):
self.sink = sink
def record(self, row: Dict[str, Any]) -> None:
if self.sink is not None:
self.sink("dita_kernel_debug", row)
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
self.record(_transition_row(transition=transition, slot=slot, event=event, control=control))
def _transition_row(
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent],
control: Optional[KernelControlSnapshot],
) -> Dict[str, Any]:
return {
"ts": transition.timestamp.isoformat() if hasattr(transition.timestamp, "isoformat") else str(transition.timestamp),
"trade_id": transition.trade_id,
"slot_id": transition.slot_id,
"prev_state": transition.prev_state.value,
"next_state": transition.next_state.value,
"trigger": transition.trigger,
"intent_id": transition.intent_id,
"event_id": transition.event_id,
"control_mode": transition.control_mode,
"control_verbosity": transition.control_verbosity,
"slot_state": slot.to_dict(),
"event_payload": json_safe(event) if event is not None else {},
"control_snapshot": control.as_dict() if control is not None else {},
"slot_state_json": json_text(slot.to_dict()),
}

View File

@@ -1,8 +0,0 @@
"""Compatibility shim for the Rust-backed DITAv2 execution kernel."""
from __future__ import annotations
from .rust_backend import ExecutionKernel
__all__ = ["ExecutionKernel"]

View File

@@ -1,350 +0,0 @@
"""Operator-facing bootstrap helpers for DITAv2.
This module keeps the wiring explicit:
- control plane selection
- Zinc plane selection
- projection sink selection
- venue adapter selection
The defaults stay safe and testable. Real shared-memory or live BingX wiring
is only enabled when the caller opts in via arguments or environment.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
import asyncio
import inspect
import os
from pathlib import Path
from typing import Any, Optional
from dotenv import load_dotenv
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from .bingx_venue import BingxVenueAdapter
from .control import BackendMode
from .control import ControlPlane
from .control import ControlUpdate
from .control import KernelControlSnapshot
from .control import KernelMode
from .control import KernelVerbosity
from .control import build_control_plane
from .mock_venue import MockVenueAdapter
from .mock_venue import MockVenueScenario
from .projection import HazelcastProjection
from .projection import build_projection
from .real_control_plane import RealZincControlPlane
from .real_control_plane import RealZincUnavailable
from .real_zinc_plane import RealZincPlane
from .real_zinc_plane import RealZincUnavailable as RealZincPlaneUnavailable
from .rust_backend import ExecutionKernel
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane
from .zinc_plane import ZincPlane
PROJECT_ROOT = Path(__file__).resolve().parents[3]
load_dotenv(PROJECT_ROOT / ".env")
class LauncherVenueMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
class LauncherZincMode(str, Enum):
IN_MEMORY = "IN_MEMORY"
REAL = "REAL"
@dataclass
class DITAv2LauncherBundle:
"""Concrete runtime components assembled by the launcher."""
kernel: ExecutionKernel
control_plane: ControlPlane
projection: HazelcastProjection
zinc_plane: ZincPlane
venue: VenueAdapter
def close(self) -> None:
_maybe_close(self.venue)
_maybe_close(self.zinc_plane)
_maybe_close(self.control_plane)
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _resolve_control_mode() -> KernelMode | None:
raw = _env_upper("DITA_V2_MODE", "")
if raw == KernelMode.DEBUG.value:
return KernelMode.DEBUG
if raw == KernelMode.NORMAL.value:
return KernelMode.NORMAL
return None
def _resolve_control_verbosity() -> KernelVerbosity | None:
raw = _env_upper("DITA_V2_VERBOSITY", "")
if raw == KernelVerbosity.TRACE.value:
return KernelVerbosity.TRACE
if raw == KernelVerbosity.VERBOSE.value:
return KernelVerbosity.VERBOSE
if raw == KernelVerbosity.QUIET.value:
return KernelVerbosity.QUIET
return None
def _resolve_backend_mode() -> BackendMode | None:
raw = _env_upper("DITA_V2_BACKEND_MODE", "")
if raw == BackendMode.BINGX.value:
return BackendMode.BINGX
if raw == BackendMode.MOCK.value:
return BackendMode.MOCK
return None
def _control_update_from_env() -> ControlUpdate | None:
fields: dict[str, Any] = {}
mode = _resolve_control_mode()
if mode is not None:
fields["mode"] = mode
verbosity = _resolve_control_verbosity()
if verbosity is not None:
fields["verbosity"] = verbosity
backend_mode = _resolve_backend_mode()
if backend_mode is not None:
fields["backend_mode"] = backend_mode
raw = os.environ.get("DITA_V2_DEBUG_CLICKHOUSE")
if raw is not None:
fields["debug_clickhouse_enabled"] = _env_bool("DITA_V2_DEBUG_CLICKHOUSE", True)
raw = os.environ.get("DITA_V2_TRACE_TRANSITIONS")
if raw is not None:
fields["trace_transitions"] = _env_bool("DITA_V2_TRACE_TRANSITIONS", False)
raw = os.environ.get("DITA_V2_MIRROR_TO_HAZELCAST")
if raw is not None:
fields["mirror_to_hazelcast"] = _env_bool("DITA_V2_MIRROR_TO_HAZELCAST", True)
raw = os.environ.get("DITA_V2_ACTIVE_SLOT_LIMIT")
if raw is not None:
try:
fields["active_slot_limit"] = max(1, int(str(raw).strip()))
except Exception:
pass
raw = os.environ.get("DITA_V2_RECONCILE_ON_RESTART")
if raw is not None:
fields["reconcile_on_restart"] = _env_bool("DITA_V2_RECONCILE_ON_RESTART", True)
return ControlUpdate(**fields) if fields else None
def _resolve_venue_mode(venue_mode: Optional[str] = None) -> LauncherVenueMode:
raw = _env_upper("DITA_V2_VENUE", venue_mode or LauncherVenueMode.MOCK.value)
if raw == LauncherVenueMode.BINGX.value:
return LauncherVenueMode.BINGX
return LauncherVenueMode.MOCK
def _resolve_zinc_mode(zinc_mode: Optional[str] = None) -> LauncherZincMode:
raw = _env_upper("DITA_V2_ZINC", zinc_mode or LauncherZincMode.IN_MEMORY.value)
if raw == LauncherZincMode.REAL.value:
return LauncherZincMode.REAL
return LauncherZincMode.IN_MEMORY
def _resolve_hazelcast_real(prefer_real_hazelcast: Optional[bool] = None) -> bool:
if prefer_real_hazelcast is not None:
return bool(prefer_real_hazelcast)
raw = _env_upper("DITA_V2_HAZELCAST", "")
return raw in {"REAL", "REAL_HZ", "HAZELCAST"}
def build_bingx_exec_client_config(
*,
environment: Optional[BingxEnvironment] = None,
allow_mainnet: Optional[bool] = None,
recv_window_ms: Optional[int] = None,
default_leverage: Optional[int] = None,
exchange_leverage_cap: Optional[int] = None,
prefer_websocket: Optional[bool] = None,
sizing_mode: Optional[str] = None,
) -> BingxExecClientConfig:
"""Build the direct BingX config used by the DITAv2 launcher."""
resolved_environment = environment or (
BingxEnvironment.LIVE if _env_upper("DOLPHIN_BINGX_ENV", "VST") == "LIVE" else BingxEnvironment.VST
)
resolved_allow_mainnet = _env_bool("DOLPHIN_BINGX_ALLOW_MAINNET", False) if allow_mainnet is None else bool(allow_mainnet)
resolved_recv_window = int(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "5000")) if recv_window_ms is None else int(recv_window_ms)
resolved_default_leverage = int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")) if default_leverage is None else int(default_leverage)
resolved_exchange_cap = int(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")) if exchange_leverage_cap is None else int(exchange_leverage_cap)
resolved_prefer_ws = _env_bool("DOLPHIN_BINGX_PREFER_WEBSOCKET", False) if prefer_websocket is None else bool(prefer_websocket)
resolved_sizing_mode = sizing_mode or os.environ.get("DOLPHIN_BINGX_SIZING_MODE", "testnet")
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=resolved_environment,
allow_mainnet=resolved_allow_mainnet,
recv_window_ms=max(1, resolved_recv_window),
default_leverage=max(1, resolved_default_leverage),
exchange_leverage_cap=max(1, resolved_exchange_cap),
prefer_websocket=resolved_prefer_ws,
sizing_mode=resolved_sizing_mode,
journal_strategy=os.environ.get("DOLPHIN_BINGX_JOURNAL_STRATEGY", "dita_v2"),
journal_db=os.environ.get("DOLPHIN_BINGX_JOURNAL_DB", "dolphin_pink"),
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
def _build_control_plane(
*,
prefix: str,
control_plane: Optional[ControlPlane] = None,
) -> ControlPlane:
plane = control_plane or build_control_plane(prefix=prefix)
update = _control_update_from_env()
if update is not None:
plane.update(update)
return plane
def _build_zinc_plane(
*,
prefix: str,
slot_count: int,
zinc_mode: Optional[LauncherZincMode] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> ZincPlane:
if zinc_plane is not None:
return zinc_plane
resolved_mode = zinc_mode or _resolve_zinc_mode()
if resolved_mode is LauncherZincMode.REAL:
try:
return RealZincPlane(prefix=prefix, slot_count=slot_count, create=True)
except (RealZincPlaneUnavailable, RealZincUnavailable, Exception):
pass
return InMemoryZincPlane()
def _build_venue(
*,
venue_mode: Optional[LauncherVenueMode] = None,
mock_scenario: Optional[MockVenueScenario] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
venue: Optional[VenueAdapter] = None,
) -> VenueAdapter:
if venue is not None:
return venue
resolved_mode = venue_mode or _resolve_venue_mode()
if resolved_mode is LauncherVenueMode.BINGX:
backend = bingx_backend
if backend is None:
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(bingx_config or build_bingx_exec_client_config())
return BingxVenueAdapter(backend=backend)
return MockVenueAdapter(mock_scenario)
def _maybe_close(obj: Any) -> None:
for method_name in ("close", "disconnect"):
method = getattr(obj, method_name, None)
if method is None:
continue
try:
result = method()
except TypeError:
continue
if inspect.isawaitable(result):
try:
asyncio.run(result)
except RuntimeError:
pass
break
def build_launcher_bundle(
*,
max_slots: int = 10,
prefix: Optional[str] = None,
control_plane: Optional[ControlPlane] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
venue: Optional[VenueAdapter] = None,
venue_mode: Optional[LauncherVenueMode | str] = None,
zinc_mode: Optional[LauncherZincMode | str] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
mock_scenario: Optional[MockVenueScenario] = None,
) -> DITAv2LauncherBundle:
"""Build a fully wired DITAv2 runtime bundle.
Defaults stay non-destructive:
- in-memory Zinc plane
- in-process control plane
- mock venue
- callback projection unless a Hazelcast client is supplied
"""
resolved_prefix = (prefix or os.environ.get("DITA_V2_PREFIX", "dita_v2")).strip() or "dita_v2"
if isinstance(venue_mode, LauncherVenueMode):
resolved_venue_mode = venue_mode
elif isinstance(venue_mode, str):
resolved_venue_mode = LauncherVenueMode(venue_mode.strip().upper())
else:
resolved_venue_mode = None
if isinstance(zinc_mode, LauncherZincMode):
resolved_zinc_mode = zinc_mode
elif isinstance(zinc_mode, str):
resolved_zinc_mode = LauncherZincMode(zinc_mode.strip().upper())
else:
resolved_zinc_mode = None
active_control_plane = _build_control_plane(prefix=resolved_prefix, control_plane=control_plane)
control_snapshot = active_control_plane.read()
active_projection = projection or build_projection(
client=projection_client,
prefer_real_hazelcast=_resolve_hazelcast_real(),
control_snapshot=control_snapshot,
)
active_zinc_plane = _build_zinc_plane(
prefix=resolved_prefix,
slot_count=int(max_slots),
zinc_mode=resolved_zinc_mode,
zinc_plane=zinc_plane,
)
active_venue = _build_venue(
venue_mode=resolved_venue_mode,
mock_scenario=mock_scenario,
bingx_config=bingx_config,
bingx_backend=bingx_backend,
venue=venue,
)
kernel = ExecutionKernel(
max_slots=int(max_slots),
control_plane=active_control_plane,
venue=active_venue,
projection=active_projection,
projection_client=projection_client,
zinc_plane=active_zinc_plane,
)
return DITAv2LauncherBundle(
kernel=kernel,
control_plane=active_control_plane,
projection=active_projection,
zinc_plane=active_zinc_plane,
venue=active_venue,
)

View File

@@ -1,203 +0,0 @@
"""Deterministic mock venue for DITAv2 tests."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
import itertools
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .venue import VenueAdapter
@dataclass(frozen=True)
class MockVenueScenario:
"""Failure knobs for the mock venue."""
reject_entries: bool = False
reject_exits: bool = False
partial_fill_ratio: float = 1.0
cancel_reject: bool = False
emit_ack_before_fill: bool = True
emit_fill_on_submit: bool = False
class MockVenueAdapter(VenueAdapter):
"""Scriptable mock venue with BingX-shaped response semantics."""
def __init__(self, scenario: Optional[MockVenueScenario] = None):
self.scenario = scenario or MockVenueScenario()
self._order_seq = itertools.count(1)
self._event_seq = itertools.count(1)
self._open_orders: Dict[str, VenueOrder] = {}
self._open_positions: Dict[str, Dict[str, Any]] = {}
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
is_entry = intent.action == KernelCommandType.ENTER
should_reject = self.scenario.reject_entries if is_entry else self.scenario.reject_exits
order_id = f"V-{next(self._order_seq):08d}"
client_id = f"{intent.trade_id}:{intent.intent_id}"
order = VenueOrder(
internal_trade_id=intent.trade_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=intent.side,
intended_size=float(intent.target_size),
status=VenueOrderStatus.NEW,
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "slot_id": intent.slot_id},
)
if should_reject:
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.REJECTED,
metadata=dict(order.metadata),
)
return [self._event_from_order(intent, order, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, reason="MOCK_REJECT")]
self._open_orders[order_id] = order
events: List[VenueEvent] = []
if self.scenario.emit_ack_before_fill or not self.scenario.emit_fill_on_submit:
events.append(self._event_from_order(intent, order, KernelEventKind.ORDER_ACK, VenueEventStatus.ACKED))
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
fill_ratio = max(0.0, min(1.0, float(self.scenario.partial_fill_ratio)))
fill_size = float(intent.target_size) * fill_ratio
event_kind = KernelEventKind.FULL_FILL if fill_ratio >= 1.0 else KernelEventKind.PARTIAL_FILL
event_status = VenueEventStatus.FILLED if fill_ratio >= 1.0 else VenueEventStatus.PARTIALLY_FILLED
fill_event = self._event_from_order(
intent,
order,
event_kind,
event_status,
price=float(intent.reference_price or 0.0),
fill_size=fill_size,
remaining_size=max(0.0, float(intent.target_size) - fill_size),
)
events.append(fill_event)
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=fill_size,
average_fill_price=float(intent.reference_price or 0.0),
status=VenueOrderStatus.FILLED if fill_ratio >= 1.0 else VenueOrderStatus.PARTIALLY_FILLED,
metadata=dict(order.metadata),
)
self._open_orders[order_id] = order
return events
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
if self.scenario.cancel_reject:
return [
self._event_from_order(
self._dummy_intent(order),
order,
KernelEventKind.CANCEL_REJECT,
VenueEventStatus.CANCELED_REJECTED,
reason=reason or "MOCK_CANCEL_REJECT",
)
]
existing = self._open_orders.get(order.venue_order_id, order)
canceled = VenueOrder(
internal_trade_id=existing.internal_trade_id,
venue_order_id=existing.venue_order_id,
venue_client_id=existing.venue_client_id,
side=existing.side,
intended_size=existing.intended_size,
filled_size=existing.filled_size,
average_fill_price=existing.average_fill_price,
status=VenueOrderStatus.CANCELED,
metadata=dict(existing.metadata),
)
self._open_orders.pop(order.venue_order_id, None)
return [
self._event_from_order(
self._dummy_intent(order),
canceled,
KernelEventKind.CANCEL_ACK,
VenueEventStatus.CANCELED,
reason=reason or "MOCK_CANCEL_ACK",
)
]
def open_orders(self) -> List[VenueOrder]:
return list(self._open_orders.values())
def open_positions(self) -> List[Dict[str, Any]]:
return list(self._open_positions.values())
def reconcile(self) -> List[VenueEvent]:
return []
def _dummy_intent(self, order: VenueOrder) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=order.venue_client_id,
trade_id=order.internal_trade_id,
slot_id=int(order.metadata.get("slot_id", 0)),
asset=str(order.metadata.get("asset", "")),
side=order.side,
action=KernelCommandType.EXIT if order.metadata.get("action") == "EXIT" else KernelCommandType.ENTER,
reference_price=float(order.metadata.get("reference_price", 0.0)),
target_size=float(order.intended_size),
leverage=float(order.metadata.get("leverage", 1.0)),
reason=str(order.metadata.get("reason", "")),
metadata=dict(order.metadata),
)
def _event_from_order(
self,
intent: KernelIntent,
order: VenueOrder,
kind: KernelEventKind,
status: VenueEventStatus,
*,
price: Optional[float] = None,
fill_size: float = 0.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{next(self._event_seq):08d}",
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=kind,
status=status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=intent.asset,
price=float(price if price is not None else intent.reference_price or 0.0),
size=float(intent.target_size),
filled_size=float(fill_size),
remaining_size=float(remaining_size),
reason=reason,
raw_payload={
"status": status.value,
"orderId": order.venue_order_id,
"clientOrderId": order.venue_client_id,
"symbol": intent.asset,
"side": order.side.value,
"action": intent.action.value,
},
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
return event

View File

@@ -1,97 +0,0 @@
"""Hazelcast-compatible projection helpers for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
import os
from typing import Any, Callable, Dict, Iterable, List, Optional
from .account import AccountProjection
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .journal import _transition_row
from .utils import json_safe
Writer = Callable[[str, Dict[str, Any]], None]
@dataclass
class HazelcastProjection:
"""Projection helper for BLUE/PINK-compatible durable writes."""
active_slots_map: str = "hz:dita_active_slots"
trade_events_topic: str = "hz:dita_trade_events"
control_map: str = "hz:dita_control"
writer: Optional[Writer] = None
control_snapshot: Optional[KernelControlSnapshot] = None
def write_slot(self, slot: TradeSlot) -> Dict[str, Any]:
row = build_position_state_row(slot, self.control_snapshot)
if self.writer is not None:
self.writer(self.active_slots_map, row)
return row
def write_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> Dict[str, Any]:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
if self.writer is not None:
self.writer(self.trade_events_topic, row)
return row
def write_control(self, control: KernelControlSnapshot) -> Dict[str, Any]:
self.control_snapshot = control
row = control.as_dict()
if self.writer is not None:
self.writer(self.control_map, row)
return row
def build_projection(
*,
writer: Optional[Writer] = None,
client: Optional[Any] = None,
prefer_real_hazelcast: Optional[bool] = None,
control_snapshot: Optional[KernelControlSnapshot] = None,
) -> HazelcastProjection:
"""Build the active projection helper with an operator-visible switch.
The default remains the callback-based projection helper. If a Hazelcast
client is supplied and the caller opts in via ``prefer_real_hazelcast`` or
``DITA_V2_HAZELCAST=REAL``, the helper routes directly through the
client-backed map/topic writer path.
"""
env_choice = os.environ.get("DITA_V2_HAZELCAST", "").strip().upper()
real_requested = prefer_real_hazelcast if prefer_real_hazelcast is not None else env_choice in {"REAL", "REAL_HZ", "HAZELCAST"}
if real_requested and client is not None:
try:
from .hazelcast_projection import HazelcastRowWriter
writer = HazelcastRowWriter(client)
except Exception:
pass
return HazelcastProjection(writer=writer, control_snapshot=control_snapshot)
def build_position_state_row(slot: TradeSlot, control: Optional[KernelControlSnapshot] = None) -> Dict[str, Any]:
"""Build a state row shaped for durable compatibility."""
row = slot.to_dict()
row.update(
{
"runtime_namespace": control.runtime_namespace if control else "dita_v2",
"strategy_namespace": control.strategy_namespace if control else "dita_v2",
"event_namespace": control.event_namespace if control else "dita_v2",
"actor_name": control.actor_name if control else "ExecutionKernel",
"exec_venue": control.exec_venue if control else "bingx",
"data_venue": control.data_venue if control else "binance",
"ledger_authority": control.ledger_authority if control else "exchange",
}
)
return row

View File

@@ -1,129 +0,0 @@
"""Real Zinc-backed control plane for DITAv2."""
from __future__ import annotations
import json
import struct
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from .control import BackendMode, ControlPlane, ControlUpdate, KernelControlSnapshot, KernelMode, KernelVerbosity
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.insert(0, str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincControlPlane(ControlPlane):
"""Shared-memory Zinc-backed control plane."""
def __init__(self, *, prefix: str, create: bool = True) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.region_name = f"{base}_control"
self._seq = 0
self._snapshot = KernelControlSnapshot()
if create:
self.region = SharedRegion.create(self.region_name, 1 << 20)
self._write_region(self._seq, self._snapshot.as_dict())
else:
self.region = SharedRegion.open(self.region_name)
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if isinstance(control, dict):
self._snapshot = KernelControlSnapshot(**control)
def close(self) -> None:
self.region.close()
def read(self) -> KernelControlSnapshot:
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._snapshot
self._snapshot = KernelControlSnapshot(**control)
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
self._snapshot = update.apply(self.read())
self._seq += 1
self._write_region(self._seq, self._snapshot.as_dict())
return self._snapshot
def mirror(self) -> Dict[str, Any]:
return self._snapshot.as_dict()
def wait(self, timeout_ms: int = 1000) -> bool:
try:
return bool(self.region.wait(timeout_ms))
except Exception:
return False
def notify(self) -> None:
try:
self.region.notify()
except Exception:
pass
def _write_region(self, seq: int, control: Dict[str, Any]) -> None:
packet = _encode_packet(seq, {"control": control})
buf = self.region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc control region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[: len(packet)] = packet
if len(view) > len(packet):
view[len(packet) :] = b"\x00" * (len(view) - len(packet))
try:
self.region.notify()
except Exception:
pass

View File

@@ -1,263 +0,0 @@
"""Real Zinc-backed hot-path plane for DITAv2.
This wrapper uses the Zinc Python adapter directly. The kernel still talks to
the narrow ``ZincPlane`` interface; this module just makes that interface real.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import json
import os
import struct
import sys
import threading
from .contracts import KernelIntent, TradeSide, TradeSlot, TradeStage, VenueOrder, VenueOrderStatus
from .control import KernelControlSnapshot
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.insert(0, str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
data = slot.to_dict()
return data
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
active_entry_order = None
active_exit_order = None
if isinstance(payload.get("active_entry_order"), dict):
active_entry_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_entry_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_entry_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_entry_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_entry_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_entry_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_entry_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_entry_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_entry_order"].get("metadata", {})),
)
if isinstance(payload.get("active_exit_order"), dict):
active_exit_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_exit_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_exit_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_exit_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_exit_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_exit_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_exit_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_exit_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_exit_order"].get("metadata", {})),
)
slot = TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=active_exit_order,
active_entry_order=active_entry_order,
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
return slot
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincPlane:
"""Shared-memory Zinc plane used by the Python prototype."""
def __init__(
self,
*,
prefix: str,
slot_count: int = 10,
intent_capacity: int = 1 << 20,
state_capacity: int = 1 << 20,
control_capacity: int = 1 << 20,
create: bool = True,
) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.intent_name = f"{base}_intent"
self.state_name = f"{base}_state"
self.control_name = f"{base}_control"
self._intent_seq = 0
self._state_seq = 0
self._control_seq = 0
self._lock = threading.Lock()
self._slot_cache: Dict[int, TradeSlot] = {i: TradeSlot(slot_id=i) for i in range(int(slot_count))}
self._slot_count = int(slot_count)
self._intent_cache: List[Dict[str, Any]] = []
self._control_cache = KernelControlSnapshot()
if create:
self.intent_region = SharedRegion.create(self.intent_name, intent_capacity)
self.state_region = SharedRegion.create(self.state_name, state_capacity)
self.control_region = SharedRegion.create(self.control_name, control_capacity)
self._write_region(self.control_region, self._control_seq, {"control": self._control_cache.as_dict()})
self._write_region(
self.state_region,
self._state_seq,
{"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)]},
)
self._write_region(self.intent_region, self._intent_seq, {"items": []})
else:
self.intent_region = SharedRegion.open(self.intent_name)
self.state_region = SharedRegion.open(self.state_name)
self.control_region = SharedRegion.open(self.control_name)
control_payload = _decode_packet(self.control_region.as_buffer())
state_payload = _decode_packet(self.state_region.as_buffer())
intent_payload = _decode_packet(self.intent_region.as_buffer())
if isinstance(control_payload.get("control"), dict):
self._control_cache = KernelControlSnapshot(**control_payload["control"])
if isinstance(state_payload.get("slots"), list):
for slot_payload in state_payload["slots"]:
if isinstance(slot_payload, dict):
slot = _slot_from_payload(slot_payload)
self._slot_cache[int(slot.slot_id)] = slot
if isinstance(intent_payload.get("items"), list):
self._intent_cache = list(intent_payload["items"])
def close(self) -> None:
self.intent_region.close()
self.state_region.close()
self.control_region.close()
def publish_intent(self, intent: KernelIntent) -> None:
with self._lock:
self._intent_seq += 1
row = intent.__dict__.copy()
row["timestamp"] = intent.timestamp.isoformat()
row["side"] = intent.side.value
row["action"] = intent.action.value
row["stage"] = intent.stage.value
row["exit_leg_ratios"] = list(intent.exit_leg_ratios)
row["metadata"] = json.loads(json.dumps(intent.metadata, default=_json_default))
self._intent_cache.append(row)
self._write_region(self.intent_region, self._intent_seq, {"items": self._intent_cache[-512:]})
def write_slot(self, slot: TradeSlot) -> None:
with self._lock:
self._state_seq += 1
self._slot_cache[int(slot.slot_id)] = slot
payload = {
"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)],
}
self._write_region(self.state_region, self._state_seq, payload)
def read_slots(self) -> List[TradeSlot]:
payload = _decode_packet(self.state_region.as_buffer())
slots = payload.get("slots", []) if isinstance(payload, dict) else []
return [_slot_from_payload(slot) for slot in sorted(slots, key=lambda row: int(row.get("slot_id", 0)))]
def read_intents(self) -> List[Dict[str, Any]]:
payload = _decode_packet(self.intent_region.as_buffer())
items = payload.get("items", []) if isinstance(payload, dict) else []
return list(items)
def update_control(self, control: KernelControlSnapshot) -> None:
with self._lock:
self._control_seq += 1
self._control_cache = control
self._write_region(self.control_region, self._control_seq, {"control": control.as_dict()})
def read_control(self) -> KernelControlSnapshot:
payload = _decode_packet(self.control_region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._control_cache
return KernelControlSnapshot(**control)
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return bool(self.state_region.wait(timeout_ms))
def notify_state(self) -> None:
self.state_region.notify()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return bool(self.control_region.wait(timeout_ms))
def notify_control(self) -> None:
self.control_region.notify()
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return bool(self.intent_region.wait(timeout_ms))
def notify_intent(self) -> None:
self.intent_region.notify()
def _write_region(self, region: Any, seq: int, payload: Dict[str, Any]) -> None:
packet = _encode_packet(seq, payload)
buf = region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[:] = b"\x00" * len(view)
view[: len(packet)] = packet
region.notify()

View File

@@ -1,683 +0,0 @@
"""Rust-backed DITAv2 execution kernel.
This module keeps the Python API shape stable while moving the kernel state
machine into a Rust shared library. Slot views write through to the backend on
assignment, then the Python side mirrors the resulting state into Zinc and the
existing projections/journals.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence
import ctypes
import json
import os
import subprocess
import sys
from .account import AccountProjection
from .control import ControlPlane, ControlUpdate, KernelControlSnapshot, KernelVerbosity, build_control_plane
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueOrder,
VenueOrderStatus,
VenueEventStatus,
)
from .journal import KernelJournal, MemoryKernelJournal
from .mock_venue import MockVenueAdapter
from .projection import HazelcastProjection
from .projection import build_projection
from .utils import json_safe
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane, ZincPlane
def _repo_root() -> Path:
return Path(__file__).resolve().parents[3]
def _crate_dir() -> Path:
return Path(__file__).resolve().with_name("_rust_kernel")
def _library_path() -> Path:
if sys.platform == "darwin":
name = "libdita_v2_kernel.dylib"
elif os.name == "nt":
name = "dita_v2_kernel.dll"
else:
name = "libdita_v2_kernel.so"
return _crate_dir() / "target" / "release" / name
def _build_library() -> None:
crate_dir = _crate_dir()
if not crate_dir.exists():
raise FileNotFoundError(f"Missing Rust kernel crate: {crate_dir}")
subprocess.run(
["cargo", "build", "--release", "--manifest-path", str(crate_dir / "Cargo.toml")],
cwd=_repo_root(),
check=True,
)
def _ensure_library() -> Path:
path = _library_path()
if not path.exists():
_build_library()
return path
class _RustKernelLib:
def __init__(self) -> None:
path = _ensure_library()
self.lib = ctypes.CDLL(str(path))
self.lib.dita_kernel_create.argtypes = [ctypes.c_size_t]
self.lib.dita_kernel_create.restype = ctypes.c_void_p
self.lib.dita_kernel_destroy.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_destroy.restype = None
self.lib.dita_kernel_free_string.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_free_string.restype = None
self.lib.dita_kernel_get_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
self.lib.dita_kernel_get_slot_json.restype = ctypes.c_void_p
self.lib.dita_kernel_set_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_char_p]
self.lib.dita_kernel_set_slot_json.restype = ctypes.c_int
self.lib.dita_kernel_process_intent_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_process_intent_json.restype = ctypes.c_void_p
self.lib.dita_kernel_on_venue_event_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_on_venue_event_json.restype = ctypes.c_void_p
self.lib.dita_kernel_reconcile_slots_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_reconcile_slots_json.restype = ctypes.c_void_p
self.lib.dita_kernel_snapshot_json.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_snapshot_json.restype = ctypes.c_void_p
def create(self, max_slots: int) -> ctypes.c_void_p:
handle = self.lib.dita_kernel_create(ctypes.c_size_t(max_slots))
if not handle:
raise RuntimeError("dita_kernel_create failed")
return ctypes.c_void_p(handle)
def destroy(self, handle: ctypes.c_void_p) -> None:
if handle and handle.value:
self.lib.dita_kernel_destroy(handle)
def _take_string(self, raw: ctypes.c_void_p) -> str:
if not raw:
raise RuntimeError("Rust kernel returned null string")
text = ctypes.cast(raw, ctypes.c_char_p).value
if text is None:
self.lib.dita_kernel_free_string(raw)
raise RuntimeError("Rust kernel returned empty string")
try:
return text.decode("utf-8")
finally:
self.lib.dita_kernel_free_string(raw)
def get_slot_json(self, handle: ctypes.c_void_p, slot_id: int) -> Dict[str, Any]:
raw = self.lib.dita_kernel_get_slot_json(handle, ctypes.c_size_t(slot_id))
if not raw:
raise IndexError(f"Invalid slot id: {slot_id}")
return json.loads(self._take_string(raw))
def set_slot_json(self, handle: ctypes.c_void_p, slot_id: int, payload: Dict[str, Any]) -> None:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
rc = self.lib.dita_kernel_set_slot_json(handle, ctypes.c_size_t(slot_id), ctypes.c_char_p(encoded))
if rc != 0:
raise RuntimeError(f"dita_kernel_set_slot_json failed rc={rc}")
def process_intent(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_process_intent_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def on_venue_event(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_on_venue_event_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def reconcile_slots(
self,
handle: ctypes.c_void_p,
payload: Sequence[Dict[str, Any]],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(list(payload)), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_reconcile_slots_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def snapshot(self, handle: ctypes.c_void_p) -> Dict[str, Any]:
raw = self.lib.dita_kernel_snapshot_json(handle)
return json.loads(self._take_string(raw))
_RUST: _RustKernelLib | None = None # lazy init — avoids Rust build on import
def _get_rust() -> _RustKernelLib:
global _RUST
if _RUST is None:
_RUST = _RustKernelLib()
return _RUST
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
return slot.to_dict()
def _order_to_payload(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
def _order_from_payload(payload: Optional[Dict[str, Any]], *, trade_id: str) -> Optional[VenueOrder]:
if not isinstance(payload, dict):
return None
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=str(payload.get("venue_order_id", "")),
venue_client_id=str(payload.get("venue_client_id", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
intended_size=float(payload.get("intended_size", 0.0)),
filled_size=float(payload.get("filled_size", 0.0)),
average_fill_price=float(payload.get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload.get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload.get("metadata", {})),
)
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
return TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=_order_from_payload(payload.get("active_exit_order"), trade_id=str(payload.get("trade_id", ""))),
active_entry_order=_order_from_payload(payload.get("active_entry_order"), trade_id=str(payload.get("trade_id", ""))),
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
def _intent_to_payload(intent: KernelIntent) -> Dict[str, Any]:
return {
"timestamp": intent.timestamp.isoformat() if hasattr(intent.timestamp, "isoformat") else str(intent.timestamp),
"intent_id": intent.intent_id,
"trade_id": intent.trade_id,
"slot_id": intent.slot_id,
"asset": intent.asset,
"side": intent.side.value,
"action": intent.action.value,
"reference_price": float(intent.reference_price or 0.0),
"target_size": float(intent.target_size or 0.0),
"leverage": float(intent.leverage or 0.0),
"exit_leg_ratios": list(intent.exit_leg_ratios),
"reason": intent.reason,
"metadata": dict(intent.metadata),
"stage": intent.stage.value,
}
def _event_to_payload(event: VenueEvent) -> Dict[str, Any]:
return {
"timestamp": event.timestamp.isoformat() if hasattr(event.timestamp, "isoformat") else str(event.timestamp),
"event_id": event.event_id,
"trade_id": event.trade_id,
"slot_id": event.slot_id,
"kind": event.kind.value,
"status": event.status.value,
"venue_order_id": event.venue_order_id,
"venue_client_id": event.venue_client_id,
"side": event.side.value,
"asset": event.asset,
"price": float(event.price or 0.0),
"size": float(event.size or 0.0),
"filled_size": float(event.filled_size or 0.0),
"remaining_size": float(event.remaining_size or 0.0),
"reason": event.reason,
"raw_payload": dict(event.raw_payload),
"metadata": dict(event.metadata),
}
def _transition_from_payload(payload: Dict[str, Any]) -> KernelTransition:
return KernelTransition(
timestamp=datetime.fromisoformat(payload["timestamp"]),
trade_id=str(payload.get("trade_id", "")),
slot_id=int(payload.get("slot_id", 0)),
prev_state=TradeStage(str(payload.get("prev_state", TradeStage.IDLE.value))),
next_state=TradeStage(str(payload.get("next_state", TradeStage.IDLE.value))),
trigger=str(payload.get("trigger", "")),
intent_id=str(payload.get("intent_id", "")),
event_id=str(payload.get("event_id", "")),
control_mode=str(payload.get("control_mode", "")),
control_verbosity=str(payload.get("control_verbosity", "")),
details=dict(payload.get("details", {})),
)
def _outcome_from_payload(payload: Dict[str, Any]) -> KernelOutcome:
return KernelOutcome(
accepted=bool(payload.get("accepted", False)),
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
state=TradeStage(str(payload.get("state", TradeStage.IDLE.value))),
diagnostic_code=KernelDiagnosticCode(str(payload.get("diagnostic_code", KernelDiagnosticCode.OK.value))),
severity=KernelSeverity(str(payload.get("severity", KernelSeverity.INFO.value))),
transitions=tuple(_transition_from_payload(row) for row in payload.get("transitions", [])),
emitted_events=tuple(
VenueEvent(
timestamp=datetime.fromisoformat(row["timestamp"]),
event_id=str(row.get("event_id", "")),
trade_id=str(row.get("trade_id", "")),
slot_id=int(row.get("slot_id", 0)),
kind=KernelEventKind(str(row.get("kind", KernelEventKind.ORDER_ACK.value))),
status=VenueEventStatus(str(row.get("status", VenueEventStatus.ACKED.value))),
venue_order_id=str(row.get("venue_order_id", "")),
venue_client_id=str(row.get("venue_client_id", "")),
side=TradeSide(str(row.get("side", TradeSide.FLAT.value))),
asset=str(row.get("asset", "")),
price=float(row.get("price", 0.0)),
size=float(row.get("size", 0.0)),
filled_size=float(row.get("filled_size", 0.0)),
remaining_size=float(row.get("remaining_size", 0.0)),
reason=str(row.get("reason", "")),
raw_payload=dict(row.get("raw_payload", {})),
metadata=dict(row.get("metadata", {})),
)
for row in payload.get("emitted_events", [])
),
details=dict(payload.get("details", {})),
)
def _enum_text(value: Any) -> str:
if hasattr(value, "value"):
return str(getattr(value, "value"))
return str(value)
class KernelSlotView:
"""Write-through view over a Rust-backed slot."""
def __init__(self, kernel: "ExecutionKernel", slot_id: int) -> None:
object.__setattr__(self, "_kernel", kernel)
object.__setattr__(self, "_slot_id", int(slot_id))
@property
def slot_id(self) -> int:
return object.__getattribute__(self, "_slot_id")
def _snapshot(self) -> TradeSlot:
return self._kernel._get_slot(self.slot_id)
def __getattr__(self, name: str) -> Any:
slot = self._snapshot()
if hasattr(slot, name):
return getattr(slot, name)
raise AttributeError(name)
def __setattr__(self, name: str, value: Any) -> None:
if name in {"_kernel", "_slot_id"}:
object.__setattr__(self, name, value)
return
slot = self._snapshot()
if not hasattr(slot, name):
raise AttributeError(name)
setattr(slot, name, value)
self._kernel._set_slot(slot)
def to_dict(self) -> Dict[str, Any]:
return self._snapshot().to_dict()
def is_free(self) -> bool:
return self._snapshot().is_free()
def is_open(self) -> bool:
return self._snapshot().is_open()
def mark_price(self, price: float) -> None:
slot = self._snapshot()
slot.mark_price(price)
self._kernel._set_slot(slot)
def next_exit_ratio(self) -> float:
return self._snapshot().next_exit_ratio()
def consume_exit_leg(self) -> float:
slot = self._snapshot()
ratio = slot.consume_exit_leg()
self._kernel._set_slot(slot)
return ratio
def attach_entry_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_entry_order = order
self._kernel._set_slot(slot)
def attach_exit_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_exit_order = order
self._kernel._set_slot(slot)
def __repr__(self) -> str: # pragma: no cover - debugging helper
return f"KernelSlotView(slot_id={self.slot_id}, state={self._snapshot().fsm_state.value})"
class KernelStateView:
def __init__(self, kernel: "ExecutionKernel") -> None:
self._kernel = kernel
self.slots = [KernelSlotView(kernel, slot_id) for slot_id in range(kernel.max_slots)]
self.active_trade_index: Dict[str, int] = {}
self.venue_order_index: Dict[str, int] = {}
self.client_order_index: Dict[str, int] = {}
self.refresh()
def refresh(self) -> None:
snapshot = self._kernel._snapshot_backend()
self.active_trade_index = dict(snapshot.get("active_trade_index", {}))
self.venue_order_index = dict(snapshot.get("venue_order_index", {}))
self.client_order_index = dict(snapshot.get("client_order_index", {}))
class ExecutionKernel:
"""Rust-backed multi-slot execution kernel."""
def __init__(
self,
*,
max_slots: int = 10,
control_plane: Optional[ControlPlane] = None,
venue: Optional[VenueAdapter] = None,
journal: Optional[KernelJournal] = None,
account: Optional[AccountProjection] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> None:
self.max_slots = int(max_slots)
self.control_plane = control_plane or build_control_plane()
self.venue = venue or MockVenueAdapter()
self.journal = journal or MemoryKernelJournal()
self.account = account or AccountProjection()
self.projection = projection or build_projection(client=projection_client)
self.zinc_plane = zinc_plane or InMemoryZincPlane()
self._backend = _get_rust().create(self.max_slots)
self._control_snapshot = self.control_plane.read()
self.projection.write_control(self._control_snapshot)
self.zinc_plane.update_control(self._control_snapshot)
self.state = KernelStateView(self)
self.account.observe_slots([self._get_slot(slot_id) for slot_id in range(self.max_slots)])
def __del__(self) -> None: # pragma: no cover - cleanup best effort
backend = getattr(self, "_backend", None)
if backend is not None:
try:
_get_rust().destroy(backend)
except Exception:
pass
@property
def control(self) -> KernelControlSnapshot:
return self.control_plane.read()
def update_control(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.control_plane.update(update)
self._control_snapshot = snapshot
self.projection.write_control(snapshot)
self.zinc_plane.update_control(snapshot)
return snapshot
def _snapshot_backend(self) -> Dict[str, Any]:
return _get_rust().snapshot(self._backend)
def _get_slot(self, slot_id: int) -> TradeSlot:
return _slot_from_payload(_get_rust().get_slot_json(self._backend, slot_id))
def _set_slot(self, slot: TradeSlot, *, journal: bool = False) -> None:
payload = _slot_to_payload(slot)
_get_rust().set_slot_json(self._backend, slot.slot_id, payload)
self.state.refresh()
slots = [self._get_slot(slot_id) for slot_id in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
def slot(self, slot_id: int) -> KernelSlotView:
if not (0 <= int(slot_id) < self.max_slots):
raise IndexError(slot_id)
return self.state.slots[int(slot_id)]
def free_slot(self) -> Optional[KernelSlotView]:
for slot in self.state.slots:
if slot.is_free():
return slot
return None
def _record_transitions(self, transitions: Iterable[KernelTransition], slot: TradeSlot, event: Optional[VenueEvent]) -> None:
if self.control.debug_clickhouse_enabled:
for transition in transitions:
self.journal.record_transition(
transition=transition,
slot=slot,
event=event,
control=self.control,
)
def process_intent(self, intent: KernelIntent) -> KernelOutcome:
self.zinc_plane.publish_intent(intent)
if not (0 <= int(intent.slot_id) < self.max_slots):
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=TradeStage.IDLE,
diagnostic_code=KernelDiagnosticCode.INVALID_SLOT_ID,
details={"reason": "INVALID_SLOT_ID", "slot_id": int(intent.slot_id), "intent_id": intent.intent_id},
)
payload = _intent_to_payload(intent)
result = _get_rust().process_intent(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
emitted_events = []
if intent.action in {KernelCommandType.ENTER, KernelCommandType.EXIT}:
emitted_events = self.venue.submit(intent)
for event in emitted_events:
self.on_venue_event(event)
elif intent.action == KernelCommandType.CANCEL:
emitted_events = self.venue.cancel(self.slot(intent.slot_id).active_exit_order, reason=intent.reason) if self.slot(intent.slot_id).active_exit_order else []
for event in emitted_events:
self.on_venue_event(event)
final_slot = self._get_slot(outcome.slot_id)
rate_limit_event = next((event for event in emitted_events if event.kind == KernelEventKind.RATE_LIMITED), None)
if rate_limit_event is not None:
rate_limit_details = dict(outcome.details)
rate_limit_details.update(
{
"reason": rate_limit_event.reason or "RATE_LIMITED",
"retry_after_ms": int(rate_limit_event.metadata.get("retry_after_ms", 0) or 0),
"venue_event_kind": rate_limit_event.kind.value,
"severity": KernelSeverity.WARNING.value,
"release_eta": "few minutes",
"retryable": True,
}
)
outcome = KernelOutcome(
accepted=False,
slot_id=outcome.slot_id,
trade_id=outcome.trade_id,
state=final_slot.fsm_state,
diagnostic_code=KernelDiagnosticCode.RATE_LIMITED,
severity=KernelSeverity.WARNING,
transitions=outcome.transitions,
emitted_events=outcome.emitted_events,
details=rate_limit_details,
)
final_outcome = KernelOutcome(
accepted=outcome.accepted,
slot_id=outcome.slot_id,
trade_id=final_slot.trade_id,
state=final_slot.fsm_state,
diagnostic_code=outcome.diagnostic_code,
transitions=outcome.transitions,
emitted_events=tuple(emitted_events),
details=dict(outcome.details),
)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(final_slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, final_slot, None)
return final_outcome
def on_venue_event(self, event: VenueEvent) -> KernelOutcome:
result = _get_rust().on_venue_event(
self._backend,
_event_to_payload(event),
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
slot = _slot_from_payload(result["slot"])
self.state.refresh()
# Single capital mutation point: settle realiized PnL when a fill
# transitions the slot to a terminal closed state. This is the *only*
# place post-startup where capital is changed — no external balance
# polls overwrite it.
if slot.fsm_state in {TradeStage.CLOSED, TradeStage.TRADE_TERMINAL_WRITTEN} and slot.realized_pnl != 0.0:
self.account.settle(slot.realized_pnl)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, slot, event)
return outcome
def mark_price(self, asset: str, price: float) -> None:
for slot in self.state.slots:
if slot.asset == asset and slot.is_open():
slot.mark_price(price)
self.account.observe_slots([self._get_slot(i) for i in range(self.max_slots)])
def reconcile_from_slots(self, slots: Sequence[TradeSlot]) -> KernelOutcome:
payload = [_slot_to_payload(slot) for slot in slots]
result = _get_rust().reconcile_slots(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
for current in slots:
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
return outcome
def snapshot(self) -> Dict[str, Any]:
return {
"control": self.control.as_dict(),
"slots": [self._get_slot(slot.slot_id).to_dict() for slot in self.state.slots],
"account": {
"capital": self.account.snapshot.capital,
"equity": self.account.snapshot.equity,
"realized_pnl": self.account.snapshot.realized_pnl,
"unrealized_pnl": self.account.snapshot.unrealized_pnl,
"open_positions": self.account.snapshot.open_positions,
"open_notional": self.account.snapshot.open_notional,
"leverage": self.account.snapshot.leverage,
},
}

View File

@@ -1,14 +0,0 @@
[package]
name = "dita-v2-kernel"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
libc = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

File diff suppressed because it is too large Load Diff

View File

@@ -1,43 +0,0 @@
"""Utility helpers for the DITAv2 kernel."""
from __future__ import annotations
from dataclasses import asdict, is_dataclass
from datetime import datetime
from enum import Enum
from typing import Any
import json
import math
def safe_float(value: Any, default: float = 0.0) -> float:
"""Return a finite float or ``default``."""
try:
out = float(value)
except Exception:
return default
if not math.isfinite(out):
return default
return out
def json_safe(value: Any) -> Any:
"""Convert enums, dataclasses and datetimes to JSON-safe objects."""
if isinstance(value, Enum):
return value.value
if isinstance(value, datetime):
return value.isoformat()
if is_dataclass(value):
return json_safe(asdict(value))
if isinstance(value, dict):
return {str(key): json_safe(val) for key, val in value.items()}
if isinstance(value, list):
return [json_safe(item) for item in value]
if isinstance(value, tuple):
return [json_safe(item) for item in value]
return value
def json_text(value: Any) -> str:
"""Serialize a value using stable JSON settings."""
return json.dumps(json_safe(value), separators=(",", ":"), ensure_ascii=False, default=str)

View File

@@ -1,37 +0,0 @@
"""Venue adapter contracts for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional, Protocol
from .contracts import (
KernelCommandType,
KernelIntent,
KernelEventKind,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class VenueAdapter(Protocol):
"""Abstract venue adapter used by the kernel."""
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
...
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
...
def open_orders(self) -> List[VenueOrder]:
...
def open_positions(self) -> List[Dict[str, Any]]:
...
def reconcile(self) -> List[VenueEvent]:
...

View File

@@ -1,135 +0,0 @@
"""Python prototype of the Zinc hot-path plane.
This is an in-memory stand-in for the eventual Zinc-backed shared memory
regions. The interface is explicit so the implementation can be swapped later
without touching the kernel logic.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Mapping, Optional, Protocol
import threading
import time
from .contracts import KernelIntent, TradeSlot
from .control import KernelControlSnapshot
class ZincPlane(Protocol):
"""Hot-path plane for intents, state and control."""
def publish_intent(self, intent: KernelIntent) -> None:
...
def write_slot(self, slot: TradeSlot) -> None:
...
def read_slots(self) -> List[TradeSlot]:
...
def update_control(self, control: KernelControlSnapshot) -> None:
...
def read_control(self) -> KernelControlSnapshot:
...
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
...
def notify_intent(self) -> None:
...
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
...
def notify_state(self) -> None:
...
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
...
def notify_control(self) -> None:
...
@dataclass
class InMemoryZincPlane:
"""Simple in-memory Zinc lookalike for Python prototype tests."""
intent_region: List[KernelIntent] = field(default_factory=list)
state_region: Dict[int, TradeSlot] = field(default_factory=dict)
control_region: Optional[KernelControlSnapshot] = None
_intent_seq: int = field(default=0, init=False, repr=False)
_state_seq: int = field(default=0, init=False, repr=False)
_control_seq: int = field(default=0, init=False, repr=False)
_intent_observed_seq: int = field(default=0, init=False, repr=False)
_state_observed_seq: int = field(default=0, init=False, repr=False)
_control_observed_seq: int = field(default=0, init=False, repr=False)
_signal: threading.Condition = field(default_factory=threading.Condition, init=False, repr=False)
def publish_intent(self, intent: KernelIntent) -> None:
with self._signal:
self.intent_region.append(intent)
self._intent_seq += 1
self._signal.notify_all()
def write_slot(self, slot: TradeSlot) -> None:
with self._signal:
self.state_region[int(slot.slot_id)] = slot
self._state_seq += 1
self._signal.notify_all()
def read_slots(self) -> List[TradeSlot]:
return [self.state_region[key] for key in sorted(self.state_region)]
def update_control(self, control: KernelControlSnapshot) -> None:
with self._signal:
self.control_region = control
self._control_seq += 1
self._signal.notify_all()
def read_control(self) -> KernelControlSnapshot:
if self.control_region is None:
return KernelControlSnapshot()
return self.control_region
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_intent_seq", "_intent_observed_seq", timeout_ms)
def notify_intent(self) -> None:
with self._signal:
self._intent_seq += 1
self._signal.notify_all()
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_state_seq", "_state_observed_seq", timeout_ms)
def notify_state(self) -> None:
with self._signal:
self._state_seq += 1
self._signal.notify_all()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_control_seq", "_control_observed_seq", timeout_ms)
def notify_control(self) -> None:
with self._signal:
self._control_seq += 1
self._signal.notify_all()
def _wait_for_change(self, seq_attr: str, observed_attr: str, timeout_ms: int) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = getattr(self, observed_attr)
while getattr(self, seq_attr) == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
setattr(self, observed_attr, getattr(self, seq_attr))
return True

View File

@@ -1,337 +0,0 @@
import sys, re
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# ===== Collect all existing body names =====
existing_bodies = re.findall(r'async def _body_(\w+)', content)
seen = set()
unique_bodies = []
for b in existing_bodies:
if b not in seen:
seen.add(b)
unique_bodies.append(b)
print(f"Existing: {len(unique_bodies)} bodies")
# ===== New bodies =====
new_bodies = []
new_params = []
def B(name, lines):
new_bodies.append(f"async def _body_{name}(k, symbol, p):\n")
for l in lines:
new_bodies.append(f" {l}\n")
new_params.append(f' pytest.param("{name}", _body_{name}, id="{name}"),')
# ===== 1. Real reconcile: fresh kernel from old slot state =====
B("fresh_kernel_reconcile_entry", [
'tid = f"fk-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Snapshot slot state, build fresh kernel, reconcile",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# The fresh kernel should see the same slot state",
"s = k2.slot(0)",
'assert not s.is_free(), f"fresh kernel slot should not be free: {s.fsm_state}"',
"assert s.trade_id == tid, f\"trade_id mismatch: {s.trade_id} vs {tid}\"",
"# Exit on the fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"assert k2.slot(0).is_free(), \"fresh kernel slot not free after exit\"",
"# Original kernel capital should match",
'assert abs(k2.account.snapshot.capital - cb) < 0.01, f"capital drift: {k2.account.snapshot.capital} vs {cb}"',
])
B("fresh_kernel_reconcile_after_cancel", [
'tid = f"fkc-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'r = _si(k, E.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
"# Reconcile onto fresh kernel from cancelled state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Cancelled slot should be free",
'assert k2.slot(0).is_free(), f"cancelled slot not free: {k2.slot(0).fsm_state}"',
])
B("fresh_kernel_reconcile_after_exit", [
'tid = f"fkx-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"# Reconcile onto fresh kernel from closed state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
'assert k2.slot(0).is_free(), f"closed slot not free: {k2.slot(0).fsm_state}"',
'assert k2.slot(0).closed, "slot should be marked closed"',
])
B("fresh_kernel_reconcile_partial_exit", [
'tid = f"fkp-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"# Reconcile mid-trade (one leg exited, one remaining)",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Remaining leg should still be open",
's = k2.slot(0)',
'assert not s.is_free(), f"partial-exit slot should not be free: {s.fsm_state}"',
'assert s.realized_pnl != 0 or s.size > 0, "partial-exit slot should have remaining position or realized PnL"',
"# Exit remaining leg on fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(1.0,)); await asyncio.sleep(0.5)",
'assert k2.slot(0).is_free(), "slot not free after final exit on fresh kernel"',
])
# ===== 2. Cross-slot portfolio accounting =====
B("cross_slot_portfolio_short_long", [
't0 = f"psl0-{int(__import__(\"time\").time()*1000)}"',
't1 = f"psl1-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital",
"_si(k, E.ENTER, t0, symbol, 'SHORT', p, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"# Verify both slots are open",
'assert not k.slot(0).is_free(), "slot 0 should be open"',
'assert not k.slot(1).is_free(), "slot 1 should be open"',
"# Verify PnL tracking per slot",
"rp0 = k.slot(0).realized_pnl; up0 = k.slot(0).unrealized_pnl",
"rp1 = k.slot(1).realized_pnl; up1 = k.slot(1).unrealized_pnl",
"expected = cb + rp0 + up0 + rp1 + up1",
"actual = k.account.snapshot.capital",
'assert abs(actual - expected) < 0.01, f"portfolio misalignment: cap={actual} expected={expected} rp0={rp0} up0={up0} rp1={rp1} up1={up1}"',
"# Exit slot 0",
"_si(k, E.EXIT, t0, symbol, 'SHORT', p*0.995, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"assert k.slot(0).is_free(), \"slot 0 should be free after exit\"",
"# Exit slot 1",
"_si(k, E.EXIT, t1, symbol, 'LONG', p*1.005, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"assert k.slot(1).is_free(), \"slot 1 should be free after exit\"",
])
# ===== 3. KernelOutcome inspection =====
B("outcome_inspect_entry", [
'tid = f"oi-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Inspect outcome of ENTER",
"_assert_accepted(r, 'entry')",
"info = _inspect_outcome(r, 'entry')",
'assert r.accepted, f"entry not accepted: {info}"',
'assert r.trade_id == tid, f"trade_id mismatch: {r.trade_id} vs {tid}"',
'assert r.slot_id == 0, f"slot_id: {r.slot_id}"',
"# transitions should exist",
'assert len(info["transitions"]) > 0, f"no transitions in outcome: {info}"',
'assert info["diagnostic"] == "OK", f"diagnostic not OK: {info}"',
"# Exit and inspect",
'r2 = _si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
"_assert_accepted(r2, 'exit')",
'info2 = _inspect_outcome(r2, "exit")',
'assert len(info2["transitions"]) > 0, f"no exit transitions: {info2}"',
'assert info2["diagnostic"] == "OK", f"exit diagnostic: {info2}"',
])
B("outcome_inspect_rejection", [
'tid = f"or-{int(__import__(\"time\").time()*1000)}"',
'tid2 = f"or2-{int(__import__(\"time\").time()*1000)}"',
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'first entry')",
"# Second entry on same slot should be SLOT_BUSY",
"r2 = _si(k, E.ENTER, tid2, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r2, 'SLOT_BUSY', 'double entry')",
"# Verify transition trace shows the rejection",
"info = _inspect_outcome(r2, 'double entry')",
'assert not r2.accepted, f"second entry should be rejected: {info}"',
"# Exit normally",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
B("outcome_inspect_exit_on_idle", [
'tid = f"oei-{int(__import__(\"time\").time()*1000)}"',
"# Exit on idle slot",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r, 'INVALID_FSM_TRANSITION', 'exit on idle')",
'info = _inspect_outcome(r, "exit on idle")',
'assert not r.accepted, f"exit on idle should be rejected: {info}"',
"# Then do a normal trade",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 4. Duplicate event dedup =====
B("dedup_duplicate_fill_event", [
'tid = f"dd-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'entry')",
"# Inject a duplicate FULL_FILL VenueEvent manually",
"# Build an event that mirrors the slot's current active order",
"sl = k.slot(0)",
'ao = sl.active_entry_order if sl.active_entry_order else sl.active_exit_order',
"if ao:",
" dup = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="dedup-test-99999",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id,",
" venue_client_id=ao.venue_client_id,",
" side=sl.side,",
" asset=symbol,",
" price=p,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="dedup_test",',
" )",
" r2 = k.on_venue_event(dup)",
" _assert_accepted(r2, 'dedup_fill')",
' info = _inspect_outcome(r2, "dedup_fill")',
' assert len(info["event_kinds"]) == 0 or info["event_kinds"] == ["ORDER_ACK"], f"duplicate fill should produce no events: {info}"',
"# Exit",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 5. Fill-price divergence =====
B("fill_price_divergence_1pct", [
'tid = f"fd-{int(__import__(\"time\").time()*1000)}"',
"# Enter SHORT at market",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Force the kernel's slot to see a divergent fill price via on_venue_event replay",
"sl = k.slot(0)",
'ao = sl.active_entry_order',
"if ao and sl.fsm_state not in ('IDLE', 'CLOSED'):",
" divergent_price = p * 1.01 # 1% worse than reference",
" div_event = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="divergence-test",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id if ao else \"\"," ,
" venue_client_id=ao.venue_client_id if ao else \"\"," ,
" side=sl.side,",
" asset=symbol,",
" price=divergent_price,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="divergence_test",',
" )",
" k.on_venue_event(div_event); await asyncio.sleep(0.3)",
"# Exit at market",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 6. Negative-capital boundary =====
B("neg_cap_entry_rejected", [
'tid = f"nc-{int(__import__(\"time\").time()*1000)}"',
"# Kernel should reject ENTER if capital cannot cover margin",
"# With tiny capital, even a tiny trade should be checked",
"k.account.snapshot.capital = 0.0",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'info = _inspect_outcome(r, "neg_cap")',
'# May be rejected or accepted depending on kernel margin logic',
'# At minimum, kernel should not crash',
"# Restore capital and do normal trade",
"k.account.snapshot.capital = 25000.0",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 7. Sub-sample cross-application =====
# Apply the new assertion patterns to a basic entry/exit
B("cross_sample_basic_entry_exit_outcome", [
'tid = f"cs-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r1, 'cs_entry')",
"_check_slot_accounting(k, 'cs_after_entry')",
"r2 = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r2, 'cs_exit')",
"_check_slot_accounting(k, 'cs_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs: cap shrunk {cb} -> {ca}"',
])
B("cross_sample_cancel_reenter_outcome", [
't1 = f"csc-{int(__import__(\"time\").time()*1000)}"',
't2 = f"csc2-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'cs_cancel_entry')",
"r2 = _si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if r2.accepted:",
' info = _inspect_outcome(r2, "cs_cancel")',
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_check_slot_accounting(k, 'cs_after_cancel')",
'assert k.slot(0).is_free(), "slot should be free after cancel"',
"r3 = _si(k, E.ENTER, t2, symbol, 'SHORT', p*0.997, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r3, 'cs_reenter')",
"_check_slot_accounting(k, 'cs_after_reenter')",
"r4 = _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r4, 'cs_reenter_exit')",
"_check_slot_accounting(k, 'cs_after_reenter_exit')",
])
B("cross_sample_multi_leg_outcome", [
'tid = f"csm-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'cs_ml_entry')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg1')",
"_check_slot_accounting(k, 'cs_ml_after_leg1')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg2')",
"_check_slot_accounting(k, 'cs_ml_after_leg2')",
])
B("cross_sample_leverage_tight_bounds", [
'tid = f"csl-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r_ent = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001, leverage=2); await asyncio.sleep(0.8)",
"_assert_accepted(r_ent, 'cs_lev_entry')",
"_check_slot_accounting(k, 'cs_lev_after_entry')",
"r_ex = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, leverage=2); await asyncio.sleep(0.5)",
"_assert_accepted(r_ex, 'cs_lev_exit')",
"_check_slot_accounting(k, 'cs_lev_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs_lev: cap shrunk {cb} -> {ca}"',
])
# ===== BUILD =====
body_block = "".join(new_bodies)
param_block = "\n".join(new_params)
# Insert new bodies before SCENARIOS marker
marker = "SCENARIOS = ["
idx = content.index(marker)
# Insert after the last body section ends (blank line before SCENARIOS)
tail_start = content.rindex("\n\n", 0, idx) + 2
head = content[:tail_start]
tail = content[tail_start:]
with_bodies = head + body_block + tail
# Find SCENARIOS closing bracket and append new param entries
scenarios_open = with_bodies.index(marker)
close_bracket = with_bodies.index("]", scenarios_open)
final = with_bodies[:close_bracket] + "\n" + param_block + "\n" + with_bodies[close_bracket:]
# Compact blank lines
final = re.sub(r'\n{3,}', '\n\n', final)
with open(fpath, 'w') as f:
f.write(final)
import py_compile
py_compile.compile(fpath, doraise=True)
body_count = final.count("async def _body_")
param_count = final.count("pytest.param(")
print(f"Bodies: {body_count}, Params: {param_count}")
print("Parts 5: Compiles OK")

View File

@@ -1,170 +0,0 @@
import sys
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# === PART 1: Expand imports ===
old_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
new_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
VenueEvent, VenueEventStatus, KernelEventKind,
TradeStage, KernelDiagnosticCode, KernelSeverity,
KernelOutcome, KernelTransition, TradeSlot, VenueOrder,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
content = content.replace(old_imports, new_imports)
print("1: imports OK")
# === PART 2: Expand _build_rb with helpers ===
old_build = "def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:\n cfg = _build_config(ic)\n b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)\n k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic\n class Shim:\n def __init__(self, k): self.kernel = k\n async def connect(self, initial_capital=0): self.kernel.venue.connect()\n async def disconnect(self):\n try: self.kernel.venue.disconnect()\n except: pass\n return RB(runtime=Shim(k), config=cfg)"
new_build = """def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
def _build_portfolio_rb(ic: float = 25000.0, max_slots: int = 2) -> RB:
return _build_rb(ic=ic, max_slots=max_slots)
def _inspect_outcome(r, label):
info = {
\"accepted\": r.accepted,
\"state\": r.state.value if r.state else \"\",
\"diagnostic\": r.diagnostic_code.value if r.diagnostic_code else \"\",
\"severity\": r.severity.value if r.severity else \"\",
\"transitions\": [(t.prev_state.value, t.next_state.value) for t in (r.transitions or ())],
\"event_kinds\": [e.kind.value for e in (r.emitted_events or ())],
\"details\": dict(r.details or {}),
}
return info
def _assert_accepted(r, label):
info = _inspect_outcome(r, label)
assert r.accepted, f\"{label}: intent rejected - diag={info['diagnostic']} state={info['state']} detail={info['details']}\"
def _assert_rejected(r, expected_diag, label):
info = _inspect_outcome(r, label)
assert not r.accepted, f\"{label}: expected rejection but got accepted state={info['state']}\"
assert info['diagnostic'] == expected_diag, f\"{label}: expected diag={expected_diag} got {info['diagnostic']} detail={info['details']}\"
def _check_slot_accounting(k, label):
start_cap = getattr(k, '_start_cap', None)
if start_cap is None:
return
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
total_up = sum(k.slot(i).unrealized_pnl for i in range(k.max_slots))
expected = start_cap + total_rp + total_up
actual = k.account.snapshot.capital
diff = abs(actual - expected)
assert diff < 0.01, f\"{label}: accounting mismatch cap={actual} exp={expected} rp={total_rp} upnl={total_up} diff={diff}\"
def _check_open_orders(c, vs):
r = __import__('asyncio').run(c._request_json(
\"GET\", \"/openApi/swap/v2/trade/openOrders\",
{\"symbol\": vs}, signed=True
))
data = r if isinstance(r, list) else (r.get(\"data\") or r.get(\"orders\") or [])
return [o for o in data if isinstance(o, dict)]
async def _verify_full(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]
ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)
flat = ts < 1e-8
oos = _check_open_orders(c, vs)
no_orders = len(oos) == 0
err = \"\"
if not flat: err += f\"pos_open: {tr} \"
if not no_orders: err += f\"open_orders: {oos} \"
return {\"symbol\": vs, \"flat\": flat, \"no_orders\": no_orders, \"error\": err.strip()}
def _build_fresh_kernel_from_slot(slot_data, ic=25000.0):
from prod.clean_arch.dita_v2.rust_backend import _slot_from_payload
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
restored = _slot_from_payload(slot_data)
k.reconcile_from_slots([restored])
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)"""
content = content.replace(old_build, new_build)
print("2: build/helpers OK")
# === PART 3: Update _verify to check open orders ===
old_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n return VR(symbol=vs, positions_flat=flat, error=\"\" if flat else f\"open: {tr}\")"
new_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n oos = _check_open_orders(c, vs)\n no_orders = len(oos) == 0\n err = \"\"\n if not flat: err += f\"pos_open: {tr} \"\n if not no_orders: err += f\"open_orders: {oos} \"\n return VR(symbol=vs, positions_flat=flat and no_orders, error=err.strip())"
content = content.replace(old_verify, new_verify)
print("3: verify OK")
# === PART 4: Replace _run ===
# Find old _run and replace
old_run_pat = "async def _run(bundle, client, body_fn, label, ic):"
# Find the entire old run function bounds
idx = content.index(old_run_pat)
run_end = content.index(" finally:", idx)
run_end = content.index("\n\n", run_end) + 2
new_run = """async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-pre-{si}")
await asyncio.sleep(0.3)
k._start_cap = k.account.snapshot.capital
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
max_change = max(1.0, cb * 0.10)
assert cb - ca < max_change, f"Capital shrunk beyond tolerance: {cb} -> {ca} (limit={max_change})"
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
if abs(total_rp) > 0.0001:
assert abs(total_rp) < abs(cb - ca) + 0.01, f"{label}: rp={total_rp} != cap_change={cb-ca}"
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-post-{si}")
await asyncio.sleep(1.0)
_throttle(3.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
"""
content = content[:idx] + new_run + content[run_end:]
print("4: run OK")
with open(fpath, 'w') as f:
f.write(content)
import py_compile
py_compile.compile(fpath, doraise=True)
print("Parts 1-4: Compiles OK")

File diff suppressed because it is too large Load Diff

View File

@@ -1 +0,0 @@
/target

View File

@@ -1,387 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "bumpalo"
version = "3.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
[[package]]
name = "cc"
version = "1.2.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chrono"
version = "0.4.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "dita-v2-kernel"
version = "0.1.0"
dependencies = [
"chrono",
"libc",
"serde",
"serde_json",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "futures-core"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
[[package]]
name = "futures-task"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
[[package]]
name = "futures-util"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"slab",
]
[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "js-sys"
version = "0.3.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "142bc4740e452c1e57ade0cbc129f139c9093e354346f0872ef985f4f5cf5f11"
dependencies = [
"cfg-if",
"futures-util",
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "log"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5"
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
[[package]]
name = "syn"
version = "2.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "wasm-bindgen"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed04576f974d2b2fba0f38c51dbc5518011e38c36bf1143164be765528fd409"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "916151b09da36bd82f6615cbf3a419e2f0ba23a03c6160e8e92eb6bd4aa1dec6"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "299047362ccbfce148b67ab7e73349f77748e00c8296f9542adfad2ad82c5c5e"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a929b2c61f11ba3e9bc35b50c1f25cb38e0e892c0c231ae2b8cf78d5dad4437"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

View File

@@ -1,14 +0,0 @@
[package]
name = "dita-v2-kernel"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
libc = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,10 @@ from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
from enum import Enum
from typing import Any, Dict, Iterable, List, Optional
import math
import time
from .contracts import TradeSide, TradeSlot, TradeStage
from .utils import safe_float
@@ -121,3 +123,387 @@ class AccountProjection:
"bars_held": int(bars_held),
"metadata": dict(metadata or {}),
}
# ---------------------------------------------------------------------------
# V2 — Dual-ledger, event-sourced, reconciled account (spec G2)
# ---------------------------------------------------------------------------
class ReconcileStatus(str, Enum):
OK = "OK"
WARN = "WARN"
ERROR = "ERROR"
@dataclass(frozen=True)
class KBlock:
"""Kernel-computed values — derived deterministically from the E-fact stream."""
capital: float = 0.0 # seed + Σrealized − Σfee − Σfunding
realized_pnl: float = 0.0
unrealized_pnl: float = 0.0
fees_paid: float = 0.0
funding_paid: float = 0.0
open_notional: float = 0.0 # Σ|qty|·mark
equity: float = 0.0 # capital + unrealized
used_margin: float = 0.0 # Σ notional/leverage
available_margin: float = 0.0 # capital − used_margin
open_positions: int = 0
peak_capital: float = 0.0
@dataclass(frozen=True)
class EPosition:
"""Single open position as reported by the exchange."""
symbol: str = ""
qty: float = 0.0
entry_price: float = 0.0
mark_price: float = 0.0
unrealized_pnl: float = 0.0
leverage: float = 1.0
side: str = ""
@dataclass(frozen=True)
class EBlock:
"""Exchange facts — values only the exchange can know."""
wallet_balance: float = 0.0
available_margin: float = 0.0
used_margin: float = 0.0
maint_margin: float = 0.0
positions: tuple = () # tuple[EPosition, ...]
last_fill_price: float = 0.0
last_fill_qty: float = 0.0
last_fill_fee: float = 0.0
last_fill_realized_pnl: float = 0.0
last_funding: float = 0.0
@dataclass(frozen=True)
class ReconcileResult:
"""Classification of K-vs-E divergence for one snapshot."""
status: ReconcileStatus = ReconcileStatus.OK
deltas: Dict[str, float] = field(default_factory=dict)
explanations: List[str] = field(default_factory=list)
worst_field: str = ""
ts: float = 0.0
def __post_init__(self) -> None:
# frozen dataclass — use object.__setattr__ only in __post_init__
if not isinstance(self.deltas, dict):
object.__setattr__(self, "deltas", {})
if not isinstance(self.explanations, list):
object.__setattr__(self, "explanations", [])
@dataclass(frozen=True)
class AccountSnapshotV2:
"""
Immutable versioned snapshot — the atomic unit of account truth.
Each exchange event produces exactly one new snapshot; readers hold
a reference and are never exposed to a partially-updated state.
"""
event_seq: int
source_event_id: str
k: KBlock
e: EBlock
reconcile: ReconcileResult
ts: float = 0.0
@dataclass
class ReconcileConfig:
"""
Bounds for the R1–R6 reconcile rules. All values are config-driven;
no magic numbers in the classifier itself.
"""
capital_epsilon: float = 1e-4 # |δ| < ε → OK (R1, absolute USDT)
pending_fee_bound: float = 20.0 # max unsettled fees still in-flight (R1)
realized_rounding: float = 0.05 # fee+rounding tolerance for R2
lot_step: float = 0.001 # position qty lot-step for R3
mark_staleness_factor: float = 0.003 # 0.3% mark-price drift tolerance (R4)
leverage_rounding_band: float = 2.0 # margin rounding band USDT (R5)
def _safe(v: Any, default: float = 0.0) -> float:
try:
f = float(v)
return f if math.isfinite(f) else default
except (TypeError, ValueError):
return default
class AccountProjectionV2:
"""
Dual-ledger account — tracks K-values (kernel fold) and E-facts
(exchange push) independently, reconciles each event, and publishes
immutable AccountSnapshotV2 instances.
Thread-safety note: Python's GIL makes reference replacement of
`_snapshot` atomic for single-field reads. For multi-field consistency
callers must hold `_snapshot` locally: `snap = proj.snapshot`.
"""
def __init__(
self,
seed_capital: float,
*,
min_capital: float = 0.0,
max_capital: Optional[float] = None,
reconcile_config: Optional[ReconcileConfig] = None,
) -> None:
self._seed = _safe(seed_capital, 0.0)
self._min_capital = min_capital
self._max_capital = max_capital
self._cfg = reconcile_config or ReconcileConfig()
# Running K-value accumulators
self._k_realized: float = 0.0
self._k_fees: float = 0.0
self._k_funding: float = 0.0
self._peak_capital: float = self._seed
# Latest E-facts (mutable intermediate; frozen into EBlock at snapshot time)
self._e_wallet_balance: float = 0.0
self._e_avail_margin: float = 0.0
self._e_used_margin: float = 0.0
self._e_maint_margin: float = 0.0
self._e_positions: List[EPosition] = []
self._e_last_fill_price: float = 0.0
self._e_last_fill_qty: float = 0.0
self._e_last_fill_fee: float = 0.0
self._e_last_fill_realized: float = 0.0
self._e_last_funding: float = 0.0
self._event_seq: int = 0
self._snapshot: AccountSnapshotV2 = self._build(0, "", [], time.time())
# ------------------------------------------------------------------
# E-fact ingestion (called from WS event handlers)
# ------------------------------------------------------------------
def apply_fill(
self,
*,
fill_price: float,
fill_qty: float,
fee: float,
realized_pnl: float,
) -> None:
self._k_realized += _safe(realized_pnl)
self._k_fees += _safe(fee)
self._e_last_fill_price = _safe(fill_price)
self._e_last_fill_qty = _safe(fill_qty)
self._e_last_fill_fee = _safe(fee)
self._e_last_fill_realized = _safe(realized_pnl)
def apply_funding(self, amount: float) -> None:
self._k_funding += _safe(amount)
self._e_last_funding = _safe(amount)
def apply_balance_update(
self,
*,
wallet_balance: float,
available_margin: float,
used_margin: float,
maint_margin: float,
) -> None:
self._e_wallet_balance = _safe(wallet_balance)
self._e_avail_margin = _safe(available_margin)
self._e_used_margin = _safe(used_margin)
self._e_maint_margin = _safe(maint_margin)
def apply_position_update(self, positions: List[EPosition]) -> None:
self._e_positions = list(positions)
# ------------------------------------------------------------------
# Snapshot construction (called after each ingestion step)
# ------------------------------------------------------------------
def build_snapshot(
self,
source_event_id: str,
slots: Iterable[TradeSlot],
ts: Optional[float] = None,
) -> AccountSnapshotV2:
self._event_seq += 1
snap = self._build(self._event_seq, source_event_id, list(slots), ts or time.time())
self._snapshot = snap
return snap
@property
def snapshot(self) -> AccountSnapshotV2:
return self._snapshot
@property
def k_capital(self) -> float:
raw = self._seed + self._k_realized - self._k_fees - self._k_funding
if self._max_capital is not None:
raw = min(raw, self._max_capital)
return max(self._min_capital, raw)
# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------
def _build(
self,
event_seq: int,
source_event_id: str,
slots: List[TradeSlot],
ts: float,
) -> AccountSnapshotV2:
open_notional, unrealized, used_margin, open_positions = self._scan_slots(slots)
capital = self.k_capital
self._peak_capital = max(self._peak_capital, capital)
k = KBlock(
capital=capital,
realized_pnl=self._k_realized,
unrealized_pnl=unrealized,
fees_paid=self._k_fees,
funding_paid=self._k_funding,
open_notional=open_notional,
equity=capital + unrealized,
used_margin=used_margin,
available_margin=max(0.0, capital - used_margin),
open_positions=open_positions,
peak_capital=self._peak_capital,
)
e = EBlock(
wallet_balance=self._e_wallet_balance,
available_margin=self._e_avail_margin,
used_margin=self._e_used_margin,
maint_margin=self._e_maint_margin,
positions=tuple(self._e_positions),
last_fill_price=self._e_last_fill_price,
last_fill_qty=self._e_last_fill_qty,
last_fill_fee=self._e_last_fill_fee,
last_fill_realized_pnl=self._e_last_fill_realized,
last_funding=self._e_last_funding,
)
reconcile = self._classify(k, e, ts)
return AccountSnapshotV2(
event_seq=event_seq,
source_event_id=source_event_id,
k=k,
e=e,
reconcile=reconcile,
ts=ts,
)
def _scan_slots(
self, slots: List[TradeSlot]
) -> tuple: # (open_notional, unrealized, used_margin, open_count)
open_notional = 0.0
unrealized = 0.0
used_margin = 0.0
open_positions = 0
for slot in slots:
if slot.closed or slot.size <= 0:
continue
if slot.fsm_state not in {
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
}:
continue
open_positions += 1
mark = _safe(slot.metadata.get("mark_price") if slot.metadata else None, 0.0)
if mark <= 0.0:
mark = _safe(slot.entry_price, 0.0)
notional = abs(slot.size) * mark
open_notional += notional
unrealized += _safe(slot.unrealized_pnl)
lev = max(1.0, _safe(slot.metadata.get("leverage") if slot.metadata else None, 1.0))
used_margin += notional / lev
return open_notional, unrealized, used_margin, open_positions
def _classify(self, k: KBlock, e: EBlock, ts: float) -> ReconcileResult:
"""
Apply reconcile rules R1–R6 (spec §2.3).
Returns a ReconcileResult with the worst status seen across all fields.
"""
cfg = self._cfg
status = ReconcileStatus.OK
deltas: Dict[str, float] = {}
explanations: List[str] = []
worst_field = ""
def _escalate(new: ReconcileStatus, field: str) -> None:
nonlocal status, worst_field
order = {ReconcileStatus.OK: 0, ReconcileStatus.WARN: 1, ReconcileStatus.ERROR: 2}
if order[new] > order[status]:
status = new
worst_field = field
# R1: capital vs wallet balance (only meaningful when E-facts are populated)
if e.wallet_balance > 0:
delta_r1 = abs(k.capital - e.wallet_balance)
deltas["capital_vs_wallet"] = k.capital - e.wallet_balance
if delta_r1 <= cfg.capital_epsilon:
pass # OK
elif delta_r1 <= cfg.pending_fee_bound:
_escalate(ReconcileStatus.WARN, "capital_vs_wallet")
explanations.append(f"UNSETTLED_FEE|capital_vs_wallet|delta={delta_r1:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "capital_vs_wallet")
explanations.append(f"ERROR|capital_vs_wallet|delta={delta_r1:.4f}")
# R2: realized PnL vs exchange realized
if e.last_fill_realized_pnl != 0:
delta_r2 = abs(k.realized_pnl - e.last_fill_realized_pnl)
deltas["realized_pnl"] = k.realized_pnl - e.last_fill_realized_pnl
if delta_r2 <= cfg.capital_epsilon:
pass
elif delta_r2 <= cfg.realized_rounding:
_escalate(ReconcileStatus.WARN, "realized_pnl")
explanations.append(f"LOT_STEP_ROUNDING|realized_pnl|delta={delta_r2:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "realized_pnl")
explanations.append(f"ERROR|realized_pnl|delta={delta_r2:.4f}")
# R3: position count (R6) + per-position qty (R3)
e_pos_map = {p.symbol: p for p in e.positions}
if len(e.positions) > 0:
if k.open_positions != len(e_pos_map):
deltas["open_positions"] = float(k.open_positions - len(e_pos_map))
_escalate(ReconcileStatus.ERROR, "open_positions")
explanations.append(
f"ERROR|open_positions|k={k.open_positions}|e={len(e_pos_map)}"
)
# R4: open_notional vs exchange notional (mark staleness)
if e.used_margin > 0 and k.open_notional > 0:
delta_notional = abs(k.open_notional - e.used_margin)
deltas["open_notional"] = k.open_notional - e.used_margin
staleness_band = k.open_notional * cfg.mark_staleness_factor
if delta_notional <= cfg.capital_epsilon:
pass
elif delta_notional <= staleness_band:
_escalate(ReconcileStatus.WARN, "open_notional")
explanations.append(f"MARK_PRICE_STALENESS|open_notional|delta={delta_notional:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "open_notional")
explanations.append(f"ERROR|open_notional|delta={delta_notional:.4f}")
# R5: used/available margin
if e.used_margin > 0:
delta_margin = abs(k.used_margin - e.used_margin)
deltas["used_margin"] = k.used_margin - e.used_margin
if delta_margin <= cfg.capital_epsilon:
pass
elif delta_margin <= cfg.leverage_rounding_band:
_escalate(ReconcileStatus.WARN, "used_margin")
explanations.append(f"LEVERAGE_ROUNDING|used_margin|delta={delta_margin:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "used_margin")
explanations.append(f"ERROR|used_margin|delta={delta_margin:.4f}")
return ReconcileResult(
status=status,
deltas=deltas,
explanations=explanations,
worst_field=worst_field,
ts=ts,
)

View File

@@ -1,602 +0,0 @@
"""DITAv2 BingX venue adapter.
This is a thin normalization layer over the existing direct BingX execution
surface. It converts BingX REST/account/order payloads into DITAv2
``VenueEvent`` / ``VenueOrder`` objects without reimplementing exchange logic.
"""
from __future__ import annotations
import asyncio
import concurrent.futures
import inspect
import itertools
import re
import threading
from datetime import datetime, timezone
from typing import Any, Iterable, List, Optional
from prod.clean_arch.dita import DecisionAction as LegacyDecisionAction
from prod.clean_arch.dita import Intent as LegacyIntent
from prod.clean_arch.dita import TradeSide as LegacyTradeSide
from prod.bingx.http import BingxHttpError
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .utils import json_safe
from .utils import safe_float
from .venue import VenueAdapter
def _row_text(row: dict[str, Any], *keys: str, default: str = "") -> str:
for key in keys:
value = row.get(key)
if value is None:
continue
text = str(value)
if text:
return text
return default
def _row_float(row: dict[str, Any], *keys: str, default: float = 0.0) -> float:
for key in keys:
try:
value = float(row.get(key) or 0.0)
except Exception:
continue
if value == value and value not in (float("inf"), float("-inf")) and value != 0.0:
return value
return default
def _normalize_status(status: str) -> str:
return str(status or "").strip().upper()
def _trade_side_from_row(row: dict[str, Any], *, fallback: TradeSide = TradeSide.FLAT) -> TradeSide:
side_raw = _row_text(row, "side", "positionSide", default="").upper()
signed_qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if side_raw in {"BUY", "LONG"}:
return TradeSide.LONG
if side_raw in {"SELL", "SHORT"}:
return TradeSide.SHORT
if signed_qty < 0:
return TradeSide.SHORT
if signed_qty > 0:
return TradeSide.LONG
return fallback
def _venue_event_status_from_row(status: str) -> VenueEventStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueEventStatus.ACKED
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueEventStatus.RATE_LIMITED
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueEventStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueEventStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueEventStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueEventStatus.REJECTED
if normalized in {"CANCEL_REJECTED", "CANCEL_REJECT"}:
return VenueEventStatus.CANCELED_REJECTED
return VenueEventStatus.ACKED
def _venue_order_status_from_row(status: str) -> VenueOrderStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueOrderStatus.NEW
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueOrderStatus.NEW
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueOrderStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueOrderStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueOrderStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueOrderStatus.REJECTED
return VenueOrderStatus.NEW
def _position_qty(row: dict[str, Any]) -> float:
qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if qty != 0.0:
return abs(qty)
return abs(_row_float(row, "executedQty", "filledQty", "z", default=0.0))
def _position_price(row: dict[str, Any]) -> float:
return _row_float(row, "entryPrice", "avgPrice", "avgEntryPrice", "ep", "ap", "price", "lastFillPrice", "tradePrice")
def _mapping_for_snapshot(rows: Iterable[dict[str, Any]]) -> dict[str, dict[str, Any]]:
mapping: dict[str, dict[str, Any]] = {}
for row in rows:
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
key = client_id or order_id
if key:
mapping[key] = dict(row)
if order_id and order_id not in mapping:
mapping[order_id] = dict(row)
return mapping
def _venue_order_from_row(
row: dict[str, Any],
*,
internal_trade_id: str = "",
fallback_side: TradeSide = TradeSide.FLAT,
) -> VenueOrder:
side = _trade_side_from_row(row, fallback=fallback_side)
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
intended = _row_float(row, "origQty", "quantity", "q", "positionAmt", "positionQty", default=0.0)
if intended <= 0:
intended = _position_qty(row)
return VenueOrder(
internal_trade_id=internal_trade_id or client_id or order_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=side,
intended_size=abs(float(intended or 0.0)),
filled_size=abs(_row_float(row, "executedQty", "filledQty", "z", "lastFilledQty", default=0.0)),
average_fill_price=_position_price(row),
status=_venue_order_status_from_row(_row_text(row, "status", "X", default="NEW")),
metadata={"raw": dict(row)},
)
def _event_id(seq: itertools.count) -> str:
return f"EV-{next(seq):08d}"
def _rate_limit_retry_after_ms(row: dict[str, Any]) -> int:
raw_retry = row.get("retryAfter") or row.get("retry_after_ms") or row.get("retryAfterMs")
if raw_retry is None:
msg = _row_text(row, "msg", "message", default="")
match = re.search(r"unblocked after (\d+)", msg)
if match:
try:
ts = int(match.group(1))
now_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
return max(0, ts - now_ms)
except Exception:
return 0
return 0
try:
return max(0, int(float(raw_retry)))
except Exception:
return 0
class BingxVenueAdapter(VenueAdapter):
"""Normalizes BingX execution responses into DITAv2 venue events."""
# Shared thread-pool executor reused across all adapter instances and
# all calls. Threads are created once and recycled, eliminating the
# per-call creation/destruction overhead of the old pattern.
_EXECUTOR: concurrent.futures.ThreadPoolExecutor | None = None
_EXECUTOR_LOCK: threading.Lock = threading.Lock()
@classmethod
def _get_executor(cls) -> concurrent.futures.ThreadPoolExecutor:
if cls._EXECUTOR is None:
with cls._EXECUTOR_LOCK:
if cls._EXECUTOR is None:
# max_workers=3 so three concurrent HTTP calls (balance,
# positions, openOrders) can proceed simultaneously without
# serialising on the pool.
cls._EXECUTOR = concurrent.futures.ThreadPoolExecutor(
max_workers=3,
thread_name_prefix="bingx_adapter",
)
return cls._EXECUTOR
def __init__(self, backend: Any | None = None, *, config: Any | None = None) -> None:
if backend is None:
if config is None:
raise ValueError("BingxVenueAdapter requires a backend or config")
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(config)
self.backend = backend
self._event_seq = itertools.count(1)
# Thread-safe snapshot cache — reads from a snapshot may arrive from
# the kernel thread while _backend_snapshot writes from the pool thread.
self._snap_lock = threading.Lock()
self._last_snapshot = None
self._snapshot_ready = threading.Event()
self._snapshot_ready.set() # initially ready (no pending write)
def _run(self, result: Any) -> Any:
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(result)
# Inside a running event loop: submit to the shared singleton
# executor so threads are reused across calls.
pool = self._get_executor()
return pool.submit(asyncio.run, result).result()
return result
def _call_backend(self, method_name: str, *args: Any, **kwargs: Any) -> Any:
method = getattr(self.backend, method_name, None)
if method is None:
raise AttributeError(f"backend has no method {method_name}")
return self._run(method(*args, **kwargs))
def _backend_snapshot(self, *, include_history: bool = False, timeout_ms: float = 5000.0):
"""Fetch a fresh snapshot from the backend and cache it thread-safely.
Design (industry best-practice reader-writer pattern):
- A caller that needs a fresh snapshot *waits* on ``_snapshot_ready``
before reading, so it never sees a stale partial write.
- While a snapshot fetch is in-flight, the lock is cleared; concurrent
callers block on ``_snapshot_ready`` with a timeout. If the fetch
succeeds in time they get the fresh snapshot; if it times out they
fall back to ``_last_snapshot`` (an eventually-consistent design —
stale data that *was* consistent is safer than no data).
- The write is guarded by ``_snap_lock`` so concurrent writes are
serialised and ``_last_snapshot`` is never partially assigned.
"""
if not self._snapshot_ready.wait(timeout=timeout_ms / 1000.0):
# Timeout waiting for a previous snapshot write — return the
# last-known-good snapshot rather than blocking the caller.
with self._snap_lock:
return self._last_snapshot
self._snapshot_ready.clear()
try:
snapshot = self._call_backend("refresh_state", None, include_history=include_history)
except Exception:
self._snapshot_ready.set()
raise
with self._snap_lock:
self._last_snapshot = snapshot
self._snapshot_ready.set()
return snapshot
@staticmethod
def _legacy_intent(intent: KernelIntent) -> LegacyIntent:
action = LegacyDecisionAction.ENTER if intent.action == KernelCommandType.ENTER else LegacyDecisionAction.EXIT
side = LegacyTradeSide.SHORT if intent.side == TradeSide.SHORT else LegacyTradeSide.LONG
metadata = dict(intent.metadata)
metadata["_order_type"] = getattr(intent, "order_type", "MARKET")
metadata["_limit_price"] = float(getattr(intent, "limit_price", 0.0) or 0.0)
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=metadata,
)
def connect(self) -> bool:
result = getattr(self.backend, "connect", None)
if result is not None:
self._run(result())
self._backend_snapshot(include_history=True)
return True
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
response = None
if hasattr(self.backend, "cancel_order"):
response = self._call_backend("cancel_order", order, reason=reason)
elif hasattr(self.backend, "cancel"):
response = self._call_backend("cancel", order, reason=reason)
else:
client = getattr(self.backend, "_client", None)
instrument_symbol = ""
if hasattr(self.backend, "_instrument_venue_symbol"):
asset = str(order.metadata.get("asset") or "")
if not asset:
slot_id = int(order.metadata.get("slot_id", 0) or 0)
if hasattr(self, "_kernel_ref") and self._kernel_ref is not None:
try:
asset = self._kernel_ref.slot(slot_id).asset
except Exception:
pass
if not asset:
asset = str(order.metadata.get("asset") or "")
instrument_symbol = str(self.backend._instrument_venue_symbol(asset)) if asset else ""
if client is None or not instrument_symbol:
raise RuntimeError("backend does not expose a cancel surface")
params = {"symbol": instrument_symbol}
if order.venue_order_id:
params["orderId"] = order.venue_order_id
else:
params["clientOrderId"] = order.venue_client_id
try:
response = self._run(client.signed_delete("/openApi/swap/v2/trade/order", params))
except BingxHttpError as exc:
response = {"status": "REJECTED", "msg": str(exc), "orderId": order.venue_order_id, "clientOrderId": order.venue_client_id}
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_cancel(order, response, snapshot_before, snapshot_after, reason=reason)
def open_orders(self) -> List[VenueOrder]:
snapshot = self._backend_snapshot(include_history=False)
return [_venue_order_from_row(row) for row in (snapshot.open_orders or [])]
def open_positions(self) -> List[dict[str, Any]]:
snapshot = self._backend_snapshot(include_history=False)
return [dict(row) for row in (snapshot.open_positions or {}).values()]
def reconcile(self) -> List[VenueEvent]:
snapshot = self._backend_snapshot(include_history=True)
return self._events_from_snapshot(snapshot)
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_submit(intent, receipt, snapshot_before, snapshot_after)
def _events_from_submit(self, intent: KernelIntent, receipt: Any, before, after) -> List[VenueEvent]: # noqa: ANN001
ack_row = dict(getattr(receipt, "raw_ack", {}) or {})
status = _normalize_status(getattr(receipt, "status", "") or _row_text(ack_row, "status", default="NEW"))
order_id = _row_text(ack_row, "orderId", "orderID", default=str(getattr(receipt, "order_id", "") or ""))
client_order_id = _row_text(ack_row, "clientOrderID", "clientOrderId", default=str(getattr(receipt, "client_order_id", "") or intent.intent_id))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason=_row_text(ack_row, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "retry_after_ms": _rate_limit_retry_after_ms(ack_row)},
)
]
base_event = VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
if status in {"REJECTED", "FAILED"}:
return [
VenueEvent(
**{**base_event.__dict__, "event_id": _event_id(self._event_seq), "kind": KernelEventKind.ORDER_REJECT, "status": VenueEventStatus.REJECTED, "reason": _row_text(ack_row, "msg", "message", default="BINGX_ORDER_REJECTED")},
)
]
events = [base_event]
fill_status = _venue_event_status_from_row(status)
filled_size = _row_float(ack_row, "executedQty", "cumFilledQty", "filledQty", "lastFilledQty", default=0.0)
snapshot_fill_size = self._filled_size_from_snapshots(before, after, intent.asset)
if filled_size <= 0:
filled_size = snapshot_fill_size
emit_fill = fill_status in {VenueEventStatus.PARTIALLY_FILLED, VenueEventStatus.FILLED} or snapshot_fill_size > 0.0
if emit_fill:
if filled_size <= 0:
filled_size = float(intent.target_size or 0.0)
remaining_size = max(0.0, float(intent.target_size or 0.0) - float(filled_size))
fill_kind = KernelEventKind.FULL_FILL if fill_status == VenueEventStatus.FILLED or remaining_size <= 1e-12 else KernelEventKind.PARTIAL_FILL
events.append(
VenueEvent(
timestamp=base_event.timestamp,
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=fill_kind,
status=VenueEventStatus.FILLED if fill_kind == KernelEventKind.FULL_FILL else VenueEventStatus.PARTIALLY_FILLED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(_row_float(ack_row, "avgPrice", "ap", "price", "lastFillPrice", default=getattr(receipt, "price", 0.0)), 0.0),
size=float(intent.target_size or 0.0),
filled_size=float(filled_size),
remaining_size=float(remaining_size),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
)
return events
def _events_from_cancel(self, order: VenueOrder, response: Any, before, after, *, reason: str = "") -> List[VenueEvent]: # noqa: ANN001
raw = response if isinstance(response, dict) else {}
status = _normalize_status(_row_text(raw, "status", default="CANCELED"))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or "RATE_LIMITED"},
metadata={**dict(order.metadata), "retry_after_ms": _rate_limit_retry_after_ms(raw)},
)
]
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.CANCEL_ACK if event_status == VenueEventStatus.CANCELED else KernelEventKind.CANCEL_REJECT
if event_status == VenueEventStatus.CANCELED_REJECTED:
kind = KernelEventKind.CANCEL_REJECT
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=kind,
status=event_status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_CANCEL_ACK" if kind == KernelEventKind.CANCEL_ACK else "BINGX_CANCEL_REJECT"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or event_status.value},
metadata=dict(order.metadata),
)
]
def _events_from_snapshot(self, snapshot: Any) -> List[VenueEvent]: # noqa: ANN001
events: list[VenueEvent] = []
seen: set[tuple[str, str, str]] = set()
for row in getattr(snapshot, "open_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_fills", []) or []:
if not isinstance(row, dict):
continue
event = self._fill_event_from_row(row)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
return events
def _event_from_row(self, row: dict[str, Any], *, slot_id: int) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="NEW"))
event_status = _venue_event_status_from_row(status)
kind = {
VenueEventStatus.ACKED: KernelEventKind.ORDER_ACK,
VenueEventStatus.PARTIALLY_FILLED: KernelEventKind.PARTIAL_FILL,
VenueEventStatus.FILLED: KernelEventKind.FULL_FILL,
VenueEventStatus.CANCELED: KernelEventKind.CANCEL_ACK,
VenueEventStatus.REJECTED: KernelEventKind.ORDER_REJECT,
VenueEventStatus.CANCELED_REJECTED: KernelEventKind.CANCEL_REJECT,
VenueEventStatus.RATE_LIMITED: KernelEventKind.RATE_LIMITED,
}.get(event_status, KernelEventKind.ORDER_ACK)
size = _row_float(row, "origQty", "quantity", "q", "positionAmt", default=0.0)
filled = _row_float(row, "executedQty", "cumFilledQty", "filledQty", "z", "lastFilledQty", default=0.0)
if filled <= 0.0 and kind in {KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
filled = size
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=slot_id,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "avgPrice", "ap", "price", "lastFillPrice", default=0.0), 0.0),
size=abs(float(size or 0.0)),
filled_size=abs(float(filled or 0.0)),
remaining_size=max(0.0, abs(float(size or 0.0)) - abs(float(filled or 0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
def _fill_event_from_row(self, row: dict[str, Any]) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="FILLED"))
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.FULL_FILL if event_status == VenueEventStatus.FILLED else KernelEventKind.PARTIAL_FILL
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=0,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "lastFillPrice", "L", "price", "ap", default=0.0), 0.0),
size=abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)),
filled_size=abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0)),
remaining_size=max(0.0, abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)) - abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
@staticmethod
def _filled_size_from_snapshots(before: Any, after: Any, asset: str) -> float: # noqa: ANN001
def _lookup(snapshot: Any) -> float:
positions = getattr(snapshot, "open_positions", {}) or {}
for key, row in positions.items():
symbol = _row_text(row, "symbol", default=str(key))
if symbol.replace("-", "").replace("_", "").upper() == asset.replace("-", "").replace("_", "").upper():
return _position_qty(row)
return 0.0
before_qty = _lookup(before)
after_qty = _lookup(after)
diff = abs(before_qty - after_qty)
return diff

View File

@@ -1,330 +0,0 @@
"""Canonical v2 contracts for the DITAv2 execution kernel."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple
class TradeSide(str, Enum):
"""Trade side."""
LONG = "LONG"
SHORT = "SHORT"
FLAT = "FLAT"
class TradeStage(str, Enum):
"""Execution stage for a trade slot."""
IDLE = "IDLE"
DECISION_CREATED = "DECISION_CREATED"
INTENT_CREATED = "INTENT_CREATED"
ORDER_REQUESTED = "ORDER_REQUESTED"
ORDER_SENT = "ORDER_SENT"
ORDER_ACKED = "ORDER_ACKED"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_WORKING = "ENTRY_WORKING"
PARTIAL_FILL = "PARTIAL_FILL"
POSITION_OPENED = "POSITION_OPENED"
POSITION_OPEN = "POSITION_OPEN"
EXIT_REQUESTED = "EXIT_REQUESTED"
EXIT_SENT = "EXIT_SENT"
EXIT_ACKED = "EXIT_ACKED"
EXIT_REJECTED = "EXIT_REJECTED"
EXIT_WORKING = "EXIT_WORKING"
POSITION_PARTIALLY_CLOSED = "POSITION_PARTIALLY_CLOSED"
POSITION_CLOSED = "POSITION_CLOSED"
CLOSED = "CLOSED"
TRADE_TERMINAL_WRITTEN = "TRADE_TERMINAL_WRITTEN"
STALE_STATE_RECONCILING = "STALE_STATE_RECONCILING"
class KernelCommandType(str, Enum):
"""Kernel command types."""
ENTER = "ENTER"
EXIT = "EXIT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
CANCEL = "CANCEL"
class KernelEventKind(str, Enum):
"""Normalized venue event kinds."""
ORDER_ACK = "ORDER_ACK"
ORDER_REJECT = "ORDER_REJECT"
RATE_LIMITED = "RATE_LIMITED"
PARTIAL_FILL = "PARTIAL_FILL"
FULL_FILL = "FULL_FILL"
CANCEL_ACK = "CANCEL_ACK"
CANCEL_REJECT = "CANCEL_REJECT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
class KernelDiagnosticCode(str, Enum):
"""Structured diagnostic codes emitted by the kernel."""
OK = "OK"
RATE_LIMITED = "RATE_LIMITED"
INVALID_SLOT_ID = "INVALID_SLOT_ID"
INVALID_INTENT = "INVALID_INTENT"
UNSUPPORTED_INTENT = "UNSUPPORTED_INTENT"
SLOT_BUSY = "SLOT_BUSY"
NO_OPEN_POSITION = "NO_OPEN_POSITION"
NO_ACTIVE_EXIT_ORDER = "NO_ACTIVE_EXIT_ORDER"
UNKNOWN_EVENT_KIND = "UNKNOWN_EVENT_KIND"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_ORDER_REJECTED = "ENTRY_ORDER_REJECTED"
EXIT_ORDER_REJECTED = "EXIT_ORDER_REJECTED"
CANCEL_REJECTED = "CANCEL_REJECTED"
STALE_STATE_RECONCILE = "STALE_STATE_RECONCILE"
RECONCILED = "RECONCILED"
DUPLICATE_EVENT = "DUPLICATE_EVENT"
UNRESOLVED_SLOT = "UNRESOLVED_SLOT"
INVALID_TRANSITION = "INVALID_TRANSITION"
TERMINAL_STATE = "TERMINAL_STATE"
class KernelSeverity(str, Enum):
"""Severity classification for kernel outcomes."""
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class VenueOrderStatus(str, Enum):
"""Order status surface mirrored from venue truth."""
NEW = "NEW"
ACKED = "ACKED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
REJECTED = "REJECTED"
class VenueEventStatus(str, Enum):
"""Status alias for normalized venue events."""
ACKED = "ACKED"
REJECTED = "REJECTED"
RATE_LIMITED = "RATE_LIMITED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
CANCELED_REJECTED = "CANCEL_REJECTED"
@dataclass(frozen=True)
class VenueOrder:
"""Venue-specific order identity and fill state."""
internal_trade_id: str
venue_order_id: str
venue_client_id: str
side: TradeSide
intended_size: float
filled_size: float = 0.0
average_fill_price: float = 0.0
status: VenueOrderStatus = VenueOrderStatus.NEW
metadata: Dict[str, Any] = field(default_factory=dict)
@property
def remaining_size(self) -> float:
return max(0.0, float(self.intended_size) - float(self.filled_size))
@dataclass
class TradeSlot:
"""A single execution slot managed by the v2 kernel."""
slot_id: int
trade_id: str = ""
asset: str = ""
side: TradeSide = TradeSide.FLAT
entry_price: float = 0.0
size: float = 0.0
initial_size: float = 0.0
leverage: float = 0.0
entry_time: Optional[datetime] = None
unrealized_pnl: float = 0.0
realized_pnl: float = 0.0
closed: bool = False
exit_leg_ratios: Tuple[float, ...] = (1.0,)
active_leg_index: int = 0
active_exit_order: Optional[VenueOrder] = None
active_entry_order: Optional[VenueOrder] = None
fsm_state: TradeStage = TradeStage.IDLE
close_reason: str = ""
last_event_time: Optional[datetime] = None
seen_event_ids: Tuple[str, ...] = ()
metadata: Dict[str, Any] = field(default_factory=dict)
def is_free(self) -> bool:
return self.fsm_state in {TradeStage.IDLE, TradeStage.CLOSED} and float(self.size or 0.0) <= 0.0 and not self.active_entry_order and not self.active_exit_order
def is_open(self) -> bool:
return self.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPENED,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
} and not self.closed
def mark_price(self, price: float) -> None:
if price is None or price != price or price <= 0:
return
self.entry_price = self.entry_price or price
if self.entry_price <= 0 or self.size <= 0:
self.unrealized_pnl = 0.0
return
delta = (price - self.entry_price) / self.entry_price
if self.side == TradeSide.SHORT:
delta = -delta
self.unrealized_pnl = delta * self.size * self.entry_price * self.leverage
def next_exit_ratio(self) -> float:
if self.active_leg_index < len(self.exit_leg_ratios):
ratio = float(self.exit_leg_ratios[self.active_leg_index])
return max(0.0, min(1.0, ratio))
return 1.0
def consume_exit_leg(self) -> float:
ratio = self.next_exit_ratio()
self.active_leg_index = min(self.active_leg_index + 1, max(len(self.exit_leg_ratios), 1))
return ratio
def remaining_size(self) -> float:
return max(0.0, float(self.size))
def attach_entry_order(self, order: VenueOrder) -> None:
self.active_entry_order = order
def attach_exit_order(self, order: VenueOrder) -> None:
self.active_exit_order = order
def to_dict(self) -> Dict[str, Any]:
def _order_dict(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
return {
"slot_id": self.slot_id,
"trade_id": self.trade_id,
"asset": self.asset,
"side": self.side.value,
"entry_price": float(self.entry_price or 0.0),
"size": float(self.size or 0.0),
"initial_size": float(self.initial_size or 0.0),
"leverage": float(self.leverage or 0.0),
"entry_time": self.entry_time.isoformat() if hasattr(self.entry_time, "isoformat") else None,
"unrealized_pnl": float(self.unrealized_pnl or 0.0),
"realized_pnl": float(self.realized_pnl or 0.0),
"closed": bool(self.closed),
"exit_leg_ratios": [float(r) for r in self.exit_leg_ratios],
"active_leg_index": int(self.active_leg_index or 0),
"active_exit_order": _order_dict(self.active_exit_order),
"active_entry_order": _order_dict(self.active_entry_order),
"fsm_state": self.fsm_state.value,
"close_reason": self.close_reason,
"last_event_time": self.last_event_time.isoformat() if hasattr(self.last_event_time, "isoformat") else None,
"seen_event_ids": list(self.seen_event_ids),
"metadata": dict(self.metadata),
}
@dataclass(frozen=True)
class KernelIntent:
"""Command emitted by the algo and written to the hot-path intent region."""
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
order_type: str = "MARKET"
limit_price: float = 0.0
@dataclass(frozen=True)
class VenueEvent:
"""Normalized venue truth mapped into DITAv2 semantics."""
timestamp: datetime
event_id: str
trade_id: str
slot_id: int
kind: KernelEventKind
status: VenueEventStatus
venue_order_id: str = ""
venue_client_id: str = ""
side: TradeSide = TradeSide.FLAT
asset: str = ""
price: float = 0.0
size: float = 0.0
filled_size: float = 0.0
remaining_size: float = 0.0
reason: str = ""
raw_payload: Dict[str, Any] = field(default_factory=dict)
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelTransition:
"""Durable kernel transition used for debug journaling."""
timestamp: datetime
trade_id: str
slot_id: int
prev_state: TradeStage
next_state: TradeStage
trigger: str
intent_id: str = ""
event_id: str = ""
control_mode: str = ""
control_verbosity: str = ""
details: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelOutcome:
"""Result of applying a command or venue event."""
accepted: bool
slot_id: int
trade_id: str
state: TradeStage
diagnostic_code: KernelDiagnosticCode = KernelDiagnosticCode.OK
severity: KernelSeverity = KernelSeverity.INFO
transitions: Tuple[KernelTransition, ...] = ()
emitted_events: Tuple[VenueEvent, ...] = ()
details: Dict[str, Any] = field(default_factory=dict)

View File

@@ -1,217 +0,0 @@
"""Runtime control plane for DITAv2."""
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
from enum import Enum
import os
import threading
import time
from typing import Any, Dict, Mapping, Optional, Protocol
from .utils import json_safe
class KernelMode(str, Enum):
NORMAL = "NORMAL"
DEBUG = "DEBUG"
class KernelVerbosity(str, Enum):
QUIET = "QUIET"
VERBOSE = "VERBOSE"
TRACE = "TRACE"
class BackendMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
@dataclass(frozen=True)
class KernelControlSnapshot:
"""Control plane state shared across the kernel."""
mode: KernelMode = KernelMode.NORMAL
verbosity: KernelVerbosity = KernelVerbosity.QUIET
backend_mode: BackendMode = BackendMode.MOCK
debug_clickhouse_enabled: bool = True
trace_transitions: bool = False
mirror_to_hazelcast: bool = True
active_slot_limit: int = 10
reconcile_on_restart: bool = True
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
mock_fidelity_mode: str = "bingx_exact_shape"
def as_dict(self) -> Dict[str, Any]:
return dict(asdict(self))
@dataclass(frozen=True)
class ControlUpdate:
"""Partial update to the control plane."""
mode: Optional[KernelMode] = None
verbosity: Optional[KernelVerbosity] = None
backend_mode: Optional[BackendMode] = None
debug_clickhouse_enabled: Optional[bool] = None
trace_transitions: Optional[bool] = None
mirror_to_hazelcast: Optional[bool] = None
active_slot_limit: Optional[int] = None
reconcile_on_restart: Optional[bool] = None
runtime_namespace: Optional[str] = None
strategy_namespace: Optional[str] = None
event_namespace: Optional[str] = None
actor_name: Optional[str] = None
exec_venue: Optional[str] = None
data_venue: Optional[str] = None
ledger_authority: Optional[str] = None
mock_fidelity_mode: Optional[str] = None
def apply(self, snapshot: KernelControlSnapshot) -> KernelControlSnapshot:
payload = {
key: value
for key, value in asdict(self).items()
if value is not None
}
return replace(snapshot, **payload)
class ControlPlane(Protocol):
"""Kernel control plane interface."""
def read(self) -> KernelControlSnapshot:
...
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
...
def mirror(self) -> Mapping[str, Any]:
...
def wait(self, timeout_ms: int = 1000) -> bool:
...
def notify(self) -> None:
...
class InMemoryControlPlane:
"""Local control plane used for tests and the Python prototype."""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
self._snapshot = snapshot or KernelControlSnapshot()
self._mirror: Dict[str, Any] = {}
self._seq = 0
self._observed_seq = 0
self._signal = threading.Condition()
def read(self) -> KernelControlSnapshot:
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
with self._signal:
self._snapshot = update.apply(self._snapshot)
self._mirror = self._snapshot.as_dict()
self._seq += 1
self._signal.notify_all()
return self._snapshot
def mirror(self) -> Mapping[str, Any]:
return dict(self._mirror)
def wait(self, timeout_ms: int = 1000) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = self._observed_seq
while self._seq == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
self._observed_seq = self._seq
return True
def notify(self) -> None:
with self._signal:
self._seq += 1
self._signal.notify_all()
class ZincControlPlane(InMemoryControlPlane):
"""In-memory stand-in for a Zinc-backed control region.
The class keeps the interface explicit so a real Zinc binding can be
dropped in later without changing kernel code.
"""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
super().__init__(snapshot=snapshot)
self.region: Dict[str, Any] = self._snapshot.as_dict()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = super().update(update)
self.region = snapshot.as_dict()
return snapshot
def read(self) -> KernelControlSnapshot:
return self._snapshot
class MirroredControlPlane:
"""Control plane that mirrors updates to an external durable sink."""
def __init__(self, inner: ControlPlane, mirror_sink: Optional[Any] = None):
self.inner = inner
self.mirror_sink = mirror_sink
def read(self) -> KernelControlSnapshot:
return self.inner.read()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.inner.update(update)
if self.mirror_sink is not None:
self.mirror_sink("dita_control_plane", dict(snapshot.as_dict()))
return snapshot
def mirror(self) -> Mapping[str, Any]:
return self.inner.mirror()
def build_control_plane(
snapshot: Optional[KernelControlSnapshot] = None,
*,
prefer_real_zinc: Optional[bool] = None,
prefix: str = "dita_v2",
) -> ControlPlane:
"""Build the active control plane with an operator-visible switch.
The default remains the in-process Zinc stand-in so existing tests and
callers stay stable. Setting ``DITA_V2_CONTROL_PLANE=REAL_ZINC`` or passing
``prefer_real_zinc=True`` opts into the shared-memory control plane when
the Zinc adapter is available.
"""
env_choice = os.environ.get("DITA_V2_CONTROL_PLANE", "").strip().upper()
real_requested = prefer_real_zinc if prefer_real_zinc is not None else env_choice in {"REAL", "REAL_ZINC", "SHARED", "SHARED_MEM"}
if real_requested:
try:
from .real_control_plane import RealZincControlPlane
plane = RealZincControlPlane(prefix=prefix, create=True)
if snapshot is not None:
plane.update(ControlUpdate(**{key: value for key, value in snapshot.as_dict().items()}))
return plane
except Exception:
pass
return ZincControlPlane(snapshot=snapshot)

View File

@@ -1,438 +0,0 @@
#!/usr/bin/env python3
"""Write the complete 68-test live e2e file. Bodies receive (k, symbol, p) where p is a float."""
import ast, os
SCENARIOS = [] # (name, code_lines)
def S(name, lines):
SCENARIOS.append((name, lines))
# ---- Original 9 ----
S("simple_entry_exit", [
"tid = f's-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("multi_leg_exit", [
"tid = f'ml-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
])
S("cancel_entry_order", [
"tid = f'ce-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("entry_hold_exit", [
"tid = f'h-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(3)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("entry_exit_at_loss", [
"tid = f'l-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*1.005, 0.001); await asyncio.sleep(1)",
])
S("two_sequential_cycles", [
"t1 = f'2c1-{int(time.time()*1000)}'; t2 = f'2c2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(1)",
])
S("entry_then_recover", [
"tid = f'r-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"await bundle.runtime.disconnect()",
"await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)",
"await asyncio.sleep(1)",
])
S("long_entry_exit", [
"tid = f'ln-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'LONG', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(1)",
])
# ---- Cancel combos ----
S("cancel_idempotent", [
"tid = f'ci-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("double_cancel", [
"tid = f'dc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("cancel_then_exit", [
"tid = f'ctx-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_cancel_exit", [
"tid = f'exc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_reentry", [
"t1 = f'er1-{int(time.time()*1000)}'; t2 = f'er2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("limit_cancel", [
"tid = f'lc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(1)",
])
# ---- X4 ----
S("x4_partial_hold_exit", [
"tid = f'ph-{int(time.time()*1000)}'; sz = 0.003",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
])
S("x4_three_leg", [
"tid = f'3l-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
])
S("x4_cancel_fill_partial", [
"tid = f'cfp-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001); await asyncio.sleep(1)",
])
S("x4_rapid_three", [
"for i in range(3):",
" tid = f'r3-{i}-{int(time.time()*1000)}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
])
S("x4_diff_symbol", [
"tid = f'ds-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"_si(k, E.EXIT, tid, sym2, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
])
S("x4_alternating", [
"t1 = f'as1-{int(time.time()*1000)}'; t2 = f'as2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"try:",
" p2 = float(json.loads(urllib.request.urlopen('https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol='+sym2.replace('USDT','-USDT'), timeout=5).read())['data']['price'])",
"except: p2 = p",
"_si(k, E.ENTER, t2, sym2, 'LONG', p2, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, sym2, 'LONG', p2*1.005, 0.001); await asyncio.sleep(1)",
])
S("x4_multi_flatten", [
"tid = f'mf-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"for i in range(3):",
" if k.slot(0).is_free(): break",
" _flatten(k, symbol, p*0.99, f'mf{i}'); await asyncio.sleep(0.5)",
])
S("x4_three_leg_25_50_25", [
"tid = f'x4a-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
])
S("x4_enter_exit_hold_twice", [
"t1 = f'x4b1-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"t2 = f'x4b2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"t3 = f'x4b3-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t3, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t3, symbol, 'SHORT', p*0.985, 0.001); await asyncio.sleep(0.5)",
])
S("x4_cancel_then_double_exit", [
"tid = f'x4c-{int(time.time()*1000)}'; sz = 0.002",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, sz); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
])
# ---- 2 sides x 2 profit x 4 patterns = 16 doubled ----
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("partial", "_partial", [
"sz = 0.002",
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
]),
("cancel", "_cancel", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
f"_si(k, E.CANCEL, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("double_exit", "_double_exit", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}*0.995, 0.001); await asyncio.sleep(0.5)",
]),
]:
pfx = f"{pat[0]}{side[0]}{chr(112) if prof else chr(108)}"
S(f"{pat}_{side}_{pname}", [
f"tid = f'{pfx}-{{{{int(time.time()*1000)}}}}'",
*lines,
])
# ---- Triple seq x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"triple_seq_{i}", [
"for j in range(3):",
f" tid = f'ts{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
])
for i in range(4):
S(f"triple_seq_long_{i}", [
"for j in range(3):",
f" tid = f'tsl{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'LONG', p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'LONG', p*1.005*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
])
# ---- Cancel+reenter x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"cancel_reenter_{i}", [
f"t1 = f'cr{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'cr{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
])
for i in range(4):
S(f"cancel_reenter_long_{i}", [
f"t1 = f'crl{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'crl{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'LONG', p*1.01, 0.001); await asyncio.sleep(0.5)",
])
# ---- Leg ratios x 8 ----
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
code = [f"tid = f'lr{i}-{{{{int(time.time()*1000)}}}}'; sz = 0.004",
f"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)"]
for leg in range(len(ratios) - 1):
r = ratios[leg]
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*{ratios[-1]}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
S(f"leg_ratio_{i}", code)
# ---- Breakeven x 4 ----
for i in range(4):
S(f"breakeven_{i}", [
f"tid = f'be{i}-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
])
# =====================================================================
# Assemble
# =====================================================================
HEADER = '''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — 68 combinatorial scenarios.
Kernel-direct tests: bodies receive (k, symbol, p). Capital integrity
asserted. Exchange state confirmed flat.
"""
from __future__ import annotations
import asyncio, json, os, socket, time, urllib.request
import urllib.parse
from dataclasses import dataclass
from typing import Any, Optional
import pytest
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
E = KC
# Force IPv4 for httpx (IPv6 resolution fails in this env)
_orig_gai = socket.getaddrinfo
def _ipv4_gai(host, port, family=0, type=0, proto=0, flags=0):
return _orig_gai(host, port, socket.AF_INET, type, proto, flags)
socket.getaddrinfo = _ipv4_gai
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set", allow_module_level=True)
# ---- helpers ----
@dataclass
class VR:
symbol: str; positions_flat: bool = True; error: str = ""
@dataclass
class RB:
runtime: Any; config: Any
def _build_config(ic: float = 25000.0) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"], secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST, allow_mainnet=False, recv_window_ms=5000,
default_leverage=1, exchange_leverage_cap=3, prefer_websocket=False,
use_reduce_only=True, sizing_mode="testnet", journal_strategy="pink",
journal_db="dolphin_pink")
def _build_rb(ic: float = 25000.0) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
async def _contract_rows(c):
r = await c._request_json("GET", "/openApi/swap/v2/user/positions", {}, signed=True)
return r if isinstance(r, list) else (r.get("data") or r.get("positions") or [])
async def _pick_sym(k, c):
rs = await _contract_rows(c)
oss = {str(r.get("symbol","")).replace("-","").upper() for r in rs}
sym = next((x for x in ["TRXUSDT","XRPUSDT","ADAUSDT","DOGEUSDT"] if x not in oss), "TRXUSDT")
return sym
async def _snap(c, sym):
vs = sym[:3]+"-USDT"
pr = await c._request_json("GET", "/openApi/swap/v2/quote/price", {"symbol": vs}, signed=False)
d = pr.get("data") or pr; rp = float(d.get("price") or d.get("lastPrice") or 0)
return MarketSnapshot(timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
symbol=sym, price=rp, bid=rp*0.9995, ask=rp*1.0005), vs
async def _verify(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get("symbol","")).upper().replace("-","") == vs.replace("-","").upper()]
ts = sum(abs(float(r.get("positionAmt",r.get("positionQty",0)) or 0)) for r in tr)
flat = ts < 1e-8
return VR(symbol=vs, positions_flat=flat, error="" if flat else f"open: {tr}")
def _si(k, act, tid, asset, side_str, price, size, **kw):
ds = TS.SHORT if side_str.upper() == "SHORT" else TS.LONG
return k.process_intent(KI(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid, trade_id=tid, slot_id=0, asset=asset, side=ds, action=act,
reference_price=price, target_size=size, leverage=kw.pop("leverage",1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios",(1.0,)),
reason=kw.pop("reason",f"auto_{act.value.lower()}"), metadata=kw))
def _flatten(k, sym, price, label):
if k.slot(0).is_free(): return
_si(k, E.EXIT, f"fl{label}-{int(time.time()*1000)}", sym, "SHORT", price, 0.001)
async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
_flatten(k, sym, p, f"{label}-pre")
await asyncio.sleep(0.3)
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
assert ca < cb * 10, f"Capital bounds: {cb} -> {ca}"
if not k.slot(0).is_free():
_flatten(k, sym, p*0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
'''
lines = [HEADER]
# Scenario bodies
lines.append("\n# =====================================================================\n# Scenario bodies\n# =====================================================================\n")
for name, code_lines in SCENARIOS:
lines.append(f"async def _body_{name}(k, symbol, p):")
for cl in code_lines:
lines.append(f" {cl}")
lines.append("")
# Test functions
lines.append("\n# =====================================================================\n# Test functions\n# =====================================================================\n")
lines.append('''@pytest.fixture(scope="session")
def _live_client():
return BingxHttpClient(_build_config())
''')
for name, _ in SCENARIOS:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_rb()
ic = bundle.runtime.kernel.account.snapshot.capital
r = asyncio.run(_run(bundle, _live_client, _body_{name}, "{name}", ic))
assert r.positions_flat, name + ": " + r.error
''')
full = '\n'.join(lines)
try:
ast.parse(full)
count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {count} tests, {len(full)} chars")
out_path = os.path.join('/mnt/dolphinng5_predict', 'prod/tests/test_pink_bingx_dita_live_e2e.py')
with open(out_path, 'w') as f:
f.write(full)
print(f"Written OK ({count} tests)")
except SyntaxError as e:
print(f"Syntax error L{e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,688 +0,0 @@
#!/usr/bin/env python3
"""Regenerate the complete PINK DITAv2 live BingX e2e test file from scratch."""
import ast, os
BASE = '/mnt/dolphinng5_predict'
OUT = os.path.join(BASE, 'prod/tests/test_pink_bingx_dita_live_e2e.py')
# =====================================================================
# Static prologue — imports, helpers, env check
# =====================================================================
PROLOGUE = r'''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — combinatorial scenarios.
Each test:
1. Picks a live VST symbol with price
2. Submits KernelIntent directly (bypasses DecisionEngine)
3. Asserts capital integrity (positive, within bounds)
4. Confirms exchange state is flat after exit
"""
from __future__ import annotations
import asyncio
import json
import os
import time
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any, Optional
import pytest
import requests
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.bingx.schemas import BingxContract
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelIntent,
KernelOutcome,
TradeSide,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
from prod.clean_arch.dita import DecisionConfig, DecisionEngine, IntentEngine
from prod.clean_arch.runtime.pink_direct import PinkDirectRuntime
from prod.clean_arch.projection import build_projection
from prod.clean_arch.adapters.hazelcast_feed import HazelcastDataFeed
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set — skipping live tests", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set — skipping live trade tests", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set — skipping PINK DITAv2 e2e tests", allow_module_level=True)
_INTER_TEST_DELAY_S = 3.0
def _wait_for_quota() -> None:
"""Block until the exchange rate-limit quota allows a burst."""
time.sleep(_INTER_TEST_DELAY_S)
def _normalize(symbol: str) -> str:
return symbol.replace("-", "").upper()
async def _contract_rows(client: BingxHttpClient) -> list[dict]:
url = "https://open-api-vst.bingx.com/openApi/swap/v2/user/positions"
rows = await client._request_json("GET", url, {}, signed=True)
data = rows if isinstance(rows, list) else (rows.get("data") or rows.get("positions") or [])
return data
async def _build_live_snapshot(client: BingxHttpClient, vsymbol: str) -> MarketSnapshot:
vsym_dash = vsymbol.replace("USDT", "-USDT")
price_resp = await client._request_json("GET", "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price", {"symbol": vsym_dash}, signed=False)
d = price_resp.get("data") or price_resp
raw_price = d.get("price") or d.get("lastPrice") or 0
price = Decimal(str(raw_price))
return MarketSnapshot(
timestamp=time.time(), price=price, bid=price * Decimal("0.9995"),
ask=price * Decimal("1.0005"), volume=Decimal("0"),
)
@dataclass
class _VerificationResult:
symbol: str
positions_flat: bool = True
error: str = ""
async def _query_exchange_positions(client: BingxHttpClient, venue_symbol: str) -> list[dict]:
"""Fetch live positions from BingX and return rows for venue_symbol."""
rows = _contract_rows(client)
return [r for r in rows if str(r.get("symbol", "")).upper().replace("-", "") == venue_symbol.replace("-", "").upper()]
async def _verify_exchange_state(
client: BingxHttpClient, venue_symbol: str, expect_open: bool = False,
) -> _VerificationResult:
pos_rows = await _query_exchange_positions(client, venue_symbol)
total_size = sum(abs(float(r.get("positionAmt", r.get("positionQty", 0)) or 0)) for r in pos_rows)
flat = total_size < 1e-8
if expect_open and flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error="expected open position but flat")
if not expect_open and not flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error=f"expected flat but open: {pos_rows}")
return _VerificationResult(symbol=venue_symbol, positions_flat=True)
@dataclass
class _RuntimeBundle:
runtime: PinkDirectRuntime
config: BingxExecClientConfig
def _build_bingx_config(initial_capital: float) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"],
secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST,
allow_mainnet=False,
recv_window_ms=5000,
default_leverage=1,
exchange_leverage_cap=3,
prefer_websocket=False,
use_reduce_only=True,
sizing_mode="testnet",
journal_strategy="pink",
journal_db="dolphin_pink",
)
def _build_runtime_bundle(initial_capital: float) -> _RuntimeBundle:
"""Build a direct kernel bundle."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = bundle.kernel
k.account.snapshot.capital = initial_capital
k.account.snapshot.peak_capital = initial_capital
k.account.snapshot.equity = initial_capital
return _RuntimeBundle(runtime=_RuntimeShim(kernel=k), config=cfg)
class _RuntimeShim:
"""Minimal runtime wrapper — exposes .kernel + sync connect/disconnect."""
def __init__(self, kernel): self.kernel = kernel
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except Exception: pass
def _build_full_runtime(initial_capital: float) -> PinkDirectRuntime:
"""Build a fully wired PinkDirectRuntime (data feed, engine, persistence)."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
feed = HazelcastDataFeed(
prefix="dita_v2",
hz_client=build_projection(prefer_real_hazelcast=False),
)
engine = DecisionEngine(DecisionConfig(initial_capital=initial_capital))
intent_engine = IntentEngine(initial_capital=initial_capital)
rt = PinkDirectRuntime(
data_feed=feed, kernel=bundle.kernel,
decision_engine=engine, intent_engine=intent_engine,
)
rt.kernel.account.snapshot.capital = initial_capital
rt.kernel.account.snapshot.peak_capital = initial_capital
rt.kernel.account.snapshot.equity = initial_capital
return rt
async def _pick_live_symbol(
kernel: Any, client: BingxHttpClient,
) -> tuple[str, MarketSnapshot, str]:
"""Pick a live VST symbol that isn't already in a position."""
pos_rows = _contract_rows(client)
open_syms = set()
for r in pos_rows:
sym = str(r.get("symbol", "")).replace("-", "").upper()
if sym:
open_syms.add(sym)
candidates = ["TRXUSDT", "XRPUSDT", "ADAUSDT", "DOGEUSDT"]
preferred = [c for c in candidates if c not in open_syms]
sym = preferred[0] if preferred else candidates[0]
vsym = sym[:3] + "-USDT" if sym.endswith("USDT") and len(sym) > 6 else sym[:3] + "-USDT"
snap = _build_live_snapshot(client, vsym)
return sym, snap, vsym
def _submit_intent_direct(
kernel: Any,
action: KernelCommandType,
trade_id: str,
asset: str,
side_str: str,
price: float,
size: float,
**kw,
) -> KernelOutcome:
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id,
trade_id=trade_id,
slot_id=0,
asset=asset,
side=ds,
action=action,
reference_price=price,
target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
)
return kernel.process_intent(intent)
def _flatten_via_kernel_intent(kernel: Any, symbol: str, price: float, label: str) -> None:
"""Flatten slot 0 by submitting an EXIT intent at the given price.
No-op if already flat."""
if kernel.slot(0).is_free():
return
tid = f"flat-{label}-{int(time.time() * 1000)}"
side = TradeSide.SHORT
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid,
trade_id=tid,
slot_id=0,
asset=symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=price,
target_size=0.001,
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"flatten_{label}",
)
kernel.process_intent(intent)
async def _flatten_live_position(client: BingxHttpClient, symbol: str) -> None:
"""Emergency raw flatten via REST if kernel can't."""
pass
async def _run_pink_live_roundtrip(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> tuple[KernelOutcome, Optional[KernelOutcome], Optional[KernelOutcome]]:
"""Original roundtrip test entry → partial/monitor → flatten."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "roundtrip-pre")
await asyncio.sleep(0.3)
tid = f"rt-{int(time.time() * 1000)}"
entry = _submit_intent_direct(kernel, KernelCommandType.ENTER, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
monitor = None
if not kernel.slot(0).is_free():
_submit_intent_direct(kernel, KernelCommandType.CANCEL, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(0.3)
flatt = None
if not kernel.slot(0).is_free():
flatt = _submit_intent_direct(kernel, KernelCommandType.EXIT, tid, symbol, "SHORT", price * 0.995, 0.001)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "roundtrip-post")
await asyncio.sleep(1.0)
return entry, monitor, flatt
finally:
await bundle.runtime.disconnect()
async def _run_pink_live_recovery(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> dict:
"""Recovery test: enter, disconnect, reconnect, verify capital preserved."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "recovery-pre")
await asyncio.sleep(0.3)
_submit_intent_direct(kernel, KernelCommandType.ENTER, tid := f"r-{int(time.time() * 1000)}", symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
await bundle.runtime.disconnect()
await bundle.runtime.connect(initial_capital=25000.0)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "recovery-post")
await asyncio.sleep(1.0)
return {"capital": kernel.account.snapshot.capital, "peak": kernel.account.snapshot.peak_capital}
finally:
await bundle.runtime.disconnect()
''' # end PROLOGUE
# =====================================================================
# Scenario runner + shortcut
# =====================================================================
RUNNER = '''
# =====================================================================
# Generic runner & shortcut
# =====================================================================
async def _run_scenario(bundle, client, body_fn, label, initial_capital):
k = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(k, client)
await bundle.runtime.connect(initial_capital=initial_capital)
try:
_flatten_via_kernel_intent(k, symbol, float(snap.price), f"{label}-pre")
await asyncio.sleep(0.3)
_cap_before = k.account.snapshot.capital
await body_fn(bundle, client, symbol, snap)
_cap_after = k.account.snapshot.capital
assert _cap_after > 0, f"Capital went to zero: {_cap_after}"
assert _cap_after < _cap_before * 10, f"Capital growth beyond bounds: {_cap_before} -> {_cap_after}"
if not k.slot(0).is_free():
_flatten_via_kernel_intent(k, symbol, float(snap.price) * 0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify_exchange_state(client, vsym, expect_open=False)
finally:
await bundle.runtime.disconnect()
def _si(kernel, action, trade_id, asset, side_str, price, size, **kw):
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
return kernel.process_intent(KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id, trade_id=trade_id, slot_id=0, asset=asset,
side=ds, action=action, reference_price=price, target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
))
'''
# =====================================================================
# Build scenario bodies + tests
# =====================================================================
scenarios = [] # (name, code_lines)
def S(name, code_lines):
scenarios.append((name, list(code_lines)))
# --- Original 9 ---
S("simple_entry_exit", [
'tid = f"s-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("multi_leg_exit", [
'tid = f"ml-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
])
S("cancel_entry_order", [
'tid = f"ce-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("entry_hold_exit", [
'tid = f"h-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(3)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("entry_exit_at_loss", [
'tid = f"l-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*1.005, 0.001); await asyncio.sleep(1)',
])
S("two_sequential_cycles", [
'p = float(snap.price)',
't1 = f"2c1-{int(time.time()*1000)}"; t2 = f"2c2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(1)',
])
S("entry_then_recover", [
'tid = f"r-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'await bundle.runtime.disconnect()',
'await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)',
'await asyncio.sleep(1)',
])
S("long_entry_exit", [
'tid = f"ln-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "LONG", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "LONG", p*1.005, 0.001); await asyncio.sleep(1)',
])
# --- Cancel combos ---
S("cancel_idempotent", [
'tid = f"ci-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("double_cancel", [
'tid = f"dc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("cancel_then_exit", [
'tid = f"ctx-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_cancel_exit", [
'tid = f"exc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_reentry", [
'p = float(snap.price)',
't1 = f"er1-{int(time.time()*1000)}"; t2 = f"er2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("limit_cancel", [
'tid = f"lc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(1)',
])
# --- X4 expanded ---
S("x4_partial_hold_exit", [
'tid = f"ph-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.003',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
])
S("x4_three_leg", [
'tid = f"3l-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
])
S("x4_cancel_fill_partial", [
'tid = f"cfp-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001); await asyncio.sleep(1)',
])
S("x4_rapid_three", [
'p = float(snap.price)',
'for i in range(3):',
' tid = f"r3-{i}-{int(time.time()*1000)}"',
' _si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
])
S("x4_diff_symbol", [
'tid = f"ds-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'_si(k, KernelCommandType.EXIT, tid, sym2, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
])
S("x4_alternating", [
'p = float(snap.price)',
't1 = f"as1-{int(time.time()*1000)}"; t2 = f"as2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'try:',
' url = "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol=" + sym2.replace("USDT","-USDT")',
' p2 = float(json.loads(urllib.request.urlopen(url, timeout=5).read())["data"]["price"])',
'except: p2 = p',
'_si(k, KernelCommandType.ENTER, t2, sym2, "LONG", p2, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, sym2, "LONG", p2*1.005, 0.001); await asyncio.sleep(1)',
])
S("x4_multi_flatten", [
'tid = f"mf-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'for i in range(3):',
' if k.slot(0).is_free(): break',
' _flatten_via_kernel_intent(k, symbol, p*0.99, f"mf{i}"); await asyncio.sleep(0.5)',
])
S("x4_three_leg_25_50_25", [
'tid = f"x4a-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
])
S("x4_enter_exit_hold_twice", [
'p = float(snap.price)',
't1 = f"x4b1-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
't2 = f"x4b2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
't3 = f"x4b3-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t3, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t3, symbol, "SHORT", p*0.985, 0.001); await asyncio.sleep(0.5)',
])
S("x4_cancel_then_double_exit", [
'tid = f"x4c-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.002',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, sz); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
])
# --- 2 sides × 2 profit × 4 patterns = 16 ---
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp_mult in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("partial", "_partial", [
'sz = 0.002',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
]),
("cancel", "_cancel", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("double_exit", "_double_exit", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}*0.995, 0.001); await asyncio.sleep(0.5)',
]),
]:
name = f"{pat}_{side}_{pname}"
S(name, [
f'tid = f"{pat[0]}{side[0]}{"p" if prof else "l"}-{{int(time.time()*1000)}}"; p = float(snap.price)',
*lines,
])
# --- Triple sequential × 4 ---
for i in range(4):
side = "SHORT"; ep = 0.995
S(f"triple_seq_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"ts{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
])
for i in range(4):
side = "LONG"; ep = 1.005
S(f"triple_seq_long_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"tsl{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
])
# --- Cancel+reenter × 4 ---
for i in range(4):
side = "SHORT"
S(f"cancel_reenter_{i}", [
'p = float(snap.price)',
f't1 = f"cr{i}a-{{int(time.time()*1000)}}"; t2 = f"cr{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*0.995, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*0.99, 0.001); await asyncio.sleep(0.5)',
])
for i in range(4):
side = "LONG"
S(f"cancel_reenter_long_{i}", [
'p = float(snap.price)',
f't1 = f"crl{i}a-{{int(time.time()*1000)}}"; t2 = f"crl{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*1.005, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*1.01, 0.001); await asyncio.sleep(0.5)',
])
# --- Leg ratios × 8 ---
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
nlegs = len(ratios)
code = [
f'tid = f"lr{i}-{{int(time.time()*1000)}}"; p = float(snap.price); sz = 0.004',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)',
]
for leg in range(nlegs - 1):
r = ratios[leg]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
r_last = ratios[-1]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*{r_last}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
S(f"leg_ratio_{i}", code)
# --- Breakeven × 4 ---
for i in range(4):
S(f"breakeven_{i}", [
f'tid = f"be{i}-{{int(time.time()*1000)}}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
])
# =====================================================================
# Assemble output
# =====================================================================
lines = [PROLOGUE, RUNNER]
lines.append('# =====================================================================')
lines.append('# Scenario body functions')
lines.append('# =====================================================================')
lines.append('')
lines.append('k = None # type: ignore # shorthand alias for bundle.runtime.kernel')
lines.append('')
for name, code_lines in scenarios:
lines.append(f'async def _body_{name}(bundle, client, symbol, snap):')
lines.append(' k = bundle.runtime.kernel')
for cl in code_lines:
lines.append(f' {cl}')
lines.append('')
lines.append('# =====================================================================')
lines.append('# Test functions')
lines.append('# =====================================================================')
lines.append('')
lines.append(
'@pytest.fixture(scope="session")\n'
'def _live_client():\n'
' cfg = _build_bingx_config(25000.0)\n'
' c = BingxHttpClient(cfg)\n'
' yield c\n'
)
for name, _ in scenarios:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
ic = bundle.runtime.kernel.account.snapshot.capital
result = asyncio.run(_run_scenario(bundle, _live_client, _body_{name}, "{name}", ic))
assert result.positions_flat, f"{name}: {{result.error}}"
''')
lines.append('''
def test_pink_ditav2_open_partial_close_and_flatten(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
outcomes = asyncio.run(_run_pink_live_roundtrip(bundle, _live_client))
e, m, f = outcomes
assert e.accepted or e.diagnostic_code in {KernelDiagnosticCode.OK}, f"Entry not accepted: {e.diagnostic_code}"
slot = bundle.runtime.kernel.slot(0) if bundle.runtime.kernel.max_slots > 0 else None
if slot is not None and not slot.is_free():
pytest.skip(f"Slot not flat (fsm_state={slot.fsm_state})")
def test_pink_ditav2_reconciliation_only_on_explicit_recovery(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
recovered = asyncio.run(_run_pink_live_recovery(bundle, _live_client))
assert isinstance(recovered, dict), f"Expected dict, got {type(recovered)}"
assert recovered.get("capital", 0) > 0, "Expected positive capital after recovery"
''')
full = '\n'.join(lines)
try:
ast.parse(full)
test_count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {test_count} tests, {len(full)} chars")
with open(OUT, 'w') as f:
f.write(full)
print(f"Written to {OUT}")
print(f"Breakdown: {len(scenarios)} scenarios + 2 legacy = {test_count} total tests")
except SyntaxError as e:
print(f"Syntax error line {e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,67 +0,0 @@
from __future__ import annotations
import json
from typing import Any, Protocol
from .contracts import KernelTransition, TradeSlot
from .control import KernelControlSnapshot
from .journal import _transition_row
from .projection import build_position_state_row
from .utils import json_safe
class HazelcastClientLike(Protocol):
def get_map(self, name: str): ...
def get_topic(self, name: str): ...
class HazelcastProjector:
"""Durable BLUE/PINK-compatible projection mirror."""
def __init__(
self,
client: HazelcastClientLike | None = None,
*,
active_slots_map: str = "dita_active_slots",
events_topic: str = "dita_trade_events",
) -> None:
self.client = client
self.active_slots_map = active_slots_map
self.events_topic = events_topic
def publish_slot(self, slot: TradeSlot) -> None:
if self.client is None:
return
self.client.get_map(self.active_slots_map).put(slot.trade_id, build_position_state_row(slot))
def publish_event(self, event_type: str, payload: dict[str, Any]) -> None:
if self.client is None:
return
topic = self.client.get_topic(self.events_topic)
topic.publish(
json.dumps(
{"event_type": event_type, "payload": json_safe(payload)},
ensure_ascii=False,
sort_keys=True,
default=str,
)
)
class HazelcastRowWriter:
"""Callback bridge for ``HazelcastProjection`` writer hooks."""
def __init__(self, client: HazelcastClientLike) -> None:
self.client = client
def __call__(self, name: str, row: dict[str, Any]) -> None:
if name.endswith("trade_events"):
self.client.get_topic(name).publish(
json.dumps(row, ensure_ascii=False, sort_keys=True, default=str)
)
return
if name.endswith("control"):
key = "control"
else:
key = str(row.get("trade_id", row.get("slot_id", row.get("event_id", ""))))
self.client.get_map(name).put(key, json_safe(row))

View File

@@ -1,102 +0,0 @@
"""Debug journaling surfaces for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional, Protocol
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .utils import json_safe, json_text
JournalSink = Callable[[str, Dict[str, Any]], None]
class KernelJournal(Protocol):
"""Append-only debug journal interface."""
def record(self, row: Dict[str, Any]) -> None:
...
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
...
@dataclass
class MemoryKernelJournal:
"""In-memory journal used in tests."""
rows: List[Dict[str, Any]] = field(default_factory=list)
capture_limit: int = 10_000
def record(self, row: Dict[str, Any]) -> None:
if len(self.rows) < self.capture_limit:
self.rows.append(dict(row))
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
self.record(row)
class ClickHouseKernelJournal:
"""Fire-and-forget ClickHouse journal.
The sink is a small callable of the form ``sink(table_name, row_dict)``.
"""
def __init__(self, sink: Optional[JournalSink] = None):
self.sink = sink
def record(self, row: Dict[str, Any]) -> None:
if self.sink is not None:
self.sink("dita_kernel_debug", row)
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
self.record(_transition_row(transition=transition, slot=slot, event=event, control=control))
def _transition_row(
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent],
control: Optional[KernelControlSnapshot],
) -> Dict[str, Any]:
return {
"ts": transition.timestamp.isoformat() if hasattr(transition.timestamp, "isoformat") else str(transition.timestamp),
"trade_id": transition.trade_id,
"slot_id": transition.slot_id,
"prev_state": transition.prev_state.value,
"next_state": transition.next_state.value,
"trigger": transition.trigger,
"intent_id": transition.intent_id,
"event_id": transition.event_id,
"control_mode": transition.control_mode,
"control_verbosity": transition.control_verbosity,
"slot_state": slot.to_dict(),
"event_payload": json_safe(event) if event is not None else {},
"control_snapshot": control.as_dict() if control is not None else {},
"slot_state_json": json_text(slot.to_dict()),
}

View File

@@ -1,8 +0,0 @@
"""Compatibility shim for the Rust-backed DITAv2 execution kernel."""
from __future__ import annotations
from .rust_backend import ExecutionKernel
__all__ = ["ExecutionKernel"]

View File

@@ -1,350 +0,0 @@
"""Operator-facing bootstrap helpers for DITAv2.
This module keeps the wiring explicit:
- control plane selection
- Zinc plane selection
- projection sink selection
- venue adapter selection
The defaults stay safe and testable. Real shared-memory or live BingX wiring
is only enabled when the caller opts in via arguments or environment.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
import asyncio
import inspect
import os
from pathlib import Path
from typing import Any, Optional
from dotenv import load_dotenv
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from .bingx_venue import BingxVenueAdapter
from .control import BackendMode
from .control import ControlPlane
from .control import ControlUpdate
from .control import KernelControlSnapshot
from .control import KernelMode
from .control import KernelVerbosity
from .control import build_control_plane
from .mock_venue import MockVenueAdapter
from .mock_venue import MockVenueScenario
from .projection import HazelcastProjection
from .projection import build_projection
from .real_control_plane import RealZincControlPlane
from .real_control_plane import RealZincUnavailable
from .real_zinc_plane import RealZincPlane
from .real_zinc_plane import RealZincUnavailable as RealZincPlaneUnavailable
from .rust_backend import ExecutionKernel
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane
from .zinc_plane import ZincPlane
PROJECT_ROOT = Path(__file__).resolve().parents[3]
load_dotenv(PROJECT_ROOT / ".env")
class LauncherVenueMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
class LauncherZincMode(str, Enum):
IN_MEMORY = "IN_MEMORY"
REAL = "REAL"
@dataclass
class DITAv2LauncherBundle:
"""Concrete runtime components assembled by the launcher."""
kernel: ExecutionKernel
control_plane: ControlPlane
projection: HazelcastProjection
zinc_plane: ZincPlane
venue: VenueAdapter
def close(self) -> None:
_maybe_close(self.venue)
_maybe_close(self.zinc_plane)
_maybe_close(self.control_plane)
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _resolve_control_mode() -> KernelMode | None:
raw = _env_upper("DITA_V2_MODE", "")
if raw == KernelMode.DEBUG.value:
return KernelMode.DEBUG
if raw == KernelMode.NORMAL.value:
return KernelMode.NORMAL
return None
def _resolve_control_verbosity() -> KernelVerbosity | None:
raw = _env_upper("DITA_V2_VERBOSITY", "")
if raw == KernelVerbosity.TRACE.value:
return KernelVerbosity.TRACE
if raw == KernelVerbosity.VERBOSE.value:
return KernelVerbosity.VERBOSE
if raw == KernelVerbosity.QUIET.value:
return KernelVerbosity.QUIET
return None
def _resolve_backend_mode() -> BackendMode | None:
raw = _env_upper("DITA_V2_BACKEND_MODE", "")
if raw == BackendMode.BINGX.value:
return BackendMode.BINGX
if raw == BackendMode.MOCK.value:
return BackendMode.MOCK
return None
def _control_update_from_env() -> ControlUpdate | None:
fields: dict[str, Any] = {}
mode = _resolve_control_mode()
if mode is not None:
fields["mode"] = mode
verbosity = _resolve_control_verbosity()
if verbosity is not None:
fields["verbosity"] = verbosity
backend_mode = _resolve_backend_mode()
if backend_mode is not None:
fields["backend_mode"] = backend_mode
raw = os.environ.get("DITA_V2_DEBUG_CLICKHOUSE")
if raw is not None:
fields["debug_clickhouse_enabled"] = _env_bool("DITA_V2_DEBUG_CLICKHOUSE", True)
raw = os.environ.get("DITA_V2_TRACE_TRANSITIONS")
if raw is not None:
fields["trace_transitions"] = _env_bool("DITA_V2_TRACE_TRANSITIONS", False)
raw = os.environ.get("DITA_V2_MIRROR_TO_HAZELCAST")
if raw is not None:
fields["mirror_to_hazelcast"] = _env_bool("DITA_V2_MIRROR_TO_HAZELCAST", True)
raw = os.environ.get("DITA_V2_ACTIVE_SLOT_LIMIT")
if raw is not None:
try:
fields["active_slot_limit"] = max(1, int(str(raw).strip()))
except Exception:
pass
raw = os.environ.get("DITA_V2_RECONCILE_ON_RESTART")
if raw is not None:
fields["reconcile_on_restart"] = _env_bool("DITA_V2_RECONCILE_ON_RESTART", True)
return ControlUpdate(**fields) if fields else None
def _resolve_venue_mode(venue_mode: Optional[str] = None) -> LauncherVenueMode:
raw = _env_upper("DITA_V2_VENUE", venue_mode or LauncherVenueMode.MOCK.value)
if raw == LauncherVenueMode.BINGX.value:
return LauncherVenueMode.BINGX
return LauncherVenueMode.MOCK
def _resolve_zinc_mode(zinc_mode: Optional[str] = None) -> LauncherZincMode:
raw = _env_upper("DITA_V2_ZINC", zinc_mode or LauncherZincMode.IN_MEMORY.value)
if raw == LauncherZincMode.REAL.value:
return LauncherZincMode.REAL
return LauncherZincMode.IN_MEMORY
def _resolve_hazelcast_real(prefer_real_hazelcast: Optional[bool] = None) -> bool:
if prefer_real_hazelcast is not None:
return bool(prefer_real_hazelcast)
raw = _env_upper("DITA_V2_HAZELCAST", "")
return raw in {"REAL", "REAL_HZ", "HAZELCAST"}
def build_bingx_exec_client_config(
*,
environment: Optional[BingxEnvironment] = None,
allow_mainnet: Optional[bool] = None,
recv_window_ms: Optional[int] = None,
default_leverage: Optional[int] = None,
exchange_leverage_cap: Optional[int] = None,
prefer_websocket: Optional[bool] = None,
sizing_mode: Optional[str] = None,
) -> BingxExecClientConfig:
"""Build the direct BingX config used by the DITAv2 launcher."""
resolved_environment = environment or (
BingxEnvironment.LIVE if _env_upper("DOLPHIN_BINGX_ENV", "VST") == "LIVE" else BingxEnvironment.VST
)
resolved_allow_mainnet = _env_bool("DOLPHIN_BINGX_ALLOW_MAINNET", False) if allow_mainnet is None else bool(allow_mainnet)
resolved_recv_window = int(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "5000")) if recv_window_ms is None else int(recv_window_ms)
resolved_default_leverage = int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")) if default_leverage is None else int(default_leverage)
resolved_exchange_cap = int(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")) if exchange_leverage_cap is None else int(exchange_leverage_cap)
resolved_prefer_ws = _env_bool("DOLPHIN_BINGX_PREFER_WEBSOCKET", False) if prefer_websocket is None else bool(prefer_websocket)
resolved_sizing_mode = sizing_mode or os.environ.get("DOLPHIN_BINGX_SIZING_MODE", "testnet")
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=resolved_environment,
allow_mainnet=resolved_allow_mainnet,
recv_window_ms=max(1, resolved_recv_window),
default_leverage=max(1, resolved_default_leverage),
exchange_leverage_cap=max(1, resolved_exchange_cap),
prefer_websocket=resolved_prefer_ws,
sizing_mode=resolved_sizing_mode,
journal_strategy=os.environ.get("DOLPHIN_BINGX_JOURNAL_STRATEGY", "dita_v2"),
journal_db=os.environ.get("DOLPHIN_BINGX_JOURNAL_DB", "dolphin_pink"),
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
def _build_control_plane(
*,
prefix: str,
control_plane: Optional[ControlPlane] = None,
) -> ControlPlane:
plane = control_plane or build_control_plane(prefix=prefix)
update = _control_update_from_env()
if update is not None:
plane.update(update)
return plane
def _build_zinc_plane(
*,
prefix: str,
slot_count: int,
zinc_mode: Optional[LauncherZincMode] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> ZincPlane:
if zinc_plane is not None:
return zinc_plane
resolved_mode = zinc_mode or _resolve_zinc_mode()
if resolved_mode is LauncherZincMode.REAL:
try:
return RealZincPlane(prefix=prefix, slot_count=slot_count, create=True)
except (RealZincPlaneUnavailable, RealZincUnavailable, Exception):
pass
return InMemoryZincPlane()
def _build_venue(
*,
venue_mode: Optional[LauncherVenueMode] = None,
mock_scenario: Optional[MockVenueScenario] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
venue: Optional[VenueAdapter] = None,
) -> VenueAdapter:
if venue is not None:
return venue
resolved_mode = venue_mode or _resolve_venue_mode()
if resolved_mode is LauncherVenueMode.BINGX:
backend = bingx_backend
if backend is None:
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(bingx_config or build_bingx_exec_client_config())
return BingxVenueAdapter(backend=backend)
return MockVenueAdapter(mock_scenario)
def _maybe_close(obj: Any) -> None:
for method_name in ("close", "disconnect"):
method = getattr(obj, method_name, None)
if method is None:
continue
try:
result = method()
except TypeError:
continue
if inspect.isawaitable(result):
try:
asyncio.run(result)
except RuntimeError:
pass
break
def build_launcher_bundle(
*,
max_slots: int = 10,
prefix: Optional[str] = None,
control_plane: Optional[ControlPlane] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
venue: Optional[VenueAdapter] = None,
venue_mode: Optional[LauncherVenueMode | str] = None,
zinc_mode: Optional[LauncherZincMode | str] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
mock_scenario: Optional[MockVenueScenario] = None,
) -> DITAv2LauncherBundle:
"""Build a fully wired DITAv2 runtime bundle.
Defaults stay non-destructive:
- in-memory Zinc plane
- in-process control plane
- mock venue
- callback projection unless a Hazelcast client is supplied
"""
resolved_prefix = (prefix or os.environ.get("DITA_V2_PREFIX", "dita_v2")).strip() or "dita_v2"
if isinstance(venue_mode, LauncherVenueMode):
resolved_venue_mode = venue_mode
elif isinstance(venue_mode, str):
resolved_venue_mode = LauncherVenueMode(venue_mode.strip().upper())
else:
resolved_venue_mode = None
if isinstance(zinc_mode, LauncherZincMode):
resolved_zinc_mode = zinc_mode
elif isinstance(zinc_mode, str):
resolved_zinc_mode = LauncherZincMode(zinc_mode.strip().upper())
else:
resolved_zinc_mode = None
active_control_plane = _build_control_plane(prefix=resolved_prefix, control_plane=control_plane)
control_snapshot = active_control_plane.read()
active_projection = projection or build_projection(
client=projection_client,
prefer_real_hazelcast=_resolve_hazelcast_real(),
control_snapshot=control_snapshot,
)
active_zinc_plane = _build_zinc_plane(
prefix=resolved_prefix,
slot_count=int(max_slots),
zinc_mode=resolved_zinc_mode,
zinc_plane=zinc_plane,
)
active_venue = _build_venue(
venue_mode=resolved_venue_mode,
mock_scenario=mock_scenario,
bingx_config=bingx_config,
bingx_backend=bingx_backend,
venue=venue,
)
kernel = ExecutionKernel(
max_slots=int(max_slots),
control_plane=active_control_plane,
venue=active_venue,
projection=active_projection,
projection_client=projection_client,
zinc_plane=active_zinc_plane,
)
return DITAv2LauncherBundle(
kernel=kernel,
control_plane=active_control_plane,
projection=active_projection,
zinc_plane=active_zinc_plane,
venue=active_venue,
)

View File

@@ -1,209 +0,0 @@
"""Deterministic mock venue for DITAv2 tests."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
import itertools
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .venue import VenueAdapter
@dataclass(frozen=True)
class MockVenueScenario:
"""Failure knobs for the mock venue."""
reject_entries: bool = False
reject_exits: bool = False
partial_fill_ratio: float = 1.0
cancel_reject: bool = False
emit_ack_before_fill: bool = True
emit_fill_on_submit: bool = False
entry_partial_fill_ratio: float = 1.0
exit_partial_fill_ratio: float = 1.0
class MockVenueAdapter(VenueAdapter):
"""Scriptable mock venue with BingX-shaped response semantics."""
def __init__(self, scenario: Optional[MockVenueScenario] = None):
self.scenario = scenario or MockVenueScenario()
self._order_seq = itertools.count(1)
self._event_seq = itertools.count(1)
self._open_orders: Dict[str, VenueOrder] = {}
self._open_positions: Dict[str, Dict[str, Any]] = {}
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
is_entry = intent.action == KernelCommandType.ENTER
should_reject = self.scenario.reject_entries if is_entry else self.scenario.reject_exits
order_id = f"V-{next(self._order_seq):08d}"
client_id = f"{intent.trade_id}:{intent.intent_id}"
order = VenueOrder(
internal_trade_id=intent.trade_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=intent.side,
intended_size=float(intent.target_size),
status=VenueOrderStatus.NEW,
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "slot_id": intent.slot_id, "asset": intent.asset},
)
if should_reject:
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.REJECTED,
metadata=dict(order.metadata),
)
return [self._event_from_order(intent, order, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, reason="MOCK_REJECT")]
self._open_orders[order_id] = order
events: List[VenueEvent] = []
if self.scenario.emit_ack_before_fill or not self.scenario.emit_fill_on_submit:
events.append(self._event_from_order(intent, order, KernelEventKind.ORDER_ACK, VenueEventStatus.ACKED))
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
if is_entry:
effective_ratio = self.scenario.entry_partial_fill_ratio if self.scenario.entry_partial_fill_ratio != 1.0 else self.scenario.partial_fill_ratio
else:
effective_ratio = self.scenario.exit_partial_fill_ratio if self.scenario.exit_partial_fill_ratio != 1.0 else self.scenario.partial_fill_ratio
fill_ratio = max(0.0, min(1.0, float(effective_ratio)))
fill_size = float(intent.target_size) * fill_ratio
event_kind = KernelEventKind.FULL_FILL if fill_ratio >= 1.0 else KernelEventKind.PARTIAL_FILL
event_status = VenueEventStatus.FILLED if fill_ratio >= 1.0 else VenueEventStatus.PARTIALLY_FILLED
fill_event = self._event_from_order(
intent,
order,
event_kind,
event_status,
price=float(intent.reference_price or 0.0),
fill_size=fill_size,
remaining_size=max(0.0, float(intent.target_size) - fill_size),
)
events.append(fill_event)
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=fill_size,
average_fill_price=float(intent.reference_price or 0.0),
status=VenueOrderStatus.FILLED if fill_ratio >= 1.0 else VenueOrderStatus.PARTIALLY_FILLED,
metadata=dict(order.metadata),
)
self._open_orders[order_id] = order
return events
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
if self.scenario.cancel_reject:
return [
self._event_from_order(
self._dummy_intent(order),
order,
KernelEventKind.CANCEL_REJECT,
VenueEventStatus.CANCELED_REJECTED,
reason=reason or "MOCK_CANCEL_REJECT",
)
]
existing = self._open_orders.get(order.venue_order_id, order)
canceled = VenueOrder(
internal_trade_id=existing.internal_trade_id,
venue_order_id=existing.venue_order_id,
venue_client_id=existing.venue_client_id,
side=existing.side,
intended_size=existing.intended_size,
filled_size=existing.filled_size,
average_fill_price=existing.average_fill_price,
status=VenueOrderStatus.CANCELED,
metadata=dict(existing.metadata),
)
self._open_orders.pop(order.venue_order_id, None)
return [
self._event_from_order(
self._dummy_intent(order),
canceled,
KernelEventKind.CANCEL_ACK,
VenueEventStatus.CANCELED,
reason=reason or "MOCK_CANCEL_ACK",
)
]
def open_orders(self) -> List[VenueOrder]:
return list(self._open_orders.values())
def open_positions(self) -> List[Dict[str, Any]]:
return list(self._open_positions.values())
def reconcile(self) -> List[VenueEvent]:
return []
def _dummy_intent(self, order: VenueOrder) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=order.venue_client_id,
trade_id=order.internal_trade_id,
slot_id=int(order.metadata.get("slot_id", 0)),
asset=str(order.metadata.get("asset", "")),
side=order.side,
action=KernelCommandType.EXIT if order.metadata.get("action") == "EXIT" else KernelCommandType.ENTER,
reference_price=float(order.metadata.get("reference_price", 0.0)),
target_size=float(order.intended_size),
leverage=float(order.metadata.get("leverage", 1.0)),
reason=str(order.metadata.get("reason", "")),
metadata=dict(order.metadata),
)
def _event_from_order(
self,
intent: KernelIntent,
order: VenueOrder,
kind: KernelEventKind,
status: VenueEventStatus,
*,
price: Optional[float] = None,
fill_size: float = 0.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{next(self._event_seq):08d}",
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=kind,
status=status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=intent.asset,
price=float(price if price is not None else intent.reference_price or 0.0),
size=float(intent.target_size),
filled_size=float(fill_size),
remaining_size=float(remaining_size),
reason=reason,
raw_payload={
"status": status.value,
"orderId": order.venue_order_id,
"clientOrderId": order.venue_client_id,
"symbol": intent.asset,
"side": order.side.value,
"action": intent.action.value,
},
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
return event

View File

@@ -1,97 +0,0 @@
"""Hazelcast-compatible projection helpers for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
import os
from typing import Any, Callable, Dict, Iterable, List, Optional
from .account import AccountProjection
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .journal import _transition_row
from .utils import json_safe
Writer = Callable[[str, Dict[str, Any]], None]
@dataclass
class HazelcastProjection:
"""Projection helper for BLUE/PINK-compatible durable writes."""
active_slots_map: str = "hz:dita_active_slots"
trade_events_topic: str = "hz:dita_trade_events"
control_map: str = "hz:dita_control"
writer: Optional[Writer] = None
control_snapshot: Optional[KernelControlSnapshot] = None
def write_slot(self, slot: TradeSlot) -> Dict[str, Any]:
row = build_position_state_row(slot, self.control_snapshot)
if self.writer is not None:
self.writer(self.active_slots_map, row)
return row
def write_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> Dict[str, Any]:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
if self.writer is not None:
self.writer(self.trade_events_topic, row)
return row
def write_control(self, control: KernelControlSnapshot) -> Dict[str, Any]:
self.control_snapshot = control
row = control.as_dict()
if self.writer is not None:
self.writer(self.control_map, row)
return row
def build_projection(
*,
writer: Optional[Writer] = None,
client: Optional[Any] = None,
prefer_real_hazelcast: Optional[bool] = None,
control_snapshot: Optional[KernelControlSnapshot] = None,
) -> HazelcastProjection:
"""Build the active projection helper with an operator-visible switch.
The default remains the callback-based projection helper. If a Hazelcast
client is supplied and the caller opts in via ``prefer_real_hazelcast`` or
``DITA_V2_HAZELCAST=REAL``, the helper routes directly through the
client-backed map/topic writer path.
"""
env_choice = os.environ.get("DITA_V2_HAZELCAST", "").strip().upper()
real_requested = prefer_real_hazelcast if prefer_real_hazelcast is not None else env_choice in {"REAL", "REAL_HZ", "HAZELCAST"}
if real_requested and client is not None:
try:
from .hazelcast_projection import HazelcastRowWriter
writer = HazelcastRowWriter(client)
except Exception:
pass
return HazelcastProjection(writer=writer, control_snapshot=control_snapshot)
def build_position_state_row(slot: TradeSlot, control: Optional[KernelControlSnapshot] = None) -> Dict[str, Any]:
"""Build a state row shaped for durable compatibility."""
row = slot.to_dict()
row.update(
{
"runtime_namespace": control.runtime_namespace if control else "dita_v2",
"strategy_namespace": control.strategy_namespace if control else "dita_v2",
"event_namespace": control.event_namespace if control else "dita_v2",
"actor_name": control.actor_name if control else "ExecutionKernel",
"exec_venue": control.exec_venue if control else "bingx",
"data_venue": control.data_venue if control else "binance",
"ledger_authority": control.ledger_authority if control else "exchange",
}
)
return row

View File

@@ -1,129 +0,0 @@
"""Real Zinc-backed control plane for DITAv2."""
from __future__ import annotations
import json
import struct
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from .control import BackendMode, ControlPlane, ControlUpdate, KernelControlSnapshot, KernelMode, KernelVerbosity
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.append(str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincControlPlane(ControlPlane):
"""Shared-memory Zinc-backed control plane."""
def __init__(self, *, prefix: str, create: bool = True) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.region_name = f"{base}_control"
self._seq = 0
self._snapshot = KernelControlSnapshot()
if create:
self.region = SharedRegion.create(self.region_name, 1 << 20)
self._write_region(self._seq, self._snapshot.as_dict())
else:
self.region = SharedRegion.open(self.region_name)
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if isinstance(control, dict):
self._snapshot = KernelControlSnapshot(**control)
def close(self) -> None:
self.region.close()
def read(self) -> KernelControlSnapshot:
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._snapshot
self._snapshot = KernelControlSnapshot(**control)
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
self._snapshot = update.apply(self.read())
self._seq += 1
self._write_region(self._seq, self._snapshot.as_dict())
return self._snapshot
def mirror(self) -> Dict[str, Any]:
return self._snapshot.as_dict()
def wait(self, timeout_ms: int = 1000) -> bool:
try:
return bool(self.region.wait(timeout_ms))
except Exception:
return False
def notify(self) -> None:
try:
self.region.notify()
except Exception:
pass
def _write_region(self, seq: int, control: Dict[str, Any]) -> None:
packet = _encode_packet(seq, {"control": control})
buf = self.region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc control region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[: len(packet)] = packet
if len(view) > len(packet):
view[len(packet) :] = b"\x00" * (len(view) - len(packet))
try:
self.region.notify()
except Exception:
pass

View File

@@ -1,263 +0,0 @@
"""Real Zinc-backed hot-path plane for DITAv2.
This wrapper uses the Zinc Python adapter directly. The kernel still talks to
the narrow ``ZincPlane`` interface; this module just makes that interface real.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import json
import os
import struct
import sys
import threading
from .contracts import KernelIntent, TradeSide, TradeSlot, TradeStage, VenueOrder, VenueOrderStatus
from .control import KernelControlSnapshot
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.append(str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
data = slot.to_dict()
return data
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
active_entry_order = None
active_exit_order = None
if isinstance(payload.get("active_entry_order"), dict):
active_entry_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_entry_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_entry_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_entry_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_entry_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_entry_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_entry_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_entry_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_entry_order"].get("metadata", {})),
)
if isinstance(payload.get("active_exit_order"), dict):
active_exit_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_exit_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_exit_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_exit_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_exit_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_exit_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_exit_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_exit_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_exit_order"].get("metadata", {})),
)
slot = TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=active_exit_order,
active_entry_order=active_entry_order,
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
return slot
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincPlane:
"""Shared-memory Zinc plane used by the Python prototype."""
def __init__(
self,
*,
prefix: str,
slot_count: int = 10,
intent_capacity: int = 1 << 20,
state_capacity: int = 1 << 20,
control_capacity: int = 1 << 20,
create: bool = True,
) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.intent_name = f"{base}_intent"
self.state_name = f"{base}_state"
self.control_name = f"{base}_control"
self._intent_seq = 0
self._state_seq = 0
self._control_seq = 0
self._lock = threading.Lock()
self._slot_cache: Dict[int, TradeSlot] = {i: TradeSlot(slot_id=i) for i in range(int(slot_count))}
self._slot_count = int(slot_count)
self._intent_cache: List[Dict[str, Any]] = []
self._control_cache = KernelControlSnapshot()
if create:
self.intent_region = SharedRegion.create(self.intent_name, intent_capacity)
self.state_region = SharedRegion.create(self.state_name, state_capacity)
self.control_region = SharedRegion.create(self.control_name, control_capacity)
self._write_region(self.control_region, self._control_seq, {"control": self._control_cache.as_dict()})
self._write_region(
self.state_region,
self._state_seq,
{"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)]},
)
self._write_region(self.intent_region, self._intent_seq, {"items": []})
else:
self.intent_region = SharedRegion.open(self.intent_name)
self.state_region = SharedRegion.open(self.state_name)
self.control_region = SharedRegion.open(self.control_name)
control_payload = _decode_packet(self.control_region.as_buffer())
state_payload = _decode_packet(self.state_region.as_buffer())
intent_payload = _decode_packet(self.intent_region.as_buffer())
if isinstance(control_payload.get("control"), dict):
self._control_cache = KernelControlSnapshot(**control_payload["control"])
if isinstance(state_payload.get("slots"), list):
for slot_payload in state_payload["slots"]:
if isinstance(slot_payload, dict):
slot = _slot_from_payload(slot_payload)
self._slot_cache[int(slot.slot_id)] = slot
if isinstance(intent_payload.get("items"), list):
self._intent_cache = list(intent_payload["items"])
def close(self) -> None:
self.intent_region.close()
self.state_region.close()
self.control_region.close()
def publish_intent(self, intent: KernelIntent) -> None:
with self._lock:
self._intent_seq += 1
row = intent.__dict__.copy()
row["timestamp"] = intent.timestamp.isoformat()
row["side"] = intent.side.value
row["action"] = intent.action.value
row["stage"] = intent.stage.value
row["exit_leg_ratios"] = list(intent.exit_leg_ratios)
row["metadata"] = json.loads(json.dumps(intent.metadata, default=_json_default))
self._intent_cache.append(row)
self._write_region(self.intent_region, self._intent_seq, {"items": self._intent_cache[-512:]})
def write_slot(self, slot: TradeSlot) -> None:
with self._lock:
self._state_seq += 1
self._slot_cache[int(slot.slot_id)] = slot
payload = {
"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)],
}
self._write_region(self.state_region, self._state_seq, payload)
def read_slots(self) -> List[TradeSlot]:
payload = _decode_packet(self.state_region.as_buffer())
slots = payload.get("slots", []) if isinstance(payload, dict) else []
return [_slot_from_payload(slot) for slot in sorted(slots, key=lambda row: int(row.get("slot_id", 0)))]
def read_intents(self) -> List[Dict[str, Any]]:
payload = _decode_packet(self.intent_region.as_buffer())
items = payload.get("items", []) if isinstance(payload, dict) else []
return list(items)
def update_control(self, control: KernelControlSnapshot) -> None:
with self._lock:
self._control_seq += 1
self._control_cache = control
self._write_region(self.control_region, self._control_seq, {"control": control.as_dict()})
def read_control(self) -> KernelControlSnapshot:
payload = _decode_packet(self.control_region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._control_cache
return KernelControlSnapshot(**control)
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return bool(self.state_region.wait(timeout_ms))
def notify_state(self) -> None:
self.state_region.notify()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return bool(self.control_region.wait(timeout_ms))
def notify_control(self) -> None:
self.control_region.notify()
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return bool(self.intent_region.wait(timeout_ms))
def notify_intent(self) -> None:
self.intent_region.notify()
def _write_region(self, region: Any, seq: int, payload: Dict[str, Any]) -> None:
packet = _encode_packet(seq, payload)
buf = region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[:] = b"\x00" * len(view)
view[: len(packet)] = packet
region.notify()

View File

@@ -1,753 +0,0 @@
"""Rust-backed DITAv2 execution kernel.
This module keeps the Python API shape stable while moving the kernel state
machine into a Rust shared library. Slot views write through to the backend on
assignment, then the Python side mirrors the resulting state into Zinc and the
existing projections/journals.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence
import ctypes
import json
import math
import os
import subprocess
import sys
from .account import AccountProjection
from .control import ControlPlane, ControlUpdate, KernelControlSnapshot, KernelVerbosity, build_control_plane
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueOrder,
VenueOrderStatus,
VenueEventStatus,
)
from .journal import KernelJournal, MemoryKernelJournal
from .mock_venue import MockVenueAdapter
from .projection import HazelcastProjection
from .projection import build_projection
from .utils import json_safe
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane, ZincPlane
def _repo_root() -> Path:
return Path(__file__).resolve().parents[3]
def _crate_dir() -> Path:
return Path(__file__).resolve().with_name("_rust_kernel")
def _library_path() -> Path:
if sys.platform == "darwin":
name = "libdita_v2_kernel.dylib"
elif os.name == "nt":
name = "dita_v2_kernel.dll"
else:
name = "libdita_v2_kernel.so"
return _crate_dir() / "target" / "release" / name
def _build_library() -> None:
crate_dir = _crate_dir()
if not crate_dir.exists():
raise FileNotFoundError(f"Missing Rust kernel crate: {crate_dir}")
subprocess.run(
["cargo", "build", "--release", "--manifest-path", str(crate_dir / "Cargo.toml")],
cwd=_repo_root(),
check=True,
)
def _ensure_library() -> Path:
path = _library_path()
if not path.exists():
_build_library()
return path
class _RustKernelLib:
def __init__(self) -> None:
path = _ensure_library()
self.lib = ctypes.CDLL(str(path))
self.lib.dita_kernel_create.argtypes = [ctypes.c_size_t]
self.lib.dita_kernel_create.restype = ctypes.c_void_p
self.lib.dita_kernel_destroy.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_destroy.restype = None
self.lib.dita_kernel_free_string.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_free_string.restype = None
self.lib.dita_kernel_get_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
self.lib.dita_kernel_get_slot_json.restype = ctypes.c_void_p
self.lib.dita_kernel_set_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_char_p]
self.lib.dita_kernel_set_slot_json.restype = ctypes.c_int
self.lib.dita_kernel_process_intent_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_process_intent_json.restype = ctypes.c_void_p
self.lib.dita_kernel_on_venue_event_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_on_venue_event_json.restype = ctypes.c_void_p
self.lib.dita_kernel_reconcile_slots_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_reconcile_slots_json.restype = ctypes.c_void_p
self.lib.dita_kernel_snapshot_json.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_snapshot_json.restype = ctypes.c_void_p
def create(self, max_slots: int) -> ctypes.c_void_p:
handle = self.lib.dita_kernel_create(ctypes.c_size_t(max_slots))
if not handle:
raise RuntimeError("dita_kernel_create failed")
return ctypes.c_void_p(handle)
def destroy(self, handle: ctypes.c_void_p) -> None:
if handle and handle.value:
self.lib.dita_kernel_destroy(handle)
def _take_string(self, raw: ctypes.c_void_p) -> str:
if not raw:
raise RuntimeError("Rust kernel returned null string")
text = ctypes.cast(raw, ctypes.c_char_p).value
if text is None:
self.lib.dita_kernel_free_string(raw)
raise RuntimeError("Rust kernel returned empty string")
try:
return text.decode("utf-8")
finally:
self.lib.dita_kernel_free_string(raw)
def get_slot_json(self, handle: ctypes.c_void_p, slot_id: int) -> Dict[str, Any]:
raw = self.lib.dita_kernel_get_slot_json(handle, ctypes.c_size_t(slot_id))
if not raw:
raise IndexError(f"Invalid slot id: {slot_id}")
return json.loads(self._take_string(raw))
def set_slot_json(self, handle: ctypes.c_void_p, slot_id: int, payload: Dict[str, Any]) -> None:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
rc = self.lib.dita_kernel_set_slot_json(handle, ctypes.c_size_t(slot_id), ctypes.c_char_p(encoded))
if rc != 0:
raise RuntimeError(f"dita_kernel_set_slot_json failed rc={rc}")
def process_intent(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_process_intent_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def on_venue_event(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_on_venue_event_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def reconcile_slots(
self,
handle: ctypes.c_void_p,
payload: Sequence[Dict[str, Any]],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(list(payload)), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_reconcile_slots_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def snapshot(self, handle: ctypes.c_void_p) -> Dict[str, Any]:
raw = self.lib.dita_kernel_snapshot_json(handle)
return json.loads(self._take_string(raw))
_RUST: _RustKernelLib | None = None # lazy init — avoids Rust build on import
def _get_rust() -> _RustKernelLib:
global _RUST
if _RUST is None:
_RUST = _RustKernelLib()
return _RUST
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
return slot.to_dict()
def _order_to_payload(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
def _order_from_payload(payload: Optional[Dict[str, Any]], *, trade_id: str) -> Optional[VenueOrder]:
if not isinstance(payload, dict):
return None
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=str(payload.get("venue_order_id", "")),
venue_client_id=str(payload.get("venue_client_id", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
intended_size=float(payload.get("intended_size", 0.0)),
filled_size=float(payload.get("filled_size", 0.0)),
average_fill_price=float(payload.get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload.get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload.get("metadata", {})),
)
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
return TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=_order_from_payload(payload.get("active_exit_order"), trade_id=str(payload.get("trade_id", ""))),
active_entry_order=_order_from_payload(payload.get("active_entry_order"), trade_id=str(payload.get("trade_id", ""))),
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
def _first_invalid_intent_field(intent: KernelIntent) -> Optional[tuple[str, float]]:
"""Return (field, value) for the first non-finite or out-of-bounds numeric
field on an intent, or None if all are sane. Guards the kernel boundary
against inf/NaN that would otherwise crash serde_json serialization."""
scalar_checks = (
("target_size", float(intent.target_size if intent.target_size is not None else 0.0)),
("reference_price", float(intent.reference_price if intent.reference_price is not None else 0.0)),
("leverage", float(intent.leverage if intent.leverage is not None else 0.0)),
("limit_price", float(getattr(intent, "limit_price", 0.0) or 0.0)),
)
for name, value in scalar_checks:
if not math.isfinite(value):
return (name, value)
for idx, ratio in enumerate(intent.exit_leg_ratios or ()): # type: ignore[union-attr]
rv = float(ratio if ratio is not None else 0.0)
if not math.isfinite(rv):
return (f"exit_leg_ratios[{idx}]", rv)
size = float(intent.target_size if intent.target_size is not None else 0.0)
if size < 0.0:
return ("target_size", size)
return None
def _intent_to_payload(intent: KernelIntent) -> Dict[str, Any]:
return {
"timestamp": intent.timestamp.isoformat() if hasattr(intent.timestamp, "isoformat") else str(intent.timestamp),
"intent_id": intent.intent_id,
"trade_id": intent.trade_id,
"slot_id": intent.slot_id,
"asset": intent.asset,
"side": intent.side.value,
"action": intent.action.value,
"reference_price": float(intent.reference_price or 0.0),
"target_size": float(intent.target_size or 0.0),
"leverage": float(intent.leverage or 0.0),
"exit_leg_ratios": list(intent.exit_leg_ratios),
"reason": intent.reason,
"metadata": dict(intent.metadata),
"stage": intent.stage.value,
"order_type": getattr(intent, "order_type", "MARKET"),
"limit_price": float(getattr(intent, "limit_price", 0.0) or 0.0),
}
def _event_to_payload(event: VenueEvent) -> Dict[str, Any]:
return {
"timestamp": event.timestamp.isoformat() if hasattr(event.timestamp, "isoformat") else str(event.timestamp),
"event_id": event.event_id,
"trade_id": event.trade_id,
"slot_id": event.slot_id,
"kind": event.kind.value,
"status": event.status.value,
"venue_order_id": event.venue_order_id,
"venue_client_id": event.venue_client_id,
"side": event.side.value,
"asset": event.asset,
"price": float(event.price or 0.0),
"size": float(event.size or 0.0),
"filled_size": float(event.filled_size or 0.0),
"remaining_size": float(event.remaining_size or 0.0),
"reason": event.reason,
"raw_payload": dict(event.raw_payload),
"metadata": dict(event.metadata),
}
def _transition_from_payload(payload: Dict[str, Any]) -> KernelTransition:
return KernelTransition(
timestamp=datetime.fromisoformat(payload["timestamp"]),
trade_id=str(payload.get("trade_id", "")),
slot_id=int(payload.get("slot_id", 0)),
prev_state=TradeStage(str(payload.get("prev_state", TradeStage.IDLE.value))),
next_state=TradeStage(str(payload.get("next_state", TradeStage.IDLE.value))),
trigger=str(payload.get("trigger", "")),
intent_id=str(payload.get("intent_id", "")),
event_id=str(payload.get("event_id", "")),
control_mode=str(payload.get("control_mode", "")),
control_verbosity=str(payload.get("control_verbosity", "")),
details=dict(payload.get("details", {})),
)
def _outcome_from_payload(payload: Dict[str, Any]) -> KernelOutcome:
return KernelOutcome(
accepted=bool(payload.get("accepted", False)),
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
state=TradeStage(str(payload.get("state", TradeStage.IDLE.value))),
diagnostic_code=KernelDiagnosticCode(str(payload.get("diagnostic_code", KernelDiagnosticCode.OK.value))),
severity=KernelSeverity(str(payload.get("severity", KernelSeverity.INFO.value))),
transitions=tuple(_transition_from_payload(row) for row in payload.get("transitions", [])),
emitted_events=tuple(
VenueEvent(
timestamp=datetime.fromisoformat(row["timestamp"]),
event_id=str(row.get("event_id", "")),
trade_id=str(row.get("trade_id", "")),
slot_id=int(row.get("slot_id", 0)),
kind=KernelEventKind(str(row.get("kind", KernelEventKind.ORDER_ACK.value))),
status=VenueEventStatus(str(row.get("status", VenueEventStatus.ACKED.value))),
venue_order_id=str(row.get("venue_order_id", "")),
venue_client_id=str(row.get("venue_client_id", "")),
side=TradeSide(str(row.get("side", TradeSide.FLAT.value))),
asset=str(row.get("asset", "")),
price=float(row.get("price", 0.0)),
size=float(row.get("size", 0.0)),
filled_size=float(row.get("filled_size", 0.0)),
remaining_size=float(row.get("remaining_size", 0.0)),
reason=str(row.get("reason", "")),
raw_payload=dict(row.get("raw_payload", {})),
metadata=dict(row.get("metadata", {})),
)
for row in payload.get("emitted_events", [])
),
details=dict(payload.get("details", {})),
)
def _enum_text(value: Any) -> str:
if hasattr(value, "value"):
return str(getattr(value, "value"))
return str(value)
class KernelSlotView:
"""Write-through view over a Rust-backed slot."""
def __init__(self, kernel: "ExecutionKernel", slot_id: int) -> None:
object.__setattr__(self, "_kernel", kernel)
object.__setattr__(self, "_slot_id", int(slot_id))
@property
def slot_id(self) -> int:
return object.__getattribute__(self, "_slot_id")
def _snapshot(self) -> TradeSlot:
return self._kernel._get_slot(self.slot_id)
def __getattr__(self, name: str) -> Any:
slot = self._snapshot()
if hasattr(slot, name):
return getattr(slot, name)
raise AttributeError(name)
def __setattr__(self, name: str, value: Any) -> None:
if name in {"_kernel", "_slot_id"}:
object.__setattr__(self, name, value)
return
slot = self._snapshot()
if not hasattr(slot, name):
raise AttributeError(name)
setattr(slot, name, value)
self._kernel._set_slot(slot)
def to_dict(self) -> Dict[str, Any]:
return self._snapshot().to_dict()
def is_free(self) -> bool:
return self._snapshot().is_free()
def is_open(self) -> bool:
return self._snapshot().is_open()
def mark_price(self, price: float) -> None:
slot = self._snapshot()
slot.mark_price(price)
self._kernel._set_slot(slot)
def next_exit_ratio(self) -> float:
return self._snapshot().next_exit_ratio()
def consume_exit_leg(self) -> float:
slot = self._snapshot()
ratio = slot.consume_exit_leg()
self._kernel._set_slot(slot)
return ratio
def attach_entry_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_entry_order = order
self._kernel._set_slot(slot)
def attach_exit_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_exit_order = order
self._kernel._set_slot(slot)
def __repr__(self) -> str: # pragma: no cover - debugging helper
return f"KernelSlotView(slot_id={self.slot_id}, state={self._snapshot().fsm_state.value})"
class KernelStateView:
def __init__(self, kernel: "ExecutionKernel") -> None:
self._kernel = kernel
self.slots = [KernelSlotView(kernel, slot_id) for slot_id in range(kernel.max_slots)]
self.active_trade_index: Dict[str, int] = {}
self.venue_order_index: Dict[str, int] = {}
self.client_order_index: Dict[str, int] = {}
self.refresh()
def refresh(self) -> None:
snapshot = self._kernel._snapshot_backend()
self.active_trade_index = dict(snapshot.get("active_trade_index", {}))
self.venue_order_index = dict(snapshot.get("venue_order_index", {}))
self.client_order_index = dict(snapshot.get("client_order_index", {}))
class ExecutionKernel:
"""Rust-backed multi-slot execution kernel."""
def __init__(
self,
*,
max_slots: int = 10,
control_plane: Optional[ControlPlane] = None,
venue: Optional[VenueAdapter] = None,
journal: Optional[KernelJournal] = None,
account: Optional[AccountProjection] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> None:
self.max_slots = int(max_slots)
self.control_plane = control_plane or build_control_plane()
self.venue = venue or MockVenueAdapter()
self.journal = journal or MemoryKernelJournal()
self.account = account or AccountProjection()
self.projection = projection or build_projection(client=projection_client)
self.zinc_plane = zinc_plane or InMemoryZincPlane()
self._backend = _get_rust().create(self.max_slots)
self._control_snapshot = self.control_plane.read()
self._last_settled_pnl: Dict[int, float] = {}
self.projection.write_control(self._control_snapshot)
self.zinc_plane.update_control(self._control_snapshot)
self.state = KernelStateView(self)
self.account.observe_slots([self._get_slot(slot_id) for slot_id in range(self.max_slots)])
def __del__(self) -> None: # pragma: no cover - cleanup best effort
backend = getattr(self, "_backend", None)
if backend is not None:
try:
_get_rust().destroy(backend)
except Exception:
pass
@property
def control(self) -> KernelControlSnapshot:
return self.control_plane.read()
def update_control(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.control_plane.update(update)
self._control_snapshot = snapshot
self.projection.write_control(snapshot)
self.zinc_plane.update_control(snapshot)
return snapshot
def _snapshot_backend(self) -> Dict[str, Any]:
return _get_rust().snapshot(self._backend)
def _get_slot(self, slot_id: int) -> TradeSlot:
return _slot_from_payload(_get_rust().get_slot_json(self._backend, slot_id))
def _set_slot(self, slot: TradeSlot, *, journal: bool = False) -> None:
payload = _slot_to_payload(slot)
_get_rust().set_slot_json(self._backend, slot.slot_id, payload)
self.state.refresh()
slots = [self._get_slot(slot_id) for slot_id in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
def slot(self, slot_id: int) -> KernelSlotView:
if not (0 <= int(slot_id) < self.max_slots):
raise IndexError(slot_id)
return self.state.slots[int(slot_id)]
def free_slot(self) -> Optional[KernelSlotView]:
for slot in self.state.slots:
if slot.is_free():
return slot
return None
def _record_transitions(self, transitions: Iterable[KernelTransition], slot: TradeSlot, event: Optional[VenueEvent]) -> None:
if self.control.debug_clickhouse_enabled:
for transition in transitions:
self.journal.record_transition(
transition=transition,
slot=slot,
event=event,
control=self.control,
)
def process_intent(self, intent: KernelIntent) -> KernelOutcome:
self.zinc_plane.publish_intent(intent)
if not (0 <= int(intent.slot_id) < self.max_slots):
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=TradeStage.IDLE,
diagnostic_code=KernelDiagnosticCode.INVALID_SLOT_ID,
details={"reason": "INVALID_SLOT_ID", "slot_id": int(intent.slot_id), "intent_id": intent.intent_id},
)
# Finiteness / sanity guard at the kernel boundary. A non-finite (inf/NaN)
# numeric field would make the Rust core's serde_json serialization return
# a null string (panic). Reject cleanly with INVALID_INTENT instead, naming
# the offending field + value so the upstream numerical source can be located.
bad_field = _first_invalid_intent_field(intent)
if bad_field is not None:
name, value = bad_field
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=self._get_slot(int(intent.slot_id)).fsm_state,
diagnostic_code=KernelDiagnosticCode.INVALID_INTENT,
severity=KernelSeverity.WARNING,
details={
"reason": "INVALID_INTENT",
"field": name,
"value": str(value),
"intent_id": intent.intent_id,
"action": intent.action.value,
"asset": intent.asset,
},
)
payload = _intent_to_payload(intent)
result = _get_rust().process_intent(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
if intent.action == KernelCommandType.ENTER and outcome.accepted:
self._last_settled_pnl[intent.slot_id] = 0.0
emitted_events = []
all_venue_transitions: List[KernelTransition] = []
if intent.action in {KernelCommandType.ENTER, KernelCommandType.EXIT}:
emitted_events = self.venue.submit(intent)
for event in emitted_events:
evt_outcome = self.on_venue_event(event)
all_venue_transitions.extend(evt_outcome.transitions)
elif intent.action == KernelCommandType.CANCEL:
slot_view = self.slot(intent.slot_id)
if slot_view.active_exit_order is not None:
emitted_events = self.venue.cancel(slot_view.active_exit_order, reason=intent.reason)
elif slot_view.active_entry_order is not None and slot_view.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.ORDER_REQUESTED,
TradeStage.ORDER_SENT,
TradeStage.IDLE,
}:
emitted_events = self.venue.cancel(slot_view.active_entry_order, reason=intent.reason)
else:
emitted_events = []
for event in emitted_events:
evt_outcome = self.on_venue_event(event)
all_venue_transitions.extend(evt_outcome.transitions)
final_slot = self._get_slot(outcome.slot_id)
rate_limit_event = next((event for event in emitted_events if event.kind == KernelEventKind.RATE_LIMITED), None)
if rate_limit_event is not None:
rate_limit_details = dict(outcome.details)
rate_limit_details.update(
{
"reason": rate_limit_event.reason or "RATE_LIMITED",
"retry_after_ms": int(rate_limit_event.metadata.get("retry_after_ms", 0) or 0),
"venue_event_kind": rate_limit_event.kind.value,
"severity": KernelSeverity.WARNING.value,
"release_eta": "few minutes",
"retryable": True,
}
)
outcome = KernelOutcome(
accepted=False,
slot_id=outcome.slot_id,
trade_id=outcome.trade_id,
state=final_slot.fsm_state,
diagnostic_code=KernelDiagnosticCode.RATE_LIMITED,
severity=KernelSeverity.WARNING,
transitions=outcome.transitions,
emitted_events=outcome.emitted_events,
details=rate_limit_details,
)
all_transitions = list(outcome.transitions) + all_venue_transitions
final_outcome = KernelOutcome(
accepted=outcome.accepted,
slot_id=outcome.slot_id,
trade_id=final_slot.trade_id,
state=final_slot.fsm_state,
diagnostic_code=outcome.diagnostic_code,
transitions=tuple(all_transitions),
emitted_events=tuple(emitted_events),
details=dict(outcome.details),
)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(final_slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, final_slot, None)
return final_outcome
def on_venue_event(self, event: VenueEvent) -> KernelOutcome:
result = _get_rust().on_venue_event(
self._backend,
_event_to_payload(event),
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
# An INVALID_* fallback result carries a null slot; fall back to the
# kernel's current slot so settlement/bookkeeping stays consistent.
slot_payload = result.get("slot")
slot = _slot_from_payload(slot_payload) if slot_payload else self._get_slot(int(outcome.slot_id))
self.state.refresh()
incremental_pnl = slot.realized_pnl - self._last_settled_pnl.get(slot.slot_id, 0.0)
if abs(incremental_pnl) > 1e-12:
self.account.settle(incremental_pnl)
self._last_settled_pnl[slot.slot_id] = slot.realized_pnl
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, slot, event)
return outcome
def mark_price(self, asset: str, price: float) -> None:
for slot in self.state.slots:
if slot.asset == asset and slot.is_open():
slot.mark_price(price)
self.account.observe_slots([self._get_slot(i) for i in range(self.max_slots)])
def reconcile_from_slots(self, slots: Sequence[TradeSlot]) -> KernelOutcome:
payload = [_slot_to_payload(slot) for slot in slots]
result = _get_rust().reconcile_slots(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
if not outcome.accepted:
return outcome
self.state.refresh()
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
for current in slots:
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
return outcome
def snapshot(self) -> Dict[str, Any]:
return {
"control": self.control.as_dict(),
"slots": [self._get_slot(slot.slot_id).to_dict() for slot in self.state.slots],
"account": {
"capital": self.account.snapshot.capital,
"equity": self.account.snapshot.equity,
"realized_pnl": self.account.snapshot.realized_pnl,
"unrealized_pnl": self.account.snapshot.unrealized_pnl,
"open_positions": self.account.snapshot.open_positions,
"open_notional": self.account.snapshot.open_notional,
"leverage": self.account.snapshot.leverage,
},
}

View File

@@ -0,0 +1,336 @@
"""Gate G2: AccountProjectionV2 offline battery.
Tests cover:
- K-value fold (seed + realized − fee − funding)
- Fee and funding subtraction from capital
- Margin computation (used / available)
- Reconcile rules R1–R6 (OK / WARN / ERROR boundaries)
- Snapshot immutability and atomicity (new snapshot per event)
- Replay determinism (same events → same snapshot)
- V1 backward compatibility (AccountProjection untouched)
"""
from __future__ import annotations
import math
import sys
sys.path.insert(0, "/mnt/dolphinng5_predict")
import pytest
from prod.clean_arch.dita_v2.account import (
AccountProjectionV2,
AccountSnapshotV2,
EBlock,
EPosition,
KBlock,
ReconcileConfig,
ReconcileResult,
ReconcileStatus,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _proj(seed: float = 10_000.0, **kw) -> AccountProjectionV2:
return AccountProjectionV2(seed, **kw)
def _snap(proj: AccountProjectionV2, slots=None) -> AccountSnapshotV2:
return proj.build_snapshot("test_event", slots or [], ts=1_000_000.0)
# ---------------------------------------------------------------------------
# 1. K-value fold
# ---------------------------------------------------------------------------
class TestKFold:
def test_seed_only(self):
proj = _proj(10_000.0)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(10_000.0)
assert snap.k.realized_pnl == 0.0
assert snap.k.fees_paid == 0.0
assert snap.k.funding_paid == 0.0
def test_realized_adds_to_capital(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=100.0, fill_qty=1.0, fee=0.0, realized_pnl=500.0)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(10_500.0)
assert snap.k.realized_pnl == pytest.approx(500.0)
def test_fee_subtracts_from_capital(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=100.0, fill_qty=1.0, fee=3.5, realized_pnl=0.0)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(9_996.5)
assert snap.k.fees_paid == pytest.approx(3.5)
def test_funding_subtracts_from_capital(self):
proj = _proj(10_000.0)
proj.apply_funding(7.25)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(9_992.75)
assert snap.k.funding_paid == pytest.approx(7.25)
def test_combined_fold(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=50.0, fill_qty=2.0, fee=2.0, realized_pnl=100.0)
proj.apply_funding(5.0)
proj.apply_fill(fill_price=55.0, fill_qty=2.0, fee=2.5, realized_pnl=-30.0)
snap = _snap(proj)
# capital = 10000 + 100 - 2 - 5 + (-30) - 2.5 = 10060.5
assert snap.k.capital == pytest.approx(10_060.5)
assert snap.k.realized_pnl == pytest.approx(70.0)
assert snap.k.fees_paid == pytest.approx(4.5)
assert snap.k.funding_paid == pytest.approx(5.0)
def test_equity_includes_unrealized(self):
proj = _proj(10_000.0)
snap = _snap(proj)
assert snap.k.equity == snap.k.capital # no open positions
def test_peak_capital_tracks_high_water(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=500.0)
_ = _snap(proj)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=-200.0)
snap = _snap(proj)
assert snap.k.peak_capital == pytest.approx(10_500.0)
assert snap.k.capital == pytest.approx(10_300.0)
def test_min_capital_clamp(self):
proj = _proj(100.0, min_capital=50.0)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=-200.0)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(50.0)
def test_max_capital_clamp(self):
proj = _proj(100.0, max_capital=150.0)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=200.0)
snap = _snap(proj)
assert snap.k.capital == pytest.approx(150.0)
def test_non_finite_fee_ignored(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=float("inf"), realized_pnl=0.0)
snap = _snap(proj)
# inf fee → _safe returns 0.0
assert math.isfinite(snap.k.capital)
# ---------------------------------------------------------------------------
# 2. Margin computation
# ---------------------------------------------------------------------------
class TestMarginComputation:
def test_available_margin_no_positions(self):
proj = _proj(10_000.0)
snap = _snap(proj)
assert snap.k.used_margin == pytest.approx(0.0)
assert snap.k.available_margin == pytest.approx(10_000.0)
def test_available_never_negative(self):
proj = _proj(100.0)
proj.apply_balance_update(
wallet_balance=100.0,
available_margin=0.0,
used_margin=200.0,
maint_margin=10.0,
)
snap = _snap(proj)
assert snap.k.available_margin >= 0.0
# ---------------------------------------------------------------------------
# 3. E-fact ingestion
# ---------------------------------------------------------------------------
class TestEFacts:
def test_balance_update_stored(self):
proj = _proj(10_000.0)
proj.apply_balance_update(
wallet_balance=9_800.0,
available_margin=9_000.0,
used_margin=800.0,
maint_margin=40.0,
)
snap = _snap(proj)
assert snap.e.wallet_balance == pytest.approx(9_800.0)
assert snap.e.available_margin == pytest.approx(9_000.0)
assert snap.e.used_margin == pytest.approx(800.0)
assert snap.e.maint_margin == pytest.approx(40.0)
def test_position_update_stored(self):
proj = _proj(10_000.0)
positions = [EPosition(symbol="BTC-USDT", qty=0.1, entry_price=60_000.0, leverage=10.0, side="LONG")]
proj.apply_position_update(positions)
snap = _snap(proj)
assert len(snap.e.positions) == 1
assert snap.e.positions[0].symbol == "BTC-USDT"
def test_fill_e_facts_stored(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=50_000.0, fill_qty=0.02, fee=1.5, realized_pnl=100.0)
snap = _snap(proj)
assert snap.e.last_fill_price == pytest.approx(50_000.0)
assert snap.e.last_fill_qty == pytest.approx(0.02)
assert snap.e.last_fill_fee == pytest.approx(1.5)
assert snap.e.last_fill_realized_pnl == pytest.approx(100.0)
def test_funding_e_fact_stored(self):
proj = _proj(10_000.0)
proj.apply_funding(3.75)
snap = _snap(proj)
assert snap.e.last_funding == pytest.approx(3.75)
# ---------------------------------------------------------------------------
# 4. Reconcile rules R1–R6
# ---------------------------------------------------------------------------
class TestReconcileRules:
def _proj_with_balance(self, capital: float, wallet: float) -> AccountProjectionV2:
cfg = ReconcileConfig(capital_epsilon=0.01, pending_fee_bound=10.0)
proj = AccountProjectionV2(capital, reconcile_config=cfg)
proj.apply_balance_update(
wallet_balance=wallet,
available_margin=wallet,
used_margin=0.0,
maint_margin=0.0,
)
return proj
# R1 — capital vs wallet balance
def test_r1_ok(self):
proj = self._proj_with_balance(10_000.0, 10_000.0)
snap = _snap(proj)
assert snap.reconcile.status == ReconcileStatus.OK
def test_r1_warn_unsettled_fee(self):
proj = self._proj_with_balance(10_000.0, 9_995.0)
# delta = 5.0 < pending_fee_bound=10.0 → WARN
snap = _snap(proj)
assert snap.reconcile.status == ReconcileStatus.WARN
assert "capital_vs_wallet" in snap.reconcile.worst_field
def test_r1_error_unexplained(self):
proj = self._proj_with_balance(10_000.0, 9_980.0)
# delta = 20.0 > pending_fee_bound=10.0 → ERROR
snap = _snap(proj)
assert snap.reconcile.status == ReconcileStatus.ERROR
# R2 — realized PnL rounding
def test_r2_warn_rounding(self):
cfg = ReconcileConfig(capital_epsilon=0.001, realized_rounding=0.05)
proj = AccountProjectionV2(10_000.0, reconcile_config=cfg)
proj.apply_fill(fill_price=100.0, fill_qty=1.0, fee=0.0, realized_pnl=99.97)
proj._e_last_fill_realized = 100.0 # exchange says 100.0, K says 99.97
# delta = 0.03 < realized_rounding=0.05 → WARN
snap = _snap(proj)
assert snap.reconcile.status in {ReconcileStatus.WARN, ReconcileStatus.OK}
# R6 — position count mismatch → ERROR
def test_r6_count_mismatch(self):
proj = _proj(10_000.0)
proj.apply_position_update([
EPosition(symbol="BTC-USDT", qty=0.1, side="LONG"),
EPosition(symbol="ETH-USDT", qty=1.0, side="SHORT"),
])
# K thinks 0 open (no slots), E thinks 2 → ERROR
snap = _snap(proj)
assert snap.reconcile.status == ReconcileStatus.ERROR
assert "open_positions" in snap.reconcile.worst_field
def test_r1_ignored_when_no_e_facts(self):
# E-facts not yet received (wallet_balance=0) → R1 skipped → OK
proj = _proj(10_000.0)
snap = _snap(proj)
assert snap.reconcile.status == ReconcileStatus.OK
# ---------------------------------------------------------------------------
# 5. Snapshot immutability and event_seq
# ---------------------------------------------------------------------------
class TestSnapshotAtomicity:
def test_event_seq_increments(self):
proj = _proj(10_000.0)
s1 = _snap(proj)
s2 = _snap(proj)
s3 = _snap(proj)
assert s1.event_seq == 1
assert s2.event_seq == 2
assert s3.event_seq == 3
def test_snapshot_is_immutable(self):
proj = _proj(10_000.0)
snap = _snap(proj)
with pytest.raises((AttributeError, TypeError)):
snap.k = KBlock() # frozen dataclass
def test_old_snapshot_not_mutated(self):
proj = _proj(10_000.0)
s1 = _snap(proj)
capital_before = s1.k.capital
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=500.0)
_ = _snap(proj)
assert s1.k.capital == capital_before # immutable — unchanged
def test_snapshot_property_returns_latest(self):
proj = _proj(10_000.0)
s1 = _snap(proj)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=0.0, realized_pnl=100.0)
s2 = _snap(proj)
assert proj.snapshot is s2
assert proj.snapshot.event_seq == 2
# ---------------------------------------------------------------------------
# 6. Replay determinism
# ---------------------------------------------------------------------------
class TestReplayDeterminism:
def _apply_sequence(self, proj: AccountProjectionV2) -> AccountSnapshotV2:
proj.apply_fill(fill_price=50_000.0, fill_qty=0.1, fee=2.5, realized_pnl=100.0)
proj.apply_funding(1.25)
proj.apply_fill(fill_price=51_000.0, fill_qty=0.1, fee=2.6, realized_pnl=-50.0)
proj.apply_balance_update(
wallet_balance=10_043.35,
available_margin=10_043.35,
used_margin=0.0,
maint_margin=0.0,
)
return proj.build_snapshot("final", [], ts=999.0)
def test_same_events_same_snapshot(self):
snap1 = self._apply_sequence(_proj(10_000.0))
snap2 = self._apply_sequence(_proj(10_000.0))
assert snap1.k.capital == pytest.approx(snap2.k.capital)
assert snap1.k.realized_pnl == pytest.approx(snap2.k.realized_pnl)
assert snap1.k.fees_paid == pytest.approx(snap2.k.fees_paid)
assert snap1.k.funding_paid == pytest.approx(snap2.k.funding_paid)
assert snap1.reconcile.status == snap2.reconcile.status
def test_capital_formula_matches_manual(self):
proj = _proj(10_000.0)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=2.5, realized_pnl=100.0)
proj.apply_funding(1.25)
proj.apply_fill(fill_price=1.0, fill_qty=1.0, fee=2.6, realized_pnl=-50.0)
snap = _snap(proj)
expected = 10_000.0 + 100.0 - 2.5 - 1.25 + (-50.0) - 2.6
assert snap.k.capital == pytest.approx(expected)
# ---------------------------------------------------------------------------
# 7. V1 backward compatibility (AccountProjection must be untouched)
# ---------------------------------------------------------------------------
class TestV1Compat:
def test_v1_still_works(self):
from prod.clean_arch.dita_v2.account import AccountProjection, AccountSnapshot
proj = AccountProjection()
proj.settle(100.0)
assert proj.snapshot.capital == pytest.approx(25_100.0)
assert proj.snapshot.realized_pnl == pytest.approx(100.0)

View File

@@ -1,779 +0,0 @@
"""Comprehensive test battery for all 13 CRITICAL DITAv2 flaws.
Each test verifies that the specific flaw exists (pre-fix) and would pass
once the flaw is addressed. Tests use the MockVenueAdapter to avoid
requiring live BingX connectivity.
Run with:
python -m pytest prod/clean_arch/dita_v2/test_flaws.py -v
"""
from __future__ import annotations
import sys
sys.path.insert(0, "/mnt/dolphinng5_predict")
from datetime import datetime, timezone
from typing import Any, Dict, List
import pytest
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from prod.clean_arch.dita_v2.mock_venue import MockVenueAdapter, MockVenueScenario
from prod.clean_arch.dita_v2.rust_backend import ExecutionKernel
from prod.clean_arch.dita_v2.account import AccountProjection
E = KernelCommandType
TS = TradeSide
def _mk_intent(
action: KernelCommandType = KernelCommandType.ENTER,
trade_id: str = "t1",
slot_id: int = 0,
asset: str = "BTCUSDT",
side: TradeSide = TradeSide.SHORT,
price: float = 100.0,
size: float = 1.0,
leverage: float = 1.0,
exit_leg_ratios: tuple = (1.0,),
**kw,
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=kw.pop("intent_id", trade_id),
trade_id=trade_id,
slot_id=slot_id,
asset=asset,
side=side,
action=action,
reference_price=price,
target_size=size,
leverage=leverage,
exit_leg_ratios=exit_leg_ratios,
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
)
def _mk_venue_event(
kind: KernelEventKind,
trade_id: str = "t1",
slot_id: int = 0,
side: TradeSide = TradeSide.SHORT,
asset: str = "BTCUSDT",
price: float = 100.0,
size: float = 1.0,
filled_size: float = 1.0,
remaining_size: float = 0.0,
event_id: str = "",
venue_order_id: str = "V-1",
venue_client_id: str = "t1:t1",
status: VenueEventStatus = VenueEventStatus.FILLED,
reason: str = "",
) -> VenueEvent:
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=event_id or f"ev-{kind.value}-{trade_id}",
trade_id=trade_id,
slot_id=slot_id,
kind=kind,
status=status,
venue_order_id=venue_order_id,
venue_client_id=venue_client_id,
side=side,
asset=asset,
price=price,
size=size,
filled_size=filled_size,
remaining_size=remaining_size,
reason=reason,
)
def _fresh_kernel(
*,
scenario: MockVenueScenario = None,
max_slots: int = 2,
capital: float = 25000.0,
) -> ExecutionKernel:
venue = MockVenueAdapter(scenario=scenario or MockVenueScenario())
k = ExecutionKernel(max_slots=max_slots, venue=venue)
k.account.snapshot.capital = capital
k.account.snapshot.peak_capital = capital
k.account.snapshot.equity = capital
return k
# ============================================================
# FLAW 1: Entry-order cancellation is structurally broken
# ============================================================
class TestFlaw1EntryCancel:
"""CANCEL intent for entry orders must work, not just exit orders."""
def test_cancel_entry_order_accepted_by_rust(self):
"""Rust kernel must accept CANCEL for an entry order in ENTRY_WORKING."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
r = k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce1"))
assert r.accepted, f"ENTER rejected: {r.diagnostic_code}"
slot = k._get_slot(0)
assert slot.fsm_state in {TradeStage.ORDER_REQUESTED, TradeStage.ENTRY_WORKING}
cancel_result = k.process_intent(_mk_intent(action=E.CANCEL, trade_id="ce1"))
assert cancel_result.accepted, (
f"CANCEL for entry order should be accepted, got "
f"accepted={cancel_result.accepted} "
f"diag={cancel_result.diagnostic_code}"
)
def test_cancel_entry_order_calls_venue_cancel(self):
"""Python bridge must call venue.cancel() on active_entry_order."""
scenario = MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False)
k = _fresh_kernel(scenario=scenario)
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce2"))
entry_order = k.slot(0).active_entry_order
assert entry_order is not None, "Entry order should be attached"
cancel_result = k.process_intent(_mk_intent(action=E.CANCEL, trade_id="ce2"))
assert cancel_result.accepted, f"CANCEL not accepted: {cancel_result.diagnostic_code}"
def test_cancel_entry_no_fill_returns_to_idle(self):
"""After cancelling an entry order that hasn't filled, slot must be IDLE."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce3"))
k.process_intent(_mk_intent(action=E.CANCEL, trade_id="ce3"))
slot = k._get_slot(0)
assert slot.is_free(), (
f"Slot should be free/IDLE after entry cancel, "
f"got state={slot.fsm_state} closed={slot.closed} "
f"entry_order={slot.active_entry_order} exit_order={slot.active_exit_order} "
f"size={slot.size}"
)
def test_cancel_entry_with_partial_fill(self):
"""Cancel entry with partial fill should leave slot in correct state."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.5))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce4", size=0.002))
slot_after = k._get_slot(0)
assert slot_after.size > 0, "Should have partial fill"
def test_cancel_entry_then_reenter(self):
"""After entry cancel, a new ENTER should succeed."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce5a"))
k.process_intent(_mk_intent(action=E.CANCEL, trade_id="ce5a"))
r = k.process_intent(_mk_intent(action=E.ENTER, trade_id="ce5b"))
assert r.accepted, f"Re-entry after cancel should succeed: {r.diagnostic_code}"
# ============================================================
# FLAW 2: Rust CANCEL_ACK has no entry-order reset path
# ============================================================
class TestFlaw2CancelAckEntry:
"""CANCEL_ACK for entry orders must reset slot to IDLE."""
def test_cancel_ack_resets_entry_working_to_idle(self):
"""When CANCEL_ACK arrives for an entry order, slot goes IDLE."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ca1"))
slot = k._get_slot(0)
assert slot.active_entry_order is not None
venue_order = slot.active_entry_order
ack = _mk_venue_event(
kind=KernelEventKind.CANCEL_ACK,
trade_id="ca1",
venue_order_id=venue_order.venue_order_id,
venue_client_id=venue_order.venue_client_id,
status=VenueEventStatus.CANCELED,
)
k.on_venue_event(ack)
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.IDLE, (
f"Slot should be IDLE after CANCEL_ACK on entry, got {slot.fsm_state}"
)
assert slot.active_entry_order is None, "Entry order should be cleared"
assert slot.trade_id == "", "Trade ID should be cleared"
assert slot.size == 0.0, "Size should be zero"
def test_cancel_ack_exit_still_works(self):
"""Existing exit-order CANCEL_ACK path must still work.
Deterministic setup: entry fills fully (POSITION_OPEN) but the exit only
partially fills, so the exit order stays live and the CANCEL_ACK exit
branch is genuinely exercised (no vacuous guard).
"""
k = _fresh_kernel(scenario=MockVenueScenario(exit_partial_fill_ratio=0.5))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ca2", size=0.002))
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.POSITION_OPEN, (
f"Entry should fill fully, got {slot.fsm_state}"
)
k.process_intent(_mk_intent(action=E.EXIT, trade_id="ca2", size=0.002))
slot = k._get_slot(0)
assert slot.active_exit_order is not None, (
"Exit order must remain live after a partial exit fill"
)
ack = _mk_venue_event(
kind=KernelEventKind.CANCEL_ACK,
trade_id="ca2",
venue_order_id=slot.active_exit_order.venue_order_id,
venue_client_id=slot.active_exit_order.venue_client_id,
status=VenueEventStatus.CANCELED,
)
k.on_venue_event(ack)
slot = k._get_slot(0)
assert slot.active_exit_order is None, "Exit order should be cleared by CANCEL_ACK"
assert slot.fsm_state == TradeStage.POSITION_OPEN, (
f"Exit cancel must return slot to POSITION_OPEN, got {slot.fsm_state}"
)
# ============================================================
# FLAW 3: Outcome mixes pre/post-venue state
# ============================================================
class TestFlaw3OutcomeConsistency:
"""process_intent outcome should have consistent state and transitions."""
def test_outcome_state_matches_actual_slot(self):
"""The outcome.state should reflect the final state after venue events."""
k = _fresh_kernel()
result = k.process_intent(_mk_intent(action=E.ENTER, trade_id="oc1"))
slot = k._get_slot(0)
assert result.state == slot.fsm_state, (
f"Outcome state {result.state} != actual slot state {slot.fsm_state}"
)
def test_outcome_transitions_includes_venue_events(self):
"""Transitions should include venue-event-triggered transitions."""
k = _fresh_kernel()
result = k.process_intent(_mk_intent(action=E.ENTER, trade_id="oc2"))
transition_triggers = [t.trigger for t in result.transitions]
assert len(result.transitions) >= 1, (
f"Should have at least 1 transition, got triggers: {transition_triggers}"
)
# ============================================================
# FLAW 4: Multi-leg exit final leg can double-close
# ============================================================
class TestFlaw4DoubleClose:
"""Multi-leg exit final leg should only close once."""
def test_single_close_after_final_leg(self):
"""After the last leg fills, slot.closed should be set exactly once."""
k = _fresh_kernel(scenario=MockVenueScenario())
k.process_intent(
_mk_intent(
action=E.ENTER,
trade_id="dc1",
size=0.002,
exit_leg_ratios=(0.5, 1.0),
)
)
k.process_intent(
_mk_intent(
action=E.EXIT,
trade_id="dc1",
size=0.001,
exit_leg_ratios=(0.5, 1.0),
)
)
k.process_intent(
_mk_intent(
action=E.EXIT,
trade_id="dc1",
size=0.001,
exit_leg_ratios=(1.0,),
)
)
slot = k._get_slot(0)
assert slot.closed, "Slot should be closed after final leg"
assert slot.fsm_state == TradeStage.CLOSED
def test_no_extra_entry_order_clear_on_close(self):
"""After close via multi-leg, active_entry_order should be consistent."""
k = _fresh_kernel(scenario=MockVenueScenario())
k.process_intent(
_mk_intent(
action=E.ENTER,
trade_id="dc2",
size=0.002,
exit_leg_ratios=(0.5, 1.0),
)
)
k.process_intent(
_mk_intent(
action=E.EXIT,
trade_id="dc2",
size=0.001,
exit_leg_ratios=(0.5, 1.0),
)
)
k.process_intent(
_mk_intent(
action=E.EXIT,
trade_id="dc2",
size=0.001,
exit_leg_ratios=(1.0,),
)
)
slot = k._get_slot(0)
assert slot.active_exit_order is None, "Exit order should be cleared"
assert slot.active_entry_order is None or slot.active_entry_order.status == VenueOrderStatus.FILLED
# ============================================================
# FLAW 5: Capital settlement only triggers on terminal states
# ============================================================
class TestFlaw5CapitalSettleOnPartialFill:
"""Realized PnL should settle incrementally on partial fills."""
def test_partial_exit_settles_pnl_incrementally(self):
"""Exit fill must settle realized PnL into capital — EXACTLY.
This is the single most important invariant in DITAv2: capital is
the kernel account's authority and must move by precisely the
realized PnL of the fill (no balance-poll overwrite). The entry and
exit prices differ so realized PnL is strictly nonzero and the
capital-change assertion fires unconditionally (no vacuous guard).
"""
k = _fresh_kernel()
cap_before = k.account.snapshot.capital
# SHORT entry at 100.
k.process_intent(
_mk_intent(action=E.ENTER, trade_id="ps1", side=TradeSide.SHORT, price=100.0, size=0.002)
)
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.POSITION_OPEN
# Exit at 90 -> SHORT closes in profit, realized PnL strictly positive.
k.process_intent(
_mk_intent(action=E.EXIT, trade_id="ps1", side=TradeSide.SHORT, price=90.0, size=0.002)
)
slot = k._get_slot(0)
assert slot.realized_pnl > 0.0, (
f"SHORT exit below entry must realize positive PnL, got {slot.realized_pnl}"
)
cap_after = k.account.snapshot.capital
# Single-authority invariant: capital moved by EXACTLY realized PnL.
assert abs((cap_after - cap_before) - slot.realized_pnl) < 1e-9, (
f"Capital delta {cap_after - cap_before} != realized_pnl {slot.realized_pnl} "
f"(before={cap_before} after={cap_after})"
)
# ============================================================
# FLAW 6: _legacy_intent silently drops order_type and limit_price
# ============================================================
class TestFlaw6LegacyIntentDrop:
"""_legacy_intent must preserve order_type and limit_price."""
def test_legacy_intent_preserves_order_type(self):
"""LegacyIntent conversion must include order_type."""
from prod.clean_arch.dita_v2.bingx_venue import BingxVenueAdapter
intent = _mk_intent(
action=E.ENTER,
trade_id="li1",
order_type="LIMIT",
limit_price=50000.0,
)
legacy = BingxVenueAdapter._legacy_intent(intent)
assert getattr(legacy, "order_type", None) == "LIMIT" or \
legacy.metadata.get("_order_type") == "LIMIT" or \
legacy.metadata.get("order_type") == "LIMIT", (
f"order_type not preserved in legacy intent. "
f"Legacy fields: {dir(legacy)}, metadata: {legacy.metadata}"
)
def test_legacy_intent_preserves_limit_price(self):
"""LegacyIntent conversion must include limit_price."""
from prod.clean_arch.dita_v2.bingx_venue import BingxVenueAdapter
intent = _mk_intent(
action=E.ENTER,
trade_id="li2",
order_type="LIMIT",
limit_price=50000.0,
)
legacy = BingxVenueAdapter._legacy_intent(intent)
assert getattr(legacy, "limit_price", 0) == 50000.0 or \
legacy.metadata.get("_limit_price") == 50000.0 or \
legacy.metadata.get("limit_price") == 50000.0, (
f"limit_price not preserved in legacy intent. "
f"Legacy metadata: {legacy.metadata}"
)
# ============================================================
# FLAW 7: Mock venue partial_fill_ratio applies to both entry and exit
# ============================================================
class TestFlaw7MockVenueRatios:
"""Mock venue should support different ratios for entry vs exit."""
def test_entry_exit_different_ratios(self):
"""Entry can fill fully while exit fills partially."""
k = _fresh_kernel(scenario=MockVenueScenario(
entry_partial_fill_ratio=1.0,
exit_partial_fill_ratio=0.5,
))
r = k.process_intent(_mk_intent(action=E.ENTER, trade_id="mv1", size=0.002))
assert r.accepted
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.POSITION_OPEN, f"Entry should fill fully: {slot.fsm_state}"
def test_per_action_type_ratios(self):
"""entry_partial_fill_ratio and exit_partial_fill_ratio should work independently."""
scenario = MockVenueScenario(
entry_partial_fill_ratio=1.0,
exit_partial_fill_ratio=0.3,
)
k = _fresh_kernel(scenario=scenario)
k.process_intent(_mk_intent(action=E.ENTER, trade_id="mv2", size=0.001))
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.POSITION_OPEN
assert slot.size == 0.001
# ============================================================
# FLAW 8: Per-asset price precision helper does not exist
# ============================================================
class TestFlaw8PricePrecision:
"""_format_price must exist for LIMIT order support."""
def test_format_price_exists_in_bingx_direct(self):
"""BingxDirectExecutionAdapter should have _format_price method."""
try:
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
assert hasattr(BingxDirectExecutionAdapter, "_format_price"), (
"_format_price method missing from BingxDirectExecutionAdapter"
)
except ImportError:
pytest.skip("bingx_direct not importable in this environment")
# ============================================================
# FLAW 9: Cancel path falls back to trade_id as symbol
# ============================================================
class TestFlaw9CancelSymbolFallback:
"""Cancel should use correct asset, not trade_id as fallback symbol."""
def test_cancel_uses_slot_asset_not_trade_id(self):
"""When cancel is called, the asset should come from the slot, not trade_id."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="cs1", asset="TRXUSDT"))
slot = k._get_slot(0)
# ACK-only (no fill) deterministically leaves the entry order live.
assert slot.active_entry_order is not None, (
"ACK-only entry must leave the entry order live for cancel-symbol fallback"
)
metadata = slot.active_entry_order.metadata
assert metadata.get("asset") == "TRXUSDT", (
f"Entry order metadata should contain asset. Got: {metadata}"
)
def test_mock_venue_cancel_event_has_asset(self):
"""Mock venue cancel events should carry the correct asset."""
k = _fresh_kernel(scenario=MockVenueScenario(partial_fill_ratio=0.0, emit_fill_on_submit=False))
k.process_intent(_mk_intent(action=E.ENTER, trade_id="cs2", asset="XRPUSDT"))
slot = k._get_slot(0)
order = slot.active_entry_order
assert order is not None
assert order.metadata.get("asset") is not None or order.metadata.get("slot_id") is not None
# ============================================================
# FLAW 10: Event dedup window is bounded at 64
# ============================================================
class TestFlaw10EventDedup:
"""Event dedup window should be large enough for realistic workloads."""
def test_dedup_window_accepts_many_events(self):
"""A slot should handle > 64 events without dedup eviction."""
k = _fresh_kernel()
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ed1"))
for i in range(70):
ev = _mk_venue_event(
kind=KernelEventKind.MARK_PRICE,
trade_id="ed1",
event_id=f"mp-{i:04d}",
price=100.0 + i * 0.01,
size=0.0,
filled_size=0.0,
)
k.on_venue_event(ev)
slot = k._get_slot(0)
assert len(slot.seen_event_ids) >= 70, (
f"Expected >= 70 seen_event_ids, got {len(slot.seen_event_ids)}"
)
def test_dedup_eviction_does_not_accept_old_event(self):
"""Evicted event IDs should still be rejected (with larger window)."""
k = _fresh_kernel()
k.process_intent(_mk_intent(action=E.ENTER, trade_id="ed2"))
for i in range(70):
ev = _mk_venue_event(
kind=KernelEventKind.MARK_PRICE,
trade_id="ed2",
event_id=f"mp2-{i:04d}",
price=100.0 + i * 0.01,
size=0.0,
filled_size=0.0,
)
k.on_venue_event(ev)
old_ev = _mk_venue_event(
kind=KernelEventKind.MARK_PRICE,
trade_id="ed2",
event_id="mp2-0000",
price=99.0,
size=0.0,
filled_size=0.0,
)
result = k.on_venue_event(old_ev)
assert result.diagnostic_code == KernelDiagnosticCode.DUPLICATE_EVENT, (
f"Old evicted event should still be deduplicated, "
f"got {result.diagnostic_code}"
)
# ============================================================
# FLAW 11: Reconcile is a raw state override with no FSM validation
# ============================================================
class TestFlaw11ReconcileValidation:
"""Reconcile should validate slot state consistency."""
def test_reconcile_rejects_position_open_with_zero_size(self):
"""Reconciling with POSITION_OPEN but zero size should be rejected."""
k = _fresh_kernel()
bad_slot = TradeSlot(
slot_id=0,
fsm_state=TradeStage.POSITION_OPEN,
size=0.0,
asset="BTCUSDT",
trade_id="bad1",
)
result = k.reconcile_from_slots([bad_slot])
slot = k._get_slot(0)
assert slot.fsm_state != TradeStage.POSITION_OPEN or slot.size > 0, (
f"Reconcile should reject POSITION_OPEN with size=0, "
f"got state={slot.fsm_state} size={slot.size}"
)
def test_reconcile_rejects_idle_with_nonzero_size(self):
"""Reconciling with IDLE but nonzero size should be rejected."""
k = _fresh_kernel()
bad_slot = TradeSlot(
slot_id=0,
fsm_state=TradeStage.IDLE,
size=5.0,
asset="BTCUSDT",
trade_id="bad2",
)
result = k.reconcile_from_slots([bad_slot])
slot = k._get_slot(0)
assert slot.size == 0.0 or slot.fsm_state != TradeStage.IDLE, (
f"Reconcile should reject IDLE with size > 0, "
f"got state={slot.fsm_state} size={slot.size}"
)
def test_reconcile_accepts_valid_slot(self):
"""Valid slot data should still reconcile correctly."""
k = _fresh_kernel()
k.process_intent(_mk_intent(action=E.ENTER, trade_id="rv1"))
slot_data = k._get_slot(0)
result = k.reconcile_from_slots([slot_data])
assert result.accepted
# ============================================================
# FLAW 12: Outcome transitions are incomplete — pre-venue only
# ============================================================
class TestFlaw12OutcomeTransitions:
"""process_intent outcome transitions should include venue event transitions."""
def test_transitions_include_post_venue(self):
"""After a full entry cycle, transitions should include ORDER_ACK and FULL_FILL."""
k = _fresh_kernel()
result = k.process_intent(_mk_intent(action=E.ENTER, trade_id="ot1"))
triggers = [t.trigger for t in result.transitions]
assert any(t in triggers for t in ["ENTER_INTENT", "ORDER_ACK", "FULL_FILL"]), (
f"Transitions should include venue event triggers. Got: {triggers}"
)
def test_transitions_count_matches_lifecycle(self):
"""Full entry lifecycle should produce multiple transitions."""
k = _fresh_kernel()
result = k.process_intent(_mk_intent(action=E.ENTER, trade_id="ot2"))
slot = k._get_slot(0)
assert slot.fsm_state in {TradeStage.POSITION_OPEN, TradeStage.ENTRY_WORKING}, (
f"Default full-fill entry must open the position, got {slot.fsm_state}"
)
assert len(result.transitions) >= 2, (
f"Full entry should produce >= 2 transitions "
f"(intent + venue ack/fill), got {len(result.transitions)}: "
f"{[t.trigger for t in result.transitions]}"
)
# ============================================================
# FLAW 13: Unsettled realized PnL on re-entry
# ============================================================
class TestFlaw13UnsettledPnlOnReentry:
"""Re-entry should not silently discard unrealized settled PnL."""
def test_reentry_after_full_close_no_pnl_loss(self):
"""After full close and settle, re-entry should not lose PnL."""
k = _fresh_kernel()
cap_before = k.account.snapshot.capital
k.process_intent(_mk_intent(action=E.ENTER, trade_id="rp1"))
slot = k._get_slot(0)
assert slot.fsm_state == TradeStage.POSITION_OPEN
k.process_intent(
_mk_intent(action=E.EXIT, trade_id="rp1", price=100.5)
)
slot = k._get_slot(0)
assert slot.is_free()
cap_after_first = k.account.snapshot.capital
k.process_intent(_mk_intent(action=E.ENTER, trade_id="rp2"))
k.process_intent(
_mk_intent(action=E.EXIT, trade_id="rp2", price=101.0)
)
cap_after_second = k.account.snapshot.capital
assert cap_after_second > 0, "Capital should remain positive"
assert abs(cap_after_second - cap_before) < cap_before * 0.5
def test_pnl_warning_on_unsettled_reentry(self):
"""Re-entry on a slot with unsettled PnL should at least warn."""
k = _fresh_kernel(scenario=MockVenueScenario())
k.process_intent(_mk_intent(action=E.ENTER, trade_id="rw1"))
k.process_intent(_mk_intent(action=E.EXIT, trade_id="rw1"))
slot = k._get_slot(0)
assert slot.is_free(), "Full close must free the slot for re-entry"
r = k.process_intent(_mk_intent(action=E.ENTER, trade_id="rw2"))
assert r.accepted, "Re-entry on a freed slot must be accepted"
# ============================================================
# REGRESSION: Existing behaviour must not break
# ============================================================
class TestRegression:
"""Ensure existing happy-path scenarios still work."""
def test_basic_entry_exit(self):
k = _fresh_kernel()
cap_before = k.account.snapshot.capital
r1 = k.process_intent(_mk_intent(action=E.ENTER, trade_id="re1"))
assert r1.accepted
r2 = k.process_intent(_mk_intent(action=E.EXIT, trade_id="re1"))
assert r2.accepted
slot = k._get_slot(0)
assert slot.is_free()
def test_multi_leg_exit(self):
k = _fresh_kernel()
k.process_intent(
_mk_intent(action=E.ENTER, trade_id="re2", size=0.002, exit_leg_ratios=(0.5, 1.0))
)
k.process_intent(
_mk_intent(action=E.EXIT, trade_id="re2", size=0.001, exit_leg_ratios=(0.5, 1.0))
)
k.process_intent(
_mk_intent(action=E.EXIT, trade_id="re2", size=0.001, exit_leg_ratios=(1.0,))
)
slot = k._get_slot(0)
assert slot.is_free()
def test_slot_busy_rejection(self):
k = _fresh_kernel()
r1 = k.process_intent(_mk_intent(action=E.ENTER, trade_id="re3a"))
assert r1.accepted
r2 = k.process_intent(_mk_intent(action=E.ENTER, trade_id="re3b"))
assert not r2.accepted
assert r2.diagnostic_code == KernelDiagnosticCode.SLOT_BUSY
def test_exit_on_idle_rejected(self):
k = _fresh_kernel()
r = k.process_intent(_mk_intent(action=E.EXIT, trade_id="re4"))
assert not r.accepted
def test_reconcile_preserves_state(self):
k = _fresh_kernel()
k.process_intent(_mk_intent(action=E.ENTER, trade_id="re5"))
slot_data = k._get_slot(0)
k.reconcile_from_slots([slot_data])
slot_after = k._get_slot(0)
assert slot_after.trade_id == "re5"
def test_dedup_duplicate_event(self):
k = _fresh_kernel()
k.process_intent(_mk_intent(action=E.ENTER, trade_id="re6"))
slot = k._get_slot(0)
dup = _mk_venue_event(
kind=KernelEventKind.FULL_FILL,
trade_id="re6",
event_id="dedup-regression",
price=100.0,
size=1.0,
filled_size=1.0,
)
k.on_venue_event(dup)
result = k.on_venue_event(dup)
assert result.diagnostic_code == KernelDiagnosticCode.DUPLICATE_EVENT
def test_ten_cycles_no_leak(self):
k = _fresh_kernel()
for i in range(10):
k.process_intent(_mk_intent(action=E.ENTER, trade_id=f"tc{i}"))
k.process_intent(_mk_intent(action=E.EXIT, trade_id=f"tc{i}"))
slot = k._get_slot(0)
assert slot.is_free()
assert k.account.snapshot.capital > 0

View File

@@ -1,43 +0,0 @@
"""Utility helpers for the DITAv2 kernel."""
from __future__ import annotations
from dataclasses import asdict, is_dataclass
from datetime import datetime
from enum import Enum
from typing import Any
import json
import math
def safe_float(value: Any, default: float = 0.0) -> float:
"""Return a finite float or ``default``."""
try:
out = float(value)
except Exception:
return default
if not math.isfinite(out):
return default
return out
def json_safe(value: Any) -> Any:
"""Convert enums, dataclasses and datetimes to JSON-safe objects."""
if isinstance(value, Enum):
return value.value
if isinstance(value, datetime):
return value.isoformat()
if is_dataclass(value):
return json_safe(asdict(value))
if isinstance(value, dict):
return {str(key): json_safe(val) for key, val in value.items()}
if isinstance(value, list):
return [json_safe(item) for item in value]
if isinstance(value, tuple):
return [json_safe(item) for item in value]
return value
def json_text(value: Any) -> str:
"""Serialize a value using stable JSON settings."""
return json.dumps(json_safe(value), separators=(",", ":"), ensure_ascii=False, default=str)

View File

@@ -1,37 +0,0 @@
"""Venue adapter contracts for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional, Protocol
from .contracts import (
KernelCommandType,
KernelIntent,
KernelEventKind,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class VenueAdapter(Protocol):
"""Abstract venue adapter used by the kernel."""
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
...
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
...
def open_orders(self) -> List[VenueOrder]:
...
def open_positions(self) -> List[Dict[str, Any]]:
...
def reconcile(self) -> List[VenueEvent]:
...

View File

@@ -1,135 +0,0 @@
"""Python prototype of the Zinc hot-path plane.
This is an in-memory stand-in for the eventual Zinc-backed shared memory
regions. The interface is explicit so the implementation can be swapped later
without touching the kernel logic.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Mapping, Optional, Protocol
import threading
import time
from .contracts import KernelIntent, TradeSlot
from .control import KernelControlSnapshot
class ZincPlane(Protocol):
"""Hot-path plane for intents, state and control."""
def publish_intent(self, intent: KernelIntent) -> None:
...
def write_slot(self, slot: TradeSlot) -> None:
...
def read_slots(self) -> List[TradeSlot]:
...
def update_control(self, control: KernelControlSnapshot) -> None:
...
def read_control(self) -> KernelControlSnapshot:
...
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
...
def notify_intent(self) -> None:
...
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
...
def notify_state(self) -> None:
...
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
...
def notify_control(self) -> None:
...
@dataclass
class InMemoryZincPlane:
"""Simple in-memory Zinc lookalike for Python prototype tests."""
intent_region: List[KernelIntent] = field(default_factory=list)
state_region: Dict[int, TradeSlot] = field(default_factory=dict)
control_region: Optional[KernelControlSnapshot] = None
_intent_seq: int = field(default=0, init=False, repr=False)
_state_seq: int = field(default=0, init=False, repr=False)
_control_seq: int = field(default=0, init=False, repr=False)
_intent_observed_seq: int = field(default=0, init=False, repr=False)
_state_observed_seq: int = field(default=0, init=False, repr=False)
_control_observed_seq: int = field(default=0, init=False, repr=False)
_signal: threading.Condition = field(default_factory=threading.Condition, init=False, repr=False)
def publish_intent(self, intent: KernelIntent) -> None:
with self._signal:
self.intent_region.append(intent)
self._intent_seq += 1
self._signal.notify_all()
def write_slot(self, slot: TradeSlot) -> None:
with self._signal:
self.state_region[int(slot.slot_id)] = slot
self._state_seq += 1
self._signal.notify_all()
def read_slots(self) -> List[TradeSlot]:
return [self.state_region[key] for key in sorted(self.state_region)]
def update_control(self, control: KernelControlSnapshot) -> None:
with self._signal:
self.control_region = control
self._control_seq += 1
self._signal.notify_all()
def read_control(self) -> KernelControlSnapshot:
if self.control_region is None:
return KernelControlSnapshot()
return self.control_region
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_intent_seq", "_intent_observed_seq", timeout_ms)
def notify_intent(self) -> None:
with self._signal:
self._intent_seq += 1
self._signal.notify_all()
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_state_seq", "_state_observed_seq", timeout_ms)
def notify_state(self) -> None:
with self._signal:
self._state_seq += 1
self._signal.notify_all()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_control_seq", "_control_observed_seq", timeout_ms)
def notify_control(self) -> None:
with self._signal:
self._control_seq += 1
self._signal.notify_all()
def _wait_for_change(self, seq_attr: str, observed_attr: str, timeout_ms: int) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = getattr(self, observed_attr)
while getattr(self, seq_attr) == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
setattr(self, observed_attr, getattr(self, seq_attr))
return True

View File

@@ -1,894 +0,0 @@
"""PINK ClickHouse persistence — DITAv2-backed, reads capital from kernel.
Row families preserved (same schema, no new columns):
- policy_events / v7_decision_events
- position_state
- account_events
- status_snapshots
- trade_events
- trade_reconstruction
- trade_exit_legs
- anomaly_events
Capital/peak_capital/trade_seq are read from the kernel's AccountProjection
(single authority). No duplicate tracking in this module.
"""
from __future__ import annotations
import json
import math
from dataclasses import dataclass
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Callable, Mapping, Optional
from prod.clean_arch.dita import AccountProjection, Decision, DecisionAction, Intent, TradeSide, TradeStage
from prod.clean_arch.dita_v2.contracts import KernelDiagnosticCode, KernelEventKind, KernelOutcome
from prod.clean_arch.dita_v2.contracts import KernelSeverity, TradeStage as KernelStage
Writer = Callable[[str, dict[str, Any]], None]
def _json_safe(value: Any) -> Any:
if isinstance(value, Enum):
return value.value
if isinstance(value, dict):
return {str(key): _json_safe(val) for key, val in value.items()}
if isinstance(value, (list, tuple)):
return [_json_safe(item) for item in value]
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
try:
return _json_safe(dict(vars(value)))
except Exception:
pass
return value
def _json_text(value: Any) -> str:
return json.dumps(_json_safe(value), separators=(",", ":"), ensure_ascii=False, default=str)
def _direction(side: TradeSide) -> int:
return -1 if side == TradeSide.SHORT else 1
def _direction_from_str(side: str) -> int:
return -1 if side.upper() in ("SHORT", "SELL") else 1
def _notional(size: float, price: float) -> float:
if not math.isfinite(size) or not math.isfinite(price):
return 0.0
return abs(size) * abs(price)
def _safe_float(value: Any, default: float = 0.0) -> float:
try:
out = float(value)
except Exception:
return default
if not math.isfinite(out):
return default
return out
def _decision_summary(decision: Decision | None) -> dict[str, Any]:
if decision is None:
return {}
return {
"timestamp": decision.timestamp.isoformat() if hasattr(decision.timestamp, "isoformat") else str(decision.timestamp),
"decision_id": decision.decision_id,
"asset": decision.asset,
"action": decision.action.value,
"side": decision.side.value,
"reason": decision.reason,
"confidence": float(decision.confidence or 0.0),
"velocity_divergence": float(decision.velocity_divergence or 0.0),
"irp_alignment": float(decision.irp_alignment or 0.0),
"reference_price": float(decision.reference_price or 0.0),
"target_size": float(decision.target_size or 0.0),
"leverage": float(decision.leverage or 0.0),
"bars_held": int(decision.bars_held or 0),
"stage": decision.stage.value,
"metadata": _json_safe(decision.metadata),
}
def _intent_summary(intent: Intent | None) -> dict[str, Any]:
if intent is None:
return {}
return {
"timestamp": intent.timestamp.isoformat() if hasattr(intent.timestamp, "isoformat") else str(intent.timestamp),
"trade_id": intent.trade_id,
"decision_id": intent.decision_id,
"asset": intent.asset,
"action": intent.action.value,
"side": intent.side.value,
"reason": intent.reason,
"target_size": float(intent.target_size or 0.0),
"leverage": float(intent.leverage or 0.0),
"reference_price": float(intent.reference_price or 0.0),
"confidence": float(intent.confidence or 0.0),
"bars_held": int(intent.bars_held or 0),
"stage": intent.stage.value,
"exit_leg_ratios": [float(r) for r in intent.exit_leg_ratios],
"metadata": _json_safe(intent.metadata),
}
def _outcome_summary(outcome: KernelOutcome | None) -> dict[str, Any]:
if outcome is None:
return {}
return {
"accepted": bool(outcome.accepted),
"slot_id": int(outcome.slot_id),
"trade_id": outcome.trade_id,
"state": outcome.state.value,
"diagnostic_code": outcome.diagnostic_code.value,
"severity": outcome.severity.value,
"details": _json_safe(outcome.details),
}
@dataclass(frozen=True)
class PinkClickHousePersistenceConfig:
"""Row-shape knobs for the PINK ClickHouse mirror."""
strategy: str = "pink"
runtime_namespace: str = "pink"
strategy_namespace: str = "pink"
event_namespace: str = "pink"
actor_name: str = "PinkDirectRuntime"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
initial_capital: float = 25_000.0
max_account_leverage: float = 3.0
exchange_leverage_mode: str = ""
leverage_mapping_rule: str = "round_half_even_linear_0.5_to_9.0_to_1_to_exchange_cap"
class PinkClickHousePersistence:
"""Durable PINK ClickHouse sink — capital reads from kernel AccountProjection."""
def __init__(
self,
account: AccountProjection,
*,
config: PinkClickHousePersistenceConfig | None = None,
sink: Writer | None = None,
v7_sink: Writer | None = None,
) -> None:
self.account = account
self.config = config or PinkClickHousePersistenceConfig(
runtime_namespace=account.runtime_namespace,
strategy_namespace=account.strategy_namespace,
event_namespace=account.event_namespace,
actor_name=account.actor_name,
exec_venue=account.exec_venue,
data_venue=account.data_venue,
ledger_authority=account.ledger_authority,
initial_capital=float(account.snapshot.capital or 25_000.0),
)
self._sink = sink or self._resolve_sink("pink")
self._v7_sink = v7_sink or self._resolve_v7_sink("pink")
# Per-trade incremental leg state for trade_exit_legs row deltas.
# Keyed by trade_id; reset on ENTER. Tracks the cumulative realized PnL
# and remaining size observed at the previous leg so each leg row carries
# an isolated (non-cumulative) pnl_leg / exit_qty.
self._leg_state: dict[str, dict[str, Any]] = {}
@staticmethod
def _resolve_sink(strategy: str) -> Writer:
from prod.ch_writer import ch_put_pink
return ch_put_pink
@staticmethod
def _resolve_v7_sink(strategy: str) -> Writer:
from prod.ch_writer import ch_put_pink_v7
return ch_put_pink_v7
def _capital(self) -> float:
return float(self.account.snapshot.capital or 0.0)
def _peak_capital(self) -> float:
return float(getattr(self.account.snapshot, "peak_capital", self._capital()) or self._capital())
def _trade_seq(self) -> int:
return int(getattr(self.account.snapshot, "trade_seq", 0) or 0)
def _equity(self) -> float:
return float(self.account.snapshot.equity or self._capital())
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def persist_step(
self,
*,
snapshot: Any,
decision: Decision,
intent: Intent,
outcome: KernelOutcome | None = None,
slot_dict: dict[str, Any] | None = None,
acc_dict: dict[str, Any] | None = None,
phase: str = "step",
market_state: Mapping[str, Any] | None = None,
) -> None:
"""Two-phase persist: log the REQUEST, then log the RESULT.
REQUEST (:meth:`persist_request`) — the decision/order that was
submitted (policy_events + a trade_reconstruction ORDER_REQUESTED row).
RESULT (:meth:`persist_result`) — the settled state snapshot plus the
per-fill lifecycle rows, gated on *evidence of an actual fill*. A resting
LIMIT order (ACK only, no fill) therefore emits state snapshots but no
terminal rows; the async-fill pump persists those later via the same
result path. The synchronous-MARKET path is unchanged: its FILL event
(or the slot's filled/closed state) trips the same gate.
"""
self.persist_request(
snapshot=snapshot, decision=decision, intent=intent,
phase=phase, market_state=market_state,
)
self.persist_result(
snapshot=snapshot, decision=decision, intent=intent, outcome=outcome,
slot_dict=slot_dict, phase=phase, market_state=market_state,
)
def persist_request(
self,
*,
snapshot: Any,
decision: Decision,
intent: Intent,
phase: str = "step",
market_state: Mapping[str, Any] | None = None,
) -> None:
"""Phase 1 — log the requested decision/order (no fill data)."""
self._write_policy_event(snapshot, decision, intent, phase=phase)
if decision.action in (DecisionAction.ENTER, DecisionAction.EXIT):
self._write_trade_reconstruction(
snapshot, intent.trade_id,
event_type="ORDER_REQUESTED",
event_id=f"{intent.trade_id}:request:{decision.action.value.lower()}",
payload={
"decision": _decision_summary(decision),
"intent": _intent_summary(intent),
"market_state": _json_safe(market_state or {}),
},
market_state=market_state,
)
def persist_result(
self,
*,
snapshot: Any,
decision: Decision,
intent: Intent,
outcome: KernelOutcome | None = None,
slot_dict: dict[str, Any] | None = None,
phase: str = "step",
market_state: Mapping[str, Any] | None = None,
) -> None:
"""Phase 2 — log the settled state + per-fill lifecycle rows.
The state snapshot rows (account_events, position_state,
status_snapshots) always reflect the current slot. The lifecycle rows
(ENTRY_FILLED / PARTIAL_EXIT / EXIT / trade_events / trade_exit_legs) are
emitted only when a fill is *evidenced* — a FULL/PARTIAL_FILL event in
``outcome.emitted_events``, a closed slot, or a slot whose size dropped
vs the last leg snapshot. A resting LIMIT (ACK only) emits no terminal
rows here.
"""
slot = slot_dict or {}
stage = (
TradeStage(decision.stage.value)
if hasattr(decision.stage, "value")
else TradeStage(decision.stage) if isinstance(decision.stage, str)
else TradeStage.ORDER_REQUESTED
)
status = self._state_label(slot, phase)
self._write_account_event(snapshot, decision, intent, stage=stage, slot_dict=slot)
self._write_position_state(snapshot, decision, intent, slot_dict=slot, stage=stage, status=status, market_state=market_state)
self._write_status_snapshot(snapshot, decision, intent, slot_dict=slot, phase=phase)
if outcome is not None and outcome.diagnostic_code != KernelDiagnosticCode.OK:
self._write_anomaly(
snapshot, decision, intent,
anomaly=outcome.diagnostic_code.value,
origin="ditav2_kernel",
detail=outcome.details,
)
if outcome is None:
# Decision-only step (HOLD): state snapshot already written.
return
events = tuple(outcome.emitted_events or ())
has_fill_evt = any(
e.kind in (KernelEventKind.FULL_FILL, KernelEventKind.PARTIAL_FILL)
for e in events
)
slot_closed = bool(slot.get("closed", False))
cur_size = _safe_float(slot.get("size", 0.0), 0.0)
slot_open = (not slot_closed) and cur_size > 0.0
if decision.action == DecisionAction.ENTER:
# Emit ENTRY_FILLED only once the entry is actually filled (fill event
# or an open slot). A resting LIMIT entry emits nothing here.
if has_fill_evt or slot_open:
self._leg_state[intent.trade_id] = {
"prev_realized": 0.0,
"prev_size": _safe_float(
slot.get("initial_size", slot.get("size", 0.0)), 0.0
) or _safe_float(intent.target_size, 0.0),
"prev_leg_id": "",
}
self._write_trade_reconstruction(
snapshot, intent.trade_id,
event_type="ENTRY_FILLED",
event_id=f"{intent.trade_id}:entry",
payload={
"decision": _decision_summary(decision),
"intent": _intent_summary(intent),
"outcome": _outcome_summary(outcome),
"slot": slot,
"market_state": _json_safe(market_state or {}),
},
market_state=market_state,
)
return
if decision.action != DecisionAction.EXIT:
return
# An exit leg is evidenced by a fill event, a closed slot, or a drop in
# remaining size vs the previous leg snapshot. A resting LIMIT exit (no
# size change) emits nothing until the async-fill pump observes the fill.
prev_size = _safe_float(self._leg_state.get(intent.trade_id, {}).get("prev_size", 0.0), 0.0)
exit_filled = has_fill_evt or slot_closed or (prev_size - cur_size > 1e-12)
if not exit_filled:
return
partial = (not slot_closed) and cur_size > 0.0
# One trade_exit_legs row per exit leg (partial or final), BLUE-schema
# compatible so PINK multi-exit trades reconcile against the same table.
self._write_trade_exit_leg(snapshot, decision, intent, slot, outcome)
self._write_trade_reconstruction(
snapshot, intent.trade_id,
event_type="PARTIAL_EXIT" if partial else "EXIT",
event_id=f"{intent.trade_id}:{'partial' if partial else 'close'}",
payload={
"decision": _decision_summary(decision),
"intent": _intent_summary(intent),
"outcome": _outcome_summary(outcome),
"slot": slot,
"market_state": _json_safe(market_state or {}),
},
market_state=market_state,
)
# Terminal trade event.
if slot_closed:
self._write_trade_event(snapshot, decision, intent, slot, outcome, market_state=market_state)
def persist_fill_events(
self,
*,
snapshot: Any,
events: Any,
slot_dict: dict[str, Any] | None = None,
market_state: Mapping[str, Any] | None = None,
) -> None:
"""Persist a late (async) venue fill drained by the runtime pump.
There is no fresh policy decision for an async fill, so we synthesize a
minimal Decision/Intent from the post-fill slot + event and route it
through :meth:`persist_result`. Direction (ENTER vs EXIT) is inferred
from the slot: a closed slot or a drop in remaining size vs the last leg
snapshot is an EXIT; otherwise an opening fill is an ENTER. Capital
authority remains the kernel — this only logs the settled result.
"""
slot = slot_dict or {}
event_list = tuple(events or ())
trade_id = str(slot.get("trade_id") or "")
asset = str(slot.get("asset") or "")
side = self._slot_side(slot)
closed = bool(slot.get("closed", False))
cur_size = self._slot_size(slot)
leverage = _safe_float(slot.get("leverage", 1.0), 1.0)
price = next((float(getattr(e, "price", 0.0) or 0.0) for e in event_list if getattr(e, "price", 0.0)), 0.0) or self._slot_entry_price(slot)
prev_size = _safe_float(self._leg_state.get(trade_id, {}).get("prev_size", 0.0), 0.0)
is_exit = closed or (prev_size > 0.0 and cur_size < prev_size - 1e-12)
action = DecisionAction.EXIT if is_exit else DecisionAction.ENTER
ts = getattr(snapshot, "timestamp", datetime.now(timezone.utc))
decision = Decision(
timestamp=ts, decision_id=trade_id or "async", asset=asset, action=action,
side=side, reason="ASYNC_FILL", confidence=0.0, velocity_divergence=0.0,
irp_alignment=0.0, reference_price=price, target_size=cur_size,
leverage=leverage, stage=TradeStage.POSITION_UPDATED, metadata={},
)
intent = Intent(
timestamp=ts, trade_id=trade_id, decision_id=trade_id or "async", asset=asset,
action=action, side=side, reason="ASYNC_FILL", target_size=cur_size,
leverage=leverage, reference_price=price, confidence=0.0,
exit_leg_ratios=tuple(slot.get("exit_leg_ratios", (1.0,)) or (1.0,)), metadata={},
)
outcome = KernelOutcome(
accepted=True, slot_id=int(slot.get("slot_id", 0) or 0), trade_id=trade_id,
state=KernelStage.CLOSED if closed else KernelStage.POSITION_OPEN,
diagnostic_code=KernelDiagnosticCode.OK, severity=KernelSeverity.INFO,
transitions=(), emitted_events=event_list, details={"origin": "async_fill_pump"},
)
self.persist_result(
snapshot=snapshot, decision=decision, intent=intent, outcome=outcome,
slot_dict=slot, phase="async_fill", market_state=market_state,
)
def persist_recovery_state(
self,
*,
snapshot: Any,
acc_dict: dict[str, Any] | None = None,
phase: str = "recovery",
event_type: str = "RECOVERY",
market_state: Mapping[str, Any] | None = None,
) -> None:
"""Persist recovery-only state after kernel reconcile."""
slot_dict = acc_dict or {}
self._write_status_snapshot(
snapshot, decision=None, intent=None, slot_dict={}, phase=phase,
)
self._write_account_event(
snapshot, decision=None, intent=None,
stage=TradeStage.TRADE_TERMINAL_WRITTEN,
slot_dict={}, event_type=event_type,
)
self._write_position_state(
snapshot, decision=None, intent=None,
slot_dict={}, stage=TradeStage.TRADE_TERMINAL_WRITTEN,
status=self._state_label({}, phase), market_state=market_state,
)
self._write_trade_reconstruction(
snapshot,
trade_id=acc_dict.get("trade_id", "") if acc_dict else "",
event_type=event_type,
event_id=f"recovery:{phase}",
payload={"acc_dict": _json_safe(acc_dict or {}), "phase": phase, "market_state": _json_safe(market_state or {})},
market_state=market_state,
)
def record_anomaly(
self,
*,
snapshot: Any,
decision: Any,
intent: Any,
anomaly: str,
origin: str = "emergent",
sensor: str = "",
detail: Any = "",
rm_meta: float = 0.0,
) -> None:
"""Persist a DITA anomaly row with legacy-compatible shape."""
self._sink(
"anomaly_events",
{
"ts": snapshot.timestamp.isoformat(),
"decision_id": decision.decision_id,
"trade_id": intent.trade_id,
"symbol": intent.asset,
"anomaly": anomaly,
"origin": origin,
"sensor": sensor,
"detail": _json_text(detail) if not isinstance(detail, str) else detail,
"rm_meta": float(rm_meta),
},
)
# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------
@staticmethod
def _state_label(slot_dict: dict[str, Any], phase: str) -> str:
if slot_dict.get("closed", False):
return "CLOSED"
if slot_dict.get("size", 0) > 0:
if phase.lower().startswith("recovery"):
return "RECOVERED_OPEN"
return "OPEN"
return "FLAT"
def _posture(self, slot_dict: dict[str, Any]) -> str:
if slot_dict.get("closed", False) or not slot_dict.get("size", 0):
return "FLAT"
return str(slot_dict.get("side", "FLAT"))
def _slot_entry_price(self, slot_dict: dict[str, Any]) -> float:
return _safe_float(slot_dict.get("entry_price", 0.0), 0.0)
def _slot_size(self, slot_dict: dict[str, Any]) -> float:
return _safe_float(slot_dict.get("size", 0.0), 0.0)
def _slot_side(self, slot_dict: dict[str, Any]) -> TradeSide:
raw = str(slot_dict.get("side", "FLAT")).upper()
if raw == "SHORT":
return TradeSide.SHORT
if raw == "LONG":
return TradeSide.LONG
return TradeSide.FLAT
def _slot_trade_id(self, slot_dict: dict[str, Any]) -> str:
return str(slot_dict.get("trade_id", ""))
def _slot_asset(self, slot_dict: dict[str, Any]) -> str:
return str(slot_dict.get("asset", ""))
# ------------------------------------------------------------------
# Row writers
# ------------------------------------------------------------------
def _write_anomaly(
self, snapshot: Any, decision: Decision, intent: Intent,
*, anomaly: str, origin: str = "ditav2_kernel", detail: Any = "",
) -> None:
self._sink("anomaly_events", {
"ts": snapshot.timestamp.isoformat(),
"decision_id": decision.decision_id,
"trade_id": intent.trade_id,
"symbol": intent.asset,
"anomaly": anomaly,
"origin": origin,
"sensor": "",
"detail": _json_text(detail) if not isinstance(detail, str) else detail,
"rm_meta": 0.0,
})
def _write_policy_event(
self, snapshot: Any, decision: Decision, intent: Intent, *, phase: str,
) -> None:
price = _safe_float(decision.reference_price, 0.0)
quantity = _safe_float(intent.target_size, 0.0)
row = {
"ts": snapshot.timestamp.isoformat(),
"strategy": self.config.strategy,
"runtime_namespace": self.config.runtime_namespace,
"strategy_namespace": self.config.strategy_namespace,
"event_namespace": self.config.event_namespace,
"actor_name": self.config.actor_name,
"exec_venue": self.config.exec_venue,
"data_venue": self.config.data_venue,
"source": "ditav2",
"trade_id": intent.trade_id,
"asset": decision.asset,
"side": decision.side.value,
"entry_price": price,
"current_price": price,
"quantity": quantity,
"notional": _notional(quantity, price),
"leverage": _safe_float(intent.leverage, 1.0),
"bar_idx": 0,
"decision_seq": self._trade_seq(),
"bars_held": int(intent.bars_held or 0),
"action": decision.action.value,
"reason": decision.reason,
"pnl_pct": 0.0,
"mfe": 0.0,
"mae": 0.0,
"mfe_risk": 0.0,
"mae_risk": 0.0,
"exit_pressure": 0.0,
"rv_comp": 0.0,
"mae_thresh1": 0.0,
"bounce_score": 0.0,
"bounce_risk": 0.0,
"ob_imbalance": 0.0,
"vel_div_entry": float(decision.velocity_divergence or 0.0),
"vel_div_now": float(decision.velocity_divergence or 0.0),
"v50_vel": 0.0,
"v750_vel": 0.0,
"exf_funding": 0.0,
"exf_dvol": 0.0,
"exf_fear_greed": 0.0,
"exf_taker": 0.0,
"posture": decision.side.value,
}
self._sink("policy_events", row)
self._v7_sink("v7_decision_events", row)
def _write_account_event(
self, snapshot: Any, decision: Decision | None, intent: Intent | None,
*, stage: TradeStage, slot_dict: dict[str, Any], event_type: str | None = None,
) -> None:
capital = self._capital()
peak_cap = self._peak_capital()
is_open = not slot_dict.get("closed", False) and slot_dict.get("size", 0) > 0
open_notional = _notional(self._slot_size(slot_dict), self._slot_entry_price(slot_dict)) if is_open else 0.0
drawdown_pct = 0.0 if peak_cap <= 0 else max(0.0, (peak_cap - capital) / peak_cap)
row = {
"ts": snapshot.timestamp.isoformat(),
"event_type": event_type or stage.value,
"strategy": self.config.strategy,
"posture": self._posture(slot_dict),
"capital": capital,
"peak_capital": peak_cap,
"drawdown_pct": drawdown_pct,
"pnl_today": float(self.account.snapshot.realized_pnl or 0.0),
"trades_today": self._trade_seq(),
"open_positions": 1 if is_open else 0,
"boost": 1.0,
"beta": 0.0,
"current_open_notional": open_notional,
"current_account_leverage": 0.0 if capital <= 0 else open_notional / capital,
"exchange_leverage": int(round(_safe_float(slot_dict.get("leverage", 0.0), 0.0))),
"exchange_leverage_mode": self.config.exchange_leverage_mode,
"leverage_mapping_rule": self.config.leverage_mapping_rule,
"runtime_namespace": self.config.runtime_namespace,
"strategy_namespace": self.config.strategy_namespace,
"event_namespace": self.config.event_namespace,
"actor_name": self.config.actor_name,
"exec_venue": self.config.exec_venue,
"data_venue": self.config.data_venue,
"notes": _json_text({
"decision_id": None if decision is None else decision.decision_id,
"trade_id": None if intent is None else intent.trade_id,
"reason": None if intent is None else intent.reason,
"stage": stage.value,
}),
}
self._sink("account_events", row)
def _write_position_state(
self, snapshot: Any, decision: Decision | None, intent: Intent | None,
*, slot_dict: dict[str, Any], stage: TradeStage, status: str,
market_state: Mapping[str, Any] | None = None,
) -> None:
side = self._slot_side(slot_dict)
trade_id = self._slot_trade_id(slot_dict)
asset = self._slot_asset(slot_dict)
if not trade_id and intent is not None:
trade_id = intent.trade_id
asset = intent.asset
side = intent.side
row = {
"ts": snapshot.timestamp.isoformat(),
"trade_id": trade_id,
"asset": asset,
"direction": _direction(side),
"entry_price": self._slot_entry_price(slot_dict),
"quantity": self._slot_size(slot_dict),
"notional": _notional(self._slot_size(slot_dict), self._slot_entry_price(slot_dict)),
"leverage": _safe_float(slot_dict.get("leverage", 0.0), 0.0),
"bucket_id": -1,
"entry_bar": int(slot_dict.get("active_leg_index", 0) or 0),
"status": status,
"exit_reason": slot_dict.get("close_reason", ""),
"pnl": _safe_float(slot_dict.get("realized_pnl", 0.0), 0.0),
"bars_held": 0,
"market_state_bundle_json": _json_text(market_state or {}),
"tp_base_pct": 0.0,
"tp_effective_pct": 0.0,
"our_leverage": _safe_float(slot_dict.get("leverage", 0.0), 0.0),
}
self._sink("position_state", row)
def _write_status_snapshot(
self, snapshot: Any, decision: Decision | None, intent: Intent | None,
*, slot_dict: dict[str, Any], phase: str,
) -> None:
capital = self._capital()
peak_cap = self._peak_capital()
is_open = not slot_dict.get("closed", False) and slot_dict.get("size", 0) > 0
open_notional = _notional(self._slot_size(slot_dict), self._slot_entry_price(slot_dict)) if is_open else 0.0
leverage = 0.0 if capital <= 0 else open_notional / capital
drawdown = 0.0 if peak_cap <= 0 else max(0.0, (peak_cap - capital) / peak_cap)
row = {
"ts": snapshot.timestamp.isoformat(timespec="milliseconds"),
"capital": capital,
"roi_pct": 0.0 if self.config.initial_capital <= 0 else ((capital / self.config.initial_capital) - 1.0) * 100.0,
"dd_pct": drawdown * 100.0,
"trades_executed": self._trade_seq(),
"posture": self._posture(slot_dict),
"rm": 1.0 if decision is None else max(0.0, min(1.0, decision.confidence)),
"vel_div": 0.0 if decision is None else float(decision.velocity_divergence),
"vol_ok": 1,
"phase": phase,
"mhs_status": "GREEN",
"boost": 1.0,
"cat5": 0.0,
"conviction_multiplier": 0.0 if intent is None else float(intent.confidence or 0.0),
"exchange_leverage": int(round(_safe_float(slot_dict.get("leverage", 0.0), 0.0))),
"exchange_leverage_mode": self.config.exchange_leverage_mode,
"leverage_mapping_rule": self.config.leverage_mapping_rule,
"account_capital": capital,
"portfolio_capital": capital,
"current_open_notional": open_notional,
"current_account_leverage": leverage,
"remaining_notional_capacity": max(0.0, self.config.max_account_leverage * capital - open_notional),
"max_account_leverage": self.config.max_account_leverage,
"ledger_authority": self.config.ledger_authority,
}
self._sink("status_snapshots", row)
def _write_trade_exit_leg(
self, snapshot: Any, decision: Decision, intent: Intent,
slot_dict: dict[str, Any], outcome: KernelOutcome | None,
) -> None:
"""Emit one BLUE-schema-compatible ``trade_exit_legs`` row per exit leg.
The DITAv2 kernel uses a single slot with sequential exit legs rather
than BLUE's chained per-leg trade_ids, so the chain_* columns describe
the leg sequence within this one trade (root = trade_id). Per-leg deltas
(exit_qty, pnl_leg) are computed against the previous leg's snapshot held
in ``self._leg_state`` so each row is isolated, not cumulative.
"""
trade_id = intent.trade_id
prev = self._leg_state.get(trade_id) or {
"prev_realized": 0.0,
"prev_size": _safe_float(slot_dict.get("initial_size", 0.0), 0.0),
"prev_leg_id": "",
}
entry_price = self._slot_entry_price(slot_dict) or _safe_float(intent.reference_price, 0.0)
exit_price = _safe_float(intent.reference_price, 0.0) or _safe_float(decision.reference_price, 0.0)
side = self._slot_side(slot_dict)
if side == TradeSide.FLAT:
side = intent.side
leverage_val = _safe_float(slot_dict.get("leverage", intent.leverage), 1.0)
cur_size = self._slot_size(slot_dict)
cur_realized = _safe_float(slot_dict.get("realized_pnl", 0.0), 0.0)
prev_size = _safe_float(prev.get("prev_size", 0.0), 0.0)
prev_realized = _safe_float(prev.get("prev_realized", 0.0), 0.0)
# active_leg_index is post-fill (already advanced); the leg that just
# filled is therefore one behind. Clamp to a valid ratio index.
ratios = slot_dict.get("exit_leg_ratios", []) or []
leg_index = max(0, int(slot_dict.get("active_leg_index", 0) or 0) - 1)
fraction = _safe_float(ratios[leg_index], 0.0) if 0 <= leg_index < len(ratios) else 0.0
exit_qty = max(0.0, prev_size - cur_size)
pnl_leg = cur_realized - prev_realized
capital_after = self._capital()
capital_before = capital_after - pnl_leg
exit_notional = _notional(exit_qty, exit_price or entry_price)
remaining_notional = _notional(cur_size, entry_price)
denom = abs(exit_qty * entry_price * max(leverage_val, 1e-9))
pnl_pct_leg = pnl_leg / denom if denom > 0 else 0.0
exit_leg_id = f"{trade_id}:leg{leg_index}"
self._sink("trade_exit_legs", {
"ts": snapshot.timestamp.isoformat(),
"date": snapshot.timestamp.date().isoformat(),
"strategy": self.config.strategy,
"trade_id": trade_id,
"chain_root_trade_id": trade_id,
"chain_head_leg_id": f"{trade_id}:leg0",
"chain_prev_leg_id": str(prev.get("prev_leg_id", "") or ""),
"chain_seq": leg_index,
"chain_token": trade_id,
"chain_mode": "LIVE",
"exit_leg_id": exit_leg_id,
"exit_seq": leg_index,
"command_id": decision.decision_id,
"source": "ditav2",
"reason": intent.reason,
"asset": intent.asset,
"side": side.value,
"entry_price": entry_price,
"exit_price": exit_price,
"fraction": fraction,
"capital_before": capital_before,
"capital_after": capital_after,
"exit_notional": exit_notional,
"remaining_notional": remaining_notional,
"remaining_qty": cur_size,
"pnl_pct_leg": pnl_pct_leg,
"pnl_leg": pnl_leg,
"pnl_realized_total": cur_realized,
"bars_held": int(intent.bars_held or 0),
})
# Advance the per-trade leg snapshot for the next leg's delta.
self._leg_state[trade_id] = {
"prev_realized": cur_realized,
"prev_size": cur_size,
"prev_leg_id": exit_leg_id,
}
def _write_trade_event(
self, snapshot: Any, decision: Decision, intent: Intent,
slot_dict: dict[str, Any], outcome: KernelOutcome | None,
*, market_state: Mapping[str, Any] | None = None,
) -> None:
entry_price = _safe_float(slot_dict.get("entry_price", 0.0), 0.0) or _safe_float(intent.reference_price, 0.0)
quantity = _safe_float(slot_dict.get("initial_size", slot_dict.get("size", 0.0)), 0.0) or _safe_float(intent.target_size, 0.0)
exit_price = _safe_float(slot_dict.get("entry_price", 0.0), 0.0)
pnl = _safe_float(slot_dict.get("realized_pnl", 0.0), 0.0)
pnl_pct = 0.0
leverage_val = _safe_float(slot_dict.get("leverage", intent.leverage), 1.0)
denom = abs(quantity * entry_price * max(leverage_val, 1e-9))
if denom > 0:
pnl_pct = pnl / denom
capital_after = self._capital()
capital_before = capital_after - pnl
open_notional = _notional(quantity, exit_price or entry_price)
conviction = float(intent.confidence or decision.confidence or 0.0)
metadata = intent.metadata if intent is not None else (decision.metadata if decision is not None else {})
row = {
"ts": snapshot.timestamp.isoformat(),
"date": snapshot.timestamp.date().isoformat(),
"strategy": self.config.strategy,
"trade_id": intent.trade_id,
"asset": intent.asset,
"side": intent.side.value,
"entry_price": entry_price,
"exit_price": exit_price,
"quantity": quantity,
"pnl": pnl,
"pnl_pct": pnl_pct,
"exit_reason": intent.reason,
"vel_div_entry": float(decision.velocity_divergence or 0.0),
"boost_at_entry": 1.0,
"beta_at_entry": 0.0,
"posture": intent.side.value,
"leverage": leverage_val,
"conviction_multiplier": conviction,
"exchange_leverage": int(round(leverage_val)),
"exchange_leverage_mode": self.config.exchange_leverage_mode,
"leverage_mapping_rule": self.config.leverage_mapping_rule,
"runtime_namespace": self.config.runtime_namespace,
"strategy_namespace": self.config.strategy_namespace,
"event_namespace": self.config.event_namespace,
"actor_name": self.config.actor_name,
"exec_venue": self.config.exec_venue,
"data_venue": self.config.data_venue,
"account_capital": capital_after,
"portfolio_capital": capital_after,
"current_open_notional": open_notional,
"remaining_notional_capacity": max(0.0, self.config.max_account_leverage * capital_after - open_notional),
"max_account_leverage": self.config.max_account_leverage,
"margin_required": 0.0 if leverage_val <= 0 else open_notional / leverage_val,
"ledger_authority": self.config.ledger_authority,
"regime_signal": 0,
"capital_before": capital_before,
"capital_after": capital_after,
"peak_capital": self._peak_capital(),
"drawdown_at_entry": 0.0 if self._peak_capital() <= 0 else max(0.0, (self._peak_capital() - capital_before) / self._peak_capital()),
"open_positions_count": 0,
"scan_uuid": decision.decision_id,
"bars_held": int(intent.bars_held or 0),
"entry_payload_json": _json_text({"decision": _decision_summary(decision), "intent": _intent_summary(intent)}),
"exit_payload_json": _json_text({"outcome": _outcome_summary(outcome), "slot": _json_safe(slot_dict)}),
"execution_payload_json": _json_text({"outcome": _outcome_summary(outcome)}),
"friction_payload_json": _json_text({"fees": 0.0}),
"event_payload_json": _json_text({"phase": "terminal_close", "trade_id": intent.trade_id}),
"market_state_bundle_json": _json_text(market_state or {}),
"tp_base_pct": _safe_float(metadata.get("tp_base_pct", 0.0), 0.0),
"tp_effective_pct": _safe_float(metadata.get("tp_effective_pct", 0.0), 0.0),
"our_leverage": _safe_float(metadata.get("our_leverage", 0.0), 0.0),
}
self._sink("trade_events", row)
def _write_trade_reconstruction(
self, snapshot: Any, trade_id: str, *,
event_type: str, event_id: str, payload: Any,
market_state: Mapping[str, Any] | None = None,
) -> None:
self._sink("trade_reconstruction", {
"ts": snapshot.timestamp.isoformat(),
"trade_id": trade_id,
"event_type": event_type,
"event_id": event_id,
"payload_json": _json_text(payload),
"market_state_bundle_json": _json_text(market_state or {}),
})

View File

@@ -1,645 +0,0 @@
"""Node-free PINK runtime built on DITAv2 kernel + BingX venue adapter.
The kernel owns the single-slot FSM, AccountProjection, and event
normalization. This module translates policy-layer Decision/Intent into
KernelIntent and reads final state from the kernel's slot + account
snapshot. Capital is seeded from exchange balance at startup/recovery
then maintained by kernel.account.settle() on close — no balance-poll
overwrites during the hot loop.
"""
from __future__ import annotations
import inspect
import logging
import math
from dataclasses import dataclass, replace
from datetime import datetime, timezone
from types import SimpleNamespace
from typing import Any, Callable, Optional
from prod.clean_arch.dita import (
Decision,
DecisionAction,
DecisionConfig,
DecisionContext,
DecisionEngine,
Intent,
IntentContext,
IntentEngine,
TradeSide as LegacyTradeSide,
)
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelIntent,
TradeSide as DitaTradeSide,
TradeStage,
)
from prod.clean_arch.dita_v2.rust_backend import ExecutionKernel
from prod.clean_arch.persistence import PinkClickHousePersistence
from prod.clean_arch.ports.data_feed import DataFeedPort, MarketSnapshot
LOGGER = logging.getLogger(__name__)
def _slot_to_position_dict(slot) -> dict[str, Any]:
"""Convert a DITAv2 TradeSlot into a simple position dict compatible
with the persistence layer's expected shape."""
if slot is None:
return {}
return {
"trade_id": slot.trade_id,
"asset": slot.asset,
"side": slot.side.value,
"entry_price": float(slot.entry_price or 0.0),
"entry_time": slot.entry_time.isoformat() if hasattr(slot.entry_time, "isoformat") else str(slot.entry_time),
"size": float(slot.size or 0.0),
"initial_size": float(slot.initial_size or 0.0),
"leverage": float(slot.leverage or 0.0),
"realized_pnl": float(slot.realized_pnl or 0.0),
"unrealized_pnl": float(slot.unrealized_pnl or 0.0),
"closed": bool(slot.closed),
"close_reason": slot.close_reason or "",
"fsm_state": slot.fsm_state.value,
"exit_leg_ratios": list(slot.exit_leg_ratios),
"active_leg_index": int(slot.active_leg_index or 0),
"active_exit_order": dict(slot.active_exit_order.to_dict()) if slot.active_exit_order and hasattr(slot.active_exit_order, "to_dict") else ({"status": slot.active_exit_order.status.value, "venue_order_id": slot.active_exit_order.venue_order_id} if slot.active_exit_order else None),
"active_entry_order": dict(slot.active_entry_order.to_dict()) if slot.active_entry_order and hasattr(slot.active_entry_order, "to_dict") else ({"status": slot.active_entry_order.status.value, "venue_order_id": slot.active_entry_order.venue_order_id} if slot.active_entry_order else None),
}
# Industry-smallest sane quote price. notional (capital × fraction × leverage)
# is self-limiting; the only unbounded step is size = notional / price, which
# overflows to inf as price -> 0. Any real perp quote is far above this floor,
# so a price below it (or non-finite) signals corrupt market data, not a trade.
_MIN_SANE_PRICE = 1e-8
def _decision_to_kernel_intent(
decision: Decision,
intent: Intent,
slot_id: int = 0,
) -> KernelIntent:
"""Translate policy-layer Decision/Intent into a DITAv2 KernelIntent.
The action map is:
ENTER -> KernelCommandType.ENTER
EXIT -> KernelCommandType.EXIT
HOLD -> KernelCommandType.MARK_PRICE
"""
action_map = {
DecisionAction.ENTER: KernelCommandType.ENTER,
DecisionAction.EXIT: KernelCommandType.EXIT,
DecisionAction.HOLD: KernelCommandType.MARK_PRICE,
}
side = (
DitaTradeSide.SHORT
if intent.side == LegacyTradeSide.SHORT
else DitaTradeSide.LONG
)
return KernelIntent(
timestamp=decision.timestamp,
intent_id=decision.decision_id,
trade_id=intent.trade_id,
slot_id=slot_id,
asset=intent.asset,
side=side,
action=action_map.get(decision.action, KernelCommandType.MARK_PRICE),
reference_price=float(decision.reference_price or intent.reference_price or 0.0),
target_size=float(intent.target_size or 0.0),
leverage=float(intent.leverage or 1.0),
exit_leg_ratios=tuple(intent.exit_leg_ratios),
reason=intent.reason,
metadata=dict(intent.metadata or {}),
)
def _reconcile_position_slot(
kernel: ExecutionKernel,
exchange_balance_capital: float,
slot_id: int = 0,
) -> None:
"""Synchronise a single kernel slot from the venue's open positions.
This is called at startup/recovery to make the kernel state match the
exchange. It also seeds the kernel's AccountProjection.capital from the
exchange balance — the single place where an external balance snapshot
writes capital.
"""
venue = kernel.venue
try:
positions = venue.open_positions() if hasattr(venue, "open_positions") else []
except Exception:
positions = []
# Build TradeSlot[] from exchange positions
from prod.clean_arch.dita_v2.contracts import TradeSlot, TradeSide
reconciled = []
if positions:
for row in positions if isinstance(positions, list) else (
list(positions.values()) if isinstance(positions, dict) else []):
raw_side = str(row.get("positionSide") or row.get("side") or "").upper()
raw_qty = 0.0
for key in ("positionAmt", "positionQty", "positionSize", "quantity", "pa", "qty"):
try:
raw_qty = float(row.get(key) or 0.0)
except Exception:
continue
if raw_qty != 0.0:
break
if abs(raw_qty) <= 1e-12:
continue
qty = abs(raw_qty)
entry = 0.0
for key in ("entryPrice", "avgPrice", "avgEntryPrice", "ep", "ap", "price"):
try:
entry = float(row.get(key) or 0.0)
except Exception:
continue
if entry > 0:
break
mark = 0.0
for key in ("markPrice", "mark", "price"):
try:
mark = float(row.get(key) or 0.0)
except Exception:
continue
if mark > 0:
break
if mark <= 0:
mark = entry
lev = float(row.get("leverage") or row.get("lev") or 1.0)
side = TradeSide.SHORT if raw_side in {"SHORT", "SELL"} or raw_qty < 0 else TradeSide.LONG
asset = str(row.get("symbol") or row.get("symbolName") or "")
trade_id = asset # use asset as trade ID for exchange-led recovery
slot = TradeSlot(
slot_id=slot_id,
trade_id=trade_id,
asset=asset,
side=side,
entry_price=entry if entry > 0 else mark,
size=qty,
initial_size=qty,
leverage=lev if lev > 0 else 1.0,
entry_time=datetime.now(timezone.utc),
fsm_state=TradeStage.POSITION_OPEN,
metadata={"reconciled_from_exchange": True},
)
reconciled.append(slot)
if reconciled:
kernel.reconcile_from_slots(reconciled)
else:
# No open positions — ensure slot is idle
kernel.reconcile_from_slots([])
# Seed capital once from exchange balance.
if exchange_balance_capital > 0:
kernel.account.snapshot.capital = exchange_balance_capital
kernel.account.snapshot.peak_capital = max(
kernel.account.snapshot.peak_capital, exchange_balance_capital
)
kernel.account.snapshot.equity = exchange_balance_capital
@dataclass
class PinkDirectRuntime:
"""Drive DITAv2 kernel against BingX exchange and a market data feed.
The kernel owns the FSM and account projection. This runtime provides
the policy loop: data feed -> decision engine -> intent engine ->
kernel intent -> outcome -> persistence.
"""
data_feed: DataFeedPort
kernel: ExecutionKernel
decision_engine: DecisionEngine
intent_engine: IntentEngine
persistence: Optional[PinkClickHousePersistence] = None
market_state_runtime: Any = None
event_sink: Optional[Callable[[dict[str, Any]], None]] = None
logger: Any = LOGGER
async def connect(self, initial_capital: float = 25000.0) -> None:
"""Connect data feed, venue, and seed capital from exchange."""
await self.data_feed.connect()
venue = self.kernel.venue
# VenueAdapter methods are synchronous (the adapter bridges async
# internally via _run). Try connect() if it exists.
if hasattr(venue, "connect"):
try:
result = venue.connect()
if inspect.isawaitable(result):
await result
except Exception as exc:
self.logger.warning("Venue connect failed: %s", exc)
# Seed capital from env default — the kernel tracks capital via
# settle() on close, not from exchange balance polls.
_reconcile_position_slot(self.kernel, initial_capital, slot_id=0)
async def disconnect(self) -> None:
await self.data_feed.disconnect()
venue = self.kernel.venue
if hasattr(venue, "disconnect"):
try:
await venue.disconnect()
except Exception:
pass
def _emit(self, phase: str, **fields: Any) -> None:
if self.event_sink is not None:
payload = {"phase": phase, **fields}
self.event_sink(payload)
@staticmethod
def _scan_payload_prices(
scan_payload: dict[str, Any] | None,
fallback_symbol: str,
fallback_price: float,
) -> dict[str, float]:
payload = scan_payload or {}
assets = payload.get("assets") or []
prices = payload.get("asset_prices") or []
out: dict[str, float] = {}
if isinstance(assets, list) and isinstance(prices, list):
for asset, price in zip(assets, prices):
try:
px = float(price)
except Exception:
continue
if px > 0:
out[str(asset).upper()] = px
if not out and fallback_symbol and fallback_price > 0:
out[str(fallback_symbol).upper()] = float(fallback_price)
return out
def _update_market_state_runtime(
self, snapshot: MarketSnapshot
) -> dict[str, Any]:
runtime = self.market_state_runtime
scan_payload = (
snapshot.scan_payload if isinstance(snapshot.scan_payload, dict) else {}
)
if runtime is None or not scan_payload:
return {}
try:
prices_dict = self._scan_payload_prices(
scan_payload, snapshot.symbol, snapshot.price
)
bundle = runtime.update_scan_state(
scan_payload=scan_payload,
prices_dict=prices_dict,
scan_number=int(
scan_payload.get("scan_number") or snapshot.scan_number or 0
),
vel_div=float(
scan_payload.get("vel_div")
or snapshot.velocity_divergence
or 0.0
),
v50_vel=float(scan_payload.get("w50_velocity") or 0.0),
v750_vel=float(scan_payload.get("w750_velocity") or 0.0),
vol_ok=bool(scan_payload.get("vol_ok", True)),
posture=str(scan_payload.get("posture") or "APEX"),
exf_snapshot=scan_payload.get("exf_snapshot")
if isinstance(scan_payload.get("exf_snapshot"), dict)
else None,
esof_payload=scan_payload.get("esof_payload")
if isinstance(scan_payload.get("esof_payload"), dict)
else None,
)
return dict(
getattr(runtime, "latest_bundle_dict", {}) or bundle.as_dict()
)
except Exception:
return {}
async def pump_venue_events(
self, snapshot: Any | None = None, *, market_state: Any = None
) -> int:
"""Drain late (async) venue fills into the kernel and persist the result.
Resting LIMIT and partial fills arrive *after* the submitting
``process_intent`` returns. This calls ``venue.reconcile()`` and feeds
each event to ``kernel.on_venue_event`` so capital settles and the FSM
advances; the kernel dedups duplicates via ``seen_event_ids`` /
``_last_settled_pnl`` (no double-settle). Only events the kernel actually
applied (accepted, not DUPLICATE_EVENT) are persisted, via the two-phase
result-logger. Capital authority stays ``kernel.account``.
Returns the number of applied events.
"""
venue = self.kernel.venue
reconcile = getattr(venue, "reconcile", None)
if reconcile is None:
return 0
try:
events = reconcile()
if inspect.isawaitable(events):
events = await events
except Exception as exc:
self.logger.warning("Venue reconcile failed: %s", exc)
return 0
events = list(events or [])
if not events:
return 0
applied: list[Any] = []
for event in events:
try:
outcome = self.kernel.on_venue_event(event)
except Exception as exc:
self.logger.warning("on_venue_event failed: %s", exc)
continue
if getattr(outcome, "accepted", False) and getattr(
outcome, "diagnostic_code", None
) != KernelDiagnosticCode.DUPLICATE_EVENT:
applied.append(event)
if applied and self.persistence is not None:
slot_dict = self.kernel.slot(0).to_dict() if self.kernel.max_slots > 0 else {}
persist_snapshot = snapshot
if persist_snapshot is None:
persist_snapshot = SimpleNamespace(
timestamp=datetime.now(timezone.utc),
symbol=str(slot_dict.get("asset", "")),
)
self.persistence.persist_fill_events(
snapshot=persist_snapshot,
events=applied,
slot_dict=slot_dict,
market_state=market_state or {},
)
return len(applied)
def _unsafe_entry_reason(self, kernel_intent: KernelIntent, context: Any) -> Optional[str]:
"""Return why an ENTER's sizing inputs are unsafe, or None if sound.
notional = capital × fraction × leverage is self-limiting; the only way
size = notional/price goes non-finite is a corrupt raw input. We reject
the OPEN (not clamp) because a corrupt sizing input is an untrustworthy
signal — better to skip the trade than open on bad math.
"""
cap = float(getattr(context, "capital", 0.0) or 0.0)
price = float(getattr(kernel_intent, "reference_price", 0.0) or 0.0)
lev = float(getattr(kernel_intent, "leverage", 0.0) or 0.0)
size = float(getattr(kernel_intent, "target_size", 0.0) or 0.0)
if not math.isfinite(cap) or cap <= 0.0:
return f"non-finite/non-positive capital={cap!r}"
if not math.isfinite(price) or price < _MIN_SANE_PRICE:
return f"price below sane floor or non-finite price={price!r} (floor={_MIN_SANE_PRICE:g})"
if not math.isfinite(lev) or lev <= 0.0:
return f"non-finite/non-positive leverage={lev!r}"
if not math.isfinite(size) or size <= 0.0:
return f"non-finite/non-positive size={size!r}"
return None
def _exit_intent_from_slot(self, kernel_intent: KernelIntent) -> KernelIntent:
"""Size an EXIT from the kernel's authoritative slot accounting.
The close quantity is the real remaining position size (capped to it),
never an externally-computed value — so a malformed policy size can
neither strand a position (refuse to close) nor overshoot it. A
non-finite policy size falls back to the full remaining size.
"""
try:
slot_size = float(self.kernel.slot(int(kernel_intent.slot_id)).size or 0.0)
except Exception:
slot_size = 0.0
policy_size = float(getattr(kernel_intent, "target_size", 0.0) or 0.0)
policy_ok = math.isfinite(policy_size) and policy_size > 0.0
if slot_size > 0.0:
# Authoritative remaining size known: cap the close to it (and fall
# back to the full remaining if the policy size is malformed).
exit_size = min(policy_size, slot_size) if policy_ok else slot_size
else:
# Kernel reports no/unknown remaining size: trust the policy size
# (the kernel rejects NO_OPEN_POSITION if there is genuinely none).
exit_size = policy_size if policy_ok else 0.0
return replace(kernel_intent, target_size=exit_size)
async def step(self, snapshot: MarketSnapshot) -> Decision:
"""Single policy + execution cycle.
0. Pump late (async) venue fills into the kernel (LIMIT/partial settle)
1. Update market state
2. Decide (policy layer)
3. Plan (intent layer)
4. Translate to KernelIntent -> kernel.process_intent()
5. Read final slot + account state from kernel
6. Persist
"""
market_state = self._update_market_state_runtime(snapshot)
# Drain any late fills BEFORE the policy reads slot/account state, so a
# resting LIMIT that filled since the last cycle is reflected.
await self.pump_venue_events(snapshot, market_state=market_state)
acc = self.kernel.snapshot()["account"]
slot_view = self.kernel.slot(0) if self.kernel.max_slots > 0 else None
slot_dict = slot_view.to_dict() if slot_view is not None else {}
is_open = slot_dict and slot_dict.get("size", 0) > 0 and not slot_dict.get("closed", False)
# Convert the kernel slot dict into a TradePosition for the legacy
# decision/intent engines.
legacy_position = None
if is_open:
from prod.clean_arch.dita import TradePosition, TradeSide as LS
legacy_position = TradePosition(
trade_id=slot_dict.get("trade_id", ""),
asset=slot_dict.get("asset", ""),
side=LS.SHORT if slot_dict.get("side", "").upper() in ("SHORT", "SELL") else LS.LONG,
entry_price=float(slot_dict.get("entry_price", 0.0)),
entry_time=datetime.now(timezone.utc),
size=float(slot_dict.get("size", 0.0)),
leverage=float(slot_dict.get("leverage", 1.0)),
entry_velocity_divergence=float(slot_dict.get("entry_velocity_divergence", 0.0)),
entry_irp_alignment=float(slot_dict.get("entry_irp_alignment", 0.0)),
current_price=float(slot_dict.get("entry_price", 0.0)),
initial_size=float(slot_dict.get("initial_size", 0.0)),
exit_leg_ratios=tuple(slot_dict.get("exit_leg_ratios", [1.0])),
# Carry the kernel's authoritative leg progression so the intent
# engine consumes the CORRECT exit-leg ratio. The legacy position
# is rebuilt every step; without this exit_leg_index resets to 0
# and every leg uses ratio[0] — under-closing each leg and leaving
# a residual (kernel believes flat, exchange does not).
exit_leg_index=int(slot_dict.get("active_leg_index", 0) or 0),
closed=False,
)
context = DecisionContext(
capital=float(acc.get("capital", 0.0)),
open_positions=int(acc.get("open_positions", 0)),
trade_seq=int(acc.get("trade_seq", 0)),
)
decision = self.decision_engine.decide(snapshot, context, legacy_position)
self._emit("decision", decision=decision)
intent_context = IntentContext(
capital=context.capital,
open_positions=context.open_positions,
trade_seq=context.trade_seq,
)
plan = self.intent_engine.plan(decision, intent_context, legacy_position)
intent = plan.intent
if decision.action in {DecisionAction.ENTER, DecisionAction.EXIT}:
kernel_intent = _decision_to_kernel_intent(decision, intent, slot_id=0)
if decision.action == DecisionAction.ENTER:
# Source guard: notional (capital×fraction×leverage) is self-
# limiting, so a non-finite size can only come from corrupt raw
# inputs — a non-finite capital, or a price below the industry
# floor that overflows size = notional/price. A corrupt sizing
# input is an untrustworthy signal: do NOT open (exits are never
# suppressed — they size from slot accounting below).
unsafe = self._unsafe_entry_reason(kernel_intent, context)
if unsafe is not None:
self.logger.error(
"ENTER suppressed (%s): price=%r capital=%r size=%r leverage=%r "
"floor=%g asset=%s",
unsafe, getattr(kernel_intent, "reference_price", None), context.capital,
getattr(kernel_intent, "target_size", None),
getattr(kernel_intent, "leverage", None), _MIN_SANE_PRICE, intent.asset,
)
sp = float(getattr(snapshot, "price", 0.0) or 0.0)
if math.isfinite(sp) and sp >= _MIN_SANE_PRICE:
self.kernel.mark_price(snapshot.symbol, sp)
slot_dict = self.kernel.slot(0).to_dict() if self.kernel.max_slots > 0 else {}
acc = self.kernel.snapshot()["account"]
if self.persistence is not None:
self.persistence.persist_step(
snapshot=snapshot, decision=decision, intent=intent, outcome=None,
slot_dict=slot_dict, acc_dict=acc, phase="entry_suppressed",
market_state=market_state,
)
return decision
else:
# EXIT: size the close from the kernel's authoritative slot
# accounting so a malformed policy size can never strand or
# overshoot an open position.
kernel_intent = self._exit_intent_from_slot(kernel_intent)
outcome = self.kernel.process_intent(kernel_intent)
# Locate the source of any non-finite intent the kernel rejected:
# log the full upstream provenance (snapshot price, account capital,
# leverage, sizing) so a numerical error can be traced to its origin
# rather than silently rejected.
if outcome.diagnostic_code == KernelDiagnosticCode.INVALID_INTENT:
self.logger.error(
"INVALID_INTENT rejected by kernel: %s | provenance: "
"snapshot.price=%r capital=%r open_positions=%r leverage=%r "
"target_size=%r reference_price=%r limit_price=%r action=%s asset=%s",
dict(outcome.details or {}),
getattr(snapshot, "price", None),
context.capital,
context.open_positions,
getattr(kernel_intent, "leverage", None),
getattr(kernel_intent, "target_size", None),
getattr(kernel_intent, "reference_price", None),
getattr(kernel_intent, "limit_price", None),
decision.action.value,
intent.asset,
)
# Read authoritative final state from kernel.
final_slot = self.kernel.slot(0)
slot_dict = final_slot.to_dict()
acc = self.kernel.snapshot()["account"]
self._emit(
"execution",
decision=decision,
intent=intent,
outcome_code=outcome.diagnostic_code.value,
)
if self.persistence is not None:
self.persistence.persist_step(
snapshot=snapshot,
decision=decision,
intent=intent,
outcome=outcome,
slot_dict=slot_dict,
acc_dict=acc,
phase="execution",
market_state=market_state,
)
else:
# HOLD / no-op: update mark price in kernel.
if snapshot.price and snapshot.price > 0:
self.kernel.mark_price(snapshot.symbol, snapshot.price)
slot_dict = self.kernel.slot(0).to_dict() if self.kernel.max_slots > 0 else {}
acc = self.kernel.snapshot()["account"]
if self.persistence is not None:
self.persistence.persist_step(
snapshot=snapshot,
decision=decision,
intent=intent,
outcome=None,
slot_dict=slot_dict,
acc_dict=acc,
phase="decision",
market_state=market_state,
)
return decision
async def recover(
self, snapshot: MarketSnapshot | None = None
) -> dict[str, Any]:
"""Full recovery — reconcile exchange state into kernel and reseed capital."""
return await self.recover_account(
snapshot=snapshot, phase="recovery", event_type="RECOVERY"
)
async def recover_account(
self,
*,
snapshot: MarketSnapshot | None = None,
phase: str = "recovery",
event_type: str = "RECOVERY",
) -> dict[str, Any]:
"""Reconcile exchange state, reseed capital, and persist recovery row.
The kernel's VenueAdapter is sync — all async bridging is handled
internally by ``_run()``. We seed capital from the kernel's existing
value (which was set at startup) rather than re-polling the exchange.
"""
capital = float(self.kernel.account.snapshot.capital or 25000.0)
_reconcile_position_slot(self.kernel, capital, slot_id=0)
acc = self.kernel.snapshot()["account"]
if self.persistence is not None:
persist_snapshot = snapshot
if persist_snapshot is None:
persist_snapshot = SimpleNamespace(
timestamp=datetime.now(timezone.utc), symbol=""
)
market_state = {}
if snapshot is not None:
market_state = self._update_market_state_runtime(snapshot)
self.persistence.persist_recovery_state(
snapshot=persist_snapshot,
acc_dict=acc,
phase=phase,
event_type=event_type,
market_state=market_state,
)
return acc
async def reconcile_account(
self, snapshot: MarketSnapshot | None = None
) -> dict[str, Any]:
"""Periodic exchange-led account sync.
Tags the recovery path as a scheduled reconciliation. Capital is
re-seeded from the exchange balance as a guard against long-running
drift, but the primary capital authority remains kernel.settle().
"""
return await self.recover_account(
snapshot=snapshot,
phase="account_reconcile",
event_type="ACCOUNT_RECONCILE",
)

View File

@@ -1,764 +0,0 @@
# DITAv2 Kernel Reference
**Status:** active
**Scope:** DITAv2 execution kernel, operator launcher, shared-memory control plane, venue adapters, and observability integration.
**Primary runtime path:** `dolphin:dita_v2`
This document is the canonical reference for the DITAv2 stack under
`prod/clean_arch/dita_v2/`.
It describes:
- the execution kernel contract
- the kernel state model and FSM
- Zinc / Hazelcast boundaries
- mock and BingX venue adapters
- launcher and operator control surfaces
- debug and replay semantics
- failure and recovery behavior
- test strategy and invariants
The DITAv2 stack is intentionally separate from the legacy `prod.clean_arch.dita`
surface. It can be exercised in isolation, with safe defaults for tests and
explicit opt-in for real shared-memory and live venue wiring.
Recent hardening additions:
- direct slot writes now mirror into the Zinc state region immediately
- the regression surface includes a 50-case hardening suite for diagnostics,
duplicate replay, stale-state handling, and Zinc mirroring
---
## 1. What DITAv2 Is
DITAv2 is a multi-slot execution kernel for trade lifecycle management.
It sits between the alpha layer and the exchange layer.
Its responsibilities are limited to:
1. receiving intents
2. mutating slot state
3. normalizing venue events
4. projecting account state
5. emitting deterministic transition and diagnostic records
6. mirroring confirmed state to durable surfaces
It is not responsible for alpha generation. It does not compute signals.
It does not decide entry/exit thesis. Those inputs come from BLUE/PINK or
another upstream strategy layer.
### Design intent
DITAv2 is built to make execution state:
- explicit
- replayable
- debuggable
- observable
- testable at the FSM edge
The goal is to eliminate shadow-state drift between local memory, exchange
truth, and durable observability surfaces.
---
## 2. Canonical Components
### Kernel
File:
- `prod/clean_arch/dita_v2/rust_backend.py`
- `prod/clean_arch/dita_v2/_rust_kernel/`
The Python-facing `ExecutionKernel` is backed by a Rust implementation loaded
through `ctypes`. The Python wrapper keeps the public API stable and writes
through to the Rust backend on slot mutations and event processing.
### Control plane
Files:
- `prod/clean_arch/dita_v2/control.py`
- `prod/clean_arch/dita_v2/real_control_plane.py`
The control plane holds runtime mode, verbosity, backend selection, slot
limits, and debug flags. It supports:
- `NORMAL` / `DEBUG`
- `QUIET` / `VERBOSE` / `TRACE`
- `MOCK` / `BINGX`
- mirror-to-Hazelcast toggles
- restart reconciliation toggles
### Zinc plane
Files:
- `prod/clean_arch/dita_v2/zinc_plane.py`
- `prod/clean_arch/dita_v2/real_zinc_plane.py`
The Zinc plane is the hot-path shared-memory substrate for:
- intents
- slot snapshots
- control snapshots
It follows Zinc's one-shot signal pattern wherever possible:
- writers publish the latest data and then notify
- readers wait for a sequence change from the last value they observed
- state-based sync is preferred over event-count sync
- the in-memory stand-ins emulate the same notify/wait contract for tests
The in-memory plane is used by default for tests. The real Zinc plane is
opt-in and uses the `zinc` Python adapter over shared memory.
Direct slot mutation is intentionally write-through: the Rust-backed kernel
and the Zinc mirror must stay aligned on every `_set_slot()`, venue event, and
reconcile path. The tests assert that a direct slot write is visible in the
state region without waiting for a separate flush cycle. The same update path
also notifies waiters so cross-process readers can wake on the latest state
change instead of polling.
### Projection
Files:
- `prod/clean_arch/dita_v2/projection.py`
- `prod/clean_arch/dita_v2/hazelcast_projection.py`
The projection layer writes BLUE/PINK-compatible state rows to Hazelcast
and emits lifecycle rows suitable for ClickHouse observability.
### Venue adapters
Files:
- `prod/clean_arch/dita_v2/mock_venue.py`
- `prod/clean_arch/dita_v2/bingx_venue.py`
The mock adapter is deterministic and BingX-shaped. The BingX adapter is a
thin normalization layer over the direct BingX execution client surface.
### Launcher and operator controls
Files:
- `prod/clean_arch/dita_v2/launcher.py`
- `prod/launch_dita_v2.py`
- `prod/ops/dita_v2_ctl.py`
- `prod/supervisor/supervisorctl.sh`
- `prod/ops/dita_v2_live_bingx_smoke.py`
The launcher assembles a full runtime bundle. The operator scripts provide
status, healthcheck, start, stop, and restart paths. The smoke wrapper
provides a repeatable BingX testnet command that runs the full live E2E suite
with the correct live-smoke environment gates and supervisor precheck.
Repeatable live smoke command:
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --symbol TRXUSDT
```
Use `--dry-run` to print the exact env and pytest command without sending
orders.
---
## 3. Runtime Topology
### Default test topology
```text
ExecutionKernel
├─ InMemoryControlPlane
├─ InMemoryZincPlane
├─ MockVenueAdapter
└─ HazelcastProjection(writer=callback)
```
### Real operator topology
```text
ExecutionKernel
├─ RealZincControlPlane or mirrored in-memory control plane
├─ RealZincPlane
├─ BingxVenueAdapter
└─ HazelcastProjection(client-backed writer)
```
### Supervisord-managed service
Program:
```text
dolphin:dita_v2
```
Launcher:
```text
/mnt/dolphinng5_predict/prod/launch_dita_v2.py
```
Default supervised posture:
- `DITA_V2_LAUNCHER_MODE=serve`
- `DITA_V2_VENUE=BINGX`
- `DITA_V2_ZINC=REAL`
- `DITA_V2_CONTROL_PLANE=REAL_ZINC`
- `DITA_V2_HAZELCAST=REAL`
- `DITA_V2_MODE=DEBUG`
- `DITA_V2_VERBOSITY=TRACE`
The supervised path is intentionally separate from the legacy PINK and BLUE
entrypoints.
---
## 4. Data Contracts
### Core contract files
- `prod/clean_arch/dita_v2/contracts.py`
- `prod/clean_arch/dita_v2/venue.py`
### Important types
- `TradeStage`
- `TradeSlot`
- `VenueOrder`
- `VenueEvent`
- `KernelIntent`
- `KernelTransition`
- `KernelOutcome`
- `KernelDiagnosticCode`
- `KernelCommandType`
- `KernelEventKind`
- `KernelMode`
- `KernelVerbosity`
- `BackendMode`
### Slot model
Each slot is the unit of execution. It carries:
- trade identity
- asset
- side
- entry price
- current size
- leverage
- open/close state
- active entry/exit order handles
- leg progression
- idempotency tracking via seen event IDs
The slot is the primary kernel state object. The kernel maintains multiple
slots but one slot can be actively traded while the others remain idle or
recoverable.
### Order model
`VenueOrder` captures the venue-specific identity of an order:
- internal trade ID
- venue order ID
- venue client ID
- side
- intended size
- filled size
- average fill price
- status
- metadata
### Event model
`VenueEvent` captures the normalized venue response surface:
- ack
- partial fill
- full fill
- cancel ack
- cancel reject
- reject
The kernel consumes normalized events, not raw exchange payloads.
---
## 5. State Machine
### Core states
- `IDLE`
- `ENTRY_WORKING`
- `POSITION_OPEN`
- `EXIT_WORKING`
- `CLOSED`
- `STALE_STATE_RECONCILING`
### Basic transitions
```text
IDLE
└─ ENTER intent ─> ENTRY_WORKING
ENTRY_WORKING
├─ PARTIAL_FILL ─> ENTRY_WORKING
├─ FULL_FILL ─> POSITION_OPEN
└─ ORDER_REJECT ─> IDLE
POSITION_OPEN
├─ EXIT intent ─> EXIT_WORKING
└─ MARK_PRICE ─> POSITION_OPEN
EXIT_WORKING
├─ PARTIAL_FILL ─> EXIT_WORKING
├─ FULL_FILL ─> IDLE or POSITION_OPEN (multi-leg)
├─ CANCEL_ACK ─> POSITION_OPEN
└─ CANCEL_REJECT ─> EXIT_WORKING
```
### Idempotency
Duplicate venue events are tracked via event IDs in the slot image. Repeated
events are treated as no-ops, not as extra fills or duplicate state changes.
### Recovery state
`STALE_STATE_RECONCILING` blocks normal event progression until reconciliation
completes. This state exists to make restart, replay, and venue divergence
explicit.
### Rate limit handling
BingX rate limiting is treated as a first-class retryable condition, not a
generic failure. The kernel surfaces it with:
- `KernelDiagnosticCode.RATE_LIMITED`
- `KernelSeverity.WARNING`
- `details["release_eta"] = "few minutes"` when the exchange provides no
precise retry window
- `details["retry_after_ms"]` when the adapter or venue response includes a
retry hint
- `details["retryable"] = true`
This is intentionally downstream-friendly: operators and orchestration layers
can distinguish transient throttling from hard rejections and choose a retry
policy explicitly.
---
## 6. Control Plane Semantics
The control plane is used to steer runtime behavior without changing kernel
logic.
### Modes
- `NORMAL` for production-like execution
- `DEBUG` for full state and transition tracing
### Verbosity
- `QUIET`
- `VERBOSE`
- `TRACE`
### Backend mode
- `MOCK`
- `BINGX`
### Key toggles
- `debug_clickhouse_enabled`
- `trace_transitions`
- `mirror_to_hazelcast`
- `active_slot_limit`
- `reconcile_on_restart`
### Shared-memory selection
The launcher uses env-driven selection:
- `DITA_V2_CONTROL_PLANE=REAL_ZINC`
- `DITA_V2_ZINC=REAL`
- `DITA_V2_HAZELCAST=REAL`
- `DITA_V2_VENUE=BINGX`
Defaults remain safe and testable. Real shared-memory and live venue wiring are
opt-in.
---
## 7. Zinc Boundary
### Why Zinc is used
Zinc provides the shared-memory substrate for:
- low-latency control-plane reads
- intent publication
- slot state snapshots
- zero-copy observation across processes
### Hot-path intent region
Written by the alpha/launcher side, read by the kernel.
### Hot-path state region
Written by the kernel, read by the alpha side or operator tooling.
### Control region
Used for runtime mode switches and operator commands.
### Invariants
1. Shared-memory state must not silently diverge from kernel state.
2. Writes should be explicit and versioned.
3. The kernel must not rely on duplicated Python shadow state as authority.
---
## 8. Hazelcast / ClickHouse Boundary
### Hazelcast
Hazelcast is the durable projection mirror for:
- confirmed slot state
- control snapshot mirroring
- active slot registry
- trade event topic emission
### ClickHouse
ClickHouse is the observability and debug journal sink. In debug mode, the
kernel should emit enough rows to reconstruct a transition timeline.
### Compatibility rule
All emitted rows must remain compatible with the BLUE/PINK schema family.
The DITAv2 layer does not invent a new observability universe unless the
schema is explicitly versioned.
---
## 9. Venue Adapters
### Mock venue
File:
- `prod/clean_arch/dita_v2/mock_venue.py`
Behavior:
- deterministic
- BingX-shaped semantics
- configurable reject / partial fill / cancel reject scenarios
- useful for FSM and race testing
### BingX venue
File:
- `prod/clean_arch/dita_v2/bingx_venue.py`
Behavior:
- thin normalization layer
- converts BingX order/account payloads into DITAv2 events/orders
- no reimplementation of exchange logic
- live adapter backed by the direct BingX client path
### Adapter rule
If a mock cannot faithfully mirror BingX behavior in an in-scope path, the
adapter layer must map actual BingX responses into DITAv2 contracts instead of
inventing a separate semantic model.
---
## 10. Launcher and Operator Flow
### Launcher responsibilities
- assemble control plane
- assemble Zinc plane
- assemble projection sink
- select venue adapter
- create the kernel
### Operator controls
Supported command surfaces:
- `prod/ops/dita_v2_ctl.py`
- `prod/supervisor/supervisorctl.sh dita_v2 ...`
- direct `supervisorctl` against `dolphin:dita_v2`
### Script modes
`prod/launch_dita_v2.py` supports:
- `once`
- `serve`
`serve` is the supervised long-running mode. `once` is for snapshot/debug use.
---
## 11. Observability and Debugging
### Debug mode
When debug mode is enabled, the kernel should log:
- state image changes
- transition triggers
- venue requests and responses
- local lock / unlock points
- reconciliation events
- diagnostics and anomaly codes
### Error surface
The kernel must emit deterministic diagnostic codes for:
- invalid slot ID
- busy slot
- no active exit order
- invalid transition
- stale-state reconcile
- duplicate event / replay no-op
- venue rejection
The point is to make failures explainable and machine-queryable.
---
## 12. Testing Strategy
The DITAv2 suite is intentionally wide. It includes:
- kernel-only FSM tests
- extensive state-machine tests
- race / off-by-one / memory anomaly tests
- Zinc interaction tests
- Hazelcast projection tests
- BingX adapter tests
- full-stack E2E / functional tests through the kernel
- BLUE/PINK-style signal gamut coverage, including entry, exit, partial exit, TP, hung orders, cancel-reject, and non-close cases
- launcher and operator path tests
- supervisor config / documentation tests
- a dedicated kernel hardening suite with 50 collected cases
- mocked exchange-first and BingX-basic E2E paths
- chaos / fuzz coverage over both mock and BingX paths
### Testing order
1. kernel-only unit tests
2. Zinc interaction tests
3. projection tests
4. BingX adapter tests
5. launcher and operator wiring tests
6. full suite rerun
7. full-stack E2E / functional coverage through the kernel
8. chaos / fuzz coverage across mock and BingX
### Current validated result
The DITAv2 suite is currently green with a broad test surface covering the
kernel, launcher, operator wrappers, Zinc, venue adapters, and the full-stack
E2E/chaos matrix through the kernel.
---
## 13. Files of Interest
### Core runtime
- `prod/clean_arch/dita_v2/rust_backend.py`
- `prod/clean_arch/dita_v2/launcher.py`
- `prod/clean_arch/dita_v2/control.py`
- `prod/clean_arch/dita_v2/projection.py`
- `prod/clean_arch/dita_v2/mock_venue.py`
- `prod/clean_arch/dita_v2/bingx_venue.py`
- `prod/clean_arch/dita_v2/real_control_plane.py`
- `prod/clean_arch/dita_v2/real_zinc_plane.py`
- `prod/launch_dita_v2.py`
- `prod/ops/dita_v2_ctl.py`
- `prod/supervisor/supervisorctl.sh`
- `prod/supervisor/dolphin-supervisord.conf`
### Tests
- `prod/tests/test_dita_v2_kernel.py`
- `prod/tests/test_dita_v2_zinc.py`
- `prod/tests/test_dita_v2_hazelcast.py`
- `prod/tests/test_dita_v2_bingx_adapter.py`
- `prod/tests/test_dita_v2_launcher.py`
- `prod/tests/test_launch_dita_v2.py`
- `prod/tests/test_dita_v2_ops.py`
### Operator docs
- `prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md`
- `prod/docs/OPERATIONAL_STATUS.md`
---
## 14. Canonical References
This DITAv2 reference is the canonical entry for the new execution kernel.
Supporting references:
- `prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md`
- `prod/docs/OPERATIONAL_STATUS.md`
- `prod/AGENT_READ_Supervisor_migration.md`
---
## 15. PINK Integration (2026-05-27)
PINK now executes trades through the DITAv2 kernel exclusively.
### How it works
The PINK launcher (`launch_dolphin_pink.py`) calls `build_launcher_bundle()` to
construct a DITAv2 bundle (kernel + BingXVenueAdapter + control plane + Zinc
plane + Hazelcast projection). The `PinkDirectRuntime` bridges policy
(DecisionEngine/IntentEngine) to execution through a `_decision_to_kernel_intent()`
translation seam that maps `Decision`/`Intent` → `KernelIntent`.
### Capital simplification
The kernel's `AccountProjection` is the **single local capital authority**:
1. Exchange balance seeds `kernel.account.snapshot.capital` once at startup/recovery.
2. `kernel.account.settle(slot.realized_pnl)` is called in `on_venue_event()` when
a fill transitions a slot to CLOSED — the **only** capital mutation post-startup.
3. `observe_slots()` handles mark-to-market (unrealized PnL) — no capital writes.
4. `PinkClickHousePersistence` reads capital/peak/trade_seq from the kernel snapshot.
No balance-poll overwrites during the hot loop.
### Files added/changed
- `prod/launch_dolphin_pink.py` — uses `build_launcher_bundle()`
- `prod/clean_arch/runtime/pink_direct.py` — `ExecutionKernel`-backed runtime
- `prod/clean_arch/persistence/pink_clickhouse.py` — reads from kernel account
- `prod/ops/pink_ctl.py` — added `ditav2-status` subcommand
- `prod/tests/test_pink_ditav2_kernel_bridge.py` — mapping tests (7)
- `prod/tests/test_pink_ditav2_rate_limit_contract.py` (1)
- `prod/tests/test_pink_ditav2_restart_reconcile.py` (3)
- `prod/tests/test_pink_ditav2_accounting_invariants.py` (2)
### Live smoke
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --pink --symbol TRXUSDT
```
### PENDING — Live exchange chaos/fuzz
**Status**: Not implemented. Requires a dedicated orchestration layer.
The mock-venue and BingX-basic chaos/fuzz matrix in
`test_dita_v2_e2e_functional.py` provides deterministic fuzzing over mock and
BingX adapter paths (24 cases, all green). True live-testnet chaos/fuzz
against a real order book — non-deterministic event ordering, partial fills at
unpredictable prices, race conditions between submissions and exchange
responses — requires:
- A **live-chaos orchestrator** that submits adversarial intents (rapid
entries/exits, competing cancels, size-at-lot-boundary, cross-book) against
a live BingX testnet symbol.
- An **event-sequencer** that captures raw exchange callback order and
replays it against the kernel to verify deterministic convergence.
- A **state-invariant checker** that asserts slot/account state converges to
the same terminal state regardless of callback ordering.
This is deferred. The current live smoke tests (`test_pink_bingx_dita_live_e2e.py`,
`test_dita_v2_live_bingx_testnet_e2e.py`) cover happy-path E2E cycles only.
### BLUE Non-Impact Proof Checklist
| # | Assertion | Method | Status |
|---|---|---|---|
| 1 | Zero PINK rows in `dolphin` (BLUE) ClickHouse tables | `pink_ctl.py mode-verify` (CH query by `strategy='pink'`) | VERIFIED |
| 2 | Zero PINK rows in `dolphin_prodgreen` ClickHouse tables | `pink_ctl.py mode-verify` (CH query by `strategy='pink'` on prodgreen DB) | VERIFIED |
| 3 | No PINK keys written to BLUE Hazelcast maps (`DOLPHIN_STATE_BLUE`, `DOLPHIN_PNL_BLUE`) | Hazelcast key scan | VERIFIED |
| 4 | No PINK keys written to PRODGREEN Hazelcast maps | Hazelcast key scan | VERIFIED |
| 5 | PINK `trade_events` baseline unchanged (106 rows) | CH count query | VERIFIED |
| 6 | Stopping/restarting PINK does not affect BLUE supervisor programs | `supervisorctl status` before/after | VERIFIED |
| 7 | No BLUE files modified in refactor | `git diff --name-only` (only PINK/DITAv2 paths) | VERIFIED |
| 8 | BLUE runtime env vars unchanged (`DOLPHIN_STATE_BLUE`, `dolphin` DB) | env comparison | VERIFIED |
**Cutover gate**: all 8 assertions must pass before PINK goes live.
**Rollback trigger**: any violation of assertions 1-4 triggers immediate rollback per §6.2 of the refactor guide.
### 15.1 Sync↔Async Seam Analysis (2026-05-27)
**7 distinct boundaries identified and tested**:
| # | Seam | Bridging Mechanism | Test Coverage |
|---|---|---|---|
| 1 | `BingxVenueAdapter._run()` → async backend | 3 modes: passthrough, `asyncio.run()` (no-loop), `ThreadPoolExecutor` (in-loop) | `test_pink_sync_async_seams.py` (36 tests) |
| 2 | `BingxVenueAdapter.connect()` → `BingxDirectExecutionAdapter.connect()` | `_run()` bridges sync→async | 3 tests |
| 3 | `kernel.process_intent()` (sync) → `venue.submit()` (sync) → `_run()` → async HTTP | Thread pool per-call | 4 race-condition tests |
| 4 | `PinkDirectRuntime.step()` (async) → `kernel.process_intent()` (sync) | Direct sync call inside coroutine | 1 nested loop test |
| 5 | `launcher._maybe_close()` (sync) → async close/disconnect | `asyncio.run()` with RuntimeError catch | 4 tests |
| 6 | `_backend_snapshot()` thread safety | No lock — `_last_snapshot` is a plain attribute | 2 concurrent access tests |
| 7 | HTTP client timeout propagation | `httpx.AsyncClient` timeout config | 2 timeout tests |
**Key findings**:
- `_run()` ThreadPoolExecutor creates a new pool per call. At high frequency this could leak threads. Mitigation: chaos harness 10-thread concurrent test verified no leaks under load.
- `_maybe_close()` swallows `RuntimeError` from `asyncio.run()` inside a running loop. This is correct behavior — the close call is best-effort.
- `pink_direct.py` `connect()` now handles both sync and async venue connect methods via `inspect.isawaitable()`.
**Chaos harness**: `test_pink_ditav2_chaos_harness.py` (22 tests) covers:
- Rapid entry→exit, two-leg partial, competing cancel, cancel-after-fill, mark-price, reconcile, size-at-boundary, 10x entry-exit loop
- Edge cases: zero-size entry, negative price entry
- Deterministic replay (ordered and shuffled) — verifies kernel doesn't crash under any event ordering
- State invariants: no stuck slots, no negative capital, no illegal FSM transitions, no critical diagnostics
### 15.2 TODO — Live testnet chaos E2E
**Status**: Not implemented. Requires dedicated work.
The chaos harness (`test_pink_ditav2_chaos_harness.py`) runs all adversarial
scenarios (rapid entry-exit, competing cancel, size-at-boundary, 10x loops)
against the `MockVenueAdapter` only. To reach prod confidence, these same
scenarios must be run against a live BingX VST symbol with:
1. **Exchange-side verification** — orders/positions/account queried directly
from the exchange after each chaos step, not just from kernel state.
2. **Quantity-compliance monitoring** — BingX may truncate or round lot sizes
differently than the adapter expects; the test must assert the exchange
accepted the intended size.
3. **Fill-price tracking** — partial fills at unpredictable prices under
rapid entry-exit must be captured and reconciled against the kernel's
accounting.
4. **Rate-limit cascade testing** — the parallel HTTP gather in
`_refresh_exchange_state` must be verified under sustained rate-limit
pressure.
**Design sketch**:
- Extend `ChaosOrchestrator.run_chaos_scenario()` to accept a
`BingxVenueAdapter` (live) in addition to `MockVenueAdapter`.
- Add a `LiveStateVerifier` that hits the BingX REST API after each step
and asserts kernel state ≈ exchange state within rounding tolerance.
- Gate the live chaos tests with the same `BINGX_SMOKE_LIVE=1` env convention.
- Run the chaos scenarios that are safe for testnet (no cross-book, no
size-at-boundary that would cause a reject chain).
This is deferred because the current live E2E tests cover happy-path cycles
only, and the mock-venue chaos harness validates kernel invariants. Bridging
the two for live chaos is a separate engineering effort.

View File

@@ -1,116 +0,0 @@
# DITAv2 Operator Playbook
This is the operator-facing control surface for the DITAv2 execution kernel.
## Supervisor program
The process is managed as:
`dolphin:dita_v2`
Launcher:
`/mnt/dolphinng5_predict/prod/launch_dita_v2.py`
## Default runtime posture
- `DITA_V2_LAUNCHER_MODE=serve`
- `DITA_V2_VENUE=BINGX`
- `DITA_V2_ZINC=REAL`
- `DITA_V2_CONTROL_PLANE=REAL_ZINC`
- `DITA_V2_HAZELCAST=REAL`
- `DITA_V2_MODE=DEBUG`
- `DITA_V2_VERBOSITY=TRACE`
The launcher defaults remain safe in-process for tests, but the supervised
program is configured for the real shared-memory / live venue path.
## Control commands
Use:
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py status
python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py start
python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py stop
python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py restart
python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py healthcheck
```
These map to:
```bash
supervisorctl -c /mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf <action> dolphin:dita_v2
```
## Live BingX testnet smoke
Use the repeatable live smoke wrapper:
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py
```
Recommended explicit symbol:
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --symbol TRXUSDT
```
What it does:
- loads `/mnt/dolphinng5_predict/.env`
- sets `BINGX_SMOKE_LIVE=1`
- sets `BINGX_SMOKE_ALLOW_TRADE=1`
- sets `DITA_V2_LIVE_BINGX=1`
- starts `dolphin:dita_v2` if it is not already running
- runs `prod/tests/test_dita_v2_live_bingx_testnet_e2e.py`
- preserves the live suite's rate-limit-aware behavior and cleanup paths
Use `--dry-run` to print the exact command and env without trading.
## Validation order
1. Start the process with `start`.
2. Check `status`.
3. Run `healthcheck`.
4. Inspect the logs:
- `/tmp/dolphin_logs/supervisor/dita_v2.log`
- `/tmp/dolphin_logs/supervisor/dita_v2-error.log`
## Stop sequence
1. `python /mnt/dolphinng5_predict/prod/ops/dita_v2_ctl.py stop`
2. Confirm `status` shows the program stopped.
3. Only after that, touch the launcher config or shared-memory state.
## PINK-on-DITAv2 commands
PINK now executes through the DITAv2 kernel. The same supervisor commands
apply, and the following PINK-specific surfaces are available:
### PINK control
```bash
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py status
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py healthcheck
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py ditav2-status
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py mode-verify
```
`ditav2-status` checks the DITAv2 env vars (`DITA_V2_MODE`, `DITA_V2_VENUE`,
etc.) and the `dolphin_pink` supervisor program status.
### PINK live BingX testnet smoke
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --pink --symbol TRXUSDT
```
Use `--dry-run` to print the exact env and pytest command without trading.
### Stop sequence
1. `python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py stop`
2. Confirm `status` shows the process stopped.
3. Inspect logs: `/tmp/dolphin_logs/supervisor/dolphin_live_pink.log`

File diff suppressed because it is too large Load Diff

View File

@@ -1,605 +0,0 @@
# PINK BingX Simplification Spec
Status: Draft for implementation review
Date: 2026-05-22
Owner: Runtime / Trading Systems
Scope: PINK only, with BLUE parity preserved for algorithm comparison
## 1. Purpose
This spec defines a simplified live-trading architecture for PINK that:
1. Preserves the BLUE algorithm exactly.
2. Makes every engine action observable.
3. Uses the exchange as the authoritative source of live position truth.
4. Reuses the existing data structures needed for BLUE/PINK comparison.
5. Reduces hidden state and duplicate decision paths.
6. Keeps PINK mechanically comparable to BLUE wherever the exchange model allows it.
This document does **not** change the signal math, thresholds, or TP/exit logic.
It only simplifies how those decisions move through the system and how they are recorded.
Where BingX semantics differ from BLUE's historical execution surface, the difference must be isolated behind the execution boundary rather than pushed into the engine.
## 2. Design Goals
The architecture must satisfy all of the following:
- Faithfulness to BLUE's original algorithm.
- Full observability of actions as:
- fired
- requested
- sent
- acknowledged
- executed
- reflected on BingX
- Minimal complexity.
- Maximum reuse of existing tables, maps, and record shapes.
- Clean comparability between BLUE and PINK.
- No second domain-level truth source.
## 3. Non-Goals
This spec does not:
- Change the trading signal formula.
- Change the TP value or exit semantics.
- Add a second live source of truth.
- Replace supervisor with a new process manager.
- Introduce a new order ledger when existing tables can be reused.
## 4. Core Principle
PINK must be exchange-led.
That means:
- BingX position state is authoritative for whether the slot is open.
- BingX open-order state is authoritative for whether an exit is pending.
- Account state is a projection of confirmed exchange events.
- Local engine state is a projection of exchange state plus decision metadata.
- ClickHouse is the durable audit trail.
- Hazelcast is the live control/state bus.
- The TUI is a derived view only.
If local state and BingX state disagree, the system must reconcile toward BingX.
BLUE comparability rule:
- The engine-side lifecycle, state names, and record shapes should remain BLUE-compatible unless BingX makes that impossible.
- Any unavoidable exchange-specific deviation must be isolated in the execution adapter and event normalization path.
- The engine itself should remain oblivious to BingX quirks except for the minimal authority rules needed to stay safe.
## 5. Minimal State Model
The system should keep only these live state categories:
- Decision state
- what the engine decided
- Order state
- what was requested and acknowledged
- Position state
- what BingX currently holds
- Account state
- capital, leverage, open notional
- Terminal trade state
- completed trades only
Everything else should be derived from those categories.
The simplification target is not "remove layers entirely".
It is "make the layers explicit and narrow":
```text
engine intent
-> execution facade
-> exchange adapter
-> exchange
-> event normalization
-> durable ledger
```
The `execution facade` is where BLUE-compatible semantics are preserved.
The `exchange adapter` is where BingX-specific request/response shapes live.
`event normalization` is a thin technical return channel inside the execution boundary:
- dedupe exchange callbacks
- normalize terminal states
- map exchange facts into canonical trade/account events
- update projections and durable rows
It is not a separate policy or trading layer.
This spec uses the following DITA split:
- `Decision`
- pure signal evaluation
- `Intent`
- candidate selection and sizing proposal
- `Trade`
- single-slot lifecycle state machine
- `Account`
- projection of confirmed execution facts
## 6. Existing Data Structures to Reuse
This spec reuses the current structures instead of introducing parallel ones.
### 6.1 ClickHouse tables
- `dolphin_pink.position_state`
- lifecycle source for open and closed trade status
- `dolphin_pink.trade_events`
- terminal ledger for completed trades
- `dolphin_pink.account_events`
- capital and exposure snapshots
- `dolphin_pink.v7_decision_events`
- decision trail
- `dolphin_pink.adaptive_exit_shadow`
- shadow-only exit analysis
### 6.2 Hazelcast maps
- `DOLPHIN_STATE_PINK`
- `DOLPHIN_PNL_PINK`
- `DOLPHIN_FEATURES`
- `DOLPHIN_SAFETY`
- `DOLPHIN_HEARTBEAT`
### 6.3 Exchange-side sources
- `user/positions`
- `trade/openOrders`
- `trade/allOrders`
- `trade/allFillOrders`
## 7. Authoritative Precedence
Live truth must be resolved in this order:
```text
BingX user/positions
↓
BingX trade/openOrders
↓
BingX journal snapshot
↓
ClickHouse account_events / position_state
↓
Hazelcast engine snapshot
↓
Supervisor log fallback
```
Rules:
- The first matching live BingX signal wins.
- Local snapshots may lag and must not override BingX.
- Log parsing is a last resort only.
For BLUE comparability:
- The adapter must emit the same semantic milestones BLUE would expose, even if the physical exchange response is different.
- If BingX cannot express a BLUE milestone exactly, preserve the closest semantic equivalent and annotate the deviation in the event payload.
## 8. High-Level Data Flow
```text
+------------------+
| Binance data |
| / HZ features |
+---------+--------+
|
v
+------------------+
| DolphinActor |
| (BLUE logic) |
+---------+--------+
|
v
+------------------+
| NDAlphaEngine |
| single slot only |
+---------+--------+
|
decision / request
|
v
+------------------+
| Execution facade |
| BLUE-compatible |
+---------+--------+
|
exchange-specific request
|
v
+------------------+
| BingXExecClient |
+---------+--------+
|
v
+------------------+
| BingX VST |
| positions/orders |
+---------+--------+
|
poll / ack / fill / close
|
v
+------------------+
| journal snapshot |
+---------+--------+
|
v
+----------------+----------------+
| ClickHouse + Hazelcast + TUI |
+---------------------------------+
```
## 9. Order Lifecycle
The system should treat every trade as a simple state machine.
```text
EMPTY
|
v
DECISION_CREATED
|
v
ORDER_REQUESTED
|
v
ORDER_SENT
|
v
ORDER_ACKNOWLEDGED
|
v
POSITION_OPENED
|
v
POSITION_UPDATED
|
v
EXIT_REQUESTED
|
v
EXIT_SENT
|
v
EXIT_ACKNOWLEDGED
|
v
POSITION_CLOSED
|
v
TRADE_TERMINAL_WRITTEN
|
v
EMPTY
```
Rules:
- A trade is not "closed" until BingX no longer reports the position.
- A terminal close row is not optional.
- The close row must be written after exchange-event normalization confirms terminality, not before.
## 10. Open / Update / Close Mechanics
### 10.1 Open
1. Engine produces a decision.
2. Actor converts it into an intent.
3. Execution facade normalizes the request into a BLUE-compatible action record.
4. Execution client submits the request to BingX.
5. BingX acknowledges or rejects.
6. BingX position becomes authoritative once open.
7. Event normalization updates `position_state` and account projections.
### 10.2 Update
1. Execution client polls `openOrders`.
2. Execution facade records the requested action.
3. Execution client polls `user/positions`.
4. Execution client refreshes account state.
5. Journal snapshot is persisted.
6. ClickHouse rows are appended.
7. Hazelcast state is refreshed.
8. TUI renders the derived result.
### 10.3 Close
1. Engine or exit manager requests exit.
2. Execution facade normalizes the exit into the same lifecycle that BLUE would represent.
3. Exit order is submitted reduce-only.
4. BingX confirms fill or terminal state.
5. Exchange position disappears.
6. Event normalization emits the terminal close fact.
7. `trade_events` close row is written.
8. `position_state` is updated to closed.
## 11. Reconciliation Model
In this spec, "reconciliation" is not a first-class domain layer.
It is the thin adapter-side return path that converts BingX facts into canonical events and projections.
The simplified model is:
```text
engine intent
-> exchange submission
-> exchange state
-> event normalization
-> durable ledger
```
Not:
```text
engine intent
-> local inferred close
-> maybe exchange close later
```
The second pattern is what creates ghost closes and confusing TUI state.
The return path must remain thin and mostly transparent:
- confirm what BingX actually did
- translate exchange reality into canonical engine state and durable ledger rows
- backfill only the minimum terminal bookkeeping needed to keep the audit trail complete
It must not:
- make trading decisions
- invent or reinterpret strategy state
- act as a second policy layer
- override engine intent except where required to reflect BingX authority
In other words:
```text
policy lives in the engine
translation lives in the execution boundary
truth lives on BingX
```
If the return path starts shaping strategy behavior, the architecture has drifted.
## 12. ClickHouse Accounting Contract
### 12.1 `account_events`
This table must represent the latest authoritative snapshot of:
- capital
- open positions
- open notional
- leverage
- fills metadata
It is not the source of truth for execution. It is the projection of confirmed execution facts and the best table for capital-path replay.
### 12.2 `position_state`
This table must represent per-trade lifecycle state.
Required lifecycle states:
- `OPEN`
- `EXIT_REQUESTED`
- `EXIT_ACKED`
- `CLOSED`
- `RECONCILED`
This table is the canonical lifecycle projection, not a second engine.
### 12.3 `trade_events`
This table must represent terminal closed trades only.
Rules:
- one terminal row per completed trade
- dedupe by `trade_id`
- never infer a close row from a fill snapshot alone
### 12.4 `status_snapshots`
When capital replay is needed, `status_snapshots` remains the preferred capital-path source because it captures:
- capital
- posture
- `trades_executed`
- `rm`
- `vol_ok`
- related snapshot state
`trade_events` alone is not enough for capital replay.
## 13. PINK and BLUE Comparison Rules
PINK must remain structurally comparable to BLUE.
That means:
- same trade identity model
- same key fields for open/close events
- same exit reason vocabulary
- same capital accounting semantics
- same bar and hold semantics
Namespace differences are allowed.
Semantic differences are not.
The DITA split must stay semantically compatible with BLUE:
- decision semantics preserved
- intent selection preserved
- trade lifecycle compatible
- account projection comparable
- return-channel normalization exchange-specific only
## 14. Simplification Rules
To reduce bugs, do the following:
### 14.1 Keep one authoritative open-slot view
Do not maintain competing local definitions of "open trade".
### 14.2 Stop inventing closed trades in the TUI
The TUI may display:
- open positions
- terminal trades
- fills
It must not convert fills into fake closes.
### 14.3 Remove recovery ambiguity
At startup:
- BingX positions are imported
- stale local slots are cleared
- journal state is restored only when it does not contradict BingX
- account projection is rebuilt from confirmed exchange facts, not from intent history
### 14.4 Keep the event trail append-only
If a state needs correction, emit a new event.
Do not rewrite history.
## 15. ASCII Failure Modes
### 15.1 Ghost close
```text
EXIT_REQUESTED
|
v
EXIT_SENT
|
+--> local snapshot says CLOSED
|
+--> BingX still shows position OPEN
|
v
BUG: local UI looks flat, exchange is not flat
```
### 15.2 Missing terminal row
```text
EXIT_ACKNOWLEDGED
|
v
POSITION_CLOSED on BingX
|
v
trade_events row missing
|
v
BUG: replay/debug cannot prove the close
```
### 15.3 Duplicate ledger row
```text
trade_events insert
|
+--> duplicate insert for same trade_id
|
v
BUG: replay capital is overstated unless deduped
```
## 16. Acceptance Criteria
The simplification is acceptable only if all of the following hold:
1. BLUE algorithm behavior is preserved exactly.
2. PINK trades can be compared to BLUE trades using the same structures.
3. Every order action is visible in the trail.
4. Every close can be traced to BingX terminal state.
5. TUI never invents a close.
6. Capital replay can be reconstructed from `status_snapshots` plus deduped trade rows.
7. BingX remains the authoritative open-position source.
## 17. Implementation Boundaries
The following are the expected boundaries for any implementation work:
- Launcher layer
- namespace wiring only
- Actor layer
- engine-slot projection and adapter ingress
- Execution facade layer
- BLUE-compatible action normalization
- order lifecycle event emission
- BingX execution layer
- order submit / poll / reconcile / snapshot
- Journal layer
- durable bridge into ClickHouse
- Observability layer
- derived display only
The return path should be treated as a translation boundary, not a policy boundary.
Its ideal steady state is nearly invisible.
Any new BingX-specific behavior should go in the execution or adapter-ingress path, not in the engine decision logic.
## 18. Recommended Simplified Architecture
```text
[decision]
|
v
[intent]
|
v
[trade FSM]
|
v
[execution adapter]
|
v
[BingX order/position]
|
v
[event normalization]
|
v
[ClickHouse account + trade ledger]
|
v
[TUI / replay]
```
This is the simplest version that still preserves BLUE faithfulness and auditability.
## 19. Open Questions
These are implementation questions, not design blockers:
- Should PINK `trade_events` remain fully separate from BLUE-compatible schema, or only namespace-tagged?
- Should the TUI use `account_events` or `position_state` as the primary open-trade panel source?
- Should `position_state` become the canonical lifecycle table for all live strategies, or only PINK first?
- Should any exchange callback normalization be shared with BLUE, or remain PINK-only until parity is proven?
## 20. Final Decision
The target simplification is:
- one engine
- one exchange authority
- one append-only audit trail
- one derived TUI
- one replay path
Anything that introduces a second truth source should be removed or demoted.
Reconciliation, if the term is retained at all, should mean only the thin adapter-side normalization of BingX facts into canonical events and account projection. It should not exist as a policy layer.

View File

@@ -1,79 +0,0 @@
# PINK-on-DITAv2 Fault Taxonomy & Operator Response
## Fault Classes
### RATE_LIMITED
**Kernel code**: `KernelDiagnosticCode.RATE_LIMITED`
**Severity**: WARNING
**Recovery**: Automatic — kernel retries on next step cycle.
Operator action: none required unless persistent (>10 min). If persistent, check BingX API limits at `/openApi/swap/v2/user/balance` directly. Reduce poll frequency via `DOLPHIN_PINK_POLL_INTERVAL_SEC` (default 1.0s).
### ORDER_REJECTED
**Kernel code**: `KernelDiagnosticCode.ORDER_REJECTED`
**Entry reject**: Slot returns to IDLE. Decision engine will re-evaluate on next cycle.
**Exit reject**: Slot stays in EXIT_WORKING. Decision engine will retry exit.
Operator action: check that instrument is tradeable on BingX VST. Symbol precision changes or contract suspensions can cause rejects. Inspect `outcome.details` for venue reason text.
### EXIT_ORDER_REJECTED
**Kernel code**: `KernelDiagnosticCode.EXIT_ORDER_REJECTED`
**Slot state**: EXIT_WORKING. The kernel will retry via process_intent(EXIT) on the next step where the decision engine produces an exit signal.
Operator action: if position remains open past `DOLPHIN_MAX_HOLD_BARS` (default 250), manually flatten via `pink_ctl.py` or direct BingX REST.
### CANCEL_REJECTED
**Kernel code**: `KernelDiagnosticCode.CANCEL_REJECTED`
**Slot state**: Unchanged. Cancel is retried on the next cycle.
Operator action: check open orders on BingX. If the order filled between cancel attempt and rejection, the slot will converge on the next reconcile cycle.
### NO_ACTIVE_EXIT_ORDER
**Kernel code**: `KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER`
**Cause**: Exit intent processed but no working exit order exists (usually because it filled between decision and execution).
Operator action: none — the fill event will converge the slot to CLOSED on the next `on_venue_event` or reconcile.
### STALE_STATE_RECONCILE
**Kernel code**: `KernelDiagnosticCode.STALE_STATE_RECONCILING`
**Slot state**: STALE_STATE_RECONCILING. Normal event progression is blocked until reconciliation completes.
Operator action: if the slot stays in this state for >30s, the exchange snapshot may be inconsistent. Run `pink_ctl.py restart` to force full restart reconcile.
### DUPLICATE_EVENT
**Kernel code**: `KernelDiagnosticCode.DUPLICATE_EVENT`
**Severity**: INFO
**Effect**: Event is dropped. No capital or state change. Idempotency via `seen_event_ids` on the slot.
Operator action: none.
### RATE_LIMITED (persistent cycle)
**Detection**: Consecutive RATE_LIMITED outcomes with no successful exchange interaction.
**Anomaly row origin**: `ditav2_kernel`
Operator action: check exchange API status. If the rate limit window is known, set `DITA_V2_RATE_LIMIT_COOLDOWN_SEC` in env.
## Diagnostic Surface
All fault codes appear in:
- `KernelOutcome.diagnostic_code` (programmatic)
- `KernelOutcome.severity` (INFO/WARNING/ERROR/CRITICAL)
- `KernelOutcome.details` (structured payload with reason, retry_after_ms, etc.)
## Log Paths
- Runtime: `/tmp/dolphin_logs/supervisor/dolphin_live_pink.log`
- Kernel: `/tmp/dolphin_logs/supervisor/dolphin_live_pink-error.log`
## Recovery Tools
```bash
# Check DITAv2 health
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py ditav2-status
# Full restart reconcile
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py restart
# Namespace isolation check
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py mode-verify
```

View File

@@ -1,470 +0,0 @@
# PINK -> DITAv2 Refactor Guide (File-by-File, Implementation-Ready)
## MANDATORY READ ORDER (Before Any Code Change)
Read these documents in this exact order before touching code:
1. `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md` (PINK/DITA addendum scope only; do not broaden scope into BLUE changes)
2. `/mnt/dolphinng5_predict/prod/docs/PINK_BINGX_SIMPLIFICATION_SPEC_2026-05-22.md`
3. `/mnt/dolphinng5_predict/prod/docs/DITA_V2_KERNEL_REFERENCE.md`
4. `/mnt/dolphinng5_predict/prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md`
5. `/mnt/dolphinng5_predict/prod/docs/CLEAN_ARCH_DITA_REFERENCE_PROD_IMPLEMENTATION_SPEC.md`
Do not begin implementation until these are read and the PINK-only boundary is explicit.
## 0) Scope and Goal
This guide is for refactoring **PINK only** to execute trades through **DITAv2 exclusively** (where DITAv2 facilities exist), while preserving:
1. the shared BLUE/PINK signal and trading algorithm semantics,
2. existing PINK observability contracts (Hazelcast, ClickHouse, TUI),
3. strict non-impact on BLUE.
The target is a PINK runtime that is testnet-stable on BingX, with deterministic execution/accounting and explicit handling of known failure classes (hung orders, non-closes, duplicate events, stale/restart drift, rate limits).
---
## 1) Hard Invariants (Must Hold Throughout)
1. **BLUE untouched**:
- No behavior changes in BLUE runtime paths.
- No BLUE namespace changes (`DOLPHIN_STATE_BLUE`, `DOLPHIN_PNL_BLUE`, `dolphin` DB surfaces).
2. **Execution boundary**:
- PINK execution calls must go through DITAv2 kernel + venue adapter.
- No direct PINK exchange-submit path outside DITAv2 where DITAv2 has equivalent functionality.
3. **Algo parity**:
- Entry/exit decision semantics remain shared with BLUE policy logic.
- DITAv2 is execution/risk-state substrate, not strategy rewrite.
4. **Exchange-led truth**:
- Reconcile from exchange snapshots; local state follows exchange, not vice versa.
5. **Accounting determinism**:
- No double-application of realized PnL.
- Multi-leg closes apply capital deltas exactly once per economic leg.
---
## 2) Pre-Refactor Safety Baseline
## 2.1 Files to snapshot before edits
- `/mnt/dolphinng5_predict/prod/launch_dolphin_pink.py`
- `/mnt/dolphinng5_predict/prod/clean_arch/runtime/pink_direct.py`
- `/mnt/dolphinng5_predict/prod/ops/pink_ctl.py`
- `/mnt/dolphinng5_predict/prod/configs/pink.yml`
- `/mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf`
## 2.2 Baseline behavior capture (mandatory)
Capture and store:
1. PINK entry -> partial exit -> final exit behavior.
2. PINK state transitions for cancel/reject/reconcile.
3. ClickHouse deltas:
- `dolphin_pink.trade_events`
- `dolphin_pink.position_state`
- `dolphin_pink.account_events`
- `dolphin_pink.v7_decision_events`
4. Hazelcast deltas:
- `DOLPHIN_STATE_PINK`
- `DOLPHIN_PNL_PINK`
5. TUI fields used by `dolphin_status_pink.py`.
This is the parity baseline used to prove "algo unchanged, execution substrate changed."
---
## 3) File-by-File Refactor Plan
## 3.1 Runtime entrypoint and boundary
### File: `/mnt/dolphinng5_predict/prod/launch_dolphin_pink.py`
### Objective
Convert launcher wiring so PINK execution is DITAv2-native by default.
### Required edits
1. Keep namespace hardening for PINK:
- `strategy_name=pink`
- `DOLPHIN_STATE_PINK`, `DOLPHIN_PNL_PINK`
- `journal_strategy=pink`, `journal_db=dolphin_pink`
2. Replace/retire legacy DITA execution object graph for trade execution:
- stop using legacy `prod.clean_arch.dita.*` execution path as primary.
- construct DITAv2 bundle (`prod.clean_arch.dita_v2.launcher`).
3. Explicit DITAv2 env defaults for PINK launcher:
- `DITA_V2_VENUE=BINGX`
- `DITA_V2_ZINC=REAL`
- `DITA_V2_CONTROL_PLANE=REAL_ZINC`
- `DITA_V2_HAZELCAST=REAL`
- `DITA_V2_LAUNCHER_MODE=serve`
4. Keep BingX env safety:
- `DOLPHIN_BINGX_ENV=VST`
- `DOLPHIN_BINGX_ALLOW_MAINNET=0`
5. Continue loading `BINGX_API_KEY`/`BINGX_SECRET_KEY` from `.env` contract.
### Acceptance checks
1. PINK launcher starts and uses DITAv2 bundle path.
2. No BLUE state map/DB writes from this path.
3. PINK still exposes expected runtime metadata in HZ/CH.
---
### File: `/mnt/dolphinng5_predict/prod/clean_arch/runtime/pink_direct.py`
### Objective
Replace legacy execution orchestration with DITAv2 intent/event orchestration while preserving decision semantics.
### Required edits
1. Introduce a dedicated translation seam:
- Decision output -> `KernelIntent` mapping (`ENTER`, `EXIT`, `MARK_PRICE`, `CANCEL`, `RECONCILE`).
2. Route execution through:
- `ExecutionKernel.process_intent(...)`
- `ExecutionKernel.on_venue_event(...)` for reconcile/event ingestion.
3. Keep policy/decision logic unchanged:
- do not rewrite velocity/IRP/threshold policy semantics.
4. On every execution phase:
- reconcile from exchange (through DITAv2 BingX venue path),
- project state from DITAv2 slot/account snapshot,
- emit persistence payloads from DITAv2 outcomes/events.
5. Handle diagnostics explicitly:
- `RATE_LIMITED`, `ORDER_REJECTED`, `EXIT_ORDER_REJECTED`, `CANCEL_REJECTED`, `NO_ACTIVE_EXIT_ORDER`, stale/reconcile signals.
6. Enforce idempotence:
- repeated venue `event_id` must not re-apply economic effects.
### Acceptance checks
1. Slot/FSM states are deterministic for nominal and rejection paths.
2. No hung local state when exchange is flat.
3. PINK accounting rows remain schema-compatible and single-application.
---
### File: `/mnt/dolphinng5_predict/prod/clean_arch/adapters/bingx_direct.py`
### Objective
Keep exchange edge behavior normalized for DITAv2 and resilient to rate limits.
### Required edits
1. Preserve/extend mapping of BingX throttle responses to `RATE_LIMITED`.
2. Ensure refresh/reconcile endpoints degrade safely (empty snapshot) under transient throttles rather than crashing runtime.
3. Preserve `reduceOnly` semantics for exits and close-out operations.
4. Ensure all normalization fields required by DITAv2 are present:
- `orderId`, `clientOrderId`, `status`, reason/message, retry hints.
### Acceptance checks
1. Adapter never causes runtime crash on nominal exchange throttling.
2. DITAv2 receives normalized status it can classify deterministically.
---
### File: `/mnt/dolphinng5_predict/prod/clean_arch/dita_v2/bingx_venue.py`
### Objective
Guarantee PINK gets first-class DITAv2 venue events for all exchange reactions.
### Required edits
1. Keep/extend mapping for:
- ACK/FILL/PARTIAL_FILL
- REJECT/CANCEL_REJECT
- RATE_LIMITED
2. Ensure `metadata` carries actionable downstream fields:
- retryability, `retry_after_ms` if present, reason, venue status text.
3. Ensure `reconcile()` emits consistent event stream usable for restart recovery.
### Acceptance checks
1. No "unknown event kind" on observed BingX payloads.
2. Reconcile events are sufficient to converge slot state after restart.
---
## 3.2 PINK persistence and observability compatibility
### File: `/mnt/dolphinng5_predict/prod/clean_arch/persistence/pink_clickhouse.py`
### Objective
Keep PINK tables contract-compatible while sourcing execution truth from DITAv2.
### Required edits
1. Ensure row builders consume DITAv2 outcome/event metadata where needed.
2. Preserve existing table contracts:
- `policy_events`
- `v7_decision_events`
- `trade_events`
- `position_state`
- `account_events`
- `anomaly_events`
3. Add explicit anomaly rows for:
- rate-limited retry cycles breaching threshold,
- hung-order timeout escalations,
- reconcile divergence resolution events.
### Acceptance checks
1. No schema drift breaking existing PINK dashboards/TUI.
2. Capital/event rows reconcile to exchange-led lifecycle.
---
### File: `/mnt/dolphinng5_predict/prod/clickhouse/pink/*.sql`
### Objective
Ensure schema supports DITAv2 diagnostic characterization without breaking old readers.
### Required edits
1. Add columns only if required and backward-compatible:
- diagnostic code,
- severity,
- retryability/retry hints,
- reconcile markers.
2. Do not remove or repurpose existing columns read by current tooling.
### Acceptance checks
1. Existing readers still run.
2. New DITAv2 fault/diagnostic fields are queryable.
---
### File: `/mnt/dolphinng5_predict/prod/ops/pink_ctl.py`
### Objective
Make PINK operator tooling DITAv2-aware.
### Required edits
1. Keep PINK namespace isolation checks as-is.
2. Add DITAv2-specific health assertions:
- kernel mode/verbosity/backend mode from control plane,
- DITAv2 process health in supervisor.
3. Add a command (or output block) for live smoke execution status.
### Acceptance checks
1. `status`, `healthcheck`, `mode-verify` remain PINK-only.
2. Tool can detect DITAv2 miswiring immediately.
---
### File: `/mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf`
### Objective
Ensure PINK runs supervised with DITAv2-backed runtime, BLUE unaffected.
### Required edits
1. Keep BLUE programs unchanged.
2. Ensure `dolphin_pink` program points to refactored PINK launcher path.
3. Keep clear comments that PINK is VST/testnet and isolated.
### Acceptance checks
1. `supervisorctl status` shows BLUE and PINK independently healthy.
2. Stopping/restarting PINK does not impact BLUE services.
---
## 3.3 Test harness and execution quality
### File: `/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py`
### Objective
Primary live testnet acceptance suite for PINK-on-DITAv2.
### Required edits
1. Ensure it drives DITAv2 path only.
2. Include full operational gamut:
- entry
- mark
- partial exit
- final exit
- cancel/cancel-after-flat
- reconcile/restart-style checks
3. Accept nominal exchange reactions while asserting deterministic kernel finality.
4. Add explicit verification blocks:
- open orders/positions are flat after cleanup,
- no orphan slot state.
### Acceptance checks
1. Suite passes reliably with rate-limit-respectful cadence.
2. No residual exposure after test completion.
---
### File: `/mnt/dolphinng5_predict/prod/tests/test_pink_direct_runtime.py`
### Objective
Kernel integration correctness in non-live conditions.
### Required edits
1. Replace old execution assertions with DITAv2-based assertions:
- intent mapping,
- emitted events,
- diagnostic handling,
- slot transitions.
2. Add tests for duplicate event replay and stale-state reconcile.
### Acceptance checks
1. Runtime behavior deterministic under mock/fuzzed event schedules.
2. No double-booking of capital in partial/full close chains.
---
### File: `/mnt/dolphinng5_predict/prod/tests/test_pink_clickhouse_persistence.py`
### Objective
Prevent accounting/persistence regressions.
### Required edits
1. Validate per-leg and terminal close semantics from DITAv2 outcomes.
2. Validate anomaly/diagnostic row emission for non-nominal conditions.
### Acceptance checks
1. Capital deltas and position-state terminality are consistent.
2. Replay/restart write paths remain coherent.
---
### New test files to add
1. `/mnt/dolphinng5_predict/prod/tests/test_pink_ditav2_kernel_bridge.py`
- Decision->KernelIntent mapping table tests.
2. `/mnt/dolphinng5_predict/prod/tests/test_pink_ditav2_rate_limit_contract.py`
- Retryable warning classification + downstream emission tests.
3. `/mnt/dolphinng5_predict/prod/tests/test_pink_ditav2_restart_reconcile.py`
- crash/restart reconcile convergence tests.
4. `/mnt/dolphinng5_predict/prod/tests/test_pink_ditav2_accounting_invariants.py`
- multi-leg non-double-book proofs.
---
## 3.4 Documentation and runbooks
### Files to update
1. `/mnt/dolphinng5_predict/prod/docs/PINK_BINGX_SIMPLIFICATION_SPEC_2026-05-22.md`
2. `/mnt/dolphinng5_predict/prod/docs/DITA_V2_KERNEL_REFERENCE.md`
3. `/mnt/dolphinng5_predict/prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md`
4. `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md` (addendum only)
### Required doc updates
1. Explicit statement: PINK execution boundary is DITAv2.
2. Exact live smoke and healthcheck commands.
3. Fault taxonomy and operator response for rate limit/reject/hung/reconcile paths.
4. BLUE non-impact proof checklist.
---
## 4) Implementation Sequence (Strict Order)
1. Freeze BLUE + baseline capture.
2. Launcher boundary wiring (`launch_dolphin_pink.py`).
3. Runtime bridge (`pink_direct.py`) to DITAv2 intents/events.
4. Persistence projection alignment (`pink_clickhouse.py` + SQL if needed).
5. Operator/control updates (`pink_ctl.py`, supervisor stanza check).
6. Non-live tests (unit/integration/fsm).
7. Mock E2E and chaos/fuzz.
8. Live BingX testnet basic cycles.
9. Live BingX testnet chaos/fuzz.
10. Soak and finalize docs/runbook.
Do not reorder. Live testing before accounting invariants is not allowed.
---
## 5) Mandatory Validation Matrix
## 5.1 Deterministic execution finality
For each action path (ENTER, EXIT partial, EXIT final, CANCEL, RECONCILE), assert:
1. deterministic final slot state,
2. deterministic diagnostic code on failure paths,
3. deterministic account/capital projection effect.
## 5.2 Known failure class coverage
1. Hung order:
- timeout monitor triggers,
- reconcile/cancel cycle emits diagnostics,
- eventual terminality is explicit.
2. Non-close:
- position remains visible in exchange snapshot until actually flat,
- no premature local close state.
3. Duplicate/replayed events:
- no duplicate capital/PnL application.
4. Restart/reconcile drift:
- restart with open exchange position converges to correct slot state.
5. Rate limit:
- classified as retryable warning,
- downstream emitted with code/severity/hints,
- no state corruption.
## 5.3 Namespace isolation
1. No `pink` strategy rows in `dolphin` or `dolphin_prodgreen`.
2. No PINK writes to BLUE HZ maps.
3. PINK stop/start/restart has zero BLUE impact.
---
## 6) Cutover and Rollback
## 6.1 Cutover gates
All must be true:
1. Non-live suite green.
2. Mock E2E + chaos/fuzz green.
3. Live testnet basic and chaos/fuzz green.
4. No unresolved hung/non-close cases in soak window.
5. Accounting parity checks pass.
## 6.2 Rollback trigger conditions
Rollback immediately if any:
1. unresolved exposure after cleanup,
2. non-deterministic capital drift,
3. repeated stale/reconcile divergence,
4. contamination of BLUE/PRODGREEN namespaces.
## 6.3 Rollback action
1. Stop PINK only.
2. Revert PINK launcher/runtime to pre-refactor revision.
3. Keep forensic artifacts (CH/HZ rows, logs, diagnostics) for postmortem.
---
## 7) Operational Commands (Post-Refactor)
1. PINK control:
```bash
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py status
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py healthcheck
python /mnt/dolphinng5_predict/prod/ops/pink_ctl.py mode-verify
```
2. DITAv2 live smoke command (rate-limit respectful suite):
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --symbol TRXUSDT
```
3. Dry-run (no orders):
```bash
python /mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py --dry-run --symbol TRXUSDT
```
---
## 8) Definition of Done
1. PINK uses DITAv2 execution facilities exclusively where available.
2. Shared BLUE/PINK strategy semantics are preserved.
3. BLUE is behaviorally unaffected.
4. PINK supports entries, exits, partial exits, TP/SL-driven exits, cancel/reconcile/restart.
5. Accounting is deterministic and restart-safe.
6. Live testnet E2E + chaos/fuzz passes with exchange-side verification.

View File

@@ -1,608 +0,0 @@
# PINK Re-Architecture Specification (Implementation Blueprint)
Status: Approved-for-coding spec (no code in this document)
Date: 2026-05-19
Owner: Runtime/Infra
Target: Add isolated `PINK` testnet execution system with identical trading algorithm behavior to BLUE, while keeping BLUE undisturbed.
---
## 1. Executive Decision
### 1.1 Decision
Build `PINK` as an **isolated sidecar system** with dedicated namespaces and control surfaces, then optionally migrate that sidecar’s infra onto Podman+Quadlet+systemd.
### 1.2 Why this decision
- BLUE must remain undisturbed.
- Current codebase hard-routes many `prod*` paths into PRODGREEN sinks; a naive clone collides.
- BingX account journaling currently dominates data volume and must be controlled explicitly.
### 1.3 Non-negotiable invariant
The **trading algorithm logic must remain identical to BLUE** (signal math, thresholds, decision state machine semantics).
---
## 2. Hard Constraints (Must Hold)
1. No behavior change in core trading logic vs BLUE.
2. No write contamination across BLUE/GREEN/PINK CH databases.
3. No write contamination across BLUE/GREEN/PINK Hazelcast maps.
4. BLUE process manager and lifecycle remain unchanged during PINK buildout.
5. PINK must run BingX in VST/testnet mode only until explicit go-live gate.
6. Any infra re-architecture must be introduced to PINK first, never by replacing BLUE in-place.
---
## 3. Current-State Evidence (Reference Anchors)
### 3.1 Supervisord-first doctrine
- `prod/docs/SYSTEM_BIBLE_v7.md` states all dolphin services are supervisord-managed and warns against dual-management races.
- See:
- `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md:11`
- `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md:1339`
- `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md:3377`
### 3.2 Namespace split already in doctrine
- BLUE: `dolphin`, `DOLPHIN_STATE_BLUE`, `DOLPHIN_PNL_BLUE`
- PRODGREEN: `dolphin_prodgreen`, `DOLPHIN_STATE_PRODGREEN`, `DOLPHIN_PNL_PRODGREEN`
- See:
- `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md:11`
- `/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md:14`
### 3.3 Hardcoded routing that collides with new strategy names
- BLUE trader hardcoded map keys:
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1740`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1741`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1850`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:2806`
- `DolphinActor` routes `strategy.startswith("prod")` to PRODGREEN sink:
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:179`
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:180`
- BingX execution hardcodes PRODGREEN strategy/db:
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:263`
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:532`
- BingX journal maps `prod*` -> `dolphin_prodgreen`:
- `/mnt/dolphinng5_predict/prod/bingx/journal.py:90`
- `/mnt/dolphinng5_predict/prod/bingx/journal.py:91`
### 3.4 Current BingX poll cadence (main source of account-event volume)
- Poll loops:
- open orders loop
- positions loop
- account loop
- See:
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:707`
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:723`
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:732`
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:741`
- Default intervals:
- `/mnt/dolphinng5_predict/prod/bingx/config.py:58`
- `/mnt/dolphinng5_predict/prod/bingx/config.py:59`
- `/mnt/dolphinng5_predict/prod/bingx/config.py:60`
### 3.5 Data volumes measured (14 complete days; 2026-05-05 to 2026-05-18)
- BLUE-like CH outgoing payload estimate: ~4.17 MB/day avg, ~12.01 MB/day p95-day.
- BLUE-like HZ outgoing payload estimate: ~100.03 MB/day avg, ~301.53 MB/day p95-day.
- PRODGREEN-style BingX `account_events` stream estimate: ~7.41 GB/day avg, ~18.57 GB/day p95-day.
---
## 4. Scope
## 4.1 In scope
1. Introduce first-class `PINK` namespace contract across CH/HZ/control-plane.
2. Preserve algorithm semantics exactly.
3. Isolate PINK execution in BingX VST.
4. Add explicit friction/cost characterization outputs.
5. Add infra spec for Podman+Quadlet+systemd deployment of PINK stack.
## 4.2 Out of scope
1. Any change to signal formula/thresholds/risk decision logic.
2. Any BLUE teardown or manager migration in this phase.
3. Any LIVE mainnet enablement for PINK.
---
## 5. Naming and Namespace Contract
## 5.1 Strategy naming
- Strategy name for new instance: `pink` (lowercase).
- Disallowed for this phase: names with `prod` prefix (e.g., `prodpink`) because current routing treats `prod*` specially.
## 5.2 ClickHouse namespace
- New DB: `dolphin_pink`.
- Required tables (minimum):
- `trade_events`
- `trade_reconstruction`
- `trade_exit_legs`
- `v7_decision_events`
- `adaptive_exit_shadow`
- `account_events`
- `status_snapshots`
- Optional parity tables if needed by downstream tooling:
- `sc_threshold_advisor_shadow`
- `sc_bucket_gauge_shadow`
- `inverse_ars_bounce_shadow`
## 5.3 Hazelcast namespace
- Maps:
- `DOLPHIN_STATE_PINK`
- `DOLPHIN_PNL_PINK`
- Control-plane runtime command queue key:
- `pink_runtime_commands`
- Capital mirror key:
- `pink_capital_update_latest`
## 5.4 Trader identity
- Trader ID default:
- `DOLPHIN-PINK-001`
---
## 6. Required File-Level Changes (Coding Agent Worklist)
Important: This section is prescriptive. Implement all items unless explicitly marked optional.
## 6.1 Sink/routing abstraction
### 6.1.1 `prod/ch_writer.py`
Current state exposes only `_writer`, `_writer_green`, `_writer_prodgreen` and corresponding functions.
- Source anchor: `/mnt/dolphinng5_predict/prod/ch_writer.py:302`
Required:
1. Add `_writer_pink = _CHWriter(db="dolphin_pink")`.
2. Add `ch_put_pink(table: str, row: dict) -> None`.
3. Do not modify behavior of existing sink functions.
Acceptance:
- Unit test asserts writes called via `ch_put_pink` target `dolphin_pink` only.
### 6.1.2 `prod/bingx/journal.py`
Current `_db_for_strategy` routes `prod*` to `dolphin_prodgreen`.
- Anchor: `/mnt/dolphinng5_predict/prod/bingx/journal.py:88`
Required:
1. Replace ad-hoc prefix routing with explicit strategy->db map.
2. Add explicit `pink -> dolphin_pink` mapping.
3. Keep existing `blue`, `green`, `prodgreen` compatibility.
4. Update sink selection to include `ch_put_pink`.
Acceptance:
- For `strategy='pink'`, both journal snapshot writes and lookup reads use only `dolphin_pink`.
### 6.1.3 `prod/bingx/execution.py`
Current code hardcodes:
- `self._journal_strategy = "prodgreen"`
- account-events insert URL database `dolphin_prodgreen`
- Anchors:
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:263`
- `/mnt/dolphinng5_predict/prod/bingx/execution.py:532`
Required:
1. Add config-driven `journal_strategy` and `journal_db` fields.
2. Default for existing prodgreen path remains unchanged.
3. PINK launcher passes `journal_strategy='pink'`, `journal_db='dolphin_pink'`.
4. Remove any remaining hardcoded `dolphin_prodgreen` in account-event path.
Acceptance:
- No writes from PINK execution appear in `dolphin_prodgreen.account_events`.
## 6.2 Actor and launcher namespace configurability
### 6.2.1 `prod/launch_dolphin_live.py`
Current defaults are prodgreen-centric:
- state/pnl maps and strategy name.
- Anchors:
- `/mnt/dolphinng5_predict/prod/launch_dolphin_live.py:78`
- `/mnt/dolphinng5_predict/prod/launch_dolphin_live.py:79`
- `/mnt/dolphinng5_predict/prod/launch_dolphin_live.py:132`
Required:
1. Introduce generic env-driven namespace fields:
- `DOLPHIN_STRATEGY_NAME`
- `DOLPHIN_STATE_MAP`
- `DOLPHIN_PNL_MAP`
- `DOLPHIN_ADAPTIVE_EXIT_DB`
- `DOLPHIN_V7_JOURNAL_DB`
2. Keep prodgreen defaults backward-compatible.
3. Add dedicated PINK launcher module or mode wrapper with PINK defaults.
Acceptance:
- Running PINK launcher without overrides lands in PINK namespaces only.
### 6.2.2 `nautilus_dolphin/nautilus/.../dolphin_actor.py`
Current default + routing:
- `strategy_name='prodgreen'`
- `startswith("prod")` sink logic
- state/pnl defaults map to PRODGREEN
- Anchors:
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:179`
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:180`
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:181`
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:185`
- `/mnt/dolphinng5_predict/nautilus_dolphin/nautilus_dolphin/nautilus/dolphin_actor.py:189`
Required:
1. Replace prefix-based sink selection with explicit strategy mapping.
2. Add first-class `pink` mapping for CH sink + default shadow db.
3. Keep old strategy names functional.
4. Ensure aliases do not include BLUE keys in PINK mode.
Acceptance:
- Actor in `pink` mode never writes to `DOLPHIN_STATE_PRODGREEN`, `DOLPHIN_PNL_PRODGREEN`, or `dolphin_prodgreen`.
## 6.3 Control-plane keys and capital surfaces
### 6.3.1 `prod/nautilus_event_trader.py` (if PINK reuses this path)
Current BLUE hardcoding includes:
- `DOLPHIN_STATE_BLUE`, `DOLPHIN_PNL_BLUE`, `blue_runtime_commands`
- Anchors:
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1740`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1741`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:1850`
- `/mnt/dolphinng5_predict/prod/nautilus_event_trader.py:2806`
Required (only if this file is used for PINK runtime):
1. Parameterize map names and runtime queue key.
2. Preserve BLUE defaults exactly.
3. Add PINK equivalents via env/config.
Acceptance:
- `SET_CAPITAL` / `CAPITAL_UPDATE` for PINK only affects PINK state surfaces.
Note: Preferred approach is to keep BLUE runtime on this file untouched and run PINK through launcher/actor path first.
## 6.4 Ops scripts and tooling
### 6.4.1 `prod/ops/prodgreen_ctl.py`
Current script is hardcoded to PRODGREEN namespaces.
- Anchors:
- `/mnt/dolphinng5_predict/prod/ops/prodgreen_ctl.py:23`
- `/mnt/dolphinng5_predict/prod/ops/prodgreen_ctl.py:24`
- `/mnt/dolphinng5_predict/prod/ops/prodgreen_ctl.py:42`
Required:
1. Create `pink_ctl.py` OR generalize into namespace-aware ctl tool.
2. Required commands: status, healthcheck, start, stop, restart, mode-verify.
3. Must not invoke BLUE program names by default.
Acceptance:
- `pink_ctl status` reports PINK CH/HZ surfaces only.
---
## 7. ClickHouse Schema Plan for `dolphin_pink`
## 7.1 Strategy
Clone `prodgreen` schema set as baseline for PINK to preserve execution-profile columns.
Reference DDLs:
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/00_create_database.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/account_events.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/status_snapshots.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/trade_events.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/v7_decision_events.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/adaptive_exit_shadow.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/02_create_trade_reconstruction.sql`
- `/mnt/dolphinng5_predict/prod/clickhouse/prodgreen/03_create_trade_exit_legs.sql`
## 7.2 Required migration artifacts
Create new folder:
- `prod/clickhouse/pink/`
Include:
1. `00_create_database.sql` -> `CREATE DATABASE IF NOT EXISTS dolphin_pink;`
2. Full table DDL scripts mirroring prodgreen table structures.
3. Apply script with idempotent checks.
## 7.3 Guardrails
1. No schema mutation to existing `dolphin` or `dolphin_prodgreen` in this phase.
2. No historical retagging/movement required for initial PINK bring-up.
---
## 8. Hazelcast Map and Key Contract
## 8.1 Required map names
- `DOLPHIN_STATE_PINK`
- `DOLPHIN_PNL_PINK`
## 8.2 Required keys in `DOLPHIN_STATE_PINK`
- `engine_snapshot`
- `capital_checkpoint`
- `latest_nautilus`
- optional replay/control keys mirrored from blue contract if PINK runtime supports same capital workflows
## 8.3 Control-plane keys
- Runtime command queue: `pink_runtime_commands`
- Latest capital update mirror: `pink_capital_update_latest`
## 8.4 Isolation validation rule
A PINK process must never read/write keys under `DOLPHIN_STATE_BLUE` or `DOLPHIN_PNL_BLUE` except explicitly allowed read-only analytics queries.
---
## 9. BingX VST Behavior Contract
## 9.1 Environment
- `DOLPHIN_BINGX_ENV=VST`
- `DOLPHIN_BINGX_ALLOW_MAINNET=0`
## 9.2 Expected data venue / exec venue
- Initial recommended mode:
- data venue: BINANCE (same sensing stream as BLUE)
- exec venue: BINGX VST
## 9.3 Leverage/sizing mode
- Use existing sizing-mode mechanisms.
- No strategy-logic change permitted.
---
## 10. Data Resource Budget and Controls
## 10.1 Baseline estimates (from measured data)
### CH + HZ for BLUE-like write path
- CH: ~4.17 MB/day avg, ~12.01 MB/day p95-day
- HZ: ~100.03 MB/day avg, ~301.53 MB/day p95-day
### BingX journal risk stream
- `account_events`: ~7.41 GB/day avg, ~18.57 GB/day p95-day if current high-rate snapshots remain.
## 10.2 Mandatory control for `account_events`
Implement at least one, preferably multiple:
1. Snapshot delta suppression beyond fingerprint-only (field-level sampling and minimum emission interval).
2. `ACCOUNT_REFRESH` write interval floor (e.g., min 2s, then tune).
3. Separate high-granularity debug table optional; production `account_events` should be rate-limited.
4. Configurable hard cap alert on rows/minute.
## 10.3 Acceptance thresholds
1. PINK `account_events` sustained rate must stay below agreed cap (set initial policy: <= 5 rows/sec average over 15 min unless debug mode explicitly enabled).
2. Alert if exceeds cap for > 3 consecutive windows.
---
## 11. Observability and ROI/Friction Outputs
## 11.1 Required KPI outputs
1. Realized ROI (closed trades).
2. Open-equity ROI (mark-to-market).
3. Cost-adjusted ROI.
4. Latency decomposition:
- decision->submit
- submit->ack
- ack->first_fill
- first_fill->done
5. Slippage decomposition (bps against decision/arrival references).
6. Fee/funding components.
## 11.2 Storage location
- PINK metrics rows in `dolphin_pink.trade_events` payload columns and/or dedicated execution quality table.
## 11.3 TUI policy
Current TUI is BLUE-hardcoded in places (`DOLPHIN_STATE_BLUE`, `dolphin.trade_events`, `blue_runtime_commands`).
- Anchors:
- `/mnt/dolphinng5_predict/Observability/dolphin_status.py:513`
- `/mnt/dolphinng5_predict/Observability/dolphin_status.py:558`
- `/mnt/dolphinng5_predict/Observability/dolphin_status.py:1164`
- `/mnt/dolphinng5_predict/Observability/dolphin_status.py:212`
Required:
1. Do not break BLUE TUI.
2. Add either:
- separate `dolphin_status_pink.py`, or
- namespace-parameterized TUI mode.
---
## 12. Podman + Quadlet + systemd Adoption Plan
## 12.1 Strategy
Apply only to PINK stack first.
## 12.2 Preflight checks (must pass before coding)
1. Podman availability on host (`podman --version`).
2. systemd user/service model chosen (rootless preferred unless operationally blocked).
3. Persistent volume paths and permissions validated.
4. ClickHouse config/users mounts parity with current docker-compose pattern.
Current host note: Podman not currently installed (`which podman` returned no result).
## 12.3 Unit boundaries
- BLUE stays under supervisord + current docker compose infra.
- PINK gets independent unit set.
- Do not dual-manage same runtime process with supervisord and systemd.
## 12.4 Quadlet file set for PINK
Create under dedicated path (example):
- `datastack-pink.pod`
- `hazelcast-pink.container` (or reuse cluster only if explicitly designed shared)
- `clickhouse-pink.container` (or shared CH with separate DB if accepted)
- `prefect-pink.container` (if needed)
- `pink-worker.container`
## 12.5 Shared vs dedicated infra policy
Decision required before implementation:
1. Option A (preferred first): shared HZ+CH infra, isolated logical namespaces.
2. Option B: dedicated PINK HZ/CH containers.
Given HZ volatility risk and operational complexity, start with Option A unless a strict physical isolation requirement is imposed.
---
## 13. Algorithm Identity Assurance (Critical)
## 13.1 Required parity harness
Implement deterministic parity checks between BLUE decision path and PINK decision path on identical input replay.
## 13.2 Comparison granularity
At each scan/bar compare tuple hash of:
- signal fired boolean
- selected asset
- side
- leverage intent
- entry/exit action
- reason code
- bars_held progression
No tolerance except for fields explicitly dependent on execution venue acknowledgements.
## 13.3 Fail criteria
Any divergence in pure strategy decisions is a release blocker.
---
## 14. Test Plan (Implementation Exit Criteria)
## 14.1 Unit tests
1. Routing tests for strategy->DB and strategy->HZ map.
2. Sink tests (`ch_put_pink` path).
3. Control key tests (`pink_runtime_commands`).
4. Account-event rate-limit logic tests.
## 14.2 Integration tests
1. Start PINK in VST and verify:
- CH writes only into `dolphin_pink.*`
- HZ writes only into `DOLPHIN_STATE_PINK` / `DOLPHIN_PNL_PINK`
2. Verify no new rows in `dolphin_prodgreen.account_events` during PINK-only test run.
3. Verify BLUE process and metrics unaffected.
## 14.3 Soak tests
1. 24h soak with PINK live in VST.
2. Monitor:
- row rates
- CH insert error rates
- HZ heartbeat age
- control-plane responsiveness
## 14.4 Regression tests
Run existing relevant suites for:
- bingx journaling/accounting
- actor routing
- launch paths
- MHS basic health checks for BLUE unaffectedness
---
## 15. Deployment Sequence (Phased)
## Phase 0: Namespace groundwork
1. Add sink and routing abstractions.
2. Add PINK CH schema migration artifacts.
3. Add PINK launcher and env contract.
Gate 0:
- Compile/tests pass.
- Static grep verifies no hardcoded fallback from `pink` to `prodgreen`.
## Phase 1: PINK logical bring-up (same infra)
1. Start PINK process under current management (or controlled runner) with VST.
2. Verify strict namespace isolation.
3. Run parity harness with replay feed.
Gate 1:
- No contamination.
- Parity pass.
## Phase 2: Data-volume control tuning
1. Tune account-event emission controls.
2. Verify row-rate caps and KPI completeness.
Gate 2:
- Resource budgets stable.
## Phase 3: Optional Podman+Quadlet packaging for PINK
1. Build PINK quadlet units.
2. Validate independent lifecycle.
3. Keep BLUE unchanged.
Gate 3:
- PINK can be fully operated without impacting BLUE.
---
## 16. Rollback Plan
## 16.1 Soft rollback
1. Stop PINK process/unit only.
2. Leave BLUE untouched.
3. Preserve PINK CH/HZ artifacts for postmortem.
## 16.2 Hard rollback
1. Revert routing patches that introduced PINK mapping.
2. Keep PINK DB as historical archive or drop only after approval.
## 16.3 Explicit no-rollback targets
Do not alter BLUE capital/state surfaces during PINK rollback.
---
## 17. Security and Safety
1. PINK VST keys isolated from BLUE credentials.
2. No mainnet enable unless separate approval gate flips `DOLPHIN_BINGX_ALLOW_MAINNET=1`.
3. Validate no accidental propagation of PINK credentials into shared logs.
---
## 18. Deliverables Checklist (Coding Agent Must Produce)
1. Code changes implementing explicit strategy/namespace routing for PINK.
2. `dolphin_pink` CH schema files in `prod/clickhouse/pink/`.
3. PINK launcher/config entrypoint.
4. PINK ops control script or generalized namespace-aware ctl tool.
5. Unit + integration tests for routing/isolation.
6. Parity harness and parity report artifact.
7. Data-rate monitor/report for `account_events` and major tables.
8. Optional: Quadlet unit files for PINK stack (if Phase 3 in scope).
---
## 19. Coding Prohibitions (Strict)
1. Do not alter algorithm constants or decision logic behavior.
2. Do not remove or repurpose BLUE maps/tables.
3. Do not bind PINK to names beginning with `prod` in this phase.
4. Do not change BLUE process manager/runtime flow as part of PINK implementation.
---
## 20. Open Decisions Requiring Explicit Operator Choice
1. PINK infra physical model:
- shared CH/HZ vs dedicated CH/HZ.
2. PINK manager in early phases:
- supervised process first vs direct Quadlet rollout.
3. Account-event rate cap values:
- initial thresholds and alert policy.
If decisions are not provided, default choices are:
- shared CH/HZ with strict logical isolation,
- supervised PINK process before Quadlet migration,
- account-events cap <= 5 rows/sec sustained (debug off).
---
## 21. Minimal Go/No-Go Matrix
Go only if all true:
1. Strategy parity = exact pass.
2. Namespace contamination tests = zero leaks.
3. Data-rate caps respected during soak.
4. BLUE observability and trade loop unchanged.
No-Go if any true:
1. `pink` rows appear in `dolphin_prodgreen` or `dolphin` unexpectedly.
2. BLUE map/table writes change baseline rates materially.
3. Decision parity drifts.
4. VST safety flags not enforced.
---
## 22. Final Operator Notes
- This spec intentionally separates **architecture modernization** from **algorithm behavior**.
- PINK is the safe proving ground for infra re-architecture.
- BLUE remains production reference and must not be structurally disturbed until PINK completes parity + soak + resource gates.

File diff suppressed because it is too large Load Diff

View File

@@ -1,103 +0,0 @@
#!/usr/bin/env python3
"""Operator-facing entrypoint for the DITAv2 kernel.
The launcher is env-driven and intentionally conservative by default:
- mock venue
- in-memory Zinc plane
- callback projection
- control-plane values may be overridden via DITA_V2_* env vars
"""
from __future__ import annotations
import json
import os
import signal
import time
from pathlib import Path
import sys
from dotenv import load_dotenv
PROJECT_ROOT = Path(__file__).parent.parent
load_dotenv(PROJECT_ROOT / ".env")
sys.path.insert(0, str(PROJECT_ROOT / "prod"))
sys.path.insert(0, str(PROJECT_ROOT / "prod" / "clean_arch"))
sys.path.insert(0, str(PROJECT_ROOT))
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _env_float(name: str, default: float) -> float:
raw = os.environ.get(name)
if raw is None:
return default
try:
value = float(str(raw).strip())
except Exception:
return default
return value if value > 0 else default
def _env_mode() -> str:
mode = str(os.environ.get("DITA_V2_LAUNCHER_MODE", "serve")).strip().lower()
if mode in {"once", "serve"}:
return mode
return "serve"
def _serve(bundle) -> int:
interval = _env_float("DITA_V2_LAUNCHER_HEARTBEAT_SEC", 30.0)
stop = False
def _handle_signal(signum, _frame) -> None:
nonlocal stop
stop = True
previous_term = signal.signal(signal.SIGTERM, _handle_signal)
previous_int = signal.signal(signal.SIGINT, _handle_signal)
try:
print(
json.dumps(
{
"status": "serving",
"control": bundle.kernel.control.as_dict(),
"venue": type(bundle.venue).__name__,
"zinc_plane": type(bundle.zinc_plane).__name__,
"projection": type(bundle.projection).__name__,
"heartbeat_sec": interval,
},
indent=2,
sort_keys=True,
default=str,
)
)
while not stop:
time.sleep(interval)
return 0
finally:
signal.signal(signal.SIGTERM, previous_term)
signal.signal(signal.SIGINT, previous_int)
def main() -> int:
bundle = build_launcher_bundle()
try:
mode = _env_mode()
if mode == "once" or _env_bool("DITA_V2_PRINT_SNAPSHOT", False):
print(json.dumps(bundle.kernel.snapshot(), indent=2, sort_keys=True, default=str))
return 0
return _serve(bundle)
finally:
bundle.close()
if __name__ == "__main__":
raise SystemExit(main())

View File

@@ -1,296 +0,0 @@
#!/usr/bin/env python3
"""
Dolphin Live Node — DolphinActor inside NT TradingNode
=======================================================
Phase 1: paper_trading=True (live Binance Futures data, paper fills).
Validates signal parity with nautilus_event_trader.py before live exec.
To go live (Phase 2): set paper_trading=False in build_node().
"""
import os
import sys
import asyncio
from copy import deepcopy
from pathlib import Path
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT / 'nautilus_dolphin'))
sys.path.insert(0, str(PROJECT_ROOT / 'prod'))
sys.path.insert(0, str(PROJECT_ROOT))
from dotenv import load_dotenv
load_dotenv(PROJECT_ROOT / '.env')
from nautilus_trader.live.node import TradingNode
from nautilus_trader.config import TradingNodeConfig, LiveDataEngineConfig, CacheConfig
from nautilus_trader.adapters.binance.config import BinanceDataClientConfig
from nautilus_trader.adapters.binance.common.enums import BinanceAccountType
from nautilus_trader.adapters.binance.factories import BinanceLiveDataClientFactory
from nautilus_trader.model.identifiers import TraderId
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.data_config import BingxDataClientConfig
from prod.bingx.enums import BingxEnvironment
from prod.bingx.data_factories import BingxLiveDataClientFactory
from prod.bingx.factories import BingxLiveExecClientFactory
# Nautilus changed this enum name across versions.
_BINANCE_USDT_FUTURES_ACCOUNT_TYPE = getattr(
BinanceAccountType,
"USDT_FUTURES",
getattr(BinanceAccountType, "USDT_FUTURE", None),
)
if _BINANCE_USDT_FUTURES_ACCOUNT_TYPE is None:
raise AttributeError("BinanceAccountType is missing both USDT_FUTURES and USDT_FUTURE")
# ---------------------------------------------------------------------------
# Universe — 50 OBF assets. Subscribed for live quote cache (order sizing).
# Must cover the full eigen universe so _exec_submit_entry finds live quotes.
# ---------------------------------------------------------------------------
LIVE_ASSETS = [
"BTCUSDT", "ETHUSDT", "BNBUSDT", "SOLUSDT", "XRPUSDT",
"ADAUSDT", "DOGEUSDT", "TRXUSDT", "DOTUSDT", "MATICUSDT",
"LTCUSDT", "AVAXUSDT", "LINKUSDT", "UNIUSDT", "ATOMUSDT",
"ETCUSDT", "XLMUSDT", "BCHUSDT", "NEARUSDT", "ALGOUSDT",
"VETUSDT", "FILUSDT", "APTUSDT", "OPUSDT", "ARBUSDT",
"INJUSDT", "SUIUSDT", "SEIUSDT", "TIAUSDT", "ORDIUSDT",
"WLDUSDT", "FETUSDT", "AGIXUSDT", "RENDERUSDT", "IOTAUSDT",
"AAVEUSDT", "SNXUSDT", "CRVUSDT", "COMPUSDT", "MKRUSDT",
"ENJUSDT", "MANAUSDT", "SANDUSDT", "AXSUSDT", "GALAUSDT",
"ZECUSDT", "DASHUSDT", "XMRUSDT", "NEOUSDT", "QTUMUSDT",
]
# ---------------------------------------------------------------------------
# DolphinActor config — gold-standard params, must match nautilus_event_trader
# ---------------------------------------------------------------------------
DOLPHIN_CONFIG = {
'live_mode': True,
'venue': 'BINANCE',
'data_venue': 'BINANCE',
'exec_venue': 'BINANCE',
'direction': 'short_only',
'hazelcast': {
'host': '127.0.0.1:5701',
'cluster': 'dolphin',
'state_map': 'DOLPHIN_STATE_PRODGREEN',
'imap_pnl': 'DOLPHIN_PNL_PRODGREEN',
},
'paper_trade': {'initial_capital': 25000.0},
'assets': LIVE_ASSETS,
'engine': {
'boost_mode': 'd_liq',
# Signal
'vel_div_threshold': -0.020,
'vel_div_extreme': -0.050,
# Leverage — gold spec: 8x soft / 9x hard
'min_leverage': 0.5,
'max_leverage': 8.0,
'abs_max_leverage': 9.0,
'leverage_convexity': 3.0,
'fraction': 0.20,
'max_account_leverage': 3.0,
# Exits — gold spec: 250 bars max hold
'fixed_tp_pct': 0.0095,
'stop_pct': 1.0,
'max_hold_bars': 250,
# Direction confirm
'use_direction_confirm': True,
'dc_lookback_bars': 7,
'dc_min_magnitude_bps': 0.75,
'dc_skip_contradicts': True,
'dc_leverage_boost': 1.0,
'dc_leverage_reduce': 0.5,
# Asset selection — gold spec: IRP filter disabled in live
'use_asset_selection': True,
'min_irp_alignment': 0.0,
'asset_selector_lookback': 10,
# Fees / slippage
'use_sp_fees': True,
'use_sp_slippage': True,
'sp_maker_entry_rate': 0.62,
'sp_maker_exit_rate': 0.50,
# OB edge
'use_ob_edge': True,
'ob_edge_bps': 5.0,
'ob_confirm_rate': 0.40,
'ob_imbalance_bias': -0.09,
'ob_depth_scale': 1.0,
# Alpha layers
'lookback': 100,
'use_alpha_layers': True,
'use_dynamic_leverage': True,
'seed': 42,
# V7 RT exit engine (GREEN only)
'use_exit_v7': True,
'use_exit_v6': False,
'v6_bar_duration_sec': 5.0,
'bounce_model_path': str(PROJECT_ROOT / 'prod' / 'models' / 'bounce_detector_v3.pkl'),
},
'strategy_name': 'prodgreen',
'vol_p60': 0.00009868,
}
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _env_bool(name: str, default: bool = False) -> bool:
raw = str(os.environ.get(name, str(default))).strip().lower()
return raw in ("1", "true", "yes", "on")
def _resolve_bingx_environment(value: str | None = None) -> BingxEnvironment:
name = str(value or os.environ.get("DOLPHIN_BINGX_ENV", "VST")).strip().upper()
return BingxEnvironment.LIVE if name == "LIVE" else BingxEnvironment.VST
def _resolve_bingx_allow_mainnet(value: str | None = None) -> bool:
if isinstance(value, bool):
return value
raw = str(value or os.environ.get("DOLPHIN_BINGX_ALLOW_MAINNET", "0")).strip().lower()
return raw in ("1", "true", "yes", "on")
def _resolve_bingx_recv_window_ms(value: str | None = None) -> int:
raw = str(value or os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "")).strip()
try:
parsed = int(raw)
return parsed if parsed > 0 else 5_000
except (TypeError, ValueError):
return 5_000
def build_actor_config(
*,
data_venue: str | None = None,
exec_venue: str | None = None,
) -> dict:
actor_cfg = deepcopy(DOLPHIN_CONFIG)
resolved_data_venue = (data_venue or _env_upper("DOLPHIN_DATA_VENUE", actor_cfg["data_venue"])).upper()
resolved_exec_venue = (exec_venue or _env_upper("DOLPHIN_EXEC_VENUE", actor_cfg["exec_venue"])).upper()
actor_cfg["data_venue"] = resolved_data_venue
actor_cfg["exec_venue"] = resolved_exec_venue
actor_cfg["venue"] = resolved_exec_venue
actor_cfg["direction"] = os.environ.get("DOLPHIN_DIRECTION", actor_cfg.get("direction", "short_only"))
return actor_cfg
def build_bingx_exec_client_config(
*,
resolved_bingx_env: BingxEnvironment,
resolved_bingx_allow_mainnet: bool,
resolved_bingx_recv_window_ms: int | None,
assets: list[str] | None = None,
) -> BingxExecClientConfig:
from prod.bingx.config import BingxInstrumentProviderConfig
default_leverage = int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1"))
symbol_filters = tuple(assets) if assets else None
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=resolved_bingx_env,
allow_mainnet=resolved_bingx_allow_mainnet,
recv_window_ms=resolved_bingx_recv_window_ms if resolved_bingx_recv_window_ms is not None else 5_000,
default_leverage=default_leverage,
leverage_by_symbol={symbol: default_leverage for symbol in (assets or [])} if assets else None,
prefer_websocket=_env_bool("DOLPHIN_BINGX_PREFER_WEBSOCKET", True),
instrument_provider=BingxInstrumentProviderConfig(
load_all=True,
symbol_filters=symbol_filters,
),
)
def build_node(
*,
data_venue: str | None = None,
exec_venue: str | None = None,
trader_id: str | None = None,
bingx_environment: BingxEnvironment | None = None,
bingx_allow_mainnet: bool | None = None,
bingx_recv_window_ms: int | None = None,
) -> TradingNode:
resolved_bingx_env = bingx_environment or _resolve_bingx_environment()
resolved_bingx_allow_mainnet = (
bingx_allow_mainnet if bingx_allow_mainnet is not None else _resolve_bingx_allow_mainnet()
)
resolved_bingx_recv_window_ms = (
bingx_recv_window_ms if bingx_recv_window_ms is not None else _resolve_bingx_recv_window_ms()
)
if resolved_bingx_env is BingxEnvironment.LIVE and not resolved_bingx_allow_mainnet:
raise RuntimeError(
"BingX LIVE requested but DOLPHIN_BINGX_ALLOW_MAINNET is not enabled"
)
actor_cfg = build_actor_config(data_venue=data_venue, exec_venue=exec_venue)
actor_cfg["bingx_environment"] = str(resolved_bingx_env.value)
resolved_data_venue = actor_cfg["data_venue"]
resolved_exec_venue = actor_cfg["exec_venue"]
data_clients = {}
exec_clients = {}
if resolved_data_venue == "BINANCE":
api_key = os.environ["BINANCE_API_KEY"]
api_secret = os.environ["BINANCE_API_SECRET"]
data_clients["BINANCE"] = BinanceDataClientConfig(
account_type=_BINANCE_USDT_FUTURES_ACCOUNT_TYPE,
api_key=api_key,
api_secret=api_secret,
testnet=False,
)
elif resolved_data_venue == "BINGX":
from prod.bingx.config import BingxInstrumentProviderConfig
data_clients["BINGX"] = BingxDataClientConfig(
environment=resolved_bingx_env,
allow_mainnet=resolved_bingx_allow_mainnet,
instrument_provider=BingxInstrumentProviderConfig(
load_all=True,
symbol_filters=tuple(actor_cfg.get("assets", [])),
),
)
else:
raise ValueError(f"Unsupported data venue: {resolved_data_venue}")
if resolved_exec_venue == "BINGX":
exec_clients["BINGX"] = build_bingx_exec_client_config(
resolved_bingx_env=resolved_bingx_env,
resolved_bingx_allow_mainnet=resolved_bingx_allow_mainnet,
resolved_bingx_recv_window_ms=resolved_bingx_recv_window_ms,
assets=actor_cfg.get("assets"),
)
trader_id_value = trader_id or os.environ.get("DOLPHIN_TRADER_ID", "DOLPHIN-LIVE-001")
from nautilus_dolphin.nautilus.dolphin_actor import DolphinActor
actor = DolphinActor(config=actor_cfg)
node_config = TradingNodeConfig(
trader_id=TraderId(trader_id_value),
data_clients=data_clients,
exec_clients=exec_clients if exec_clients else None,
data_engine=LiveDataEngineConfig(time_bars_build_with_no_updates=False),
cache=CacheConfig(database=None),
)
node = TradingNode(config=node_config)
if "BINANCE" in data_clients:
node.add_data_client_factory("BINANCE", BinanceLiveDataClientFactory)
if "BINGX" in data_clients:
node.add_data_client_factory("BINGX", BingxLiveDataClientFactory)
if "BINGX" in exec_clients:
node.add_exec_client_factory("BINGX", BingxLiveExecClientFactory)
node.trader.add_strategy(actor)
node.build()
return node
async def run() -> None:
node = build_node()
await node.run_async()
if __name__ == "__main__":
asyncio.run(run())

View File

@@ -1,399 +0,0 @@
#!/usr/bin/env python3
"""PINK live launcher — DITAv2-backed execution.
Wires PINK decision/intent logic through the DITAv2 kernel + BingX venue
adapter. The kernel owns the single-slot FSM, AccountProjection (capital
settled from fills, not balance-poll overwritten), Zinc shared-memory mirror,
and Hazelcast slot projection.
"""
from __future__ import annotations
import asyncio
from copy import deepcopy
import contextlib
import os
import sys
from pathlib import Path
from enum import Enum
from typing import Any
from datetime import datetime
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT / "prod"))
sys.path.insert(0, str(PROJECT_ROOT / "prod" / "clean_arch"))
sys.path.insert(0, str(PROJECT_ROOT))
from dotenv import load_dotenv
load_dotenv(PROJECT_ROOT / ".env")
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from prod.clean_arch.adapters.hazelcast_feed import HazelcastDataFeed
from prod.clean_arch.dita import DecisionConfig
from prod.clean_arch.dita import DecisionEngine
from prod.clean_arch.dita import IntentEngine
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.persistence import PinkClickHousePersistence
from adaptive_exit.market_state_runtime import MarketStateRuntime
from prod.clean_arch.runtime.pink_direct import PinkDirectRuntime
from prod.clean_arch.runtime.runner_heartbeat import (
build_runner_heartbeat_payload,
write_runner_heartbeat,
)
PINK_DEFAULTS = {
"strategy_name": "pink",
"state_map": "DOLPHIN_STATE_PINK",
"pnl_map": "DOLPHIN_PNL_PINK",
"trader_id": "DOLPHIN-PINK-001",
"journal_strategy": "pink",
"journal_db": "dolphin_pink",
"fixed_tp_pct": 0.0020,
"vol_p60_threshold": -1000000000.0,
}
class PinkPhase(str, Enum):
"""Feature-gate phases for the standalone PINK launcher."""
BOOTSTRAP = "bootstrap"
SINGLE_LEG = "single_leg"
MULTI_EXIT = "multi_exit"
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _resolve_bingx_environment() -> BingxEnvironment:
name = str(os.environ.get("DOLPHIN_BINGX_ENV", "VST")).strip().upper()
return BingxEnvironment.LIVE if name == "LIVE" else BingxEnvironment.VST
def _resolve_bingx_allow_mainnet() -> bool:
return _env_bool("DOLPHIN_BINGX_ALLOW_MAINNET", False)
def _resolve_bingx_recv_window_ms() -> int:
raw = str(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "5000")).strip()
try:
parsed = int(raw)
except Exception:
return 5000
return parsed if parsed > 0 else 5000
def _resolve_bingx_exchange_leverage_cap() -> int:
raw = str(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")).strip()
try:
parsed = int(raw)
except Exception:
return 3
return parsed if parsed > 0 else 3
def _resolve_pink_vol_p60_threshold() -> float:
raw = str(os.environ.get("DOLPHIN_PINK_VOL_P60_THRESHOLD", PINK_DEFAULTS["vol_p60_threshold"])).strip()
try:
return float(raw)
except Exception:
return float(PINK_DEFAULTS["vol_p60_threshold"])
def _resolve_pink_phase() -> PinkPhase:
raw = str(os.environ.get("DOLPHIN_PINK_PHASE", PinkPhase.SINGLE_LEG.value)).strip().lower()
for phase in PinkPhase:
if raw == phase.value:
return phase
return PinkPhase.SINGLE_LEG
def _resolve_pink_account_sync_interval_sec() -> float:
"""Account sync is now advisory — kernel tracks capital via settle()
on close. Periodic reconcile re-seeds capital from exchange balance,
mainly as a safety net for long-running sessions."""
raw = str(os.environ.get("DOLPHIN_PINK_ACCOUNT_SYNC_INTERVAL_SEC", "300")).strip()
try:
parsed = float(raw)
except Exception:
return 300.0
return parsed if parsed > 0 else 300.0
def _resolve_pink_exit_leg_ratios(phase: PinkPhase) -> tuple[float, ...]:
if phase is PinkPhase.MULTI_EXIT:
raw = str(os.environ.get("DOLPHIN_PINK_EXIT_LEG_RATIOS", "0.5,1.0")).strip()
ratios: list[float] = []
for chunk in raw.split(","):
try:
value = float(chunk.strip())
except Exception:
continue
if 0.0 < value <= 1.0:
ratios.append(value)
if ratios:
return tuple(ratios)
return (0.5, 1.0)
return (1.0,)
def _set_ditav2_env_defaults() -> None:
os.environ.setdefault("DITA_V2_VENUE", "BINGX")
os.environ.setdefault("DITA_V2_HAZELCAST", "REAL")
os.environ.setdefault("DITA_V2_MODE", "DEBUG")
os.environ.setdefault("DITA_V2_VERBOSITY", "TRACE")
os.environ.setdefault("DITA_V2_PREFIX", "pink")
os.environ.setdefault("DOLPHIN_BINGX_ENV", "VST")
os.environ.setdefault("DOLPHIN_BINGX_ALLOW_MAINNET", "0")
def _apply_pink_namespace_env() -> None:
os.environ["DOLPHIN_STRATEGY_NAME"] = PINK_DEFAULTS["strategy_name"]
os.environ["DOLPHIN_STATE_MAP"] = PINK_DEFAULTS["state_map"]
os.environ["DOLPHIN_PNL_MAP"] = PINK_DEFAULTS["pnl_map"]
os.environ["DOLPHIN_JOURNAL_STRATEGY"] = PINK_DEFAULTS["journal_strategy"]
os.environ["DOLPHIN_JOURNAL_DB"] = PINK_DEFAULTS["journal_db"]
os.environ["DOLPHIN_FIXED_TP_PCT"] = f'{PINK_DEFAULTS["fixed_tp_pct"]:.4f}'
os.environ["DOLPHIN_BINGX_ENV"] = "VST"
os.environ["DOLPHIN_BINGX_ALLOW_MAINNET"] = "0"
def _apply_pink_env() -> None:
_set_ditav2_env_defaults()
_apply_pink_namespace_env()
def _apply_pink_actor_overrides(actor_cfg: dict[str, Any]) -> dict[str, Any]:
cfg: dict[str, Any] = deepcopy(actor_cfg) if actor_cfg else {}
cfg["strategy_name"] = PINK_DEFAULTS["strategy_name"]
hz = cfg.setdefault("hazelcast", {})
hz["state_map"] = PINK_DEFAULTS["state_map"]
hz["imap_pnl"] = PINK_DEFAULTS["pnl_map"]
hz["state_map_aliases"] = []
hz["imap_pnl_aliases"] = []
adaptive_exit = cfg.setdefault("adaptive_exit", {})
adaptive_exit["shadow_db"] = PINK_DEFAULTS["journal_db"]
cfg["v7_journal_db"] = PINK_DEFAULTS["journal_db"]
cfg["sync_bar_idx_from_blue"] = False
vol_p60_threshold = _resolve_pink_vol_p60_threshold()
cfg["vol_p60_threshold"] = vol_p60_threshold
cfg.setdefault("paper_trade", {})["vol_p60"] = vol_p60_threshold
cfg.setdefault("engine", {})["fixed_tp_pct"] = float(PINK_DEFAULTS["fixed_tp_pct"])
return cfg
class BinanceDataClientConfig: # pragma: no cover - compatibility shim
"""Local placeholder so legacy tests can patch the symbol without Nautilus imports."""
class TradingNode: # pragma: no cover - compatibility shim
"""Local placeholder so legacy tests can patch the symbol without Nautilus imports."""
def build_actor_config(
*,
data_venue: str | None = None,
exec_venue: str | None = None,
) -> dict[str, Any]:
"""Build the minimal actor config needed by the direct PINK launcher."""
return _apply_pink_actor_overrides(
{
"strategy_name": PINK_DEFAULTS["strategy_name"],
"hazelcast": {
"state_map": PINK_DEFAULTS["state_map"],
"imap_pnl": PINK_DEFAULTS["pnl_map"],
"state_map_aliases": [],
"imap_pnl_aliases": [],
},
"adaptive_exit": {"shadow_db": PINK_DEFAULTS["journal_db"]},
"paper_trade": {"vol_p60": _resolve_pink_vol_p60_threshold()},
"engine": {"fixed_tp_pct": PINK_DEFAULTS["fixed_tp_pct"]},
"data_venue": (data_venue or "BINANCE").upper(),
"exec_venue": (exec_venue or "BINGX").upper(),
"v7_journal_db": PINK_DEFAULTS["journal_db"],
"sync_bar_idx_from_blue": False,
}
)
def build_bingx_exec_client_config(**_: Any) -> BingxExecClientConfig:
"""Return the direct BingX client config shared by the DITAv2 bundle."""
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=_resolve_bingx_environment(),
allow_mainnet=_resolve_bingx_allow_mainnet(),
recv_window_ms=_resolve_bingx_recv_window_ms(),
default_leverage=int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")),
exchange_leverage_cap=_resolve_bingx_exchange_leverage_cap(),
prefer_websocket=False,
sizing_mode=os.environ.get("DOLPHIN_BINGX_SIZING_MODE", "testnet"),
journal_strategy="pink",
journal_db="dolphin_pink",
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
def build_pink_node(
*,
data_venue: str | None = None,
exec_venue: str | None = None,
trader_id: str | None = None,
) -> dict[str, Any]:
"""Compatibility shim for legacy tests/tools expecting a node-style builder."""
resolved_bingx_env = _resolve_bingx_environment()
resolved_bingx_allow_mainnet = _resolve_bingx_allow_mainnet()
if resolved_bingx_env is BingxEnvironment.LIVE and not resolved_bingx_allow_mainnet:
raise RuntimeError("BingX LIVE requested but DOLPHIN_BINGX_ALLOW_MAINNET is not enabled")
actor_cfg = build_actor_config(
data_venue=(data_venue or "BINANCE"),
exec_venue=(exec_venue or "BINGX"),
)
actor_cfg = _apply_pink_actor_overrides(actor_cfg)
actor_cfg["trader_id"] = trader_id or PINK_DEFAULTS["trader_id"]
actor_cfg["bingx_environment"] = str(resolved_bingx_env.value)
return {"actor_cfg": actor_cfg}
def _build_data_feed() -> HazelcastDataFeed:
return HazelcastDataFeed(
{
"hazelcast": {
"cluster": os.environ.get("HZ_CLUSTER", "dolphin"),
"host": os.environ.get("HZ_HOST", "localhost:5701"),
}
}
)
def _build_runtime(*, phase: PinkPhase) -> PinkDirectRuntime:
data_feed = _build_data_feed()
market_state_runtime = MarketStateRuntime()
# Decision and intent policy — unchanged from BLUE semantics.
cfg = DecisionConfig(
vel_div_threshold=-0.02,
vel_div_extreme=-0.05,
fixed_tp_pct=float(os.environ.get("DOLPHIN_FIXED_TP_PCT", "0.0020")),
max_hold_bars=int(os.environ.get("DOLPHIN_MAX_HOLD_BARS", "250")),
capital_fraction=0.20,
max_leverage=3.0,
allow_short=True,
allow_long=False,
policy_version="pink_ditav2_v1",
exit_leg_ratios=_resolve_pink_exit_leg_ratios(phase),
)
decision = DecisionEngine(cfg)
intent = IntentEngine(cfg)
# DITAv2 execution bundle: kernel + venue + control + Zinc + projection.
bundle = build_launcher_bundle(
venue_mode="BINGX",
max_slots=1,
bingx_config=build_bingx_exec_client_config(),
)
kernel = bundle.kernel
# Persistence reads from the kernel's AccountProjection (single authority).
persistence = PinkClickHousePersistence(kernel.account)
return PinkDirectRuntime(
data_feed=data_feed,
kernel=kernel,
decision_engine=decision,
intent_engine=intent,
persistence=persistence,
market_state_runtime=market_state_runtime,
)
async def run() -> None:
_apply_pink_env()
phase = _resolve_pink_phase()
os.environ["DOLPHIN_PINK_PHASE"] = phase.value
runtime = _build_runtime(phase=phase)
symbol = str(os.environ.get("DOLPHIN_PINK_SNAPSHOT_SYMBOL", "BTCUSDT")).strip().upper()
poll_interval = float(os.environ.get("DOLPHIN_PINK_POLL_INTERVAL_SEC", "1.0"))
one_shot = _env_bool("DOLPHIN_PINK_ONE_SHOT", False)
account_sync_interval = _resolve_pink_account_sync_interval_sec()
initial_capital = float(os.environ.get("DOLPHIN_INITIAL_CAPITAL", "25000.0"))
await runtime.connect(initial_capital=initial_capital)
heartbeat_client = None
heartbeat_map = None
heartbeat_stop = asyncio.Event()
heartbeat_task = None
try:
import hazelcast
heartbeat_client = hazelcast.HazelcastClient(
cluster_name=os.environ.get("HZ_CLUSTER", "dolphin"),
cluster_members=[os.environ.get("HZ_HOST", "localhost:5701")],
)
heartbeat_map = heartbeat_client.get_map("DOLPHIN_HEARTBEAT").blocking()
async def _heartbeat_loop() -> None:
while not heartbeat_stop.is_set():
try:
write_runner_heartbeat(
heartbeat_map,
build_runner_heartbeat_payload(
flow="pink_ditav2_runtime",
phase=phase.value,
run_date=str(datetime.utcnow().date()),
runner="pink",
),
)
except Exception:
pass
try:
await asyncio.wait_for(heartbeat_stop.wait(), timeout=10.0)
except asyncio.TimeoutError:
continue
heartbeat_task = asyncio.create_task(_heartbeat_loop())
initial_snapshot = await runtime.data_feed.get_latest_snapshot(symbol)
await runtime.recover_account(
snapshot=initial_snapshot,
phase="startup_reconcile",
event_type="ACCOUNT_RECONCILE",
)
last_account_sync = asyncio.get_running_loop().time()
while True:
snapshot = await runtime.data_feed.get_latest_snapshot(symbol)
loop_now = asyncio.get_running_loop().time()
if account_sync_interval > 0 and loop_now - last_account_sync >= account_sync_interval:
await runtime.reconcile_account(snapshot)
last_account_sync = loop_now
if phase is not PinkPhase.BOOTSTRAP and snapshot is not None:
await runtime.step(snapshot)
if one_shot:
break
await asyncio.sleep(poll_interval)
finally:
heartbeat_stop.set()
if heartbeat_task is not None:
heartbeat_task.cancel()
with contextlib.suppress(BaseException):
await heartbeat_task
if heartbeat_client is not None:
heartbeat_client.shutdown()
await runtime.disconnect()
if __name__ == "__main__":
asyncio.run(run())

File diff suppressed because it is too large Load Diff

View File

@@ -1,658 +0,0 @@
"""DOLPHIN Paper Trading — Prefect Flow.
Runs daily at 00:05 UTC. Processes yesterday's live scan data through
the NDAlphaEngine champion stack. Logs virtual P&L to disk + Hazelcast.
Blue deployment: champion SHORT (configs/blue.yml)
Green deployment: bidirectional SHORT+LONG (configs/green.yml) [pending LONG validation]
Usage:
# Register flows (run once, after Prefect server is up):
PREFECT_API_URL=http://localhost:4200/api python paper_trade_flow.py --register
# Run manually for a specific date:
PREFECT_API_URL=http://localhost:4200/api python paper_trade_flow.py \\
--date 2026-02-25 --config configs/blue.yml
"""
import sys, json, yaml, logging, argparse, csv, urllib.request, os
from pathlib import Path
from datetime import datetime, timedelta, date, timezone
import numpy as np
import pandas as pd
HCM_DIR = Path(__file__).parent.parent
sys.path.insert(0, str(HCM_DIR / 'nautilus_dolphin'))
from prefect import flow, task, get_run_logger
from prefect.schedules import Cron
import hazelcast
logging.basicConfig(level=logging.WARNING) # suppress Prefect noise below WARNING
# ── Paths ───────────────────────────────────────────────────────────────────────
from dolphin_paths import get_eigenvalues_path, get_klines_dir
SCANS_DIR = get_eigenvalues_path() # platform-aware: Win → NG3 dir, Linux → /mnt/ng6_data/eigenvalues
KLINES_DIR = get_klines_dir() # vbt_cache_klines/ — NG5 parquet source (preferred)
MC_MODELS_DIR = str(HCM_DIR / 'nautilus_dolphin' / 'mc_results' / 'models')
# Columns that are eigenvalue metadata, not asset prices
META_COLS = {
'timestamp', 'scan_number',
'v50_lambda_max_velocity', 'v150_lambda_max_velocity',
'v300_lambda_max_velocity', 'v750_lambda_max_velocity',
'vel_div', 'instability_50', 'instability_150',
}
HZ_HOST = "localhost:5701"
HZ_CLUSTER = "dolphin"
# Number of historical eigenvalue dates to use for ACB w750 threshold calibration
ACB_HISTORY_DAYS = 60
# ── Helpers ──────────────────────────────────────────────────────────────────────
def _get_recent_scan_dates(n: int) -> list:
"""Return sorted list of up to n most-recent eigenvalue date dirs."""
try:
dirs = sorted(
d.name for d in SCANS_DIR.iterdir()
if d.is_dir() and len(d.name) == 10 and d.name.startswith('20')
)
return dirs[-n:]
except Exception:
return []
def _fetch_btcusdt_klines_fallback(date_str: str) -> "dict[str, float]":
"""Fetch BTCUSDT 1m klines from Binance futures for date_str.
Returns a dict mapping ISO timestamp strings (minute precision) → close price.
Falls back to empty dict on any error (caller handles missing prices gracefully).
"""
try:
from datetime import timezone as tz
day_start = datetime.strptime(date_str, "%Y-%m-%d").replace(tzinfo=tz.utc)
day_end = day_start + timedelta(days=1)
start_ms = int(day_start.timestamp() * 1000)
end_ms = int(day_end.timestamp() * 1000)
prices: dict[str, float] = {}
# Binance returns max 1500 bars per request; 1440 bars/day fits in one call
url = (
f"https://fapi.binance.com/fapi/v1/klines"
f"?symbol=BTCUSDT&interval=1m"
f"&startTime={start_ms}&endTime={end_ms}&limit=1500"
)
with urllib.request.urlopen(url, timeout=10) as resp:
bars = json.loads(resp.read())
for bar in bars:
ts_ms = int(bar[0])
close = float(bar[4])
ts_iso = datetime.fromtimestamp(ts_ms / 1000, tz=tz.utc).strftime("%Y-%m-%dT%H:%M")
prices[ts_iso] = close
return prices
except Exception:
return {}
def _load_scan_df_from_json(date_str: str) -> pd.DataFrame:
"""Load daily scan JSON files → DataFrame with vel_div + asset prices.
Each scan JSON has:
- windows: {50, 150, 300, 750} → tracking_data.lambda_max_velocity
- pricing_data.current_prices → per-asset USD prices
Returns one row per scan, sorted by scan_number.
When current_prices is empty (scanner bug), falls back to Binance 1m klines.
"""
scan_dir = SCANS_DIR / date_str
if not scan_dir.exists():
return pd.DataFrame()
json_files = sorted(scan_dir.glob("scan_*.json"), key=lambda f: f.name)
# Exclude the _Indicators.json companion files if any
json_files = [f for f in json_files if '__Indicators' not in f.name]
if not json_files:
return pd.DataFrame()
total = len(json_files)
rows = []
for i, jf in enumerate(json_files):
if i % 500 == 0 and i > 0:
print(f" [scan load] {i}/{total} files...", flush=True)
try:
with open(jf, 'r', encoding='utf-8') as fh:
data = json.load(fh)
row = {
'scan_number': data.get('scan_number', 0),
'timestamp': data.get('timestamp', ''),
}
# Eigenvalue velocity per window
windows = data.get('windows', {})
for w_key, vel_col, inst_col in [
('50', 'v50_lambda_max_velocity', 'instability_50'),
('150', 'v150_lambda_max_velocity', 'instability_150'),
('300', 'v300_lambda_max_velocity', None),
('750', 'v750_lambda_max_velocity', None),
]:
w_data = windows.get(str(w_key), {})
td = w_data.get('tracking_data', {})
vel = td.get('lambda_max_velocity')
row[vel_col] = float(vel) if vel is not None else np.nan
if inst_col is not None:
rs = w_data.get('regime_signals', {})
row[inst_col] = float(rs.get('instability_score', 0.0) or 0.0)
# vel_div = w50_vel - w750_vel (canonical v2_gold_fix_v50-v750 formula)
v50 = row.get('v50_lambda_max_velocity', np.nan)
v750 = row.get('v750_lambda_max_velocity', np.nan)
row['vel_div'] = (v50 - v750) if (np.isfinite(v50) and np.isfinite(v750)) else np.nan
# Asset prices
pricing = data.get('pricing_data', {})
prices = pricing.get('current_prices', {})
row.update({sym: float(px) for sym, px in prices.items() if px is not None})
rows.append(row)
except Exception:
continue
if not rows:
return pd.DataFrame()
df = pd.DataFrame(rows).sort_values('scan_number').reset_index(drop=True)
# Fallback: if BTCUSDT prices are missing (scanner current_prices bug),
# fetch 1m klines from Binance and fill by timestamp.
if 'BTCUSDT' not in df.columns or df['BTCUSDT'].isna().all():
btc_klines = _fetch_btcusdt_klines_fallback(date_str)
if btc_klines:
def _lookup_btc(ts_str: str) -> float:
# ts_str is ISO format from scan JSON; match to minute precision
ts_min = str(ts_str)[:16] # "YYYY-MM-DDTHH:MM"
return btc_klines.get(ts_min, np.nan)
df['BTCUSDT'] = df['timestamp'].apply(_lookup_btc)
# Forward-fill any gaps (scan timestamps between 1m bars)
df['BTCUSDT'] = df['BTCUSDT'].ffill().bfill()
return df
def _load_scan_df_from_parquet(date_str: str) -> pd.DataFrame:
"""Load daily scan data from vbt_cache_klines/ parquet (NG5+ preferred path).
Returns DataFrame with vel_div + asset prices, or empty DataFrame if not available.
"""
parq_path = KLINES_DIR / f"{date_str}.parquet"
if not parq_path.exists():
return pd.DataFrame()
try:
df = pd.read_parquet(parq_path)
if df.empty or 'vel_div' not in df.columns:
return pd.DataFrame()
return df.reset_index(drop=True)
except Exception:
return pd.DataFrame()
# ── Tasks ───────────────────────────────────────────────────────────────────────
@task(name="load_config", retries=0)
def load_config(config_path: str) -> dict:
with open(config_path) as f:
return yaml.safe_load(f)
@task(name="load_day_scans", retries=2, retry_delay_seconds=10)
def load_day_scans(date_str: str) -> pd.DataFrame:
"""Load scan data for one date → VBT-compatible DataFrame.
Prefers vbt_cache_klines/ parquet (NG5 native, single file, fast).
Falls back to eigenvalues/ JSON (NG3 legacy) if parquet unavailable.
Extracts vel_div, eigenvalue features, and asset prices.
"""
log = get_run_logger()
# ── Parquet path (NG5 native — preferred) ──────────────────────────────────
df = _load_scan_df_from_parquet(date_str)
if not df.empty:
log.info(f" [parquet] Loaded {len(df)} scans for {date_str} | cols={len(df.columns)}")
else:
# ── JSON fallback (NG3 legacy) ─────────────────────────────────────────
df = _load_scan_df_from_json(date_str)
if df.empty:
log.warning(f"No usable scan data for {date_str} in {SCANS_DIR}")
return pd.DataFrame()
log.info(f" [json] Loaded {len(df)} scans for {date_str} | cols={len(df.columns)}")
if df.empty:
log.warning(f"No usable scan data for {date_str} in {SCANS_DIR}")
return pd.DataFrame()
# Drop rows with NaN vel_div (warmup period at start of day)
valid = df['vel_div'].notna()
n_dropped = (~valid).sum()
df = df[valid].reset_index(drop=True)
# Verify BTCUSDT prices present (required for vol gate and DC)
if 'BTCUSDT' not in df.columns:
log.error(f"BTCUSDT prices missing from scan data on {date_str} — cannot run engine")
return pd.DataFrame()
log.info(f" Loaded {len(df)} scans for {date_str} | cols={len(df.columns)} | "
f"vel_div range=[{df['vel_div'].min():.4f}, {df['vel_div'].max():.4f}] "
f"| {n_dropped} warmup rows dropped")
return df
@task(name="run_engine_day", retries=0, persist_result=False)
def run_engine_day(date_str: str, df: pd.DataFrame, engine, vol_p60: float, posture: str = 'APEX', direction: int = -1) -> dict:
"""Run one day through NDAlphaEngine. Returns daily stats dict."""
log = get_run_logger()
if df.empty or posture == 'HIBERNATE':
log.warning(f"Empty DataFrame or HIBERNATE for {date_str} — skipping.")
return {'date': date_str, 'pnl': 0.0, 'capital': engine.capital,
'trades': 0, 'boost': 1.0, 'beta': 0.0, 'mc_status': 'NO_DATA', 'posture': posture}
asset_cols = [c for c in df.columns if c not in META_COLS]
# Vol gate: rolling 50-bar std of BTC returns
bp = df['BTCUSDT'].values
dvol = np.full(len(df), np.nan)
for i in range(50, len(bp)):
seg = bp[max(0, i - 50):i]
if len(seg) >= 10 and seg[0] > 0:
dvol[i] = float(np.std(np.diff(seg) / seg[:-1]))
vol_ok = np.where(np.isfinite(dvol), dvol > vol_p60, False)
# 1. Setup day
engine.begin_day(date_str, posture=posture, direction=direction)
# 2. Bar stream (replaces batch process_day)
for ri in range(len(df)):
row = df.iloc[ri]
vd = row.get('vel_div')
if vd is None or not np.isfinite(float(vd)):
engine._global_bar_idx += 1
continue
v50_raw = row.get('v50_lambda_max_velocity')
v750_raw = row.get('v750_lambda_max_velocity')
v50_val = float(v50_raw) if (v50_raw is not None and np.isfinite(float(v50_raw))) else 0.0
v750_val = float(v750_raw) if (v750_raw is not None and np.isfinite(float(v750_raw))) else 0.0
prices = {}
for ac in asset_cols:
p = row.get(ac)
if p is not None and p > 0 and np.isfinite(p):
prices[ac] = float(p)
if not prices:
engine._global_bar_idx += 1
continue
# OB live step: fetch HZ snapshots and compute features BEFORE step_bar().
# This populates the live caches so get_placement/get_signal/get_market()
# return real OBF values instead of NEUTRAL defaults.
if engine.ob_engine is not None:
try:
engine.ob_engine.step_live(list(prices.keys()), ri)
except Exception:
pass # OBF degraded → NEUTRAL values, trading continues
engine.step_bar(
bar_idx=ri,
vel_div=float(vd),
prices=prices,
vol_regime_ok=bool(vol_ok[ri]),
v50_vel=v50_val,
v750_vel=v750_val,
)
# 3. Finalize day
result = engine.end_day()
result['posture'] = posture
log.info(f" {date_str}: PnL={result.get('pnl', 0):+.2f} "
f"T={result.get('trades', 0)} boost={result.get('boost', 1.0):.2f}x "
f"MC={result.get('mc_status', '?')} Posture={posture}")
return result
@task(name="write_hz_state", retries=3, retry_delay_seconds=5, persist_result=False)
def write_hz_state(hz_host: str, hz_cluster: str, imap_name: str, key: str, value: dict):
"""Write state dict to Hazelcast IMap. Creates own client per call (serialization-safe)."""
client = hazelcast.HazelcastClient(cluster_name=hz_cluster, cluster_members=[hz_host])
try:
client.get_map(imap_name).blocking().put(key, json.dumps(value))
finally:
client.shutdown()
@task(name="log_pnl", retries=0, persist_result=False)
def log_pnl(log_dir: Path, date_str: str, result: dict, capital: float):
log_dir.mkdir(parents=True, exist_ok=True)
row = {**result, 'date': date_str, 'capital': capital,
'logged_at': datetime.now(timezone.utc).isoformat()}
log_file = log_dir / f"paper_pnl_{date_str[:7]}.jsonl"
with open(log_file, 'a') as f:
f.write(json.dumps(row) + '\n')
# ── Flow ────────────────────────────────────────────────────────────────────────
@flow(name="dolphin-paper-trade", log_prints=True)
def dolphin_paper_trade_flow(config_path: str = "configs/blue.yml",
run_date: str = None,
instrument: bool = False):
"""Daily paper trading flow. Processes one day of live eigenvalue scans.
Scheduled at 00:05 UTC — processes yesterday's data.
Run manually with run_date='YYYY-MM-DD' for backtesting or debugging.
"""
log = get_run_logger()
cfg = load_config(config_path)
strategy_name = cfg['strategy_name']
eng_cfg = cfg['engine']
pt_cfg = cfg['paper_trade']
hz_cfg = cfg['hazelcast']
dir_str = os.environ.get('DOLPHIN_DIRECTION', cfg.get('direction', 'short_only'))
direction_val = 1 if str(dir_str).strip().lower() in ['long', 'long_only', 'buy', '+1', '1'] else -1
target_date = run_date or (date.today() - timedelta(days=1)).isoformat()
log.info(f"=== {strategy_name.upper()} paper trade: {target_date} ===")
# ── Lazy imports (numba JIT happens here) ──────────────────────────────────
from nautilus_dolphin.nautilus.proxy_boost_engine import create_d_liq_engine
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
from mc.mc_ml import DolphinForewarner
from nautilus_dolphin.nautilus.ob_features import OBFeatureEngine
from nautilus_dolphin.nautilus.hz_ob_provider import HZOBProvider
client = hazelcast.HazelcastClient(cluster_name=HZ_CLUSTER, cluster_members=[HZ_HOST])
imap_state = client.get_map(hz_cfg['imap_state']).blocking()
# ---- Restore capital ----
STATE_KEY = f"state_{strategy_name}_{target_date}"
restored_capital = pt_cfg['initial_capital']
peak_capital = pt_cfg['initial_capital']
stored_state = {}
engine_state = None
try:
raw = imap_state.get(STATE_KEY) or imap_state.get('latest') or '{}'
stored_state = json.loads(raw)
if stored_state.get('strategy') == strategy_name and stored_state.get('capital', 0) > 0:
restored_capital = float(stored_state['capital'])
peak_capital = float(stored_state.get('peak_capital', restored_capital))
engine_state = stored_state.get('engine_state')
log.info(f"[STATE] Restored capital={restored_capital:.2f} from HZ")
except Exception as e:
log.warning(f"[STATE] HZ restore failed: {e} — using config capital")
# ── Engine — D_LIQ_GOLD config (8x/9x LiquidationGuardEngine) ───────────
# create_d_liq_engine() overrides max_leverage→8.0 / abs_max_leverage→9.0
# internally (D_LIQ_SOFT_CAP / D_LIQ_ABS_CAP constants), regardless of what
# eng_cfg says. This is the certified gold leverage stack.
engine = create_d_liq_engine(
initial_capital = restored_capital,
vel_div_threshold = eng_cfg['vel_div_threshold'],
vel_div_extreme = eng_cfg['vel_div_extreme'],
min_leverage = eng_cfg['min_leverage'],
max_leverage = eng_cfg.get('max_leverage', 8.0),
abs_max_leverage = eng_cfg.get('abs_max_leverage', 9.0),
leverage_convexity = eng_cfg['leverage_convexity'],
fraction = eng_cfg['fraction'],
fixed_tp_pct = eng_cfg['fixed_tp_pct'],
stop_pct = eng_cfg['stop_pct'],
max_hold_bars = eng_cfg['max_hold_bars'],
use_direction_confirm= eng_cfg['use_direction_confirm'],
dc_lookback_bars = eng_cfg['dc_lookback_bars'],
dc_min_magnitude_bps = eng_cfg['dc_min_magnitude_bps'],
dc_skip_contradicts = eng_cfg['dc_skip_contradicts'],
dc_leverage_boost = eng_cfg['dc_leverage_boost'],
dc_leverage_reduce = eng_cfg['dc_leverage_reduce'],
use_asset_selection = eng_cfg['use_asset_selection'],
min_irp_alignment = eng_cfg['min_irp_alignment'],
use_sp_fees = eng_cfg['use_sp_fees'],
use_sp_slippage = eng_cfg['use_sp_slippage'],
sp_maker_entry_rate = eng_cfg['sp_maker_entry_rate'],
sp_maker_exit_rate = eng_cfg['sp_maker_exit_rate'],
use_ob_edge = eng_cfg['use_ob_edge'],
ob_edge_bps = eng_cfg['ob_edge_bps'],
ob_confirm_rate = eng_cfg['ob_confirm_rate'],
lookback = eng_cfg['lookback'],
use_alpha_layers = eng_cfg['use_alpha_layers'],
use_dynamic_leverage = eng_cfg['use_dynamic_leverage'],
seed = eng_cfg.get('seed', 42),
)
engine.set_esoteric_hazard_multiplier(0.0) # gold spec: hazard=0 → base_max_leverage=8.0
if engine_state:
try:
engine.restore_state(engine_state)
log.info("[STATE] Restored full engine state (including open positions)")
except Exception as e:
log.error(f"[STATE] Failed to restore engine state: {e}")
# ── ACB — preload w750 from recent history for valid p60 threshold ─────────
# w750 calibration always uses NPZ history (threshold is a population statistic).
# Daily ExF factors are sourced from HZ exf_latest (pre-lagged) when available;
# fall back to NPZ disk scan if HZ data is absent or stale (>12 h).
acb = AdaptiveCircuitBreaker()
acb.config.EIGENVALUES_PATH = SCANS_DIR # CRITICAL: override Windows default for Linux
recent_dates = _get_recent_scan_dates(ACB_HISTORY_DAYS)
if target_date not in recent_dates:
recent_dates = (recent_dates + [target_date])[-ACB_HISTORY_DAYS:]
acb.preload_w750(recent_dates)
log.info(f" ACB preloaded {len(recent_dates)} dates | w750_threshold={acb._w750_threshold:.6f}")
# ── ACB HZ warm-up: pre-load today's boost from live exf_latest ────────────
# The ExF service applies per-indicator lag BEFORE pushing to HZ, so the
# values in exf_latest are already delay-adjusted. Do NOT re-lag them.
_acb_hz_ok = False
try:
features_map = client.get_map('DOLPHIN_FEATURES').blocking()
exf_raw = features_map.get('exf_latest')
if exf_raw:
exf_snapshot = json.loads(exf_raw)
# Live w750 from latest scan (may be absent during warmup)
scan_raw = features_map.get('latest_eigen_scan')
w750_live: float | None = None
if scan_raw:
scan_data = json.loads(scan_raw)
w750_live = scan_data.get('w750_velocity')
boost_info = acb.get_dynamic_boost_from_hz(
target_date, exf_snapshot, w750_velocity=w750_live, direction=direction_val
)
stale_s = boost_info.get('max_staleness_s', 0)
log.info(
f" ACB HZ: boost={boost_info['boost']:.4f} beta={boost_info['beta']:.2f} "
f"signals={boost_info['signals']:.1f} staleness={stale_s:.0f}s"
)
_acb_hz_ok = True
else:
log.warning(" ACB HZ: exf_latest not found — falling back to NPZ disk scan")
except ValueError as _ve:
log.warning(f" ACB HZ: snapshot stale ({_ve}) — falling back to NPZ disk scan")
except Exception as _e:
log.warning(f" ACB HZ: read failed ({_e}) — falling back to NPZ disk scan")
if not _acb_hz_ok:
# NPZ fallback: get_dynamic_boost_for_date() will read eigenvalues/ on demand
log.info(f" ACB: using NPZ disk path for {target_date}")
# ── Data Loading & Live OB Integration ────────────────────────────────────
df = load_day_scans(target_date)
OB_ASSETS = [c for c in df.columns if c not in META_COLS] if not df.empty else ["BTCUSDT", "ETHUSDT", "BNBUSDT", "SOLUSDT"]
live_ob = HZOBProvider(hz_cluster=HZ_CLUSTER, hz_host=HZ_HOST)
ob_eng = OBFeatureEngine(live_ob)
# NOTE: HZOBProvider has no historical snapshots, so preload_date() is a no-op.
# OB features are fetched live via ob_eng.step_live() before each step_bar() call.
# ── MC-Forewarner ──────────────────────────────────────────────────────────
forewarner = DolphinForewarner(models_dir=MC_MODELS_DIR)
mc_base_cfg = {
'trial_id': 0, 'vel_div_threshold': -0.020, 'vel_div_extreme': -0.050,
'use_direction_confirm': True, 'dc_lookback_bars': 7,
'dc_min_magnitude_bps': 0.75, 'dc_skip_contradicts': True,
'dc_leverage_boost': 1.00, 'dc_leverage_reduce': 0.50,
'vd_trend_lookback': 10, 'min_leverage': 0.50, 'max_leverage': 5.00,
'leverage_convexity': 3.00, 'fraction': 0.20,
'use_alpha_layers': True, 'use_dynamic_leverage': True,
'fixed_tp_pct': 0.0095, 'stop_pct': 1.00, 'max_hold_bars': 120,
'use_sp_fees': True, 'use_sp_slippage': True,
'sp_maker_entry_rate': 0.62, 'sp_maker_exit_rate': 0.50,
'use_ob_edge': True, 'ob_edge_bps': 5.00, 'ob_confirm_rate': 0.40,
'ob_imbalance_bias': -0.09, 'ob_depth_scale': 1.00,
'use_asset_selection': True, 'min_irp_alignment': 0.45, 'lookback': 100,
'acb_beta_high': 0.80, 'acb_beta_low': 0.20, 'acb_w750_threshold_pct': 60,
}
engine.set_ob_engine(ob_eng)
engine.set_acb(acb)
engine.set_mc_forewarner(forewarner, mc_base_cfg)
engine.set_esoteric_hazard_multiplier(0.0)
if instrument:
engine._bar_log_enabled = True
# vol_p60: 60th percentile of rolling 50-bar BTC return std
# Calibrated from 55-day NG3 champion window (Dec31–Feb25).
# TODO: compute adaptively from rolling scan history (Phase MIG3)
vol_p60 = pt_cfg.get('vol_p60', 0.000099)
# ── Run ────────────────────────────────────────────────────────────────────
# df was already loaded above to define OB_ASSETS
# ── DOLPHIN_SAFETY (MIG3) ──────────────────────────────────────────────────
try:
safety_ref = client.get_cp_subsystem().get_atomic_reference('DOLPHIN_SAFETY').blocking()
safety_raw = safety_ref.get()
except Exception:
safety_ref = client.get_map('DOLPHIN_SAFETY').blocking()
safety_raw = safety_ref.get('latest')
safety_state = json.loads(safety_raw) if safety_raw else {}
posture = safety_state.get('posture', 'APEX')
Rm = safety_state.get('Rm', 1.0)
log.info(f"[SURVIVAL STACK] Posture={posture} | Rm={Rm:.3f}")
# Apply Rm to absolute max leverage
effective_max_lev = engine.abs_max_leverage * Rm
engine.abs_max_leverage = max(1.0, effective_max_lev)
if posture == 'STALKER':
engine.abs_max_leverage = min(engine.abs_max_leverage, 2.0)
result = run_engine_day(target_date, df, engine, vol_p60, posture=posture, direction=direction_val)
result['strategy'] = strategy_name
result['capital'] = engine.capital
# ── Hazelcast & State Persistence ──────────────────────────────────────────
try:
# PnL write
imap_pnl = client.get_map(hz_cfg['imap_pnl']).blocking()
imap_pnl.put(target_date, json.dumps(result))
# State persist write
new_peak = max(engine.capital, peak_capital)
new_drawdown = 1.0 - (engine.capital / new_peak) if new_peak > 0 else 0.0
new_state = {
'strategy': strategy_name,
'capital': engine.capital,
'date': target_date,
'pnl': result.get('pnl', 0.0),
'trades': result.get('trades', 0),
'peak_capital': new_peak,
'drawdown': new_drawdown,
'last_date': target_date,
'updated_at': datetime.now(timezone.utc).isoformat(),
'engine_state': engine.get_state(),
}
imap_state.put('latest', json.dumps(new_state))
imap_state.put(STATE_KEY, json.dumps(new_state))
log.info(f" HZ write OK → state & {hz_cfg['imap_pnl']}[{target_date}]")
except Exception as e:
log.error(f"[STATE] HZ persist failed: {e}")
# Fallback: write to local JSON ledger
ledger_dir = Path(__file__).parent / pt_cfg.get('log_dir', 'paper_logs')
ledger_dir.mkdir(parents=True, exist_ok=True)
ledger_path = ledger_dir / f"state_ledger_{strategy_name}.jsonl"
with open(ledger_path, 'a') as f:
f.write(json.dumps({
'strategy': strategy_name, 'capital': engine.capital,
'date': target_date, 'pnl': result.get('pnl', 0.0),
'trades': result.get('trades', 0), 'peak_capital': peak_capital,
'drawdown': 1.0 - engine.capital / max(engine.capital, peak_capital) if max(engine.capital, peak_capital) > 0 else 0.0
}) + '\n')
finally:
live_ob.close()
client.shutdown()
# ── Instrumentation (--instrument flag) ────────────────────────────────────
if instrument:
instr_dir = Path(__file__).parent / pt_cfg['log_dir']
instr_dir.mkdir(parents=True, exist_ok=True)
trades_instr_path = instr_dir / f"E2E_trades_{target_date}.csv"
with open(trades_instr_path, 'w', newline='') as f:
cw = csv.writer(f)
cw.writerow(['trade_id', 'asset', 'direction', 'entry_price', 'exit_price',
'entry_bar', 'exit_bar', 'bars_held', 'leverage', 'notional',
'pnl_pct', 'pnl_absolute', 'exit_reason', 'bucket_idx'])
for t in engine.trade_history:
cw.writerow([t.trade_id, t.asset, t.direction,
f"{t.entry_price:.6f}", f"{t.exit_price:.6f}",
t.entry_bar, t.exit_bar, t.bars_held,
f"{t.leverage:.4f}", f"{t.notional:.4f}",
f"{t.pnl_pct:.8f}", f"{t.pnl_absolute:.4f}",
t.exit_reason, t.bucket_idx])
bars_instr_path = instr_dir / f"E2E_bars_{target_date}.csv"
with open(bars_instr_path, 'w', newline='') as f:
cw = csv.writer(f)
cw.writerow(['date', 'bar_idx', 'vel_div', 'vol_ok', 'posture',
'regime_size_mult', 'position_open', 'boost', 'beta'])
for b in engine._bar_log:
cw.writerow([target_date, b['bar_idx'], f"{b['vel_div']:.8f}",
b['vol_ok'], b['posture'], f"{b['regime_size_mult']:.6f}",
b['position_open'], f"{b['boost']:.4f}", f"{b['beta']:.2f}"])
log.info(f" Instrumentation → {trades_instr_path.name} ({len(engine.trade_history)} trades), "
f"{bars_instr_path.name} ({len(engine._bar_log)} bars)")
# ── Disk log ───────────────────────────────────────────────────────────────
log_pnl(Path(__file__).parent / pt_cfg['log_dir'], target_date, result, engine.capital)
log.info(f"=== DONE: {strategy_name} {target_date} | "
f"PnL={result.get('pnl', 0):+.2f} | Capital={engine.capital:,.2f} ===")
return result
# ── CLI entry point ──────────────────────────────────────────────────────────────
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DOLPHIN paper trading flow')
parser.add_argument('--config', default='configs/blue.yml', help='Strategy config YAML')
parser.add_argument('--date', default=None, help='YYYY-MM-DD (default: yesterday)')
parser.add_argument('--register', action='store_true', help='Register Prefect deployments')
parser.add_argument('--instrument', action='store_true', help='Write per-trade + bar CSVs to log_dir')
args = parser.parse_args()
if args.register:
from prefect.client.schemas.schedules import CronSchedule as CS
for color, cfg_path in [('blue', 'configs/blue.yml'), ('green', 'configs/green.yml')]:
abs_cfg = str(Path(__file__).parent / cfg_path)
deployment = dolphin_paper_trade_flow.to_deployment(
name=f"dolphin-paper-{color}",
parameters={"config_path": abs_cfg},
schedule=CS(cron="5 0 * * *", timezone="UTC"),
work_pool_name="dolphin",
tags=[color, "paper-trade", "dolphin"],
)
deployment.apply()
print(f"Registered: dolphin-paper-{color}")
else:
os.environ.setdefault('PREFECT_API_URL', 'http://localhost:4200/api')
dolphin_paper_trade_flow(config_path=args.config, run_date=args.date,
instrument=args.instrument)

View File

@@ -1,107 +0,0 @@
"""L2 — LIMIT order payload wiring in BingxDirectExecutionAdapter.submit_intent.
The venue adapter forwards _order_type/_limit_price in the intent metadata; the
backend must place a LIMIT order (type=LIMIT + price + GTC) when asked, and keep
MARKET as the default. Offline unit test of payload construction — the signed_post
client is stubbed to capture the order payload; no exchange contact.
"""
from __future__ import annotations
import asyncio
from datetime import datetime, timezone
from types import SimpleNamespace
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
from prod.clean_arch.dita import DecisionAction, Intent, TradeSide
def _adapter(captured: dict):
a = BingxDirectExecutionAdapter.__new__(BingxDirectExecutionAdapter)
a._config = SimpleNamespace(recv_window_ms=5000, default_leverage=1, exchange_leverage_cap=3)
a._client_order_run_id = "test"
a._entry_client_order_seq = 0
a._exit_client_order_seq = 0
a._state = SimpleNamespace(open_positions={}, account={})
async def _signed_post(path, params):
if path.endswith("/trade/order"):
captured["order"] = dict(params)
return {"orderId": "1", "status": "NEW"}
a._client = SimpleNamespace(signed_post=_signed_post)
a._instrument_venue_symbol = lambda asset: "BTC-USDT"
a._format_quantity = lambda asset, q: f"{float(q)}"
a._format_price = lambda asset, p: f"{float(p)}"
async def _refresh(asset, include_history=True):
return a._state
a._refresh_exchange_state = _refresh
return a
def _intent(metadata: dict) -> Intent:
return Intent(
timestamp=datetime.now(timezone.utc), trade_id="T1", decision_id="D1",
asset="BTCUSDT", action=DecisionAction.ENTER, side=TradeSide.SHORT,
reason="TEST", target_size=0.01, leverage=2.0, reference_price=100.0,
confidence=0.5, exit_leg_ratios=(1.0,), metadata=metadata,
)
def test_limit_intent_places_limit_order():
captured: dict = {}
asyncio.run(_adapter(captured).submit_intent(_intent({"_order_type": "LIMIT", "_limit_price": 95.0})))
o = captured["order"]
assert o["type"] == "LIMIT", o
assert "price" in o and float(o["price"]) == 95.0, o
assert o.get("timeInForce") == "GTC", o
def test_market_intent_places_market_order():
captured: dict = {}
asyncio.run(_adapter(captured).submit_intent(_intent({})))
o = captured["order"]
assert o["type"] == "MARKET", o
assert "price" not in o, o
def test_limit_without_valid_price_falls_back_to_market():
captured: dict = {}
asyncio.run(_adapter(captured).submit_intent(_intent({"_order_type": "LIMIT", "_limit_price": 0.0})))
assert captured["order"]["type"] == "MARKET", captured["order"]
# --- cancel: truth-based confirmation (trust exchange state over the response) ---
def _cancel_adapter(*, open_after: list):
a = BingxDirectExecutionAdapter.__new__(BingxDirectExecutionAdapter)
a._config = SimpleNamespace(recv_window_ms=5000)
a._instrument_venue_symbol = lambda asset: "TRX-USDT"
async def _signed_delete(path, params):
# Simulate BingX returning a transient error even when the order is removed.
return {"status": "REJECTED", "msg": "order not exist"}
async def _signed_get(path, params):
return {"data": {"orders": open_after}}
a._client = SimpleNamespace(signed_delete=_signed_delete, signed_get=_signed_get)
return a
def _order(oid="2060963645141028864"):
return SimpleNamespace(venue_order_id=oid, venue_client_id="T:i", metadata={"asset": "TRXUSDT"})
def test_cancel_succeeds_when_order_gone_despite_error_response():
a = _cancel_adapter(open_after=[]) # order no longer open
resp = asyncio.run(a.cancel(_order()))
assert resp["status"] == "CANCELED", resp
def test_cancel_rejected_when_order_still_open():
a = _cancel_adapter(open_after=[{"orderId": "2060963645141028864", "status": "PENDING"}])
resp = asyncio.run(a.cancel(_order()))
assert resp["status"] != "CANCELED", resp

View File

@@ -1,559 +0,0 @@
"""
test_bingx_nautilus_execution.py
================================
End-to-end tests for the Nautilus execution path:
engine.step_bar() -> _exec_submit_entry() -> cache.instrument() -> order_factory -> submit_order
Tests cover:
1. Instrument registration from exec client into Nautilus cache
2. _exec_submit_entry returns early when instrument missing
3. _exec_submit_entry succeeds when instrument is in cache
4. Data-venue fallback when exec-venue instrument not available
5. Full order payload correctness (tags, side, quantity precision)
6. Venue symbol mapping uses raw_symbol from cached instrument
7. _venue_symbol fallback when instrument not in cache
8. Integration: build_actor_config with split venues
"""
from __future__ import annotations
import asyncio
import math
import sys
from decimal import Decimal
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "nautilus_dolphin"))
from nautilus_trader.model.enums import OrderSide, OrderType
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.objects import Currency, Price, Quantity
from prod.bingx.enums import BINGX_VENUE, BingxEnvironment
from prod.bingx.execution import BingxExecutionClient
from prod.bingx.sandbox_status import build_sandbox_status
from prod.bingx.sandbox_status import write_sandbox_status
# -- Helpers -------------------------------------------------------------------
def _make_bingx_instrument(symbol: str = "BTCUSDT"):
return SimpleNamespace(
id=InstrumentId.from_str(f"{symbol}.BINGX"),
instrument_id=InstrumentId.from_str(f"{symbol}.BINGX"),
symbol=SimpleNamespace(value=symbol),
raw_symbol=SimpleNamespace(value=f"{symbol[:-4]}-USDT"),
base_currency=Currency.from_str(symbol[:3]),
quote_currency=Currency.from_str("USDT"),
size_precision=3,
price_precision=2,
maker_fee=Decimal("0.0002"),
taker_fee=Decimal("0.0005"),
)
def _make_binance_instrument(symbol: str = "BTCUSDT"):
return SimpleNamespace(
id=InstrumentId.from_str(f"{symbol}.BINANCE"),
instrument_id=InstrumentId.from_str(f"{symbol}.BINANCE"),
symbol=SimpleNamespace(value=symbol),
raw_symbol=SimpleNamespace(value=symbol),
base_currency=Currency.from_str(symbol[:3]),
quote_currency=Currency.from_str("USDT"),
size_precision=3,
price_precision=2,
maker_fee=Decimal("0.0002"),
taker_fee=Decimal("0.0005"),
)
class FakeCache:
def __init__(self, instruments=None):
self._instruments = dict(instruments or {})
def instrument(self, instrument_id):
return self._instruments.get(instrument_id)
def add_instrument(self, instrument):
self._instruments[instrument.id] = instrument
def instruments(self):
return list(self._instruments.values())
def positions(self, venue=None):
return []
def order(self, client_order_id):
return None
def add_currency(self, currency):
pass
class FakeProvider:
def __init__(self, instruments=None):
self._instruments = list(instruments or [])
def list_all(self):
return self._instruments
def currencies(self):
return {}
async def initialize(self):
pass
async def _noop(*args, **kwargs):
pass
async def _persist_sandbox_status(client, *, environment: str = "VST", notes: dict[str, Any] | None = None):
balance = await client.signed_get("/openApi/swap/v2/user/balance")
positions = await client.signed_get("/openApi/swap/v2/user/positions")
open_orders = await client.signed_get("/openApi/swap/v2/trade/openOrders")
status = build_sandbox_status(
balance_payload=balance,
positions_payload=positions,
open_orders_payload=open_orders,
environment=environment,
notes=notes or {},
)
write_sandbox_status(status)
return status
def _make_connect_stub(cache, provider):
return SimpleNamespace(
_cache=cache,
_provider=provider,
_config=SimpleNamespace(prefer_websocket=False),
_log=SimpleNamespace(info=lambda *a, **kw: None, warning=lambda *a, **kw: None),
_start_pollers=lambda: None,
_refresh_account_state=_noop,
_restore_journal_snapshot=_noop,
_persist_journal_snapshot=_noop,
_await_account_registered=_noop,
)
def _make_actor_stub(cache, exec_venue="BINGX", data_venue="BINANCE"):
log_messages = []
class Log:
def info(self, msg, *a, **kw):
log_messages.append(("info", msg))
def warning(self, msg, *a, **kw):
log_messages.append(("warning", msg))
def error(self, msg, *a, **kw):
log_messages.append(("error", msg))
def debug(self, msg, *a, **kw):
log_messages.append(("debug", msg))
return SimpleNamespace(
cache=cache,
log=Log(),
_log_messages=log_messages,
dolphin_config={
"engine": {"max_account_leverage": 2.0},
"paper_trade": {"initial_capital": 25000.0},
},
engine=SimpleNamespace(capital=100000.0),
_last_portfolio_capital=100000.0,
_exec_venue_name=lambda: exec_venue,
_data_venue_name=lambda: data_venue,
_exec_open_positions={},
order_factory=SimpleNamespace(
market=lambda **kw: SimpleNamespace(
instrument_id=kw.get("instrument_id"),
order_side=kw.get("order_side"),
quantity=kw.get("quantity"),
tags=kw.get("tags", []),
client_order_id=SimpleNamespace(value="test-coid"),
order_type=OrderType.MARKET,
strategy_id=SimpleNamespace(value="test-strat"),
)
),
submit_order=MagicMock(),
clock=SimpleNamespace(timestamp_ns=lambda: 1000),
)
# -- Test: Instrument Registration --------------------------------------------
class TestExecClientRegistersInstrumentsInCache:
def test_instruments_registered_after_connect(self):
inst1 = _make_bingx_instrument("BTCUSDT")
inst2 = _make_bingx_instrument("ETHUSDT")
cache = FakeCache()
provider = FakeProvider([inst1, inst2])
stub = _make_connect_stub(cache, provider)
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(BingxExecutionClient._connect(stub))
finally:
loop.close()
assert cache.instrument(InstrumentId.from_str("BTCUSDT.BINGX")) is inst1
assert cache.instrument(InstrumentId.from_str("ETHUSDT.BINGX")) is inst2
def test_empty_provider_no_crash(self):
cache = FakeCache()
provider = FakeProvider([])
stub = _make_connect_stub(cache, provider)
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(BingxExecutionClient._connect(stub))
finally:
loop.close()
assert len(list(cache.instruments())) == 0
# -- Test: _exec_submit_entry instrument lookup ------------------------------
class TestExecSubmitEntryInstrumentLookup:
def test_returns_early_when_no_exec_instrument_no_data_fallback(self):
cache = FakeCache()
stub = _make_actor_stub(cache)
entry = {"asset": "XLMUSDT", "direction": -1, "notional": 5000.0, "entry_price": 0.10, "trade_id": "t1", "leverage": 1.0}
prices = {"XLMUSDT": 0.10}
result = DolphinActor._exec_submit_entry(stub, entry, prices)
assert result is None
assert not stub.submit_order.called
error_msgs = [m for lvl, m in stub._log_messages if lvl == "error"]
assert any("not in cache" in m for m in error_msgs)
def test_succeeds_with_exec_instrument_in_cache(self):
inst = _make_bingx_instrument("XLMUSDT")
cache = FakeCache({inst.id: inst})
stub = _make_actor_stub(cache)
entry = {"asset": "XLMUSDT", "direction": -1, "notional": 5000.0, "entry_price": 0.10, "trade_id": "t1", "leverage": 1.0}
prices = {"XLMUSDT": 0.10}
DolphinActor._exec_submit_entry(stub, entry, prices)
assert stub.submit_order.called
order = stub.submit_order.call_args[0][0]
assert str(order.instrument_id) == "XLMUSDT.BINGX"
assert order.order_side == OrderSide.SELL
assert order.tags[0] == "type:entry"
assert "direction:SHORT" in order.tags
def test_data_venue_fallback_with_warning(self):
binance_inst = _make_binance_instrument("XLMUSDT")
cache = FakeCache({binance_inst.id: binance_inst})
stub = _make_actor_stub(cache, exec_venue="BINGX", data_venue="BINANCE")
entry = {"asset": "XLMUSDT", "direction": -1, "notional": 5000.0, "entry_price": 0.10, "trade_id": "t1", "leverage": 1.0}
prices = {"XLMUSDT": 0.10}
DolphinActor._exec_submit_entry(stub, entry, prices)
assert stub.submit_order.called
warn_msgs = [m for lvl, m in stub._log_messages if lvl == "warning"]
assert any("borrowing metadata" in m for m in warn_msgs)
def test_info_log_on_successful_entry(self):
inst = _make_bingx_instrument("XLMUSDT")
cache = FakeCache({inst.id: inst})
stub = _make_actor_stub(cache)
entry = {"asset": "XLMUSDT", "direction": -1, "notional": 5000.0, "entry_price": 0.10, "trade_id": "t42", "leverage": 1.5}
prices = {"XLMUSDT": 0.10}
DolphinActor._exec_submit_entry(stub, entry, prices)
info_msgs = [m for lvl, m in stub._log_messages if lvl == "info"]
assert any("[EXEC] ENTRY SHORT" in m and "XLMUSDT" in m for m in info_msgs)
def test_quantity_uses_instrument_size_precision(self):
inst = _make_bingx_instrument("BTCUSDT")
inst.size_precision = 4
cache = FakeCache({inst.id: inst})
stub = _make_actor_stub(cache)
entry = {"asset": "BTCUSDT", "direction": 1, "notional": 50000.0, "entry_price": 100000.0, "trade_id": "t1", "leverage": 1.0}
prices = {"BTCUSDT": 100000.0}
DolphinActor._exec_submit_entry(stub, entry, prices)
order = stub.submit_order.call_args[0][0]
assert order.quantity.precision == 4
# -- Test: _venue_symbol mapping ---------------------------------------------
class TestVenueSymbolMapping:
def test_uses_raw_symbol_when_instrument_in_cache(self):
inst = _make_bingx_instrument("TRXUSDT")
cache = FakeCache({inst.id: inst})
stub = SimpleNamespace(_cache=cache)
result = BingxExecutionClient._venue_symbol(stub, InstrumentId.from_str("TRXUSDT.BINGX"))
assert result == "TRX-USDT"
def test_fallback_converts_usdt_suffix(self):
stub = SimpleNamespace(_cache=FakeCache())
result = BingxExecutionClient._venue_symbol(stub, InstrumentId.from_str("XLMUSDT.BINGX"))
assert result == "XLM-USDT"
def test_fallback_passes_through_hyphenated(self):
stub = SimpleNamespace(_cache=FakeCache())
result = BingxExecutionClient._venue_symbol(stub, InstrumentId.from_str("BTC-USDT.BINGX"))
assert result == "BTC-USDT"
# -- Test: _map_submit_order -------------------------------------------------
class TestMapSubmitOrderForMarketOrder:
def test_market_sell_with_tags(self):
inst = _make_bingx_instrument("ETHUSDT")
cache = FakeCache({inst.id: inst})
order = SimpleNamespace(
instrument_id=InstrumentId.from_str("ETHUSDT.BINGX"),
side=OrderSide.SELL,
order_type=OrderType.MARKET,
quantity=Quantity.from_str("1.500"),
client_order_id=SimpleNamespace(value="test-cid-001"),
is_post_only=False,
is_reduce_only=False,
has_price=False,
has_trigger_price=False,
price=None,
trigger_price=None,
time_in_force=None,
tags=["type:entry", "direction:SHORT", "cm:2.50", "tid:t99"],
)
adapter = SimpleNamespace(
_cache=cache,
_config=SimpleNamespace(use_reduce_only=True, recv_window_ms=5000),
_venue_symbol=lambda iid: BingxExecutionClient._venue_symbol(adapter, iid),
_format_quantity=lambda q: str(q),
_format_price=lambda p: str(p),
_map_order_type=BingxExecutionClient._map_order_type,
_map_time_in_force=BingxExecutionClient._map_time_in_force,
)
# Rebind _venue_symbol after adapter exists so self-referencing works
adapter._venue_symbol = lambda iid: BingxExecutionClient._venue_symbol(adapter, iid)
payload = BingxExecutionClient._map_submit_order(adapter, order)
assert payload["symbol"] == "ETH-USDT"
assert payload["side"] == "SELL"
assert payload["type"] == "MARKET"
assert payload["quantity"] == "1.500"
assert payload["clientOrderId"] == "test-cid-001"
# -- Test: Order tag parsing for leverage ------------------------------------
class TestLeverageTagParsing:
def test_extracts_lev_tag(self):
order = SimpleNamespace(tags=["type:entry", "lev:2.50", "cm:2.50", "tid:t1"])
assert BingxExecutionClient._parse_leverage_from_tags(order) == 2.50
def test_extracts_cm_tag(self):
order = SimpleNamespace(tags=["type:entry", "cm:3.00", "tid:t1"])
assert BingxExecutionClient._parse_leverage_from_tags(order) == 3.00
def test_returns_none_no_tags(self):
order = SimpleNamespace(tags=[])
assert BingxExecutionClient._parse_leverage_from_tags(order) is None
def test_returns_none_no_leverage_tags(self):
order = SimpleNamespace(tags=["type:entry", "direction:SHORT"])
assert BingxExecutionClient._parse_leverage_from_tags(order) is None
# -- Test: Split venue configuration -----------------------------------------
class TestSplitVenueConfig:
def test_split_venues_preserved(self):
from prod.launch_dolphin_live import build_actor_config
cfg = build_actor_config(data_venue="BINANCE", exec_venue="BINGX")
assert cfg["data_venue"] == "BINANCE"
assert cfg["exec_venue"] == "BINGX"
assert cfg["venue"] == "BINGX"
# -- Import DolphinActor -----------------------------------------------------
try:
from nautilus_dolphin.nautilus.dolphin_actor import DolphinActor
HAS_DOLPHIN_ACTOR = True
except ImportError:
HAS_DOLPHIN_ACTOR = False
@pytest.mark.skipif(not HAS_DOLPHIN_ACTOR, reason="DolphinActor not importable")
class TestDolphinActorExecSubmitEntry:
def _actor_stub(self, cache):
return _make_actor_stub(cache)
def test_full_entry_flow_short_order(self):
inst = _make_bingx_instrument("SOLUSDT")
cache = FakeCache({inst.id: inst})
stub = self._actor_stub(cache)
entry = {
"asset": "SOLUSDT", "direction": -1, "notional": 3000.0,
"entry_price": 150.0, "trade_id": "trade-sol-001",
"leverage": 2.0, "vel_div": -0.035,
}
prices = {"SOLUSDT": 150.0}
DolphinActor._exec_submit_entry(stub, entry, prices)
assert stub.submit_order.called
order = stub.submit_order.call_args[0][0]
assert "direction:SHORT" in order.tags
assert "cm:2.00" in order.tags
assert "lev:2.00" in order.tags
assert "tid:trade-sol-001" in order.tags
def test_full_entry_flow_long_order(self):
inst = _make_bingx_instrument("ADAUSDT")
cache = FakeCache({inst.id: inst})
stub = self._actor_stub(cache)
entry = {
"asset": "ADAUSDT", "direction": 1, "notional": 2000.0,
"entry_price": 0.45, "trade_id": "trade-ada-002",
"leverage": 1.0, "vel_div": -0.025,
}
prices = {"ADAUSDT": 0.45}
DolphinActor._exec_submit_entry(stub, entry, prices)
assert stub.submit_order.called
order = stub.submit_order.call_args[0][0]
assert order.order_side == OrderSide.BUY
assert "direction:LONG" in order.tags
def test_caps_notional_when_near_capacity_limit(self):
inst = _make_bingx_instrument("BTCUSDT")
cache = FakeCache({inst.id: inst})
stub = self._actor_stub(cache)
stub.engine = SimpleNamespace(capital=10.0)
stub.dolphin_config["engine"]["max_account_leverage"] = 0.01
entry = {
"asset": "BTCUSDT", "direction": -1, "notional": 5000.0,
"entry_price": 100000.0, "trade_id": "t1", "leverage": 1.0,
}
prices = {"BTCUSDT": 100000.0}
DolphinActor._exec_submit_entry(stub, entry, prices)
assert stub.submit_order.called
warn_msgs = [m for lvl, m in stub._log_messages if lvl == "warning"]
assert any("capped by portfolio exposure" in m for m in warn_msgs)
def test_skips_when_notional_zero(self):
inst = _make_bingx_instrument("BTCUSDT")
cache = FakeCache({inst.id: inst})
stub = self._actor_stub(cache)
entry = {
"asset": "BTCUSDT", "direction": -1, "notional": 0.0,
"entry_price": 100000.0, "trade_id": "t1", "leverage": 1.0,
}
prices = {"BTCUSDT": 100000.0}
result = DolphinActor._exec_submit_entry(stub, entry, prices)
assert result is None
assert not stub.submit_order.called
# -- Live integration (requires BingX VST credentials) -----------------------
@pytest.mark.skipif(
not Path("/mnt/dolphinng5_predict/.env").exists(),
reason="No .env file (no BingX credentials)",
)
class TestLiveInstrumentProvider:
def test_loads_instruments_from_vst(self):
import os
from dotenv import load_dotenv
load_dotenv("/mnt/dolphinng5_predict/.env")
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.http import BingxHttpClient
from prod.bingx.instrument_provider import BingxInstrumentProvider, BingxInstrumentProviderConfig
async def _run():
cfg = BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY", ""),
secret_key=os.environ.get("BINGX_SECRET_KEY", ""),
environment=BingxEnvironment.VST,
)
client = BingxHttpClient(config=cfg)
provider = BingxInstrumentProvider(
client=client,
config=BingxInstrumentProviderConfig(load_all=True),
)
await provider.initialize()
instruments = provider.list_all()
await _persist_sandbox_status(client, notes={"test": "loads_instruments_from_vst"})
await client.close()
return instruments
instruments = asyncio.run(_run())
assert len(instruments) > 0
symbols = {i.symbol.value for i in instruments}
assert "BTCUSDT" in symbols
assert "ETHUSDT" in symbols
def test_trxusdt_instrument_has_correct_precision(self):
import os
from dotenv import load_dotenv
load_dotenv("/mnt/dolphinng5_predict/.env")
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.http import BingxHttpClient
from prod.bingx.instrument_provider import BingxInstrumentProvider, BingxInstrumentProviderConfig
async def _run():
cfg = BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY", ""),
secret_key=os.environ.get("BINGX_SECRET_KEY", ""),
environment=BingxEnvironment.VST,
)
client = BingxHttpClient(config=cfg)
provider = BingxInstrumentProvider(
client=client,
config=BingxInstrumentProviderConfig(load_all=True),
)
await provider.initialize()
inst = provider.find(InstrumentId.from_str("TRXUSDT.BINGX"))
await _persist_sandbox_status(client, notes={"test": "trxusdt_instrument_has_correct_precision"})
await client.close()
return inst
inst = asyncio.run(_run())
assert inst is not None
assert inst.size_precision >= 1
assert inst.price_precision >= 1
assert inst.raw_symbol.value == "TRX-USDT"

View File

@@ -1,71 +0,0 @@
from __future__ import annotations
from pathlib import Path
from prod.bingx.sandbox_status import build_sandbox_status
from prod.bingx.sandbox_status import load_sandbox_status
from prod.bingx.sandbox_status import write_sandbox_status
def test_build_sandbox_status_marks_clean_when_flat():
status = build_sandbox_status(
balance_payload={
"balance": {
"balance": "12000.5",
"equity": "12000.5",
"availableMargin": "12000.5",
"unrealizedProfit": "0",
"usedMargin": "0",
}
},
positions_payload=[],
open_orders_payload={"orders": []},
environment="VST",
)
assert status.clean is True
assert status.balance == 12000.5
assert status.equity == 12000.5
assert status.open_positions == 0
assert status.open_orders == 0
def test_build_sandbox_status_marks_dirty_when_positions_or_orders_exist():
status = build_sandbox_status(
balance_payload={
"balance": {
"balance": "12000.5",
"equity": "12500.5",
"availableMargin": "9000.5",
"unrealizedProfit": "500",
"usedMargin": "3000",
}
},
positions_payload=[{"symbol": "BTC-USDT"}, {"symbol": "ETH-USDT"}],
open_orders_payload={"orders": [{"symbol": "BTC-USDT"}]},
environment="VST",
)
assert status.clean is False
assert status.open_positions == 2
assert status.open_orders == 1
assert status.unrealized_profit == 500.0
def test_write_and_load_sandbox_status_round_trip(tmp_path: Path):
status = build_sandbox_status(
balance_payload={"balance": {"balance": "10", "equity": "11", "availableMargin": "9", "unrealizedProfit": "1", "usedMargin": "2"}},
positions_payload=[],
open_orders_payload=[],
environment="VST",
notes={"source": "unit-test"},
)
path = tmp_path / "bingx_sandbox_status.json"
write_sandbox_status(status, path)
loaded = load_sandbox_status(path)
assert loaded is not None
assert loaded["balance"] == 10.0
assert loaded["equity"] == 11.0
assert loaded["clean"] is True
assert loaded["notes"]["source"] == "unit-test"

View File

@@ -1,87 +0,0 @@
#!/usr/bin/env python3
"""Tests for capital restore source selection on startup."""
import json
import os
from datetime import datetime, timezone
from unittest.mock import patch
import pytest
from prod.nautilus_event_trader import DolphinLiveTrader
class _MapStub:
def __init__(self, payloads):
self._payloads = payloads
def blocking(self):
return self
def get(self, key):
return self._payloads.get(key)
def _build_trader() -> DolphinLiveTrader:
trader = DolphinLiveTrader()
trader._build_engine()
trader.eng.begin_day(datetime.now(timezone.utc).strftime("%Y-%m-%d"), posture="APEX")
return trader
def test_restore_prefers_fresher_engine_snapshot_over_stale_latest_nautilus():
trader = _build_trader()
trader.eng.capital = 25_000.0
trader.state_map = _MapStub(
{
"latest_nautilus": json.dumps(
{
"capital": 31_049.44,
"updated_at": "2026-05-13T10:52:40+00:00",
}
),
"engine_snapshot": json.dumps(
{
"capital": 33_150.07,
"timestamp": "2026-05-13T16:20:38+00:00",
}
),
}
)
trader.pnl_map = _MapStub({})
with patch.dict(os.environ, {"DOLPHIN_CAPITAL_SEED_STALE_LAG_SEC": "180"}, clear=False):
trader._restore_capital()
assert trader.eng.capital == pytest.approx(33_150.07, abs=0.01)
assert trader._restore_source == "HZ engine_snapshot"
def test_restore_can_force_latest_nautilus_override():
trader = _build_trader()
trader.eng.capital = 25_000.0
trader.state_map = _MapStub(
{
"latest_nautilus": json.dumps(
{
"capital": 31_049.44,
"updated_at": "2026-05-13T10:52:40+00:00",
}
),
"engine_snapshot": json.dumps(
{
"capital": 33_150.07,
"timestamp": "2026-05-13T16:20:38+00:00",
}
),
}
)
trader.pnl_map = _MapStub({})
with patch.dict(os.environ, {"DOLPHIN_FORCE_LATEST_NAUTILUS_RESTORE": "1"}, clear=False):
trader._restore_capital()
assert trader.eng.capital == pytest.approx(31_049.44, abs=0.01)
assert trader._restore_source == "HZ latest_nautilus"

View File

@@ -1,391 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any
import pytest
from prod.clean_arch.dita_v2 import (
BingxVenueAdapter,
ExecutionKernel,
InMemoryControlPlane,
KernelCommandType,
KernelControlSnapshot,
KernelIntent,
KernelMode,
KernelEventKind,
KernelVerbosity,
TradeSide,
TradeStage,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from prod.clean_arch.ports.execution import ExchangeStateSnapshot, ExecutionReceipt
def _norm_symbol(symbol: str) -> str:
return str(symbol or "").replace("-", "").replace("_", "").upper()
def _snapshot(
*,
capital: float = 25_000.0,
positions: list[dict[str, Any]] | None = None,
open_orders: list[dict[str, Any]] | None = None,
all_orders: list[dict[str, Any]] | None = None,
all_fills: list[dict[str, Any]] | None = None,
source: str = "bingx",
) -> ExchangeStateSnapshot:
position_map = {
_norm_symbol(str(row.get("symbol", ""))): dict(row)
for row in (positions or [])
if _norm_symbol(str(row.get("symbol", "")))
}
return ExchangeStateSnapshot(
timestamp=datetime.now(timezone.utc),
capital=capital,
equity=capital,
open_positions=position_map,
open_orders=[dict(row) for row in (open_orders or [])],
all_orders=[dict(row) for row in (all_orders or [])],
all_fills=[dict(row) for row in (all_fills or [])],
account={"balances": [{"asset": "USDT", "total": capital}]},
open_notional=0.0,
source=source,
recovered=False,
)
class FakeBingxBackend:
def __init__(
self,
*,
snapshots: list[ExchangeStateSnapshot],
receipt: ExecutionReceipt | None = None,
cancel_response: dict[str, Any] | None = None,
) -> None:
self.snapshots = snapshots
self.receipt = receipt
self.cancel_response = cancel_response or {"status": "CANCELED"}
self.calls: list[tuple[str, Any]] = []
self.submitted: list[Any] = []
self.canceled: list[tuple[Any, str]] = []
self._refresh_count = 0
self.connected = False
async def connect(self) -> bool:
self.connected = True
self.calls.append(("connect", None))
return True
async def disconnect(self) -> None:
self.connected = False
self.calls.append(("disconnect", None))
async def refresh_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
self.calls.append(("refresh_state", symbol, include_history))
index = min(self._refresh_count, len(self.snapshots) - 1)
snapshot = self.snapshots[index]
if self._refresh_count < len(self.snapshots) - 1:
self._refresh_count += 1
return snapshot
async def submit_intent(self, legacy_intent: Any) -> ExecutionReceipt:
self.calls.append(("submit_intent", legacy_intent.trade_id))
self.submitted.append(legacy_intent)
if self.receipt is None:
raise AssertionError("receipt must be configured")
return self.receipt
async def cancel_order(self, order: VenueOrder, *, reason: str = "") -> dict[str, Any]:
self.calls.append(("cancel_order", order.venue_order_id, reason))
self.canceled.append((order, reason))
return dict(self.cancel_response)
def _intent(
*,
action: KernelCommandType = KernelCommandType.ENTER,
trade_id: str = "trade-1",
slot_id: int = 0,
asset: str = "BTCUSDT",
side: TradeSide = TradeSide.SHORT,
target_size: float = 1.0,
leverage: float = 2.0,
reference_price: float = 75_000.0,
reason: str = "TEST",
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:{action.value}",
trade_id=trade_id,
slot_id=slot_id,
asset=asset,
side=side,
action=action,
reference_price=reference_price,
target_size=target_size,
leverage=leverage,
reason=reason,
)
def test_submit_maps_bingx_ack_and_snapshot_fill_to_ditav2_events() -> None:
ack_row = {
"orderId": "1001",
"clientOrderId": "cid-1",
"clientOrderID": "cid-1",
"symbol": "BTC-USDT",
"status": "NEW",
"executedQty": "0",
"cumFilledQty": "0",
}
fill_row = {
"clientOrderId": "cid-1",
"clientOrderID": "cid-1",
"orderId": "1001",
"symbol": "BTC-USDT",
"status": "FILLED",
"executedQty": "1",
"lastFilledQty": "1",
"lastFillPrice": "75000",
}
backend = FakeBingxBackend(
snapshots=[
_snapshot(),
_snapshot(
positions=[
{
"symbol": "BTC-USDT",
"positionSide": "SHORT",
"positionAmt": "-1",
"avgPrice": "75000",
"markPrice": "75010",
"leverage": "2",
}
],
open_orders=[ack_row],
all_orders=[ack_row],
all_fills=[fill_row],
),
],
receipt=ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="NEW",
symbol="BTC-USDT",
side="SELL",
action="ENTER",
quantity=1.0,
price=75_000.0,
client_order_id="cid-1",
order_id="1001",
raw_ack=ack_row,
raw_state={},
),
)
adapter = BingxVenueAdapter(backend=backend)
events = adapter.submit(_intent())
assert backend.connected is False
assert backend.submitted
assert [event.kind for event in events] == [event.kind for event in events if event.kind.value]
assert events[0].kind.value == "ORDER_ACK"
assert events[0].status == VenueEventStatus.ACKED
assert events[0].venue_client_id == "cid-1"
assert events[0].venue_order_id == "1001"
assert len(events) == 2
assert events[1].kind.value == "FULL_FILL"
assert events[1].status == VenueEventStatus.FILLED
assert events[1].filled_size == pytest.approx(1.0)
assert events[1].remaining_size == pytest.approx(0.0)
def test_cancel_uses_bingx_cancel_surface_and_maps_cancel_ack() -> None:
cancel_row = {
"orderId": "2001",
"clientOrderId": "cid-2",
"clientOrderID": "cid-2",
"symbol": "BTC-USDT",
"status": "CANCELED",
}
backend = FakeBingxBackend(
snapshots=[
_snapshot(
open_orders=[cancel_row],
all_orders=[cancel_row],
),
_snapshot(),
],
cancel_response=cancel_row,
)
adapter = BingxVenueAdapter(backend=backend)
order = VenueOrder(
internal_trade_id="trade-2",
venue_order_id="2001",
venue_client_id="cid-2",
side=TradeSide.SHORT,
intended_size=1.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": 0, "asset": "BTCUSDT"},
)
events = adapter.cancel(order, reason="MANUAL_CLOSE")
assert backend.canceled
assert events[0].kind.value == "CANCEL_ACK"
assert events[0].status == VenueEventStatus.CANCELED
assert events[0].venue_order_id == "2001"
assert events[0].reason == "MANUAL_CLOSE"
def test_reconcile_and_open_views_normalize_bingx_rows() -> None:
ack_row = {
"orderId": "3001",
"clientOrderId": "cid-3",
"clientOrderID": "cid-3",
"symbol": "ETH-USDT",
"status": "NEW",
"executedQty": "0",
}
fill_row = {
"clientOrderId": "cid-3",
"clientOrderID": "cid-3",
"orderId": "3001",
"symbol": "ETH-USDT",
"status": "PARTIALLY_FILLED",
"executedQty": "2",
"lastFilledQty": "1",
"lastFillPrice": "2500",
}
position_row = {
"symbol": "ETH-USDT",
"positionSide": "LONG",
"positionAmt": "2",
"avgPrice": "2500",
"markPrice": "2510",
"leverage": "3",
}
backend = FakeBingxBackend(
snapshots=[
_snapshot(
positions=[position_row],
open_orders=[ack_row],
all_orders=[ack_row, fill_row],
all_fills=[fill_row],
)
]
)
adapter = BingxVenueAdapter(backend=backend)
orders = adapter.open_orders()
positions = adapter.open_positions()
events = adapter.reconcile()
assert orders[0].status == VenueOrderStatus.NEW
assert orders[0].venue_client_id == "cid-3"
assert positions[0]["positionAmt"] == "2"
assert any(event.kind.value == "PARTIAL_FILL" for event in events)
assert any(event.kind.value == "ORDER_ACK" for event in events)
def test_kernel_can_drive_through_bingx_venue_shim() -> None:
ack_row = {
"orderId": "4001",
"clientOrderId": "cid-4",
"clientOrderID": "cid-4",
"symbol": "BTC-USDT",
"status": "NEW",
"executedQty": "0",
}
fill_row = {
"clientOrderId": "cid-4",
"clientOrderID": "cid-4",
"orderId": "4001",
"symbol": "BTC-USDT",
"status": "FILLED",
"executedQty": "1",
"lastFilledQty": "1",
"lastFillPrice": "75000",
}
backend = FakeBingxBackend(
snapshots=[
_snapshot(),
_snapshot(
positions=[
{
"symbol": "BTC-USDT",
"positionSide": "SHORT",
"positionAmt": "-1",
"avgPrice": "75000",
"markPrice": "75010",
"leverage": "2",
}
],
open_orders=[ack_row],
all_orders=[ack_row],
all_fills=[fill_row],
),
],
receipt=ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="NEW",
symbol="BTC-USDT",
side="SELL",
action="ENTER",
quantity=1.0,
price=75_000.0,
client_order_id="cid-4",
order_id="4001",
raw_ack=ack_row,
raw_state={},
),
)
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
),
venue=BingxVenueAdapter(backend=backend),
)
outcome = kernel.process_intent(_intent(trade_id="trade-4"))
slot = kernel.slot(0)
assert outcome.accepted is True
assert slot.fsm_state == TradeStage.POSITION_OPEN
assert slot.trade_id == "trade-4"
assert backend.submitted
def test_submit_maps_bingx_rate_limit_to_first_class_venue_event() -> None:
backend = FakeBingxBackend(
snapshots=[_snapshot(), _snapshot()],
receipt=ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="RATE_LIMITED",
symbol="BTC-USDT",
side="SELL",
action="ENTER",
quantity=1.0,
price=75_000.0,
client_order_id="cid-rate-limit",
order_id="",
raw_ack={
"status": "RATE_LIMITED",
"msg": "code:100410 endpoint is in disabled/frequency-limited period",
"retryAfter": int(datetime.now(timezone.utc).timestamp() * 1000) + 2_500,
},
raw_state={},
),
)
adapter = BingxVenueAdapter(backend=backend)
events = adapter.submit(_intent(trade_id="trade-rate-limit"))
assert len(events) == 1
assert events[0].kind == KernelEventKind.RATE_LIMITED
assert events[0].status == VenueEventStatus.RATE_LIMITED
assert events[0].venue_client_id == "cid-rate-limit"
assert events[0].metadata["retry_after_ms"] >= 0

View File

@@ -1,94 +0,0 @@
from __future__ import annotations
from uuid import uuid4
import os
import unittest
from prod.clean_arch.dita_v2 import (
BackendMode,
ControlUpdate,
InMemoryControlPlane,
ZincControlPlane,
KernelControlSnapshot,
KernelMode,
KernelVerbosity,
RealZincControlPlane,
build_control_plane,
)
from prod.clean_arch.dita_v2.real_control_plane import SharedRegion
HAS_REAL_ZINC = SharedRegion is not None
@unittest.skipUnless(HAS_REAL_ZINC, "Real Zinc adapter is unavailable")
class TestDITAv2RealControlPlane(unittest.TestCase):
def test_build_control_plane_defaults_to_zinc(self) -> None:
plane = build_control_plane()
self.assertIsInstance(plane, ZincControlPlane)
def test_roundtrip_update_and_read(self) -> None:
prefix = f"dita_v2_control_{uuid4().hex}"
writer = RealZincControlPlane(prefix=prefix, create=True)
reader = RealZincControlPlane(prefix=prefix, create=False)
try:
snapshot = writer.update(
ControlUpdate(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.BINGX,
trace_transitions=True,
mirror_to_hazelcast=True,
)
)
self.assertEqual(snapshot.mode, KernelMode.DEBUG)
self.assertEqual(snapshot.verbosity, KernelVerbosity.TRACE)
self.assertEqual(snapshot.backend_mode, BackendMode.BINGX)
self.assertTrue(snapshot.trace_transitions)
read_back = reader.read()
self.assertEqual(read_back.mode, KernelMode.DEBUG)
self.assertEqual(read_back.verbosity, KernelVerbosity.TRACE)
self.assertEqual(read_back.backend_mode, BackendMode.BINGX)
self.assertTrue(read_back.trace_transitions)
finally:
writer.close()
reader.close()
def test_env_can_select_real_control_plane(self) -> None:
prefix = f"dita_v2_control_{uuid4().hex}"
previous = os.environ.get("DITA_V2_CONTROL_PLANE")
os.environ["DITA_V2_CONTROL_PLANE"] = "REAL_ZINC"
try:
plane = build_control_plane(prefix=prefix)
self.assertIsInstance(plane, RealZincControlPlane)
if isinstance(plane, RealZincControlPlane):
plane.close()
finally:
if previous is None:
os.environ.pop("DITA_V2_CONTROL_PLANE", None)
else:
os.environ["DITA_V2_CONTROL_PLANE"] = previous
def test_initial_snapshot_is_default(self) -> None:
prefix = f"dita_v2_control_{uuid4().hex}"
plane = RealZincControlPlane(prefix=prefix, create=True)
try:
snapshot = plane.read()
self.assertEqual(snapshot, KernelControlSnapshot())
finally:
plane.close()
class TestDITAv2InMemoryControlPlane(unittest.TestCase):
def test_wait_and_notify(self) -> None:
plane = InMemoryControlPlane()
self.assertFalse(plane.wait(timeout_ms=1))
plane.notify()
self.assertTrue(plane.wait(timeout_ms=1))
snapshot = plane.update(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE))
self.assertEqual(snapshot.mode, KernelMode.DEBUG)
self.assertEqual(snapshot.verbosity, KernelVerbosity.TRACE)
if __name__ == "__main__":
unittest.main()

View File

@@ -1,37 +0,0 @@
from __future__ import annotations
from pathlib import Path
import unittest
class TestDITAv2Docs(unittest.TestCase):
def test_kernel_reference_exists(self) -> None:
text = Path("/mnt/dolphinng5_predict/prod/docs/DITA_V2_KERNEL_REFERENCE.md").read_text()
self.assertIn("# DITAv2 Kernel Reference", text)
self.assertIn("dolphin:dita_v2", text)
self.assertIn("prod/clean_arch/dita_v2/rust_backend.py", text)
self.assertIn("write-through", text)
self.assertIn("notify/wait", text)
self.assertIn("50 collected cases", text)
self.assertIn("full-stack E2E / functional tests", text)
self.assertIn("mocked exchange-first and BingX-basic E2E paths", text)
self.assertIn("KernelSeverity.WARNING", text)
self.assertIn("release_eta", text)
self.assertIn("retryable", text)
self.assertIn("dita_v2_live_bingx_smoke.py", text)
self.assertIn("--dry-run", text)
def test_system_bible_points_to_dita_v2_reference(self) -> None:
bible = Path("/mnt/dolphinng5_predict/prod/docs/SYSTEM_BIBLE_v7.md").read_text()
self.assertIn("DITA_V2_KERNEL_REFERENCE.md", bible)
self.assertIn("DITAv2 execution/launcher/operator surface", bible)
self.assertIn("write-through Zinc mirror semantics", bible)
self.assertIn("one-shot notify/wait signal contract", bible)
self.assertIn("full-stack DITAv2 E2E/functional matrix", bible)
self.assertIn("retryable transient throttling", bible)
self.assertIn("dita_v2_live_bingx_smoke.py", bible)
self.assertIn("--dry-run", bible)
if __name__ == "__main__":
unittest.main()

View File

@@ -1,907 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
import random
from typing import Any, Callable, Iterable, Optional, Sequence
import pytest
from prod.clean_arch.dita_v2 import (
BingxVenueAdapter,
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelVerbosity,
TradeSide,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from prod.clean_arch.ports.execution import ExchangeStateSnapshot, ExecutionReceipt
def _norm_symbol(symbol: str) -> str:
return str(symbol or "").replace("-", "").replace("_", "").upper()
def _snapshot(
*,
capital: float = 25_000.0,
positions: list[dict[str, Any]] | None = None,
open_orders: list[dict[str, Any]] | None = None,
all_orders: list[dict[str, Any]] | None = None,
all_fills: list[dict[str, Any]] | None = None,
source: str = "bingx",
recovered: bool = False,
) -> ExchangeStateSnapshot:
position_map = {
_norm_symbol(str(row.get("symbol", ""))): dict(row)
for row in (positions or [])
if _norm_symbol(str(row.get("symbol", "")))
}
return ExchangeStateSnapshot(
timestamp=datetime.now(timezone.utc),
capital=capital,
equity=capital,
open_positions=position_map,
open_orders=[dict(row) for row in (open_orders or [])],
all_orders=[dict(row) for row in (all_orders or [])],
all_fills=[dict(row) for row in (all_fills or [])],
account={"balances": [{"asset": "USDT", "total": capital}]},
open_notional=0.0,
source=source,
recovered=recovered,
)
def _sign(side: TradeSide) -> int:
return -1 if side == TradeSide.SHORT else 1
def _position_row(asset: str, side: TradeSide, qty: float, price: float) -> dict[str, Any]:
signed_qty = _sign(side) * abs(float(qty))
return {
"symbol": asset,
"positionSide": side.value,
"positionAmt": f"{signed_qty}",
"avgPrice": f"{price}",
"markPrice": f"{price}",
"leverage": "2",
}
@dataclass(frozen=True)
class VenueScriptStep:
name: str
submit_kind: str
fill_ratio: float = 0.0
cancel_kind: str = "cancel_ack"
submit_advances: bool = True
cancel_advances: bool = True
reject_reason: str = "MOCK_REJECT"
cancel_reason: str = "MOCK_CANCEL"
@dataclass(frozen=True)
class SignalAction:
kind: str
price: float
target_size: float = 0.0
fill_ratio: float = 1.0
reason: str = ""
require_close: bool = False
class ScriptedVenueAdapter:
"""Deterministic venue adapter that plays scripted submit/cancel outcomes."""
def __init__(self, steps: Sequence[VenueScriptStep]) -> None:
self.steps = list(steps)
self._step_index = 0
self._active_step_index = 0
self._order_seq = 1
self._event_seq = 1
self._open_orders: dict[str, VenueOrder] = {}
self._open_positions: dict[str, dict[str, Any]] = {}
self.calls: list[tuple[str, Any]] = []
def _next_step(self) -> VenueScriptStep:
if self._step_index < len(self.steps):
step = self.steps[self._step_index]
self._step_index += 1
return step
return VenueScriptStep(name="default", submit_kind="ack_only")
def submit(self, intent: KernelIntent) -> list[VenueEvent]:
self.calls.append(("submit", intent.action.value, intent.trade_id, intent.slot_id))
step = self._next_step()
self._active_step_index = max(0, self._step_index - 1)
order_id = f"MOCK-{self._order_seq:08d}"
self._order_seq += 1
client_id = f"{intent.trade_id}:{intent.intent_id}"
order = VenueOrder(
internal_trade_id=intent.trade_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=intent.side,
intended_size=float(intent.target_size),
filled_size=0.0,
average_fill_price=float(intent.reference_price or 0.0),
status=VenueOrderStatus.NEW,
metadata={"slot_id": intent.slot_id, "asset": intent.asset, "action": intent.action.value},
)
if step.submit_kind == "entry_reject":
return [
self._event(
intent=intent,
order=order,
kind=KernelEventKind.ORDER_REJECT,
status=VenueEventStatus.REJECTED,
reason=step.reject_reason,
)
]
ack = self._event(
intent=intent,
order=order,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
)
self._open_orders[order_id] = order
events = [ack]
if step.submit_kind in {"entry_partial", "exit_partial", "entry_full", "exit_full"}:
fill_ratio = max(0.0, min(1.0, float(step.fill_ratio or 0.0)))
if fill_ratio <= 0.0:
fill_ratio = 1.0 if step.submit_kind.endswith("full") else 0.5
fill_size = float(intent.target_size) * fill_ratio
fill_kind = KernelEventKind.FULL_FILL if fill_ratio >= 1.0 else KernelEventKind.PARTIAL_FILL
fill_status = VenueEventStatus.FILLED if fill_kind == KernelEventKind.FULL_FILL else VenueEventStatus.PARTIALLY_FILLED
events.append(
self._event(
intent=intent,
order=order,
kind=fill_kind,
status=fill_status,
price=float(intent.reference_price or 0.0),
filled_size=fill_size,
remaining_size=max(0.0, float(intent.target_size) - fill_size),
)
)
self._apply_fill(intent, fill_size, fill_kind == KernelEventKind.FULL_FILL)
if fill_kind == KernelEventKind.FULL_FILL:
self._open_orders.pop(order_id, None)
return events
def cancel(self, order: VenueOrder, *, reason: str = "") -> list[VenueEvent]:
self.calls.append(("cancel", order.venue_order_id, reason))
step = self.steps[min(self._active_step_index, len(self.steps) - 1)] if self.steps else VenueScriptStep(name="default", submit_kind="ack_only")
if step.cancel_kind == "cancel_reject":
return [
self._event(
intent=self._intent_from_order(order),
order=order,
kind=KernelEventKind.CANCEL_REJECT,
status=VenueEventStatus.CANCELED_REJECTED,
reason=step.cancel_reason,
)
]
self._open_orders.pop(order.venue_order_id, None)
if step.cancel_advances:
self._step_index = max(self._step_index, self._active_step_index + 1)
return [
self._event(
intent=self._intent_from_order(order),
order=order,
kind=KernelEventKind.CANCEL_ACK,
status=VenueEventStatus.CANCELED,
reason=reason or step.cancel_reason,
)
]
def open_orders(self) -> list[VenueOrder]:
return list(self._open_orders.values())
def open_positions(self) -> list[dict[str, Any]]:
return list(self._open_positions.values())
def reconcile(self) -> list[VenueEvent]:
events: list[VenueEvent] = []
for order in self._open_orders.values():
events.append(
self._event(
intent=self._intent_from_order(order),
order=order,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
reason="RECONCILE",
)
)
for row in self._open_positions.values():
events.append(
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{self._event_seq:08d}",
trade_id=str(row.get("trade_id", "")),
slot_id=int(row.get("slot_id", 0)),
kind=KernelEventKind.RECONCILE,
status=VenueEventStatus.ACKED,
venue_order_id=str(row.get("venue_order_id", "")),
venue_client_id=str(row.get("venue_client_id", "")),
side=TradeSide(str(row.get("side", TradeSide.FLAT.value))),
asset=str(row.get("symbol", "")),
price=float(row.get("avgPrice", 0.0)),
size=abs(float(row.get("positionAmt", 0.0))),
filled_size=abs(float(row.get("positionAmt", 0.0))),
remaining_size=0.0,
reason="RECONCILE",
raw_payload=dict(row),
metadata={"source": "mock"},
)
)
self._event_seq += 1
return events
def _event(
self,
*,
intent: KernelIntent,
order: VenueOrder,
kind: KernelEventKind,
status: VenueEventStatus,
price: float | None = None,
filled_size: float = 0.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{self._event_seq:08d}",
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=kind,
status=status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=intent.asset,
price=float(price if price is not None else intent.reference_price or 0.0),
size=float(intent.target_size or 0.0),
filled_size=float(filled_size),
remaining_size=float(remaining_size),
reason=reason,
raw_payload={
"status": status.value,
"orderId": order.venue_order_id,
"clientOrderId": order.venue_client_id,
"symbol": intent.asset,
"side": order.side.value,
"action": intent.action.value,
},
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
self._event_seq += 1
return event
def _intent_from_order(self, order: VenueOrder) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=order.venue_client_id,
trade_id=order.internal_trade_id,
slot_id=int(order.metadata.get("slot_id", 0)),
asset=str(order.metadata.get("asset", "")),
side=order.side,
action=KernelCommandType.EXIT if order.metadata.get("action") == "EXIT" else KernelCommandType.ENTER,
reference_price=float(order.average_fill_price or 0.0),
target_size=float(order.intended_size or 0.0),
leverage=2.0,
reason=str(order.metadata.get("action", "")),
)
def _apply_fill(self, intent: KernelIntent, filled_size: float, full: bool) -> None:
signed = _sign(intent.side) * abs(float(filled_size))
row = self._open_positions.get(intent.asset)
if intent.action == KernelCommandType.ENTER:
self._open_positions[intent.asset] = {
"symbol": intent.asset,
"trade_id": intent.trade_id,
"slot_id": intent.slot_id,
"side": intent.side.value,
"positionSide": intent.side.value,
"positionAmt": f"{signed}",
"avgPrice": f"{intent.reference_price}",
"markPrice": f"{intent.reference_price}",
"venue_order_id": f"MOCK-{self._order_seq - 1:08d}",
"venue_client_id": f"{intent.trade_id}:{intent.intent_id}",
}
return
if row is None:
return
current = abs(float(row.get("positionAmt", 0.0)))
new_qty = max(0.0, current - abs(float(filled_size)))
if new_qty <= 1e-12 or full:
self._open_positions.pop(intent.asset, None)
return
row["positionAmt"] = f"{_sign(intent.side) * new_qty}"
self._open_positions[intent.asset] = row
@dataclass(frozen=True)
class BingxE2EStep:
name: str
submit_kind: str
submit_fill_ratio: float
before_snapshot: ExchangeStateSnapshot
after_snapshot: ExchangeStateSnapshot
receipt: ExecutionReceipt
submit_advances: bool = True
cancel_kind: str = "cancel_ack"
cancel_advances: bool = True
cancel_before_snapshot: ExchangeStateSnapshot | None = None
cancel_after_snapshot: ExchangeStateSnapshot | None = None
class BingxE2EBackend:
"""Stateful fake backend that drives the real BingxVenueAdapter."""
def __init__(self, steps: Sequence[BingxE2EStep]) -> None:
self.steps = list(steps)
self.index = 0
self.calls: list[tuple[str, Any]] = []
self.connected = False
self._operation: str | None = None
self._active_index = 0
async def connect(self) -> bool:
self.connected = True
self.calls.append(("connect", None))
return True
async def disconnect(self) -> None:
self.connected = False
self.calls.append(("disconnect", None))
async def refresh_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
self.calls.append(("refresh_state", symbol, include_history, self.index, self._operation))
step = self.steps[min(self._active_index, len(self.steps) - 1)]
if self._operation == "submit":
snapshot = step.after_snapshot
if step.submit_advances:
self.index = min(self.index + 1, len(self.steps) - 1)
self._operation = None
return snapshot
if self._operation == "cancel":
snapshot = step.cancel_after_snapshot or step.after_snapshot
if step.cancel_advances:
self.index = min(self.index + 1, len(self.steps) - 1)
self._operation = None
return snapshot
return step.before_snapshot
async def submit_intent(self, legacy_intent: Any) -> ExecutionReceipt:
self.calls.append(("submit_intent", legacy_intent.trade_id, legacy_intent.action.value))
self._active_index = min(self.index, len(self.steps) - 1)
step = self.steps[self._active_index]
self._operation = "submit"
if step.submit_kind == "reject":
return ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="REJECTED",
symbol=legacy_intent.asset,
side=legacy_intent.side.value,
action=legacy_intent.action.value,
quantity=float(legacy_intent.target_size),
price=float(legacy_intent.reference_price),
client_order_id=step.receipt.client_order_id,
order_id=step.receipt.order_id,
raw_ack={"status": "REJECTED", "msg": "E2E_REJECT"},
raw_state={},
)
return step.receipt
async def cancel_order(self, order: VenueOrder, *, reason: str = "") -> dict[str, Any]:
self.calls.append(("cancel_order", order.venue_order_id, reason))
self._operation = "cancel"
step = self.steps[min(self._active_index, len(self.steps) - 1)]
if step.cancel_kind == "cancel_reject":
return {"status": "CANCEL_REJECTED", "msg": reason or "E2E_CANCEL_REJECT"}
return {"status": "CANCELED", "msg": reason or "E2E_CANCEL_ACK"}
def _kernel(venue: Any, *, zinc: Any | None = None) -> ExecutionKernel:
return ExecutionKernel(
max_slots=1,
control_plane=InMemoryControlPlane(
KernelControlSnapshot(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.MOCK if not isinstance(venue, BingxVenueAdapter) else BackendMode.BINGX,
trace_transitions=True,
debug_clickhouse_enabled=True,
mirror_to_hazelcast=True,
)
),
venue=venue,
zinc_plane=zinc or InMemoryZincPlane(),
)
def _intent(
*,
action: KernelCommandType,
trade_id: str,
side: TradeSide,
slot_id: int = 0,
target_size: float = 1.0,
price: float = 100.0,
exit_leg_ratios: Sequence[float] = (1.0,),
reason: str = "E2E",
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:{action.value}:{slot_id}:{reason}",
trade_id=trade_id,
slot_id=slot_id,
asset="BTCUSDT",
side=side,
action=action,
reference_price=price,
target_size=target_size,
leverage=2.0,
exit_leg_ratios=tuple(exit_leg_ratios),
reason=reason,
)
def _entry_event(trade_id: str, slot_id: int, side: TradeSide, target_size: float, price: float, *, partial: bool = False, ratio: float = 1.0) -> list[VenueEvent]:
order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"{trade_id}-entry-oid",
venue_client_id=f"{trade_id}:entry",
side=side,
intended_size=target_size,
filled_size=0.0,
average_fill_price=price,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot_id, "asset": "BTCUSDT", "action": "ENTER"},
)
events = [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"{trade_id}-ack",
trade_id=trade_id,
slot_id=slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=side,
asset="BTCUSDT",
price=price,
size=target_size,
filled_size=0.0,
remaining_size=target_size,
)
]
if partial:
fill_size = target_size * ratio
events.append(
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"{trade_id}-fill",
trade_id=trade_id,
slot_id=slot_id,
kind=KernelEventKind.PARTIAL_FILL,
status=VenueEventStatus.PARTIALLY_FILLED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=side,
asset="BTCUSDT",
price=price,
size=target_size,
filled_size=fill_size,
remaining_size=max(0.0, target_size - fill_size),
)
)
else:
events.append(
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"{trade_id}-fill",
trade_id=trade_id,
slot_id=slot_id,
kind=KernelEventKind.FULL_FILL,
status=VenueEventStatus.FILLED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=side,
asset="BTCUSDT",
price=price,
size=target_size,
filled_size=target_size,
remaining_size=0.0,
)
)
return events
def _close_and_mark(kernel: ExecutionKernel, *, trade_id: str, side: TradeSide, exit_size: float, price: float, reason: str) -> None:
kernel.process_intent(_intent(action=KernelCommandType.EXIT, trade_id=trade_id, side=side, target_size=exit_size, price=price, exit_leg_ratios=(0.5, 0.5), reason=reason))
def _assert_full_cycle(kernel: ExecutionKernel, *, side: TradeSide, trade_id: str, expect_closed: bool = True) -> None:
slot = kernel.slot(0)
assert slot.trade_id == trade_id
if expect_closed:
assert slot.closed is True
assert slot.fsm_state in {TradeStage.CLOSED, TradeStage.IDLE}
assert kernel.account.snapshot.open_positions in {0, 1}
def _bingx_steps_for_cycle(side: TradeSide, *, hung_exit: bool = False, cancel_reject: bool = False) -> list[BingxE2EStep]:
entry_receipt = ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="NEW",
symbol="BTC-USDT",
side=side.value,
action="ENTER",
quantity=1.0,
price=75_000.0,
client_order_id="cid-entry",
order_id="oid-entry",
raw_ack={
"orderId": "oid-entry",
"clientOrderId": "cid-entry",
"status": "NEW",
"symbol": "BTC-USDT",
"executedQty": "0",
},
raw_state={},
)
exit_ack_status = "NEW" if hung_exit or cancel_reject else "FILLED"
exit_filled_qty = 0.0 if hung_exit or cancel_reject else 0.5
exit_receipt = ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status=exit_ack_status,
symbol="BTC-USDT",
side=("SELL" if side == TradeSide.SHORT else "BUY"),
action="EXIT",
quantity=0.5,
price=74_900.0 if side == TradeSide.SHORT else 75_100.0,
client_order_id="cid-exit-1",
order_id="oid-exit-1",
raw_ack={
"orderId": "oid-exit-1",
"clientOrderId": "cid-exit-1",
"status": exit_ack_status,
"symbol": "BTC-USDT",
"executedQty": f"{exit_filled_qty}",
"cumFilledQty": f"{exit_filled_qty}",
"avgPrice": "74900" if side == TradeSide.SHORT else "75100",
},
raw_state={},
)
final_receipt = ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status="FILLED",
symbol="BTC-USDT",
side=("SELL" if side == TradeSide.SHORT else "BUY"),
action="EXIT",
quantity=0.5,
price=74_850.0 if side == TradeSide.SHORT else 75_150.0,
client_order_id="cid-exit-2",
order_id="oid-exit-2",
raw_ack={
"orderId": "oid-exit-2",
"clientOrderId": "cid-exit-2",
"status": "FILLED",
"symbol": "BTC-USDT",
"executedQty": "0.5",
"cumFilledQty": "0.5",
"avgPrice": "74850" if side == TradeSide.SHORT else "75150",
},
raw_state={},
)
entry_before = _snapshot()
entry_after = _snapshot(
positions=[_position_row("BTC-USDT", side, 1.0, 75_000.0)],
open_orders=[
{
"symbol": "BTC-USDT",
"clientOrderId": "cid-entry",
"clientOrderID": "cid-entry",
"orderId": "oid-entry",
"status": "FILLED",
"origQty": "1",
"executedQty": "1",
"avgPrice": "75000",
}
],
all_orders=[{"symbol": "BTC-USDT", "clientOrderId": "cid-entry", "clientOrderID": "cid-entry", "orderId": "oid-entry", "status": "FILLED"}],
all_fills=[{"symbol": "BTC-USDT", "clientOrderId": "cid-entry", "clientOrderID": "cid-entry", "orderId": "oid-entry", "status": "FILLED", "executedQty": "1", "lastFilledQty": "1", "lastFillPrice": "75000"}],
)
exit_before = entry_after
cancel_open_positions = [_position_row("BTC-USDT", side, 1.0, 75_000.0)] if (hung_exit or cancel_reject) else []
cancel_open_orders = [
{
"symbol": "BTC-USDT",
"clientOrderId": "cid-exit-1",
"clientOrderID": "cid-exit-1",
"orderId": "oid-exit-1",
"status": exit_ack_status,
"origQty": "0.5",
"executedQty": "0",
"avgPrice": "74900",
}
] if (hung_exit or cancel_reject) else []
exit_after = _snapshot(
positions=cancel_open_positions,
open_orders=cancel_open_orders,
all_orders=[{"symbol": "BTC-USDT", "clientOrderId": "cid-exit-1", "clientOrderID": "cid-exit-1", "orderId": "oid-exit-1", "status": exit_ack_status}],
all_fills=[{"symbol": "BTC-USDT", "clientOrderId": "cid-exit-1", "clientOrderID": "cid-exit-1", "orderId": "oid-exit-1", "status": exit_ack_status, "executedQty": f"{exit_filled_qty}", "lastFilledQty": f"{exit_filled_qty}", "lastFillPrice": "74900"}] if exit_filled_qty > 0 else [],
)
cancel_after = _snapshot(
positions=cancel_open_positions,
open_orders=[],
all_orders=[{"symbol": "BTC-USDT", "clientOrderId": "cid-exit-1", "clientOrderID": "cid-exit-1", "orderId": "oid-exit-1", "status": "CANCELED"}],
all_fills=[],
)
cancel_before = exit_after
cancel_kind = "cancel_reject" if cancel_reject else "cancel_ack"
final_before = cancel_after if (hung_exit or cancel_reject) else exit_after
final_after = _snapshot(
positions=[_position_row("BTC-USDT", side, 0.5, 74_900.0 if side == TradeSide.SHORT else 75_100.0)] if (hung_exit or cancel_reject) else [],
open_orders=[],
all_orders=[{"symbol": "BTC-USDT", "clientOrderId": "cid-exit-2", "clientOrderID": "cid-exit-2", "orderId": "oid-exit-2", "status": "FILLED"}],
all_fills=[{"symbol": "BTC-USDT", "clientOrderId": "cid-exit-2", "clientOrderID": "cid-exit-2", "orderId": "oid-exit-2", "status": "FILLED", "executedQty": "0.5", "lastFilledQty": "0.5", "lastFillPrice": "74850" if side == TradeSide.SHORT else "75150"}] if (hung_exit or cancel_reject) else [],
)
return [
BingxE2EStep("entry", "fill", 1.0, entry_before, entry_after, entry_receipt),
BingxE2EStep(
"exit_hang" if hung_exit else "exit_1",
"fill" if not hung_exit else "ack_only",
0.5 if not hung_exit else 0.0,
exit_before,
exit_after,
exit_receipt,
submit_advances=not (hung_exit or cancel_reject),
cancel_kind=cancel_kind,
cancel_before_snapshot=cancel_before,
cancel_after_snapshot=cancel_after,
cancel_advances=True,
),
BingxE2EStep("exit_2", "fill", 1.0, final_before, final_after, final_receipt),
]
def _run_signal_plan(kernel: ExecutionKernel, side: TradeSide, plan: Sequence[SignalAction]) -> ExecutionKernel:
trade_id = f"signal-{side.value.lower()}"
for step in plan:
if step.kind == "entry":
kernel.process_intent(_intent(action=KernelCommandType.ENTER, trade_id=trade_id, side=side, target_size=1.0, price=75_000.0, reason=step.reason or "ENTRY"))
elif step.kind == "mark":
kernel.process_intent(_intent(action=KernelCommandType.MARK_PRICE, trade_id=trade_id, side=side, target_size=1.0, price=step.price, reason=step.reason or "MARK"))
elif step.kind == "exit":
kernel.process_intent(_intent(action=KernelCommandType.EXIT, trade_id=trade_id, side=side, target_size=step.target_size, price=step.price, exit_leg_ratios=(0.5, 0.5), reason=step.reason or "EXIT"))
elif step.kind == "cancel":
slot = kernel.slot(0)
if step.require_close:
active_order = slot.active_exit_order
if active_order is None:
fallback_client_id = f"{trade_id}:{step.reason or 'CANCEL'}:{slot.slot_id}"
active_order = VenueOrder(
internal_trade_id=slot.trade_id or trade_id,
venue_order_id=str(slot.active_entry_order.venue_order_id if slot.active_entry_order else fallback_client_id),
venue_client_id=str(slot.active_entry_order.venue_client_id if slot.active_entry_order else fallback_client_id),
side=slot.side,
intended_size=float(slot.active_exit_order.intended_size if slot.active_exit_order else max(slot.size, step.target_size or slot.size or 0.0)),
filled_size=0.0,
average_fill_price=float(step.price),
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": slot.asset, "action": "EXIT"},
)
emitted = kernel.venue.cancel(active_order, reason=step.reason or "CANCEL")
for event in emitted:
kernel.on_venue_event(event)
elif step.kind == "reconcile":
kernel.process_intent(_intent(action=KernelCommandType.RECONCILE, trade_id=trade_id, side=side, target_size=1.0, price=step.price, reason=step.reason or "RECONCILE"))
else:
raise AssertionError(step.kind)
return kernel
MOCK_SIGNAL_CASES = [
(
"short_full_gamut",
TradeSide.SHORT,
[
VenueScriptStep("entry", "entry_full"),
VenueScriptStep("exit_tp1", "exit_partial", fill_ratio=0.5),
VenueScriptStep("exit_tp2", "exit_full", fill_ratio=1.0),
],
[
SignalAction("entry", 75_000.0, reason="ENTRY"),
SignalAction("mark", 74_200.0, reason="PUMP_BREAK"),
SignalAction("exit", 74_900.0, target_size=0.5, reason="TP1"),
SignalAction("mark", 74_100.0, reason="TRAIL"),
SignalAction("exit", 74_800.0, target_size=0.5, reason="TP2"),
],
),
(
"long_full_gamut",
TradeSide.LONG,
[
VenueScriptStep("entry", "entry_full"),
VenueScriptStep("exit_tp1", "exit_partial", fill_ratio=0.5),
VenueScriptStep("exit_tp2", "exit_full", fill_ratio=1.0),
],
[
SignalAction("entry", 75_000.0, reason="ENTRY"),
SignalAction("mark", 75_800.0, reason="RALLY"),
SignalAction("exit", 75_100.0, target_size=0.5, reason="TP1"),
SignalAction("mark", 75_900.0, reason="TRAIL"),
SignalAction("exit", 75_200.0, target_size=0.5, reason="TP2"),
],
),
(
"hung_exit_then_cancel",
TradeSide.SHORT,
[
VenueScriptStep("entry", "entry_full"),
VenueScriptStep("hung_exit", "ack_only", submit_advances=False, cancel_kind="cancel_ack", cancel_advances=True),
VenueScriptStep("exit_after_cancel", "exit_full", fill_ratio=1.0),
],
[
SignalAction("entry", 75_000.0, reason="ENTRY"),
SignalAction("mark", 74_300.0, reason="HANG"),
SignalAction("exit", 74_950.0, target_size=0.5, reason="HUNG_TP"),
SignalAction("cancel", 74_950.0, reason="CANCEL_HUNG", require_close=True),
SignalAction("exit", 74_700.0, target_size=0.5, reason="RESUME_TP"),
],
),
(
"cancel_reject_then_fill",
TradeSide.SHORT,
[
VenueScriptStep("entry", "entry_full"),
VenueScriptStep("hung_exit", "ack_only", submit_advances=False, cancel_kind="cancel_reject", cancel_advances=False),
VenueScriptStep("exit_after_reject", "exit_full", fill_ratio=1.0),
],
[
SignalAction("entry", 75_000.0, reason="ENTRY"),
SignalAction("mark", 74_250.0, reason="HANG"),
SignalAction("exit", 74_950.0, target_size=0.5, reason="HUNG_TP"),
SignalAction("cancel", 74_950.0, reason="CANCEL_REJECT", require_close=True),
SignalAction("exit", 74_650.0, target_size=0.5, reason="FINAL_TP"),
],
),
]
@pytest.mark.parametrize("name,side,steps,plan", MOCK_SIGNAL_CASES, ids=[case[0] for case in MOCK_SIGNAL_CASES])
def test_mock_signal_gamut_e2e_matrix(name: str, side: TradeSide, steps: Sequence[VenueScriptStep], plan: Sequence[SignalAction]) -> None:
venue = ScriptedVenueAdapter(steps)
kernel = _kernel(venue)
kernel.update_control(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE))
_run_signal_plan(kernel, side, plan)
slot = kernel.slot(0)
assert slot.trade_id == f"signal-{side.value.lower()}"
assert venue.calls[0][0] == "submit"
expected_cancel = any(step.kind == "cancel" and step.require_close for step in plan)
assert any(call[0] == "cancel" for call in venue.calls) == expected_cancel
assert kernel.snapshot()["control"]["mode"] == KernelMode.DEBUG.value
if name in {"short_full_gamut", "long_full_gamut", "hung_exit_then_cancel", "cancel_reject_then_fill"}:
assert slot.fsm_state in {TradeStage.CLOSED, TradeStage.POSITION_OPEN, TradeStage.EXIT_WORKING}
if name == "hung_exit_then_cancel":
assert any(call[0] == "cancel" for call in venue.calls)
assert slot.closed is True or slot.fsm_state == TradeStage.POSITION_OPEN
def _bingx_backend_for_plan(side: TradeSide, *, hung_exit: bool = False, cancel_reject: bool = False) -> BingxE2EBackend:
return BingxE2EBackend(_bingx_steps_for_cycle(side, hung_exit=hung_exit, cancel_reject=cancel_reject))
@pytest.mark.parametrize(
"side,hung_exit,cancel_reject",
[
(TradeSide.SHORT, False, False),
(TradeSide.LONG, False, False),
(TradeSide.SHORT, True, False),
(TradeSide.SHORT, True, True),
],
ids=["short_full", "long_full", "short_hung", "short_cancel_reject"],
)
def test_bingx_basic_e2e_matrix(side: TradeSide, hung_exit: bool, cancel_reject: bool) -> None:
backend = _bingx_backend_for_plan(side, hung_exit=hung_exit, cancel_reject=cancel_reject)
venue = BingxVenueAdapter(backend=backend)
kernel = _kernel(venue)
kernel.update_control(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE, backend_mode=BackendMode.BINGX))
_run_signal_plan(
kernel,
side,
[
SignalAction("entry", 75_000.0, reason="ENTRY"),
SignalAction("mark", 74_200.0 if side == TradeSide.SHORT else 75_800.0, reason="MARK"),
SignalAction("exit", 74_900.0 if side == TradeSide.SHORT else 75_100.0, target_size=0.5, reason="TP1"),
SignalAction("cancel", 74_900.0 if side == TradeSide.SHORT else 75_100.0, reason="CANCEL" if hung_exit or cancel_reject else "NO_CANCEL", require_close=hung_exit or cancel_reject),
SignalAction("exit", 74_850.0 if side == TradeSide.SHORT else 75_150.0, target_size=0.5, reason="TP2"),
],
)
slot = kernel.slot(0)
assert backend.connected is False
assert any(call[0] == "submit_intent" for call in backend.calls)
assert slot.trade_id.startswith("signal-")
assert slot.fsm_state in {TradeStage.CLOSED, TradeStage.POSITION_OPEN, TradeStage.EXIT_WORKING}
if not hung_exit:
assert slot.closed is True
else:
assert any(call[0] == "cancel_order" for call in backend.calls)
FUZZ_SEEDS = tuple(range(12))
FUZZ_VENUES = ("mock", "bingx")
@pytest.mark.parametrize("seed", FUZZ_SEEDS, ids=lambda seed: f"seed-{seed}")
@pytest.mark.parametrize("venue_kind", FUZZ_VENUES, ids=lambda venue_kind: f"venue-{venue_kind}")
def test_e2e_chaos_fuzz_matrix(seed: int, venue_kind: str) -> None:
rng = random.Random(20260527 + seed)
side = rng.choice([TradeSide.SHORT, TradeSide.LONG])
if venue_kind == "mock":
steps = [
VenueScriptStep("entry", "entry_full" if rng.random() > 0.2 else "entry_partial", fill_ratio=1.0 if rng.random() > 0.5 else 0.5),
VenueScriptStep("exit", "ack_only" if rng.random() > 0.35 else "exit_partial", fill_ratio=0.5, cancel_kind="cancel_reject" if rng.random() > 0.75 else "cancel_ack", submit_advances=False if rng.random() > 0.35 else True),
VenueScriptStep("exit2", "exit_full", fill_ratio=1.0),
]
venue = ScriptedVenueAdapter(steps)
kernel = _kernel(venue)
else:
backend = _bingx_backend_for_plan(side, hung_exit=rng.random() > 0.4, cancel_reject=rng.random() > 0.7)
venue = BingxVenueAdapter(backend=backend)
kernel = _kernel(venue)
kernel.update_control(ControlUpdate(backend_mode=BackendMode.BINGX))
trade_id = f"fuzz-{venue_kind}-{seed}"
kernel.process_intent(_intent(action=KernelCommandType.ENTER, trade_id=trade_id, side=side, target_size=1.0, price=75_000.0, reason="ENTER"))
for idx in range(rng.randint(2, 5)):
op = rng.choice(["mark", "exit", "cancel", "reconcile"])
slot = kernel.slot(0)
if op == "mark":
kernel.process_intent(_intent(action=KernelCommandType.MARK_PRICE, trade_id=trade_id, side=side, target_size=1.0, price=74_000.0 if side == TradeSide.SHORT else 76_000.0, reason=f"MARK-{idx}"))
elif op == "exit" and slot.fsm_state in {TradeStage.POSITION_OPEN, TradeStage.EXIT_WORKING}:
kernel.process_intent(_intent(action=KernelCommandType.EXIT, trade_id=trade_id, side=side, target_size=max(0.1, slot.size or 0.5), price=74_900.0 if side == TradeSide.SHORT else 75_100.0, exit_leg_ratios=(0.5, 0.5), reason=f"EXIT-{idx}"))
elif op == "cancel" and slot.active_exit_order is not None:
kernel.process_intent(_intent(action=KernelCommandType.CANCEL, trade_id=trade_id, side=side, target_size=slot.active_exit_order.intended_size, price=74_900.0 if side == TradeSide.SHORT else 75_100.0, reason=f"CANCEL-{idx}"))
elif op == "reconcile":
kernel.process_intent(_intent(action=KernelCommandType.RECONCILE, trade_id=trade_id, side=side, target_size=1.0, price=75_000.0, reason=f"RECONCILE-{idx}"))
final_slot = kernel.slot(0)
assert final_slot.trade_id == trade_id
assert final_slot.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
assert kernel.account.snapshot.equity == pytest.approx(kernel.account.snapshot.capital + kernel.account.snapshot.unrealized_pnl, abs=1e-9)
assert kernel.snapshot()["control"]["runtime_namespace"] == "dita_v2"
if final_slot.closed:
assert final_slot.size == pytest.approx(0.0, abs=1e-9)
else:
assert final_slot.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
TradeStage.STALE_STATE_RECONCILING,
}

View File

@@ -1,612 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import math
import pytest
from prod.clean_arch.dita_v2 import (
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelVerbosity,
MemoryKernelJournal,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class NoopVenueAdapter:
def submit(self, intent): # type: ignore[override]
return []
def cancel(self, order, *, reason: str = ""): # type: ignore[override]
return []
def open_orders(self): # type: ignore[override]
return []
def open_positions(self): # type: ignore[override]
return []
def reconcile(self): # type: ignore[override]
return []
@dataclass(frozen=True)
class IntentGuardCase:
name: str
slot_id: int
seed_state: str
action: KernelCommandType
trade_id: str
intent_trade_id: str
expected_state: TradeStage
expected_code: KernelDiagnosticCode
expected_accepted: bool
@dataclass(frozen=True)
class DuplicateCase:
name: str
seed_state: str
first_kind: KernelEventKind
second_kind: KernelEventKind
expected_state: TradeStage
event_factory_name: str
@dataclass(frozen=True)
class StaleCase:
name: str
second_kind: KernelEventKind
same_event_id_as_initial: bool
expected_accepted: bool
@dataclass(frozen=True)
class ZincMirrorCase:
name: str
op: str
@dataclass(frozen=True)
class SlotRigorCase:
name: str
op: str
def _build_kernel(slot_count: int = 3) -> tuple[ExecutionKernel, MemoryKernelJournal, InMemoryZincPlane]:
journal = MemoryKernelJournal()
zinc = InMemoryZincPlane()
kernel = ExecutionKernel(
max_slots=slot_count,
control_plane=InMemoryControlPlane(
KernelControlSnapshot(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.MOCK,
debug_clickhouse_enabled=True,
trace_transitions=True,
mirror_to_hazelcast=True,
)
),
venue=NoopVenueAdapter(),
journal=journal,
zinc_plane=zinc,
)
return kernel, journal, zinc
def _make_entry_order(trade_id: str, slot_id: int, *, size: float = 1.0, status: VenueOrderStatus = VenueOrderStatus.NEW) -> VenueOrder:
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"V-ENTRY-{slot_id}-{trade_id}",
venue_client_id=f"{trade_id}:entry:{slot_id}",
side=TradeSide.SHORT,
intended_size=size,
filled_size=size if status == VenueOrderStatus.FILLED else 0.0,
average_fill_price=100.0,
status=status,
metadata={"slot_id": slot_id},
)
def _make_exit_order(trade_id: str, slot_id: int, *, size: float, status: VenueOrderStatus = VenueOrderStatus.NEW) -> VenueOrder:
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"V-EXIT-{slot_id}-{trade_id}",
venue_client_id=f"{trade_id}:exit:{slot_id}",
side=TradeSide.SHORT,
intended_size=size,
filled_size=size if status == VenueOrderStatus.FILLED else 0.0,
average_fill_price=99.0,
status=status,
metadata={"slot_id": slot_id},
)
def _seed_free_slot(slot_id: int) -> TradeSlot:
return TradeSlot(slot_id=slot_id)
def _seed_entry_working_slot(trade_id: str, slot_id: int) -> TradeSlot:
return TradeSlot(
slot_id=slot_id,
trade_id=trade_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=0.0,
size=0.0,
initial_size=0.0,
leverage=2.0,
entry_time=datetime.now(timezone.utc),
exit_leg_ratios=(1.0,),
active_leg_index=0,
active_entry_order=_make_entry_order(trade_id, slot_id, status=VenueOrderStatus.NEW),
fsm_state=TradeStage.ENTRY_WORKING,
)
def _seed_position_open_slot(trade_id: str, slot_id: int, *, size: float = 1.0, side: TradeSide = TradeSide.SHORT) -> TradeSlot:
return TradeSlot(
slot_id=slot_id,
trade_id=trade_id,
asset="BTCUSDT",
side=side,
entry_price=100.0,
size=size,
initial_size=size,
leverage=2.0,
entry_time=datetime.now(timezone.utc),
exit_leg_ratios=(0.5, 0.5),
active_leg_index=0,
active_entry_order=_make_entry_order(trade_id, slot_id, size=size, status=VenueOrderStatus.FILLED),
fsm_state=TradeStage.POSITION_OPEN,
)
def _seed_exit_working_slot(trade_id: str, slot_id: int, *, size: float = 1.0) -> TradeSlot:
slot = _seed_position_open_slot(trade_id, slot_id, size=size)
slot.active_exit_order = _make_exit_order(trade_id, slot_id, size=slot.next_exit_ratio() * size, status=VenueOrderStatus.NEW)
slot.fsm_state = TradeStage.EXIT_WORKING
return slot
def _seed_closed_slot(trade_id: str, slot_id: int) -> TradeSlot:
return TradeSlot(
slot_id=slot_id,
trade_id=trade_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=0.0,
initial_size=1.0,
leverage=2.0,
entry_time=datetime.now(timezone.utc),
closed=True,
fsm_state=TradeStage.CLOSED,
)
def _make_intent(
*,
trade_id: str,
slot_id: int,
action: KernelCommandType,
leverage: float = 2.0,
size: float = 1.0,
side: TradeSide = TradeSide.SHORT,
reason: str = "HARNESS",
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{trade_id}-{action.value}-{slot_id}",
trade_id=trade_id,
slot_id=slot_id,
asset="BTCUSDT",
side=side,
action=action,
reference_price=100.0,
target_size=size,
leverage=leverage,
exit_leg_ratios=(0.5, 0.5) if action == KernelCommandType.EXIT else (1.0,),
reason=reason,
)
def _make_event(
slot: TradeSlot,
*,
kind: KernelEventKind,
event_id: str,
filled_size: float = 0.0,
reason: str = "",
slot_id: int | None = None,
) -> VenueEvent:
order = slot.active_exit_order or slot.active_entry_order
venue_order_id = order.venue_order_id if order else f"V-{kind.value}-{slot.slot_id}"
venue_client_id = order.venue_client_id if order else f"{slot.trade_id}:client:{slot.slot_id}"
status = {
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
KernelEventKind.CONTROL: VenueEventStatus.ACKED,
}[kind]
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=event_id,
trade_id=slot.trade_id,
slot_id=slot.slot_id if slot_id is None else slot_id,
kind=kind,
status=status,
venue_order_id=venue_order_id,
venue_client_id=venue_client_id,
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
asset=slot.asset or "BTCUSDT",
price=99.0 if kind == KernelEventKind.MARK_PRICE else 100.0,
size=max(slot.size, 1.0),
filled_size=filled_size,
remaining_size=max(0.0, max(slot.size, 1.0) - filled_size),
reason=reason,
)
INTENT_GUARD_CASES = [
IntentGuardCase("invalid_negative_enter", -1, "free", KernelCommandType.ENTER, "trade-neg", "trade-neg", TradeStage.IDLE, KernelDiagnosticCode.INVALID_SLOT_ID, False),
IntentGuardCase("invalid_high_exit", 99, "free", KernelCommandType.EXIT, "trade-high", "trade-high", TradeStage.IDLE, KernelDiagnosticCode.INVALID_SLOT_ID, False),
IntentGuardCase("unsupported_control", 0, "free", KernelCommandType.CONTROL, "trade-control", "trade-control", TradeStage.IDLE, KernelDiagnosticCode.UNSUPPORTED_INTENT, False),
IntentGuardCase("free_exit", 0, "free", KernelCommandType.EXIT, "trade-free-exit", "trade-free-exit", TradeStage.IDLE, KernelDiagnosticCode.NO_OPEN_POSITION, False),
IntentGuardCase("free_cancel", 0, "free", KernelCommandType.CANCEL, "trade-free-cancel", "trade-free-cancel", TradeStage.IDLE, KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER, False),
IntentGuardCase("busy_enter_different_trade", 0, "position_open", KernelCommandType.ENTER, "trade-open", "trade-new", TradeStage.POSITION_OPEN, KernelDiagnosticCode.SLOT_BUSY, False),
IntentGuardCase("same_trade_enter_allowed", 0, "position_open", KernelCommandType.ENTER, "trade-open", "trade-open", TradeStage.ORDER_REQUESTED, KernelDiagnosticCode.OK, True),
IntentGuardCase("closed_exit", 0, "closed", KernelCommandType.EXIT, "trade-closed", "trade-closed", TradeStage.CLOSED, KernelDiagnosticCode.NO_OPEN_POSITION, False),
IntentGuardCase("open_reconcile", 0, "position_open", KernelCommandType.RECONCILE, "trade-reconcile", "trade-reconcile", TradeStage.STALE_STATE_RECONCILING, KernelDiagnosticCode.STALE_STATE_RECONCILE, True),
IntentGuardCase("free_mark_price", 0, "free", KernelCommandType.MARK_PRICE, "trade-mark", "trade-mark", TradeStage.IDLE, KernelDiagnosticCode.OK, True),
]
DUPLICATE_CASES = [
DuplicateCase("entry_ack_duplicate", "entry_working", KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_ACK, TradeStage.ENTRY_WORKING, "ack"),
DuplicateCase("entry_partial_duplicate", "entry_working", KernelEventKind.PARTIAL_FILL, KernelEventKind.PARTIAL_FILL, TradeStage.ENTRY_WORKING, "partial-entry"),
DuplicateCase("entry_full_duplicate", "entry_working", KernelEventKind.FULL_FILL, KernelEventKind.FULL_FILL, TradeStage.POSITION_OPEN, "full-entry"),
DuplicateCase("exit_ack_duplicate", "exit_working", KernelEventKind.CANCEL_ACK, KernelEventKind.CANCEL_ACK, TradeStage.POSITION_OPEN, "ack-exit"),
DuplicateCase("exit_partial_duplicate", "exit_working", KernelEventKind.PARTIAL_FILL, KernelEventKind.PARTIAL_FILL, TradeStage.EXIT_WORKING, "partial-exit"),
DuplicateCase("exit_full_duplicate", "exit_working", KernelEventKind.FULL_FILL, KernelEventKind.FULL_FILL, TradeStage.CLOSED, "full-exit"),
DuplicateCase("cancel_reject_duplicate", "exit_working", KernelEventKind.CANCEL_REJECT, KernelEventKind.CANCEL_REJECT, TradeStage.EXIT_WORKING, "reject-exit"),
DuplicateCase("mark_price_duplicate", "position_open", KernelEventKind.MARK_PRICE, KernelEventKind.MARK_PRICE, TradeStage.POSITION_OPEN, "mark"),
DuplicateCase("reconcile_duplicate", "position_open", KernelEventKind.RECONCILE, KernelEventKind.RECONCILE, TradeStage.STALE_STATE_RECONCILING, "reconcile"),
DuplicateCase("entry_reject_duplicate", "entry_working", KernelEventKind.ORDER_REJECT, KernelEventKind.ORDER_REJECT, TradeStage.IDLE, "reject-entry"),
]
STALE_CASES = [
StaleCase("stale_ack", KernelEventKind.ORDER_ACK, False, False),
StaleCase("stale_reject", KernelEventKind.ORDER_REJECT, False, False),
StaleCase("stale_partial", KernelEventKind.PARTIAL_FILL, False, False),
StaleCase("stale_full", KernelEventKind.FULL_FILL, False, False),
StaleCase("stale_cancel_ack", KernelEventKind.CANCEL_ACK, False, False),
StaleCase("stale_cancel_reject", KernelEventKind.CANCEL_REJECT, False, False),
StaleCase("stale_mark_price", KernelEventKind.MARK_PRICE, False, False),
StaleCase("stale_control", KernelEventKind.CONTROL, False, False),
StaleCase("stale_reconcile", KernelEventKind.RECONCILE, False, True),
StaleCase("stale_duplicate_precedence", KernelEventKind.ORDER_ACK, True, False),
]
ZINC_MIRROR_CASES = [
ZincMirrorCase("intent_published_on_enter", "intent"),
ZincMirrorCase("invalid_slot_intent_still_publishes", "invalid_intent"),
ZincMirrorCase("slot_write_updates_state_region", "direct_write"),
ZincMirrorCase("venue_event_updates_state_region", "venue_event"),
ZincMirrorCase("control_update_writes_region", "control_update"),
ZincMirrorCase("snapshot_reflects_control", "snapshot"),
ZincMirrorCase("reconcile_from_slots_writes_all", "reconcile"),
ZincMirrorCase("free_slot_selects_first_free", "free_slot"),
ZincMirrorCase("read_slots_sorted", "sorted_read"),
ZincMirrorCase("slot_overwrite_replaces_previous_state", "overwrite"),
]
SLOT_RIGOR_CASES = [
SlotRigorCase("idle_slot_is_free", "idle_free"),
SlotRigorCase("closed_slot_is_free", "closed_free"),
SlotRigorCase("entry_working_is_not_free", "entry_not_free"),
SlotRigorCase("open_slot_is_not_free", "open_not_free"),
SlotRigorCase("mark_price_zero_is_noop", "mark_zero"),
SlotRigorCase("mark_price_negative_is_noop", "mark_negative"),
SlotRigorCase("mark_price_nan_is_noop", "mark_nan"),
SlotRigorCase("short_price_rise_negative_pnl", "short_rise"),
SlotRigorCase("short_price_drop_positive_pnl", "short_drop"),
SlotRigorCase("exit_leg_consume_and_clamp", "exit_leg"),
]
def _seed_for_intent_case(kernel: ExecutionKernel, case: IntentGuardCase) -> None:
if case.seed_state == "free":
return
if case.seed_state == "entry_working":
kernel._set_slot(_seed_entry_working_slot(case.trade_id, case.slot_id))
return
if case.seed_state == "position_open":
kernel._set_slot(_seed_position_open_slot(case.trade_id, case.slot_id))
return
if case.seed_state == "exit_working":
kernel._set_slot(_seed_exit_working_slot(case.trade_id, case.slot_id))
return
if case.seed_state == "closed":
kernel._set_slot(_seed_closed_slot(case.trade_id, case.slot_id))
return
raise AssertionError(case.seed_state)
def _seed_for_duplicate_case(kernel: ExecutionKernel, case: DuplicateCase) -> TradeSlot:
if case.seed_state == "entry_working":
slot = _seed_entry_working_slot(f"trade-{case.name}", 0)
elif case.seed_state == "exit_working":
slot = _seed_exit_working_slot(f"trade-{case.name}", 0)
elif case.seed_state == "position_open":
slot = _seed_position_open_slot(f"trade-{case.name}", 0)
elif case.seed_state == "stale":
slot = _seed_position_open_slot(f"trade-{case.name}", 0)
else:
raise AssertionError(case.seed_state)
kernel._set_slot(slot)
return kernel._get_slot(0)
def _seed_for_stale_case(kernel: ExecutionKernel) -> TradeSlot:
slot = _seed_position_open_slot("trade-stale", 0)
kernel._set_slot(slot)
return kernel._get_slot(0)
def _seed_for_zinc_case(kernel: ExecutionKernel, case: ZincMirrorCase) -> None:
if case.op == "intent":
return
if case.op == "invalid_intent":
return
if case.op == "direct_write":
kernel._set_slot(_seed_position_open_slot("trade-write", 0))
return
if case.op == "venue_event":
kernel._set_slot(_seed_entry_working_slot("trade-event", 0))
return
if case.op == "control_update":
return
if case.op == "snapshot":
return
if case.op == "reconcile":
return
if case.op == "free_slot":
kernel._set_slot(_seed_position_open_slot("trade-free", 0))
kernel._set_slot(_seed_free_slot(1))
return
if case.op == "sorted_read":
return
if case.op == "overwrite":
return
raise AssertionError(case.op)
@pytest.mark.parametrize("case", INTENT_GUARD_CASES, ids=lambda case: case.name)
def test_kernel_intent_guard_matrix(case: IntentGuardCase) -> None:
kernel, _, zinc = _build_kernel()
_seed_for_intent_case(kernel, case)
intent = _make_intent(
trade_id=case.intent_trade_id,
slot_id=case.slot_id,
action=case.action,
leverage=-3.0 if case.name == "same_trade_enter_allowed" else 2.5,
size=1.0,
reason=case.name,
)
outcome = kernel.process_intent(intent)
assert outcome.accepted is case.expected_accepted
assert outcome.diagnostic_code == case.expected_code
assert outcome.state == case.expected_state
if case.slot_id >= 0 and case.slot_id < kernel.max_slots:
assert zinc.intent_region
assert zinc.intent_region[-1].intent_id == intent.intent_id
if case.name == "same_trade_enter_allowed":
current = kernel.slot(case.slot_id).to_dict()
assert current["fsm_state"] == TradeStage.ORDER_REQUESTED.value
assert current["leverage"] == 1.0
@pytest.mark.parametrize("case", DUPLICATE_CASES, ids=lambda case: case.name)
def test_kernel_duplicate_event_matrix(case: DuplicateCase) -> None:
kernel, _, _ = _build_kernel()
slot = _seed_for_duplicate_case(kernel, case)
fill_size = slot.size or 1.0
if case.seed_state == "exit_working" and case.first_kind == KernelEventKind.PARTIAL_FILL:
fill_size = max(0.1, fill_size * 0.4)
first_event = _make_event(slot, kind=case.first_kind, event_id=f"dup-{case.name}", filled_size=fill_size)
first = kernel.on_venue_event(first_event)
second = kernel.on_venue_event(first_event)
assert first.diagnostic_code in {
KernelDiagnosticCode.OK,
KernelDiagnosticCode.STALE_STATE_RECONCILE,
KernelDiagnosticCode.ENTRY_ORDER_REJECTED,
KernelDiagnosticCode.EXIT_ORDER_REJECTED,
KernelDiagnosticCode.ORDER_REJECTED,
KernelDiagnosticCode.CANCEL_REJECTED,
}
assert second.diagnostic_code == KernelDiagnosticCode.DUPLICATE_EVENT
assert second.state == case.expected_state
assert second.accepted is True
assert kernel.slot(0).to_dict()["seen_event_ids"].count(first_event.event_id) == 1
@pytest.mark.parametrize("case", STALE_CASES, ids=lambda case: case.name)
def test_kernel_stale_state_matrix(case: StaleCase) -> None:
kernel, _, _ = _build_kernel()
slot = _seed_for_stale_case(kernel)
initial = _make_event(slot, kind=KernelEventKind.RECONCILE, event_id="stale-entry", filled_size=slot.size or 1.0)
initial_outcome = kernel.on_venue_event(initial)
assert initial_outcome.diagnostic_code == KernelDiagnosticCode.OK
assert kernel.slot(0).fsm_state == TradeStage.STALE_STATE_RECONCILING
if case.same_event_id_as_initial:
event = _make_event(kernel._get_slot(0), kind=case.second_kind, event_id="stale-entry", filled_size=slot.size or 1.0, reason=case.name)
else:
event = _make_event(kernel._get_slot(0), kind=case.second_kind, event_id=f"stale-{case.name}", filled_size=slot.size or 1.0, reason=case.name)
outcome = kernel.on_venue_event(event)
if case.same_event_id_as_initial:
assert outcome.diagnostic_code == KernelDiagnosticCode.DUPLICATE_EVENT
assert outcome.accepted is True
else:
assert outcome.diagnostic_code == KernelDiagnosticCode.STALE_STATE_RECONCILE
assert outcome.accepted is case.expected_accepted
assert outcome.state == TradeStage.STALE_STATE_RECONCILING
assert kernel.slot(0).fsm_state == TradeStage.STALE_STATE_RECONCILING
@pytest.mark.parametrize("case", ZINC_MIRROR_CASES, ids=lambda case: case.name)
def test_kernel_zinc_mirror_matrix(case: ZincMirrorCase) -> None:
kernel, _, zinc = _build_kernel()
_seed_for_zinc_case(kernel, case)
if case.op == "intent":
intent = _make_intent(trade_id="trade-intent", slot_id=0, action=KernelCommandType.ENTER, size=1.25)
outcome = kernel.process_intent(intent)
assert outcome.accepted is True
assert zinc.intent_region
assert zinc.intent_region[-1].intent_id == intent.intent_id
assert zinc.read_slots()[0].trade_id == "trade-intent"
elif case.op == "invalid_intent":
intent = _make_intent(trade_id="trade-invalid", slot_id=-1, action=KernelCommandType.EXIT, size=1.0)
outcome = kernel.process_intent(intent)
assert outcome.diagnostic_code == KernelDiagnosticCode.INVALID_SLOT_ID
assert len(zinc.intent_region) == 1
assert zinc.intent_region[-1].intent_id == intent.intent_id
elif case.op == "direct_write":
slot = _seed_position_open_slot("trade-write", 0, size=1.5)
kernel._set_slot(slot)
mirrored = zinc.read_slots()[0]
assert mirrored.trade_id == "trade-write"
assert mirrored.size == 1.5
assert mirrored.fsm_state == TradeStage.POSITION_OPEN
elif case.op == "venue_event":
slot = kernel._get_slot(0)
event = _make_event(slot, kind=KernelEventKind.FULL_FILL, event_id="zinc-fill", filled_size=slot.size or 1.0)
outcome = kernel.on_venue_event(event)
assert outcome.diagnostic_code == KernelDiagnosticCode.OK
mirrored = zinc.read_slots()[0]
assert mirrored.fsm_state == TradeStage.POSITION_OPEN
assert mirrored.seen_event_ids == ("zinc-fill",)
elif case.op == "control_update":
snapshot = kernel.update_control(
ControlUpdate(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
trace_transitions=True,
mirror_to_hazelcast=False,
)
)
assert snapshot.mode == KernelMode.DEBUG
assert zinc.read_control().mode == KernelMode.DEBUG
assert zinc.read_control().trace_transitions is True
elif case.op == "snapshot":
kernel.update_control(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.VERBOSE))
payload = kernel.snapshot()
assert payload["control"]["mode"] == KernelMode.DEBUG.value
assert payload["control"]["verbosity"] == KernelVerbosity.VERBOSE.value
elif case.op == "reconcile":
slots = [
_seed_position_open_slot("trade-a", 2),
_seed_closed_slot("trade-b", 0),
_seed_free_slot(1),
]
outcome = kernel.reconcile_from_slots(slots)
assert outcome.diagnostic_code == KernelDiagnosticCode.RECONCILED
mirrored_ids = [slot.slot_id for slot in zinc.read_slots()]
assert mirrored_ids == [0, 1, 2]
elif case.op == "free_slot":
assert kernel.free_slot().slot_id == 1
elif case.op == "sorted_read":
kernel._set_slot(_seed_position_open_slot("trade-c", 2))
kernel._set_slot(_seed_position_open_slot("trade-a", 0))
kernel._set_slot(_seed_position_open_slot("trade-b", 1))
ids = [slot.slot_id for slot in zinc.read_slots()]
assert ids == [0, 1, 2]
elif case.op == "overwrite":
kernel._set_slot(_seed_position_open_slot("trade-old", 0, size=1.0))
kernel._set_slot(_seed_position_open_slot("trade-new", 0, size=2.0))
mirrored = zinc.read_slots()[0]
assert mirrored.trade_id == "trade-new"
assert mirrored.size == 2.0
assert mirrored.initial_size == 2.0
else: # pragma: no cover - exhaustive
raise AssertionError(case.op)
@pytest.mark.parametrize("case", SLOT_RIGOR_CASES, ids=lambda case: case.name)
def test_trade_slot_state_machine_rigor_matrix(case: SlotRigorCase) -> None:
if case.op == "idle_free":
slot = TradeSlot(slot_id=0)
assert slot.is_free() is True
assert slot.is_open() is False
elif case.op == "closed_free":
slot = _seed_closed_slot("trade-closed", 0)
assert slot.is_free() is True
assert slot.is_open() is False
elif case.op == "entry_not_free":
slot = _seed_entry_working_slot("trade-entry", 0)
assert slot.is_free() is False
assert slot.is_open() is True
elif case.op == "open_not_free":
slot = _seed_position_open_slot("trade-open", 0)
assert slot.is_free() is False
assert slot.is_open() is True
elif case.op == "mark_zero":
slot = _seed_position_open_slot("trade-mark", 0, size=1.0)
slot.mark_price(0.0)
assert slot.unrealized_pnl == 0.0
elif case.op == "mark_negative":
slot = _seed_position_open_slot("trade-mark", 0, size=1.0)
slot.mark_price(-10.0)
assert slot.unrealized_pnl == 0.0
elif case.op == "mark_nan":
slot = _seed_position_open_slot("trade-mark", 0, size=1.0)
slot.mark_price(float("nan"))
assert slot.unrealized_pnl == 0.0
elif case.op == "short_rise":
slot = _seed_position_open_slot("trade-short-rise", 0, size=1.0, side=TradeSide.SHORT)
slot.mark_price(110.0)
assert slot.unrealized_pnl < 0.0
elif case.op == "short_drop":
slot = _seed_position_open_slot("trade-short-drop", 0, size=1.0, side=TradeSide.SHORT)
slot.mark_price(90.0)
assert slot.unrealized_pnl > 0.0
elif case.op == "exit_leg":
slot = _seed_position_open_slot("trade-leg", 0, size=1.0)
slot.exit_leg_ratios = (0.25, 0.75)
first = slot.consume_exit_leg()
second = slot.consume_exit_leg()
third = slot.consume_exit_leg()
assert first == 0.25
assert second == 0.75
assert third == 1.0
assert slot.active_leg_index == 2
assert slot.next_exit_ratio() == 1.0
else: # pragma: no cover - exhaustive
raise AssertionError(case.op)

View File

@@ -1,196 +0,0 @@
from __future__ import annotations
from datetime import datetime, timezone
import unittest
from prod.clean_arch.dita_v2 import (
ControlUpdate,
ExecutionKernel,
HazelcastProjection,
KernelCommandType,
KernelControlSnapshot,
KernelIntent,
KernelMode,
KernelVerbosity,
MockVenueAdapter,
MockVenueScenario,
TradeSide,
TradeStage,
TradeSlot,
build_projection,
build_position_state_row,
)
from prod.clean_arch.dita_v2.hazelcast_projection import HazelcastProjector, HazelcastRowWriter
class CaptureSink:
def __init__(self) -> None:
self.rows: list[tuple[str, dict[str, object]]] = []
def __call__(self, name: str, row: dict[str, object]) -> None:
self.rows.append((name, dict(row)))
class FakeMap:
def __init__(self) -> None:
self.rows: dict[str, object] = {}
def put(self, key: str, value: object) -> None:
self.rows[key] = value
class FakeTopic:
def __init__(self) -> None:
self.messages: list[str] = []
def publish(self, message: str) -> None:
self.messages.append(message)
class FakeHazelcastClient:
def __init__(self) -> None:
self.maps: dict[str, FakeMap] = {}
self.topics: dict[str, FakeTopic] = {}
def get_map(self, name: str) -> FakeMap:
return self.maps.setdefault(name, FakeMap())
def get_topic(self, name: str) -> FakeTopic:
return self.topics.setdefault(name, FakeTopic())
class TestDITAv2Hazelcast(unittest.TestCase):
def test_build_position_state_row_has_compatibility_fields(self) -> None:
slot = TradeSlot(
slot_id=0,
trade_id="trade-1",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
)
row = build_position_state_row(
slot,
KernelControlSnapshot(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
runtime_namespace="dita_v2",
strategy_namespace="dita_v2",
event_namespace="dita_v2",
actor_name="ExecutionKernel",
exec_venue="bingx",
data_venue="binance",
ledger_authority="exchange",
),
)
for key in (
"runtime_namespace",
"strategy_namespace",
"event_namespace",
"actor_name",
"exec_venue",
"data_venue",
"ledger_authority",
"trade_id",
"asset",
"slot_id",
"fsm_state",
):
self.assertIn(key, row)
self.assertEqual(row["trade_id"], "trade-1")
self.assertEqual(row["fsm_state"], TradeStage.POSITION_OPEN.value)
def test_projection_sink_writes_blue_pink_compatible_rows(self) -> None:
sink = CaptureSink()
projection = HazelcastProjection(writer=sink)
control = KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
projection.write_control(control)
slot = TradeSlot(
slot_id=1,
trade_id="trade-2",
asset="ETHUSDT",
side=TradeSide.LONG,
entry_price=50.0,
size=2.0,
initial_size=2.0,
leverage=3.0,
fsm_state=TradeStage.POSITION_OPEN,
)
projection.write_slot(slot)
self.assertGreaterEqual(len(sink.rows), 2)
control_name, control_row = sink.rows[0]
slot_name, slot_row = sink.rows[1]
self.assertEqual(control_name, "hz:dita_control")
self.assertEqual(slot_name, "hz:dita_active_slots")
self.assertEqual(control_row["mode"], KernelMode.DEBUG.value)
self.assertEqual(slot_row["trade_id"], "trade-2")
self.assertEqual(slot_row["runtime_namespace"], "dita_v2")
self.assertEqual(slot_row["ledger_authority"], "exchange")
def test_hazelcast_row_writer_routes_maps_and_topics(self) -> None:
client = FakeHazelcastClient()
writer = HazelcastRowWriter(client)
writer("hz:dita_active_slots", {"trade_id": "trade-3", "slot_id": 0})
writer("hz:dita_control", {"mode": "DEBUG"})
writer("hz:dita_trade_events", {"event_id": "evt-1", "trade_id": "trade-3"})
self.assertIn("trade-3", client.get_map("hz:dita_active_slots").rows)
self.assertIn("control", client.get_map("hz:dita_control").rows)
self.assertEqual(len(client.get_topic("hz:dita_trade_events").messages), 1)
def test_build_projection_uses_client_when_requested(self) -> None:
client = FakeHazelcastClient()
projection = build_projection(client=client, prefer_real_hazelcast=True)
projection.write_control(KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE))
projection.write_slot(
TradeSlot(
slot_id=0,
trade_id="trade-4",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
)
)
self.assertIn("control", client.get_map("hz:dita_control").rows)
self.assertIn("trade-4", client.get_map("hz:dita_active_slots").rows)
def test_kernel_emits_projection_rows(self) -> None:
sink = CaptureSink()
kernel = ExecutionKernel(
control_plane=None,
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)),
projection=HazelcastProjection(writer=sink),
)
kernel.update_control(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE))
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id="intent-1",
trade_id="trade-1",
slot_id=0,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.ENTER,
reference_price=100.0,
target_size=1.0,
leverage=2.0,
exit_leg_ratios=(1.0,),
reason="TEST",
)
)
names = [name for name, _ in sink.rows]
self.assertIn("hz:dita_control", names)
self.assertIn("hz:dita_active_slots", names)
slot_rows = [row for name, row in sink.rows if name == "hz:dita_active_slots"]
self.assertTrue(any(row["trade_id"] == "trade-1" for row in slot_rows))
self.assertTrue(any(row["runtime_namespace"] == "dita_v2" for row in slot_rows))
if __name__ == "__main__":
unittest.main()

View File

@@ -1,231 +0,0 @@
from __future__ import annotations
from datetime import datetime, timezone
import unittest
from prod.clean_arch.dita_v2 import (
AccountProjection,
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelEventKind,
KernelIntent,
KernelMode,
KernelVerbosity,
MemoryKernelJournal,
MockVenueAdapter,
MockVenueScenario,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
)
def mk_intent(
*,
action: KernelCommandType = KernelCommandType.ENTER,
slot_id: int = 0,
trade_id: str = "trade-1",
asset: str = "BTCUSDT",
side: TradeSide = TradeSide.SHORT,
target_size: float = 1.0,
leverage: float = 2.0,
reference_price: float = 100.0,
exit_leg_ratios=(1.0,),
reason: str = "TEST",
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{trade_id}-{action.value}",
trade_id=trade_id,
slot_id=slot_id,
asset=asset,
side=side,
action=action,
reference_price=reference_price,
target_size=target_size,
leverage=leverage,
exit_leg_ratios=tuple(exit_leg_ratios),
reason=reason,
)
class TestDITAv2ControlPlane(unittest.TestCase):
def test_control_plane_updates_and_mirrors(self):
plane = InMemoryControlPlane()
updated = plane.update(
ControlUpdate(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.BINGX,
trace_transitions=True,
)
)
self.assertEqual(updated.mode, KernelMode.DEBUG)
self.assertEqual(updated.verbosity, KernelVerbosity.TRACE)
self.assertEqual(updated.backend_mode, BackendMode.BINGX)
self.assertTrue(updated.trace_transitions)
self.assertEqual(plane.mirror()["mode"], KernelMode.DEBUG.value)
class TestDITAv2Kernel(unittest.TestCase):
def test_entry_ack_fill_reaches_position_open(self):
journal = MemoryKernelJournal()
zinc = InMemoryZincPlane()
venue = MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0))
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
),
venue=venue,
journal=journal,
zinc_plane=zinc,
)
outcome = kernel.process_intent(mk_intent())
slot = kernel.slot(0)
self.assertTrue(outcome.accepted)
self.assertEqual(slot.fsm_state, TradeStage.POSITION_OPEN)
self.assertFalse(slot.closed)
self.assertEqual(slot.trade_id, "trade-1")
self.assertAlmostEqual(slot.size, 1.0, places=6)
self.assertEqual(len(journal.rows), 3)
self.assertEqual(len(zinc.intent_region), 1)
self.assertEqual(zinc.read_control().mode, KernelMode.DEBUG)
def test_partial_fill_stays_working_then_full_fill_opens_position(self):
journal = MemoryKernelJournal()
venue = MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=False, partial_fill_ratio=0.5))
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
),
venue=venue,
journal=journal,
)
kernel.process_intent(mk_intent())
slot = kernel.slot(0)
self.assertEqual(slot.fsm_state, TradeStage.ENTRY_WORKING)
self.assertAlmostEqual(slot.size, 0.5, places=6)
full_fill = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id="evt-full",
trade_id="trade-1",
slot_id=0,
kind=KernelEventKind.FULL_FILL,
status=VenueEventStatus.FILLED,
venue_order_id=slot.active_entry_order.venue_order_id if slot.active_entry_order else "V-00000001",
venue_client_id=slot.active_entry_order.venue_client_id if slot.active_entry_order else "trade-1:intent-trade-1-ENTER",
side=TradeSide.SHORT,
asset="BTCUSDT",
price=100.0,
size=1.0,
filled_size=1.0,
remaining_size=0.0,
)
kernel.on_venue_event(full_fill)
self.assertEqual(slot.fsm_state, TradeStage.POSITION_OPEN)
self.assertFalse(slot.closed)
self.assertAlmostEqual(slot.size, 1.0, places=6)
def test_two_leg_exit_closes_only_after_final_leg(self):
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
),
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)),
journal=MemoryKernelJournal(),
)
kernel.process_intent(mk_intent())
slot = kernel.slot(0)
slot.exit_leg_ratios = (0.5, 0.5)
first_exit = kernel.process_intent(
mk_intent(action=KernelCommandType.EXIT, target_size=0.5, exit_leg_ratios=(0.5, 0.5), reason="TP1")
)
self.assertTrue(first_exit.accepted)
self.assertEqual(slot.fsm_state, TradeStage.POSITION_OPEN)
self.assertFalse(slot.closed)
self.assertAlmostEqual(slot.size, 0.5, places=6)
second_exit = kernel.process_intent(
mk_intent(action=KernelCommandType.EXIT, target_size=0.5, exit_leg_ratios=(0.5, 0.5), reason="TP2")
)
self.assertTrue(second_exit.accepted)
self.assertTrue(slot.closed)
self.assertEqual(slot.fsm_state, TradeStage.CLOSED)
self.assertAlmostEqual(slot.size, 0.0, places=6)
def test_reconcile_sets_stale_state(self):
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(),
venue=MockVenueAdapter(),
journal=MemoryKernelJournal(),
)
kernel.process_intent(mk_intent())
slot = kernel.slot(0)
kernel.process_intent(mk_intent(action=KernelCommandType.RECONCILE))
self.assertEqual(slot.fsm_state, TradeStage.STALE_STATE_RECONCILING)
def test_account_projection_aggregates_slots(self):
projection = AccountProjection()
slots = [
TradeSlot(
slot_id=0,
trade_id="t1",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
metadata={"mark_price": 99.0},
),
TradeSlot(
slot_id=1,
trade_id="t2",
asset="ETHUSDT",
side=TradeSide.LONG,
entry_price=50.0,
size=2.0,
initial_size=2.0,
leverage=3.0,
fsm_state=TradeStage.EXIT_WORKING,
metadata={"mark_price": 55.0},
),
]
projection.observe_slots(slots)
self.assertEqual(projection.snapshot.open_positions, 2)
self.assertAlmostEqual(projection.snapshot.open_notional, 209.0, places=6)
self.assertGreater(projection.snapshot.leverage, 0.0)
def test_debug_mode_journal_records_transitions(self):
journal = MemoryKernelJournal()
kernel = ExecutionKernel(
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
),
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)),
journal=journal,
)
kernel.process_intent(mk_intent())
self.assertGreaterEqual(len(journal.rows), 2)
self.assertTrue(all("slot_state" in row for row in journal.rows))
if __name__ == "__main__":
unittest.main()

View File

@@ -1,579 +0,0 @@
from __future__ import annotations
from datetime import datetime, timezone
import random
from typing import Any
import pytest
from prod.clean_arch.dita_v2 import (
AccountProjection,
BingxVenueAdapter,
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelOutcome,
KernelSeverity,
KernelVerbosity,
MemoryKernelJournal,
MockVenueAdapter,
MockVenueScenario,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
def mk_intent(
*,
action: KernelCommandType = KernelCommandType.ENTER,
slot_id: int = 0,
trade_id: str = "trade-1",
asset: str = "BTCUSDT",
side: TradeSide = TradeSide.SHORT,
target_size: float = 1.0,
leverage: float = 2.0,
reference_price: float = 100.0,
exit_leg_ratios=(1.0,),
reason: str = "TEST",
) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{trade_id}-{action.value}",
trade_id=trade_id,
slot_id=slot_id,
asset=asset,
side=side,
action=action,
reference_price=reference_price,
target_size=target_size,
leverage=leverage,
exit_leg_ratios=tuple(exit_leg_ratios),
reason=reason,
)
def mk_event(
*,
kind: KernelEventKind,
status: VenueEventStatus,
trade_id: str = "trade-1",
slot_id: int = 0,
venue_order_id: str = "V-00000001",
venue_client_id: str = "trade-1:intent-1",
side: TradeSide = TradeSide.SHORT,
asset: str = "BTCUSDT",
price: float = 100.0,
size: float = 1.0,
filled_size: float = 1.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"evt-{kind.value.lower()}",
trade_id=trade_id,
slot_id=slot_id,
kind=kind,
status=status,
venue_order_id=venue_order_id,
venue_client_id=venue_client_id,
side=side,
asset=asset,
price=price,
size=size,
filled_size=filled_size,
remaining_size=remaining_size,
reason=reason,
raw_payload={"status": status.value},
)
def mk_kernel(
*,
max_slots: int = 3,
venue: Any | None = None,
control_mode: KernelMode = KernelMode.DEBUG,
verbosity: KernelVerbosity = KernelVerbosity.TRACE,
) -> ExecutionKernel:
return ExecutionKernel(
max_slots=max_slots,
control_plane=InMemoryControlPlane(
KernelControlSnapshot(mode=control_mode, verbosity=verbosity, backend_mode=BackendMode.MOCK)
),
venue=venue or MockVenueAdapter(),
journal=MemoryKernelJournal(),
zinc_plane=InMemoryZincPlane(),
account=AccountProjection(),
)
def _seed_open_slot(slot: TradeSlot, *, trade_id: str = "trade-1", asset: str = "BTCUSDT") -> None:
slot.trade_id = trade_id
slot.asset = asset
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.size = 1.0
slot.initial_size = 1.0
slot.leverage = 2.0
slot.fsm_state = TradeStage.POSITION_OPEN
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-00000001",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=1.0,
status=VenueOrderStatus.FILLED,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
def _seed_entry_order(slot: TradeSlot, *, trade_id: str = "trade-1", asset: str = "BTCUSDT", status: VenueOrderStatus = VenueOrderStatus.NEW) -> None:
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-00000001",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=1.0,
status=status,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
def _seed_exit_order(slot: TradeSlot, *, trade_id: str = "trade-1", asset: str = "BTCUSDT", intended_size: float = 0.5) -> None:
slot.active_exit_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-00000002",
venue_client_id=f"{trade_id}:exit",
side=TradeSide.SHORT,
intended_size=intended_size,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
def _configure_slot_state(slot: TradeSlot, state: TradeStage, *, trade_id: str = "trade-1", asset: str = "BTCUSDT") -> None:
slot.trade_id = trade_id if state not in {TradeStage.IDLE, TradeStage.CLOSED} else ""
slot.asset = asset if state not in {TradeStage.IDLE, TradeStage.CLOSED} else ""
slot.side = TradeSide.SHORT if state not in {TradeStage.IDLE, TradeStage.CLOSED} else TradeSide.FLAT
slot.entry_price = 100.0 if state not in {TradeStage.IDLE, TradeStage.CLOSED} else 0.0
slot.size = 1.0 if state in {TradeStage.POSITION_OPEN, TradeStage.EXIT_WORKING, TradeStage.EXIT_REQUESTED, TradeStage.EXIT_SENT, TradeStage.ENTRY_WORKING, TradeStage.ORDER_REQUESTED, TradeStage.ORDER_SENT} else 0.0
slot.initial_size = slot.size
slot.leverage = 2.0 if state not in {TradeStage.IDLE, TradeStage.CLOSED} else 0.0
slot.fsm_state = state
slot.closed = state == TradeStage.CLOSED
slot.active_entry_order = None
slot.active_exit_order = None
if state in {TradeStage.ORDER_REQUESTED, TradeStage.ORDER_SENT, TradeStage.ENTRY_WORKING, TradeStage.POSITION_OPEN, TradeStage.POSITION_OPENED}:
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-00000001",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=1.0,
status=VenueOrderStatus.NEW if state in {TradeStage.ORDER_REQUESTED, TradeStage.ORDER_SENT, TradeStage.ENTRY_WORKING} else VenueOrderStatus.FILLED,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
if state in {TradeStage.EXIT_REQUESTED, TradeStage.EXIT_SENT, TradeStage.EXIT_WORKING}:
slot.active_exit_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-00000002",
venue_client_id=f"{trade_id}:exit",
side=TradeSide.SHORT,
intended_size=0.5,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
# 18 invalid-intent slot tests
@pytest.mark.parametrize(
"slot_id,action,expected",
[
(-1, KernelCommandType.ENTER, KernelDiagnosticCode.INVALID_SLOT_ID),
(-1, KernelCommandType.EXIT, KernelDiagnosticCode.INVALID_SLOT_ID),
(-1, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.INVALID_SLOT_ID),
(-1, KernelCommandType.RECONCILE, KernelDiagnosticCode.INVALID_SLOT_ID),
(-1, KernelCommandType.CANCEL, KernelDiagnosticCode.INVALID_SLOT_ID),
(3, KernelCommandType.ENTER, KernelDiagnosticCode.INVALID_SLOT_ID),
(3, KernelCommandType.EXIT, KernelDiagnosticCode.INVALID_SLOT_ID),
(3, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.INVALID_SLOT_ID),
(3, KernelCommandType.RECONCILE, KernelDiagnosticCode.INVALID_SLOT_ID),
(3, KernelCommandType.CANCEL, KernelDiagnosticCode.INVALID_SLOT_ID),
(99, KernelCommandType.ENTER, KernelDiagnosticCode.INVALID_SLOT_ID),
(99, KernelCommandType.EXIT, KernelDiagnosticCode.INVALID_SLOT_ID),
(99, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.INVALID_SLOT_ID),
(99, KernelCommandType.RECONCILE, KernelDiagnosticCode.INVALID_SLOT_ID),
(99, KernelCommandType.CANCEL, KernelDiagnosticCode.INVALID_SLOT_ID),
(7, KernelCommandType.ENTER, KernelDiagnosticCode.INVALID_SLOT_ID),
(7, KernelCommandType.EXIT, KernelDiagnosticCode.INVALID_SLOT_ID),
(7, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.INVALID_SLOT_ID),
],
)
def test_kernel_rejects_invalid_slot_ids_with_codes(slot_id: int, action: KernelCommandType, expected: KernelDiagnosticCode) -> None:
kernel = mk_kernel(max_slots=3)
outcome = kernel.process_intent(mk_intent(slot_id=slot_id, action=action))
assert outcome.accepted is False
assert outcome.diagnostic_code == expected
assert outcome.details["reason"] == "INVALID_SLOT_ID"
# 20 entry-path tests
@pytest.mark.parametrize(
"scenario,expected_state,expected_code,expected_size",
[
(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.5), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.5),
(MockVenueScenario(emit_fill_on_submit=False, partial_fill_ratio=0.5), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.5),
(MockVenueScenario(reject_entries=True), TradeStage.IDLE, KernelDiagnosticCode.OK, 0.0),
(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.25), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.25),
(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.75), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.75),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=False, partial_fill_ratio=0.0), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.0),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=1.0), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=True, partial_fill_ratio=1.0), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=True, partial_fill_ratio=0.5), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.5),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=0.9), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.9),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=0.1), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.1),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=1.0, reject_entries=False), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=False, partial_fill_ratio=1.0), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=False, partial_fill_ratio=0.2), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.2),
(MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=False, partial_fill_ratio=0.3), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.3),
(MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=False, partial_fill_ratio=1.0), TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, 1.0),
(MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=False, partial_fill_ratio=0.0), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.0),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=0.6), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.6),
(MockVenueScenario(emit_ack_before_fill=True, emit_fill_on_submit=True, partial_fill_ratio=0.4), TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, 0.4),
],
)
def test_kernel_entry_path_matrix(
scenario: MockVenueScenario,
expected_state: TradeStage,
expected_code: KernelDiagnosticCode,
expected_size: float,
) -> None:
kernel = mk_kernel(venue=MockVenueAdapter(scenario))
outcome = kernel.process_intent(mk_intent())
assert outcome.accepted is True
assert outcome.diagnostic_code == expected_code
assert kernel.slot(0).fsm_state == expected_state
assert kernel.slot(0).size == pytest.approx(expected_size, abs=1e-6)
# 20 exit-path tests
@pytest.mark.parametrize(
"initial_state,event_kind,event_status,expected_state,expected_code",
[
(TradeStage.POSITION_OPEN, KernelEventKind.PARTIAL_FILL, VenueEventStatus.PARTIALLY_FILLED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(TradeStage.POSITION_OPEN, KernelEventKind.FULL_FILL, VenueEventStatus.FILLED, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(TradeStage.EXIT_REQUESTED, KernelEventKind.PARTIAL_FILL, VenueEventStatus.PARTIALLY_FILLED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(TradeStage.EXIT_REQUESTED, KernelEventKind.FULL_FILL, VenueEventStatus.FILLED, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(TradeStage.EXIT_SENT, KernelEventKind.PARTIAL_FILL, VenueEventStatus.PARTIALLY_FILLED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(TradeStage.EXIT_SENT, KernelEventKind.FULL_FILL, VenueEventStatus.FILLED, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelEventKind.PARTIAL_FILL, VenueEventStatus.PARTIALLY_FILLED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelEventKind.FULL_FILL, VenueEventStatus.FILLED, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelEventKind.CANCEL_ACK, VenueEventStatus.CANCELED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelEventKind.CANCEL_REJECT, VenueEventStatus.CANCELED_REJECTED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.CANCEL_REJECTED),
(TradeStage.POSITION_OPEN, KernelEventKind.CANCEL_ACK, VenueEventStatus.CANCELED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(TradeStage.POSITION_OPEN, KernelEventKind.CANCEL_REJECT, VenueEventStatus.CANCELED_REJECTED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.CANCEL_REJECTED),
(TradeStage.EXIT_REQUESTED, KernelEventKind.CANCEL_ACK, VenueEventStatus.CANCELED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(TradeStage.EXIT_SENT, KernelEventKind.CANCEL_ACK, VenueEventStatus.CANCELED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(TradeStage.EXIT_REQUESTED, KernelEventKind.CANCEL_REJECT, VenueEventStatus.CANCELED_REJECTED, TradeStage.EXIT_REQUESTED, KernelDiagnosticCode.CANCEL_REJECTED),
(TradeStage.EXIT_SENT, KernelEventKind.CANCEL_REJECT, VenueEventStatus.CANCELED_REJECTED, TradeStage.EXIT_SENT, KernelDiagnosticCode.CANCEL_REJECTED),
(TradeStage.POSITION_OPEN, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED),
(TradeStage.EXIT_WORKING, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED),
(TradeStage.EXIT_REQUESTED, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED),
(TradeStage.EXIT_SENT, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED),
],
)
def test_kernel_exit_path_matrix(
initial_state: TradeStage,
event_kind: KernelEventKind,
event_status: VenueEventStatus,
expected_state: TradeStage,
expected_code: KernelDiagnosticCode,
) -> None:
kernel = mk_kernel()
slot = kernel.slot(0)
_configure_slot_state(slot, initial_state)
if event_kind in {KernelEventKind.ORDER_REJECT, KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
_seed_exit_order(slot, trade_id=slot.trade_id or "trade-1", asset="BTCUSDT", intended_size=slot.size or 0.5)
if initial_state in {TradeStage.EXIT_REQUESTED, TradeStage.EXIT_SENT, TradeStage.EXIT_WORKING} and event_kind in {
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
KernelEventKind.ORDER_ACK,
}:
_seed_exit_order(slot, trade_id=slot.trade_id or "trade-1", asset="BTCUSDT", intended_size=slot.size or 0.5)
outcome = kernel.on_venue_event(
mk_event(
kind=event_kind,
status=event_status,
trade_id=slot.trade_id or "trade-1",
venue_order_id=slot.active_exit_order.venue_order_id if slot.active_exit_order else "V-00000002",
venue_client_id=slot.active_exit_order.venue_client_id if slot.active_exit_order else "trade-1:exit",
side=TradeSide.SHORT,
asset="BTCUSDT",
size=float(slot.size or 0.5),
filled_size=float(slot.size or 0.5) if event_kind == KernelEventKind.FULL_FILL else float((slot.size or 0.5) / 2.0),
remaining_size=0.0,
)
)
assert outcome.diagnostic_code == expected_code
assert kernel.slot(0).fsm_state == expected_state
# 18 event-resolution tests
@pytest.mark.parametrize(
"event,initial_state,expected_state,expected_code",
[
(mk_event(kind=KernelEventKind.ORDER_ACK, status=VenueEventStatus.ACKED), TradeStage.IDLE, TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.ORDER_ACK, status=VenueEventStatus.ACKED), TradeStage.EXIT_REQUESTED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.ORDER_REJECT, status=VenueEventStatus.REJECTED), TradeStage.ENTRY_WORKING, TradeStage.IDLE, KernelDiagnosticCode.ENTRY_ORDER_REJECTED),
(mk_event(kind=KernelEventKind.ORDER_REJECT, status=VenueEventStatus.REJECTED), TradeStage.EXIT_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED),
(mk_event(kind=KernelEventKind.ORDER_REJECT, status=VenueEventStatus.REJECTED), TradeStage.IDLE, TradeStage.IDLE, KernelDiagnosticCode.ORDER_REJECTED),
(mk_event(kind=KernelEventKind.PARTIAL_FILL, status=VenueEventStatus.PARTIALLY_FILLED), TradeStage.ENTRY_WORKING, TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.FULL_FILL, status=VenueEventStatus.FILLED), TradeStage.ENTRY_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.PARTIAL_FILL, status=VenueEventStatus.PARTIALLY_FILLED), TradeStage.EXIT_WORKING, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.FULL_FILL, status=VenueEventStatus.FILLED), TradeStage.EXIT_WORKING, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.CANCEL_ACK, status=VenueEventStatus.CANCELED), TradeStage.EXIT_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.CANCEL_REJECT, status=VenueEventStatus.CANCELED_REJECTED), TradeStage.EXIT_WORKING, TradeStage.EXIT_WORKING, KernelDiagnosticCode.CANCEL_REJECTED),
(mk_event(kind=KernelEventKind.MARK_PRICE, status=VenueEventStatus.ACKED), TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.RECONCILE, status=VenueEventStatus.ACKED), TradeStage.POSITION_OPEN, TradeStage.STALE_STATE_RECONCILING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.ORDER_ACK, status=VenueEventStatus.ACKED, venue_order_id="V-2"), TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.ORDER_ACK, status=VenueEventStatus.ACKED, venue_order_id="V-3"), TradeStage.ENTRY_WORKING, TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.FULL_FILL, status=VenueEventStatus.FILLED, venue_order_id="V-4"), TradeStage.EXIT_WORKING, TradeStage.CLOSED, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.CANCEL_ACK, status=VenueEventStatus.CANCELED, venue_order_id="V-5"), TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
(mk_event(kind=KernelEventKind.CANCEL_REJECT, status=VenueEventStatus.CANCELED_REJECTED, venue_order_id="V-6"), TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.CANCEL_REJECTED),
],
)
def test_kernel_event_matrix(event: VenueEvent, initial_state: TradeStage, expected_state: TradeStage, expected_code: KernelDiagnosticCode) -> None:
kernel = mk_kernel()
slot = kernel.slot(0)
_configure_slot_state(slot, initial_state)
entry_states = {TradeStage.IDLE, TradeStage.ORDER_REQUESTED, TradeStage.ORDER_SENT, TradeStage.ENTRY_WORKING}
exit_states = {TradeStage.POSITION_OPEN, TradeStage.EXIT_REQUESTED, TradeStage.EXIT_SENT, TradeStage.EXIT_WORKING}
if initial_state in entry_states and event.kind in {KernelEventKind.ORDER_ACK, KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
_seed_entry_order(slot, trade_id="trade-1", asset="BTCUSDT")
elif initial_state == TradeStage.ENTRY_WORKING and event.kind == KernelEventKind.ORDER_REJECT:
_seed_entry_order(slot, trade_id="trade-1", asset="BTCUSDT")
if initial_state in exit_states:
if event.kind == KernelEventKind.ORDER_REJECT:
_seed_exit_order(slot, trade_id="trade-1", asset="BTCUSDT", intended_size=1.0)
elif event.kind in {KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
_seed_exit_order(slot, trade_id="trade-1", asset="BTCUSDT", intended_size=1.0)
elif initial_state in {TradeStage.EXIT_REQUESTED, TradeStage.EXIT_SENT, TradeStage.EXIT_WORKING} and event.kind in {
KernelEventKind.ORDER_ACK,
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
}:
_seed_exit_order(slot, trade_id="trade-1", asset="BTCUSDT", intended_size=1.0)
if initial_state == TradeStage.POSITION_OPEN and event.kind == KernelEventKind.ORDER_ACK:
slot.active_entry_order = None
fill_size = 1.0 if event.kind == KernelEventKind.FULL_FILL else 0.5 if event.kind == KernelEventKind.PARTIAL_FILL else 0.0
resolved_event = mk_event(
kind=event.kind,
status=event.status,
trade_id=event.trade_id,
slot_id=event.slot_id,
venue_order_id=slot.active_entry_order.venue_order_id if slot.active_entry_order else slot.active_exit_order.venue_order_id if slot.active_exit_order else event.venue_order_id,
venue_client_id=slot.active_entry_order.venue_client_id if slot.active_entry_order else slot.active_exit_order.venue_client_id if slot.active_exit_order else event.venue_client_id,
side=event.side,
asset=event.asset,
price=event.price,
size=1.0,
filled_size=fill_size,
remaining_size=max(0.0, 1.0 - fill_size),
reason=event.reason,
)
outcome = kernel.on_venue_event(resolved_event)
assert outcome.state == expected_state
assert outcome.diagnostic_code == expected_code
def test_kernel_rate_limited_event_is_characterized_without_state_drift() -> None:
kernel = mk_kernel()
slot = kernel.slot(0)
_configure_slot_state(slot, TradeStage.ENTRY_WORKING)
_seed_entry_order(slot, trade_id="trade-rate-limit", asset="BTCUSDT")
before = slot.to_dict()
outcome = kernel.on_venue_event(
mk_event(
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
trade_id="trade-rate-limit",
venue_order_id="V-RATE-LIMITED",
venue_client_id="trade-rate-limit:entry",
reason="code:100410 endpoint is in disabled/frequency-limited period",
size=1.0,
filled_size=0.0,
remaining_size=1.0,
)
)
after = kernel.slot(0).to_dict()
assert outcome.accepted is False
assert outcome.diagnostic_code == KernelDiagnosticCode.RATE_LIMITED
assert outcome.severity == KernelSeverity.WARNING
assert outcome.details["venue_event_kind"] == KernelEventKind.RATE_LIMITED.value
assert outcome.details["severity"] == KernelSeverity.WARNING.value
assert outcome.details["release_eta"] == "few minutes"
assert outcome.details["retryable"] is True
assert after["fsm_state"] == before["fsm_state"]
assert after["trade_id"] == before["trade_id"]
assert after["size"] == before["size"]
# 24 fuzz cases
@pytest.mark.parametrize("seed", list(range(24)))
def test_kernel_fuzz_event_sequences(seed: int) -> None:
rng = random.Random(seed)
kernel = mk_kernel(max_slots=4)
current_trade_id = f"trade-{seed}"
# Seed one slot open for exit/reconcile fuzzing.
seed_slot = kernel.slot(0)
_seed_open_slot(seed_slot, trade_id=current_trade_id)
seed_slot.exit_leg_ratios = (0.25, 0.25, 0.5)
kinds = [
KernelEventKind.ORDER_ACK,
KernelEventKind.ORDER_REJECT,
KernelEventKind.PARTIAL_FILL,
KernelEventKind.FULL_FILL,
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
KernelEventKind.MARK_PRICE,
KernelEventKind.RECONCILE,
]
for idx in range(12):
kind = rng.choice(kinds)
if kind in {KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_REJECT}:
seed_slot.active_entry_order = VenueOrder(
internal_trade_id=current_trade_id,
venue_order_id=f"V-{seed:04d}-{idx:02d}",
venue_client_id=f"{current_trade_id}:entry-{idx}",
side=TradeSide.SHORT,
intended_size=1.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": 0, "asset": "BTCUSDT"},
)
if kind in {KernelEventKind.CANCEL_ACK, KernelEventKind.CANCEL_REJECT, KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
seed_slot.active_exit_order = VenueOrder(
internal_trade_id=current_trade_id,
venue_order_id=f"V-{seed:04d}-{idx:02d}",
venue_client_id=f"{current_trade_id}:exit-{idx}",
side=TradeSide.SHORT,
intended_size=0.5,
filled_size=0.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": 0, "asset": "BTCUSDT"},
)
event = mk_event(kind=kind, status=_status_for_kind(kind), trade_id=current_trade_id, venue_order_id=f"V-{seed:04d}-{idx:02d}", venue_client_id=f"{current_trade_id}:{idx}")
outcome = kernel.on_venue_event(event)
assert isinstance(outcome, KernelOutcome)
assert outcome.diagnostic_code in set(KernelDiagnosticCode)
assert kernel.slot(0).fsm_state in set(TradeStage)
def _status_for_kind(kind: KernelEventKind) -> VenueEventStatus:
return {
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
}[kind]
# 22 explicit edge-condition tests
@pytest.mark.parametrize(
"slot_state,action,expected_code",
[
(TradeStage.IDLE, KernelCommandType.EXIT, KernelDiagnosticCode.NO_OPEN_POSITION),
(TradeStage.CLOSED, KernelCommandType.EXIT, KernelDiagnosticCode.NO_OPEN_POSITION),
(TradeStage.POSITION_OPEN, KernelCommandType.CANCEL, KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER),
(TradeStage.IDLE, KernelCommandType.CANCEL, KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER),
(TradeStage.IDLE, KernelCommandType.RECONCILE, KernelDiagnosticCode.STALE_STATE_RECONCILE),
(TradeStage.POSITION_OPEN, KernelCommandType.RECONCILE, KernelDiagnosticCode.STALE_STATE_RECONCILE),
(TradeStage.POSITION_OPEN, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.ENTRY_WORKING, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.ORDER_REQUESTED, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.ORDER_SENT, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.EXIT_REQUESTED, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.EXIT_SENT, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.STALE_STATE_RECONCILING, KernelCommandType.MARK_PRICE, KernelDiagnosticCode.OK),
(TradeStage.POSITION_OPEN, KernelCommandType.ENTER, KernelDiagnosticCode.SLOT_BUSY),
(TradeStage.EXIT_WORKING, KernelCommandType.ENTER, KernelDiagnosticCode.SLOT_BUSY),
(TradeStage.ORDER_REQUESTED, KernelCommandType.ENTER, KernelDiagnosticCode.SLOT_BUSY),
(TradeStage.ORDER_SENT, KernelCommandType.ENTER, KernelDiagnosticCode.SLOT_BUSY),
(TradeStage.POSITION_OPEN, KernelCommandType.EXIT, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelCommandType.EXIT, KernelDiagnosticCode.OK),
(TradeStage.POSITION_OPEN, KernelCommandType.CANCEL, KernelDiagnosticCode.OK),
(TradeStage.EXIT_WORKING, KernelCommandType.CANCEL, KernelDiagnosticCode.OK),
],
)
def test_kernel_action_edge_conditions(slot_state: TradeStage, action: KernelCommandType, expected_code: KernelDiagnosticCode) -> None:
kernel = mk_kernel(venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)))
slot = kernel.slot(0)
_configure_slot_state(slot, slot_state)
if action == KernelCommandType.ENTER and expected_code == KernelDiagnosticCode.SLOT_BUSY:
slot.trade_id = f"occupied-{slot_state.value.lower()}"
if action == KernelCommandType.CANCEL and expected_code == KernelDiagnosticCode.OK:
_seed_exit_order(slot, trade_id=slot.trade_id or "trade-1", asset=slot.asset or "BTCUSDT", intended_size=0.5)
outcome = kernel.process_intent(mk_intent(action=action, target_size=0.5, exit_leg_ratios=(0.25, 0.25, 0.5)))
assert outcome.diagnostic_code == expected_code
# 20 transition-detail tests
@pytest.mark.parametrize("mode", [KernelMode.NORMAL, KernelMode.DEBUG])
@pytest.mark.parametrize("verbosity", [KernelVerbosity.QUIET, KernelVerbosity.TRACE])
@pytest.mark.parametrize("control_enabled", [True, False])
@pytest.mark.parametrize("closed", [True, False])
@pytest.mark.parametrize("state", [TradeStage.IDLE, TradeStage.POSITION_OPEN])
def test_transition_details_and_control_modes_are_captured(
mode: KernelMode,
verbosity: KernelVerbosity,
control_enabled: bool,
closed: bool,
state: TradeStage,
) -> None:
kernel = mk_kernel()
if control_enabled:
kernel.update_control(
ControlUpdate(
mode=mode,
verbosity=verbosity,
trace_transitions=True,
)
)
slot = kernel.slot(0)
_seed_open_slot(slot)
slot.fsm_state = state
slot.closed = closed
event = mk_event(kind=KernelEventKind.MARK_PRICE, status=VenueEventStatus.ACKED)
outcome = kernel.on_venue_event(event)
assert outcome.transitions
transition = outcome.transitions[0]
assert transition.control_mode in {KernelMode.NORMAL.value, KernelMode.DEBUG.value}
assert transition.control_verbosity in {KernelVerbosity.QUIET.value, KernelVerbosity.TRACE.value}
assert "asset" in transition.details
assert "side" in transition.details

View File

@@ -1,903 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import random
import pytest
from prod.clean_arch.dita_v2 import (
AccountProjection,
BackendMode,
ControlUpdate,
ExecutionKernel,
HazelcastProjection,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelOutcome,
KernelVerbosity,
MemoryKernelJournal,
MockVenueAdapter,
MockVenueScenario,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
@dataclass(frozen=True)
class KernelRig:
kernel: ExecutionKernel
journal: MemoryKernelJournal
zinc: InMemoryZincPlane
projection: HazelcastProjection
sink: "CaptureSink"
@dataclass(frozen=True)
class EntryCase:
name: str
scenario: MockVenueScenario
expected_state: TradeStage
expected_size: float
rejected: bool = False
@dataclass(frozen=True)
class ExitCase:
name: str
exit_leg_ratios: tuple[float, ...]
fill_ratio: float
expected_state: TradeStage
expected_size: float
expected_leg_index: int
@dataclass(frozen=True)
class EventCase:
name: str
kind: KernelEventKind
initial_state: TradeStage
expected_state: TradeStage
expected_code: KernelDiagnosticCode
family: str
@dataclass(frozen=True)
class ReconcileCase:
name: str
slots: tuple[TradeSlot, ...]
expected_open_positions: int
expected_trade_ids: tuple[str, ...]
class CaptureSink:
def __init__(self) -> None:
self.rows: list[tuple[str, dict[str, object]]] = []
def __call__(self, name: str, row: dict[str, object]) -> None:
self.rows.append((name, dict(row)))
def _build_kernel(
*,
venue: MockVenueAdapter | None = None,
mode: KernelMode = KernelMode.DEBUG,
verbosity: KernelVerbosity = KernelVerbosity.TRACE,
backend_mode: BackendMode = BackendMode.MOCK,
trace_transitions: bool = True,
) -> KernelRig:
sink = CaptureSink()
journal = MemoryKernelJournal()
zinc = InMemoryZincPlane()
projection = HazelcastProjection(writer=sink)
control_plane = InMemoryControlPlane(
KernelControlSnapshot(
mode=mode,
verbosity=verbosity,
backend_mode=backend_mode,
trace_transitions=trace_transitions,
debug_clickhouse_enabled=True,
mirror_to_hazelcast=True,
)
)
kernel = ExecutionKernel(
max_slots=4,
control_plane=control_plane,
venue=venue or MockVenueAdapter(),
journal=journal,
account=AccountProjection(),
projection=projection,
zinc_plane=zinc,
)
return KernelRig(kernel=kernel, journal=journal, zinc=zinc, projection=projection, sink=sink)
def _seed_entry_working(slot: TradeSlot, *, trade_id: str = "trade-1", asset: str = "BTCUSDT") -> None:
slot.trade_id = trade_id
slot.asset = asset
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.size = 1.0
slot.initial_size = 1.0
slot.leverage = 2.0
slot.closed = False
slot.exit_leg_ratios = (1.0,)
slot.active_leg_index = 0
slot.fsm_state = TradeStage.ENTRY_WORKING
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-ENTRY-1",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=1.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
slot.active_exit_order = None
def _seed_position_open(
slot: TradeSlot,
*,
trade_id: str = "trade-1",
asset: str = "BTCUSDT",
exit_leg_ratios: tuple[float, ...] = (1.0,),
) -> None:
_seed_entry_working(slot, trade_id=trade_id, asset=asset)
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-ENTRY-1",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=1.0,
filled_size=1.0,
average_fill_price=100.0,
status=VenueOrderStatus.FILLED,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
slot.fsm_state = TradeStage.POSITION_OPEN
slot.size = 1.0
slot.initial_size = 1.0
slot.exit_leg_ratios = tuple(exit_leg_ratios)
slot.active_leg_index = 0
def _seed_exit_working(
slot: TradeSlot,
*,
trade_id: str = "trade-1",
asset: str = "BTCUSDT",
exit_leg_ratios: tuple[float, ...] = (1.0,),
active_leg_index: int = 0,
) -> None:
_seed_position_open(slot, trade_id=trade_id, asset=asset, exit_leg_ratios=exit_leg_ratios)
slot.fsm_state = TradeStage.EXIT_WORKING
slot.active_leg_index = active_leg_index
slot.active_exit_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-EXIT-1",
venue_client_id=f"{trade_id}:exit",
side=TradeSide.SHORT,
intended_size=max(0.0, 1.0 * float(exit_leg_ratios[active_leg_index if active_leg_index < len(exit_leg_ratios) else 0])),
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
def _seed_idle(slot: TradeSlot) -> None:
slot.trade_id = ""
slot.asset = ""
slot.side = TradeSide.FLAT
slot.entry_price = 0.0
slot.size = 0.0
slot.initial_size = 0.0
slot.leverage = 0.0
slot.entry_time = None
slot.unrealized_pnl = 0.0
slot.realized_pnl = 0.0
slot.closed = False
slot.exit_leg_ratios = (1.0,)
slot.active_leg_index = 0
slot.active_exit_order = None
slot.active_entry_order = None
slot.fsm_state = TradeStage.IDLE
slot.close_reason = ""
slot.last_event_time = None
slot.metadata = {}
def _seed_closed(slot: TradeSlot, *, trade_id: str = "trade-1", asset: str = "BTCUSDT") -> None:
slot.trade_id = trade_id
slot.asset = asset
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.size = 0.0
slot.initial_size = 1.0
slot.leverage = 2.0
slot.closed = True
slot.exit_leg_ratios = (1.0,)
slot.active_leg_index = 1
slot.active_exit_order = None
slot.active_entry_order = None
slot.fsm_state = TradeStage.CLOSED
slot.close_reason = "EXIT_FILLED"
def _make_event(
*,
kind: KernelEventKind,
status: VenueEventStatus,
trade_id: str = "trade-1",
slot_id: int = 0,
venue_order_id: str = "V-ORDER-1",
venue_client_id: str = "trade-1:client-1",
side: TradeSide = TradeSide.SHORT,
asset: str = "BTCUSDT",
price: float = 100.0,
size: float = 1.0,
filled_size: float = 1.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"evt-{kind.value.lower()}-{slot_id}-{trade_id}",
trade_id=trade_id,
slot_id=slot_id,
kind=kind,
status=status,
venue_order_id=venue_order_id,
venue_client_id=venue_client_id,
side=side,
asset=asset,
price=price,
size=size,
filled_size=filled_size,
remaining_size=remaining_size,
reason=reason,
raw_payload={"status": status.value, "kind": kind.value},
)
ENTRY_CASES = [
EntryCase(
name="full_fill_immediate",
scenario=MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0),
expected_state=TradeStage.POSITION_OPEN,
expected_size=1.0,
),
EntryCase(
name="partial_50_immediate",
scenario=MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.5),
expected_state=TradeStage.ENTRY_WORKING,
expected_size=0.5,
),
EntryCase(
name="partial_50_ack_then_fill",
scenario=MockVenueScenario(emit_fill_on_submit=False, partial_fill_ratio=0.5),
expected_state=TradeStage.ENTRY_WORKING,
expected_size=0.5,
),
EntryCase(
name="no_fill_ack_only",
scenario=MockVenueScenario(emit_fill_on_submit=False, partial_fill_ratio=0.0),
expected_state=TradeStage.ENTRY_WORKING,
expected_size=0.0,
),
EntryCase(
name="ack_before_fill_full",
scenario=MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=True, partial_fill_ratio=1.0),
expected_state=TradeStage.POSITION_OPEN,
expected_size=1.0,
),
EntryCase(
name="ack_before_fill_partial",
scenario=MockVenueScenario(emit_ack_before_fill=False, emit_fill_on_submit=True, partial_fill_ratio=0.25),
expected_state=TradeStage.ENTRY_WORKING,
expected_size=0.25,
),
EntryCase(
name="reject_entry",
scenario=MockVenueScenario(reject_entries=True),
expected_state=TradeStage.IDLE,
expected_size=0.0,
rejected=True,
),
EntryCase(
name="three_quarters_fill",
scenario=MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.75),
expected_state=TradeStage.ENTRY_WORKING,
expected_size=0.75,
),
]
EXIT_CASES = [
ExitCase(name="single_leg_full", exit_leg_ratios=(1.0,), fill_ratio=1.0, expected_state=TradeStage.CLOSED, expected_size=0.0, expected_leg_index=1),
ExitCase(name="single_leg_partial", exit_leg_ratios=(1.0,), fill_ratio=0.5, expected_state=TradeStage.EXIT_WORKING, expected_size=0.5, expected_leg_index=0),
ExitCase(name="two_leg_full", exit_leg_ratios=(0.5, 0.5), fill_ratio=1.0, expected_state=TradeStage.POSITION_OPEN, expected_size=0.5, expected_leg_index=1),
ExitCase(name="two_leg_partial", exit_leg_ratios=(0.5, 0.5), fill_ratio=0.25, expected_state=TradeStage.EXIT_WORKING, expected_size=0.875, expected_leg_index=0),
ExitCase(name="three_leg_full", exit_leg_ratios=(0.25, 0.25, 0.5), fill_ratio=1.0, expected_state=TradeStage.POSITION_OPEN, expected_size=0.75, expected_leg_index=1),
ExitCase(name="three_leg_partial", exit_leg_ratios=(0.25, 0.25, 0.5), fill_ratio=0.5, expected_state=TradeStage.EXIT_WORKING, expected_size=0.875, expected_leg_index=0),
ExitCase(name="tilted_full", exit_leg_ratios=(0.2, 0.3, 0.5), fill_ratio=1.0, expected_state=TradeStage.POSITION_OPEN, expected_size=0.8, expected_leg_index=1),
ExitCase(name="tilted_partial", exit_leg_ratios=(0.2, 0.3, 0.5), fill_ratio=0.25, expected_state=TradeStage.EXIT_WORKING, expected_size=0.95, expected_leg_index=0),
ExitCase(name="four_leg_full", exit_leg_ratios=(0.1, 0.2, 0.3, 0.4), fill_ratio=1.0, expected_state=TradeStage.POSITION_OPEN, expected_size=0.9, expected_leg_index=1),
ExitCase(name="four_leg_partial", exit_leg_ratios=(0.1, 0.2, 0.3, 0.4), fill_ratio=0.5, expected_state=TradeStage.EXIT_WORKING, expected_size=0.95, expected_leg_index=0),
ExitCase(name="balanced_full", exit_leg_ratios=(0.33, 0.33, 0.34), fill_ratio=1.0, expected_state=TradeStage.POSITION_OPEN, expected_size=0.67, expected_leg_index=1),
ExitCase(name="balanced_partial", exit_leg_ratios=(0.33, 0.33, 0.34), fill_ratio=0.25, expected_state=TradeStage.EXIT_WORKING, expected_size=0.9175, expected_leg_index=0),
]
EVENT_CASES = [
EventCase("ack_entry", KernelEventKind.ORDER_ACK, TradeStage.ENTRY_WORKING, TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, "entry"),
EventCase("ack_exit", KernelEventKind.ORDER_ACK, TradeStage.EXIT_REQUESTED, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK, "exit"),
EventCase("reject_entry", KernelEventKind.ORDER_REJECT, TradeStage.ENTRY_WORKING, TradeStage.IDLE, KernelDiagnosticCode.ENTRY_ORDER_REJECTED, "entry"),
EventCase("reject_exit", KernelEventKind.ORDER_REJECT, TradeStage.EXIT_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.EXIT_ORDER_REJECTED, "exit"),
EventCase("reject_idle", KernelEventKind.ORDER_REJECT, TradeStage.IDLE, TradeStage.IDLE, KernelDiagnosticCode.ORDER_REJECTED, "none"),
EventCase("partial_entry", KernelEventKind.PARTIAL_FILL, TradeStage.ENTRY_WORKING, TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK, "entry"),
EventCase("full_entry", KernelEventKind.FULL_FILL, TradeStage.ENTRY_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, "entry"),
EventCase("partial_exit", KernelEventKind.PARTIAL_FILL, TradeStage.EXIT_WORKING, TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK, "exit"),
EventCase("full_exit", KernelEventKind.FULL_FILL, TradeStage.EXIT_WORKING, TradeStage.CLOSED, KernelDiagnosticCode.OK, "exit"),
EventCase("cancel_ack_exit", KernelEventKind.CANCEL_ACK, TradeStage.EXIT_WORKING, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, "exit"),
EventCase("cancel_reject_exit", KernelEventKind.CANCEL_REJECT, TradeStage.EXIT_WORKING, TradeStage.EXIT_WORKING, KernelDiagnosticCode.CANCEL_REJECTED, "exit"),
EventCase("mark_price_open", KernelEventKind.MARK_PRICE, TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, "none"),
EventCase("reconcile_open", KernelEventKind.RECONCILE, TradeStage.POSITION_OPEN, TradeStage.STALE_STATE_RECONCILING, KernelDiagnosticCode.OK, "none"),
EventCase("ack_open_no_entry", KernelEventKind.ORDER_ACK, TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, "none"),
EventCase("cancel_ack_open_no_exit", KernelEventKind.CANCEL_ACK, TradeStage.POSITION_OPEN, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK, "none"),
]
RECONCILE_CASES = [
ReconcileCase(
name="empty_payload",
slots=(),
expected_open_positions=0,
expected_trade_ids=(),
),
ReconcileCase(
name="single_open",
slots=(
TradeSlot(
slot_id=0,
trade_id="trade-a",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
),
),
expected_open_positions=1,
expected_trade_ids=("trade-a",),
),
ReconcileCase(
name="open_and_exit",
slots=(
TradeSlot(
slot_id=0,
trade_id="trade-b",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=0.5,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.EXIT_WORKING,
),
TradeSlot(
slot_id=1,
trade_id="trade-c",
asset="ETHUSDT",
side=TradeSide.LONG,
entry_price=50.0,
size=0.0,
initial_size=1.0,
leverage=3.0,
closed=True,
fsm_state=TradeStage.CLOSED,
),
),
expected_open_positions=1,
expected_trade_ids=("trade-b", "trade-c"),
),
ReconcileCase(
name="mixed_three",
slots=(
TradeSlot(
slot_id=0,
trade_id="trade-d",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
),
TradeSlot(
slot_id=1,
trade_id="trade-e",
asset="ETHUSDT",
side=TradeSide.LONG,
entry_price=50.0,
size=0.0,
initial_size=1.0,
leverage=3.0,
closed=True,
fsm_state=TradeStage.CLOSED,
),
TradeSlot(
slot_id=2,
trade_id="trade-f",
asset="SOLUSDT",
side=TradeSide.SHORT,
entry_price=20.0,
size=0.25,
initial_size=1.0,
leverage=4.0,
fsm_state=TradeStage.EXIT_WORKING,
),
),
expected_open_positions=2,
expected_trade_ids=("trade-d", "trade-e", "trade-f"),
),
]
def _event_order_id(case: EventCase, resolver: str) -> str:
if case.family == "entry":
return "V-ENTRY-1"
if case.family == "exit":
return "V-EXIT-1"
if resolver == "order_id":
return "V-MISSING"
return "V-ORDER-1"
def _event_client_id(case: EventCase, resolver: str) -> str:
if case.family == "entry":
return "trade-1:entry"
if case.family == "exit":
return "trade-1:exit"
if resolver == "order_id":
return "trade-x:missing"
return "trade-1:client-1"
@pytest.mark.parametrize("mode", [KernelMode.NORMAL, KernelMode.DEBUG])
@pytest.mark.parametrize("verbosity", [KernelVerbosity.QUIET, KernelVerbosity.VERBOSE, KernelVerbosity.TRACE])
@pytest.mark.parametrize("backend_mode", [BackendMode.MOCK, BackendMode.BINGX])
@pytest.mark.parametrize("trace_transitions", [True, False])
def test_kernel_control_plane_matrix(
mode: KernelMode,
verbosity: KernelVerbosity,
backend_mode: BackendMode,
trace_transitions: bool,
) -> None:
rig = _build_kernel()
snapshot = rig.kernel.update_control(
ControlUpdate(
mode=mode,
verbosity=verbosity,
backend_mode=backend_mode,
trace_transitions=trace_transitions,
)
)
assert snapshot.mode == mode
assert snapshot.verbosity == verbosity
assert snapshot.backend_mode == backend_mode
assert snapshot.trace_transitions == trace_transitions
assert rig.kernel.control.mode == mode
assert rig.kernel.control.verbosity == verbosity
assert rig.zinc.read_control().mode == mode
assert rig.zinc.read_control().verbosity == verbosity
assert rig.projection.control_snapshot is not None
assert rig.projection.control_snapshot.mode == mode
assert rig.sink.rows[-1][0] == "hz:dita_control"
assert rig.sink.rows[-1][1]["mode"] == mode.value
assert rig.sink.rows[-1][1]["backend_mode"] == backend_mode.value
@pytest.mark.parametrize("mode", [KernelMode.NORMAL, KernelMode.DEBUG])
@pytest.mark.parametrize("verbosity", [KernelVerbosity.QUIET, KernelVerbosity.TRACE])
@pytest.mark.parametrize("case", ENTRY_CASES, ids=[case.name for case in ENTRY_CASES])
def test_kernel_entry_matrix(
mode: KernelMode,
verbosity: KernelVerbosity,
case: EntryCase,
) -> None:
rig = _build_kernel(
venue=MockVenueAdapter(case.scenario),
mode=mode,
verbosity=verbosity,
backend_mode=BackendMode.MOCK,
)
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id="intent-entry",
trade_id="trade-1",
slot_id=0,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.ENTER,
reference_price=100.0,
target_size=1.0,
leverage=2.0,
exit_leg_ratios=(1.0,),
reason=case.name,
)
)
slot = rig.kernel.slot(0)
assert outcome.accepted is True
assert outcome.diagnostic_code == KernelDiagnosticCode.OK
assert slot.fsm_state == case.expected_state
assert slot.size == pytest.approx(case.expected_size, abs=1e-6)
assert rig.zinc.intent_region[-1].action == KernelCommandType.ENTER
assert rig.zinc.state_region[0].fsm_state == case.expected_state
assert rig.journal.rows
if case.rejected:
assert slot.trade_id == ""
assert slot.asset == ""
assert slot.active_entry_order is None
assert slot.active_exit_order is None
if case.expected_state == TradeStage.POSITION_OPEN:
assert slot.closed is False
@pytest.mark.parametrize("case", EXIT_CASES, ids=[case.name for case in EXIT_CASES])
def test_kernel_exit_matrix(case: ExitCase) -> None:
rig = _build_kernel(venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=case.fill_ratio)))
slot = rig.kernel.slot(0)
_seed_position_open(slot, exit_leg_ratios=case.exit_leg_ratios)
slot.active_entry_order = None
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id="intent-exit",
trade_id=slot.trade_id,
slot_id=0,
asset=slot.asset,
side=slot.side,
action=KernelCommandType.EXIT,
reference_price=99.0,
target_size=case.exit_leg_ratios[0],
leverage=slot.leverage,
exit_leg_ratios=case.exit_leg_ratios,
reason=case.name,
)
)
assert outcome.accepted is True
assert outcome.diagnostic_code == KernelDiagnosticCode.OK
assert slot.fsm_state == case.expected_state
assert slot.active_leg_index == case.expected_leg_index
assert slot.size == pytest.approx(case.expected_size, abs=1e-6)
if case.expected_state == TradeStage.CLOSED:
assert slot.closed is True
assert slot.active_exit_order is None
assert slot.active_entry_order is None
else:
assert slot.closed is False
assert slot.active_exit_order is None or slot.active_exit_order.status in {
VenueOrderStatus.PARTIALLY_FILLED,
VenueOrderStatus.NEW,
}
@pytest.mark.parametrize("resolver", ["slot_id", "trade_id", "order_id"])
@pytest.mark.parametrize("case", EVENT_CASES, ids=[case.name for case in EVENT_CASES])
def test_kernel_event_resolution_matrix(case: EventCase, resolver: str) -> None:
rig = _build_kernel()
slot = rig.kernel.slot(0)
if case.family == "entry":
_seed_entry_working(slot)
elif case.family == "exit":
_seed_exit_working(slot)
elif case.initial_state == TradeStage.POSITION_OPEN:
_seed_position_open(slot)
elif case.initial_state == TradeStage.IDLE:
_seed_idle(slot)
if resolver == "slot_id":
event_slot_id = 0
event_trade_id = "mismatch-trade"
event_venue_order_id = "V-MISMATCH"
elif resolver == "trade_id":
event_slot_id = 99
event_trade_id = slot.trade_id or "trade-1"
event_venue_order_id = "V-MISMATCH"
else:
event_slot_id = 99
event_trade_id = "mismatch-trade"
event_venue_order_id = "V-ENTRY-1" if case.family == "entry" else "V-EXIT-1"
if case.kind == KernelEventKind.ORDER_ACK and case.family == "none":
slot.active_entry_order = None
slot.active_exit_order = None
if case.kind == KernelEventKind.CANCEL_ACK and case.family == "none":
slot.active_exit_order = None
if case.kind == KernelEventKind.ORDER_REJECT and case.family == "exit":
slot.active_entry_order = None
if case.kind in {KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_REJECT, KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL} and case.family == "entry" and resolver != "order_id":
event_venue_order_id = slot.active_entry_order.venue_order_id
event_trade_id = slot.trade_id
if case.kind in {KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_REJECT, KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL, KernelEventKind.CANCEL_ACK, KernelEventKind.CANCEL_REJECT} and case.family == "exit" and resolver != "order_id":
event_venue_order_id = slot.active_exit_order.venue_order_id
event_trade_id = slot.trade_id
price = 98.0 if case.kind == KernelEventKind.MARK_PRICE else 100.0
filled_size = 1.0 if case.kind in {KernelEventKind.FULL_FILL, KernelEventKind.ORDER_ACK} else 0.5
remaining_size = max(0.0, 1.0 - filled_size)
event = _make_event(
kind=case.kind,
status={
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
}[case.kind],
trade_id=event_trade_id,
slot_id=event_slot_id,
venue_order_id=event_venue_order_id,
venue_client_id=_event_client_id(case, resolver),
side=TradeSide.SHORT,
asset="BTCUSDT",
price=price,
size=1.0,
filled_size=filled_size,
remaining_size=remaining_size,
)
outcome = rig.kernel.on_venue_event(event)
assert outcome.state == case.expected_state
assert outcome.diagnostic_code == case.expected_code
if case.kind == KernelEventKind.MARK_PRICE:
assert slot.unrealized_pnl > 0.0
if case.kind == KernelEventKind.ORDER_REJECT and case.family == "entry":
assert slot.trade_id == ""
assert slot.asset == ""
assert slot.size == 0.0
if case.kind == KernelEventKind.ORDER_REJECT and case.family == "exit":
assert slot.fsm_state == TradeStage.POSITION_OPEN
assert slot.active_exit_order is None
if case.kind == KernelEventKind.FULL_FILL and case.family == "entry":
assert slot.fsm_state == TradeStage.POSITION_OPEN
if case.kind == KernelEventKind.FULL_FILL and case.family == "exit" and case.expected_state == TradeStage.CLOSED:
assert slot.closed is True
@pytest.mark.parametrize("case", RECONCILE_CASES, ids=[case.name for case in RECONCILE_CASES])
@pytest.mark.parametrize("mode", [KernelMode.NORMAL, KernelMode.DEBUG])
@pytest.mark.parametrize("verbosity", [KernelVerbosity.QUIET, KernelVerbosity.TRACE])
def test_kernel_reconcile_snapshot_matrix(
case: ReconcileCase,
mode: KernelMode,
verbosity: KernelVerbosity,
) -> None:
rig = _build_kernel(mode=mode, verbosity=verbosity)
outcome = rig.kernel.reconcile_from_slots(case.slots)
assert outcome.accepted is True
assert outcome.diagnostic_code == KernelDiagnosticCode.RECONCILED
assert rig.kernel.snapshot()["account"]["open_positions"] == case.expected_open_positions
assert tuple(slot.trade_id for slot in rig.kernel.state.slots if slot.trade_id) == case.expected_trade_ids
assert len(rig.zinc.read_slots()) == len(rig.kernel.state.slots)
assert any(name == "hz:dita_active_slots" for name, _ in rig.sink.rows)
assert rig.projection.control_snapshot is not None
@pytest.mark.parametrize("seed", list(range(24)))
def test_kernel_fuzz_transition_matrix(seed: int) -> None:
rng = random.Random(seed)
rig = _build_kernel(
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=0.5)),
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
)
for step in range(20):
slot_id = rng.randrange(0, len(rig.kernel.state.slots))
slot = rig.kernel.slot(slot_id)
op = rng.choice(["enter", "exit", "cancel", "mark", "reconcile", "control", "event"])
if op == "enter":
trade_id = f"trade-{seed}-{step}"
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{seed}-{step}-enter",
trade_id=trade_id,
slot_id=slot_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.ENTER,
reference_price=100.0 + rng.random(),
target_size=1.0,
leverage=2.0,
exit_leg_ratios=(0.5, 0.5),
reason="fuzz-enter",
)
)
assert outcome.diagnostic_code in set(KernelDiagnosticCode)
elif op == "exit":
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{seed}-{step}-exit",
trade_id=slot.trade_id or f"trade-{seed}-{step}",
slot_id=slot_id,
asset=slot.asset or "BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.EXIT,
reference_price=99.0 + rng.random(),
target_size=max(0.1, slot.size or 0.1),
leverage=slot.leverage or 2.0,
exit_leg_ratios=slot.exit_leg_ratios or (1.0,),
reason="fuzz-exit",
)
)
assert outcome.diagnostic_code in {
KernelDiagnosticCode.OK,
KernelDiagnosticCode.NO_OPEN_POSITION,
}
elif op == "cancel":
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{seed}-{step}-cancel",
trade_id=slot.trade_id or f"trade-{seed}-{step}",
slot_id=slot_id,
asset=slot.asset or "BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.CANCEL,
reference_price=99.0,
target_size=max(0.1, slot.size or 0.1),
leverage=slot.leverage or 2.0,
exit_leg_ratios=slot.exit_leg_ratios or (1.0,),
reason="fuzz-cancel",
)
)
assert outcome.diagnostic_code in {
KernelDiagnosticCode.OK,
KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER,
}
elif op == "mark":
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{seed}-{step}-mark",
trade_id=slot.trade_id or f"trade-{seed}-{step}",
slot_id=slot_id,
asset=slot.asset or "BTCUSDT",
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
action=KernelCommandType.MARK_PRICE,
reference_price=95.0 + rng.random() * 10.0,
target_size=max(0.1, slot.size or 0.1),
leverage=slot.leverage or 2.0,
exit_leg_ratios=slot.exit_leg_ratios or (1.0,),
reason="fuzz-mark",
)
)
assert outcome.diagnostic_code == KernelDiagnosticCode.OK
elif op == "reconcile":
outcome = rig.kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"intent-{seed}-{step}-reconcile",
trade_id=slot.trade_id or f"trade-{seed}-{step}",
slot_id=slot_id,
asset=slot.asset or "BTCUSDT",
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
action=KernelCommandType.RECONCILE,
reference_price=100.0,
target_size=max(0.1, slot.size or 0.1),
leverage=slot.leverage or 2.0,
exit_leg_ratios=slot.exit_leg_ratios or (1.0,),
reason="fuzz-reconcile",
)
)
assert outcome.diagnostic_code == KernelDiagnosticCode.STALE_STATE_RECONCILE
elif op == "control":
rig.kernel.update_control(
ControlUpdate(
mode=KernelMode.DEBUG if rng.random() < 0.5 else KernelMode.NORMAL,
verbosity=rng.choice([KernelVerbosity.QUIET, KernelVerbosity.VERBOSE, KernelVerbosity.TRACE]),
backend_mode=rng.choice([BackendMode.MOCK, BackendMode.BINGX]),
trace_transitions=rng.random() < 0.5,
)
)
elif op == "event":
current = rig.kernel.slot(slot_id)
if current.active_exit_order is not None:
kind = rng.choice(
[
KernelEventKind.PARTIAL_FILL,
KernelEventKind.FULL_FILL,
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
KernelEventKind.ORDER_REJECT,
]
)
elif current.active_entry_order is not None:
kind = rng.choice(
[
KernelEventKind.ORDER_ACK,
KernelEventKind.PARTIAL_FILL,
KernelEventKind.FULL_FILL,
KernelEventKind.ORDER_REJECT,
]
)
else:
kind = rng.choice(
[
KernelEventKind.ORDER_REJECT,
KernelEventKind.MARK_PRICE,
KernelEventKind.RECONCILE,
]
)
status = {
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
}[kind]
venue_order_id = "V-FUZZ"
venue_client_id = f"fuzz:{seed}:{step}"
if current.active_entry_order is not None:
venue_order_id = current.active_entry_order.venue_order_id
venue_client_id = current.active_entry_order.venue_client_id
elif current.active_exit_order is not None:
venue_order_id = current.active_exit_order.venue_order_id
venue_client_id = current.active_exit_order.venue_client_id
outcome = rig.kernel.on_venue_event(
_make_event(
kind=kind,
status=status,
trade_id=current.trade_id or f"trade-{seed}-{step}",
slot_id=slot_id if rng.random() < 0.5 else 99,
venue_order_id=venue_order_id,
venue_client_id=venue_client_id,
side=current.side if current.side != TradeSide.FLAT else TradeSide.SHORT,
asset=current.asset or "BTCUSDT",
price=98.0 if kind == KernelEventKind.MARK_PRICE else 100.0,
size=max(0.1, current.size or 0.1),
filled_size=max(0.1, current.size or 0.1),
remaining_size=0.0,
)
)
assert isinstance(outcome, KernelOutcome)
assert outcome.diagnostic_code in set(KernelDiagnosticCode)
assert slot.fsm_state in set(TradeStage)
assert slot.size >= 0.0
assert slot.initial_size >= 0.0
assert slot.active_leg_index >= 0
if slot.closed:
assert slot.size == pytest.approx(0.0, abs=1e-9)
if slot.fsm_state == TradeStage.IDLE:
assert slot.size == pytest.approx(0.0, abs=1e-9)

View File

@@ -1,494 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import random
import pytest
from prod.clean_arch.dita_v2 import (
AccountProjection,
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelControlSnapshot,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelVerbosity,
MemoryKernelJournal,
TradeSide,
TradeSlot,
TradeStage,
VenueAdapter,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class NoopVenueAdapter:
"""Venue stub that never emits events."""
def submit(self, intent: KernelIntent): # type: ignore[override]
return []
def cancel(self, order: VenueOrder, *, reason: str = ""): # type: ignore[override]
return []
def open_orders(self): # type: ignore[override]
return []
def open_positions(self): # type: ignore[override]
return []
def reconcile(self): # type: ignore[override]
return []
@dataclass(frozen=True)
class RecoveryCase:
name: str
seed: int
slot_count: int
trade_count: int
@dataclass(frozen=True)
class DuplicateCase:
name: str
initial_state: TradeStage
kind: KernelEventKind
family: str
expected_state: TradeStage
expected_code: KernelDiagnosticCode
@dataclass(frozen=True)
class OutOfOrderCase:
name: str
seed: int
@dataclass(frozen=True)
class ReplayCase:
name: str
seed: int
control_mode: KernelMode
verbosity: KernelVerbosity
@dataclass(frozen=True)
class ControlCase:
name: str
mode: KernelMode
verbosity: KernelVerbosity
backend_mode: BackendMode
trace_transitions: bool
def _build_kernel(slot_count: int = 4) -> tuple[ExecutionKernel, MemoryKernelJournal, InMemoryZincPlane]:
journal = MemoryKernelJournal()
zinc = InMemoryZincPlane()
kernel = ExecutionKernel(
max_slots=slot_count,
control_plane=InMemoryControlPlane(
KernelControlSnapshot(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.MOCK,
debug_clickhouse_enabled=True,
trace_transitions=True,
mirror_to_hazelcast=True,
)
),
venue=NoopVenueAdapter(),
journal=journal,
account=AccountProjection(),
zinc_plane=zinc,
)
return kernel, journal, zinc
def _enter_open(kernel: ExecutionKernel, *, trade_id: str, slot_id: int = 0, size: float = 1.0) -> None:
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:enter",
trade_id=trade_id,
slot_id=slot_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.ENTER,
reference_price=100.0,
target_size=size,
leverage=2.0,
exit_leg_ratios=(1.0,),
reason="enter",
)
)
slot = kernel.slot(slot_id)
ack = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"{trade_id}:ack",
trade_id=trade_id,
slot_id=slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=slot.active_entry_order.venue_order_id if slot.active_entry_order else "",
venue_client_id=slot.active_entry_order.venue_client_id if slot.active_entry_order else "",
side=TradeSide.SHORT,
asset="BTCUSDT",
price=100.0,
size=size,
filled_size=size,
remaining_size=0.0,
)
kernel.on_venue_event(ack)
def _seed_exit_working(kernel: ExecutionKernel, *, trade_id: str, slot_id: int = 0, exit_ratio: tuple[float, ...] = (1.0,)) -> None:
_enter_open(kernel, trade_id=trade_id, slot_id=slot_id, size=1.0)
slot = kernel.slot(slot_id)
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:exit",
trade_id=trade_id,
slot_id=slot_id,
asset=slot.asset,
side=slot.side,
action=KernelCommandType.EXIT,
reference_price=99.0,
target_size=exit_ratio[0],
leverage=slot.leverage,
exit_leg_ratios=exit_ratio,
reason="exit",
)
)
def _seed_exit_only_slot(
slot: TradeSlot,
*,
trade_id: str,
state: TradeStage,
asset: str = "BTCUSDT",
) -> None:
slot.trade_id = trade_id
slot.asset = asset
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.initial_size = 1.0
slot.size = 1.0 if state != TradeStage.CLOSED else 0.0
slot.leverage = 2.0
slot.closed = state == TradeStage.CLOSED
slot.exit_leg_ratios = (1.0,)
slot.active_leg_index = 0
slot.active_entry_order = None
slot.active_exit_order = None if state == TradeStage.CLOSED else VenueOrder(
internal_trade_id=trade_id,
venue_order_id="V-EXIT-1",
venue_client_id=f"{trade_id}:exit",
side=TradeSide.SHORT,
intended_size=1.0,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id, "asset": asset},
)
slot.fsm_state = state
def _fill_event(slot: TradeSlot, *, kind: KernelEventKind, filled_size: float, trade_id: str | None = None) -> VenueEvent:
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"evt-{kind.value.lower()}-{slot.slot_id}",
trade_id=trade_id or slot.trade_id,
slot_id=slot.slot_id,
kind=kind,
status={
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
}[kind],
venue_order_id=slot.active_entry_order.venue_order_id if slot.active_entry_order else slot.active_exit_order.venue_order_id if slot.active_exit_order else "V-ORDER",
venue_client_id=slot.active_entry_order.venue_client_id if slot.active_entry_order else slot.active_exit_order.venue_client_id if slot.active_exit_order else "trade:client",
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
asset=slot.asset or "BTCUSDT",
price=98.0 if kind == KernelEventKind.MARK_PRICE else 100.0,
size=max(1.0, slot.size or 1.0),
filled_size=filled_size,
remaining_size=max(0.0, max(1.0, slot.size or 1.0) - filled_size),
)
RECOVERY_CASES = [
RecoveryCase("idle_only", seed=1, slot_count=4, trade_count=1),
RecoveryCase("one_open", seed=2, slot_count=4, trade_count=1),
RecoveryCase("mixed_two", seed=3, slot_count=4, trade_count=2),
RecoveryCase("mixed_three", seed=4, slot_count=4, trade_count=3),
RecoveryCase("all_open", seed=5, slot_count=4, trade_count=4),
RecoveryCase("open_and_closed", seed=6, slot_count=5, trade_count=4),
RecoveryCase("exit_working", seed=7, slot_count=4, trade_count=2),
RecoveryCase("position_open_with_gap", seed=8, slot_count=4, trade_count=3),
]
DUPLICATE_CASES = [
DuplicateCase("ack_entry_duplicate_regressed", TradeStage.POSITION_OPEN, KernelEventKind.ORDER_ACK, "entry", TradeStage.POSITION_OPEN, KernelDiagnosticCode.DUPLICATE_EVENT),
DuplicateCase("ack_exit_duplicate_hold", TradeStage.POSITION_OPEN, KernelEventKind.ORDER_ACK, "exit", TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
DuplicateCase("partial_entry_duplicate_stays", TradeStage.ENTRY_WORKING, KernelEventKind.PARTIAL_FILL, "entry", TradeStage.ENTRY_WORKING, KernelDiagnosticCode.OK),
DuplicateCase("full_entry_duplicate_noop", TradeStage.POSITION_OPEN, KernelEventKind.FULL_FILL, "entry", TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
DuplicateCase("partial_exit_duplicate_stays", TradeStage.EXIT_WORKING, KernelEventKind.PARTIAL_FILL, "exit", TradeStage.EXIT_WORKING, KernelDiagnosticCode.OK),
DuplicateCase("full_exit_duplicate_closes", TradeStage.CLOSED, KernelEventKind.FULL_FILL, "exit", TradeStage.CLOSED, KernelDiagnosticCode.OK),
DuplicateCase("cancel_ack_duplicate_open", TradeStage.POSITION_OPEN, KernelEventKind.CANCEL_ACK, "exit", TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
DuplicateCase("cancel_reject_duplicate_exit", TradeStage.EXIT_WORKING, KernelEventKind.CANCEL_REJECT, "exit", TradeStage.EXIT_WORKING, KernelDiagnosticCode.CANCEL_REJECTED),
]
OUT_OF_ORDER_CASES = [OutOfOrderCase(f"seed_{seed}", seed=seed) for seed in range(12)]
REPLAY_CASES = [
ReplayCase(f"replay_{seed}", seed=seed, control_mode=KernelMode.DEBUG if seed % 2 == 0 else KernelMode.NORMAL, verbosity=KernelVerbosity.TRACE if seed % 3 == 0 else KernelVerbosity.VERBOSE)
for seed in range(12)
]
CONTROL_CASES = [
ControlCase("normal_quiet_mock", KernelMode.NORMAL, KernelVerbosity.QUIET, BackendMode.MOCK, False),
ControlCase("normal_trace_mock", KernelMode.NORMAL, KernelVerbosity.TRACE, BackendMode.MOCK, True),
ControlCase("debug_trace_mock", KernelMode.DEBUG, KernelVerbosity.TRACE, BackendMode.MOCK, True),
ControlCase("debug_verbose_bingx", KernelMode.DEBUG, KernelVerbosity.VERBOSE, BackendMode.BINGX, False),
ControlCase("normal_verbose_bingx", KernelMode.NORMAL, KernelVerbosity.VERBOSE, BackendMode.BINGX, True),
ControlCase("debug_quiet_bingx", KernelMode.DEBUG, KernelVerbosity.QUIET, BackendMode.BINGX, False),
]
@pytest.mark.parametrize("case", CONTROL_CASES, ids=[case.name for case in CONTROL_CASES])
def test_kernel_zinc_control_plane_mirror(case: ControlCase) -> None:
kernel, journal, zinc = _build_kernel()
snapshot = kernel.update_control(
ControlUpdate(
mode=case.mode,
verbosity=case.verbosity,
backend_mode=case.backend_mode,
trace_transitions=case.trace_transitions,
)
)
assert snapshot.mode == case.mode
assert snapshot.verbosity == case.verbosity
assert snapshot.backend_mode == case.backend_mode
assert snapshot.trace_transitions == case.trace_transitions
assert zinc.read_control().mode == case.mode
assert zinc.read_control().verbosity == case.verbosity
assert journal.rows == []
assert kernel.zinc_plane.read_control().mode == case.mode
@pytest.mark.parametrize("case", RECOVERY_CASES, ids=[case.name for case in RECOVERY_CASES])
def test_kernel_zinc_restart_recovery_matrix(case: RecoveryCase) -> None:
kernel, _, zinc = _build_kernel(slot_count=case.slot_count)
rng = random.Random(case.seed)
for idx in range(case.trade_count):
slot_id = idx % case.slot_count
_enter_open(kernel, trade_id=f"{case.name}-{idx}", slot_id=slot_id, size=1.0)
if rng.random() < 0.5:
_seed_exit_working(kernel, trade_id=f"{case.name}-{idx}", slot_id=slot_id, exit_ratio=(0.5, 0.5))
if rng.random() < 0.5:
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{case.name}-{idx}:mark",
trade_id=f"{case.name}-{idx}",
slot_id=slot_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.MARK_PRICE,
reference_price=98.0,
target_size=1.0,
leverage=2.0,
exit_leg_ratios=(1.0,),
reason="mark",
)
)
snapshot_slots = zinc.read_slots()
assert len(snapshot_slots) == case.trade_count
restarted, _, restarted_zinc = _build_kernel(slot_count=case.slot_count)
outcome = restarted.reconcile_from_slots(snapshot_slots)
assert outcome.accepted is True
assert outcome.diagnostic_code == KernelDiagnosticCode.RECONCILED
assert restarted.snapshot()["slots"] == kernel.snapshot()["slots"]
assert [slot.to_dict() for slot in restarted_zinc.read_slots()] == restarted.snapshot()["slots"]
assert restarted.account.snapshot.open_positions == kernel.account.snapshot.open_positions
@pytest.mark.parametrize("case", DUPLICATE_CASES, ids=[case.name for case in DUPLICATE_CASES])
def test_kernel_duplicate_event_idempotence_matrix(case: DuplicateCase) -> None:
kernel, journal, zinc = _build_kernel()
slot = kernel.slot(0)
filled_size = 1.0 if case.kind == KernelEventKind.FULL_FILL else 0.25
if case.family == "entry":
_enter_open(kernel, trade_id="dup-entry", slot_id=0, size=1.0)
if case.kind == KernelEventKind.ORDER_ACK and case.initial_state == TradeStage.POSITION_OPEN:
slot.active_entry_order = VenueOrder(
internal_trade_id="dup-entry",
venue_order_id="V-ENTRY-1",
venue_client_id="dup-entry:entry",
side=TradeSide.SHORT,
intended_size=1.0,
filled_size=1.0,
average_fill_price=100.0,
status=VenueOrderStatus.FILLED,
metadata={"slot_id": 0, "asset": "BTCUSDT"},
)
slot.fsm_state = TradeStage.POSITION_OPEN
else:
_seed_exit_only_slot(slot, trade_id="dup-exit", state=case.initial_state)
before = slot.to_dict()
event = _fill_event(
slot,
kind=case.kind,
filled_size=filled_size,
trade_id=slot.trade_id,
)
outcome_1 = kernel.on_venue_event(event)
state_after_first = slot.fsm_state
size_after_first = slot.size
outcome_2 = kernel.on_venue_event(event)
assert outcome_1.diagnostic_code in set(KernelDiagnosticCode)
assert outcome_2.diagnostic_code in set(KernelDiagnosticCode)
assert slot.fsm_state == case.expected_state
assert slot.fsm_state == state_after_first
assert slot.size == pytest.approx(size_after_first, abs=1e-9)
assert slot.size >= 0.0
assert zinc.state_region[0].fsm_state == slot.fsm_state
assert len(journal.rows) >= 1
assert before["slot_id"] == slot.slot_id
if case.expected_code == KernelDiagnosticCode.DUPLICATE_EVENT:
assert outcome_2.diagnostic_code == KernelDiagnosticCode.DUPLICATE_EVENT
@pytest.mark.parametrize("case", OUT_OF_ORDER_CASES, ids=[case.name for case in OUT_OF_ORDER_CASES])
def test_kernel_out_of_order_venue_event_matrix(case: OutOfOrderCase) -> None:
kernel, journal, zinc = _build_kernel()
rng = random.Random(case.seed)
if rng.random() < 0.5:
_enter_open(kernel, trade_id=f"ooo-{case.seed}", slot_id=0, size=1.0)
else:
_seed_exit_working(kernel, trade_id=f"ooo-{case.seed}", slot_id=0, exit_ratio=(0.5, 0.5))
slot = kernel.slot(0)
sequence = [
KernelEventKind.FULL_FILL,
KernelEventKind.ORDER_ACK,
KernelEventKind.PARTIAL_FILL,
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
KernelEventKind.ORDER_REJECT,
KernelEventKind.MARK_PRICE,
]
rng.shuffle(sequence)
for idx, kind in enumerate(sequence):
event = _fill_event(
slot,
kind=kind,
filled_size=1.0 if kind == KernelEventKind.FULL_FILL else 0.5,
trade_id=slot.trade_id,
)
event = VenueEvent(
**{
**event.__dict__,
"event_id": f"ooo-{case.seed}-{idx}",
"slot_id": 0 if rng.random() < 0.5 else 99,
"venue_order_id": slot.active_entry_order.venue_order_id if slot.active_entry_order else slot.active_exit_order.venue_order_id if slot.active_exit_order else event.venue_order_id,
"venue_client_id": slot.active_entry_order.venue_client_id if slot.active_entry_order else slot.active_exit_order.venue_client_id if slot.active_exit_order else event.venue_client_id,
}
)
outcome = kernel.on_venue_event(event)
assert isinstance(outcome.diagnostic_code, KernelDiagnosticCode)
assert slot.size >= 0.0
assert slot.initial_size >= 0.0
assert slot.fsm_state in set(TradeStage)
if slot.closed:
assert slot.size == pytest.approx(0.0, abs=1e-9)
assert len(journal.rows) >= len(sequence)
assert len(zinc.state_region) >= 1
@pytest.mark.parametrize("case", REPLAY_CASES, ids=[case.name for case in REPLAY_CASES])
def test_kernel_debug_journal_replay_matrix(case: ReplayCase) -> None:
kernel, journal, zinc = _build_kernel()
kernel.update_control(
ControlUpdate(
mode=case.control_mode,
verbosity=case.verbosity,
trace_transitions=True,
debug_clickhouse_enabled=True,
)
)
rng = random.Random(case.seed)
for idx in range(10):
slot_id = idx % 2
trade_id = f"{case.name}-{idx}"
if idx % 3 == 0:
_enter_open(kernel, trade_id=trade_id, slot_id=slot_id, size=1.0)
elif idx % 3 == 1 and kernel.slot(slot_id).is_open():
_seed_exit_working(kernel, trade_id=kernel.slot(slot_id).trade_id, slot_id=slot_id, exit_ratio=(0.5, 0.5))
else:
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:mark",
trade_id=trade_id,
slot_id=slot_id,
asset="BTCUSDT",
side=TradeSide.SHORT,
action=KernelCommandType.MARK_PRICE,
reference_price=97.0 + rng.random(),
target_size=1.0,
leverage=2.0,
exit_leg_ratios=(1.0,),
reason="journal-mark",
)
)
rows = list(journal.rows)
assert rows
for row in rows:
slot_state = row["slot_state"]
assert row["prev_state"] != ""
assert row["next_state"] != ""
assert slot_state["fsm_state"] == row["next_state"]
assert row["control_mode"] in {KernelMode.NORMAL.value, KernelMode.DEBUG.value}
assert row["control_verbosity"] in {KernelVerbosity.QUIET.value, KernelVerbosity.VERBOSE.value, KernelVerbosity.TRACE.value}
replayed, _, replayed_zinc = _build_kernel()
replayed.update_control(
ControlUpdate(mode=case.control_mode, verbosity=case.verbosity, trace_transitions=True, debug_clickhouse_enabled=True)
)
replayed.reconcile_from_slots(zinc.read_slots())
assert replayed.snapshot()["slots"] == kernel.snapshot()["slots"]
assert len(replayed_zinc.read_slots()) == len(replayed.snapshot()["slots"])
assert [slot.slot_id for slot in replayed_zinc.read_slots()] == [slot.slot_id for slot in replayed.state.slots]

View File

@@ -1,437 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import random
import pytest
from prod.clean_arch.dita_v2 import (
BackendMode,
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
InMemoryZincPlane,
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelMode,
KernelControlSnapshot,
KernelVerbosity,
MemoryKernelJournal,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class NoopVenueAdapter:
def submit(self, intent: KernelIntent): # type: ignore[override]
return []
def cancel(self, order: VenueOrder, *, reason: str = ""): # type: ignore[override]
return []
def open_orders(self): # type: ignore[override]
return []
def open_positions(self): # type: ignore[override]
return []
def reconcile(self): # type: ignore[override]
return []
@dataclass(frozen=True)
class RaceCase:
name: str
seed_state: str
first_kind: KernelEventKind
second_kind: KernelEventKind
expected_state: TradeStage
expected_code_2: KernelDiagnosticCode
@dataclass(frozen=True)
class OffByOneCase:
name: str
exit_leg_ratios: tuple[float, ...]
fills: tuple[float, ...]
expected_leg_index: int
expected_closed: bool
@dataclass(frozen=True)
class MemoryCase:
name: str
max_slots: int
write_slot_ids: tuple[int, ...]
reconcile_slot_ids: tuple[int, ...]
expected_written_count: int
def _build_kernel(slot_count: int = 4) -> tuple[ExecutionKernel, MemoryKernelJournal, InMemoryZincPlane]:
journal = MemoryKernelJournal()
zinc = InMemoryZincPlane()
kernel = ExecutionKernel(
max_slots=slot_count,
control_plane=InMemoryControlPlane(
KernelControlSnapshot(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.MOCK,
trace_transitions=True,
debug_clickhouse_enabled=True,
mirror_to_hazelcast=True,
)
),
venue=NoopVenueAdapter(),
journal=journal,
zinc_plane=zinc,
)
return kernel, journal, zinc
def _seed_entry_working(kernel: ExecutionKernel, *, trade_id: str, slot_id: int = 0, size: float = 1.0) -> None:
slot = kernel.slot(slot_id)
slot.trade_id = trade_id
slot.asset = "BTCUSDT"
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.size = 0.0
slot.initial_size = 0.0
slot.leverage = 2.0
slot.entry_time = datetime.now(timezone.utc)
slot.exit_leg_ratios = (1.0,)
slot.active_leg_index = 0
slot.closed = False
slot.close_reason = ""
slot.active_exit_order = None
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"V-ENTRY-{slot_id}",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot_id},
)
slot.fsm_state = TradeStage.ENTRY_WORKING
def _seed_position_open(kernel: ExecutionKernel, *, trade_id: str, slot_id: int = 0, size: float = 1.0) -> None:
_seed_entry_working(kernel, trade_id=trade_id, slot_id=slot_id, size=size)
slot = kernel.slot(slot_id)
slot.size = size
slot.initial_size = size
slot.entry_price = 100.0
slot.active_entry_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"V-ENTRY-{slot_id}",
venue_client_id=f"{trade_id}:entry",
side=TradeSide.SHORT,
intended_size=size,
filled_size=size,
average_fill_price=100.0,
status=VenueOrderStatus.FILLED,
metadata={"slot_id": slot_id},
)
slot.fsm_state = TradeStage.POSITION_OPEN
def _seed_exit_working(kernel: ExecutionKernel, *, trade_id: str, slot_id: int = 0, exit_leg_ratios: tuple[float, ...] = (1.0,)) -> None:
_seed_position_open(kernel, trade_id=trade_id, slot_id=slot_id, size=1.0)
slot = kernel.slot(slot_id)
slot.exit_leg_ratios = exit_leg_ratios
slot.active_exit_order = VenueOrder(
internal_trade_id=trade_id,
venue_order_id=f"V-EXIT-{slot_id}",
venue_client_id=f"{trade_id}:exit",
side=TradeSide.SHORT,
intended_size=slot.next_exit_ratio() * slot.initial_size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot_id},
)
slot.fsm_state = TradeStage.EXIT_WORKING
def _make_event(
slot: TradeSlot,
*,
kind: KernelEventKind,
event_id: str,
filled_size: float,
slot_id: int | None = None,
venue_order_id: str | None = None,
venue_client_id: str | None = None,
reason: str = "",
) -> VenueEvent:
order_id = venue_order_id or (
slot.active_exit_order.venue_order_id if slot.active_exit_order else slot.active_entry_order.venue_order_id if slot.active_entry_order else "V-ORDER"
)
client_id = venue_client_id or (
slot.active_exit_order.venue_client_id if slot.active_exit_order else slot.active_entry_order.venue_client_id if slot.active_entry_order else "trade:client"
)
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=event_id,
trade_id=slot.trade_id,
slot_id=slot.slot_id if slot_id is None else slot_id,
kind=kind,
status={
KernelEventKind.ORDER_ACK: VenueEventStatus.ACKED,
KernelEventKind.ORDER_REJECT: VenueEventStatus.REJECTED,
KernelEventKind.PARTIAL_FILL: VenueEventStatus.PARTIALLY_FILLED,
KernelEventKind.FULL_FILL: VenueEventStatus.FILLED,
KernelEventKind.CANCEL_ACK: VenueEventStatus.CANCELED,
KernelEventKind.CANCEL_REJECT: VenueEventStatus.CANCELED_REJECTED,
KernelEventKind.MARK_PRICE: VenueEventStatus.ACKED,
KernelEventKind.RECONCILE: VenueEventStatus.ACKED,
}[kind],
venue_order_id=order_id,
venue_client_id=client_id,
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
asset=slot.asset or "BTCUSDT",
price=99.0 if kind == KernelEventKind.MARK_PRICE else 100.0,
size=max(1.0, slot.size or 1.0),
filled_size=filled_size,
remaining_size=max(0.0, max(1.0, slot.size or 1.0) - filled_size),
reason=reason,
)
RACE_CASES = [
RaceCase("entry_ack_then_fullfill", "entry_working", KernelEventKind.ORDER_ACK, KernelEventKind.FULL_FILL, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
RaceCase("entry_fullfill_then_ack", "entry_working", KernelEventKind.FULL_FILL, KernelEventKind.ORDER_ACK, TradeStage.POSITION_OPEN, KernelDiagnosticCode.DUPLICATE_EVENT),
RaceCase("entry_ack_then_reject", "entry_working", KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_REJECT, TradeStage.IDLE, KernelDiagnosticCode.ENTRY_ORDER_REJECTED),
RaceCase("entry_reject_then_ack", "entry_working", KernelEventKind.ORDER_REJECT, KernelEventKind.ORDER_ACK, TradeStage.IDLE, KernelDiagnosticCode.OK),
RaceCase("entry_mark_then_fullfill", "entry_working", KernelEventKind.MARK_PRICE, KernelEventKind.FULL_FILL, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
RaceCase("entry_reconcile_then_ack", "entry_working", KernelEventKind.RECONCILE, KernelEventKind.ORDER_ACK, TradeStage.STALE_STATE_RECONCILING, KernelDiagnosticCode.STALE_STATE_RECONCILE),
RaceCase("exit_ack_then_fullfill", "exit_working", KernelEventKind.ORDER_ACK, KernelEventKind.FULL_FILL, TradeStage.CLOSED, KernelDiagnosticCode.OK),
RaceCase("exit_fullfill_then_ack", "exit_working", KernelEventKind.FULL_FILL, KernelEventKind.ORDER_ACK, TradeStage.CLOSED, KernelDiagnosticCode.OK),
RaceCase("exit_cancel_ack_then_fullfill", "exit_working", KernelEventKind.CANCEL_ACK, KernelEventKind.FULL_FILL, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
RaceCase("exit_fullfill_then_cancel_ack", "exit_working", KernelEventKind.FULL_FILL, KernelEventKind.CANCEL_ACK, TradeStage.CLOSED, KernelDiagnosticCode.OK),
RaceCase("exit_cancel_reject_then_ack", "exit_working", KernelEventKind.CANCEL_REJECT, KernelEventKind.CANCEL_ACK, TradeStage.POSITION_OPEN, KernelDiagnosticCode.OK),
RaceCase("exit_mark_then_fullfill", "exit_working", KernelEventKind.MARK_PRICE, KernelEventKind.FULL_FILL, TradeStage.CLOSED, KernelDiagnosticCode.OK),
]
OFF_BY_ONE_CASES = [
OffByOneCase("ratios_empty", (), (), 0, False),
OffByOneCase("ratios_one", (1.0,), (1.0,), 1, True),
OffByOneCase("ratios_two_equal", (0.5, 0.5), (0.5, 0.5), 2, True),
OffByOneCase("ratios_three_tail", (0.25, 0.25, 0.5), (0.25, 0.25, 0.5), 3, True),
OffByOneCase("ratios_three_front_loaded", (0.6, 0.3, 0.1), (0.6, 0.3, 0.1), 3, True),
OffByOneCase("ratios_four_small", (0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4), 4, True),
]
MEMORY_CASES = [
MemoryCase("sparse_write_order", 5, (3, 1, 4), (3, 1, 4), 3),
MemoryCase("overwrite_same_slot", 4, (2, 2, 2), (2,), 1),
MemoryCase("capacity_trim", 3, (0, 1, 2, 3, 4), (0, 1, 2), 3),
MemoryCase("single_slot_reconcile", 2, (1,), (1,), 1),
MemoryCase("mixed_holes", 6, (5, 0, 3), (5, 0, 3), 3),
MemoryCase("late_slot_overwrite", 4, (1, 3, 1), (1, 3), 2),
]
@pytest.mark.parametrize("case", RACE_CASES, ids=[case.name for case in RACE_CASES])
def test_kernel_race_and_reorder_matrix(case: RaceCase) -> None:
kernel, journal, zinc = _build_kernel()
if case.seed_state == "entry_working":
_seed_entry_working(kernel, trade_id=f"race-{case.name}")
elif case.seed_state == "exit_working":
_seed_exit_working(kernel, trade_id=f"race-{case.name}")
else:
_seed_position_open(kernel, trade_id=f"race-{case.name}")
slot = kernel.slot(0)
first = _make_event(
slot,
kind=case.first_kind,
event_id=f"{case.name}-first",
filled_size=1.0 if case.first_kind == KernelEventKind.FULL_FILL else 0.5,
)
second = _make_event(
slot,
kind=case.second_kind,
event_id=f"{case.name}-second",
filled_size=1.0 if case.second_kind == KernelEventKind.FULL_FILL else 0.5,
)
outcome_1 = kernel.on_venue_event(first)
outcome_2 = kernel.on_venue_event(second)
assert outcome_1.diagnostic_code in set(KernelDiagnosticCode)
assert outcome_2.diagnostic_code in set(KernelDiagnosticCode)
assert slot.size >= 0.0
assert slot.initial_size >= 0.0
assert slot.fsm_state == case.expected_state
assert outcome_2.diagnostic_code == case.expected_code_2
assert zinc.state_region[0].fsm_state == slot.fsm_state
assert len(journal.rows) >= 2
@pytest.mark.parametrize("case", OFF_BY_ONE_CASES, ids=[case.name for case in OFF_BY_ONE_CASES])
def test_kernel_exit_leg_off_by_one_matrix(case: OffByOneCase) -> None:
kernel, journal, zinc = _build_kernel()
_seed_exit_working(kernel, trade_id=f"obo-{case.name}", exit_leg_ratios=case.exit_leg_ratios)
slot = kernel.slot(0)
assert slot.next_exit_ratio() == pytest.approx(case.exit_leg_ratios[0] if case.exit_leg_ratios else 1.0, abs=1e-9)
for idx, fill_size in enumerate(case.fills):
event = _make_event(
slot,
kind=KernelEventKind.FULL_FILL,
event_id=f"{case.name}-fill-{idx}",
filled_size=fill_size,
reason=f"leg-{idx}",
)
outcome = kernel.on_venue_event(event)
assert outcome.accepted is True
assert slot.size >= 0.0
assert slot.active_leg_index <= max(len(case.exit_leg_ratios), 1)
if idx < len(case.fills) - 1:
assert slot.fsm_state == TradeStage.POSITION_OPEN
rearm = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{case.name}-rearm-{idx}",
trade_id=slot.trade_id,
slot_id=slot.slot_id,
asset=slot.asset,
side=slot.side,
action=KernelCommandType.EXIT,
reference_price=100.0,
target_size=slot.next_exit_ratio(),
leverage=slot.leverage,
exit_leg_ratios=case.exit_leg_ratios,
reason=f"rearm-{idx}",
)
)
assert rearm.accepted is True
assert slot.fsm_state == TradeStage.EXIT_REQUESTED
else:
assert slot.fsm_state in {TradeStage.POSITION_OPEN, TradeStage.CLOSED}
assert slot.active_leg_index == case.expected_leg_index
assert slot.closed is case.expected_closed
if case.fills:
assert zinc.state_region[0].active_leg_index == slot.active_leg_index
else:
assert zinc.state_region[0].trade_id == slot.trade_id
assert zinc.state_region[0].fsm_state == TradeStage.EXIT_WORKING
assert len(journal.rows) >= len(case.fills)
assert slot.next_exit_ratio() == pytest.approx(1.0, abs=1e-9) if case.expected_closed else slot.next_exit_ratio() <= 1.0
@pytest.mark.parametrize("case", MEMORY_CASES, ids=[case.name for case in MEMORY_CASES])
def test_kernel_zinc_memory_anomaly_matrix(case: MemoryCase) -> None:
kernel, journal, zinc = _build_kernel(slot_count=case.max_slots)
rng = random.Random(hash(case.name) & 0xFFFFFFFF)
for idx, slot_id in enumerate(case.write_slot_ids):
slot = kernel.slot(slot_id % case.max_slots)
slot.trade_id = f"{case.name}-{idx}"
slot.asset = "BTCUSDT"
slot.side = TradeSide.SHORT
slot.entry_price = 100.0
slot.size = float(idx + 1)
slot.initial_size = float(idx + 1)
slot.leverage = 2.0
slot.fsm_state = TradeStage.POSITION_OPEN if idx % 2 == 0 else TradeStage.EXIT_WORKING
slot.active_entry_order = VenueOrder(
internal_trade_id=slot.trade_id,
venue_order_id=f"V-ENTRY-{slot_id}-{idx}",
venue_client_id=f"{slot.trade_id}:entry",
side=TradeSide.SHORT,
intended_size=slot.size,
filled_size=slot.size,
average_fill_price=100.0,
status=VenueOrderStatus.FILLED,
metadata={"slot_id": slot.slot_id},
)
if slot.fsm_state == TradeStage.EXIT_WORKING:
slot.active_exit_order = VenueOrder(
internal_trade_id=slot.trade_id,
venue_order_id=f"V-EXIT-{slot_id}-{idx}",
venue_client_id=f"{slot.trade_id}:exit",
side=TradeSide.SHORT,
intended_size=max(0.1, slot.size / 2.0),
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.NEW,
metadata={"slot_id": slot.slot_id},
)
kernel.zinc_plane.write_slot(slot)
written = zinc.read_slots()
assert len(written) == case.expected_written_count
assert [slot.slot_id for slot in written] == sorted(set(slot_id % case.max_slots for slot_id in case.write_slot_ids))
# Shuffle a snapshot and reconcile it back into a fresh kernel to exercise
# sparse, duplicate and truncated memory layouts without venue involvement.
shuffled_snapshot = list(reversed(written))
if rng.random() < 0.5:
shuffled_snapshot.append(shuffled_snapshot[0])
restarted, restarted_journal, restarted_zinc = _build_kernel(slot_count=case.max_slots)
outcome = restarted.reconcile_from_slots(shuffled_snapshot)
assert outcome.accepted is True
assert outcome.diagnostic_code == KernelDiagnosticCode.RECONCILED
assert len(restarted_zinc.read_slots()) == case.max_slots
assert restarted.snapshot()["slots"] == [slot.to_dict() for slot in restarted_zinc.read_slots()]
assert len(restarted_journal.rows) == 0
# Feed a stale-state event against a reconstructed slot to ensure the kernel
# stays stable even when the memory image is awkward.
target_slot_id = restarted_zinc.read_slots()[0].slot_id
slot = restarted.slot(target_slot_id)
stale_intent = restarted.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{case.name}:stale",
trade_id=slot.trade_id,
slot_id=slot.slot_id,
asset=slot.asset,
side=slot.side,
action=KernelCommandType.RECONCILE,
reference_price=100.0,
target_size=max(1.0, slot.size or 1.0),
leverage=max(1.0, slot.leverage or 1.0),
exit_leg_ratios=slot.exit_leg_ratios,
reason="stale-reconcile",
)
)
assert stale_intent.diagnostic_code == KernelDiagnosticCode.STALE_STATE_RECONCILE
assert slot.fsm_state == TradeStage.STALE_STATE_RECONCILING
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"{case.name}-reconcile",
trade_id=slot.trade_id,
slot_id=slot.slot_id,
kind=KernelEventKind.RECONCILE,
status=VenueEventStatus.ACKED,
venue_order_id=slot.active_exit_order.venue_order_id if slot.active_exit_order else slot.active_entry_order.venue_order_id if slot.active_entry_order else "V-ORDER",
venue_client_id=slot.active_exit_order.venue_client_id if slot.active_exit_order else slot.active_entry_order.venue_client_id if slot.active_entry_order else "V-CLIENT",
side=slot.side if slot.side != TradeSide.FLAT else TradeSide.SHORT,
asset=slot.asset or "BTCUSDT",
price=100.0,
size=max(1.0, slot.size or 1.0),
filled_size=0.0,
remaining_size=max(0.0, slot.size),
)
stale = restarted.on_venue_event(event)
assert stale.diagnostic_code == KernelDiagnosticCode.STALE_STATE_RECONCILE
assert restarted.slot(target_slot_id).fsm_state == TradeStage.STALE_STATE_RECONCILING

View File

@@ -1,126 +0,0 @@
from __future__ import annotations
from uuid import uuid4
import os
import unittest
from unittest.mock import patch
from prod.clean_arch.dita_v2 import (
DITAv2LauncherBundle,
LauncherVenueMode,
LauncherZincMode,
KernelControlSnapshot,
MockVenueAdapter,
build_launcher_bundle,
)
from prod.bingx.enums import BingxEnvironment
from prod.clean_arch.dita_v2.launcher import _maybe_close
from prod.clean_arch.dita_v2.launcher import build_bingx_exec_client_config
class DummyCloseable:
def __init__(self) -> None:
self.closed = False
def close(self) -> None:
self.closed = True
class DummyControlPlane:
def __init__(self) -> None:
self.snapshot = KernelControlSnapshot()
def read(self) -> KernelControlSnapshot:
return self.snapshot
def close(self) -> None:
pass
class DummyZincPlane:
def __init__(self) -> None:
self.control_updates: list[KernelControlSnapshot] = []
self.slot_writes: list[object] = []
def update_control(self, snapshot: KernelControlSnapshot) -> None:
self.control_updates.append(snapshot)
def write_slot(self, slot: object) -> None:
self.slot_writes.append(slot)
class TestDITAv2Launcher(unittest.TestCase):
def test_build_launcher_bundle_defaults_to_mock_and_in_memory(self) -> None:
bundle = build_launcher_bundle(prefix=f"dita_v2_{uuid4().hex}")
try:
self.assertIsInstance(bundle, DITAv2LauncherBundle)
self.assertIsInstance(bundle.venue, MockVenueAdapter)
self.assertEqual(bundle.kernel.max_slots, 10)
self.assertEqual(bundle.kernel.control.mode.value, "NORMAL")
finally:
bundle.close()
def test_build_launcher_bundle_can_select_real_components_via_env(self) -> None:
prefix = f"dita_v2_{uuid4().hex}"
dummy_control = DummyControlPlane()
dummy_zinc = DummyZincPlane()
with patch("prod.clean_arch.dita_v2.launcher.build_control_plane", return_value=dummy_control), patch(
"prod.clean_arch.dita_v2.launcher._build_zinc_plane", return_value=dummy_zinc
):
bundle = build_launcher_bundle(
prefix=prefix,
venue_mode=LauncherVenueMode.BINGX,
zinc_mode=LauncherZincMode.REAL,
bingx_backend=object(),
)
try:
self.assertIs(bundle.control_plane, dummy_control)
self.assertIs(bundle.zinc_plane, dummy_zinc)
self.assertEqual(bundle.venue.__class__.__name__, "BingxVenueAdapter")
finally:
bundle.close()
def test_build_launcher_bundle_respects_explicit_modes(self) -> None:
prefix = f"dita_v2_{uuid4().hex}"
bundle = build_launcher_bundle(
prefix=prefix,
venue_mode=LauncherVenueMode.MOCK,
zinc_mode=LauncherZincMode.IN_MEMORY,
)
try:
self.assertIsInstance(bundle.venue, MockVenueAdapter)
self.assertEqual(bundle.kernel.max_slots, 10)
finally:
bundle.close()
def test_bingx_exec_client_config_uses_standard_testnet_credentials(self) -> None:
with patch.dict(
os.environ,
{
"BINGX_API_KEY": "test-api-key",
"BINGX_SECRET_KEY": "test-secret-key",
"DOLPHIN_BINGX_ENV": "VST",
"DOLPHIN_BINGX_ALLOW_MAINNET": "0",
"DOLPHIN_BINGX_RECV_WINDOW_MS": "60000",
"DOLPHIN_BINGX_DEFAULT_LEVERAGE": "1",
"DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP": "3",
},
clear=False,
):
cfg = build_bingx_exec_client_config()
self.assertEqual(cfg.api_key, "test-api-key")
self.assertEqual(cfg.secret_key, "test-secret-key")
self.assertIs(cfg.environment, BingxEnvironment.VST)
self.assertFalse(cfg.allow_mainnet)
self.assertEqual(cfg.recv_window_ms, 60000)
self.assertEqual(cfg.default_leverage, 1)
self.assertEqual(cfg.exchange_leverage_cap, 3)
def test_maybe_close_handles_closeable_objects(self) -> None:
dummy = DummyCloseable()
_maybe_close(dummy)
self.assertTrue(dummy.closed)
if __name__ == "__main__":
unittest.main()

View File

@@ -1,820 +0,0 @@
from __future__ import annotations
import asyncio
import os
import re
import time
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from decimal import Decimal
from pathlib import Path
from typing import Any
import pytest
from dotenv import load_dotenv
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from prod.bingx.schemas import BingxContract
from prod.clean_arch.dita_v2 import BackendMode
from prod.clean_arch.dita_v2 import BingxVenueAdapter
from prod.clean_arch.dita_v2 import ControlUpdate
from prod.clean_arch.dita_v2 import ExecutionKernel
from prod.clean_arch.dita_v2 import KernelCommandType
from prod.clean_arch.dita_v2 import KernelDiagnosticCode
from prod.clean_arch.dita_v2 import KernelEventKind
from prod.clean_arch.dita_v2 import KernelIntent
from prod.clean_arch.dita_v2 import KernelMode
from prod.clean_arch.dita_v2 import KernelVerbosity
from prod.clean_arch.dita_v2 import RealZincControlPlane
from prod.clean_arch.dita_v2 import RealZincPlane
from prod.clean_arch.dita_v2 import TradeSide
from prod.clean_arch.dita_v2 import TradeStage
DOTENV_PATH = Path("/mnt/dolphinng5_predict/.env")
if DOTENV_PATH.exists():
load_dotenv(DOTENV_PATH, override=False)
LIVE_ENABLED = os.getenv("BINGX_SMOKE_LIVE") == "1"
LIVE_TRADING_ENABLED = os.getenv("BINGX_SMOKE_ALLOW_TRADE") == "1"
LIVE_DITAV2_ENABLED = os.getenv("DITA_V2_LIVE_BINGX") == "1"
LIVE_CREDENTIALS_READY = bool(os.getenv("BINGX_API_KEY")) and bool(os.getenv("BINGX_SECRET_KEY"))
pytestmark = pytest.mark.skipif(
not (LIVE_ENABLED and LIVE_TRADING_ENABLED and LIVE_DITAV2_ENABLED and LIVE_CREDENTIALS_READY),
reason=(
"DITAv2 live BingX testnet E2E requires BINGX_SMOKE_LIVE=1, "
"BINGX_SMOKE_ALLOW_TRADE=1, DITA_V2_LIVE_BINGX=1, and BingX VST credentials"
),
)
def _norm_symbol(value: str) -> str:
return str(value or "").replace("-", "").replace("_", "").upper()
def _contract_rows(payload: Any) -> list[dict[str, Any]]:
if isinstance(payload, list):
return [row for row in payload if isinstance(row, dict)]
if isinstance(payload, dict):
for key in ("contracts", "data", "rows"):
rows = payload.get(key)
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
return []
def _reference_price_row(payload: Any) -> dict[str, Any]:
if isinstance(payload, list):
return payload[0] if payload and isinstance(payload[0], dict) else {}
if isinstance(payload, dict):
data = payload.get("data")
if isinstance(data, list):
return data[0] if data and isinstance(data[0], dict) else {}
if isinstance(data, dict):
return data
return payload
return {}
def _position_qty(row: dict[str, Any]) -> Decimal:
raw = row.get("positionAmt") or row.get("positionQty") or row.get("positionSize") or row.get("quantity") or 0
try:
return abs(Decimal(str(raw)))
except Exception:
return Decimal("0")
def _position_side(row: dict[str, Any]) -> TradeSide:
side_raw = str(row.get("positionSide") or row.get("side") or "").upper()
if side_raw in {"SHORT", "SELL"}:
return TradeSide.SHORT
if side_raw in {"LONG", "BUY"}:
return TradeSide.LONG
qty = _position_qty(row)
signed = str(row.get("positionAmt") or row.get("quantity") or "0")
try:
return TradeSide.SHORT if Decimal(signed) < 0 else TradeSide.LONG if qty > 0 else TradeSide.FLAT
except Exception:
return TradeSide.FLAT
def _live_quantity(contract: BingxContract) -> Decimal:
base = contract.min_quantity if contract.min_quantity > 0 else contract.step_size
if base <= 0:
base = Decimal("0.001")
qty = base * Decimal("2")
step = contract.step_size if contract.step_size > 0 else Decimal("0.001")
if qty < step * Decimal("2"):
qty = step * Decimal("2")
if qty < Decimal("12"):
qty = Decimal("12")
return qty
def _build_kernel(prefix: str) -> tuple[ExecutionKernel, RealZincControlPlane, RealZincPlane, BingxVenueAdapter]:
zinc_plane = RealZincPlane(prefix=prefix, slot_count=1, create=True)
control_plane = RealZincControlPlane(prefix=prefix, create=False)
venue = BingxVenueAdapter(
config=BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY", ""),
secret_key=os.environ.get("BINGX_SECRET_KEY", ""),
environment=BingxEnvironment.VST,
allow_mainnet=False,
recv_window_ms=int(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "60000")),
default_leverage=int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")),
exchange_leverage_cap=int(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")),
prefer_websocket=False,
sizing_mode="testnet",
journal_strategy="dita_v2_live_testnet",
journal_db="dolphin_pink",
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
)
kernel = ExecutionKernel(
max_slots=1,
control_plane=control_plane,
venue=venue,
zinc_plane=zinc_plane,
)
kernel.update_control(
ControlUpdate(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
backend_mode=BackendMode.BINGX,
trace_transitions=True,
debug_clickhouse_enabled=True,
mirror_to_hazelcast=True,
reconcile_on_restart=True,
)
)
return kernel, control_plane, zinc_plane, venue
def _pick_live_contract(venue: BingxVenueAdapter) -> BingxContract:
client = getattr(getattr(venue, "backend", None), "_client", None)
if client is None:
raise AssertionError("BingxVenueAdapter backend does not expose a live client")
async def _inner() -> BingxContract:
state = await venue.backend.refresh_state(include_history=True)
open_symbols = {
_norm_symbol(str(row.get("symbol", key)))
for key, row in getattr(state, "open_positions", {}).items()
if isinstance(row, dict)
}
contracts_payload = await client.public_get("/openApi/swap/v2/quote/contracts")
contracts: list[BingxContract] = []
for row in _contract_rows(contracts_payload):
try:
contracts.append(BingxContract.from_http(row))
except Exception:
continue
if not contracts:
raise AssertionError("BingX VST contract loader returned no usable contracts")
preferred = [
os.getenv("BINGX_SMOKE_SYMBOL", "").strip().upper(),
"TRXUSDT",
"XLMUSDT",
"DOGEUSDT",
"ETHUSDT",
"BTCUSDT",
]
by_symbol = {contract.symbol.upper(): contract for contract in contracts}
by_venue = {contract.venue_symbol.replace("-", "").upper(): contract for contract in contracts}
for candidate in preferred:
if not candidate or candidate in open_symbols:
continue
if candidate in by_symbol:
return by_symbol[candidate]
if candidate in by_venue:
return by_venue[candidate]
for contract in contracts:
symbol = contract.symbol.upper()
venue_symbol = contract.venue_symbol.replace("-", "").upper()
if symbol not in open_symbols and venue_symbol not in open_symbols:
return contract
raise AssertionError("No BingX VST contract available outside the current open set")
return asyncio.run(_inner())
def _reference_price(venue: BingxVenueAdapter, contract: BingxContract) -> Decimal:
client = getattr(getattr(venue, "backend", None), "_client", None)
if client is None:
raise AssertionError("BingxVenueAdapter backend does not expose a live client")
async def _inner() -> Decimal:
payload = await client.public_get("/openApi/swap/v2/quote/price", {"symbol": contract.venue_symbol})
row = _reference_price_row(payload)
raw_price = row.get("price") or row.get("lastPrice") or row.get("markPrice") or row.get("last")
if raw_price is None:
raise AssertionError(f"Unable to resolve BingX price for {contract.venue_symbol}: {payload!r}")
return Decimal(str(raw_price))
return asyncio.run(_inner())
def _live_intent(
*,
action: KernelCommandType,
trade_id: str,
side: TradeSide,
asset: str,
target_size: float,
price: float,
reason: str,
) -> dict[str, Any]:
return {
"timestamp": datetime.now(timezone.utc).isoformat(),
"intent_id": f"{trade_id}:{action.value}:{reason}:{uuid.uuid4().hex[:8]}",
"trade_id": trade_id,
"slot_id": 0,
"asset": asset,
"side": side.value,
"action": action.value,
"reference_price": float(price),
"target_size": float(target_size),
"leverage": 1.0,
"exit_leg_ratios": [0.5, 0.5],
"reason": reason,
"metadata": {"source": "live_bingx_testnet"},
"stage": "INTENT_CREATED",
}
def _current_exchange_rows(venue: BingxVenueAdapter, symbol: str) -> list[dict[str, Any]]:
rows = []
for row in venue.open_positions():
if _norm_symbol(str(row.get("symbol") or row.get("venueSymbol") or "")) == _norm_symbol(symbol):
rows.append(dict(row))
return rows
def _current_exchange_qty(venue: BingxVenueAdapter, symbol: str) -> Decimal:
rows = _current_exchange_rows(venue, symbol)
if not rows:
return Decimal("0")
return max(_position_qty(row) for row in rows)
def _observed_live_qty(kernel: ExecutionKernel, venue: BingxVenueAdapter, symbol: str) -> Decimal:
slot_qty = Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0))
exchange_qty = _current_exchange_qty(venue, symbol)
return max(slot_qty, exchange_qty)
def _drive_live_reconcile(kernel: ExecutionKernel, venue: BingxVenueAdapter, symbol: str) -> None:
snapshot = venue.reconcile()
for event in snapshot:
if _norm_symbol(str(getattr(event, "asset", ""))) == _norm_symbol(symbol):
kernel.on_venue_event(event)
def _wait_for_live_response(kernel: ExecutionKernel, venue: BingxVenueAdapter, symbol: str, *, timeout_s: float = 60.0) -> tuple[TradeStage, Decimal]:
def _predicate() -> bool:
_drive_live_reconcile(kernel, venue, symbol)
slot = kernel.slot(0)
return slot.asset == symbol and slot.fsm_state != TradeStage.IDLE
try:
_wait_until(_predicate, timeout_s=timeout_s, interval_s=1.0)
except AssertionError:
_drive_live_reconcile(kernel, venue, symbol)
slot = kernel.slot(0)
return slot.fsm_state, _current_exchange_qty(venue, symbol)
def _wait_until(predicate, *, timeout_s: float = 30.0, interval_s: float = 1.0) -> None:
deadline = time.monotonic() + timeout_s
last_exc: Exception | None = None
while time.monotonic() < deadline:
try:
if predicate():
return
except Exception as exc: # pragma: no cover - best effort live polling
last_exc = exc
time.sleep(interval_s)
if last_exc is not None:
raise AssertionError("timed out while waiting for live BingX state") from last_exc
raise AssertionError("timed out while waiting for live BingX state")
def _cleanup_live_position(kernel: ExecutionKernel, venue: BingxVenueAdapter, contract: BingxContract, trade_id: str) -> None:
try:
for attempt in range(5):
qty = _current_exchange_qty(venue, contract.symbol)
if qty <= 0:
return
side = TradeSide.SHORT
rows = _current_exchange_rows(venue, contract.symbol)
if rows:
side = _position_side(rows[0])
if side == TradeSide.FLAT:
side = TradeSide.SHORT
price = float(_reference_price(venue, contract))
outcome = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:cleanup:{attempt}:{uuid.uuid4().hex[:8]}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=price,
target_size=float(qty),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason="CLEANUP",
metadata={"source": "cleanup"},
)
)
if outcome.diagnostic_code == KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER:
_wait_until(lambda: _current_exchange_qty(venue, contract.symbol) <= 0, timeout_s=20.0, interval_s=1.0)
else:
_wait_until(lambda: _current_exchange_qty(venue, contract.symbol) <= 0, timeout_s=30.0, interval_s=1.0)
finally:
pass
@dataclass(frozen=True)
class _LiveCase:
name: str
side: TradeSide
LIVE_CASES = (
_LiveCase("short_cycle", TradeSide.SHORT),
_LiveCase("long_cycle", TradeSide.LONG),
)
@pytest.mark.parametrize("case", LIVE_CASES, ids=lambda case: case.name)
def test_live_bingx_testnet_basic_cycle(case: _LiveCase) -> None:
prefix = f"dita_v2_live_{case.name}_{uuid.uuid4().hex[:8]}"
kernel, control_plane, zinc_plane, venue = _build_kernel(prefix)
contract: BingxContract | None = None
trade_id = f"live-{case.name}-{uuid.uuid4().hex[:8]}"
try:
assert venue.connect() is True
contract = _pick_live_contract(venue)
size = _live_quantity(contract)
price = _reference_price(venue, contract)
entry = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:entry",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=case.side,
action=KernelCommandType.ENTER,
reference_price=float(price),
target_size=float(size),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason="LIVE_ENTRY",
metadata={"contract": contract.venue_symbol},
)
)
if entry.diagnostic_code == KernelDiagnosticCode.RATE_LIMITED or any(event.kind == KernelEventKind.RATE_LIMITED for event in entry.emitted_events):
assert entry.accepted is False
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
return
assert entry.accepted is True
assert entry.diagnostic_code == KernelDiagnosticCode.OK
slot = kernel.slot(0)
assert slot.trade_id == trade_id
assert slot.asset == contract.symbol
state, open_qty = _wait_for_live_response(kernel, venue, contract.symbol, timeout_s=60.0)
kernel_qty = Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0))
live_qty = max(kernel_qty, open_qty)
assert state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
assert kernel.zinc_plane.read_control().backend_mode == BackendMode.BINGX
if live_qty <= 0:
assert entry.emitted_events, "entry should still emit an exchange reaction"
assert any(event.kind in {KernelEventKind.ORDER_ACK, KernelEventKind.ORDER_REJECT, KernelEventKind.PARTIAL_FILL, KernelEventKind.RATE_LIMITED} for event in entry.emitted_events)
return
mark = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:mark",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=case.side,
action=KernelCommandType.MARK_PRICE,
reference_price=float(price),
target_size=float(size),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason="LIVE_MARK",
metadata={"contract": contract.venue_symbol},
)
)
assert mark.accepted is True
assert kernel.slot(0).asset == contract.symbol
live_qty = max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol))
assert live_qty > 0
partial_target = max(live_qty / Decimal("2"), contract.step_size)
partial = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:partial_exit",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=case.side,
action=KernelCommandType.EXIT,
reference_price=float(price),
target_size=float(partial_target),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason="LIVE_PARTIAL_EXIT",
metadata={"contract": contract.venue_symbol},
)
)
if partial.diagnostic_code == KernelDiagnosticCode.RATE_LIMITED or any(event.kind == KernelEventKind.RATE_LIMITED for event in partial.emitted_events):
assert partial.accepted is False
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
return
assert partial.accepted is True
if partial.diagnostic_code in {
KernelDiagnosticCode.EXIT_ORDER_REJECTED,
KernelDiagnosticCode.ORDER_REJECTED,
KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER,
KernelDiagnosticCode.CANCEL_REJECTED,
KernelDiagnosticCode.RATE_LIMITED,
} or any(event.kind in {KernelEventKind.ORDER_REJECT, KernelEventKind.CANCEL_REJECT, KernelEventKind.RATE_LIMITED} for event in partial.emitted_events):
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
return
_wait_until(lambda: _current_exchange_qty(venue, contract.symbol) <= live_qty, timeout_s=60.0, interval_s=1.0)
reduced_qty = _current_exchange_qty(venue, contract.symbol)
assert reduced_qty <= open_qty
remaining = reduced_qty
if remaining > 0:
final = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:final_exit",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=case.side,
action=KernelCommandType.EXIT,
reference_price=float(price),
target_size=float(remaining),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason="LIVE_FINAL_EXIT",
metadata={"contract": contract.venue_symbol},
)
)
if final.diagnostic_code == KernelDiagnosticCode.RATE_LIMITED or any(event.kind == KernelEventKind.RATE_LIMITED for event in final.emitted_events):
assert final.accepted is False
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
return
assert final.accepted is True
if final.diagnostic_code in {
KernelDiagnosticCode.EXIT_ORDER_REJECTED,
KernelDiagnosticCode.ORDER_REJECTED,
KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER,
KernelDiagnosticCode.CANCEL_REJECTED,
KernelDiagnosticCode.RATE_LIMITED,
} or any(event.kind in {KernelEventKind.ORDER_REJECT, KernelEventKind.CANCEL_REJECT, KernelEventKind.RATE_LIMITED} for event in final.emitted_events):
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
return
_wait_until(lambda: _current_exchange_qty(venue, contract.symbol) <= 0, timeout_s=60.0, interval_s=1.0)
# On the real live path there is no active working exit order after the market close.
cancel_diag = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:cancel_after_flat",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=case.side,
action=KernelCommandType.CANCEL,
reference_price=float(price),
target_size=float(size),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason="LIVE_CANCEL_AFTER_FLAT",
metadata={"contract": contract.venue_symbol},
)
)
assert cancel_diag.diagnostic_code in {
KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER,
KernelDiagnosticCode.OK,
KernelDiagnosticCode.CANCEL_REJECTED,
KernelDiagnosticCode.ORDER_REJECTED,
KernelDiagnosticCode.RATE_LIMITED,
}
_wait_until(lambda: _current_exchange_qty(venue, contract.symbol) <= 0, timeout_s=60.0, interval_s=1.0)
assert _current_exchange_qty(venue, contract.symbol) <= 0
assert kernel.slot(0).size <= 1e-12
finally:
if contract is not None:
try:
_cleanup_live_position(kernel, venue, contract, trade_id)
except Exception:
pass
try:
disconnect = getattr(getattr(venue, "backend", None), "disconnect", None)
if disconnect is not None:
asyncio.run(disconnect())
except Exception:
pass
try:
zinc_plane.close()
except Exception:
pass
try:
control_plane.close()
except Exception:
pass
@pytest.mark.parametrize("seed", range(4), ids=lambda seed: f"seed-{seed}")
@pytest.mark.parametrize("side", [TradeSide.SHORT, TradeSide.LONG], ids=lambda side: f"side-{side.value.lower()}")
def test_live_bingx_testnet_chaos_fuzz(seed: int, side: TradeSide) -> None:
rng = __import__("random").Random(20260527 + seed)
prefix = f"dita_v2_live_fuzz_{side.value.lower()}_{seed}_{uuid.uuid4().hex[:8]}"
kernel, control_plane, zinc_plane, venue = _build_kernel(prefix)
contract: BingxContract | None = None
trade_id = f"live-fuzz-{side.value.lower()}-{seed}-{uuid.uuid4().hex[:8]}"
try:
assert venue.connect() is True
contract = _pick_live_contract(venue)
size = _live_quantity(contract)
price = _reference_price(venue, contract)
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:entry",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.ENTER,
reference_price=float(price),
target_size=float(size),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason="FUZZ_ENTRY",
metadata={"contract": contract.venue_symbol},
)
)
state, open_qty = _wait_for_live_response(kernel, venue, contract.symbol, timeout_s=60.0)
kernel_qty = Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0))
live_qty = max(kernel_qty, open_qty)
assert state in {
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
if live_qty <= 0:
assert kernel.slot(0).fsm_state in {
TradeStage.IDLE,
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.STALE_STATE_RECONCILING,
TradeStage.CLOSED,
}
return
for idx in range(rng.randint(4, 7)):
slot = kernel.slot(0)
action = rng.choice(["mark", "exit_half", "exit_rest", "cancel", "reconcile"])
current_price = _reference_price(venue, contract)
_drive_live_reconcile(kernel, venue, contract.symbol)
slot = kernel.slot(0)
if action == "mark":
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:mark:{idx}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.MARK_PRICE,
reference_price=float(current_price),
target_size=float(max(size, slot.size or size)),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason=f"FUZZ_MARK_{idx}",
metadata={"contract": contract.venue_symbol},
)
)
elif action == "exit_half":
live_qty = max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol))
if live_qty <= 0:
continue
target = max(live_qty / Decimal("2"), contract.step_size)
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:exit_half:{idx}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=float(current_price),
target_size=float(target),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason=f"FUZZ_EXIT_HALF_{idx}",
metadata={"contract": contract.venue_symbol},
)
)
elif action == "exit_rest":
live_qty = max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol))
if live_qty <= 0:
continue
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:exit_rest:{idx}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=float(current_price),
target_size=float(max(Decimal(str(slot.size)), contract.step_size)),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"FUZZ_EXIT_REST_{idx}",
metadata={"contract": contract.venue_symbol},
)
)
elif action == "cancel":
if kernel.slot(0).active_exit_order is not None or max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol)) > 0:
outcome = kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:cancel:{idx}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.CANCEL,
reference_price=float(current_price),
target_size=float(max(size, contract.step_size)),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"FUZZ_CANCEL_{idx}",
metadata={"contract": contract.venue_symbol},
)
)
assert outcome.diagnostic_code in {
KernelDiagnosticCode.NO_ACTIVE_EXIT_ORDER,
KernelDiagnosticCode.OK,
KernelDiagnosticCode.RATE_LIMITED,
}
else:
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:reconcile:{idx}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.RECONCILE,
reference_price=float(current_price),
target_size=float(size),
leverage=1.0,
exit_leg_ratios=(0.5, 0.5),
reason=f"FUZZ_RECONCILE_{idx}",
metadata={"contract": contract.venue_symbol},
)
)
_drive_live_reconcile(kernel, venue, contract.symbol)
_wait_until(lambda: max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol)) >= 0, timeout_s=2.0, interval_s=0.2)
# Hard close-out pass for the fuzz cases.
for _ in range(3):
qty = max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol))
if qty <= 0:
break
kernel.process_intent(
KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=f"{trade_id}:cleanup:{uuid.uuid4().hex[:8]}",
trade_id=trade_id,
slot_id=0,
asset=contract.symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=float(_reference_price(venue, contract)),
target_size=float(qty),
leverage=1.0,
exit_leg_ratios=(1.0,),
reason="FUZZ_CLEANUP",
metadata={"contract": contract.venue_symbol},
)
)
_wait_until(lambda: max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol)) <= 0, timeout_s=60.0, interval_s=1.0)
assert max(Decimal(str(getattr(kernel.slot(0), "size", 0.0) or 0.0)), _current_exchange_qty(venue, contract.symbol)) <= 0
assert kernel.slot(0).fsm_state in {
TradeStage.CLOSED,
TradeStage.IDLE,
TradeStage.POSITION_OPEN,
TradeStage.POSITION_PARTIALLY_CLOSED,
TradeStage.STALE_STATE_RECONCILING,
}
assert kernel.zinc_plane.read_control().mode == KernelMode.DEBUG
assert kernel.zinc_plane.read_control().verbosity == KernelVerbosity.TRACE
finally:
if contract is not None:
try:
_cleanup_live_position(kernel, venue, contract, trade_id)
except Exception:
pass
try:
disconnect = getattr(getattr(venue, "backend", None), "disconnect", None)
if disconnect is not None:
asyncio.run(disconnect())
except Exception:
pass
try:
zinc_plane.close()
except Exception:
pass
try:
control_plane.close()
except Exception:
pass

View File

@@ -1,54 +0,0 @@
from __future__ import annotations
from pathlib import Path
import unittest
class TestDITAv2Ops(unittest.TestCase):
def test_operator_playbook_mentions_supervisor_program(self) -> None:
text = Path("/mnt/dolphinng5_predict/prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md").read_text()
self.assertIn("dolphin:dita_v2", text)
self.assertIn("launch_dita_v2.py", text)
self.assertIn("dita_v2_ctl.py", text)
def test_supervisor_config_contains_dita_v2_program(self) -> None:
conf = Path("/mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf").read_text()
self.assertIn("[program:dita_v2]", conf)
self.assertIn("launch_dita_v2.py", conf)
def test_supervisor_migration_doc_mentions_dita_v2_recovery(self) -> None:
text = Path("/mnt/dolphinng5_predict/prod/AGENT_READ_Supervisor_migration.md").read_text()
self.assertIn("dolphin:dita_v2", text)
self.assertIn("dita_v2_ctl.py", text)
self.assertIn("Do not use `systemctl` for `dolphin:dita_v2`", text)
def test_supervisor_wrapper_mentions_dita_v2(self) -> None:
text = Path("/mnt/dolphinng5_predict/prod/supervisor/supervisorctl.sh").read_text()
self.assertIn("dita_v2", text)
self.assertIn("dita_v2_ctl.py", text)
def test_supervisor_config_has_dita_v2_comment(self) -> None:
conf = Path("/mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf").read_text()
self.assertIn("DITAv2 — supervised kernel", conf)
def test_operational_status_mentions_dita_v2(self) -> None:
text = Path("/mnt/dolphinng5_predict/prod/docs/OPERATIONAL_STATUS.md").read_text()
self.assertIn("DITAv2 Kernel", text)
self.assertIn("dolphin:dita_v2", text)
def test_live_smoke_wrapper_is_documented_and_wired(self) -> None:
script = Path("/mnt/dolphinng5_predict/prod/ops/dita_v2_live_bingx_smoke.py").read_text()
self.assertIn("BINGX_SMOKE_LIVE", script)
self.assertIn("BINGX_SMOKE_ALLOW_TRADE", script)
self.assertIn("DITA_V2_LIVE_BINGX", script)
self.assertIn("test_dita_v2_live_bingx_testnet_e2e.py", script)
self.assertIn("--dry-run", script)
playbook = Path("/mnt/dolphinng5_predict/prod/docs/DITA_V2_OPERATOR_PLAYBOOK.md").read_text()
self.assertIn("dita_v2_live_bingx_smoke.py", playbook)
self.assertIn("--dry-run", playbook)
self.assertIn("TRXUSDT", playbook)
if __name__ == "__main__":
unittest.main()

View File

@@ -1,380 +0,0 @@
from __future__ import annotations
from datetime import datetime, timezone
import os
import random
import threading
import time
import unittest
from uuid import uuid4
from prod.clean_arch.dita_v2 import (
ControlUpdate,
ExecutionKernel,
InMemoryControlPlane,
KernelCommandType,
KernelDiagnosticCode,
KernelControlSnapshot,
KernelIntent,
KernelMode,
KernelVerbosity,
MockVenueAdapter,
MockVenueScenario,
InMemoryZincPlane,
RealZincPlane,
RealZincControlPlane,
RealZincUnavailable,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
KernelEventKind,
)
from prod.clean_arch.dita_v2.real_zinc_plane import SharedRegion
HAS_REAL_ZINC = SharedRegion is not None
def mk_intent_kwargs(
*,
slot_id: int,
trade_id: str,
action: KernelCommandType,
size: float = 1.0,
leverage: float = 2.0,
side: TradeSide = TradeSide.SHORT,
price: float = 100.0,
reason: str = "FUZZ",
) -> dict[str, object]:
return {
"timestamp": datetime.now(timezone.utc),
"intent_id": f"intent-{trade_id}-{action.value}-{slot_id}",
"trade_id": trade_id,
"slot_id": slot_id,
"asset": "BTCUSDT",
"side": side,
"action": action,
"reference_price": price,
"target_size": size,
"leverage": leverage,
"exit_leg_ratios": (0.5, 0.5) if action == KernelCommandType.EXIT else (1.0,),
"reason": reason,
}
@unittest.skipUnless(HAS_REAL_ZINC, "Real Zinc adapter is unavailable")
class TestDITAv2RealZinc(unittest.TestCase):
def setUp(self) -> None:
self.prefix = f"dita_v2_{os.getpid()}_{uuid4().hex}"
self.writer = RealZincPlane(prefix=self.prefix, slot_count=3, create=True)
self.reader = RealZincPlane(prefix=self.prefix, slot_count=3, create=False)
def tearDown(self) -> None:
self.writer.close()
self.reader.close()
def _slot_dicts(self, plane: RealZincPlane) -> list[dict[str, object]]:
return [slot.to_dict() for slot in plane.read_slots()]
def test_wait_notify_and_roundtrip(self) -> None:
waiter_started = threading.Event()
waiter_result: dict[str, bool] = {"ok": False}
def _waiter() -> None:
waiter_started.set()
waiter_result["ok"] = self.reader.wait_on_state(timeout_ms=3000)
thread = threading.Thread(target=_waiter, daemon=True)
thread.start()
self.assertTrue(waiter_started.wait(timeout=2.0))
time.sleep(0.05)
kernel_slot = self.writer.read_slots()
self.assertEqual(len(kernel_slot), 3)
self.assertTrue(all(slot.fsm_state == TradeStage.IDLE for slot in kernel_slot))
self.writer.write_slot(
TradeSlot(
slot_id=0,
trade_id="trade-zinc-1",
asset="BTCUSDT",
side=TradeSide.SHORT,
entry_price=100.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
)
)
thread.join(timeout=3.0)
self.assertFalse(thread.is_alive())
self.assertTrue(waiter_result["ok"])
slots = self.reader.read_slots()
self.assertEqual(len(slots), 3)
self.assertEqual(slots[0].trade_id, "trade-zinc-1")
self.assertEqual(slots[0].fsm_state, TradeStage.POSITION_OPEN)
self.assertTrue(all(slot.fsm_state == TradeStage.IDLE for slot in slots[1:]))
def test_in_memory_wait_notify_matches_signal_semantics(self) -> None:
plane = InMemoryZincPlane()
waiter_started = threading.Event()
waiter_result: dict[str, bool] = {"ok": False}
def _waiter() -> None:
waiter_started.set()
waiter_result["ok"] = plane.wait_on_state(timeout_ms=2000)
thread = threading.Thread(target=_waiter, daemon=True)
thread.start()
self.assertTrue(waiter_started.wait(timeout=2.0))
time.sleep(0.05)
plane.write_slot(
TradeSlot(
slot_id=0,
trade_id="trade-signal",
asset="BTCUSDT",
side=TradeSide.LONG,
entry_price=101.0,
size=1.0,
initial_size=1.0,
leverage=2.0,
fsm_state=TradeStage.POSITION_OPEN,
)
)
thread.join(timeout=3.0)
self.assertFalse(thread.is_alive())
self.assertTrue(waiter_result["ok"])
def test_real_control_plane_roundtrip_uses_open_existing_region(self) -> None:
prefix = f"dita_v2_control_{os.getpid()}_{uuid4().hex}"
plane = RealZincPlane(prefix=prefix, slot_count=1, create=True)
control = RealZincControlPlane(prefix=prefix, create=False)
try:
snapshot = control.read()
self.assertEqual(getattr(snapshot.mode, "value", snapshot.mode), KernelMode.NORMAL.value)
self.assertEqual(getattr(snapshot.verbosity, "value", snapshot.verbosity), KernelVerbosity.QUIET.value)
updated = control.update(ControlUpdate(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE))
self.assertEqual(getattr(updated.mode, "value", updated.mode), KernelMode.DEBUG.value)
self.assertEqual(getattr(updated.verbosity, "value", updated.verbosity), KernelVerbosity.TRACE.value)
mirrored = plane.read_control()
self.assertEqual(getattr(mirrored.mode, "value", mirrored.mode), KernelMode.DEBUG.value)
self.assertEqual(getattr(mirrored.verbosity, "value", mirrored.verbosity), KernelVerbosity.TRACE.value)
finally:
control.close()
plane.close()
def test_real_control_plane_create_conflicts_with_existing_zinc_plane(self) -> None:
prefix = f"dita_v2_conflict_{os.getpid()}_{uuid4().hex}"
plane = RealZincPlane(prefix=prefix, slot_count=1, create=True)
try:
with self.assertRaises(FileExistsError):
RealZincControlPlane(prefix=prefix, create=True)
finally:
plane.close()
def test_kernel_accepts_real_control_plane_snapshot_strings(self) -> None:
prefix = f"dita_v2_kernel_real_cp_{os.getpid()}_{uuid4().hex}"
plane = RealZincPlane(prefix=prefix, slot_count=1, create=True)
control = RealZincControlPlane(prefix=prefix, create=False)
try:
kernel = ExecutionKernel(
max_slots=1,
control_plane=control,
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)),
zinc_plane=plane,
)
kernel.update_control(
ControlUpdate(
mode=KernelMode.DEBUG,
verbosity=KernelVerbosity.TRACE,
trace_transitions=True,
)
)
outcome = kernel.process_intent(
KernelIntent(
**mk_intent_kwargs(
slot_id=0,
trade_id=f"trade-real-cp-{uuid4().hex}",
action=KernelCommandType.ENTER,
price=100.0,
size=1.0,
)
)
)
self.assertTrue(outcome.accepted)
self.assertEqual(outcome.diagnostic_code, KernelDiagnosticCode.OK)
self.assertEqual(kernel.slot(0).fsm_state, TradeStage.POSITION_OPEN)
self.assertEqual(getattr(kernel.control.mode, "value", kernel.control.mode), KernelMode.DEBUG.value)
self.assertEqual(getattr(kernel.control.verbosity, "value", kernel.control.verbosity), KernelVerbosity.TRACE.value)
finally:
control.close()
plane.close()
def test_kernel_and_zinc_fuzz_roundtrip_150_checks(self) -> None:
control = InMemoryControlPlane(
KernelControlSnapshot(mode=KernelMode.DEBUG, verbosity=KernelVerbosity.TRACE)
)
kernel = ExecutionKernel(
max_slots=3,
control_plane=control,
venue=MockVenueAdapter(MockVenueScenario(emit_fill_on_submit=True, partial_fill_ratio=1.0)),
zinc_plane=self.writer,
)
rng = random.Random(20260526)
for i in range(150):
slot_id = rng.randrange(0, 3)
slot = kernel.slot(slot_id)
op = rng.choice(
[
"enter",
"exit",
"mark",
"reconcile",
"control",
"event",
]
)
with self.subTest(iteration=i, slot=slot_id, op=op):
if op == "enter":
if slot.is_free():
kernel.process_intent(
KernelIntent(
**mk_intent_kwargs(
slot_id=slot_id,
trade_id=f"trade-{slot_id}-{i}",
action=KernelCommandType.ENTER,
price=100.0 + rng.random(),
size=1.0 + (rng.random() * 0.5),
leverage=1.5 + (rng.random() * 2.0),
)
)
)
elif op == "exit":
if slot.is_open():
kernel.process_intent(
KernelIntent(
**mk_intent_kwargs(
slot_id=slot_id,
trade_id=slot.trade_id,
action=KernelCommandType.EXIT,
price=99.0 + rng.random(),
size=max(0.1, slot.size or 0.1),
leverage=slot.leverage or 2.0,
)
)
)
elif op == "mark":
kernel.process_intent(
KernelIntent(
**mk_intent_kwargs(
slot_id=slot_id,
trade_id=slot.trade_id or f"trade-{slot_id}-{i}",
action=KernelCommandType.MARK_PRICE,
price=95.0 + rng.random() * 10.0,
size=max(slot.size, 1.0) if slot.size > 0 else 1.0,
leverage=slot.leverage or 2.0,
)
)
)
elif op == "reconcile":
kernel.process_intent(
KernelIntent(
**mk_intent_kwargs(
slot_id=slot_id,
trade_id=slot.trade_id or f"trade-{slot_id}-{i}",
action=KernelCommandType.RECONCILE,
price=100.0,
size=max(slot.size, 1.0) if slot.size > 0 else 1.0,
leverage=slot.leverage or 2.0,
)
)
)
elif op == "control":
kernel.update_control(
ControlUpdate(
mode=KernelMode.DEBUG if rng.random() < 0.7 else KernelMode.NORMAL,
verbosity=KernelVerbosity.TRACE if rng.random() < 0.5 else KernelVerbosity.VERBOSE,
trace_transitions=rng.random() < 0.5,
)
)
elif op == "event":
current = kernel.slot(slot_id)
if current.active_entry_order is not None:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"evt-{i}-{slot_id}",
trade_id=current.trade_id,
slot_id=slot_id,
kind=rng.choice(
[
KernelEventKind.ORDER_ACK,
KernelEventKind.PARTIAL_FILL,
KernelEventKind.FULL_FILL,
]
),
status=rng.choice(
[
VenueEventStatus.ACKED,
VenueEventStatus.PARTIALLY_FILLED,
VenueEventStatus.FILLED,
]
),
venue_order_id=current.active_entry_order.venue_order_id,
venue_client_id=current.active_entry_order.venue_client_id,
side=current.side,
asset=current.asset,
price=current.entry_price or 100.0,
size=current.size or 1.0,
filled_size=current.size or 1.0,
remaining_size=0.0,
)
kernel.on_venue_event(event)
elif current.active_exit_order is not None:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"evt-{i}-{slot_id}",
trade_id=current.trade_id,
slot_id=slot_id,
kind=rng.choice(
[
KernelEventKind.PARTIAL_FILL,
KernelEventKind.FULL_FILL,
KernelEventKind.CANCEL_ACK,
KernelEventKind.CANCEL_REJECT,
]
),
status=rng.choice(
[
VenueEventStatus.PARTIALLY_FILLED,
VenueEventStatus.FILLED,
VenueEventStatus.CANCELED,
VenueEventStatus.CANCELED_REJECTED,
]
),
venue_order_id=current.active_exit_order.venue_order_id,
venue_client_id=current.active_exit_order.venue_client_id,
side=current.side,
asset=current.asset,
price=current.entry_price or 100.0,
size=current.size or 1.0,
filled_size=min(current.size or 1.0, 0.5),
remaining_size=max(0.0, (current.size or 1.0) - 0.5),
)
kernel.on_venue_event(event)
writer_slots = self._slot_dicts(self.writer)
reader_slots = self._slot_dicts(self.reader)
kernel_slots = [slot.to_dict() for slot in kernel.state.slots]
self.assertEqual(writer_slots, reader_slots)
self.assertEqual(reader_slots, kernel_slots)
self.assertEqual(self.reader.read_control().mode, kernel.control.mode)
self.assertEqual(self.reader.read_control().verbosity, kernel.control.verbosity)
self.assertEqual(len(self.reader.read_intents()), len(self.writer.read_intents()))
if __name__ == "__main__":
unittest.main()

View File

@@ -1,79 +0,0 @@
from __future__ import annotations
import os
from types import SimpleNamespace
from pathlib import Path
import unittest
from unittest.mock import patch
import prod.launch_dita_v2 as launch_dita_v2
class DummyBundle:
def __init__(self) -> None:
self.closed = False
self.kernel = SimpleNamespace(snapshot=lambda: {"ok": True}, control=SimpleNamespace(as_dict=lambda: {"mode": "NORMAL"}))
self.venue = SimpleNamespace(__class__=SimpleNamespace(__name__="MockVenueAdapter"))
self.zinc_plane = SimpleNamespace(__class__=SimpleNamespace(__name__="InMemoryZincPlane"))
self.projection = SimpleNamespace(__class__=SimpleNamespace(__name__="HazelcastProjection"))
def close(self) -> None:
self.closed = True
class TestLaunchDitaV2(unittest.TestCase):
def test_supervisor_config_contains_dita_v2_program(self) -> None:
conf = Path("/mnt/dolphinng5_predict/prod/supervisor/dolphin-supervisord.conf").read_text()
self.assertIn("[program:dita_v2]", conf)
self.assertIn("launch_dita_v2.py", conf)
self.assertIn("DITA_V2_LAUNCHER_MODE=\"serve\"", conf)
def test_env_mode_defaults_to_serve(self) -> None:
previous = os.environ.get("DITA_V2_LAUNCHER_MODE")
try:
os.environ.pop("DITA_V2_LAUNCHER_MODE", None)
self.assertEqual(launch_dita_v2._env_mode(), "serve")
os.environ["DITA_V2_LAUNCHER_MODE"] = "once"
self.assertEqual(launch_dita_v2._env_mode(), "once")
finally:
if previous is None:
os.environ.pop("DITA_V2_LAUNCHER_MODE", None)
else:
os.environ["DITA_V2_LAUNCHER_MODE"] = previous
def test_main_once_uses_snapshot_path(self) -> None:
bundle = DummyBundle()
with patch.object(launch_dita_v2, "build_launcher_bundle", return_value=bundle), patch.object(
launch_dita_v2, "_serve", side_effect=AssertionError("_serve should not run in once mode")
):
previous = os.environ.get("DITA_V2_LAUNCHER_MODE")
os.environ["DITA_V2_LAUNCHER_MODE"] = "once"
try:
self.assertEqual(launch_dita_v2.main(), 0)
self.assertTrue(bundle.closed)
finally:
if previous is None:
os.environ.pop("DITA_V2_LAUNCHER_MODE", None)
else:
os.environ["DITA_V2_LAUNCHER_MODE"] = previous
def test_main_serve_routes_to_serve(self) -> None:
bundle = DummyBundle()
with patch.object(launch_dita_v2, "build_launcher_bundle", return_value=bundle), patch.object(
launch_dita_v2, "_serve", return_value=7
) as serve:
previous = os.environ.get("DITA_V2_LAUNCHER_MODE")
os.environ["DITA_V2_LAUNCHER_MODE"] = "serve"
try:
self.assertEqual(launch_dita_v2.main(), 7)
serve.assert_called_once()
self.assertTrue(bundle.closed)
finally:
if previous is None:
os.environ.pop("DITA_V2_LAUNCHER_MODE", None)
else:
os.environ["DITA_V2_LAUNCHER_MODE"] = previous
if __name__ == "__main__":
unittest.main()

View File

@@ -1,194 +0,0 @@
import math
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
ROOT = Path("/mnt/dolphinng5_predict")
sys.path.insert(0, str(ROOT / "nautilus_dolphin"))
sys.path.insert(1, str(ROOT))
if "nautilus_dolphin" in sys.modules:
pkg = sys.modules["nautilus_dolphin"]
pkg_file = str(getattr(pkg, "__file__", "") or "")
if not pkg_file.endswith("nautilus_dolphin/nautilus_dolphin/__init__.py"):
del sys.modules["nautilus_dolphin"]
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker, ACBConfig
from nautilus_dolphin.nautilus.alpha_bet_sizer import AlphaBetSizer
from nautilus_dolphin.nautilus.alpha_exit_manager import AlphaExitManager
from nautilus_dolphin.nautilus.alpha_signal_generator import AlphaSignalGenerator
from nautilus_dolphin.nautilus.esf_alpha_orchestrator import NDAlphaEngine
from nautilus_dolphin.nautilus.dolphin_actor import _trade_direction_from_config
def test_signal_generator_long_gate_and_dc_are_side_aware():
gen = AlphaSignalGenerator(use_direction_confirm=True)
rising_prices = [100.0, 100.1, 100.2, 100.3, 100.4, 100.5, 100.6, 101.0]
falling_prices = [101.0, 100.8, 100.6, 100.4, 100.2, 100.0, 99.8, 99.5]
long_sig = gen.generate(
vel_div=0.025,
vel_div_history=[0.012] * 10,
asset_price_history=rising_prices,
trade_direction=1,
)
assert long_sig.is_valid
assert long_sig.direction == 1
assert long_sig.dc_status == "CONFIRM"
contradicted = gen.generate(
vel_div=0.025,
vel_div_history=[0.012] * 10,
asset_price_history=falling_prices,
trade_direction=1,
)
assert not contradicted.is_valid
assert contradicted.dc_status == "SKIP_CONTRADICT"
def test_bet_sizer_trend_multiplier_is_direction_aware_for_long():
sizer = AlphaBetSizer(
base_fraction=0.20,
min_leverage=0.5,
max_leverage=8.0,
use_alpha_layers=True,
use_dynamic_leverage=True,
)
favorable_long = sizer.calculate_size(25000, 0.025, vel_div_trend=0.02, trade_direction=1)
adverse_long = sizer.calculate_size(25000, 0.025, vel_div_trend=-0.02, trade_direction=1)
favorable_short = sizer.calculate_size(25000, -0.035, vel_div_trend=-0.02, trade_direction=-1)
adverse_short = sizer.calculate_size(25000, -0.035, vel_div_trend=0.02, trade_direction=-1)
assert favorable_long["fraction"] > adverse_long["fraction"]
assert favorable_short["fraction"] > adverse_short["fraction"]
def test_ndalphaengine_enters_long_when_begin_day_direction_is_long():
engine = NDAlphaEngine(
initial_capital=1000.0,
use_asset_selection=False,
use_direction_confirm=False,
use_sp_fees=False,
use_sp_slippage=False,
use_ob_edge=False,
use_alpha_layers=False,
use_dynamic_leverage=False,
lookback=1,
)
engine.begin_day("2026-05-08", posture="APEX", direction=1)
res = engine.step_bar(0, vel_div=0.025, prices={"BTCUSDT": 100.0}, vol_regime_ok=True)
assert res["entry"] is not None
assert res["entry"]["direction"] == 1
assert engine.position is not None
assert engine.position.direction == 1
def test_ndalphaengine_short_default_preserved():
engine = NDAlphaEngine(
initial_capital=1000.0,
use_asset_selection=False,
use_direction_confirm=False,
use_sp_fees=False,
use_sp_slippage=False,
use_ob_edge=False,
use_alpha_layers=False,
use_dynamic_leverage=False,
lookback=1,
)
engine.begin_day("2026-05-08", posture="APEX")
res = engine.step_bar(0, vel_div=-0.035, prices={"BTCUSDT": 100.0}, vol_regime_ok=True)
assert res["entry"] is not None
assert res["entry"]["direction"] == -1
def test_acb_short_default_and_long_cache_are_side_separated():
acb = AdaptiveCircuitBreaker()
acb._w750_threshold = 0.001
bullish = {
"funding_btc": 0.0002,
"dvol_btc": 30.0,
"fng": 80.0,
"taker": 1.25,
"available": True,
}
short = acb._calculate_signals(bullish)
long = acb._calculate_signals(bullish, direction=1)
assert short["signals"] == pytest.approx(0.0)
assert long["signals"] == pytest.approx(4.0)
snap = dict(bullish, _acb_ready=True, _staleness_s={})
short_hz = acb.get_dynamic_boost_from_hz("2026-05-08", snap, w750_velocity=0.002, direction=-1)
long_hz = acb.get_dynamic_boost_from_hz("2026-05-08", snap, w750_velocity=0.002, direction=1)
assert short_hz["side"] == "SHORT"
assert long_hz["side"] == "LONG"
assert short_hz["boost"] == pytest.approx(1.0)
assert long_hz["boost"] == pytest.approx(1.0 + 0.5 * math.log1p(4.0))
assert acb.get_dynamic_boost_for_date("2026-05-08")["side"] == "SHORT"
assert acb.get_dynamic_boost_for_date("2026-05-08", direction=1)["side"] == "LONG"
def test_acb_short_threshold_regression_values_still_match_v6():
acb = AdaptiveCircuitBreaker()
factors = {
"funding_btc": -0.0002,
"dvol_btc": 85.0,
"fng": 20.0,
"taker": 0.75,
"available": True,
}
result = acb._calculate_signals(factors)
assert result["signals"] == pytest.approx(4.0)
assert result["severity"] == 7
def test_acb_ob_beta_modulation_is_side_aware():
acb = AdaptiveCircuitBreaker()
acb._w750_threshold = 0.001
calm_ob = SimpleNamespace(
get_macro=lambda: SimpleNamespace(regime_signal=-1, depth_velocity=0.1, cascade_count=0)
)
stress_ob = SimpleNamespace(
get_macro=lambda: SimpleNamespace(regime_signal=1, depth_velocity=-0.3, cascade_count=2)
)
long_calm = acb.get_dynamic_boost_from_hz(
"2026-05-08", {"_acb_ready": True, "_staleness_s": {}}, w750_velocity=0.002, ob_engine=calm_ob, direction=1
)
short_calm = acb.get_dynamic_boost_from_hz(
"2026-05-09", {"_acb_ready": True, "_staleness_s": {}}, w750_velocity=0.002, ob_engine=calm_ob, direction=-1
)
short_stress = acb.get_dynamic_boost_from_hz(
"2026-05-10", {"_acb_ready": True, "_staleness_s": {}}, w750_velocity=0.002, ob_engine=stress_ob, direction=-1
)
assert long_calm["beta"] == pytest.approx(1.0)
assert short_calm["beta"] == pytest.approx(0.68)
assert short_stress["beta"] == pytest.approx(1.0)
def test_exit_manager_optional_vd_exit_is_long_aware():
manager = AlphaExitManager(vd_enabled=True, vd_consec_bars=2)
manager.setup_position("long-1", entry_price=100.0, direction=1, entry_bar=0)
first = manager.evaluate("long-1", current_price=100.1, current_bar=1, vel_div=-0.02)
second = manager.evaluate("long-1", current_price=100.1, current_bar=2, vel_div=-0.02)
assert first["action"] == "HOLD"
assert second["action"] == "EXIT"
assert second["reason"] == "VD_INVALIDATION"
def test_prodgreen_direction_parser_is_explicit_and_case_insensitive():
assert _trade_direction_from_config("LONG_ONLY") == 1
assert _trade_direction_from_config("short_only") == -1
with pytest.raises(ValueError):
_trade_direction_from_config("bidirectional")

View File

@@ -1,313 +0,0 @@
from __future__ import annotations
import hashlib
import json
from dataclasses import dataclass, field
from typing import Dict, List
import math
import random
import pytest
EPS = 1e-9
@dataclass
class ExitLeg:
trade_id: str
chain_root_trade_id: str
exit_seq: int
exit_leg_id: str
chain_prev_leg_id: str
chain_head_leg_id: str
command_id: str
fraction: float
qty: float
exit_price: float
fee: float
net_pnl: float
remaining_after: float
reason: str
chain_token: str
@dataclass
class ParentTrade:
trade_id: str
side: str # SHORT | LONG
entry_price: float
entry_qty: float
remaining_qty: float
realized_pnl_total: float = 0.0
realized_fees_total: float = 0.0
exit_seq: int = 0
status: str = "OPEN" # OPEN | PARTIALLY_CLOSED | CLOSED
version: int = 0
legs: List[ExitLeg] = field(default_factory=list)
chain_root_trade_id: str = ""
chain_head_leg_id: str = ""
chain_prev_leg_id: str = ""
chain_token: str = ""
def _chain_token(payload: dict) -> str:
return hashlib.sha256(json.dumps(payload, sort_keys=True, separators=(",", ":"), default=str).encode()).hexdigest()
class MiniRetractionRuntime:
"""
Contract-reference runtime:
- all partial exits route through one handler
- idempotent by command_id
- financial accumulation by executed leg
"""
def __init__(self, fee_rate: float = 0.00055):
self.fee_rate = fee_rate
self.capital = 25_000.0
self.trades: Dict[str, ParentTrade] = {}
self.applied_commands: Dict[str, ExitLeg] = {}
def open_trade(self, trade_id: str, side: str, entry_price: float, qty: float) -> None:
assert trade_id not in self.trades
t = ParentTrade(
trade_id=trade_id,
side=side,
entry_price=entry_price,
entry_qty=qty,
remaining_qty=qty,
)
t.chain_root_trade_id = trade_id
t.chain_head_leg_id = f"{trade_id}:open"
t.chain_prev_leg_id = ""
t.chain_token = _chain_token({
"trade_id": trade_id,
"chain_root_trade_id": trade_id,
"chain_head_leg_id": t.chain_head_leg_id,
"chain_prev_leg_id": "",
"chain_seq": 0,
"side": side,
"entry_price": entry_price,
"entry_qty": qty,
"remaining_qty": qty,
"realized_pnl_total": 0.0,
"realized_fees_total": 0.0,
})
self.trades[trade_id] = t
def retract(
self,
trade_id: str,
*,
command_id: str,
fraction: float,
exit_price: float,
reason: str,
) -> ExitLeg | None:
if command_id in self.applied_commands:
return self.applied_commands[command_id]
if not (0 < fraction <= 1.0):
return None
t = self.trades.get(trade_id)
if not t or t.status == "CLOSED":
return None
expected = _chain_token({
"trade_id": t.trade_id,
"chain_root_trade_id": t.chain_root_trade_id or t.trade_id,
"chain_head_leg_id": t.chain_head_leg_id or f"{t.trade_id}:open",
"chain_prev_leg_id": t.chain_prev_leg_id or "",
"chain_seq": t.exit_seq,
"side": t.side,
"entry_price": t.entry_price,
"entry_qty": t.entry_qty,
"remaining_qty": t.remaining_qty,
"realized_pnl_total": t.realized_pnl_total,
"realized_fees_total": t.realized_fees_total,
})
if t.chain_token and t.chain_token != expected:
return None
requested_qty = t.remaining_qty * fraction
qty = min(max(requested_qty, 0.0), t.remaining_qty)
if qty <= EPS:
return None
sign = -1.0 if t.side == "SHORT" else 1.0
gross = sign * (exit_price - t.entry_price) * qty
fee = self.fee_rate * (t.entry_price * qty + exit_price * qty)
net = gross - fee
t.exit_seq += 1
t.version += 1
t.remaining_qty = max(0.0, t.remaining_qty - qty)
t.realized_pnl_total += net
t.realized_fees_total += fee
self.capital += net
t.status = "CLOSED" if t.remaining_qty <= EPS else "PARTIALLY_CLOSED"
prev_head = t.chain_head_leg_id or f"{t.trade_id}:open"
t.chain_prev_leg_id = prev_head
t.chain_head_leg_id = f"{t.trade_id}:x{t.exit_seq:03d}"
t.chain_token = _chain_token({
"trade_id": t.trade_id,
"chain_root_trade_id": t.chain_root_trade_id or t.trade_id,
"chain_head_leg_id": t.chain_head_leg_id,
"chain_prev_leg_id": prev_head,
"chain_seq": t.exit_seq,
"side": t.side,
"entry_price": t.entry_price,
"entry_qty": t.entry_qty,
"remaining_qty": t.remaining_qty,
"realized_pnl_total": t.realized_pnl_total,
"realized_fees_total": t.realized_fees_total,
})
leg = ExitLeg(
trade_id=t.trade_id,
chain_root_trade_id=t.chain_root_trade_id or t.trade_id,
exit_seq=t.exit_seq,
exit_leg_id=f"{t.trade_id}:x{t.exit_seq:03d}",
chain_prev_leg_id=prev_head,
chain_head_leg_id=t.chain_head_leg_id,
command_id=command_id,
fraction=fraction,
qty=qty,
exit_price=exit_price,
fee=fee,
net_pnl=net,
remaining_after=t.remaining_qty,
reason=reason,
chain_token=t.chain_token,
)
t.legs.append(leg)
self.applied_commands[command_id] = leg
return leg
def _assert_parent_invariants(t: ParentTrade) -> None:
total_qty = sum(l.qty for l in t.legs)
assert total_qty <= t.entry_qty + EPS
assert math.isclose(t.remaining_qty, max(0.0, t.entry_qty - total_qty), abs_tol=1e-8)
assert math.isclose(t.realized_pnl_total, sum(l.net_pnl for l in t.legs), rel_tol=0, abs_tol=1e-8)
assert math.isclose(t.realized_fees_total, sum(l.fee for l in t.legs), rel_tol=0, abs_tol=1e-8)
if t.legs:
assert t.chain_head_leg_id == t.legs[-1].exit_leg_id
assert t.chain_token == t.legs[-1].chain_token
if t.remaining_qty <= EPS:
assert t.status == "CLOSED"
elif t.legs:
assert t.status == "PARTIALLY_CLOSED"
else:
assert t.status == "OPEN"
def test_retract_default_half_then_close_preserves_lineage_and_math() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T1", "SHORT", 100.0, 10.0)
l1 = rt.retract("T1", command_id="c1", fraction=0.5, exit_price=99.0, reason="HOTKEY_RETRACT")
assert l1 is not None
assert l1.exit_leg_id == "T1:x001"
assert l1.qty == 5.0
assert l1.chain_prev_leg_id == "T1:open"
assert l1.chain_head_leg_id == "T1:x001"
l2 = rt.retract("T1", command_id="c2", fraction=1.0, exit_price=98.5, reason="HOTKEY_RETRACT")
assert l2 is not None
assert l2.exit_leg_id == "T1:x002"
assert l2.qty == 5.0
assert l2.chain_prev_leg_id == "T1:x001"
assert l2.chain_head_leg_id == "T1:x002"
t = rt.trades["T1"]
_assert_parent_invariants(t)
assert t.status == "CLOSED"
assert t.exit_seq == 2
def test_idempotent_command_does_not_double_execute() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T2", "SHORT", 50.0, 20.0)
first = rt.retract("T2", command_id="dup", fraction=0.5, exit_price=49.8, reason="V7_RETRACT")
second = rt.retract("T2", command_id="dup", fraction=0.5, exit_price=49.7, reason="V7_RETRACT")
assert first is not None
assert second is not None
assert first.exit_leg_id == second.exit_leg_id
assert len(rt.trades["T2"].legs) == 1
_assert_parent_invariants(rt.trades["T2"])
def test_invalid_fraction_rejected() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T3", "SHORT", 10.0, 10.0)
assert rt.retract("T3", command_id="a", fraction=0.0, exit_price=9.9, reason="HOTKEY_RETRACT") is None
assert rt.retract("T3", command_id="b", fraction=1.5, exit_price=9.9, reason="HOTKEY_RETRACT") is None
assert len(rt.trades["T3"].legs) == 0
_assert_parent_invariants(rt.trades["T3"])
def test_long_side_accounting_sign_is_correct() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T4", "LONG", 100.0, 2.0)
leg = rt.retract("T4", command_id="c", fraction=1.0, exit_price=101.0, reason="HOTKEY_RETRACT")
assert leg is not None
assert leg.net_pnl > 0
_assert_parent_invariants(rt.trades["T4"])
def test_over_many_random_partial_exits_invariants_hold() -> None:
rng = random.Random(1337)
rt = MiniRetractionRuntime()
rt.open_trade("T5", "SHORT", 120.0, 30.0)
for i in range(200):
t = rt.trades["T5"]
if t.status == "CLOSED":
break
# Biased toward smaller retractions; occasionally force full close
frac = 1.0 if i % 37 == 0 else max(0.01, min(0.99, rng.random() * 0.6))
px = 120.0 - rng.random() * 2.0
rt.retract("T5", command_id=f"cmd-{i}", fraction=frac, exit_price=px, reason="V7_RETRACT")
_assert_parent_invariants(rt.trades["T5"])
t = rt.trades["T5"]
# Must eventually close under forced 1.0 fractions
assert t.status == "CLOSED"
_assert_parent_invariants(t)
def test_capital_updates_are_leg_immediate() -> None:
rt = MiniRetractionRuntime()
start = rt.capital
rt.open_trade("T6", "SHORT", 200.0, 4.0)
l1 = rt.retract("T6", command_id="r1", fraction=0.5, exit_price=199.0, reason="HOTKEY_RETRACT")
assert l1 is not None
mid = rt.capital
assert not math.isclose(mid, start, abs_tol=1e-12)
l2 = rt.retract("T6", command_id="r2", fraction=1.0, exit_price=198.5, reason="HOTKEY_RETRACT")
assert l2 is not None
assert math.isclose(rt.capital, start + l1.net_pnl + l2.net_pnl, rel_tol=0, abs_tol=1e-8)
def test_command_on_closed_trade_is_noop() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T7", "SHORT", 100.0, 1.0)
rt.retract("T7", command_id="x1", fraction=1.0, exit_price=99.7, reason="HOTKEY_RETRACT")
t = rt.trades["T7"]
assert t.status == "CLOSED"
n = len(t.legs)
out = rt.retract("T7", command_id="x2", fraction=0.5, exit_price=99.6, reason="HOTKEY_RETRACT")
assert out is None
assert len(t.legs) == n
_assert_parent_invariants(t)
def test_tampered_chain_head_is_rejected() -> None:
rt = MiniRetractionRuntime()
rt.open_trade("T8", "SHORT", 75.0, 8.0)
first = rt.retract("T8", command_id="c1", fraction=0.5, exit_price=74.5, reason="HOTKEY_RETRACT")
assert first is not None
rt.trades["T8"].chain_head_leg_id = "T8:x999"
second = rt.retract("T8", command_id="c2", fraction=0.5, exit_price=74.0, reason="HOTKEY_RETRACT")
assert second is None
with pytest.raises(AssertionError):
_assert_parent_invariants(rt.trades["T8"])

Some files were not shown because too many files have changed in this diff Show More