"""Replay loop-claude's judge on archived verdicts, to find the lowest rung that returns them: docs/decisions.md → `routing: on a model judge-replay covered`. usage: judge-replay is a project model on the routing ladder, such as claude-opus-5-5. The tool replays the verdicts archived for the projects that ~/.claude/loop-claude-models.json runs on it: the latest 15 of each kind, finished, unfinished, interrupt and quoted answer. Each transcript copy in the archive is what the judge read for the verdict beside it, so the replay hands the judge that copy, with the task, premises and records each project holds now. It first replays every verdict on the reference judge: the model at xhigh effort, the rung the judge ran on before loop-claude routed it. A verdict the reference judge returns another kind on may rest on records or judge rules that have changed since; either way no rung is held to it. It then replays the rest on each candidate rung, highest first: from the model's cheapest rung down to the lowest rung a worker routes to. It stops at the first rung that misses one. It prints each verdict the reference judge did not reproduce, each verdict a rung missed, and the lowest rung that, like each rung above it, missed none. A rung it names is the evidence to add the model to JUDGE_REPLAYED in bin/loop_claude/routing.py, which routes that model's judge, and to record the run in docs/decisions.md → `routing: on a model judge-replay covered`. Like the loop, it tries a judge three times running before it counts the reply as failed. A verdict whose project or transcript cannot be read replays as failed too. Exit 1, saying why, on bad arguments, an unreadable archive or models file, no archived verdict of the model's projects, or a usage limit. """ import pathlib import typing from transcript_archive.location import archive_root, transcript_of from transcript_archive.read import archived_outcomes from rung_replay.command import arguments_or_exit, down_from, failed, limited_exit, projects_on from judge_replay.replay import Case, case_from, Kind, report, reproduced, sample, verdict_kind from project_key import checkout_of from loop_claude.effects import Effects from failure import Failure from rung_replay.search import first_reply, replayed from loop_claude.cycle.judge import judge_once from claude_code.reply import Limited from model_routing.ladder import lowest_worker_rung, project_rung, Rung from loop_claude.project.premises import read_premises from loop_claude.project.records import read_records from claude_code.transcript import read_transcript from loop_claude.stop import Stop from loop_claude.verdicts import Verdict TOOL = "judge-replay" USAGE = f"{TOOL} , a model on the routing ladder" PER_KIND = 15 # The effort settings.json gave every judge before loop-claude routed it; the archived verdicts came from it. REFERENCE_EFFORT = "xhigh" def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, effects: Effects) -> int: anchor = arguments_or_exit(TOOL, argv, __doc__, USAGE, model_argument) if isinstance(anchor, int): return anchor cases = archived_cases(anchor.model, archive_root(home, env), home) if isinstance(cases, Failure): return failed(TOOL, cases.reason) if not cases: return failed(TOOL, f"no archived verdicts of projects on {anchor.model} in {archive_root(home, env)}; run loop-claude on a project on that model first") cases = sample(cases, PER_KIND) reference = Rung(anchor.model, REFERENCE_EFFORT) # The candidates stop at the lowest rung a worker routes to: no route uses a rung below it for the judge. replay = replayed(cases, reference, down_from(anchor, lowest_worker_rung(anchor)), lambda found, rung: rule(found, rung, home / ".claude", effects), reproduced, "verdicts") if isinstance(replay, Limited): return limited_exit(TOOL, replay) print(report(anchor.model, reference, cases, replay)) return 0 def model_argument(argv: list[str]) -> Rung | None: """The project's rung of the one model argv names; None for any other argv.""" return project_rung(argv[0]) if len(argv) == 1 else None def archived_cases(model: str, root: pathlib.Path, home: pathlib.Path) -> list[Case] | Failure: projects = projects_on(model, home) if isinstance(projects, Failure): return projects listed = archived_outcomes(root) if isinstance(listed, Failure): return listed found = [] try: for key, outcomes in listed: case = case_from(checkout_of(key, home), outcomes.read_text(encoding="utf-8"), transcript_of(outcomes)) if key in projects else None found += [case] if case else [] except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read the archive {root}: {failure}") return found def rule(case: Case, rung: Rung, claude_dir: pathlib.Path, effects: Effects) -> Kind | Limited: premises, records, entries = read_premises(case.root), read_records(case.root, claude_dir), read_transcript(str(case.transcript)) if isinstance(premises, Stop): return verdict_kind(Failure(premises.reason)) if isinstance(records, Stop): return verdict_kind(Failure(records.reason)) if isinstance(entries, Failure): return verdict_kind(entries) # 0 for the route's mark, as before any step-up: the replay compares verdict kinds, and a mark changes only which escalation quotes count. verdict: Verdict | Failure | Limited = first_reply(lambda: judge_once(case.root, case.task, premises, records, entries, 0, rung, effects)) return verdict if isinstance(verdict, Limited) else verdict_kind(verdict)