"""What judge-replay's and stability-replay's commands share: the argument, the projects on its model, the candidate rungs and the exits.""" import pathlib import sys from failure import Failure from model_routing.ladder import LADDER, lowest_worker_rung, position, project_rung, Rung from claude_code.reply import Limited from loop_claude.project.model_choice import read_models_file from loop_claude.stop import Stop def project_rung_or_exit(tool: str, argv: list[str], help_text: str) -> Rung | int: """The project's rung of the model argv names, or the exit code: 0 after printing help_text for -h or --help, 1 after saying why argv cannot be read.""" if argv[:1] in (["-h"], ["--help"]): print(help_text.strip()) return 0 anchor = project_rung(argv[0]) if len(argv) == 1 else None if anchor is None: return failed(tool, f"cannot read the arguments{' ' + ' '.join(argv) if argv else ''}; usage: {tool} , a model on the routing ladder") return anchor def projects_on(model: str, home: pathlib.Path) -> list[str] | Failure: """The keys of the projects ~/.claude/loop-claude-models.json runs on model, sorted.""" models = read_models_file(home / ".claude" / "loop-claude-models.json") if isinstance(models, Stop): return Failure(models.reason) return sorted(key for key, on in models.items() if on == model) def down_from(top: Rung) -> list[Rung]: """top, the project's rung, and each rung below it, highest first, down to the lowest rung a worker routes to: no route uses a rung below it, so a replay there could change no routing.""" return list(LADDER[position(lowest_worker_rung(top)):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