From e6c64ebf7edc53155b7e8ee647d1cb381cb45edb Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Mon, 31 Mar 2025 11:23:46 -0300 Subject: [PATCH] assistant2: Fail find-replace tool if both strings are equal (#27783) Models seem to do this ever so often and get very confused. Failing here helps them recover. Release Notes: - N/A Co-authored-by: Richard Feldman --- crates/assistant_tools/src/find_replace_file_tool.rs | 4 ++++ crates/assistant_tools/src/find_replace_tool/description.md | 2 ++ 2 files changed, 6 insertions(+) diff --git a/crates/assistant_tools/src/find_replace_file_tool.rs b/crates/assistant_tools/src/find_replace_file_tool.rs index a4eca10d38eedb23d65e095c997c7024c0aac856..65ab46bcbc8bdd48500c37350b655126e492d285 100644 --- a/crates/assistant_tools/src/find_replace_file_tool.rs +++ b/crates/assistant_tools/src/find_replace_file_tool.rs @@ -182,6 +182,10 @@ impl Tool for FindReplaceFileTool { return Err(anyhow!("`find` string cannot be empty. Use a different tool if you want to create a file.")); } + if input.find == input.replace { + return Err(anyhow!("The `find` and `replace` strings are identical, so no changes would be made.")); + } + let result = cx .background_spawn(async move { replace_exact(&input.find, &input.replace, &snapshot).await diff --git a/crates/assistant_tools/src/find_replace_tool/description.md b/crates/assistant_tools/src/find_replace_tool/description.md index ba2b05708e71231c24a96809fd8cf9bf629f7976..63308cf7ab8f5607919b05ce95f8c525859e6750 100644 --- a/crates/assistant_tools/src/find_replace_tool/description.md +++ b/crates/assistant_tools/src/find_replace_tool/description.md @@ -5,3 +5,5 @@ This tool is the preferred way to make edits to files. If you have multiple edit You should use this tool when you want to edit a subset of a file's contents, but not the entire file. You should not use this tool when you want to replace the entire contents of a file with completely different contents. You also should not use this tool when you want to move or rename a file. You absolutely must NEVER use this tool to create new files from scratch. If you ever consider using this tool to create a new file from scratch, for any reason, instead you must reconsider and choose a different approach. DO NOT call this tool until the code to be edited appears in the conversation! You must use another tool to read the file's contents into the conversation, or ask the user to add it to context first. + +Never call this tool with identical "find" and "replace" strings. Instead, stop and think about what you actually want to do.