feat(slot): BAT-030 — 자산 중심 cutover 완성 (dcy-020 BAT-δ, REQ-056·057·062·064·065)
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>
This commit is contained in:
@@ -1,34 +1,46 @@
|
||||
"""Ledger-gap operational check tests (REQ-055 / dcy-011 FR-5, issue #11).
|
||||
|
||||
PostgreSQL-gated (TREX_POSTGRESQL_TEST_DSN). Inserts closed slots + outbox rows
|
||||
directly and asserts find_ledger_gap_slots detects only the gaps.
|
||||
PostgreSQL-gated (TREX_POSTGRESQL_TEST_DSN). dcy-020 (BAT-030): the detector
|
||||
keys off slot_transition_log sell fills (the `closed` status no longer
|
||||
exists). Inserts sold slots + outbox rows directly and asserts
|
||||
find_ledger_gap_slots detects only the gaps.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import date
|
||||
from datetime import date, datetime, timezone
|
||||
|
||||
from trex_core.adapters.persistence.ops_checks import find_ledger_gap_slots
|
||||
|
||||
_DAY = date(2026, 6, 8)
|
||||
_DAY = date(2026, 6, 10)
|
||||
_TS = datetime(2026, 6, 10, 6, 0, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def _insert_slot(conn, *, status, portfolio_id, symbol="005930"):
|
||||
sid = str(uuid.uuid4())
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO daily_slot (slot_id, trading_date, status, symbol, portfolio_id) "
|
||||
"VALUES (%s, %s, %s, %s, %s)",
|
||||
(sid, _DAY, status, symbol, portfolio_id),
|
||||
"INSERT INTO daily_slot (slot_id, status, symbol, portfolio_id) "
|
||||
"VALUES (%s, %s, %s, %s)",
|
||||
(sid, status, symbol, portfolio_id),
|
||||
)
|
||||
return sid
|
||||
|
||||
|
||||
def _insert_sell_fill(conn, slot_id, *, reason="sell_filled", ts=_TS):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO slot_transition_log (slot_id, from_state, to_state, ts, reason, actor) "
|
||||
"VALUES (%s, 'pending_sell', 'inactive', %s, %s, 'execution_loop')",
|
||||
(slot_id, ts, reason),
|
||||
)
|
||||
|
||||
|
||||
def _insert_outbox(conn, *, asset_id, order_id="O1", execution_id="E1"):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO ledger_outbox (correlation_id, asset_id, order_id, execution_id, "
|
||||
"side, qty, price) VALUES (%s, %s, %s, %s, 'buy', 1, 1000)",
|
||||
"side, qty, price) VALUES (%s, %s, %s, %s, 'sell', 1, 1000)",
|
||||
(f"{asset_id}:{order_id}:{execution_id}", asset_id, order_id, execution_id),
|
||||
)
|
||||
|
||||
@@ -37,23 +49,40 @@ def test_detects_only_gap_slots(pg_clean):
|
||||
import psycopg
|
||||
dsn = pg_clean
|
||||
with psycopg.connect(dsn, autocommit=True) as conn:
|
||||
gap_null = _insert_slot(conn, status="closed", portfolio_id=None) # gap (no pf)
|
||||
gap_no_outbox = _insert_slot(conn, status="closed", portfolio_id="PF-GAP") # gap (no outbox row)
|
||||
ok = _insert_slot(conn, status="closed", portfolio_id="PF-OK") # not a gap
|
||||
gap_null = _insert_slot(conn, status="inactive", portfolio_id=None) # gap (no pf)
|
||||
_insert_sell_fill(conn, gap_null)
|
||||
gap_no_outbox = _insert_slot(conn, status="inactive", portfolio_id="PF-GAP")
|
||||
_insert_sell_fill(conn, gap_no_outbox) # gap (no outbox row)
|
||||
ok = _insert_slot(conn, status="inactive", portfolio_id="PF-OK") # not a gap
|
||||
_insert_sell_fill(conn, ok)
|
||||
_insert_outbox(conn, asset_id="PF-OK")
|
||||
_insert_slot(conn, status="holding", portfolio_id=None) # not closed → ignored
|
||||
never_sold = _insert_slot(conn, status="holding", portfolio_id=None) # no sell fill → ignored
|
||||
|
||||
gaps = find_ledger_gap_slots(dsn, trading_date=_DAY)
|
||||
gap_ids = {str(g["slot_id"]) for g in gaps}
|
||||
assert gap_null in gap_ids
|
||||
assert gap_no_outbox in gap_ids
|
||||
assert ok not in gap_ids
|
||||
assert never_sold not in gap_ids
|
||||
|
||||
|
||||
def test_no_gaps_when_all_recorded(pg_clean):
|
||||
import psycopg
|
||||
dsn = pg_clean
|
||||
with psycopg.connect(dsn, autocommit=True) as conn:
|
||||
_insert_slot(conn, status="closed", portfolio_id="PF-1")
|
||||
sid = _insert_slot(conn, status="inactive", portfolio_id="PF-1")
|
||||
_insert_sell_fill(conn, sid)
|
||||
_insert_outbox(conn, asset_id="PF-1")
|
||||
assert find_ledger_gap_slots(dsn, trading_date=_DAY) == []
|
||||
|
||||
|
||||
def test_date_scope_filters_by_sell_fill_day(pg_clean):
|
||||
import psycopg
|
||||
dsn = pg_clean
|
||||
other_day_ts = datetime(2026, 6, 9, 6, 0, tzinfo=timezone.utc)
|
||||
with psycopg.connect(dsn, autocommit=True) as conn:
|
||||
sid = _insert_slot(conn, status="inactive", portfolio_id="PF-OLD")
|
||||
_insert_sell_fill(conn, sid, ts=other_day_ts)
|
||||
assert find_ledger_gap_slots(dsn, trading_date=_DAY) == []
|
||||
gaps = find_ledger_gap_slots(dsn, trading_date=date(2026, 6, 9))
|
||||
assert {str(g["slot_id"]) for g in gaps} == {sid}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
"""PostgreSQL integration tests (REQ-006). Skipped without TREX_POSTGRESQL_TEST_DSN."""
|
||||
"""PostgreSQL integration tests (REQ-006; BAT-030 asset-centric).
|
||||
Skipped without TREX_POSTGRESQL_TEST_DSN."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore # noqa: F401
|
||||
from trex_core.adapters.persistence.postgresql import (
|
||||
PostgreSQLIntentStore,
|
||||
@@ -12,15 +9,17 @@ from trex_core.adapters.persistence.postgresql import (
|
||||
)
|
||||
from trex_core.application.intents import BuyIntent, IntentService, SellIntent
|
||||
from trex_core.domain.slot import SlotService, SlotState
|
||||
from trex_core.domain.slot.errors import SlotTransitionError
|
||||
|
||||
DAY = date(2026, 6, 3)
|
||||
INST = "default"
|
||||
_SYMS = {"PF-1": "005930", "PF-2": "000660", "PF-3": "035720"}
|
||||
|
||||
|
||||
def _wire(dsn):
|
||||
def _wire(dsn, max_active=None):
|
||||
repo = PostgreSQLSlotRepository(dsn)
|
||||
svc = SlotService(repo)
|
||||
intents = IntentService(svc, repo, PostgreSQLIntentStore(dsn))
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
intents = IntentService(svc, repo, PostgreSQLIntentStore(dsn),
|
||||
resolve_symbol=_SYMS.get,
|
||||
instance_key=INST, max_active_slots=max_active)
|
||||
return repo, svc, intents
|
||||
|
||||
|
||||
@@ -30,30 +29,33 @@ def test_universe_reconcile_pg(pg_clean):
|
||||
# partial-index upsert + find_slot_by_portfolio/count_active.
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
inst = "inst-pg"
|
||||
r1 = repo.reconcile_universe(inst, DAY, {"pf-a", "pf-b"})
|
||||
r1 = repo.reconcile_universe(inst, {"pf-a", "pf-b"})
|
||||
assert r1["created"] == 2
|
||||
r2 = repo.reconcile_universe(inst, DAY, {"pf-a", "pf-b"}) # idempotent (ON CONFLICT)
|
||||
r2 = repo.reconcile_universe(inst, {"pf-a", "pf-b"}) # idempotent (ON CONFLICT)
|
||||
assert r2["created"] == 0
|
||||
assert repo.find_slot_by_portfolio(inst, "pf-a") is not None
|
||||
slot_a = repo.find_slot_by_portfolio(inst, "pf-a")
|
||||
assert slot_a is not None and slot_a.status is SlotState.INACTIVE
|
||||
assert repo.count_active(inst) == 0
|
||||
# pf-b leaves universe while inactive -> soft-retire
|
||||
r3 = repo.reconcile_universe(inst, DAY, {"pf-a"})
|
||||
r3 = repo.reconcile_universe(inst, {"pf-a"})
|
||||
assert r3["retired"] == 1
|
||||
assert repo.find_slot_by_portfolio(inst, "pf-b").retired_at is not None
|
||||
|
||||
|
||||
def test_preallocate_and_capacity(pg_clean):
|
||||
def test_capacity_instance_scoped(pg_clean):
|
||||
repo, svc, _ = _wire(pg_clean)
|
||||
assert svc.preallocate_daily_slots(DAY, 10) == 10
|
||||
assert svc.preallocate_daily_slots(DAY, 10) == 0 # idempotent
|
||||
cap = svc.compute_slot_capacity(DAY, 10)
|
||||
assert cap.idle_count == 10 and cap.scarcity_ratio == 0.0
|
||||
repo.reconcile_universe(INST, set(_SYMS))
|
||||
cap = svc.compute_slot_capacity()
|
||||
assert cap.inactive_count == 3 and cap.available_count == 3
|
||||
cap_m = svc.compute_slot_capacity(max_active_slots=2)
|
||||
assert cap_m.total_slots == 2 and cap_m.available_count == 2
|
||||
assert cap_m.scarcity_ratio == 0.0
|
||||
|
||||
|
||||
def test_buy_intent_persists_transition(pg_clean):
|
||||
repo, svc, intents = _wire(pg_clean)
|
||||
svc.preallocate_daily_slots(DAY, 3)
|
||||
d = intents.submit_buy(BuyIntent(intent_id="pg-buy-1", trading_date=DAY, symbol="005930", qty=10, portfolio_id="PF-1"))
|
||||
repo.reconcile_universe(INST, set(_SYMS))
|
||||
d = intents.submit_buy(BuyIntent(intent_id="pg-buy-1", qty=10, portfolio_id="PF-1"))
|
||||
assert d.admitted
|
||||
slot = repo.fetch_slot(d.slot_id)
|
||||
assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930"
|
||||
@@ -61,29 +63,57 @@ def test_buy_intent_persists_transition(pg_clean):
|
||||
|
||||
def test_buy_intent_idempotent_on_pg(pg_clean):
|
||||
repo, svc, intents = _wire(pg_clean)
|
||||
svc.preallocate_daily_slots(DAY, 3)
|
||||
d1 = intents.submit_buy(BuyIntent(intent_id="pg-dup", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="pg-dup", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
|
||||
repo.reconcile_universe(INST, set(_SYMS))
|
||||
d1 = intents.submit_buy(BuyIntent(intent_id="pg-dup", portfolio_id="PF-1"))
|
||||
d2 = intents.submit_buy(BuyIntent(intent_id="pg-dup", portfolio_id="PF-1"))
|
||||
assert d1.slot_id == d2.slot_id and d2.status == "admitted"
|
||||
pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY]
|
||||
pending = [s for s in repo.list_slots(INST) if s.status is SlotState.PENDING_BUY]
|
||||
assert len(pending) == 1 # no double transition
|
||||
|
||||
|
||||
def test_full_lifecycle_then_closed_reallocation_bumps_entry_count(pg_clean):
|
||||
# dcy-020: closed→idle recovery removed. Reuse a closed slot via
|
||||
# closed→pending_buy reallocation (dcy-019); entry_count is cumulative.
|
||||
def test_buy_by_slot_id_contract_pg(pg_clean):
|
||||
# dcy-020/REQ-061: slot_id is the canonical buy key against real PG.
|
||||
repo, svc, intents = _wire(pg_clean)
|
||||
svc.preallocate_daily_slots(DAY, 1)
|
||||
d = intents.submit_buy(BuyIntent(intent_id="pg-life", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
|
||||
repo.reconcile_universe(INST, set(_SYMS))
|
||||
slot = repo.find_slot_by_portfolio(INST, "PF-2")
|
||||
d = intents.submit_buy(BuyIntent(intent_id="pg-sid", slot_id=slot.slot_id))
|
||||
assert d.admitted and d.slot_id == slot.slot_id
|
||||
assert repo.fetch_slot(slot.slot_id).symbol == "000660"
|
||||
|
||||
|
||||
def test_full_lifecycle_returns_to_inactive_and_reuses(pg_clean):
|
||||
# dcy-020 (BAT-030): sell fill -> inactive (occupancy cleared, asset
|
||||
# binding kept); the same permanent slot activates again.
|
||||
repo, svc, intents = _wire(pg_clean)
|
||||
repo.reconcile_universe(INST, set(_SYMS))
|
||||
d = intents.submit_buy(BuyIntent(intent_id="pg-life", portfolio_id="PF-1"))
|
||||
sid = d.slot_id
|
||||
svc.transit_slot(sid, SlotState.PENDING_BUY, SlotState.HOLDING, reason="filled", actor="t")
|
||||
svc.transit_slot(sid, SlotState.PENDING_BUY, SlotState.HOLDING,
|
||||
reason="filled", actor="t", qty=10.0, avg_price=1000.0)
|
||||
intents.submit_sell(SellIntent(intent_id="pg-sell", slot_id=sid))
|
||||
svc.transit_slot(sid, SlotState.PENDING_SELL, SlotState.CLOSED, reason="filled", actor="t")
|
||||
assert repo.fetch_slot(sid).entry_count == 1
|
||||
# closed→idle is now rejected
|
||||
with pytest.raises(SlotTransitionError):
|
||||
svc.transit_slot(sid, SlotState.CLOSED, SlotState.IDLE, reason="recovery", actor="t")
|
||||
# reuse via reallocation
|
||||
realloc = svc.transit_slot(sid, SlotState.CLOSED, SlotState.PENDING_BUY,
|
||||
reason="reallocate", actor="t", symbol="000660")
|
||||
assert realloc.entry_count == 2 and realloc.symbol == "000660"
|
||||
svc.transit_slot(sid, SlotState.PENDING_SELL, SlotState.INACTIVE,
|
||||
reason="sell_filled", actor="t")
|
||||
flat = repo.fetch_slot(sid)
|
||||
assert flat.status is SlotState.INACTIVE
|
||||
assert flat.qty is None and flat.portfolio_id == "PF-1"
|
||||
# permanent reuse
|
||||
again = svc.transit_slot(sid, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="re-entry", actor="t", symbol="005930")
|
||||
assert again.status is SlotState.PENDING_BUY
|
||||
|
||||
|
||||
def test_dispatch_queue_instance_scoped(pg_clean):
|
||||
# dcy-020/REQ-056 (BAT-030): list_undispatched_pending / list_dispatched_pending
|
||||
# never cross instance boundaries in a shared DB.
|
||||
repo_a = PostgreSQLSlotRepository(pg_clean)
|
||||
repo_a.reconcile_universe("inst-a", {"pf-x"})
|
||||
repo_a.reconcile_universe("inst-b", {"pf-x"})
|
||||
svc_a = SlotService(repo_a, instance_key="inst-a")
|
||||
slot_a = repo_a.find_slot_by_portfolio("inst-a", "pf-x")
|
||||
svc_a.transit_slot(slot_a.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="r", actor="t", symbol="005930")
|
||||
assert [s.slot_id for s in repo_a.list_undispatched_pending("inst-a")] == [slot_a.slot_id]
|
||||
assert repo_a.list_undispatched_pending("inst-b") == []
|
||||
repo_a.mark_dispatched(slot_a.slot_id, "ord-1")
|
||||
assert [s.slot_id for s in repo_a.list_dispatched_pending("inst-a")] == [slot_a.slot_id]
|
||||
assert repo_a.list_dispatched_pending("inst-b") == []
|
||||
|
||||
Reference in New Issue
Block a user