fix: provide tolerance for apply_patch tool #993
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As explained in detail in the doc comment for
ParseMode::Lenient, we have observed that GPT-4.1 does not always generate a valid invocation ofapply_patch. Fortunately, the error is predictable, so we introduce some new logic to thecodex-apply-patchcrate to recover from this error.Because we would like to avoid this becoming a de facto standard (as it would be incompatible if
apply_patchwere provided as an actual executable, unless we also introduced the lenient behavior in the executable, as well), we require passingParseMode::Lenienttoparse_patch_text()to make it clear that the caller is opting into supporting this special case.Note the analogous change to the TypeScript CLI was #930. In addition to changing the accepted input to
apply_patch, it also introduced additional instructions for the model, which we include in this PR.Note that
apply-patchdoes not depend on eitherregexorregex-lite, so some of the checks are slightly more verbose to avoid introducing this dependency.That said, this PR does not leverage the existing
extract_heredoc_body_from_apply_patch_command(), which depends ontree-sitterandtree-sitter-bash:codex/codex-rs/apply-patch/src/lib.rs
Lines 191 to 246 in 5a5aa89
though perhaps it should.