118 lines
4.7 KiB
Python
118 lines
4.7 KiB
Python
"""Which archived verdicts to replay, the cheapest rung that returns them, and the report: docs/decisions.md → `routing: on a model judge-replay covered`."""
|
|
import collections
|
|
import dataclasses
|
|
import pathlib
|
|
import typing
|
|
|
|
from transcript_archive.outcomes import ANSWERED, FINISHED, first_verdict, INTERRUPTED, task_of, UNFINISHED
|
|
from failure import Failure
|
|
from loop_claude.cycle.judge import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
|
|
from loop_claude.cycle.retry import Limited
|
|
from model_routing.ladder import Rung
|
|
|
|
# claude's reply as the replay compares it: a verdict as the archive words it, such as FINISHED, or a failure with its reason.
|
|
Kind = str
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Case:
|
|
"""One archived verdict: on_record, judged at its UTC stamp."""
|
|
root: str
|
|
task: str
|
|
transcript: pathlib.Path
|
|
at: str
|
|
on_record: Kind
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Tried:
|
|
"""A rung, and each case it returned another kind on, with that kind."""
|
|
rung: Rung
|
|
missed: list[tuple[Case, Kind]]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Replay:
|
|
"""unreproduced holds each case the reference judge returned another kind on; tried, each rung tried, cheapest first; cheapest, the first rung that missed none, or None."""
|
|
unreproduced: list[tuple[Case, Kind]]
|
|
tried: list[Tried]
|
|
cheapest: Rung | None
|
|
|
|
|
|
def case_from(root: str, outcomes: str, transcript: pathlib.Path) -> Case | None:
|
|
"""The first verdict an outcomes file holds: the judge ruled on the transcript copy beside it as the copy stands. None when it holds none.
|
|
|
|
A fourth quoted answer in a cycle is archived as an interrupt. The reference judge replays it as a quoted answer, so it lands among the unreproduced verdicts and no rung is held to it.
|
|
"""
|
|
task, verdict = task_of(outcomes), first_verdict(outcomes)
|
|
if task is None or verdict is None:
|
|
return None
|
|
return Case(root, task, transcript, *verdict)
|
|
|
|
|
|
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: collections.Counter[Kind] = collections.Counter()
|
|
kept = []
|
|
for found in sorted(cases, key=lambda found: (found.at, found.root, str(found.transcript)), 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) -> Kind:
|
|
match verdict:
|
|
case Finished():
|
|
return FINISHED
|
|
case Unfinished():
|
|
return UNFINISHED
|
|
case Interrupt():
|
|
return INTERRUPTED
|
|
case QuotedAnswer():
|
|
return ANSWERED
|
|
case Failure(reason):
|
|
return f"failed: {reason}"
|
|
case _:
|
|
typing.assert_never(verdict)
|
|
|
|
|
|
def replayed(cases: list[Case], reference: Rung, candidates: list[Rung], rule_all: typing.Callable[[list[Case], Rung], list[Kind] | Limited]) -> Replay | Limited:
|
|
"""Replays the cases on the reference rung, then only those it reproduces on each candidate, cheapest first, until one returns every verdict."""
|
|
ruled = rule_all(cases, reference)
|
|
if isinstance(ruled, Limited):
|
|
return ruled
|
|
reproduced = [found for found, kind in zip(cases, ruled) if kind == found.on_record]
|
|
unreproduced = [(found, kind) for found, kind in zip(cases, ruled) if kind != found.on_record]
|
|
tried: list[Tried] = []
|
|
# With no verdict to hold a rung to, every rung would miss none.
|
|
for rung in candidates if reproduced else []:
|
|
ruled = rule_all(reproduced, rung)
|
|
if isinstance(ruled, Limited):
|
|
return ruled
|
|
tried.append(Tried(rung, [(found, kind) for found, kind in zip(reproduced, ruled) if kind != found.on_record]))
|
|
if not tried[-1].missed:
|
|
return Replay(unreproduced, tried, rung)
|
|
return Replay(unreproduced, tried, None)
|
|
|
|
|
|
def report(model: str, reference: Rung, cases: list[Case], replay: Replay) -> str:
|
|
kinds = collections.Counter(found.on_record for found in cases)
|
|
lines = [
|
|
f"Replayed {len(cases)} verdicts of {model} projects: {', '.join(f'{count} {kind}' for kind, count in sorted(kinds.items()))}.",
|
|
f"The reference judge, {reference}, reproduced {len(cases) - len(replay.unreproduced)}{'. It returned another kind on:' if replay.unreproduced else '.'}",
|
|
*missed_lines(replay.unreproduced),
|
|
]
|
|
for tried in replay.tried:
|
|
lines += [f"{tried.rung} missed {len(tried.missed)}{':' if tried.missed else '.'}", *missed_lines(tried.missed)]
|
|
if not replay.tried:
|
|
lines.append("No rung tried: the reference judge reproduced no verdict to hold one to.")
|
|
else:
|
|
cheapest = replay.cheapest or "none up to the project's rung"
|
|
lines.append(f"Cheapest rung that returns every verdict the reference judge reproduces: {cheapest}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def missed_lines(missed: list[tuple[Case, Kind]]) -> list[str]:
|
|
return [f" {found.root} {found.at}: on record {found.on_record}, replayed {kind}" for found, kind in missed]
|