fix(apply-patch): skip initial new-file hunk header emitted by some AI models #695
+100
−1
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.
What & Why
Some AI-generated patches (e.g. from Gemini 2.0 Flash) prepend a full diff hunk header like:
@@ -0,0 +1,2 @@ SectionName
before any
+
lines. Our parser treats that as invalid, causing parse failures or stray blank lines when adding new files.This PR makes both the TypeScript and Rust parsers ignore only the first such "new-file" hunk header, then proceed normally.
How
TypeScript
NEW_FILE_HUNK
regex +isFirst
flag inparse_add_file()
Rust
regex::Regex
new_file_re
+ "first" logic inparse_one_hunk()
Tests
TS
process_patch
– add file skips full hunk headersRust
test_add_file_skip_full_hunk_header_with_section
test_new_file_hunk_regex