feat(slot): BAT-030 — 자산 중심 cutover 완성 (dcy-020 BAT-δ, REQ-056·057·062·064·065)
M1 literal inactive 전면화: SlotState 5-state(idle/closed 별칭 제거), 마이그레이션 010(CHECK 교체+행 rewrite), ops_checks 를 transition_log 기반 재작성. M2 집행 매도 수량 브로커 정본(D3): dispatch 매도 qty=broker hldg_qty, 미러 fallback 없음, 브로커 0 → skip+WARN, 리스트 기반 dispatch(head-block 방지). M3 컬럼 drop + legacy pool 제거: 마이그레이션 011(trading_date·entry_count DROP), repo 계약 instance 스코프 개편(집행 큐 포함), preallocation 폐기(데몬 7스레드), admission 자산 중심 단일 경로(slot_id 정본), capacity 재정의(available=M−active), daily_symbol_entries 코드 경로 제거. M4 cutover 도구(D1·D4): trex-core-cutover(전체 rebuild, preflight/브로커 carry-over/ --allow-unmapped/--promote-unique/dry-run) + PG 리허설 테스트 8건. M5 baseline 마감: data-model/runtime-flows/project-structure/interface-contract 자산 중심 최종 갱신 + run-trex-core-daemon 스킬 정합. 검증: 단위 367 passed + ruff clean; PG-gated 29 passed(010/011 round-trip·드리프트 가드·cutover 리허설); RabbitMQ 21 passed; run-the-app e2e(test DB+실 MQ, M=1): reconcile 슬롯 생성→slot_id 매수→dedup→capacity_full→fill→already_active→ 매도→inactive(자산 바인딩 보존)→영구 슬롯 재활성 전 구간 green. 실 cutover 는 운영자 실행(REL 단계, D4). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,8 +4,12 @@ A single background execution loop (invariant A single writer, invariant B
|
||||
single broker session) that, per tick:
|
||||
dispatch: claim pending slots with broker_order_id IS NULL (option A queue),
|
||||
submit the order, record broker_order_id (idempotency guard).
|
||||
dcy-020/REQ-062 (BAT-030): a SELL is dispatched with the broker
|
||||
balance quantity (hldg_qty) as the single source of truth — never
|
||||
the slot's mirror qty. Broker flat (qty 0) -> skip + WARN
|
||||
(divergence surfaces via reconcile, REQ-062 A).
|
||||
poll: observe fills on dispatched pending slots and transition the slot
|
||||
(pending_buy->holding / pending_sell->closed), or roll back on reject.
|
||||
(pending_buy->holding / pending_sell->inactive), or roll back on reject.
|
||||
On a successful fill, enqueue a LedgerWorkItem (slot transition is
|
||||
kept ON the critical path; the ledger write is eventually-consistent
|
||||
via a separate writer — REQ-019).
|
||||
@@ -47,6 +51,7 @@ class ExecutionResult:
|
||||
ledger_enqueued: int = 0 # REQ-019: ledger work items appended this tick
|
||||
ledger_skipped: int = 0 # REQ-018 AC-4: fills without portfolio_id
|
||||
stale_pending: int = 0 # dcy-013 / REQ-014 FR-3: pending orders over threshold
|
||||
sell_skipped_no_balance: int = 0 # dcy-020 / REQ-062: broker flat -> sell skipped
|
||||
|
||||
|
||||
class ExecutionService:
|
||||
@@ -58,12 +63,16 @@ class ExecutionService:
|
||||
ledger_outbox: Optional[LedgerOutbox] = None,
|
||||
event_emitter: Optional[SlotEventEmitter] = None,
|
||||
stale_pending_threshold_sec: Optional[int] = None,
|
||||
instance_key: str = "default",
|
||||
) -> None:
|
||||
self._svc = slot_service
|
||||
self._repo = repo
|
||||
self._broker = broker
|
||||
self._ledger_outbox = ledger_outbox
|
||||
self._event_emitter = event_emitter
|
||||
# dcy-020/REQ-056 (BAT-030): dispatch/poll are instance-scoped so two
|
||||
# daemons sharing a DB never touch each other's orders.
|
||||
self._instance_key = instance_key
|
||||
# dcy-013 / REQ-014 (issue #9): Alert-only pending-order stale watchdog.
|
||||
# <=0 or None disables it. When set, a dispatched-pending slot whose
|
||||
# last update is older than the threshold is surfaced (WARN + counter)
|
||||
@@ -93,12 +102,32 @@ class ExecutionService:
|
||||
|
||||
# ------------------------------------------------------------
|
||||
def _dispatch(self, r: ExecutionResult) -> None:
|
||||
for _ in range(_MAX_DISPATCH_PER_TICK):
|
||||
slot = self._repo.find_pending_for_dispatch()
|
||||
if slot is None:
|
||||
return
|
||||
sell_balance: Optional[dict[str, int]] = None # lazy, once per tick
|
||||
for slot in self._repo.list_undispatched_pending(
|
||||
self._instance_key, _MAX_DISPATCH_PER_TICK
|
||||
):
|
||||
side = "buy" if slot.status is SlotState.PENDING_BUY else "sell"
|
||||
qty = int(slot.qty or 0)
|
||||
if side == "sell":
|
||||
# dcy-020 / REQ-062 (BAT-030, D3): the sell quantity is the
|
||||
# BROKER balance (hldg_qty) — the slot's mirror qty is never
|
||||
# trusted. Broker flat -> skip dispatch + WARN; the divergence
|
||||
# surfaces via reconcile_core_holdings (REQ-062 A). No mirror
|
||||
# fallback by design.
|
||||
if sell_balance is None:
|
||||
sell_balance = {
|
||||
b.symbol: int(b.hldg_qty) for b in self._broker.get_balance()
|
||||
}
|
||||
qty = sell_balance.get(slot.symbol or "", 0)
|
||||
if qty <= 0:
|
||||
r.sell_skipped_no_balance += 1
|
||||
_log.warning(
|
||||
"sell dispatch skipped: slot=%s symbol=%s broker balance is 0 "
|
||||
"(divergence — no order submitted; operator/reconcile attention)",
|
||||
slot.slot_id, slot.symbol,
|
||||
)
|
||||
continue
|
||||
else:
|
||||
qty = int(slot.qty or 0)
|
||||
# dcy-012 / REQ-013: side-agnostic — use the slot's pending-order
|
||||
# limit_price only. A sell never reuses a buy limit (issue #10):
|
||||
# limit_price is cleared on entering HOLDING, so a market sell
|
||||
@@ -123,7 +152,7 @@ class ExecutionService:
|
||||
|
||||
def _poll(self, r: ExecutionResult) -> None:
|
||||
now = datetime.now(tz=timezone.utc)
|
||||
for slot in self._repo.list_dispatched_pending():
|
||||
for slot in self._repo.list_dispatched_pending(self._instance_key):
|
||||
side = "buy" if slot.status is SlotState.PENDING_BUY else "sell"
|
||||
fs = self._broker.poll_fill(slot.broker_order_id, side=side, symbol=slot.symbol or "")
|
||||
if fs.is_filled:
|
||||
@@ -153,9 +182,9 @@ class ExecutionService:
|
||||
avg_price=fs.avg_price, qty=float(fs.filled_qty) or slot.qty,
|
||||
)
|
||||
event_type = "slot_filled_buy"
|
||||
else: # PENDING_SELL
|
||||
else: # PENDING_SELL — asset slot returns to inactive (dcy-020/REQ-057)
|
||||
updated = self._svc.transit_slot(
|
||||
slot.slot_id, SlotState.PENDING_SELL, SlotState.CLOSED,
|
||||
slot.slot_id, SlotState.PENDING_SELL, SlotState.INACTIVE,
|
||||
reason="sell_filled", actor="execution_loop",
|
||||
)
|
||||
event_type = "slot_filled_sell"
|
||||
@@ -271,9 +300,9 @@ class ExecutionService:
|
||||
try:
|
||||
if slot.status is SlotState.PENDING_BUY:
|
||||
self._svc.transit_slot(
|
||||
slot.slot_id, SlotState.PENDING_BUY, SlotState.IDLE,
|
||||
slot.slot_id, SlotState.PENDING_BUY, SlotState.INACTIVE,
|
||||
reason=reason, actor="execution_loop",
|
||||
) # idle reset clears broker_order_id in the domain
|
||||
) # inactive reset clears broker_order_id in the domain
|
||||
else: # PENDING_SELL rejected -> back to holding, clear order for re-dispatch
|
||||
self._svc.transit_slot(
|
||||
slot.slot_id, SlotState.PENDING_SELL, SlotState.HOLDING,
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
"""Intent application service (REQ-007).
|
||||
"""Intent application service (REQ-007; dcy-020/REQ-059·061 asset-centric).
|
||||
|
||||
Turns a buy/sell intent submitted over HTTP into a single-writer slot
|
||||
Turns a buy/sell intent submitted over messaging into a single-writer slot
|
||||
transition, with intent_id idempotency backed by a durable intent_log.
|
||||
|
||||
Idempotency model (dcy-002 OQ-1): `intent_store.try_insert` is the dedup
|
||||
gate. On a duplicate intent_id it returns the prior decision without
|
||||
re-driving any transition (NFR-3). Slot transitions go exclusively through
|
||||
SlotService (advisory-lock serialized) so the single-writer invariant
|
||||
holds (NFR-2). Concurrency on the last idle slot is resolved by retrying
|
||||
on SlotStateMismatchError.
|
||||
holds (NFR-2).
|
||||
|
||||
dcy-020 (BAT-030): admission is asset-centric only — the legacy pool path
|
||||
(daily preallocation + idle∪closed candidate selection) was removed at the
|
||||
cutover. Buy is keyed by slot_id (REQ-061 canonical; portfolio_id is the
|
||||
legacy dual-accept mapping) and admits FCFS under the `max_active_slots` cap
|
||||
M when configured (no cap when unset).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date, datetime
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any, Optional
|
||||
from uuid import UUID
|
||||
@@ -26,7 +31,7 @@ from trex_core.domain.slot import (
|
||||
SlotStateMismatchError,
|
||||
)
|
||||
|
||||
_MAX_IDLE_RETRY = 8
|
||||
_MAX_ADMIT_RETRY = 8
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -45,15 +50,11 @@ class IntentDecision:
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BuyIntent:
|
||||
# dcy-011 / REQ-007: portfolio_id (= asset_id) is the required canonical key.
|
||||
# symbol is no longer a client input — it is resolved server-side from the
|
||||
# universe (SnapshotStore) at admission. It remains here as the resolved
|
||||
# value carried onto the slot.
|
||||
# dcy-020 / REQ-061: slot_id is the canonical buy key (snapshot slots[]
|
||||
# round-trip); portfolio_id is accepted as a legacy dual-accept mapping.
|
||||
# symbol is not a client input — it is resolved server-side from the
|
||||
# universe at admission and carried onto the slot.
|
||||
intent_id: str
|
||||
trading_date: date
|
||||
# dcy-020 / REQ-061 (BAT-029): asset-centric contract key. slot_id is the
|
||||
# canonical buy key (snapshot slots[] round-trip); portfolio_id is retained
|
||||
# for the legacy pool path and as dual-accept in asset-centric mode.
|
||||
portfolio_id: Optional[str] = None
|
||||
slot_id: Optional[UUID] = None
|
||||
symbol: Optional[str] = None
|
||||
@@ -100,9 +101,8 @@ class IntentService:
|
||||
# callable(portfolio_id) -> Optional[str]. When set, admission resolves symbol
|
||||
# from the universe and rejects unknown_portfolio if absent.
|
||||
self._resolve_symbol = resolve_symbol
|
||||
# dcy-020 / REQ-059 (BAT-028): asset-centric mode gate. When max_active_slots
|
||||
# is set, buy admission activates the asset's permanent slot under an M cap
|
||||
# (FCFS, no eviction); otherwise the legacy pool path runs (unchanged).
|
||||
# dcy-020 / REQ-059·060: M cap. None = no capacity cap (the operator is
|
||||
# expected to set max_active_slots in the instance TOML — design §5).
|
||||
self._instance_key = instance_key
|
||||
self._max_active_slots = max_active_slots
|
||||
|
||||
@@ -116,14 +116,14 @@ class IntentService:
|
||||
|
||||
def submit_buy(self, intent: BuyIntent) -> IntentDecision:
|
||||
payload = {
|
||||
"trading_date": intent.trading_date.isoformat(),
|
||||
"slot_id": str(intent.slot_id) if intent.slot_id else None,
|
||||
"portfolio_id": intent.portfolio_id,
|
||||
"symbol": intent.symbol,
|
||||
"qty": intent.qty,
|
||||
"allocated_capital": intent.allocated_capital,
|
||||
"entry_price": intent.entry_price,
|
||||
"strategy_id": intent.strategy_id,
|
||||
"sell_strategy_id": intent.sell_strategy_id,
|
||||
"portfolio_id": intent.portfolio_id,
|
||||
}
|
||||
prior = self._dedup(intent.intent_id, "buy", payload)
|
||||
if prior is not None:
|
||||
@@ -163,68 +163,14 @@ class IntentService:
|
||||
return IntentDecision(intent_id, rec.status, rec.slot_id, rec.reason)
|
||||
|
||||
def _admit_buy(self, intent: BuyIntent) -> IntentDecision:
|
||||
# dcy-020 / REQ-061 (BAT-029): asset-centric mode keys buy by slot_id
|
||||
# (legacy portfolio_id dual-accept); the target slot + symbol are resolved
|
||||
# inside the asset-centric path.
|
||||
if self._max_active_slots is not None:
|
||||
return self._admit_buy_asset_centric(intent)
|
||||
# legacy pool path (dcy-011 / REQ-007): portfolio_id required; symbol is
|
||||
# resolved server-side from the universe.
|
||||
if not intent.portfolio_id:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "portfolio_id_required")
|
||||
symbol = intent.symbol
|
||||
if self._resolve_symbol is not None:
|
||||
symbol = self._resolve_symbol(intent.portfolio_id)
|
||||
if not symbol:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "unknown_portfolio")
|
||||
elif not symbol:
|
||||
# No resolver wired and no symbol carried — cannot determine the order target.
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "unknown_portfolio")
|
||||
# Retry on the last-candidate race: another intent may claim the slot
|
||||
# between our find and transit (SlotStateMismatchError).
|
||||
# dcy-019 / REQ-007: admission candidate is idle ∪ closed (closed-slot
|
||||
# reallocation), ordered by entry_count asc → updated_at asc → slot_id.
|
||||
# The candidate may already be CLOSED, so the transit's from_state must
|
||||
# follow the slot's current status rather than a hardcoded IDLE.
|
||||
for _ in range(_MAX_IDLE_RETRY):
|
||||
slot = self._repo.find_allocatable_slot(intent.trading_date)
|
||||
if slot is None:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "no_idle_slot")
|
||||
try:
|
||||
updated = self._svc.transit_slot(
|
||||
slot.slot_id,
|
||||
slot.status,
|
||||
SlotState.PENDING_BUY,
|
||||
reason="buy_intent",
|
||||
actor="intent_api",
|
||||
symbol=symbol,
|
||||
qty=intent.qty,
|
||||
allocated_capital=intent.allocated_capital,
|
||||
# dcy-012 / REQ-007: buy entry price maps to the slot's
|
||||
# side-agnostic pending limit_price (cleared on fill→HOLDING).
|
||||
limit_price=intent.entry_price,
|
||||
strategy_id=intent.strategy_id,
|
||||
sell_strategy_id=intent.sell_strategy_id,
|
||||
portfolio_id=intent.portfolio_id,
|
||||
forced_close_at=intent.forced_close_at,
|
||||
stop_loss_price=intent.stop_loss_price,
|
||||
take_profit_price=intent.take_profit_price,
|
||||
extra={"intent_id": intent.intent_id, "symbol": symbol},
|
||||
)
|
||||
except SlotStateMismatchError:
|
||||
continue # slot was claimed concurrently; try another idle slot
|
||||
return IntentDecision(intent.intent_id, "admitted", updated.slot_id, None)
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "idle_slot_contended")
|
||||
|
||||
# ------------------------------------------------------------
|
||||
def _admit_buy_asset_centric(self, intent: BuyIntent) -> IntentDecision:
|
||||
"""dcy-020 / REQ-059·REQ-061: activate the slot under the M cap.
|
||||
"""dcy-020 / REQ-059·REQ-061: activate the asset's permanent slot.
|
||||
|
||||
Contract key is slot_id (REQ-061); portfolio_id is accepted as a legacy
|
||||
dual-accept mapping. Symbol is resolved from the slot's portfolio_id.
|
||||
FCFS + no eviction: already-active -> already_active; at M -> capacity_full
|
||||
(existing holdings untouched, incl. over-cap). Single intent thread +
|
||||
instance-scoped advisory lock serialize admissions (dcy-020 OQ-1)."""
|
||||
FCFS + no eviction: already-active -> already_active; at M ->
|
||||
capacity_full (existing holdings untouched, incl. over-cap). Single
|
||||
intent thread + instance-scoped advisory lock serialize admissions
|
||||
(dcy-020 OQ-1)."""
|
||||
_ACTIVE = (SlotState.PENDING_BUY, SlotState.HOLDING, SlotState.PENDING_SELL)
|
||||
|
||||
def _resolve(sl):
|
||||
@@ -233,7 +179,7 @@ class IntentService:
|
||||
return self._resolve_symbol(pid)
|
||||
return intent.symbol or sl.symbol
|
||||
|
||||
for _ in range(_MAX_IDLE_RETRY):
|
||||
for _ in range(_MAX_ADMIT_RETRY):
|
||||
# resolve the target slot: slot_id (canonical) or portfolio_id (legacy)
|
||||
if intent.slot_id is not None:
|
||||
slot = self._repo.fetch_slot(intent.slot_id)
|
||||
@@ -250,7 +196,8 @@ class IntentService:
|
||||
symbol = _resolve(slot)
|
||||
if not symbol:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "unknown_portfolio")
|
||||
if self._repo.count_active(self._instance_key) >= self._max_active_slots:
|
||||
if (self._max_active_slots is not None
|
||||
and self._repo.count_active(self._instance_key) >= self._max_active_slots):
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "capacity_full")
|
||||
try:
|
||||
updated = self._svc.transit_slot(
|
||||
@@ -262,6 +209,8 @@ class IntentService:
|
||||
symbol=symbol,
|
||||
qty=intent.qty,
|
||||
allocated_capital=intent.allocated_capital,
|
||||
# dcy-012 / REQ-007: buy entry price maps to the slot's
|
||||
# side-agnostic pending limit_price (cleared on fill→HOLDING).
|
||||
limit_price=intent.entry_price,
|
||||
strategy_id=intent.strategy_id,
|
||||
sell_strategy_id=intent.sell_strategy_id,
|
||||
|
||||
@@ -13,7 +13,7 @@ from datetime import date as date_cls
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from trex_core.application.portfolio import GLOBAL_KILLSWITCH, KillSwitchStore
|
||||
from trex_core.domain.slot import SlotRepository, SlotService, SlotState
|
||||
from trex_core.domain.slot import SlotRepository, SlotService
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
_ACTOR = "kill-switch"
|
||||
@@ -28,10 +28,12 @@ class KillSwitchService:
|
||||
slot_repo: SlotRepository,
|
||||
slot_service: SlotService,
|
||||
killswitch_store: KillSwitchStore,
|
||||
instance_key: str = "default",
|
||||
) -> None:
|
||||
self._repo = slot_repo
|
||||
self._svc = slot_service
|
||||
self._store = killswitch_store
|
||||
self._instance_key = instance_key # dcy-020/REQ-056: instance-scoped sweep
|
||||
|
||||
@staticmethod
|
||||
def _trading_date(now: datetime) -> date_cls:
|
||||
@@ -50,8 +52,8 @@ class KillSwitchService:
|
||||
scope: "portfolio" (requires portfolio_id) | "global".
|
||||
"""
|
||||
now = now or datetime.now(tz=timezone.utc)
|
||||
day = self._trading_date(now)
|
||||
holding = [s for s in self._repo.list_slots(day) if s.status is SlotState.HOLDING]
|
||||
day = self._trading_date(now) # day-scoped exclusion key (REQ-044)
|
||||
holding = self._repo.list_holding_slots(self._instance_key)
|
||||
if scope == "portfolio":
|
||||
holding = [s for s in holding if s.portfolio_id == portfolio_id]
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ from trex_core.domain.slot import (
|
||||
SlotNotFoundError,
|
||||
SlotRepository,
|
||||
SlotService,
|
||||
SlotState,
|
||||
)
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
@@ -72,12 +71,12 @@ def make_intent_rpc_handler(
|
||||
intent_type = str(body.get("intent_type") or "").lower()
|
||||
try:
|
||||
if intent_type == "buy":
|
||||
# dcy-011 / REQ-021: buy payload carries portfolio_id (legacy) or,
|
||||
# in asset-centric mode (dcy-020/REQ-061), slot_id (canonical key).
|
||||
# symbol is NOT a client field — resolved from the universe.
|
||||
# dcy-020 / REQ-061: buy is keyed by slot_id (canonical);
|
||||
# portfolio_id is the legacy dual-accept mapping. symbol is NOT
|
||||
# a client field — resolved from the universe. trading_date was
|
||||
# dropped from the contract at the cutover (BAT-030).
|
||||
buy = BuyIntent(
|
||||
intent_id=intent_id,
|
||||
trading_date=_parse_date(body.get("trading_date")),
|
||||
portfolio_id=_opt_str(body.get("portfolio_id")) or None,
|
||||
slot_id=(UUID(str(body["slot_id"])) if body.get("slot_id") else None),
|
||||
qty=_opt_float(body.get("qty")),
|
||||
@@ -166,9 +165,10 @@ def make_snapshot_rpc_handler(
|
||||
slot_repo: SlotRepository,
|
||||
slot_service: SlotService,
|
||||
*,
|
||||
instance_key: str = "default",
|
||||
snapshot_store: Optional[SnapshotStore] = None,
|
||||
pause_store: Optional[PauseStore] = None,
|
||||
default_total_slots: int = 10,
|
||||
max_active_slots: Optional[int] = None,
|
||||
watchdog_store: Optional[WatchdogStore] = None,
|
||||
watchdog_timeout_sec: int = 120,
|
||||
killswitch_store=None,
|
||||
@@ -206,9 +206,8 @@ def make_snapshot_rpc_handler(
|
||||
if str(body.get("actor") or "").lower() == "operator":
|
||||
touch_watchdog = False
|
||||
if watchdog_store is not None and touch_watchdog:
|
||||
for s in slot_repo.list_slots(day):
|
||||
if s.status is SlotState.HOLDING:
|
||||
watchdog_store.update(str(s.slot_id))
|
||||
for s in slot_repo.list_holding_slots(instance_key):
|
||||
watchdog_store.update(str(s.slot_id))
|
||||
|
||||
# --- BAT-008 envelope path (snapshot store wired) ---
|
||||
if snapshot_store is not None and pause_store is not None:
|
||||
@@ -218,45 +217,49 @@ def make_snapshot_rpc_handler(
|
||||
except ValueError:
|
||||
view = View.ALL
|
||||
strategy_key = _opt_str(body.get("strategy_key"))
|
||||
cap = slot_service.compute_slot_capacity(day, default_total_slots)
|
||||
# dcy-020 / REQ-059: available = remaining admission headroom under
|
||||
# the M cap (max(0, M − active)); inactive count when no cap.
|
||||
cap = slot_service.compute_slot_capacity(max_active_slots)
|
||||
envelope = build_snapshot_envelope(
|
||||
store=snapshot_store,
|
||||
pause_store=pause_store,
|
||||
slot_repo=slot_repo,
|
||||
available_slots=cap.idle_count,
|
||||
instance_key=instance_key,
|
||||
available_slots=cap.available_count,
|
||||
view=view,
|
||||
strategy_key=strategy_key,
|
||||
trading_date=day,
|
||||
killswitch_store=killswitch_store,
|
||||
)
|
||||
payload = envelope.to_dict()
|
||||
_attach_watchdog(payload, slot_repo, day, watchdog_store, watchdog_timeout_sec)
|
||||
_attach_watchdog(payload, slot_repo, instance_key, watchdog_store, watchdog_timeout_sec)
|
||||
_apply_cash_estimate(payload, ledger_outbox)
|
||||
return RpcResponse(payload=payload)
|
||||
|
||||
# --- BAT-007 legacy stub-shape path ---
|
||||
view_raw = str(body.get("view") or "all")
|
||||
strategy_filter = _opt_str(body.get("strategy_id"))
|
||||
slots = slot_repo.list_slots(day)
|
||||
slots = slot_repo.list_slots(instance_key)
|
||||
if view_raw == "open":
|
||||
slots = [s for s in slots if s.status.value in ("pending_buy", "holding", "pending_sell")]
|
||||
if strategy_filter is not None:
|
||||
slots = [s for s in slots if s.strategy_id == strategy_filter]
|
||||
cap = slot_service.compute_slot_capacity(day, max(1, len(slot_repo.list_slots(day))))
|
||||
cap = slot_service.compute_slot_capacity(max_active_slots)
|
||||
payload = {
|
||||
"view": view_raw,
|
||||
"strategy_id": strategy_filter,
|
||||
"trading_date": day.isoformat(),
|
||||
"slots": [_slot_to_dict(s) for s in slots],
|
||||
"capacity": {
|
||||
"idle_count": cap.idle_count,
|
||||
"inactive_count": cap.inactive_count,
|
||||
"holding_count": cap.holding_count,
|
||||
"pending_count": cap.pending_count,
|
||||
"active_count": cap.active_count,
|
||||
"available_count": cap.available_count,
|
||||
"total_slots": cap.total_slots,
|
||||
"scarcity_ratio": cap.scarcity_ratio,
|
||||
},
|
||||
}
|
||||
_attach_watchdog(payload, slot_repo, day, watchdog_store, watchdog_timeout_sec)
|
||||
_attach_watchdog(payload, slot_repo, instance_key, watchdog_store, watchdog_timeout_sec)
|
||||
return RpcResponse(payload=payload)
|
||||
|
||||
return handler
|
||||
@@ -283,7 +286,7 @@ def _apply_cash_estimate(payload, ledger_outbox) -> None:
|
||||
return
|
||||
|
||||
|
||||
def _attach_watchdog(payload, slot_repo, day, watchdog_store, timeout_sec) -> None:
|
||||
def _attach_watchdog(payload, slot_repo, instance_key, watchdog_store, timeout_sec) -> None:
|
||||
"""Attach holding-slot watchdog state to a snapshot response (dcy-017/REQ-034).
|
||||
|
||||
`watchdog` = list of {slot_id, portfolio_id, symbol, expired, last_seen} for
|
||||
@@ -293,9 +296,7 @@ def _attach_watchdog(payload, slot_repo, day, watchdog_store, timeout_sec) -> No
|
||||
return
|
||||
try:
|
||||
states = []
|
||||
for s in slot_repo.list_slots(day):
|
||||
if s.status is not SlotState.HOLDING:
|
||||
continue
|
||||
for s in slot_repo.list_holding_slots(instance_key):
|
||||
sid = str(s.slot_id)
|
||||
last = watchdog_store.last_seen(sid)
|
||||
states.append({
|
||||
@@ -441,7 +442,6 @@ def slot_transition_event_payload(slot, *, event_type: str) -> dict:
|
||||
"qty": float(slot.qty) if slot.qty is not None else None,
|
||||
"avg_price": float(slot.avg_price) if slot.avg_price is not None else None,
|
||||
"portfolio_id": slot.portfolio_id,
|
||||
"trading_date": slot.trading_date.isoformat(),
|
||||
"sequence_at": (slot.updated_at or datetime.now(tz=timezone.utc)).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@@ -308,7 +308,6 @@ def slot_to_dict(s) -> dict:
|
||||
"""
|
||||
return {
|
||||
"slot_id": str(s.slot_id),
|
||||
"trading_date": s.trading_date.isoformat(),
|
||||
"status": s.status.value,
|
||||
"symbol": s.symbol,
|
||||
"qty": float(s.qty) if s.qty is not None else None,
|
||||
@@ -344,6 +343,7 @@ def build_snapshot_envelope(
|
||||
store: SnapshotStore,
|
||||
pause_store: PauseStore,
|
||||
slot_repo,
|
||||
instance_key: str = "default",
|
||||
available_slots: int,
|
||||
view: View = View.ALL,
|
||||
strategy_key: Optional[str] = None,
|
||||
@@ -352,14 +352,15 @@ def build_snapshot_envelope(
|
||||
) -> SnapshotEnvelope:
|
||||
"""Compose base(view) − mask(pending·warning·pause·killswitch) − strategy_key.
|
||||
|
||||
`slot_repo` is the SlotRepository used to read holding/pending state.
|
||||
`trading_date` defaults to UTC today (matches the daemon's preallocate).
|
||||
`slot_repo` is the SlotRepository used to read holding/pending state —
|
||||
instance-scoped (dcy-020/REQ-056). `trading_date` (default UTC today) is
|
||||
used only for the day-scoped kill-switch exclusion mask.
|
||||
`killswitch_store` (REQ-046) adds the day-scoped kill-switch exclusion to
|
||||
the mask: a `GLOBAL_KILLSWITCH` ("*") entry masks the whole universe.
|
||||
"""
|
||||
# Read core authoritative slot state once.
|
||||
day = trading_date or datetime.now(tz=timezone.utc).date()
|
||||
slots = slot_repo.list_slots(day)
|
||||
slots = slot_repo.list_slots(instance_key)
|
||||
holding_pids: set[str] = set()
|
||||
pending_pids: set[str] = set()
|
||||
holding_qty: dict[str, float] = {}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
"""Daily slot preallocation scheduler (dcy-014 / REQ-005, issue #14).
|
||||
|
||||
trading_date is computed in a configurable timezone (default Asia/Seoul) so the
|
||||
DB trading_date matches the local trading calendar, while all timestamps stay
|
||||
UTC (NFR-1). Modeled on SnapshotScheduler (REQ-030): a tick loop fires once per
|
||||
TZ-day at/after the configured trigger time, calling the idempotent
|
||||
`preallocate_daily_slots` so the next day's slots are created without a daemon
|
||||
restart (NFR-2/NFR-3).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from datetime import date, datetime, time, timezone
|
||||
from typing import Callable, Optional, Protocol
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class _Preallocator(Protocol):
|
||||
def preallocate_daily_slots(self, trading_date: date, count: int) -> int:
|
||||
...
|
||||
|
||||
|
||||
class DailyPreallocationScheduler:
|
||||
"""Background-thread-friendly daily preallocation. Use `run_once()` for tests."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
slot_service: _Preallocator,
|
||||
total_slots: int,
|
||||
tz: timezone,
|
||||
schedule_time: time = time(hour=0, minute=0),
|
||||
clock: Optional[Callable[[], datetime]] = None,
|
||||
) -> None:
|
||||
self._svc = slot_service
|
||||
self._total = total_slots
|
||||
self._tz = tz # tzinfo used to derive trading_date (default Asia/Seoul)
|
||||
self._schedule = schedule_time
|
||||
self._clock = clock or (lambda: datetime.now(tz=timezone.utc))
|
||||
self._last_fired_date: Optional[date] = None
|
||||
|
||||
# ---- callable from a daemon thread loop --------------------------------
|
||||
def run_forever(self, stop: threading.Event, *, tick_sec: float = 60.0) -> None:
|
||||
# Boot: ensure the current TZ-day's slots exist immediately (idempotent),
|
||||
# then enter the daily cadence for subsequent day boundaries.
|
||||
self.run_once()
|
||||
while not stop.wait(tick_sec):
|
||||
try:
|
||||
self._maybe_tick()
|
||||
except Exception: # noqa: BLE001 - loop must survive transient errors
|
||||
_log.exception("preallocation scheduler tick failed")
|
||||
|
||||
def _maybe_tick(self) -> None:
|
||||
now_tz = self._clock().astimezone(self._tz)
|
||||
target = datetime.combine(now_tz.date(), self._schedule, tzinfo=self._tz)
|
||||
if now_tz < target:
|
||||
return # before today's trigger time
|
||||
if self._last_fired_date is not None and self._last_fired_date == now_tz.date():
|
||||
return # already created today's slots
|
||||
self.run_once()
|
||||
|
||||
# ---- the actual preallocation -----------------------------------------
|
||||
def run_once(self) -> date:
|
||||
"""Preallocate slots for the current TZ trading_date (idempotent). Returns it."""
|
||||
trading_date = self._clock().astimezone(self._tz).date()
|
||||
created = self._svc.preallocate_daily_slots(trading_date, self._total)
|
||||
if created:
|
||||
_log.info("preallocated %d slots for trading_date=%s", created, trading_date)
|
||||
self._last_fired_date = trading_date
|
||||
return trading_date
|
||||
@@ -25,7 +25,7 @@ from datetime import datetime, time as time_cls, timedelta, timezone
|
||||
|
||||
from trex_core.application.broker_port import BrokerPort
|
||||
from trex_core.application.watchdog import WatchdogStore
|
||||
from trex_core.domain.slot import SlotRepository, SlotService, SlotState
|
||||
from trex_core.domain.slot import SlotRepository, SlotService
|
||||
from trex_core.domain.slot.models import Slot
|
||||
from trex_core.runtime.config import SafetySellConfig
|
||||
|
||||
@@ -133,13 +133,10 @@ class SafetySellService:
|
||||
))
|
||||
|
||||
# ---------------------------------------------------------------- helpers
|
||||
@staticmethod
|
||||
def _trading_date(now: datetime) -> date_cls:
|
||||
return now.astimezone(timezone.utc).date()
|
||||
|
||||
def _holding(self, now: datetime) -> list[Slot]:
|
||||
return [s for s in self._repo.list_slots(self._trading_date(now))
|
||||
if s.status is SlotState.HOLDING]
|
||||
# dcy-020 (BAT-030): permanent asset slots — instance-scoped holding
|
||||
# sweep (the day-scoped pool listing was removed with trading_date).
|
||||
return self._repo.list_holding_slots(self._instance_key)
|
||||
|
||||
def _watchdog_expired(self, slot: Slot) -> bool:
|
||||
return self._watchdog.is_expired(str(slot.slot_id), self._cfg.watchdog_timeout_sec)
|
||||
|
||||
@@ -40,8 +40,6 @@ class SnapshotScheduler:
|
||||
slot_repo=None,
|
||||
slot_service=None,
|
||||
instance_key: str = "default",
|
||||
reconcile_enabled: bool = False,
|
||||
trading_date_fn: Optional[Callable[[], object]] = None,
|
||||
) -> None:
|
||||
self._read = read_port
|
||||
self._store = store
|
||||
@@ -50,13 +48,12 @@ class SnapshotScheduler:
|
||||
self._clock = clock or (lambda: datetime.now(tz=timezone.utc))
|
||||
self._daemon_id: Optional[str] = None
|
||||
self._last_fired_date: Optional[datetime] = None
|
||||
# dcy-020 / REQ-058 (BAT-028): asset-centric slot reconcile after each
|
||||
# universe fetch (only when enabled = asset-centric mode).
|
||||
# dcy-020 / REQ-058 (BAT-030): asset-centric slot reconcile after each
|
||||
# universe fetch — always on when slot_repo is wired (the reconcile is
|
||||
# what creates/retires the permanent asset slots).
|
||||
self._slot_repo = slot_repo
|
||||
self._slot_service = slot_service
|
||||
self._instance_key = instance_key
|
||||
self._reconcile_enabled = reconcile_enabled
|
||||
self._trading_date_fn = trading_date_fn
|
||||
self._last_universe_size: Optional[int] = None
|
||||
|
||||
# ---- callable from a daemon thread loop --------------------------------
|
||||
@@ -112,7 +109,7 @@ class SnapshotScheduler:
|
||||
reconcile_positions(store=self._store, broker=self._broker)
|
||||
except Exception: # noqa: BLE001
|
||||
_log.exception("reconciliation pass failed")
|
||||
if self._reconcile_enabled and self._slot_repo is not None:
|
||||
if self._slot_repo is not None:
|
||||
try:
|
||||
self._reconcile_slots(items)
|
||||
except Exception: # noqa: BLE001
|
||||
@@ -144,12 +141,8 @@ class SnapshotScheduler:
|
||||
"slot reconcile: universe shrank %d->%d (>=50%%) — retire held",
|
||||
prior, len(universe),
|
||||
)
|
||||
if self._trading_date_fn is not None:
|
||||
trading_date = self._trading_date_fn()
|
||||
else:
|
||||
trading_date = self._clock().date()
|
||||
result = self._slot_repo.reconcile_universe(
|
||||
self._instance_key, trading_date, universe, allow_retire=allow_retire,
|
||||
self._instance_key, universe, allow_retire=allow_retire,
|
||||
)
|
||||
self._last_universe_size = len(universe)
|
||||
if any(result.values()):
|
||||
|
||||
Reference in New Issue
Block a user