Skip to content

Prevent no-op replacements from corrupting CRLF files - #1772

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/noop-crlf-str-replace
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/noop-crlf-str-replace

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

When every str_replace pair is skipped because the old and new strings are identical, currentContent still contains the original CRLF bytes. Restoring CRLF before checking for changes turns each \r\n into \r\r\n. The runtime then sends a patch and reports success despite making no requested edit.

Changes

  • Check for content changes with normalized line endings before restoring the file's line endings. A no-op returns the existing error result without creating a patch.
  • Change the existing identical-pair regression to use CRLF. It fails on unmodified main and passes with the fix; no additional test cases or files are added.

Verification

  • Bun 1.3.14: the regression file changes from 31 pass / 1 fail on main to 32 pass / 0 fail.
  • Related replacement, indentation, bounded-patch and file-processing tests: 40 pass / 0 fail across 4 files.
  • A temporary-file probe runs processStrReplace → postStreamProcessing → SDK changeFile: main writes once and produces CRCRLF; this patch performs zero writes and preserves the bytes. This is a file-processing probe, not an interactive CLI test.
  • Runtime and SDK typechecks pass with the installed native TypeScript 7.0.2 compiler. bun install --frozen-lockfile --no-env-file reports no changes. git diff --check passes.
  • ESLint: 0 errors, 4 existing TypeScript resolver warnings. Prettier's whole-file check fails on the checkout; comparison with main after normalizing checkout line endings shows no new formatting differences.
  • SDK and Freebuff builds are blocked by the existing dts-bundle-generator / TypeScript resolution error (ts.sys.getCurrentDirectory is undefined), also recorded on unmodified main. Binary version/smoke checks were not run because no binary was built.

Scope / Tradeoffs

Source-audit fix; no existing issue is claimed. Successful replacements retain the current line-ending policy, including normalization of mixed-ending files. No-op detection ignores CRLF/LF differences, consistent with the existing replacement matching and identical-pair checks. Matching heuristics and client-side patch application are unchanged. Related PR #1291 guards whitespace-only fuzzy matches; this fix addresses the later no-op/line-ending boundary.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch. The bug is real: currentContent.replaceAll('\n', lineEnding) was applied before the no-op check, so a file with CRLF line endings that goes through processStrReplace with an identical old/new pair would still be converted and then falsely compared against the un-normalized initialContent, producing \r\r\n and a spurious successful patch. Moving the line-ending restoration to after the no-op check, and doing the no-op comparison via normalizeLineEndings on both sides, is the right fix and keeps the scope minimal (packages/agent-runtime/src/process-str-replace.ts).

The updated test in process-str-replace.test.ts (switching the identical-pair fixture from \n to \r\n) is a reasonable regression test and demonstrates the fix directly - good that you showed it fails on main and passes with the patch.

Two things I'd want double-checked before this lands upstream:

  1. Confirm normalizeLineEndings is already imported/available in this file (the diff doesn't show an import change, so it must already exist - worth confirming in your PR description since it's load-bearing for compilation).
  2. It might be worth an additional test case with a genuinely mixed-ending file that does contain a real replacement, to confirm the successful-replacement path still normalizes correctly post-fix, since the PR only adds coverage for the no-op case.

Overall this is a small, well-scoped, well-reasoned bug fix in an in-scope package. Worth a maintainer porting it.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants