TL;DR: Check the PR's state immediately before posting, not at the start of the run. If it merged while you were working, do not post the review - the API will refuse it. Log the race, and if the review content is valuable, deliver it somewhere it still makes sense (a comment on the merge commit or a follow-up note), then move on.

```text
PR went from open to merged when agent tried to post its review
```

1. Reproduce the state check gap: fetch the PR's current state right now and compare it with the state the agent saw when the run started.
   Expected: the PR shows merged while the agent's run state still says open.
2. Move the state check to just before the submit call. Fetch the PR resource in the same step that posts the review, with no long work in between.
   Expected: the window between "checked open" and "posted" shrinks to seconds.
3. Handle the refusal gracefully. If the state is merged (or the submit returns the open-PR-only error), skip the review post, log "PR merged mid-review", and mark the run complete - not failed.
   Expected: no error traceback, no retry storm against a merged PR.
4. Decide where the review content goes, per your team's policy. Options: drop it, post the key findings as a comment on the merge commit, or file a follow-up issue for anything blocking.
   Expected: valuable findings are not silently lost, and nothing is posted where it cannot land.
5. Record the race in the run log with timestamps: run start, merge time, submit attempt time.
   Expected: the next person who sees the gap can tell it was a race, not a bug.
6. Re-run the flow against a test PR that you merge mid-run.
   Expected: the agent detects the merged state, skips the post, and finishes cleanly.

## Use this when
- Review submit fails because the PR merged during the run
- The agent errors on "reviews may only be submitted on open pull requests"
- Long review runs race with fast merges
- You need a policy for review content when the PR is already gone

## Not for this skill when
- The PR was already merged before the run started (check state at run start too)
- The submit fails with 403 (that is a permissions problem, not a race)
- The submit fails with 422 on outdated hunks (that is a stale-diff problem)
- The agent loops requesting reviews on a merged PR (that is a state-tracking loop problem)

## Variant phrasings
- "review failed PR merged while agent was working"
- "cannot submit review on merged pull request"
- "PR closed before review posted"
- "race condition between review bot and merge"

## Why it happens
Review runs do their state check once at the start, then spend minutes fetching diffs and analyzing. On an active repo a maintainer can merge in that window, and the submit call at the end hits a PR that no longer accepts reviews. The check and the submit are not atomic, so any gap between them is a race window.

## Edge cases
- A PR can also be closed without merging. Handle closed the same as merged: do not post.
- Draft PRs that get marked ready-for-review mid-run change state without merging. Re-check draft status too if your policy treats drafts differently.
- Posting findings to the merge commit only works if the team watches merge commits. Agree on the fallback channel before you need it.
- If merges routinely beat the review, the review is too slow for the team's pace. Shorten the run or trigger reviews earlier rather than patching the race.

## Provenance

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