dcy-019/issue #18: slot 자원을 진입 횟수 기준으로 고르게 재사용하고 idle 소진 시 closed 슬롯을 직접 재할당한다. 거버넌스는 전부 change-req(신규 REQ 없음). - 도메인(REQ-002): Slot.entry_count(기본 0) + EXPECTED_SLOT_FIELDS, 상태 머신 closed→pending_buy 신규 전이(base 8→9, ALLOWED_TRANSITIONS 14→15), _apply_transition_fields closed→pending_buy clean-slate + →pending_buy 시 +1 - 영속화(REQ-006): 멱등 마이그레이션 008(entry_count), flat 매핑, find_allocatable_slot(idle∪closed, entry_count→updated_at→slot_id) + in-memory 동등 - admission(REQ-007): _admit_buy find_idle_slot→find_allocatable_slot + from_state=slot.status - 테스트(REQ-003): 전이 카운트/계약 가드 정합 + test_slot_entry_count.py / test_buy_admission_reallocation.py - 문서(REQ-009): data-model.md / runtime-flows.md 정합 - 기존 daily_symbol_entries(symbol-scoped) 분리·미변경 BAT-025(release-candidate) delivery + change-req REQ-002/003/006/007/009(CHG-20260609-01). REL-005(confirmed, app-service): BAT-024+BAT-025 묶음. operator-facing run-the-app 스모크로 enter --forced-close-at(BAT-024)·closed 재할당(BAT-025) 실 데몬 검증 (실 PG 마이그레이션 008 round-trip 포함). 검증: 단위 366 passed / 35 skipped(KIS sandbox 1건 deselect — live .env 환경 의존), ruff clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
67 lines
2.6 KiB
Python
67 lines
2.6 KiB
Python
"""Slot state machine: 6 states + 9 base allowed transitions (REQ-001, REQ-002).
|
|
|
|
The transitions match dcy-001 FR-2 and BAT-001 design, extended by dcy-019 with
|
|
`closed -> pending_buy` (closed-slot direct reallocation, BAT-025).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from enum import Enum
|
|
|
|
|
|
# Shared PostgreSQL advisory lock id for Buy/Sell decision threads and
|
|
# operator CLI manual transitions. Single constant so all callers serialize
|
|
# through the same lock (REQ-002 NFR-1, dcy-001 R-1).
|
|
SLOT_ADVISORY_LOCK_ID: int = 0x5107_4D31 # arbitrary mnemonic constant
|
|
|
|
|
|
class SlotState(str, Enum):
|
|
"""6-state slot lifecycle (dcy-001 5.3.5.1)."""
|
|
|
|
IDLE = "idle"
|
|
PENDING_BUY = "pending_buy"
|
|
HOLDING = "holding"
|
|
PENDING_SELL = "pending_sell"
|
|
CLOSED = "closed"
|
|
ERROR = "error"
|
|
|
|
|
|
# 9 base allowed transitions (dcy-001 FR-2 + dcy-019). Any pair not in this set
|
|
# must be rejected by SlotService.
|
|
# Note: `* -> error` is represented as transitions from every non-error state.
|
|
_BASE_TRANSITIONS: frozenset[tuple[SlotState, SlotState]] = frozenset(
|
|
{
|
|
(SlotState.IDLE, SlotState.PENDING_BUY), # 1. buy decision
|
|
(SlotState.PENDING_BUY, SlotState.HOLDING), # 2. buy filled
|
|
(SlotState.PENDING_BUY, SlotState.IDLE), # 3. buy rejected/rollback
|
|
(SlotState.HOLDING, SlotState.PENDING_SELL), # 4. sell decision
|
|
(SlotState.PENDING_SELL, SlotState.CLOSED), # 5. sell filled
|
|
(SlotState.PENDING_SELL, SlotState.HOLDING), # 6. sell rejected/rollback
|
|
(SlotState.CLOSED, SlotState.IDLE), # 7. recovery (with daily_symbol_entries +1)
|
|
(SlotState.CLOSED, SlotState.PENDING_BUY), # 9. closed-slot direct reallocation (dcy-019/BAT-025)
|
|
}
|
|
)
|
|
|
|
# 8th transition family: any non-error state may move to error (operator manual
|
|
# resolve uses `error -> idle | holding` which is also covered explicitly).
|
|
_TO_ERROR_TRANSITIONS: frozenset[tuple[SlotState, SlotState]] = frozenset(
|
|
(s, SlotState.ERROR) for s in SlotState if s is not SlotState.ERROR
|
|
)
|
|
|
|
# Operator manual resolve transitions out of error (REQ-004).
|
|
_FROM_ERROR_TRANSITIONS: frozenset[tuple[SlotState, SlotState]] = frozenset(
|
|
{
|
|
(SlotState.ERROR, SlotState.IDLE),
|
|
(SlotState.ERROR, SlotState.HOLDING),
|
|
}
|
|
)
|
|
|
|
|
|
ALLOWED_TRANSITIONS: frozenset[tuple[SlotState, SlotState]] = (
|
|
_BASE_TRANSITIONS | _TO_ERROR_TRANSITIONS | _FROM_ERROR_TRANSITIONS
|
|
)
|
|
|
|
|
|
def is_allowed_transition(from_state: SlotState, to_state: SlotState) -> bool:
|
|
"""Return True iff (from_state, to_state) is one of the 8 transition families."""
|
|
return (from_state, to_state) in ALLOWED_TRANSITIONS
|