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>
This commit is contained in:
2026-06-08 14:11:49 +09:00
co-authored by Claude Opus 4.8
parent cc28e1fba5
commit 4746f74aae
24 changed files with 1063 additions and 6 deletions
@@ -148,3 +148,42 @@
#### Validation Plan
- BAT-004 verification evidence 재참조로 충분.
### CHG-20260608-01
- Date: 2026-06-08
- Author: Justin Ko
#### Change Summary
- (change-req, dcy-013/issue #9, OQ-2 Alert-only) pending 주문 stale 워치독 추가: dispatched-pending 슬롯이 `TREX_PENDING_STALE_THRESHOLD`(초, 0=비활성) 초과로 머물면 WARN 로그 + `ExecutionResult.stale_pending` 카운터로 가시화한다. **슬롯/주문 상태는 변경하지 않는다**(auto-remediation 후속). 취소 감지 누락(R-1) 등 미지 원인 무한 갇힘이 silent 로 남지 않게 하는 reconciliation 안전망. BAT-017 재구현·재검증 완료 → Implemented 유지.
#### Intent
- 자동 취소 감지(REQ-012)가 놓치는 케이스까지 포함해, 비정상적으로 오래 pending 인 주문을 운영자에게 노출하는 durability 안전망 제공.
#### Acceptance Criteria Delta
- Added:
- pending stale 임계 초과 시 WARN+카운터 가시화(상태 미변경, Alert-only). 임계 env `TREX_PENDING_STALE_THRESHOLD`(0=비활성).
#### Impacted Area Delta
- Modules: `trex_core/application/execution.py`(`_check_stale_pending`, ExecutionService threshold), `trex_core/runtime/config.py`(`pending_stale_threshold_sec`/env), `trex_core/runtime/daemon.py`(배선)
- Tests: tests/application/test_execution_cancel.py(stale WARN + 비활성)
#### Delivery Trace Delta
- Batches: BAT-017
- Releases: none
#### Revalidation Impact
- Reverification Needed: yes (BAT-017 에서 완료)
- Existing Implementation Invalidated: no (관측만 추가, 기존 reconcile/poll 동작 불변)
- Status Recommendation: Implemented (change-req → BAT-017 재검증)
- Follow-up Action: confirm-req-implemented(BAT-017).
#### Validation Plan
- BAT-017: 임계 초과 pending 주입 → WARN+카운터, 상태 미변경 / 비활성(default) → 무경고 단위.