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:
@@ -131,6 +131,74 @@ class PostgreSQLSlotRepository:
|
||||
row = cur.fetchone()
|
||||
return self._row_to_slot(row) if row else None
|
||||
|
||||
# --- asset-centric (dcy-020 / BAT-028) ---
|
||||
def find_slot_by_portfolio(self, instance_key: str, portfolio_id: str) -> Optional[Slot]:
|
||||
with self._psycopg.connect(self._dsn) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
f"SELECT {_SLOT_COLUMNS} FROM daily_slot "
|
||||
"WHERE instance_key = %s AND portfolio_id = %s LIMIT 1",
|
||||
(instance_key, portfolio_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return self._row_to_slot(row) if row else None
|
||||
|
||||
def count_active(self, instance_key: str) -> int:
|
||||
with self._psycopg.connect(self._dsn) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM daily_slot WHERE instance_key = %s "
|
||||
"AND status IN ('holding','pending_buy','pending_sell')",
|
||||
(instance_key,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return int(row[0]) if row else 0
|
||||
|
||||
def reconcile_universe(
|
||||
self, instance_key: str, trading_date: date, universe_portfolio_ids: set[str],
|
||||
*, allow_retire: bool = True,
|
||||
) -> dict[str, int]:
|
||||
now = datetime.now(tz=timezone.utc)
|
||||
created = retired = tombstoned = 0
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT portfolio_id, status, retired_at FROM daily_slot "
|
||||
"WHERE instance_key = %s AND portfolio_id IS NOT NULL",
|
||||
(instance_key,),
|
||||
)
|
||||
existing = {r[0]: (r[1], r[2]) for r in cur.fetchall()}
|
||||
# create new asset slots (idempotent via partial unique)
|
||||
for pid in universe_portfolio_ids:
|
||||
if pid in existing:
|
||||
continue
|
||||
cur.execute(
|
||||
"INSERT INTO daily_slot "
|
||||
"(slot_id, trading_date, status, instance_key, portfolio_id, "
|
||||
"created_at, updated_at) VALUES (%s, %s, 'idle', %s, %s, %s, %s) "
|
||||
"ON CONFLICT (instance_key, portfolio_id) "
|
||||
"WHERE portfolio_id IS NOT NULL DO NOTHING",
|
||||
(uuid4(), trading_date, instance_key, pid, now, now),
|
||||
)
|
||||
created += cur.rowcount
|
||||
# retire / tombstone assets that left the universe (skipped under
|
||||
# the mass-disappearance guard, dcy-020/OQ-2 D3)
|
||||
if allow_retire:
|
||||
for pid, (status, retired_at) in existing.items():
|
||||
if pid in universe_portfolio_ids:
|
||||
continue
|
||||
if status in ("holding", "pending_buy", "pending_sell"):
|
||||
tombstoned += 1
|
||||
elif retired_at is None:
|
||||
cur.execute(
|
||||
"UPDATE daily_slot SET retired_at = %s, updated_at = %s "
|
||||
"WHERE instance_key = %s AND portfolio_id = %s",
|
||||
(now, now, instance_key, pid),
|
||||
)
|
||||
retired += 1
|
||||
conn.commit()
|
||||
return {"created": created, "retired": retired, "tombstoned": tombstoned}
|
||||
|
||||
def update_slot(self, slot: Slot) -> None:
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn:
|
||||
with conn.cursor() as cur:
|
||||
|
||||
@@ -85,6 +85,9 @@ class IntentService:
|
||||
slot_repo: SlotRepository,
|
||||
intent_store: IntentStore,
|
||||
resolve_symbol=None,
|
||||
*,
|
||||
instance_key: str = "default",
|
||||
max_active_slots: Optional[int] = None,
|
||||
) -> None:
|
||||
self._svc = slot_service
|
||||
self._repo = slot_repo
|
||||
@@ -93,6 +96,11 @@ class IntentService:
|
||||
# callable(portfolio_id) -> Optional[str]. When set, admission resolves symbol
|
||||
# from the universe and rejects unknown_portfolio if absent.
|
||||
self._resolve_symbol = resolve_symbol
|
||||
# dcy-020 / REQ-059 (BAT-028): asset-centric mode gate. When max_active_slots
|
||||
# is set, buy admission activates the asset's permanent slot under an M cap
|
||||
# (FCFS, no eviction); otherwise the legacy pool path runs (unchanged).
|
||||
self._instance_key = instance_key
|
||||
self._max_active_slots = max_active_slots
|
||||
|
||||
# ------------------------------------------------------------
|
||||
def lookup(self, intent_id: str) -> Optional[IntentDecision]:
|
||||
@@ -163,6 +171,9 @@ class IntentService:
|
||||
elif not symbol:
|
||||
# No resolver wired and no symbol carried — cannot determine the order target.
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "unknown_portfolio")
|
||||
# dcy-020 / REQ-059 (BAT-028): asset-centric admission when M is configured.
|
||||
if self._max_active_slots is not None:
|
||||
return self._admit_buy_asset_centric(intent, symbol)
|
||||
# Retry on the last-candidate race: another intent may claim the slot
|
||||
# between our find and transit (SlotStateMismatchError).
|
||||
# dcy-019 / REQ-007: admission candidate is idle ∪ closed (closed-slot
|
||||
@@ -199,6 +210,48 @@ class IntentService:
|
||||
return IntentDecision(intent.intent_id, "admitted", updated.slot_id, None)
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "idle_slot_contended")
|
||||
|
||||
# ------------------------------------------------------------
|
||||
def _admit_buy_asset_centric(self, intent: BuyIntent, symbol: str) -> IntentDecision:
|
||||
"""dcy-020 / REQ-059: activate the asset's permanent slot under the M cap.
|
||||
|
||||
FCFS + no eviction: if the asset is already active -> already_active; if the
|
||||
instance is at M active slots -> capacity_full (existing holdings untouched,
|
||||
incl. over-cap when M is lowered). Single intent thread + instance-scoped
|
||||
advisory lock serialize admissions (REQ-002 NFR-1 / dcy-020 OQ-1)."""
|
||||
_ACTIVE = (SlotState.PENDING_BUY, SlotState.HOLDING, SlotState.PENDING_SELL)
|
||||
for _ in range(_MAX_IDLE_RETRY):
|
||||
slot = self._repo.find_slot_by_portfolio(self._instance_key, intent.portfolio_id)
|
||||
if slot is None:
|
||||
# no asset slot for this instance (not in reconciled universe)
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "unknown_portfolio")
|
||||
if slot.status in _ACTIVE:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "already_active")
|
||||
if self._repo.count_active(self._instance_key) >= self._max_active_slots:
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "capacity_full")
|
||||
try:
|
||||
updated = self._svc.transit_slot(
|
||||
slot.slot_id,
|
||||
slot.status,
|
||||
SlotState.PENDING_BUY,
|
||||
reason="buy_intent",
|
||||
actor="intent_api",
|
||||
symbol=symbol,
|
||||
qty=intent.qty,
|
||||
allocated_capital=intent.allocated_capital,
|
||||
limit_price=intent.entry_price,
|
||||
strategy_id=intent.strategy_id,
|
||||
sell_strategy_id=intent.sell_strategy_id,
|
||||
portfolio_id=intent.portfolio_id,
|
||||
forced_close_at=intent.forced_close_at,
|
||||
stop_loss_price=intent.stop_loss_price,
|
||||
take_profit_price=intent.take_profit_price,
|
||||
extra={"intent_id": intent.intent_id, "symbol": symbol},
|
||||
)
|
||||
except SlotStateMismatchError:
|
||||
continue # slot changed under us; re-fetch and re-evaluate
|
||||
return IntentDecision(intent.intent_id, "admitted", updated.slot_id, None)
|
||||
return IntentDecision(intent.intent_id, "rejected", None, "already_active")
|
||||
|
||||
def _admit_sell(self, intent: SellIntent) -> IntentDecision:
|
||||
slot = self._repo.fetch_slot(intent.slot_id)
|
||||
if slot is None:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)."""
|
||||
|
||||
@@ -22,6 +22,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
import signal
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from datetime import time as time_cls
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
@@ -144,6 +145,8 @@ class Daemon:
|
||||
self.intent_service = IntentService(
|
||||
self.slot_service, self.slot_repo, self.intent_store,
|
||||
resolve_symbol=self._resolve_symbol,
|
||||
instance_key=config.instance_key,
|
||||
max_active_slots=config.max_active_slots, # dcy-020: asset-centric gate
|
||||
)
|
||||
self.broker = _build_broker(config)
|
||||
self.outbox = PostgreSQLLedgerOutbox(config.dsn)
|
||||
@@ -173,6 +176,11 @@ class Daemon:
|
||||
store=self.snapshot_store,
|
||||
broker=self.broker,
|
||||
schedule_kst=_parse_kst_time(config.snapshot_schedule.kst_time),
|
||||
# dcy-020 / REQ-058 (BAT-028): asset-centric slot reconcile when M set.
|
||||
slot_repo=self.slot_repo,
|
||||
instance_key=config.instance_key,
|
||||
reconcile_enabled=config.max_active_slots is not None,
|
||||
trading_date_fn=(lambda: datetime.now(ZoneInfo(config.trading_timezone)).date()),
|
||||
)
|
||||
# dcy-014 / REQ-005: daily slot preallocation in the configured
|
||||
# trading timezone (default KST), so trading_date matches the local
|
||||
|
||||
Reference in New Issue
Block a user