Answer stability round 1: 1 🔴, 8 🟡 (0 misread); read only a regular file, only as far as the shown lines, skip cut-short output, and test the surviving mutants

This commit is contained in:
2026-10-28 14:14:53 +01:00
parent e41f4e0979
commit b1a90abff4
12 changed files with 203 additions and 67 deletions
+45 -23
View File
@@ -1,15 +1,14 @@
"""Whether a read asks for lines of a file already in the agent's context, for the `repeat-read` rule.
A read is a Read call, or a Bash command that is one `cat <file>`, `head [-n <N> | -<N>] <file>` or `sed -n '<A>[,<B>]p' <file>`, with no pipe or redirect.
Lines are in context when earlier reads' results show each of them, as the file holds it now and at its line number now, in the transcript's thread since its last compaction.
main.py reads the file and the transcript; this module only decides."""
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 re
from rule_hook.rules import Breach
from rule_hook.call import FILE_TOOLS, text
from claude_code.entries import parts, tool_call, tool_result, tool_use_id
from shell_command import resolved, simple_commands
from rule_hook.call import text
# The lines a Read without a limit returns.
READ_LIMIT = 2000
@@ -27,24 +26,21 @@ class Asked:
last: int | None
def repeat_breaches(asked: Asked, lines: list[str], thread: list[dict], cwd: str, home: str) -> list[Breach]:
"""thread is the calling agent's transcript thread since its last compaction, and lines the asked file's lines now."""
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_lines(thread, asked.path, home)):
if not in_context(asked.first, last, lines, shown):
return []
return [Breach("repeat-read", f"lines {asked.first}-{last} of {asked.path} are in your context, unchanged since you read them; read them there", cwd)]
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 file_lines(content: str) -> list[str]:
return content.removesuffix("\n").split("\n") if content else []
def asked_lines(tool: object, given: object, cwd: str, home: str) -> Asked | None:
"""The lines a tool call reads; None for a call that is no read, or whose output goes elsewhere than the agent's context."""
"""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
@@ -73,21 +69,47 @@ def shell_asked(command: str, cwd: str, home: str) -> Asked | None:
return None
def shown_lines(thread: list[dict], path: 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."""
reads: dict[str, tuple[object, Asked]] = {}
def shown_lines(thread: list[dict], 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((stamp for tool, _, _, stamp in used if tool == "Read" or tool in FILE_TOOLS), default="")
if asker == "Read" and changed > touched:
return {}
shown: dict[int, str] = {}
for entry in thread:
for part in parts(entry):
if (call := tool_call(part)) and (called := tool_use_id(part)) and (asked := asked_lines(*call, str(entry.get("cwd", "")), home)) and asked.path == path:
reads[called] = (call[0], asked)
elif (result := tool_result(part)) and not result.failed and (read := reads.get(result.tool_use_id)):
shown |= result_lines(*read, result.text)
for tool, asked, result, _ in used:
shown |= result_lines(tool, asked, result) if asked else {}
return shown
def results_on(thread: list[dict], path: str, home: str) -> list[tuple[object, Asked | None, str, str]]:
"""Each successful result in the thread of a call that read lines of path or wrote it: the call's tool, the lines it read (None for a write), the result's text and its entry's timestamp."""
calls: dict[str, tuple[object, Asked | None]] = {}
found: list[tuple[object, Asked | None, str, str]] = []
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(*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((use[0], use[1], result.text, text(entry, "timestamp")))
return found
def written(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))}
# A Bash output Claude Code cut short shows a note in place of the lines it left out, so those lines differ from the file's and count as unread.
# 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}