czbar / ChessForge

Chess Forge application
MIT License
11 stars 2 forks source link

Incorrect FEN #640

Closed akaKarthick closed 1 year ago

akaKarthick commented 1 year ago

To reproduce the bug, follow these steps:

  1. Import the game [game URL] to a chapter.

  2. Go to the 18th move (Kf8, black).

  3. Copy the FEN.

  4. Go to chess.com or Lichess and paste the FEN in analyze mode.

  5. Move Nxc4 (19th move of White).

In chess.com and lichess, this move will be registered as the 18th move of White, even though it is the 19th move according to the original PGN file.

#needs fixing

How did I step into this?

When I try to import a game and then evaluate it ("Evaluate games in chapter"), the FEN for white moves is incorrect. This causes Stockfish and Leela Chess Zero to crash.

I have attached the game of the crash message from Leela Chess Zero and Stockfish.

https://docs.google.com/spreadsheets/d/1wjPtRvrbqxqPleoS5JuPO2hkmfBtcYDDWprkC7O-VNw/edit?usp=sharing Screenshot (339)

I believe this is caused by an incorrect FEN for white moves being supplied by ChessForge. Would you be able to take a look at this issue and see if you can find a solution?

czbar commented 1 year ago

Sure, I'll certainly have a look and resolve the issue (as well as the other issues you have raised recently). It may have to wait a few more days as I am in the middle of a trip across Central America right now. From next Tuesday I'll be staying put in Costa Rica for another 2 months so I'll start clearing the backlog while there!

akaKarthick commented 1 year ago

Enjoy your wonderful trip. 💐😉

czbar commented 1 year ago

Just fixed it (in private build 1.2.1 that I will publish in a few days). Indeed, Chess Forge was generating move numbers half-move too early.

Just to be sure that I fixed everything, can you please elaborate on "When I try to import a game and then evaluate it ("Evaluate games in chapter"), the FEN for white moves is incorrect. This causes Stockfish and Leela Chess Zero to crash." In what environment were the engines crashing? In Chess Forge or elsewhere?

akaKarthick commented 1 year ago

I guess you fixed it.

akaKarthick commented 1 year ago

Just to be sure that I fixed everything, can you please elaborate on "When I try to import a game and then evaluate it ("Evaluate games in chapter"), the FEN for white moves is incorrect. This causes Stockfish and Leela Chess Zero to crash." In what environment were the engines crashing? In Chess Forge or elsewhere?

https://youtu.be/1f6x1-VGEWQ

This is what i saw on that day. Leela not accepted the move 14.Bxa8 and crashed. (check the picture in first comment)

Stockfish understand the move and continued. But after 23 rd move its crashed. I dont know why. Maybe bad FEN in pgn. But how is that possible? That game was played in online.

https://www.chess.com/game/live/5134397257?username=selvasumi

Is this new issue? or same issue?

akaKarthick commented 1 year ago

this game also. I guess capturing rook is cause this error. https://www.chess.com/game/live/68647953759

czbar commented 1 year ago

Indeed, the rook capture is causing the problem! There is a bug in the way FEN is generated (and passed on to the engine) whereby the castling rights are not updated (removed) when the rook is captured. The FEN incorrectly indicates that Black still has the right to castle to the side where the rook was actually captured. Stockfish behaves totally unpredictably when facing an error (the developers have refused for years to handle/report errors) and sends garbage moves back (e.g., long castle for Black on the first rank :)) which Chess Forge does not handle gracefully (I will fix that too, btw). I don't know much about Leela yet, but it seems to handle errors differently. I'll look into that later. The end result is the same, though.

Great find!

czbar commented 1 year ago

Just for the record: this part of my above comment is not accurate "whereby the castling rights are not updated (removed) when the rook is captured." In fact, the code has always been checking for that. The bug was that in some circumstances (when white has no castling right), the check was skipped! It's fixed now.