{"record":{"id":"683a20e1ffc406ad","repo":"FoundationAgents/MetaGPT","slug":"error-the-position-replaced-line-number-does","errorCode":null,"errorMessage":"Error: The `{position}_replaced_line_number` does not match the `{position}_replaced_line_content`. Please correct the parameters.\nThe `{position}_replaced_line_number` is {line_number} and the corresponding content is \"{true_content}\".\nBut the `{position}_replaced_line_content ` is \"{fake_content}\".\nThe content around the specified line is:\n{context}\nPay attention to the new content. Ensure that it aligns with the new parameters.","messagePattern":"Error: The `(.+?)_replaced_line_number` does not match the `(.+?)_replaced_line_content`\\. Please correct the parameters\\.\nThe `(.+?)_replaced_line_number` is (.+?) and the corresponding content is \"(.+?)\"\\.\nBut the `(.+?)_replaced_line_content ` is \"(.+?)\"\\.\nThe content around the specified line is:\n(.+?)\nPay attention to the new content\\. Ensure that it aligns with the new parameters\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":813,"sourceCode":"                    start = max(1, line_number - 3)\n                    end = min(total_lines, line_number + 3)\n                    context = \"\\n\".join(\n                        [\n                            f'The {cur_line_number:03d} line is \"{lines[cur_line_number-1].rstrip()}\"'\n                            for cur_line_number in range(start, end + 1)\n                        ]\n                    )\n                    mismatch_error += LINE_NUMBER_AND_CONTENT_MISMATCH.format(\n                        position=position,\n                        line_number=line_number,\n                        true_content=lines[line_number - 1].rstrip()\n                        if line_number - 1 < len(lines)\n                        else \"OUT OF FILE RANGE!\",\n                        fake_content=line_content.replace(\"\\n\", \"\\\\n\"),\n                        context=context.strip(),\n                    )\n        if mismatch_error:\n            raise ValueError(mismatch_error)\n        ret_str = self._edit_file_impl(\n            file_name,\n            start=first_replaced_line_number,\n            end=last_replaced_line_number,\n            content=new_content,\n        )\n        # TODO: automatically tries to fix linter error (maybe involve some static analysis tools on the location near the edit to figure out indentation)\n        self.resource.report(file_name, \"path\")\n        return ret_str\n\n    def _edit_file_by_replace(self, file_name: str, to_replace: str, new_content: str) -> str:\n        \"\"\"Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.\n\n        Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.\n\n        Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.\n\n        For example, given a file \"/workspace/example.txt\" with the following content:","sourceCodeStart":795,"sourceCodeEnd":831,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L795-L831","documentation":"Raised by the line-based edit path (_edit_file_by_edit / _edit_file_with_line_numbers) when the caller-supplied replaced_line_content does not match what is actually at the supplied replaced_line_number. The editor verifies content-at-line as a safety check so stale or hallucinated line references cannot silently overwrite the wrong code; the message includes the true content, the claimed content, and surrounding context.","triggerScenarios":"Calling the line-number edit API where first/last_replaced_line_number point at lines whose actual text differs from the accompanying *_replaced_line_content values; using line numbers from an older version of the file; off-by-N line drift after earlier edits in the same session.","commonSituations":"LLM agents reusing line/content pairs from a previous read after the file changed; concurrent modifications by formatters or other agents; whitespace/indentation mismatches between the claimed and real content.","solutions":["Re-read the file (editor.read/open_file) and re-extract the exact current line contents before editing","Copy the content strings verbatim from the fresh read, preserving indentation","If the file changed externally, restart from the new contents rather than patching stale references"],"exampleFix":"// before\neditor._edit_file_by_line_numbers(path,\n    first_replaced_line_number=42, first_replaced_line_content='def old():', ...)  # content mismatch\n\n// after\n# re-read window around line 42, then pass the exact current text\neditor._edit_file_by_line_numbers(path,\n    first_replaced_line_number=42, first_replaced_line_content='def newname():', ...)","handlingStrategy":"validation","validationCode":"lines = Path(file_name).read_text().splitlines()\nassert lines[line_number - 1].rstrip() == claimed_content.rstrip(), 'line/content drift'\n# only then invoke the line-number edit","typeGuard":"def content_matches(path: str, line_number: int, claimed: str) -> bool:\n    lines = Path(path).read_text().splitlines()\n    return 0 < line_number <= len(lines) and lines[line_number - 1].rstrip() == claimed.rstrip()","tryCatchPattern":"try:\n    result = editor._edit_file_by_edit(...)\nexcept ValueError as e:\n    if 'does not match' in str(e):\n        fresh = editor.read(file_name)  # refresh, rebuild params, retry once\n        raise","preventionTips":["Always read the file in the same turn as the edit; never reuse older line/content pairs","Keep (line_number, line_content) pairs atomic — derive content from a fresh read, not memory","Serialize edits per file so no other process changes it between read and write"],"tags":["editor","consistency-check","line-numbers","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}