Files
trex-core/docs/batches/bat-011_20260607_safety-sell-triggers/implementation.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

3.5 KiB

BAT-011 Implementation

  • Status: completed
  • Batch: BAT-011

Plan Summary

  • 구현 순서: REQ-038 KIS 현재가 어댑터 → SafetySellService(REQ-037/038/039) + trex-safety-sell worker 배선 → REQ-040 update_thresholds RPC → REQ-042 baseline 문서 정합. 각 단계 unit test green 확인 후 진행.

Changed Files

신규 파일

  • trex_core/application/safety_sell.py — SafetySellService: forced_close(REQ-037) / stop-loss·take-profit(REQ-038) / 장 마감 청산(REQ-039) + run_once 캐던스. watchdog read-only, sell() 통해 매도, tick 예외 격리.
  • tests/adapters/broker/test_kis_price.py — KIS 현재가 어댑터 7종(tr_id 분기, stck_prpr 파싱, 에러→None, paper stub).
  • tests/application/test_safety_sell_worker.py — 트리거 서비스 23종(forced_close 6 / 가격 10 / 장 마감 6 / run_once 2).
  • tests/application/test_update_thresholds_handler.py — update_thresholds RPC 8종(부분 갱신/null 해제/non-holding/last_seen 리셋/미존재).

기존 파일 수정

  • trex_core/application/broker_port.py — BrokerPort.get_current_price(symbol) -> Optional[Decimal] 포트 추가 (REQ-038).
  • trex_core/adapters/broker/kis.py — _tr_price()(TTTC8397R/VTTC8397R) + get_current_price()(inquire-price, stck_prpr 파싱, 에러→None) (REQ-038).
  • trex_core/adapters/broker/paper.py — set_current_price()/get_current_price() 프로그래머블 stub (REQ-038).
  • trex_core/domain/slot/service.py — SlotService.update_thresholds() + _UNSET sentinel(유지 vs 해제 구분) (REQ-040).
  • trex_core/application/messaging_handlers.py — make_intent_rpc_handler 에 slot_service/watchdog_store 인자 + update_thresholds action 라우팅(_handle_update_thresholds) + last_seen_at 리셋 (REQ-040).
  • trex_core/runtime/daemon.py — WatchdogStore 인스턴스 + SafetySellService 빌드 + trex-safety-sell worker(7번째 스레드) + snapshot/intent RPC handler 에 watchdog 배선 + docstring 갱신 (REQ-034/037/038/039/040).
  • docs/interface-contract.md, docs/runtime-flows.md, docs/project-structure.md — baseline 정합 (REQ-042).
  • tests/domain/slot/test_price_threshold_fields.py — 중복 import 정리(ruff clean).

Execution Log

  1. broker_port + kis + paper 현재가 구현 → test_kis_price.py 7 passed.
  2. safety_sell.py 구현 → test_safety_sell_worker.py 23 passed.
  3. daemon 배선(watchdog + worker + handler) → import 회귀(test_daemon_no_http_surface) green.
  4. update_thresholds 구현 → test_update_thresholds_handler.py 8 passed.
  5. baseline 3문서 정합(REQ-042).
  6. 전체 회귀: 단위 236 passed / 23 skipped, ruff check All passed.
  7. 인프라 게이트 포함(PG+RabbitMQ): 259 passed (마이그레이션 001~004 적용 + PG repo round-trip 포함).

Validation

  • 단위: pytest -q --deselect ...::test_sandbox_token_and_submit → 236 passed / 23 skipped.
  • 인프라 게이트: TREX_RABBITMQ_TEST_URL=... TREX_POSTGRESQL_TEST_DSN=...(trex_core_test) pytest -q → 259 passed.
  • lint: ruff check trex_core tests → All checks passed.

Remaining Risks

  • R-1 (운영 실증 이월): stop-loss·take-profit·forced_close·장 마감의 실 KIS 라이브 트리거는 휴장/운영 환경 제약으로 단위(mock)·정적 검증까지 수행. 차기 거래일 운영 관찰 필요(verification Carried-Over).
  • R-5 해소: BAT-010 R-2(실 PostgreSQL 004_add_price_thresholds.sql round-trip)는 인프라 게이트 259 passed 로 적용 확인(마이그레이션 idempotency + PG repo 테스트 green).