-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for eval command #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughA new non-standard UCI command Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Test_EngineEval
participant Engine as Engine
participant Stockfish as Stockfish Engine
Test->>Engine: New()
Test->>Engine: Run(CmdUci, CmdIsReady, CmdUcinewgame, CmdSetPosition, CmdEval)
Engine->>Stockfish: Send "eval" command
Stockfish-->>Engine: Output lines
Engine->>Engine: Parse "Final evaluation" line, update eval
Test->>Engine: Eval()
Engine-->>Test: Return eval value
Assessment against linked issues
Poem
Note β‘οΈ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note β‘οΈ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure β¨ Finishing Touches
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. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
π§Ή Nitpick comments (2)
uci/cmd.go (1)
123-144: Improve the CmdEval implementation with better magic number handling and error handlingThe implementation works correctly for Stockfish, but has a few areas for improvement:
- Magic numbers should be named constants for better readability
- Error handling for the ParseFloat operation could be improved
- The implementation assumes a Stockfish-specific output format
// CmdEval is a non-standard command that requests the engine's static evaluation of the current position. +const ( + // evalIndexInParts is the index of the evaluation value in the space-separated output + evalIndexInParts = 2 + // centipawnScale converts from decimal pawn values to centipawn integer values + centipawnScale = 100 +) CmdEval = cmdNoOptions{Name: "eval", F: func(e *Engine) error { scanner := bufio.NewScanner(e.out) for scanner.Scan() { text := e.readLine(scanner) if strings.Contains(text, "error") { return errors.New("eval command not supported") } if strings.HasPrefix(text, "Final evaluation") { parts := strings.Fields(text) - if len(parts) >= 3 { + if len(parts) > evalIndexInParts { - evalStr := parts[2] + evalStr := parts[evalIndexInParts] eval, err := strconv.ParseFloat(evalStr, 64) if err == nil { - e.eval = int(math.Round(eval * 100)) + e.eval = int(math.Round(eval * centipawnScale)) + } else { + return fmt.Errorf("failed to parse evaluation value: %w", err) } break } } } return nil }}π§° Tools
πͺ golangci-lint (1.64.8)
137-137: Magic number: 100, in detected
(mnd)
133-133: Magic number: 3, in detected
(mnd)
uci/engine_test.go (1)
22-60: LGTM: Good test coverage for eval functionalityThe test thoroughly checks both successful evaluation with Stockfish and proper error handling with lc0, which doesn't support the eval command. The test covers:
- Setting up a specific chess position
- Running the eval command
- Verifying the expected outcomes for different engines
One minor suggestion: consider making the minimum expected evaluation value a named constant for better maintainability.
func Test_EngineEval(t *testing.T) { + const minExpectedEval = 500 for _, name := range engines { fenStr := "4k3/8/8/8/8/8/8/4K2R w - - 0 1" t.Run("EngineEval_"+name, func(t *testing.T) { if !isEngineAvailable(name) { t.Skipf("engine %s not available", name) } pos := &chess.Position{} if err := pos.UnmarshalText([]byte(fenStr)); err != nil { t.Fatal("failed to parse FEN", err) } eng, err := uci.New(name, uci.Debug) if err != nil { t.Fatal(err) } defer eng.Close() cmdPos := uci.CmdPosition{Position: pos} err = eng.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame, cmdPos, uci.CmdEval) if name == "stockfish" { if err != nil { t.Fatal("failed to run command", err) } - if eng.Eval() < 500 { + if eng.Eval() < minExpectedEval { - t.Errorf("expected an eval greater than or equal to 500, got %d", eng.Eval()) + t.Errorf("expected an eval greater than or equal to %d, got %d", minExpectedEval, eng.Eval()) } } else if name == "lc0" { if err == nil { t.Fatal("expected an error", err) } } }) } }
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting
π Files selected for processing (3)
uci/cmd.go(2 hunks)uci/engine.go(2 hunks)uci/engine_test.go(1 hunks)
π§° Additional context used
𧬠Code Graph Analysis (2)
uci/engine_test.go (2)
uci/engine.go (2)
New(46-64)Debug(31-33)uci/cmd.go (3)
CmdPosition(181-184)CmdPosition(202-204)CmdEval(124-144)
uci/cmd.go (1)
uci/engine.go (2)
Engine(15-27)New(46-64)
πͺ golangci-lint (1.64.8)
uci/cmd.go
137-137: Magic number: 100, in detected
(mnd)
133-133: Magic number: 3, in detected
(mnd)
π Additional comments (3)
uci/engine.go (2)
25-25: LGTM: Adding evaluation field to Engine structThe
evalfield is a good addition to store the evaluation score from chess engines.
128-132: LGTM: Thread-safe accessor method for evaluation scoreThe
Eval()method provides a thread-safe way to access the evaluation score, properly using a read lock on the engine's mutex. This follows the same pattern as other accessor methods in the Engine struct.uci/cmd.go (1)
7-7: LGTM: Added math import for roundingAdded math package import to support the
math.Roundfunction used for evaluation score processing.
|
Thank you very much for the contribution |
Resolves #38
Let me know if you want any changes
Summary by CodeRabbit
New Features
Tests