Answer principles round 1: 0 🔴, 2 🟡 (0 misread); kill the surviving mutants and split repeat-read's tests to one behaviour each

This commit is contained in:
2026-10-28 13:05:00 +01:00
parent 8602b8a7ee
commit 488d9b0eee
2 changed files with 63 additions and 15 deletions
+6
View File
@@ -54,6 +54,12 @@ class FirstTimestampedLine(unittest.TestCase):
self.assertEqual(transcript.first_timestamped_line(b""), b"")
class CurrentThread(unittest.TestCase):
def test_a_parent_cycle_ends_the_thread(self):
looped = [{"uuid": "a", "parentUuid": "b"}, {"uuid": "b", "parentUuid": "a"}]
self.assertEqual([entry["uuid"] for entry in transcript.current_thread(looped)], ["a", "b"])
class HumanTurns(unittest.TestCase):
def test_counts_typed_turns_and_not_tool_results(self):
self.assertEqual(transcript.human_turns([
+57 -15
View File
@@ -374,7 +374,14 @@ class RepeatRead(unittest.TestCase):
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_read_without_a_limit_asks_for_the_whole_file(self):
self.read(1, 10)
self.assertEqual(self.found("Read", {"file_path": self.file}), [])
def test_a_read_result_in_the_older_arrow_format_counts(self):
self.used("Read", {"file_path": self.file, "offset": 1, "limit": 10}, "\n".join(f"{n:6}→{self.LINES[n - 1]}" for n in range(1, 11)))
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 1, "limit": 10})), ["repeat-read"])
def test_a_compaction_lets_the_read_through(self):
self.read(1, 10)
@@ -382,11 +389,18 @@ class RepeatRead(unittest.TestCase):
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)
def change_the_first_line(self) -> None:
pathlib.Path(self.file).write_text("changed\n" + "\n".join(self.LINES[1:]) + "\n")
def test_a_changed_line_lets_the_read_through(self):
self.read(1, 10)
self.change_the_first_line()
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_lines_that_did_not_change_stay_in_context(self):
self.read(1, 10)
self.change_the_first_line()
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 2, "limit": 9})), ["repeat-read"])
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)
@@ -398,13 +412,21 @@ class RepeatRead(unittest.TestCase):
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"]:
def test_head_and_sed_of_lines_read_are_blocked(self):
self.read(1, 20)
for command in [f"head -n 5 {self.file}", f"head -n5 {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")
def test_a_cat_asks_for_the_whole_file(self):
self.read(1, 20)
self.assertEqual(self.found("Bash", {"command": f"cat {self.file}"}), [])
def test_a_sed_output_puts_its_lines_in_context(self):
self.used("Bash", {"command": f"sed -n '1,20p' {self.file}"}, self.shown(1, 20))
self.assertEqual(rules(self.found("Read", {"file_path": self.file, "offset": 4, "limit": 3})), ["repeat-read"])
def test_a_cat_output_puts_every_line_in_context(self):
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"])
@@ -414,22 +436,42 @@ class RepeatRead(unittest.TestCase):
with self.subTest(command):
self.assertEqual(self.found("Bash", {"command": command}), [])
def test_a_truncated_output_holds_only_the_lines_it_shows(self):
def test_a_truncated_output_leaves_out_the_lines_it_cut(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}"}), [])
def test_a_truncated_output_holds_the_lines_it_shows(self):
self.used("Bash", {"command": f"cat {self.file}"}, self.shown(1, 5) + "\n... [25 lines truncated] ...")
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)
def called(self, agent_id: str = "") -> Call:
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(read_breaches(subagent, "/h"), [], "the parent's reads are not in the subagent's context")
self.assertEqual(rules(read_breaches(parsed_call(given), "/h")), ["repeat-read"])
return parsed_call(given | ({"agent_id": agent_id, "agent_type": "Explore"} if agent_id else {}))
def test_a_subagent_s_transcript_is_beside_the_session_s(self):
self.assertEqual(self.called("a1").transcript, f"{self.folder}/s/subagents/agent-a1.jsonl")
def test_the_session_s_reads_are_not_in_a_subagent_s_context(self):
self.read(1, 10)
self.assertEqual(read_breaches(self.called("a1"), "/h"), [])
def test_the_session_is_judged_on_its_own_transcript(self):
self.read(1, 10)
self.assertEqual(rules(read_breaches(self.called(), "/h")), ["repeat-read"])
def test_a_missing_transcript_blocks_nothing(self):
self.assertEqual(read_breaches(Call("Read", {"file_path": self.file}, self.folder, "", f"{self.folder}/none.jsonl"), "/h"), [])
def test_the_hook_blocks_a_repeated_read_without_offering_an_override(self):
self.read(1, 10)
self.used("Bash", {"command": f"head -n 5 {self.file}"}, self.shown(1, 5))
for tool, given in [("Read", {"file_path": self.file, "offset": 1, "limit": 10}), ("Bash", {"command": f"head -n 5 {self.file}"})]:
with self.subTest(tool):
ran = hook({"cwd": self.folder, "tool_name": tool, "tool_input": given, "transcript_path": self.transcript}, self.folder)
self.assertEqual(ran.returncode, 2)
self.assertIn("Blocked by rule repeat-read: ", ran.stderr)
self.assertNotIn("rule-override", ran.stderr)
class Hook(unittest.TestCase):
def setUp(self):