Answer stability round 3: an empty argument separates no command, and two overrides on one line are read apart
This commit is contained in:
@@ -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}: <reason>` to {where}; larv-review's judge rules on the reason.", file=sys.stderr)
|
||||
marker = f"<!-- rule-override {rule}: <reason> -->" if markdown else f"# rule-override {rule}: <reason>"
|
||||
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
|
||||
|
||||
|
||||
|
||||
@@ -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"(?:#|<!--)\s*rule-override ([a-z-]+):([^\n]*)")
|
||||
OVERRIDE = re.compile(r"(?:#|<!--)\s*rule-override ([a-z-]+):([^\n]*?)(?=[ \t]*(?:#|<!--)\s*rule-override |$)", re.MULTILINE)
|
||||
# [-] keeps this file from matching itself.
|
||||
ATTRIBUTION = re.compile(r"co-authored[-]by:[^\n]*\b(?:claude|anthropic)\b|generated with \[?claude code", re.IGNORECASE)
|
||||
SEMVER = re.compile(r"v?\d+(?:\.\d+){0,2}")
|
||||
@@ -43,9 +43,11 @@ class Commit:
|
||||
from_file: bool
|
||||
|
||||
|
||||
def overrides(text: str) -> dict[str, str]:
|
||||
given = {rule: reason.strip().removesuffix("-->").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)
|
||||
|
||||
@@ -45,6 +45,9 @@ class Commits(unittest.TestCase):
|
||||
self.assertEqual(len(commits("cat <<EOF > 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("<!-- rule-override no-ai-attribution: quotes the banned line -->"), {"no-ai-attribution": "quotes the banned line"})
|
||||
self.assertEqual(overrides("<!-- rule-override no-ai-attribution: quotes the banned line -->")[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("`<!-- rule-override no-ai-attribution: <reason> -->`", 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"}]:
|
||||
|
||||
Reference in New Issue
Block a user