|
| 1 | +# Smart Rules - Intelligent Semantic Rule Injection |
| 2 | + |
| 3 | +Smart Rules is a feature that automatically selects and injects relevant rules into the AI context based on the current task, dramatically improving token efficiency and response quality. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Traditional rule systems inject all rules into every conversation, leading to: |
| 8 | + |
| 9 | +- **Token waste**: Irrelevant rules consume valuable context tokens |
| 10 | +- **Context pollution**: Unrelated rules can mislead AI responses |
| 11 | +- **Poor scalability**: Rule overhead grows linearly with project complexity |
| 12 | +- **Limited rule depth**: Users avoid detailed rules due to constant overhead |
| 13 | + |
| 14 | +Smart Rules solves these problems by intelligently matching user queries against rule triggers and only including relevant rules. |
| 15 | + |
| 16 | +## How It Works |
| 17 | + |
| 18 | +1. **Rule Definition**: Create markdown files with YAML frontmatter specifying when rules should apply |
| 19 | +2. **Semantic Matching**: The system analyzes user queries and calculates similarity scores against rule triggers |
| 20 | +3. **Automatic Selection**: Only rules exceeding the similarity threshold are included in the context |
| 21 | +4. **Dependency Resolution**: Rules can specify dependencies that are automatically included |
| 22 | + |
| 23 | +## Creating Smart Rules |
| 24 | + |
| 25 | +### File Structure |
| 26 | + |
| 27 | +Smart rules are stored in special directories within your `.roo` folder: |
| 28 | + |
| 29 | +``` |
| 30 | +.roo/ |
| 31 | +├── smart-rules/ # General smart rules |
| 32 | +│ ├── supabase.md |
| 33 | +│ ├── testing.md |
| 34 | +│ └── api-design.md |
| 35 | +└── smart-rules-{mode}/ # Mode-specific smart rules |
| 36 | + ├── database.md |
| 37 | + └── frontend.md |
| 38 | +``` |
| 39 | + |
| 40 | +### Rule Format |
| 41 | + |
| 42 | +Each smart rule is a markdown file with YAML frontmatter: |
| 43 | + |
| 44 | +````markdown |
| 45 | +--- |
| 46 | +use-when: "interacting with Supabase client, database queries, or authentication" |
| 47 | +priority: 10 |
| 48 | +dependencies: |
| 49 | + - "typescript.md" |
| 50 | +--- |
| 51 | + |
| 52 | +# Supabase Best Practices |
| 53 | + |
| 54 | +When working with Supabase: |
| 55 | + |
| 56 | +## Authentication |
| 57 | + |
| 58 | +- Always use TypeScript for better type safety |
| 59 | +- Prefer RLS policies over client-side filtering |
| 60 | +- Store sensitive configuration in environment variables |
| 61 | + |
| 62 | +## Database Queries |
| 63 | + |
| 64 | +```typescript |
| 65 | +// Good: Type-safe query with error handling |
| 66 | +const { data, error } = await supabase.from<User>("users").select("*").eq("id", userId).single() |
| 67 | + |
| 68 | +if (error) throw error |
| 69 | +``` |
| 70 | +```` |
| 71 | + |
| 72 | +## Real-time Subscriptions |
| 73 | + |
| 74 | +- Always clean up subscriptions in useEffect cleanup |
| 75 | +- Use proper TypeScript types for real-time payloads |
| 76 | + |
| 77 | +```` |
| 78 | +
|
| 79 | +### Frontmatter Fields |
| 80 | +
|
| 81 | +- **use-when** (required): Description of when this rule should be applied. This is matched semantically against user queries. |
| 82 | +- **priority** (optional): Higher priority rules are selected first when multiple rules match (default: 0) |
| 83 | +- **dependencies** (optional): Array of other rule filenames that should be included when this rule is selected |
| 84 | +
|
| 85 | +## Configuration |
| 86 | +
|
| 87 | +Smart Rules can be configured in VS Code settings: |
| 88 | +
|
| 89 | +```json |
| 90 | +{ |
| 91 | + "roo-cline.smartRules.enabled": true, |
| 92 | + "roo-cline.smartRules.minSimilarity": 0.7, |
| 93 | + "roo-cline.smartRules.maxRules": 5, |
| 94 | + "roo-cline.smartRules.showSelectedRules": false, |
| 95 | + "roo-cline.smartRules.debugRuleSelection": false |
| 96 | +} |
| 97 | +```` |
| 98 | + |
| 99 | +### Settings |
| 100 | + |
| 101 | +- **enabled**: Enable/disable smart rules functionality |
| 102 | +- **minSimilarity**: Minimum similarity score (0-1) for rule selection. Lower values include more rules. |
| 103 | +- **maxRules**: Maximum number of smart rules to include in a single prompt |
| 104 | +- **showSelectedRules**: Display which rules were selected in the UI |
| 105 | +- **debugRuleSelection**: Enable detailed logging of the rule selection process |
| 106 | + |
| 107 | +## Examples |
| 108 | + |
| 109 | +### Example 1: Database Operations |
| 110 | + |
| 111 | +**User Query**: "Set up Supabase authentication in my Next.js app" |
| 112 | + |
| 113 | +**Selected Rules**: |
| 114 | + |
| 115 | +1. `supabase.md` (score: 0.85) - Matched "Supabase" and "authentication" |
| 116 | +2. `nextjs-app-router.md` (score: 0.72) - Matched "Next.js app" |
| 117 | +3. `typescript.md` (score: 0.70) - Included as dependency of supabase.md |
| 118 | + |
| 119 | +### Example 2: API Development |
| 120 | + |
| 121 | +**User Query**: "Create a REST API endpoint for user management" |
| 122 | + |
| 123 | +**Selected Rules**: |
| 124 | + |
| 125 | +1. `api-design.md` (score: 0.88) - Matched "REST API" and "endpoint" |
| 126 | +2. `testing.md` (score: 0.70) - Included as dependency |
| 127 | +3. `validation.md` (score: 0.71) - Matched "user management" |
| 128 | + |
| 129 | +## Best Practices |
| 130 | + |
| 131 | +### Writing Effective use-when Triggers |
| 132 | + |
| 133 | +1. **Be Specific**: Include key terms and phrases that users would naturally use |
| 134 | + |
| 135 | + ```yaml |
| 136 | + # Good |
| 137 | + use-when: "working with React hooks, useState, useEffect, or custom hooks" |
| 138 | + |
| 139 | + # Too vague |
| 140 | + use-when: "React development" |
| 141 | + ``` |
| 142 | +
|
| 143 | +2. **Include Variations**: Account for different ways users might describe the same task |
| 144 | +
|
| 145 | + ```yaml |
| 146 | + use-when: "database queries, SQL operations, data fetching, or ORM usage" |
| 147 | + ``` |
| 148 | +
|
| 149 | +3. **Use Natural Language**: Write triggers as if describing when a human would need the rule |
| 150 | + ```yaml |
| 151 | + use-when: "debugging performance issues, optimizing slow code, or profiling applications" |
| 152 | + ``` |
| 153 | +
|
| 154 | +### Organizing Rules |
| 155 | +
|
| 156 | +1. **Granular Rules**: Create focused rules for specific topics rather than large, general rules |
| 157 | +2. **Use Dependencies**: Link related rules instead of duplicating content |
| 158 | +3. **Mode-Specific Rules**: Place mode-specific rules in `smart-rules-{mode}` directories |
| 159 | +4. **Prioritize Important Rules**: Use the priority field for rules that should take precedence |
| 160 | + |
| 161 | +### Performance Considerations |
| 162 | + |
| 163 | +1. **Rule Count**: While there's no hard limit, 50-100 well-organized rules perform well |
| 164 | +2. **Content Size**: Keep individual rules focused; very large rules still consume tokens when selected |
| 165 | +3. **Similarity Threshold**: Adjust `minSimilarity` based on your rule specificity: |
| 166 | + - 0.8-1.0: Very strict matching, fewer rules selected |
| 167 | + - 0.6-0.8: Balanced matching (recommended) |
| 168 | + - 0.4-0.6: Loose matching, more rules selected |
| 169 | + |
| 170 | +## Migration Guide |
| 171 | + |
| 172 | +### From Traditional Rules |
| 173 | + |
| 174 | +1. **Identify Rule Categories**: Group your existing rules by topic or use case |
| 175 | +2. **Create Smart Rule Files**: Convert each group into a smart rule with appropriate `use-when` trigger |
| 176 | +3. **Add Dependencies**: Link related rules using the dependencies field |
| 177 | +4. **Test Selection**: Use debug mode to verify rules are selected appropriately |
| 178 | +5. **Adjust Triggers**: Refine `use-when` descriptions based on actual usage |
| 179 | + |
| 180 | +### Example Migration |
| 181 | + |
| 182 | +**Before** (`.roo/rules/database.md`): |
| 183 | + |
| 184 | +```markdown |
| 185 | +# Database Rules |
| 186 | +
|
| 187 | +Always use prepared statements... |
| 188 | +``` |
| 189 | + |
| 190 | +**After** (`.roo/smart-rules/database.md`): |
| 191 | + |
| 192 | +```markdown |
| 193 | +--- |
| 194 | +use-when: "database operations, SQL queries, data persistence, or ORM configuration" |
| 195 | +priority: 5 |
| 196 | +--- |
| 197 | +
|
| 198 | +# Database Rules |
| 199 | +
|
| 200 | +Always use prepared statements... |
| 201 | +``` |
| 202 | + |
| 203 | +## Troubleshooting |
| 204 | + |
| 205 | +### Rules Not Being Selected |
| 206 | + |
| 207 | +1. **Check Similarity Score**: Enable `debugRuleSelection` to see similarity scores |
| 208 | +2. **Lower Threshold**: Temporarily reduce `minSimilarity` to test |
| 209 | +3. **Improve Triggers**: Add more relevant keywords to `use-when` |
| 210 | +4. **Verify File Location**: Ensure rules are in correct directories |
| 211 | + |
| 212 | +### Too Many Rules Selected |
| 213 | + |
| 214 | +1. **Increase Threshold**: Raise `minSimilarity` value |
| 215 | +2. **Reduce Max Rules**: Lower `maxRules` setting |
| 216 | +3. **Refine Triggers**: Make `use-when` descriptions more specific |
| 217 | + |
| 218 | +### Performance Issues |
| 219 | + |
| 220 | +1. **Check Rule Count**: Large numbers of rules may slow selection |
| 221 | +2. **Optimize Content**: Keep rule content focused and concise |
| 222 | +3. **Review Dependencies**: Avoid circular or excessive dependencies |
| 223 | + |
| 224 | +## Advanced Usage |
| 225 | + |
| 226 | +### Dynamic Rule Generation |
| 227 | + |
| 228 | +Smart rules can be generated programmatically for large projects: |
| 229 | + |
| 230 | +```javascript |
| 231 | +const generateSmartRule = (component, guidelines) => ({ |
| 232 | + filename: `${component.toLowerCase()}.md`, |
| 233 | + frontmatter: { |
| 234 | + "use-when": `working with ${component} component, ${component} API, or ${component} configuration`, |
| 235 | + priority: component.critical ? 10 : 5, |
| 236 | + }, |
| 237 | + content: guidelines, |
| 238 | +}) |
| 239 | +``` |
| 240 | + |
| 241 | +### Integration with CI/CD |
| 242 | + |
| 243 | +Validate smart rules in your pipeline: |
| 244 | + |
| 245 | +```bash |
| 246 | +# Check for required frontmatter |
| 247 | +find .roo/smart-rules -name "*.md" -exec grep -L "use-when:" {} \; |
| 248 | + |
| 249 | +# Validate YAML frontmatter |
| 250 | +for file in .roo/smart-rules/*.md; do |
| 251 | + head -n 20 "$file" | sed -n '/^---$/,/^---$/p' | yaml-lint |
| 252 | +done |
| 253 | +``` |
| 254 | + |
| 255 | +## Future Enhancements |
| 256 | + |
| 257 | +- **Machine Learning**: Improve matching using embeddings and vector similarity |
| 258 | +- **Rule Analytics**: Track which rules are most frequently selected |
| 259 | +- **Auto-generation**: Generate rules from codebase patterns and documentation |
| 260 | +- **Rule Sharing**: Community marketplace for smart rule templates |
0 commit comments