Turn PR review comments into rules an AI coding assistant checks
An AI coding assistant kept earning the same pull request comments on a strict codebase. Here is how I turned the review history into rules it reads first, and the mistakes that survived anyway.
I was using an AI coding assistant on a client codebase with strict reviewers. The code compiled and the tests passed, and the pull requests (PRs) still came back with comments. The same comments, again and again.
A new helper written next to one that already did the job. Doc comments in a project that had almost none. A method wrapped around a single statement. A name that said where a value came from, not what it was.
Each one was small, and each one cost a review round.
How it works
An assistant like Claude Code (Anthropic’s coding assistant, which reads and edits files in your repository from a terminal) starts every session knowing nothing about your team. It writes plausible code in a generic style, and your reviewers then teach it, one comment at a time, to a reader who forgets everything by morning. The fix is to move that teaching into files it reads.
I ended up with three, each answering a different question and read at a different moment.
CLAUDE.mdis a file Claude Code loads by itself at the start of every session. Mine sits in~/.claude/, so it applies to every project, and holds how I want code written anywhere.PROJECT.mdis a map of how this one codebase already does things, measured from the code. It is not a built-in file: myCLAUDE.mdtells the assistant to read it before every change.REVIEW-STANDARDS.mdis what this team’s reviewers actually ask for, with the reason behind each rule. The self-review step at the end reads it before every commit.
The loop that keeps them honest is the second diagram. A comment becomes a rule, the rule is read before the next change, and a self-review before the commit catches what slipped through.
Layer 1: principles that apply everywhere
These came from watching the assistant repeat itself. When it added a second way to load content next to an existing one, I asked it to look for how the codebase already did the job and reuse that. When it added summary comments to files that had none, I asked it to match the project. Both became lines in my global CLAUDE.md.
**Search before you write.** Before writing a fetch, a mapping, a loop overcontent, a URL build or a helper of any kind, look for one that already existsand use it. Calling a framework API directly when the project has its ownwrapper around that API is a defect, not a style choice.
**Comments match the project.** Look at the surrounding file and its neighboursfirst. If they carry comments, use the same form, placement and density. If theycarry none, add none, however useful a comment might seem.
**No method for a single statement.** If the work is one line, write the linewhere it happens. A private helper that wraps one statement adds a name, a jumpand a place for the two to drift apart, and buys nothing. Extract when there isreal work to name or a genuine second caller, not to make a block look tidy.Short, blunt and checkable. A principle like “write clean code” does nothing, because the assistant already believes it is doing that.
Layer 2: a project map that is measured, not remembered
“Search before you write” only helps if the search finds something. So I had the assistant survey the repository and write down what it found, in PROJECT.md. I wrapped the survey in a slash command (a Markdown file in ~/.claude/commands/ that you run as /init-project) so it runs the same way on every project.
The rule at the top of that command is the one that matters:
**The rule that governs everything below: measure, do not guess.** Every claimin the file must be backed by a real path, a line number or a count you actuallyran. If you could not verify it, it does not go in.The comment pass shows why. Instead of “this project doesn’t use doc comments”, the file says how many files and how many comments, so the rule falls out of the numbers.
### CommentsCounted recursively over the folders a typical change touches:about 220 files, 2 with `///` doc comments. Rule: add none here.
### Wrappers (use these, not the raw API)| Need | Use | Not | Evidence || --- | --- | --- | --- || Logging | `IAppLogger` | `ILogger<T>` | 174 files vs 14 |The wrappers table is the most valuable part. A project that puts its own type in front of a framework API will have reviewers rejecting the raw call, and a number like 174 against 14 turns that preference into something the assistant can act on. The counts come from my project, with the file count rounded and the type names changed; the shape is what to copy.
Layer 3: mining the review history
This is where the real rules are. Written guidelines drift. Reviewer comments are what is actually enforced.
Bitbucket collapses resolved comment threads on the PR page, and when the assistant read a PR through a browser it could not see what a resolved thread said. The API (the interface programs use to read the same data) returns every comment with its full text in content.raw, resolved or not. I first pulled it through a logged-in browser session. This version runs from a terminal with credentials in an environment variable.
Create a Bitbucket API token with the read:pullrequest:bitbucket scope, then set BITBUCKET_AUTH to your-atlassian-email:token. The API wants your Atlassian account email here, not your Bitbucket username. App passwords, the older credential, stopped working in July 2026. Never put the token in the script.
import json, os, sys, base64from urllib.request import Request, urlopen
WORKSPACE, REPO = "example-workspace", "example-repo"BASE = f"https://api.bitbucket.org/2.0/repositories/{WORKSPACE}/{REPO}"AUTH = base64.b64encode(os.environ["BITBUCKET_AUTH"].encode()).decode()
def get(url): request = Request(url, headers={"Authorization": f"Basic {AUTH}"}) with urlopen(request) as response: return json.load(response)
def comments(pr_id): url = f"{BASE}/pullrequests/{pr_id}/comments?pagelen=100" while url: page = get(url) yield from page["values"] url = page.get("next") # absent on the last page
for pr_id in sys.argv[1:]: for comment in comments(pr_id): if comment.get("deleted"): continue path = (comment.get("inline") or {}).get("path", "(general)") text = " ".join(comment["content"]["raw"].split()) print(f"PR {pr_id} | {path} | {text}")Run it with the PR numbers you want: python scripts/pull_pr_comments.py 101 102 103. Each line is a file and a comment. I tested the paging and the deleted-comment filter against a fake response; I did not run it against a live workspace for this post, so check the first page of output against what you see in the browser.
I started with eleven PRs and about 150 comments, then added three PRs I had missed. The assistant grouped the comments into a standards file that, after two refreshes, held 102 rules over about 500 lines. Each rule has a number, the comment it came from, and the reason. I asked for the reason on purpose: a rule you understand survives a case it does not quite cover.
**1.2 Use a real method body, assign the response to a variable, then return the variable.***Three reviewers on three PRs: "Set up a variable and return that variable insteadof returning stuff directly."*You cannot put a breakpoint on an expression-bodied return and inspect what isabout to be serialised. The reason given is always debugging.
Pre-flight check: git diff origin/develop...HEAD -- '*.cs' \ | grep -nE '^\+\s*(public|private|protected|internal)[^=;{]*=>'An expression-bodied member is a method or property written as => expression; instead of a body in braces. That grep, a simplified version of the checks in my file, lists every added line that declares a member and then uses =>. The [^=;{]* part stops at the first =, ; or {, so a field or property whose initial value contains a lambda is not reported. It misses a member whose parameters have default values and one where => starts the next line, and I still read each hit, because a one-line property is fine.
Only some rules can be grepped. Naming, layering and response shape need a reader, so the standards file has two parts: a block of mechanical checks for what a regular expression can see, and a checklist for the rest.
Layer 4: the assistant reviews itself before the commit
A standards file nobody runs does nothing. I got a reminder of that from a sample of four open front-end PRs from teammates: of 20 review points, about 12 broke a rule that was already written down. The rules existed. Nobody ran the checklist.
So I made running it the assistant’s job. A skill (a saved set of instructions Claude Code can run on request) called pr-sanity reads the diff, the standards files, the repository’s own instruction files and PROJECT.md, runs the mechanical greps, reads the whole diff against the rules, and checks the change against the ticket’s acceptance criteria. It can be started with a command, and its description lets the assistant start it unprompted after finishing a change. It reports findings in a table with the rule number, the file and line, and a one-line fix, and it is told not to change any code. The general version of this is why the model never marks its own work.
My setup also has a second review skill that splits the job between two sub-agents (separate assistant sessions, each given one task) running in parallel: one checks the diff against the standards, the other against the spec. Splitting them keeps each reviewer on one question, and both are told only to report, so a wrong finding costs a read, not a change to the branch.
One trap is easy to miss. My shell was routing git through a tool that summarises its output, so every grep over git diff saw nothing and reported a clean run. The self-review now says to confirm that one grep finds a line you know is there before trusting an empty result.
What still went wrong
The rule was written down, and the assistant still walked past it. A reviewer had asked for read-only list types on six properties of one model. Later the assistant opened that same model to make one property nullable and left the six list properties as they were, even though the rule was in the standards file it had written itself.
The fix is to treat every comment as a search query: what other lines in this diff, and in my other open PRs, break the same rule? Reviewers also repeat themselves across sibling PRs, so one comment is often a preview of the next.
The assistant stated things that were not true. Early on it called an existing URL-decode call a double-decode bug and removed it. The call was needed, because the client sent names encoded twice, and the next test returned an empty list. It also claimed that no controller used path parameters; eight did.
The first PROJECT.md carried wrong counts because it was written from memory of earlier searches. The recount that replaced them started out inflated too: it searched the build output folders (bin and obj) and found 182,788 matches for one attribute, until it was re-run with a search tool that skips files listed in .gitignore. What caught each of these was asking for evidence: the command, the count and a path with a line number. The survey command has a step before handover to re-check five random claims, including at least one count and one path with a line number.
Following the majority pointed the wrong way. Repo-wide, the old Sass stylesheet variables were used 2,929 times against 1,186 uses of the newer design tokens, so “do what the code does” said to use the old ones. In the file the PR actually touched, the split was 113 tokens to 33, and reviewers wanted tokens. Legacy weight and direction of travel disagree.
PROJECT.md now records both numbers for anything that is mid-migration. The survey command says that when the repo-wide count and a recently touched component disagree, the component is the signal.
Do it on your team
- Write down your last ten review comments. Group the repeats. Turn each into one line of principle in
CLAUDE.md, in the checkable style above. - Run a survey. Ask the assistant for a
PROJECT.mdand make “measure, do not guess” the first rule. Spot-check five claims yourself. - Pull the comments. Use the script, run it over your last ten or so PRs, and delete acknowledgement replies.
- Turn comments into rules. Ask the assistant to group them and write each rule with its source comment and a reason. Check that each quote matches the source, and make it add a grep only where a regex can see the problem.
- Add a self-review step. A command or skill that reads the diff and the files, runs the greps, and reports without editing. Run it before every commit.
- Refresh. After each review round, pull the new comments and add the rules. If the code and a file disagree, the code wins; fix the file.
If your team uses more than one AI tool, keep one source for the instructions and generate each tool’s files so these rules reach all of them.
How to check it worked
Pick a branch that earned comments before and run the self-review. It should name the rules those comments would have cited. Then make sure the greps can fail: on a scratch branch, commit a deliberate => member and confirm the check flags it. It has to be committed, because the three-dot git diff compares commits and ignores uncommitted edits.
I do not have a clean before-and-after count of review comments, so I will not give you one. What I can show is the gap that started this: in the sample above, most review points were already covered by a written rule, and a self-review that reads the rules is how you close it.
Takeaways
- Reviewers’ rules live in comment threads. Pull them out through the API, because the web page collapses resolved ones.
- Split instructions by when they are read: always, before a change, before a commit.
- Measure the project and write the counts down. A rule backed by a number can be checked.
- A rules file only works if something runs it, so make the self-review a step.
- Turn each comment into a search across your whole diff, not a fix to one line.
- Ask the assistant for evidence on every factual claim, and count both ways when a codebase is mid-migration.