fix(file_editor): insert at EOF of file without trailing newline concatenates onto last line - #4582
Open
ksk2023 wants to merge 1 commit into
Open
Conversation
When inserting after the last line (insert_line == num_lines) of a file that does not end with a newline, the retained last line lacks "\n", so the inserted content gets concatenated onto the last line instead of starting on its own line. Before: insert(insert_line=2, new_str="X") on "a\nb\nc" -> "a\nb\ncX\n" After: insert(insert_line=2, new_str="X") on "a\nb\nc" -> "a\nb\nc\nX\n" This adds a missing-newline guard right before appending the inserted lines, plus regression tests covering: single/multi-line inserts at EOF without trailing newline, the unchanged behavior for files that already end with a newline, and middle-of-file inserts.
Collaborator
|
👋 This PR needs a couple of things fixed before OpenHands can review it:
Push an update once this is addressed and this check re-runs automatically. This is an automated check - no AI was used to generate this comment. |
Collaborator
|
🚦 CI is currently failing on this PR's latest commit. Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request This is an automated check - no AI was used to generate this comment. |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
HUMAN:
I manually reproduced the bug on the current main, applied the fix, and ran the file_editor test suite: my four new regression tests fail on main and all pass with the fix, with no new failures versus the baseline.
AGENT:
Issue Number
Fixes #4583
Why
FileEditor.insertsilently corrupts a file when asked to insert after the last line of a file that does not end with a newline: the inserted content is concatenated onto the last line instead of starting on its own line. This is a real data-corruption path for agent-driven edits, and the bug was inherited from the original Anthropic computer-use-demo implementation this editor derives from.Summary
In
_execute_insert(), the file is read line-by-line and retained lines are collected up toinsert_line. Python's line iterator omits the trailing\nfrom the last line when the file has no final newline, sonew_lines[-1]is e.g."c", and the new content is appended directly, producingcX\n.The fix adds a guard right before appending the inserted lines:
Reproducer:
How to Test
Added 4 regression tests to
tests/tools/file_editor/test_basic_operations.py:test_insert_at_eof_without_trailing_newline— single-line insert at EOF (fails onmain, passes with fix)test_insert_multiple_lines_at_eof_without_trailing_newline— multi-line insert at EOF (fails onmain, passes with fix)test_insert_at_eof_with_trailing_newline_unchanged— files already ending with\nkeep existing behavior (no extra blank line)test_insert_in_middle_unaffected_by_eof_fix— middle-of-file inserts unchangedVerified on
main:2 failed, 2 passed→ with fix:4 passed. The full file_editor suite shows no new failures compared to baseline (the 9 pre-existing failures onmainunder Windows — includingtest_insert_non_utf8_file, which fails identically without this change — are unrelated environment/encoding issues).