apply_code_patch
Apply a verified code patch to a file by replacing exact code blocks, with automatic syntax validation and instant rollback if checks fail.
Instructions
Applies an atomic, verified code edit to a specific file by substituting original_code with new_code, with automatic syntax validation and instant rollback on failure.
• Side Effects: Modifies the target file on the local filesystem. If syntax checks pass, the file is overwritten with patched contents; if syntax validation fails, the file is immediately restored to its exact original state (zero dirty diff). • Auth & Permissions: Requires write permissions for the target file on the host filesystem within the workspace boundary. Path traversal outside workspace root is blocked. • Rate Limits: None. Local disk I/O. • Return Shape: Returns a JSON object with 'status' ('success' or 'error'), 'file_path', 'lines_changed', 'verification' ('passed' or 'reverted'), and 'message'. • Failure Modes: Fails and aborts without touching the file if file_path is not found, if original_code does not match the file content uniquely, or if the compiler/linter check fails after patch application. • When to use: Use when you have finalized a bug fix or refactoring snippet and need safe, transactional application with zero risk of syntax corruption. • When NOT to use: Do NOT use for speculative edits without prior diagnosis (use get_error_context first), and do NOT use for whole-file generation when only a small block changes. • Prerequisites: Target file must exist and be within the current workspace directory.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dry_run | No | Optional. When true, validates that the patch matches uniquely and checks syntax without writing any changes to disk. Defaults to false. | |
| new_code | Yes | The replacement code block to substitute in place of original_code. Must maintain correct language syntax and indentation matching the surrounding code. | |
| file_path | Yes | Target file path to modify. Can be relative to the workspace root or an absolute path located inside the workspace boundary. Path traversal outside the workspace is rejected. | |
| original_code | Yes | The exact character-for-character contiguous code block to be replaced, including exact leading indentation, newlines, and whitespace. Must match exactly one location in the file. |