पाठ 8 / 25
Minimal Diffs and Size Limits
The smaller the patch, the easier to trust.
Measure and cap the diff
Small diffs are easier to review, less likely to hide unrelated changes, and simpler to revert. Measure each candidate patch (changed lines, files touched) and reject anything above a threshold for automated fixes; larger changes need a human-led change. Watch for drive-by refactors, signature changes, new helpers and reformatting mixed into a bug fix. Formatting tools should run separately so they do not inflate fix diffs.
A diff-size gate, run
I ran this with Python 3 (standard library) and, where it uses git, real git in a throwaway temporary repository. Candidate patches are written by hand to stand in for model output. The minimal fix changes 2 lines (one removed, one added) and passes the gate; a sprawling version that also changes the function signature and adds a helper and a cache changes 10 lines and is rejected. The minimal unified diff is printed.
import difflib
before = """def discount(total, code):
if code == "SAVE10":
return total * 0.9
if code == "FLAT50" and total > 50:
return total - 50
return total
"""
minimal = before.replace("total > 50", "total >= 50")
sprawling = minimal.replace("def discount(total, code):", "def discount(total, code, *, currency='INR', log=None):") \
+ "\n\ndef helper():\n pass\n\nCACHE = {}\n"
def changed_lines(a, b):
diff = list(difflib.unified_diff(a.splitlines(), b.splitlines(), lineterm=""))
return sum(1 for l in diff if l[:1] in "+-" and not l.startswith(("+++", "---")))
for name, patch in [("minimal fix", minimal), ("sprawling fix", sprawling)]:
n = changed_lines(before, patch)
print(f"{name:<14} changed lines {n:>2} -> {'OK' if n <= 6 else 'REJECT: too large for an auto-fix'}")
print("\n".join(l for l in difflib.unified_diff(before.splitlines(), minimal.splitlines(), "a/pricing.py", "b/pricing.py", lineterm="", n=0)))
Output:
minimal fix changed lines 2 -> OK sprawling fix changed lines 10 -> REJECT: too large for an auto-fix --- a/pricing.py +++ b/pricing.py @@ -4 +4 @@ - if code == "FLAT50" and total > 50: + if code == "FLAT50" and total >= 50:
Separate formatting from fixes
Run formatters in their own commits or before the fix, so the fix diff shows only the real change.
त्वरित जाँच: Why reject large diffs from an automated fixer?
- Git cannot store them
- Large diffs never compile
- They are harder to review and more likely to include unrelated or risky changes
- They always fail tests
Answer
They are harder to review and more likely to include unrelated or risky changes — Small changes are reviewable changes.