M1 literal inactive 전면화: SlotState 5-state(idle/closed 별칭 제거), 마이그레이션 010(CHECK 교체+행 rewrite), ops_checks 를 transition_log 기반 재작성. M2 집행 매도 수량 브로커 정본(D3): dispatch 매도 qty=broker hldg_qty, 미러 fallback 없음, 브로커 0 → skip+WARN, 리스트 기반 dispatch(head-block 방지). M3 컬럼 drop + legacy pool 제거: 마이그레이션 011(trading_date·entry_count DROP), repo 계약 instance 스코프 개편(집행 큐 포함), preallocation 폐기(데몬 7스레드), admission 자산 중심 단일 경로(slot_id 정본), capacity 재정의(available=M−active), daily_symbol_entries 코드 경로 제거. M4 cutover 도구(D1·D4): trex-core-cutover(전체 rebuild, preflight/브로커 carry-over/ --allow-unmapped/--promote-unique/dry-run) + PG 리허설 테스트 8건. M5 baseline 마감: data-model/runtime-flows/project-structure/interface-contract 자산 중심 최종 갱신 + run-trex-core-daemon 스킬 정합. 검증: 단위 367 passed + ruff clean; PG-gated 29 passed(010/011 round-trip·드리프트 가드·cutover 리허설); RabbitMQ 21 passed; run-the-app e2e(test DB+실 MQ, M=1): reconcile 슬롯 생성→slot_id 매수→dedup→capacity_full→fill→already_active→ 매도→inactive(자산 바인딩 보존)→영구 슬롯 재활성 전 구간 green. 실 cutover 는 운영자 실행(REL 단계, D4). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
183 lines
7.0 KiB
Python
183 lines
7.0 KiB
Python
"""Snapshot selection + masking + view composition (REQ-029, REQ-028)."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
from trex_core.application.portfolio import (
|
|
DegradedReason,
|
|
InMemoryPauseStore,
|
|
PortfolioItem,
|
|
SnapshotStore,
|
|
View,
|
|
build_snapshot_envelope,
|
|
)
|
|
from trex_core.domain.slot import SlotService, SlotState
|
|
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
|
|
|
|
DAY = date(2026, 6, 5)
|
|
|
|
|
|
def _universe():
|
|
return [
|
|
PortfolioItem(portfolio_id="pf-A", symbol="005930",
|
|
cash_balance=1_000_000, asset_quantity=0,
|
|
active_strategy_id="target-value-v1"),
|
|
PortfolioItem(portfolio_id="pf-B", symbol="000660",
|
|
cash_balance=2_000_000, asset_quantity=10,
|
|
active_strategy_id="trend-follow-v1"),
|
|
PortfolioItem(portfolio_id="pf-C", symbol="035720",
|
|
cash_balance=3_000_000, asset_quantity=0,
|
|
active_strategy_id="target-value-v1"),
|
|
]
|
|
|
|
|
|
def _wire(*, slots_setup=None):
|
|
store = SnapshotStore()
|
|
store.swap(_universe())
|
|
pause = InMemoryPauseStore()
|
|
repo = InMemorySlotRepository()
|
|
svc = SlotService(repo)
|
|
repo.reconcile_universe("default", {"pf-A", "pf-B", "pf-C"})
|
|
if slots_setup:
|
|
slots_setup(svc, repo)
|
|
return store, pause, repo, svc
|
|
|
|
|
|
def _take_slot(svc, repo, *, portfolio_id, symbol, strategy_id, status):
|
|
s = repo.find_slot_by_portfolio("default", portfolio_id)
|
|
assert s is not None
|
|
svc.transit_slot(
|
|
s.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
|
reason="buy_intent", actor="t",
|
|
symbol=symbol, strategy_id=strategy_id, qty=10,
|
|
)
|
|
if status is SlotState.PENDING_BUY:
|
|
return s.slot_id
|
|
svc.transit_slot(
|
|
s.slot_id, SlotState.PENDING_BUY, SlotState.HOLDING,
|
|
reason="filled", actor="t", avg_price=100.0, qty=10,
|
|
)
|
|
return s.slot_id
|
|
|
|
|
|
def _env(store, pause, repo, **kw):
|
|
"""Helper: always pin trading_date to the test's hardcoded DAY."""
|
|
return build_snapshot_envelope(
|
|
store=store, pause_store=pause, slot_repo=repo, trading_date=DAY, **kw,
|
|
)
|
|
|
|
|
|
def test_view_all_includes_holding_excludes_pending_for_same_symbol():
|
|
def setup(svc, repo):
|
|
_take_slot(svc, repo, portfolio_id="pf-A", symbol="005930",
|
|
strategy_id="target-value-v1", status=SlotState.HOLDING)
|
|
_take_slot(svc, repo, portfolio_id="pf-B", symbol="000660",
|
|
strategy_id="trend-follow-v1", status=SlotState.PENDING_BUY)
|
|
store, pause, repo, svc = _wire(slots_setup=setup)
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL)
|
|
pids = {a["asset_id"] for a in env.assets}
|
|
assert "pf-A" in pids # holding -> included
|
|
assert "pf-B" not in pids # pending -> masked
|
|
assert "pf-C" in pids # universe-only -> included
|
|
|
|
|
|
def test_view_buyable_with_zero_available_slots_returns_empty():
|
|
store, pause, repo, svc = _wire()
|
|
env = _env(store, pause, repo, available_slots=0, view=View.BUYABLE)
|
|
assert env.assets == []
|
|
assert env.available_slots == 0
|
|
|
|
|
|
def test_view_buyable_excludes_occupied_portfolios():
|
|
def setup(svc, repo):
|
|
_take_slot(svc, repo, portfolio_id="pf-A", symbol="005930",
|
|
strategy_id="target-value-v1", status=SlotState.PENDING_BUY)
|
|
_take_slot(svc, repo, portfolio_id="pf-B", symbol="000660",
|
|
strategy_id="trend-follow-v1", status=SlotState.HOLDING)
|
|
store, pause, repo, svc = _wire(slots_setup=setup)
|
|
env = _env(store, pause, repo, available_slots=3, view=View.BUYABLE)
|
|
pids = {a["asset_id"] for a in env.assets}
|
|
assert pids == {"pf-C"} # A occupied (pending), B occupied (holding)
|
|
|
|
|
|
def test_view_held_returns_only_holding_slot_assets():
|
|
def setup(svc, repo):
|
|
_take_slot(svc, repo, portfolio_id="pf-A", symbol="005930",
|
|
strategy_id="target-value-v1", status=SlotState.HOLDING)
|
|
_take_slot(svc, repo, portfolio_id="pf-B", symbol="000660",
|
|
strategy_id="trend-follow-v1", status=SlotState.PENDING_BUY)
|
|
store, pause, repo, svc = _wire(slots_setup=setup)
|
|
env = _env(store, pause, repo, available_slots=3, view=View.HELD)
|
|
pids = {a["asset_id"] for a in env.assets}
|
|
assert pids == {"pf-A"}
|
|
|
|
|
|
def test_strategy_key_orthogonal_filter():
|
|
store, pause, repo, svc = _wire()
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL,
|
|
strategy_key="target-value-v1")
|
|
assert {a["active_strategy_id"] for a in env.assets} == {"target-value-v1"}
|
|
|
|
|
|
def test_strategy_key_missing_includes_everything_unmasked():
|
|
store, pause, repo, svc = _wire()
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL, strategy_key=None)
|
|
assert {a["asset_id"] for a in env.assets} == {"pf-A", "pf-B", "pf-C"}
|
|
|
|
|
|
def test_warning_overlay_masks_asset_uniformly_across_views():
|
|
store, pause, repo, svc = _wire()
|
|
store.set_warning("pf-A", True)
|
|
for view in (View.ALL, View.BUYABLE):
|
|
env = _env(store, pause, repo, available_slots=3, view=view)
|
|
pids = {a["asset_id"] for a in env.assets}
|
|
assert "pf-A" not in pids
|
|
|
|
|
|
def test_pause_overlay_masks_asset():
|
|
store, pause, repo, svc = _wire()
|
|
pause.pause("pf-B")
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL)
|
|
assert all(a["asset_id"] != "pf-B" for a in env.assets)
|
|
|
|
|
|
def test_envelope_shape_no_price_field():
|
|
store, pause, repo, svc = _wire()
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL)
|
|
d = env.to_dict()
|
|
# BAT-022: `slots[]` is an additive operator-facing field alongside assets.
|
|
assert set(d.keys()) == {"assets", "view", "available_slots", "degraded",
|
|
"reason", "message", "slots"}
|
|
assert isinstance(d["assets"], list)
|
|
assert isinstance(d["slots"], list)
|
|
# strategy-facing assets[] keep the strict no-price shape (REQ-028 AC-3).
|
|
for asset in d["assets"]:
|
|
assert set(asset.keys()) == {"symbol", "asset_id", "cash_balance",
|
|
"asset_quantity", "active_strategy_id",
|
|
"strategy_params"}
|
|
assert "price" not in asset
|
|
|
|
|
|
def test_empty_envelope_assets_is_list_not_null():
|
|
store, pause, repo, svc = _wire()
|
|
pause.pause("pf-A")
|
|
pause.pause("pf-B")
|
|
pause.pause("pf-C")
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL)
|
|
d = env.to_dict()
|
|
assert d["assets"] == []
|
|
assert d["assets"] is not None
|
|
|
|
|
|
def test_degraded_signal_in_envelope():
|
|
store, pause, repo, svc = _wire()
|
|
store.set_degraded(DegradedReason.PORTFOLIO_HUB_UNAVAILABLE,
|
|
"auth.daemon get failed (Bearer abc123)")
|
|
env = _env(store, pause, repo, available_slots=3, view=View.ALL)
|
|
d = env.to_dict()
|
|
assert d["degraded"] is True
|
|
assert d["reason"] == "portfolio_hub_unavailable"
|
|
# sanitized — credentials stripped
|
|
assert "Bearer abc123" not in d["message"]
|
|
assert "<redacted>" in d["message"] or "auth" in d["message"]
|