Answer stability round 1: 1 🔴, 6 🟡 (0 misread); read every title through both templates, hold a title to its template only on an item's branch, and fail on a config fault

This commit is contained in:
2026-11-14 01:47:41 +01:00
parent 1692679985
commit f98e8a84dd
42 changed files with 537 additions and 256 deletions
+27 -5
View File
@@ -33,6 +33,7 @@ override cannot be logged; 0 otherwise. `rule-hook overrides` exits 1 when git c
checkout or the branch's reflog. Exit 1, a non-blocking error to Claude Code, on input it
cannot read.
"""
import functools
import json
import os
import pathlib
@@ -41,18 +42,23 @@ import time
import typing
from rule_hook.call import applied, Call, FILE_TOOLS, parsed_call, text
from rule_hook.rules import Breach, check, Checked, command_breaches, file_breaches, rules_help
from git_cli.git_command import commits
from rule_hook.rules import Breach, check, Checked, command_breaches, file_breaches, rules_help, title_breaches
from git_cli.git_command import CommitCall, commits
from rule_hook.committed_files import committed_file_breaches
from rule_hook.env_file import env_file_breaches
from failure import Failure
from git_cli.git_repo import git
from rule_hook.git import guarded_git
from project_config.main import located
from rule_hook.override_log import Log, log_path, logged, print_overrides, write_override
from project_config.settings import ProjectConfig, Unnamed
from rule_hook.file_text import read
from rule_hook.repeat_read import read_breaches
from rule_hook.reviewer_report import report_breaches
from rule_hook.reviewer_rung import reviewer_breaches, skill_breaches
from rule_hook.round_cap import round_cap_breaches
from command_lines.shell_command import simple_commands
from project_config import templates
from rule_hook.todo_check import todo_breaches
@@ -119,9 +125,25 @@ def answer(checked: Checked, what: str, now: float) -> int:
def bash_breaches(command: str, cwd: str, home: str) -> list[Breach]:
"""The breaches of the command's own words, then of each commit it makes, then of the files those commits may take."""
simples = simple_commands(command, cwd, home)
found = commits(simples, home)
in_commits = [breach for commit in found for breach in commit_breaches(commit, pathlib.Path(home))]
in_files = [breach for directory in dict.fromkeys(commit.directory for commit in found) for breach in committed_file_breaches(directory)]
round_cap = [breach for commit in found for breach in round_cap_breaches(commit, pathlib.Path(home))]
todo = [breach for commit in found for breach in todo_breaches(commit.directory, pathlib.Path(home))]
return command_breaches(command, simples, home) + round_cap + todo + in_files
return command_breaches(command, simples) + in_commits + in_files
def commit_breaches(commit: CommitCall, home: pathlib.Path) -> list[Breach]:
"""The breaches of one commit: its title's and its round's, read through the title templates of the project's config, and its plan's. A config that cannot be read is a breach itself, since no title can then be checked."""
config = located(commit.directory, home, functools.partial(git, guarded=True))
unread = []
if isinstance(config, Failure) and not isinstance(config, Unnamed):
unread = [Breach("commit-title", f"the project's config cannot be read, so the title is not checked against its template: {config.reason}", commit.directory)]
titles = config.titles if isinstance(config, ProjectConfig) else templates.BARE
return unread + title_breaches(commit, titles, isinstance(config, ProjectConfig) and on_item_branch(commit.directory, config.branch)) + round_cap_breaches(commit, titles) + todo_breaches(commit.directory, home)
def on_item_branch(directory: str, template: str) -> bool:
"""Whether directory's checkout is on a branch that fits template, the config's `branch`: a plan item's branch, whose commits carry the item's ID. Any other branch, such as a release review's, has no item to give {id}."""
branch = guarded_git(directory, "symbolic-ref", "--quiet", "--short", "HEAD")
return not isinstance(branch, Failure) and templates.branch_fits(template, branch)
+5 -10
View File
@@ -1,25 +1,20 @@
import functools
import pathlib
from rule_hook.rules import Breach
from review_rules.round_titles import cap, past_cap, round_counts, round_pass
from project_config.main import commit_title
from git_cli.git_command import CommitCall
from failure import Failure
from git_cli import git_repo
from review_rules.review_passes import PASSES
from project_config import templates
from project_config.templates import Titles
def round_cap_breaches(commit: CommitCall, home: pathlib.Path) -> list[Breach]:
"""A breach where the commit's title names no pass, or takes its pass past the cap, counted with the branch's earlier round commits. Each title is read as its summary in the project's `commit-title` template."""
template = commit_title(commit.directory, home, functools.partial(git_repo.git, guarded=True))
summaries = [templates.summary_of(template, message.strip()) for message in commit.messages]
def round_cap_breaches(commit: CommitCall, titles: Titles) -> list[Breach]:
"""A breach where the commit's title names no pass, or takes its pass past the cap, counted with the branch's earlier round commits. Each title is read as its summary in titles, the project's title templates."""
summaries = [titles.summary(message.strip()) for message in commit.messages]
named = [(name, summary) for summary in summaries if (name := round_pass(summary))]
unknown = [Breach("round-cap", f"`{name}` names no larv-review pass, so its rounds would never reach a cap; name one of: {', '.join(PASSES)}", commit.directory) for name, _ in named if name not in PASSES]
if unknown or not named:
return unknown
earlier = [templates.summary_of(template, title) for title in earlier_titles(commit.directory, commit.amends)]
earlier = [titles.summary(title) for title in earlier_titles(commit.directory, commit.amends)]
return [Breach("round-cap", refusal(name, message, earlier), commit.directory) for name, message in named if past_cap(message, earlier)]
+19 -20
View File
@@ -1,15 +1,11 @@
import dataclasses
import functools
import pathlib
import posixpath
import re
import typing
from review_rules.round_titles import CAP, COUNTS, EXAMPLE, lacks_counts, OPENING, RELEASE_REVIEW_CAP
from project_config.main import commit_title
from git_cli.git_command import commits
from git_cli.git_command import CommitCall
from run_heavy.capped_docker import finds_this_docker
from git_cli.git_repo import git
from rule_hook.heavy_command import heavy
from command_lines.shell_command import launched, Simple
from review_rules.review_folder import REPORTS
@@ -37,8 +33,8 @@ RULES: dict[RuleName, Rule] = {
"git commit takes its message as one -m with no line break; -F, a second -m and a heredoc are blocked",
),
"commit-title": Rule(
"a commit's title fits the `commit-title` template the project's config names, such as `PAY-{id} - {summary}`, so the project's log reads as its owners write it",
"the -m of a git commit in a checkout whose config sets `commit-title`; a round title's `Answer <pass> round <n>:` is the template's {summary}",
"a commit's title fits the `commit-title` template the project's config names, such as `[{id}] {summary}`, so the project's log reads as its owners write it",
"the -m of a git commit in a checkout whose config sets `commit-title`, on a branch that fits the config's `branch` template; on any other branch, as for a chunk with no plan item, the title may be the bare summary. A round title, from `Answer <pass> round <n>:` to its end, is the template's {summary}. While the config cannot be read, every commit is blocked",
),
"docker-run-memory": Rule(
"a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap",
@@ -156,9 +152,8 @@ def check(breaches: list[Breach], written: str, markdown: bool, bash: bool) -> C
return Checked(messages, bool(blocking), [(breach, grant[breach.rule]) for breach, grant in zip(breaches, grants) if breach.rule in grant])
def command_breaches(command: str, simples: list[Simple], home: str) -> list[Breach]:
def command_breaches(command: str, simples: list[Simple]) -> list[Breach]:
breaches = []
found = commits(simples, home)
publishing = [simple for simple in simples if posixpath.basename(simple.words[0]) in ("chunk-pr", "gh", "git", "tea")]
if publishing and ATTRIBUTION.search(command):
breaches.append(Breach("no-ai-attribution", "the command carries an attribution", publishing[0].directory))
@@ -167,17 +162,21 @@ def command_breaches(command: str, simples: list[Simple], home: str) -> list[Bre
breaches.append(Breach("run-heavy", f"run `{' '.join(simple.words)[:80]}` under `~/.claude/bin/run-heavy`, in the background", simple.directory))
if unlimited_container(simple.words):
breaches.append(Breach("docker-run-memory", f"give `{' '.join(simple.words)[:80]}` a memory limit and a swap limit as large as its work needs", simple.directory))
for commit in found:
if commit.from_file:
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', not from a file", commit.directory))
elif len(commit.messages) > 1 or any("\n" in message.strip() for message in commit.messages):
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', with no heredoc, second -m or newline", commit.directory))
template = commit_title(commit.directory, pathlib.Path(home), functools.partial(git, guarded=True))
# A message of several lines breaks commit-one-line above, which says how to fix it.
if misfit := [message for message in commit.messages[:1] if "\n" not in message.strip() and not templates.fits(template, message.strip())]:
breaches.append(Breach("commit-title", f"title the commit as the project's config asks, `{template}`, where {{id}} is the ID of the chunk's first item; `{misfit[0].strip()[:80]}` doesn't fit", commit.directory))
if any(lacks_counts(templates.summary_of(template, message.strip())) for message in commit.messages):
breaches.append(Breach("round-title-counts", f"open the summary after the round's colon with `{COUNTS}`, where <j> is at most <k> + <m>", commit.directory))
return breaches
def title_breaches(commit: CommitCall, titles: templates.Titles, held: bool) -> list[Breach]:
"""The breaches of the commit's message, read through titles, the project's title templates. held is whether the title must fit the commit template: it must on a branch that fits the config's `branch` template."""
breaches = []
if commit.from_file:
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', not from a file", commit.directory))
elif len(commit.messages) > 1 or any("\n" in message.strip() for message in commit.messages):
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', with no heredoc, second -m or newline", commit.directory))
# A message of several lines breaks commit-one-line above, which says how to fix it.
if held and (misfit := [message for message in commit.messages[:1] if "\n" not in message.strip() and not templates.fits(titles.commit, message.strip())]):
breaches.append(Breach("commit-title", f"title the commit as the project's config asks, `{titles.commit}`, where {{id}} is the ID of the chunk's first item; `{misfit[0].strip()[:80]}` doesn't fit", commit.directory))
if any(lacks_counts(titles.summary(message.strip())) for message in commit.messages):
breaches.append(Breach("round-title-counts", f"open the summary after the round's colon with `{COUNTS}`, where <j> is at most <k> + <m>", commit.directory))
return breaches
+54 -4
View File
@@ -14,7 +14,8 @@ import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from rule_hook.repeat_read import Asked, modified, read_breaches, result_lines
from rule_hook.rules import blocks, Breach, check, command_breaches, file_breaches, overrides, RuleName, RULES
from project_config.templates import BARE, Titles
from rule_hook.rules import blocks, Breach, check, command_breaches, file_breaches, overrides, RuleName, RULES, title_breaches
from rule_hook.main import breaches_of
from rule_hook.call import Call, parsed_call
from git_cli.git_command import commit_message, CommitCall, commits
@@ -40,8 +41,14 @@ def committed(command: str, cwd: str = "/r") -> list[CommitCall]:
return commits(simple_commands(command, cwd, "/h"), "/h")
def breaches(command: str, titles: Titles = BARE, held: bool = False) -> list[Breach]:
"""The command's breaches and those of each commit's title, read through titles; held is whether the title must fit the commit template."""
simples = simple_commands(command, "/r", "/h")
return command_breaches(command, simples) + [breach for commit in commits(simples, "/h") for breach in title_breaches(commit, titles, held)]
def checked(command: str) -> list[str]:
return rules(command_breaches(command, simple_commands(command, "/r", "/h"), "/h"))
return rules(breaches(command))
def hook(call: dict, cwd: str, env: dict[str, str] | None = None) -> subprocess.CompletedProcess:
@@ -112,7 +119,7 @@ class CommandBreaches(unittest.TestCase):
self.assertEqual(checked("git commit -m 'Answer stability round 1: fix the parser'"), ["round-title-counts"])
self.assertEqual(checked("git commit -m 'Answer stability round 1: 1 🔴, 2 🟡; fix the parser'"), ["round-title-counts"])
command = "git commit -m 'Answer stability round 1: fix the parser'"
self.assertEqual(command_breaches(command, simple_commands(command, "/r", "/h"), "/h"), [Breach("round-title-counts", "open the summary after the round's colon with `<k> 🔴, <m> 🟡 (<j> misread);`, where <j> is at most <k> + <m>", "/r")])
self.assertEqual(breaches(command), [Breach("round-title-counts", "open the summary after the round's colon with `<k> 🔴, <m> 🟡 (<j> misread);`, where <j> is at most <k> + <m>", "/r")])
self.assertEqual(checked("git commit --allow-empty -m 'Answer prose round 2: 0 🔴, 0 🟡 (0 misread)'"), [])
self.assertEqual(checked("git commit -m 'Answer stability round 1: 1 🔴, 2 🟡 (1 misread); fix the parser'"), [])
self.assertEqual(checked("git commit -m 'Answer the question in the README'"), [])
@@ -143,7 +150,20 @@ class CommandBreaches(unittest.TestCase):
self.assertEqual(checked(f"grep -rn '{TRAILER}' ."), [])
def test_a_commit_breach_names_the_commit_s_checkout(self):
self.assertEqual(command_breaches("cd w && git commit -m a -m b", simple_commands("cd w && git commit -m a -m b", "/r", "/h"), "/h")[0].directory, "/r/w")
self.assertEqual(breaches("cd w && git commit -m a -m b")[0].directory, "/r/w")
def test_a_title_must_fit_the_commit_template_only_where_the_branch_holds_it_to_it(self):
titles = Titles("PAY-{id} - {summary}", "{summary}")
self.assertEqual(breaches("git commit -m 'Release review of 0.3.0'", titles, held=True), [Breach("commit-title", "title the commit as the project's config asks, `PAY-{id} - {summary}`, where {id} is the ID of the chunk's first item; `Release review of 0.3.0` doesn't fit", "/r")])
self.assertEqual(breaches("git commit -m 'Release review of 0.3.0'", titles), [])
self.assertEqual(breaches("git commit -m 'PAY-7 - Tidy'", titles, held=True), [])
def test_a_round_title_is_read_through_either_template_for_its_counts(self):
titles = Titles("PAY-{id} - {summary}", "fix: {summary}")
for title in ("PAY-7 - Answer prose round 1: fix", "fix: Answer prose round 1: fix", "Answer prose round 1: fix"):
with self.subTest(title):
self.assertEqual(rules(breaches(f"git commit -m '{title}'", titles)), ["round-title-counts"])
self.assertEqual(breaches("git commit -m 'PAY-7 - Answer prose round 1: 0 🔴, 0 🟡 (0 misread)'", titles, held=True), [])
class FileBreaches(unittest.TestCase):
@@ -1029,6 +1049,7 @@ class Hook(unittest.TestCase):
def test_a_commit_is_titled_by_the_configs_template_and_its_round_title_read_through_it(self):
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
git(self.repo, "checkout", "-q", "-b", "item-7")
for title in ["PAY-7 - Answer stability round 1: 1 🔴, 0 🟡 (0 misread); a", "PAY-7 - Answer stability round 2: 0 🔴, 1 🟡 (0 misread); c"]:
git(self.repo, "commit", "-q", "--allow-empty", "-m", title)
past = self.round_commit("PAY-7 - Answer stability round 2: 0 🔴, 0 🟡 (0 misread); d")
@@ -1041,6 +1062,35 @@ class Hook(unittest.TestCase):
self.assertEqual(self.round_commit("PAY-7 - Answer prose round 1: 0 🔴, 0 🟡 (0 misread); x").returncode, 0)
self.assertEqual(self.round_commit("Tidy # rule-override commit-title: the client asked for this title").returncode, 0)
def test_a_commit_on_a_branch_that_doesnt_fit_the_configs_branch_template_takes_a_bare_title(self):
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\nbranch = "feature/PAY-{id}"\n')
git(self.repo, "checkout", "-q", "-b", "release-review-0.3.0")
self.assertEqual(self.round_commit("Release review of 0.3.0", "--allow-empty ").returncode, 0)
self.assertIn("Blocked by rule round-title-counts", self.round_commit("PAY-7 - Answer prose round 1: fix").stderr)
git(self.repo, "checkout", "-q", "-b", "feature/PAY-7")
held = self.round_commit("Release review of 0.3.0", "--allow-empty ")
self.assertEqual(held.returncode, 2)
self.assertIn("Blocked by rule commit-title: title the commit as the project's config asks, `PAY-{id} - {summary}`", held.stderr)
self.assertEqual(self.round_commit("PAY-7 - Tidy").returncode, 0)
def test_a_commit_is_refused_naming_the_fault_while_a_config_cannot_be_read(self):
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n')
refused = self.round_commit("PAY-7 - Tidy")
self.assertEqual(refused.returncode, 2)
self.assertIn("Blocked by rule commit-title: the project's config cannot be read, so the title is not checked against its template: ", refused.stderr)
self.assertIn("`commit-title` is 'PAY-{id}'", refused.stderr)
config.write_text(config.read_text().replace('commit-title = "PAY-{id}"\n', ""))
other = config.parents[1] / "other" / "config.toml"
other.parent.mkdir()
other.write_text("checkout = [")
refused = self.round_commit("Tidy")
self.assertEqual(refused.returncode, 2)
self.assertIn(f"cannot read {other}", refused.stderr)
other.unlink()
self.assertEqual(self.round_commit("Tidy").returncode, 0)
def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self):
configured(self, pathlib.Path(self.repo))
pathlib.Path(self.repo, "docs").mkdir()