Skip to content

fix(tools): stop file edits rewriting every line ending in the file - #4864

Merged
kovtcharov-amd merged 1 commit into
mainfrom
fix/preserve-line-endings-in-file-tools
Oct 6, 2026
Merged

kovtcharov-amd merged 1 commit into
mainfrom
fix/preserve-line-endings-in-file-tools

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Branch: fix/preserve-line-endings-in-file-tools
Base: e0fd6717f609a5be7ca8676a6be2c869fd4fe4b2 (e0fd6717f)
Commits:

SHA Subject
acaf4f3920b4044d0d632f26f9a556fd9eb58b02 fix(tools): stop file edits rewriting every line ending in the file

The problem

open() in text mode, and Path.read_text / Path.write_text, use universal
newlines by default. Reading turns every CRLF into LF; writing turns every LF
into os.linesep. None of the text I/O sites in file_io_tools.py or
file_tools.py passed newline=.

On Windows that means a single one-line edit_file against an LF-terminated
file writes the whole file back as CRLF.

Nothing about this is visible on screen — the decoded text is identical either
way — which is why it has survived. The damage only shows up in a byte
comparison, and that is where it matters most:

  • A three-line change to a 400-line file is reported as 400 lines changed.
    The real edit is buried in a review diff, and any size-capped diff view drops
    it entirely.
  • An agent that re-reads the file afterwards no longer finds the text it
    remembers, so its next exact-match edit fails for a reason nothing reports.
  • Tools that flag whitespace (git diff --check) emit one warning per added
    line, which reads as "my edit is corrupt".

Reproduction

Create a 200-line LF-terminated file, call edit_file once to change one line,
and diff:

200 lines modified, expected 1

The fix

Every read and write that feeds a read/modify/write round trip now passes
newline="", so bytes go in and out untranslated and a file keeps whatever
endings it already had. The pathlib sites cannot take newline=, so they go
through small _read_exact / _write_exact helpers instead.

replace_function splices in generated text that is always LF-terminated, so it
now converts that block to the file's own endings rather than leaving the file
carrying both.

The matching change this forces, and why it belongs here

Reading exactly makes the match in apply_unique_replacement stricter, because
read_file still normalises what it shows. A multi-line old_content copied
from it is therefore LF even when the file on disk is CRLF, and matching it
byte-for-byte would start refusing edits that are correct in every way that
matters.

So a failed match is retried once with old_content rewritten to the file's
own endings, and accepted only if that matches exactly one place — nothing
ambiguous is guessed at. The replacement text is rewritten the same way, so a
repaired edit cannot reintroduce a mix.

Without this half, the first half is a regression. They ship together.

Deliberately not changed

read_file still normalises what it displays. That is a presentation choice, it
is not part of any round trip, and changing it would alter what every caller
sees for no corruption-related gain.

Tests

tests/unit/test_file_io_line_endings.py asserts on raw bytes, since the
decoded text is the same either way. It covers both directions — an LF file must
stay LF and a CRLF file must stay CRLF — so the fix cannot be "normalise
everything to LF", which would be the same bug pointed the other way. Both
directions of the retargeted match are pinned too.

Risk

Low. The change is confined to how bytes are decoded and encoded at the I/O
boundary plus one retry path that only runs after a match has already failed.

Test plan

  • pytest tests/unit/test_file_io_line_endings.py tests/unit/agents/test_file_edit_semantics.py tests/unit/agents/test_file_tools_text_encoding.py tests/unit/agents/test_read_before_edit.py tests/unit/agents/test_write_reports_syntax.py tests/unit/test_file_tools.py tests/unit/test_file_write_guardrails.py tests/unit/test_file_io_confinement.py tests/unit/agents/test_image_outcome_guard.py tests/unit/eval/test_flagship_tasks.py::test_an_adversarial_task_rejects_the_shortcut
    on this branch alone — 504 passed, 7 skipped
  • Full tests/unit — see below
  • python util/lint.py --black --isort
  • Windows CI, since this bug is invisible on a Linux-only lane

Full unit suite

tests/unit, Windows, --timeout=300, run on unmodified main (e0fd6717f)
and on a disposable build with all four open branches stacked (line endings in file tools, step-budget notices, post-edit
read-back, model-endpoint retry):

Passed Failed Skipped
main e0fd6717f 19,373 5 410
stacked build 19,602 5 410

The same five tests fail on both, and none of them is touched by this branch:
three installer TUI-detection tests, test_file_access_scope.py::test_no_allowlist_still_defaults_to_cwd,
and test_lemonade_asr.py::TestRealServer::test_real_transcription_round_trip,
which needs a live server. No test fails on the stacked build that passes on
main.

Stacking with other open branches

Applies cleanly to main on its own. It touches no file any of the other open branches above touch, so it stacks with them in any order without conflict.

🤖 Generated with Claude Code

`open()` in text mode, and `Path.read_text` / `Path.write_text`, use
universal newlines by default. Reading turns every CRLF into LF; writing
turns every LF into `os.linesep`. None of the text I/O sites in
`file_io_tools.py` or `file_tools.py` passed `newline=`, so on Windows a
one-line `edit_file` against an LF-terminated file wrote the whole file
back as CRLF.

The text is identical on screen, which is why this is easy to miss, but
every byte-comparing tool disagrees: a three-line change to a 400-line
file is reported as 400 lines changed. That is enough to bury the real
edit in a review diff, and an agent re-reading the file afterwards no
longer finds the text it remembers, so its next exact-match edit fails
for a reason nothing reports.

Reproduced with a 200-line LF file and a single `edit_file` call: 200
lines modified, expected 1.

Every read and write that feeds a read/modify/write round trip now passes
`newline=""`, so bytes go in and out untranslated and a file keeps the
endings it already had. The `pathlib` sites cannot take `newline=`, so
they go through small `_read_exact` / `_write_exact` helpers instead.
`replace_function` splices in generated text that is always LF, so it now
converts that block to the file's own endings rather than leaving the
file with a mix of both.

Reading exactly makes the match in `apply_unique_replacement` stricter,
because `read_file` still normalises what it shows the model. A multi-line
`old_content` copied from it is therefore LF even when the file is CRLF,
and matching it byte-for-byte would refuse an edit that is correct in
every way that matters. A failed match is now retried once with
`old_content` rewritten to the file's own endings, and accepted only if
that matches exactly one place, so nothing ambiguous is guessed at. The
replacement text is rewritten the same way, so the repaired edit cannot
reintroduce a mix.

Tests assert on raw bytes, since the decoded text is the same either way,
and cover both directions — an LF file must stay LF and a CRLF file must
stay CRLF, so the fix cannot be "normalise everything to LF" — plus both
directions of the retargeted match.

## Test plan
- [x] `pytest tests/unit/test_file_io_line_endings.py
  tests/unit/agents/test_file_edit_semantics.py
  tests/unit/agents/test_read_file_line_ranges.py
  tests/unit/agents/test_file_tools_text_encoding.py` — 125 passed
- [x] `pytest tests/unit -k "file or edit or text or io"` — no new
  failures against the same selection on `main`
- [x] `python util/lint.py --black --isort`

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added tests Test changes agents labels Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

Agent file edits no longer rewrite every line ending in the file. Reads and writes now pass bytes through unchanged, and the tests check raw bytes in both directions. One gap remains: a CRLF file can still come out with mixed line endings after an ordinary edit, and the diff preview now has the whole-file problem this PR fixes elsewhere.

  • Mixed endings on a normal edit to a CRLF file. When the text to replace is on a single line, it matches the file straight away, so the replacement text is used exactly as the model sent it. The model sends \n line endings, so adding a line or two to a CRLF file leaves bare LF lines in the middle of it. Before this PR, Windows rewrote the whole file as CRLF, so the result was at least consistent. Convert the new text to the file's line endings on every successful match, not only on the retry path.
  • The diff preview now flags every line as changed on CRLF files. It reads the file byte-exact but compares it against model text that uses LF endings. Bring both sides to the same endings before diffing.

Real-world evidence

N/A: the evidence stage reports no CLI, API or MCP surface changed. The verdict rests on static review and the PR's unit-test runs. The Windows CI item in the PR's test plan is still unchecked, and Windows is the platform this bug affects.

🔍 Technical details

🟡 New content is only converted to the file's endings on the retry path (src/gaia/agents/tools/file_edit.py:367-377)

new_content = _retarget_newlines(...) runs only when the first _match_offsets finds nothing. A single-line old_content (e.g. "return 1") always matches on the first try, so a multi-line new_content such as "return 1\n # note" is spliced into a CRLF file with bare \n. The CRLF tests miss this because test_edit_file_leaves_a_crlf_file_crlf uses a single-line new_content. Retarget unconditionally once offsets are known:

    if not offsets:
        # Retry once with old_content's line endings rewritten to the file's.
        # Only a line-ending difference can be rescued this way, and only when
        # it then matches exactly one place, so nothing ambiguous is guessed at.
        retargeted = _retarget_newlines(old_content, current_content)
        if retargeted != old_content:
            retargeted_offsets = _match_offsets(current_content, retargeted)
            if len(retargeted_offsets) == 1:
                old_content = retargeted
                offsets = retargeted_offsets

    if offsets:
        new_content = _retarget_newlines(new_content, current_content)

Also add a test: a CRLF file, single-line old_content, multi-line LF new_content, then assert lone_lf == 0. This affects edit_file and edit_python_file in both mixins.

🟡 generate_diff compares the exact file against normalised model text (src/gaia/agents/tools/file_io_tools.py:987)

Since this change, original_content keeps its \r\n endings while new_content from the model uses \n. difflib.unified_diff therefore marks every line of a CRLF file as changed, which is the "400 lines changed" symptom the PR describes. This read is not part of a write round trip, so either revert it to plain open(...) or run new_content through _match_newlines(new_content, original_content) before diffing.

🟢 Duplicated helpers

_read_exact / _write_exact appear verbatim in both file_io_tools.py and file_tools.py. _match_newlines in file_io_tools.py does the same job as _retarget_newlines in file_edit.py. Both modules already import from file_edit.py, so defining the helpers once there would leave a single copy to maintain.

🟢 Behaviour change worth noting: write_file with LF content over an existing CRLF file now produces an LF file on Windows. Before, Windows wrote it as CRLF. That is probably the intended result, but the PR description doesn't mention it.

Strengths

  • The tests assert on raw bytes and cover both directions, so "normalise everything to LF" can't pass as a fix.
  • The retry for a failed match accepts a result only when it matches exactly one place, so it never has to guess between candidates.
  • The PR states the reasoning clearly, including why read_file's display was left alone.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit c5c9141 Oct 6, 2026
@kovtcharov-amd
kovtcharov-amd deleted the fix/preserve-line-endings-in-file-tools branch October 6, 2026 09:08
kovtcharov-amd added a commit that referenced this pull request Oct 6, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@kovtcharov-amd kovtcharov-amd mentioned this pull request Oct 6, 2026
5 of 10 tasks
grtninja pushed a commit to grtninja/gaia that referenced this pull request Oct 6, 2026
# GAIA v0.25.0 Release Notes

## Breaking Changes

- **`gaia chat` runs the flagship GAIA agent**, the same agent as the
TUI and Agent UI, instead of the retired ChatAgent. `gaia chat -q "…"`
queries it once. (PR [amd#4520](amd#4520))
- **`gaia talk` transcribes and speaks through Lemonade** — it uses
Lemonade's Whisper and `kokoro-v1`, and stops with the reason if
Lemonade is down. The `[audio]` extra is gone; `pip install
amd-gaia[audio]` only warns. (PR
[amd#4665](amd#4665))
- **The Agent UI runs only the GAIA agent** — the Agent Hub home, agent
picker, and custom-agent export/import are removed from the UI. The CLI
and eval harness can still run other agents. (PR
[amd#4509](amd#4509))
- **The retired nomic embedder is removed** — memory saved with it is
re-embedded on first start. (PR
[amd#4489](amd#4489))
- **Indexed documents are re-indexed once** — the document cache is now
keyed on chunking settings, so the first query after upgrading rebuilds
it. (PR [amd#4624](amd#4624))
- **A skill that asks for more than its tier allows no longer loads** —
the permission ceiling is checked at every load, so a skill already on
disk that exceeds it is refused with the reason. (PR
[amd#4849](amd#4849))

## What's New

- **A larger local model where the GPU holds it** — `gaia init` picks
Qwen3.6-35B-A3B on a 64 GB+ Strix Halo or a 32 GB GPU and keeps
Gemma-4-E4B elsewhere, with each context window sized to available
memory. Qwen3.8 Flash Next is opt-in on 128 GB Strix Halo. (PRs
[amd#4479](amd#4479),
[amd#4670](amd#4670),
[amd#4671](amd#4671),
[amd#4658](amd#4658),
[amd#4655](amd#4655))
- **The Agent UI opens straight to a chat and sets itself up** — a fresh
install runs the same setup steps as the TUI in the window, with
provider, key and model settings, permission modes, and revocable
"always allow" grants. (PR
[amd#4509](amd#4509))
- **A much smaller install** — PyTorch, transformers and accelerate are
gone from the core, `[ui]` and `[talk]` installs; the desktop app's
Python environment drops from ~0.9 GB to ~0.1 GB on Windows. (PRs
[amd#4666](amd#4666),
[amd#4621](amd#4621),
[amd#4665](amd#4665))
- **One Windows installer for the desktop app and the terminal**, with a
GAIA Windows Terminal profile. (PRs
[amd#4720](amd#4720),
[amd#4707](amd#4707))
- **A "no" holds** — a declined action isn't retried through another
tool; the agent asks again with the exact command. The TUI gains
Shift+Tab permission modes. (PRs
[amd#4460](amd#4460),
[amd#4461](amd#4461))
- **Answers are checked against what the tools actually did** — the
agent looks at files you name before answering about them, and a short
plain-English note appears only when work isn't confirmed. (PRs
[amd#4532](amd#4532),
[amd#4501](amd#4501),
[amd#4508](amd#4508))
- **One shell session** — commands share a persistent shell, risky ones
ask instead of being refused, and "full access" is one setting
everywhere. (PR [amd#4408](amd#4408))
- **Memory surfaces what's relevant to each request, with its age**,
keeps what fixed a failure as a lesson for that workspace, and extracts
after the answer instead of before it. (PRs
[amd#4061](amd#4061),
[amd#4066](amd#4066),
[amd#4406](amd#4406))
- **`gaia.log` stays bounded** — it rotates at 10 MB with three backups
(`GAIA_LOG_MAX_MB`, `GAIA_LOG_BACKUPS`). (PRs
[amd#4668](amd#4668),
[amd#4760](amd#4760))
- **Child processes get a trimmed environment** — shell, MCP and skill
children no longer inherit GAIA's internal credentials; `GAIA_NO_DOTENV`
skips loading `.env`. (PRs
[amd#4635](amd#4635),
[amd#4659](amd#4659))
- **Every file tool checks the allowed-folders rule**, and paths inside
an inline shell script are checked one by one. (PRs
[amd#4656](amd#4656),
[amd#4758](amd#4758))

## Bug Fixes

A selection of the user-visible fixes; the full list is in the changelog
below.

- **Local Qwen models no longer loop on repeated tool calls** — they run
with their model card's sampling instead of near-greedy settings. (PR
[amd#4655](amd#4655))
- **Qwen3.6 runs on a Strix Halo with an older AMD driver** — it used to
crash on its first token, so `gaia init` failed; GAIA now turns off the
Vulkan path that crashes on those drivers only, and newer drivers keep
the faster path. (PR [amd#4674](amd#4674))
- **A long PDF no longer hangs or times out while indexing** — big
documents index in the background, and PDFs that only carry
owner-password restrictions now index. (PRs
[amd#4754](amd#4754),
[amd#4679](amd#4679),
[amd#4734](amd#4734))
- **Document answers cite the right document and page**, and every chunk
is embedded whole instead of its first 1,200 characters. (PRs
[amd#4688](amd#4688),
[amd#4723](amd#4723))
- **`gaia init` fails when model verification fails**, shows downloads
live, and no longer hangs while Lemonade hashes a large model. (PRs
[amd#4620](amd#4620),
[amd#4564](amd#4564),
[amd#4673](amd#4673))
- **`gaia init` adds a profile's extras without reinstalling GAIA**,
which failed on Windows while `gaia.exe` was running. (PR
[amd#4672](amd#4672))
- **Every SDK and CLI entry point starts GAIA's own Lemonade**, and
every call goes to the resolved URL with its API key. (PRs
[amd#4657](amd#4657),
[amd#4627](amd#4627))
- **Local chat stays cached across turns** — the system prompt is fixed
and side requests no longer evict the conversation. (PRs
[amd#4698](amd#4698),
[amd#4543](amd#4543))
- **A cloud model selection never preloads a local model**, and cloud
models no longer get llama.cpp-only fields. (PRs
[amd#4757](amd#4757),
[amd#4709](amd#4709))
- **The Agent UI stops claiming "100% local" while chat goes to a cloud
model**, and the chat header names the session's real model. (PRs
[amd#4711](amd#4711),
[amd#4550](amd#4550))
- **Enter can't approve a permission prompt**, and prompts wait 10
minutes before timing out. (PRs
[amd#4700](amd#4700),
[amd#4699](amd#4699))
- **Stopping a run keeps the turn and its steps visible.** (PR
[amd#4743](amd#4743))
- **A file edit keeps the file's line endings** — a one-line edit on
Windows used to rewrite every line ending, so a small change showed up
as the whole file changed. (PR
[amd#4864](amd#4864))
- **A broken agent install is reported as broken, not as missing** — the
error names both package versions and the real import error, and
suggests a reinstall that works. (PR
[amd#4588](amd#4588))
- **A meeting or document summary is never cut off mid-sentence** — a
thinking model used to spend the summary's output budget reasoning, so a
long meeting lost its second half. (PR
[amd#4827](amd#4827))
- **"What can you do?" gets an answer** instead of an attempt to
generate an image of the question. (PR
[amd#4809](amd#4809))
- **Writes and approved edits can't reach credential folders**
(`~/.ssh`, `~/.aws`, `~/.kube` and others), and shell option values and
script arguments get the same path check as plain paths. (PRs
[amd#4848](amd#4848),
[amd#4847](amd#4847))
- **`gaia connectors connect google --grant-agent installed:gaia`
works** — it used to fail with no sign-in link for the shipped agent; it
now asks for read-only Gmail and grants exactly that. (PR
[amd#4859](amd#4859))
- **A "no" to creating a folder also covers writing into it** — GAIA no
longer asks again to create the same folder a different way. (PR
[amd#4857](amd#4857))
- **`gaia skill import` runs the same checks as a hub install** — https
only, the audit, the permission ceiling, and an explicit opt-in for
skills that ship code. (PR
[amd#4849](amd#4849))
- **Memory off and private chats apply to scheduled and background runs
too.** (PRs [amd#4842](amd#4842),
[amd#4821](amd#4821))
- **Memory stops false apologies and reusing deleted values**, saves
"remember this" requests, and skips loading the embedder when memory is
off. (PRs [amd#4708](amd#4708),
[amd#4756](amd#4756))
- **A reply cut off by the token limit is continued** instead of ending
mid-sentence, and a tool call cut off at the output cap is retried
shorter. (PRs [amd#4054](amd#4054),
[amd#4556](amd#4556))
- **The TUI remembers the chosen model** across restarts and
agent-process restarts. (PRs
[amd#4497](amd#4497),
[amd#4710](amd#4710))
- **`gaia uninstall --purge` removes installer binaries, PATH edits and
daemon state.** (PR [amd#4413](amd#4413))

## Contributors

Thanks to the community members who contributed to this release:

- [@itomek](https://github.com/itomek) — the flagship's browser and tool
picker, one shell session, memory extraction off the answer path, stale
tool-result eviction, the TUI's sourced Fireworks rankings, and
`--purge` cleanup (PRs [amd#4407](amd#4407),
[amd#4408](amd#4408),
[amd#4406](amd#4406),
[amd#4465](amd#4465),
[amd#4414](amd#4414),
[amd#4413](amd#4413))
- [@mikemikimike](https://github.com/mikemikimike) — chained shell
command timeouts, bundled skills in frozen builds, and sidecar cleanup
reporting (PRs [amd#4463](amd#4463),
[amd#4467](amd#4467),
[amd#4419](amd#4419))
- [@Hashim1999164](https://github.com/Hashim1999164) — honoring
`GAIA_HOME` for cache, chat and index paths (PR
[amd#4381](amd#4381))
- [@krtadev](https://github.com/krtadev) — `~` expansion in file and
document tool paths (PR [amd#4576](amd#4576))
- [@aksh8b](https://github.com/aksh8b) — telling a broken agent install
apart from a missing one, with a reinstall hint that works (PR
[amd#4588](amd#4588))
- [@Dev-next-gen](https://github.com/Dev-next-gen) — numeric prerelease
ordering in the hub (PR [amd#4517](amd#4517))
- [@vianmangal](https://github.com/vianmangal) — recording denied tool
calls as unsuccessful in memory (PR
[amd#4412](amd#4412))
- [@GaetanVDB07](https://github.com/GaetanVDB07) — links to upstream
credential setup guides for connectors (PR
[amd#4669](amd#4669))
- [@rradjabi](https://github.com/rradjabi) — moving self-hosted CI jobs
to the xsj-dispatch runner pool (PR
[amd#4534](amd#4534))




## Also in this PR

- **Hub floors raised to core 0.25.0** — the chat agent imports the
allowed-folders check that only 0.25.0 ships (amd#4656), so
`gaia-agent-chat` goes to 0.2.0 with `amd-gaia>=0.25.0`, and the
flagship requires `amd-gaia>=0.25.0` and `gaia-agent-chat>=0.2.0`.
- **Two follow-ups held back until that floor could rise** —
`~/.gaia/logs/gaia-agent.log` now rotates like `gaia.log` (amd#4668), and
the chat agent's Python runners and Windows notification start their
children without GAIA's internal credentials (amd#4659).

The full changelog (300 commits) is in `docs/releases/v0.25.0.mdx`.

## Test plan

- [x] `util/validate_release_notes.py docs/releases/v0.25.0.mdx --tag
v0.25.0` passes; narrative is 590 words (cap 600)
- [x] `util/check_component_core_api.py --release-version 0.25.0` passes
- [x] `pytest tests/unit/test_docs_json_release.py
tests/unit/test_check_component_core_api.py
hub/agents/chat/python/tests/test_chat_dependency_floor.py
hub/agents/chat/python/tests/test_dependency_floor.py
hub/agents/gaia/python/tests/test_gen_binaries_lock.py` (72 passed)
- [x] `pytest hub/agents/gaia/python/tests/test_stdio.py` (91 passed,
incl. the new rotation test); `pytest
tests/unit/test_execute_python_file_cwd.py
tests/unit/test_run_python_tool.py` (42 passed)
- [ ] `mintlify validate` (docs CI job) — timed out locally, relying on
CI
- [x] Full flagship eval on main 9ce6598
(https://github.com/amd/gaia/actions/runs/37436249503): 181/308
scenarios pass vs 100/183 for the same categories in the baseline, 39
FAIL→PASS, 13 PASS→FAIL (two fixed in amd#4878, held until after rc1);
retrieval lane still finishing at merge time
- [ ] CI green

## Release checklist

- [ ] Publish `gaia-agent-chat` 0.2.0 only after core 0.25.0 is on PyPI
— it fails at import on an older core
- [ ] Build Installers on the merged SHA (pre-tag verification) before
pushing `v0.25.0`
- [ ] terminal-hub 0.25.0 published before the flagship npm release
consumes `binaries.lock.json`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Co-authored-by: Kalin Ovtcharov <[email protected]>
grtninja pushed a commit to grtninja/gaia that referenced this pull request Nov 4, 2026
…edits (amd#6607)

## 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

amd#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 amd#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

- [x] `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)
- [x] `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
- [x] `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.
- [x] `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

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

1 participant