Aider-AI/aider · error · DiffError

{path}: Overlapping or out-of-order chunk detected. Current

Error message

{path}: Overlapping or out-of-order chunk detected. Current index {current_orig_line_idx}, chunk starts at {chunk_start_index}.

What it means

Inside _apply_update: after sorting chunks by orig_index, a chunk's start line is before the file position already consumed by a previous chunk. The parser should never produce overlapping/backwards chunks, so this guards against parser bugs or externally built chunk lists with wrong absolute indices.

Source

Thrown at aider/coders/patch_coder.py:665

        if action.type is not ActionType.UPDATE:
            # Should not be called otherwise, but check for safety
            raise DiffError("_apply_update called with non-update action")

        orig_lines = text.splitlines()  # Use splitlines to handle endings consistently
        dest_lines: List[str] = []
        current_orig_line_idx = 0  # Tracks index in orig_lines processed so far

        # Sort chunks by their original index to apply them sequentially
        sorted_chunks = sorted(action.chunks, key=lambda c: c.orig_index)

        for chunk in sorted_chunks:
            # chunk.orig_index is the absolute line number where the change starts
            # (where the first deleted line was, or where inserted lines go if no deletes)
            chunk_start_index = chunk.orig_index

            if chunk_start_index < current_orig_line_idx:
                # This indicates overlapping chunks or incorrect indices from parsing
                raise DiffError(
                    f"{path}: Overlapping or out-of-order chunk detected."
                    f" Current index {current_orig_line_idx}, chunk starts at {chunk_start_index}."
                )

            # Add lines from original file between the last chunk and this one
            dest_lines.extend(orig_lines[current_orig_line_idx:chunk_start_index])

            # Verify that the lines to be deleted actually match the original file content
            # (The parser should have used find_context, but double-check here)
            num_del = len(chunk.del_lines)
            actual_deleted_lines = orig_lines[chunk_start_index : chunk_start_index + num_del]

            # Use the same normalization as find_context_core for comparison robustness
            norm_chunk_del = [_norm(s).strip() for s in chunk.del_lines]
            norm_actual_del = [_norm(s).strip() for s in actual_deleted_lines]

            if norm_chunk_del != norm_actual_del:
                # This indicates the context matching failed or the file changed since parsing

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. If constructing chunks manually, ensure orig_index is the absolute 0-based line in the original file and chunks are non-overlapping and in order
  2. Reparse the patch from text so the parser computes indices via find_context
  3. Shrink or merge overlapping edits into a single chunk
Defensive patterns

Strategy: validation

Validate before calling

def chunks_non_overlapping(chunks) -> bool:
    ordered = sorted(chunks, key=lambda c: c.orig_index)
    cursor = 0
    for c in ordered:
        if c.orig_index < cursor:
            return False
        cursor = c.orig_index + len(c.del_lines)
    return True

Try / catch

try:
    coder.apply_edits(edits)
except ValueError as e:
    if "Overlapping or out-of-order chunk" in str(e):
        raise RuntimeError("chunk indices invalid; reparse patch from text")
    raise

Prevention

When it happens

Trigger: Two chunks whose orig_index values overlap given their del_lines lengths; chunks with relative (not absolute) indices passed in; duplicated chunks after manual manipulation.

Common situations: Building PatchAction.chunks programmatically with peek-relative indices instead of the parser's absolute-adjusted ones; replaying captured chunks onto a different base text; parser regressions with fuzzy matches assigning wrong found_index.

Related errors


AI-assisted analysis of Aider-AI/aider@5dc9490bb3 (2026-08-15). Data as JSON: /api/errors/cbc6f90f61b3a232. Report an issue: GitHub.