Worker architecture¶
Overview¶
The worker is a standalone Python application (Python >= 3.8) that runs on contributor machines. It fetches testing tasks from the fishtest server, compiles Stockfish from source, runs games via fastchess, and reports results back.
Source files:
| File | Purpose |
|---|---|
worker/worker.py |
Main control loop, configuration, signal handling, heartbeat |
worker/games.py |
Engine compilation, game execution, fastchess output parsing |
worker/updater.py |
Self-update mechanism |
worker/sri.txt |
SRI hashes of the three files above, checked against the server's copy |
The worker writes its configuration to worker/fishtest.cfg on first start;
the file is not part of the repository.
Constants¶
| Constant | Value | Location |
|---|---|---|
WORKER_VERSION |
the protocol version the server compares | worker.py |
FASTCHESS_SHA |
the fastchess commit the worker builds | worker.py |
HTTP_TIMEOUT |
30.0 s | worker.py, games.py |
INITIAL_RETRY_TIME |
15.0 s | worker.py |
MAX_RETRY_TIME |
900 s (15 min) | worker.py |
THREAD_JOIN_TIMEOUT |
15.0 s | worker.py |
UPDATE_RETRY_TIME |
15.0 s | games.py |
FASTCHESS_KILL_TIMEOUT |
15.0 s | games.py |
MIN_GCC_MAJOR.MINOR |
10.2 | worker.py |
MIN_CLANG_MAJOR.MINOR |
11.0 | worker.py |
Control flow¶
High-level worker control flow:
flowchart TD
start[worker] --> init[Setup parameters and verify worker]
init --> toolchain[Verify toolchain and setup fastchess]
toolchain --> heartbeat[Start heartbeat thread]
heartbeat --> fetch[fetch_and_handle_task]
fetch --> assigned{Task assigned}
assigned -- No --> retry[Wait and retry] --> fetch
assigned -- Yes --> prep[Build engines and stage inputs]
prep --> batch[Run fastchess batch]
batch --> report[POST /api/update_task]
report --> more{More games needed}
more -- Yes --> batch
more -- No --> upload[POST /api/upload_pgn unless SPSA] --> fetch
prep -- Failure --> stop[POST /api/failed_task or /api/stop_run]
batch -- Failure --> stop
stop --> upload
worker() -- entry point¶
- Acquire a file lock (
fishtest_worker.lock) to prevent multiple instances in the same directory. - Install signal handlers (SIGINT, SIGTERM, SIGQUIT, SIGBREAK).
- Call
setup_parameters()to read/validatefishtest.cfg, probe hardware, parse CLI arguments, validate credentials, and write config back. - Write SRI hashes (
sri.txt). - If
--only_configwas passed, exit. - Create
testing/. - Verify the worker version with the server (may trigger self-update).
- Verify the toolchain (
make,strip). - Build fastchess from source if not already cached, and run its tests.
- Verify worker integrity via remote SRI comparison.
- Assemble
worker_info(username, concurrency, compiler, UUID, and theworker_archthat Stockfish'sget_native_properties.shreports). - Start the heartbeat thread (daemon).
- Enter the main loop: call
fetch_and_handle_task()repeatedly, and stop whenfish.exitexists in the worker directory.
fetch_and_handle_task()¶
- Re-verify worker version (may trigger self-update).
- Clean up old files in
testing/. - Check remaining GitHub API calls.
- POST
/api/request_taskto get a task assignment. - If a task is assigned, call
run_games(). - On exception, POST
/api/stop_runfor aRunExceptionand/api/failed_taskfor any other. - For a run that is not SPSA, upload the recorded games via POST
/api/upload_pgnwhen the PGN file is non-empty and its CRC matches, whether the task finished or failed.
run_games()¶
- Set up
resultdict with existing stats (supports resume). - Download the opening book if missing or corrupted (SRI check).
- Build both
newandbaseengines from source viasetup_engine(). - Download neural networks via
establish_validated_net(). - Verify engine bench signatures.
- Compute NPS, derive CPU scaling factor.
- Reject if the machine is too slow.
- Adjust time control based on CPU scaling factor.
- Construct the fastchess command line.
- Call
launch_fastchess()in a loop (one iteration per batch for SPSA, one call for SPRT/NumGames).
launch_fastchess()¶
- For SPSA: POST
/api/request_spsato get tuning parameters. - Insert SPSA option values into the fastchess command line (stochastic rounding).
- Start fastchess as a subprocess.
- Call
parse_fastchess_output()to monitor stdout/stderr. - On exit, send SIGINT to fastchess and wait for graceful shutdown.
parse_fastchess_output()¶
- Read fastchess stdout/stderr via background threads feeding a queue.
- Parse WLD and pentanomial results from fastchess output blocks.
- Detect crashes, time losses, and fastchess errors.
- After each batch of games, POST
/api/update_taskwith accumulated stats. - Respect
task_aliveflag from server response. - Enforce a time limit (
tc_limit).
Heartbeat¶
A daemon thread sends POST /api/beat once 120 seconds pass without a
successful update_task or beat, and skips the beat while no task is
assigned. If the server responds with task_alive: false, the current task
is abandoned.
Signal handling¶
| Signal | Behavior |
|---|---|
| SIGINT | Set current_state["alive"] = False, raise FatalException |
| SIGTERM | Same as SIGINT |
| SIGQUIT | Same as SIGINT (Unix only) |
| SIGBREAK | Same as SIGINT (Windows only) |
A fish.exit file in the worker directory stops the worker. It is checked
after every successful update_task, which ends the task with
WorkerException("Task stopped by 'fish.exit'.") and posts failed_task,
and after every task. The worker removes the file on exit.
Configuration file¶
File: worker/fishtest.cfg -- INI format, managed by ConfigParser.
Sections and options¶
[login]
username = myuser
password = mypassword
[parameters]
protocol = https ; http or https
host = tests.stockfishchess.org
port = 443
concurrency = max(1,min(3,MAX-1)) ; expression using MAX = cpu_count
max_memory = MAX/2 ; expression using MAX = total_ram_MiB
uuid_prefix = _hw ; _hw = hardware-derived, or alphanumeric
min_threads = 1 ; reject tasks with fewer threads
fleet = False ; True = quit on error or empty queue
global_cache = ; shared cache path for multi-worker setups
compiler = g++ ; g++ or clang++
[private]
hw_seed = 3418512882 ; random seed for UUID derivation
The concurrency and max_memory fields accept expressions with MAX as a
variable and min/max as functions. They are evaluated at startup.
On every start the worker also:
- sets the port to 80 for
httpon 443, and to 443 forhttpson 80; - lowers
concurrencyuntil an STC test fits inmax_memory, and refuses to start whenmax_memorycannot hold one; - removes sections and options it does not know.
Every [parameters] option has a command line flag (e.g., --concurrency,
--max_memory, --fleet), and the [login] values are the positional
USERNAME PASSWORD. [private] hw_seed has no flag. A flag's value is
written back to fishtest.cfg, so it persists to later starts.
Command line flags¶
Usage: python worker.py [USERNAME PASSWORD] [OPTIONS]
Each flag defaults to the value in fishtest.cfg; the defaults below are
those of a first start.
| Flag | Short | Type | Default | Description |
|---|---|---|---|---|
--protocol |
-P |
{http,https} |
https |
Protocol for server communication |
--host |
-n |
string | tests.stockfishchess.org |
Server hostname |
--port |
-p |
int | 443 |
Server port |
--concurrency |
-c |
expression | max(1,min(3,MAX-1)) |
Max cores to use (MAX = cpu_count) |
--max_memory |
-m |
expression | MAX/2 |
Max memory in MiB (MAX = total RAM) |
--uuid_prefix |
-u |
string | _hw |
UUID prefix (_hw = hardware-derived) |
--min_threads |
-t |
int | 1 |
Reject tasks with fewer threads |
--fleet |
-f |
{False,True} |
False |
Quit on error or empty queue |
--global_cache |
-g |
path | (empty) | Shared cache directory for multi-worker setups |
--compiler |
-C |
a detected compiler | g++ when usable, else the first detected |
Compiler for engine builds |
--only_config |
-w |
flag | -- | Write config and SRI hashes, then exit |
--no_validation |
-v |
flag | -- | Skip username/password validation with server |
The concurrency and max_memory flags accept expressions using MAX as
a variable and min/max as functions.
Self-update mechanism¶
When verify_worker_version() learns from the server that a newer version
exists:
updater.py:update()downloads the master branch zip from GitHub.- Extracts the
worker/directory to a temp folder. - Verifies SRI hashes of the downloaded files.
- Replaces the local worker files with the new ones.
- Renames
testing/to_testing_<timestamp>and migrates cached files. - Calls
do_restart()which usesos.execv()to replace the current process with a fresh invocation.
Engine build pipeline¶
setup_engine() in games.py:
- Check if a cached engine binary exists in
testing/(keyed by SHA + compiler version + environment hash). - If cached and healthy (verified by running bench), return the cached path.
- Otherwise, download the source zip from GitHub (with global cache support).
- Extract, download default neural networks from source headers.
- Determine the best CPU architecture target via
find_arch(). - Run
make profile-build(ormake buildfor Apple Silicon). - Strip the binary.
- Move to
testing/with the canonical name.
Compiler detection¶
detect_compilers() probes g++ and clang++ by running them with
-E -dM - and parsing version macros. It rejects:
- g++ < 10.2
- clang++ < 11.0
- clang++ masquerading as g++
- clang++ without
llvm-profdata
UUID generation¶
Each worker instance gets a unique key:
uuid_prefix[:8] + uuid4()[8:]
When uuid_prefix = _hw, the prefix is derived from:
hw_seed XOR fingerprint(machine_id) XOR fingerprint(worker_path)
Machine ID sources: /etc/machine-id, then /var/lib/dbus/machine-id
(Linux), registry MachineGuid (Windows), ioreg IOPlatformExpertDevice
(macOS).
Fleet mode¶
When fleet = True, the worker exits immediately if:
- No tasks are available
- An error occurs during task execution
- The server is unreachable
This allows fleet orchestrators to spin workers up/down based on queue depth.
Global cache¶
When global_cache points to an existing directory, multiple workers on the
same machine share downloaded artifacts (source zips, fastchess zips, neural
networks). Writes use atomic link() to avoid partial-file races.
File management¶
trim_files() runs before each task to clean up old files in testing/:
| Pattern | Keep | Expiration |
|---|---|---|
fastchess |
1 | never |
stockfish-*-old |
0 | immediately, during a self-update only |
stockfish-* |
50 | 30 days |
nn-*.nnue |
10 | 30 days |
results-*.pgn |
10 | 30 days |
*.epd |
4 | 365 days |
*.pgn |
4 | 365 days |
Files are sorted by access time; the most recently accessed are preserved.
API endpoints used by the worker¶
Every fishtest endpoint except /api/nn/{id}, a plain GET, takes a
JSON-encoded POST body with password and worker_info fields. Responses
are JSON dicts that may contain an error key.
Fishtest server endpoints¶
| Endpoint | Method | Phase | Purpose |
|---|---|---|---|
/api/request_version |
POST | Setup | Check worker version, trigger update |
/api/request_task |
POST | Setup | Request a task assignment |
/api/nn/{id} |
GET | Setup | Download a neural network file |
/api/update_task |
POST | Main loop | Report game results (batch updates) |
/api/request_spsa |
POST | Main loop | Get SPSA tuning parameters |
/api/beat |
POST | Heartbeat | Keep the task alive after 120 s without an update |
/api/failed_task |
POST | Finish | Report task failure |
/api/stop_run |
POST | Finish | Request early run termination |
/api/upload_pgn |
POST | Finish | Upload compressed PGN game records |
/api/worker_log |
POST | Main loop | Log a fastchess warning on the server |
External endpoints¶
| Endpoint | Method | Purpose |
|---|---|---|
https://api.github.com/rate_limit |
GET | Check remaining API quota |
https://api.github.com/repos/Disservin/fastchess/zipball/{sha} |
GET | Download fastchess source |
https://api.github.com/repos/{user}/{repo}/zipball/{sha} |
GET | Download engine source |
https://raw.githubusercontent.com/official-stockfish/books/master/<book>.zip |
GET | Download an opening book |
https://api.github.com/repos/official-stockfish/books/contents/<book>.zip?ref=master |
GET | Download an opening book (fallback) |
https://github.com/official-stockfish/fishtest/archive/master.zip |
GET | Download the worker for a self-update |
Exception hierarchy¶
| Exception | Effect |
|---|---|
FatalException |
Post failed_task, upload the games, stop the worker |
RunException |
Stop the current run (POST /api/stop_run), continue to next |
WorkerException |
Fail the current task, continue to next |
| any other exception | Post failed_task with the traceback, stop the worker |
WorkerException.__new__ forwards existing WorkerException subclass
instances (including FatalException) unchanged, preventing exception type
downgrade during re-wrapping.
Logging¶
The worker writes api.log in its install directory, beside worker.py.
Each API call records the server-side and worker-side latency, and each task
start records the run, the task and its size:
2025-01-15 12:00:00.123456+00:00 : 1.23 ms (s) 45.67 ms (w) https://tests.stockfishchess.org:443/api/update_task
On self-update, the log is rotated to api.log.previous.
Developer: regenerating SRI hashes¶
When modifying SRI-monitored files (worker.py, games.py, updater.py),
the sri.txt file must be regenerated before committing. Use the
--only_config flag to write the config and update SRI hashes without
starting the worker:
The dummy a a arguments satisfy the positional username/password
parameters. --no_validation skips server contact. The command needs a
usable compiler (g++ >= 10.2 or clang++ >= 11.0), writes updated hashes to
worker/sri.txt and worker/fishtest.cfg, and exits. Delete the generated
fishtest.cfg afterwards: the worker tests expect none.