feat: add messaging transport rollout

This commit is contained in:
2026-06-06 00:02:52 +09:00
parent 4b91343ee8
commit 6c7a1cf6f6
383 changed files with 99045 additions and 487 deletions
@@ -0,0 +1,131 @@
"""Slot transition event emission tests (REQ-021 + REQ-023 + REQ-018 hook)."""
from __future__ import annotations
import threading
import time
from datetime import date
from trex_core.adapters.broker.paper import PaperBroker
from trex_core.adapters.ledger.in_memory import InMemoryLedger
from trex_core.adapters.messaging import topology
from trex_core.adapters.messaging.in_memory import InMemoryTransport
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
from trex_core.application.execution import ExecutionService
from trex_core.application.intents import BuyIntent, IntentService
from trex_core.application.ledger_outbox import InMemoryLedgerOutbox, LedgerWriter
from trex_core.application.messaging_handlers import slot_transition_event_payload
from trex_core.domain.slot import SlotService, SlotState
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
DAY = date(2026, 6, 5)
def _wire(transport):
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
outbox = InMemoryLedgerOutbox()
ledger = InMemoryLedger()
writer = LedgerWriter(outbox, ledger)
svc.preallocate_daily_slots(DAY, 3)
def emitter(event_type: str, slot) -> None:
rk = topology.event_routing_key(event_type, slot.strategy_id or "_", slot.symbol or "_")
payload = slot_transition_event_payload(slot, event_type=event_type)
try:
transport.publish_event(rk, payload)
except Exception: # noqa: BLE001
pass
execution = ExecutionService(svc, repo, PaperBroker(fill_after_polls=0),
ledger_outbox=outbox, event_emitter=emitter)
return repo, svc, intents, execution, writer
def test_fill_emits_slot_filled_event_with_routing_key_and_state():
transport = InMemoryTransport()
received: list[tuple[str, dict]] = []
stop = threading.Event()
th = threading.Thread(
target=transport.consume_events_forever,
args=("trex.events.strategy.A", topology.strategy_event_binding_pattern("A"),
lambda rk, p: received.append((rk, p)), stop),
daemon=True,
)
th.start()
time.sleep(0.05)
repo, _, intents, execution, _ = _wire(transport)
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10,
strategy_id="A", portfolio_id="PF-1",
))
execution.run_once()
time.sleep(0.05)
stop.set()
th.join(timeout=1.0)
assert len(received) == 1
rk, payload = received[0]
assert rk == "slot_filled_buy.A.005930"
# payload carries current state + monotonic sequence (REQ-023 AC-5)
assert payload["event_type"] == "slot_filled_buy"
assert payload["status"] == SlotState.HOLDING.value
assert payload["strategy_id"] == "A"
assert payload["symbol"] == "005930"
assert "sequence_at" in payload # last-wins ordering token
def test_event_for_strategy_a_does_not_reach_strategy_b_queue():
"""REQ-021 AC-4: per-strategy routing key binding filters at the broker."""
transport = InMemoryTransport()
received_a: list[tuple[str, dict]] = []
received_b: list[tuple[str, dict]] = []
stop = threading.Event()
th_a = threading.Thread(
target=transport.consume_events_forever,
args=("q.A", topology.strategy_event_binding_pattern("A"),
lambda rk, p: received_a.append((rk, p)), stop),
daemon=True,
)
th_b = threading.Thread(
target=transport.consume_events_forever,
args=("q.B", topology.strategy_event_binding_pattern("B"),
lambda rk, p: received_b.append((rk, p)), stop),
daemon=True,
)
th_a.start()
th_b.start()
time.sleep(0.05)
_, _, intents, execution, _ = _wire(transport)
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10,
strategy_id="A", portfolio_id="PF-1",
))
execution.run_once()
time.sleep(0.05)
stop.set()
th_a.join(timeout=1.0)
th_b.join(timeout=1.0)
assert any(rk == "slot_filled_buy.A.005930" for rk, _ in received_a)
assert received_b == []
def test_publish_failure_does_not_break_execution_loop():
"""Publish errors must NOT roll back slot transitions or ledger enqueue."""
transport = InMemoryTransport()
transport.set_available(False) # all publishes will raise TransportUnavailableError
repo, _, intents, execution, _ = _wire(transport)
intents.submit_buy(BuyIntent(
intent_id="b1", trading_date=DAY, symbol="005930", qty=10,
strategy_id="A", portfolio_id="PF-1",
))
r = execution.run_once()
# Slot still transitioned; ledger still enqueued.
assert r.filled == 1
assert r.ledger_enqueued == 1
pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.HOLDING]
assert len(pending) == 1
@@ -0,0 +1,132 @@
"""Intent admission RPC handler + dedup tests (REQ-021/022/023, dcy-006).
The handler calls IntentService.submit_*, so dedup uses the IntentStore
(intent_log) gate (REQ-007). Two messages with the same correlation_id
trigger exactly ONE slot transition (REQ-023 AC-1).
"""
from __future__ import annotations
from datetime import date
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
from trex_core.application.intents import IntentService
from trex_core.application.messaging import RpcRequest
from trex_core.application.messaging_handlers import make_intent_rpc_handler
from trex_core.domain.slot import SlotService, SlotState
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
DAY = date(2026, 6, 5)
def _wire():
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
svc.preallocate_daily_slots(DAY, 3)
return repo, svc, intents
def _buy_req(intent_id: str, symbol: str = "005930") -> RpcRequest:
return RpcRequest(
payload={
"intent_type": "buy",
"intent_id": intent_id,
"trading_date": DAY.isoformat(),
"symbol": symbol,
"qty": 10,
},
correlation_id=intent_id,
)
def test_buy_admission_returns_admitted_and_transitions_slot():
repo, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
resp = handler(_buy_req("buy-1"))
assert resp.payload["status"] == "admitted"
assert resp.payload["slot_id"]
from uuid import UUID
slot = repo.fetch_slot(UUID(resp.payload["slot_id"]))
assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930"
def test_duplicate_correlation_id_dedups_via_intent_log():
"""REQ-023 AC-1: same intent_id ⇒ slot transition exactly once."""
repo, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
r1 = handler(_buy_req("buy-dup"))
r2 = handler(_buy_req("buy-dup"))
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
def test_buy_rejected_when_no_idle_slot():
repo, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
for i in range(3):
assert handler(_buy_req(f"buy-{i}")).payload["status"] == "admitted"
resp = handler(_buy_req("overflow"))
assert resp.payload["status"] == "rejected"
assert resp.payload["reason"] == "no_idle_slot"
def test_sell_admission_from_holding():
repo, svc, intents = _wire()
handler = make_intent_rpc_handler(intents)
sid = handler(_buy_req("buy-h")).payload["slot_id"]
from uuid import UUID
svc.transit_slot(UUID(sid), SlotState.PENDING_BUY, SlotState.HOLDING,
reason="filled", actor="t")
resp = handler(RpcRequest(
payload={"intent_type": "sell", "intent_id": "sell-h", "slot_id": sid},
correlation_id="sell-h",
))
assert resp.payload["status"] == "admitted"
assert repo.fetch_slot(UUID(sid)).status is SlotState.PENDING_SELL
def test_bad_payload_returns_rejected_not_exception():
"""Handler must not raise — bad input ⇒ structured rejection."""
_, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
# missing slot_id for sell
resp = handler(RpcRequest(
payload={"intent_type": "sell", "intent_id": "bad"},
correlation_id="bad",
))
assert resp.payload["status"] == "rejected"
assert resp.payload["reason"].startswith("bad_request:")
def test_unknown_intent_type_rejected():
_, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
resp = handler(RpcRequest(
payload={"intent_type": "ping", "intent_id": "x"},
correlation_id="x",
))
assert resp.payload["status"] == "rejected"
assert "unknown_intent_type" in resp.payload["reason"]
def test_correlation_id_is_used_as_intent_id_when_missing_in_payload():
"""The transport's correlation_id is the canonical idempotency key."""
repo, _, intents = _wire()
handler = make_intent_rpc_handler(intents)
req = RpcRequest(
payload={
"intent_type": "buy",
"trading_date": DAY.isoformat(),
"symbol": "005930",
"qty": 5,
},
correlation_id="cid-only",
)
r1 = handler(req)
r2 = handler(req)
assert r1.payload["status"] == "admitted"
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
@@ -0,0 +1,74 @@
"""Snapshot RPC handler tests (REQ-021 AC-2, BAT-007 stub-shape)."""
from __future__ import annotations
from datetime import date
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
from trex_core.application.intents import BuyIntent, IntentService
from trex_core.application.messaging import RpcRequest
from trex_core.application.messaging_handlers import make_snapshot_rpc_handler
from trex_core.domain.slot import SlotService
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
DAY = date(2026, 6, 5)
def _wire():
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
svc.preallocate_daily_slots(DAY, 3)
return repo, svc, intents
def _req(view="all", strategy_id=None):
return RpcRequest(
payload={
"view": view,
"strategy_id": strategy_id,
"trading_date": DAY.isoformat(),
},
correlation_id="snap-cid",
)
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",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req())
assert resp.payload["trading_date"] == DAY.isoformat()
assert resp.payload["view"] == "all"
assert len(resp.payload["slots"]) == 3 # all 3 slots returned
cap = resp.payload["capacity"]
assert cap["pending_count"] == 1 and cap["idle_count"] == 2
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",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req(view="open"))
statuses = {s["status"] for s in resp.payload["slots"]}
assert "idle" not in statuses
assert statuses == {"pending_buy"}
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",
))
intents.submit_buy(BuyIntent(
intent_id="b2", trading_date=DAY, symbol="000660", qty=5, strategy_id="B",
))
handler = make_snapshot_rpc_handler(repo, svc)
resp = handler(_req(view="open", strategy_id="A"))
assert len(resp.payload["slots"]) == 1
assert resp.payload["slots"][0]["strategy_id"] == "A"
assert resp.payload["slots"][0]["symbol"] == "005930"