FoundationAgents/MetaGPT · error · ValueError

Cannot insert and append at the same time.

Error message

Cannot insert and append at the same time.

What it means

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.

Source

Thrown at metagpt/tools/libs/editor.py:549

            "Your changes have NOT been applied. Please fix your edit command and try again.\n"
            "You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\n"
            "DO NOT re-run the same failed edit command. Running it again will lead to the same error."
        )

        if not self._is_valid_filename(file_name.name):
            raise FileNotFoundError("Invalid file name.")

        if not self._is_valid_path(file_name):
            raise FileNotFoundError("Invalid path or file name.")

        if not self._create_paths(file_name):
            raise PermissionError("Could not access or create directories.")

        if not file_name.is_file():
            raise FileNotFoundError(f"File {file_name} not found.")

        if is_insert and is_append:
            raise ValueError("Cannot insert and append at the same time.")

        # Use a temporary file to write changes
        content = str(content or "")
        temp_file_path = ""
        src_abs_path = file_name.resolve()
        first_error_line = None
        # The file to store previous content and will be removed automatically.
        temp_backup_file = tempfile.NamedTemporaryFile("w", delete=True)

        try:
            # lint the original file
            # enable_auto_lint = os.getenv("ENABLE_AUTO_LINT", "false").lower() == "true"
            if self.enable_auto_lint:
                original_lint_error, _ = self._lint_file(file_name)

            # Create a temporary file
            with tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
                temp_file_path = temp_file.name

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Set exactly one mode flag per call (is_insert XOR is_append)
  2. Use the dedicated insert/append methods instead of the shared impl with flags
  3. Validate in your dispatcher: reject calls where both flags are true before invoking the editor

Example fix

// before
editor._edit_file_impl(path, content=c, is_insert=True, is_append=True)  # ValueError

// after
editor._edit_file_impl(path, content=c, is_append=True)  # pick one mode
Defensive patterns

Strategy: type-guard

Validate before calling

assert not (is_insert and is_append), 'pick one mode'
editor._edit_file_impl(path, content=content, is_insert=is_insert, is_append=is_append)

Type guard

def valid_mode(is_insert: bool, is_append: bool) -> bool:
    return is_insert != is_append or not (is_insert or is_append)  # at most one flag

Try / catch

try:
    editor._edit_file_impl(path, content=c, is_insert=i, is_append=a)
except ValueError as e:
    if 'insert and append' in str(e):
        a = a and not i  # resolve to insert priority, retry
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/870c0eeb764d941f. Report an issue: GitHub.