Commit 0d5f997
authored
🤖 fix: prevent double newlines in file_edit_insert tool (#480)
## Problem
The `file_edit_insert` tool was adding an extra newline when content
already ended with `\n`, causing failures on simple tasks like
terminal-bench's `hello-world` where file content precision matters.
**Example failure:**
- Task: Create `hello.txt` with `"Hello, world!\n"`
- Agent: `file_edit_insert(content="Hello, world!\n")`
- Result: File contains `"Hello, world!\n\n"` (double newline!)
- Test: ❌ FAILED - Expected exactly one newline
## Root Cause
The tool's implementation (lines 96-97):
```typescript
const newLines = [...lines.slice(0, line_offset), content, ...lines.slice(line_offset)];
const newContent = newLines.join("\n");
```
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:
1. Undocumented in the tool description
2. Unexpected by the agent (naturally includes `\n` when instructed to
"make sure it ends in a newline")
3. Causing subtle bugs where exact file content matters
## Solution
**Smart newline detection** - If content already ends with `\n`, strip
it before joining to prevent doubling:
```typescript
const contentEndsWithNewline = content.endsWith("\n");
const normalizedContent = contentEndsWithNewline ? content.slice(0, -1) : content;
```
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:
- ✅ Content with trailing newline (the hello-world case)
- ✅ Multiline content with trailing newline
All 14 tests pass (12 existing + 2 new).
## Impact
**Estimated:** +5-10% accuracy on terminal-bench tasks
**Direct fixes:**
- hello-world task (was failing 1/2 tests, now should pass both)
- Likely fixes other tasks where exact file content matters
**Indirect improvements:**
- Agent won't avoid tool after encountering unexpected behavior
- Fewer edge case bugs in config files, data formats, etc.
## Analysis Source
Full analysis:
[terminal-bench-results/analysis_run_18894357631.md](https://github.com/coder/cmux/blob/tb-baseline/terminal-bench-results/analysis_run_18894357631.md)
From run 18894357631:
- 40% accuracy (16/40 tasks)
- This bug identified as Priority 1 critical issue
- Several other tasks showed partial failures (5/7 tests, 4/5 tests)
that may also benefit from this fix
_Generated with `cmux`_1 parent cb523ed commit 0d5f997
File tree
2 files changed
+87
-1
lines changed- src/services/tools
2 files changed
+87
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
332 | 405 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
97 | 110 | | |
98 | 111 | | |
99 | 112 | | |
| |||
0 commit comments