Hmbown/CodeWhale · error

patch context not found in {}: {}

Error message

patch context not found in {}: {}

What it means

A context line (leading space) must match some line of the target file at or after the current cursor (eval.rs:720). When no such line exists the patch is stale for the current file content and is rejected with the file path and the unmatched line, before any write.

Source

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

        }
        if raw_line.starts_with("*** ") {
            return Err(anyhow!("unexpected patch directive: {raw_line}"));
        }
        if raw_line.starts_with("@@") {
            continue;
        }

        let (kind, rest) = raw_line.split_at(1);
        let content = rest.to_string();

        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);
            }
            "+" => {

View on GitHub (pinned to 8880682c63)

Solutions

  1. Re-read the current file and regenerate the patch from its actual lines
  2. Apply patches strictly one at a time, re-reading the file between attempts
  3. Normalize line endings and preserve exact whitespace in context lines
Defensive patterns

Strategy: retry

Validate before calling

let original = std::fs::read_to_string(&file_path)?;
for ctx in patch_context_lines(&patch) {
    anyhow::ensure!(
        original.lines().any(|l| l == ctx),
        "context not present in current file: {ctx}"
    );
}

Try / catch

let mut attempt = patch.clone();
loop {
    match apply_patch(&root, &attempt) {
        Ok(()) => break,
        Err(err) if err.to_string().contains("patch context not found") => {
            attempt = regenerate_patch_against_current_file(&file_path, &edit)?;
        }
        Err(err) => return Err(err),
    }
}

Prevention

When it happens

Trigger: The file changed since the patch was generated (another patch or hand edit landed first); the context line's whitespace differs (CRLF, trailing spaces); the patch was built against a different version of the file; earlier hunks advanced the cursor past the context.

Common situations: Concurrent edits to the same file; model hallucinates file content; line-ending conversion between environments.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/99fadb8bbcc7c3e0. Report an issue: GitHub.