157 lines
8.1 KiB
Python
157 lines
8.1 KiB
Python
"""Whether a read asks for lines of a file already in the agent's context, for the `repeat-read` rule.
|
|
|
|
A line is in context when a read since the transcript thread's last compaction showed it at its line number, as the file holds it now.
|
|
Against a Read, no line is in context 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."""
|
|
import dataclasses
|
|
import datetime
|
|
import itertools
|
|
import os
|
|
import re
|
|
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 command_lines.shell_command import resolved, simple_commands
|
|
|
|
# The lines a Read without a limit returns.
|
|
READ_LIMIT = 2000
|
|
# A line of a Read result: its number, then a tab, or an arrow in older Claude Code versions.
|
|
NUMBERED = re.compile(r" *(\d+)[\t→](.*)", re.DOTALL)
|
|
HEAD_COUNT = re.compile(r"-(?:n ?)?(\d+)")
|
|
SED_RANGE = re.compile(r"(\d+)(?:,(\d+|\$))?p")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Asked:
|
|
path: str
|
|
first: int
|
|
# None: to the file's end.
|
|
last: int | None
|
|
|
|
|
|
def read_breaches(call: Call, home: str) -> list[Breach]:
|
|
"""The repeat-read breaches of a call, read from the calling agent's transcript and the asked file; none where either cannot be read, or the file is not a regular file."""
|
|
asked = asked_lines(call.tool, call.given, call.cwd, home)
|
|
changed = modified(asked.path) if asked else None
|
|
entries = read_transcript(call.transcript) if changed and call.transcript else []
|
|
if asked is None or changed is None or isinstance(entries, Failure):
|
|
return []
|
|
shown = shown_lines(current_thread(entries), call.tool, asked.path, changed, home)
|
|
# No line past the last one shown is in context, so the file's lines through one past it tell as much as all of them, and a long file is not read whole.
|
|
return repeat_breaches(asked, first_lines(asked.path, max(shown) + 1), shown, call.cwd) if shown else []
|
|
|
|
|
|
def modified(path: str) -> str | None:
|
|
"""When a regular file last changed, stamped as a transcript entry is; None for any other path, such as a FIFO, whose read could hang."""
|
|
try:
|
|
status = os.stat(path)
|
|
except (OSError, ValueError):
|
|
return None
|
|
return datetime.datetime.fromtimestamp(status.st_mtime, datetime.UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z") if stat.S_ISREG(status.st_mode) else None
|
|
|
|
|
|
def first_lines(path: str, count: int) -> list[str]:
|
|
"""The file's first count lines, without their line ends; none where it cannot be read as UTF-8."""
|
|
try:
|
|
with open(path, encoding="utf-8") as file:
|
|
return [line.removesuffix("\n") for line in itertools.islice(file, count)]
|
|
except (OSError, UnicodeDecodeError):
|
|
return []
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Used:
|
|
"""A successful result in the thread of a call that read lines of a file or wrote it."""
|
|
tool: object
|
|
# None for a write.
|
|
asked: Asked | None
|
|
result: str
|
|
# The result's entry's timestamp.
|
|
stamp: str
|
|
|
|
|
|
def repeat_breaches(asked: Asked, lines: list[str], shown: dict[int, str], cwd: str) -> list[Breach]:
|
|
"""shown is what shown_lines found, and lines the asked file's lines now, read at least through one past the last shown line."""
|
|
last = len(lines) if asked.last is None else min(asked.last, len(lines))
|
|
if not in_context(asked.first, last, lines, shown):
|
|
return []
|
|
named = f"line {last} of {asked.path} is" if asked.first == last else f"lines {asked.first}-{last} of {asked.path} are"
|
|
return [Breach("repeat-read", f"{named} in your context, unchanged since your last read of it", cwd)]
|
|
|
|
|
|
def in_context(first: int, last: int, lines: list[str], shown: dict[int, str]) -> bool:
|
|
return first <= last and all(shown.get(number) == lines[number - 1] for number in range(first, last + 1))
|
|
|
|
|
|
def asked_lines(tool: object, given: object, cwd: str, home: str) -> Asked | None:
|
|
"""The lines a tool call reads; None for any call but a Read and the Bash reads the rule names, such as one whose output goes elsewhere."""
|
|
if not isinstance(given, dict):
|
|
return None
|
|
return read_asked(given, cwd, home) if tool == "Read" else shell_asked(text(given, "command"), cwd, home) if tool == "Bash" else None
|
|
|
|
|
|
def read_asked(given: dict, cwd: str, home: str) -> Asked | None:
|
|
path, offset, limit = text(given, "file_path"), given.get("offset"), given.get("limit")
|
|
first = offset if isinstance(offset, int) and offset > 0 else 1
|
|
return Asked(resolved(cwd, path, home), first, first + (limit if isinstance(limit, int) and limit > 0 else READ_LIMIT) - 1) if path else None
|
|
|
|
|
|
# reading_cost/shell_reads.py parses a shell read too, loosely: a measure of reading counts any file a reader names, while a rule that blocks must match only a read whose lines it knows exactly.
|
|
def shell_asked(command: str, cwd: str, home: str) -> Asked | None:
|
|
simples = simple_commands(command, cwd, home) if not set("|<>") & set(command) else []
|
|
if len(simples) != 1 or len(simples[0].words) < 2 or simples[0].words[-1].startswith("-"):
|
|
return None
|
|
(name, *options, path), directory = simples[0].words, simples[0].directory
|
|
path = resolved(directory, path, home)
|
|
if name == "cat" and not options:
|
|
return Asked(path, 1, None)
|
|
if name == "head" and (counted := HEAD_COUNT.fullmatch(" ".join(options) or "-10")):
|
|
return Asked(path, 1, int(counted.group(1)))
|
|
if name == "sed" and len(options) == 2 and options[0] == "-n" and (ranged := SED_RANGE.fullmatch(options[1])):
|
|
start, end = ranged.groups()
|
|
return Asked(path, int(start), None if end == "$" else int(end or start))
|
|
return None
|
|
|
|
|
|
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."""
|
|
used = results_on(thread, path, home)
|
|
# Claude Code's Edit and Write refuse such a file until a Read, and a Bash read does not count as one.
|
|
touched = max((use.stamp for use in used if use.tool == "Read" or use.tool in FILE_TOOLS), default="")
|
|
if asker == "Read" and changed > touched:
|
|
return {}
|
|
shown: dict[int, str] = {}
|
|
for use in used:
|
|
shown |= result_lines(use.tool, use.asked, use.result) if use.asked else {}
|
|
return shown
|
|
|
|
|
|
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[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:
|
|
"""The file an Edit, MultiEdit or Write call writes; empty for any other call."""
|
|
return resolved(cwd, path, home) if tool in FILE_TOOLS and isinstance(given, dict) and (path := text(given, "file_path")) else ""
|
|
|
|
|
|
def result_lines(tool: object, asked: Asked, result: str) -> dict[int, str]:
|
|
if tool == "Read":
|
|
return {int(numbered.group(1)): numbered.group(2) for line in result.split("\n") if (numbered := NUMBERED.fullmatch(line))}
|
|
# Claude Code cuts a long Bash output short with a note: before the output, "<persisted-output>\n" or "<truncated-output>\n" and a few more lines, or after it, "\n\n... [N lines truncated] ...".
|
|
# The note's lines would take the numbers of the file's, so such an output shows no lines.
|
|
if result.startswith(("<persisted-output>\n", "<truncated-output>\n")) or result.endswith(" lines truncated] ..."):
|
|
return {}
|
|
return {number: line for number, line in enumerate(result.split("\n"), asked.first) if asked.last is None or number <= asked.last}
|