60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
"""What judge-replay's and review-replay's commands share: reading the arguments, the projects on a model, the candidate rungs and the exits."""
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from rung_replay.archive import kept_report
|
|
from model_routing.ladder import Ladder, Rung
|
|
from claude_code.reply import Limited
|
|
from loop_state.paths import paths_from
|
|
from chunk_driver.project.model_choice import read_models_file
|
|
|
|
# What a tool reads from its arguments, such as the project's rung of the model they name.
|
|
Parsed = typing.TypeVar("Parsed")
|
|
|
|
|
|
def arguments_or_exit(tool: str, argv: list[str], help_text: str, usage: str, read: typing.Callable[[list[str]], Parsed | None]) -> Parsed | int:
|
|
"""What read makes of argv, or the exit code: 0 after printing help_text for -h or --help, 1 after saying, with usage, that argv cannot be read, where read gives None."""
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(help_text.strip())
|
|
return 0
|
|
arguments = read(argv)
|
|
if arguments is None:
|
|
return failed(tool, f"cannot read the arguments{' ' + ' '.join(argv) if argv else ''}; usage: {usage}")
|
|
return arguments
|
|
|
|
|
|
def projects_on(model: str, home: pathlib.Path) -> list[str] | Failure:
|
|
"""The keys of the projects loop-claude's models file, under home, runs on model, sorted."""
|
|
models = read_models_file(paths_from(home).models_file)
|
|
if isinstance(models, Failure):
|
|
return models
|
|
return sorted(key for key, on in models.items() if on == model)
|
|
|
|
|
|
def down_from(ladder: Ladder, top: Rung, floor: Rung) -> list[Rung]:
|
|
"""top, the project's rung, and each rung below it on ladder, highest first, down to floor."""
|
|
return list(ladder.rungs[ladder.position(floor):ladder.position(top) + 1])[::-1]
|
|
|
|
|
|
def limited_exit(tool: str, limited: Limited) -> int:
|
|
return failed(tool, f"claude hit a usage limit: {limited.said}; replay again once it resets")
|
|
|
|
|
|
def failed(tool: str, reason: str) -> int:
|
|
print(f"{tool}: {reason}", file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
def say(text: str) -> None:
|
|
"""Writes text to stderr, as each replay's progress and each round it leaves out are told."""
|
|
print(text, file=sys.stderr, flush=True)
|
|
|
|
|
|
def print_report(folder: pathlib.Path, report: str) -> None:
|
|
"""Prints the report a replay ends with, and keeps it in folder, beside the runs."""
|
|
print(report)
|
|
if kept := kept_report(folder, report):
|
|
say(kept.reason)
|