Aider-AI/aider · error · DiffError

Could not find patch context {marker} starting near line {cu

Error message

Could not find patch context {marker} starting near line {current_file_index}:\n{ctx_txt}

What it means

Core context-matching failure: find_context() could not place the patch's context block anywhere in the original file (returns -1), after adding any fuzz. The message includes the near-start line (current_file_index) and whether the block was anchored to end-of-file.

Source

Thrown at aider/coders/patch_coder.py:497

                            total_fuzz += 1  # Add fuzz for scope match difference
                            break
                        temp_index += 1

                if not found_scope:
                    scope_txt = "\n".join(scope_lines)
                    raise DiffError(f"Could not find scope context:\n{scope_txt}")

            # Peek and parse the next context/change section
            context_block, chunks_in_section, next_index, is_eof = peek_next_section(lines, index)

            # Find where this context block appears in the original file
            found_index, fuzz = find_context(orig_lines, context_block, current_file_index, is_eof)
            total_fuzz += fuzz

            if found_index == -1:
                ctx_txt = "\n".join(context_block)
                marker = "*** End of File" if is_eof else ""
                raise DiffError(
                    f"Could not find patch context {marker} starting near line"
                    f" {current_file_index}:\n{ctx_txt}"
                )

            # Adjust chunk original indices to be absolute within the file
            for chunk in chunks_in_section:
                # chunk.orig_index from peek is relative to context_block start
                # We need it relative to the file start
                chunk.orig_index += found_index
                action.chunks.append(chunk)

            # Advance file index past the matched context block
            current_file_index = found_index + len(context_block)
            # Advance line index past the processed section in the patch
            index = next_index

        return action, index, total_fuzz

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Refresh the file in chat context and re-ask so the patch is generated from current content
  2. Shrink the context block to a few unique, exactly-copied lines around the change
  3. Check the is_eof expectation: remove '*** End of File' anchoring if the context is not actually at EOF
Defensive patterns

Strategy: try-catch

Validate before calling

def context_block_findable(ctx: list[str], file_lines: list[str]) -> bool:
    n = len(ctx)
    hay = [l.strip() for l in file_lines]
    needle = [l.strip() for l in ctx]
    return any(hay[i:i+n] == needle for i in range(len(hay) - n + 1))

Try / catch

try:
    edits = coder.get_edits(reply)
except DiffError as e:
    if "Could not find patch context" in str(e):
        # echo the missing context back — aider-style reflection usually fixes it
        edits = coder.get_edits(reflect_and_retry(reply, str(e)))
    else:
        raise

Prevention

When it happens

Trigger: Context lines in an UPDATE chunk do not appear in the file at or after the tracked position; end-of-file marker present but the context sits mid-file (or vice versa); file drifted from what the model saw.

Common situations: Concurrent edits to the file between chat load and patch apply; model reformatting context lines (quotes, wrapping); patches generated against an older revision of the file.

Related errors


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