77 lines
3.6 KiB
Python
77 lines
3.6 KiB
Python
"""`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 chunk_driver.project.model_choice import read_models_file
|
|
from claude_code.reply import Claude, claude_reply, Limited
|
|
from failure import Failure
|
|
from git_cli import git_repo
|
|
from git_cli.git_repo import Git, top
|
|
from loop_state.paths import paths_from
|
|
from model_routing.ladder import Rung
|
|
from model_routing.ladder_file import project_ladder
|
|
from review_rules.review_folder import review_folder
|
|
from task_bench.bench_folder import held
|
|
from transcript_archive.project_key import project_key
|
|
|
|
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:
|
|
"""What the gathering claude answered, once the bench's folder of the checkout the process runs in is gone. 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(checkout, home, git)
|
|
if isinstance(rung, Failure):
|
|
return rung
|
|
with held(pathlib.Path(review_folder(str(home), checkout))) 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}")
|
|
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(checkout: str, home: pathlib.Path, git: Git) -> Rung | Failure:
|
|
"""The cheapest rung of the model loop-claude runs the project on, on the project's routing ladder."""
|
|
common = git("rev-parse", "--path-format=absolute", "--git-common-dir", cwd=checkout)
|
|
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(checkout, 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.")
|