apply_patch
Apply unified diffs, Codex patches, or Aider SEARCH/REPLACE blocks to edit files. Preview changes without writing, or write and handle stale context/conflicts.
Instructions
Apply a unified diff, a Codex *** Begin Patch document, or an Aider SEARCH/REPLACE / DiffFenced document. Default apply=true writes (same as CLI patch apply --apply). Set apply=false for check-only preview (CLI patch check; disk is unchanged). The diff parameter is the full unified diff text, a *** Begin Patch ... *** End Patch envelope (Add/Update/Delete/Move), or <<<<<<< SEARCH / ======= / >>>>>>> REPLACE blocks (dest path above ------- after SEARCH). SEARCH/REPLACE is unique by default (multi-match is ambiguous, no write); set replace_all=true to update every exact match. Empty-hunk +++ /dev/null (git deleted file mode, no hunks) unlinks. A hunked delete applies minus lines first; leftover bytes rewrite the file (preview --diff). Stale minus lines are ambiguous and the file is not removed; regenerate minus lines or use file.delete for path-only unlink. Use on_stale=merge for three-way merge on stale unified-diff context; allow_conflicts=true writes conflict markers. Never commit files containing conflict markers. Example: {"diff": "--- a/file.txt\n+++ b/file.txt\n@@ -1 +1 @@\n-old\n+new", "on_stale": "fail"}
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| diff | Yes | ||
| apply | No | When true (default), apply the patch. Set `apply=false` for check-only preview (CLI `patch check`; disk is unchanged). | |
| strict | No | Roll back all writes when format/validate lifecycle steps fail. | |
| on_stale | No | fail | |
| replace_all | No | SEARCH/REPLACE only: update every exact match. Default unique. | |
| allow_conflicts | No |