Skip to content

fix(apply-patch): skip initial new-file hunk header emitted by some AI models #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

msmhrt
Copy link

@msmhrt msmhrt commented Apr 27, 2025

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

  • Introduce NEW_FILE_HUNK regex + isFirst flag in parse_add_file()
  • Skip the very first header matching that pattern

Rust

  • Import regex::Regex
  • Apply the same new_file_re + "first" logic in parse_one_hunk()

Tests

  • TS

    • process_patch – add file skips full hunk headers
    • Regex self-test
  • Rust

    • test_add_file_skip_full_hunk_header_with_section
    • test_new_file_hunk_regex

Copy link

github-actions bot commented Apr 27, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@msmhrt
Copy link
Author

msmhrt commented Apr 27, 2025

I have read the CLA Document and I hereby sign the CLA

Gemini 2.0 Flash emits hunk headers in Add File directives (e.g. "@@ -0,0 +1 @@" or "@@ -0,0 +1,5 @@"). Use "/^@@\s+-0,0\s+\+1(?:,[1-9]\d*)?\s+@@/" to detect and skip these headers.

Signed-off-by: Masami HIRATA <[email protected]>
@msmhrt msmhrt force-pushed the fix/apply-patch-skip-gemini-hunk branch from 77bdda0 to 389d877 Compare April 28, 2025 01:13
msmhrt added 3 commits April 28, 2025 15:01
…I models and add unit

    tests

        - TypeScript parser: introduce NEW_FILE_HUNK regex + isFirst flag
        - Rust parser: same regex+first logic in parse_one_hunk
        - Tests (TS & Rust) covering skip-header behavior and regex self-test

Signed-off-by: Masami HIRATA <[email protected]>
@msmhrt msmhrt changed the title apply-patch: skip gemini-2.0-flash hunk headers in Add File blocks fix(apply-patch): skip initial new-file hunk header emitted by some AI models Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant