"""What both kinds of task-bench's tasks share, a planted bug and a feature to build: the task itself, the bench it runs in, and the steps that commit a task's tree and run the tests over a copy of one.""" import dataclasses import pathlib import shutil import typing from task_bench.runs import Containers, ran_tests from failure import Failure from git_cli.git_repo import Git from mutate_changes.tool_process import Ran from task_bench.figures import Side # The one commit of a task's tree doesn't name anyone, and neither a hook nor a signing key of the caller's reaches it. COMMIT = ["-c", "user.name=task-bench", "-c", "user.email=task-bench@localhost", "-c", "commit.gpgsign=false", "-c", "core.hooksPath=/dev/null", "commit", "--quiet", "--message", "Import"] class Kind(typing.NamedTuple): """A kind of task: what the report calls one, what Haiku is asked, with the task's own text for `{told}`, and what a side's tree is without one.""" noun: str prompt: str without: str BUG = Kind("bug", "A test of this project fails with the message below. Find the bug in the code and fix it. Change no test.\n\n{told}", "a planted bug") FEATURE = Kind("feature", "Build the feature below in this project.\n\n{told}", "a feature's acceptance tests") # By the value --tasks takes. A panel of three readers, seated as the README's audience, picked `--tasks features` on 2026-11-23, # 3 to 0, over a bare `--features` and a `features` subcommand: it names what the bench runs, and the bugs as its other value. KINDS = {"bugs": BUG, "features": FEATURE} @dataclasses.dataclass(frozen=True) class Task: """A task for Haiku: its number from 1, its name, and per side the tree it starts in and what Haiku is told there. file is the file a bug is in, and acceptance the folder of a feature's acceptance tests; each is None in a task of the other kind.""" number: int name: str trees: dict[Side, str] told: dict[Side, str] file: str | None = None acceptance: str | None = None class Asked(typing.NamedTuple): """What the caller asked of a bench: the kind of its tasks and how often to run each. For bugs also the seed that orders the mutants and how many bugs to plant; both are None for features, which the bench takes as their folder holds them.""" kind: Kind runs: int seed: int | None = None bugs: int | None = None @dataclasses.dataclass(frozen=True) class Bench: """What a bench's phases share: the checkout, per side its commit and its tree without a task, what the caller asked, the containers, the bench's folder, git, and say, which tells the caller the bench's progress.""" checkout: str sides: dict[Side, str] trees: dict[Side, str] asked: Asked containers: Containers folder: pathlib.Path git: Git say: typing.Callable[[str], None] class Planned(typing.NamedTuple): """The tasks a bench runs, and a line for its report on each thing it left out.""" tasks: list[Task] left_out: list[str] def on_disk[T](doing: str, act: typing.Callable[[], T]) -> T | Failure: """act's answer, or a Failure that says what could not be done where the filesystem refuses act.""" try: return act() except OSError as failure: return Failure(f"could not {doing}: {failure}") def committed(git: Git, tree: str) -> Failure | None: """Commits all of tree once, in a repository of its own, so git doesn't show Haiku what the bench put there.""" for args in (["init", "--quiet"], ["add", "--all", "--force"], COMMIT): done = git(*args, cwd=tree) if isinstance(done, Failure): return done return None def tests_in_copy(containers: Containers, tree: str, folder: pathlib.Path, prepare: typing.Callable[[str], Failure | None] = lambda copy: None) -> Ran | Failure: """The test command's run in a copy of tree, removed afterwards, so what the tests write stays out of tree. prepare changes the copy before the tests run; where it fails, they don't run.""" copy = folder / "tested" copied = on_disk(f"copy {tree} to test it", lambda: shutil.copytree(tree, copy, symlinks=True)) if isinstance(copied, Failure): return copied try: return prepare(str(copy)) or ran_tests(containers, str(copy)) finally: shutil.rmtree(copy, ignore_errors=True) def with_acceptance(tree: str, acceptance: str) -> None: """Puts a feature's acceptance tests into tree, each file at its path under `acceptance`, over any file the tree holds there. Raises OSError where a file cannot be copied.""" shutil.copytree(acceptance, tree, symlinks=True, dirs_exist_ok=True)