UCI "position" question

Discussion of chess software programming and technical issues.

Moderator: Ras

syzygy
Posts: 5693
Joined: Tue Feb 28, 2012 11:56 pm

Re: UCI "position" question

Post by syzygy »

benvining wrote: Mon Jun 02, 2025 8:05 pm
assuming you do validate moves from the "GUI"
I didn't realize we were supposed to validate moves received via UCI. What is the engine supposed to do if the "position" command contains an illegal move?
The engine indeed does not need to validate moves since the UCI protocol requires the moves to be legal.
If the engine does validate the moves, you can let it do whatever you want upon encountering an illegal move. (Users wouldn't appreciate their harddrive being formatted. But in the end it is their responsibility not to feed a UCI engine with illegal positions or moves.)
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI "position" question

Post by hgm »

syzygy wrote: Tue Jun 03, 2025 4:01 am The engine indeed does not need to validate moves since the UCI protocol requires the moves to be legal.
Where does it actually say that? I searched the UCI specs for the word 'legal', but it wasn't there.
syzygy
Posts: 5693
Joined: Tue Feb 28, 2012 11:56 pm

Re: UCI "position" question

Post by syzygy »

hgm wrote: Tue Jun 03, 2025 9:51 am
syzygy wrote: Tue Jun 03, 2025 4:01 am The engine indeed does not need to validate moves since the UCI protocol requires the moves to be legal.
Where does it actually say that? I searched the UCI specs for the word 'legal', but it wasn't there.
There is no UCI mechanism for the engine to report an error in the input it receives to the GUI. So apparently the GUI has to do the checking.

(Of course it is a serious deficiency of the spec that it leaves room for speculation on ths point.)
syzygy
Posts: 5693
Joined: Tue Feb 28, 2012 11:56 pm

Re: UCI "position" question

Post by syzygy »

See also this post, which quotes the author of the UCI spec (for what that's worth):
viewtopic.php?p=876799#p876799
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI "position" question

Post by hgm »

That there would be no dedicated formal way to report an error doesn't seem justification for the idea that an error should be allowed to cause 'undefined behavior' such as formatting the hard disc. And of course UCI engine actually do have the possibility to report errors: there is an "info string" command that they could use to relay any message to the GUI, which then presumably would relay that to the user, and they could use that to report an error.

A discussion between two GUI developers, which did not really reach a conclusion doesn't carry much weight. As remarked, if being legal would be a protocol requirement, the protocol specs should contain a definition of what is considered legal, as the FIDE definition for this would be impossibly complicated, especially for a GUI. There are many severities of illegality, from not having move syntax or containing off-board coordinates to merely leaving a King in check.

A more sensible approach would be to take all moves in the position-moves command at face value, i.e. accept them if the coordinates are on board and the promotion suffix corresponds to a known piece type. After all, their purpose is just to define a position, and the only thing relevant for the inner workings of the engine is whether it can handle the resulting position.
syzygy
Posts: 5693
Joined: Tue Feb 28, 2012 11:56 pm

Re: UCI "position" question

Post by syzygy »

hgm wrote: Wed Jun 04, 2025 10:14 am That there would be no dedicated formal way to report an error doesn't seem justification for the idea that an error should be allowed to cause 'undefined behavior' such as formatting the hard disc.
Deliberately programming the engine to do that would of course be evil. But without input validation, it is difficult to guarantee that data loss cannot happen if you feed the engine cleverly crafted malicious data that you downloaded from the internet. And even if it does do some input validation, it will almost probably not be complete.
A discussion between two GUI developers, which did not really reach a conclusion doesn't carry much weight.
The author of the UCI spec was pretty clear. The idea behind UCI is to move all complexity as much as possible to the GUI.
Of course his opinion expressed in a forum post is not automatically part of the spec, but it is another confirmation that this is how the spec is generally understood by the community. Another confirmation is that many authors of top engine accept that their engines crash on invalid input.

Of course there is no law against validating the input. Whatever the engine does will confuse the GUI, but the GUI wasn't supposed to give the engine invalid input in the first place.
As remarked, if being legal would be a protocol requirement, the protocol specs should contain a definition of what is considered legal, as the FIDE definition for this would be impossibly complicated, especially for a GUI. There are many severities of illegality, from not having move syntax or containing off-board coordinates to merely leaving a King in check.
You will not hear from me that the UCI spec is complete.
benvining
Posts: 22
Joined: Fri May 30, 2025 10:18 pm
Full name: Ben Vining

Re: UCI "position" question

Post by benvining »

It seems to me that crashing on invalid input is one of the better options for the engine. This makes it impossible for the error to go unnoticed by the handler (whether UI or tournament manager), and prevents further errors in deeper logic by failing as soon as possible.
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI "position" question

Post by hgm »

Well, people would blame the engine, and label it is 'unreliable' for errors in the GUI. I would at least print a message in an 'info string' command before terminating.