apply_diff
The apply_diff tool performs precise modifications to files by specifying the exact content to replace. It employs multiple sophisticated strategies to locate and apply changes while maintaining proper code formatting and structure.
Parameters
The tool accepts the following parameters:
path(required): The path of the file to be modified relative to the current working directory.diff(required): The search/replace block defining the changes. All currently implemented strategies require line numbers to be included in the diff content format.
Note: While the system is designed to support extending different diff strategies, all currently implemented strategies require line numbers to be specified in the diff content itself using the :start_line: marker.
Function Overview
This tool uses sophisticated strategies to make targeted changes to existing files, precisely locating and replacing content. Unlike simple search and replace, it employs intelligent matching algorithms (including fuzzy matching) that can adapt to different content types and file sizes, with fallback mechanisms for complex edits.
Application Scenarios
- When VJSP needs to make precise changes to existing code without rewriting entire files.
- When refactoring specific parts of code while maintaining surrounding context.
- When fixing errors in existing code with surgical precision.
- When implementing feature enhancements that modify only certain parts of a file.
Core Features
- Adopts intelligent fuzzy matching with configurable confidence thresholds (typically 0.8-1.0).
- Uses
BUFFER_LINES(default value 40) to provide matching context. - Implements an overlapping window search method for large files.
- Automatically preserves code formatting and indentation.
- Combines overlapping matches to improve confidence scores.
- Displays changes in a diff view for user review and editing before applying.
- Tracks consecutive errors per file (
consecutiveMistakeCountForApplyDiff) to prevent repeated failures. - Validates file access based on
.VJSPcodeignorerules. - Effectively handles multi-line edits.
Limitations
- Works best for unique, highly recognizable code sections.
- Performance may vary for extremely large files or highly repetitive code patterns.
- Fuzzy matching may occasionally select incorrect positions if content is ambiguous.
- Each diff strategy has specific format requirements.
- Complex edits may require careful strategy selection or manual review.
Working Mechanism
When the apply_diff tool is called, it follows this process:
- Parameter Validation: Validates the required
pathanddiffparameters. - VJSPCodeIgnore Check: Verifies if the target file path complies with
.VJSPcodeignorerules. - File Analysis: Loads the content of the target file.
- Match Localization: Uses the selected strategy's algorithms (exact matching, fuzzy matching, overlapping windows) to locate target content, considering confidence thresholds and context (
BUFFER_LINES). - Change Preparation: Generates proposed changes while preserving original indentation.
- User Interaction:
- Displays changes in a diff view.
- Allows users to review and possibly edit proposed changes.
- Waits for user approval or rejection.
- Change Application: If approved, applies changes (potentially including user edits) to the file.
- Error Handling: If errors occur (such as matching failures, partial application), increments the file's
consecutiveMistakeCountForApplyDiffand reports the failure type. - Feedback Return: Returns results, including any user feedback or error details.
Diff Strategies
VJSP employs the following strategies for applying diffs:
MultiSearchReplaceDiffStrategy
An enhanced search/replace format that supports executing multiple changes in a single request. Requires line numbers to be provided for each search block.
- Optimal Use Case: When multiple distinct changes are needed and line numbers are known or can be estimated.
- Requirements: Exact match for
SEARCHblock content, including spaces and indentation. Line numbers (:start_line:,:end_line:) must be provided. Markers within content must be escaped (\).
Example format for the <diff> block:
<<<<<<< SEARCH
:start_line:10
:end_line:12
-------
// Old calculation logic
const result = value * 0.9;
return result;
=======
// Updated calculation logic with logging
console.log(`Calculating for value: ${value}`);
const result = value * 0.95; // Adjusted factor
return result;
>>>>>>> REPLACE
<<<<<<< SEARCH
:start_line:25
:end_line:25
-------
const defaultTimeout = 5000;
=======
const defaultTimeout = 10000; // Increased timeout
>>>>>>> REPLACE