## TL;DR

The PR branch was deleted while the agent was reviewing, so diff fetches 404. Treat branch-deleted as a terminal state: stop the review, clean up partial state, and log why  -  do not retry the fetch.

## Error

```text
Agent log: GET /repos/acme/app/compare/main...feature-x → 404 Not Found (branch deleted mid-review).
```

## Steps

1. Confirm the cause: the PR timeline shows the branch was deleted during the run. Expected: a delete event, not an API outage.
2. Handle the 404 explicitly: catch it on diff fetch and check whether the PR is merged/closed. Expected: the agent knows the review is moot.
3. Stop cleanly: discard partial review state and log 'branch deleted, review abandoned'. Expected: no crash, no partial review posted.
4. Do not retry the fetch in a loop  -  the branch is gone, not slow. Expected: the run ends instead of spinning.

## When to use

- The agent's diff fetch 404s because the PR branch was deleted mid-run.
- The agent fetches diffs itself.

## When not to use

- The PR itself 404s (deleted PR)  -  handle that as a separate case.
- The 404 is transient API flakiness; that deserves a retry.

## Tool compatibility

- GitHub / GitLab compare/diff APIs.
- Any agent fetching PR diffs.

## Variant phrasings

### Branch deleted after merge

The normal case  -  the review is simply moot; stop quietly.

### Branch force-deleted by accident

The PR can be restored; the agent should still stop and let a human decide.

## Why it happens

Deleting the branch removes the diff the agent is reviewing. Retrying a 404 for a deleted ref never succeeds  -  the correct response is to recognize the terminal state and stop, not to hammer the API.

## Edge cases

- Distinguish 'branch deleted' from 'never existed'  -  the latter is a bug in ref resolution, worth an alert.
- Partial reviews already posted stay posted; note in the log what was abandoned.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_mM5RkomillySDalyb1xY3w
