{"record":{"id":"99fadb8bbcc7c3e0","repo":"Hmbown/CodeWhale","slug":"patch-context-not-found-in","errorCode":null,"errorMessage":"patch context not found in {}: {}","messagePattern":"patch context not found in (.+?): (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/eval.rs","lineNumber":720,"sourceCode":"        }\n        if raw_line.starts_with(\"*** \") {\n            return Err(anyhow!(\"unexpected patch directive: {raw_line}\"));\n        }\n        if raw_line.starts_with(\"@@\") {\n            continue;\n        }\n\n        let (kind, rest) = raw_line.split_at(1);\n        let content = rest.to_string();\n\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            \"+\" => {","sourceCodeStart":702,"sourceCodeEnd":738,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/eval.rs#L702-L738","documentation":"When applying a context line (` ` prefix) in a patch hunk, apply_patch scans the remaining file lines for an exact match; if the context line is not found from the current cursor onward, it fails reporting the file and the unmatched line. The patch cannot be located within the current file contents.","triggerScenarios":"A ` ` context line (or `-` removal line's context) does not appear verbatim in the target file from the current position — the file content differs from what the patch assumed.","commonSituations":"File was already edited (by an earlier patch or the model) so context drifted; whitespace/indentation mismatch; patch generated against a different file version; trailing whitespace or CRLF differences.","solutions":["Regenerate the patch against the file's current contents.","Verify exact whitespace/indentation of the context line; it must match byte-for-byte.","Check the file hasn't been modified by an earlier step in the same eval run; reorder or refresh the patch.","Normalize line endings (CRLF vs LF) in both the file and the patch."],"exampleFix":"// before: context line doesn't match file\n    fn main() {\n// after: match the actual file text exactly (e.g. 4-space indent)\n    fn main() {","handlingStrategy":"try-catch","validationCode":"// pre-check: every ' ' context line must appear in the current file\nlet file = std::fs::read_to_string(&target)?;\nfor line in patch.lines().filter(|l| l.starts_with(' ')) {\n    let ctx = &line[1..];\n    if !file.contains(ctx) {\n        eprintln!(\"context line not in current file: {ctx:?}\");\n    }\n}","typeGuard":null,"tryCatchPattern":"match apply_patch(&root, patch) {\n    Err(e) if e.to_string().starts_with(\"patch context not found\") => {\n        eprintln!(\"file drifted from patch assumptions; regenerate the patch against current contents\");\n    }\n    other => other?,\n}","preventionTips":["Regenerate patches immediately before applying; don't cache them across edits.","Copy context lines verbatim from the current file, including indentation.","Normalize line endings between patch and file.","Apply patches sequentially and re-read files between applications."],"tags":["patch","apply-patch","eval","mismatch"],"backgroundTag":"invalid-argument-value","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"}