feat(rel-007): dcy-025/024 + BAT-035/036 — slots.req 즉시 교체 + safety_sell_enabled

BAT-035 (dcy-025): trex.snapshot.req → trex.slots.req 즉시 교체
- topology: SNAPSHOT_REQ_QUEUE 제거, SLOTS_REQ_QUEUE = "trex.slots.req" 추가
- portfolio.py: View/build_snapshot_envelope/SnapshotEnvelope/_asset_to_dict 제거;
  slot_to_dict 시그니처 확장(snapshot_store/watchdog_store/pause_store/watchdog_timeout_sec)
  — asset_quantity(hub)/cash_balance/cash_estimated/warned/watchdog{}/paused 병합 (REQ-074)
- messaging_handlers.py: make_snapshot_rpc_handler 제거, make_slots_rpc_handler 추가
  — filter(all/inactive/pending/holding/error) + spy(bool) 처리; strategy 필드 제외 (REQ-073/074)
- daemon.py: _snapshot_rpc_loop → _slots_rpc_loop, make_slots_rpc_handler 연결
- cli.py: universe/slots → slots_queue + spy=True + server-side filter; buyable→inactive 매핑 (REQ-063)
- rabbitmq.py: snapshot_queue → slots_queue
- docs: interface-contract.md + runtime-flows.md slots.req 계약으로 업데이트 (REQ-075)
- REQ-028: Implemented → Deprecated (코드 삭제 완료)
- 테스트: test_snapshot_handler/selection/rpc_watchdog/messaging_snapshot_consumer 삭제;
  test_slots_handler.py(신규, 13 cases), test_killswitch_masking_resume 재작성,
  test_ledger_option_d._apply_cash_estimate → slots handler 경유, test_topology/cli_operator 갱신

BAT-036 (dcy-024): per-instance safety_sell_enabled 플래그
- config.py: DaemonInstanceConfig + DaemonConfig 에 safety_sell_enabled: bool = True 추가;
  from_toml 파싱, from_config 전파
- daemon.py: start() workers 조건부 — safety_sell_enabled=false 시 trex-safety-sell 미기동 + WARNING
- tests: test_instance_config.py 에 safety_sell_enabled 파싱 3개 케이스 추가

REQ: 073/074/075/021/034/063/048/005 Approved → Implemented

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 14:45:45 +09:00
co-authored by Claude Opus 4.8
parent fb05a73130
commit f0ad37747b
44 changed files with 1963 additions and 1189 deletions
@@ -1,4 +1,9 @@
"""Kill-switch day-exclusion masking + resume tests (REQ-046 / BAT-012)."""
"""Kill-switch day-exclusion store + resume tests (REQ-046 / BAT-012).
Tests the InMemoryKillSwitchStore and KillSwitchService.resume().
View-masking through build_snapshot_envelope was removed in BAT-035 (dcy-025).
Exclusion semantics are now tested at the store level.
"""
from __future__ import annotations
from datetime import date
@@ -9,11 +14,6 @@ from trex_core.application.killswitch import KillSwitchService
from trex_core.application.portfolio import (
GLOBAL_KILLSWITCH,
InMemoryKillSwitchStore,
InMemoryPauseStore,
PortfolioItem,
SnapshotStore,
View,
build_snapshot_envelope,
)
from trex_core.domain.slot import SlotService
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
@@ -22,86 +22,56 @@ _DAY = date(2026, 6, 7)
_DAY_NEXT = date(2026, 6, 8)
@pytest.fixture
def store():
s = SnapshotStore()
s.swap([
PortfolioItem(portfolio_id="pf-A", symbol="005930", cash_balance=0, asset_quantity=0),
PortfolioItem(portfolio_id="pf-B", symbol="000660", cash_balance=0, asset_quantity=0),
])
return s
@pytest.fixture
def repo():
return InMemorySlotRepository()
@pytest.fixture
def ks_store():
return InMemoryKillSwitchStore()
def _asset_ids(envelope):
return {a["asset_id"] for a in envelope.to_dict()["assets"]}
def _envelope(store, repo, ks_store, *, view=View.ALL, day=_DAY):
return build_snapshot_envelope(
store=store, pause_store=InMemoryPauseStore(), slot_repo=repo,
available_slots=10, view=view, trading_date=day, killswitch_store=ks_store,
)
def test_portfolio_exclusion_masks_that_asset(store, repo, ks_store):
"""AC-1: an excluded portfolio is masked from the envelope."""
def test_portfolio_exclusion_recorded(ks_store):
"""AC-1: excluding a portfolio records it in the store for the day."""
ks_store.exclude("pf-A", _DAY)
ids = _asset_ids(_envelope(store, repo, ks_store))
assert "pf-A" not in ids
assert "pf-B" in ids
assert "pf-A" in ks_store.list_excluded(_DAY)
assert "pf-B" not in ks_store.list_excluded(_DAY)
def test_global_exclusion_masks_all(store, repo, ks_store):
"""AC-2: global ('*') exclusion masks the whole universe."""
def test_global_exclusion_uses_sentinel(ks_store):
"""AC-2: global exclusion uses GLOBAL_KILLSWITCH sentinel."""
ks_store.exclude(GLOBAL_KILLSWITCH, _DAY)
assert _asset_ids(_envelope(store, repo, ks_store)) == set()
assert GLOBAL_KILLSWITCH in ks_store.list_excluded(_DAY)
def test_buyable_view_excludes(store, repo, ks_store):
"""AC-3: excluded asset is not buyable (blocks new buys)."""
def test_day_scoped_auto_recovery(ks_store):
"""AC-4: next trading day, the exclusion is absent (day-scoped)."""
ks_store.exclude("pf-A", _DAY)
ids = _asset_ids(_envelope(store, repo, ks_store, view=View.BUYABLE))
assert "pf-A" not in ids and "pf-B" in ids
assert "pf-A" not in ks_store.list_excluded(_DAY_NEXT)
def test_day_scoped_auto_recovery(store, repo, ks_store):
"""AC-4: next trading day, the exclusion is gone (auto recovery)."""
def test_persistence_same_day(ks_store):
"""AC-5: same day, exclusion persists across repeated calls."""
ks_store.exclude("pf-A", _DAY)
ids = _asset_ids(_envelope(store, repo, ks_store, day=_DAY_NEXT))
assert "pf-A" in ids # back on the next day
assert "pf-A" in ks_store.list_excluded(_DAY)
assert "pf-A" in ks_store.list_excluded(_DAY)
def test_persistence_same_day(store, repo, ks_store):
"""AC-5: a fresh envelope build same day still masks (store-backed persistence)."""
def test_resume_clears_exclusion(ks_store):
"""AC-6: resume (clear) removes the exclusion for that day."""
repo = InMemorySlotRepository()
svc = KillSwitchService(slot_repo=repo, slot_service=SlotService(repo),
killswitch_store=ks_store)
ks_store.exclude("pf-A", _DAY)
# simulate restart: new envelope build reading the same store
assert "pf-A" not in _asset_ids(_envelope(store, repo, ks_store))
def test_resume_reincludes(store, repo, ks_store):
"""AC-6: resume (clear) re-includes the asset from the next build."""
svc = KillSwitchService(slot_repo=repo, slot_service=SlotService(repo), killswitch_store=ks_store)
ks_store.exclude("pf-A", _DAY)
assert "pf-A" not in _asset_ids(_envelope(store, repo, ks_store))
assert "pf-A" in ks_store.list_excluded(_DAY)
svc.resume(scope="portfolio", portfolio_id="pf-A",
now=__import__("datetime").datetime(2026, 6, 7, 6, tzinfo=__import__("datetime").timezone.utc))
assert "pf-A" in _asset_ids(_envelope(store, repo, ks_store))
now=__import__("datetime").datetime(2026, 6, 7, 6,
tzinfo=__import__("datetime").timezone.utc))
assert "pf-A" not in ks_store.list_excluded(_DAY)
def test_no_killswitch_store_no_masking(store, repo):
"""Backward compat: without killswitch_store nothing extra is masked."""
env = build_snapshot_envelope(
store=store, pause_store=InMemoryPauseStore(), slot_repo=repo,
available_slots=10, view=View.ALL, trading_date=_DAY,
)
assert _asset_ids(env) == {"pf-A", "pf-B"}
def test_no_exclusion_empty_list(ks_store):
"""Baseline: with no exclusions, list_excluded returns empty set."""
assert ks_store.list_excluded(_DAY) == set()
def test_clear_without_prior_exclude_is_noop(ks_store):
"""Clearing a non-excluded portfolio is safe (no exception)."""
ks_store.clear("pf-X", _DAY)
assert ks_store.list_excluded(_DAY) == set()