Answer architecture round 1: 2 🔴, 13 🟡; test the replay's main and report, share one judge run and the archive format, pin the reference judge, route only replayed models, and say why the judge climbs
This commit is contained in:
+52
-41
@@ -1,101 +1,112 @@
|
||||
"""Which archived verdicts to replay, and the cheapest rung that returns them: docs/decisions.md → `routing:`."""
|
||||
"""Which archived verdicts to replay, the cheapest rung that returns them, and the report: docs/decisions.md → `routing:`."""
|
||||
import collections
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
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
|
||||
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)
|
||||
# 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 is its kind, as the archive words it, and at its UTC stamp."""
|
||||
"""One archived verdict: on_record, judged at its UTC stamp."""
|
||||
root: str
|
||||
task: str
|
||||
transcript: pathlib.Path
|
||||
at: str
|
||||
on_record: str
|
||||
on_record: Kind
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Tried:
|
||||
"""A rung, and each case it returned another kind of verdict on, with that kind."""
|
||||
"""A rung, and each case it returned another kind on, with that kind."""
|
||||
rung: Rung
|
||||
missed: list[tuple[Case, str]]
|
||||
missed: list[tuple[Case, Kind]]
|
||||
|
||||
|
||||
@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]]
|
||||
"""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 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):
|
||||
"""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."""
|
||||
task, verdict = task_of(outcomes), first_verdict(outcomes)
|
||||
if task is None or verdict is None:
|
||||
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}"
|
||||
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[str] = collections.Counter()
|
||||
seen: collections.Counter[Kind] = collections.Counter()
|
||||
kept = []
|
||||
for found in sorted(cases, key=lambda found: found.at, reverse=True):
|
||||
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) -> str:
|
||||
def verdict_kind(verdict: Verdict | Failure) -> Kind:
|
||||
match verdict:
|
||||
case Finished():
|
||||
return "finished"
|
||||
return FINISHED
|
||||
case Unfinished():
|
||||
return "unfinished"
|
||||
return UNFINISHED
|
||||
case Interrupt():
|
||||
return "interrupt"
|
||||
return INTERRUPTED
|
||||
case QuotedAnswer():
|
||||
return "quoted answer"
|
||||
return ANSWERED
|
||||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
A verdict the reference 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))
|
||||
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 = []
|
||||
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:
|
||||
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)]
|
||||
lines.append(f"Cheapest rung that returns every verdict the reference judge reproduces: {replay.cheapest or 'none up to the project rung'}")
|
||||
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]
|
||||
|
||||
Reference in New Issue
Block a user