ADR: Migrate patch to V4A apply_patch
Status: proposed
Context
The current patch tool accepts conventional unified diffs. Unified diffs are
familiar to developers, but modern coding models are trained more heavily on
the file-oriented V4A apply_patch envelope used by Codex and OpenCode.
Exposing both formats at once creates routing ambiguity and consumes tool
context.
The current implementation also applies multi-file patches sequentially. It is not transactional: a later failed hunk does not roll back files already changed. Its description now states this behavior.
Decision
Introduce one model-visible tool named apply_patch with the V4A grammar:
*** Begin Patch
*** Add File: path
+initial content
*** Update File: path
@@ optional context anchor
-old
+new
*** Delete File: path
*** End PatchSupported operations:
Add Filecreates one file from+lines.Update Fileapplies context-addressed hunks.Delete Fileremoves one file.Move torenames an updated file.- One envelope may contain several file operations.
Paths are workspace-relative by default and use the same path policy as other filesystem tools. Snapshot recording remains per file.
Compatibility plan
- Implement the V4A parser and applier in
internal/tool/tools/applypatch. - Add parser fixtures for add, update, delete, move, multiple hunks, malformed envelopes, ambiguous context, and end-of-file anchors.
- Add behavioral tests for snapshots, workspace-relative paths, permissions, and partial multi-file failure.
- Register
apply_patchas the only default patching tool. - Keep the current
patchpackage as an opt-in compatibility tool for one release. Do not register both by default. - Translate configuration allowlists containing
patchto a deprecation warning with the documentedapply_patchreplacement. - Remove the unified-diff compatibility registration in the following release.
Atomicity
The first V4A release preserves current sequential semantics and reports which file operations completed before a failure. Transactional multi-file rollback is deferred until the snapshot manager exposes a batch transaction API.
The model-visible description must not call a multi-file envelope atomic before that API exists.
Rejected alternatives
- Keep unified diff permanently: lower implementation cost, but misses the model-trained format and its explicit file operations.
- Register both tools by default: maximizes compatibility but recreates the
routing problem removed with
lsandlist. - Translate V4A into textual unified diff before parsing: loses context-anchor semantics and creates two parser failure surfaces.
Acceptance criteria
- One default model-visible mutation tool handles add, update, delete, and move.
- The description includes the complete supported grammar and one minimal example.
- Malformed input fails before modifying any file.
- Runtime results identify every completed operation and any partial failure.
- Existing snapshot and permission guarantees remain covered by tests.
- Agent prompts and documentation mention only
apply_patchafter migration.