name: pr-flow
description: |
Automate the full PR lifecycle with mandatory sequential gates: create PR, poll for review bot (MUST use recurring job), read and triage ALL threads before fixing, fix + reply to every thread, resolve all threads (verify 0 unresolved), re-review, merge after approval. Use this skill when the user says "PR", "pull request", "create PR", "merge PR", "提PR", "合并", "PR流程", "开PR", "check PR status", "review comments", "标准PR流程". Use this skill after dev work reaches implementation_done, the branch is pushed, and the task has passed main_review. It replaces the manual C4-C7 steps in the Mercury workflow.
PR Flow — Mandatory Sequential Protocol
Overview
This skill automates the complete PR lifecycle with mandatory sequential gates. Every phase has a GATE that MUST pass before proceeding. Do NOT skip gates. Do NOT combine phases.
Gates Summary
| Gate | After | Condition |
|---|---|---|
| G1 | Phase 1 | PR created, PR_NUMBER stored |
| G2 | Phase 2 | Recurring poll active, reviews arrived |
| G3 | Phase 3 | All threads enumerated + triaged |
| G4 | Phase 4 | Every bot thread has a reply |
| G5 | Phase 5 | 0 unresolved threads (verified via re-query) |
| G6 | Phase 6 | Re-review requested, new poll created |
| G7 | Phase 7 | reviewDecision=APPROVED, CI passes, 0 unresolved |
Codex adaptation:
- use
scripts/codex/git-safe.ps1foradd,commit, andpush - use
gh api graphqlfor thread resolution - if the current Codex tool surface does not expose
CronCreateorCronDelete, keep the same 10-minute cadence via an external scheduler, host automation, or explicit follow-up invocations with persisted state files - do not use
git stash,git switch, orgit checkoutinside guarded Codex task sessions; for multi-PR splits use separate worktrees
Prerequisites
ghCLI v2.x+ (authenticated)gitwith push access to the PR branchjqfor JSON parsing- current branch must not be
develop,main, ormaster - follow
.mercury/docs/guides/git-flow.mdas the authoritative branching policy
Pipeline
Phase 1: Create PR(s)
Single-PR Mode (default)
$branch = git branch --show-current
powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 push -u origin $branch
gh pr create --base develop --title "<type>(<scope>): description (#issue)" --body @"
## Summary
- bullet points
## Test plan
- [ ] test items
Generated with Codex
"@
Multi-PR Mode
When changes should be split by category:
- Create separate worktrees branching off
origin/develop:git worktree add ../Mercury-split-A -b feature/TASK-123-part-A origin/developgit worktree add ../Mercury-split-B -b feature/TASK-123-part-B origin/develop
- Restore only the intended files into each worktree via direct file copy or
git restore --source <branch> -- <path>. 2a. Copy the guard scripts into each worktree so the worktree-local copy can be invoked (required becausegit-safe.ps1uses$PSScriptRootto resolve$repoRoot):Copy-Item -Recurse scripts/codex ../Mercury-split-A/scripts/Copy-Item -Recurse scripts/codex ../Mercury-split-B/scripts/
- Stage, commit, and push inside the target worktree directory.
git-safe.ps1uses$PSScriptRootto resolve$repoRoot, so always invoke the worktree-local copy of the script — never the main-repo copy (or extendgit-safe.ps1with a-RepoRootparameter first):Set-Location ../Mercury-split-Apowershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 add <path>powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 commit -Message "<message>"powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 push origin <branch>
- Create one PR per worktree branch (specify
--headexplicitly):gh pr create --base develop --head feature/TASK-123-part-A --title "feat(scope): part A"gh pr create --base develop --head feature/TASK-123-part-B --title "feat(scope): part B"
- Track all PR numbers for parallel monitoring.
Do not rely on git stash in guarded Codex sessions.
Phase 2: Poll for Review Bot (Non-Blocking)
Do not use long blocking sleep loops for 10-minute review polling.
Preferred model:
- if the host exposes recurring jobs, schedule a 10-minute recurring check
- otherwise, persist state files and have the host, wrapper, or operator reinvoke the same review-check prompt every 10 minutes
State files (stored in the repo root of the working worktree; the <PR_NUMBER> suffix prevents collision when multiple worktrees run in parallel):
.pr-flow-check-count-<PR_NUMBER>
.pr-flow-iteration-<PR_NUMBER>
.pr-flow-multi.txt
Each check should:
- fetch review status with
gh pr view <N> --json reviews,reviewDecision - fetch inline comments with
gh api repos/{owner}/{repo}/pulls/<N>/comments - detect new activity
- increment or clear the consecutive-no-activity counter
- after 3 consecutive quiet checks, trigger
/reviewonce if it has not already been requested
Phase 3: Respond to ALL Review Threads
Iteration cap: default MAX_ITERATIONS=5.
Critical rule: every review bot comment must receive a response, even if you disagree.
This includes:
- inline comments
- outside-diff comments embedded in review bodies
- review body suggestions
For each inline comment
- Read the full comment body
- Assess whether the issue is valid
- If valid, fix the code and reply with commit SHA and what changed
- If you disagree, reply with the reasoning
- Reply via
gh api repos/{owner}/{repo}/pulls/<PR_NUMBER>/comments/<ID>/replies -f body="..."
For outside-diff comments
These appear in the review body, not as inline threads.
Always address them in a PR comment summarizing fixes, then trigger re-review separately:
gh pr comment <PR_NUMBER> --body "## Addressed review feedback
### Inline comments (N/N resolved):
1. **Issue** - fixed in <sha>
### Outside-diff comments (N/N resolved):
1. **Issue** - fixed in <sha>"
Phase 4: Fix Issues and Push
- Read the relevant code before editing
- Edit files to address valid feedback
- Build to verify
- Run milestone code review before committing
- Stage explicit files through:
powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 add <path> [more paths...]
- Mark review complete:
powershell -ExecutionPolicy Bypass -File scripts/codex/guard.ps1 mark-review
Run auto-verify (lint, type-check, scope check). If auto-verify modifies any staged files, re-stage the affected files and re-run
guard.ps1 mark-reviewbefore committing —mark-reviewsnapshots the staged tree and the commit guard will reject a mismatch.Commit through the wrapper:
powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 commit -Message "fix(PR-feedback): address comment <ID>"
- Push through the wrapper:
powershell -ExecutionPolicy Bypass -File scripts/codex/git-safe.ps1 push origin <branch>
Phase 5: Resolve Threads
Resolve threads only after the fixes are pushed.
Use GraphQL to resolve review threads. Replace <OWNER>, <NAME>, <N> with the actual repository owner, name, and PR number before running:
$threads = @()
$cursor = $null
do {
$afterClause = if ($cursor) { ", after: `"$cursor`"" } else { "" }
$query = @"
query {
repository(owner: "<OWNER>", name: "<NAME>") {
pullRequest(number: <N>) {
reviewThreads(first: 100$afterClause) {
pageInfo { hasNextPage endCursor }
nodes { id isResolved path }
}
}
}
}
"@
$resp = gh api graphql -f query="$query" | ConvertFrom-Json
$page = $resp.data.repository.pullRequest.reviewThreads
$threads += $page.nodes
$cursor = if ($page.pageInfo.hasNextPage) { $page.pageInfo.endCursor } else { $null }
} while ($cursor)
Then call:
gh api graphql -f query='mutation { resolveReviewThread(input: {threadId: "<THREAD_ID>"}) { thread { id isResolved } } }'
Even threads you disagree with must be commented on and resolved. For out-of-scope suggestions, acknowledge that they are out of scope, reply, and resolve.
Phase 6: Wait for Re-Review
Increment the iteration counter, then return to Phase 2.
If MAX_ITERATIONS is reached:
- post a PR comment requesting human intervention
- stop automatic rework
- wait for guidance
Phase 7: Merge
Pre-merge checks:
- CI status passes
reviewDecision == APPROVED- unresolved review thread count is zero
Run the reusable guard first; it aborts on any failed check, calls
gh pr view --json reviewDecision,statusCheckRollup, and paginates
reviewThreads(first: 100, after: "<cursor>") until hasNextPage == false
before allowing the merge:
powershell -ExecutionPolicy Bypass -File scripts/codex/guard.ps1 pre-merge -PullRequestNumber <PR_NUMBER>
Then merge:
gh pr merge <PR_NUMBER> --squash --delete-branch
Phase 8: Update Issues and Tasks
After merge:
- close related issues if the PR closes them
- update Mercury task state if the orchestrator is available
- clean up state files:
Remove-Item .pr-flow-iteration-*, .pr-flow-check-count-*, .pr-flow-multi.txt -ErrorAction SilentlyContinue
Review Response Protocol
| Situation | Action |
|---|---|
| Valid inline issue | Fix code, reply with SHA, resolve thread |
| Valid outside-diff issue | Fix code, post PR comment |
| Disagree with suggestion | Reply explaining reasoning, resolve thread |
| Nitpick or style suggestion | Fix if trivial, otherwise explain and resolve |
| Out-of-scope suggestion | Acknowledge, explain it is out of scope, resolve |
Rules:
- even threads you disagree with must be commented on and resolved
- trigger re-review via
/reviewas a separate comment after all fixes are pushed - outside-diff comments still count as required responses
Output
PR: #<number> (<url>)
Review: approved | changes_requested | pending
Feedback: <N> inline, <N> outside-diff, <N> addressed, iteration=<N>
Merge: merged | waiting | blocked
Task: <taskId> -> done | pending