{"record":{"id":"f26b3ae13de6154d","repo":"Aider-AI/aider","slug":"could-not-find-patch-context-marker-starting-nea","errorCode":null,"errorMessage":"Could not find patch context {marker} starting near line {current_file_index}:\\n{ctx_txt}","messagePattern":"Could not find patch context (.+?) starting near line (.+?):\\\\n(.+?)","errorType":"exception","errorClass":"DiffError","httpStatus":null,"severity":"error","filePath":"aider/coders/patch_coder.py","lineNumber":497,"sourceCode":"                            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:\n                # chunk.orig_index from peek is relative to context_block start\n                # We need it relative to the file start\n                chunk.orig_index += found_index\n                action.chunks.append(chunk)\n\n            # Advance file index past the matched context block\n            current_file_index = found_index + len(context_block)\n            # Advance line index past the processed section in the patch\n            index = next_index\n\n        return action, index, total_fuzz\n","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/Aider-AI/aider/blob/5dc9490bb35f9729ef2c95d00a19ccd30c26339c/aider/coders/patch_coder.py#L479-L515","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Refresh the file in chat context and re-ask so the patch is generated from current content","Shrink the context block to a few unique, exactly-copied lines around the change","Check the is_eof expectation: remove '*** End of File' anchoring if the context is not actually at EOF"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"def context_block_findable(ctx: list[str], file_lines: list[str]) -> bool:\n    n = len(ctx)\n    hay = [l.strip() for l in file_lines]\n    needle = [l.strip() for l in ctx]\n    return any(hay[i:i+n] == needle for i in range(len(hay) - n + 1))","typeGuard":null,"tryCatchPattern":"try:\n    edits = coder.get_edits(reply)\nexcept DiffError as e:\n    if \"Could not find patch context\" in str(e):\n        # echo the missing context back — aider-style reflection usually fixes it\n        edits = coder.get_edits(reflect_and_retry(reply, str(e)))\n    else:\n        raise","preventionTips":["Keep chat file content in sync with disk (re-/add after external changes)","Use unique, short context blocks copied byte-for-byte from the file","Only use '*** End of File' anchoring when the edit truly touches EOF"],"tags":["aider","patch","context-match","fuzzy-match"],"backgroundTag":null,"analyzedSha":"5dc9490bb35f9729ef2c95d00a19ccd30c26339c","analyzedAt":"2026-08-15T05:40:10.498Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}