Skip to content

fix(tools): keep CRLF files intact in diff previews and direct-match edits - #6607

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
lakshyab20:fix/4871-crlf-diff-and-edit
Nov 4, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
lakshyab20:fix/4871-crlf-diff-and-edit

Conversation

@lakshyab20

@lakshyab20 lakshyab20 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Diff previews and edit_file now respect a CRLF file's line endings: a one-line change on a Windows-style file diffs as one line, and a direct-match edit no longer leaves the file with mixed endings.

Why

#4864 started reading files with their original endings, but two paths were left out:

  • generate_diff read the original with newline="" (CRLF kept) and diffed it against the model's LF new_content, so every line of a CRLF file showed as deleted and re-added. The counts were wrong too, so a one-line change looked like a full rewrite.
  • apply_unique_replacement converted new_content to the file's endings only inside the line-ending retry branch. When old_content matched as-is (any single-line old_content), a multi-line LF replacement went into a CRLF file unchanged.

Linked issue

Closes #4871

Changes

  • file_io_tools.generate_diff: read the original with universal newlines and normalise new_content to LF before diffing. A diff is a preview, not a write round trip, so nothing on disk changes.
  • file_edit.apply_unique_replacement: when a one-line old_content matches directly on a line that ends in CRLF, retarget new_content to CRLF. The decision follows the matched line, not the whole file, so LF edits in a mostly-LF file with one stray CRLF line stay LF. A multi-line old_content already shows which endings it matched and is left alone. If it spells out CRLF, it's a deliberate line-ending edit (e.g. test_line_ending_only_edit_still_draws_a_card).
  • generate_diff docstring: notes that an endings-only change now diffs as no change.
  • tests/unit/test_file_io_line_endings.py: 6 regression tests (a direct-match edit on a CRLF file, a deliberate CRLF→LF edit that must be kept, single- and multi-line LF edits in a mostly-LF file with one CRLF line, a diff of a CRLF file with one change, a diff of an unchanged CRLF file).

Test plan

  • pytest tests/unit/test_file_io_line_endings.py: 16 passed (3 of the new tests fail on main; the others guard the deliberate-edit and mixed-file cases)
  • pytest tests/unit/agents/test_file_edit_semantics.py tests/unit/chat/ui/test_edit_diff_card.py tests/unit/test_relative_write_workspace.py tests/unit/test_host_attr_contract.py: pass
  • pytest -n auto tests/unit/ (macOS, Python 3.12): 34,971 passed. The only failures are 2 in test_schedule_missed_runs.py, which fail the same way on unmodified main on this machine.
  • python util/lint.py --black --isort: pass

Evidence

Direct calls to the registered generate_diff and edit_file tools on a 6-line CRLF file:

## BEFORE (main)
generate_diff on a 6-line CRLF file, 1 line changed -> additions=6 deletions=6
edit_file single-line match, LF replacement -> status=success CRLF=6 lone LF=1
## AFTER (this branch)
generate_diff on a 6-line CRLF file, 1 line changed -> additions=1 deletions=1
edit_file single-line match, LF replacement -> status=success CRLF=7 lone LF=0
  • Agent UI: N/A. The change is inside the file tools and adds no UI. The tool-level before/after is above.
  • MCP / CLI / HTTP: N/A. None of these surfaces changed.

Checklist

  • I have linked a GitHub issue above (Closes #N / Fixes #N / Refs #N).
  • I have described why this change is being made, not just what changed.
  • I have run linting and tests locally (python util/lint.py --all, pytest tests/unit/).
  • I have attached real-world evidence matched to the surface I changed (see Evidence above), or marked each surface N/A.
  • I have updated documentation if user-visible behavior changed (no docs change needed).

…edits

generate_diff read the original with newline="" (CRLF kept) and diffed it
against the model's LF text, so every line of a CRLF file showed as deleted
and re-added. A diff is a preview, not a write round trip, so both sides are
now compared with LF endings.

apply_unique_replacement only converted new_content to the file's endings
on the line-ending retry path. When old_content matched as-is in a CRLF
file without carrying CRLF itself (any single-line old_content), a
multi-line LF replacement was spliced in, leaving mixed endings. That case
is now converted too. An old_content that spells out CRLF is a deliberate
line-ending edit and its new_content is still left exactly as given.

Closes amd#4871
@github-actions github-actions Bot added tests Test changes agents labels Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

The diff preview fix is right: a one-line change to a Windows-style (CRLF) file now shows as one line instead of a full rewrite. The edit fix goes too far, though. It decides whether to write CRLF by asking whether the file has a CRLF anywhere. In a file that is mostly LF with one stray CRLF line, which is common in mixed files, ordinary LF edits that used to come out clean now get CRLF endings and leave the file mixed. That's the exact damage this PR is meant to prevent.

  • What to do: look at the line endings of the line that matched, not the whole file. The change is three lines (below), and all four of your new tests still pass with it.

Real-world evidence

N/A: there is no evidence bundle for this run. The verdict rests on static review, plus running the changed edit function directly against the mixed-file case. The project test suite was not run here.

🔍 Technical details

🟡 A direct-match edit in a mostly-LF file now writes CRLF (src/gaia/agents/tools/file_edit.py:376)

The guard "\r\n" in current_content and _retarget_newlines both check the whole file. So any direct match whose old_content has no CRLF has its replacement converted to CRLF, even when the matched region is LF. I ran the function as patched:

cur = 'header\r\ndef f():\n    a = 1\n    return a\n'
apply_unique_replacement('x', cur, '    a = 1\n    return a', '    a = 2\n    return a')
-> 'header\r\ndef f():\n    a = 2\r\n    return a\n'      # was clean LF before this PR
apply_unique_replacement('x', cur, '    return a', '    b = 2\n    return b')
-> 'header\r\ndef f():\n    a = 1\n    b = 2\r\n    return b\n'

A multi-line old_content with no CRLF can only match directly in an LF region, so it should never be retargeted. Only a single-line old_content is ambiguous, and its own line tells you which ending to use:

    if len(offsets) == 1 and "\n" not in old_content and "\r\n" in current_content:
        # A one-line match carries no endings of its own; use its line's.
        eol = current_content.find("\n", offsets[0])
        if eol == -1 or current_content[eol - 1] == "\r":
            new_content = _retarget_newlines(new_content, current_content)

I checked this with the function loaded on its own. Your direct-match CRLF case still gives x = 1\r\n return x\r\n, the deliberate a\r\n → a\n edit is still kept, and both mixed-file cases above stay LF. It's worth adding the mixed-file case (header\r\n followed by LF lines) as a regression test.

🟢 The diff preview can no longer show an ending-only change (src/gaia/agents/tools/file_io_tools.py:1028)

Both sides of the diff are now LF, so a deliberate CRLF↔LF conversion reports has_changes: False. That's acceptable for a preview. Mentioning it in the docstring would stop a caller from reading "no changes" as "nothing will be written".

Strengths

  • The regression tests are well chosen, especially the one guarding the deliberate CRLF→LF edit (test_a_deliberate_crlf_to_lf_edit_is_kept).
  • The generate_diff fix is minimal and touches nothing on disk.

The direct-match conversion asked whether the file had a CRLF anywhere, so
in a mostly-LF file with one stray CRLF line an ordinary LF edit came out
CRLF and left the file mixed. A multi-line old_content already says which
endings it matched, so only a one-line old_content is retargeted, and only
when the line it matched ends in CRLF.

Note in generate_diff's docstring that an endings-only change now diffs as
no change.

Refs amd#4871
@kovtcharov-amd kovtcharov-amd added this to the v0.25.4 milestone Nov 4, 2026
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 4, 2026
Merged via the queue into amd:main with commit 6b2cee2 Nov 4, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent file diffs and edits mishandle Windows (CRLF) line endings since #4864

2 participants