Skip to content

agent: Fix edit_file corrupting indentation when old_text omits first-line indent - #60613

Merged
MartinYe1234 merged 1 commit into
mainfrom
corrupted-edit-file-apply
Jul 10, 2026
Merged

agent: Fix edit_file corrupting indentation when old_text omits first-line indent#60613
MartinYe1234 merged 1 commit into
mainfrom
corrupted-edit-file-apply

Conversation

@MartinYe1234

Copy link
Copy Markdown
Contributor

The edit_file tool re-indents new_text by computing a single indent delta from the first line of old_text versus the matched buffer line, then applying that delta to every line of the replacement. When a model omits the leading indentation on only the first line of old_text/new_text (a common pattern when copying from mid-line context), the delta computed from the first line was wrongly applied to the remaining, already-correctly-indented lines, doubling their indentation.

This PR:

  • Tracks the (query_row, buffer_row) line pairs aligned by the streaming fuzzy matcher so the indent of each old_text line can be compared against the buffer line it actually matched.
  • Computes a separate indent delta for the lines after the first: when those lines agree on a consistent delta, it's used for the rest of the replacement; otherwise the previous uniform behavior is preserved.
  • Keeps query_lines/line pairs in sync when finish() extends a match with a trailing incomplete line.
  • Adds an end-to-end regression test reproducing the issue, plus unit tests for the new re-indentation logic.

Closes #60302

Release Notes:

  • Fixed the agent's edit_file tool corrupting indentation when a replacement omitted leading whitespace on only its first line.

@MartinYe1234 MartinYe1234 self-assigned this Jul 8, 2026
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Jul 8, 2026
@zed-community-bot zed-community-bot Bot added the staff Pull requests authored by a current member of Zed staff label Jul 8, 2026
@MartinYe1234
MartinYe1234 marked this pull request as ready for review July 8, 2026 18:15
@MartinYe1234
MartinYe1234 added this pull request to the merge queue Jul 10, 2026
Merged via the queue into main with commit 7344a31 Jul 10, 2026
48 checks passed
@MartinYe1234
MartinYe1234 deleted the corrupted-edit-file-apply branch July 10, 2026 20:24
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…-line indent (zed-industries#60613)

The `edit_file` tool re-indents `new_text` by computing a single indent
delta from the first line of `old_text` versus the matched buffer line,
then applying that delta to every line of the replacement. When a model
omits the leading indentation on only the first line of
`old_text`/`new_text` (a common pattern when copying from mid-line
context), the delta computed from the first line was wrongly applied to
the remaining, already-correctly-indented lines, doubling their
indentation.

This PR:

- Tracks the `(query_row, buffer_row)` line pairs aligned by the
streaming fuzzy matcher so the indent of each `old_text` line can be
compared against the buffer line it actually matched.
- Computes a separate indent delta for the lines after the first: when
those lines agree on a consistent delta, it's used for the rest of the
replacement; otherwise the previous uniform behavior is preserved.
- Keeps `query_lines`/line pairs in sync when `finish()` extends a match
with a trailing incomplete line.
- Adds an end-to-end regression test reproducing the issue, plus unit
tests for the new re-indentation logic.

Closes zed-industries#60302

Release Notes:

- Fixed the agent's `edit_file` tool corrupting indentation when a
replacement omitted leading whitespace on only its first line.
playdohface pushed a commit to playdohface/zed that referenced this pull request Aug 29, 2026
…-line indent (zed-industries#60613)

The `edit_file` tool re-indents `new_text` by computing a single indent
delta from the first line of `old_text` versus the matched buffer line,
then applying that delta to every line of the replacement. When a model
omits the leading indentation on only the first line of
`old_text`/`new_text` (a common pattern when copying from mid-line
context), the delta computed from the first line was wrongly applied to
the remaining, already-correctly-indented lines, doubling their
indentation.

This PR:

- Tracks the `(query_row, buffer_row)` line pairs aligned by the
streaming fuzzy matcher so the indent of each `old_text` line can be
compared against the buffer line it actually matched.
- Computes a separate indent delta for the lines after the first: when
those lines agree on a consistent delta, it's used for the rest of the
replacement; otherwise the previous uniform behavior is preserved.
- Keeps `query_lines`/line pairs in sync when `finish()` extends a match
with a trailing incomplete line.
- Adds an end-to-end regression test reproducing the issue, plus unit
tests for the new re-indentation logic.

Closes zed-industries#60302

Release Notes:

- Fixed the agent's `edit_file` tool corrupting indentation when a
replacement omitted leading whitespace on only its first line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent tool edit_file corrupts multi-line new_text indentation

2 participants