[Feature] apply_patch tool for atomic multi-file edits with a single preview
Description
Multi-file changes are applied as a series of independent tool calls (string_replace, diff_edit, write_file, one file per call; see source/tools/file-ops/). There is no way to submit one patch that changes, creates, renames and deletes several files together, reviewed once and applied all-or-nothing. A search of the code and of the issues finds no apply_patch tool.
Use Case
Renaming a function across eight files, moving a module, or adding a feature that touches code, tests and docs takes many calls with one confirmation each. If the model gets stopped halfway (cancel, error, context limit, a failed edit), the repository is left in a half-changed state that neither compiles nor matches the original. Codex CLI is built around a single patch format for this reason, and reviewers get one coherent diff to approve instead of a stream of fragments.
Proposed Solution
An apply_patch tool that takes a patch in a simple, model-friendly format (per file: add, update with hunks, delete, move) and:
- Validates every hunk against the current file contents and the read tracker before touching anything.
- Shows a single combined preview for confirmation, with per-file summary lines.
- Applies all changes or none: stage the new contents, then replace files in sequence with the existing atomic write helper, and roll back already-replaced files if a later step fails.
- Records one checkpoint covering the whole patch so
/checkpointcan undo it in a single step. - Registered in the same mode policy as other file tools, including plan mode (preview only) and architect mode.
Alternatives Considered
- Keep per-file tools and rely on checkpoints to undo. Recovery works, but the user still approves many small edits and partial states are visible in the meantime.
- Ask the model to emit a unified diff through bash and
git apply. Bypasses the file tool validators, read tracker and previews.
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] This feature aligns with the project's goals (local-first AI assistance)
Related but different: #2172 (edit tools write in place) is about single-file atomicity; this is about atomicity across files.
Implementation Notes (optional)
source/utils/fs/atomic-write.ts and the edit recovery logic in source/tools/file-ops/edit-recovery.ts supply most of the primitives. The patch parser should be small and strict, with exact-match context lines and a clear error naming the failing hunk so the model can retry.
I'd like to take this one, @akramcodez
@akramcodez this one?
Assigned @yunuschoudhurywork-lang, go ahead.
@Ember452 you asked first, but you already have #2277 and #2195 assigned (with #2207 open), so I'm giving this one to @yunuschoudhurywork-lang to keep the work spread out. Once those are done, feel free to pick up another issue.
Got it, thanks for the clarification. I'll finish #2277 first, then pick up another issue.