Merge remote-tracking branch 'origin/main' into item-316
CI / tests (pull_request) Successful in 3m32s

# Conflicts:
#	docs/reading-cost.md
This commit is contained in:
2026-11-18 19:52:58 +01:00
124 changed files with 5044 additions and 705 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
"""Whether a Read or a Grep reads a credentials file, or a Bash command names it or searches a folder holding it, for the `env-file-read` rule.
The files are `~/.claude/.env` and every config's `env-file`, not only the calling project's: a worker in one client's project must not read another client's credentials either, and listing every config's file needs no git call, as finding the calling project's would.
The files are `~/.claude/.env` and every config's `env-file`, do-epic's included, not only the calling project's: a worker in one client's project must not read another client's credentials either, and listing every config's file needs no git call, as finding the calling project's would.
A Bash command names one where its text spells the file's path, or one of its words, a redirect's target included, leads to the file from the directory `cd` left the command in. Whether the command reads the file is not judged, so one that only mentions the path is refused too.
It searches a folder holding one where `folder_search.searches` finds that an `ag`, `grep -r` or `rg` reaches the file.
This guards against an agent reading the file in passing, not against one that hides the path, such as behind a glob, a variable of its own, `xargs` or `find -exec`."""
+2 -1
View File
@@ -61,6 +61,7 @@ from rule_hook.round_report import round_report_breaches
from command_lines.shell_command import simple_commands
from project_config import templates
from rule_hook.todo_check import todo_breaches
from rule_hook.transcript_read import transcript_breaches
def main(argv: list[str], stdin: str) -> int:
@@ -96,7 +97,7 @@ def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tup
command = text(call.given, "command")
return command, command.split("\n", 1)[0], bash_breaches(command, call.cwd, home) + read_breaches(call, home) + env_file_breaches(call, home)
if call.tool == "Read":
return "", text(call.given, "file_path"), read_breaches(call, home) + env_file_breaches(call, home)
return "", text(call.given, "file_path"), read_breaches(call, home) + env_file_breaches(call, home) + transcript_breaches(call, home)
if call.tool == "Grep":
# Only env-file-read checks a Grep, and it takes no override, so no marker or log line concerns the call.
return "", "", env_file_breaches(call, home)
+7 -1
View File
@@ -12,7 +12,7 @@ from review_rules.review_folder import REPORTS
from project_config import templates
from command_lines.docker_command import unlimited
RuleName = typing.Literal["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]
RuleName = typing.Literal["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check", "transcript-read"]
@dataclasses.dataclass(frozen=True)
@@ -94,6 +94,12 @@ RULES: dict[RuleName, Rule] = {
"while todo.md has changes, staged or not, a git commit waits until rank_todo.py --check passes on it: the checkout's own bin/rank_todo.py where the checkout is of this setup's repo, else ~/.claude/bin/rank_todo.py",
"a git commit in a checkout whose todo.md has changes, and every git commit where the project's config puts todo.md outside the checkout, where the checkout's commits don't say whether it changed; and a git commit in the config's folder, `~/.claude-projects/<key>/`, whose todo.md has changes",
),
# A Read carries no text for a marker, so no marker waives it.
"transcript-read": Rule(
"an agent never reads a session transcript under ~/.claude/projects with Read, since the file holds every tool result of its session, and reading it costs far more than the lines the agent needs; the agent prints only those lines with grep or jq; the `tool-results/` files that Claude Code saves a long Bash result to are not transcripts and may be read",
"a Read call whose file is a `.jsonl` file under ~/.claude/projects; a Grep or a Bash command that reads one is not checked",
overridable=False,
),
}
# The lookahead ends each reason at the next marker on its line, or at the line's end. The reason is lazy, so it stops at the next marker instead of running on and swallowing the markers after it.
# An HTML marker's closing `-->` stays in its reason; `overrides` strips it.
+40
View File
@@ -1497,6 +1497,46 @@ class CommittedFiles(unittest.TestCase):
self.assertEqual(self.commit().returncode, 0)
class TranscriptRead(unittest.TestCase):
HOME = "/home/h"
TRANSCRIPT = "/home/h/.claude/projects/-home-h-code-acme/abc-123.jsonl"
def breached(self, tool: str, given: dict[str, typing.Any], cwd: str = "/r") -> list[str]:
found = breaches_of(Call(tool, given, cwd), self.HOME, {})
return [rule for rule in rules(found[2]) if rule == "transcript-read"] if found else []
def test_a_read_of_a_session_s_transcript_or_a_subagent_s_is_refused(self):
self.assertEqual(self.breached("Read", {"file_path": self.TRANSCRIPT}), ["transcript-read"])
self.assertEqual(self.breached("Read", {"file_path": "/home/h/.claude/projects/-home-h-code-acme/abc-123/subagents/agent-a1.jsonl"}), ["transcript-read"])
def test_a_read_spelt_from_home_or_from_the_working_directory_is_refused(self):
self.assertEqual(self.breached("Read", {"file_path": "~/.claude/projects/-home-h-code-acme/abc-123.jsonl"}), ["transcript-read"])
self.assertEqual(self.breached("Read", {"file_path": "abc-123.jsonl"}, "/home/h/.claude/projects/-home-h-code-acme"), ["transcript-read"])
def test_the_refusal_names_the_file_and_what_to_use_instead(self):
found = breaches_of(Call("Read", {"file_path": self.TRANSCRIPT}, "/r"), self.HOME, {})
[detail] = [breach.detail for breach in found[2] if breach.rule == "transcript-read"]
self.assertIn(self.TRANSCRIPT, detail)
self.assertIn("grep or jq", detail)
def test_the_refusal_is_logged_in_the_checkout_of_the_working_directory(self):
found = breaches_of(Call("Read", {"file_path": self.TRANSCRIPT}, "/r"), self.HOME, {})
self.assertEqual([breach.directory for breach in found[2] if breach.rule == "transcript-read"], ["/r"])
def test_a_read_of_a_long_result_claude_code_saved_to_a_file_or_of_a_memory_file_passes(self):
for path in ("/home/h/.claude/projects/-home-h-code-acme/abc-123/tool-results/b3ij9u144.txt", "/home/h/.claude/projects/-home-h-code-acme/memory/MEMORY.md"):
with self.subTest(path):
self.assertEqual(self.breached("Read", {"file_path": path}), [])
def test_a_jsonl_file_outside_the_projects_folder_passes(self):
self.assertEqual(self.breached("Read", {"file_path": "/home/h/code/acme/cases.jsonl"}), [])
self.assertEqual(self.breached("Read", {"file_path": "/home/h/.claude/loop-claude-transcripts/x.jsonl"}), [])
def test_a_search_of_a_transcript_is_not_checked(self):
self.assertEqual(self.breached("Grep", {"pattern": "x", "path": self.TRANSCRIPT}), [])
self.assertEqual(self.breached("Bash", {"command": f"grep x {self.TRANSCRIPT}"}), [])
class RuleNames(unittest.TestCase):
def test_every_rule_name_has_a_rule_and_every_rule_a_name(self):
self.assertEqual(set(RULES), set(typing.get_args(RuleName)))
+19
View File
@@ -0,0 +1,19 @@
"""Whether a Read asks for a session transcript under ~/.claude/projects, for the `transcript-read` rule.
A transcript is a `.jsonl` file there, a session's own or a subagent's. It holds every tool result of its session, so reading one costs far more than the lines a task needs. The files Claude Code saves a long Bash result to, under the session's `tool-results/` folder, are `.txt` files and pass: the model is handed their path to read."""
import posixpath
from rule_hook.rules import Breach
from rule_hook.call import Call, text
from command_lines.shell_command import resolved
READS = "the call reads {}, a session transcript that holds every tool result of its session; print only the lines you need with grep or jq"
def transcript_breaches(call: Call, home: str) -> list[Breach]:
if call.tool != "Read":
return []
path = resolved(call.cwd, text(call.given, "file_path"), home)
if path.endswith(".jsonl") and path.startswith(posixpath.join(home, ".claude", "projects") + "/"):
return [Breach("transcript-read", READS.format(path), call.cwd)]
return []