{"record":{"id":"e200a14e430e67fa","repo":"Aider-AI/aider","slug":"could-not-find-scope-context-n-scope-txt","errorCode":null,"errorMessage":"Could not find scope context:\\n{scope_txt}","messagePattern":"Could not find scope context:\\\\n(.+?)","errorType":"exception","errorClass":"DiffError","httpStatus":null,"severity":"error","filePath":"aider/coders/patch_coder.py","lineNumber":485,"sourceCode":"                    while temp_index < len(orig_lines):\n                        match = True\n                        for i, scope in enumerate(scope_lines):\n                            if (\n                                temp_index + i >= len(orig_lines)\n                                or _norm(orig_lines[temp_index + i]).strip() != scope.strip()\n                            ):\n                                match = False\n                                break\n                        if match:\n                            current_file_index = temp_index + len(scope_lines)\n                            found_scope = True\n                            total_fuzz += 1  # Add fuzz for scope match difference\n                            break\n                        temp_index += 1\n\n                if not found_scope:\n                    scope_txt = \"\\n\".join(scope_lines)\n                    raise DiffError(f\"Could not find scope context:\\n{scope_txt}\")\n\n            # Peek and parse the next context/change section\n            context_block, chunks_in_section, next_index, is_eof = peek_next_section(lines, index)\n\n            # Find where this context block appears in the original file\n            found_index, fuzz = find_context(orig_lines, context_block, current_file_index, is_eof)\n            total_fuzz += fuzz\n\n            if found_index == -1:\n                ctx_txt = \"\\n\".join(context_block)\n                marker = \"*** End of File\" if is_eof else \"\"\n                raise DiffError(\n                    f\"Could not find patch context {marker} starting near line\"\n                    f\" {current_file_index}:\\n{ctx_txt}\"\n                )\n\n            # Adjust chunk original indices to be absolute within the file\n            for chunk in chunks_in_section:","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/Aider-AI/aider/blob/5dc9490bb35f9729ef2c95d00a19ccd30c26339c/aider/coders/patch_coder.py#L467-L503","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-read the file (/read or /add again) so chat context matches disk, then retry the patch","Make the model quote the file's exact lines for scope context — aider's retry prompt usually surfaces the mismatch","Fix whitespace/indentation in the patch's context lines to match the file byte-for-byte"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"def scope_lines_in_file(patch_text: str, file_lines: list[str], norm=lambda s: s.strip()) -> bool:\n    # crude pre-check: every non-marker context line inside Update blocks\n    # must appear somewhere in the file\n    file_set = {norm(l) for l in file_lines}\n    in_update = False\n    for line in patch_text.splitlines():\n        if line.startswith(\"*** Update File: \"):\n            in_update = True\n        elif line.startswith(\"*** \"):\n            in_update = False\n        elif in_update and line and not line.startswith((\"+\", \"-\")):\n            if norm(line) not in file_set:\n                return False\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    edits = coder.get_edits(reply)\nexcept DiffError as e:\n    if \"Could not find scope context\" in str(e):\n        coder.commands.cmd_read(path)  # refresh file content in chat\n        edits = coder.get_edits(regenerate_patch(reply))\n    else:\n        raise","preventionTips":["Refresh files in chat before requesting edits when they may have changed","Tell the model to copy context lines verbatim (no paraphrasing or reformatting)","Prefer small context windows of exactly-copied lines"],"tags":["aider","patch","context-match","update"],"backgroundTag":null,"analyzedSha":"5dc9490bb35f9729ef2c95d00a19ccd30c26339c","analyzedAt":"2026-08-15T05:40:10.498Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}