Repository navigation
fix(tools): keep CRLF files intact in diff previews and direct-match edits - #6607
Conversation
…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
|
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.
Real-world evidenceN/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 ( The guard A multi-line I checked this with the function loaded on its own. Your direct-match CRLF case still gives 🟢 The diff preview can no longer show an ending-only change ( Both sides of the diff are now LF, so a deliberate CRLF↔LF conversion reports Strengths
|
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
Summary
Diff previews and
edit_filenow 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_diffread the original withnewline=""(CRLF kept) and diffed it against the model's LFnew_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_replacementconvertednew_contentto the file's endings only inside the line-ending retry branch. Whenold_contentmatched as-is (any single-lineold_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 normalisenew_contentto 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-lineold_contentmatches directly on a line that ends in CRLF, retargetnew_contentto 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-lineold_contentalready 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_diffdocstring: 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 onmain; 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: passpytest -n auto tests/unit/(macOS, Python 3.12): 34,971 passed. The only failures are 2 intest_schedule_missed_runs.py, which fail the same way on unmodifiedmainon this machine.python util/lint.py --black --isort: passEvidence
Direct calls to the registered
generate_diffandedit_filetools on a 6-line CRLF file:Checklist
Closes #N/Fixes #N/Refs #N).python util/lint.py --all,pytest tests/unit/).