feat(rel-007): dcy-025/024 + BAT-035/036 — slots.req 즉시 교체 + safety_sell_enabled

BAT-035 (dcy-025): trex.snapshot.req → trex.slots.req 즉시 교체
- topology: SNAPSHOT_REQ_QUEUE 제거, SLOTS_REQ_QUEUE = "trex.slots.req" 추가
- portfolio.py: View/build_snapshot_envelope/SnapshotEnvelope/_asset_to_dict 제거;
  slot_to_dict 시그니처 확장(snapshot_store/watchdog_store/pause_store/watchdog_timeout_sec)
  — asset_quantity(hub)/cash_balance/cash_estimated/warned/watchdog{}/paused 병합 (REQ-074)
- messaging_handlers.py: make_snapshot_rpc_handler 제거, make_slots_rpc_handler 추가
  — filter(all/inactive/pending/holding/error) + spy(bool) 처리; strategy 필드 제외 (REQ-073/074)
- daemon.py: _snapshot_rpc_loop → _slots_rpc_loop, make_slots_rpc_handler 연결
- cli.py: universe/slots → slots_queue + spy=True + server-side filter; buyable→inactive 매핑 (REQ-063)
- rabbitmq.py: snapshot_queue → slots_queue
- docs: interface-contract.md + runtime-flows.md slots.req 계약으로 업데이트 (REQ-075)
- REQ-028: Implemented → Deprecated (코드 삭제 완료)
- 테스트: test_snapshot_handler/selection/rpc_watchdog/messaging_snapshot_consumer 삭제;
  test_slots_handler.py(신규, 13 cases), test_killswitch_masking_resume 재작성,
  test_ledger_option_d._apply_cash_estimate → slots handler 경유, test_topology/cli_operator 갱신

BAT-036 (dcy-024): per-instance safety_sell_enabled 플래그
- config.py: DaemonInstanceConfig + DaemonConfig 에 safety_sell_enabled: bool = True 추가;
  from_toml 파싱, from_config 전파
- daemon.py: start() workers 조건부 — safety_sell_enabled=false 시 trex-safety-sell 미기동 + WARNING
- tests: test_instance_config.py 에 safety_sell_enabled 파싱 3개 케이스 추가

REQ: 073/074/075/021/034/063/048/005 Approved → Implemented

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 14:45:45 +09:00
co-authored by Claude Opus 4.8
parent fb05a73130
commit f0ad37747b
44 changed files with 1963 additions and 1189 deletions
+3 -3
View File
@@ -40,7 +40,7 @@ from trex_core.adapters.messaging.topology import (
control_queue,
direct_routing_key,
intent_queue,
snapshot_queue,
slots_queue,
)
from trex_core.application.messaging import (
EventHandler,
@@ -138,9 +138,9 @@ class RabbitMQTransport:
ch.exchange_declare(BROADCAST_EXCHANGE, exchange_type="fanout", durable=True)
# Per-instance RPC queues (default → literal names, REQ-049 backward-compat).
iq = intent_queue(instance_key)
sq = snapshot_queue(instance_key)
sq = slots_queue(instance_key)
cq = control_queue(instance_key)
for q, channel_type in ((iq, "intent"), (sq, "snapshot"), (cq, "control")):
for q, channel_type in ((iq, "intent"), (sq, "slots"), (cq, "control")):
ch.queue_declare(q, durable=True, arguments=dlx)
# Directed binding: trex.direct → {instance_key}.{channel_type}
ch.queue_bind(q, DIRECT_EXCHANGE,
+6 -6
View File
@@ -1,19 +1,19 @@
"""Messaging topology constants + builders (REQ-021, dcy-006 OQ-2).
Three patterns:
1. snapshot RPC — direct queue `trex.snapshot.req`, reply via amq.rabbitmq.reply-to
1. slots RPC — direct queue `trex.slots.req`, reply via amq.rabbitmq.reply-to
2. intent RPC — direct queue `trex.intent.req`, reply via amq.rabbitmq.reply-to
3. events — topic exchange `trex.events`, routing key {event_type}.{strategy_id}.{symbol}
Mapping principle: requests carry view/strategy_id in *payload*; events carry
Mapping principle: requests carry filter/spy in *payload*; events carry
strategy_id/symbol in the *routing key* so brokers can filter per-strategy
queues with patterns like `*.{strategy_id}.#`.
"""
from __future__ import annotations
# --- RPC queues (durable for intent; transient-ish for snapshot but kept
# --- RPC queues (durable for intent; transient-ish for slots but kept
# durable for simplicity since the queue itself is shared) ---
SNAPSHOT_REQ_QUEUE = "trex.snapshot.req"
SLOTS_REQ_QUEUE = "trex.slots.req" # REQ-073 / dcy-025: replaces snapshot.req
INTENT_REQ_QUEUE = "trex.intent.req"
PAUSE_REQ_QUEUE = "trex.pause.req" # BAT-008 / REQ-032 (legacy alias of control)
CONTROL_REQ_QUEUE = "trex.control.req" # BAT-012 / REQ-043 operator control channel
@@ -40,8 +40,8 @@ def intent_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
return _instance_queue(INTENT_REQ_QUEUE, instance_key)
def snapshot_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
return _instance_queue(SNAPSHOT_REQ_QUEUE, instance_key)
def slots_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
return _instance_queue(SLOTS_REQ_QUEUE, instance_key)
def control_queue(instance_key: str | None = DEFAULT_INSTANCE_KEY) -> str:
+65 -131
View File
@@ -17,8 +17,6 @@ from trex_core.application.messaging import RpcRequest, RpcResponse
from trex_core.application.portfolio import (
PauseStore,
SnapshotStore,
View,
build_snapshot_envelope,
slot_to_dict,
)
from trex_core.application.watchdog import WatchdogStore
@@ -168,158 +166,98 @@ def _handle_update_thresholds(
return RpcResponse(payload=payload)
# ---- snapshot RPC handler ---------------------------------------------------
# ---- slots RPC handler (REQ-073/074) ----------------------------------------
def make_snapshot_rpc_handler(
def make_slots_rpc_handler(
slot_repo: SlotRepository,
slot_service: SlotService,
*,
instance_key: str = "default",
snapshot_store: Optional[SnapshotStore] = None,
pause_store: Optional[PauseStore] = None,
max_active_slots: Optional[int] = None,
watchdog_store: Optional[WatchdogStore] = None,
watchdog_timeout_sec: int = 120,
killswitch_store=None,
ledger_outbox=None,
):
"""Build the RPC handler for `trex.snapshot.req` (REQ-028).
"""Build the RPC handler for `trex.slots.req` (REQ-073/074).
When `snapshot_store` + `pause_store` are provided (production wiring),
returns the BAT-008 envelope:
{
"assets": list of {symbol, asset_id, cash_balance,
asset_quantity, active_strategy_id,
strategy_params},
"view": "all" | "buyable" | "held",
"available_slots": int,
"degraded": bool,
"reason": DegradedReason value or null,
"message": sanitized text or null
}
Request: {filter?: str, spy?: bool}
filter: "all"|"inactive"|"pending"|"holding"|"error" (default "all")
spy=true: watchdog not updated (operator view, REQ-034 CHG-01)
Without those, falls back to the BAT-007 slot+capacity stub shape so
older callers and existing tests don't break.
Response: {slots: [...], degraded: bool, reason: str|null, message: str|null}
Each slot row carries slot/core state + hub asset data + watchdog + overlay
flags (strategy fields excluded — REQ-074 AC-5).
"""
def handler(req: RpcRequest) -> RpcResponse:
body = req.payload or {}
day = _parse_date(body.get("trading_date")) if body.get("trading_date") else _today()
filter_val = str(body.get("filter") or "all").lower()
spy = bool(body.get("spy", False))
# REQ-034: update watchdog for all holding slots visible in this RPC
# (strategy polling == liveness signal). dcy-016/REQ-021: an operator
# query (`touch_watchdog=false` / `actor="operator"`) must NOT count as
# liveness — otherwise an operator looking at slots would suppress
# safety-sell on a dead strategy (R-4). Default keeps legacy behavior.
touch_watchdog = bool(body.get("touch_watchdog", True))
if str(body.get("actor") or "").lower() == "operator":
touch_watchdog = False
if watchdog_store is not None and touch_watchdog:
# Retrieve and filter slots by status
slots = slot_repo.list_slots(instance_key)
if filter_val == "inactive":
slots = [s for s in slots if s.status.value == "inactive"]
elif filter_val == "pending":
slots = [s for s in slots if s.status.value in ("pending_buy", "pending_sell")]
elif filter_val == "holding":
slots = [s for s in slots if s.status.value == "holding"]
elif filter_val == "error":
slots = [s for s in slots if s.status.value == "error"]
# "all" or unrecognized → no filter (REQ-073 AC-2)
# REQ-034 CHG-01: update watchdog for holding slots unless spy=True
if not spy and watchdog_store is not None:
for s in slot_repo.list_holding_slots(instance_key):
watchdog_store.update(str(s.slot_id))
# --- BAT-008 envelope path (snapshot store wired) ---
if snapshot_store is not None and pause_store is not None:
requested_view = str(body.get("view") or "all").lower()
try:
view = View(requested_view)
except ValueError:
view = View.ALL
strategy_key = _opt_str(body.get("strategy_key"))
# dcy-020 / REQ-059: available = remaining admission headroom under
# the M cap (max(0, M − active)); inactive count when no cap.
cap = slot_service.compute_slot_capacity(max_active_slots)
envelope = build_snapshot_envelope(
store=snapshot_store,
# Build merged slot rows (REQ-074)
slot_rows = [
slot_to_dict(
s,
snapshot_store=snapshot_store,
watchdog_store=watchdog_store,
pause_store=pause_store,
slot_repo=slot_repo,
instance_key=instance_key,
available_slots=cap.available_count,
view=view,
strategy_key=strategy_key,
trading_date=day,
killswitch_store=killswitch_store,
watchdog_timeout_sec=watchdog_timeout_sec,
)
payload = envelope.to_dict()
_attach_watchdog(payload, slot_repo, instance_key, watchdog_store, watchdog_timeout_sec)
_apply_cash_estimate(payload, ledger_outbox)
return RpcResponse(payload=payload)
for s in slots
]
# --- BAT-007 legacy stub-shape path ---
view_raw = str(body.get("view") or "all")
strategy_filter = _opt_str(body.get("strategy_id"))
slots = slot_repo.list_slots(instance_key)
if view_raw == "open":
slots = [s for s in slots if s.status.value in ("pending_buy", "holding", "pending_sell")]
if strategy_filter is not None:
slots = [s for s in slots if s.strategy_id == strategy_filter]
cap = slot_service.compute_slot_capacity(max_active_slots)
payload = {
"view": view_raw,
"strategy_id": strategy_filter,
"slots": [_slot_to_dict(s) for s in slots],
"capacity": {
"inactive_count": cap.inactive_count,
"holding_count": cap.holding_count,
"pending_count": cap.pending_count,
"active_count": cap.active_count,
"available_count": cap.available_count,
"total_slots": cap.total_slots,
"scarcity_ratio": cap.scarcity_ratio,
},
}
_attach_watchdog(payload, slot_repo, instance_key, watchdog_store, watchdog_timeout_sec)
return RpcResponse(payload=payload)
# dcy-018 / REQ-029: cash-estimated overlay (best-effort)
if ledger_outbox is not None:
try:
deltas = ledger_outbox.pending_cash_delta()
if deltas:
for row in slot_rows:
pid = row.get("portfolio_id")
d = deltas.get(pid)
if d:
base = row.get("cash_balance") or 0.0
row["cash_balance"] = base + d
row["cash_estimated"] = True
except Exception: # noqa: BLE001 - estimate must never break the response
pass
# Degraded state from snapshot store
degraded = False
reason = None
message = None
if snapshot_store is not None:
reason_code, msg = snapshot_store.degraded_state()
degraded = reason_code is not None
reason = reason_code.value if reason_code is not None else None
message = msg
return RpcResponse(payload={
"slots": slot_rows,
"degraded": degraded,
"reason": reason,
"message": message,
})
return handler
def _apply_cash_estimate(payload, ledger_outbox) -> None:
"""Overlay an estimated cash for assets with unsettled (pending) ledger
writes (dcy-018 / REQ-029, OQ-4). cash_balance += pending cash delta and the
asset is flagged `cash_estimated=true` until option-D write-back lands.
Best-effort: any failure leaves the response unchanged (NFR-1)."""
if ledger_outbox is None:
return
try:
deltas = ledger_outbox.pending_cash_delta()
if not deltas:
return
for a in payload.get("assets", []):
d = deltas.get(a.get("asset_id"))
if d:
base = a.get("cash_balance") or 0.0
a["cash_balance"] = base + d
a["cash_estimated"] = True
except Exception: # noqa: BLE001 - estimate must never break the response
return
def _attach_watchdog(payload, slot_repo, instance_key, watchdog_store, timeout_sec) -> None:
"""Attach holding-slot watchdog state to a snapshot response (dcy-017/REQ-034).
`watchdog` = list of {slot_id, portfolio_id, symbol, expired, last_seen} for
holding slots — lets `trex-core slots` surface watchdog ownership (strategy
absence). Best-effort: any failure leaves the response unchanged (FR-4)."""
if watchdog_store is None:
return
try:
states = []
for s in slot_repo.list_holding_slots(instance_key):
sid = str(s.slot_id)
last = watchdog_store.last_seen(sid)
states.append({
"slot_id": sid,
"portfolio_id": s.portfolio_id,
"symbol": s.symbol,
"expired": watchdog_store.is_expired(sid, timeout_sec),
"last_seen": last.isoformat() if last else None,
})
payload["watchdog"] = states
except Exception: # noqa: BLE001 - visibility must never break the response
return
# ---- pause/resume RPC handler (REQ-032 AC-4) --------------------------------
def make_pause_rpc_handler(pause_store: PauseStore):
@@ -528,7 +466,3 @@ def _today() -> date_cls:
return datetime.now(tz=timezone.utc).date()
# BAT-022: canonical slot dict shared with build_snapshot_envelope (single
# source of truth so BAT-007 stub and BAT-008 envelope emit an identical
# `slots[]` shape). Local alias kept for existing call sites.
_slot_to_dict = slot_to_dict
+52 -171
View File
@@ -60,15 +60,6 @@ class PortfolioHubReadPort(Protocol):
...
# ---------------------------------------------------------------- selection enums
class View(str, Enum):
"""Snapshot view modes (REQ-029, dcy-005 OQ-11/OQ-12)."""
ALL = "all" # universe ∪ holding slots
BUYABLE = "buyable" # universe − occupied slots
HELD = "held" # holding slots only
class DegradedReason(str, Enum):
"""Machine-readable degraded reason codes (REQ-028 AC-2, dcy-005 OQ-5)."""
PORTFOLIO_HUB_UNAVAILABLE = "portfolio_hub_unavailable"
@@ -255,76 +246,74 @@ class InMemoryKillSwitchStore:
self._excluded.discard((portfolio_id, trading_date))
# ---------------------------------------------------------------- selection
@dataclass(frozen=True)
class SnapshotEnvelope:
"""REQ-028 response envelope (transport-agnostic)."""
assets: list[dict]
view: str
available_slots: int
degraded: bool
reason: Optional[str]
message: Optional[str] = None
# REQ-028 CHG-20260608-01 (BAT-022): operator-facing per-slot state list,
# additive and independent of strategy-facing `assets[]` (universe). Lets
# `trex-core slots` show the daemon's real slot table (idle/holding/pending)
# instead of universe portfolios. Always a list ([] when no slots).
slots: list[dict] = field(default_factory=list)
def slot_to_dict(
s,
*,
snapshot_store: "Optional[SnapshotStore]" = None,
watchdog_store=None,
pause_store: "Optional[PauseStore]" = None,
watchdog_timeout_sec: int = 120,
) -> dict:
"""Merged slot row for trex.slots.req (REQ-073/074).
def to_dict(self) -> dict:
return {
"assets": list(self.assets),
"view": self.view,
"available_slots": self.available_slots,
"degraded": self.degraded,
"reason": self.reason,
"message": self.message,
"slots": list(self.slots),
}
def _asset_to_dict(item: PortfolioItem) -> dict:
"""REQ-027 AC-1 / REQ-028 AC-3 — strict no-price field shape."""
return {
"symbol": item.symbol,
"asset_id": item.portfolio_id,
"cash_balance": item.cash_balance,
"asset_quantity": item.asset_quantity,
"active_strategy_id": item.active_strategy_id,
"strategy_params": dict(item.strategy_params or {}),
}
def slot_to_dict(s) -> dict:
"""Canonical operator-facing per-slot state shape (REQ-028 AC-4, BAT-022).
Shared by the BAT-008 envelope (`build_snapshot_envelope`) and the BAT-007
stub-shape handler so `trex-core slots` sees an identical slot dict on either
path. Unlike `_asset_to_dict` (strategy universe, no-price), this is
operator-only and exposes prices. External read field name `entry_price`
is kept for contract compatibility (dcy-012 / NFR-3 — internally backed by
the renamed `limit_price`, the pending order limit cleared once holding).
Carries slot/core state + hub asset data + watchdog + overlay flags.
Field-level authority: slot/core = slot_id/status/qty/avg_price/entry_price/
forced_close_at; hub = asset_quantity/cash_balance; watchdog = watchdog{};
overlay = paused/warned. strategy fields excluded (REQ-074 AC-5).
"""
return {
d = {
"slot_id": str(s.slot_id),
"status": s.status.value,
"symbol": s.symbol,
"qty": float(s.qty) if s.qty is not None else None,
"avg_price": float(s.avg_price) if s.avg_price is not None else None,
"entry_price": float(s.limit_price) if s.limit_price is not None else None,
"strategy_id": s.strategy_id,
"sell_strategy_id": s.sell_strategy_id,
"portfolio_id": s.portfolio_id,
"forced_close_at": s.forced_close_at.isoformat() if s.forced_close_at else None,
# dcy-020 / REQ-063 (BAT-029): asset-centric single view — universe-detached
# (soft-retired/tombstone) marker so the operator sees retired assets.
"universe_detached": getattr(s, "retired_at", None) is not None,
}
# hub authority: asset_quantity (hub primary) + cash_balance
asset_qty: Optional[float] = None
cash_balance: Optional[float] = None
if snapshot_store is not None and s.portfolio_id:
snap = snapshot_store.get(s.portfolio_id)
if snap is not None:
asset_qty = float(snap.item.asset_quantity)
cash_balance = float(snap.item.cash_balance)
# qty (slot mirror) vs asset_quantity (hub): divergence → warned=True
slot_qty = float(s.qty) if s.qty is not None else None
warned = bool(slot_qty != asset_qty) if (slot_qty is not None and asset_qty is not None) else False
d["asset_quantity"] = asset_qty
d["cash_balance"] = cash_balance
d["cash_estimated"] = False # handler overlays ledger_outbox estimate if available
d["warned"] = warned
# watchdog nested object (WatchdogStore)
if watchdog_store is not None:
sid = str(s.slot_id)
last = watchdog_store.last_seen(sid)
d["watchdog"] = {
"expired": watchdog_store.is_expired(sid, watchdog_timeout_sec),
"last_seen": last.isoformat() if last else None,
}
else:
d["watchdog"] = None
# paused overlay (PauseStore) — orthogonal to status
paused = False
if pause_store is not None and s.portfolio_id:
paused = s.portfolio_id in pause_store.list_paused()
d["paused"] = paused
return d
def _sanitize(message: str) -> str:
"""Strip credentials/URLs/tokens — REQ-028 AC-2 sanitization."""
"""Strip credentials/URLs/tokens from degraded messages."""
if not message:
return ""
out = message
@@ -333,114 +322,6 @@ def _sanitize(message: str) -> str:
if idx >= 0:
out = out[:idx] + "<redacted>"
break
# Truncate to keep response payload bounded.
return out[:200]
def build_snapshot_envelope(
*,
store: SnapshotStore,
pause_store: PauseStore,
slot_repo,
instance_key: str = "default",
available_slots: int,
view: View = View.ALL,
strategy_key: Optional[str] = None,
trading_date=None,
killswitch_store: "Optional[KillSwitchStore]" = None,
) -> SnapshotEnvelope:
"""Compose base(view) − mask(pending·warning·pause·killswitch) − strategy_key.
`slot_repo` is the SlotRepository used to read holding/pending state —
instance-scoped (dcy-020/REQ-056). `trading_date` (default UTC today) is
used only for the day-scoped kill-switch exclusion mask.
`killswitch_store` (REQ-046) adds the day-scoped kill-switch exclusion to
the mask: a `GLOBAL_KILLSWITCH` ("*") entry masks the whole universe.
"""
# Read core authoritative slot state once.
day = trading_date or datetime.now(tz=timezone.utc).date()
slots = slot_repo.list_slots(instance_key)
holding_pids: set[str] = set()
pending_pids: set[str] = set()
holding_qty: dict[str, float] = {}
for s in slots:
if s.portfolio_id is None:
continue
if s.status.value == "holding":
holding_pids.add(s.portfolio_id)
holding_qty[s.portfolio_id] = float(s.qty or 0.0)
elif s.status.value in ("pending_buy", "pending_sell"):
pending_pids.add(s.portfolio_id)
assets = store.list_assets()
paused = pause_store.list_paused()
warned = {a.item.portfolio_id for a in assets if a.warning}
# REQ-046: day-scoped kill-switch exclusion. '*' sentinel = whole universe.
excluded = killswitch_store.list_excluded(day) if killswitch_store is not None else set()
killswitch_global = GLOBAL_KILLSWITCH in excluded
# Build the base set (view).
by_pid = {a.item.portfolio_id: a for a in assets}
base_pids: list[str]
if view is View.HELD:
base_pids = sorted(holding_pids)
elif view is View.BUYABLE:
if available_slots <= 0:
base_pids = []
else:
# universe − occupied slots (pending OR holding occupy)
occupied = holding_pids | pending_pids
base_pids = [a.item.portfolio_id for a in assets
if a.item.portfolio_id not in occupied]
else: # ALL
base_pids = list({a.item.portfolio_id for a in assets} | holding_pids)
base_pids.sort()
# Apply masks: pending OR warning OR pause OR kill-switch day-exclusion.
masked = pending_pids | warned | paused | excluded
# Filter and shape.
out: list[dict] = []
for pid in base_pids:
if killswitch_global or pid in masked:
continue
if pid in by_pid:
item = by_pid[pid].item
elif pid in holding_pids:
# holding slot for an asset NOT in the current universe (e.g. fetch
# gap) — synthesize a minimal item using slot data so the response
# still carries authoritative position info (REQ-029 slot overlay
# priority over portfolio-hub presence).
slot = next((s for s in slots if s.portfolio_id == pid), None)
item = PortfolioItem(
portfolio_id=pid,
symbol=(slot.symbol if slot else "") or "",
cash_balance=0.0,
asset_quantity=float((slot.qty if slot else 0.0) or 0.0),
active_strategy_id=(slot.strategy_id if slot else None),
strategy_params={},
)
else:
continue
if strategy_key is not None and item.active_strategy_id != strategy_key:
continue
d = _asset_to_dict(item)
# holding slot quantity wins for held assets (slot-overlay priority).
if pid in holding_qty:
d["asset_quantity"] = holding_qty[pid]
out.append(d)
# REQ-028 CHG-20260608-01 (BAT-022): operator-facing per-slot state — the
# full daemon slot table for `day`, independent of the asset view filter.
slot_states = [slot_to_dict(s) for s in slots]
reason_code, message = store.degraded_state()
return SnapshotEnvelope(
assets=out,
view=view.value,
available_slots=max(0, int(available_slots)),
degraded=reason_code is not None,
reason=reason_code.value if reason_code is not None else None,
message=message,
slots=slot_states,
)
+39 -50
View File
@@ -102,101 +102,90 @@ def _today_iso(cfg: DaemonConfig) -> str:
def _cmd_universe(args, cfg, rpc) -> int:
# Universe = admittable slots (filter=inactive, spy=true — operator view)
instance = _resolve_instance(args)
payload = {"view": "all", "actor": "operator", "touch_watchdog": False,
"trading_date": _today_iso(cfg)}
resp = rpc(topology.snapshot_queue(instance), payload,
payload = {"filter": "inactive", "spy": True}
resp = rpc(topology.slots_queue(instance), payload,
correlation_id=str(uuid.uuid4()), timeout=args.timeout)
assets = resp.get("assets", [])
slots = resp.get("slots", [])
if args.json:
# Scope to universe data — `slots[]`/`watchdog[]` belong to `slots`.
_emit({"instance": instance, "view": resp.get("view"), "assets": assets,
"available_slots": resp.get("available_slots"),
_emit({"instance": instance, "filter": "inactive", "slots": slots,
"degraded": resp.get("degraded"), "reason": resp.get("reason")},
as_json=True)
else:
print(f"instance={instance} view=all assets={len(assets)} "
print(f"instance={instance} filter=inactive count={len(slots)} "
f"degraded={resp.get('degraded')}")
for a in assets:
print(f" {a.get('asset_id')}\t{a.get('symbol')}\tqty={a.get('asset_quantity')}")
for s in slots:
print(f" {s.get('portfolio_id')}\t{s.get('symbol')}\t"
f"qty(hub)={s.get('asset_quantity')}\tqty(slot)={s.get('qty')}")
return 0
def _cmd_slots(args, cfg, rpc) -> int:
instance = _resolve_instance(args)
payload = {"view": "all", "actor": "operator", "touch_watchdog": False,
"trading_date": _today_iso(cfg)}
resp = rpc(topology.snapshot_queue(instance), payload,
correlation_id=str(uuid.uuid4()), timeout=args.timeout)
# REQ-032 CHG-20260608-03 (BAT-022): show the real daemon slot table
# (envelope `slots[]`), not the universe `assets[]`. `--status` filters by
# slot status; `universe` keeps showing `assets[]`.
slots = resp.get("slots", [])
status = (args.status or "all").lower()
if status in ("holding", "held"):
slots = [s for s in slots if s.get("status") == "holding"]
elif status == "buyable":
# dcy-020 (BAT-030): admittable = inactive, not universe-detached.
slots = [s for s in slots
if s.get("status") == "inactive" and not s.get("universe_detached")]
elif status == "pending":
slots = [s for s in slots if s.get("status") in ("pending_buy", "pending_sell")]
# --symbol / --slot-id: client-side filters (composable with --status).
# REQ-063 CHG-20260611-01: map --status to filter; buyable → inactive
filter_val = "inactive" if status == "buyable" else status
payload = {"filter": filter_val, "spy": True}
resp = rpc(topology.slots_queue(instance), payload,
correlation_id=str(uuid.uuid4()), timeout=args.timeout)
slots = resp.get("slots", [])
# --symbol / --slot-id: client-side filters (composable with server-side filter)
if args.symbol:
slots = [s for s in slots if s.get("symbol") == args.symbol]
if args.slot_id:
slots = [s for s in slots if s.get("slot_id") == args.slot_id]
# dcy-017: holding-slot watchdog state (strategy-absent ownership).
owned = {w.get("portfolio_id") for w in resp.get("watchdog", []) if w.get("expired")}
wd_by_slot = {w.get("slot_id"): w for w in resp.get("watchdog", [])}
# watchdog-expired ownership: slots with watchdog.expired=True
owned = {s.get("portfolio_id") for s in slots
if (s.get("watchdog") or {}).get("expired")}
if args.json:
_emit({"instance": instance, "status": status, "slots": slots}, as_json=True)
_emit({"instance": instance, "filter": filter_val, "slots": slots}, as_json=True)
else:
header = f"instance={instance} status={status}"
header = f"instance={instance} filter={filter_val}"
if args.symbol:
header += f" symbol={args.symbol}"
if args.slot_id:
header += f" slot_id={args.slot_id}"
print(f"{header} count={len(slots)} "
f"watchdog_owned={len([s for s in slots if s.get('portfolio_id') in owned])}")
# When filtered (--symbol / --slot-id), print a detailed per-slot block;
# otherwise the terse one-line table.
f"watchdog_expired={len([s for s in slots if s.get('portfolio_id') in owned])}")
if args.symbol or args.slot_id:
for s in slots:
_print_slot_detail(s, wd_by_slot.get(s.get("slot_id")))
_print_slot_detail(s)
else:
for s in slots:
pid = s.get("portfolio_id")
tag = " [WATCHDOG-OWNED: strategy absent]" if pid in owned else ""
wd = s.get("watchdog") or {}
tag = " [WATCHDOG: strategy absent]" if wd.get("expired") else ""
print(f" {s.get('slot_id')}\t{s.get('status')}\t{s.get('symbol')}\t"
f"qty={s.get('qty')}\tlimit={s.get('entry_price')}\t"
f"avg={s.get('avg_price')}\tpid={s.get('portfolio_id')}{tag}")
f"qty={s.get('qty')}\taqty={s.get('asset_quantity')}\t"
f"limit={s.get('entry_price')}\tavg={s.get('avg_price')}\t"
f"pid={pid}\tpaused={s.get('paused')}\twarned={s.get('warned')}{tag}")
return 0
def _print_slot_detail(s: dict, wd: dict | None) -> None:
def _print_slot_detail(s: dict) -> None:
"""Verbose per-slot block used when `slots` is filtered (e.g. --symbol)."""
wd = s.get("watchdog") or {}
fields = [
("status", s.get("status")),
("symbol", s.get("symbol")),
("portfolio_id", s.get("portfolio_id")),
("qty", s.get("qty")),
("qty (slot mirror)", s.get("qty")),
("asset_quantity (hub)", s.get("asset_quantity")),
("warned", s.get("warned")),
("avg_price", s.get("avg_price")),
("entry_price (limit)", s.get("entry_price")),
("strategy_id", s.get("strategy_id")),
("sell_strategy_id", s.get("sell_strategy_id")),
("cash_balance", s.get("cash_balance")),
("cash_estimated", s.get("cash_estimated")),
("paused", s.get("paused")),
("forced_close_at", s.get("forced_close_at")),
("universe_detached", s.get("universe_detached")),
("watchdog.expired", wd.get("expired")),
("watchdog.last_seen", wd.get("last_seen")),
]
if wd is not None:
fields.append((
"watchdog",
f"expired={wd.get('expired')} last_seen={wd.get('last_seen')}",
))
print(f"slot {s.get('slot_id')}")
for label, value in fields:
print(f" {label:<20}: {value}")
return 0
print(f" {label:<24}: {value}")
def _intent_guard(args, cfg, summary: dict, action: str, confirm) -> int | None:
+8
View File
@@ -148,6 +148,9 @@ class DaemonInstanceConfig:
# dcy-022 / REQ-068: default order timeout in seconds (opt-in per instance).
# None = OFF = indefinite wait (legacy behavior). Per-intent timeout_sec overrides.
default_order_timeout_sec: Optional[int] = None
# dcy-024 / REQ-048 CHG-20260611-01: disable autonomous safety-sell worker.
# Absent = True (safe default). False → trex-safety-sell thread not started.
safety_sell_enabled: bool = True
@staticmethod
def from_toml(path: str) -> "DaemonInstanceConfig":
@@ -178,6 +181,7 @@ class DaemonInstanceConfig:
trading_timezone=str(data.get("trading_timezone") or "Asia/Seoul"),
max_active_slots=max_active,
default_order_timeout_sec=default_timeout,
safety_sell_enabled=bool(data.get("safety_sell_enabled", True)),
)
@@ -216,6 +220,9 @@ class DaemonConfig:
# dcy-014 / REQ-048: operator-local timezone (CLI date display etc.). The
# daily preallocation trigger was removed with the pool model (BAT-030).
trading_timezone: str = "Asia/Seoul"
# dcy-024 / REQ-005 CHG-20260611-01: disable autonomous safety-sell worker.
# False → trex-safety-sell thread not started in daemon.start().
safety_sell_enabled: bool = True
# 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
@@ -335,4 +342,5 @@ class DaemonConfig:
portfolio_hub=pf,
trading_timezone=inst.trading_timezone,
max_active_slots=inst.max_active_slots,
safety_sell_enabled=inst.safety_sell_enabled,
)
+14 -10
View File
@@ -53,7 +53,7 @@ from trex_core.application.killswitch import KillSwitchService
from trex_core.application.messaging_handlers import (
make_control_rpc_handler,
make_intent_rpc_handler,
make_snapshot_rpc_handler,
make_slots_rpc_handler,
slot_transition_event_payload,
)
from trex_core.application.portfolio import (
@@ -259,26 +259,24 @@ class Daemon:
transport.consume_rpc_forever(
topology.intent_queue(self.config.instance_key), handler, self.stop)
def _snapshot_rpc_loop(self) -> None:
def _slots_rpc_loop(self) -> None:
transport = _build_transport(self.config)
self._transports.append(transport)
try:
transport.declare_topology(self.config.instance_key)
except Exception: # noqa: BLE001
pass
handler = make_snapshot_rpc_handler(
self.slot_repo, self.slot_service,
handler = make_slots_rpc_handler(
self.slot_repo,
instance_key=self.config.instance_key,
snapshot_store=self.snapshot_store,
pause_store=self.pause_store,
max_active_slots=self.config.max_active_slots,
watchdog_store=self.watchdog,
watchdog_timeout_sec=self.config.safety_sell.watchdog_timeout_sec,
killswitch_store=self.killswitch_store,
ledger_outbox=self.outbox, # dcy-018 / REQ-029: estimated-cash overlay
ledger_outbox=self.outbox,
)
transport.consume_rpc_forever(
topology.snapshot_queue(self.config.instance_key), handler, self.stop)
topology.slots_queue(self.config.instance_key), handler, self.stop)
def _control_rpc_loop(self) -> None:
# REQ-043: operator control channel (pause/resume/list + kill_switch/resume_kill_switch).
@@ -329,11 +327,17 @@ class Daemon:
("trex-execution", self._execution_loop),
("trex-ledger", self._ledger_loop),
("trex-intent-rpc", self._intent_rpc_loop),
("trex-snapshot-rpc", self._snapshot_rpc_loop),
("trex-slots-rpc", self._slots_rpc_loop),
("trex-control-rpc", self._control_rpc_loop),
("trex-snapshot-scheduler", self._snapshot_scheduler_loop),
("trex-safety-sell", self._safety_sell_loop),
]
if self.config.safety_sell_enabled:
workers.append(("trex-safety-sell", self._safety_sell_loop))
else:
_log.warning(
"safety_sell_enabled=false — trex-safety-sell worker not started. "
"Autonomous safety-sell will NOT run."
)
for name, target in workers:
t = threading.Thread(target=target, name=name, daemon=True)
t.start()