Repository navigation
Expand file tree
/
Copy pathengine.py
More file actions
651 lines (516 loc) · 18.5 KB
/
Copy pathengine.py
File metadata and controls
651 lines (516 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/env pypy
# -*- coding: utf-8 -*-
from __future__ import annotations
import re
import subprocess
import threading
from collections import deque
from typing import Optional
_SCORE_RE = re.compile(r"\bscore\s+(cp|mate)\s+([+-]?\d+)\b")
_PV_RE = re.compile(r"\bpv\s+(.+)$")
_MULTIPV_RE = re.compile(r"\bmultipv\s+(\d+)\b")
_BOUND_RE = re.compile(r"\b(?:lowerbound|upperbound)\b")
class Engine:
"""
Thread-safe UCI/XBoard process wrapper.
"""
def __init__(
self,
engine_path: str,
*,
verbose: bool = True,
max_info_lines: int = 5000,
stop_drain_timeout: float = 2.0,
):
self.verbose = bool(verbose)
self.max_info_lines = max(100, int(max_info_lines))
self.stop_drain_timeout = max(0.1, float(stop_drain_timeout))
self.engine = subprocess.Popen(
[engine_path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
errors="replace",
bufsize=1,
)
if self.engine.stdin is None or self.engine.stdout is None:
raise RuntimeError("Failed to open engine stdin/stdout pipes.")
self.move_info = []
self.best_move_line: Optional[str] = None
self.ponder_hint: Optional[str] = None
self._move_event = threading.Event()
self._hint_event = threading.Event()
self._engine_closed_event = threading.Event()
self._reader_thread: Optional[threading.Thread] = None
self._reader_started = False
self._reader_exception: Optional[BaseException] = None
self._state_lock = threading.RLock()
self._write_lock = threading.Lock()
self._reader_start_lock = threading.Lock()
self._mate_stop_requested = False
self._discard_bestmove_once = False
self._search_mode: Optional[str] = None
self._stopped_search_drained = threading.Event()
self._stopped_search_drained.set()
self.latest_eval: Optional[str] = None
self.latest_score_type: Optional[str] = None
self.latest_score_value: Optional[int] = None
def _log_out(self, line: str) -> None:
if self.verbose:
print(">>", line)
def _log_in(self, line: str) -> None:
if self.verbose:
print("<<", line)
def is_alive(self) -> bool:
return self.engine.poll() is None
def _check_alive(self) -> None:
code = self.engine.poll()
if code is not None:
raise RuntimeError(f"Engine process is not running (exit code {code}).")
def put(self, command: str):
"""
Send one command atomically to the engine.
Multiple worker threads can call put(); command lines will not interleave.
"""
if not isinstance(command, str):
command = str(command)
command = command.rstrip("\r\n")
self._check_alive()
with self._write_lock:
self._check_alive()
stdin = self.engine.stdin
if stdin is None:
raise RuntimeError("Engine stdin is closed.")
self._log_out(command)
try:
stdin.write(command + "\n")
stdin.flush()
except (BrokenPipeError, OSError) as exc:
self._engine_closed_event.set()
raise RuntimeError("Failed to write to engine process.") from exc
def start_reader(self):
"""
Start the single stdout consumer.
Safe to call repeatedly. Once started, no other code may call
engine.stdout.readline() directly.
"""
with self._reader_start_lock:
if self._reader_started:
return
self._reader_started = True
self._reader_thread = threading.Thread(
target=self._reader_loop,
name="engine-stdout",
daemon=True,
)
self._reader_thread.start()
def _append_info_line(self, line: str) -> None:
with self._state_lock:
self.move_info.append(line)
excess = len(self.move_info) - self.max_info_lines
if excess > 0:
del self.move_info[:excess]
@staticmethod
def _multipv_number(line: str) -> int:
match = _MULTIPV_RE.search(line)
if not match:
return 1
return int(match.group(1))
@classmethod
def _is_primary_pv(cls, line: str) -> bool:
return cls._multipv_number(line) == 1
@staticmethod
def _format_score(score_type: str, score_value: int) -> str:
if score_type == "cp":
return f"{score_value / 100.0:+.2f}"
if score_value > 0:
return f"Mate in {score_value}"
if score_value < 0:
return f"Mated in {abs(score_value)}"
return "Mate"
def _handle_info_line(self, line: str) -> None:
"""
Track only the root/primary PV evaluation.
MultiPV 2+ lines are useful diagnostics but must not overwrite the score
associated with the move the engine is actually choosing.
"""
if not self._is_primary_pv(line):
return
score_match = _SCORE_RE.search(line)
if score_match:
score_type = score_match.group(1)
score_value = int(score_match.group(2))
with self._state_lock:
self.latest_score_type = score_type
self.latest_score_value = score_value
self.latest_eval = self._format_score(
score_type,
score_value,
)
self._maybe_return_forced_mate(line)
def _maybe_return_forced_mate(self, line: str) -> None:
"""
Return a synthetic bestmove as soon as an EXACT primary-PV mate is known.
Do not use:
- MultiPV 2+
- lowerbound / upperbound mate scores
- ponder searches
Those can otherwise make the worker instantly play the wrong move.
"""
if not self._is_primary_pv(line):
return
if _BOUND_RE.search(line):
return
score_match = _SCORE_RE.search(line)
if not score_match or score_match.group(1) != "mate":
return
pv_match = _PV_RE.search(line)
if not pv_match:
return
pv_moves = pv_match.group(1).split()
if not pv_moves:
return
with self._state_lock:
if self._search_mode != "normal":
return
if self._mate_stop_requested:
return
synthetic = "bestmove " + pv_moves[0]
if len(pv_moves) >= 2:
synthetic += " ponder " + pv_moves[1]
self.ponder_hint = pv_moves[1]
self._hint_event.set()
self.best_move_line = synthetic
self._mate_stop_requested = True
self._discard_bestmove_once = True
self._stopped_search_drained.clear()
if self.verbose:
print(
"Mate score detected on primary PV - returning immediately:",
synthetic,
)
try:
self.put("stop")
except RuntimeError:
pass
self._move_event.set()
def _handle_bestmove(self, line: str) -> None:
with self._state_lock:
if self._discard_bestmove_once:
self._discard_bestmove_once = False
self._mate_stop_requested = False
self._stopped_search_drained.set()
if self.verbose:
print("Discarding stopped-search bestmove:", line)
return
self.best_move_line = line
self._mate_stop_requested = False
self._search_mode = None
parts = line.split()
if len(parts) >= 4 and parts[2] == "ponder":
self.ponder_hint = parts[3]
self._hint_event.set()
self._move_event.set()
def _handle_xboard_move(self, line: str) -> None:
with self._state_lock:
self.best_move_line = line
self._search_mode = None
self._move_event.set()
def _reader_loop(self):
try:
stdout = self.engine.stdout
if stdout is None:
raise RuntimeError("Engine stdout is closed.")
while True:
line = stdout.readline()
if line == "":
if self.verbose:
print(
"Engine closed or produced EOF "
f"(exit code {self.engine.poll()})"
)
break
line = line.strip()
if not line:
continue
self._log_in(line)
self._append_info_line(line)
if line.startswith("info "):
self._handle_info_line(line)
continue
if line.startswith("Hint:"):
with self._state_lock:
self.ponder_hint = line.split(":", 1)[1].strip()
self._hint_event.set()
continue
if line.startswith("bestmove "):
self._handle_bestmove(line)
continue
if line.startswith("move "):
self._handle_xboard_move(line)
continue
except BaseException as exc:
self._reader_exception = exc
if self.verbose:
print("Engine reader thread failed:", repr(exc))
finally:
self._engine_closed_event.set()
self._move_event.set()
self._hint_event.set()
self._stopped_search_drained.set()
def read_until(self, stop_token: str):
"""
Synchronous reader for startup handshakes only.
The existing server can keep using this BEFORE start_reader().
"""
if self._reader_started:
raise RuntimeError(
"read_until() cannot be used after start_reader(); "
"the background reader owns stdout."
)
output = []
stdout = self.engine.stdout
if stdout is None:
raise RuntimeError("Engine stdout is closed.")
while True:
line = stdout.readline()
if line == "":
self._engine_closed_event.set()
if self.verbose:
print("Engine closed or produced EOF")
break
line = line.strip()
if line:
self._log_in(line)
output.append(line)
if line == stop_token or line.startswith(stop_token):
break
return output
def _wait_for_forced_stop_drain(self) -> None:
"""
If an earlier mate shortcut sent `stop`, wait for that search's real
bestmove before starting another search.
Without this, the acknowledgement bestmove can arrive during the next
search and accidentally consume the next search result.
"""
with self._state_lock:
pending = self._discard_bestmove_once
if not pending:
return
if not self._stopped_search_drained.wait(self.stop_drain_timeout):
raise TimeoutError(
"Timed out waiting for the previous stopped search to drain."
)
def _reset_eval(self) -> None:
with self._state_lock:
self.latest_eval = None
self.latest_score_type = None
self.latest_score_value = None
def clear_move_info(self):
"""
Reset state for a brand-new normal search.
Pending forced-stop acknowledgements are intentionally not cleared here;
_begin_new_search() drains them first.
"""
with self._state_lock:
self.move_info = []
self.best_move_line = None
self.ponder_hint = None
self._mate_stop_requested = False
self.latest_eval = None
self.latest_score_type = None
self.latest_score_value = None
self._move_event.clear()
self._hint_event.clear()
def _prepare_for_next_move(
self,
clear_hint: bool = True,
*,
clear_eval: bool = False,
):
with self._state_lock:
self.move_info = []
self.best_move_line = None
self._mate_stop_requested = False
if clear_eval:
self.latest_eval = None
self.latest_score_type = None
self.latest_score_value = None
if clear_hint:
self.ponder_hint = None
self._move_event.clear()
if clear_hint:
self._hint_event.clear()
def _begin_new_search(
self,
mode: str,
*,
clear_hint: bool = True,
) -> None:
self._wait_for_forced_stop_drain()
self._prepare_for_next_move(
clear_hint=clear_hint,
clear_eval=True,
)
with self._state_lock:
self._search_mode = mode
def wait_for_move(self, timeout=None):
if not self._reader_started:
self.start_reader()
if not self._move_event.wait(timeout):
return "search timeout"
with self._state_lock:
has_move = self.best_move_line is not None
if has_move:
return "search complete"
if self._engine_closed_event.is_set():
return "engine closed"
return "search interrupted"
def wait_for_hint(self, timeout=0.25):
with self._state_lock:
if self.ponder_hint:
return self.ponder_hint
self._hint_event.wait(timeout)
with self._state_lock:
return self.ponder_hint
def get_eval(self):
"""Return the latest formatted PRIMARY-PV evaluation."""
with self._state_lock:
return self.latest_eval
def get_score(self):
"""
Return raw score information as (type, value), e.g. ("cp", 34).
"""
with self._state_lock:
return self.latest_score_type, self.latest_score_value
def get_move_info(self):
"""Return a thread-safe snapshot of recent engine output."""
with self._state_lock:
return list(self.move_info)
def get(self):
return self.wait_for_move()
def search_fen_str_with_clocks(
self,
fen: str,
clocks: dict,
increments: dict,
delays: dict = None,
timeout=None,
):
if delays is None:
delays = {c: 0 for c in ("r", "b", "y", "g")}
self._begin_new_search("normal")
self.put("position fen " + fen)
go_cmd = (
"go depth 64"
" clocks {rtime} {btime} {ytime} {gtime}"
" incs {rinc} {binc} {yinc} {ginc}"
).format(
rtime=clocks.get("r", 60000),
btime=clocks.get("b", 60000),
ytime=clocks.get("y", 60000),
gtime=clocks.get("g", 60000),
rinc=increments.get("r", 0),
binc=increments.get("b", 0),
yinc=increments.get("y", 0),
ginc=increments.get("g", 0),
)
self.put(go_cmd)
return self.wait_for_move(timeout)
def search_fen_str(
self,
fen: str,
time_allowed: str,
uci_allowed=True,
timeout=None,
):
self._begin_new_search("normal")
if uci_allowed:
self.put("position fen " + fen)
self.put("go movetime " + str(time_allowed))
else:
self.put("post")
self.put("setboard " + fen)
self.put("st " + str(time_allowed))
self.put("go")
return self.wait_for_move(timeout)
def start_ponder(
self,
fen: str,
moves: str,
ponder_move: str = None,
):
self._begin_new_search("ponder", clear_hint=True)
self.put("position fen " + fen + " moves " + moves)
self.put("go ponder")
def ponder_hit(self, timeout=None):
self._prepare_for_next_move(clear_hint=False, clear_eval=False)
with self._state_lock:
self._search_mode = "normal"
self.put("ponderhit")
return self.wait_for_move(timeout)
def xboard_ponder_hit(self, opponent_move: str, timeout=None):
self._prepare_for_next_move(clear_hint=True, clear_eval=False)
with self._state_lock:
self._search_mode = "normal"
self.put(opponent_move)
return self.wait_for_move(timeout)
def stop(self, uci_allowed=True, timeout=1.0):
"""
Stop the active search and reset move/ponder state.
This remains compatible with the old server, but handles an engine that
exits during stop without hanging indefinitely.
"""
if not self.is_alive():
self._prepare_for_next_move(clear_hint=True, clear_eval=False)
return
if uci_allowed:
self._move_event.clear()
with self._state_lock:
self.best_move_line = None
try:
self.put("stop")
except RuntimeError:
pass
self._move_event.wait(timeout)
else:
try:
self.put("force")
except RuntimeError:
pass
self._prepare_for_next_move(clear_hint=True, clear_eval=False)
with self._state_lock:
self._search_mode = None
def quit(self, timeout=2.0):
"""
Shut down cleanly.
UCI quit is attempted first. If the engine ignores it, terminate(), then
kill() is used as a final fallback.
"""
if self.engine.poll() is not None:
return
try:
self.put("quit")
except RuntimeError:
pass
try:
self.engine.wait(timeout=timeout)
except subprocess.TimeoutExpired:
self.engine.terminate()
try:
self.engine.wait(timeout=1.0)
except subprocess.TimeoutExpired:
self.engine.kill()
self.engine.wait(timeout=1.0)
finally:
self._engine_closed_event.set()
self._move_event.set()
self._hint_event.set()
self._stopped_search_drained.set()
close = quit
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
self.quit()