Skip to Content
Nextra 2 Alpha
ExamplesEnhanced Context Injection System

Enhanced Context Injection System

The enhanced context injection system automatically detects task types and selects appropriate context for prompt patterns.

Task Types

The system supports the following task types:

  • CodeReview - Code review and quality assurance

  • BugFix - Bug fixing and debugging

  • FeatureDevelopment - New feature development

  • Testing - Test writing and test-related tasks

  • Documentation - Documentation writing and updates

  • Refactoring - Code refactoring and restructuring

  • SecurityReview - Security-focused reviews and audits

  • PerformanceOptimization - Performance improvements

  • DependencyUpdate - Dependency and package updates

  • Deployment - Deployment and infrastructure tasks

  • Custom - Custom task types

Automatic Task Detection

The system automatically detects task types based on:

  1. Git Status - Analyzes modified files and commit messages

  2. File Types - Examines file extensions and paths

  3. Directory Structure - Looks at folder names and organization

Detection Examples

# Git status detection modified: src/auth/security.rs # → SecurityReview modified: tests/user_test.rs # → Testing modified: docs/README.md # → Documentation # File type detection *.test.js, *.spec.js # → Testing *.md, *.txt # → Documentation Cargo.lock, package-lock.json # → DependencyUpdate

Context Injection Rules

Each task type has predefined injection rules that specify:

  • Context Files - Which .rhema/ files to load

  • Injection Method - How to combine context with prompts

  • Priority - Rule precedence when multiple rules match

  • Additional Context - Task-specific guidance

Default Rules

# Code Review task_type: CodeReview context_files: ["patterns.yaml", "knowledge.yaml"] injection_method: TemplateVariable priority: 1 additional_context: "Focus on code quality, best practices, and potential issues." # Bug Fix task_type: BugFix context_files: ["knowledge.yaml", "decisions.yaml"] injection_method: Prepend priority: 2 additional_context: "Consider previous bug fixes and known issues." # Security Review task_type: SecurityReview context_files: ["patterns.yaml", "knowledge.yaml"] injection_method: Prepend priority: 3 additional_context: "Focus on security vulnerabilities, authentication, and data protection."

Usage Examples

Basic Testing

# Test a prompt pattern with automatic task detection rhema prompt test "Code Review" # Test with specific task type rhema prompt test "Code Review" --task-type security # Test with explicit task type rhema prompt test-with-task "Code Review" security

Context Rules Management

# List all context injection rules rhema context-rules list # Test context injection for a specific task type rhema context-rules test security # Add a custom context injection rule (not yet persisted) rhema context-rules add "custom_task" \ --context-files "patterns.yaml,knowledge.yaml" \ --injection-method prepend \ --priority 5 \ --additional-context "Custom task-specific guidance"

Interactive Testing

# Start interactive mode rhema interactive --advanced # Test context injection builder prompt # Then test with: prompt test "Code Review" --task-type security

Context File Selection

The system intelligently selects context files based on task type:

Task TypeContext FilesRationale
CodeReviewpatterns.yaml, knowledge.yamlBest practices and domain knowledge
BugFixknowledge.yaml, decisions.yamlPrevious fixes and architectural decisions
Testingpatterns.yamlTesting patterns and conventions
Documentationknowledge.yamlDomain knowledge and terminology
SecurityReviewpatterns.yaml, knowledge.yamlSecurity patterns and vulnerabilities
PerformanceOptimizationpatterns.yamlPerformance patterns and benchmarks
Refactoringpatterns.yaml, decisions.yamlArchitectural patterns and decisions
FeatureDevelopmentpatterns.yaml, knowledge.yaml, decisions.yamlFull context for new features

Injection Methods

Three injection methods are supported:

  1. Prepend - Add context before the prompt template

  2. Append - Add context after the prompt template

  3. TemplateVariable - Replace {{CONTEXT}} placeholder in template

Example Output

$ rhema prompt test "Code Review" --task-type security 🧪 Testing prompt pattern 'Code Review': ============================================================ === patterns.yaml === # Security patterns security_patterns: input_validation: description: "Always validate user input" implementation: ["Use regex validation", "Sanitize input"] === knowledge.yaml === # Security knowledge security_vulnerabilities: - "SQL injection in user input" - "XSS in form submissions" === Task Context === Focus on security vulnerabilities, authentication, and data protection. === Task Type === Current task: SecurityReview Context files: patterns.yaml, knowledge.yaml Please review this code for security issues: {{CONTEXT}} Let me know if there are any security concerns. ============================================================ Injection method: TemplateVariable Success rate: 0.85 Task type: SecurityReview

Advanced Features

Custom Task Types

You can create custom task types for specialized workflows:

# Test with custom task type rhema prompt test "Code Review" --task-type "ml_model_review"

Priority System

Rules with higher priority take precedence when multiple rules match:

# High priority rule for security in authentication code task_type: SecurityReview priority: 5 additional_context: "Extra security focus for authentication code" # Lower priority general security rule task_type: SecurityReview priority: 3 additional_context: "General security review"

Future Enhancements

Planned improvements include:

  • Persistent Custom Rules - Save custom injection rules to .rhema/context-rules.yaml

  • Git Integration - Real git status analysis using git2

  • AI-Powered Detection - Use AI to suggest task types based on code changes

  • Rule Templates - Pre-built rule templates for common workflows

  • Rule Validation - Validate that specified context files exist

  • Performance Optimization - Cache context loading for better performance

Troubleshooting

Common Issues

  1. No context files found - Ensure .rhema/ directory exists with context files

  2. Task type not detected - Check git status and file types in current directory

  3. Rule not matching - Verify task type spelling and check rule priorities

Debug Mode

Enable debug output to see task detection details:

RUST_LOG=debug rhema prompt test "Code Review"

This will show:

  • Git status analysis

  • File type detection

  • Rule matching process

  • Context loading details

Last updated on