# Atlas apply_patch: "Failed to find context '<context>' in <path>"

> To fix "Failed to find context" in Atlas apply_patch, re-read the file and regenerate the patch from its current contents, since the patch was built against a stale copy.

Atlas apply_patch fails with "Failed to find context '<context>' in <path>" because the patch applier could not locate the lines the patch expects, and the patch was almost certainly generated against a stale copy of the file. The fix is to re-read the file and regenerate the patch from its current contents. The applier seeks the change_context line, then seeks the hunk's old_lines sequence from that point, retrying once without a trailing empty line. If either seek returns -1, it throws naming the file and the lines it expected. For a single small change, fall back to the edit tool, which has fuzzy replacers apply_patch does not.

## Symptom

apply_patch fails with: Failed to find context '<context>' in <path>, or: Failed to find expected lines in <path>: followed by the old lines it was looking for.

## Cause

The Atlas patch applier seeks the change_context line, then seeks the hunk's old_lines sequence from that point, retrying once without a trailing empty line. If either seek returns -1, it throws naming the file and the lines it expected. The file on disk no longer contains the lines the patch was written against.

## Fix

1. Re-read the file: the patch was almost certainly generated against a stale copy, so start by seeing what the file actually contains now.
2. Regenerate the patch from the current file contents rather than editing the old patch to match.
3. Check for whitespace or line-ending drift introduced by a formatter between generation and application, since the seek is on the exact old_lines sequence.
4. For a single small change, fall back to the edit tool, which has fuzzy replacers apply_patch does not.
5. Re-run apply_patch and review the unified diff Atlas surfaces for approval before it writes.

## Why does Atlas apply_patch fail to find the hunk context

Atlas apply_patch fails to find the hunk context because the applier seeks the change_context line, then seeks the hunk's old_lines sequence from that point, retrying once without a trailing empty line. When either seek returns -1, apply_patch throws and names the file and the exact lines it expected.

The applier is doing a two-stage lookup, and the error tells you which stage failed. "Failed to find context '<context>' in <path>" means the first seek missed: the anchor line the patch keys on is not in the file. "Failed to find expected lines in <path>:" followed by the old lines means the anchor was found but the block underneath it does not match what the patch expected to replace. That second message is the more useful one, because it prints the lines apply_patch was looking for, and you can diff them by eye against what the file actually contains. The single retry without a trailing empty line handles the common case of a stray blank line at the end of a hunk. Beyond that, apply_patch does not fuzz the match, and it will not guess.

## How to fix a stale patch in Atlas apply_patch

Fix a stale Atlas patch in 2 steps: re-read the file to see its current contents, then regenerate the patch from those contents. The patch was almost certainly generated against a stale copy, so editing the old patch to make it fit is slower and less reliable than producing a fresh one.

Regeneration works because the failure is a mismatch between two versions of the same file, and the only version that matters is the one on disk right now. Re-reading gives the generator the current text, and a patch built from current text seeks lines that are genuinely there. Hand-repairing the old patch means reconstructing, line by line, what changed since it was written, and any line you get slightly wrong reproduces the same seek failure with a different message. The honest caveat: if the file changed a lot, the change you originally wanted may no longer make sense against the new code. Read the current file before you regenerate, not just to feed the generator, but to check that your intent still applies.

## How whitespace and line-ending drift breaks Atlas patches

Whitespace and line-ending drift breaks Atlas patches because the applier seeks the hunk's old_lines sequence by content. A formatter that runs between generation and application can reindent a block, and 1 changed character is enough for the seek to return -1.

The drift is invisible in a normal reading of the code. The block still says the same thing, the logic is unchanged, and the diff apply_patch is trying to make still looks correct to a human. But the seek is over exact lines, so two spaces where there was a tab, or a rewritten line ending, is enough to return -1. This is why the error message printing the expected old lines is so valuable: put those lines next to what is actually in the file and the drift usually jumps out. If a formatter runs on save or in a pre-commit hook, assume it may have touched the file between generation and application, and regenerate the patch after the formatter has had its turn rather than before.

## When to use the Atlas edit tool instead of apply_patch

Use the Atlas edit tool instead of apply_patch for a single small change. The edit tool has fuzzy replacers apply_patch does not, so it tolerates the minor drift that makes an exact old_lines seek return -1, and it does not require you to assemble a full patch envelope for a one-line fix.

The two tools trade off differently. apply_patch is the right choice for multi-hunk changes across a file, where an exact match is a feature: you want a hard failure rather than a fuzzy write when the file is not what you thought. The edit tool is the right choice when the change is small and localized, because its fuzzy replacers absorb the whitespace variance that would otherwise cost you a regeneration cycle. Both go through the same safety path. Atlas computes a unified diff for every file edit and surfaces it for approval before writing, and Atlas snapshots file changes as git patches so edits can be diffed and rolled back. Choosing edit does not mean giving up the review gate.

## How to verify the Atlas patch context fix worked

Verify the Atlas apply_patch fix by re-running the tool against the current file. A regenerated patch applies without "Failed to find context '<context>' in <path>" and without "Failed to find expected lines in <path>:", and Atlas then surfaces the unified diff for approval before writing 1 byte.

Read the diff rather than accepting it reflexively. A patch that now seeks successfully is a patch whose anchors match the file, which is necessary but does not prove the change is the one you wanted, especially if the file moved significantly since your original intent was formed. Compare the diff against what you set out to do. If the error returns after regeneration, the likely culprit is that something is still rewriting the file between generation and application, so look at formatters, watchers, and background subagents before you assume the patch is at fault. Atlas fans out work to subagents that can run in the foreground or in parallel background sessions, and a background session touching the same file will keep invalidating your patch no matter how many times you regenerate it.

## FAQ

### what does Failed to find context mean in Atlas apply_patch

It means the Atlas patch applier could not locate the change_context anchor line in the file. The patch was almost certainly generated against a stale copy, so the anchor it keys on no longer exists as written.

### why does Atlas say Failed to find expected lines in my file

The applier found the context anchor but the hunk's old_lines sequence did not match the file. The error prints the lines it expected, so you can compare them against the current contents.

### how do I fix a stale patch in Atlas

Re-read the file to see its current contents, then regenerate the patch from those contents. Do not hand-edit the old patch to fit, since any reconstructed line that is slightly off reproduces the failure.

### can a formatter cause apply_patch to fail in Atlas

Yes. Whitespace or line-ending drift introduced by a formatter between generation and application changes the exact old_lines the applier seeks, and the seek then returns -1.

### should I use edit or apply_patch for a one line change in Atlas

Use the edit tool. It has fuzzy replacers apply_patch does not, so it tolerates minor whitespace drift and does not require assembling a full patch for a small localized change.

### does Atlas apply_patch retry a failed hunk

Only once, and only in one specific way: it retries the old_lines seek without a trailing empty line. Beyond that it does not fuzz the match, and it throws naming the file and the lines it expected.

### how do I confirm my regenerated Atlas patch applied correctly

Re-run apply_patch and read the unified diff Atlas surfaces for approval. A successful run shows the diff without the failed-to-find-context or failed-to-find-expected-lines errors.

---

Canonical HTML: https://runatlas.sh/resources/troubleshooting/apply-patch-context-not-found
Source of truth: aeo_pages row `/resources/troubleshooting/apply-patch-context-not-found` (segment: Troubleshooting) (this file is generated from it, never hand-edited).
Licence: Atlas is proprietary with a free core. It is not open source and there is no public source repository.
