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:
@@ -1,14 +1,11 @@
|
||||
"""dcy-020 / REQ-059 (BAT-028): asset-centric buy admission (FCFS·M캡·over-cap)."""
|
||||
"""dcy-020 / REQ-059 (BAT-028; BAT-030 단일 경로): asset-centric buy admission (FCFS·M캡·over-cap)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
|
||||
from trex_core.application.intents import BuyIntent, IntentService
|
||||
from trex_core.domain.slot import SlotService, SlotState
|
||||
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
|
||||
|
||||
DAY = date(2026, 6, 10)
|
||||
INST = "inst-1"
|
||||
# portfolio_id -> symbol (universe resolver)
|
||||
_SYMS = {"pf-a": "005930", "pf-b": "000660", "pf-c": "035720"}
|
||||
@@ -27,8 +24,8 @@ def _wire(max_active):
|
||||
|
||||
def test_activates_asset_slot():
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-a"))
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b"})
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", portfolio_id="pf-a"))
|
||||
assert d.status == "admitted"
|
||||
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930"
|
||||
@@ -37,9 +34,9 @@ def test_activates_asset_slot():
|
||||
def test_activates_by_slot_id():
|
||||
# dcy-020/REQ-061: slot_id is the canonical buy contract key.
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a"})
|
||||
repo.reconcile_universe(INST, {"pf-a"})
|
||||
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, slot_id=slot.slot_id))
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", slot_id=slot.slot_id))
|
||||
assert d.status == "admitted" and d.slot_id == slot.slot_id
|
||||
got = repo.fetch_slot(slot.slot_id)
|
||||
assert got.status is SlotState.PENDING_BUY and got.symbol == "005930" # resolved from slot.portfolio_id
|
||||
@@ -48,25 +45,25 @@ def test_activates_by_slot_id():
|
||||
def test_unknown_slot_for_bad_slot_id():
|
||||
from uuid import uuid4
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a"})
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, slot_id=uuid4()))
|
||||
repo.reconcile_universe(INST, {"pf-a"})
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", slot_id=uuid4()))
|
||||
assert d.status == "rejected" and d.reason == "unknown_slot"
|
||||
|
||||
|
||||
def test_already_active_is_rejected():
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-a"))
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="b2", trading_date=DAY, portfolio_id="pf-a"))
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b"})
|
||||
intents.submit_buy(BuyIntent(intent_id="b1", portfolio_id="pf-a"))
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="b2", portfolio_id="pf-a"))
|
||||
assert d2.status == "rejected" and d2.reason == "already_active"
|
||||
|
||||
|
||||
def test_capacity_full_at_M_no_eviction():
|
||||
repo, svc, intents = _wire(max_active=1)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
d1 = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-a"))
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b"})
|
||||
d1 = intents.submit_buy(BuyIntent(intent_id="b1", portfolio_id="pf-a"))
|
||||
assert d1.status == "admitted"
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="b2", trading_date=DAY, portfolio_id="pf-b"))
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="b2", portfolio_id="pf-b"))
|
||||
assert d2.status == "rejected" and d2.reason == "capacity_full"
|
||||
# existing holding untouched (no eviction)
|
||||
assert repo.find_slot_by_portfolio(INST, "pf-a").status is SlotState.PENDING_BUY
|
||||
@@ -74,32 +71,31 @@ def test_capacity_full_at_M_no_eviction():
|
||||
|
||||
def test_unknown_portfolio_when_no_asset_slot():
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a"})
|
||||
repo.reconcile_universe(INST, {"pf-a"})
|
||||
# pf-c is resolvable to a symbol but has no reconciled asset slot
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-c"))
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", portfolio_id="pf-c"))
|
||||
assert d.status == "rejected" and d.reason == "unknown_portfolio"
|
||||
|
||||
|
||||
def test_over_cap_blocks_new_keeps_existing():
|
||||
# M=2: fill 2, then lower M to 1 (over-cap) -> new rejected, existing kept
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b", "pf-c"})
|
||||
intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-a"))
|
||||
intents.submit_buy(BuyIntent(intent_id="b2", trading_date=DAY, portfolio_id="pf-b"))
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b", "pf-c"})
|
||||
intents.submit_buy(BuyIntent(intent_id="b1", portfolio_id="pf-a"))
|
||||
intents.submit_buy(BuyIntent(intent_id="b2", portfolio_id="pf-b"))
|
||||
assert repo.count_active(INST) == 2
|
||||
intents._max_active_slots = 1 # operator lowered M (over-cap)
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b3", trading_date=DAY, portfolio_id="pf-c"))
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b3", portfolio_id="pf-c"))
|
||||
assert d.status == "rejected" and d.reason == "capacity_full"
|
||||
# both existing positions kept
|
||||
assert repo.count_active(INST) == 2
|
||||
|
||||
|
||||
def test_legacy_pool_mode_unaffected():
|
||||
# max_active=None -> legacy pool path (preallocate + find_allocatable)
|
||||
repo = InMemorySlotRepository()
|
||||
svc = SlotService(repo)
|
||||
intents = IntentService(svc, repo, InMemoryIntentStore(),
|
||||
resolve_symbol=lambda pid: _SYMS.get(pid))
|
||||
svc.preallocate_daily_slots(DAY, 1)
|
||||
d = intents.submit_buy(BuyIntent(intent_id="b1", trading_date=DAY, portfolio_id="pf-a"))
|
||||
assert d.status == "admitted"
|
||||
def test_no_cap_admits_without_capacity_check():
|
||||
# BAT-030: max_active_slots=None -> no capacity cap (asset-centric only path)
|
||||
repo, svc, intents = _wire(max_active=None)
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b", "pf-c"})
|
||||
for i, pid in enumerate(sorted(_SYMS)):
|
||||
d = intents.submit_buy(BuyIntent(intent_id=f"b{i}", portfolio_id=pid))
|
||||
assert d.status == "admitted"
|
||||
assert repo.count_active(INST) == 3
|
||||
|
||||
Reference in New Issue
Block a user