96 lines
4.4 KiB
Python
96 lines
4.4 KiB
Python
"""The routing figures summed over every chunk, and the routing changes they call for: docs/decisions.md → `routing: loop-claude measures review rounds…`."""
|
|
import collections
|
|
import dataclasses
|
|
import statistics
|
|
import typing
|
|
|
|
from routing_figures.chunk import ChunkFigures, Reports
|
|
from routing_figures.dollars import DIMENSIONS, Dollars
|
|
from model_routing.ladder import Ladder, Rung
|
|
|
|
# A rung or a subagent type is compared only once this many of its chunks or subagents are measured, so a single odd chunk calls for no change.
|
|
MEASURED_AT_LEAST = 5
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Figures:
|
|
"""escalated counts every chunk's escalations by trigger, routed or not; skipped names each chunk folder the archive could not read whole; unmatched counts the routed chunks whose later fixes are unknown; unread names each project whose default branch could not be read, with why."""
|
|
chunks: int
|
|
dollars: Dollars
|
|
routed: dict[Rung, list[ChunkFigures]]
|
|
reports: dict[str, Reports]
|
|
escalated: collections.Counter[int]
|
|
skipped: tuple[str, ...] = ()
|
|
unattributed: float = 0.0
|
|
lacking: int = 0
|
|
unmatched: int = 0
|
|
unread: tuple[str, ...] = ()
|
|
|
|
|
|
def totals(chunks: list[ChunkFigures], skipped: tuple[str, ...] = (), unread: tuple[str, ...] = ()) -> Figures:
|
|
dollars: Dollars = {dimension: collections.defaultdict(float) for dimension in DIMENSIONS}
|
|
for chunk in chunks:
|
|
for dimension in DIMENSIONS:
|
|
for label, amount in chunk.dollars.get(dimension, {}).items():
|
|
dollars[dimension][label] += amount
|
|
routed: dict[Rung, list[ChunkFigures]] = {}
|
|
reports: dict[str, Reports] = {}
|
|
for chunk in chunks:
|
|
if chunk.start:
|
|
routed.setdefault(chunk.start, []).append(chunk)
|
|
for kind, found in chunk.reports.items():
|
|
reports[kind] = reports.get(kind, Reports(0, 0, 0)) + found
|
|
return Figures(
|
|
chunks=len(chunks),
|
|
dollars=dollars,
|
|
routed=routed,
|
|
reports=reports,
|
|
escalated=summed(chunks, lambda chunk: chunk.escalated),
|
|
skipped=skipped,
|
|
unattributed=sum(chunk.unattributed for chunk in chunks),
|
|
lacking=sum(chunk.lacking for chunk in chunks),
|
|
unmatched=sum(chunk.start is not None and chunk.fixed_later is None for chunk in chunks),
|
|
unread=unread,
|
|
)
|
|
|
|
|
|
Kind = typing.Literal["rounds", "unfinished", "reports"]
|
|
# Each kind of call to the sentence it is put to the human in, filled with its subjects in order.
|
|
SENTENCES: dict[Kind, str] = {
|
|
"rounds": "Chunks routed onto {} took more review rounds past each pass's first than chunks routed onto {}.",
|
|
"unfinished": "Chunks routed onto {} took more unfinished verdicts than chunks routed onto {}.",
|
|
"reports": "{} subagents' reports took more of the worker's context than their searches would have.",
|
|
}
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Call:
|
|
"""A routing change the figures call for. Its fields, not its sentence, tell whether it was raised before. kind names its sentence in SENTENCES; subjects are the two rungs compared, lower first, or the subagent type."""
|
|
kind: Kind
|
|
subjects: tuple[str, ...]
|
|
|
|
def sentence(self) -> str:
|
|
return SENTENCES[self.kind].format(*self.subjects)
|
|
|
|
|
|
def calls(ladder: Ladder, figures: Figures) -> list[Call]:
|
|
"""Each routing change the figures call for, comparing the rungs on ladder."""
|
|
compared = sorted((rung for rung, chunks in figures.routed.items() if len(chunks) >= MEASURED_AT_LEAST and ladder.position(rung) >= 0), key=ladder.position)
|
|
found = []
|
|
for lower, upper in zip(compared, compared[1:]):
|
|
measures: tuple[tuple[Kind, typing.Callable[[ChunkFigures], int]], ...] = (("rounds", lambda chunk: chunk.rounds_past_first), ("unfinished", lambda chunk: chunk.unfinished))
|
|
for kind, of in measures:
|
|
if mean(figures.routed[lower], of) > mean(figures.routed[upper], of):
|
|
found.append(Call(kind, (str(lower), str(upper))))
|
|
# Characters stand in for tokens: reports and the tool results their subagents read are both text the worker would read, so their sums compare their shares of its context.
|
|
found += [Call("reports", (kind,)) for kind, reports in sorted(figures.reports.items()) if reports.subagents >= MEASURED_AT_LEAST and reports.report_chars > reports.read_chars]
|
|
return found
|
|
|
|
|
|
def mean(chunks: list[ChunkFigures], of: typing.Callable[[ChunkFigures], int]) -> float:
|
|
return statistics.mean(map(of, chunks))
|
|
|
|
|
|
def summed(chunks: list[ChunkFigures], of: typing.Callable[[ChunkFigures], collections.Counter]) -> collections.Counter:
|
|
return sum(map(of, chunks), collections.Counter())
|