PatchProcessor Core Stable
Intelligent code patching with sophisticated matching algorithms. Apply modifications to source code even when it has minor variationsβideal for AI-assisted code modification workflows.
// Configure a custom FuzzyPatchMatcher
val processor = FuzzyPatchMatcher(
contextSize = 3,
levenshteinThresholdDivisor = 4,
minLineLengthForFuzzyMatch = 5,
enableFuzzyMatching = true,
enableSnippetPatching = true,
snippetMatchThreshold = 0.8
)
// Apply a patch
val result = processor.applyPatch(source, patch)
// Source (with minor variations)
fun calculateTotal(items: List<Item>) {
- var total = 0
+ var total = 0.0
var total = 0.0
for (item in items) {
- total += item.price
+ total += item.price * item.quantity
total += item.price * item.quantity
}
return total.roundToTwoDecimals()
+ return total.roundToTwoDecimals()
}
β Patch applied successfully
- 2 lines modified, 1 line added
- Fuzzy matched 3 context lines
Available Processors
Pre-configured processors optimized for different use cases and precision requirements.
π· Fuzzy (Default)
Balanced default for most languages. Uses a Levenshtein threshold divisor of 4 and 80% snippet match threshold.
PatchProcessors.Fuzzy
π Strict
Maximum precision. No fuzzy matching or snippet patching. Requires exact line matches with 5 lines of context.
PatchProcessors.Strict
π Lenient
Maximum flexibility for heavily modified codebases. Very lenient thresholds and minimal context requirements.
PatchProcessors.Lenient
π Python
Specialized for indentation-sensitive languages like Python and YAML. Preserves leading whitespace exactly.
PatchProcessors.Python
βοΈ Thermodynamic
Physics-based matching using DNA-binding principles. Calculates optimal alignment via binding energy.
PatchProcessors.Thermodynamic
π FullReplacement
Simple full-file replacement. Ideal for creating new files or complete rewrites where patching is unnecessary.
PatchProcessors.FullReplacement
10 Key Innovations
What makes Cognotik's patching system different from traditional diff/patch tools.
1. Bidirectional Line Linking
Lines know their neighbors, enabling context-aware matching and bidirectional traversal during the alignment phase.
2. Multi-Phase Matching
Unique line matching β Adjacent line propagation β Recursive subsequence linking. Adapts to code structure organically.
3. Adaptive Fuzzy Matching
Levenshtein distance with structural type checking and adaptive thresholds that scale with line length.
4. Snippet Patching
Handles AI-generated code blocks without explicit diff markers using a three-tier matching strategy.
5. Move Detection
Identifies relocated code blocks by detecting order inversions, representing them as clean delete + add operations.
6. Intelligent Context Management
Truncates large context blocks with ellipsis while preserving critical lines before and after changes.
7. No-op Annihilation
Cleans up redundant DELETE/ADD pairs where the content remains identical after processing.
8. Thermodynamic Alternative
Physics-based matching for specialized scenarios where traditional string matching fails.
9. Language-Specific Support
Dedicated processors for indentation-sensitive languages like Python and YAML.
10. Validation Integration
Seamlessly integrates with grammar validation to ensure patches don't introduce syntax errors.
// FuzzyPatchMatcher Configuration Options
val processor = FuzzyPatchMatcher(
contextSize = 3, // Context lines before/after changes
maxRecursionDepth = 100, // Max recursion in subsequence linking
levenshteinThresholdDivisor = 4, // Stricter = higher value
minLineLengthForFuzzyMatch = 5, // Min length for fuzzy matching
enableFuzzyMatching = true, // Enable Levenshtein matching
enableSnippetPatching = true, // Enable snippet application
snippetMatchThreshold = 0.8, // Min match % for snippets
requireAnchorMatch = true // Require first/last line match
)
// ThermodynamicPatchMatcher Configuration
val thermoProcessor = ThermodynamicPatchMatcher(
temperature = 1.0, // Matching stringency
cooperativityBonus = 2.0, // Bonus for adjacent matches
entropyPenalty = 1.0, // Energy cost per gap
contextSize = 3
)
// High-level API with SimpleDiffApplier
val applier = SimpleDiffApplier()
val result = applier.apply(
originalCode = sourceCode,
response = aiMarkdownResponse,
filename = "Service.kt",
processor = PatchProcessors.Fuzzy
)
if (result.isValid) {
println("Patched successfully: ${result.newCode}")
} else {
result.errors.forEach { println("Error: ${it.message}") }
}
// Basic Patch Generation
val processor = PatchProcessors.Fuzzy
val patch = processor.generatePatch(oldCode, newCode)
/* Output:
fun hello() {
- return 1
+ return 2
}
*/
Ready to Get Started?
PatchProcessor is part of the Cognotik core library. Explore the source code on GitHub or read the full technical documentation.