Diff Hunk Repair
GitAuto automatically recalculates and repairs incorrect line counts in unified diff hunk headers (@@ -X,Y +A,B @@). The model frequently gets these counts wrong because it cannot reliably count lines, making this one of the most common failure modes in AI-generated diffs.
Why This Exists
When the model generates a unified diff, it must specify how many lines are removed and added in each hunk header. For example, @@ -1,5 +1,7 @@ means 5 lines from the original and 7 lines in the new version. The model routinely miscounts these numbers, especially in larger hunks. When the counts are wrong, git apply silently fails or corrupts the file. Before this fix existed, wrong hunk counts were the single largest source of failed file edits.
Why Models Miscount Hunk Headers
Models generate diffs token-by-token and cannot reliably count how many lines appear in each hunk. The hunk header (e.g., @@ -1,7 +1,9 @@) requires knowing the exact line count before the content is generated, but the model writes the header first and the content after. It is predicting the count before it knows what it will write. Benchmarks evaluate whether the final code is correct, not whether the diff format is valid, so models are never penalized for malformed hunk headers during training.
How It Works
After the model produces a diff, GitAuto runs a repair pass that uses regex to find each @@ hunk header, then counts the actual context lines (lines starting with a space), removal lines (starting with -), and addition lines (starting with +) in the hunk body. It rebuilds each header with the correct counts.
Before: @@ -1,5 +1,7 @@ (wrong count)
After: @@ -1,5 +1,8 @@ (correct count)
This repair runs automatically on every diff before git apply is called, requiring zero agent iterations and zero additional API calls.
Related Features
- Diff Prefix Repair - Ensures proper
a/andb/prefixes in diff file paths - Tool Argument Correction - Detects when diff arguments are sent to the wrong tool
Need Help?
Have questions or suggestions? We're here to help you get the most out of GitAuto.
Contact us with your questions or feedback!