Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions bench/candidate_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>``, 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"))
Expand Down
5 changes: 5 additions & 0 deletions changelog.d/1283-algorithm-folder-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed
`source_fingerprint` now refuses an `ALGORITHMS` key that names no
`examples/<key>/` 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).
58 changes: 58 additions & 0 deletions tests/test_candidate_algorithm_folders.py
Original file line number Diff line number Diff line change
@@ -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/<key>/*.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
Loading