feat(intents): dcy-011 buy intent portfolio_id 필수 계약 + ledger skip 가시화 (BAT-015)

dcy-011 전 주기 delivery. issue #6(buy 식별자 정정) + issue #11(ledger silent skip) 흡수.
거버넌스: 기존 Implemented REQ는 change-req, 신규 기능만 신규 REQ(OQ-3).

- REQ-007 (change-req): buy admission portfolio_id 필수화 + SnapshotStore symbol
  resolve + unknown_portfolio 거부 (hard cutover, OQ-1/OQ-2)
- REQ-021 (change-req): buy 메시지 payload symbol 제거
- REQ-018 (change-req): _record_ledger skip 가시화 (WARN + ledger_skipped 카운터)
- REQ-055 (신규): closed↔ledger_outbox anti-join 운영 점검 find_ledger_gap_slots

코드: intents.py(BuyIntent portfolio_id 필수 + resolve_symbol 주입 + _admit_buy
reject 분기), messaging_handlers.py, runtime/daemon.py(_resolve_symbol 배선),
execution.py(skip WARN 로그), adapters/persistence/ops_checks.py(신규 anti-join).

검증: 단위 282 passed/32 skipped, 인프라 게이트(PG+RabbitMQ) 314 passed, ruff clean.
BAT-015(standard) release-candidate. srs/index Next ID → REQ-056.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 12:57:51 +09:00
co-authored by Claude Opus 4.8
parent a24ebb242e
commit aee3512383
27 changed files with 729 additions and 33 deletions
@@ -52,7 +52,8 @@ def test_intent_admission_rpc_dedups_duplicate_correlation_id_over_rabbitmq(mq_u
"""REQ-023 AC-1: same correlation_id processed twice ⇒ slot transitions ONCE."""
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
intents = IntentService(svc, repo, InMemoryIntentStore(),
resolve_symbol=lambda pid: "005930") # dcy-011: universe resolve
svc.preallocate_daily_slots(DAY, 3)
handler = make_intent_rpc_handler(intents)
@@ -77,7 +78,7 @@ def test_intent_admission_rpc_dedups_duplicate_correlation_id_over_rabbitmq(mq_u
cid = "buy-dup-rmq"
payload = {
"intent_type": "buy", "intent_id": cid,
"trading_date": DAY.isoformat(), "symbol": "005930", "qty": 10,
"trading_date": DAY.isoformat(), "portfolio_id": "PF-1", "qty": 10,
}
r1 = client.call_rpc(queue, payload, correlation_id=cid, timeout=3.0)
r2 = client.call_rpc(queue, payload, correlation_id=cid, timeout=3.0)
@@ -0,0 +1,59 @@
"""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.
"""
from __future__ import annotations
import uuid
from datetime import date
from trex_core.adapters.persistence.ops_checks import find_ledger_gap_slots
_DAY = date(2026, 6, 8)
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),
)
return sid
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)",
(f"{asset_id}:{order_id}:{execution_id}", asset_id, order_id, execution_id),
)
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
_insert_outbox(conn, asset_id="PF-OK")
_insert_slot(conn, status="holding", portfolio_id=None) # not closed → 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
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")
_insert_outbox(conn, asset_id="PF-1")
assert find_ledger_gap_slots(dsn, trading_date=_DAY) == []
@@ -32,7 +32,7 @@ def test_preallocate_and_capacity(pg_clean):
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))
d = intents.submit_buy(BuyIntent(intent_id="pg-buy-1", trading_date=DAY, symbol="005930", 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"
@@ -41,8 +41,8 @@ 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"))
d2 = intents.submit_buy(BuyIntent(intent_id="pg-dup", trading_date=DAY, symbol="005930"))
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"))
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]
assert len(pending) == 1 # no double transition
@@ -51,7 +51,7 @@ def test_buy_intent_idempotent_on_pg(pg_clean):
def test_full_lifecycle_recovery_bumps_entry_count(pg_clean):
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"))
d = intents.submit_buy(BuyIntent(intent_id="pg-life", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
sid = d.slot_id
svc.transit_slot(sid, SlotState.PENDING_BUY, SlotState.HOLDING, reason="filled", actor="t")
intents.submit_sell(SellIntent(intent_id="pg-sell", slot_id=sid))
+1 -1
View File
@@ -26,7 +26,7 @@ def _wire(broker):
def _buy(intents, iid="b1", symbol="005930", qty=10):
return intents.submit_buy(BuyIntent(intent_id=iid, trading_date=DAY, symbol=symbol, qty=qty, entry_price=1000))
return intents.submit_buy(BuyIntent(intent_id=iid, trading_date=DAY, symbol=symbol, qty=qty, entry_price=1000, portfolio_id="PF-1"))
def test_buy_lifecycle_to_holding():
+22 -1
View File
@@ -80,7 +80,13 @@ def test_portfolio_id_absent_skips_ledger_but_transitions_slot():
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
sid = _buy(intents, iid="no-pf", portfolio_id=None).slot_id
# dcy-011: buy admission now requires portfolio_id, so create the
# pending_buy slot directly (bypass admission) to exercise the
# execution-loop ledger skip + visibility (REQ-018 FR-4).
idle = repo.find_idle_slot(DAY)
svc.transit_slot(idle.slot_id, SlotState.IDLE, SlotState.PENDING_BUY,
reason="buy", actor="test", symbol="005930", portfolio_id=None)
sid = idle.slot_id
r = execution.run_once()
assert r.filled == 1 and r.ledger_enqueued == 0 and r.ledger_skipped == 1
assert repo.fetch_slot(sid).status is SlotState.HOLDING
@@ -88,6 +94,21 @@ def test_portfolio_id_absent_skips_ledger_but_transitions_slot():
assert ledger.count() == 0
def test_ledger_skip_emits_warning(caplog):
"""dcy-011 / REQ-018 FR-4 (S-3): ledger skip is logged, not silent."""
import logging
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
idle = repo.find_idle_slot(DAY)
svc.transit_slot(idle.slot_id, SlotState.IDLE, SlotState.PENDING_BUY,
reason="buy", actor="test", symbol="005930", portfolio_id=None)
with caplog.at_level(logging.WARNING, logger="trex_core.application.execution"):
execution.run_once()
msgs = [r.getMessage() for r in caplog.records]
assert any("ledger skip" in m and "missing_portfolio_id" in m for m in msgs)
def test_duplicate_execution_id_results_in_zero_duplicate_in_ledger():
"""REQ-019 AC-1: same execution_id processed twice = 1 ledger entry."""
ledger = InMemoryLedger()
+2 -2
View File
@@ -26,7 +26,7 @@ def test_concurrent_buys_admit_exactly_idle_count():
results_lock = threading.Lock()
def worker(i: int) -> None:
d = intents.submit_buy(BuyIntent(intent_id=f"buy-{i}", trading_date=DAY, symbol="005930", qty=1))
d = intents.submit_buy(BuyIntent(intent_id=f"buy-{i}", trading_date=DAY, symbol="005930", qty=1, portfolio_id=f"PF-{i}"))
with results_lock:
results.append(d.status)
@@ -56,7 +56,7 @@ def test_duplicate_intent_id_admits_once_under_concurrency():
lock = threading.Lock()
def worker() -> None:
d = intents.submit_buy(BuyIntent(intent_id="same-id", trading_date=DAY, symbol="005930"))
d = intents.submit_buy(BuyIntent(intent_id="same-id", trading_date=DAY, symbol="005930", portfolio_id="PF-1"))
with lock:
statuses.append(d.status)
@@ -22,18 +22,21 @@ DAY = date(2026, 6, 5)
def _wire():
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
# dcy-011 / REQ-007: buy resolves symbol from the universe via resolver.
intents = IntentService(svc, repo, InMemoryIntentStore(),
resolve_symbol=lambda pid: "005930")
svc.preallocate_daily_slots(DAY, 3)
return repo, svc, intents
def _buy_req(intent_id: str, symbol: str = "005930") -> RpcRequest:
def _buy_req(intent_id: str, portfolio_id: str = "PF-1") -> RpcRequest:
# dcy-011 / REQ-021: buy payload carries portfolio_id (required); no symbol.
return RpcRequest(
payload={
"intent_type": "buy",
"intent_id": intent_id,
"trading_date": DAY.isoformat(),
"symbol": symbol,
"portfolio_id": portfolio_id,
"qty": 10,
},
correlation_id=intent_id,
@@ -119,7 +122,7 @@ def test_correlation_id_is_used_as_intent_id_when_missing_in_payload():
payload={
"intent_type": "buy",
"trading_date": DAY.isoformat(),
"symbol": "005930",
"portfolio_id": "PF-1",
"qty": 5,
},
correlation_id="cid-only",
@@ -130,3 +133,43 @@ def test_correlation_id_is_used_as_intent_id_when_missing_in_payload():
assert r1.payload == r2.payload
pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY]
assert len(pending) == 1
# ---- dcy-011 / REQ-007/021: portfolio_id required + symbol resolve ----------
def test_buy_without_portfolio_id_rejected():
"""FR-1 / S-1: missing portfolio_id → portfolio_id_required reject."""
repo, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
req = RpcRequest(
payload={"intent_type": "buy", "intent_id": "no-pf",
"trading_date": DAY.isoformat(), "qty": 5},
correlation_id="no-pf",
)
resp = handler(req)
assert resp.payload["status"] == "rejected"
assert resp.payload["reason"] == "portfolio_id_required"
def test_buy_unknown_portfolio_rejected():
"""FR-3 / S-1: portfolio_id absent from universe → unknown_portfolio reject."""
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore(),
resolve_symbol=lambda pid: None) # universe empty
svc.preallocate_daily_slots(DAY, 3)
handler = make_intent_rpc_handler(intents)
resp = handler(_buy_req("u1", portfolio_id="PF-X"))
assert resp.payload["status"] == "rejected"
assert resp.payload["reason"] == "unknown_portfolio"
def test_buy_symbol_resolved_from_universe():
"""FR-2 / S-1: symbol is resolved server-side from portfolio_id (no client symbol)."""
repo, _, intents = _wire() # resolver → "005930"
handler = make_intent_rpc_handler(intents)
resp = handler(_buy_req("r1", portfolio_id="PF-9"))
assert resp.payload["status"] == "admitted"
from uuid import UUID
slot = repo.fetch_slot(UUID(resp.payload["slot_id"]))
assert slot.symbol == "005930" and slot.portfolio_id == "PF-9"
@@ -36,7 +36,7 @@ def test_snapshot_returns_all_slots_and_capacity():
repo, svc, intents = _wire()
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10,
strategy_id="A",
strategy_id="A", portfolio_id="PF-1",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req())
@@ -51,6 +51,7 @@ def test_snapshot_open_view_filters_to_active_slots():
repo, svc, intents = _wire()
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10, strategy_id="A",
portfolio_id="PF-1",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req(view="open"))
@@ -63,9 +64,11 @@ def test_snapshot_strategy_id_filter():
repo, svc, intents = _wire()
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10, strategy_id="A",
portfolio_id="PF-1",
))
intents.submit_buy(BuyIntent(
intent_id="b2", trading_date=DAY, symbol="000660", qty=5, strategy_id="B",
portfolio_id="PF-2",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req(view="open", strategy_id="A"))