Skip to content

fix(core): honor End of File hunks when the file ends in a newline - #4661

Merged
seratch merged 1 commit into
openai:mainfrom
lllakshit:fix/apply-diff-eof-trailing-newline
Aug 26, 2026
Merged

seratch merged 1 commit into
openai:mainfrom
lllakshit:fix/apply-diff-eof-trailing-newline

Conversation

@lllakshit

Copy link
Copy Markdown
Contributor

Summary

apply_diff is how sandbox agents edit files. *** End of File hunks were silently wrong on almost every real source file because those files end in a newline.

str.split("\n") keeps a trailing empty element for newline-terminated text. EOF matching treated that phantom as a real line, so:

  1. An append at EOF inserted a blank line and dropped the trailing newline ("a\nb\n" + +c became "a\nb\n\nc" instead of "a\nb\nc\n").
  2. Repeated context with an EOF marker matched the first occurrence, not the last.

The same locator is used by sandbox apply_patch, so this is not a helper-only issue. Agents could apply a plausible patch and leave the wrong code in the workspace with no error.

The locator now ignores that terminator when eof=True. Existing non-EOF and CRLF behavior is unchanged.

Fixes #4660

Test plan

  • python -m pytest tests/test_apply_diff.py tests/test_apply_diff_helpers.py
    • 31 passed, including EOF append, last-occurrence context, CRLF, and helper coverage for the phantom split element

str.split keeps a trailing empty element for newline-terminated files.
EOF matching treated that phantom as a real line, so appends inserted
a blank line and repeated context resolved from the start of the file.
Ignore the terminator when locating EOF hunks.

Fixes openai#4660
@seratch seratch changed the title fix(apply_diff): honor End of File hunks when the file ends in a newline fix(core): honor End of File hunks when the file ends in a newline Aug 26, 2026
@seratch seratch added this to the 0.22.x milestone Aug 26, 2026
@seratch
seratch merged commit 18de651 into openai:main Aug 26, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apply_diff: *** End of File hunks corrupt or mis-locate edits when the input ends in a newline

2 participants