From b4d00ab4fb338631de039dc445936f7af2c5b79e Mon Sep 17 00:00:00 2001 From: Wen Sun Date: Tue, 22 Jul 2025 06:24:02 +0900 Subject: [PATCH] feat(edit): Prevent no-op edits (#3520) --- packages/core/src/tools/edit.test.ts | 13 +++++++++++++ packages/core/src/tools/edit.ts | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/packages/core/src/tools/edit.test.ts b/packages/core/src/tools/edit.test.ts index 84ad1daf..4ff33ff4 100644 --- a/packages/core/src/tools/edit.test.ts +++ b/packages/core/src/tools/edit.test.ts @@ -608,6 +608,19 @@ describe('EditTool', () => { /User modified the `new_string` content/, ); }); + + it('should return error if old_string and new_string are identical', async () => { + const initialContent = 'This is some identical text.'; + fs.writeFileSync(filePath, initialContent, 'utf8'); + const params: EditToolParams = { + file_path: filePath, + old_string: 'identical', + new_string: 'identical', + }; + const result = await tool.execute(params, new AbortController().signal); + expect(result.llmContent).toMatch(/No changes to apply/); + expect(result.returnDisplay).toMatch(/No changes to apply/); + }); }); describe('getDescription', () => { diff --git a/packages/core/src/tools/edit.ts b/packages/core/src/tools/edit.ts index ccba3d72..87a2ea44 100644 --- a/packages/core/src/tools/edit.ts +++ b/packages/core/src/tools/edit.ts @@ -246,6 +246,11 @@ Expectation for required parameters: display: `Failed to edit, expected ${expectedReplacements} ${occurenceTerm} but found ${occurrences}.`, raw: `Failed to edit, Expected ${expectedReplacements} ${occurenceTerm} but found ${occurrences} for old_string in file: ${params.file_path}`, }; + } else if (finalOldString === finalNewString) { + error = { + display: `No changes to apply. The old_string and new_string are identical.`, + raw: `No changes to apply. The old_string and new_string are identical in file: ${params.file_path}`, + }; } } else { // Should not happen if fileExists and no exception was thrown, but defensively: