|
|
|
@@ -24,7 +24,7 @@ PROMPT = "prompt"
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
|
|
|
class Subagent:
|
|
|
|
|
"""What the subagent's .meta.json names, its agentType and the toolUseId of the worker's Agent call that launched it, each empty where it names none; and its transcript's entries."""
|
|
|
|
|
"""kind is the agentType and launched_by the toolUseId of the launching Agent call, as the subagent's .meta.json names them, each empty where it names none; entries are its transcript's entries."""
|
|
|
|
|
kind: str
|
|
|
|
|
launched_by: str
|
|
|
|
|
entries: list[dict]
|
|
|
|
@@ -113,7 +113,7 @@ def add(tokens: dict[str, collections.Counter[str]], project: str, kind: str, ru
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rung_at(cycles: list[tuple[str, str]], timestamp: str) -> Rung | str:
|
|
|
|
|
"""The rung the worker line stamped first at or after timestamp names: the rung of the cycle that ran then. A line off the ladder names itself."""
|
|
|
|
|
"""The rung of the cycle running at timestamp: the worker named by the first outcome stamped at or after it, or else by the last outcome. A worker off the ladder is returned as its text."""
|
|
|
|
|
second = timestamp[:19] + "Z"
|
|
|
|
|
found = next((rung for stamp, rung in cycles if stamp >= second), cycles[-1][1] if cycles else "unrouted")
|
|
|
|
|
route = route_from(found)
|
|
|
|
@@ -128,10 +128,7 @@ def subagent_rung(entries: list[dict], arguments: dict, worker: Rung | str) -> R
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def spend(entries: list[dict]) -> list[tuple[str, list[str], int]]:
|
|
|
|
|
"""Each message's timestamp, the tools whose results it took in, and its input, cache-written and output tokens.
|
|
|
|
|
|
|
|
|
|
Claude Code writes one entry per content block of a message, each with the message's usage, so a message counts once.
|
|
|
|
|
"""
|
|
|
|
|
"""Each message's timestamp, the tools whose results it took in, and its input, cache-written and output tokens, counted once per message, as reading_cost's tokens_in counts them."""
|
|
|
|
|
messages: dict[str, tuple[str, object, list[str]]] = {}
|
|
|
|
|
for entry in entries:
|
|
|
|
|
if entry.get("type") != "assistant" or not isinstance(message := entry.get("message"), dict):
|
|
|
|
@@ -177,7 +174,7 @@ def rounds_past_first(worker: list[dict], home: str) -> int:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def due(cycle: int, last_measured: int) -> bool:
|
|
|
|
|
"""Whether the archive's cycle count has run CYCLES_PER_CHECK past the cycle the figures were last measured at: counting from the last measurement, a count another loop pushed past a multiple still measures."""
|
|
|
|
|
"""Whether CYCLES_PER_CHECK cycles have closed since the figures were last measured. Counting from the last measurement still measures when another loop's cycles carried the count past a multiple of CYCLES_PER_CHECK."""
|
|
|
|
|
return cycle >= last_measured + CYCLES_PER_CHECK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -201,7 +198,7 @@ def calls(figures: Figures) -> list[str]:
|
|
|
|
|
for what, of in (("review rounds past each pass's first", lambda chunk: chunk.rounds_past_first), ("unfinished verdicts", lambda chunk: chunk.unfinished)):
|
|
|
|
|
if mean(figures.routed[lower], of) > mean(figures.routed[upper], of):
|
|
|
|
|
found.append(f"Chunks routed onto {lower} took more {what} than chunks routed onto {upper}.")
|
|
|
|
|
# Characters stand in for tokens: both sides are text the worker would read. Both would fill the same peak context, so comparing the sums compares their shares of it.
|
|
|
|
|
# Characters stand in for tokens: reports and searches are both text the worker would read, so their sums compare their shares of its context.
|
|
|
|
|
found += [f"{kind} subagents' reports took more of the worker's context than their searches would have." for kind, reports in sorted(figures.reports.items()) if reports.subagents >= MEASURED_AT_LEAST and reports.report > reports.searches]
|
|
|
|
|
return found
|
|
|
|
|
|
|
|
|
@@ -214,7 +211,7 @@ def render(figures: Figures, found: list[str], at: str, cycle: int, since: str)
|
|
|
|
|
"""The figures as markdown; MEASURED_AT reads the cycle back. since is the latest reset's stamp, empty for none."""
|
|
|
|
|
sections = [
|
|
|
|
|
"# Routing figures",
|
|
|
|
|
f"Measured {at}, at cycle {cycle} across loop-claude's archive, from {figures.chunks} finished chunk{'' if figures.chunks == 1 else 's'}{f' that finished after the reset of {since}' if since else ''}. Tokens are input, cache-written and output tokens; cache reads are left out. A message's tokens count towards the tools whose results it took in, split evenly.",
|
|
|
|
|
f"Measured {at}, at cycle {cycle} across loop-claude's archive, from {figures.chunks} finished chunk{'' if figures.chunks == 1 else 's'}{f' that finished after the reset of {since}' if since else ''}. Tokens are input, cache-written and output tokens; cache reads are left out. A message's tokens count towards the tools whose results it took in, split evenly; a message that took in no tool result counts towards `prompt`.",
|
|
|
|
|
"## Calls for a routing change",
|
|
|
|
|
"".join(f"- {call}\n" for call in found).rstrip("\n") or "None.",
|
|
|
|
|
*[part for dimension in DIMENSIONS for part in (f"## Tokens by {dimension}", table((dimension.capitalize(), "Tokens"), [(label, str(tokens)) for label, tokens in figures.tokens[dimension].most_common()]))],
|
|
|
|
|