Files
trex-core/trex_core/runtime/config.py
T
genichinandClaude Opus 4.8 587e987c5c feat(config): BAT-028 M1 — max_active_slots per-instance TOML (dcy-020/REQ-060)
자산 중심 모드 게이트 + M 값. DaemonInstanceConfig/DaemonConfig 에
max_active_slots: Optional[int] 추가, from_toml 파싱(>0 검증), from_config 전파.
미설정(None) → legacy pool 모드. additive — 전체 375 passed, ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 11:06:56 +09:00

335 lines
14 KiB
Python

"""Daemon configuration resolution (REQ-005, REQ-010, REQ-012/013)."""
from __future__ import annotations
import os
from dataclasses import dataclass, replace
from typing import Optional
DEFAULT_CONFIG_DIR = "config"
def resolve_config_toml(name: str, config_path: Optional[str] = None) -> str:
"""Resolve `--config <name>` within a config directory (REQ-048 CHG-20260609-01).
config_dir = `--config-path` if given, else `./config`. `<name>` selects the
TOML inside it; a missing `.toml` extension is appended.
Backward-compat: if `name` contains a path separator (e.g. `config/test.toml`),
it is treated as an explicit path and used as-is so existing ops scripts that
pass a path keep working.
resolve_config_toml("test") -> config/test.toml
resolve_config_toml("test.toml") -> config/test.toml
resolve_config_toml("prod", "/etc/trex") -> /etc/trex/prod.toml
resolve_config_toml("config/test.toml") -> config/test.toml (path-form)
"""
if os.sep in name or (os.altsep and os.altsep in name):
return name # explicit path (backward-compat)
config_dir = config_path or DEFAULT_CONFIG_DIR
filename = name if name.endswith(".toml") else f"{name}.toml"
return os.path.join(config_dir, filename)
def _load_dotenv(config_path: Optional[str] = None, env_file: Optional[str] = None) -> None:
"""Best-effort load of a .env file into os.environ (REQ-010 / CHG-20260609-01).
Resolution (trex-orb-v1 aligned, deterministic & CWD-independent given a
config_path): `env_file` (explicit) wins, else `<config_dir>/.env` next to the
TOML. With neither (standalone `from_env()`), fall back to legacy CWD-relative
`find_dotenv` so existing root `.env` setups keep working (backward-compat).
Real OS env vars always take precedence (`override=False`). python-dotenv
missing or the resolved file absent => no-op (no regression).
"""
try:
from dotenv import load_dotenv
except ImportError: # pragma: no cover - optional at runtime
return
if env_file:
if os.path.isfile(env_file):
load_dotenv(env_file, override=False)
return
if config_path:
path = os.path.join(os.path.dirname(os.path.abspath(config_path)), ".env")
if os.path.isfile(path):
load_dotenv(path, override=False)
return
# No config context: legacy CWD-relative discovery (backward-compat fallback).
from dotenv import find_dotenv
load_dotenv(find_dotenv(usecwd=True), override=False)
@dataclass(frozen=True)
class KISConfig:
base_url: str
app_key: str
app_secret: str
account_ref: str
timeout: float
allow_live: bool
redis_url: str = "" # REQ-016: optional token cache backend
@dataclass(frozen=True)
class PortfolioHubConfig:
"""portfolio-hub ledger adapter config (REQ-017)."""
base_url: str
token: str = ""
timeout: float = 5.0
@dataclass(frozen=True)
class PaperBrokerConfig:
"""Paper broker tuning for e2e test scenarios (REQ-011).
Defaults reproduce immediate, full fills with no rejects, so an unset
environment is byte-for-byte the prior `PaperBroker()` behaviour.
"""
fill_after_polls: int = 0
qty_per_poll: Optional[int] = None
reject_symbols: frozenset[str] = frozenset()
@dataclass(frozen=True)
class RabbitMQConfig:
"""RabbitMQ transport adapter config (REQ-022, dcy-006 OQ-6)."""
url: str
timeout: float = 5.0
@dataclass(frozen=True)
class SnapshotScheduleConfig:
"""Daily snapshot refresh schedule (REQ-030)."""
kst_time: str = "07:30"
tick_sec: float = 60.0
@dataclass(frozen=True)
class SafetySellConfig:
"""Safety-sell feature tuning (REQ-041 / BAT-010).
All five env vars are parsed here even though only WATCHDOG_TIMEOUT and
WATCHDOG_GRACE_PERIOD are consumed in BAT-010; the remaining three are
used by BAT-011 trigger loops once they land.
"""
watchdog_timeout_sec: int = 120
watchdog_grace_period_sec: int = 60
forced_close_check_interval_sec: int = 60
stop_loss_poll_interval_sec: int = 30
market_close_liquidation_time: Optional[str] = None # "HH:MM" KST, optional
# BAT-014 / REQ-053: persistent-rejection retry cooldown (dcy-010).
min_retry_interval_sec: int = 600 # per-slot cooldown between sell retries
max_retry_warn_count: int = 3 # WARNING after this many consecutive retries
@dataclass(frozen=True)
class DaemonInstanceConfig:
"""Per-instance TOML settings (REQ-048 / BAT-013).
`instance_key` and `portfolio_hub_token` live in a TOML file (not .env)
so they are not committed and so multiple instances can run distinct
queues. Loaded via `--config <path>.toml`.
"""
instance_key: str
portfolio_hub_token: str = ""
# dcy-014 / REQ-048: trading_date timezone + daily preallocation trigger.
# Optional — absent keys fall back to KST / midnight defaults.
trading_timezone: str = "Asia/Seoul"
daily_preallocation_time: str = "00:00"
# dcy-020 / REQ-060 (BAT-028): max concurrent active (holding) slots, M.
# Presence is the asset-centric mode gate: set -> universe reconcile + FCFS/M
# admission; absent -> legacy pool model (preallocation + find_allocatable).
max_active_slots: Optional[int] = None
@staticmethod
def from_toml(path: str) -> "DaemonInstanceConfig":
import tomllib
with open(path, "rb") as fh:
data = tomllib.load(fh)
instance_key = data.get("instance_key")
if not instance_key:
raise RuntimeError(
f"instance TOML missing required 'instance_key': {path}"
)
raw_max = data.get("max_active_slots")
max_active = int(raw_max) if raw_max is not None else None
if max_active is not None and max_active <= 0:
raise RuntimeError(
f"instance TOML 'max_active_slots' must be > 0, got {raw_max}: {path}"
)
return DaemonInstanceConfig(
instance_key=str(instance_key),
portfolio_hub_token=str(data.get("portfolio_hub_token") or ""),
trading_timezone=str(data.get("trading_timezone") or "Asia/Seoul"),
daily_preallocation_time=str(data.get("daily_preallocation_time") or "00:00"),
max_active_slots=max_active,
)
@dataclass(frozen=True)
class DaemonConfig:
dsn: str
total_slots: int
broker: str # 'paper' | 'kis'
execution_interval_sec: float
# BAT-013 / REQ-049: instance identity for multi-instance queue naming.
# "default" keeps legacy literal queue names (backward-compat).
instance_key: str = "default"
# dcy-020 / REQ-060 (BAT-028): M (max concurrent active slots) + asset-centric
# mode gate. None -> legacy pool model.
max_active_slots: Optional[int] = None
kis: Optional[KISConfig] = None
# REQ-011: paper broker tuning (only consulted when broker == 'paper').
paper: PaperBrokerConfig = PaperBrokerConfig()
# REQ-017/019: ledger writer config (None => in-memory stub ledger,
# appropriate for slice-1 paper-broker integration tests).
portfolio_hub: Optional[PortfolioHubConfig] = None
ledger_interval_sec: float = 1.0
# REQ-022 (BAT-007): RabbitMQ transport config. Required by the new
# sync+thread daemon — HTTP API has been removed (REQ-025).
rabbitmq: Optional[RabbitMQConfig] = None
# REQ-030 (BAT-008): daily snapshot refresh.
snapshot_schedule: SnapshotScheduleConfig = SnapshotScheduleConfig()
# REQ-041 (BAT-010): safety-sell feature config.
safety_sell: SafetySellConfig = SafetySellConfig()
# dcy-013 / REQ-014 (BAT-017): Alert-only pending-order stale watchdog
# threshold in seconds. 0 disables (default). When >0, dispatched-pending
# slots older than this are surfaced (WARN + counter) without state change.
pending_stale_threshold_sec: int = 0
# dcy-014 / REQ-005 (BAT-018): trading_date timezone (default KST) + daily
# preallocation trigger time (HH:MM, TZ-local). Operative surface is the
# per-instance TOML (REQ-048); these are the env-only defaults.
trading_timezone: str = "Asia/Seoul"
daily_preallocation_time: str = "00:00"
# dcy-015 / REQ-019 (BAT-019): ledger outbox retry backoff. base*2^(n-1)
# capped at cap (+jitter); after max_attempts a retryable row dead-letters.
ledger_retry_base_sec: float = 1.0
ledger_retry_cap_sec: float = 300.0
ledger_max_attempts: int = 8
@staticmethod
def from_env() -> "DaemonConfig":
_load_dotenv()
dsn = os.environ.get("TREX_POSTGRESQL_DSN")
if not dsn:
raise RuntimeError(
"TREX_POSTGRESQL_DSN is required to start the slot-core daemon."
)
broker = os.environ.get("TREX_BROKER", "paper").strip().lower()
kis: Optional[KISConfig] = None
if broker == "kis":
kis = KISConfig(
base_url=os.environ["TREX_KIS_BROKER_BASE_URL"],
app_key=os.environ["TREX_KIS_BROKER_APP_KEY"],
app_secret=os.environ["TREX_KIS_BROKER_APP_SECRET"],
account_ref=os.environ["TREX_KIS_BROKER_ACCOUNT_REF"],
timeout=float(os.environ.get("TREX_KIS_BROKER_TIMEOUT", "5")),
allow_live=os.environ.get("TREX_KIS_ALLOW_LIVE", "").lower() in ("1", "true", "yes"),
redis_url=os.environ.get("TREX_KIS_REDIS_URL", ""),
)
qpp_raw = os.environ.get("TREX_PAPER_QTY_PER_POLL", "").strip()
reject_raw = os.environ.get("TREX_PAPER_REJECT_SYMBOLS", "")
paper = PaperBrokerConfig(
fill_after_polls=int(os.environ.get("TREX_PAPER_FILL_AFTER_POLLS", "0")),
qty_per_poll=int(qpp_raw) if qpp_raw else None,
reject_symbols=frozenset(
s.strip() for s in reject_raw.split(",") if s.strip()
),
)
portfolio_hub: Optional[PortfolioHubConfig] = None
pf_base = os.environ.get("TREX_PORTFOLIO_HUB_BASE_URL", "").strip()
if pf_base:
portfolio_hub = PortfolioHubConfig(
base_url=pf_base,
token=os.environ.get("TREX_PORTFOLIO_HUB_TOKEN", "").strip(),
timeout=float(os.environ.get("TREX_PORTFOLIO_HUB_TIMEOUT", "5")),
)
rabbitmq: Optional[RabbitMQConfig] = None
mq_url = os.environ.get("TREX_RABBITMQ_URL", "").strip()
if mq_url:
rabbitmq = RabbitMQConfig(
url=mq_url,
timeout=float(os.environ.get("TREX_RABBITMQ_TIMEOUT", "5")),
)
snapshot_schedule = SnapshotScheduleConfig(
kst_time=os.environ.get("TREX_SNAPSHOT_SCHEDULE_KST", "07:30").strip() or "07:30",
tick_sec=float(os.environ.get("TREX_SNAPSHOT_TICK_SEC", "60")),
)
_mc_time = os.environ.get("TREX_MARKET_CLOSE_LIQUIDATION_TIME", "").strip() or None
safety_sell = SafetySellConfig(
watchdog_timeout_sec=int(os.environ.get("TREX_WATCHDOG_TIMEOUT", "120")),
watchdog_grace_period_sec=int(os.environ.get("TREX_WATCHDOG_GRACE_PERIOD", "60")),
forced_close_check_interval_sec=int(
os.environ.get("TREX_FORCED_CLOSE_CHECK_INTERVAL", "60")
),
stop_loss_poll_interval_sec=int(
os.environ.get("TREX_STOP_LOSS_POLL_INTERVAL", "30")
),
market_close_liquidation_time=_mc_time,
min_retry_interval_sec=int(
os.environ.get("TREX_SAFETY_SELL_MIN_RETRY_INTERVAL", "600")
),
max_retry_warn_count=int(
os.environ.get("TREX_SAFETY_SELL_MAX_RETRY_WARN_COUNT", "3")
),
)
return DaemonConfig(
dsn=dsn,
total_slots=int(os.environ.get("TREX_TOTAL_SLOTS", "10")),
broker=broker,
execution_interval_sec=float(os.environ.get("TREX_EXECUTION_INTERVAL_SEC", "1.0")),
kis=kis,
paper=paper,
portfolio_hub=portfolio_hub,
ledger_interval_sec=float(os.environ.get("TREX_LEDGER_INTERVAL_SEC", "1.0")),
rabbitmq=rabbitmq,
snapshot_schedule=snapshot_schedule,
safety_sell=safety_sell,
pending_stale_threshold_sec=int(
os.environ.get("TREX_PENDING_STALE_THRESHOLD", "0")
),
ledger_retry_base_sec=float(
os.environ.get("TREX_LEDGER_RETRY_BASE_SEC", "1.0")
),
ledger_retry_cap_sec=float(
os.environ.get("TREX_LEDGER_RETRY_CAP_SEC", "300.0")
),
ledger_max_attempts=int(os.environ.get("TREX_LEDGER_MAX_ATTEMPTS", "8")),
)
@staticmethod
def from_config(toml_path: str, env_file: Optional[str] = None) -> "DaemonConfig":
"""Build config from env + a per-instance TOML file (REQ-048 / BAT-013).
`instance_key` and `portfolio_hub_token` come from TOML (not .env).
TOML `portfolio_hub_token` overrides the (env-derived) PortfolioHubConfig
token when a portfolio-hub base_url is configured.
REQ-010 CHG-20260609-01: the config-dir `.env` (next to the TOML, or
`--env-file`) is loaded first so it wins over the legacy root `.env`
fallback loaded inside `from_env()` (both `override=False`).
"""
_load_dotenv(toml_path, env_file)
base = DaemonConfig.from_env()
inst = DaemonInstanceConfig.from_toml(toml_path)
pf = base.portfolio_hub
if inst.portfolio_hub_token and pf is not None:
pf = replace(pf, token=inst.portfolio_hub_token)
# dcy-014 / REQ-048: trading_date TZ + daily trigger come from TOML.
return replace(
base,
instance_key=inst.instance_key,
portfolio_hub=pf,
trading_timezone=inst.trading_timezone,
daily_preallocation_time=inst.daily_preallocation_time,
max_active_slots=inst.max_active_slots,
)