From 48027f11421e5f720ca800ba7cdd7cbdb12647c6 Mon Sep 17 00:00:00 2001 From: claude Date: Tue, 13 Oct 2026 21:28:35 +0200 Subject: [PATCH] Pipelines: the settings of a step, filled in the way Create Test fills them in A pure module that turns an engine, a preset and a dev branch into every setting a Test step needs: the repository and default network, the default preset, then the chosen one, with both_ going to dev and base alike. With it a pipeline only has to be told the dev branch, as a single test does. What no preset can fill in is reported instead of guessed. Includes the unit tests, which run without Django. --- OpenBench/pipeline_defaults.py | 227 +++++++++++++++++++++ UnitTests/test_pipeline_defaults.py | 304 ++++++++++++++++++++++++++++ 2 files changed, 531 insertions(+) create mode 100644 OpenBench/pipeline_defaults.py create mode 100644 UnitTests/test_pipeline_defaults.py diff --git a/OpenBench/pipeline_defaults.py b/OpenBench/pipeline_defaults.py new file mode 100644 index 0000000..c858ef4 --- /dev/null +++ b/OpenBench/pipeline_defaults.py @@ -0,0 +1,227 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# OpenBench is a chess engine testing framework authored by Andrew Grant. # +# # +# # +# OpenBench is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# OpenBench is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + + +# The settings of a pipeline step, filled in the way that the Create Test page +# fills them in ( see create_workload.js ): the engine, its repository and its +# default network, then the "default" preset of the engine, then the preset that +# was asked for, where a "both_" setting goes to the dev and the base alike. +# That is what lets a pipeline ask for one thing only, the dev branch, just like +# a single test does. +# +# Pure Python, like the graph module: no Django, no database. Everything that +# comes from the server is handed in, so that all of it can be tested on its own +# ( see UnitTests/test_pipeline_defaults.py ). + +import re + +from OpenBench.pipeline_graph import ALLOWED_FIELDS, CONDITIONAL_FIELDS, REQUIRED_FIELDS +from OpenBench.pipeline_graph import DEFAULT_MAX_STEPS, DEFAULT_MAX_VISITS, SCHEMA_VERSION + +# What the Create Test page starts out with, before any preset is applied +FORM_DEFAULTS = { + 'upload_pgns' : 'FALSE', 'priority' : '0', 'throughput' : '1000', + 'syzygy_wdl' : 'DISABLED', 'syzygy_adj' : 'OPTIONAL', +} + +# Only for engines whose presets say nothing about them, instead of a blank that can never run +FALLBACKS = { + 'workload_size' : '32', + 'win_adj' : 'movecount=3 score=400', + 'draw_adj' : 'movenumber=40 movecount=8 score=10', +} + +# Required settings that are allowed to be empty: "no network" +BLANK_OK = ('dev_network', 'base_network') + +# One word, no spaces or quotes. Whether it exists is for Github to say, later +BRANCH_PATTERN = re.compile(r'[^\s"\'\\\x00-\x1f\x7f]{1,255}') + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# PRESETS # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def preset_names(engine_config): + + """ The test presets of an engine, in the order that its config lists them """ + + return [name for name in (engine_config.get('test_presets') or {}) if name != 'default'] + +def _expand(settings): + + """ ( key, value ) pairs, with both_x turned into dev_x and base_x """ + + pairs = [] + + for key, value in settings.items(): + if key.startswith('both_'): + pairs.append(('dev_' + key[len('both_'):], value)) + pairs.append(('base_' + key[len('both_'):], value)) + else: + pairs.append((key, value)) + + return pairs + +def preset_settings(engine_config, preset): + + """ What choosing a preset sets, in the order that the page applies it """ + + presets = engine_config.get('test_presets') or {} + pairs = _expand(presets.get('default') or {}) + + if preset != 'default': + pairs += _expand(presets.get(preset) or {}) + + return pairs + +def time_control(engine_config, preset): + return str(dict(preset_settings(engine_config, preset)).get('dev_time_control', '')) + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# NETWORKS # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def default_network(networks, engine): + + """ The network that the page has selected for an engine: the last one flagged default """ + + chosen = '' + + for network in networks: + if network.get('engine') == engine and network.get('default'): + chosen = network.get('sha256') or '' + + return chosen + +def find_network(networks, engine, wanted): + + """ A preset may name a network, or give its hash. None when it is neither """ + + wanted = str(wanted) + + if wanted in ('', 'None'): + return '' + + found = None + + for network in networks: + if network.get('engine') == engine and wanted in (network.get('name'), network.get('sha256')): + found = network.get('sha256') or '' + + return found + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# PAYLOAD # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def clean_branch(text): + + """ The branch, or None if it cannot be one """ + + text = (text or '').strip() + return text if BRANCH_PATTERN.fullmatch(text) else None + +def test_payload(engine, engine_config, preset, dev_branch, repos=None, networks=(), books=(), base_branch=''): + + """ Returns ( payload, missing ): every setting of a Test step, as the Create + Test page would have it, and the names of the settings that nothing + could fill in. The payload may only be used when nothing is missing. """ + + repo = (repos or {}).get(engine) or engine_config.get('source') or '' + network = default_network(networks, engine) + + payload = dict(FORM_DEFAULTS) + payload.update({ + 'dev_engine' : engine, 'base_engine' : engine, + 'dev_repo' : repo, 'base_repo' : repo, + 'dev_network' : network, 'base_network' : network, + }) + + mode = 'SPRT' + + for key, value in preset_settings(engine_config, preset): + + if key in ('dev_network', 'base_network'): + value = find_network(networks, engine, value) + if value is None: continue # Not one that exists. The page keeps what it had + + # Like the page: the last of these that a preset sets decides the mode + if key == 'test_max_games': mode = 'GAMES' + if key in ('test_bounds', 'test_confidence'): mode = 'SPRT' + + payload[key] = str(value) + + for key, value in FALLBACKS.items(): + if not str(payload.get(key, '')).strip(): + payload[key] = value + + if not str(payload.get('book_name', '')).strip(): + payload['book_name'] = next(iter(books), '') + + payload['test_mode'] = mode + payload['dev_branch'] = dev_branch + + if base_branch: + payload['base_branch'] = base_branch + + unused = ('test_max_games',) if mode == 'SPRT' else ('test_bounds', 'test_confidence') + payload = { key : value for key, value in payload.items() if key in ALLOWED_FIELDS and key not in unused } + + required = REQUIRED_FIELDS['TEST'] + CONDITIONAL_FIELDS[mode] + missing = [key for key in required if key not in BLANK_OK and not str(payload.get(key, '')).strip()] + + return payload, missing + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# GRAPH # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def quick_graph(steps): + + """ steps is [ ( name, payload ) ]. Each one starts when the one before it + passed. If anything else happens, the pipeline simply ends. """ + + graph = { + 'version' : SCHEMA_VERSION, 'start' : 'step1', + 'max_visits' : DEFAULT_MAX_VISITS, 'max_steps' : DEFAULT_MAX_STEPS, + 'steps' : {}, 'edges' : [], + } + + seen = {} + + for index, (name, payload) in enumerate(steps, 1): + + key = 'step%d' % (index) + seen[name] = seen.get(name, 0) + 1 + label = name if seen[name] == 1 else '%s (%d)' % (name, seen[name]) + + graph['steps'][key] = { 'name' : label[:64], 'kind' : 'TEST', 'payload' : payload } + + if index > 1: + graph['edges'].append({ 'from' : 'step%d' % (index - 1), 'on' : 'passed', 'to' : key }) + + return graph + +def quick_name(dev_branch, names): + return ('%s: %s' % (dev_branch, ' then '.join(names)))[:128] diff --git a/UnitTests/test_pipeline_defaults.py b/UnitTests/test_pipeline_defaults.py new file mode 100644 index 0000000..95f7252 --- /dev/null +++ b/UnitTests/test_pipeline_defaults.py @@ -0,0 +1,304 @@ +#!/usr/bin/python3 + +import os +import sys +import unittest + +# Needed to include from ../OpenBench/pipeline_defaults.py, which has no Django dependencies +PARENT = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir) +sys.path.insert(0, os.path.abspath(PARENT)) + +from OpenBench.pipeline_defaults import * +from OpenBench.pipeline_graph import ALLOWED_FIELDS, REQUIRED_FIELDS, has_errors, validate_graph + +# The shape of a real engine config ( Engines/Eleanor.json ), presets only +ENGINE = { + 'source' : 'https://github.com/o/Engine', + 'test_presets' : { + 'default' : { + 'base_branch' : 'main', 'book_name' : 'UHO.epd', + 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]', + 'win_adj' : 'movecount=3 score=400', 'draw_adj' : 'movenumber=40 movecount=8 score=10', + }, + 'STC' : { 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '8.0+0.08', 'workload_size' : 32 }, + 'LTC' : { 'both_options' : 'Threads=1 Hash=64', 'both_time_control' : '40.0+0.4', 'workload_size' : 8 }, + }, +} + +NETWORKS = [ + { 'engine' : 'Engine', 'name' : 'old.nn', 'sha256' : 'aaaaaaaa', 'default' : False }, + { 'engine' : 'Engine', 'name' : 'newest.nn', 'sha256' : 'bbbbbbbb', 'default' : True }, + { 'engine' : 'Other', 'name' : 'other.nn', 'sha256' : 'cccccccc', 'default' : True }, +] + +def build(preset='STC', branch='dev', engine_config=ENGINE, **kwargs): + return test_payload('Engine', engine_config, preset, branch, **kwargs) + +def with_presets(**presets): + return { 'source' : ENGINE['source'], 'test_presets' : dict(ENGINE['test_presets'], **presets) } + + +class PresetTests(unittest.TestCase): + + def test_the_names_leave_out_the_defaults(self): + self.assertEqual(preset_names(ENGINE), ['STC', 'LTC']) + + def test_an_engine_without_presets_has_none(self): + self.assertEqual(preset_names({}), []) + self.assertEqual(preset_names({ 'test_presets' : None }), []) + + def test_both_goes_to_the_dev_and_the_base_alike(self): + payload, _ = build('LTC') + self.assertEqual((payload['dev_options'], payload['base_options']), ('Threads=1 Hash=64', 'Threads=1 Hash=64')) + self.assertEqual((payload['dev_time_control'], payload['base_time_control']), ('40.0+0.4', '40.0+0.4')) + + def test_the_defaults_come_first_and_a_preset_wins_over_them(self): + + engine = with_presets(Slow={ 'test_bounds' : '[-1.00, 3.00]', 'base_branch' : 'master', + 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' }) + payload, missing = build('Slow', engine_config=engine) + + self.assertEqual(payload['test_bounds'], '[-1.00, 3.00]') # The preset + self.assertEqual(payload['base_branch'], 'master') # The preset + self.assertEqual(payload['test_confidence'], '[0.05, 0.05]') # The default, untouched + self.assertEqual(missing, []) + + def test_the_time_control_of_a_preset_for_showing(self): + self.assertEqual((time_control(ENGINE, 'STC'), time_control(ENGINE, 'LTC')), ('8.0+0.08', '40.0+0.4')) + self.assertEqual(time_control(ENGINE, 'nope'), '') + + +class PayloadTests(unittest.TestCase): + + def test_one_branch_is_all_that_it_takes(self): + + payload, missing = build(branch='my-branch') + + self.assertEqual(missing, []) + self.assertEqual(payload['dev_branch'], 'my-branch') + self.assertEqual(payload['base_branch'], 'main') + self.assertEqual(payload['test_mode'], 'SPRT') + self.assertEqual(payload['workload_size'], '32') + + def test_it_starts_out_like_the_create_test_page(self): + + payload, _ = build() + + for key, value in FORM_DEFAULTS.items(): + self.assertEqual(payload[key], value, key) + + self.assertEqual(payload['syzygy_wdl'], 'DISABLED') # The page says disabled, whatever the editor said + self.assertEqual((payload['priority'], payload['throughput']), ('0', '1000')) + + def test_every_setting_that_a_step_needs_is_there_and_is_text(self): + + for preset in ('STC', 'LTC'): + payload, missing = build(preset) + self.assertEqual(missing, []) + + for key in REQUIRED_FIELDS['TEST'] + ['test_bounds', 'test_confidence']: + self.assertIn(key, payload, key) + + self.assertTrue(all(isinstance(value, str) for value in payload.values())) + self.assertTrue(set(payload) <= ALLOWED_FIELDS) + + def test_engine_and_repo_are_the_engines_own_unless_the_profile_says_otherwise(self): + + payload, _ = build() + self.assertEqual((payload['dev_engine'], payload['base_engine']), ('Engine', 'Engine')) + self.assertEqual((payload['dev_repo'], payload['base_repo']), ('https://github.com/o/Engine',) * 2) + + payload, _ = build(repos={ 'Engine' : 'https://github.com/me/Engine' }) + self.assertEqual((payload['dev_repo'], payload['base_repo']), ('https://github.com/me/Engine',) * 2) + + payload, _ = build(repos={ 'Another' : 'https://github.com/me/Another' }) # Not for this engine + self.assertEqual(payload['dev_repo'], 'https://github.com/o/Engine') + + payload, _ = build(repos=None) # A profile with nothing saved + self.assertEqual(payload['dev_repo'], 'https://github.com/o/Engine') + + def test_the_base_branch_can_be_chosen(self): + payload, missing = build(base_branch='release') + self.assertEqual((payload['base_branch'], missing), ('release', [])) + + def test_a_missing_base_branch_is_reported_not_guessed(self): + + engine = { 'source' : 'x', 'test_presets' : { 'STC' : ENGINE['test_presets']['STC'], + 'default' : { 'book_name' : 'b.epd', 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' } } } + + self.assertEqual(build(engine_config=engine)[1], ['base_branch']) + self.assertEqual(build(engine_config=engine, base_branch='main')[1], []) + + def test_what_the_presets_leave_out_is_reported(self): + + engine = { 'source' : 'x', 'test_presets' : { 'default' : { 'base_branch' : 'main', 'book_name' : 'b.epd', + 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' }, 'Bare' : {} } } + + payload, missing = build('Bare', engine_config=engine) + + self.assertEqual(sorted(missing), ['base_options', 'base_time_control', 'dev_options', 'dev_time_control']) + self.assertEqual(payload['workload_size'], FALLBACKS['workload_size']) # These have a standard value + self.assertEqual(payload['win_adj'], FALLBACKS['win_adj']) + + def test_an_engine_that_names_no_book_gets_the_first_one_there_is(self): + + engine = with_presets(default={ 'base_branch' : 'main', 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' }) + + self.assertEqual(build(engine_config=engine, books=['one.epd', 'two.epd'])[0]['book_name'], 'one.epd') + self.assertEqual(build(engine_config=engine)[1], ['book_name']) + + def test_an_unknown_preset_only_has_the_defaults_to_go_on(self): + _, missing = build('nope') + self.assertIn('dev_time_control', missing) + + def test_whatever_else_a_preset_holds_is_left_out(self): + + engine = with_presets(Odd={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1', 'rubbish' : 'x' }) + payload, _ = build('Odd', engine_config=engine) + + self.assertNotIn('rubbish', payload) + + def test_a_bench_that_a_preset_gives_is_kept_and_otherwise_it_is_left_to_autofill(self): + + self.assertNotIn('dev_bench', build()[0]) + + engine = with_presets(Fixed={ 'both_bench' : 12345, 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' }) + payload, _ = build('Fixed', engine_config=engine) + + self.assertEqual((payload['dev_bench'], payload['base_bench']), ('12345', '12345')) + + +class ModeTests(unittest.TestCase): + + def test_a_preset_of_fixed_games_is_a_games_test(self): + + engine = with_presets(Fixed={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1', 'test_max_games' : 40000 }) + payload, missing = build('Fixed', engine_config=engine) + + self.assertEqual((payload['test_mode'], payload['test_max_games'], missing), ('GAMES', '40000', [])) + self.assertNotIn('test_bounds', payload); self.assertNotIn('test_confidence', payload) + + def test_sprt_leaves_the_number_of_games_out(self): + + engine = with_presets(default=dict(ENGINE['test_presets']['default'], test_max_games=100)) + payload, _ = build(engine_config=engine) + + self.assertEqual(payload['test_mode'], 'GAMES') # The last word of the page, as it is for a person + self.assertEqual(build()[0]['test_mode'], 'SPRT') + self.assertNotIn('test_max_games', build()[0]) + + def test_bounds_after_games_make_it_sprt_again(self): + + engine = with_presets(Both={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1', + 'test_max_games' : 100, 'test_bounds' : '[0.00, 2.00]' }) + payload, _ = build('Both', engine_config=engine) + + self.assertEqual(payload['test_mode'], 'SPRT') + self.assertNotIn('test_max_games', payload) + + +class NetworkTests(unittest.TestCase): + + def test_the_default_network_of_the_engine(self): + self.assertEqual(build(networks=NETWORKS)[0]['dev_network'], 'bbbbbbbb') + self.assertEqual(build(networks=NETWORKS)[0]['base_network'], 'bbbbbbbb') + + def test_no_default_means_no_network(self): + self.assertEqual(build(networks=NETWORKS[:1])[0]['dev_network'], '') + self.assertEqual(build()[0]['dev_network'], '') + + def test_a_network_of_another_engine_is_never_used(self): + self.assertEqual(default_network(NETWORKS, 'Nothing'), '') + self.assertEqual(default_network(NETWORKS, 'Other'), 'cccccccc') + + def test_the_last_default_wins_like_it_does_on_the_page(self): + twice = NETWORKS + [{ 'engine' : 'Engine', 'name' : 'later.nn', 'sha256' : 'dddddddd', 'default' : True }] + self.assertEqual(default_network(twice, 'Engine'), 'dddddddd') + + def test_a_preset_may_name_a_network_or_give_its_hash(self): + + for wanted in ('old.nn', 'aaaaaaaa'): + engine = with_presets(Net={ 'both_network' : wanted, 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' }) + self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], 'aaaaaaaa', wanted) + + def test_a_preset_may_ask_for_none(self): + engine = with_presets(Net={ 'both_network' : 'None', 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' }) + self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], '') + + def test_a_network_that_does_not_exist_changes_nothing(self): + engine = with_presets(Net={ 'both_network' : 'ghost.nn', 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' }) + self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], 'bbbbbbbb') + + def test_having_no_network_is_not_a_missing_setting(self): + self.assertEqual(build(networks=[])[1], []) + + +class BranchTests(unittest.TestCase): + + def test_what_a_branch_may_look_like(self): + + for text in ('main', 'feature/new-search_2', 'v1.2.3', 'a' * 255, '0' * 40, ' padded '): + self.assertEqual(clean_branch(text), text.strip(), text) + + def test_what_it_may_not(self): + + for text in (None, '', ' ', 'two words', 'tab\there', 'quote"d', "it's", 'back\\slash', 'a' * 256, 'new\nline'): + self.assertIsNone(clean_branch(text), repr(text)) + + +class GraphTests(unittest.TestCase): + + def steps(self, *presets): + return [(preset, build(preset)[0]) for preset in presets] + + def test_a_graph_of_one_step_is_sound(self): + + graph = quick_graph(self.steps('STC')) + + self.assertFalse(has_errors(validate_graph(graph))) + self.assertEqual((graph['start'], list(graph['steps']), graph['edges']), ('step1', ['step1'], [])) + + def test_the_next_one_starts_when_the_one_before_passed(self): + + graph = quick_graph(self.steps('STC', 'LTC')) + issues = validate_graph(graph) + + self.assertFalse(has_errors(issues)) + self.assertEqual([issue['level'] for issue in issues], ['info', 'info']) # Only "it ends here if it fails or stops" + self.assertEqual(graph['edges'], [{ 'from' : 'step1', 'on' : 'passed', 'to' : 'step2' }]) + self.assertEqual([step['name'] for step in graph['steps'].values()], ['STC', 'LTC']) + + def test_any_number_of_steps_make_a_chain(self): + + graph = quick_graph(self.steps('STC', 'LTC', 'STC')) + + self.assertFalse(has_errors(validate_graph(graph))) + self.assertEqual([(e['from'], e['to']) for e in graph['edges']], [('step1', 'step2'), ('step2', 'step3')]) + + def test_the_same_name_twice_is_told_apart(self): + graph = quick_graph(self.steps('STC', 'STC')) + self.assertEqual([step['name'] for step in graph['steps'].values()], ['STC', 'STC (2)']) + + def test_a_long_name_is_cut_to_what_a_step_may_have(self): + graph = quick_graph([('x' * 100, build()[0])]) + self.assertEqual(len(graph['steps']['step1']['name']), 64) + self.assertFalse(has_errors(validate_graph(graph))) + + def test_each_step_has_settings_of_its_own(self): + + graph = quick_graph(self.steps('STC', 'LTC')) + first, second = graph['steps']['step1']['payload'], graph['steps']['step2']['payload'] + + self.assertEqual((first['dev_time_control'], second['dev_time_control']), ('8.0+0.08', '40.0+0.4')) + self.assertEqual((first['dev_branch'], second['dev_branch']), ('dev', 'dev')) + + def test_the_name_says_what_is_tested_and_what_runs(self): + self.assertEqual(quick_name('feature/x', ['STC', 'LTC']), 'feature/x: STC then LTC') + self.assertEqual(quick_name('feature/x', ['STC']), 'feature/x: STC') + + def test_the_name_always_fits(self): + self.assertEqual(len(quick_name('b' * 255, ['STC', 'LTC'])), 128) + + +if __name__ == '__main__': + unittest.main()