Files
trex-core/docs/srs/Interface/req-034_per-slot-watchdog.md
T
genichinandClaude Opus 4.8 a30efa8f3b feat(safety-sell): dcy-007 core 자율 안전 매도 — BAT-010 기반 + BAT-011 트리거
dcy-007(core 자율 안전 매도) 전체 delivery. BAT-010 기반 인프라가 미커밋
상태였고 BAT-011 트리거 레이어와 공유 파일에서 섞여 있어 한 커밋으로 묶음.

BAT-010 (REQ-034/035/036/041, release-candidate):
- WatchdogStore(application/watchdog.py) + snapshot RPC 워치독 갱신
- Slot stop_loss_price/take_profit_price 필드 + 004_add_price_thresholds.sql
- SlotService.sell() 자율 매도 내부 경로 + idempotency
- SafetySellConfig env 5종 파싱

BAT-011 (REQ-037/038/039/040/042, release-candidate):
- SafetySellService(application/safety_sell.py): forced_close 만기 / stop-loss·
  take-profit KIS polling / 장 마감 강제 청산 + trex-safety-sell 7번째 스레드
- BrokerPort.get_current_price + KIS inquire-price(TTTC8397R/VTTC8397R, stck_prpr)
  + paper stub
- SlotService.update_thresholds() + intent RPC update_thresholds 액션
  (부분 갱신/null 해제, 워치독 last_seen_at 리셋)
- baseline 3문서 정합(interface-contract/runtime-flows/project-structure)

거버넌스: REQ-034~042 Implemented 동기화, BAT-010/011 release-candidate,
dcy-007 discovery 포함.

검증: 신규 38 테스트 green, 단위 236 passed/23 skipped,
인프라 게이트(PG+RabbitMQ) 259 passed(마이그레이션 001~004 round-trip 포함),
ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 08:32:30 +09:00

5.1 KiB
Raw Blame History

req-034: per-slot 워치독 — snapshot RPC 기반 전략 모듈 활성 감지

  • Status: Implemented
  • Type: Interface
  • Priority: High
  • Owner: project maintainers

Intent

전략 모듈이 슬롯을 활성 관리 중인지 데몬이 감지하기 위해, snapshot RPC 응답 시 해당 슬롯들의 last_seen_at 타임스탬프를 in-memory 워치독 store에 기록한다. TREX_WATCHDOG_TIMEOUT 이상 갱신되지 않은 슬롯은 "전략 모듈 미감시" 상태로 판정해 자율 안전 매도 체크 대상이 된다(dcy-007 FR-5).

Requirement

  • 시스템은 trex.snapshot.req 요청을 처리할 때, 응답 envelope에 포함되는 holding 슬롯들의 last_seen_at을 in-memory 워치독 store(dict[slot_id, datetime])에 현재 시각(UTC 또는 KST 일관 적용)으로 갱신해야 한다.
  • 시스템은 now() − last_seen_at > TREX_WATCHDOG_TIMEOUT(초) 조건이 성립하는 슬롯을 "워치독 만료(watchdog-expired)"로 판정해야 한다.
  • 데몬 기동 후 TREX_WATCHDOG_GRACE_PERIOD(초) 동안은 워치독 만료 판정을 유예해야 한다(grace period). grace period 중에는 어떤 슬롯도 만료로 판정하지 않는다.
  • 워치독 store는 Postgres에 영속하지 않으며 데몬 재시작 시 초기화된다.

Acceptance Criteria

  1. Given trex.snapshot.req handler가 응답을 산출할 때, When 응답 슬롯 목록에 slot_id = S가 포함되면, Then watchdog_store[S]가 현재 시각으로 갱신된다.
  2. Given TREX_WATCHDOG_TIMEOUT=120이고 slot S의 watchdog_store[S]가 121초 전일 때, When 워치독 만료 여부를 평가하면, Then S는 만료로 판정된다.
  3. Given TREX_WATCHDOG_TIMEOUT=120이고 slot S의 watchdog_store[S]가 60초 전일 때, When 워치독 만료 여부를 평가하면, Then S는 만료로 판정되지 않는다.
  4. Given 데몬이 기동된 지 TREX_WATCHDOG_GRACE_PERIOD=60 초 이내일 때, When 워치독 만료 평가를 수행하면, Then 모든 슬롯의 만료 판정이 유예된다(store 비어 있는 슬롯 포함).
  5. Given 데몬 재시작이 발생하면, When 기동 직후 워치독 store를 조회하면, Then store는 비어 있다.
  6. Given 전략 모듈이 TREX_WATCHDOG_TIMEOUT / 2 미만 주기로 snapshot RPC를 보낼 때, When 정상 동작 중이면, Then 해당 슬롯의 워치독이 만료되지 않는다(NFR-4 관계식 검증).

Impacted Area

  • Docs:
    • docs/runtime-flows.md — 워치독 갱신 흐름 추가 (REQ-042 참조)
    • docs/interface-contract.md — snapshot RPC가 watchdog 갱신 신호임을 명시 (REQ-042 참조)
  • Modules:
    • trex_core/application/messaging_handlers.py — make_snapshot_rpc_handler 확장: 응답 슬롯의 last_seen_at 갱신
    • trex_core/application/portfolio.py 또는 신규 trex_core/application/watchdog.py — WatchdogStore (dict[slot_id, datetime]), grace period 시작 타임스탬프 관리
  • Tests:
    • make_snapshot_rpc_handler 단위 테스트 — last_seen_at 갱신 확인
    • 워치독 만료 판정 단위 테스트 — 만료/비만료/grace period 시나리오

Notes

  • 워치독 store 갱신은 snapshot RPC 응답 슬롯 기준이다. intent RPC(매수/매도)는 갱신 신호가 아니다(update_thresholds 액션은 제외 — REQ-040 참조).
  • NFR-4 권장 관계식: snapshot RPC 호출 주기 < TREX_WATCHDOG_TIMEOUT / 2. 기본값 기준 전략 모듈은 60초 이내 주기로 snapshot RPC를 호출해야 한다. 관계식 위반 시 정상 동작 슬롯에서도 워치독 오탐 발생.
  • 워치독 store 구현 위치(portfolio.py 확장 vs 신규 watchdog.py)는 batch planning에서 결정한다.
  • TREX_WATCHDOG_TIMEOUT, TREX_WATCHDOG_GRACE_PERIOD env var 파싱은 REQ-041 참조.

Change Log

CHG-20260607-01

  • Date: 2026-06-07
  • Author: Justin Ko

Change Summary

  • dcy-007 confirmed → REQ-034 초안 생성

Intent

  • per-slot 워치독 메커니즘(FR-5)을 독립 구현·검증 단위로 정규화

Acceptance Criteria Delta

  • Added: AC-1~6 전체

Impacted Area Delta

  • Docs: runtime-flows.md, interface-contract.md (REQ-042에서 처리)
  • Modules: messaging_handlers.py, watchdog store 신규/확장
  • Tests: snapshot handler + 워치독 판정 단위 테스트

Delivery Trace Delta

  • Batches: none (미할당)
  • Releases: none

Revalidation Impact

  • Reverification Needed: no
  • Existing Implementation Invalidated: no
  • Status Recommendation: keep-current
  • Follow-up Action: suggest-batch-reqs 후 batch 배정

CHG-20260607-02

  • Date: 2026-06-07
  • Author: Justin Ko

Change Summary

  • Proposed → Approved (confirm-req)

Intent

  • 승인 게이트 통과: 필수 필드·AC·Impacted Area·Change Log 완비 확인. dcy-007(confirmed) 범위 및 결정 사항과 모순 없음.

Acceptance Criteria Delta

  • (변경 없음)

Impacted Area Delta

  • (변경 없음)

Delivery Trace Delta

  • Batches: none (미할당)
  • Releases: none

Revalidation Impact

  • Reverification Needed: no
  • Existing Implementation Invalidated: no
  • Status Recommendation: keep-current
  • Follow-up Action: suggest-batch-reqs 후 batch 배정