# agent: Fix edit_file corrupting indentation when old_text omits first-line indent \(\#60613\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/7344a31ddc144f12bd8ee52328cfb9bd275c97f5)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 7344a31ddc144f12bd8ee52328cfb9bd275c97f5

Requested commit: 7344a31ddc144f12bd8ee52328cfb9bd275c97f5

Commit: 7344a31ddc144f12bd8ee52328cfb9bd275c97f5

Tree: d08526b0501d1f37e397166de191944b2761b823

Author: MartinYe1234

Committer: GitHub

## Message

```
agent: Fix edit_file corrupting indentation when old_text omits first-line indent (#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 https://github.com/zed-industries/zed/issues/60302

Release Notes:

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

## Parents

- [2b9b3c7ea21293c9f271d993503e6d8dba2c6891](https://git.cafe/gitcafe/zed/commit/2b9b3c7ea21293c9f271d993503e6d8dba2c6891?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/7344a31ddc144f12bd8ee52328cfb9bd275c97f5?format=markdown)
