/learn — append a rule from a mistake
Turn a correction into a durable rule in the canonical agent file's Self-Learning Rules, so the next session doesn't repeat it. This is the "compounding engineering" loop: every mistake makes the rules sharper.
Procedure
- Find the canonical agent file (the one holding the
CC-RULESblock). Project root =git rev-parse --show-toplevel(fallback: cwd). Resolve the same way/claude-mddoes: aCLAUDE.mdsymlink → its target;CLAUDE.mdimporting@AGENTS.md(or AGENTS.md canonical) →AGENTS.md; onlyAGENTS.md→AGENTS.md; elseCLAUDE.md. In practice: pick whichever ofCLAUDE.md/AGENTS.mdactually contains<!-- LEARN:ANCHOR -->. If neither has it, tell the user to run/claude-mdfirst (don't hand-edit). - Determine the rule:
- If the user passed text after
/learn, use that as the rule (lightly cleaned). - Otherwise, look at the most recent correction in this conversation — what Claude did wrong and what the user actually wanted — and distill it into one rule.
- If the user passed text after
- Write the rule well (this matters more than anything):
- Imperative and general, not a story. ✅ "When editing a shared function, grep all call sites before changing its signature." ❌ "Earlier you broke the build."
- One line, one lesson. Actionable next time. Prefer "always/never/before X do Y" shapes.
- Preserve the user's language (Korean correction → Korean rule).
- Capture the generalizable lesson, not the one-off detail.
- Append (deduped) via the helper:
(node "${CLAUDE_PLUGIN_ROOT}/scripts/append-rule.mjs" <target file> "<the one-line rule>"<target file>= the canonical file resolved in step 1.) It inserts- (YYYY-MM-DD) <rule>after the anchor (newest first) and skips near-duplicates. - Confirm the added line (or report it was a duplicate). Offer to commit (don't commit without approval).
Examples
Why a separate section + skill (not full automation)
Auto-detecting "mistakes" from chat is noisy and unreliable. /learn keeps a human in the loop (you decide a correction is worth a rule), while the helper makes the write deterministic and dedup-safe. The rules live in CLAUDE.md so they load every session and travel with the repo.