Automatically enhance Claude Code's Plan Mode with quality guardrails, auto-revision, and rich context
- Quick Start
- How It Works
- Plan Mode Detection
- Context Injection
- Quality Guardrails
- Auto-Revision System
- Plan QA Verification
- Configuration Reference
- Troubleshooting
- Performance
Plan Mode integration activates automatically when Claude Code enters planning mode. No additional setup is required beyond the standard Claude Code Memory installation.
# Explicit marker (recommended)
@plan Create an authentication system for the app
# Planning keywords (auto-detected)
Create a step-by-step plan for implementing user authentication
# Environment variable
export CLAUDE_PLAN_MODE=true- Planning guidelines injected into context
- Exploration hints suggest memory queries
- Quality guardrails validate the generated plan
- Auto-revision adds missing tasks (tests, docs)
- QA verification ensures plan completeness
sequenceDiagram
participant U as User
participant D as Detector
participant I as Injector
participant C as Claude
participant G as Guardrails
participant R as Revision
participant Q as QA
U->>D: "@plan Create feature X"
D->>D: Detect Plan Mode (<10ms)
D->>I: Plan Mode active
I->>C: Inject guidelines + hints (<50ms)
C->>C: Generate plan
C->>G: Validate plan (<500ms)
G->>R: Findings
R->>R: Auto-revise (<200ms)
R->>Q: Revised plan
Q->>Q: Verify quality (<50ms)
Q->>U: Plan + QA feedback
| Stage | Component | Latency | Purpose |
|---|---|---|---|
| 1 | Plan Mode Detector | <10ms | Identify planning intent |
| 2 | Context Injector | <50ms | Add guidelines + hints |
| 3 | Guardrail Engine | <500ms | Validate against 5 rules |
| 4 | Auto-Revision | <200ms | Fix issues automatically |
| 5 | Plan QA | <50ms | Final verification |
Total overhead: <100ms (excluding validation)
The detector uses four signals to identify when Claude is in planning mode:
@agent-plan # Full explicit marker
@plan # Short marker
--plan # Flag style
plan mode # Natural language
Pattern: (create|make|write|design|implement) ... plan
"Create a plan for authentication" # 0.7 confidence
"Design a step-by-step migration plan" # 0.8 confidence (boosted)
"Write a detailed implementation plan" # 0.7 confidence
Confidence Boosters (+0.1 each):
- "step-by-step"
- "phases"
- "milestones"
- "tasks"
- "timeline"
- "roadmap"
Confidence Reducers (-0.15):
- "execute"
- "run"
- "implement this"
- "start coding"
export CLAUDE_PLAN_MODE=true # Enable
export CLAUDE_PLAN_MODE=false # DisableAccepted values: true, 1, yes, on (case-insensitive)
Once Plan Mode is detected, it persists for the session unless explicitly disabled.
{
"is_plan_mode": True,
"confidence": 1.0,
"source": "explicit_marker",
"detected_markers": ["@plan"],
"detection_time_ms": 0.5,
"reasoning": "Explicit @plan marker detected"
}When Plan Mode is detected, two types of context are injected:
Five sections guide Claude toward high-quality plans:
Before proposing ANY new function, class, or component:
- Search the codebase: `mcp__project-memory__search_similar("functionality")`
- Check existing patterns: `mcp__project-memory__read_graph(entity="Component")`
- If similar exists, plan to REUSE or EXTEND it
- State explicitly: "Verified no existing implementation" or "Will extend existing Y"Every plan that modifies code MUST include:
- [ ] Unit tests for new/modified functions
- [ ] Integration tests for API changes
- Task format: "Add tests for [feature] in [test_file]"Include documentation tasks when:
- Adding public APIs -> Update API docs
- Changing user-facing behavior -> Update README
- Adding configuration -> Update config docsYour plan MUST align with project patterns:
[Patterns extracted from CLAUDE.md]Flag any step that may introduce:
- O(n^2) or worse complexity
- Unbounded memory usage
- Missing timeouts on network callsSuggested MCP queries based on entities extracted from the prompt:
## Duplicate Check
mcp__project-memory__search_similar("UserService", entityTypes=["function", "class"])
## Test Discovery
mcp__project-memory__search_similar("UserService test", entityTypes=["file", "function"])
## Documentation
mcp__project-memory__search_similar("documentation README", entityTypes=["documentation"])
## Entity Analysis
mcp__project-memory__read_graph(entity="UserService", mode="smart")Entities are extracted from prompts using pattern matching:
| Pattern | Example | Extraction |
|---|---|---|
| CamelCase | UserService |
Class/component names |
| snake_case | user_service |
Function/variable names |
| Quoted | "login" |
Specific terms |
| Technical | api, database |
Domain terms |
Five validation rules ensure plan quality:
| Property | Value |
|---|---|
| Category | coverage |
| Severity | MEDIUM |
| Is Fast | Yes |
Detects: Feature/implementation tasks without corresponding test tasks.
Trigger Keywords:
- implement, add, create, build, develop, introduce, design, write
Trivial Exceptions (no test required):
- fix typo, rename, move, delete comment, update readme
Auto-Revision: Adds a test task with:
- ID:
TASK-TST-{feature_id} - Title: "Add tests for {feature_title}"
- Dependency on the feature task
- Tags:
["testing", "quality"]
Example:
Finding: Task "Implement user authentication" has no test task
Suggestion: Add unit/integration test task for authentication
Auto-Fix: Creates "Add tests for user authentication" task
| Property | Value |
|---|---|
| Category | coverage |
| Severity | LOW |
| Is Fast | Yes |
Detects: User-facing changes without documentation tasks.
User-Facing Indicators:
- api, user, interface, config, cli, command, endpoint
- ui, ux, frontend, dashboard, setting, option
- public, external, exposed, visible, accessible
Auto-Revision: Adds a documentation task with:
- ID:
TASK-DOC-{feature_id} - Title: "Document {feature_title}"
- Dependency on the feature task
- Tags:
["documentation"]
| Property | Value |
|---|---|
| Category | consistency |
| Severity | HIGH |
| Is Fast | No (uses memory search) |
Detects: Tasks proposing code that may already exist in the codebase.
Similarity Threshold: 70% (configurable)
Search Entity Types:
- function
- class
- implementation_pattern
Auto-Revision: Modifies the task description to:
- Reference the existing implementation
- Add acceptance criteria: "Verified no duplication with existing code"
Example:
Finding: Task "Create password hasher" is 85% similar to existing hashPassword() in auth/utils.py
Suggestion: Review existing implementation before proceeding
Auto-Fix: Adds note referencing auth/utils.py:hashPassword()
| Property | Value |
|---|---|
| Category | architecture |
| Severity | MEDIUM |
| Is Fast | Yes |
Detects: File paths that don't match established project patterns.
Expected Patterns:
| Type | Expected Locations |
|---|---|
| tests | tests/, __tests__/, *.test.py, *.spec.ts |
| components | src/components/, app/components/ |
| utils | src/utils/, lib/, helpers/ |
| config | src/config/, *.config.py |
| api | src/api/, routes/, endpoints/ |
| models | src/models/, entities/, schemas/ |
| services | src/services/, providers/ |
Auto-Revision: Adds a warning note to the task about the non-standard location.
| Property | Value |
|---|---|
| Category | performance |
| Severity | LOW |
| Is Fast | Yes |
Detects: Potential performance anti-patterns in task descriptions.
Anti-Patterns:
| Pattern | Detection | Suggestion |
|---|---|---|
| N+1 Query | "for each...query", "individual requests" | Batch database queries, use eager loading |
| Missing Cache | "no cache", "every request", "always fetch" | Implement caching strategy |
| Blocking Operation | "synchronous", "blocking", "wait for" | Use async/await, background jobs |
| Unbounded Load | "load all", "fetch everything" | Implement pagination, lazy loading |
| Large Payload | "entire object", "full response" | Use field selection, streaming |
Auto-Revision: Adds a performance note with specific suggestions.
The auto-revision engine applies fixes automatically based on guardrail findings.
class RevisionType(Enum):
ADD_TASK = "add_task" # Add missing test/doc task
MODIFY_TASK = "modify_task" # Update description/criteria
REMOVE_TASK = "remove_task" # Remove redundant task
ADD_DEPENDENCY = "add_dependency" # Link tasks
REORDER_TASKS = "reorder_tasks" # Fix execution order- Sort by severity: CRITICAL > HIGH > MEDIUM > LOW
- Check for conflicts: Ensure revision won't break plan
- Detect cycles: Prevent circular dependencies
- Apply revision: Modify plan in-place
- Resolve dependencies: Clean up orphaned references
| Setting | Default | Purpose |
|---|---|---|
| MAX_ITERATIONS | 3 | Prevent infinite loops |
| max_revisions_per_plan | 10 | Limit total changes |
| revision_confidence_threshold | 0.7 | Minimum confidence to apply |
Revisions are skipped if they would:
- Create a task with an existing ID
- Introduce circular dependencies
- Modify a non-existent task
- Remove a task that others depend on
All revisions are tracked for transparency:
## Plan Revisions Applied
### 1. Added Test Task (PLAN.TEST_REQUIREMENT)
- **Reason**: Feature 'Add user authentication' needs test coverage
- **Added**: TASK-TST-0001 "Add tests for user authentication"
- **Confidence**: 95%
### 2. Modified Task (PLAN.DUPLICATE_DETECTION)
- **Reason**: Potential duplicate of existing 'AuthService.login()'
- **Modified**: TASK-0002 description to reference existing code
- **Confidence**: 78%Post-generation verification ensures the final plan meets quality standards.
| Check | Detection Pattern | Suggestion |
|---|---|---|
| Missing Tests | Code changes without test tasks | "Add unit/integration test task" |
| Missing Docs | User-facing changes without doc tasks | "Add documentation task" |
| No Reuse Check | New code without duplicate verification | "Verify no existing implementation" |
| Architecture | Performance anti-patterns detected | Specific performance notes |
=== Plan QA Feedback ===
[WARN] Missing Test Coverage:
- Plan modifies code but includes no test tasks
[WARN] Potential Duplicates (no explicit reuse check):
- New UserService implementation without checking existing code
[SUGGESTIONS]:
- Add unit/integration test task
- Search memory for existing implementations before coding
=== End Plan QA ===Or if all checks pass:
[Plan QA: All quality checks passed]| Variable | Values | Default | Purpose |
|---|---|---|---|
CLAUDE_PLAN_MODE |
true/false/1/0/yes/no | false | Force Plan Mode on/off |
CLAUDE_PLAN_MODE_COMPACT |
true/false | false | Use abbreviated guidelines |
CLAUDE_PLAN_MODE_CONFIG |
file path | none | Custom config file |
class PlanGuardrailConfig(BaseModel):
# Master toggle
enabled: bool = True
# Category toggles
check_coverage: bool = True # Test/doc requirements
check_consistency: bool = True # Duplicate detection
check_architecture: bool = True # Pattern alignment
check_performance: bool = True # Anti-pattern detection
# Auto-revision settings
auto_revise: bool = True
max_revisions_per_plan: int = 10
revision_confidence_threshold: float = 0.7
# Output limits
max_findings_per_rule: int = 10
# Severity thresholds
severity_thresholds: dict = {
"block": "HIGH",
"warn": "MEDIUM"
}
# Rule-specific configuration
rules: dict[str, RuleConfig] = {}class RuleConfig(BaseModel):
enabled: bool = True
severity: str = "MEDIUM"
threshold: float | None = None # For duplicate detection
auto_revise: bool = True@dataclass
class PlanQAConfig:
enabled: bool = True
check_tests: bool = True
check_docs: bool = True
check_duplicates: bool = True
check_architecture: bool = True
fail_on_missing_tests: bool = False # Strict mode
fail_on_missing_docs: bool = False # Strict mode{
"enabled": true,
"inject_guidelines": true,
"inject_hints": true,
"compact_mode": false,
"guidelines": {
"include_code_reuse_check": true,
"include_testing_requirements": true,
"include_documentation_requirements": true,
"include_architecture_alignment": true,
"include_performance_considerations": true
},
"hints": {
"max_entity_hints": 3,
"include_duplicate_check": true,
"include_test_discovery": true,
"include_doc_discovery": true,
"include_architecture_hints": true
},
"guardrails": {
"check_coverage": true,
"check_consistency": true,
"auto_revise": true,
"rules": {
"PLAN.DUPLICATE_DETECTION": {
"threshold": 0.75
}
}
}
}Symptoms: No guidelines injected, no validation occurring
Solutions:
- Use explicit marker:
@plan Your request here - Set environment variable:
export CLAUDE_PLAN_MODE=true - Check confidence threshold (default: 0.6)
- Verify hook is configured in
.claude/settings.json
Debug:
# Test detection directly
echo '{"prompt": "@plan Test"}' | python claude_indexer/hooks/prompt_handler.pySymptoms: Plan Mode detected but no guidelines appear
Solutions:
- Check
CLAUDE_PLAN_MODE_CONFIGpath is valid - Verify
inject_guidelines: truein config - Check
CLAUDE.mdexists for project patterns - Look for errors in hook output
Debug:
# Check guidelines generation
python -c "from claude_indexer.hooks.planning import generate_planning_guidelines; print(generate_planning_guidelines('test-collection'))"Symptoms: Plans pass validation without expected findings
Solutions:
- Verify category toggles are enabled:
check_coverage,check_consistency, etc.
- Check rule-specific config isn't disabled
- Verify similarity threshold for duplicate detection
- Ensure memory client is connected (for duplicate detection)
Debug:
# Test guardrail engine
python -c "
from claude_indexer.ui.plan.guardrails import PlanGuardrailEngine, PlanGuardrailConfig
engine = PlanGuardrailEngine(PlanGuardrailConfig())
print('Rules loaded:', list(engine.rules.keys()))
"Symptoms: Plan validation taking too long (>1s)
Solutions:
- Use
validate_fast()for quick checks (<100ms) - Disable slow rules:
PLAN.DUPLICATE_DETECTIONuses memory search - Reduce
max_findings_per_rulelimit - Check memory search latency
Optimization Options:
# Fast validation only
result = engine.validate_fast(context)
# Disable slow rules
config = PlanGuardrailConfig(
check_consistency=False # Disables duplicate detection
)Symptoms: Findings generated but plan not modified
Solutions:
- Verify
auto_revise: truein config - Check
revision_confidence_threshold(default: 0.7) - Ensure finding has
can_auto_revise: true - Check for revision conflicts in audit trail
Symptoms: Duplicate detection returns no results
Solutions:
- Verify collection is indexed:
claude-indexer search "test" -c collection - Check Qdrant connection:
curl http://localhost:6333/health - Verify MCP server is running
- Check API keys in
settings.txt
| Operation | Target | Typical | Notes |
|---|---|---|---|
| Plan Mode Detection | <10ms | <1ms | Pattern matching |
| Guidelines Generation | <20ms | ~15ms | Template + CLAUDE.md |
| Exploration Hints | <30ms | ~20ms | Entity extraction |
| Context Injection | <50ms | ~35ms | Combined injection |
| Guardrail Validation | <500ms | ~200ms | 5 rules |
| Auto-Revision | <200ms | ~50ms | Conflict checking |
| Plan QA | <50ms | ~30ms | Pattern matching |
| Total Overhead | <100ms | ~70ms | Excluding validation |
- LRU Caching: CLAUDE.md patterns cached with mtime invalidation
- Entity Cache: 128-entry cache for extracted entities
- Fast Rules First: Quick rules run before slow ones
- Early Exit: Stop on critical findings if configured
- Parallel Ready: Engine supports parallel rule execution (flag available)
Run the performance benchmark suite:
pytest tests/benchmarks/test_plan_mode_performance.py -vTests include:
- Detection latency (p95 target)
- Guidelines generation (p95 target)
- Exploration hints (p95 target)
- Full validation pipeline
- Memory usage (<50MB peak)
- Scalability (linear scaling verification)
- Hooks System - Hook configuration and lifecycle
- Memory Guard - Code quality enforcement
- CLI Reference - Command line tools
- Architecture - System design overview
Part of Claude Code Memory - Milestone 13.3