Hmbown/CodeWhale · error · anyhow::Error

patch removal mismatch in

Error message

patch removal mismatch in {}: expected '{}'

What it means

The TUI's eval patch applier parses a unified-diff-like patch and replays it against the target file's lines. A '-' removal line must exactly equal the line currently under the cursor in the working file; if it doesn't, the file content has diverged from what the patch expects, so the applier aborts with this message rather than removing the wrong text.

Solutions

  1. Re-read the current file and regenerate the patch from its actual contents before applying.
  2. Diff the '-' lines of the patch against the current file to find the mismatching line and fix the patch text (whitespace, line endings, stale context).
  3. If the file changed intentionally, reject or re-request the patch rather than forcing it; the applier has no fuzzy matching.

Example fix

// before (patch expects old text)
-    let x = compute_old();
// after (regenerated from current file)
-    let x = compute_new();
Defensive patterns

Strategy: validation

Validate before calling

fn patch_removals_match(file: &str, patch_lines: &[String]) -> bool {
    let file_lines: Vec<&str> = file.lines().collect();
    let mut cursor = 0usize;
    for l in patch_lines {
        if let Some(content) = l.strip_prefix('-') {
            if cursor >= file_lines.len() || file_lines[cursor] != content { return false; }
            cursor += 0; // removal does not advance
        } else if l.starts_with('+') {
            cursor += 1;
        } else {
            cursor += 1;
        }
    }
    true
}

Try / catch

match apply_patch(path, &patch) {
    Ok(updated) => write_file(path, updated)?,
    Err(e) if e.to_string().contains("patch removal mismatch") => {
        let fresh = read_file(path)?;
        let regenerated = regenerate_patch(&fresh, intent)?;
        write_file(path, apply_patch(path, &regenerated)?)?;
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Applying an eval patch containing a '-' line whose `content` does not match `file_lines[cursor]` — i.e. the file at `file_path` no longer contains the expected text at the removal position (edited, reordered, or already partially patched).

Common situations: The model generated a patch against stale file contents; the user edited the file between patch generation and application; a prior patch in the same run already removed/inserted lines shifting positions; CRLF or trailing-whitespace differences make lines unequal.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/f2025c25fbc3b294. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/eval.rs:730

        match kind {
            " " => {
                let Some(found) = file_lines[cursor..]
                    .iter()
                    .position(|line| line == &content)
                    .map(|offset| cursor + offset)
                else {
                    return Err(anyhow!(
                        "patch context not found in {}: {}",
                        file_path.display(),
                        content
                    ));
                };
                cursor = found + 1;
            }
            "-" => {
                if cursor >= file_lines.len() || file_lines[cursor] != content {
                    return Err(anyhow!(
                        "patch removal mismatch in {}: expected '{}'",
                        file_path.display(),
                        content
                    ));
                }
                file_lines.remove(cursor);
            }
            "+" => {
                file_lines.insert(cursor, content);
                cursor += 1;
            }
            _ => return Err(anyhow!("unsupported patch line: {raw_line}")),
        }
    }

    let mut updated = file_lines.join("\n");
    if had_trailing_newline {
        updated.push('\n');

View on GitHub (pinned to 73e0f67d83)