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:
@@ -37,6 +37,10 @@ class SnapshotScheduler:
|
||||
broker: BrokerPort,
|
||||
schedule_kst: time = time(hour=7, minute=30),
|
||||
clock: Optional[Callable[[], datetime]] = None,
|
||||
slot_repo=None,
|
||||
instance_key: str = "default",
|
||||
reconcile_enabled: bool = False,
|
||||
trading_date_fn: Optional[Callable[[], object]] = None,
|
||||
) -> None:
|
||||
self._read = read_port
|
||||
self._store = store
|
||||
@@ -45,6 +49,13 @@ 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).
|
||||
self._slot_repo = slot_repo
|
||||
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 --------------------------------
|
||||
def run_forever(self, stop: threading.Event, *, tick_sec: float = 60.0) -> None:
|
||||
@@ -99,5 +110,36 @@ 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:
|
||||
try:
|
||||
self._reconcile_slots(items)
|
||||
except Exception: # noqa: BLE001
|
||||
_log.exception("slot reconcile pass failed")
|
||||
self._last_fired_date = self._clock()
|
||||
return True
|
||||
|
||||
# ---- asset-centric slot reconcile (dcy-020/REQ-058, BAT-028) ----------
|
||||
def _reconcile_slots(self, items: list) -> None:
|
||||
universe = {it.portfolio_id for it in items if getattr(it, "portfolio_id", None)}
|
||||
if not universe:
|
||||
_log.warning("slot reconcile skipped: empty universe (degrade guard)")
|
||||
return
|
||||
# mass-disappearance guard (D3): retire 보류 on N→0 (above) or >=50% shrink
|
||||
allow_retire = True
|
||||
prior = self._last_universe_size
|
||||
if prior is not None and len(universe) < 0.5 * prior:
|
||||
allow_retire = False
|
||||
_log.warning(
|
||||
"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._last_universe_size = len(universe)
|
||||
if any(result.values()):
|
||||
_log.info("slot reconcile: %s (instance=%s)", result, self._instance_key)
|
||||
|
||||
Reference in New Issue
Block a user