From pr-review
Step 4/4: Analyze completed PR review plans for mistake patterns and propose rules for CLAUDE.md. Uses a 2-PR promotion threshold — patterns must appear across 2+ PRs before proposing a rule. Triggers include "extract PR lessons", "what did I learn from the review", "check for patterns in reviews", "PR retrospective". Manages candidates at $MAIN_REPO/.claude/pr-lesson-candidates.md. Always asks for user approval before editing CLAUDE.md.
How this skill is triggered — by the user, by Claude, or both
Slash command
/pr-review:extract-lessonsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Analyze completed PR review plan artifacts to find recurring mistake patterns, accumulate candidates across PRs, and propose rules for CLAUDE.md when a pattern repeats. This skill implements a feedback loop: review comments from automated tools (Copilot, CodeRabbit) become project-level rules that prevent the same class of mistake from recurring.
Analyze completed PR review plan artifacts to find recurring mistake patterns, accumulate candidates across PRs, and propose rules for CLAUDE.md when a pattern repeats. This skill implements a feedback loop: review comments from automated tools (Copilot, CodeRabbit) become project-level rules that prevent the same class of mistake from recurring.
.pr-review/ (any status — it reads the triage data, not the posting state)This skill often runs inside a git worktree which is ephemeral — it gets deleted after the branch is merged. Lessons and rules must be written to the main repo, not the worktree.
At the start of this skill, detect if you're in a worktree:
MAIN_REPO=$(git rev-parse --path-format=absolute --git-common-dir | sed 's|/\.git$||')
CURRENT_DIR=$(pwd)
if [ "$MAIN_REPO" != "$(git rev-parse --show-toplevel)" ]; then
echo "WORKTREE detected. Main repo: $MAIN_REPO"
else
echo "Not a worktree. Main repo: $CURRENT_DIR"
fi
.pr-review/ plan files from the current directory (worktree or main — wherever the plan was written)CLAUDE.md changes to $MAIN_REPO/CLAUDE.md — never to the worktree copy$MAIN_REPO/.claude/pr-lesson-candidates.md — per-project, in the main repo, never in a worktreeA single PR's review isn't enough signal to create a project rule — it could be a one-off mistake or a reviewer quirk. Instead:
$MAIN_REPO/.claude/pr-lesson-candidates.md as a candidate at 1/22/2 and proposed for CLAUDE.mdThis prevents overfitting on a single PR while still capturing genuine recurring issues.
Find the most recent plan artifact in .pr-review/ (or use the PR number the user specifies). Extract all threads that were classified as apply_suggestion or fix — these represent actual mistakes that needed correction.
If there are fewer than 2 actionable threads, report "Not enough signal — only N actionable thread(s). Skipping analysis." and stop.
Group the actionable threads by root cause. Don't just restate the reviewer's comment — ask why the mistake was made:
Only keep categories with ≥2 threads in this PR. A single instance of a mistake isn't a pattern.
Read the project's CLAUDE.md file. For each surviving pattern, check if a rule already covers it. If so, skip — don't duplicate.
For example, if CLAUDE.md already says "Always add KDoc to public methods" and the pattern is "missing documentation on public APIs", that's already covered.
The candidate file lives at $MAIN_REPO/.claude/pr-lesson-candidates.md. If it doesn't exist yet, create it with this header:
# PR Lesson Candidates
Accumulated patterns from PR reviews. Rules are promoted to project CLAUDE.md
after appearing in 2+ separate PRs. Managed by /pr-review:pr-review-extract-lessons.
For each surviving pattern (not already in CLAUDE.md):
Add a new entry:
## Candidate: <short pattern name>
- Proposed rule: "<the generic, actionable rule>"
- First seen: PR #<N> (<date>) — <count> threads (<brief description>)
- Promotion count: 1/2
Check if it's from a different PR (same PR shouldn't count twice). If different PR, update:
- Seen again: PR #<N> (<date>) — <count> thread(s)
- Promotion count: 2/2 → **ready for promotion**
For each candidate at 2/2:
Draft the rule. It must be:
Bad (too specific): "Always add a null check in processFrame() after calling getBuffer()"
Good (generic): "Always null-check return values from methods that acquire hardware resources — they can fail silently under contention."
Bad (too vague): "Think carefully about edge cases" Good (actionable): "When adding error handling, check for both synchronous exceptions and callback-delivered errors — Camera2 surfaces failures through both paths."
Show the user the proposed rule and which PRs triggered it.
Ask for approval. Do not edit CLAUDE.md without explicit confirmation.
On approval: append the rule to the "Rules for AI Agents" section of CLAUDE.md in the main repo (use $MAIN_REPO/CLAUDE.md, not the worktree copy). Remove the candidate from $MAIN_REPO/.claude/pr-lesson-candidates.md.
On rejection: remove the candidate from $MAIN_REPO/.claude/pr-lesson-candidates.md. Don't re-propose it.
Present a summary of what happened:
## Lessons from PR #<N>
**Patterns observed:** <count> mistake categories found
**Already covered by CLAUDE.md:** <count> (skipped)
**New candidates added (1/2):** <count>
- "<pattern name>" — <brief description>
**Promoted to CLAUDE.md (2/2):** <count>
- "<rule text>"
**No pattern found:** <only if zero patterns survived filtering>
Takeaway: <one sentence summarizing the most significant insight, or "No recurring patterns yet — will check again after the next review.">
Creates, edits, and optimizes skills for Claude Code, including drafting, evaluating with test prompts, iterating on performance, and improving skill descriptions for better triggering accuracy.
npx claudepluginhub shreeyak/claude-pr-review-plugin --plugin pr-review