From ab9d5770e4a91d5f524bc4eedeb3e3d151fc8803 Mon Sep 17 00:00:00 2001 From: VIOLETee0 <1842771550@qq.com> Date: Wed, 2 Dec 2026 00:57:30 +0800 Subject: [PATCH] Prevent no-op replacements from corrupting CRLF files --- .../src/__tests__/process-str-replace.test.ts | 4 ++-- packages/agent-runtime/src/process-str-replace.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/agent-runtime/src/__tests__/process-str-replace.test.ts b/packages/agent-runtime/src/__tests__/process-str-replace.test.ts index 58525b0eef..d6b3ac6907 100644 --- a/packages/agent-runtime/src/__tests__/process-str-replace.test.ts +++ b/packages/agent-runtime/src/__tests__/process-str-replace.test.ts @@ -164,12 +164,12 @@ describe('processStrReplace', () => { } }) - it('says why when the old and new strings are identical', async () => { + it('does not rewrite CRLF when the old and new strings are identical', async () => { const line = 'import kotlinx.coroutines.flow.Flow' const result = await processStrReplace({ path: 'test.kt', replacements: [{ oldString: line, newString: line, allowMultiple: false }], - initialContentPromise: Promise.resolve(`${line}\n`), + initialContentPromise: Promise.resolve(`${line}\r\n`), logger, }) diff --git a/packages/agent-runtime/src/process-str-replace.ts b/packages/agent-runtime/src/process-str-replace.ts index d167a7a648..7e4ff78fec 100644 --- a/packages/agent-runtime/src/process-str-replace.ts +++ b/packages/agent-runtime/src/process-str-replace.ts @@ -98,10 +98,11 @@ export async function processStrReplace(params: { ) } - currentContent = currentContent.replaceAll('\n', lineEnding) - - // If no successful replacements occurred, return error - if (initialContent === currentContent) { + // Line-ending normalization alone is not a successful replacement. + if ( + normalizeLineEndings({ str: initialContent }) === + normalizeLineEndings({ str: currentContent }) + ) { logger.debug( { path, @@ -117,6 +118,8 @@ export async function processStrReplace(params: { } } + currentContent = currentContent.replaceAll('\n', lineEnding) + let patch = createBoundedPatch(path, initialContent, currentContent) const lines = patch.split('\n') const hunkStartIndex = lines.findIndex((line) => line.startsWith('@@'))