Skip to content

Commit 9b145a1

Browse files
Allow "engine debugging" to be enabled. (#1152)
* Allow "engine debugging" to be enabled. * Adapt test-code for debug-mode change. * Corrected description of debug-flag in config.yml.default * Updated wiki for engine debug-flag. * Add debug parameter to ScholarsMate engine Parameter is unused. * Typos * Add debug to Minimal Engine Remove unused name parameter (no other code uses it) and replace it with debug so all EngineWrapper classes have same __init__(). Remove default argument values to ensure __init__() is called correctly. --------- Co-authored-by: Mark Harrison <MarkZH@users.noreply.github.com>
1 parent 1dd55c2 commit 9b145a1

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

config.yml.default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ url: "https://lichess.org/" # Lichess base URL.
44
engine: # Engine settings.
55
dir: "./engines/" # Directory containing the engine. This can be an absolute path or one relative to lichess-bot/.
66
name: "engine_name" # Binary name of the engine to use.
7+
debug: False # When enabled, log all startup communication with the engine.
78
# interpreter: "java"
89
# interpreter_options:
910
# - "-jar"

lib/engine_wrapper.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def create_engine(engine_config: Configuration, game: model.Game | None = None)
6767
f" Invalid engine type: {engine_type}. Expected xboard, uci, or homemade.")
6868
options = remove_managed_options(cfg.lookup(f"{engine_type}_options") or Configuration({}))
6969
logger.debug(f"Starting engine: {commands}")
70-
return Engine(commands, options, stderr, cfg.draw_or_resign, game, cwd=cfg.working_dir)
70+
return Engine(commands, options, stderr, cfg.draw_or_resign, game, cfg.debug,
71+
cwd=cfg.working_dir)
7172

7273

7374
def remove_managed_options(config: Configuration) -> OPTIONS_GO_EGTB_TYPE:
@@ -478,7 +479,8 @@ class UCIEngine(EngineWrapper):
478479
"""The class used to communicate with UCI engines."""
479480

480481
def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stderr: int | None,
481-
draw_or_resign: Configuration, game: model.Game | None, **popen_args: str) -> None:
482+
draw_or_resign: Configuration, game: model.Game | None, debug: bool,
483+
**popen_args: str) -> None:
482484
"""
483485
Communicate with UCI engines.
484486
@@ -487,10 +489,11 @@ def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stder
487489
:param stderr: Whether we should silence the stderr.
488490
:param draw_or_resign: Options on whether the bot should resign or offer draws.
489491
:param game: The first Game message from the game stream.
492+
:param debug: Whether to debug or not.
490493
:param popen_args: The cwd of the engine.
491494
"""
492495
super().__init__(options, draw_or_resign)
493-
self.engine = chess.engine.SimpleEngine.popen_uci(commands, timeout=60., debug=False, setpgrp=True, stderr=stderr,
496+
self.engine = chess.engine.SimpleEngine.popen_uci(commands, timeout=60., debug=debug, setpgrp=True, stderr=stderr,
494497
**popen_args)
495498
self.configure(options, game)
496499

@@ -499,7 +502,8 @@ class XBoardEngine(EngineWrapper):
499502
"""The class used to communicate with XBoard engines."""
500503

501504
def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stderr: int | None,
502-
draw_or_resign: Configuration, game: model.Game | None, **popen_args: str) -> None:
505+
draw_or_resign: Configuration, game: model.Game | None, debug: bool,
506+
**popen_args: str) -> None:
503507
"""
504508
Communicate with XBoard engines.
505509
@@ -508,10 +512,11 @@ def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stder
508512
:param stderr: Whether we should silence the stderr.
509513
:param draw_or_resign: Options on whether the bot should resign or offer draws.
510514
:param game: The first Game message from the game stream.
515+
:param debug: Whether to debug or not.
511516
:param popen_args: The cwd of the engine.
512517
"""
513518
super().__init__(options, draw_or_resign)
514-
self.engine = chess.engine.SimpleEngine.popen_xboard(commands, timeout=60., debug=False, setpgrp=True,
519+
self.engine = chess.engine.SimpleEngine.popen_xboard(commands, timeout=60., debug=debug, setpgrp=True,
515520
stderr=stderr, **popen_args)
516521
egt_paths = cast(EGTPATH_TYPE, options.pop("egtpath", {}) or {})
517522
protocol = cast(chess.engine.XBoardProtocol, self.engine.protocol)
@@ -539,7 +544,7 @@ class MinimalEngine(EngineWrapper):
539544
"""
540545

541546
def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stderr: int | None, # noqa: ARG002
542-
draw_or_resign: Configuration, game: model.Game | None = None, name: str | None = None, # noqa: ARG002
547+
draw_or_resign: Configuration, game: model.Game | None, debug: bool, # noqa: ARG002
543548
**popen_args: str) -> None: # noqa: ARG002 Unused argument popen_args
544549
"""
545550
Initialize the values of the engine that all homemade engines inherit.
@@ -548,9 +553,7 @@ def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stder
548553
:param draw_or_resign: Options on whether the bot should resign or offer draws.
549554
"""
550555
super().__init__(options, draw_or_resign)
551-
552-
self.engine_name = self.__class__.__name__ if name is None else name
553-
556+
self.engine_name = self.__class__.__name__
554557
self.engine = FillerEngine(self, name=self.engine_name)
555558

556559
def get_pid(self) -> str:

test_bot/homemade.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class ScholarsMate(ExampleEngine):
1313
"""A homemade engine that plays the scholar's mate."""
1414

1515
def __init__(self, commands: COMMANDS_TYPE, options: OPTIONS_GO_EGTB_TYPE, stderr: int | None,
16-
draw_or_resign: Configuration, game: model.Game | None, **popen_args: str) -> None:
16+
draw_or_resign: Configuration, game: model.Game | None, debug: bool, **popen_args: str) -> None:
1717
"""Set up engine."""
18-
super().__init__(commands, options, stderr, draw_or_resign, game, **popen_args)
18+
super().__init__(commands, options, stderr, draw_or_resign, game, debug, **popen_args)
1919

2020
def search(self, board: chess.Board, time_limit: chess.engine.Limit, ponder: bool, draw_offered: bool,
2121
root_moves: MOVE) -> chess.engine.PlayResult:

wiki/Configure-lichess-bot.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Lichess allows a bot to play 100 games against other bots in a single day (games
1717
## Engine options
1818
- `interpreter`: Specify whether your engine requires an interpreter to run (e.g. `java`, `python`).
1919
- `interpreter_options`: A list of options passed to the interpreter (e.g. `-jar` for `java`).
20+
- `debug`: When enabled, log all startup communication with the engine.
2021
- `protocol`: Specify which protocol your engine uses. Choices are:
2122
1. `"uci"` for the [Universal Chess Interface](https://wbec-ridderkerk.nl/html/UCIProtocol.html)
2223
2. `"xboard"` for the XBoard/WinBoard/[Chess Engine Communication Protocol](https://www.gnu.org/software/xboard/engine-intf.html)

0 commit comments

Comments
 (0)