🤖 fix: prevent double newlines in file_edit_insert tool #480
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.
Problem
The
file_edit_inserttool was adding an extra newline when content already ended with\n, causing failures on simple tasks like terminal-bench'shello-worldwhere file content precision matters.Example failure:
hello.txtwith"Hello, world!\n"file_edit_insert(content="Hello, world!\n")"Hello, world!\n\n"(double newline!)Root Cause
The tool's implementation (lines 96-97):
When content is inserted as an array element and joined with
"\n", it always gets a trailing newline. When content already includes"\n", this produces double newlines.The tool was designed to add a trailing newline, but this behavior was:
\nwhen instructed to "make sure it ends in a newline")Solution
Smart newline detection - If content already ends with
\n, strip it before joining to prevent doubling:This matches the agent's natural expectation - if they explicitly add
\n, it should be preserved as-is without doubling.Testing
Added 2 regression tests:
All 14 tests pass (12 existing + 2 new).
Impact
Estimated: +5-10% accuracy on terminal-bench tasks
Direct fixes:
Indirect improvements:
Analysis Source
Full analysis: terminal-bench-results/analysis_run_18894357631.md
From run 18894357631:
Generated with
cmux