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

75 lines
4.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.
A task counts only where its script writes the same register at the base and at the head, byte for byte, which a seeded script
does."""
import os
import pathlib
import shutil
from task_bench.tasks import Bench, committed, on_disk
from task_bench.runs import Containers, entries, in_tree, NotStarted, 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}"
# What `docker run` exits with where docker itself failed, before any container started.
DOCKER_FAILED = 125
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, writes a `.git`, writes no new file, or
writes one that is no regular 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")
# With 126 or 127 the container started and the image could not run the script, as the task's fault as any other exit.
if isinstance(ran, NotStarted) and ran.ran.code != DOCKER_FAILED:
ran = ran.ran
if isinstance(ran, Failure):
return ran
if ran.code:
said = ran.output.strip().splitlines()[-1:]
return NotGenerated(f"its {GENERATE} exits {ran.code}" + "".join(f" after printing `{line[:200]}`" for line in said))
# git runs on the host in this tree next, and would run what a `.git/config` there names, outside the container.
if os.path.lexists(os.path.join(tree, ".git")):
return NotGenerated(f"its {GENERATE} writes a `.git`")
new = [path for path in entries(tree) if path not in before]
# The host reads each next: it would follow a link into its own files, and never finish reading a pipe.
if odd := [path for path in new if os.path.islink(os.path.join(tree, path)) or not os.path.isfile(os.path.join(tree, path))]:
return NotGenerated(f"its {GENERATE} writes `{odd[0]}`, which is no regular file")
register = on_disk(f"read the register in {tree}", lambda: {path: pathlib.Path(tree, path).read_bytes() for path in new})
return register or NotGenerated(f"its {GENERATE} doesn't write any new file")