Skip to main content

Overview

Refactron’s Pattern Learning System learns from your refactoring decisions, building a knowledge base that improves the quality and relevance of future recommendations. The more you use Refactron, the smarter it gets!

How It Works

1

Pattern Fingerprinting

When Refactron suggests a refactoring, it creates a unique “fingerprint” of the code pattern
2

Feedback Collection

You provide feedback: accepted, rejected, or ignored
3

Pattern Learning

Refactron tracks:
  • Acceptance rates for each pattern
  • Pattern frequency and recency
  • Code metrics improvements
  • Project-specific preferences
4

Smart Ranking

Future suggestions ranked based on historical acceptance rates and patterns

Key Features

Automatic Learning

Learns from every refactoring decision you make

Smart Ranking

Ranks suggestions by historical acceptance patterns

Project-Specific

Adapts to your project’s unique coding style

Persistent Storage

Patterns persist across sessions

Configuration

Enable Pattern Learning

Pattern learning is enabled by default. Configure in .refactron.yaml:
.refactron.yaml
# Pattern learning settings
enable_pattern_learning: true          # Master switch
pattern_learning_enabled: true         # Learn from feedback
pattern_ranking_enabled: true          # Rank by patterns
pattern_storage_dir: null              # null = auto-detect

Python API

from refactron import Refactron
from refactron.core.config import RefactronConfig

# Enable all pattern learning features
config = RefactronConfig(
    enable_pattern_learning=True,
    pattern_learning_enabled=True,
    pattern_ranking_enabled=True
)
refactron = Refactron(config)

# Disable pattern learning
config = RefactronConfig(enable_pattern_learning=False)
refactron = Refactron(config)

Providing Feedback

Interactive Feedback

# Refactor with feedback prompts
refactron refactor myfile.py --preview --feedback
During preview, you’ll be prompted to accept/reject each suggestion.

Automatic Feedback

Feedback is automatically recorded when you apply refactorings:
# Accepted suggestions are automatically recorded
refactron refactor myfile.py --apply

Manual Feedback

Provide explicit feedback for specific operations:
# Accept a suggestion
refactron feedback <operation-id> --action accepted

# Reject with reason
refactron feedback <operation-id> --action rejected --reason "Breaks API"

# Mark as ignored
refactron feedback <operation-id> --action ignored

Python API Feedback

from refactron import Refactron

refactron = Refactron()
result = refactron.refactor("myfile.py", preview=True)

# Record feedback for first operation
if result.operations:
    op = result.operations[0]
    refactron.record_feedback(
        operation_id=op.operation_id,
        action="accepted",
        reason="Improved readability",
        operation=op
    )

Pattern Analysis

View Pattern Statistics

# Analyze project patterns
refactron patterns analyze
Output shows:
  • Most accepted patterns
  • Patterns with low acceptance
  • Recommendations for tuning

Get Tuning Recommendations

# Get automated recommendations
refactron patterns recommend

Apply Tuning

# Auto-apply recommended tuning
refactron patterns tune --auto

# View current pattern profile
refactron patterns profile

Ranking in Action

When pattern learning is enabled, suggestions show ranking scores:
refactron refactor myfile.py --preview
Output:
📊 Refactoring Suggestions (ranked by learned patterns)

Operation 1: Extract Constant
  File: myfile.py:42
  Ranking Score: 0.85 ⭐  # High acceptance history
  Risk: Low (0.2)
  Description: Extract magic number 42 to constant

Operation 2: Simplify Conditional
  File: myfile.py:67
  Ranking Score: 0.35      # Lower acceptance history
  Risk: Moderate (0.4)
  Description: Simplify nested if statements

Storage Location

Pattern data is stored in JSON files: Default Locations:
  • Project root: .refactron/patterns/
  • User home: ~/.refactron/patterns/ (fallback)
Storage Files:
  • patterns.json - Learned patterns
  • feedback.json - Feedback records
  • project_profiles.json - Project configs
  • pattern_metrics.json - Pattern metrics
Custom Location:
.refactron.yaml
pattern_storage_dir: /custom/path/patterns

Configuration Options

enable_pattern_learning
boolean
default:"true"
Master switch for all pattern learning features
pattern_learning_enabled
boolean
default:"true"
Enable learning from feedback (requires enable_pattern_learning)
pattern_ranking_enabled
boolean
default:"true"
Enable ranking based on learned patterns (requires enable_pattern_learning)
pattern_storage_dir
string
default:"null"
Custom storage directory (null = auto-detect project root or home directory)

Best Practices

The more feedback you provide, the better the system learns:
  • Accept good refactorings
  • Reject inappropriate suggestions
  • Ignore if unsure
For large projects, use automated tuning:
refactron patterns analyze
refactron patterns tune --auto
Periodically check pattern performance:
refactron patterns analyze
In CI/CD environments, use consistent storage:
pattern_storage_dir: /ci/patterns

Advanced Usage

Custom Pattern Weights

from refactron.patterns.storage import PatternStorage
from pathlib import Path

storage = PatternStorage()
profile = storage.get_project_profile(project_path=Path("/project"))

# Set custom weight for a pattern
profile.set_pattern_weight("pattern-id-123", 0.9)

# Save profile
storage.save_project_profile(profile)

Batch Learning

from refactron.patterns.learner import PatternLearner

learner = PatternLearner(storage=storage, fingerprinter=fingerprinter)

# Load pending feedback
feedback_list = storage.load_feedback()

# Process in batch
learner.batch_learn(feedback_list)

Pattern Cleanup

from refactron.patterns.learning_service import LearningService

service = LearningService(storage=storage)

# Remove patterns older than 90 days
service.cleanup_old_patterns(days=90)

Integration with AI

Pattern learning integrates with AI Features:
  • AI suggestions use learned patterns for better context
  • Feedback on AI refactorings improves future AI suggestions
  • RAG system considers pattern preferences

Troubleshooting

Solutions:
  1. Check pattern_learning_enabled is true
  2. Verify storage directory is writable
  3. Check logs: refactron refactor --log-level DEBUG
Solutions:
  1. Check pattern_ranking_enabled is true
  2. Ensure patterns have been learned (provide feedback first)
  3. Verify sufficient pattern history exists
Solutions:
  1. Check directory permissions
  2. Use custom pattern_storage_dir if needed
  3. Verify disk space available

Performance

  • Memory: In-memory caching with invalidation on updates
  • Storage: ~100-200 KB for typical project (100-1000 patterns)
  • Learning Speed: <100ms per feedback record
  • Ranking Speed: ~10ms per operation

Next Steps