fix: 하드닝 루프 9회차 — 매수 원복·잔고 연속조회·결과 불명 매도·브레이커·경보 보강

- 8회차 HIGH 수정 16건 레드팀 재검증: 12 확인·3 부분·1 미수정 → 전부 보강
- trade_executor/fill_sweep: 늦게 확정된 0체결 BUY → CANCELLED + ActiveTrade close, 부분체결 → 수량 축소(멱등, 재기동 후 TH 재구성)
- korea_investment_api: get_balance_all_v2(tr_cont 연속조회, _partial), get_todays_orders_v2(실패 None), 브레이커 청산용 조회 면제(OPEN 중 미집계)
- swing/exit_service·mdd_manager: 매도 직전 상한·MDD 축소가 전체 잔고 페이지 사용(§8.5-37), 불명 대기 중 부분익절 skip·쿨다운·pre_hldg 차감, 대기 TTL 15:30+재매수 폐기(대조 1회 선행)(§8.5-38 해소)
- swing/scheduler: 청산 TIMED_OUT/SKIPPED/in-flight 시 MDD 축소·진입 보류(예외는 진입만), 3사이클 연속 시 error+텔레그램
- daytrading: 결과 불명 매도 재판정 v3(주문조회 v2 None→보류, 등록 이후 일치 행→accepted, 원장≤0→보류), 재기동 복원 min(주문가능,보유), 잔고 조회 후 일일손실한도 재확인, 단독 실행 Redis 접속 제거
- main: 스트림 무수신 경보 v4(희망 구독 기준), 첫 하트비트 없음 stale(HB_FIRST_BEAT_GRACE_SEC)
- 운영: Jenkinsfile health_watch.py 디렉터리 잔존 복구, prod_smoke 사이드카 확인, config 키 12개 선언(동작 무변경)
- 진입가 v3(max(AT,PA))는 KIS 이동평균 평단 전제 오류로 레드팀 반증 → 폐기
- CLAUDE.md §3.2 ③-d, §8.5-37/38 갱신, §8.5-39~48 신규(Crisis 손절 소급 §8.5-39 는 사용자 결정 대기)
- 유닛 스위트 2781 passed / 0 failed (소켓 차단), 네트워크 시도 0건

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 15:37:16 +09:00
parent 0feaa61bfc
commit d140055e64
60 changed files with 2853 additions and 50 deletions

View File

@@ -106,6 +106,7 @@ def _loop_env(monkeypatch, *, exit_mock=None, balance=None, can_open=True):
monkeypatch.setattr(fs, "sweep_unconfirmed_fills_v2", AsyncMock(return_value=0))
api = MagicMock()
api.get_balance = balance or AsyncMock(return_value={"output2": [{"tot_evlu_amt": "10000000"}]})
api.get_balance_all_v2 = api.get_balance # [R9] 연속조회 목 보정 — 같은 목을 공유
monkeypatch.setattr(kia, "korea_investment_api", api)
monkeypatch.setattr(sch.mdd_manager, "update", AsyncMock(return_value={"action": "NONE"}))
# [R2] MDD 입력 가드가 실제 peak/단계를 읽는다 — 앞선 테스트의 싱글톤 상태가 새지 않게 고정
@@ -156,7 +157,8 @@ async def test_hung_exit_check_does_not_freeze_loop_and_is_not_cancelled(monkeyp
assert started.is_set()
api.get_balance.assert_awaited() # MDD 단계 도달
assert calls["entry"] == 1 # 진입 단계 도달
# [R9 A-2] 청산 판정이 상한 초과(백그라운드 진행)인 사이클은 진입을 생략한다(redteam #8)
assert calls["entry"] == 0
task = sch._inflight_steps["청산 판정"]
assert not task.done() # 취소하지 않음(매도 이중 실행 방지)
assert sch._exit_health["timeouts"] == 1
@@ -621,3 +623,6 @@ async def test_repeated_zero_total_on_live_account_triggers_blind_block(monkeypa
assert health["empty_account"] is False
assert health["consecutive_failures"] == threshold
assert real._is_update_blind() is True
from _swing_redis_guard import _swing_redis_guard # noqa: E402,F401 (R9: 단독 실행 시 Redis 실접속 방지)