diff --git a/bench/candidate_methods.py b/bench/candidate_methods.py index 620e2517..d0315e50 100644 --- a/bench/candidate_methods.py +++ b/bench/candidate_methods.py @@ -76,14 +76,38 @@ DEFAULT_OUTPUT = Path("bench/results/candidate-methods-framework-live.json") +def _examples_dir() -> Path: + return Path(__file__).resolve().parent.parent / "examples" + + +def algorithm_source_dir( + name: str, examples_dir: Optional[Path] = None +) -> Path: + """Return ``examples/``, refusing a key that is not a directory. + + ``source_fingerprint`` globs ``*.py`` under this folder. A missing + directory would contribute no paths and no error, so a renamed port + would stop invalidating cached results. + """ + root = _examples_dir() if examples_dir is None else examples_dir + path = root / name + if not path.is_dir(): + raise FileNotFoundError( + f"algorithm {name!r} names no folder at {path}" + ) + return path + + def source_fingerprint() -> str: """Hash every module defining this matrix: the shared examples runtime, each method's folder, and this file. Generated results are excluded.""" - examples_dir = Path(__file__).resolve().parent.parent / "examples" + examples_dir = _examples_dir() paths = [Path(__file__).resolve()] paths += sorted(examples_dir.glob("_*.py")) for name in ALGORITHMS: - paths += sorted((examples_dir / name).glob("*.py")) + paths += sorted( + algorithm_source_dir(name, examples_dir=examples_dir).glob("*.py") + ) digest = hashlib.sha256() for path in paths: digest.update(path.name.encode("utf-8")) diff --git a/changelog.d/1283-algorithm-folder-check.md b/changelog.d/1283-algorithm-folder-check.md new file mode 100644 index 00000000..3eb7a1fd --- /dev/null +++ b/changelog.d/1283-algorithm-folder-check.md @@ -0,0 +1,5 @@ +Fixed +`source_fingerprint` now refuses an `ALGORITHMS` key that names no +`examples//` directory, instead of silently hashing nothing for +that port, and a test pins each `PROPOSAL_CALLS_PER_CANDIDATE` entry +to the policy's reservation (#1283). diff --git a/tests/test_candidate_algorithm_folders.py b/tests/test_candidate_algorithm_folders.py new file mode 100644 index 00000000..6dfe30a3 --- /dev/null +++ b/tests/test_candidate_algorithm_folders.py @@ -0,0 +1,58 @@ +"""An ALGORITHMS key must name a real examples folder, and the dry-run +reservation must match the policy each port actually builds. + +source_fingerprint globs examples//*.py. A key that names no +directory would contribute no paths and no error, so editing that port +would stop changing the fingerprint. The first tests fail that mutation +from the filesystem alone. The reservation test compares +PROPOSAL_CALLS_PER_CANDIDATE to builder(0), so a literal that drifts +from the policy fails here too. +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +from bench.candidate_methods import ( + ALGORITHMS, + PROPOSAL_CALLS_PER_CANDIDATE, + algorithm_source_dir, + source_fingerprint, +) + +EXAMPLES_DIR = Path(__file__).resolve().parents[1] / "examples" + + +def test_algorithm_source_dir_refuses_a_key_that_names_no_folder(tmp_path): + with pytest.raises(FileNotFoundError, match="names no folder"): + algorithm_source_dir("missing-port", examples_dir=tmp_path) + + +def test_algorithm_source_dir_returns_an_existing_folder(tmp_path): + folder = tmp_path / "promptbreeder" + folder.mkdir() + assert algorithm_source_dir("promptbreeder", examples_dir=tmp_path) == folder + + +def test_every_algorithm_key_names_an_examples_folder(): + for name in ALGORITHMS: + path = algorithm_source_dir(name) + assert path.is_dir(), name + assert path == EXAMPLES_DIR / name + + +def test_source_fingerprint_refuses_a_key_that_names_no_folder(monkeypatch): + monkeypatch.setitem( + ALGORITHMS, "missing-port", ("mechanism_microport", lambda seed: None) + ) + with pytest.raises(FileNotFoundError, match="names no folder"): + source_fingerprint() + + +def test_proposal_calls_constant_matches_each_policy(): + for name, (_, builder) in ALGORITHMS.items(): + assert PROPOSAL_CALLS_PER_CANDIDATE[name] == ( + builder(0).proposal_calls_per_candidate + ), name