feat(slot): BAT-028 M2/M3 — universe reconcile + 자산 중심 admission (dcy-020/REQ-058·059)
config 게이트 이중 경로(max_active_slots 설정 시 자산 중심, 미설정 시 legacy pool). 운영 데몬은 cutover(BAT-δ) 전까지 pool 무중단. - repository: find_slot_by_portfolio·count_active(holding+pending_buy+pending_sell)· reconcile_universe(생성 멱등/soft-retire/tombstone, allow_retire 가드) — protocol+in_memory+PG (PG: ON CONFLICT (instance_key, portfolio_id) WHERE portfolio_id IS NOT NULL) - snapshot_scheduler: run_once swap 직후 _reconcile_slots(빈 universe skip, ≥50%↓ retire 보류) - intents: _admit_buy 모드 분기 + _admit_buy_asset_centric(활성화/already_active/ capacity_full/unknown_portfolio/over-cap, 인스턴스 lock) - daemon: instance_key·max_active_slots·reconcile_enabled·trading_date_fn 배선 - 신규 테스트: test_universe_reconcile(6), test_asset_centric_admission(6), PG reconcile(1) 검증: 단위 387 passed / 35 skipped, PG-gated 17 passed, ruff clean. legacy 무파손. REQ-058/059/060 은 Approved 유지(자산 중심 경로 도입 검증; default 전환·cutover 는 BAT-δ). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""dcy-020 / REQ-059 (BAT-028): 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"}
|
||||
|
||||
|
||||
def _wire(max_active):
|
||||
repo = InMemorySlotRepository()
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
intents = IntentService(
|
||||
svc, repo, InMemoryIntentStore(),
|
||||
resolve_symbol=lambda pid: _SYMS.get(pid),
|
||||
instance_key=INST, max_active_slots=max_active,
|
||||
)
|
||||
return repo, svc, intents
|
||||
|
||||
|
||||
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"))
|
||||
assert d.status == "admitted"
|
||||
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930"
|
||||
|
||||
|
||||
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"))
|
||||
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"))
|
||||
assert d1.status == "admitted"
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="b2", trading_date=DAY, 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
|
||||
|
||||
|
||||
def test_unknown_portfolio_when_no_asset_slot():
|
||||
repo, svc, intents = _wire(max_active=2)
|
||||
repo.reconcile_universe(INST, DAY, {"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"))
|
||||
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"))
|
||||
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"))
|
||||
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"
|
||||
Reference in New Issue
Block a user