Parse each transcript entry once into a typed value naming its kind, and have every consumer read those types

This commit is contained in:
2026-11-09 00:12:13 +01:00
parent bc2d7d6d5e
commit b83c1ddf19
20 changed files with 408 additions and 254 deletions
+12 -19
View File
@@ -3,7 +3,7 @@ import collections
import dataclasses
import re
from claude_code.entries import assistant_parts, parts, tool_call, tool_result
from claude_code.entries import AssistantTurn, Entry, tool_calls, ToolResults
from branch_chunks.cut import BranchChunk
from git_cli.git_command import commits
from routing_figures.dollars import costed, Dollars, Message, messages, recorded_costs, Spent
@@ -21,7 +21,7 @@ class Subagent:
"""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]
entries: list[Entry]
@dataclasses.dataclass(frozen=True)
@@ -29,7 +29,7 @@ class Chunk:
"""outcomes holds each outcomes file archived for the chunk, one per transcript copy, each opening with its task; worker is its finished transcript's entries."""
project: str
outcomes: list[str]
worker: list[dict]
worker: list[Entry]
subagents: list[Subagent]
@@ -137,15 +137,9 @@ def escalations(launches: dict[str, tuple[str, dict]]) -> collections.Counter[in
return collections.Counter(int(found[1]) for found in opened if found)
def agent_calls(worker: list[dict]) -> dict[str, tuple[str, dict]]:
def agent_calls(worker: list[Entry]) -> dict[str, tuple[str, dict]]:
"""The timestamp and the arguments of each Agent call, by the call's id."""
found = {}
for entry in worker:
for part in assistant_parts(entry):
call = tool_call(part)
if call and call[0] == "Agent" and isinstance(part, dict) and isinstance(call[1], dict):
found[str(part.get("id"))] = (str(entry.get("timestamp", "")), call[1])
return found
return {call.id: (turn.timestamp, call.input) for turn, call in tool_calls(worker) if call.name == "Agent"}
def rung_at(cycles: list[tuple[str, str]], timestamp: str) -> Rung | str:
@@ -160,23 +154,22 @@ def to_the_second(timestamp: str) -> str:
return timestamp[:19] + "Z"
def subagent_rung(entries: list[dict], arguments: dict, worker: Rung | str) -> Rung:
def subagent_rung(entries: list[Entry], arguments: dict, worker: Rung | str) -> Rung:
"""The model the subagent ran, at the effort its Agent call set, or else at the worker's."""
model = next((message["model"] for entry in entries if entry.get("type") == "assistant" and isinstance(message := entry.get("message"), dict) and isinstance(message.get("model"), str)), str(arguments.get("model") or "unknown"))
model = next((entry.model for entry in entries if isinstance(entry, AssistantTurn) and entry.model), str(arguments.get("model") or "unknown"))
effort = arguments.get("effort") or (worker.effort if isinstance(worker, Rung) else "")
return Rung(model, str(effort))
def results(entries: list[dict]) -> dict[str, str]:
def results(entries: list[Entry]) -> dict[str, str]:
"""Each tool result's text by the id of the call it answers."""
return {result.tool_use_id: result.text for entry in entries for part in parts(entry) if (result := tool_result(part))}
return {result.tool_use_id: result.text for entry in entries if isinstance(entry, ToolResults) for result in entry.results}
def commit_titles(worker: list[dict], home: str) -> list[str]:
def commit_titles(worker: list[Entry], home: str) -> list[str]:
"""The title of each commit the worker's Bash calls made, in order, each once: a commit retried after a failed hook repeats its title."""
return list(dict.fromkeys(
commit.messages[0].split("\n", 1)[0]
for entry in worker for part in assistant_parts(entry)
if (found := tool_call(part)) and found[0] == "Bash" and isinstance(found[1], dict)
for commit in commits(simple_commands(str(found[1].get("command", "")), str(entry.get("cwd") or "/"), home), home) if commit.messages
for turn, call in tool_calls(worker) if call.name == "Bash"
for commit in commits(simple_commands(str(call.input.get("command", "")), turn.cwd or "/", home), home) if commit.messages
))