Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(diff): keep combined-diff changes made against a later parent (#4318)

Jay Hemnani4b788fd04d40
diff --git a/crates/headroom-core/src/transforms/diff_compressor.rs b/crates/headroom-core/src/transforms/diff_compressor.rs
index 5a200404b..dc20ddf09 100644
--- a/crates/headroom-core/src/transforms/diff_compressor.rs
+++ b/crates/headroom-core/src/transforms/diff_compressor.rs
@@ -802,7 +802,18 @@ fn parse_diff(lines: &[&str]) -> ParsedDiff {
// Hunk content.
if let Some(h) = current_hunk.as_mut() {
- if line.starts_with('+') && !line.starts_with("+++") {
+ let parents = hunk_parents(&h.header);
+ if parents > 1 && !line.starts_with('\\') {
+ let (added, deleted) = combined_change(line, parents);
+ if deleted {
+ h.deletions += 1;
+ } else if added {
+ h.additions += 1;
+ } else {
+ h.context_lines += 1;
+ }
+ h.lines.push(line.to_string());
+ } else if line.starts_with('+') && !line.starts_with("+++") {
h.additions += 1;
h.lines.push(line.to_string());
} else if line.starts_with('-') && !line.starts_with("---") {
@@ -996,6 +1007,24 @@ fn select_hunks(hunks: Vec<DiffHunk>, max_per_file: usize) -> (Vec<DiffHunk>, Ve
)
}
+/// Number of `+`/`-` prefix columns on a hunk's lines: one per parent, so 1
+/// for `@@` and N for a combined-diff `@@@…` header with N parents.
+fn hunk_parents(header: &str) -> usize {
+ header
+ .bytes()
+ .take_while(|&b| b == b'@')
+ .count()
+ .saturating_sub(1)
+ .max(1)
+}
+
+/// `(is_addition, is_deletion)` for a combined-diff hunk line. A change can
+/// sit in any parent column, e.g. ` -old` / ` +new` against the second parent.
+fn combined_change(line: &str, parents: usize) -> (bool, bool) {
+ let cols = &line.as_bytes()[..parents.min(line.len())];
+ (cols.contains(&b'+'), cols.contains(&b'-'))
+}
+
fn extract_line_number(header: &str) -> usize {
// Use the dedicated `+N` regex — works for both `@@` and `@@@` headers.
// The previous implementation captured group(1) of the hunk-header
@@ -1014,13 +1043,23 @@ fn extract_line_number(header: &str) -> usize {
// ─── Context trimming ──────────────────────────────────────────────────────
fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
+ let parents = hunk_parents(&hunk.header);
+ let change_kind = |l: &str| -> (bool, bool) {
+ if parents > 1 {
+ combined_change(l, parents)
+ } else {
+ (l.starts_with('+'), l.starts_with('-'))
+ }
+ };
+
// Indices of `+`/`-` lines.
let change_positions: Vec<usize> = hunk
.lines
.iter()
.enumerate()
.filter_map(|(i, l)| {
- if l.starts_with('+') || l.starts_with('-') {
+ let (added, deleted) = change_kind(l);
+ if added || deleted {
Some(i)
} else {
None
@@ -1075,10 +1114,11 @@ fn reduce_context(hunk: &DiffHunk, max_context: usize) -> DiffHunk {
for &i in &keep {
let line = &hunk.lines[i];
new_lines.push(line.clone());
- if line.starts_with('+') {
- additions += 1;
- } else if line.starts_with('-') {
+ let (added, deleted) = change_kind(line);
+ if deleted {
deletions += 1;
+ } else if added {
+ additions += 1;
} else {
context_lines += 1;
}
@@ -1744,6 +1784,56 @@ mod tests {
assert!(r.compressed.contains("++merge_added"));
}
+ /// A combined-diff change against the second parent (` -old` / ` +new`)
+ /// starts with a space, like context. It must still count as a change
+ /// and survive the context trim, wherever it sits in the hunk.
+ #[test]
+ fn combined_diff_second_parent_changes_survive_context_trim() {
+ let lines = [
+ "diff --combined merge.py",
+ "--- a/merge.py",
+ "+++ b/merge.py",
+ "@@@ -1,7 -1,7 +1,7 @@@",
+ " ctx_1",
+ " ctx_2",
+ " ctx_3",
+ " -old_from_p2",
+ " +new_from_p1",
+ " ctx_4",
+ " ctx_5",
+ " ctx_6",
+ "@@@ -20,8 -20,8 +20,8 @@@",
+ "- old_p1",
+ " -old_p2",
+ "++merged",
+ " ctx_a",
+ " ctx_b",
+ " ctx_c",
+ " ctx_d",
+ " -far_old_from_p2",
+ " +far_new_from_p1",
+ ];
+ let input = lines.join("\n");
+ let cfg = DiffCompressorConfig {
+ min_lines_for_ccr: 5,
+ ..Default::default()
+ };
+ let r = DiffCompressor::new(cfg).compress(&input, "");
+ for change in [
+ " -old_from_p2",
+ " +new_from_p1",
+ " -far_old_from_p2",
+ " +far_new_from_p1",
+ ] {
+ assert!(
+ r.compressed.lines().any(|l| l == change),
+ "{change:?} dropped as context:\n{}",
+ r.compressed
+ );
+ }
+ assert_eq!((r.additions, r.deletions), (3, 4));
+ }
+
/// Bug-fix test: pre-diff content (commit headers, email-style
/// metadata) is preserved verbatim before the diff sections. Before
/// the fix, anything before the first `diff --git` was silently