Files
trex-core/docs/batches/bat-021_20260608_watchdog-ownership-visibility/implementation.md
T
genichinandClaude Opus 4.8 6d7bca1460 feat(safety-sell): dcy-017 워치독 소유권 가시화 — snapshot 상태 + 발동 로깅 (BAT-021)
전략 이탈로 슬롯이 내부 청산 루틴(워치독) 소유로 넘어간 상태가 무성이라
운영자가 인지할 수 없던 문제(issue #12)를 가시화한다.

거버넌스: 전부 change-req (신규 REQ 없음, dcy-017 OQ-3; Redis 미채택).
- REQ-034/021: WatchdogStore.last_seen + snapshot RPC 응답에 holding 슬롯
  워치독 상태(expired/last_seen) read-only 동봉(best-effort)
- REQ-032: trex-core slots 가 expired 슬롯을 WATCHDOG-OWNED(전략 이탈) 표시
- REQ-037/038/039: safety-sell 워치독 소유 진입(WARN)/이탈(INFO) + 발동
  (forced_close·stop_loss=WARN, take_profit·market_close=INFO) 로그 +
  in-process 카운터(SafetySellService.counters), 구조화 필드(slot_id/
  portfolio_id/reason/phase/instance_key). per-tick 스킵은 카운터만
- REQ-042: runtime-flows/interface-contract 정합

best-effort 격리(NFR-1): 모든 가시화는 예외 격리 — 워치독 판정·청산 불변.
dcy-016 억제 읽기로 관측이 워치독을 리셋하지 않음(비침습).

검증: 단위 335 passed/34 skipped, 인프라 게이트(PG+RabbitMQ) 369 passed, ruff clean.
신규 test_safety_sell_visibility(6) + snapshot watchdog(1) + CLI(1). BAT-021 rc.

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

2.8 KiB

BAT-021 Implementation

  • Status: completed
  • Batch: BAT-021

구현 요약

  • dcy-017 워치독 소유권 가시화(snapshot 워치독 상태 + safety-sell 발동 로깅)를 전부 change-req 로 구현(신규 REQ 없음, Redis 미채택).

변경 내역

REQ-034/021 — 워치독 상태 노출

  • trex_core/application/watchdog.py: last_seen(slot_id) -> Optional[datetime] read-only getter.
  • trex_core/application/messaging_handlers.py: make_snapshot_rpc_handler(..., watchdog_timeout_sec=120) + _attach_watchdog(payload, ...) — holding 슬롯 {slot_id, portfolio_id, symbol, expired, last_seen} 를 payload["watchdog"] 에 best-effort 동봉(envelope·stub 두 경로). try/except 격리.
  • trex_core/runtime/daemon.py: handler 에 watchdog_timeout_sec=config.safety_sell.watchdog_timeout_sec 배선.

REQ-037/038/039 — safety-sell 발동·소유 가시화

  • trex_core/application/safety_sell.py:
    • SafetySellService(instance_key="default") + self._owned: set + self.counters: dict + _count.
    • _log_fire(slot, reason): WARN(forced_close_at/stop_loss)·INFO(take_profit/market_close_liquidation) + fire:{reason} 카운터, 구조화 필드.
    • _update_ownership(now): grace 밖에서 holding∧expired 집합 diff → 진입 WARN(owned:enter)/이탈 INFO(owned:exit). 예외 격리.
    • check_forced_close/check_prices/check_market_close 발동 지점에서 _log_fire; run_once 가 매 tick _update_ownership.
  • trex_core/runtime/daemon.py: SafetySellService 에 instance_key=config.instance_key.

REQ-032 — CLI 표시

  • trex_core/runtime/cli.py::_cmd_slots: snapshot 응답 watchdog 에서 expired 슬롯 → [WATCHDOG-OWNED: strategy absent] 태그 + watchdog_owned=N 요약.

REQ-042 — baseline 문서

  • docs/runtime-flows.md: 워치독 갱신 flow 에 상태 노출(5) + safety-sell 가시화 flow 신규. Last Updated → BAT-021.
  • docs/interface-contract.md: snapshot 응답 watchdog[] 필드. Last Updated → BAT-021.

테스트

  • 신규 tests/application/test_safety_sell_visibility.py: 6건 — 소유 진입 WARN+카운터(instance_key 포함), 이탈 INFO+카운터, 안정 시 churn 없음, forced_close WARN, take_profit INFO, grace 억제.
  • tests/application/test_messaging_snapshot_consumer.py 확장: 워치독 만료 holding → watchdog 필드 expired/last_seen 노출(operator 비침습).
  • tests/runtime/test_cli_operator.py 확장: slots 가 expired 슬롯 WATCHDOG-OWNED 표시.

검증 실행

  • 단위: TREX_BROKER=paper .venv/bin/python -m pytest -q → 335 passed / 34 skipped(::test_sandbox_token_and_submit deselect — LIVE .env 환경 의존, 본 batch 무관).
  • 인프라 게이트: TREX_POSTGRESQL_TEST_DSN=...(trex_core_test) TREX_RABBITMQ_TEST_URL=... pytest -q → 369 passed.
  • ruff check trex_core tests → All checks passed.