{"record":{"id":"f2025c25fbc3b294","repo":"Hmbown/CodeWhale","slug":"patch-removal-mismatch-in-expected","errorCode":null,"errorMessage":"patch removal mismatch in {}: expected '{}'","messagePattern":"patch removal mismatch in (.+?): expected '(.+?)'","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/eval.rs","lineNumber":730,"sourceCode":"\n        match kind {\n            \" \" => {\n                let Some(found) = file_lines[cursor..]\n                    .iter()\n                    .position(|line| line == &content)\n                    .map(|offset| cursor + offset)\n                else {\n                    return Err(anyhow!(\n                        \"patch context not found in {}: {}\",\n                        file_path.display(),\n                        content\n                    ));\n                };\n                cursor = found + 1;\n            }\n            \"-\" => {\n                if cursor >= file_lines.len() || file_lines[cursor] != content {\n                    return Err(anyhow!(\n                        \"patch removal mismatch in {}: expected '{}'\",\n                        file_path.display(),\n                        content\n                    ));\n                }\n                file_lines.remove(cursor);\n            }\n            \"+\" => {\n                file_lines.insert(cursor, content);\n                cursor += 1;\n            }\n            _ => return Err(anyhow!(\"unsupported patch line: {raw_line}\")),\n        }\n    }\n\n    let mut updated = file_lines.join(\"\\n\");\n    if had_trailing_newline {\n        updated.push('\\n');","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/eval.rs#L712-L748","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Re-read the current file and regenerate the patch from its actual contents before applying.","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).","If the file changed intentionally, reject or re-request the patch rather than forcing it; the applier has no fuzzy matching."],"exampleFix":"// before (patch expects old text)\n-    let x = compute_old();\n// after (regenerated from current file)\n-    let x = compute_new();","handlingStrategy":"validation","validationCode":"fn patch_removals_match(file: &str, patch_lines: &[String]) -> bool {\n    let file_lines: Vec<&str> = file.lines().collect();\n    let mut cursor = 0usize;\n    for l in patch_lines {\n        if let Some(content) = l.strip_prefix('-') {\n            if cursor >= file_lines.len() || file_lines[cursor] != content { return false; }\n            cursor += 0; // removal does not advance\n        } else if l.starts_with('+') {\n            cursor += 1;\n        } else {\n            cursor += 1;\n        }\n    }\n    true\n}","typeGuard":null,"tryCatchPattern":"match apply_patch(path, &patch) {\n    Ok(updated) => write_file(path, updated)?,\n    Err(e) if e.to_string().contains(\"patch removal mismatch\") => {\n        let fresh = read_file(path)?;\n        let regenerated = regenerate_patch(&fresh, intent)?;\n        write_file(path, apply_patch(path, &regenerated)?)?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Always generate patches from a freshly read snapshot of the file.","Apply patches immediately after generation; never let user edits intervene.","Normalize line endings (CRLF vs LF) before comparing lines."],"tags":["patch","diff-apply","text-mismatch","rust"],"backgroundTag":"patch-removal-mismatch","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}