Implement BAT-006: portfolio-hub ledger integration (REQ-017~019)

- REQ-017: LedgerPort contract (application/ledger_port.py) + portfolio-hub
  adapter + in-memory stub (adapters/ledger/), TREX_PORTFOLIO_HUB_* config
- REQ-018: fill→ledger recording in execution loop; FillStatus carries
  execution_id (deterministic synth) + fee; paper/kis adapters updated
- REQ-019: ledger outbox + retry worker (application/ledger_outbox.py),
  correlation_id idempotency, outcome vocabulary; 002_create_ledger_outbox.sql
- Tests: in-memory/portfolio-hub ledger, execution ledger hook, outbox,
  domain import sweep

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-04 22:57:31 +09:00
co-authored by Claude Opus 4.8
parent 6277d610ea
commit 13837acc60
20 changed files with 1224 additions and 15 deletions
View File
@@ -0,0 +1,58 @@
"""In-memory ledger stub tests (REQ-017 AC-1)."""
from __future__ import annotations
from datetime import datetime, timezone
import pytest
from trex_core.adapters.ledger.in_memory import InMemoryLedger
from trex_core.application.ledger_port import (
LedgerConflictError,
LedgerRetryableError,
LedgerTrade,
)
def _trade(execution_id="paper:PAPER-000001:10") -> LedgerTrade:
return LedgerTrade(
asset_id="PF-1", order_id="PAPER-000001", execution_id=execution_id,
side="buy", qty=10, price=1000.0, fee=1.5, tax=0.0,
executed_at=datetime(2026, 6, 4, 9, 0, tzinfo=timezone.utc),
)
def test_record_trade_preserves_fields_and_is_idempotent_on_execution_id():
ledger = InMemoryLedger()
t = _trade()
ledger.record_trade(t)
ledger.record_trade(t) # same execution_id — no duplicate entry
assert ledger.count() == 1
stored = ledger.trades()[0]
assert (stored.asset_id, stored.side, stored.qty, stored.price,
stored.fee, stored.tax, stored.executed_at) == (
"PF-1", "buy", 10, 1000.0, 1.5, 0.0,
datetime(2026, 6, 4, 9, 0, tzinfo=timezone.utc),
)
def test_retryable_failure_then_success():
ledger = InMemoryLedger(fail_n_then_succeed=2)
t = _trade()
with pytest.raises(LedgerRetryableError):
ledger.record_trade(t)
with pytest.raises(LedgerRetryableError):
ledger.record_trade(t)
ledger.record_trade(t) # third attempt succeeds
assert ledger.count() == 1
def test_conflict_surfaces_as_ledger_conflict_error():
ledger = InMemoryLedger(conflict_executions={"paper:PAPER-000001:10"})
with pytest.raises(LedgerConflictError):
ledger.record_trade(_trade())
assert ledger.count() == 0
def test_correlation_id_format():
t = _trade()
assert t.correlation_id == "PF-1:PAPER-000001:paper:PAPER-000001:10"
@@ -0,0 +1,71 @@
"""portfolio-hub HTTP ledger adapter tests (REQ-017 AC-2/3)."""
from __future__ import annotations
from datetime import datetime, timezone
import httpx
import pytest
from trex_core.adapters.ledger.portfolio_hub import PortfolioHubLedger
from trex_core.application.ledger_port import (
LedgerConflictError,
LedgerRetryableError,
LedgerTrade,
)
def _trade(asset="PF-1", execution_id="paper:PAPER-000001:10") -> LedgerTrade:
return LedgerTrade(
asset_id=asset, order_id="PAPER-000001", execution_id=execution_id,
side="buy", qty=10, price=1000.0, fee=1.5, tax=0.0,
executed_at=datetime(2026, 6, 4, 9, 0, tzinfo=timezone.utc),
)
def _client(handler):
return httpx.Client(transport=httpx.MockTransport(handler))
def test_post_contract_includes_execution_id_and_path_uses_asset_id():
captured = {}
def handler(request: httpx.Request) -> httpx.Response:
captured["url"] = str(request.url)
captured["body"] = request.read().decode()
captured["auth"] = request.headers.get("authorization")
return httpx.Response(201, json={"ok": True})
adapter = PortfolioHubLedger("https://portfolio-hub.test", token="tok",
client=_client(handler))
adapter.record_trade(_trade(asset="PF-77"))
assert captured["url"] == "https://portfolio-hub.test/v1/assets/PF-77/ledger/trades"
assert "execution_id" in captured["body"]
assert "\"side\": \"buy\"" in captured["body"]
assert captured["auth"] == "Bearer tok"
def test_409_surfaces_as_ledger_conflict_error():
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(409, text='{"error":"duplicate execution"}')
adapter = PortfolioHubLedger("https://portfolio-hub.test", client=_client(handler))
with pytest.raises(LedgerConflictError):
adapter.record_trade(_trade())
def test_5xx_is_retryable():
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(503, text="upstream down")
adapter = PortfolioHubLedger("https://portfolio-hub.test", client=_client(handler))
with pytest.raises(LedgerRetryableError):
adapter.record_trade(_trade())
def test_network_error_is_retryable():
def handler(request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("boom")
adapter = PortfolioHubLedger("https://portfolio-hub.test", client=_client(handler))
with pytest.raises(LedgerRetryableError):
adapter.record_trade(_trade())
+179
View File
@@ -0,0 +1,179 @@
"""Execution loop ledger-hook tests (REQ-018, REQ-019, dcy-004).
Exercises the full buy/sell lifecycle with the in-memory paper broker and
in-memory outbox + ledger writer, ensuring the slot transition is on the
critical path while the ledger write is eventually-consistent.
"""
from __future__ import annotations
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.persistence.in_memory_intent_store import InMemoryIntentStore
from trex_core.application.execution import ExecutionService
from trex_core.application.intents import BuyIntent, IntentService, SellIntent
from trex_core.application.ledger_outbox import (
InMemoryLedgerOutbox,
LedgerWriter,
OutboxOutcome,
WorkItemStatus,
)
from trex_core.domain.slot import SlotService, SlotState
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
DAY = date(2026, 6, 4)
def _wire(broker, *, ledger=None):
repo = InMemorySlotRepository()
svc = SlotService(repo)
intents = IntentService(svc, repo, InMemoryIntentStore())
outbox = InMemoryLedgerOutbox()
execution = ExecutionService(svc, repo, broker, ledger_outbox=outbox)
writer = LedgerWriter(outbox, ledger or InMemoryLedger())
svc.preallocate_daily_slots(DAY, 5)
return repo, svc, intents, execution, outbox, writer
def _buy(intents, *, iid="b1", portfolio_id="PF-1"):
return intents.submit_buy(BuyIntent(
intent_id=iid, trading_date=DAY, symbol="005930", qty=10, entry_price=1000,
portfolio_id=portfolio_id,
))
def test_buy_fill_enqueues_one_ledger_item_and_writer_records_it():
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
sid = _buy(intents).slot_id
r = execution.run_once()
assert r.filled == 1 and r.ledger_enqueued == 1
assert repo.fetch_slot(sid).status is SlotState.HOLDING
stats = writer.run_once()
assert stats.completed == 1
assert ledger.count() == 1
t = ledger.trades()[0]
assert (t.asset_id, t.side, t.qty, t.price) == ("PF-1", "buy", 10, 1000.0)
assert t.execution_id.startswith("paper:PAPER-")
assert t.fee > 0.0 and t.tax == 0.0
def test_sell_fill_enqueues_sell_ledger_item():
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
sid = _buy(intents).slot_id
execution.run_once() # -> holding + buy ledger item
writer.run_once() # drain buy
intents.submit_sell(SellIntent(intent_id="s1", slot_id=sid))
r = execution.run_once() # dispatch sell + fill -> closed + sell ledger item
assert r.ledger_enqueued == 1
assert repo.fetch_slot(sid).status is SlotState.CLOSED
writer.run_once()
sides = [t.side for t in ledger.trades()]
assert sides == ["buy", "sell"]
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
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
assert outbox.all_items() == []
assert ledger.count() == 0
def test_duplicate_execution_id_results_in_zero_duplicate_in_ledger():
"""REQ-019 AC-1: same execution_id enqueued twice = 1 ledger entry."""
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
_buy(intents).slot_id
execution.run_once()
writer.run_once()
assert ledger.count() == 1
# Re-enqueue the same trade explicitly (simulate accidental re-fill).
first = outbox.all_items()[0]
outbox.enqueue(first.trade) # idempotent on correlation_id
stats = writer.run_once() # still terminal-completed; reports duplicate
assert stats.duplicates >= 1
assert ledger.count() == 1 # no duplicate in the ledger
def test_retryable_failure_then_eventually_completes():
"""REQ-019 AC-2: retry until completed; slot transition unaffected."""
ledger = InMemoryLedger(fail_n_then_succeed=2)
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
sid = _buy(intents).slot_id
execution.run_once()
assert repo.fetch_slot(sid).status is SlotState.HOLDING # slot already transitioned
stats1 = writer.run_once()
assert stats1.retryable == 1 and stats1.completed == 0 and ledger.count() == 0
stats2 = writer.run_once()
assert stats2.retryable == 1 and ledger.count() == 0
stats3 = writer.run_once()
assert stats3.completed == 1 and ledger.count() == 1
# outbox row terminal-completed
[item] = outbox.all_items()
assert item.status is WorkItemStatus.COMPLETED
assert item.last_outcome is OutboxOutcome.COMPLETED
def test_conflict_blocks_without_auto_retry():
"""REQ-019 AC-3: 409 conflict halts auto-retry."""
# The execution_id paper synthesizes is paper:{oid}:{qty}; first dispatched
# order id is PAPER-000001 / qty=10 => execution_id = paper:PAPER-000001:10.
conflict_id = "paper:PAPER-000001:10"
ledger = InMemoryLedger(conflict_executions={conflict_id})
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
_buy(intents)
execution.run_once()
stats = writer.run_once()
assert stats.conflicts == 1
[item] = outbox.all_items()
assert item.status is WorkItemStatus.CONFLICT_BLOCKED
# second writer tick must NOT call the adapter again
stats2 = writer.run_once()
assert stats2.completed == 0 and stats2.retryable == 0
# ledger is still empty (conflict blocked; operator resolves)
assert ledger.count() == 0
def test_duplicate_completed_when_reprocessing_terminal_item():
"""REQ-019 AC-4: a completed item reports duplicate_completed if re-listed."""
ledger = InMemoryLedger()
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
_buy(intents)
execution.run_once()
writer.run_once()
# Force a duplicate enqueue of the same trade — outbox keeps single row,
# ledger keeps single entry.
[item] = outbox.all_items()
outbox.enqueue(item.trade)
assert len(outbox.all_items()) == 1
# Writer sees no PENDING rows now; stats are all zero (terminal).
stats = writer.run_once()
assert stats.completed == 0 and stats.duplicates == 0 and stats.retryable == 0
assert ledger.count() == 1
def test_slot_transition_proceeds_when_ledger_is_down():
"""REQ-019 AC-5: portfolio-hub outage does not block the fill transition."""
ledger = InMemoryLedger(fail_n_then_succeed=100)
repo, svc, intents, execution, outbox, writer = _wire(PaperBroker(fill_after_polls=0),
ledger=ledger)
sid = _buy(intents).slot_id
execution.run_once()
assert repo.fetch_slot(sid).status is SlotState.HOLDING
writer.run_once() # all retries fail
assert ledger.count() == 0
[item] = outbox.all_items()
assert item.status is WorkItemStatus.PENDING # still waiting for retry
+65
View File
@@ -0,0 +1,65 @@
"""LedgerOutbox + LedgerWriter direct unit tests (REQ-019)."""
from __future__ import annotations
from datetime import datetime, timezone
from trex_core.adapters.ledger.in_memory import InMemoryLedger
from trex_core.application.ledger_outbox import (
InMemoryLedgerOutbox,
LedgerWriter,
OutboxOutcome,
WorkItemStatus,
)
from trex_core.application.ledger_port import LedgerTrade
def _trade(execution_id="paper:PAPER-1:10") -> LedgerTrade:
return LedgerTrade(
asset_id="PF-1", order_id="PAPER-1", execution_id=execution_id,
side="buy", qty=10, price=1000.0,
executed_at=datetime(2026, 6, 4, tzinfo=timezone.utc),
)
def test_enqueue_is_idempotent_on_correlation_id():
outbox = InMemoryLedgerOutbox()
a = outbox.enqueue(_trade())
b = outbox.enqueue(_trade())
assert a.correlation_id == b.correlation_id
assert len(outbox.all_items()) == 1
def test_writer_run_once_drains_pending_and_marks_completed():
outbox = InMemoryLedgerOutbox()
ledger = InMemoryLedger()
writer = LedgerWriter(outbox, ledger)
outbox.enqueue(_trade(execution_id="e-1"))
outbox.enqueue(_trade(execution_id="e-2"))
stats = writer.run_once()
assert stats.completed == 2 and ledger.count() == 2
assert all(it.status is WorkItemStatus.COMPLETED for it in outbox.all_items())
assert all(it.last_outcome is OutboxOutcome.COMPLETED for it in outbox.all_items())
def test_writer_marks_conflict_blocked_and_does_not_retry():
outbox = InMemoryLedgerOutbox()
ledger = InMemoryLedger(conflict_executions={"e-x"})
writer = LedgerWriter(outbox, ledger)
outbox.enqueue(_trade(execution_id="e-x"))
writer.run_once()
[item] = outbox.all_items()
assert item.status is WorkItemStatus.CONFLICT_BLOCKED
assert item.last_outcome is OutboxOutcome.CONFLICT_BLOCKED
# No pending items remain, so a subsequent run is a no-op.
assert outbox.list_pending() == []
def test_writer_retries_until_success():
outbox = InMemoryLedgerOutbox()
ledger = InMemoryLedger(fail_n_then_succeed=1)
writer = LedgerWriter(outbox, ledger)
outbox.enqueue(_trade(execution_id="e-r"))
s1 = writer.run_once()
assert s1.retryable == 1 and ledger.count() == 0
s2 = writer.run_once()
assert s2.completed == 1 and ledger.count() == 1
+46
View File
@@ -0,0 +1,46 @@
"""Static import sweep guarding the pure-domain boundary (REQ-017 AC-4, NFR-1).
`trex_core.domain` must NOT depend on portfolio-hub, HTTP, ledger adapters,
or any other infrastructure module. The sweep walks the source tree of
trex_core/domain and inspects every `import` statement.
"""
from __future__ import annotations
import ast
from pathlib import Path
_DOMAIN_ROOT = Path(__file__).resolve().parents[2] / "trex_core" / "domain"
_FORBIDDEN_PREFIXES = (
"httpx",
"fastapi",
"uvicorn",
"psycopg",
"redis",
"trex_core.adapters", # any adapter (broker, ledger, persistence, api)
"trex_core.runtime", # daemon / config
"trex_core.application", # ports/services live one layer up
)
def _iter_imports(path: Path):
tree = ast.parse(path.read_text(), filename=str(path))
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
yield alias.name
elif isinstance(node, ast.ImportFrom):
if node.module:
yield node.module
def test_domain_has_no_forbidden_imports():
offenders: list[tuple[str, str]] = []
for py in _DOMAIN_ROOT.rglob("*.py"):
for imp in _iter_imports(py):
if any(imp == p or imp.startswith(p + ".") for p in _FORBIDDEN_PREFIXES):
offenders.append((str(py.relative_to(_DOMAIN_ROOT.parent.parent)), imp))
assert offenders == [], (
"trex_core/domain must remain free of adapter/runtime/application imports; "
f"found: {offenders}"
)