"""Which archived verdicts to replay, and the cheapest rung that returns them: docs/decisions.md → `routing:`.""" import collections import dataclasses import pathlib import re import typing from failure import Failure from loop_claude.cycle.judge import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict from model_routing.ladder import Rung from utc_stamp import STAMPED # The first verdict archived beside a transcript copy: the judge ruled on that copy as it stands. FIRST_VERDICT = re.compile(rf"^({STAMPED}) Verdict: (finished|unfinished|interrupt|quoted answer)$", re.MULTILINE) TASK = re.compile(rf"\ATask: (.*?)\n\n{STAMPED} ", re.DOTALL) @dataclasses.dataclass(frozen=True) class Case: """One archived verdict: on_record is its kind, as the archive words it, and at its UTC stamp.""" root: str task: str transcript: pathlib.Path at: str on_record: str @dataclasses.dataclass(frozen=True) class Tried: """A rung, and each case it returned another kind of verdict on, with that kind.""" rung: Rung missed: list[tuple[Case, str]] @dataclasses.dataclass(frozen=True) class Replay: """unreproduced holds each case today's judge returned another kind on; tried, each rung tried, cheapest first; cheapest, the first rung that missed none, or None.""" unreproduced: list[tuple[Case, str]] tried: list[Tried] cheapest: Rung | None def case_from(root: str, outcomes: str, transcript: pathlib.Path) -> Case | None: """The case an outcomes file holds; None when it holds no verdict.""" task, verdict = TASK.match(outcomes), FIRST_VERDICT.search(outcomes) if not (task and verdict): return None return Case(root, task[1], transcript, verdict[1], verdict[2]) def checkout_root(key: str, home: pathlib.Path) -> str: """The checkout an archive key names: the inverse of project_key.""" under_code = home / "code" / key return str(under_code) if under_code.is_dir() else f"/{key}" def sample(cases: list[Case], per_kind: int) -> list[Case]: """The latest per_kind cases of each kind of verdict, in the order they were judged.""" seen, kept = collections.Counter(), [] for found in sorted(cases, key=lambda found: found.at, reverse=True): seen[found.on_record] += 1 if seen[found.on_record] <= per_kind: kept.append(found) return kept[::-1] def verdict_kind(verdict: Verdict | Failure) -> str: match verdict: case Finished(): return "finished" case Unfinished(): return "unfinished" case Interrupt(): return "interrupt" case QuotedAnswer(): return "quoted answer" case Failure(reason): return f"failed: {reason}" case _: typing.assert_never(verdict) def replayed(cases: list[Case], today: Rung, candidates: list[Rung], rule_all: typing.Callable[[list[Case], Rung], list[str]]) -> Replay: """Replays the cases on today's rung, then only those it reproduces on each candidate, cheapest first, until one returns every verdict. A verdict today's judge does not reproduce rests on the records and judge rules of its day, so no rung is held to it. """ reproduced, unreproduced = [], [] for found, kind in zip(cases, rule_all(cases, today)): if kind == found.on_record: reproduced.append(found) else: unreproduced.append((found, kind)) tried = [] for rung in candidates: missed = [(found, kind) for found, kind in zip(reproduced, rule_all(reproduced, rung)) if kind != found.on_record] tried.append(Tried(rung, missed)) if not missed: return Replay(unreproduced, tried, rung) return Replay(unreproduced, tried, None)