{"record":{"id":"9ae5691b5322dcff","repo":"FoundationAgents/MetaGPT","slug":"invalid-start-line-number-start-line-numbers-m","errorCode":null,"errorMessage":"Invalid start line number: {start}. Line numbers must be between 1 and {len(lines)} (inclusive).","messagePattern":"Invalid start line number: (.+?)\\. Line numbers must be between 1 and (.+?) \\(inclusive\\)\\.","errorType":"exception","errorClass":"LineNumberError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":461,"sourceCode":"\n        Args:\n            lines: list[str]: The lines in the original file.\n            start: int: The start line number for editing.\n            end: int: The end line number for editing.\n            content: str: The content to replace the lines with.\n\n        Returns:\n            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","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L443-L479","documentation":"Raised by Editor._edit_impl when the resolved start line is outside 1..len(lines). start defaults to 1 when None, so this fires only for explicitly supplied values: 0, negative numbers, values past the end of the file, or non-numeric garbage that survives comparison.","triggerScenarios":"editor.edit(path, start=0, end=5, content=...) or start beyond EOF; stale line numbers from a previous longer version of the file; 0-based indexing passed by mistake.","commonSituations":"Edits computed against outdated file snapshots (the file shrank since it was read); LLM agents guessing line numbers; converting 0-based diff hunks to 1-based incorrectly.","solutions":["Re-read the file and recompute line numbers immediately before editing","Convert 0-based indices to 1-based (add 1)","Validate 1 <= start <= len(lines) in your wrapper before delegating to the editor"],"exampleFix":"// before\neditor.edit(path, start=0, end=3, content='x = 1\\n')  # ValueError\n\n// after\neditor.edit(path, start=1, end=3, content='x = 1\\n')","handlingStrategy":"validation","validationCode":"n = len(editor.current_file.read_text().splitlines())\nstart = min(max(1, int(start)), n)\nend = min(max(start, int(end)), n)\neditor.edit(path, start=start, end=end, content=content)","typeGuard":"def valid_range(start: int, end: int, n: int) -> bool:\n    return 1 <= start <= end <= n","tryCatchPattern":"try:\n    editor.edit(path, start=s, end=e, content=c)\nexcept LineNumberError as err:\n    # re-read and recompute, then retry once with clamped values\n    raise","preventionTips":["Editor line numbers are 1-based and inclusive","Re-read the file immediately before computing ranges","In wrappers, clamp inputs instead of trusting upstream arithmetic"],"tags":["editor","line-numbers","validation","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}