{"record":{"id":"870c0eeb764d941f","repo":"FoundationAgents/MetaGPT","slug":"cannot-insert-and-append-at-the-same-time","errorCode":null,"errorMessage":"Cannot insert and append at the same time.","messagePattern":"Cannot insert and append at the same time\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":549,"sourceCode":"            \"Your changes have NOT been applied. Please fix your edit command and try again.\\n\"\n            \"You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\\n\"\n            \"DO NOT re-run the same failed edit command. Running it again will lead to the same error.\"\n        )\n\n        if not self._is_valid_filename(file_name.name):\n            raise FileNotFoundError(\"Invalid file name.\")\n\n        if not self._is_valid_path(file_name):\n            raise FileNotFoundError(\"Invalid path or file name.\")\n\n        if not self._create_paths(file_name):\n            raise PermissionError(\"Could not access or create directories.\")\n\n        if not file_name.is_file():\n            raise FileNotFoundError(f\"File {file_name} not found.\")\n\n        if is_insert and is_append:\n            raise ValueError(\"Cannot insert and append at the same time.\")\n\n        # Use a temporary file to write changes\n        content = str(content or \"\")\n        temp_file_path = \"\"\n        src_abs_path = file_name.resolve()\n        first_error_line = None\n        # The file to store previous content and will be removed automatically.\n        temp_backup_file = tempfile.NamedTemporaryFile(\"w\", delete=True)\n\n        try:\n            # lint the original file\n            # enable_auto_lint = os.getenv(\"ENABLE_AUTO_LINT\", \"false\").lower() == \"true\"\n            if self.enable_auto_lint:\n                original_lint_error, _ = self._lint_file(file_name)\n\n            # Create a temporary file\n            with tempfile.NamedTemporaryFile(\"w\", delete=False) as temp_file:\n                temp_file_path = temp_file.name","sourceCodeStart":531,"sourceCodeEnd":567,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L531-L567","documentation":"Raised by Editor._edit_file_impl when both is_insert and is_append flags are True. The two modes have contradictory placement semantics (insert at a line vs. append at EOF), so requesting both is treated as a caller bug and rejected before any file I/O happens.","triggerScenarios":"Calling insert_content_at/append operations via a wrapper that sets both flags; a tool-dispatch layer defaulting flags to True; an LLM emitting both parameters in one call.","commonSituations":"Generic edit wrappers with boolean mode flags; agent tool schemas that allow both parameters simultaneously; copy-pasted calls where a flag was forgotten to be cleared.","solutions":["Set exactly one mode flag per call (is_insert XOR is_append)","Use the dedicated insert/append methods instead of the shared impl with flags","Validate in your dispatcher: reject calls where both flags are true before invoking the editor"],"exampleFix":"// before\neditor._edit_file_impl(path, content=c, is_insert=True, is_append=True)  # ValueError\n\n// after\neditor._edit_file_impl(path, content=c, is_append=True)  # pick one mode","handlingStrategy":"type-guard","validationCode":"assert not (is_insert and is_append), 'pick one mode'\neditor._edit_file_impl(path, content=content, is_insert=is_insert, is_append=is_append)","typeGuard":"def valid_mode(is_insert: bool, is_append: bool) -> bool:\n    return is_insert != is_append or not (is_insert or is_append)  # at most one flag","tryCatchPattern":"try:\n    editor._edit_file_impl(path, content=c, is_insert=i, is_append=a)\nexcept ValueError as e:\n    if 'insert and append' in str(e):\n        a = a and not i  # resolve to insert priority, retry\n    raise","preventionTips":["Expose separate insert/append endpoints instead of boolean flags","Default mode flags to False in wrappers","Reject tool calls that set mutually exclusive parameters"],"tags":["editor","api-misuse","validation","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}