reversal_tracking.rs

   1use std::ops::Range;
   2use std::path::Path;
   3use std::sync::Arc;
   4
   5use edit_prediction::udiff::apply_diff_to_string;
   6use language::{char_diff, text_diff};
   7
   8use zeta_prompt::ZetaPromptInput;
   9
  10fn apply_diff_to_string_lenient(diff_str: &str, text: &str) -> String {
  11    let hunks = parse_diff_hunks(diff_str);
  12    let mut result = text.to_string();
  13
  14    for hunk in hunks {
  15        let hunk_diff = format!("--- a/file\n+++ b/file\n{}", format_hunk(&hunk));
  16        if let Ok(updated) = apply_diff_to_string(&hunk_diff, &result) {
  17            result = updated;
  18        }
  19    }
  20
  21    result
  22}
  23
  24#[derive(Debug, Clone, PartialEq, Eq)]
  25struct ParsedHunk {
  26    old_start: u32,
  27    old_count: u32,
  28    new_start: u32,
  29    new_count: u32,
  30    lines: Vec<HunkLine>,
  31}
  32
  33#[derive(Debug, Clone, PartialEq, Eq)]
  34enum HunkLine {
  35    Context(String),
  36    Addition(String),
  37    Deletion(String),
  38}
  39
  40fn parse_hunk_header(line: &str) -> Option<(u32, u32, u32, u32)> {
  41    let line = line.strip_prefix("@@ -")?;
  42    let (old_part, rest) = line.split_once(' ')?;
  43    let rest = rest.strip_prefix('+')?;
  44    let (new_part, _) = rest.split_once(" @@")?;
  45
  46    let (old_start, old_count) = if let Some((start, count)) = old_part.split_once(',') {
  47        (start.parse().ok()?, count.parse().ok()?)
  48    } else {
  49        (old_part.parse().ok()?, 1)
  50    };
  51
  52    let (new_start, new_count) = if let Some((start, count)) = new_part.split_once(',') {
  53        (start.parse().ok()?, count.parse().ok()?)
  54    } else {
  55        (new_part.parse().ok()?, 1)
  56    };
  57
  58    Some((old_start, old_count, new_start, new_count))
  59}
  60
  61fn parse_diff_hunks(diff: &str) -> Vec<ParsedHunk> {
  62    let mut hunks = Vec::new();
  63    let mut current_hunk: Option<ParsedHunk> = None;
  64
  65    for line in diff.lines() {
  66        if let Some((old_start, old_count, new_start, new_count)) = parse_hunk_header(line) {
  67            if let Some(hunk) = current_hunk.take() {
  68                hunks.push(hunk);
  69            }
  70            current_hunk = Some(ParsedHunk {
  71                old_start,
  72                old_count,
  73                new_start,
  74                new_count,
  75                lines: Vec::new(),
  76            });
  77        } else if let Some(ref mut hunk) = current_hunk {
  78            if let Some(stripped) = line.strip_prefix('+') {
  79                hunk.lines.push(HunkLine::Addition(stripped.to_string()));
  80            } else if let Some(stripped) = line.strip_prefix('-') {
  81                hunk.lines.push(HunkLine::Deletion(stripped.to_string()));
  82            } else if let Some(stripped) = line.strip_prefix(' ') {
  83                hunk.lines.push(HunkLine::Context(stripped.to_string()));
  84            } else if line.is_empty() {
  85                hunk.lines.push(HunkLine::Context(String::new()));
  86            }
  87        }
  88    }
  89
  90    if let Some(hunk) = current_hunk {
  91        hunks.push(hunk);
  92    }
  93
  94    hunks
  95}
  96
  97fn format_hunk(hunk: &ParsedHunk) -> String {
  98    let mut result = format!(
  99        "@@ -{},{} +{},{} @@\n",
 100        hunk.old_start, hunk.old_count, hunk.new_start, hunk.new_count
 101    );
 102    for line in &hunk.lines {
 103        match line {
 104            HunkLine::Context(text) => {
 105                result.push(' ');
 106                result.push_str(text);
 107                result.push('\n');
 108            }
 109            HunkLine::Addition(text) => {
 110                result.push('+');
 111                result.push_str(text);
 112                result.push('\n');
 113            }
 114            HunkLine::Deletion(text) => {
 115                result.push('-');
 116                result.push_str(text);
 117                result.push('\n');
 118            }
 119        }
 120    }
 121    result
 122}
 123
 124fn filter_diff_hunks_by_excerpt(
 125    diff: &str,
 126    excerpt_start_row: u32,
 127    excerpt_row_count: u32,
 128) -> (String, i32) {
 129    let hunks = parse_diff_hunks(diff);
 130    let excerpt_start_0based = excerpt_start_row;
 131    let excerpt_end_0based = excerpt_start_row + excerpt_row_count;
 132
 133    let mut filtered_hunks = Vec::new();
 134    let mut cumulative_line_offset: i32 = 0;
 135
 136    for hunk in hunks {
 137        let hunk_start_0based = hunk.new_start.saturating_sub(1);
 138        let hunk_end_0based = hunk_start_0based + hunk.new_count;
 139
 140        let additions: i32 = hunk
 141            .lines
 142            .iter()
 143            .filter(|l| matches!(l, HunkLine::Addition(_)))
 144            .count() as i32;
 145        let deletions: i32 = hunk
 146            .lines
 147            .iter()
 148            .filter(|l| matches!(l, HunkLine::Deletion(_)))
 149            .count() as i32;
 150        let hunk_line_delta = additions - deletions;
 151
 152        if hunk_end_0based <= excerpt_start_0based {
 153            cumulative_line_offset += hunk_line_delta;
 154            continue;
 155        }
 156
 157        if hunk_start_0based >= excerpt_end_0based {
 158            continue;
 159        }
 160
 161        let mut filtered_lines = Vec::new();
 162        let mut current_row_0based = hunk_start_0based;
 163        let mut filtered_old_count = 0u32;
 164        let mut filtered_new_count = 0u32;
 165        let mut first_included_row: Option<u32> = None;
 166
 167        for line in &hunk.lines {
 168            match line {
 169                HunkLine::Context(text) => {
 170                    if current_row_0based >= excerpt_start_0based
 171                        && current_row_0based < excerpt_end_0based
 172                    {
 173                        if first_included_row.is_none() {
 174                            first_included_row = Some(current_row_0based);
 175                        }
 176                        filtered_lines.push(HunkLine::Context(text.clone()));
 177                        filtered_old_count += 1;
 178                        filtered_new_count += 1;
 179                    }
 180                    current_row_0based += 1;
 181                }
 182                HunkLine::Addition(text) => {
 183                    if current_row_0based >= excerpt_start_0based
 184                        && current_row_0based < excerpt_end_0based
 185                    {
 186                        if first_included_row.is_none() {
 187                            first_included_row = Some(current_row_0based);
 188                        }
 189                        filtered_lines.push(HunkLine::Addition(text.clone()));
 190                        filtered_new_count += 1;
 191                    }
 192                    current_row_0based += 1;
 193                }
 194                HunkLine::Deletion(text) => {
 195                    if current_row_0based >= excerpt_start_0based
 196                        && current_row_0based < excerpt_end_0based
 197                    {
 198                        if first_included_row.is_none() {
 199                            first_included_row = Some(current_row_0based);
 200                        }
 201                        filtered_lines.push(HunkLine::Deletion(text.clone()));
 202                        filtered_old_count += 1;
 203                    }
 204                }
 205            }
 206        }
 207
 208        if !filtered_lines.is_empty() {
 209            let first_row = first_included_row.unwrap_or(excerpt_start_0based);
 210            let new_start_1based = (first_row - excerpt_start_0based) + 1;
 211
 212            filtered_hunks.push(ParsedHunk {
 213                old_start: new_start_1based,
 214                old_count: filtered_old_count,
 215                new_start: new_start_1based,
 216                new_count: filtered_new_count,
 217                lines: filtered_lines,
 218            });
 219        }
 220
 221        cumulative_line_offset += hunk_line_delta;
 222    }
 223
 224    let mut result = String::new();
 225    for hunk in &filtered_hunks {
 226        result.push_str(&format_hunk(hunk));
 227    }
 228
 229    (result, cumulative_line_offset)
 230}
 231
 232fn compute_excerpt_aware_reversal_overlap(
 233    edit_history_diffs: &[&str],
 234    excerpt_content: &str,
 235    excerpt_start_row: u32,
 236    predicted_content: &str,
 237) -> ReversalOverlap {
 238    let mut current_content = excerpt_content.to_string();
 239    let mut current_excerpt_start_row = excerpt_start_row;
 240
 241    for diff in edit_history_diffs.iter().rev() {
 242        if diff.is_empty() {
 243            continue;
 244        }
 245
 246        let current_row_count = current_content.lines().count() as u32;
 247        let (filtered_diff, _line_offset) =
 248            filter_diff_hunks_by_excerpt(diff, current_excerpt_start_row, current_row_count.max(1));
 249
 250        if filtered_diff.is_empty() {
 251            let hunks = parse_diff_hunks(diff);
 252            for hunk in hunks {
 253                let hunk_end = hunk.new_start.saturating_sub(1) + hunk.new_count;
 254                if hunk_end <= current_excerpt_start_row {
 255                    let additions: u32 = hunk
 256                        .lines
 257                        .iter()
 258                        .filter(|l| matches!(l, HunkLine::Addition(_)))
 259                        .count() as u32;
 260                    let deletions: u32 = hunk
 261                        .lines
 262                        .iter()
 263                        .filter(|l| matches!(l, HunkLine::Deletion(_)))
 264                        .count() as u32;
 265                    if additions >= deletions {
 266                        current_excerpt_start_row =
 267                            current_excerpt_start_row.saturating_sub(additions - deletions);
 268                    } else {
 269                        current_excerpt_start_row += deletions - additions;
 270                    }
 271                }
 272            }
 273            continue;
 274        }
 275
 276        let reversed = reverse_diff(&format!("--- a/file\n+++ b/file\n{}", filtered_diff));
 277        match apply_diff_to_string(&reversed, &current_content) {
 278            Ok(updated) => {
 279                current_content = updated;
 280            }
 281            Err(_) => {
 282                continue;
 283            }
 284        }
 285
 286        let hunks = parse_diff_hunks(diff);
 287        for hunk in hunks {
 288            let hunk_end = hunk.new_start.saturating_sub(1) + hunk.new_count;
 289            if hunk_end <= current_excerpt_start_row {
 290                let additions: u32 = hunk
 291                    .lines
 292                    .iter()
 293                    .filter(|l| matches!(l, HunkLine::Addition(_)))
 294                    .count() as u32;
 295                let deletions: u32 = hunk
 296                    .lines
 297                    .iter()
 298                    .filter(|l| matches!(l, HunkLine::Deletion(_)))
 299                    .count() as u32;
 300                if additions >= deletions {
 301                    current_excerpt_start_row =
 302                        current_excerpt_start_row.saturating_sub(additions - deletions);
 303                } else {
 304                    current_excerpt_start_row += deletions - additions;
 305                }
 306            }
 307        }
 308    }
 309
 310    compute_reversal_overlap(&current_content, excerpt_content, predicted_content)
 311}
 312
 313fn reverse_diff(diff: &str) -> String {
 314    let mut result: String = diff
 315        .lines()
 316        .map(|line| {
 317            if line.starts_with("--- ") {
 318                line.replacen("--- ", "+++ ", 1)
 319            } else if line.starts_with("+++ ") {
 320                line.replacen("+++ ", "--- ", 1)
 321            } else if line.starts_with('+') && !line.starts_with("+++") {
 322                format!("-{}", &line[1..])
 323            } else if line.starts_with('-') && !line.starts_with("---") {
 324                format!("+{}", &line[1..])
 325            } else {
 326                line.to_string()
 327            }
 328        })
 329        .collect::<Vec<_>>()
 330        .join("\n");
 331    if diff.ends_with('\n') {
 332        result.push('\n');
 333    }
 334    result
 335}
 336
 337#[derive(Debug, Clone, PartialEq, Eq)]
 338struct GranularEdit {
 339    range: Range<usize>,
 340    old_text: String,
 341    new_text: String,
 342}
 343
 344fn compute_granular_edits(old_text: &str, new_text: &str) -> Vec<GranularEdit> {
 345    text_diff(old_text, new_text)
 346        .into_iter()
 347        .map(|(range, new_text)| GranularEdit {
 348            old_text: old_text[range.clone()].to_string(),
 349            range,
 350            new_text: new_text.to_string(),
 351        })
 352        .collect()
 353}
 354
 355#[derive(Debug, Clone)]
 356struct HistoryAdditionRange {
 357    range_in_current: Range<usize>,
 358}
 359
 360#[derive(Debug, Clone)]
 361struct HistoryDeletionRange {
 362    deleted_text: String,
 363    position_in_current: usize,
 364}
 365
 366fn compute_history_addition_ranges(history_edits: &[GranularEdit]) -> Vec<HistoryAdditionRange> {
 367    let mut result = Vec::new();
 368    let mut offset_delta: isize = 0;
 369
 370    for edit in history_edits {
 371        if !edit.new_text.is_empty() {
 372            let new_start = (edit.range.start as isize + offset_delta) as usize;
 373            let new_end = new_start + edit.new_text.len();
 374            result.push(HistoryAdditionRange {
 375                range_in_current: new_start..new_end,
 376            });
 377        }
 378
 379        offset_delta += edit.new_text.len() as isize - edit.old_text.len() as isize;
 380    }
 381
 382    result
 383}
 384
 385fn compute_history_deletion_ranges(history_edits: &[GranularEdit]) -> Vec<HistoryDeletionRange> {
 386    let mut result = Vec::new();
 387    let mut offset_delta: isize = 0;
 388
 389    for edit in history_edits {
 390        if !edit.old_text.is_empty() {
 391            let position_in_current = (edit.range.start as isize + offset_delta) as usize;
 392            result.push(HistoryDeletionRange {
 393                deleted_text: edit.old_text.clone(),
 394                position_in_current,
 395            });
 396        }
 397
 398        offset_delta += edit.new_text.len() as isize - edit.old_text.len() as isize;
 399    }
 400
 401    result
 402}
 403
 404#[derive(Debug, Clone, Default, PartialEq, Eq)]
 405struct ReversalOverlap {
 406    chars_reversing_user_edits: usize,
 407    total_chars_in_prediction: usize,
 408}
 409
 410impl ReversalOverlap {
 411    fn ratio(&self) -> f32 {
 412        if self.total_chars_in_prediction == 0 {
 413            0.0
 414        } else {
 415            self.chars_reversing_user_edits as f32 / self.total_chars_in_prediction as f32
 416        }
 417    }
 418}
 419
 420/// Normalize edits where `old_text` appears as a subsequence within `new_text` (extension),
 421/// or where `new_text` appears as a subsequence within `old_text` (reduction).
 422///
 423/// For extensions: when the user's text is preserved (in order) within the prediction,
 424/// we only count the newly inserted characters, not the preserved ones.
 425/// E.g., "epr" → "eprintln!()" becomes 8 inserted chars ("intln!()")
 426/// E.g., "test_my_function" → "a_test_for_my_special_function_plz" becomes 18 inserted chars
 427///
 428/// For reductions: when the prediction's text is preserved (in order) within the original,
 429/// we only count the deleted characters, not the preserved ones.
 430/// E.g., "ifrom" → "from" becomes 1 deleted char ("i")
 431fn normalize_extension_edits(edits: Vec<GranularEdit>) -> Vec<GranularEdit> {
 432    edits
 433        .into_iter()
 434        .flat_map(|edit| {
 435            if edit.old_text.is_empty() || edit.new_text.is_empty() {
 436                return vec![edit];
 437            }
 438
 439            // Use character-wise diff to find exact byte ranges of changes
 440            let char_edits = char_diff(&edit.old_text, &edit.new_text);
 441
 442            let all_deletions = !char_edits.is_empty()
 443                && char_edits
 444                    .iter()
 445                    .all(|(range, replacement)| !range.is_empty() && replacement.is_empty());
 446            let all_insertions = !char_edits.is_empty()
 447                && char_edits
 448                    .iter()
 449                    .all(|(range, replacement)| range.is_empty() && !replacement.is_empty());
 450            if all_deletions || all_insertions {
 451                return char_edits
 452                    .into_iter()
 453                    .map(|(range, replacement)| GranularEdit {
 454                        range: edit.range.start + range.start..edit.range.start + range.end,
 455                        old_text: edit.old_text[range].to_string(),
 456                        new_text: replacement.to_string(),
 457                    })
 458                    .collect();
 459            }
 460
 461            // Otherwise, keep the original edit (mixed changes)
 462            vec![edit]
 463        })
 464        .collect()
 465}
 466
 467fn compute_reversal_overlap(
 468    original_content: &str,
 469    current_content: &str,
 470    predicted_content: &str,
 471) -> ReversalOverlap {
 472    let history_edits =
 473        normalize_extension_edits(compute_granular_edits(original_content, current_content));
 474    let prediction_edits =
 475        normalize_extension_edits(compute_granular_edits(current_content, predicted_content));
 476
 477    let history_addition_ranges = compute_history_addition_ranges(&history_edits);
 478    let history_deletion_ranges = compute_history_deletion_ranges(&history_edits);
 479
 480    let reversed_additions =
 481        compute_reversed_additions(&history_addition_ranges, &prediction_edits);
 482    let restored_deletions =
 483        compute_restored_deletions(&history_deletion_ranges, &prediction_edits);
 484
 485    let total_chars_in_prediction: usize = prediction_edits
 486        .iter()
 487        .map(|e| e.new_text.chars().count() + e.old_text.chars().count())
 488        .sum();
 489
 490    ReversalOverlap {
 491        chars_reversing_user_edits: reversed_additions + restored_deletions,
 492        total_chars_in_prediction,
 493    }
 494}
 495
 496fn compute_reversed_additions(
 497    history_addition_ranges: &[HistoryAdditionRange],
 498    prediction_edits: &[GranularEdit],
 499) -> usize {
 500    let mut reversed_chars = 0;
 501
 502    for pred_edit in prediction_edits {
 503        for history_addition in history_addition_ranges {
 504            let overlap_start = pred_edit
 505                .range
 506                .start
 507                .max(history_addition.range_in_current.start);
 508            let overlap_end = pred_edit
 509                .range
 510                .end
 511                .min(history_addition.range_in_current.end);
 512
 513            if overlap_start < overlap_end {
 514                let relative_start = overlap_start - pred_edit.range.start;
 515                let relative_end = overlap_end - pred_edit.range.start;
 516                let overlap_text = &pred_edit.old_text[relative_start..relative_end];
 517                reversed_chars += overlap_text.chars().count();
 518            }
 519        }
 520    }
 521
 522    reversed_chars
 523}
 524
 525fn compute_restored_deletions(
 526    history_deletion_ranges: &[HistoryDeletionRange],
 527    prediction_edits: &[GranularEdit],
 528) -> usize {
 529    let mut restored = 0;
 530
 531    for pred_edit in prediction_edits {
 532        if pred_edit.new_text.is_empty() {
 533            continue;
 534        }
 535
 536        for deletion in history_deletion_ranges {
 537            if pred_edit.range.contains(&deletion.position_in_current)
 538                || deletion.position_in_current == pred_edit.range.start
 539            {
 540                restored += compute_lcs_length(&deletion.deleted_text, &pred_edit.new_text);
 541            }
 542        }
 543    }
 544
 545    restored
 546}
 547
 548fn compute_lcs_length(a: &str, b: &str) -> usize {
 549    let a_chars: Vec<char> = a.chars().collect();
 550    let b_chars: Vec<char> = b.chars().collect();
 551    let m = a_chars.len();
 552    let n = b_chars.len();
 553
 554    if m == 0 || n == 0 {
 555        return 0;
 556    }
 557
 558    let mut prev = vec![0; n + 1];
 559    let mut curr = vec![0; n + 1];
 560
 561    for i in 1..=m {
 562        for j in 1..=n {
 563            if a_chars[i - 1] == b_chars[j - 1] {
 564                curr[j] = prev[j - 1] + 1;
 565            } else {
 566                curr[j] = prev[j].max(curr[j - 1]);
 567            }
 568        }
 569        std::mem::swap(&mut prev, &mut curr);
 570        curr.fill(0);
 571    }
 572
 573    prev[n]
 574}
 575
 576fn filter_edit_history_by_path<'a>(
 577    edit_history: &'a [Arc<zeta_prompt::Event>],
 578    cursor_path: &std::path::Path,
 579) -> Vec<&'a zeta_prompt::Event> {
 580    edit_history
 581        .iter()
 582        .filter(|event| match event.as_ref() {
 583            zeta_prompt::Event::BufferChange { path, .. } => {
 584                let event_path = path.as_ref();
 585                if event_path == cursor_path {
 586                    return true;
 587                }
 588                let stripped = event_path
 589                    .components()
 590                    .skip(1)
 591                    .collect::<std::path::PathBuf>();
 592                stripped == cursor_path
 593            }
 594        })
 595        .map(|arc| arc.as_ref())
 596        .collect()
 597}
 598
 599fn extract_diff_from_event(event: &zeta_prompt::Event) -> &str {
 600    match event {
 601        zeta_prompt::Event::BufferChange { diff, .. } => diff.as_str(),
 602    }
 603}
 604
 605fn is_predicted_event(event: &zeta_prompt::Event) -> bool {
 606    match event {
 607        zeta_prompt::Event::BufferChange { predicted, .. } => *predicted,
 608    }
 609}
 610
 611pub fn compute_prediction_reversal_ratio(
 612    prompt_inputs: &ZetaPromptInput,
 613    predicted_content: &str,
 614    cursor_path: &Path,
 615) -> f32 {
 616    let current_content: &str = prompt_inputs.cursor_excerpt.as_ref();
 617
 618    let edit_history: &[Arc<zeta_prompt::Event>] = &prompt_inputs.events;
 619    let relevant_events = filter_edit_history_by_path(edit_history, cursor_path);
 620
 621    let most_recent = match relevant_events.last() {
 622        Some(event) if !is_predicted_event(event) => *event,
 623        _ => return 0.0,
 624    };
 625
 626    let diff = extract_diff_from_event(most_recent);
 627    if diff.is_empty() {
 628        return 0.0;
 629    }
 630
 631    if let Some(excerpt_start_row) = prompt_inputs.excerpt_start_row {
 632        let diffs = vec![diff];
 633        let overlap = compute_excerpt_aware_reversal_overlap(
 634            &diffs,
 635            current_content,
 636            excerpt_start_row,
 637            predicted_content,
 638        );
 639        return overlap.ratio();
 640    }
 641
 642    let reversed = reverse_diff(diff);
 643    let with_headers = format!("--- a/file\n+++ b/file\n{}", reversed);
 644    let original_content = match apply_diff_to_string(&with_headers, current_content) {
 645        Ok(updated_content) => updated_content,
 646        Err(_) => apply_diff_to_string_lenient(&reversed, current_content),
 647    };
 648
 649    let overlap = compute_reversal_overlap(&original_content, current_content, predicted_content);
 650    overlap.ratio()
 651}
 652
 653#[cfg(test)]
 654mod tests {
 655    use super::*;
 656    use edit_prediction::udiff::apply_diff_to_string;
 657    use indoc::indoc;
 658    use zeta_prompt::ExcerptRanges;
 659
 660    fn make_test_prompt_inputs(
 661        content: &str,
 662        events: Vec<Arc<zeta_prompt::Event>>,
 663        excerpt_start_row: Option<u32>,
 664    ) -> ZetaPromptInput {
 665        ZetaPromptInput {
 666            cursor_path: Arc::from(Path::new("src/test.rs")),
 667            cursor_excerpt: content.into(),
 668            cursor_offset_in_excerpt: 0,
 669            excerpt_start_row,
 670            events,
 671            related_files: Vec::new(),
 672            excerpt_ranges: ExcerptRanges {
 673                editable_150: 0..content.len(),
 674                editable_180: 0..content.len(),
 675                editable_350: 0..content.len(),
 676                editable_150_context_350: 0..content.len(),
 677                editable_180_context_350: 0..content.len(),
 678                editable_350_context_150: 0..content.len(),
 679                ..Default::default()
 680            },
 681            experiment: None,
 682            in_open_source_repo: false,
 683            can_collect_data: false,
 684        }
 685    }
 686
 687    #[test]
 688    fn test_reversal_overlap() {
 689        struct Case {
 690            name: &'static str,
 691            original: &'static str,
 692            current: &'static str,
 693            predicted: &'static str,
 694            expected_reversal_chars: usize,
 695            expected_total_chars: usize,
 696        }
 697
 698        let cases = [
 699            Case {
 700                name: "user_adds_line_prediction_removes_it",
 701                original: indoc! {"
 702                    a
 703                    b
 704                    c"},
 705                current: indoc! {"
 706                    a
 707                    new line
 708                    b
 709                    c"},
 710                predicted: indoc! {"
 711                    a
 712                    b
 713                    c"},
 714                expected_reversal_chars: 9,
 715                expected_total_chars: 9,
 716            },
 717            Case {
 718                name: "user_deletes_line_prediction_restores_it",
 719                original: indoc! {"
 720                    a
 721                    deleted
 722                    b"},
 723                current: indoc! {"
 724                    a
 725                    b"},
 726                predicted: indoc! {"
 727                    a
 728                    deleted
 729                    b"},
 730                expected_reversal_chars: 8,
 731                expected_total_chars: 8,
 732            },
 733            Case {
 734                name: "user_deletes_text_prediction_restores_partial",
 735                original: "hello beautiful world",
 736                current: "hello world",
 737                predicted: "hello beautiful world",
 738                expected_reversal_chars: 10,
 739                expected_total_chars: 10,
 740            },
 741            Case {
 742                name: "user_deletes_foo_prediction_adds_bar",
 743                original: "foo",
 744                current: "",
 745                predicted: "bar",
 746                expected_reversal_chars: 0,
 747                expected_total_chars: 3,
 748            },
 749            Case {
 750                name: "independent_edits_different_locations",
 751                original: indoc! {"
 752                    line1
 753                    line2
 754                    line3"},
 755                current: indoc! {"
 756                    LINE1
 757                    line2
 758                    line3"},
 759                predicted: indoc! {"
 760                    LINE1
 761                    line2
 762                    LINE3"},
 763                expected_reversal_chars: 0,
 764                expected_total_chars: 10,
 765            },
 766            Case {
 767                name: "no_history_edits",
 768                original: "same",
 769                current: "same",
 770                predicted: "different",
 771                expected_reversal_chars: 0,
 772                expected_total_chars: 13,
 773            },
 774            Case {
 775                name: "user_replaces_text_prediction_reverses",
 776                original: indoc! {"
 777                    keep
 778                    delete_me
 779                    keep2"},
 780                current: indoc! {"
 781                    keep
 782                    added
 783                    keep2"},
 784                predicted: indoc! {"
 785                    keep
 786                    delete_me
 787                    keep2"},
 788                expected_reversal_chars: 14,
 789                expected_total_chars: 14,
 790            },
 791            Case {
 792                name: "user_modifies_word_prediction_modifies_differently",
 793                original: "the quick brown fox",
 794                current: "the slow brown fox",
 795                predicted: "the fast brown fox",
 796                expected_reversal_chars: 4,
 797                expected_total_chars: 8,
 798            },
 799            Case {
 800                name: "user finishes function name (suffix)",
 801                original: "",
 802                current: "epr",
 803                predicted: "eprintln!()",
 804                expected_reversal_chars: 0,
 805                expected_total_chars: 8,
 806            },
 807            Case {
 808                name: "user starts function name (prefix)",
 809                original: "",
 810                current: "my_function()",
 811                predicted: "test_my_function()",
 812                expected_reversal_chars: 0,
 813                expected_total_chars: 5,
 814            },
 815            Case {
 816                name: "user types partial, prediction extends in multiple places",
 817                original: "",
 818                current: "test_my_function",
 819                predicted: "a_test_for_my_special_function_plz",
 820                expected_reversal_chars: 0,
 821                expected_total_chars: 18,
 822            },
 823            // Edge cases for subsequence matching
 824            Case {
 825                name: "subsequence with interleaved underscores",
 826                original: "",
 827                current: "a_b_c",
 828                predicted: "_a__b__c__",
 829                expected_reversal_chars: 0,
 830                expected_total_chars: 5,
 831            },
 832            Case {
 833                name: "not a subsequence - different characters",
 834                original: "",
 835                current: "abc",
 836                predicted: "xyz",
 837                expected_reversal_chars: 3,
 838                expected_total_chars: 6,
 839            },
 840            Case {
 841                name: "not a subsequence - wrong order",
 842                original: "",
 843                current: "abc",
 844                predicted: "cba",
 845                expected_reversal_chars: 3,
 846                expected_total_chars: 6,
 847            },
 848            Case {
 849                name: "partial subsequence - only some chars match",
 850                original: "",
 851                current: "abcd",
 852                predicted: "axbx",
 853                expected_reversal_chars: 4,
 854                expected_total_chars: 8,
 855            },
 856            // Common completion patterns
 857            Case {
 858                name: "completing a method call",
 859                original: "",
 860                current: "vec.pu",
 861                predicted: "vec.push(item)",
 862                expected_reversal_chars: 0,
 863                expected_total_chars: 8,
 864            },
 865            Case {
 866                name: "completing an import statement",
 867                original: "",
 868                current: "use std::col",
 869                predicted: "use std::collections::HashMap",
 870                expected_reversal_chars: 0,
 871                expected_total_chars: 17,
 872            },
 873            Case {
 874                name: "completing a struct field",
 875                original: "",
 876                current: "name: St",
 877                predicted: "name: String",
 878                expected_reversal_chars: 0,
 879                expected_total_chars: 4,
 880            },
 881            Case {
 882                name: "prediction replaces with completely different text",
 883                original: "",
 884                current: "hello",
 885                predicted: "world",
 886                expected_reversal_chars: 5,
 887                expected_total_chars: 10,
 888            },
 889            Case {
 890                name: "empty prediction removes user text",
 891                original: "",
 892                current: "mistake",
 893                predicted: "",
 894                expected_reversal_chars: 7,
 895                expected_total_chars: 7,
 896            },
 897            Case {
 898                name: "fixing typo is not reversal",
 899                original: "",
 900                current: "<dv",
 901                predicted: "<div>",
 902                expected_reversal_chars: 0,
 903                expected_total_chars: 2,
 904            },
 905            Case {
 906                name: "infix insertion not reversal",
 907                original: indoc! {"
 908                    from my_project import Foo
 909                "},
 910                current: indoc! {"
 911                    ifrom my_project import Foo
 912                "},
 913                predicted: indoc! {"
 914                    import
 915                    from my_project import Foo
 916                "},
 917                expected_reversal_chars: 0,
 918                expected_total_chars: 6,
 919            },
 920            Case {
 921                name: "non-word based reversal",
 922                original: "from",
 923                current: "ifrom",
 924                predicted: "from",
 925                expected_reversal_chars: 1,
 926                expected_total_chars: 1,
 927            },
 928            Case {
 929                name: "multiple insertions no reversal",
 930                original: "print(\"Hello, World!\")",
 931                current: "sys.(\"Hello, World!\")",
 932                predicted: "sys.stdout.write(\"Hello, World!\\n\")",
 933                expected_reversal_chars: 0,
 934                expected_total_chars: 14,
 935            },
 936        ];
 937
 938        for case in &cases {
 939            let overlap = compute_reversal_overlap(case.original, case.current, case.predicted);
 940            assert_eq!(
 941                overlap.chars_reversing_user_edits, case.expected_reversal_chars,
 942                "Test '{}': expected {} reversal chars, got {}",
 943                case.name, case.expected_reversal_chars, overlap.chars_reversing_user_edits
 944            );
 945            assert_eq!(
 946                overlap.total_chars_in_prediction, case.expected_total_chars,
 947                "Test '{}': expected {} total chars, got {}",
 948                case.name, case.expected_total_chars, overlap.total_chars_in_prediction
 949            );
 950        }
 951    }
 952
 953    #[test]
 954    fn test_reverse_diff() {
 955        let forward_diff = indoc! {"
 956            --- a/file.rs
 957            +++ b/file.rs
 958            @@ -1,3 +1,4 @@
 959             fn main() {
 960            +    let x = 42;
 961                 println!(\"hello\");
 962            }"};
 963
 964        let reversed = reverse_diff(forward_diff);
 965
 966        assert!(
 967            reversed.contains("+++ a/file.rs"),
 968            "Should have +++ for old path"
 969        );
 970        assert!(
 971            reversed.contains("--- b/file.rs"),
 972            "Should have --- for new path"
 973        );
 974        assert!(
 975            reversed.contains("-    let x = 42;"),
 976            "Added line should become deletion"
 977        );
 978        assert!(
 979            reversed.contains(" fn main()"),
 980            "Context lines should be unchanged"
 981        );
 982    }
 983
 984    #[test]
 985    fn test_reverse_diff_roundtrip() {
 986        // Applying a diff and then its reverse should get back to original
 987        let original = indoc! {"
 988            first line
 989            hello world
 990            last line
 991        "};
 992        let modified = indoc! {"
 993            first line
 994            hello beautiful world
 995            last line
 996        "};
 997
 998        // unified_diff doesn't include file headers, but apply_diff_to_string needs them
 999        let diff_body = language::unified_diff(original, modified);
1000        let forward_diff = format!("--- a/file\n+++ b/file\n{}", diff_body);
1001        let reversed_diff = reverse_diff(&forward_diff);
1002
1003        // Apply forward diff to original
1004        let after_forward = apply_diff_to_string(&forward_diff, original).unwrap();
1005        assert_eq!(after_forward, modified);
1006
1007        // Apply reversed diff to modified
1008        let after_reverse = apply_diff_to_string(&reversed_diff, &after_forward).unwrap();
1009        assert_eq!(after_reverse, original);
1010    }
1011
1012    #[test]
1013    fn test_filter_edit_history_by_path() {
1014        // Test that filter_edit_history_by_path correctly matches paths when
1015        // the edit history has paths with a repo prefix (e.g., "repo/src/file.rs")
1016        // but the cursor_path doesn't have the repo prefix (e.g., "src/file.rs")
1017        let events = vec![
1018            Arc::new(zeta_prompt::Event::BufferChange {
1019                path: Arc::from(Path::new("myrepo/src/file.rs")),
1020                old_path: Arc::from(Path::new("myrepo/src/file.rs")),
1021                diff: indoc! {"
1022                    @@ -1 +1 @@
1023                    -old
1024                    +new"}
1025                .into(),
1026                predicted: false,
1027                in_open_source_repo: true,
1028            }),
1029            Arc::new(zeta_prompt::Event::BufferChange {
1030                path: Arc::from(Path::new("myrepo/other.rs")),
1031                old_path: Arc::from(Path::new("myrepo/other.rs")),
1032                diff: indoc! {"
1033                    @@ -1 +1 @@
1034                    -a
1035                    +b"}
1036                .into(),
1037                predicted: false,
1038                in_open_source_repo: true,
1039            }),
1040            Arc::new(zeta_prompt::Event::BufferChange {
1041                path: Arc::from(Path::new("src/file.rs")),
1042                old_path: Arc::from(Path::new("src/file.rs")),
1043                diff: indoc! {"
1044                    @@ -1 +1 @@
1045                    -x
1046                    +y"}
1047                .into(),
1048                predicted: false,
1049                in_open_source_repo: true,
1050            }),
1051        ];
1052
1053        // "myrepo/src/file.rs" stripped -> "src/file.rs" matches cursor_path
1054        // "src/file.rs" exact match
1055        let cursor_path = Path::new("src/file.rs");
1056        let filtered = filter_edit_history_by_path(&events, cursor_path);
1057        assert_eq!(
1058            filtered.len(),
1059            2,
1060            "Should match myrepo/src/file.rs (stripped) and src/file.rs (exact)"
1061        );
1062
1063        // "myrepo/src/file.rs" stripped -> "src/file.rs" != "file.rs"
1064        // "src/file.rs" stripped -> "file.rs" == "file.rs"
1065        let cursor_path = Path::new("file.rs");
1066        let filtered = filter_edit_history_by_path(&events, cursor_path);
1067        assert_eq!(
1068            filtered.len(),
1069            1,
1070            "Should only match src/file.rs (stripped to file.rs)"
1071        );
1072
1073        // "myrepo/other.rs" stripped -> "other.rs" == "other.rs"
1074        let cursor_path = Path::new("other.rs");
1075        let filtered = filter_edit_history_by_path(&events, cursor_path);
1076        assert_eq!(filtered.len(), 1, "Should match only myrepo/other.rs");
1077    }
1078
1079    #[test]
1080    fn test_reverse_diff_preserves_trailing_newline() {
1081        let diff_with_trailing_newline = indoc! {"
1082            --- a/file
1083            +++ b/file
1084            @@ -1 +1 @@
1085            -old
1086            +new
1087        "};
1088        let reversed = reverse_diff(diff_with_trailing_newline);
1089        assert!(
1090            reversed.ends_with('\n'),
1091            "Reversed diff should preserve trailing newline"
1092        );
1093
1094        let diff_without_trailing_newline = indoc! {"
1095            --- a/file
1096            +++ b/file
1097            @@ -1 +1 @@
1098            -old
1099            +new"};
1100        let reversed = reverse_diff(diff_without_trailing_newline);
1101        assert!(
1102            !reversed.ends_with('\n'),
1103            "Reversed diff should not add trailing newline if original didn't have one"
1104        );
1105    }
1106
1107    #[test]
1108    fn test_filter_hunks_by_excerpt_region() {
1109        struct Case {
1110            name: &'static str,
1111            diff: &'static str,
1112            excerpt_start_row: u32,
1113            excerpt_row_count: u32,
1114            expected_filtered_diff: &'static str,
1115            expected_line_offset: i32,
1116        }
1117
1118        let cases = [
1119            Case {
1120                name: "hunk_entirely_before_excerpt",
1121                diff: indoc! {"
1122                    @@ -1,3 +1,4 @@
1123                     line1
1124                    +inserted
1125                     line2
1126                     line3
1127                "},
1128                excerpt_start_row: 10,
1129                excerpt_row_count: 5,
1130                expected_filtered_diff: "",
1131                expected_line_offset: 1,
1132            },
1133            Case {
1134                name: "hunk_entirely_inside_excerpt",
1135                diff: indoc! {"
1136                    @@ -12,3 +12,4 @@
1137                     line12
1138                    +inserted
1139                     line13
1140                     line14
1141                "},
1142                excerpt_start_row: 10,
1143                excerpt_row_count: 10,
1144                expected_filtered_diff: indoc! {"
1145                    @@ -2,3 +2,4 @@
1146                     line12
1147                    +inserted
1148                     line13
1149                     line14
1150                "},
1151                expected_line_offset: 1,
1152            },
1153            Case {
1154                name: "hunk_entirely_after_excerpt",
1155                diff: indoc! {"
1156                    @@ -50,3 +50,4 @@
1157                     line50
1158                    +inserted
1159                     line51
1160                     line52
1161                "},
1162                excerpt_start_row: 10,
1163                excerpt_row_count: 5,
1164                expected_filtered_diff: "",
1165                expected_line_offset: 0,
1166            },
1167            Case {
1168                name: "hunk_straddles_excerpt_start",
1169                diff: indoc! {"
1170                    @@ -8,5 +8,6 @@
1171                     line8
1172                     line9
1173                    +inserted
1174                     line10
1175                     line11
1176                     line12
1177                "},
1178                excerpt_start_row: 10,
1179                excerpt_row_count: 10,
1180                expected_filtered_diff: indoc! {"
1181                    @@ -1,3 +1,3 @@
1182                     line10
1183                     line11
1184                     line12
1185                "},
1186                expected_line_offset: 1,
1187            },
1188            Case {
1189                name: "hunk_straddles_excerpt_end",
1190                diff: indoc! {"
1191                    @@ -18,5 +18,6 @@
1192                     line18
1193                     line19
1194                    +inserted
1195                     line20
1196                     line21
1197                     line22
1198                "},
1199                excerpt_start_row: 10,
1200                excerpt_row_count: 10,
1201                expected_filtered_diff: indoc! {"
1202                    @@ -8,2 +8,3 @@
1203                     line18
1204                     line19
1205                    +inserted
1206                "},
1207                expected_line_offset: 1,
1208            },
1209            Case {
1210                name: "multiple_hunks_mixed",
1211                diff: indoc! {"
1212                    @@ -1,2 +1,3 @@
1213                     line1
1214                    +before_excerpt
1215                     line2
1216                    @@ -12,2 +13,3 @@
1217                     line12
1218                    +inside_excerpt
1219                     line13
1220                    @@ -50,2 +52,3 @@
1221                     line50
1222                    +after_excerpt
1223                     line51
1224                "},
1225                excerpt_start_row: 10,
1226                excerpt_row_count: 10,
1227                expected_filtered_diff: indoc! {"
1228                    @@ -3,2 +3,3 @@
1229                     line12
1230                    +inside_excerpt
1231                     line13
1232                "},
1233                expected_line_offset: 2,
1234            },
1235            Case {
1236                name: "deletion_before_excerpt",
1237                diff: indoc! {"
1238                    @@ -1,4 +1,3 @@
1239                     line1
1240                    -deleted
1241                     line2
1242                     line3
1243                "},
1244                excerpt_start_row: 10,
1245                excerpt_row_count: 5,
1246                expected_filtered_diff: "",
1247                expected_line_offset: -1,
1248            },
1249            Case {
1250                name: "deletion_inside_excerpt",
1251                diff: indoc! {"
1252                    @@ -12,4 +12,3 @@
1253                     line12
1254                    -deleted
1255                     line13
1256                     line14
1257                "},
1258                excerpt_start_row: 10,
1259                excerpt_row_count: 10,
1260                expected_filtered_diff: indoc! {"
1261                    @@ -2,4 +2,3 @@
1262                     line12
1263                    -deleted
1264                     line13
1265                     line14
1266                "},
1267                expected_line_offset: -1,
1268            },
1269            Case {
1270                name: "empty_diff",
1271                diff: "",
1272                excerpt_start_row: 10,
1273                excerpt_row_count: 5,
1274                expected_filtered_diff: "",
1275                expected_line_offset: 0,
1276            },
1277            Case {
1278                name: "hunk_spans_entire_excerpt",
1279                diff: indoc! {"
1280                    @@ -8,10 +8,12 @@
1281                     line8
1282                     line9
1283                     line10
1284                     line11
1285                    +inserted1
1286                     line12
1287                     line13
1288                    +inserted2
1289                     line14
1290                     line15
1291                     line16
1292                     line17
1293                "},
1294                excerpt_start_row: 10,
1295                excerpt_row_count: 5,
1296                expected_filtered_diff: indoc! {"
1297                    @@ -1,3 +1,5 @@
1298                     line11
1299                    +inserted1
1300                     line12
1301                     line13
1302                    +inserted2
1303                "},
1304                expected_line_offset: 2,
1305            },
1306            Case {
1307                name: "replacement_inside_excerpt",
1308                diff: indoc! {"
1309                    @@ -12,3 +12,3 @@
1310                     line12
1311                    -old_text
1312                    +new_text
1313                     line14
1314                "},
1315                excerpt_start_row: 10,
1316                excerpt_row_count: 10,
1317                expected_filtered_diff: indoc! {"
1318                    @@ -2,3 +2,3 @@
1319                     line12
1320                    -old_text
1321                    +new_text
1322                     line14
1323                "},
1324                expected_line_offset: 0,
1325            },
1326        ];
1327
1328        for case in &cases {
1329            let (filtered, line_offset) = filter_diff_hunks_by_excerpt(
1330                case.diff,
1331                case.excerpt_start_row,
1332                case.excerpt_row_count,
1333            );
1334            assert_eq!(
1335                filtered, case.expected_filtered_diff,
1336                "Test '{}': filtered diff mismatch.\nExpected:\n{}\nGot:\n{}",
1337                case.name, case.expected_filtered_diff, filtered
1338            );
1339            assert_eq!(
1340                line_offset, case.expected_line_offset,
1341                "Test '{}': line offset mismatch. Expected {}, got {}",
1342                case.name, case.expected_line_offset, line_offset
1343            );
1344        }
1345    }
1346
1347    #[test]
1348    fn test_excerpt_aware_reversal_tracking() {
1349        struct Case {
1350            name: &'static str,
1351            edit_history_diffs: Vec<&'static str>,
1352            excerpt_content: &'static str,
1353            excerpt_start_row: u32,
1354            predicted_content: &'static str,
1355            expected_reversal_chars: usize,
1356            expected_total_chars: usize,
1357        }
1358
1359        let cases = [
1360            Case {
1361                name: "edit_outside_excerpt_no_reversal",
1362                edit_history_diffs: vec![indoc! {"
1363                    @@ -1,2 +1,3 @@
1364                     line1
1365                    +added_outside
1366                     line2
1367                "}],
1368                excerpt_content: indoc! {"
1369                    line10
1370                    line11
1371                    line12
1372                "},
1373                excerpt_start_row: 10,
1374                predicted_content: indoc! {"
1375                    line10
1376                    modified
1377                    line12
1378                "},
1379                expected_reversal_chars: 0,
1380                expected_total_chars: 14,
1381            },
1382            Case {
1383                name: "edit_inside_excerpt_with_reversal",
1384                edit_history_diffs: vec![indoc! {"
1385                    @@ -10,3 +10,4 @@
1386                     line10
1387                    +user_added
1388                     line11
1389                     line12
1390                "}],
1391                excerpt_content: indoc! {"
1392                    line10
1393                    user_added
1394                    line11
1395                    line12
1396                "},
1397                excerpt_start_row: 10,
1398                predicted_content: indoc! {"
1399                    line10
1400                    line11
1401                    line12
1402                "},
1403                expected_reversal_chars: 11,
1404                expected_total_chars: 11,
1405            },
1406            Case {
1407                name: "straddling_edit_partial_reversal",
1408                edit_history_diffs: vec![indoc! {"
1409                    @@ -8,6 +8,8 @@
1410                     line8
1411                     line9
1412                    +before_excerpt
1413                     line10
1414                    +inside_excerpt
1415                     line11
1416                     line12
1417                     line13
1418                "}],
1419                excerpt_content: indoc! {"
1420                    line10
1421                    inside_excerpt
1422                    line11
1423                    line12
1424                    line13
1425                "},
1426                excerpt_start_row: 10,
1427                predicted_content: indoc! {"
1428                    line10
1429                    line11
1430                    line12
1431                    line13
1432                "},
1433                expected_reversal_chars: 15,
1434                expected_total_chars: 15,
1435            },
1436            Case {
1437                name: "multiple_edits_mixed_locations",
1438                edit_history_diffs: vec![
1439                    indoc! {"
1440                        @@ -1,2 +1,3 @@
1441                         line1
1442                        +outside1
1443                         line2
1444                    "},
1445                    indoc! {"
1446                        @@ -11,2 +12,3 @@
1447                         line11
1448                        +inside1
1449                         line12
1450                    "},
1451                ],
1452                excerpt_content: indoc! {"
1453                    line10
1454                    line11
1455                    inside1
1456                    line12
1457                    line13
1458                "},
1459                excerpt_start_row: 10,
1460                predicted_content: indoc! {"
1461                    line10
1462                    line11
1463                    line12
1464                    line13
1465                "},
1466                expected_reversal_chars: 8,
1467                expected_total_chars: 8,
1468            },
1469            Case {
1470                name: "no_edit_history",
1471                edit_history_diffs: vec![],
1472                excerpt_content: indoc! {"
1473                    line10
1474                    line11
1475                    line12
1476                "},
1477                excerpt_start_row: 10,
1478                predicted_content: indoc! {"
1479                    line10
1480                    modified
1481                    line12
1482                "},
1483                expected_reversal_chars: 0,
1484                expected_total_chars: 14,
1485            },
1486            Case {
1487                name: "edit_after_excerpt_no_effect",
1488                edit_history_diffs: vec![indoc! {"
1489                    @@ -50,2 +50,3 @@
1490                     line50
1491                    +added_after
1492                     line51
1493                "}],
1494                excerpt_content: indoc! {"
1495                    line10
1496                    line11
1497                    line12
1498                "},
1499                excerpt_start_row: 10,
1500                predicted_content: indoc! {"
1501                    line10
1502                    changed
1503                    line12
1504                "},
1505                expected_reversal_chars: 0,
1506                expected_total_chars: 13,
1507            },
1508            Case {
1509                name: "line_offset_tracking_across_hunks",
1510                edit_history_diffs: vec![
1511                    indoc! {"
1512                        @@ -1,2 +1,4 @@
1513                         line1
1514                        +added1
1515                        +added2
1516                         line2
1517                    "},
1518                    indoc! {"
1519                        @@ -12,2 +14,3 @@
1520                         line12
1521                        +inside_after_offset
1522                         line13
1523                    "},
1524                ],
1525                excerpt_content: indoc! {"
1526                    line10
1527                    line11
1528                    line12
1529                    inside_after_offset
1530                    line13
1531                "},
1532                excerpt_start_row: 10,
1533                predicted_content: indoc! {"
1534                    line10
1535                    line11
1536                    line12
1537                    line13
1538                "},
1539                expected_reversal_chars: 20,
1540                expected_total_chars: 20,
1541            },
1542        ];
1543
1544        for case in &cases {
1545            let overlap = compute_excerpt_aware_reversal_overlap(
1546                &case.edit_history_diffs,
1547                case.excerpt_content,
1548                case.excerpt_start_row,
1549                case.predicted_content,
1550            );
1551            assert_eq!(
1552                overlap.chars_reversing_user_edits, case.expected_reversal_chars,
1553                "Test '{}': expected {} reversal chars, got {}",
1554                case.name, case.expected_reversal_chars, overlap.chars_reversing_user_edits
1555            );
1556            assert_eq!(
1557                overlap.total_chars_in_prediction, case.expected_total_chars,
1558                "Test '{}': expected {} total chars, got {}",
1559                case.name, case.expected_total_chars, overlap.total_chars_in_prediction
1560            );
1561        }
1562    }
1563
1564    #[test]
1565    fn test_lenient_diff_application() {
1566        struct Case {
1567            name: &'static str,
1568            diff: &'static str,
1569            content: &'static str,
1570            expected_result: &'static str,
1571        }
1572
1573        let cases = [
1574            Case {
1575                name: "hunk_context_not_found_skipped",
1576                diff: indoc! {"
1577                    @@ -1,3 +1,4 @@
1578                     context_not_in_content
1579                    +added_line
1580                     more_context
1581                     final_context
1582                "},
1583                content: indoc! {"
1584                    completely
1585                    different
1586                    content
1587                "},
1588                expected_result: indoc! {"
1589                    completely
1590                    different
1591                    content
1592                "},
1593            },
1594            Case {
1595                name: "hunk_context_found_applied",
1596                diff: indoc! {"
1597                    @@ -1,3 +1,4 @@
1598                     line1
1599                    +inserted
1600                     line2
1601                     line3
1602                "},
1603                content: indoc! {"
1604                    line1
1605                    line2
1606                    line3
1607                "},
1608                expected_result: indoc! {"
1609                    line1
1610                    inserted
1611                    line2
1612                    line3
1613                "},
1614            },
1615            Case {
1616                name: "multiple_hunks_partial_match",
1617                diff: indoc! {"
1618                    @@ -1,2 +1,3 @@
1619                     not_found
1620                    +skipped
1621                     also_not_found
1622                    @@ -5,2 +6,3 @@
1623                     line5
1624                    +applied
1625                     line6
1626                "},
1627                content: indoc! {"
1628                    line1
1629                    line2
1630                    line3
1631                    line4
1632                    line5
1633                    line6
1634                "},
1635                expected_result: indoc! {"
1636                    line1
1637                    line2
1638                    line3
1639                    line4
1640                    line5
1641                    applied
1642                    line6
1643                "},
1644            },
1645            Case {
1646                name: "empty_diff",
1647                diff: "",
1648                content: indoc! {"
1649                    unchanged
1650                    content
1651                "},
1652                expected_result: indoc! {"
1653                    unchanged
1654                    content
1655                "},
1656            },
1657        ];
1658
1659        for case in &cases {
1660            let result = apply_diff_to_string_lenient(case.diff, case.content);
1661            assert_eq!(
1662                result, case.expected_result,
1663                "Test '{}': expected:\n{}\ngot:\n{}",
1664                case.name, case.expected_result, result
1665            );
1666        }
1667    }
1668
1669    #[test]
1670    fn test_unicode_reversal_overlap() {
1671        struct Case {
1672            name: &'static str,
1673            original: &'static str,
1674            current: &'static str,
1675            predicted: &'static str,
1676            expected_reversal_chars: usize,
1677            expected_total_chars: usize,
1678        }
1679
1680        let cases = [
1681            Case {
1682                name: "unicode_extension_cjk",
1683                original: "",
1684                current: "",       // 1 char
1685                predicted: "日本語", // 3 chars, adds 2 chars
1686                expected_reversal_chars: 0,
1687                expected_total_chars: 2, // "本語" = 2 chars added
1688            },
1689            Case {
1690                name: "unicode_extension_emoji",
1691                original: "",
1692                current: "🎉",       // 1 char
1693                predicted: "🎉🎊🎈", // 3 chars, adds 2 chars
1694                expected_reversal_chars: 0,
1695                expected_total_chars: 2, // "🎊🎈" = 2 chars added
1696            },
1697            Case {
1698                name: "unicode_deletion_restored",
1699                original: "héllo wörld",    // 11 chars
1700                current: "héllo",           // 5 chars
1701                predicted: "héllo wörld",   // restores " wörld" = 6 chars
1702                expected_reversal_chars: 6, // LCS(" wörld", " wörld") = 6 chars
1703                expected_total_chars: 6,
1704            },
1705            Case {
1706                name: "unicode_addition_reversed",
1707                original: "café",           // 4 chars
1708                current: "café latté",      // 10 chars, added " latté" = 6 chars
1709                predicted: "café",          // removes " latté"
1710                expected_reversal_chars: 6, // 6 chars removed
1711                expected_total_chars: 6,
1712            },
1713            Case {
1714                name: "mixed_ascii_unicode",
1715                original: "",
1716                current: "test日本",         // 6 chars
1717                predicted: "test日本語です", // 9 chars
1718                expected_reversal_chars: 0,
1719                expected_total_chars: 3, // 3 new chars after subsequence normalization
1720            },
1721            Case {
1722                name: "unicode_replacement_not_subsequence",
1723                original: "",
1724                current: "日本",            // 2 chars
1725                predicted: "中国",          // 2 chars, different
1726                expected_reversal_chars: 2, // removes "日本" = 2 chars
1727                expected_total_chars: 4,    // 2 removed + 2 added
1728            },
1729        ];
1730
1731        for case in &cases {
1732            let overlap = compute_reversal_overlap(case.original, case.current, case.predicted);
1733            assert_eq!(
1734                overlap.chars_reversing_user_edits, case.expected_reversal_chars,
1735                "Test '{}': expected {} reversal chars, got {}",
1736                case.name, case.expected_reversal_chars, overlap.chars_reversing_user_edits
1737            );
1738            assert_eq!(
1739                overlap.total_chars_in_prediction, case.expected_total_chars,
1740                "Test '{}': expected {} total chars, got {}",
1741                case.name, case.expected_total_chars, overlap.total_chars_in_prediction
1742            );
1743        }
1744    }
1745
1746    #[test]
1747    fn test_compute_lcs_length() {
1748        assert_eq!(compute_lcs_length("", ""), 0);
1749        assert_eq!(compute_lcs_length("abc", ""), 0);
1750        assert_eq!(compute_lcs_length("", "abc"), 0);
1751        assert_eq!(compute_lcs_length("abc", "abc"), 3);
1752        assert_eq!(compute_lcs_length("abc", "def"), 0);
1753        assert_eq!(compute_lcs_length("abcdef", "ace"), 3);
1754        assert_eq!(compute_lcs_length("AGGTAB", "GXTXAYB"), 4);
1755        assert_eq!(compute_lcs_length("日本語", "日語"), 2);
1756    }
1757
1758    #[test]
1759    fn test_compute_prediction_reversal_ratio_full_file() {
1760        let prompt_inputs = make_test_prompt_inputs(
1761            indoc! {"
1762                line1
1763                user_added
1764                line2
1765            "},
1766            vec![Arc::new(zeta_prompt::Event::BufferChange {
1767                path: Arc::from(Path::new("src/test.rs")),
1768                old_path: Arc::from(Path::new("src/test.rs")),
1769                diff: indoc! {"
1770                    @@ -1,2 +1,3 @@
1771                     line1
1772                    +user_added
1773                     line2
1774                "}
1775                .into(),
1776                predicted: false,
1777                in_open_source_repo: false,
1778            })],
1779            None,
1780        );
1781
1782        let predicted = indoc! {"
1783            line1
1784            line2
1785        "};
1786        let ratio =
1787            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
1788
1789        assert!(
1790            ratio > 0.9,
1791            "Expected high reversal ratio when prediction removes user addition, got {}",
1792            ratio
1793        );
1794    }
1795
1796    #[test]
1797    fn test_compute_prediction_reversal_ratio_with_excerpt() {
1798        let prompt_inputs = make_test_prompt_inputs(
1799            indoc! {"
1800                line10
1801                user_added
1802                line11
1803            "},
1804            vec![Arc::new(zeta_prompt::Event::BufferChange {
1805                path: Arc::from(Path::new("src/test.rs")),
1806                old_path: Arc::from(Path::new("src/test.rs")),
1807                diff: indoc! {"
1808                    @@ -10,2 +10,3 @@
1809                     line10
1810                    +user_added
1811                     line11
1812                "}
1813                .into(),
1814                predicted: false,
1815                in_open_source_repo: false,
1816            })],
1817            Some(10),
1818        );
1819
1820        let predicted = indoc! {"
1821            line10
1822            line11
1823        "};
1824        let ratio =
1825            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
1826
1827        assert!(
1828            ratio > 0.9,
1829            "Expected high reversal ratio for excerpt-aware computation, got {}",
1830            ratio
1831        );
1832    }
1833
1834    #[test]
1835    fn test_compute_prediction_reversal_ratio_no_history() {
1836        let prompt_inputs = make_test_prompt_inputs(
1837            indoc! {"
1838                original content
1839            "},
1840            vec![],
1841            None,
1842        );
1843
1844        let predicted = indoc! {"
1845            completely different
1846        "};
1847        let ratio =
1848            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
1849
1850        assert_eq!(
1851            ratio, 0.0,
1852            "Expected zero reversal ratio with no edit history"
1853        );
1854    }
1855
1856    #[test]
1857    fn test_compute_prediction_reversal_ratio_path_filtering() {
1858        let prompt_inputs = make_test_prompt_inputs(
1859            indoc! {"
1860                line1
1861                user_added
1862                line2
1863            "},
1864            vec![Arc::new(zeta_prompt::Event::BufferChange {
1865                path: Arc::from(Path::new("src/other.rs")),
1866                old_path: Arc::from(Path::new("src/other.rs")),
1867                diff: indoc! {"
1868                    @@ -1,2 +1,3 @@
1869                     line1
1870                    +user_added
1871                     line2
1872                "}
1873                .into(),
1874                predicted: false,
1875                in_open_source_repo: false,
1876            })],
1877            None,
1878        );
1879
1880        let predicted = indoc! {"
1881            line1
1882            line2
1883        "};
1884        let ratio =
1885            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
1886
1887        assert_eq!(
1888            ratio, 0.0,
1889            "Expected zero reversal when edit history is for different file"
1890        );
1891    }
1892
1893    #[test]
1894    fn test_compute_prediction_reversal_ratio_lenient_fallback() {
1895        let prompt_inputs = make_test_prompt_inputs(
1896            indoc! {"
1897                actual_line1
1898                user_added
1899                actual_line2
1900            "},
1901            vec![Arc::new(zeta_prompt::Event::BufferChange {
1902                path: Arc::from(Path::new("src/test.rs")),
1903                old_path: Arc::from(Path::new("src/test.rs")),
1904                diff: indoc! {"
1905                    @@ -1,2 +1,3 @@
1906                     wrong_context
1907                    +user_added
1908                     more_wrong
1909                "}
1910                .into(),
1911                predicted: false,
1912                in_open_source_repo: false,
1913            })],
1914            None,
1915        );
1916
1917        let predicted = indoc! {"
1918            actual_line1
1919            actual_line2
1920        "};
1921        let ratio =
1922            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
1923
1924        assert!(
1925            ratio >= 0.0 && ratio <= 1.0,
1926            "Ratio should be valid even with lenient fallback, got {}",
1927            ratio
1928        );
1929    }
1930
1931    #[test]
1932    fn test_excerpt_aware_reversal_error_recovery() {
1933        let diffs = vec![indoc! {"
1934            @@ -1,2 +1,3 @@
1935             nonexistent_context
1936            +added
1937             more_nonexistent
1938        "}];
1939        let excerpt_content = indoc! {"
1940            completely
1941            different
1942            content
1943        "};
1944        let predicted_content = indoc! {"
1945            completely
1946            modified
1947            content
1948        "};
1949
1950        let overlap =
1951            compute_excerpt_aware_reversal_overlap(&diffs, excerpt_content, 0, predicted_content);
1952
1953        assert!(
1954            overlap.ratio() >= 0.0 && overlap.ratio() <= 1.0,
1955            "Should handle failed diff application gracefully"
1956        );
1957    }
1958
1959    #[test]
1960    fn test_only_most_recent_edit_tracked() {
1961        let prompt_inputs = make_test_prompt_inputs(
1962            indoc! {"
1963                line1
1964                first_add
1965                second_add
1966                line2
1967            "},
1968            vec![
1969                Arc::new(zeta_prompt::Event::BufferChange {
1970                    path: Arc::from(Path::new("src/test.rs")),
1971                    old_path: Arc::from(Path::new("src/test.rs")),
1972                    diff: indoc! {"
1973                        @@ -1,2 +1,3 @@
1974                         line1
1975                        +first_add
1976                         line2
1977                    "}
1978                    .into(),
1979                    predicted: false,
1980                    in_open_source_repo: false,
1981                }),
1982                Arc::new(zeta_prompt::Event::BufferChange {
1983                    path: Arc::from(Path::new("src/test.rs")),
1984                    old_path: Arc::from(Path::new("src/test.rs")),
1985                    diff: indoc! {"
1986                        @@ -2,2 +2,3 @@
1987                         first_add
1988                        +second_add
1989                         line2
1990                    "}
1991                    .into(),
1992                    predicted: false,
1993                    in_open_source_repo: false,
1994                }),
1995            ],
1996            None,
1997        );
1998
1999        let predicted = indoc! {"
2000            line1
2001            first_add
2002            line2
2003        "};
2004        let ratio =
2005            compute_prediction_reversal_ratio(&prompt_inputs, predicted, Path::new("src/test.rs"));
2006
2007        assert!(
2008            ratio > 0.9,
2009            "Expected high reversal ratio when prediction exactly reverses the most recent edit, got {}",
2010            ratio
2011        );
2012    }
2013}