Why Do AI Coding Agents Use Diff and JSON Patch for File Edits? 2026 From V4A and search_replace to RFC 6902 Tool Calls
A coding agent does not write disk directly. The model returns JSON inside a function_call or apply_patch_call; the harness validates, then the host writes files. Source uses text diffs; structured JSON uses add / replace with paths.
Before you read
| Mechanism | Public practice in 2026 | Who executes |
|---|---|---|
| Source files | OpenRouter documents apply_patch with V4A diffs; many IDE agents use search_replace on a text buffer | The host parses the diff or exact match, then writes |
| JSON documents | RFC 6902 JSON Patch; EMNLP 2025 JSON Whisperer reports roughly 31% fewer tokens than full regeneration | A library applies by path without escaped search strings |
| Agent state | session, pending_edits, order_id live in JSON state | Small replace steps beat rewriting the whole state each turn |
| MCP tools | Community json-patch-mcp exposes apply_json_patch_to_file | The model fills a patch array; the MCP host writes the absolute path |
A-1042, sess_demo_7f2a, and demo-harness are fictional. OpenRouter apply_patch is a Responses API capability. superglue’s January 2026 post describes moving config editing from search_replace to JSON Patch.
JSON Patch matters because agents need verifiable deltas, not another full document
In 2026, coding agents still edit code with text-level patches: unified diff, V4A, or search_replace with old_string and new_string. OpenRouter’s Responses API offers openrouter:apply_patch: the model proposes a diff, the platform checks syntax, and your app still applies it to disk. When the target is JSON—tool config, agent state, plugin manifest—text search breaks on escaping and newlines. superglue’s engineering post in January 2026 states plainly: for JSON with a fixed schema, RFC 6902 path operations are more deterministic.
- Text diff: fits .ts, .py, Markdown; whitespace and quotes must match byte for byte.
- JSON Patch: fits settings.json, session state, OpenAPI fragments; paths must match the live tree.
- Full rewrite: easy to implement, costly in tokens, and easy to drop sibling fields; JSON Whisperer pushes cost down with patches.
How state is stored and who may write files: harness and JSON state. Shell, Git, and FS boundaries: coding agent permissions. This piece only covers how patch JSON from the model becomes file changes.
State, search_replace, JSON Patch, and V4A are four layers
Harness JSON state holds context first. The model reads active_file and order_id from state, not from guesswork:
{
"session_id": "sess_demo_7f2a",
"turn": 3,
"workspace": "/workspace/demo-app",
"active_file": "config/settings.json",
"order_id": "A-1042",
"pending_edits": []
}
Configs still treated as text often expose search_replace. arguments is a stringified JSON object with old_string and new_string—each escape layer is another failure mode:
{
"type": "function_call",
"id": "call_edit_91c2",
"name": "search_replace",
"arguments": "{\"path\":\"config/settings.json\",\"old_string\":\"\\\"timeout\\\": 30\",\"new_string\":\"\\\"timeout\\\": 60\"}"
}
When the host parses the file as a JSON tree, the better tool output is an RFC 6902 array. Each op carries op, path, and optional value:
[
{ "op": "replace", "path": "/settings/timeout", "value": 60 },
{ "op": "add", "path": "/metadata/last_agent", "value": "demo-harness" }
]
For TypeScript source, the Responses shape looks like apply_patch_call: operation is create, update, or delete, and diff holds V4A line changes. After platform validation, your harness still checks policy before write:
{
"type": "apply_patch_call",
"call_id": "patch_4e1a",
"status": "completed",
"operation": {
"type": "update_file",
"path": "src/handler.ts",
"diff": "@@\\n- const timeout = 30;\\n+ const timeout = 60;\\n"
}
}
One task often uses both patch styles
Do not assume the agent exposes only one edit tool:
| Situation | Better patch |
|---|---|
| Polyglot source | V4A, unified diff, or search_replace; Git diff is for humans, tools need machine apply |
| Tool config JSON | JSON Patch replace/add; schema validation can confirm paths exist |
| Session state | Patch small objects; split large blobs so one key is not a giant string |
| Review and rollback | JSON Diff for structure; text diff for lines; Patch is a replayable change list |
JSON Patch does not replace Git: it describes operations on one JSON document, not a commit graph. Agents often diff before and after apply for human or policy approval.
Six checks before handoff to the writer
Wrong order treats a valid patch as permission to touch production:
- Classify the target. If it is .json and the host parses it, prefer Patch; source files use text tools.
- Validate arguments JSON. Parse the search_replace string, then confirm old_string matches uniquely.
- Dry-run Patch. Apply RFC 6902 on an in-memory copy; on failure return the test error to the model, never half-apply.
- Check policy. Path inside workspace, read-only flags—tool allowlists, not prompt hope.
- Update state after apply. Clear pending_edits or bump a version so the next turn does not patch the same path twice.
- Keep an audit copy. Format before/after for logs with redaction; sample keys stay sk_demo_not_real.
MCP json-patch tools require absolute paths so you can reconcile the model’s relative path with the host root.
Spread three JSON kinds before approve
Failures are usually stale old_string, drifted Patch paths, or treating diff validation as authorization.
- Use format on tool arguments and confirm a parsed object, not a broken double-escaped string.
- On JSON documents use JSON Diff for structure, then JSON Patch to try apply or build a reverse patch.
- Use JSONPath on state for
$.active_fileand$.order_idand match the patch target. - Before write, read FS and Git rules in coding agent permissions; apply_patch only means the diff syntax passed.
- JSONSort runs locally in the browser; do not paste real repo paths or secrets into public pages.
When the three line up, the harness is executing your edit protocol, not the model’s full-file improvisation.
Common mistakes
- search_replace on JSON with nested escaped strings. Function bodies inside strings never match; parse or switch to Patch.
- JSON Patch on source code. Parsing .ts as JSON fails; text diff is the right abstraction.
- Ignoring array index shift. Sequential remove moves indexes; JSON Whisperer’s EASE uses stable keys instead of raw indexes.
- Treating platform apply_patch as auto-deploy. OpenRouter docs: syntax check only; your app writes files—human in the loop.
- Patch succeeded but state is stale. The model still thinks timeout is 30 and repeats or misses the path.
- Mixing JSON Diff with Patch. Diff is for review; Patch is for execution. Tool pages split the same way.
FAQ
Should a coding agent use JSON Patch on .py files?
Usually no. Python is text; return line diffs or search_replace. JSON Patch targets a JSON value model; forcing it breaks syntax.
Is JSON Patch the same as JSON Merge Patch?
No. RFC 6902 is an array of operations. Merge Patch (RFC 7396) merges objects. Tool schemas should name which one you accept.
Why does JSON Whisperer push patches over rewrite?
The paper reports about 31% fewer tokens at similar quality and steadier list edits. Long session state also benefits from deltas.
Who validates first—MCP json-patch or the IDE?
The host always: legal path, applicable patch, allowed permission. MCP standardizes the tool name; it does not bypass sandbox.
Can Git diff be JSON Patch?
No. Git diff is a line text format; JSON Patch is operations on JSON Pointer paths. Agents may Patch first, then let humans commit with Git.
Conclusion
Coding agents need JSON Patch not because every file is JSON, but because structured state and config need a delta format cheaper than full rewrite and steadier than text search. Source stays on V4A and search_replace; JSON stays on RFC 6902; the harness unifies validation, policy, and write.
When you see replace in tool output, ask first: text buffer or JSON tree? Answer that, then pick Diff for review or Patch to try apply.
Further reading
- Agent harness and JSON state Where session and pending_edits live
- Coding agent permissions FS and Git policy before write
- JSON Patch tool (RFC 6902) Try add, replace, remove locally
- JSON Diff Review structure, not execution
- All posts More on JSON and AI development
Changelog: First published