Files
claude-config/bin/task_bench/registers.py
T

60 lines
3.1 KiB
Python

"""The register a data-source task hands Haiku: the files the task's `generate-register` writes into the tree Haiku starts in.
The script is seeded, so it writes the same register at the base and at the head, byte for byte; a task whose script doesn't is
passed over."""
import pathlib
import shutil
from task_bench.tasks import Bench, committed, on_disk
from task_bench.runs import Containers, entries, in_tree, run_in_image, TimedOut
from failure import Failure
from task_bench.figures import Side, SIDES
# A panel of three readers, seated as the README's audience, picked the name on 2026-11-23: each had it first or second, over
# `register`, which reads as a data file, and `generate-source`, which two read as generating source code.
GENERATE = "generate-register"
# Where a container finds the script, outside the tree, so the tree never holds it.
MOUNTED = f"/{GENERATE}"
class NotGenerated(Failure):
"""The script gave no register a task can start from. The reason ends a sentence that opens with the task's name, as in
`its generate-register exits 3 at the base`."""
def registered_trees(bench: Bench, name: str, script: str) -> dict[Side, str] | Failure:
"""A copy of each side's tree with the register that script generates in it, committed once, for the data source called name.
NotGenerated where the script fails at a side, or writes one register at the base and another at the head."""
trees: dict[Side, str] = {}
registers: dict[Side, dict[str, bytes]] = {}
for side in SIDES:
tree = bench.folder / "data-sources" / name / 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
register = generated(bench.containers, str(tree), script)
if isinstance(register, NotGenerated):
return NotGenerated(f"{register.reason} at the {side}")
if isinstance(register, Failure):
return register
if failure := committed(bench.git, str(tree)):
return failure
trees[side], registers[side] = str(tree), register
if registers["base"] != registers["head"]:
return NotGenerated(f"its {GENERATE} writes one register at the base and another at the head")
return trees
def generated(containers: Containers, tree: str, script: str) -> dict[str, bytes] | Failure:
"""The register script writes when run at the top of tree, in the bench's image with no network: each file new to the tree,
by its path from the top. NotGenerated where the script runs out of time, fails or writes no new file."""
before = set(entries(tree))
ran = run_in_image(containers, ["--network", "none", *in_tree(containers, tree), "--volume", f"{script}:{MOUNTED}:ro", containers.image, MOUNTED])
if isinstance(ran, TimedOut):
return NotGenerated(f"its {GENERATE} runs out of time")
if isinstance(ran, Failure):
return ran
if ran.code:
return NotGenerated(f"its {GENERATE} exits {ran.code}")
register = on_disk(f"read the register in {tree}", lambda: {path: pathlib.Path(tree, path).read_bytes() for path in entries(tree) if path not in before})
return register or NotGenerated(f"its {GENERATE} doesn't write any new file")