Parse each transcript entry once into a typed value naming its kind, and have every consumer read those types
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -3,7 +3,6 @@ import typing
|
||||
|
||||
from rule_hook.rules import Breach
|
||||
from rule_hook.call import Call, text
|
||||
from claude_code.entries import content
|
||||
from failure import Failure
|
||||
from git_cli import git_repo
|
||||
from rule_hook.git import guarded_git
|
||||
@@ -11,6 +10,7 @@ from model_routing.worker_rungs import handed
|
||||
from model_routing.ladder import position, review_rung, ReviewKind, Rung
|
||||
from review_rules.review_passes import prose, REVIEWERS
|
||||
from claude_code.transcript import read_transcript
|
||||
from claude_code.entries import UserText
|
||||
|
||||
# The pass each agent under agents/ reviews.
|
||||
PASS_OF = {agent: kind for kind, agent in REVIEWERS.items()}
|
||||
@@ -57,9 +57,8 @@ def skill_breaches(call: Call, environ: typing.Mapping[str, str]) -> list[Breach
|
||||
def opening_prompt(transcript: str) -> str:
|
||||
"""The prompt a transcript opens with, such as a subagent's from its Agent call; empty where it cannot be read."""
|
||||
entries = read_transcript(transcript) if transcript else []
|
||||
first = entries[0] if isinstance(entries, list) and entries else {}
|
||||
prompt = content(first) if first.get("type") == "user" else None
|
||||
return prompt.lstrip() if isinstance(prompt, str) else ""
|
||||
first = entries[0] if isinstance(entries, list) and entries else None
|
||||
return first.text.lstrip() if isinstance(first, UserText) else ""
|
||||
|
||||
|
||||
def review_kind(given: dict[str, typing.Any]) -> ReviewKind | None:
|
||||
|
||||
Reference in New Issue
Block a user