"""Tally the shell calls of archived chunks by command pattern, such as `tea pr create`.""" import dataclasses import datetime import posixpath import re from claude_code.entries import AssistantTurn, parse_entry, ToolCall, ToolResults from command_lines.shell_command import segments # A subcommand word, such as `pr` in `tea pr create`. Any other word ends the pattern. SUBCOMMAND = re.compile(r"[a-z][a-z0-9-]*") # The words after the tool a pattern keeps at most. SUBCOMMANDS = 2 CHARACTERS_PER_TOKEN = 4 @dataclasses.dataclass(frozen=True) class Call: command: str result: str seconds: float @dataclasses.dataclass(frozen=True) class Row: pattern: str chunks: int calls: int command_tokens: int result_tokens: int seconds: int def pattern(command: str) -> str: """The call's first simple command other than `cd`: its tool's name, then up to two subcommand words.""" commands = segments(command) for words in commands: if words[0] != "cd": kept = [] for word in words[1:1 + SUBCOMMANDS]: if not SUBCOMMAND.fullmatch(word): break kept.append(word) return " ".join([posixpath.basename(words[0]), *kept]) return "cd" if commands else "(unparsed)" def calls(thread: bytes) -> list[Call]: """Each Bash call in a transcript, with its result's text and the seconds until the result; a call without a result has an empty one, taking no time.""" asked: dict[str, tuple[str, str]] = {} answered: dict[str, tuple[str, str]] = {} for line in thread.splitlines(): entry = parse_entry(line) if isinstance(entry, AssistantTurn): for part in entry.parts: if isinstance(part, ToolCall) and part.name == "Bash" and isinstance(command := part.input.get("command"), str): asked.setdefault(part.id, (command, entry.timestamp)) elif isinstance(entry, ToolResults): # Claude Code logs some results twice; the first carries the time the tool ended. for found in entry.results: answered.setdefault(found.tool_use_id, (found.text, entry.timestamp)) return [Call(command, answered.get(id, ("", ""))[0], elapsed(at, answered.get(id, ("", ""))[1])) for id, (command, at) in asked.items()] def elapsed(start: str, end: str) -> float: """Seconds from one transcript timestamp to another; 0 where either is missing or unreadable.""" try: return max(0.0, (datetime.datetime.fromisoformat(end) - datetime.datetime.fromisoformat(start)).total_seconds()) except ValueError: return 0.0 def tally(chunks: list[list[bytes]]) -> list[Row]: """One row per pattern over the chunks, each given as its threads, most calls first.""" found: dict[str, list[tuple[int, Call]]] = {} for index, threads in enumerate(chunks): for thread in threads: for each in calls(thread): found.setdefault(pattern(each.command), []).append((index, each)) rows = [Row(name, len({index for index, _ in made}), len(made), sum(len(each.command) for _, each in made) // CHARACTERS_PER_TOKEN, sum(len(each.result) for _, each in made) // CHARACTERS_PER_TOKEN, round(sum(each.seconds for _, each in made))) for name, made in found.items()] return sorted(rows, key=lambda row: (-row.calls, row.pattern)) def table(rows: list[Row]) -> str: lines = ["| Pattern | Chunks | Calls | Command tokens | Result tokens | Tool seconds |", "|---|---|---|---|---|---|"] return "\n".join([*lines, *(f"| {row.pattern} | {row.chunks} | {row.calls} | {row.command_tokens} | {row.result_tokens} | {row.seconds} |" for row in rows)])