Iterate PR
Autonomously iterate on a pull request until CI checks pass and all comments are resolved. Each iteration runs in a fresh subagent.
Arguments
$ARGUMENTS- Maximum iterations (default: 3)
Instructions
Step 1: Initialize
Parse max iterations from $ARGUMENTS (default: 3). Set iteration counter to 0.
Step 2: Check State
Get the PR for the current branch:
!gh pr view --json number,url,headRefName,statusCheckRollup 2>/dev/null || echo "NO_PR"
If no PR exists: Proceed to Step 3 (first iteration will create one).
If PR exists: Get unresolved comments data:
!bash "${CLAUDE_PLUGIN_ROOT:-.agents}/skills/unresolved-pr-comments/scripts/unresolvedPrComments.sh" 2>/dev/null
Parse the JSON output and evaluate exit conditions.
If any CI checks are still PENDING, QUEUED, or IN_PROGRESS: Proceed to Step 3 regardless of comment count. Automated reviewers (e.g. CodeRabbit) may not have posted comments yet, so comment data is unreliable until all checks complete.
If statusCheckRollup is empty or null: Treat CI as passing (some PRs have no required checks configured).
Exit with success when ALL conditions are met:
- All CI checks have completed (no PENDING, QUEUED, or IN_PROGRESS statuses in statusCheckRollup) and none have failed (or no checks exist)
- No unresolved comments (totalUnresolvedComments == 0)
- No nitpicks (totalNitpicks == 0)
JSON Structure for Exit Evaluation:
{
"totalUnresolvedComments": number,
"totalNitpicks": number,
"unresolvedComments": [...],
"nitpickComments": [...]
}
Check totalUnresolvedComments == 0 and totalNitpicks == 0 for exit condition.
Report: "PR is clean! All CI checks pass and no unresolved comments."
Exit with status report when iteration counter >= max iterations:
- List failing CI checks
- List unresolved comment/nitpick counts
- Suggest running
/iterate-pragain to continue
Step 3: Spawn Iteration Subagent
Spawn a Task subagent with subagent_type: "general-purpose" using this prompt:
Handle one iteration of the PR feedback loop:
- Record Starting Commit: !
git rev-parse HEAD(save asstartCommit)- Commit and Push: Invoke
core:commit-push-prvia the Skill tool- Get PR Number: !
gh pr view --json number --jq '.number'- Wait for CI: !
rc=0; if command -v gtimeout >/dev/null 2>&1; then gtimeout 600 gh pr checks --watch || rc=$?; elif command -v timeout >/dev/null 2>&1; then timeout 600 gh pr checks --watch || rc=$?; else gh pr checks --watch || rc=$?; fi; case $rc in 0|1|8|124) ;; *) exit $rc;; esac(10 minute timeout; exit codes: 0=pass, 1=fail, 8=pending, 124=timeout are expected and handled in next step; other codes like 4=auth error are re-raised; uses gtimeout on macOS, timeout on Linux, no timeout as fallback)- Check CI Status: Run
gh pr checks --json name,state,bucketand parse the output
- If any check has
bucket: "fail", invokecore:fix-civia the Skill tool. Since you are running autonomously, do NOT wait for user approval — apply the fixes directly. Report what was fixed and exit.- Check Comments: Run
bash "${CLAUDE_PLUGIN_ROOT:-.agents}/skills/unresolved-pr-comments/scripts/unresolvedPrComments.sh"and parse the JSON output
- If unresolved comments or nitpicks exist:
- Group comments by file path and read each file once (not per-comment)
- If a file no longer exists, note the comment may be outdated and skip it
- Assess each comment with an explicit verdict:
- Agree: Explain why, then fix it
- Disagree: Explain why the current code is acceptable; do NOT change the code
- Already fixed: Note that the code already addresses this concern
- After assessing all comments, fix only those you agreed with and exit (next iteration will commit fixes)
- Report: Run
git rev-parse HEADand compare tostartCommitto determine if commits were made. Include PR status, CI status, and comments addressed
Step 4: Loop
After the subagent completes:
- Increment iteration counter
- If no commits were made this iteration:
- Get unresolved comments by running:
bash "${CLAUDE_PLUGIN_ROOT:-.agents}/skills/unresolved-pr-comments/scripts/unresolvedPrComments.sh" - If unresolved comments remain, exit with: "Comments addressed, awaiting reviewer resolution. Run
/iterate-prafter reviewer responds."
- Get unresolved comments by running:
- Report: "Iteration [N]/[max] complete. Checking state..."
- Return to Step 2
Examples
Successful completion:
Iteration 1/3: No PR exists
> commit-push-pr -> PR #347 created
> CI failed (lint errors) -> fix-ci -> fixed missing semicolons
Iteration 2/3: CI failures remain
> commit-push-pr -> pushed fixes
> CI passed, 2 review comments
> unresolved-pr-comments -> addressed null check, const usage
Iteration 3/3: Comments pending
> commit-push-pr -> pushed fixes
> CI passed, no unresolved comments
PR is clean! All CI checks pass and no unresolved comments.
Awaiting reviewer resolution:
Iteration 1/3: PR exists with comments
> commit-push-pr -> pushed comment fixes
> CI passed, 1 comment remains (disagreed with suggestion)
Iteration 2/3: No commits made, comments remain
Comments addressed, awaiting reviewer resolution. Run `/iterate-pr` after reviewer responds.
Input
Maximum iterations: $ARGUMENTS
Converted and distributed by TomeVault — claim your Tome and manage your conversions.