"""The features task-bench has Haiku build: each a folder in the project's `bench-tasks` folder, with a spec, a reference patch that builds it, and acceptance tests Haiku never sees. A feature counts at the two commits of a bench only where those tests fail at both before it is built, and pass at both with the reference patch.""" import itertools import pathlib import shutil import typing from task_bench.tasks import Bench, check_made, committed, on_disk, Planned, Task from project_config.settings import BENCH_TASKS, CONFIG from failure import Failure from project_config.main import located from mutate_changes.tool_process import Ran from task_bench.runs import ran_tests, TimedOut from task_bench.figures import Side, SIDES SPEC = "spec.md" PATCH = "reference.patch" ACCEPTANCE = "acceptance" class Feature(typing.NamedTuple): """A feature as its folder holds it: the folder's name, the spec Haiku is told, the reference patch's file, and the folder of the acceptance tests, each at the path it takes from the tree's top.""" name: str spec: str patch: str acceptance: str class NotApplied(Failure): """The reference patch doesn't apply to the tree.""" class Features(typing.NamedTuple): """A project's features, found in the folder that keeps them.""" folder: pathlib.Path found: list[Feature] def read_features(home: pathlib.Path) -> Features | Failure: """The features of the current directory's project, read from the folder its config names.""" folder = features_folder(home) if isinstance(folder, Failure): return folder found = features(folder) return found if isinstance(found, Failure) else Features(folder, found) def features_folder(home: pathlib.Path) -> pathlib.Path | Failure: """The folder the config of the current directory's project names as `bench-tasks`.""" config = located(None, home) if isinstance(config, Failure): return config return config.paths.get(BENCH_TASKS) or Failure(f"{config.folder / CONFIG} sets no `{BENCH_TASKS}`, the folder of the features to bench, as `project-config --help` describes it") def features(folder: pathlib.Path) -> list[Feature] | Failure: """The features that folder holds, one in each folder there, in the order of their names. A Failure where it holds none, or one lacks a part.""" if not folder.is_dir(): return Failure(f"could not read the features in {folder}: it is no folder") try: found = [feature_in(inside) for inside in sorted(folder.iterdir()) if inside.is_dir()] except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read the features in {folder}: {failure}") if not found: return Failure(f"{folder} holds no feature: a folder for each, with {SPEC}, {PATCH} and the acceptance tests under {ACCEPTANCE}") return next((feature for feature in found if isinstance(feature, Failure)), None) or [feature for feature in found if not isinstance(feature, Failure)] def feature_in(folder: pathlib.Path) -> Feature | Failure: """The feature that folder holds. Raises OSError or UnicodeDecodeError where a part cannot be read.""" spec, patch, acceptance = folder / SPEC, folder / PATCH, folder / ACCEPTANCE for part in (spec, patch): if not part.is_file(): return Failure(f"the feature in {folder} lacks {part.name}") told = spec.read_text(encoding="utf-8").strip() if not told: return Failure(f"the feature in {folder} has an empty {SPEC}") if not acceptance.is_dir() or not any(inside.is_file() for inside in acceptance.rglob("*")): return Failure(f"the feature in {folder} holds no file under {ACCEPTANCE}") return Feature(folder.name, told, str(patch), str(acceptance)) def feature_tasks(bench: Bench, found: list[Feature], folder: pathlib.Path) -> Planned | Failure: """The task of building each feature of found that counts at the bench's commits, numbered from 1, and a line on each that doesn't. A Failure where none counts. folder is where the features are kept.""" trees = committed_trees(bench) if isinstance(trees, Failure): return trees tasks: list[Task] = [] passed_over: list[tuple[str, str]] = [] for feature in found: why = why_passed_over(bench, trees, feature) if isinstance(why, Failure): return why if why is None: tasks.append(Task(len(tasks) + 1, feature.name, trees, dict.fromkeys(SIDES, feature.spec), acceptance=feature.acceptance)) else: passed_over.append((feature.name, why)) bench.say(f"task-bench: passed over the feature {feature.name}: {why}.") if not tasks: return Failure(f"None of the {len(found)} features in {folder} counts. {' '.join(f'{name}: {why}.' for name, why in passed_over)}") return Planned(tasks, [f"Passed over the feature {name}: {why}." for name, why in passed_over], {}) def committed_trees(bench: Bench) -> dict[Side, str] | Failure: """A copy of each side's tree, committed once. Every feature starts from it, since a feature doesn't add anything to the tree Haiku works in.""" trees: dict[Side, str] = {} for side in SIDES: tree = bench.folder / "features" / side copied = on_disk(f"copy the {side}'s tree to {tree}", lambda: shutil.copytree(bench.trees[side], tree, symlinks=True)) if isinstance(copied, Failure): return copied if failure := committed(bench.git, str(tree)): return failure trees[side] = str(tree) return trees def why_passed_over(bench: Bench, trees: dict[Side, str], feature: Feature) -> str | Failure | None: """Why feature doesn't count at the first side where it doesn't, in the committed trees; None where it counts at both. A Failure where the tests could not run.""" for side, is_built in itertools.product(SIDES, (False, True)): ran = checked_run(bench, trees[side], feature, is_built) if isinstance(ran, NotApplied): return f"its reference patch doesn't apply at the {side}" if isinstance(ran, TimedOut): return f"the tests run out of time at the {side}" if isinstance(ran, Failure): return ran if not is_built and ran.code == 0: return f"its acceptance tests pass at the {side} before it is built" if is_built and ran.code: return f"the tests fail with its reference patch at the {side}" return None def checked_run(bench: Bench, tree: str, feature: Feature, is_built: bool) -> Ran | Failure: """The test command's run over the tree that checks a copy of tree, with the reference patch applied to the copy where is_built. It is checked as a run of Haiku's is, so a patch that leans on a test or `testdata` file of its own fails, as a build would. Both trees are removed afterwards.""" copy, check = bench.folder / "copy", bench.folder / "check" try: copied = on_disk(f"copy {tree} to check {feature.name} in it", lambda: shutil.copytree(tree, copy, symlinks=True)) if isinstance(copied, Failure): return copied # git has run in this bench already, so a failure here is the patch's. applied = bench.git("apply", feature.patch, cwd=str(copy)) if is_built else None if isinstance(applied, Failure): return NotApplied(applied.reason) made = on_disk(f"make the tree that checks {feature.name}", lambda: check_made(tree, str(copy), str(check), feature.acceptance)) return made or ran_tests(bench.containers, str(check)) finally: shutil.rmtree(copy, ignore_errors=True) shutil.rmtree(check, ignore_errors=True)