{"record":{"id":"9175ca33823e1518","repo":"openai/codex","slug":"error","errorCode":null,"errorMessage":"{error}","messagePattern":"\\{error\\}","errorType":"exception","errorClass":"ApplyPatchFailure","httpStatus":null,"severity":"error","filePath":"codex-rs/apply-patch/src/lib.rs","lineNumber":313,"sourceCode":"    Add {\n        content: String,\n        overwritten_content: Option<String>,\n    },\n    Delete {\n        content: String,\n    },\n    Update {\n        move_path: Option<PathUri>,\n        old_content: String,\n        overwritten_move_content: Option<String>,\n        new_content: String,\n    },\n}\n\n/// A failed patch application together with the textual mutations that were\n/// definitely committed before the failure was observed.\n#[derive(Debug, Error)]\n#[error(\"{error}\")]\npub struct ApplyPatchFailure {\n    #[source]\n    error: ApplyPatchError,\n    delta: AppliedPatchDelta,\n}\n\nimpl ApplyPatchFailure {\n    fn new(error: ApplyPatchError, delta: AppliedPatchDelta) -> Self {\n        Self { error, delta }\n    }\n\n    fn without_delta(error: ApplyPatchError) -> Self {\n        Self::new(error, AppliedPatchDelta::empty())\n    }\n\n    pub fn delta(&self) -> &AppliedPatchDelta {\n        &self.delta\n    }","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/openai/codex/blob/339751715c64496cb86246bfb3935f40e309dd3d/codex-rs/apply-patch/src/lib.rs#L295-L331","documentation":"Top-level error returned by the apply_patch family (apply_patch, apply_patch_with_options, apply_hunks). Its Display forwards to the wrapped ApplyPatchError, so the visible message comes from the underlying cause: a parse error, an IO error, a failed hunk context match, or even a failure writing the summary to stdout/stderr. The payload's real value is the AppliedPatchDelta it carries alongside the error: the ordered list of file mutations that were definitely committed to disk before the failure was observed, intended for undo/rollback.","triggerScenarios":"Any call to apply_patch/apply_patch_with_options/apply_hunks that fails: an Update File hunk whose context lines do not match the file on disk, an Add File onto an unwritable path, an unparseable patch (wrapped as ApplyPatchError::ParseError with an empty delta), or a failure writing the result summary. Multi-hunk patches that fail midway return it with a non-empty delta of already-applied changes.","commonSituations":"Applying model-generated patches against files that changed since the patch was computed; concurrent edits between read and write; permission or ENOSPC failures mid-write; truncated model output that fails parsing before anything is applied (empty delta).","solutions":["Call into_parts() (or delta()) on the failure and use the returned AppliedPatchDelta to undo changes already committed before propagating the error","Inspect the inner ApplyPatchError for the real cause - the printed message is the inner error's text, not this wrapper's","If the delta is empty, nothing was modified; fix the patch itself (see the inner ParseError or IO cause) and re-apply the whole patch","Re-read the target files and regenerate the patch so hunk contexts match current contents"],"exampleFix":"// before\nif let Err(failure) = apply_patch(patch, &cwd, &mut stdout, &mut stderr, fs, None).await {\n    eprintln!(\"patch failed: {failure}\"); // loses partial-change info\n}\n\n// after\nif let Err(failure) = apply_patch(patch, &cwd, &mut stdout, &mut stderr, fs, None).await {\n    let (err, applied) = failure.into_parts();\n    eprintln!(\"patch failed: {err}; {} change(s) already committed\", applied.changes.len());\n    for change in applied.changes {\n        // restore old content recorded in `change.change` before propagating\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match apply_patch(patch, &cwd, &mut stdout, &mut stderr, fs, sandbox).await {\n    Ok(delta) => { /* success */ }\n    Err(failure) => {\n        let (err, applied) = failure.into_parts();\n        // rollback the changes recorded in `applied` before propagating\n        return Err(err.into());\n    }\n}","preventionTips":["Commit or snapshot the working tree before applying patches so the delta can be cross-checked against it","Regenerate patches against current file contents rather than stale snapshots","Always handle the Err arm of every apply_patch call - partial application is the normal outcome on failure, not an edge case"],"tags":["rust","apply-patch","partial-failure","rollback"],"backgroundTag":"patch-partially-applied","analyzedSha":"339751715c64496cb86246bfb3935f40e309dd3d","analyzedAt":"2026-08-25T05:35:09.876Z","schemaVersion":2},"datasetVersion":"2026-08-25T06:17:31.827Z"}