Skip to content

fix(tools): match and keep CRLF line endings in string_replace - #1961

Merged
addyCooks merged 4 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1930-string-replace-crlf
Nov 7, 2026
Merged

addyCooks merged 4 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1930-string-replace-crlf

Conversation

@Mahdi-Hasan

@Mahdi-Hasan Mahdi-Hasan commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

string_replace compared old_str with the raw file text. Models send LF, so a multi-line edit to a CRLF file was refused with Content not found in file, and an edit that added lines wrote LF lines into the file. Fixes #1930.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Changeset

  • Added a changeset (pnpm changeset) describing this change for the changelog

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files

  • All existing tests pass (pnpm test:all completes successfully)

  • Tests cover both success and error scenarios

  • 5 new tests in string-replace.spec.tsx: both repros from the issue, the validator, the preview, and the result preview in a mixed-ending file. All fail on main.

  • 1 new test in acp-tool-call.spec.ts: the ACP diff for an LF edit to a CRLF file. Fails on main.

  • 6 new tests in source/utils/line-endings.spec.ts, including the mixed-file case.

  • string-replace.spec.tsx + acp-tool-call.spec.ts + line-endings.spec.ts: 88 pass. Types, biome and knip pass.

  • Developed on Windows, where pnpm test:ava has unrelated failures on main (POSIX paths). CI on Linux should settle it.

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

Checklist

  • If this was for an open issue, I was assigned to it
  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

string_replace compared old_str with the raw file text. Models send LF,
so a multi-line edit to a CRLF file was not found, and new lines were
written as LF into a CRLF file.

Add fitEditToLineEndings: when most lines of the file end in CRLF,
convert old_str and new_str to CRLF. Keep the edit as sent when only
the LF search text matches (an LF region of a mixed file). Use it in
the tool, its validator, the VS Code diff and the preview.

Closes Nano-Collective#1930

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

nc-review: needs work — 1 blocking, 2 important, 1 nit

@Mahdi-Hasan — there is a blocking item below.

The core CRLF matching and writing logic in string_replace is sound: the new fitEditToLineEndings / detectLineEnding utilities correctly match an LF-authored old_str against a CRLF file and re-fit the replacement to the file's dominant line ending, and the mixed-ending fallback is a sensible design. Two gaps block merging: the ACP diff path was not updated, so the diff an editor user approves for a CRLF file no longer matches what lands on disk, and the post-execution terminal preview re-derives the line-ending fit from the already-written file, which mislocates the edit in mixed-ending files. The PR also duplicates the still-open PR #1950.

Possible duplicate of #1950 — worth checking before going further.

🔴 blocking · duplicate · source/tools/file-ops/string-replace.tsx

This PR duplicates the still-open PR #1950: identical title (fix(tools): match and keep CRLF line endings in string_replace), same target issue #1930, and overlapping changes to the same CRLF-matching behaviour in string_replace. Only one of the two should land, otherwise the same fix is reviewed and merged twice and the second merge will conflict.

The PR description notes that the issue was assigned to you after addyCooks pointed at it, so the maintainer may prefer to keep this one — but either way one of #1950 or #1961 needs to be closed before this can merge. If this one is kept, please note in the description which PR supersedes the other so the decision is visible to reviewers.

🟠 important · correctness · source/acp/acp-tool-call.ts:219

The ACP diff path still matches and replaces on the raw old_str/new_str the model sent, so it was not updated along with the tool:

const occurrences = current.split(oldStr).length - 1;
if (occurrences !== 1) {
	return {type: 'diff', path: absPath, oldText: oldStr, newText: newStr};
}

For a CRLF file the model sends old_str with LF endings; current.split(oldStr) therefore finds 0 occurrences, the guard falls through to the fallback hunk, and the user is shown a diff built from LF text against a CRLF file. Meanwhile the tool itself now writes the CRLF-fitted text. That breaks the invariant asserted in source/acp/acp-tool-call.spec.ts — "The previewed diff has to be the diff that lands on disk" — for exactly the scenario this PR is about. Since ACP is how Zed and other editors consume nanocoder, this is the surface where the user reviews and authorises the edit, so an inaccurate diff is worse than no diff.

Fix by running old_str/new_str through the same fitEditToLineEndings helper the tool uses before computing occurrences and building the diff, and add a spec covering a CRLF file.

🟠 important · correctness · source/tools/file-ops/string-replace-preview.tsx:62

The preview re-derives the line-ending fit from the file content it just read, but in the isResult (post-execution) case that content is the file after the write, so the fallback that produced the correct replacement is no longer reproducible:

  • Before execution, fileContent.includes(oldStr) is true, so the helper falls back to "keep the model's LF endings as sent".
  • After execution, fileContent no longer contains oldStr, so that fallback cannot fire and the helper re-fits new_str to the file's dominant ending (CRLF).
  • fileContent.indexOf(searchStr) at line 125 then searches for the CRLF-fitted text, but the file contains the LF-fitted text that was actually written, so matchIndex becomes -1.

With matchIndex === -1, beforeContent is empty and the preview reports the change at line 1 with the wrong surrounding context — exactly the mixed-ending case the new fallback exists to handle. The preview should either derive the fit from the pre-write content, or search using the text that was actually written (the new_str returned by the tool) rather than re-fitting it.

Please add a preview spec for the mixed-ending case so the reported location is asserted, not just the written bytes.

⚪ nit · completeness · source/utils/line-endings.ts

The new fitEditToLineEndings helper is written generically, but only string_replace uses it. diff_edit has the same blindness: its SEARCH blocks keep the line endings the model sent (LF), so a SEARCH block authored against a CRLF file never matches and the tool reports "Search block was not found in file".

This is outside the scope of issue #1930, so it is not a blocker — but since the utility is already generic, wiring it into diff_edit (and noting it in the changeset) would close the same class of bug in the other editor-style tool rather than leaving a known-broken sibling behind.


🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional

Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with /re-review.

@github-actions github-actions Bot added the agent:needs-work nc-review found blocking findings label Nov 5, 2026
The ACP diff still matched the raw LF old_str, so for a CRLF file it
showed a hunk that did not match what the tool wrote. Run the edit
through fitEditToLineEndings there too.

The result preview re-fitted against the file after the write. In a
mixed-ending file an edit kept as sent was then not found, and the
preview reported line 1. Use new_str as sent when that is what the
file holds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

Addressed the nc-review findings in 6d9c7ab:

  • ACP diff (acp-tool-call.ts) now runs the edit through fitEditToLineEndings, so the diff an editor shows for a CRLF file is what lands on disk. New spec.
  • The result preview uses new_str as sent when that is what the file holds. A mixed-ending edit at lines 5-6 was reported at lines 1-2; new spec asserts the location.
  • diff_edit has the same CRLF miss. I left it out to keep this PR to [Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings #1930 and can open a follow-up.

The duplicate flag (#1950) is for a maintainer to decide. I've claimed #1930 on the issue.

@addyCooks addyCooks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Mahdi-Hasan, the CRLF fitting looks right, and I checked it locally: with main merged in, the type check and Biome pass, the line-ending, string_replace and ACP specs pass (95), and the new tests fail without the change. The ACP diff and result-preview fixes from the review bot are in.

Two small things before merge:

  1. The branch conflicts with main in source/tools/file-ops/string-replace.spec.tsx (both sides added tests at the end). Please merge main and keep both sets.
  2. Please drop the "Generated with Claude Code" line from the PR description.

I'll approve the CI run once the branch is updated. Happy to re-review then.

…lace-crlf

# Conflicts:
#	source/tools/file-ops/string-replace.spec.tsx
@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

@addyCooks Thanks! Both done:

  1. Merged main in 0ff553b and kept both test sets at the end of string-replace.spec.tsx (the CRLF tests and the mutation-firewall test).
  2. Removed the "Generated with Claude Code" line from the description.

With main merged: type check and Biome pass, and the line-ending, string_replace and ACP tool-call specs pass (95).

The mixed-ending result test looked for the plain text 'Location: lines 5-6'. CI renders colour codes between 'Location: ' and the line numbers, so the substring was not found. Strip the styling first, as the read-file spec does.
@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

Thanks @addyCooks for fixing that test in 42d04ce, and sorry for the miss. Colour was off in my local Windows runs, so the styled Location: line never showed up. I now run the specs with FORCE_COLOR=1, which reproduces the CI failure on the old test and passes with your fix. I've merged your commit into #1992 too, since it carries the same test.

@addyCooks addyCooks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both points from my review are done (branch merged, description cleaned) and the CRLF fitting looks right. The new tests fail without the change. A follow-up issue for the same CRLF miss in diff_edit would be welcome.

Approving 🎉

@addyCooks
addyCooks merged commit f9d89b1 into Nano-Collective:main Nov 7, 2026
18 checks passed
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…Collective#1961)

* fix(tools): match and keep CRLF line endings in string_replace

string_replace compared old_str with the raw file text. Models send LF,
so a multi-line edit to a CRLF file was not found, and new lines were
written as LF into a CRLF file.

Add fitEditToLineEndings: when most lines of the file end in CRLF,
convert old_str and new_str to CRLF. Keep the edit as sent when only
the LF search text matches (an LF region of a mixed file). Use it in
the tool, its validator, the VS Code diff and the preview.

Closes Nano-Collective#1930

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(tools): fit CRLF edits in the ACP diff and the result preview

The ACP diff still matched the raw LF old_str, so for a CRLF file it
showed a hunk that did not match what the tool wrote. Run the edit
through fitEditToLineEndings there too.

The result preview re-fitted against the file after the write. In a
mixed-ending file an edit kept as sent was then not found, and the
preview reported line 1. Use new_str as sent when that is what the
file holds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(string-replace): strip ANSI before matching the result location

The mixed-ending result test looked for the plain text 'Location: lines 5-6'. CI renders colour codes between 'Location: ' and the line numbers, so the substring was not found. Strip the styling first, as the read-file spec does.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: addyCooks <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent:needs-work nc-review found blocking findings area:tools Tool implementations and tool-calling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings

2 participants