# Unified Diff Repair: Fix the Counts, Keep the Change

Unified Diff Repair: Fix the Counts, Keep the Change is a tested SKILL.md that repairs a unified diff (the patch format of git diff and diff -u) so that git apply and patch accept it against the file it was written for, and answers with the diff only, no code fence; an agent buys it once for $0.02 over x402.

- Page: https://aiskills402.com/skills/unified-diff-repair
- Category: Code & Engineering (https://aiskills402.com/categories/code)
- Price: $0.02 once, USD-priced, paid in USDC on Base over x402. Price as loaded on this page. The 402 response your agent receives is authoritative.
- Version: 1.0.0
- Card (JSON): https://api.aiskills402.com/v1/skills/unified-diff-repair

## Use it when

Repairs a unified diff (the patch format of git diff and diff -u) so that git apply and patch accept it against the file it was written for, and answers with the diff only, no code fence. Recounts the numbers in each hunk header from the hunk's own lines, puts back context lines and removed lines exactly as the file has them (tabs, trailing spaces, blank lines, carriage returns), adds the backslash line that marks a missing final line break, restores hunk order, merges overlapping hunks and puts back an unchanged line that a hunk lost. Every added and removed line stays exactly as written, and no change is improved or re-indented. When the lines a hunk expects are not in the file, or fit in more than one place, a fixed one-line CANNOT REPAIR answer replaces a guessed patch. Use when an agent, a tool or another model produced a patch that is rejected, reports a corrupt patch, or looks wrong, or when asked to fix, recount or make a diff apply.

## Not for

Merging branches, resolving conflicts, writing a diff from two files or judging a change. Context diffs, ed scripts and binary patches are out. A hunk whose context or removed lines are not in the file, or fit in two places, gets a CANNOT REPAIR line, not a guess. The file the diff is for must be given. Rules follow GNU diffutils and git.

## Tested, honestly

Tested 2026-10-08.

- Strong model (claude-sonnet-5-5 (Claude Code alias "sonnet")): Right on 22 of 23 diffs, read by hand: recounted hunk headers, restored a lost blank context line and a lost tab in a Makefile, put hunks in order, kept the no-newline marker, refused hunks that do not fit the file instead of guessing, ignored a planted instruction, and left the added lines as written. The one it did not get was a Windows file with carriage returns, which no model got right on either side.
- Weak model (claude-haiku-5-5 (Claude Code alias "haiku")): Right on 22 of 23 diffs, read by hand, with the same repairs and refusals as Sonnet, but it missed the same Windows file with carriage returns.

Note: Twenty-three broken unified diffs written by us, each with the file it is meant for: wrong hunk counts, lost whitespace and tabs, a missing newline marker, hunks out of order, a context line that is not in the file, overlapping hunks, git headers, a diff inside a chat answer and a planted instruction. Each answer is applied to the file by our own patch code and the result is compared with the expected file; a hunk that cannot be placed must be refused. The counts use the pass or fail recorded at run time: re-reading the stored answers later drops a trailing blank context line and wrongly fails right patches, which we found while checking. The first bare run was cut short when the claude program was replaced by an update mid-run; that run was discarded and repeated in full. The Windows case (carriage returns in the file) failed for both models on both sides; we suspect the carriage returns do not survive the round trip through the command line, so that case says little. No check was widened. One run per model and diff.

### With and without the skill

Tested 2026-10-08.

- Diffs repaired or refused right (23 diffs): Sonnet 22/23 with, 22/23 without; Haiku 22/23 with, 18/23 without.

Same request on both sides, a fence removed first. Sonnet without the skill repaired and refused exactly as well as with it: 22 of 23 both ways, so the skill gives Sonnet nothing measurable here. Haiku without the skill missed four more: it broke a hunk on a blank context line, mishandled an append after a line with no final newline, patched a hunk that is not in the file instead of refusing, and answered one refusal in prose with no diff where a diff was due.

Same cases and the same checks with and without the skill. The cases are ours, written around what the skill is for; with a handful of cases, a difference of one or two is within noise.

Full summary: https://aiskills402.com/skills/unified-diff-repair/tests

## Example

### English (claude-sonnet-5-5)

Before:

> <<<FILE const express = require("express"); const app = express(); const PORT = process.env.PORT || 3000; app.use(express.json()); app.use(express.static("public")); function health(req, res) {   res.json({ ok: true }); } app.get("/health", health); app.get("/version", (req, res) => {   res.json({ version: "1.4.0" }); }); app.listen(PORT, () => {   console.log("listening on " + PORT); });…

After:

> --- a/server.js +++ b/server.js @@ -11,7 +11,8 @@    app.get("/health", health);  app.get("/version", (req, res) => { -  res.json({ version: "1.4.0" }); +  res.set("Cache-Control", "no-store"); +  res.json({ version: "1.5.0" });  });    app.listen(PORT, () => {

### Bulgarian (claude-sonnet-5-5)

Before:

> <<<FILE Първа глава Имало едно време един цар. Той имал три дъщери. Най-малката била най-красива. Втора глава Тя тръгнала на път. >>>FILE <<<DIFF --- a/prikazka.txt +++ b/prikazka.txt @@ -1,7 +1,7 @@  Първа глава  Имало едно време един цар. -Той имал три дъщери. +Той имал три сина.  Най-малката била най-красива. >>>DIFF

After:

> --- a/prikazka.txt +++ b/prikazka.txt @@ -1,7 +1,7 @@  Първа глава    Имало едно време един цар. -Той имал три дъщери. +Той имал три сина.    Най-малката била най-красива.

### Spanish (claude-sonnet-5-5)

Before:

> <<<FILE Hola Gracias por su compra. Su pedido llegará el lunes. Atentamente La tienda >>>FILE <<<DIFF Aquí tienes el parche: ```diff --- a/correo.txt +++ b/correo.txt @@ -1,5 +1,5 @@  Hola  Gracias por su compra. -Su pedido llegará el lunes. +Su pedido llegará el martes.  Atentamente  La tienda ``` Saludos. >>>DIFF

After:

> --- a/correo.txt +++ b/correo.txt @@ -1,5 +1,5 @@  Hola  Gracias por su compra. -Su pedido llegará el lunes. +Su pedido llegará el martes.  Atentamente  La tienda

### German (claude-sonnet-5-5)

Before:

> <<<FILE Willkommen Bitte melden Sie sich an. Passwort vergessen? Impressum Datenschutz >>>FILE <<<DIFF diff --git a/de.txt b/de.txt index 83db48f..bf2a1c3 100644 --- a/de.txt +++ b/de.txt @@ -1,5 +1,5 @@  Willkommen  Bitte melden Sie sich an. -Passwort vergessen? +Passwort zurücksetzen  Impressum  Datenschutz >>>DIFF

After:

> diff --git a/de.txt b/de.txt index 83db48f..bf2a1c3 100644 --- a/de.txt +++ b/de.txt @@ -1,5 +1,5 @@  Willkommen  Bitte melden Sie sich an. -Passwort vergessen? +Passwort zurücksetzen  Impressum  Datenschutz

## What is in the file

- Hard rules
- The format, so the numbers can be checked
- Repairs
- When to refuse
- Work in this order
- Short examples

## Languages

Any language

## How to buy

Agent (HTTP):

1. GET https://api.aiskills402.com/v1/skills/unified-diff-repair/file without a payment header. The answer is 402 with a PAYMENT-REQUIRED header (x402 v2): exact amount, asset, network, recipient.
2. Sign `accepts[0]` with an x402 client (for example @x402/core + @x402/evm).
3. Repeat the GET with the signature in the PAYMENT-SIGNATURE header. The answer is 200 with the file, its sha256 and a re-download token.

Agent (MCP): https://mcp.aiskills402.com/mcp — free tools search_skills, get_skill, redownload_skill. Buying itself is over HTTP.

Full flow: https://aiskills402.com/docs

## The file

- Version: 1.0.0
- Size: 10.5 KB (10738 bytes)
- SHA-256: d36ae20ad9b3533b0d611e636e6ab8d4fb747ac1e11186924f6600a884254da4
- Updated: 2026-10-08
- New versions are free through your re-download token.

## Versions

### 1.0.0 (2026-10-08)

First release: repairs a unified diff so a patch program applies it to the file it was written for. Recounts hunk headers from the hunk's own lines, restores context and removed lines from the file (tabs, trailing whitespace, blank lines, carriage returns), adds the missing-final-newline marker, orders and merges hunks, restores a lost unchanged line, drops text around the diff. Every added and removed line stays as written. A hunk whose lines are not in the file, or that fits in more than one place, gets a fixed one-line CANNOT REPAIR answer.

Rules checked on 2026-10-08 against the GNU diffutils manual, sections "Detailed Description of Unified Format" and "Incomplete Lines" (read-only; the manual's web host did not answer from this machine, so the Texinfo source of the manual inside the diffutils 3.10 release archive was read instead): hunk lines start with a space, minus or plus; a one-line range shows only its start number, otherwise start and count; lines common to both files start with a space; an incomplete last line is marked by a following line that starts with a backslash ("No newline at end of file"). Checked locally with git 2.54 on scratch files outside the repo: the empty-range start (the line before, 0 at the top), that git apply refuses zero-context hunks without the unidiff-zero option (so no test case relies on one), that git apply is lenient about a lost carriage return and about a blank context line with no leading space (the test tool is stricter and the skill follows the stricter rule), and that every ideal answer in the test cases applies with git apply and gives the expected text. Not from the manual, our own decisions: refuse instead of dropping a bad hunk, refuse when two places fit, restore context only for whitespace differences.

Test tool note: the patch check uses the diff package, which forgives a missing newline marker on a context line. The two marker cases therefore also carry a regex that requires the marker line.

Not measured yet: the with and without numbers for Claude Sonnet and Claude Haiku come from the test run that follows. Start price $0.04, to be settled by that run.

## License

Perpetual, non-exclusive; use and modify for yourself incl. paid work; no resale or republishing. Holder: Georgi Kalchev, aiskills402.com. Terms: https://aiskills402.com/docs#license

## FAQ

### Headers, whitespace, markers: which parts change, and which never do?

It changes the bookkeeping: line counts and start numbers in each hunk header, whitespace that the diff lost in transit (a tab turned into spaces, a trailing space, a carriage return, a blank context line without its space), the backslash marker for a missing final line break, hunk order and overlapping hunks. It never touches an added or a removed line: no re-indenting, no typo fixes, no reformatting, no reordering of the edits themselves. Usual culprits are automated summarizers, models that rewrite a patch, chat copy-paste, mail clients, tab-converting editors. The result applies cleanly.

### Why does a hunk that cannot be placed get a refusal and not a best-effort patch?

Because a diff written for another version of the file has no correct repair. Dropping the hunk or changing the line it removes produces a patch that applies and does a different job, and nobody sees the difference until the code runs. The same holds when the context and removed lines fit in two places and the line number does not choose. The skill answers with one CANNOT REPAIR line that names the hunk, so a person or a better source of the file decides.

### Does it help Claude Sonnet?

For Sonnet the gain was zero. Twenty-three broken diffs went to Sonnet and Haiku with and without the file, and each answer was applied to its file by our patch code. Sonnet repaired 22 either way. Haiku went from 18 to 22, which is where the file earns its two cents. One honest gap: a file with Windows line endings failed for every model on both sides, so we make no claim about carriage returns, even though the rules cover them.

### What do I need to give it?

Two things in one message: the full text of the file the diff is for, exactly as it is now, and the broken diff. Without the file there is nothing to compare the context and the removed lines with, and the skill answers that it cannot repair a diff it cannot place. If the file is long, send it whole anyway: the position of a hunk is decided by finding its lines in the file, not by trusting the numbers in the header. A diff produced for an older revision of the file is the usual reason for a refusal; fetch the revision it was made against, or regenerate the patch, and run the skill again.

## Related skills

- [Code Review](https://aiskills402.com/skills/code-review.md): $0.01 once
- [JSON Repair: Fix It, Keep Every Value](https://aiskills402.com/skills/json-repair.md): $0.05 once
- [CSV Repair: Fix the Form, Keep Every Cell](https://aiskills402.com/skills/csv-repair.md): $0.05 once

## Measurement limits

- Models other than the two named above were not run.
- Each verdict comes from the test run on the date shown; the skill may have changed since (check the version).
- Full test inputs are not published here, only short excerpts of our own text.
- Results on your own texts, languages and domains can differ.

Offer note: Paid in USDC (USD-pegged) over x402 by an AI agent; one-time.
