Code & Engineering

Unified Diff Repair: Fix the Counts, Keep the Change

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.

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.

Tested 2026-10-08No code, no hidden instructionsv1.0.0 · 10.5 KB · perpetual license

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 with a strong and a weak model.

With and without the skill

Results with and without the skill, for Sonnet and Haiku
SonnetHaiku
withwithoutwithwithout
Diffs repaired or refused right (23 diffs)22/2322/2322/2318/23

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.

SonnetStrong model, claude-sonnet-5-5
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.
HaikuWeak model, claude-haiku-5-5
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.

Full test summary

Example

Our own test text, before and after the skill ran. Excerpts only.

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. Tried in: English, Bulgarian, Spanish, German.

License

Perpetual, non-exclusive; use and modify for yourself incl. paid work; no resale or republishing. Holder: Georgi Kalchev, aiskills402.com. Full terms.

Versions

Current version 1.0.0, updated 2026-10-08. Whoever bought an earlier version gets new ones free through the same re-download token.

  1. v1.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.

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.

Share

Read this page as Markdown: /skills/unified-diff-repair.md.

  • Code Review

    Code & Engineering

    SKILL.md · v1.0.4 · 6.0 KB

    Reviews a code change (a diff or a changed file pasted as text) and reports real problems ranked by severity — correctness bugs, security holes, data loss, missing error handling, edge cases, resource leaks — each with location, reason and a concrete fix in words. Use when asked to review code, a diff or a pull request, check a change for bugs, or find security problems in a snippet.

    $0.01once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 30 Sep 2026

  • JSON Repair: Fix It, Keep Every Value

    Data & Analysis

    SKILL.md · v1.0.1 · 6.9 KB

    Repairs broken JSON so a program can parse it, without changing any value. Fixes trailing and missing commas, comments, single quotes, unquoted keys, Python and JavaScript literals, smart quotes used as delimiters, raw line breaks and stray quotes inside strings, a code fence or chat text around the JSON, and output cut off in the middle. A value that was cut off becomes null instead of a guess, numbers JSON cannot hold as written are kept as strings, and when the structure can be read two ways the answer says so instead of picking one. Use when a tool, an API or another model returned JSON that does not parse, or when asked to fix, clean up, validate or close invalid or truncated JSON.

    $0.05once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 7 Oct 2026

  • CSV Repair: Fix the Form, Keep Every Cell

    Data & Analysis

    SKILL.md · v1.0.0 · 8.3 KB

    Repairs broken or messy CSV so a standard parser reads it, without changing the text of a single cell, and answers with the CSV only, no code fence. Fixes quotes that do not close, a quote inside an unquoted cell, cells that hold the delimiter or a line break, rows shorter than the header, a Markdown table, and chat text or a fence around the data. Numbers such as 1,250, 007 and 1.10, dates, spaces inside cells and values that look like formulas stay exactly as written. Semicolon and tab files keep their delimiter. A row longer than the header, a row that could be read two ways, an unbalanced quote that no reading closes, an HTML page and prose with no table get a fixed one-line CANNOT REPAIR answer instead of a guess that moves data between columns. Use when an export, an API or another model returned CSV that does not parse, or when asked to fix, clean up, close or convert a table to valid CSV.

    $0.05once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026