{"record":{"id":"807f27dd56f41af8","repo":"FoundationAgents/MetaGPT","slug":"to-replace-and-new-content-must-be-different","errorCode":null,"errorMessage":"`to_replace` and `new_content` must be different.","messagePattern":"`to_replace` and `new_content` must be different\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":876,"sourceCode":"\n        edit_file_by_replace(\n            '/workspace/example.txt',\n            to_replace='line 2\\nline 3',\n            new_content='',\n        )\n\n        Args:\n            file_name: (str): The name of the file to edit.\n            to_replace: (str): The content to search for and replace.\n            new_content: (str): The new content to replace the old content with.\n        NOTE:\n            This tool is exclusive. If you use this tool, you cannot use any other commands in the current response.\n            If you need to use it multiple times, wait for the next turn.\n        \"\"\"\n        # FIXME: support replacing *all* occurrences\n\n        if to_replace == new_content:\n            raise ValueError(\"`to_replace` and `new_content` must be different.\")\n\n        # search for `to_replace` in the file\n        # if found, replace it with `new_content`\n        # if not found, perform a fuzzy search to find the closest match and replace it with `new_content`\n        file_name = self._try_fix_path(file_name)\n        with file_name.open(\"r\") as file:\n            file_content = file.read()\n\n        if to_replace.strip() == \"\":\n            if file_content.strip() == \"\":\n                raise ValueError(f\"The file '{file_name}' is empty. Please use the append method to add content.\")\n            raise ValueError(\"`to_replace` must not be empty.\")\n\n        if file_content.count(to_replace) > 1:\n            raise ValueError(\n                \"`to_replace` appears more than once, please include enough lines to make code in `to_replace` unique.\"\n            )\n        start = file_content.find(to_replace)","sourceCodeStart":858,"sourceCodeEnd":894,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L858-L894","documentation":"Raised by Editor._edit_file_by_replace at the top of the call when to_replace == new_content. A replace that changes nothing is treated as a caller error (likely a copy-paste mistake), and the tool refuses the no-op before touching the file.","triggerScenarios":"editor.edit_file_by_replace(path, to_replace=X, new_content=X); an LLM echoing the original snippet as the replacement; templating code that falls back to the original string when a substitution variable is empty.","commonSituations":"Agent pipelines where the 'new code' slot is populated from the same source as 'old code'; template default values collapsing to identity; debugging sessions where the intended change was never filled in.","solutions":["Make sure new_content actually differs from to_replace (diff the two strings before calling)","If the source of new_content defaulted to the original, fix the upstream substitution","Skip the call entirely when the strings are equal — it would be a no-op anyway"],"exampleFix":"// before\neditor.edit_file_by_replace(path, to_replace=s, new_content=s)  # ValueError\n\n// after\nif new_content != to_replace:\n    editor.edit_file_by_replace(path, to_replace=to_replace, new_content=new_content)","handlingStrategy":"validation","validationCode":"if to_replace == new_content:\n    raise ValueError('no-op replace; check that new_content was filled in')\neditor.edit_file_by_replace(path, to_replace, new_content)","typeGuard":"def is_real_replace(old: str, new: str) -> bool:\n    return old != new","tryCatchPattern":"try:\n    editor.edit_file_by_replace(path, to_replace, new_content)\nexcept ValueError as e:\n    if 'must be different' in str(e):\n        return 'no-op'  # treat as success or fix upstream substitution\n    raise","preventionTips":["Diff old/new strings before calling","Fail loudly when a template substitution leaves new_content identical to the original","Treat this error as a symptom of an upstream data-flow bug, not a file problem"],"tags":["editor","replace","no-op","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}