Files
trex-core/docs/batches/bat-005_20260604_kis-token-redis-cache/planning.md
T
genichinandClaude Opus 4.8 ac6061f510 Deliver BAT-005: KIS token Redis cache (REQ-016, minor-change)
Port the trex-orbit Redis token-cache pattern so the KIS access token
survives daemon restarts and is shared across processes, avoiding KIS
token-issuance rate limits. KISBroker._ensure_token now resolves L1
in-memory -> L2 cache -> issue, storing issued tokens back to the cache.

- adapters/broker/token_cache.py: TokenCache protocol + RedisTokenCache
  (key trex:broker:kis:{account_ref}:token, token+expiry+app_key suffix
  JSON, ex=ttl, best-effort with graceful fallback) + InMemoryTokenCache.
- kis.py: optional token_cache injection.
- runtime config/daemon: TREX_KIS_REDIS_URL wires a RedisTokenCache.
- pyproject: redis>=5.

Verification: 96 passed, ruff clean — cache hit avoids issuance, cache is
shared across instances (restart/multi-process survival), backend errors
fall back to issuance (no regression), and app_key-suffix mismatch
invalidates a stale cache. Tested deterministically with an in-memory
double (no Redis server required). Processed via the minor-change fast
path: REQ-016 -> Implemented, BAT-005 (batch-lite) -> release-candidate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 18:44:09 +09:00

1.6 KiB

BAT-005 Planning

  • Status: confirmed
  • Batch: BAT-005
  • Profile: batch-lite

Included REQ

  • REQ-016 (Integration): KIS 인증 토큰 Redis 캐시

Delivery Plan

  1. trex_core/adapters/broker/token_cache.py: TokenCache Protocol(load/store) + RedisTokenCache(redis.from_url, JSON 토큰+만료+app_key suffix, ex=ttl, 예외 graceful) + InMemoryTokenCache(테스트/폴백 더블).
  2. kis.py: token_cache 주입, _ensure_token = L1 인메모리 → L2 캐시(load, suffix 검증, 만료 확인) → 발급+store.
  3. runtime/config.py: TREX_KIS_REDIS_URL → KISConfig.redis_url. runtime/daemon.py: redis_url 있으면 RedisTokenCache 주입.
  4. pyproject.toml: redis>=5 의존.
  5. tests/adapters/broker/test_token_cache.py: 캐시 히트(HTTP 0)/공유 재사용/graceful fallback/app_key suffix.

Dependencies

  • Internal: REQ-012 KISBroker 확장.
  • External: redis 라이브러리(+ 운영 시 Redis 서버). 테스트는 인메모리 더블.

Design Gate

  • Design Required Before Implementation: no
  • Reason: 기존 BrokerPort 뒤 토큰 캐시 계층 추가뿐. 외부 인터페이스/슬롯 런타임 흐름 변경 없음. batch-lite + design 생략(minor-change).

Milestones

  1. M1 — TokenCache + RedisTokenCache + KISBroker 3-레벨.
  2. M2 — config/daemon wiring + pyproject.
  3. M3 — 단위테스트 green(히트/공유/폴백/suffix).

Risks

  • R-1 (Low): Redis 장애가 주문 경로를 막음. 대응: 캐시 예외 graceful fallback(발급으로 진행), 캐시는 best-effort.
  • R-2 (Low): 자격증명 교체 후 stale 토큰. 대응: app_key suffix 검증으로 캐시 무효화(AC-4).