/rcc:writing-hooksfrom rcc
Creates Claude Code hooks that enforce code quality, static analysis, or workflow automation deterministically. Guides through TDD-based hook creation with event selection and validation.
Ships in rcc. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Writing hooks IS creating automated quality gates.
Hooks run on Claude Code events (PreToolUse, PostToolUse, etc.) and can block actions with exit code 2.
Core principle: Hooks enforce what humans forget. Fast checks only—slow hooks kill productivity.
Follow task initialization protocol.
Tasks: 0. Fetch latest official hook spec
Announce: "Created 8 tasks (0–7). Starting execution..."
| TDD Phase | Hook Creation | What You Do |
|---|---|---|
| RED | Test without hook | Write code, observe quality issues slip through |
| Verify RED | Document violations | Note specific issues that should be caught |
| GREEN | Write hook | Create script that catches those issues |
| Verify GREEN | Test blocking | Verify exit code 2 blocks violating code |
| REFACTOR | Optimize speed | Reduce hook runtime, filter file types |
Goal: Pull the current Anthropic hook spec before designing — never trust cached memory.
Action:
Skill tool: fetching-claude-docs
component: hook
question: "hook events (PreToolUse, PostToolUse, etc.), matcher syntax,
exit code contract, settings.json schema, additionalContext field,
security considerations"
Verification: Received YAML with source: https://code.claude.com/docs/en/hooks.md and non-empty spec_excerpt. Use as authoritative reference; if any rule in this SKILL conflicts with the fetched spec, the fetched spec wins.
Goal: Understand what quality gate to create.
Questions to answer:
package.json, go.mod, Cargo.toml, pyproject.toml, *.csproj, etc.)Event Selection:
| Event | When | Use For |
|---|---|---|
PreToolUse | Before tool runs | Block bad writes before they happen |
PostToolUse | After tool runs | Validate written code |
UserPromptSubmit | Before prompt processed | Add context to prompts |
Stop | When agent tries to end its turn | Self-verify loop — block stop until checks pass (Ralph Wiggum pattern, enables L-Thread) |
Verification: Can describe the violation and the command to detect it.
Goal: Write code WITHOUT the hook. Observe violations that slip through.
Process:
Verification: Documented at least 1 violation that should have been caught.
Goal: Create Python script that catches the violations you documented.
.claude/hooks/
├── eslint_check.py
├── prettier_check.py
└── typecheck.py
| Code | Meaning | Effect |
|---|---|---|
| 0 | Pass | Continue, stdout shown in verbose |
| 2 | Block | Action blocked, stderr fed to Claude |
| Other | Warning | Continue, stderr shown in verbose |
See references/static-checks.md for complete hook templates. For performance-optimized security hooks, see references/performance-optimization.md. Key pattern: read JSON from stdin, filter by extension, run check, exit 2 to block.
When the agent tries to end its turn, block until deterministic checks pass — turning a one-shot agent into a loop that won't quit until the work is actually done. Use for long-running tasks, refactors, migrations, anything where "I think I'm done" is unreliable.
See references/stop-event-self-verify.md for the hook script and settings.json registration. Critical rule: always check stop_hook_active to bail after one retry — otherwise the agent loops forever on unfixable failures.
uv → fallback python3 → fallback python (see cross-platform-scripts.md)Important: Read cross-platform-scripts.md for full cross-platform rules covering paths, shell commands, line endings, and common pitfalls.
Verification:
chmod +x) — skip on Windowspathlib.Path for all path operationsshell=True with string commands (use list args)/tmp/ or path separatorsuv → python3 --version → python); python3 probed by --version to avoid Windows Microsoft Store stubGoal: Register hook in .claude/settings.json.
Important: Use .claude/settings.json, NOT settings.local.json. Settings are team-shared.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "\"${CLAUDE_PROJECT_DIR}\"/.claude/hooks/eslint_check.py",
"timeout": 30
}]
}
]
}
}
| Matcher | Matches |
|---|---|
Write|Edit | Write or Edit tools |
Bash | Bash tool only |
.* | All tools |
Verification:
${CLAUDE_PROJECT_DIR} (braced — see cross-platform-scripts.md)Goal: Verify hook script works correctly.
Test command:
echo '{"tool_input":{"file_path":"test.ts"}}' | .claude/hooks/your_hook.py
echo $? # Should be 0 (pass) or 2 (block)
Checklist:
Verification: Manual test passes for both valid and invalid inputs.
Goal: Verify hook actually blocks bad code in Claude Code.
Process:
Verification:
Goal: Have hook reviewed by hook-reviewer subagent.
Agent tool:
- subagent_type: "rcc:hook-reviewer"
- prompt: "Review hook at [path/to/hook] with settings at [path/to/.claude/settings.json]"
Interpret YAML output:
pass: true → Hook completepass: false → Fix all issues listed, re-run reviewer, repeat until pass: trueThis is the REFACTOR phase: Close loopholes identified by reviewer.
Verification: hook-reviewer returns YAML with pass: true.
See references/static-checks.md for linting, type checking, and auto-fix examples.
These thoughts mean you're rationalizing. STOP and reconsider:
All of these mean: You're about to create a weak hook. Follow the process.
| Excuse | Reality |
|---|---|
| "I'll remember to lint" | You won't. Hooks enforce what humans forget. |
| "Slow hooks are thorough" | Slow hooks = disabled hooks. Keep it fast. |
| "Check everything" | Full project check = 30+ seconds. Check changed file only. |
| "settings.local.json" | Local = not shared. Team hooks go in settings.json. |
| "Simple logic doesn't need tests" | Hook failures are silent. Always test. |
${CLAUDE_SKILL_DIR}/scripts/add_hook.py — scaffolds a new hook file and registers it in settings.jsonWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
23 skills in this plugin
/advising-architectureValidates component-type choices for agent system work, classifying knowledge as CLAUDE.md vs rule vs skill vs agent vs hook and checking for conflicts. Use when starting any skill/agent/rule workflow to validate approach. Use when classifying knowledge type. Use when checking for component conflicts./analyzing-agent-systemsScans project structure and agent system components, runs 11-category weakness analysis, and produces actionable restructuring recommendations. Use when analyzing an existing agent system for weaknesses. Use when user says "analyze agent system", "check agent setup", "audit agent config". Use when called by migrating-agent-systems or refactoring-skills./npx claudepluginhub wayne930242/reflexive-claude-code --plugin rccOther plugins' skills for the same job