"""What task-bench's four kinds of task share: a planted bug, a feature to build, a data source to add and a shipped feature to rebuild. They share 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 checked_tree, Containers, ran_tests from failure import Failure from git_cli.git_repo import Git from mutate_changes.tool_process import Ran # A bench's two sides: the commit before the change and the one after it. Side = typing.Literal["base", "head"] SIDES: tuple[Side, Side] = ("base", "head") # 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 how a failure names a side's tree before the bench puts a task there, as in `without a planted bug`.""" noun: str prompt: str without: str @property def key(self) -> str: """The noun as results.json and a run's file name spell it, without a space.""" return self.noun.replace(" ", "-") 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") DATA_SOURCE = Kind("data source", "Add the data source below to this project.\n\n{told}", "a data source's acceptance tests") # What Haiku is asked is a feature's prompt: the feature is new to the tree Haiku gets. SHIPPED = Kind("shipped feature", FEATURE.prompt, "a shipped feature taken out") # 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. # Such a panel picked `data-sources` the same day, 3 to 0, over `sources` and `imports`, which read as source code and as import # statements. Such a panel picked `shipped-features` on 2026-11-24, 3 to 0, over `shipped`, `rebuilds`, `reverts` and `history`: # it says what was shipped, and stands apart from the features someone wrote for the bench. KINDS = {"bugs": BUG, "features": FEATURE, "data-sources": DATA_SOURCE, "shipped-features": SHIPPED} @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 the acceptance tests of a feature or a data source; each is None in a task of another 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.""" kind: Kind runs: int @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, a line for its report on each thing it left out, and what drew the tasks, as results.json records it: the seed for bugs. Features come as their folder holds them, so for them it is empty.""" tasks: list[Task] left_out: list[str] drawn_by: dict[str, int] 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) -> Ran | Failure: """The test command's run in a copy of tree, removed afterwards, so what the tests write stays out of tree.""" 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 ran_tests(containers, str(copy)) finally: shutil.rmtree(copy, ignore_errors=True) def check_made(tree: str, copy: str, check: str, acceptance: str | None) -> None: """Makes at check the tree that checks what was built in copy, a copy of tree: `checked_tree`'s, then a feature's acceptance tests over it, where acceptance names their folder. A run of Haiku's and a feature's reference patch are checked alike. Only a task without acceptance tests has its Rust test modules kept as the tree has them: a feature's change may update such a module. Raises OSError where a file cannot be copied.""" checked_tree(tree, copy, check, rust_tests_planted=not acceptance) if acceptance: with_acceptance(check, acceptance) 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)