ANTIGRAVITY LABJP
Articles/Antigravity Basics
Antigravity Basics/2026-06-03Intermediate

Why Antigravity Agent Edits Fail With 'patch does not apply' and How to Fix It

Why Antigravity agent edits stall with 'patch does not apply' or 'hunk failed', and how to fix it. Focused on the race where the file changes after the agent reads it, with the settings that stop it from recurring.

Antigravity338AI Agents14patchdiff3Troubleshooting6

You ask the Antigravity agent to fix a function, it reasons through the change just fine, and then the edit dies on a single line: patch does not apply or hunk #2 FAILED. The logic the agent produced is correct, but the final write to disk misses. This started happening to me regularly once I began running several agents in parallel across my apps.

Here is the conclusion first: in almost every case, the code the agent wants to write is not wrong. The problem is that the file changed between the moment the agent read it and the moment it tried to apply the diff. This is a timing and state issue, not a code issue. Miss that distinction and you will rewrite the prompt forever without fixing anything.

Read which hunk actually failed

A patch does not apply error comes from git apply (or an equivalent patcher) being unable to match the diff's context lines against the real file. The agent anchors its diff on a few lines around the edit. If even one character of those anchor lines differs in the actual file, the match fails.

The first thing to check is the hunk number in the error. If you see hunk #1 succeeded but hunk #2 FAILED, the front of the file applied and the back drifted. That tells you the whole file is not stale — something touched one specific region — which narrows the cause immediately.

Why an unappliable patch appears in the first place

In my setup the causes fell into three buckets.

The first is a conflict with format-on-save. Prettier, ESLint --fix, or your editor's format-on-save rewrites indentation or quotes right after the agent read the file, so the anchor lines no longer match. The classic case: the agent built its diff assuming 'single' quotes, but the save hook converted them to "double".

The second is concurrent edits from multiple agents. I keep several wallpaper and wellness apps running, and I would occasionally point two sub-agents at the same utility file. If one rewrites the file and the other then applies a diff built against the older version, it fails by definition.

The third is mismatched line endings. In a repo where CRLF and LF are mixed, the agent may build its diff assuming LF while the file is CRLF, so every line reads as "changed" and the whole patch dies. This creeps in silently when you develop across Windows and macOS.

Fix 1: make the agent re-read the latest state

The fastest fix is to have the agent read the target again before editing. Name the failed file explicitly and ask again.

Before editing src/utils/format.ts, read the whole file again,
then rebuild the diff against the current contents.
The previous diff was based on a stale snapshot and failed to apply.

This realigns the anchor lines with the file as it is now, and it usually applies. The state was simply out of sync, so no rethinking of the code is needed.

Fix 2: pin the order between formatter and agent edits

To stop recurrence, keep the save hook from cutting in on the agent's edit. I write the formatter policy into AGENTS.md so the agent's output matches the existing style from the start.

# AGENTS.md (excerpt)
 
## Code style
- Formatter is Prettier; follow .prettierrc.
- Single quotes, semicolons on, two-space indentation.
- Before building a diff, check the file's current style and match it.

Pinning the formatter config in the repo also keeps the agent's output and the post-save state identical, which avoids anchor drift.

// .prettierrc
{
  "singleQuote": true,
  "semi": true,
  "tabWidth": 2
}

Fix 3: normalize line endings at the repo level

The line-ending wipeout disappears at the root once you normalize with .gitattributes. Drop this at the repo root to force LF for text files.

# .gitattributes
* text=auto eol=lf

If CRLF is already mixed in, renormalize the whole tree once after adding the setting.

git add --renormalize .
git commit -m "Normalize line endings to LF"

Now the LF the agent assumes and the real file agree, and the "every line changed" verdict from a newline mismatch stops happening.

A small practice to keep parallel edits from colliding

When you run multiple agents, splitting ownership by file is the most reliable guard. Running the four Dolice Labs sites and several apps at once, one simple rule — never let two agents touch the same file simultaneously — removed most of my apply failures. If they must touch the same area, serialize: let one edit finish and commit before the next starts. Failures you can prevent structurally are steadier to remove through workflow than through prompt tweaks.

I hope this shortens the diagnosis for anyone losing time to the same symptom.

Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

If you found this article helpful, a small tip ($1.50) would mean a lot to us. Your support helps keep this site ad-free and covers server and hosting costs.

Related Articles

Antigravity2026-07-01
When Your MCP Servers Vanish From the Chat App in Antigravity 2.0
After Antigravity 2.0 split into an IDE and a separate chat-style agent app, an MCP server I had set up in the IDE stopped showing up on the chat side. Here is why the settings scopes are separate, and how to fix it by making a single workspace-level source of truth that both apps read.
Antigravity2026-05-18
When Antigravity Ignores Your AGENTS.md — How to Diagnose and Fix It
You dropped an AGENTS.md at the repo root with clear rules, but the agent still pulls in banned libraries and blows past your conventions. Here is how I diagnose and fix the three most common causes I have seen across more than a hundred Antigravity sessions.
Antigravity2026-04-30
An Observability Blueprint for Antigravity Agents in Production
A definitive guide to designing observability for AI agents running on Antigravity in production. Presents a practical framework that unifies traces, metrics, and outcome logs.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →