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:
@@ -0,0 +1,114 @@
|
||||
"""FastAPI app factory for the slot-core daemon (REQ-005/007/008).
|
||||
|
||||
`create_app` wires the HTTP /v1 surface to injected services so the same app
|
||||
runs against the in-memory adapters (tests) or PostgreSQL adapters (daemon).
|
||||
All slot writes go through IntentService → SlotService (single writer).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date as date_cls
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
|
||||
from trex_core.adapters.api.schemas import (
|
||||
BuyIntentRequest,
|
||||
CapacityResponse,
|
||||
HealthResponse,
|
||||
IntentResponse,
|
||||
SellIntentRequest,
|
||||
SlotSnapshot,
|
||||
)
|
||||
from trex_core.application.intents import BuyIntent, IntentService, SellIntent
|
||||
from trex_core.domain.slot import SlotRepository, SlotService
|
||||
|
||||
|
||||
def create_app(
|
||||
*,
|
||||
slot_service: SlotService,
|
||||
slot_repo: SlotRepository,
|
||||
intent_service: IntentService,
|
||||
default_total_slots: int,
|
||||
) -> FastAPI:
|
||||
app = FastAPI(title="trex-core slot daemon", version="0.1.0")
|
||||
|
||||
@app.get("/v1/healthz", response_model=HealthResponse)
|
||||
def healthz() -> HealthResponse:
|
||||
return HealthResponse()
|
||||
|
||||
@app.post("/v1/intents/buy", response_model=IntentResponse)
|
||||
def submit_buy(req: BuyIntentRequest) -> IntentResponse:
|
||||
d = intent_service.submit_buy(
|
||||
BuyIntent(
|
||||
intent_id=req.intent_id,
|
||||
trading_date=req.trading_date,
|
||||
symbol=req.symbol,
|
||||
qty=req.qty,
|
||||
allocated_capital=req.allocated_capital,
|
||||
entry_price=req.entry_price,
|
||||
strategy_id=req.strategy_id,
|
||||
sell_strategy_id=req.sell_strategy_id,
|
||||
portfolio_id=req.portfolio_id,
|
||||
forced_close_at=req.forced_close_at,
|
||||
)
|
||||
)
|
||||
return IntentResponse(intent_id=d.intent_id, status=d.status, slot_id=d.slot_id, reason=d.reason)
|
||||
|
||||
@app.post("/v1/intents/sell", response_model=IntentResponse)
|
||||
def submit_sell(req: SellIntentRequest) -> IntentResponse:
|
||||
d = intent_service.submit_sell(
|
||||
SellIntent(
|
||||
intent_id=req.intent_id,
|
||||
slot_id=req.slot_id,
|
||||
reason=req.reason,
|
||||
sell_strategy_id=req.sell_strategy_id,
|
||||
)
|
||||
)
|
||||
return IntentResponse(intent_id=d.intent_id, status=d.status, slot_id=d.slot_id, reason=d.reason)
|
||||
|
||||
@app.get("/v1/intents/{intent_id}", response_model=IntentResponse)
|
||||
def get_intent(intent_id: str) -> IntentResponse:
|
||||
d = intent_service.lookup(intent_id)
|
||||
if d is None:
|
||||
raise HTTPException(status_code=404, detail="intent not found")
|
||||
return IntentResponse(intent_id=d.intent_id, status=d.status, slot_id=d.slot_id, reason=d.reason)
|
||||
|
||||
@app.get("/v1/slots", response_model=list[SlotSnapshot])
|
||||
def list_slots(
|
||||
status: Optional[str] = Query(default=None),
|
||||
trading_date: Optional[date_cls] = Query(default=None),
|
||||
) -> list[SlotSnapshot]:
|
||||
day = trading_date or _today()
|
||||
slots = slot_repo.list_slots(day)
|
||||
if status is not None:
|
||||
slots = [s for s in slots if s.status.value == status]
|
||||
return [
|
||||
SlotSnapshot(
|
||||
slot_id=s.slot_id, trading_date=s.trading_date, status=s.status.value,
|
||||
symbol=s.symbol, qty=s.qty, avg_price=s.avg_price, entry_price=s.entry_price,
|
||||
allocated_capital=s.allocated_capital, strategy_id=s.strategy_id,
|
||||
sell_strategy_id=s.sell_strategy_id, forced_close_at=s.forced_close_at,
|
||||
)
|
||||
for s in slots
|
||||
]
|
||||
|
||||
@app.get("/v1/capacity", response_model=CapacityResponse)
|
||||
def capacity(
|
||||
trading_date: Optional[date_cls] = Query(default=None),
|
||||
total_slots: Optional[int] = Query(default=None),
|
||||
) -> CapacityResponse:
|
||||
day = trading_date or _today()
|
||||
cap = slot_service.compute_slot_capacity(day, total_slots or default_total_slots)
|
||||
return CapacityResponse(
|
||||
idle_count=cap.idle_count, holding_count=cap.holding_count,
|
||||
pending_count=cap.pending_count, total_slots=cap.total_slots,
|
||||
scarcity_ratio=cap.scarcity_ratio,
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def _today() -> date_cls:
|
||||
from datetime import datetime, timezone
|
||||
|
||||
return datetime.now(tz=timezone.utc).date()
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Pydantic request/response schemas for the /v1 HTTP API (REQ-007/008)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class BuyIntentRequest(BaseModel):
|
||||
intent_id: str
|
||||
trading_date: date
|
||||
symbol: str
|
||||
qty: Optional[float] = None
|
||||
allocated_capital: Optional[float] = None
|
||||
entry_price: Optional[float] = None
|
||||
strategy_id: Optional[str] = None
|
||||
sell_strategy_id: Optional[str] = None
|
||||
portfolio_id: Optional[str] = None
|
||||
forced_close_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class SellIntentRequest(BaseModel):
|
||||
intent_id: str
|
||||
slot_id: UUID
|
||||
reason: str = "discretionary_sell"
|
||||
sell_strategy_id: Optional[str] = None
|
||||
|
||||
|
||||
class IntentResponse(BaseModel):
|
||||
intent_id: str
|
||||
status: str
|
||||
slot_id: Optional[UUID] = None
|
||||
reason: Optional[str] = None
|
||||
|
||||
|
||||
class SlotSnapshot(BaseModel):
|
||||
slot_id: UUID
|
||||
trading_date: date
|
||||
status: str
|
||||
symbol: Optional[str] = None
|
||||
qty: Optional[float] = None
|
||||
avg_price: Optional[float] = None
|
||||
entry_price: Optional[float] = None
|
||||
allocated_capital: Optional[float] = None
|
||||
strategy_id: Optional[str] = None
|
||||
sell_strategy_id: Optional[str] = None
|
||||
forced_close_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class CapacityResponse(BaseModel):
|
||||
idle_count: int
|
||||
holding_count: int
|
||||
pending_count: int
|
||||
total_slots: int
|
||||
scarcity_ratio: float
|
||||
|
||||
|
||||
class HealthResponse(BaseModel):
|
||||
status: str = "ok"
|
||||
Reference in New Issue
Block a user