Answer stability round 1: sort imports, share rank_todo's check, scope YAML to compose and workflows, judge only newly unsorted ignore blocks, cut heredoc bodies, check COPY --from and container:, log what each override let through, keep CLAUDE.md lines for what the hook cannot see
This commit is contained in:
+23
-29
@@ -6,19 +6,22 @@ usage: rule-hook (a PreToolUse hook: reads the call as JSON on stdin)
|
||||
settings.json runs it before each Bash, Edit, MultiEdit and Write call, in every session and
|
||||
subagent. The rules:
|
||||
|
||||
action-pin a workflow's `uses:` names a semver tag, such as @v3 or @v3.1.2
|
||||
action-pin a workflow's or action.yml's `uses:` names a semver tag, such as @v3 or @v3.1.2
|
||||
commit-one-line git commit takes its message as one -m with no newline
|
||||
ignorefile-sorted each block of a .*ignore file is sorted; a blank line or comment ends a block
|
||||
image-pin a Dockerfile FROM or YAML `image:` names a full patch version or a digest
|
||||
ignorefile-sorted each block of a .*ignore file that an edit leaves unsorted is sorted; a blank
|
||||
line or comment ends a block
|
||||
image-pin a Dockerfile FROM or COPY --from, or a compose or workflow `image:` or
|
||||
`container:`, names a full patch version, a date stamp or a digest
|
||||
no-ai-attribution no Claude or Anthropic co-author or generated-with line, in a file written or
|
||||
in a git, gh or tea command
|
||||
todo-rank-check a commit that stages todo.md, or commits with -a while todo.md has changes,
|
||||
needs rank_todo.py --check to pass on it
|
||||
|
||||
A call overrides a rule with `# rule-override <rule>: <reason>`: in a Bash command, as a trailing
|
||||
comment; in a file, as a comment line in the text the edit writes. Each override that let a call
|
||||
through is appended to rule-overrides.tsv in the checkout's git directory: UTC time, branch, rule,
|
||||
reason. `rule-hook overrides` prints the lines for the current branch.
|
||||
A call overrides a rule with `# rule-override <rule>: <reason>`: anywhere in a Bash command, such as
|
||||
a trailing comment; in a file, as a comment line in the text the edit writes. Each override that
|
||||
let a call through is appended to rule-overrides.tsv in the checkout's git directory: UTC time,
|
||||
branch, rule, reason, and the file written or the command's first line. `rule-hook overrides`
|
||||
prints the lines for the current branch.
|
||||
|
||||
Exit 2 with the rule, why it holds and how to override it, when a rule blocks the call; 0 otherwise.
|
||||
Exit 1, a non-blocking error to Claude Code, on input it cannot read.
|
||||
@@ -31,9 +34,8 @@ import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
import rank_todo
|
||||
from rule_hook.rules import Breach, command_breaches, commits, file_breaches, overrides, RULES
|
||||
import rank_todo
|
||||
from utc_stamp import utc
|
||||
|
||||
LOG = "rule-overrides.tsv"
|
||||
@@ -58,14 +60,15 @@ def main(argv: list[str], stdin: str) -> int:
|
||||
if checked is None:
|
||||
return 0
|
||||
text, directory, breaches = checked
|
||||
what = text.split("\n", 1)[0] if tool == "Bash" else str(given.get("file_path"))
|
||||
granted = overrides(text)
|
||||
for breach in breaches:
|
||||
if breach.rule in granted:
|
||||
log_override(directory, breach.rule, granted[breach.rule])
|
||||
log_override(directory, breach.rule, granted[breach.rule], what)
|
||||
blocking = [breach for breach in breaches if breach.rule not in granted]
|
||||
if not blocking:
|
||||
return 0
|
||||
where = "the command, as a trailing comment" if tool == "Bash" else "the text you write, as a comment line"
|
||||
where = "the command" if tool == "Bash" else "the text you write, as a comment line"
|
||||
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)
|
||||
@@ -75,22 +78,22 @@ def main(argv: list[str], stdin: str) -> int:
|
||||
def checked_call(tool: str, given: dict[str, typing.Any], cwd: str) -> tuple[str, str, list[Breach]] | None:
|
||||
"""The text that may carry an override, the directory whose checkout logs it, and the call's breaches."""
|
||||
if tool == "Bash":
|
||||
command = given.get("command", "")
|
||||
command = str(given.get("command") or "")
|
||||
todo = [breach for commit in commits(command, cwd) for breach in todo_breaches(commit.directory, commit.all)]
|
||||
return command, cwd, command_breaches(command, cwd) + todo
|
||||
path = given.get("file_path", "")
|
||||
path = str(given.get("file_path") or "")
|
||||
if tool not in ("Edit", "MultiEdit", "Write") or not path:
|
||||
return None
|
||||
before = read(path)
|
||||
edits = [given] if tool == "Edit" else given.get("edits", [])
|
||||
edits = [given] if tool == "Edit" else [edit for edit in given.get("edits") or [] if isinstance(edit, dict)]
|
||||
if tool == "Write":
|
||||
added = after = given.get("content", "")
|
||||
added = after = str(given.get("content") or "")
|
||||
else:
|
||||
after = before
|
||||
for edit in edits:
|
||||
old, new = edit.get("old_string", ""), edit.get("new_string", "")
|
||||
old, new = str(edit.get("old_string") or ""), str(edit.get("new_string") or "")
|
||||
after = after.replace(old, new) if edit.get("replace_all") else after.replace(old, new, 1)
|
||||
added = "\n".join(edit.get("new_string", "") for edit in edits)
|
||||
added = "\n".join(str(edit.get("new_string") or "") for edit in edits)
|
||||
return added, os.path.dirname(path), file_breaches(path, before, added, after)
|
||||
|
||||
|
||||
@@ -101,25 +104,16 @@ def todo_breaches(directory: str, staged_all: bool) -> list[Breach]:
|
||||
changed = git(top, "diff", "--cached", "--name-only", "--", "todo.md") or (staged_all and git(top, "diff", "--name-only", "--", "todo.md"))
|
||||
if not changed:
|
||||
return []
|
||||
todo = pathlib.Path(top) / "todo.md"
|
||||
texts = rank_todo.read_all(todo)
|
||||
if isinstance(texts, Failure):
|
||||
return [Breach("todo-rank-check", texts.reason)]
|
||||
ranked = rank_todo.rank(*texts)
|
||||
if isinstance(ranked, Failure):
|
||||
return [Breach("todo-rank-check", ranked.reason)]
|
||||
if ranked.text != texts[0]:
|
||||
return [Breach("todo-rank-check", f"{todo} is not ranked")]
|
||||
return [Breach("todo-rank-check", line) for line in ranked.under_bar]
|
||||
return [Breach("todo-rank-check", line) for line in rank_todo.check_failures(pathlib.Path(top) / "todo.md")]
|
||||
|
||||
|
||||
def log_override(directory: str, rule: str, reason: str) -> None:
|
||||
def log_override(directory: str, rule: str, reason: str, what: str) -> None:
|
||||
common = git(existing(directory), "rev-parse", "--path-format=absolute", "--git-common-dir")
|
||||
if not common:
|
||||
return
|
||||
branch = git(existing(directory), "branch", "--show-current") or "HEAD"
|
||||
with open(pathlib.Path(common) / LOG, "a", encoding="utf-8") as log:
|
||||
log.write("\t".join([utc(time.time()), branch, rule, " ".join(reason.split())]) + "\n")
|
||||
log.write("\t".join([utc(time.time()), branch, rule, " ".join(reason.split()), " ".join(what.split())]) + "\n")
|
||||
|
||||
|
||||
def print_overrides(directory: str) -> int:
|
||||
|
||||
Reference in New Issue
Block a user