Deliver BAT-002: slot-core daemon + HTTP intent API + PostgreSQL

Implement REQ-005~009 (dcy-002 slice 1) as an onion stack on top of the
BAT-001 pure domain:
- application: IntentService (admission + single-writer transition +
  intent_id idempotency via durable intent_log)
- adapters/persistence: PostgreSQLSlotRepository + PostgreSQLIntentStore +
  trex-core-owned migrations (daily_slot per Appendix A, daily_symbol_entries,
  slot_transition_log, intent_log) + in-memory intent store
- adapters/api: FastAPI /v1 (healthz, intents/buy|sell|{id}, slots, capacity)
- runtime: trex-core-daemon entrypoint with lifespan migrate+preallocate

Verification (against local PostgreSQL 18): 74 passed, ruff clean, domain
import sweep shows zero web/db deps (NFR-1), 20-thread concurrency admits
exactly the idle count (NFR-2), idempotent resubmit causes no double
transition (NFR-3), EXPECTED_SLOT_FIELDS<->information_schema drift test
(realizes dcy-001 OQ-2's deferred DB-side check), and a daemon E2E smoke.

Align all four baseline docs to the new daemon/API/persistence layers.
BAT-002 -> release-candidate; REQ-005~009 -> Implemented. Full async outbox,
broker execution, safety floor, and strategy daemons remain deferred to a
follow-up Discovery.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-04 13:46:08 +09:00
co-authored by Claude Opus 4.8
parent a44f9fddb7
commit a4e263c8e7
41 changed files with 1778 additions and 38 deletions
@@ -0,0 +1,55 @@
"""Migration + column-contract drift test (REQ-006).
Realizes the DB-side drift check dcy-001 OQ-2 deferred to the persistence
iteration: EXPECTED_SLOT_FIELDS (code contract) vs daily_slot actual columns
(information_schema). Skipped without TREX_POSTGRESQL_TEST_DSN.
"""
from __future__ import annotations
import psycopg
from trex_core.adapters.persistence.migrate import apply_migrations, migration_files
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS
_EXPECTED_TABLES = {"daily_slot", "daily_symbol_entries", "slot_transition_log", "intent_log"}
def _columns(dsn: str, table: str) -> set[str]:
with psycopg.connect(dsn) as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT column_name FROM information_schema.columns "
"WHERE table_schema = 'public' AND table_name = %s",
(table,),
)
return {r[0] for r in cur.fetchall()}
def test_migrations_create_expected_tables(pg_clean):
assert migration_files(), "no migration files found"
with psycopg.connect(pg_clean) as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
)
tables = {r[0] for r in cur.fetchall()}
assert _EXPECTED_TABLES <= tables
def test_daily_slot_columns_match_expected_contract(pg_clean):
"""AC: daily_slot columns == EXPECTED_SLOT_FIELDS (dcy-001 Appendix A)."""
actual = _columns(pg_clean, "daily_slot")
assert actual == set(EXPECTED_SLOT_FIELDS)
def test_drift_detected_when_contract_extended(pg_clean):
"""Negative case: a contract field absent from the table is caught."""
actual = _columns(pg_clean, "daily_slot")
drifted_contract = set(EXPECTED_SLOT_FIELDS) | {"phantom_column"}
assert actual != drifted_contract
def test_apply_migrations_is_idempotent(pg_dsn):
# applying twice must not error (CREATE ... IF NOT EXISTS)
apply_migrations(pg_dsn)
apply_migrations(pg_dsn)
@@ -0,0 +1,61 @@
"""PostgreSQL integration tests (REQ-006). Skipped without TREX_POSTGRESQL_TEST_DSN."""
from __future__ import annotations
from datetime import date
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore # noqa: F401
from trex_core.adapters.persistence.postgresql import (
PostgreSQLIntentStore,
PostgreSQLSlotRepository,
)
from trex_core.application.intents import BuyIntent, IntentService, SellIntent
from trex_core.domain.slot import SlotService, SlotState
DAY = date(2026, 6, 3)
def _wire(dsn):
repo = PostgreSQLSlotRepository(dsn)
svc = SlotService(repo)
intents = IntentService(svc, repo, PostgreSQLIntentStore(dsn))
return repo, svc, intents
def test_preallocate_and_capacity(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
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))
assert d.admitted
slot = repo.fetch_slot(d.slot_id)
assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930"
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"))
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
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"))
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))
svc.transit_slot(sid, SlotState.PENDING_SELL, SlotState.CLOSED, reason="filled", actor="t")
assert repo.get_symbol_entry_count(DAY, "005930") == 0
svc.transit_slot(sid, SlotState.CLOSED, SlotState.IDLE, reason="recovery", actor="t")
assert repo.get_symbol_entry_count(DAY, "005930") == 1