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:
@@ -24,6 +24,24 @@ def _wire(dsn):
|
||||
return repo, svc, intents
|
||||
|
||||
|
||||
def test_universe_reconcile_pg(pg_clean):
|
||||
# dcy-020/REQ-058 (BAT-028): validate reconcile against real PG, esp. the
|
||||
# ON CONFLICT (instance_key, portfolio_id) WHERE portfolio_id IS NOT NULL
|
||||
# partial-index upsert + find_slot_by_portfolio/count_active.
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
inst = "inst-pg"
|
||||
r1 = repo.reconcile_universe(inst, DAY, {"pf-a", "pf-b"})
|
||||
assert r1["created"] == 2
|
||||
r2 = repo.reconcile_universe(inst, DAY, {"pf-a", "pf-b"}) # idempotent (ON CONFLICT)
|
||||
assert r2["created"] == 0
|
||||
assert repo.find_slot_by_portfolio(inst, "pf-a") is not None
|
||||
assert repo.count_active(inst) == 0
|
||||
# pf-b leaves universe while inactive -> soft-retire
|
||||
r3 = repo.reconcile_universe(inst, DAY, {"pf-a"})
|
||||
assert r3["retired"] == 1
|
||||
assert repo.find_slot_by_portfolio(inst, "pf-b").retired_at is not None
|
||||
|
||||
|
||||
def test_preallocate_and_capacity(pg_clean):
|
||||
repo, svc, _ = _wire(pg_clean)
|
||||
assert svc.preallocate_daily_slots(DAY, 10) == 10
|
||||
|
||||
@@ -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"
|
||||
@@ -0,0 +1,74 @@
|
||||
"""dcy-020 / REQ-058 (BAT-028): universe reconcile lifecycle (in-memory)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
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"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo():
|
||||
return InMemorySlotRepository()
|
||||
|
||||
|
||||
def test_creates_asset_slots_idempotently(repo):
|
||||
r1 = repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
assert r1["created"] == 2
|
||||
# idempotent — second pass creates nothing
|
||||
r2 = repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
assert r2["created"] == 0
|
||||
assert repo.find_slot_by_portfolio(INST, "pf-a").status is SlotState.IDLE
|
||||
|
||||
|
||||
def test_soft_retire_inactive_asset_that_left_universe(repo):
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
r = repo.reconcile_universe(INST, DAY, {"pf-a"}) # pf-b left
|
||||
assert r["retired"] == 1
|
||||
slot_b = repo.find_slot_by_portfolio(INST, "pf-b")
|
||||
assert slot_b.retired_at is not None # soft-retired, row preserved
|
||||
|
||||
|
||||
def test_tombstone_holding_asset_that_left_universe(repo):
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a"})
|
||||
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
svc.transit_slot(slot.slot_id, slot.status, SlotState.PENDING_BUY,
|
||||
reason="r", actor="a", symbol="005930")
|
||||
svc.transit_slot(slot.slot_id, SlotState.PENDING_BUY, SlotState.HOLDING,
|
||||
reason="r", actor="a", avg_price=70_000)
|
||||
r = repo.reconcile_universe(INST, DAY, set()) # pf-a left while holding
|
||||
assert r["tombstoned"] == 1 and r["retired"] == 0
|
||||
held = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
assert held.status is SlotState.HOLDING and held.retired_at is None # protected
|
||||
|
||||
|
||||
def test_allow_retire_false_skips_retire(repo):
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
r = repo.reconcile_universe(INST, DAY, {"pf-a"}, allow_retire=False)
|
||||
assert r["retired"] == 0
|
||||
assert repo.find_slot_by_portfolio(INST, "pf-b").retired_at is None
|
||||
|
||||
|
||||
def test_count_active(repo):
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
||||
assert repo.count_active(INST) == 0
|
||||
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
svc.transit_slot(slot.slot_id, slot.status, SlotState.PENDING_BUY,
|
||||
reason="r", actor="a", symbol="005930")
|
||||
assert repo.count_active(INST) == 1 # pending_buy counts as active
|
||||
|
||||
|
||||
def test_instance_isolation(repo):
|
||||
repo.reconcile_universe("inst-1", DAY, {"pf-a"})
|
||||
repo.reconcile_universe("inst-2", DAY, {"pf-a"})
|
||||
# same portfolio_id, different instances -> two distinct slots
|
||||
assert repo.find_slot_by_portfolio("inst-1", "pf-a").slot_id != \
|
||||
repo.find_slot_by_portfolio("inst-2", "pf-a").slot_id
|
||||
assert repo.count_active("inst-1") == 0
|
||||
Reference in New Issue
Block a user