AI code review tools miscount line numbers on long pull requests because of a documented quirk in the unified diff format itself: a hunk header’s line count is omitted entirely when that count is exactly one. A parser that always expects a comma and a number after the starting line will misread the very next hunk, and on a long diff with dozens of hunks, that single misread shifts every line number after it — which is why the bug is nearly invisible on a three-line diff and devastating on a three-hundred-line one.
Where the mistake actually lives
A unified diff hunk header looks like @@ -l,s +l,s @@, where l is the starting line number in that version of the file and s is how many lines the hunk spans, as Git’s own documentation on generating patches describes it. The part most tooling gets wrong is a small formatting exception the GNU diffutils manual spells out directly: “If a hunk contains just one line, only its start line number appears.” No comma, no count — just the bare line number. Code that was written (or generated) assuming every hunk header has the shape -l,s +l,s will either throw on that line or, worse, silently parse the wrong token as the count and shift every subsequent line offset by one.
Single-line hunks are not rare edge cases confined to toy examples. They show up constantly in real diffs: a one-line import added, a single changed config value, a lone removed debug statement sitting between two untouched blocks. A diff of any real length will almost certainly contain at least one of them, which is exactly why this bug surfaces on long diffs far more reliably than on short ones.
Why it compounds instead of staying contained
A single misread hunk header wouldn’t matter much if its effect stayed local. It doesn’t, because most line-mapping code tracks a running offset: current line number in the old file, current line number in the new file, updated hunk by hunk as the parser walks the diff. Get one hunk’s starting line wrong by one, and every hunk after it inherits that same one-line shift. A review comment generated against “line 412” lands on line 411 or 413 in the actual file — close enough to look plausible, wrong enough to point a human reviewer at the wrong statement entirely.
This is a known operational problem for AI code review products, not a theoretical one. CodeRabbit has written publicly about the broader challenge of context engineering for diff review at scale — the general point being that once a diff is long enough to require chunking for a model’s context window, keeping every chunk’s line-number bookkeeping consistent with the whole file becomes its own engineering problem, separate from the actual code-quality judgment the model is supposed to make. Off-by-one hunk parsing is one of the simplest ways that bookkeeping breaks.
What to check before trusting a line-specific AI comment
- Open the actual line the comment references before acting on it, especially past the first few hunks of a large diff. If the comment describes code that doesn’t match what’s on that line, don’t assume the content judgment was wrong — check whether the line number itself drifted.
- Watch diffs with several single-line hunks close together. A run of small, scattered one-line changes is the exact pattern that triggers repeated header-parsing exceptions, compounding the drift faster than one isolated hunk would.
- Treat “line not found” or obviously mismatched comments as a parsing symptom, not a sign the tool misunderstood the code. The two failure modes look different in practice: a bad judgment usually still points at real code; a line-drift bug points at code that has nothing to do with the comment.
How this interacts with long-context handling generally
The failure gets worse, not better, on exactly the diffs where review matters most. Long diffs are also the ones most likely to get split across multiple model calls to fit a context window, and each split is another place for a running line-offset to lose sync with the actual file. It’s the same class of problem covered in Anthropic’s own prompting guidance for long inputs: when a long document gets chunked or restructured for a model, anything that depends on exact position — like a line number — needs to be carried forward explicitly, because the model has no independent way to recover it once it’s lost.
This is a different failure mode than a bad suggestion
It’s worth separating this from the kind of mistake we covered in our piece on AI code review missing race conditions: that’s a judgment failure, where the model looks at the right code and reasons about it incorrectly. An off-by-one line mapping is a plumbing failure — the judgment might be completely correct, applied to the wrong line. The fix is also different. A judgment gap needs a better prompt or an extra review pass, which is the approach behind our four-pass code review prompt framework. A line-mapping gap needs the underlying tool fixed, or at minimum, a habit of verifying line references before acting on them.
What a tool that handles this correctly actually does
A parser that handles the single-line case correctly treats the hunk header’s count as optional and defaults it to one when absent, rather than assuming the comma-and-count form is mandatory. It also recomputes the running line offset per file, resetting at each new diff --git block instead of carrying a stale offset from the previous file in a multi-file diff. Neither fix is complicated once you know the failure mode exists; the difficulty is almost entirely in noticing it, since the symptom — a comment on the wrong line — looks identical to a dozen other, more mundane reasons an AI reviewer might be slightly off.
Where this shows up most often in practice
| Diff pattern | Risk of line drift | Why |
|---|---|---|
| Single large hunk, one file | Low | Only one header to parse; a miscount doesn’t compound |
| Many small hunks, one file | High | Each single-line hunk is a chance to mis-parse the header format |
| Many files, mixed hunk sizes | Highest | Offsets must reset correctly per file; a leftover offset from the previous file compounds the error |
Seeing the exception yourself, without any AI tool involved
You don’t need an AI code review tool to reproduce the exact formatting quirk that causes this. Change a single line in any tracked file and run a plain git diff:
- Edit exactly one line in a file, then run
git diff -- path/to/file. - Look at the hunk header. If the changed region is a single line on either side, that side’s header segment shows only the line number — no comma, no count.
- Now edit two or more consecutive lines and re-run the same command. The header for that side changes shape, adding the comma and count.
That difference in shape is the entire bug surface. Any script, webhook handler, or custom integration that splits a hunk header on a fixed pattern like -(\d+),(\d+) will fail to match, or match the wrong group, the moment it hits the single-line form. It’s a two-minute experiment, and it’s worth running once so the failure mode stops being abstract: it’s not “the AI misunderstood the diff,” it’s “the header didn’t have the shape the parser assumed it would always have.”
This also explains why the bug tends to survive testing. Developers writing or evaluating a diff parser naturally test it against multi-line changes, because those are what they’re usually thinking about when they write the parser in the first place. The single-line case is the kind of input that only shows up once real, messy pull requests start flowing through the tool, which is exactly when it’s hardest to notice.
FAQ
Is this specific to any one AI code review product?
No. It’s a consequence of how the unified diff format itself is specified, which is documented behavior in Git and GNU diffutils, not a bug introduced by any particular vendor. Any tool that parses raw diffs is exposed to it unless it specifically handles the single-line hunk-header case.
Does using a pull request’s web-based review UI avoid this?
Mostly, since the UI maps comments to lines using the platform’s own rendering rather than a raw text parse. The risk is highest when an AI tool reads a raw diff (from a CLI, a webhook payload, or a patch file) and does its own line-number bookkeeping, which is exactly what our checklist for reviewing AI-generated pull requests recommends treating as unverified until a human confirms it against the actual file.
None of this means AI code review on large diffs isn’t worth doing, and it isn’t a reason to abandon automated review on big pull requests. It means a line number attached to a comment is a claim, not a fact, until someone actually opens that line and checks it against the surrounding code — the same baseline discipline that should already apply to any AI-assisted pull request description that references specific code by line number.
Photo: “Coding Shots Annual Plan high res-5.jpg” by Matthew (WMF), CC BY-SA 3.0, via Wikimedia Commons.



