"""The bar review-replay holds a rung to, and its report: docs/decisions.md → `routing: a review pass runs below the project's rung only on a rung whose replay of its rounds finds a share of their 🔴 and 🟡 findings no more than a margin below the project's rung's own replay`.""" import dataclasses from fractions import Fraction from model_routing.ladder import LADDER, Rung from review_replay.rounds import Round # A rung qualifies with a share of the archived findings up to this far below the project's rung's. The project's rung found 69 to 73% of its own findings again, so about 3 of the 30 to 40 that 10 rounds hold go either way between two replays. MARGIN = Fraction(1, 10) # The rounds a rung is tried on: 8 of the 10 the tool samples, so up to two may fail on the project's rung. ENOUGH_ROUNDS = 8 # Lowest first, as Claude Code's --effort takes them. EFFORTS = ("low", "medium", "high", "xhigh", "max") # The two replays on the project's rung: each round's indexes of its findings that each found. Reference = tuple[list[set[int]], list[set[int]]] @dataclasses.dataclass(frozen=True) class RungRecall: """A rung's replay: its share of the archived findings, and each 🔴 it missed that the project's rung found both times, as a round and the finding's index.""" rung: Rung share: Fraction missed_red: list[tuple[Round, int]] qualifies: bool @dataclasses.dataclass(frozen=True) class Recall: """rounds, the rounds no replay on the project's rung failed on; left_out, how many more it failed on; tried, each candidate tried, highest first; lowest, the lowest that, like each above it, qualifies.""" rounds: list[Round] reference: Reference left_out: int tried: list[RungRecall] lowest: Rung | None def candidates(project: Rung) -> list[Rung]: """The rungs tried below the project's, highest first: its model at each lower effort, which the ladder leaves out, then each ladder rung below it, down to the ladder's lowest. That reaches below the lowest a worker routes to, since a pass may run below the worker's rung.""" lower_efforts = [Rung(project.model, effort) for effort in reversed(EFFORTS[:EFFORTS.index(project.effort)])] return lower_efforts + list(LADDER[:LADDER.index(project)])[::-1] def enough(rounds: list[Round]) -> bool: return len(rounds) >= ENOUGH_ROUNDS def found_any(reference: Reference) -> bool: return any(map(any, reference)) def share(rounds: list[Round], found: list[set[int]]) -> Fraction: """The share of rounds' findings that found holds; rounds is never empty, and each holds a finding.""" return Fraction(sum(map(len, found)), sum(len(one.findings) for one in rounds)) def bar(rounds: list[Round], reference: Reference) -> Fraction: """The share a rung needs: the mean of the project's rung's two shares, less MARGIN.""" return (share(rounds, reference[0]) + share(rounds, reference[1])) / 2 - MARGIN def tried(rung: Rung, rounds: list[Round], reference: Reference, found: list[set[int]]) -> RungRecall: """rung's recall, from the indexes of each round's findings it found. It qualifies at the bar or above, missing no 🔴 the project's rung found both times; where the project's rung found nothing, no rung qualifies.""" missed_red = [(one, index) for one, first, second, got in zip(rounds, *reference, found) for index in sorted((first & second) - got) if "🔴" in one.findings[index].splitlines()[0]] got = share(rounds, found) return RungRecall(rung, got, missed_red, found_any(reference) and got >= bar(rounds, reference) and not missed_red) def recall_report(reviewed: str, project: Rung, recall: Recall) -> str: rounds, (first, second) = recall.rounds, recall.reference lines = [ f"Replayed {len(rounds)} {reviewed} rounds of {project.model} projects, which hold {sum(len(one.findings) for one in rounds)} 🔴 and 🟡 findings, twice on the project's rung, {project}.", *([f"Left out {recall.left_out} more, which a replay on the project's rung failed on; stderr says why."] if recall.left_out else []), ] if not enough(rounds): return "\n".join([*lines, f"No rung tried: fewer than {ENOUGH_ROUNDS} rounds were replayed on the project's rung without a failure."]) if not found_any(recall.reference): return "\n".join([*lines, "No rung tried: the project's rung found none of them."]) lines.append(f"The project's rung found {sum(map(len, first))} and then {sum(map(len, second))} of them, {percent(bar(rounds, recall.reference) + MARGIN)}, so a rung qualifies at {percent(bar(rounds, recall.reference))} or more and missing no 🔴 it found both times.") for one in recall.tried: verdict = ": it qualifies." if one.qualifies else ":" if one.missed_red else "." lines += [f"{one.rung} found {percent(one.share)} and missed {len(one.missed_red) or 'no'} such 🔴{verdict}", *(f" {at.root} {at.at}: {at.findings[index].splitlines()[0]}" for at, index in one.missed_red)] lowest = recall.lowest or "none below the project's rung" lines.append(f"Lowest rung that qualifies: {lowest}") return "\n".join(lines) def percent(fraction: Fraction) -> str: return f"{float(fraction):.0%}"