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}
|
||||
|
||||
Reference in New Issue
Block a user