Block a read of file lines already in the agent's context, unless a compaction dropped them or the file changed
This commit is contained in:
@@ -11,7 +11,8 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from rule_hook.rules import blocks, Breach, check, command_breaches, file_breaches, overrides
|
||||
from rule_hook.main import breaches_of
|
||||
from rule_hook.call import Call
|
||||
from rule_hook.call import Call, parsed_call
|
||||
from rule_hook.repeat_read import repeat_breaches
|
||||
from git_command import Commit, commit_message, commits
|
||||
from test_rank_todo import DECISIONS, README, TODO
|
||||
from test_env import isolated_env
|
||||
@@ -328,6 +329,109 @@ class ReviewerReport(unittest.TestCase):
|
||||
self.assertEqual(self.breached("Write", "/r/README.md", "general-purpose"), [])
|
||||
|
||||
|
||||
class RepeatRead(unittest.TestCase):
|
||||
LINES = [f"line {n}" for n in range(1, 31)]
|
||||
|
||||
def setUp(self):
|
||||
self.folder = tempfile.mkdtemp()
|
||||
self.addCleanup(subprocess.run, ["rm", "-rf", self.folder])
|
||||
self.file = f"{self.folder}/f.txt"
|
||||
pathlib.Path(self.file).write_text("\n".join(self.LINES) + "\n")
|
||||
self.transcript = f"{self.folder}/s.jsonl"
|
||||
self.entries: list[dict] = []
|
||||
|
||||
def add(self, entry: dict, parent: str | None = "last") -> str:
|
||||
uuid = f"u{len(self.entries)}"
|
||||
parent = (self.entries[-1]["uuid"] if self.entries else None) if parent == "last" else parent
|
||||
self.entries.append({"uuid": uuid, "parentUuid": parent, "cwd": self.folder} | entry)
|
||||
pathlib.Path(self.transcript).write_text("\n".join(json.dumps(entry) for entry in self.entries) + "\n")
|
||||
return uuid
|
||||
|
||||
def used(self, tool: str, given: dict, result: str, error: bool = False) -> None:
|
||||
tool_id = f"toolu_{len(self.entries)}"
|
||||
self.add({"type": "assistant", "message": {"content": [{"type": "tool_use", "id": tool_id, "name": tool, "input": given}]}})
|
||||
self.add({"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": tool_id, "content": result, "is_error": error}]}})
|
||||
|
||||
def read(self, start: int, end: int) -> None:
|
||||
self.used("Read", {"file_path": self.file, "offset": start, "limit": end - start + 1}, "\n".join(f"{n}\t{self.LINES[n - 1]}" for n in range(start, end + 1)))
|
||||
|
||||
def shown(self, start: int, end: int) -> str:
|
||||
return "\n".join(self.LINES[start - 1:end])
|
||||
|
||||
def found(self, tool: str, given: dict, agent: str = "") -> list[Breach]:
|
||||
return repeat_breaches(Call(tool, given, self.folder, agent, self.transcript), "/h")
|
||||
|
||||
def test_a_read_of_lines_already_read_is_blocked_with_a_note(self):
|
||||
self.read(1, 10)
|
||||
found = self.found("Read", {"file_path": self.file, "offset": 2, "limit": 5})
|
||||
self.assertEqual(rules(found), ["repeat-read"])
|
||||
self.assertIn(f"lines 2-6 of {self.file} are in your context", found[0].detail)
|
||||
|
||||
def test_lines_read_in_two_reads_are_in_context(self):
|
||||
self.read(1, 10)
|
||||
self.read(11, 20)
|
||||
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 5, "limit": 10})), ["repeat-read"])
|
||||
|
||||
def test_a_read_past_what_was_read_goes_through(self):
|
||||
self.read(1, 10)
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file, "offset": 5, "limit": 10}), [])
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file}), [], "a Read without a limit asks for the whole file")
|
||||
|
||||
def test_a_compaction_lets_the_read_through(self):
|
||||
self.read(1, 10)
|
||||
self.add({"type": "system", "subtype": "compact_boundary"}, parent=None)
|
||||
self.add({"type": "user", "message": {"content": "This session is being continued"}})
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file, "offset": 1, "limit": 10}), [])
|
||||
|
||||
def test_a_changed_file_lets_the_read_through(self):
|
||||
self.read(1, 10)
|
||||
pathlib.Path(self.file).write_text("changed\n" + "\n".join(self.LINES[1:]) + "\n")
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file, "offset": 1, "limit": 10}), [])
|
||||
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 2, "limit": 9})), ["repeat-read"], "the lines that did not change are still in context")
|
||||
|
||||
def test_a_failed_read_holds_no_lines(self):
|
||||
self.used("Read", {"file_path": self.file, "offset": 1, "limit": 10}, "\n".join(f"{n}\t{self.LINES[n - 1]}" for n in range(1, 11)), error=True)
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file, "offset": 1, "limit": 10}), [])
|
||||
|
||||
def test_a_read_on_a_branch_the_session_left_holds_no_lines(self):
|
||||
start = self.add({"type": "user", "message": {"content": "go"}})
|
||||
self.read(1, 10)
|
||||
self.add({"type": "user", "message": {"content": "go again"}}, parent=start)
|
||||
self.assertEqual(self.found("Read", {"file_path": self.file, "offset": 1, "limit": 10}), [])
|
||||
|
||||
def test_cat_head_and_sed_count_as_reads_both_ways(self):
|
||||
self.used("Bash", {"command": f"sed -n '1,20p' {self.file}"}, self.shown(1, 20))
|
||||
for command in [f"head -n 5 {self.file}", f"head -5 {self.file}", f"head {self.file}", "sed -n '3,8p' f.txt", "sed -n 7p f.txt"]:
|
||||
with self.subTest(command):
|
||||
self.assertEqual(rules(self.found("Bash", {"command": command})), ["repeat-read"])
|
||||
self.assertEqual(self.found("Bash", {"command": f"cat {self.file}"}), [], "only 20 of the 30 lines were read")
|
||||
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 4, "limit": 3})), ["repeat-read"])
|
||||
self.used("Bash", {"command": f"cat {self.file}"}, self.shown(1, 30))
|
||||
self.assertEqual(rules(self.found("Bash", {"command": "sed -n '25,$p' f.txt"})), ["repeat-read"])
|
||||
|
||||
def test_a_read_whose_output_goes_elsewhere_is_not_checked(self):
|
||||
self.used("Bash", {"command": f"cat {self.file}"}, self.shown(1, 30))
|
||||
for command in [f"cat {self.file} > copy", f"cat {self.file} | wc -l", f"cat {self.file} {self.file}", f"head -c 5 {self.file}", f"sed 's/a/b/' {self.file}"]:
|
||||
with self.subTest(command):
|
||||
self.assertEqual(self.found("Bash", {"command": command}), [])
|
||||
|
||||
def test_a_truncated_output_holds_only_the_lines_it_shows(self):
|
||||
self.used("Bash", {"command": f"cat {self.file}"}, self.shown(1, 5) + "\n... [25 lines truncated] ...")
|
||||
self.assertEqual(self.found("Bash", {"command": f"head -n 10 {self.file}"}), [])
|
||||
self.assertEqual(rules(self.found("Bash", {"command": f"head -n 5 {self.file}"})), ["repeat-read"])
|
||||
|
||||
def test_a_subagent_is_judged_on_its_own_transcript(self):
|
||||
self.read(1, 10)
|
||||
given = {"cwd": self.folder, "tool_name": "Read", "tool_input": {"file_path": self.file, "offset": 1, "limit": 10}, "transcript_path": self.transcript}
|
||||
subagent = parsed_call(given | {"agent_id": "a1", "agent_type": "Explore"})
|
||||
self.assertEqual(subagent.transcript, f"{self.folder}/s/subagents/agent-a1.jsonl")
|
||||
self.assertEqual(repeat_breaches(subagent, "/h"), [], "the parent's reads are not in the subagent's context")
|
||||
self.assertEqual(rules(repeat_breaches(parsed_call(given), "/h")), ["repeat-read"])
|
||||
|
||||
def test_a_missing_transcript_blocks_nothing(self):
|
||||
self.assertEqual(repeat_breaches(Call("Read", {"file_path": self.file}, self.folder, "", f"{self.folder}/none.jsonl"), "/h"), [])
|
||||
|
||||
|
||||
class Hook(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.repo = tempfile.mkdtemp()
|
||||
@@ -477,7 +581,7 @@ class Hook(unittest.TestCase):
|
||||
|
||||
def test_help_lists_every_rule(self):
|
||||
helped = subprocess.run([str(HOOK), "--help"], capture_output=True, check=True, env=isolated_env(), text=True).stdout
|
||||
for rule in ["action-pin", "commit-one-line", "docker-run-memory", "ignorefile-sorted", "image-pin", "no-ai-attribution", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]:
|
||||
for rule in ["action-pin", "commit-one-line", "docker-run-memory", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]:
|
||||
self.assertIn(f" {rule}\n The rule: ", helped)
|
||||
self.assertIn("reviewer-report\n The rule: a reviewer writes nothing but its report", helped)
|
||||
self.assertRegex(helped, r" reviewer-report\n( .*\n){2} Takes no override\.\n")
|
||||
|
||||
Reference in New Issue
Block a user