packages/kilo-docs/pages/automate/tools/apply-diff.md
The apply_diff tool makes precise, surgical changes to files by specifying exactly what content to replace. It uses multiple sophisticated strategies for finding and applying changes while maintaining proper code formatting and structure.
The tool accepts these parameters:
path (required): The path of the file to modify relative to the current working directory.diff (required): The search/replace block defining the changes. Line numbers are mandatory within the diff content format for all currently implemented strategies.Note: While the system is designed to be extensible with different diff strategies, all currently implemented strategies require line numbers to be specified within the diff content itself using the :start_line: marker.
This tool applies targeted changes to existing files using sophisticated strategies to locate and replace content precisely. Unlike simple search and replace, it uses intelligent matching algorithms (including fuzzy matching) that adapt to different content types and file sizes, with fallback mechanisms for complex edits.
BUFFER_LINES (default 40).consecutiveMistakeCountForApplyDiff) to prevent repeated failures..kilocodeignore rules.When the apply_diff tool is invoked, it follows this process:
path and diff parameters..kilocodeignore rules.BUFFER_LINES).consecutiveMistakeCountForApplyDiff for the file and reports the failure type.Kilo Code uses this strategy for applying diffs:
An enhanced search/replace format supporting multiple changes in one request. Line numbers are mandatory for each search block.
SEARCH block content, including whitespace and indentation. The :start_line: marker is required within each SEARCH block. 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