AI-Powered Auto-Fix: From Detection to Resolution
Beyond Detection
Finding code issues is only half the battle. The real value is in **fixing** them.
Traditional tools tell you "Function X has cyclomatic complexity of 15" but leave you to figure out the fix. With Repotoire's auto-fix system, we go from detection to resolution using GPT-4o and RAG (Retrieval-Augmented Generation).
How Auto-Fix Works
When you run `repotoire auto-fix`, here's what happens:
1. **Issue Detection**: Run all detectors to find problems
2. **Context Retrieval**: Use embeddings to find related code
3. **Fix Generation**: GPT-4o generates a fix with evidence
4. **Human Review**: You approve, modify, or reject the fix
5. **Application**: Apply approved fixes via clean diffs
The key insight is **evidence-based fixing**. Instead of generating fixes in a vacuum, we retrieve relevant code patterns from your codebase to inform the fix.
RAG in Action
Say we detect a "God Class" with 50+ methods. Before generating a fix, we search for similar patterns:
# Semantic search using embeddings
similar_classes = retriever.search(
query="well-structured class with single responsibility",
entity_type="Class",
top_k=5
)
This retrieves examples of well-designed classes from *your own codebase*. GPT-4o then uses these as templates for the refactoring suggestion.
Human-in-the-Loop
We believe AI-generated fixes should always be reviewed by humans. Auto-fix presents each change with:
- **Before/After diff**: See exactly what will change
- **Evidence**: The similar code patterns that informed the fix
- **Justification**: Why this fix addresses the issue
- **Confidence score**: How certain the AI is about the fix
You can:
- **Accept**: Apply the fix as-is
- **Modify**: Edit the fix before applying
- **Reject**: Skip this fix
- **Regenerate**: Ask for a different approach
Example Session
$ repotoire auto-fix /path/to/repo --severity high
Found 3 high-severity issues
[1/3] Complex function: calculate_metrics (complexity: 18)
File: src/analytics/metrics.py:45-120
Suggested fix:
- Extract 4 helper functions
- Reduce complexity from 18 to 6
- Preserve all behavior
Evidence: Similar refactoring in src/reports/generator.py
[a]ccept, [m]odify, [r]eject, [s]kip? a
Applied fix to src/analytics/metrics.py
Getting Started
# Install with auto-fix dependencies
pip install repotoire[autofix]
export OPENAI_API_KEY="sk-..."
# Generate embeddings for RAG
repotoire ingest /path/to/repo --generate-embeddings
# Run auto-fix
repotoire auto-fix /path/to/repo
For high-confidence fixes, you can enable auto-approve:
repotoire auto-fix /path/to/repo --auto-approve-high
This automatically applies fixes with >90% confidence, speeding up bulk refactoring while keeping you in control for uncertain cases.