Files
trex-core/docs/batches/bat-011_20260607_safety-sell-triggers/implementation.md
T
genichinandClaude Sonnet 4.6 0082d3b5da fix(kis): REQ-038 현재가 tr_id 정정 + REL-003 확정 + dcy-008 kill-switch Discovery
## REQ-038 CHG-04/05: KIS 시세 tr_id 정정 (FHKST01010100)
- kis.py _tr_price(): TTTC8397R/VTTC8397R → FHKST01010100 (실전/모의 공용)
  배포 전 실 KIS LIVE 진단에서 구 tr_id가 rt_cd=1 "없는 서비스 코드"로 거부됨을
  확인 — stop-loss/take-profit이 LIVE에서 조용히 미발동하는 결함이었음.
  FHKST01010100으로 정정 후 get_current_price('005930')=329000(rt_cd=0) 확인.
- test_kis_price.py: AC-7 테스트 정정 (FHKST01010100 공용 검증)
- REQ-038: CHG-04/05 Change Log 추가, Requirement/AC-7/Notes 정정
- BAT-011 implementation.md: LIVE 검증 및 tr_id 정정 이력 추가
- BAT-011 verification.md: AC-7 evidence를 CHG-04 정정 버전으로 갱신
- docs/srs/index.md: CHG-04/05 Change Log 항목 추가

## REL-003: core 자율 안전 매도 release 문서 생성
- docs/releases/rel-003_20260607_core-autonomous-safety-sell.md 신규 생성
  (BAT-010 + BAT-011, app-service profile, Status: confirmed)
- docs/releases/index.md: REL-003 행 추가

## dcy-008: 비상 정지/강제 청산(kill-switch) Discovery 초안
- docs/discovery/dcy-008_20260607_emergency-stop-kill-switch.md 신규 생성
  GitHub #3 기반; SlotService.sell() 재사용, 당일 전략 제외 day-scoped
  FR 6개, NFR 3개, OQ 4개 (트리거 표면/영속 방식/수동 해제 결정 필요)
- docs/discovery/index.md: dcy-008 행 추가

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 10:17:36 +09:00

3.9 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 포함).
  8. 배포 전 실 KIS LIVE 시세 검증(읽기 전용) → REQ-038 시세 tr_id 결함 발견(TTTC8397R = "없는 서비스 코드", rt_cd=1). change-req REQ-038 CHG-04/05 로 FHKST01010100(실전/모의 공용) 정정 → kis.py _tr_price 수정 + test_kis_price AC-7 정정 → LIVE 재검증 get_current_price('005930')=329000(rt_cd=0). 단위 236 passed 재확인.

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).