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:
@@ -100,6 +100,54 @@ class InMemorySlotRepository:
|
||||
raise SlotNotFoundError(f"slot not found: {slot.slot_id}")
|
||||
self._slots[slot.slot_id] = slot
|
||||
|
||||
# --- asset-centric (dcy-020 / BAT-028) ---
|
||||
_ACTIVE = (SlotState.HOLDING, SlotState.PENDING_BUY, SlotState.PENDING_SELL)
|
||||
|
||||
def find_slot_by_portfolio(self, instance_key: str, portfolio_id: str) -> Optional[Slot]:
|
||||
for s in self._slots.values():
|
||||
if s.instance_key == instance_key and s.portfolio_id == portfolio_id:
|
||||
return s
|
||||
return None
|
||||
|
||||
def count_active(self, instance_key: str) -> int:
|
||||
return sum(
|
||||
1 for s in self._slots.values()
|
||||
if s.instance_key == instance_key and s.status in self._ACTIVE
|
||||
)
|
||||
|
||||
def reconcile_universe(
|
||||
self, instance_key: str, trading_date: date, universe_portfolio_ids: set[str],
|
||||
*, allow_retire: bool = True,
|
||||
) -> dict[str, int]:
|
||||
existing = {
|
||||
s.portfolio_id: s for s in self._slots.values()
|
||||
if s.instance_key == instance_key and s.portfolio_id is not None
|
||||
}
|
||||
now = datetime.now(tz=timezone.utc)
|
||||
created = retired = tombstoned = 0
|
||||
# create asset slots for newly-appeared universe assets (idempotent)
|
||||
for pid in universe_portfolio_ids:
|
||||
if pid not in existing:
|
||||
sid = uuid4()
|
||||
self._slots[sid] = Slot(
|
||||
slot_id=sid, trading_date=trading_date, status=SlotState.IDLE,
|
||||
instance_key=instance_key, portfolio_id=pid,
|
||||
created_at=now, updated_at=now,
|
||||
)
|
||||
created += 1
|
||||
# retire / tombstone assets that left the universe (skipped under the
|
||||
# mass-disappearance guard, dcy-020/OQ-2 D3 — caller passes allow_retire=False)
|
||||
if allow_retire:
|
||||
for pid, s in existing.items():
|
||||
if pid in universe_portfolio_ids:
|
||||
continue
|
||||
if s.status in self._ACTIVE:
|
||||
tombstoned += 1 # holding/pending — keep (safety-sell), no retire
|
||||
elif s.retired_at is None:
|
||||
self._slots[s.slot_id] = replace(s, retired_at=now, updated_at=now)
|
||||
retired += 1
|
||||
return {"created": created, "retired": retired, "tombstoned": tombstoned}
|
||||
|
||||
# --- slot_transition_log (append-only) ---
|
||||
def append_transition_log(self, record: SlotTransitionRecord) -> None:
|
||||
# Defensive copy; reject any external mutation attempt on the list.
|
||||
|
||||
@@ -63,6 +63,34 @@ class SlotRepository(Protocol):
|
||||
"""Persist slot row (UPDATE by slot_id)."""
|
||||
...
|
||||
|
||||
# --- asset-centric (dcy-020 / BAT-028) ---
|
||||
def find_slot_by_portfolio(self, instance_key: str, portfolio_id: str) -> Optional[Slot]:
|
||||
"""Return the (single, permanent) slot bound to `portfolio_id` for this
|
||||
instance (dcy-020/REQ-059), or None. Keyed by (instance_key, portfolio_id)
|
||||
— independent of trading_date."""
|
||||
...
|
||||
|
||||
def count_active(self, instance_key: str) -> int:
|
||||
"""Count position-occupying slots for this instance — status in
|
||||
{holding, pending_buy, pending_sell} (dcy-020/REQ-059 M cap, D2)."""
|
||||
...
|
||||
|
||||
def reconcile_universe(
|
||||
self, instance_key: str, trading_date: date, universe_portfolio_ids: set[str],
|
||||
*, allow_retire: bool = True,
|
||||
) -> dict[str, int]:
|
||||
"""Reconcile the slot set to the portfolio-hub universe (dcy-020/REQ-058).
|
||||
|
||||
- new portfolio_id (no slot for instance) -> create an inactive asset slot
|
||||
(idempotent; keyed by (instance_key, portfolio_id), permanent).
|
||||
- slot whose portfolio_id left the universe AND is inactive AND not yet
|
||||
retired -> soft-retire (set retired_at).
|
||||
- slot that left the universe but is holding/pending -> tombstone (left
|
||||
as-is; not retired).
|
||||
Returns counts {created, retired, tombstoned}. Degrade / mass-disappearance
|
||||
guards are the caller's responsibility (scheduler)."""
|
||||
...
|
||||
|
||||
# --- slot_transition_log (append-only) ---
|
||||
def append_transition_log(self, record: SlotTransitionRecord) -> None:
|
||||
"""Append one row to slot_transition_log (no UPDATE/DELETE allowed)."""
|
||||
|
||||
Reference in New Issue
Block a user