{"record":{"id":"72112a1f0e5eb31a","repo":"FoundationAgents/OpenManus","slug":"no-replacement-was-performed-multiple-occurrences","errorCode":null,"errorMessage":"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique","messagePattern":"No replacement was performed\\. Multiple occurrences of old_str `(.+?)` in lines (.+?)\\. Please ensure it is unique","errorType":"exception","errorClass":"ToolError","httpStatus":null,"severity":"error","filePath":"app/tool/str_replace_editor.py","lineNumber":311,"sourceCode":"        file_content = (await operator.read_file(path)).expandtabs()\n        old_str = old_str.expandtabs()\n        new_str = new_str.expandtabs() if new_str is not None else \"\"\n\n        # Check if old_str is unique in the file\n        occurrences = file_content.count(old_str)\n        if occurrences == 0:\n            raise ToolError(\n                f\"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}.\"\n            )\n        elif occurrences > 1:\n            # Find line numbers of occurrences\n            file_content_lines = file_content.split(\"\\n\")\n            lines = [\n                idx + 1\n                for idx, line in enumerate(file_content_lines)\n                if old_str in line\n            ]\n            raise ToolError(\n                f\"No replacement was performed. Multiple occurrences of old_str `{old_str}` \"\n                f\"in lines {lines}. Please ensure it is unique\"\n            )\n\n        # Replace old_str with new_str\n        new_file_content = file_content.replace(old_str, new_str)\n\n        # Write the new content to the file\n        await operator.write_file(path, new_file_content)\n\n        # Save the original content to history\n        self._file_history[path].append(file_content)\n\n        # Create a snippet of the edited section\n        replacement_line = file_content.split(old_str)[0].count(\"\\n\")\n        start_line = max(0, replacement_line - SNIPPET_LINES)\n        end_line = replacement_line + SNIPPET_LINES + new_str.count(\"\\n\")\n        snippet = \"\\n\".join(new_file_content.split(\"\\n\")[start_line : end_line + 1])","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/tool/str_replace_editor.py#L293-L329","documentation":"Thrown by str_replace_editor when old_str appears more than once in the file. Because the tool performs a whole-file string replace, an ambiguous old_str would corrupt every match site, so it refuses to edit and reports the 1-based line numbers where matches were found.","triggerScenarios":"Passing an old_str that is a short or common snippet (e.g. a single line like 'return None', a closing brace, or an import statement) that occurs on multiple lines; multi-line old_str where one line substring-matches several places. Note: line numbers are computed per line containing old_str, so multi-line matches list every line that contains the string.","commonSituations":"Editing boilerplate, repeated log statements, repeated closing tags/braces, or methods duplicated across a class. Typical in LLM-agent file editing when the model picks an insufficient context window around the target line.","solutions":["Expand old_str with surrounding lines (function signature, preceding comment) until the snippet is unique in the file.","Use the reported line numbers to pick a distinguishing anchor line directly above or below the target occurrence.","If all occurrences must change identically, do sequential unique replacements or rewrite the whole file via write.","Verify uniqueness first with a search (e.g. count matches) before calling str_replace."],"exampleFix":"// before\nstr_replace(path, old_str='    return total', new_str='    return round(total, 2)')  // 'return total' appears 3 times\n// after\nstr_replace(path, old_str='def sum_cart(items):\\n    total = 0\\n    ...\\n    return total', new_str='def sum_cart(items):\\n    total = 0\\n    ...\\n    return round(total, 2)')  // whole function is unique","handlingStrategy":"validation","validationCode":"content = (await operator.read_file(path)).expandtabs()\ncount = content.count(old_str.expandtabs())\nassert count == 1, f'old_str appears {count} times; expand the snippet to make it unique'","typeGuard":"def is_unique_snippet(content: str, old_str: str) -> bool:\n    return content.expandtabs().count(old_str.expandtabs()) == 1","tryCatchPattern":"try:\n    await editor.str_replace(path, old_str, new_str)\nexcept ToolError as e:\n    if 'Multiple occurrences' in str(e):\n        # parse reported lines, grow old_str with neighboring lines, retry\n        raise","preventionTips":["Include a unique anchor (function signature, distinct comment) in every old_str.","Pre-check uniqueness with a substring count before calling str_replace.","Avoid single common lines like 'return', 'pass', or closing braces as old_str."],"tags":["file-editing","str-replace","ambiguity","agent-tools"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}