[错误]: str_replace 会重新缩进带制表的文件中的每一行

作者: sxh313创建于 2026年9月28日更新于 2026年9月28日

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:

```Python 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. gofmt formats Go with tabs, so a tab-indented Go file that this tool turned into spaces is listed by gofmt -l and fails any CI that checks go fmt; Rust projects that set hard_tabs = true in rustfmt.toml have the same gate. (Stated from those tools' documented behaviour — this box has no Go toolchain, so I did not run gofmt myself.) * The patch is unreadable. git diff --numstat for a one-line edit reads 30 30 main.go, and git blame attributes all 30 lines to the agent. That is what a reviewer of a --must-patch run has to read through. * The replacement text itself lands mis-indented when the match covers part of a tab. old_str is 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 = 1 line 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 for insert 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
…

内容来源: bytedance/trae-agent