Files
Jay HemnaniandJD Davis 9355ec7019 fix(diff): renumber hunk headers after trimming context (#4319)
## Description

`DiffCompressor` trims each hunk to `max_context_lines` (2) around its
changes but keeps the hunk's original `@@ -a,b +c,d @@` header. The
header no longer describes the lines under it:

- Leading context is dropped first, so every line number read off a hunk
is off by the lines cut (one with git's default `-U3`).
- Context dropped between two changes more than 4 lines apart leaves
them next to each other. Nothing marks the gap, and every line after it
is numbered wrong.

A 400-line file with lines 10 and 17 changed, `git diff` (73 lines)
through `DiffCompressor().compress()` on 0.41.0:

```text
@@ -7,14 +7,14 @@ line 6
 line 8
 line 9
-line 10
+line 10 changed
 line 11
 line 12
 line 15
 line 16
-line 17
+line 17 changed
```

By the header, `line 8` is line 7 and `-line 17` is line 14. Anything
that maps these hunks to file lines (a model writing an edit, `git
apply`) gets the wrong place.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `reduce_context` splits a trimmed hunk at each gap and recomputes
every header from the original line numbers, one range per parent for
combined diffs, formatted the way git writes them. The section heading
is carried over unchanged.
- A hunk that loses no lines keeps its header byte-for-byte.
- Lines after the extent a header declares (the empty string from a
trailing newline, a format-patch `-- ` signature) are not numbered and
no longer count as changes.
- A format-patch signature and what follows it stay whatever the context
limit.
- A hunk with edits past the extent its header declares is malformed
input and passes through as it came.
- `hunks_kept` / `hunks_removed` still count the original hunks, so a
split can't underflow `hunks_removed`.
- Five `diff_compressor` parity fixtures recorded the old headers. In
their outputs 33 hunks had header counts that disagree with their own
lines; with this change there are none. Only `output.compressed` changes
in those files.

## Testing

- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ cargo test -p headroom-core --lib
test result: ok. 979 passed; 0 failed; 1 ignored

# new tests against the previous commit:
  left: ["@@ -7,14 +7,14 @@ l6", "@@ -23,6 +23,7 @@ l22"]
 right: ["@@ -8,5 +8,5 @@ l6", "@@ -15,5 +15,5 @@ l6", "@@ -24,4 +24,5 @@ l22"]
test result: FAILED. 0 passed; 2 failed

$ cargo run -p headroom-parity -- run --fixtures tests/parity/fixtures
[diff_compressor ] total=27 matched=27 skipped=0 diffed=0

$ pytest <the 12 test files that contain hunk headers> tests/test_compress*.py tests/test_transforms/test_diff_compressor_rust_parity.py tests/parity
754 passed, 3 skipped
$ cargo clippy -p headroom-core --lib --tests -- -D warnings   # clean
```

## Real Behavior Proof

- Environment: Ubuntu, Python 3.12, headroom-ai 0.41.0 from PyPI vs this
branch built with `pip install -e .`
- Exact command / steps: for each commit, `git diff c~1 c` through
`DiffCompressor(DiffCompressorConfig(max_hunks_per_file=10**6,
max_files=10**6)).compress(...)`. Then compare every hunk (header ranges
and lines) with `git diff -U2 c~1 c`. Last 300 non-merge commits of this
repo, plus 200 of another Python repo. Merge commits the same way with
`git show -c` / `-c -U2`.
- Observed result:
  ```text
  0.41.0, this repo:    compressed=276 match_git_U2=3
  branch, this repo:    compressed=276 match_git_U2=276
  branch, second repo:  compressed=163 match_git_U2=163
  branch, git show -c:  compressed=36  match=36
  ```
  Same example after the change:
  ```text
  @@ -8,5 +8,5 @@ line 6
   line 8
   line 9
  -line 10
  +line 10 changed
   line 11
   line 12
  @@ -15,5 +15,5 @@ line 6
   line 15
  ```
- Not fully matched: `git show --cc`. 8 of 11 merges match `--cc -U2`.
In the other 3, git hides more single-parent hunks when the context
shrinks, which a context trim doesn't try to copy. The compressor keeps
every change line of the `--cc` input there.

## Runtime Rollout Safety

N/A: no rollout-managed feature, no config change.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

The section heading after the second `@@` is kept from the original
hunk. git recomputes it for each hunk, but that depends on the file's
language driver, and the original heading still names the enclosing
context.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4319"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: JD Davis <mxjerrett@gmail.com>
2026-11-04 22:51:06 -06:00
..