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
+9 -15
View File
@@ -12,8 +12,8 @@ import stat
from rule_hook.rules import Breach
from rule_hook.call import Call, FILE_TOOLS, text
from claude_code.transcript import current_thread, read_transcript
from claude_code.entries import Entry, tool_calls, ToolResults
from failure import Failure
from claude_code.entries import parts, tool_call, tool_result, tool_use_id
from command_lines.shell_command import resolved, simple_commands
# The lines a Read without a limit returns.
@@ -116,7 +116,7 @@ def shell_asked(command: str, cwd: str, home: str) -> Asked | None:
return None
def shown_lines(thread: list[dict], asker: str, path: str, changed: str, home: str) -> dict[int, str]:
def shown_lines(thread: list[Entry], asker: str, path: str, changed: str, home: str) -> dict[int, str]:
"""Each line number of path that a successful read in the thread showed, with the line its latest read showed; none against a Read when the file changed after the agent last read it with Read or wrote it with Edit, MultiEdit or Write, or when it never did.
changed is when the file last changed, stamped as the transcript stamps its entries, so the two compare as text."""
@@ -131,20 +131,14 @@ def shown_lines(thread: list[dict], asker: str, path: str, changed: str, home: s
return shown
def results_on(thread: list[dict], path: str, home: str) -> list[Used]:
def results_on(thread: list[Entry], path: str, home: str) -> list[Used]:
"""Each successful result in the thread of a call that read lines of path or wrote it."""
calls: dict[str, tuple[object, Asked | None]] = {}
found: list[Used] = []
for entry in thread:
cwd = str(entry.get("cwd", ""))
for part in parts(entry):
if (call := tool_call(part)) and (called := tool_use_id(part)):
asked = asked_lines(*call, cwd, home)
if (asked.path if asked else written_path(*call, cwd, home)) == path:
calls[called] = (call[0], asked)
elif (result := tool_result(part)) and not result.failed and (use := calls.get(result.tool_use_id)):
found.append(Used(use[0], use[1], result.text, text(entry, "timestamp")))
return found
calls: dict[str, tuple[str, Asked | None]] = {}
for turn, call in tool_calls(thread):
asked = asked_lines(call.name, call.input, turn.cwd, home)
if call.id and (asked.path if asked else written_path(call.name, call.input, turn.cwd, home)) == path:
calls[call.id] = (call.name, asked)
return [Used(*calls[result.tool_use_id], result.text, entry.timestamp) for entry in thread if isinstance(entry, ToolResults) for result in entry.results if not result.failed and result.tool_use_id in calls]
def written_path(tool: object, given: object, cwd: str, home: str) -> str: