[Bug]: one str_replace re-indents every line of a tab-indented file
What happened?
str_replace_based_edit_tool expands the tabs of the whole file before writing it back, so one edit in a tab-indented file re-indents every line in it. The lines the agent never mentioned come out byte-different from what the user had.
Measured on main @ e839e55, Python 3.12.13 — a 30-line tab-indented Go file, one replacement of the text line 7:
src = "\n".join(f'\tfmt.Println("line {i}")' for i in range(30)) + "\n"
# write src to main.go, then:
await tool.execute({"command": "str_replace", "path": str(path),
"old_str": "line 7", "new_str": "line seven"})main @ e839e55 : tabs 0 of 30 lines moved 30 line 0 as written ' fmt.Println("line 0")'
with the fix : tabs 30 of 30 lines moved 1 line 0 as written '\tfmt.Println("line 0")'str_replace asked for one line; 30 changed. Three things follow from that:
- The change breaks the project's own format gate.
gofmtformats Go with tabs, so a tab-indented Go file that this tool turned into spaces is listed bygofmt -land fails any CI that checksgo fmt; Rust projects that sethard_tabs = trueinrustfmt.tomlhave the same gate. (Stated from those tools' documented behaviour — this box has no Go toolchain, so I did not rungofmtmyself.) - The patch is unreadable.
git diff --numstatfor a one-line edit reads30 30 main.go, andgit blameattributes all 30 lines to the agent. That is what a reviewer of a--must-patchrun has to read through. - The replacement text itself lands mis-indented when the match covers part of a tab.
old_stris matched against the expanded view, so a match that starts mid-tab keeps the tab's leftover columns in the file. Asking for 4-space indentation out of a\tvalue = 1line gets 8 spaces:
main @ e839e55 : 'if x:\n result = 1\n' # 8 spaces
with the fix : 'if x:\n result = 1\n' # the 4 that were asked forinsert moves the same lines (test_an_insertion_keeps_every_other_lines_tabs), and a str_replace two rows away rewrites its neighbour's indent: \tpass comes back as pass.
What did you expect to happen?
Only the matched region should change; the text outside the edit should be written back exactly as it was read.
Matching against the expanded view must keep working, because that is what the tool shows the model: _make_output at trae_agent/tools/edit_tool.py:292-303 expands tabs for display (expand_tabs=True by default), so the model quotes return 1 when the file says \treturn 1. The fix is about which text gets written, not about what gets matched.
Traceback
No exception — the damage is silent, which is why it survives review. Evidence is the four assertions below, from the regression tests in the PR, run against clean main @ e839e55:
FAILED tests/tools/test_edit_tool_indentation.py::TestIndentationSurvivesAnEdit::test_a_replacement_keeps_every_other_lines_tabs
FAILED tests/tools/test_edit_tool_indentation.py::TestIndentationSurvivesAnEdit::test_an_insertion_keeps_every_other_lines_tabs
AssertionError: ' fmt.Println("line 0")\n fm[915 chars]")\n' != '\tfmt.Println("line 0")\n\tfmt.Println("l[735 chars]")\n'
FAILED tests/tools/test_edit_tool_indentation.py::TestIndentationSurvivesAnEdit::test_the_model_can_still_match_on_the_expanded_view_it_was_shown
AssertionError: 'def f():\n return 2\n pass\n' != 'def f():\n return 2\n\tpass\n'
def f():
return 2
- pass
+ pass
FAILED tests/tools/test_edit_tool_indentation.py::TestIndentationSurvivesAnEdit::test_a_match_that_covers_half_a_tab_consumes_the_whole_tab
AssertionError: 'if x:\n result = 1\n' != 'if x:\n result = 1\n'
if x:
- result = 1
? ----
+ result = 1
4 failed, 1 passedWhat is your system, Python, dependency version?
- OS: Windows 11 (
Windows-11-10.0.26200-SP0). Unlike the line-ending bug in #471, this one is not host-dependent:expandtabs()is a plain string method, so the four assertions above fail identically on the ubuntu CI runner as written. - Python: 3.12.13 (repo venv from
uv sync --all-extras) - Commit:
e839e55(main)
Additional information that you believe is relevant to this bug
The four sites, found by reading every expandtabs() call in the tool:
| file | line | what it does |
|---|---|---|
trae_agent/tools/edit_tool.py |
200 | str_replace reads the file and expands it, then writes that back at :221 |
trae_agent/tools/edit_tool.py |
240 | _insert reads and expands, then writes that back |
trae_agent/tools/edit_tool_cli.py |
244 | the same two lines in the Docker copy |
trae_agent/tools/edit_tool_cli.py |
284 |
edit_tool_cli.py is not dead code: it is the PyInstaller entry point that trae_agent/tools/docker_tool_executor.py:113 runs inside the container, so Docker mode has the same defect. trae_agent/tools/edit_tool.py:302 is the display-only expansion in _make_output and stays as it is.
What the PR does: keep matching on the expanded view, but splice the replacement into the text as read, using a map from each expanded position back to the character it came from. Every column a tab was expanded into maps to that tab, so a match that covers only part of a tab consumes the whole tab instead of leaving stray spaces (that is the third failure above). _insert stops expanding entirely — it splices whole lines, and expanding tabs neither adds nor removes a line.
Three deliberate scope notes:
new_str.expandtabs()is left exactly as it is in both commands. That is what the model's own tabs render as today; changing it is a separate behaviour decision, so one of the new tests pins it and a future change has to be deliberate.- This overlaps #470 (locale encoding) and #472 (line terminators) — three distinct defects that all sit in the same two reads and one write, so whichever of the three merges second needs a small rebase. On this Windows host the
30 30numstat is the union of the tab defect here and the newline defect in #472; the numbers above normalise line terminators so they measure tabs only. - The two copies of the tool have already drifted on an unrelated detail (
SNIPPET_LINESis 4 inedit_tool.py:24and 5 inedit_tool_cli.py:50). Not touched here.
Source: bytedance/trae-agent