Bench adding a data source in task-bench, from a register a seeded script generates

This commit is contained in:
2026-11-23 18:48:03 +01:00
parent f6b180fd9d
commit c45472397e
12 changed files with 320 additions and 56 deletions
+54 -29
View File
@@ -1,18 +1,23 @@
"""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."""
at both before it is built, and pass at both with the reference patch.
A data source Haiku adds is such a folder too, with a `generate-register` beside the three parts: the script that writes the
register Haiku imports, as task_bench.registers runs it. It counts as a feature does, in trees that hold its register."""
import itertools
import os
import pathlib
import shutil
import typing
from task_bench.tasks import Bench, check_made, committed, on_disk, Planned, Task
from task_bench.tasks import Bench, check_made, committed, DATA_SOURCE, FEATURE, Kind, 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
from task_bench.registers import GENERATE, NotGenerated, registered_trees
SPEC = "spec.md"
PATCH = "reference.patch"
@@ -21,12 +26,14 @@ 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."""
of the acceptance tests, each at the path it takes from the tree's top. register is the script that generates a data
source's register, and None in a feature."""
name: str
spec: str
patch: str
acceptance: str
register: str | None = None
class NotApplied(Failure):
@@ -40,12 +47,13 @@ class Features(typing.NamedTuple):
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."""
def read_features(home: pathlib.Path, kind: Kind) -> Features | Failure:
"""The features of the current directory's project, or its data sources where kind is DATA_SOURCE, read from the folder its
config names."""
folder = features_folder(home)
if isinstance(folder, Failure):
return folder
found = features(folder)
found = features(folder, kind)
return found if isinstance(found, Failure) else Features(folder, found)
@@ -57,54 +65,71 @@ def features_folder(home: pathlib.Path) -> pathlib.Path | Failure:
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."""
def features(folder: pathlib.Path, kind: Kind = FEATURE) -> list[Feature] | Failure:
"""The tasks of that kind that folder holds, in the order of their names: a data source in each folder there that holds a
`generate-register`, and a feature in each other. A Failure where it holds none, or one has a part at fault."""
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()]
found = [feature_in(inside, kind.noun) for inside in sorted(folder.iterdir()) if inside.is_dir() and (inside / GENERATE).exists() == (kind is DATA_SOURCE)]
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}")
parts = f"{SPEC}, {PATCH}, {GENERATE}" if kind is DATA_SOURCE else f"{SPEC}, {PATCH}"
return Failure(f"{folder} holds no {kind.noun}: a folder for each, with {parts} 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
def feature_in(folder: pathlib.Path, noun: str) -> Feature | Failure:
"""The task that folder holds, which a failure calls by noun. Raises OSError or UnicodeDecodeError where a part cannot be read."""
spec, patch, acceptance, register = folder / SPEC, folder / PATCH, folder / ACCEPTANCE, folder / GENERATE
for part in (spec, patch):
if not part.is_file():
return Failure(f"the feature in {folder} lacks {part.name}")
return Failure(f"the {noun} 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}")
return Failure(f"the {noun} 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))
return Failure(f"the {noun} in {folder} holds no file under {ACCEPTANCE}")
if not register.exists():
return Feature(folder.name, told, str(patch), str(acceptance))
if not register.is_file() or not os.access(register, os.X_OK):
return Failure(f"the {noun} in {folder} has a {GENERATE} that is no executable file")
return Feature(folder.name, told, str(patch), str(acceptance), str(register))
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
shared = committed_trees(bench)
if isinstance(shared, Failure):
return shared
noun = bench.asked.kind.noun
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:
trees = counted_trees(bench, shared, feature)
if isinstance(trees, Failure):
return trees
if not isinstance(trees, str):
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}.")
passed_over.append((feature.name, trees))
bench.say(f"task-bench: passed over the {noun} {feature.name}: {trees}.")
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], {})
return Failure(f"None of the {len(found)} {noun}s in {folder} counts. {' '.join(f'{name}: {why}.' for name, why in passed_over)}")
return Planned(tasks, [f"Passed over the {noun} {name}: {why}." for name, why in passed_over], {})
def counted_trees(bench: Bench, shared: dict[Side, str], feature: Feature) -> dict[Side, str] | str | Failure:
"""Each side's tree that feature's task starts in, or why the feature doesn't count. A feature starts in shared, and a data
source in trees of its own, which hold its register. A Failure where a tree could not be made or the tests could not run."""
trees = shared if feature.register is None else registered_trees(bench, feature.name, feature.register)
if isinstance(trees, NotGenerated):
return trees.reason
if isinstance(trees, Failure):
return trees
return why_passed_over(bench, trees, feature) or trees
def committed_trees(bench: Bench) -> dict[Side, str] | Failure: