Fix/engine correctness and performance#1
Conversation
Fix 1 (LMR): single null-window search + conditional full-depth re-search when reduced or score < beta, avoiding duplicate re-search on cutoffs. Fix 2 (fast eval): add _evaluate_material_fast() — O(64) material+PST+bishop pair only — for quiescence stand_pat; full eval kept for depth-0 nodes. Fix 3 (adaptive timeout): introduce _timeout_mask; tighten to 255 nodes for time controls < 100 ms (was always 2047) so short moves don't overrun budget. Fix 4 (TT mate scores): add _score_to_tt/_score_from_tt ply-distance encoding so stored mate-in-N values remain correct when retrieved at different plies. Fix 5 (two-pass generate_all_legal_moves): replace recursive refresh with a non-recursive two-pass approach; always set board.turn to match the requested side inside generate_all_legal_moves and get_legal_moves via try/finally. Fix 6 (in-place sort): order_moves now calls moves.sort() instead of sorted(), eliminating a full list copy on every negamax node. Fix 7 (perft try/finally): wrap push/perft/pop in try/finally so board state is always restored even if perft raises. Fix 8 (_extract_pv): remove ValueError from suppressed exceptions so real bugs surface instead of silently truncating the PV. Fix 9 (en passant undo): restructure board.undo_move so destination square is set to "." before restoring the captured pawn when undoing en passant, removing a window where piece_positions contained a phantom piece. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
test_perft.py: use standard kiwipete FEN (no c5 pawn, matches CPW reference values D2=2039, D3=97862); replace broken promotions position with the classic CPW Position 5 (n1n5/PPPk4/8/...) whose D1/D2/D3 values are well-known; add depth-4 cases for startpos (197281) and endgame_ep (43238). test_state_integrity.py (new): 5 tests verifying board state is fully restored after push/pop, search timeout, stop flag, and UCI stop; and that generate_all_legal_moves honours the requested side regardless of board.turn. test_notation_pgn_uci.py: relax go-runs-in-background timing assertion from 50 ms to 500 ms to accommodate Windows thread-spawn overhead without losing the non-blocking guarantee. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR enhances the chess engine's robustness and performance through three interconnected improvements: corrected en passant undo logic in the board state manager, significant refactoring of the move generator to add time management, TT integration with mate scoring, fast material evaluation, and defensive state guards, and expanded test coverage validating state integrity across search and board operations. ChangesBoard State and Search Engine Improvements
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
reduced or score < beta; eliminates the double full-depth re-search bug._evaluate_material_fast()— O(64) material+PST+bishop-pair only — for quiescencestand_pat; avoids slow king-safety/pawn-map eval on every capture node._timeout_masktightened from 2047→255 for time controls < 100 ms so short moves don't overshoot their budget._score_to_tt/_score_from_ttencode/decode mate scores relative to the storing ply so retrieved values remain correct at other plies.generate_all_legal_moves/get_legal_movesnow setboard.turnto match the requested side viatry/finally, removing the stale-turn bug that caused wrong move counts when called out of turn; two-pass refresh replaces the unbounded recursive retry.order_movesuseslist.sort()instead ofsorted(), eliminating a full list copy per negamax node.push/popin perft wrapped intry/finallyso board state is always restored even on exception.ValueErrorfrom the suppressed-exception tuple so real bugs surface instead of silently truncating the PV.board.undo_moveto set the destination square to"."before restoring the captured pawn, closing a window wherepiece_positionstemporarily held a phantom piece.Test changes
n1n5/PPPk4/..., D1=24 D2=496 D3=9483); added depth-4 for startpos and endgame_ep.generate_all_legal_movesside-independence.Test plan
pytest -q— all 97 tests pass (perft depth-4 included)Summary by CodeRabbit
Release Notes
Bug Fixes
Performance & Stability