Aider-AI/aider · error · DiffError

Could not find scope context:\n{scope_txt}

Error message

Could not find scope context:\n{scope_txt}

What it means

During UPDATE parsing, the section prefixed with '*** ...scope' context lines could not be located in the original file content, even with fuzzy matching. The parser scans forward from the current file index comparing normalized scope lines; on failure it raises with the offending scope text.

Source

Thrown at aider/coders/patch_coder.py:485

                    while temp_index < len(orig_lines):
                        match = True
                        for i, scope in enumerate(scope_lines):
                            if (
                                temp_index + i >= len(orig_lines)
                                or _norm(orig_lines[temp_index + i]).strip() != scope.strip()
                            ):
                                match = False
                                break
                        if match:
                            current_file_index = temp_index + len(scope_lines)
                            found_scope = True
                            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:

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Re-read the file (/read or /add again) so chat context matches disk, then retry the patch
  2. Make the model quote the file's exact lines for scope context — aider's retry prompt usually surfaces the mismatch
  3. Fix whitespace/indentation in the patch's context lines to match the file byte-for-byte
Defensive patterns

Strategy: validation

Validate before calling

def scope_lines_in_file(patch_text: str, file_lines: list[str], norm=lambda s: s.strip()) -> bool:
    # crude pre-check: every non-marker context line inside Update blocks
    # must appear somewhere in the file
    file_set = {norm(l) for l in file_lines}
    in_update = False
    for line in patch_text.splitlines():
        if line.startswith("*** Update File: "):
            in_update = True
        elif line.startswith("*** "):
            in_update = False
        elif in_update and line and not line.startswith(("+", "-")):
            if norm(line) not in file_set:
                return False
    return True

Try / catch

try:
    edits = coder.get_edits(reply)
except DiffError as e:
    if "Could not find scope context" in str(e):
        coder.commands.cmd_read(path)  # refresh file content in chat
        edits = coder.get_edits(regenerate_patch(reply))
    else:
        raise

Prevention

When it happens

Trigger: An UPDATE chunk whose leading scope/context lines do not exist in the target file (typo, wrong indentation, stale content), or the scope matches only earlier in the file than current_file_index.

Common situations: File changed on disk after it was loaded into chat context; model paraphrases context lines instead of copying them verbatim; tabs-vs-spaces or line-ending drift between the model output and the file.

Related errors


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