"""`task-bench gather`: a claude on the project's rung reads the digest of the checkout's last bench and files a plan item for each place the runs show to be hard, and then the bench's folder goes, streams and digest with it.""" import functools import os import pathlib import shutil import subprocess import typing from claude_code.reply import Claude, claude_reply, Limited from failure import Failure from git_cli.git_repo import Git, top from git_cli import git_repo from task_bench.bench_folder import held from review_rules.review_folder import made_review_folder from loop_state.paths import paths_from from transcript_archive.project_key import project_key from model_routing.ladder_file import project_ladder from chunk_driver.project.model_choice import read_models_file from model_routing.ladder import Rung DIGEST = "digest.md" PROMPT = pathlib.Path(__file__).with_name("gather-prompt.md") def gather(env: typing.Mapping[str, str], home: pathlib.Path, claude: Claude) -> list[str] | Failure: """Gathers the bench of the checkout the process runs in: what the gathering claude answered, once the bench's folder is removed. A Failure, with the folder kept, where there is no digest, the project's rung cannot be read or claude fails.""" git = functools.partial(git_repo.git, env=env) checkout = top(git) if isinstance(checkout, Failure): return checkout rung = project_rung(home, git) if isinstance(rung, Failure): return rung review = made_review_folder(checkout, home) if isinstance(review, Failure): return review with held(review) as folder: if isinstance(folder, Failure): return folder try: digest = (folder / DIGEST).read_text(encoding="utf-8") prompt = PROMPT.read_text(encoding="utf-8") except FileNotFoundError: return Failure(f"{folder} holds no digest: run task-bench in this checkout first.") except OSError as failure: return Failure(f"could not read {failure.filename}: {failure.strerror}") reply = claude_reply(claude, [*rung.flags(), "--strict-mcp-config"], checkout, f"{prompt.strip()}\n\nThe bench's folder: {folder}\n\n{digest}", "the gathering claude") if isinstance(reply, Limited): reply = Failure(f"claude hit a usage limit: {reply.said}") elif not isinstance(reply, Failure) and reply.get("is_error"): reply = Failure(f"the gathering claude ended in an error: {reply.get('result') or 'it gave none'}") if isinstance(reply, Failure): return Failure(f"{reply.reason}\nThe digest stays in {folder} until the next bench: run `task-bench gather` again.") shutil.rmtree(folder, ignore_errors=True) return [str(reply.get("result") or "The gathering claude didn't say anything.").strip(), "", f"Removed the bench's folder, {folder}."] def run_claude(args: list[str], cwd: str, prompt: str) -> subprocess.CompletedProcess[str]: return subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=prompt, text=True) def project_rung(home: pathlib.Path, git: Git) -> Rung | Failure: """The cheapest rung for the model loop-claude runs the project on. The routing ladder is that of the project the process runs in.""" common = git("rev-parse", "--path-format=absolute", "--git-common-dir") if isinstance(common, Failure): return common # A worktree's project is its main checkout's, which holds the folder every worktree shares. key = project_key(os.path.dirname(common.strip()), home) saved = paths_from(home).models_file models = read_models_file(saved) if isinstance(models, Failure): return models if key not in models: return Failure(f"{saved} names no model for {key}: run loop-claude in the project once, which asks for it.") ladder = project_ladder(None, home) if isinstance(ladder, Failure): return ladder project = ladder.project_rung(models[key]) return project.rung if project else Failure(f"{key} runs on {models[key]}, which is not on its routing ladder.")