Pr:Iterate Auto

Automated review-fix iteration loop until PR is approved or max iterations reached

jifflee updated 4mo ago
Claude CodeGeneric
View source ↗
---
description: Automated review-fix iteration loop until PR is approved or max iterations reached
---

# PR Iterate

Automated review-fix loop that runs `/pr-review-internal` and `/pr-fix` until the PR is approved or the maximum iteration count is reached.

## Usage

/pr-iterate # Default: max 3 iterations /pr-iterate --max 5 # Allow up to 5 iterations /pr-iterate --auto # Non-interactive mode (no prompts) /pr-iterate --skip-security # Skip security review for faster iterations


## Container Mode Support

When running in a container (detected automatically), `/pr-iterate` operates in non-interactive mode:

**Auto-detection triggers:**
- Environment variable `CLAUDE_CONTAINER_MODE=true`
- No TTY available (`[ ! -t 0 ]`)
- Running inside Docker (presence of `/.dockerenv`)

**Container mode behavior:**
- `--auto` flag is automatically enabled
- No user prompts - continues until max iterations or approval
- Status updates written to `pr-status.json` for host monitoring
- Uses reduced iteration count (default: 2) for faster feedback
- Automatically pushes fixes after each iteration

**Example container invocation:**
```bash
# In container context (automatically detected)
claude -p "/pr-iterate"

# Explicit container-friendly invocation
claude -p "/pr-iterate --auto --max 2"

Status persistence: Container mode reads/writes to pr-status.json at these locations (in order):

  1. PR_STATUS_FILE environment variable
  2. /workspace/repo/pr-status.json
  3. /workspace/pr-status.json
  4. ./pr-status.json

Prerequisites

  • Must be in a worktree with an open PR
  • PR must target dev branch
  • pr-status.json will be created if it doesn't exist

Token Optimization

This skill uses a data-gathering script to batch GitHub API calls:

Data Script: ./scripts/pr/pr-iterate-data.sh

Optimizations:

  • Single gh pr view call captures all PR metadata
  • Reads pr-status.json for review state (no API needed)
  • Pre-calculates iteration limits and stop conditions
  • Returns structured JSON for direct consumption

Token Usage:

  • Before: ~2,300 tokens (7 inline gh calls, parsing overhead)
  • After: ~725 tokens (single data script, structured output)
  • Savings: 68%

Steps

1. Initialize

# Detect container mode
CONTAINER_MODE=false
if [ "$CLAUDE_CONTAINER_MODE" = "true" ] || [ -f "/.dockerenv" ] || [ ! -t 0 ]; then
    CONTAINER_MODE=true
    echo "Container mode detected - running non-interactively"
    AUTO_MODE=true  # Force auto mode in containers
fi

# Gather all data in single script call (container-aware)
DATA=$(./scripts/pr/pr-iterate-data.sh --max "${MAX:-3}" ${CONTAINER_MODE:+--container})

# Check for errors
if [ "$(echo "$DATA" | jq -r '.has_pr')" = "false" ]; then
  echo "Error: $(echo "$DATA" | jq -r '.error')"
  echo "$(echo "$DATA" | jq -r '.suggestion')"
  exit 1
fi

# Extract PR info from cached data
PR_NUMBER=$(echo "$DATA" | jq -r '.pr.number')
PR_TITLE=$(echo "$DATA" | jq -r '.pr.title')
echo "Starting review iteration for PR #${PR_NUMBER}: ${PR_TITLE}"

# Get iteration config from cached data (includes container-aware defaults)
MAX_ITERATIONS=$(echo "$DATA" | jq -r '.iteration_config.max_iterations')
CURRENT_ITERATION=$(echo "$DATA" | jq -r '.review_state.iteration')
PR_STATUS_FILE=$(echo "$DATA" | jq -r '.status_file // "pr-status.json"')

echo "Using status file: ${PR_STATUS_FILE}"

# Check if we should continue
if [ "$(echo "$DATA" | jq -r '.iteration_config.can_iterate')" = "false" ]; then
  REASON=$(echo "$DATA" | jq -r '.iteration_config.stop_reason')
  case "$REASON" in
    already_approved)
      echo "PR already approved - no iteration needed"
      ;;
    max_iterations_reached)
      echo "Max iterations ($MAX_ITERATIONS) already reached"
      ;;
    pr_already_merged)
      echo "PR already merged - nothing to iterate"
      ;;
    pr_closed)
      echo "PR is closed - cannot iterate"
      ;;
  esac
  exit 0
fi

2. Iteration Loop

while CURRENT_ITERATION < MAX_ITERATIONS:
    CURRENT_ITERATION++

    echo "=== Iteration ${CURRENT_ITERATION}/${MAX_ITERATIONS} ==="

    # Step 2a: Run review
    /pr-review-internal

    # Step 2b: Check status (read from local file, no API call)
    STATUS=$(jq -r '.review_state.status' pr-status.json)

    if STATUS == "approved":
        echo "PR approved after ${CURRENT_ITERATION} iteration(s)"
        break

    if STATUS == "needs_fixes":
        BLOCKING_COUNT=$(jq '[.blocking_issues[] | select(.status=="open")] | length' pr-status.json)
        echo "Found ${BLOCKING_COUNT} blocking issue(s)"

        # Step 2c: Run fixes
        /pr-fix

        # Step 2d: Push changes
        git push

        # Continue to next iteration

    if STATUS == "incomplete":
        echo "Review incomplete (agent timeout?)"
        # Prompt for manual intervention unless --auto
        if not AUTO_MODE:
            prompt "Continue iteration? [y/n]"

3. Review Phase

For each iteration, invoke /pr-review-internal:

Use the Skill tool to invoke /pr-review-internal

The skill will:
1. Run all PR review agents (code, test, docs, security)
2. Collect findings into pr-status.json
3. Generate review-findings.md
4. Set review_status to "approved" or "needs-fixes"

Skip security option:

If --skip-security flag is set, pass it to the review:

/pr-review-internal --skip-security

4. Fix Phase

When status is "needs-fixes", invoke /pr-fix:

Use the Skill tool to invoke /pr-fix

The skill will:
1. Read blocking_issues from pr-status.json
2. Group issues by owning agent
3. Invoke each agent to fix their issues
4. Create commits for fixes
5. Update pr-status.json with resolved issues

5. Push Changes

After fixes are applied:

# Push to remote
git push

# Update iteration count in pr-status.json
jq --arg iter "$CURRENT_ITERATION" \
  '.iteration = ($iter | tonumber)' \
  pr-status.json > pr-status.json.tmp && mv pr-stat

Maintain Pr:Iterate Auto?

Let people know it's listed here — add the badge (live metrics, light/dark aware) or a plain link to your README or docs.

[Pr:Iterate Auto on getagentictools](https://getagentictools.com/loops/jifflee-pr-iterate?ref=badge)
npx agentictools info loops/jifflee-pr-iterate

The second line is the CLI lookup for this page — handy in READMEs and docs.