Files
trex-core/docs/batches/bat-017_20260608_external-cancel-detection-stale-watchdog/verification.md
T
genichinandClaude Opus 4.8 4746f74aae fix(broker): dcy-013 KIS 외부 취소 감지 + 부분 체결 회계 + stale 워치독 (BAT-017)
KIS 계좌에서 직접 매도 주문을 취소하면 poll_fill 이 cncl_yn 을 파싱하지 않아
슬롯이 pending_sell 에 영구히 갇히고 죽은 주문을 무한 폴링하던 버그(issue #9)
정정.

거버넌스: 전부 change-req (신규 REQ 없음, dcy-013 OQ-3).
- REQ-012: KIS poll_fill cncl_yn 파싱 → 외부 취소 감지. FillStatus.cancelled
  /is_partial_cancel 추가. 체결 0 취소→cancelled, 부분 체결 후 취소→partial+cancelled
- REQ-013: 집행 _on_partial_cancel — 체결분 원장 1회 + qty 차감(매도 잔여/매수 체결분)
  + holding 복귀 (OQ-1 A안). 전량 취소는 기존 롤백 경로 재사용
- REQ-018: 부분 체결분 원장 1회 기록 (_record_ledger 재사용)
- REQ-031: 잔여 포지션 정합 연계 (slot.qty ↔ 실 잔고, 과매도 방지)
- REQ-014: pending stale 워치독 Alert-only (TREX_PENDING_STALE_THRESHOLD,
  WARN+카운터, 상태 미변경) — 취소 감지 누락 backstop
- REQ-015: interface-contract/runtime-flows 정합

전량 취소는 holding 자동 복구(limit_price clear→재매도 시장가, dcy-012 연계).
KIS cncl_yn 실 규약은 운영 검증(미지 값 보수 처리 + 워치독 backstop).

검증: 단위 294 passed/32 skipped, 인프라 게이트(PG+RabbitMQ) 326 passed, ruff clean.
신규 test_kis_cancel(httpx stub) + test_execution_cancel(programmable broker).
BAT-017 release-candidate.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 14:11:49 +09:00

4.0 KiB

BAT-017 Verification

  • Status: verified
  • Batch: BAT-017

Acceptance Mapping

REQ-012 (KIS 취소 감지, change-req)

AC Evidence
cncl_yn → cancelled (체결 0) tests/adapters/broker/test_kis_cancel.py::test_full_external_cancel_maps_to_cancelled
비취소 zero-fill → accepted ::test_zero_fill_not_cancelled_stays_accepted
부분 체결 + 취소 → is_partial_cancel + execution_id ::test_partial_fill_then_cancel_flags_partial_cancel
전량 체결 회귀 ::test_full_fill_unaffected_by_cancel_flag

REQ-013 (집행 부분 체결 회계, change-req)

AC Evidence
전량 외부 취소 → holding 복귀 + clear (영구 갇힘 0) tests/application/test_execution_cancel.py::test_full_external_cancel_returns_slot_to_holding
부분 취소 → qty 차감 + holding 잔여 + cost basis 유지 ::test_partial_fill_then_cancel_records_and_keeps_remainder
기존 filled/reject 회귀 tests/application/test_execution.py 전체

REQ-018 (부분 체결분 원장 1회, change-req)

AC Evidence
부분 취소 시 sell 원장 1건(qty=체결분, price=체결가) test_execution_cancel.py::test_partial_fill_then_cancel_records_and_keeps_remainder

REQ-031 (포지션 정합 연계, change-req)

AC Evidence
부분 취소 후 slot.qty = 잔여 held (과매도 0) test_execution_cancel.py::test_partial_fill_then_cancel_records_and_keeps_remainder (qty==6)
reconcile 로직 불변 회귀 tests/application/test_reconciliation.py 전체

REQ-014 (pending stale 워치독 Alert-only, change-req)

AC Evidence
임계 초과 pending → WARN + 카운터, 상태 미변경 test_execution_cancel.py::test_stale_pending_watchdog_warns_without_state_change
비활성(default) → 무경고 ::test_stale_watchdog_disabled_by_default

REQ-015 (baseline 문서, change-req)

AC Evidence
interface-contract/runtime-flows 정합 문서 diff(BAT-017)

Evidence

  • Test:
    • 단위 전체 회귀: 294 passed / 32 skipped(신규 KIS 취소 4 + 집행 취소/워치독 4 포함; ::test_sandbox_token_and_submit deselect).
    • 인프라 게이트(PG trex_core_test + RabbitMQ): 326 passed.
    • ruff check trex_core tests → All checks passed.
  • Logs: 294 passed, 32 skipped, 1 deselected / 326 passed, 1 deselected ~15.9s.

성공 기준(dcy-013) 대응

  • S-1(외부 취소 → cancelled 감지 → holding 복귀, 영구 갇힘 0): test_full_external_cancel_maps_to_cancelled + test_full_external_cancel_returns_slot_to_holding.
  • S-2(죽은 주문 추가 폴링 없음): 취소 감지 후 clear_dispatch 로 broker_order_id 비워져 list_dispatched_pending 에서 제외(폴링 중단) — test_full_external_cancel_returns_slot_to_holding(broker_order_id is None).
  • S-3(부분 체결분 원장 1회 + 잔여 정합, 과매도/이중기록 0): test_partial_fill_then_cancel_records_and_keeps_remainder(sell 1건 + qty 6 + cost basis 1000).
  • S-4(임계 초과 pending stale 가시화, 상태 미변경): test_stale_pending_watchdog_warns_without_state_change.

Carried-Over / 비차단

  • R-1(KIS cncl_yn 실제 필드/값 규약): cncl_yn="Y" 가정으로 구현, 실 응답 확인은 운영/검증 단계. 미지 값 보수 처리 + stale 워치독(FR-3) backstop 으로 무한 갇힘 가시화 안전망 확보.
  • 실 KIS 외부 취소 full-stack e2e: 실계좌 세션 필요로 이월(운영 검증, 비차단).
  • R-2(외부 취소→재매도 반복 루프): dcy-010 per-slot 쿨다운과 연계, cancelled 쿨다운 트리거 포함은 후속.
  • 워치독 auto-remediation: 후속 분리(OQ-2 Alert-only 결정).

Result

  • Ready for Release: true
  • Blocking Issues:
    • 없음. 전량 취소 자동 복구·부분 체결 회계·stale 워치독 모두 단위 green(326 passed 회귀). LIVE sandbox 토큰 deselect 는 .env 환경 의존(본 batch 무관). KIS cncl_yn 실 규약은 운영 검증 비차단(보수 처리 + 워치독 backstop).