Skip to content

fix(tools): match and keep CRLF line endings in diff_edit - #2007

Open
Mahdi-Hasan wants to merge 4 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1993-diff-edit-crlf
Open

Mahdi-Hasan wants to merge 4 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1993-diff-edit-crlf

Conversation

@Mahdi-Hasan

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

Copy link
Copy Markdown
Contributor

Description

diff_edit matched each SEARCH block against the raw file text. Models send LF, so a multi-line SEARCH block on a CRLF file was refused with Search block 1 was not found in file, and REPLACE text that added lines wrote LF lines into the file. Fixes #1993.

It reuses fitEditToLineEndings from source/utils/line-endings.ts (#1961), as proposed on the issue.

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 diff-edit.spec.tsx: the repro from the issue, multiple blocks that add lines in a CRLF file, the validator, an LF block in the LF region of a mixed file, and recovery evidence for a missed block on a CRLF file.

  • Four fail on main. The recovery test retries with the suggested actualText and checks the file stays CRLF. The mixed-file test passes on both and guards against over-converting.

  • diff-edit.spec.tsx: 36 pass. Types, biome and knip pass.

  • Developed on Windows; I ran the focused spec, not pnpm test:all.

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)

🤖 Generated with Claude Code

@github-actions github-actions Bot added the area:tools Tool implementations and tool-calling label Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

nc-review: needs work — 1 blocking, 2 nits

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

The PR makes diff_edit match and preserve CRLF line endings by fitting every SEARCH/REPLACE block to the file's dominant line endings with the existing fitEditToLineEndings helper, applied consistently in the executor, the validator and the VS Code diff path; the implementation itself is correct and closes the repro in #1993, and the four new tests genuinely exercise the change. The blocker is that open PR #1994 ("fix(diff-edit): match SEARCH blocks on CRLF files", branch fix/diff-edit-crlf) solves the same problem in the same file, so a maintainer needs to pick one. Two smaller points: the changeset's mixed-file claim is only true for multi-line blocks, and the changed failure path (the search text handed to EditRecoveryError is now the fitted one) has no test.

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

🔴 blocking · duplicate · source/tools/file-ops/diff-edit.tsx:135

This appears to duplicate the still-open PR #1994, "fix(diff-edit): match SEARCH blocks on CRLF files" (branch fix/diff-edit-crlf, author SatriaBagusNurhadianto), which targets the same issue #1993, the same tool and the same file with the same symptom (multi-line SEARCH blocks not matching on a CRLF file). Two contributors fixing one bug is exactly the pattern the project rubric warns about in a queue this size.

I could not read #1994's diff from the review context, so I cannot prove the two implementations are identical — but the titles, branches and target issue are the same problem, and merging both would land the same fix twice and close #1993 twice. A maintainer should compare the two diffs and keep one; this PR is the more complete of the two as far as the diff shows (it fits the blocks in the executor, the validator and the VS Code preview path, and adds four tests including a validator test), so keeping this one and closing #1994 would be the reasonable outcome.

Note also that the changeset body ends with "Closes #1993." — whichever PR lands should own that.

⚪ nit · changeset · .changeset/diff-edit-crlf.md

The changeset states "an LF block that only matches an LF part of a mixed file is applied as sent". That holds only for multi-line SEARCH blocks. fitEditToLineEndings decides whether to keep the edit as sent from the search text alone:

const fittedOld = toLineEnding(oldStr, ending);
if (
    fittedOld !== oldStr &&
    !fileContent.includes(fittedOld) &&
    fileContent.includes(oldStr)
) {
    return {oldStr, newStr};
}
return {oldStr: fittedOld, newStr: toLineEnding(newStr, ending)};

For a single-line SEARCH (no \n to convert) fittedOld === oldStr, so the guard can never fire and the REPLACE is always rewritten to the file's dominant ending. On a mixed file such as a\r\nb\r\nc\r\nd\r\nx\ny\n, a single-line edit of x → X\nY writes X\r\nY, i.e. a CRLF is inserted into the LF region — so the block is not "applied as sent".

This is inherited from the shared helper and matches string_replace's existing behaviour, so the fix belongs either in the helper or in the wording. Suggest softening the sentence to "a multi-line LF block that only matches an LF part of a mixed file is applied as sent" so the changelog does not overstate what happens.

⚪ nit · tests · source/tools/file-ops/diff-edit.spec.tsx

The failing-search path changed shape without coverage: validateBlocks now passes the fitted (CRLF) block.search into EditRecoveryError, so the recovery payload built by buildEditRecovery sees a CRLF search against a CRLF file instead of the LF text the model sent. I traced the cases in source/utils/edit-recovery.ts and the outcome looks equivalent to before (the CRLF-fitted text either matches and no error is raised, or the candidate analysis lands on the whitespace/fuzzy branch as it did before), but nothing pins that down.

A single test that triggers a failed search on a CRLF file and asserts the error still carries usable recovery evidence (a candidate_found status with an actualText the model can copy) would lock the behaviour in and guard the next change to this path.


🔴 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 10, 2026
@Mahdi-Hasan

Mahdi-Hasan commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three addressed:

  1. Duplicate of fix(diff-edit): match SEARCH blocks on CRLF files #1994: addyCooks assigned [Bug] diff_edit fails to match SEARCH blocks on CRLF files #1993 to me and asked the authors of fix(diff-edit): match SEARCH blocks on CRLF files #1994 and fix(tools): match and keep CRLF line endings in diff_edit #1995 to close theirs (comment). fix(tools): match and keep CRLF line endings in diff_edit #1995 is closed; fix(diff-edit): match SEARCH blocks on CRLF files #1994 is still open.
  2. Changeset wording: right, the keep-as-sent guard only fires when the SEARCH text has a line break. The changeset and the PR description now say "a multi-line LF block". Single-line blocks are fitted to the dominant ending, the same as string_replace. (b73399a)
  3. Recovery on CRLF: added diff_edit validator: a missed block on a CRLF file still gives usable recovery evidence. It misses a block on a CRLF file, checks the evidence is candidate_found, retries with the candidate's actualText, and checks the file stays CRLF. It fails on main and passes here. (b73399a)

@Mahdi-Hasan
Mahdi-Hasan force-pushed the fix/1993-diff-edit-crlf branch 2 times, most recently from 951dea1 to c1e1836 Compare November 17, 2026 05:54
@akramcodez

Copy link
Copy Markdown
Member

Thanks @Mahdi-Hasan, this fixes #1993 properly. Reusing fitEditToLineEndings and applying the fitted blocks in the tool, the validator and the VS Code diff means the preview matches what lands on disk, and all three file editors now keep CRLF.

What looks good

  • LF files are untouched, and the mixed-file guard only applies to multi-line blocks, as the changeset now says.
  • The new tests cover the repro, multiple blocks, the validator, the mixed file and recovery evidence on a CRLF file, and four of them fail on main. I ran the diff-edit and line-endings specs on the branch (44 tests) and they pass. All checks are green and the changeset is in place.

What to fix before merge

I'll close #1994 in favour of this one. Happy to look again once it is rebased.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @Mahdi-Hasan, thanks for this PR! It looks like a maintainer has left feedback
or review activity and there are still some outstanding items to wrap up.

Whenever you get a chance, could you take a look at the open comments?
If anything is unclear or you'd like a hand, just reply here and we'll help you get it across the line.

Mahdi-Hasan and others added 4 commits November 26, 2026 16:52
Models send SEARCH/REPLACE blocks with LF line breaks, so a multi-line
block never matched a CRLF file. Fit each block with fitEditToLineEndings
before validating and applying it, in the tool, the validator and the
VS Code diff.

Closes Nano-Collective#1993

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pin the failed-search path now that the fitted search text reaches
EditRecoveryError: the candidate's actualText must apply cleanly and keep
CRLF. Also limit the changeset's mixed-file claim to multi-line blocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Upstream's context-visibility firewall needs the lines marked visible,
so the CRLF validator tests use markFileSeenAndVisible.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Block 2 is matched in block 1's output, so block 1's fitted lines must be
CRLF. A second test checks that a fitted replacement which repeats block 2's
search is rejected by the ordered dry run from Nano-Collective#2248.
@Mahdi-Hasan
Mahdi-Hasan force-pushed the fix/1993-diff-edit-crlf branch from c1e1836 to e254ebb Compare November 26, 2026 10:54
@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

Thanks, rebased onto main. All three call sites now fit the blocks first, then use #2248's validateBlocks dry run (executor, validator, VS Code diff).
I added two ordered-block CRLF tests. In one, block 2 is matched in block 1's output. In the other, block 1's fitted text makes block 2 ambiguous, so it's rejected.
One note: I couldn't test a block 2 SEARCH that only exists after block 1. validateBlocks still requires each SEARCH to match the original once, so that edit is refused on LF files too.

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] diff_edit fails to match SEARCH blocks on CRLF files

2 participants