{"record":{"id":"2df4c4ff71f860ea","repo":"FoundationAgents/OpenManus","slug":"invalid-insert-line-parameter-insert-line-it","errorCode":null,"errorMessage":"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}","messagePattern":"Invalid `insert_line` parameter: (.+?)\\. It should be within the range of lines of the file: (.+?)","errorType":"exception","errorClass":"ToolError","httpStatus":null,"severity":"error","filePath":"app/tool/str_replace_editor.py","lineNumber":356,"sourceCode":"        return CLIResult(output=success_msg)\n\n    async def insert(\n        self,\n        path: PathLike,\n        insert_line: int,\n        new_str: str,\n        operator: FileOperator = None,\n    ) -> CLIResult:\n        \"\"\"Insert text at a specific line in a file.\"\"\"\n        # Read and prepare content\n        file_text = (await operator.read_file(path)).expandtabs()\n        new_str = new_str.expandtabs()\n        file_text_lines = file_text.split(\"\\n\")\n        n_lines_file = len(file_text_lines)\n\n        # Validate insert_line\n        if insert_line < 0 or insert_line > n_lines_file:\n            raise ToolError(\n                f\"Invalid `insert_line` parameter: {insert_line}. It should be within \"\n                f\"the range of lines of the file: {[0, n_lines_file]}\"\n            )\n\n        # Perform insertion\n        new_str_lines = new_str.split(\"\\n\")\n        new_file_text_lines = (\n            file_text_lines[:insert_line]\n            + new_str_lines\n            + file_text_lines[insert_line:]\n        )\n\n        # Create a snippet for preview\n        snippet_lines = (\n            file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]\n            + new_str_lines\n            + file_text_lines[insert_line : insert_line + SNIPPET_LINES]\n        )","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/tool/str_replace_editor.py#L338-L374","documentation":"Thrown by str_replace_editor's insert operation when the insert_line argument is outside [0, n_lines_file]. The valid range is inclusive on both ends: 0 inserts before the first line and n_lines_file (the line count of the split content) appends after the last line.","triggerScenarios":"Calling insert with a negative insert_line, or an insert_line greater than the number of lines in the file. Common off-by-one confusion: the upper bound equals the total line count, not count-1, because inserting after the last line is legal.","commonSituations":"Agent code computing the insertion point from a stale view of the file (file shrank or was rewritten since read); passing a 1-based line number where the API expects a 0-based index plus treating the bound as exclusive; empty or one-line files where valid range is [0, 0] or [0, 1] and callers assume larger indices.","solutions":["Clamp insert_line into [0, number_of_lines] before calling, re-reading the file to get the current line count.","Remember the index is 0-based and the upper bound is the line count itself (insert-at-end is allowed).","If appending to the file, pass exactly n_lines_file; if prepending, pass 0.","Refresh your cached view of the file before computing the insertion point."],"exampleFix":"// before\nawait editor.insert(path, insert_line=15, new_str='# note')  // file has 10 lines -> error\n// after\nlines = (await operator.read_file(path)).split('\\n')\ninsert_line = min(15, len(lines))  # clamp to valid inclusive range [0, len(lines)]\nawait editor.insert(path, insert_line=insert_line, new_str='# note')","handlingStrategy":"validation","validationCode":"n_lines = len((await operator.read_file(path)).expandtabs().split('\\n'))\ninsert_line = max(0, min(insert_line, n_lines))  # clamp into inclusive [0, n_lines]","typeGuard":"def is_valid_insert_line(insert_line: int, n_lines: int) -> bool:\n    return isinstance(insert_line, int) and 0 <= insert_line <= n_lines","tryCatchPattern":null,"preventionTips":["Treat insert_line as 0-based with an inclusive upper bound equal to the file's line count.","Recompute the line count from a fresh read right before inserting.","For append operations, pass exactly n_lines_file; for prepend, pass 0."],"tags":["file-editing","insert","bounds-check","off-by-one"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}