Skip to content
ADR: Migrate patch to V4A apply_patch

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 Patch

Supported operations:

  • Add File creates one file from + lines.
  • Update File applies context-addressed hunks.
  • Delete File removes one file.
  • Move to renames 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

  1. Implement the V4A parser and applier in internal/tool/tools/applypatch.
  2. Add parser fixtures for add, update, delete, move, multiple hunks, malformed envelopes, ambiguous context, and end-of-file anchors.
  3. Add behavioral tests for snapshots, workspace-relative paths, permissions, and partial multi-file failure.
  4. Register apply_patch as the only default patching tool.
  5. Keep the current patch package as an opt-in compatibility tool for one release. Do not register both by default.
  6. Translate configuration allowlists containing patch to a deprecation warning with the documented apply_patch replacement.
  7. 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 ls and list.
  • 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_patch after migration.