From 0292c1a55c2da64e90bb24b977cabf037b7a3009 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Mon, 12 Oct 2026 23:26:07 +0200 Subject: [PATCH] Answer stability round 3: an empty argument separates no command, and two overrides on one line are read apart --- bin/rule_hook/main.py | 10 ++++++---- bin/rule_hook/rules.py | 14 ++++++++------ bin/rule_hook/test_rule_hook.py | 15 +++++++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/bin/rule_hook/main.py b/bin/rule_hook/main.py index d5dbbccd..5dc37fd7 100644 --- a/bin/rule_hook/main.py +++ b/bin/rule_hook/main.py @@ -67,7 +67,7 @@ def main(argv: list[str], stdin: str) -> int: return 0 text, directory, breaches = checked what = text.split("\n", 1)[0] if tool == "Bash" else str(given.get("file_path")) - granted = overrides(text) + granted, malformed = overrides(text) for breach in breaches: if breach.rule in granted: log_override(directory, breach.rule, granted[breach.rule], what) @@ -75,11 +75,13 @@ def main(argv: list[str], stdin: str) -> int: if not blocking: return 0 where = "the command" if tool == "Bash" else "the text you write, as a comment line" - if "rule-override" in text and not granted: - print("The rule-override in this call names no rule below, or gives no reason.", file=sys.stderr) + markdown = tool != "Bash" and str(given.get("file_path")).endswith(".md") + for marker in malformed: + print(f"`{marker}` names no rule below, or gives no reason.", file=sys.stderr) for rule in sorted({breach.rule for breach in blocking}): details = "; ".join(breach.detail for breach in blocking if breach.rule == rule) - print(f"Blocked by rule {rule}: {details}. The rule: {RULES[rule]}. Where it does not fit this case, add `# rule-override {rule}: ` to {where}; larv-review's judge rules on the reason.", file=sys.stderr) + marker = f"" if markdown else f"# rule-override {rule}: " + print(f"Blocked by rule {rule}: {details}. The rule: {RULES[rule]}. Where it does not fit this case, add `{marker}` to {where}; larv-review's judge rules on the reason.", file=sys.stderr) return 2 diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index e5c2b6a7..6de83d01 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -10,9 +10,9 @@ RULES = { "ignorefile-sorted": "each block of an ignore file is sorted, since its order carries no meaning; where order matters, such as a re-include after its exclude, split the block with a blank line or a comment", "image-pin": "a container image is pinned to its full patch version, such as node:24.18.1-bookworm-slim, or to a date-stamped tag or an @sha256: digest, so the same commit builds the same image on every day; a bot such as Renovate raises the bumps as reviewable PRs. An image that publishes no three-part tag, such as postgres:16.4, takes an override", "no-ai-attribution": "no commit, PR, issue comment or file carries a Claude or Anthropic attribution, such as a co-author trailer or a generated-with line", - "todo-rank-check": "a committed todo.md passes ~/.claude/bin/rank_todo.py --check", + "todo-rank-check": "while todo.md has changes, staged or not, a git commit waits until ~/.claude/bin/rank_todo.py --check passes on it", } -OVERRIDE = re.compile(r"(?:#|").strip() for rule, reason in OVERRIDE.findall(text)} - return {rule: reason for rule, reason in given.items() if rule in RULES and reason} +def overrides(text: str) -> tuple[dict[str, str], list[str]]: + """The rules the text overrides with their reasons, and the markers that name no rule or give no reason.""" + given = [(rule, reason.strip().removesuffix("-->").strip()) for rule, reason in OVERRIDE.findall(text)] + granted = {rule: reason for rule, reason in given if rule in RULES and reason} + return granted, [f"rule-override {rule}: {reason}" for rule, reason in given if rule not in granted] def segments(command: str) -> list[list[str]] | None: @@ -61,7 +63,7 @@ def segments(command: str) -> list[list[str]] | None: split: list[list[str]] = [[]] for word in words: # Unquoted, a run such as "&&\n" lexes as one word. - if set(word) <= SEPARATING: + if word and set(word) <= SEPARATING: split.append([]) else: split[-1].append(word) diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index ac0250f8..82ff29e4 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -45,6 +45,9 @@ class Commits(unittest.TestCase): self.assertEqual(len(commits("cat < notes\nhello\nEOF\ngit commit -m a", "/r")), 1) self.assertEqual(len(commits("git status &&\n\ngit commit -m a", "/r")), 1) + def test_an_empty_argument_separates_nothing(self): + self.assertEqual(rules(command_breaches("git commit --author '' -m a -m b", "/r")), ["commit-one-line"]) + def test_an_arithmetic_shift_is_no_heredoc(self): self.assertEqual(len(commits("echo $((1<<2))\ngit commit -m a\n2", "/r")), 1) @@ -106,10 +109,10 @@ class FileBreaches(unittest.TestCase): class Overrides(unittest.TestCase): def test_a_marker_names_a_known_rule_and_a_reason(self): - self.assertEqual(overrides("x # rule-override image-pin: vendor ships floating tags only\n# rule-override nope: x\n# rule-override action-pin:"), {"image-pin": "vendor ships floating tags only"}) + self.assertEqual(overrides("x # rule-override image-pin: vendor ships floating tags only\n# rule-override nope: x\n# rule-override action-pin:"), ({"image-pin": "vendor ships floating tags only"}, ["rule-override nope: x", "rule-override action-pin: "])) def test_markdown_takes_an_html_comment(self): - self.assertEqual(overrides(""), {"no-ai-attribution": "quotes the banned line"}) + self.assertEqual(overrides("")[0], {"no-ai-attribution": "quotes the banned line"}) class Hook(unittest.TestCase): @@ -150,8 +153,12 @@ class Hook(unittest.TestCase): self.assertIn("todo.md: not ranked", ran.stderr) def test_a_malformed_override_is_named(self): - ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m a -m b # rule-override commit-oneline: x"}, "tool_name": "Bash"}, self.repo) - self.assertIn("names no rule below, or gives no reason", ran.stderr) + ran = hook({"cwd": self.repo, "tool_input": {"command": f"git commit -m a -m b -m '{TRAILER}' # rule-override no-ai-attribution: quotes it # rule-override commit-oneline: x"}, "tool_name": "Bash"}, self.repo) + self.assertIn("`rule-override commit-oneline: x` names no rule below", ran.stderr) + + def test_a_markdown_file_is_told_the_html_comment(self): + ran = hook({"cwd": self.repo, "tool_input": {"content": TRAILER, "file_path": f"{self.repo}/a.md"}, "tool_name": "Write"}, self.repo) + self.assertIn("``", ran.stderr) def test_a_missing_field_breaks_nothing(self): for given in [{"command": None}, {"content": None, "file_path": f"{self.repo}/a.md"}, {"edits": [None], "file_path": f"{self.repo}/a.md"}]: