refactor: extract evaluate-game business logic from router to service layer #202

Open
opened 2026-04-08 00:24:01 +00:00 by cal · 1 comment
Owner

Problem

The evaluate_game function in app/routers_v2/refractor.py lines 318–452 is 134 lines of orchestration logic: pair extraction, auto-init, dry_run evaluation, multi-tier boost loop with partial failure handling, and notification assembly.

All in the router layer, which makes testing require HTTP round-trips through the test client.

Fix

Extract into a service function (evaluate_and_boost in a refractor service module). The spec in docs/plans/refractor-service-layer.md already documents this as the target architecture.

Impact

Working fine as-is. Refactor when next touching this code — don't rush. The main benefit is testability of edge cases without HTTP overhead.

## Problem The `evaluate_game` function in `app/routers_v2/refractor.py` lines 318–452 is 134 lines of orchestration logic: pair extraction, auto-init, dry_run evaluation, multi-tier boost loop with partial failure handling, and notification assembly. All in the router layer, which makes testing require HTTP round-trips through the test client. ## Fix Extract into a service function (`evaluate_and_boost` in a refractor service module). The spec in `docs/plans/refractor-service-layer.md` already documents this as the target architecture. ## Impact Working fine as-is. Refactor when next touching this code — don't rush. The main benefit is testability of edge cases without HTTP overhead.
Claude added the
ai-working
label 2026-04-08 05:31:17 +00:00
Collaborator

PR #207 opens the refactor: #207

Approach: Created app/services/refractor_service.py with two functions:

  • ensure_variant_cards — idempotent variant-card creation up to a target tier; handles partial failures, REFRACTOR_BOOST_ENABLED kill switch, and skips tiers where variants already exist
  • evaluate_and_boost — composes evaluate_card(dry_run=True) + ensure_variant_cards into a single call

Both router endpoints (POST /cards/{card_id}/evaluate and POST /evaluate-game/{game_id}) now delegate to evaluate_and_boost. The evaluate-game router loses ~55 lines of inline boost orchestration. Response shapes are unchanged.

PR #207 opens the refactor: https://git.manticorum.com/cal/paper-dynasty-database/pulls/207 **Approach:** Created `app/services/refractor_service.py` with two functions: - `ensure_variant_cards` — idempotent variant-card creation up to a target tier; handles partial failures, REFRACTOR_BOOST_ENABLED kill switch, and skips tiers where variants already exist - `evaluate_and_boost` — composes `evaluate_card(dry_run=True)` + `ensure_variant_cards` into a single call Both router endpoints (`POST /cards/{card_id}/evaluate` and `POST /evaluate-game/{game_id}`) now delegate to `evaluate_and_boost`. The evaluate-game router loses ~55 lines of inline boost orchestration. Response shapes are unchanged.
Claude added
ai-pr-opened
and removed
ai-working
labels 2026-04-08 05:38:09 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: cal/paper-dynasty-database#202
No description provided.