102 lines
3.8 KiB
Python
102 lines
3.8 KiB
Python
"""The rung search judge-replay and review-replay share: replay archived cases on a reference rung, then hold each candidate rung to what the reference reproduced."""
|
|
import concurrent.futures
|
|
import dataclasses
|
|
import sys
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from loop_claude.cycle.retry import FAILURES_RUNNING
|
|
from claude_code.reply import Limited
|
|
from model_routing.ladder import Rung
|
|
|
|
# Cases replayed at once.
|
|
AT_ONCE = 4
|
|
|
|
Case = typing.TypeVar("Case")
|
|
# What of an archived case a reply reproduces, such as a verdict's kind or a finding's index.
|
|
Item = typing.TypeVar("Item")
|
|
Reply = typing.TypeVar("Reply")
|
|
T = typing.TypeVar("T")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Miss(typing.Generic[Case, Item, Reply]):
|
|
"""An item of case that the reference reproduced and a rung's reply missed."""
|
|
case: Case
|
|
item: Item
|
|
reply: Reply
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Tried(typing.Generic[Case, Item, Reply]):
|
|
rung: Rung
|
|
missed: list[Miss[Case, Item, Reply]]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Replay(typing.Generic[Case, Item, Reply]):
|
|
"""on_reference holds the reference's reply on each case; tried, each candidate tried, highest first; lowest, the lowest rung that, like each candidate above it, missed nothing, or None."""
|
|
on_reference: list[Reply]
|
|
tried: list[Tried[Case, Item, Reply]]
|
|
lowest: Rung | None
|
|
|
|
|
|
def replayed(
|
|
cases: list[Case],
|
|
reference: Rung,
|
|
candidates: list[Rung],
|
|
replay_one: typing.Callable[[Case, Rung], Reply | Limited],
|
|
reproduced: typing.Callable[[Case, Reply], list[Item]],
|
|
what: str,
|
|
) -> Replay[Case, Item, Reply] | Limited:
|
|
"""Replays the cases on the reference rung, then each candidate, highest first, on the cases where the reference reproduced anything, until one misses an item it reproduced.
|
|
|
|
Going down, every rung at or above the one named was tried and missed nothing, so the result needs no assumption that a higher rung does at least as well; and a miss high up spares the cheaper rungs their replays.
|
|
"""
|
|
on_reference = replayed_on(cases, reference, replay_one, what)
|
|
if isinstance(on_reference, Limited):
|
|
return on_reference
|
|
held = [(case, aim) for case, reply in zip(cases, on_reference) if (aim := reproduced(case, reply))]
|
|
tried: list[Tried[Case, Item, Reply]] = []
|
|
lowest = None
|
|
# With nothing to hold a rung to, every rung would miss nothing.
|
|
for rung in candidates if held else []:
|
|
replies = replayed_on([case for case, _ in held], rung, replay_one, what)
|
|
if isinstance(replies, Limited):
|
|
return replies
|
|
misses: list[Miss[Case, Item, Reply]] = []
|
|
for (case, aim), reply in zip(held, replies):
|
|
got = reproduced(case, reply)
|
|
misses += [Miss(case, item, reply) for item in aim if item not in got]
|
|
tried.append(Tried(rung, misses))
|
|
if misses:
|
|
break
|
|
lowest = rung
|
|
return Replay(on_reference, tried, lowest)
|
|
|
|
|
|
def replayed_on(cases: list[Case], rung: Rung, replay_one: typing.Callable[[Case, Rung], Reply | Limited], what: str) -> list[Reply] | Limited:
|
|
"""Each case's reply on rung, AT_ONCE at a time; a usage limit cancels the rest."""
|
|
print(f"replaying {len(cases)} {what} on {rung}", file=sys.stderr, flush=True)
|
|
with concurrent.futures.ThreadPoolExecutor(AT_ONCE) as pool:
|
|
running = [pool.submit(replay_one, case, rung) for case in cases]
|
|
replies: list[Reply] = []
|
|
for future in running:
|
|
reply = future.result()
|
|
if isinstance(reply, Limited):
|
|
pool.shutdown(cancel_futures=True)
|
|
return reply
|
|
replies.append(reply)
|
|
return replies
|
|
|
|
|
|
def first_reply(attempt: typing.Callable[[], T | Failure | Limited]) -> T | Failure | Limited:
|
|
"""The first reply that is no Failure, or the last of FAILURES_RUNNING failures.
|
|
|
|
It counts failures as loop-claude's retried does, but neither waits between attempts nor waits out a usage limit, which ends a replay.
|
|
"""
|
|
for _ in range(FAILURES_RUNNING - 1):
|
|
if not isinstance(reply := attempt(), Failure):
|
|
return reply
|
|
return attempt()
|