paper-dynasty-database/app/services
Cal Corum ab15228c44 fix(refractor): mask variant hash to 31 bits to fit Postgres INTEGER
compute_variant_hash took the first 8 hex chars of a SHA-256 digest and
cast to int, producing values up to 2^32 - 1. The variant columns on
Card, BattingCard, PitchingCard, and RefractorCardState are Peewee
IntegerField → Postgres INTEGER, which is signed 32-bit (max 2^31 - 1).
Roughly half of all players (~50%) would hash into the range [2^31,
2^32 - 1] and crash tier-up writes with:

  peewee.DataError: integer out of range

Surfaced via /dev refractor-test card_id:64460 (Charles Nagy,
player_id=10795), whose tier-1 hash was 2874960417. The outer
exception handler in refractor.evaluate_game caught the error and
logged a warning, so the tier-up was silently dropped — the test
harness reported "No tier-up detected (evaluated 2 cards)" while
apply_tier_boost was actually failing mid-write.

Fix: mask the hash with & 0x7FFFFFFF, dropping one bit of entropy.
~2.1B distinct values remain — still astronomically collision-safe.

Backwards-compatible: all 9 existing refractor_boost_audit rows and
9 persisted non-zero variants have hashes where the high bit was
already 0 (those tier-ups happened to land in the safe half). Masking
leaves those values unchanged.

Added regression test test_fits_postgres_int32 covering 10,000 player
IDs × 5 tiers = 50,000 combinations, all asserted ≤ 2,147,483,647.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 13:11:40 -05:00
..
__init__.py feat: formula engine for evolution value computation (WP-09) 2026-03-12 19:34:40 -05:00
apng_generator.py feat: APNG animated card effects for T3/T4 refractor tiers (#186) 2026-04-07 03:22:05 +00:00
card_storage.py feat: S3 upload pipeline for APNG animated cards (#198) 2026-04-08 10:04:21 -05:00
formula_engine.py refactor: rename evolution system to refractor 2026-03-23 13:31:55 -05:00
refractor_boost.py fix(refractor): mask variant hash to 31 bits to fit Postgres INTEGER 2026-04-11 13:11:40 -05:00
refractor_evaluator.py feat: add REFRACTOR_START_SEASON floor to evaluator queries (#195) 2026-04-08 08:03:07 -05:00
refractor_init.py fix: complete remaining evolution→refractor renames from review 2026-03-23 14:17:03 -05:00
season_stats.py fix: add pitcher_id null guard in _get_player_pairs 2026-03-23 12:50:16 -05:00