{"record":{"id":"f6a9aef192fef761","repo":"FoundationAgents/MetaGPT","slug":"invalid-line-range-start-end-start-must-be-l","errorCode":null,"errorMessage":"Invalid line range: {start}-{end}. Start must be less than or equal to end.","messagePattern":"Invalid line range: (.+?)-(.+?)\\. Start must be less than or equal to end\\.","errorType":"exception","errorClass":"LineNumberError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":469,"sourceCode":"            content: str: The new content of the file.\n            n_added_lines: int: The number of lines added to the file.\n        \"\"\"\n        # Handle cases where start or end are None\n        if start is None:\n            start = 1  # Default to the beginning\n        if end is None:\n            end = len(lines)  # Default to the end\n        # Check arguments\n        if not (1 <= start <= len(lines)):\n            raise LineNumberError(\n                f\"Invalid start line number: {start}. Line numbers must be between 1 and {len(lines)} (inclusive).\"\n            )\n        if not (1 <= end <= len(lines)):\n            raise LineNumberError(\n                f\"Invalid end line number: {end}. Line numbers must be between 1 and {len(lines)} (inclusive).\"\n            )\n        if start > end:\n            raise LineNumberError(f\"Invalid line range: {start}-{end}. Start must be less than or equal to end.\")\n\n        # Split content into lines and ensure it ends with a newline\n        if not content.endswith(\"\\n\"):\n            content += \"\\n\"\n        content_lines = content.splitlines(True)\n\n        # Calculate the number of lines to be added\n        n_added_lines = len(content_lines)\n\n        # Remove the specified range of lines and insert the new content\n        new_lines = lines[: start - 1] + content_lines + lines[end:]\n\n        # Handle the case where the original lines are empty\n        if len(lines) == 0:\n            new_lines = content_lines\n\n        # Join the lines to create the new content\n        content = \"\".join(new_lines)","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L451-L487","documentation":"Raised by Editor._edit_impl after both bounds checks pass but start > end, i.e. an inverted line range. The edit splice lines[:start-1] + content + lines[end:] only makes sense for an ordered range, so an inverted range is rejected as a caller bug rather than normalized.","triggerScenarios":"editor.edit(path, start=10, end=5, ...); start/end swapped by mistake; a diff parser emitting deletion ranges in reverse after a bad merge.","commonSituations":"Swapping positional arguments; computing end as start - len(...) and going negative-relative; hand-written range arithmetic in refactoring agents.","solutions":["Swap start and end when you detect start > end before calling","Derive end from start plus the hunk length, never the reverse","Add an assertion in your wrapper: assert 1 <= start <= end <= total_lines"],"exampleFix":"// before\neditor.edit(path, start=20, end=10, content='...')  # ValueError\n\n// after\nif start > end:\n    start, end = end, start\neditor.edit(path, start=start, end=end, content='...')","handlingStrategy":"validation","validationCode":"if start > end:\n    start, end = end, start\neditor.edit(path, start=start, end=end, content=content)","typeGuard":"def ordered_range(start: int, end: int) -> bool:\n    return start <= end","tryCatchPattern":"try:\n    editor.edit(path, start=s, end=e, content=c)\nexcept LineNumberError as err:\n    if 'Start must be less than or equal to end' in str(err):\n        editor.edit(path, start=e, end=s, content=c)\n    else:\n        raise","preventionTips":["Use keyword arguments (start=, end=) to avoid positional swaps","Derive end from start plus hunk length, never independently","Assert 1 <= start <= end <= total before delegating"],"tags":["editor","line-numbers","range","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}