{"record":{"id":"ddeece286c656942","repo":"openai/codex","slug":"invalid-hunk-at-line-line-number-message","errorCode":null,"errorMessage":"invalid hunk at line {line_number}, {message}","messagePattern":"invalid hunk at line (.+?), (.+?)","errorType":"exception","errorClass":"ParseError","httpStatus":null,"severity":"error","filePath":"codex-rs/apply-patch/src/parser.rs","lineNumber":59,"sourceCode":"pub(crate) const UPDATE_FILE_MARKER: &str = \"*** Update File: \";\npub(crate) const MOVE_TO_MARKER: &str = \"*** Move to: \";\npub(crate) const EOF_MARKER: &str = \"*** End of File\";\npub(crate) const CHANGE_CONTEXT_MARKER: &str = \"@@ \";\npub(crate) const EMPTY_CHANGE_CONTEXT_MARKER: &str = \"@@\";\n\n/// Currently, the only OpenAI model that knowingly requires lenient parsing is\n/// gpt-4.1. While we could try to require everyone to pass in a strictness\n/// param when invoking apply_patch, it is a pain to thread it through all of\n/// the call sites, so we resign ourselves allowing lenient parsing for all\n/// models. See [`ParseMode::Lenient`] for details on the exceptions we make for\n/// gpt-4.1.\nconst PARSE_IN_STRICT_MODE: bool = false;\n\n#[derive(Debug, PartialEq, Error, Clone)]\npub enum ParseError {\n    #[error(\"invalid patch: {0}\")]\n    InvalidPatchError(String),\n    #[error(\"invalid hunk at line {line_number}, {message}\")]\n    InvalidHunkError { message: String, line_number: usize },\n}\nuse ParseError::*;\n\n#[derive(Debug, PartialEq, Clone)]\n#[allow(clippy::enum_variant_names)]\npub enum Hunk {\n    AddFile {\n        path: PathBuf,\n        contents: String,\n    },\n    DeleteFile {\n        path: PathBuf,\n    },\n    UpdateFile {\n        path: PathBuf,\n        move_path: Option<PathBuf>,\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/openai/codex/blob/339751715c64496cb86246bfb3935f40e309dd3d/codex-rs/apply-patch/src/parser.rs#L41-L77","documentation":"Hunk-level syntax error: the patch as a whole parsed, but one hunk violates the grammar. Display embeds the 1-based line_number within the patch text and a message naming the violation - for example a change line missing its leading ' ', '+', or '-' prefix, an Update File hunk with no context/changes, a misplaced '*** End of File', or a malformed '*** Move to:'. Lenient mode tolerates gpt-4.1 quirks, so this variant indicates real structural damage inside a hunk.","triggerScenarios":"A change line inside an Update hunk missing its context/add/remove prefix; an '*** Update File:' section with no @@ context or change lines; '*** End of File' used inside an Add hunk; the final patch line missing its trailing newline so the last hunk line is cut.","commonSituations":"A model emits hunk bodies with wrong prefixes; hand-edits damage a hunk; CRLF or whitespace mangling; copy-paste drops leading spaces on context lines.","solutions":["Go to the reported line_number in the patch text and fix the violation named in the message","Verify every change line inside Update hunks starts with ' ', '+', or '-'","Ensure the final line of the patch ends with a newline","Regenerate the patch from the model or tool rather than hand-editing hunk syntax"],"exampleFix":"# before (line reported invalid: context line missing its leading space)\n*** Update File: a.txt\n@@ def main():\nprint(\"hi\")\n\n# after\n*** Update File: a.txt\n@@ def main():\n print(\"hi\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn is_invalid_hunk(e: &ParseError) -> bool {\n    matches!(e, ParseError::InvalidHunkError { .. })\n}","tryCatchPattern":"if let ApplyPatchError::ParseError(ParseError::InvalidHunkError { message, line_number }) = inner {\n    // surface line_number to the user/model so the hunk can be self-corrected\n    eprintln!(\"bad hunk at patch line {line_number}: {message}\");\n}","preventionTips":["End patch text with a trailing newline so the last hunk line is not cut","Never hand-edit hunk line prefixes when modifying a patch","Feed parser errors with line numbers back to the generating model for self-correction"],"tags":["rust","apply-patch","parser","syntax"],"backgroundTag":"patch-hunk-syntax-error","analyzedSha":"339751715c64496cb86246bfb3935f40e309dd3d","analyzedAt":"2026-08-25T05:35:09.876Z","schemaVersion":2},"datasetVersion":"2026-08-25T06:17:31.827Z"}