"""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 model_routing.ladder import LADDER, position, Rung from claude_code.reply import Limited from loop_claude.paths import paths_from from loop_claude.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(top: Rung, floor: Rung) -> list[Rung]: """top, the project's rung, and each rung below it, highest first, down to floor.""" return list(LADDER[position(floor):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