extract-rules
npx skills add https://github.com/hiroro-work/claude-plugins --skill extract-rules
Agent 安装分布
Skill 文档
Extract Rules
Analyzes existing codebase to extract project-specific coding rules and domain knowledge, generating structured markdown documentation for AI agents.
Usage
/extract-rules # Extract rules from codebase (initial)
/extract-rules --update # Re-scan and add new patterns (preserve existing)
/extract-rules --force # Overwrite all rule files (discard existing)
/extract-rules --from-conversation # Extract rules from conversation and append
Configuration
Users can configure extraction settings in extract-rules.local.md:
- Project-level:
.claude/extract-rules.local.md(takes precedence) - User-level:
~/.claude/extract-rules.local.md
File format: YAML frontmatter only (no markdown body). The file uses .md extension for consistency with other Claude Code config files, but contains only YAML between --- delimiters.
---
# Target directories for analysis
# Default: "." (all directories not excluded by .gitignore)
# Set specific directories to limit scope
target_dirs:
- .
# Directories to exclude (in addition to .gitignore)
# These are applied even if not in .gitignore
exclude_dirs:
- .git
- .claude
# File patterns to exclude (in addition to .gitignore)
exclude_patterns:
- "*.generated.ts"
- "*.d.ts"
- "*.min.js"
# Note: .gitignore patterns are automatically applied
# Common exclusions like node_modules/, dist/, build/ are typically in .gitignore
# Output directory
output_dir: .claude/rules
# Output language for reports
language: ja
---
Output Structure
.claude/rules/
âââ languages/
â âââ typescript.md
â âââ ... # python.md, go.md, ruby.md, etc.
âââ frameworks/
â âââ react.md
â âââ ... # nextjs.md, firebase.md, rails.md, etc.
âââ project.md # Domain, architecture (not portable)
Processing Flow
Mode Detection
Check arguments to determine mode:
- No arguments or
--forceâ Full Extraction Mode (Step 1-7) --updateâ Update Mode (Step U1-U5)--from-conversationâ Conversation Extraction Mode (Step C1-C4)
Full Extraction Mode
Step 1: Load Settings
Search for extract-rules.local.md:
- Project-level:
.claude/extract-rules.local.md - User-level:
~/.claude/extract-rules.local.md
Priority:
- If both exist, use project-level only
- If only one exists, use that file
- If neither exists, use default settings
Extract from settings:
target_dirs(default:["."]– all directories)exclude_dirs(default:[".git", ".claude"])exclude_patterns(default:["*.generated.ts", "*.d.ts", "*.min.js"])output_dir(default:.claude/rules)language(default:ja)
Note: .gitignore patterns are always applied. Common exclusions like node_modules/, dist/, build/ are typically in .gitignore and automatically excluded.
Step 2: Detect Project Type
Detect project language and framework:
1. Check configuration files:
package.jsonâ Node.js/TypeScript/JavaScripttsconfig.jsonâ TypeScriptpyproject.toml,requirements.txtâ Pythongo.modâ GoCargo.tomlâ RustGemfileâ Ruby/Railspom.xml,build.gradleâ Java
2. Count file extensions:
.ts,.tsxâ TypeScript.js,.jsxâ JavaScript.pyâ Python.goâ Go.rbâ Ruby
3. Detect framework-specific files:
Identify frameworks by their config files (e.g., next.config.*, playwright.config.*, jest.config.*) and dependencies in package.json, requirements.txt, Gemfile, etc.
Output: List of detected languages and frameworks
Step 3: Collect Sample Files
Collect target files for analysis:
-
Get git-tracked files (if in a git repository)
- Use
git ls-filesto get list of tracked files - This automatically respects ALL
.gitignorefiles (root and subdirectories) - If not a git repo, fall back to Glob with manual exclusions:
- Read
.gitignoreif present and apply patterns - Apply
exclude_dirsandexclude_patternsfrom settings - Note: Nested
.gitignorefiles may not be fully respected in non-git mode
- Read
- Use
-
Filter files by
target_dirssetting -
Exclude files matching:
exclude_dirsfrom settingsexclude_patternsfrom settings
-
Filter by detected language extensions
-
Sample files per category, distributed across directories for representative coverage
Note: Using git ls-files ensures that nested .gitignore files in subdirectories are automatically respected. Untracked files (e.g., .env, local configs) are excluded, which helps protect sensitive information.
Step 4: Analyze by Category
For each detected language and framework:
-
Use Grep/Read to collect relevant code patterns
-
Classify each pattern (see Concrete Example Criteria):
- General style choice (uses only language built-ins) â Abstract principle + hints
- Project-defined symbol (types, functions, hooks defined in project) â Include concrete example
-
For general style patterns:
- Group related patterns (e.g., “prefer const”, “avoid mutations”, “use spread” â Immutability)
- Formulate as principle with parenthetical implementation hints (2-4 keywords)
-
For project-specific patterns:
- Extract only the minimal signature (type definition, function signature, or API combination)
- Format as one line:
signature– brief context (2-5 words) - Avoid multi-line code blocks to minimize context overhead
-
Apply AI judgment to determine which patterns meet the extraction criteria (see Principle Extraction Criteria)
Determine appropriate detection methods based on language and project structure.
Step 5: Analyze Documentation
Also analyze non-code documentation:
- README.md
- CONTRIBUTING.md
- PR templates
- Existing CLAUDE.md
Extract explicit coding rules and guidelines from these documents.
Step 6: Generate Output
-
Check if output directory exists
- If exists and
--forcenot set: Error “Output directory already exists. Use –force to overwrite.” - If exists and
--forceset:- Warning: “Existing rules will be overwritten. Manual edits will be lost.”
- List files that will be overwritten
- Proceed with overwrite (backup is user’s responsibility via git)
- If not exists: Create directory
- If exists and
-
Generate rule files:
languages/<lang>.mdfor language-specific rulesframeworks/<framework>.mdfor framework-specific rulesproject.mdfor project-specific rules
Rule file format (hybrid: principles + project-specific patterns):
---
paths:
- "**/*.ts"
- "**/*.tsx"
---
# TypeScript Rules
## Principles
- Immutability (spreadæ¼ç®å, map/filter/reduce, conståªå
)
- Declarative style (å½ä»¤çã«ã¼ããã宣è¨ç夿)
- Type safety (strict mode, æç¤ºçåæ³¨é, anyç¦æ¢)
## Project-specific patterns
- `RefOrNull<T extends { id: string }> = T | { id: null }` - nullable relationships
- `pathFor(page) + url()` - Page Object navigation pair
- `useAuthClient()` returns `{ user, login, logout }` - auth hook interface
Format guidelines:
For Principles section:
- Each principle:
Principle name (hint1, hint2, hint3) - 2-4 implementation hints per principle
- Only for general style choices (language built-ins)
For Project-specific patterns section:
- One line per pattern:
signature/definition– brief context - Use inline code for signatures, not code blocks
- Keep context to 2-5 words maximum
- Only include the minimal signature needed for AI to infer usage
paths patterns by category:
- TypeScript:
**/*.ts,**/*.tsx - Python:
**/*.py - React:
**/*.tsx,**/*.jsx - (project.md: no paths frontmatter = applies to all files)
Step 7: Report Summary
Display analysis summary:
## Extraction Complete
**Project**: [project name]
**Languages**: [detected languages]
**Frameworks**: [detected frameworks]
**Analyzed files**: [count]
### Generated Files
| File | Principles |
|------|------------|
| languages/typescript.md | 3 principles |
| frameworks/react.md | 2 principles |
| project.md | 2 principles |
**Output**: `<output_dir>` (default: .claude/rules/)
### Recommended Actions
1. Review generated rules and edit if needed
2. Add reference to CLAUDE.md:
\`\`\`markdown
## Coding Rules
See .claude/rules/ for project-specific coding rules.
\`\`\`
3. Re-run with `/extract-rules --update` when codebase evolves
Update Mode
When --update is specified, re-scan the codebase and add new patterns while preserving existing rules.
Step U1: Check Prerequisites
-
Check if output directory exists (default:
.claude/rules/)- If not exists: Error “Run /extract-rules first to initialize rule files.”
-
Load existing rule files to understand current rules
Step U2: Re-scan Codebase
Execute Step 1-5 from Full Extraction Mode:
- Load settings
- Detect project type
- Collect sample files
- Analyze by category
- Analyze documentation
Step U3: Compare and Merge
For each extracted principle/pattern:
-
Check if already exists: Compare with existing rules
- Exact match â Skip
- Similar but different â Keep both (let user review)
- New â Add
-
Preserve manual edits: Do not modify existing rules
Step U4: Append New Rules
- Append new principles to appropriate section (
## Principles) - Append new project-specific patterns to appropriate section (
## Project-specific patterns) - Maintain file structure and formatting
Step U5: Report Changes
## Update Complete
### Added to languages/typescript.md:
#### Principles
- (none)
#### Project-specific patterns
- `useNewFeature()` returns `{ data, refresh }` - new feature hook
### Added to frameworks/react.md:
- (none)
### Unchanged files:
- project.md
**Tip**: Review added rules and remove any that are incorrect or redundant.
Conversation Extraction Mode
When --from-conversation is specified, extract rules from the conversation history.
Step C1: Check Prerequisites
-
Check if output directory exists (default:
.claude/rules/)- If not exists: Error “Run /extract-rules first to initialize rule files.”
-
Load existing rule files to understand current rules
Step C2: Determine Conversation Source
Option A: Read from transcript file (preferred, full history)
If transcript file path is known:
- Location:
~/.claude/projects/<project-path>/<session-id>.jsonl - Read the JSONL file (each line is a JSON object)
- Focus on
type: "user"andtype: "assistant"entries - This includes ALL messages from session start (even after compaction)
Option B: Use current context (fallback)
If transcript path is unknown (e.g., running in Codex or other AI tools):
- Analyze the current conversation context
- Note: May have limited history if context was compacted
Step C3: Extract Principles and Patterns
Look for user preferences and classify them (same as Full Extraction Mode):
1. General style preferences â Abstract to principles:
- “Use type instead of interface” â Type safety principle
- “Avoid mutations” â Immutability principle
2. Project-specific patterns â Extract with concrete examples:
- “Use
RefOrNull<T>for nullable refs” â Include type definition - “Always use
pathFor()withurl()” â Include usage pattern
3. Code review feedback: Identify underlying philosophy or specific patterns
Apply the same criteria as Full Extraction Mode (see Principle Extraction Criteria and Concrete Example Criteria).
Step C4: Append Principles and Patterns
-
Categorize each extracted item:
- Language-specific â
languages/<lang>.md - Framework-specific â
frameworks/<framework>.md - Project-specific â
project.md
- Language-specific â
-
Check for duplicates: Skip if already exists or covered
-
Append in appropriate format:
- Principles:
Principle name (hint1, hint2, hint3) - Project-specific patterns:
signature– brief context (one line)
- Principles:
-
Report what was added:
## Extracted from Conversation ### Added to languages/typescript.md: #### Principles - Immutability (spread operators, map/filter, avoid mutations) #### Project-specific patterns - `RefOrNull<T extends { id: string }> = T | { id: null }` - nullable refs ### No changes: - Functional style - Already documented
Important Notes
- This skill uses AI to understand intent, not just pattern matching
- Both code AND documentation are analyzed
- Use
--from-conversationafter significant discussions about coding style - Generated rules are meant to be reviewed and refined by humans
Principle Extraction Criteria
Goal: Extract abstract principles that guide AI to write code consistent with project style.
Extract these principles
Principles where multiple common approaches exist and AI might choose differently without guidance:
- Immutability vs Mutability – AI often writes mutable code by default
- Declarative vs Imperative – Both are common approaches
- Functional vs Class-based – Both are valid paradigms
- OOP vs FP – Different design philosophies
Do NOT extract these
Principles where only one practical approach exists:
- React components use PascalCase (no alternative)
- Python uses snake_case (language standard)
- TypeScript files use
.tsextension
Decision criterion
“Would a general AI write code differently without this principle?”
- Yes â Extract it
- No â Skip it
Abstraction examples
| Concrete patterns observed | Abstract principle |
|---|---|
const preferred, spread operators, no mutations |
Immutability (const, spread, map/filter) |
| Arrow functions, no classes, pure functions | Functional style (arrow functions, pure, no this) |
| Strict TypeScript, explicit types, no any | Type safety (strict, explicit, no any) |
Concrete Example Criteria
Goal: Determine when to include concrete code examples vs abstract principles.
Include concrete examples when
Pattern involves project-defined symbols that AI cannot infer:
- Custom types/interfaces defined in the project (not from node_modules)
- Project-specific hooks (e.g.,
useAuthClient,useDataFetch) - Utility functions with non-obvious signatures
- Non-obvious combinations (e.g.,
pathFor()+url()must be used together)
Important: Keep examples minimal
- One line per pattern:
signature– context (2-5 words) - Include only the type signature or function signature
- Omit implementation details, only show the “shape” AI needs to know
Keep abstract (principles only) when
Pattern uses only language built-ins or well-known patterns:
const,let, spread operators, map/filter/reduce- Standard design patterns with well-known implementations
- Framework APIs documented in official docs
Decision criterion
“Can AI correctly implement this pattern by knowing only its name?”
- Yes â Abstract principle with hints
- No â Include concrete example
Example classification
| Pattern | Classification | Reason |
|---|---|---|
Prefer const over let |
Principle only | Language built-in, AI knows this |
RefOrNull<T> type usage |
Concrete example | Project-defined type, AI cannot infer |
| Page Object Model | Principle + hints | Well-known pattern |
pathFor() + url() combination |
Concrete example | Project-specific API combination |
Gray zone handling
For patterns that are not clearly general or project-specific:
- Extended types from node_modules (e.g.,
type MyUser = User & { custom: string }) - Specific combinations of standard libraries (e.g., zod + react-hook-form patterns)
Fallback rule: When uncertain, include a concrete example.
Rationale: Over-specifying is less harmful than under-specifying. An unnecessary example adds minimal context overhead, but a missing example may cause AI to guess incorrectly.
Security Considerations
Sensitive Information Protection:
git ls-filesonly analyzes tracked files, automatically excluding untracked.env, credentials, and other gitignored files- Warning: If
.envor credential files are accidentally tracked in git, they WILL be included in analysis - Hardcoded secrets in source code may appear in examples
- When generating rule files, avoid including:
- API keys, tokens, or credentials found in code
- Internal URLs or endpoints
- Customer names or personal information
- High-entropy strings that may be secrets
- If sensitive information is detected in samples, redact with placeholders (e.g.,
API_KEY_REDACTED) - Review generated rule files before committing to repository
- Conversation extraction: Same rules apply – do not extract sensitive information from conversation history (API keys, credentials, internal URLs mentioned in chat)