---
name: coderabbitloop
description: >
  Iteratively drives a GitHub PR to zero unresolved CodeRabbit (coderabbitai[bot]) findings. Reads
  each inline comment's own "Prompt for AI Agents" block (CodeRabbit's ready-made fix spec per
  issue), applies the fix, replies to the thread explaining what was done, resolves it, pushes, and
  re-triggers CodeRabbit. Repeats until clean or a real blocker needs a human. Use when the user
  wants a PR fully addressed against CodeRabbit's review, or says "run coderabbitloop" / "satisfy
  CodeRabbit" / "resolve CodeRabbit comments".
compatibility: Requires git and gh (GitHub CLI) authenticated, with the CodeRabbit GitHub App installed on the repo.
metadata:
  version: "1.1"
allowed-tools: Bash(gh:*) Bash(git:*)
---
<!-- Generated by scripts/build-adapters.sh. Do not edit directly. -->

# Coderabbitloop

Fix every actionable CodeRabbit finding on a PR, answer each thread, resolve it, and keep going until CodeRabbit has nothing left to say (or a finding needs a human call).

## Inputs

- **PR number** (optional): detect from the current branch if not given.

## How CodeRabbit actually posts (verified against docs.coderabbit.ai and a live install: don't assume otherwise)

CodeRabbit runs as `coderabbitai[bot]` and posts **two independent things**, not one:

1. An auto-generated **walkthrough/summary issue comment**: body starts with the HTML marker `<!-- This is an auto-generated comment: summarize by coderabbit.ai -->`. This is a description, not a review: use it for context only, never as a source of fixable findings.
2. A **formal PR review** carrying one **inline review comment per finding**, each a real, resolvable GitHub review thread at the finding's file/line. Each one contains a `<details><summary>🤖 Prompt for AI Agents</summary>` block: a ready-made fix spec, CodeRabbit's own words, meant to be handed straight to an agent.

**Use the inline threads as the source of truth**, exactly like Greptile/Qodo: fetch via GraphQL `reviewThreads`, which carries native `isResolved` state (see `references/graphql-queries.md`, shared with `qodoloop`; only the bot login and the prompt-heading text differ).

CodeRabbit has slash commands (official, `docs.coderabbit.ai/reference/review-commands`), the ones this loop actually uses:
- `@coderabbitai review`: incremental review of new changes (use this to re-trigger after a push, if automatic review-on-push is off for this repo).
- `@coderabbitai resolve`: marks **all** CodeRabbit threads resolved in one shot. This is a bulk escape hatch, not the default path: this loop resolves threads one at a time, right after replying to each, so the reply-then-resolve record stays intact per finding.
- CodeRabbit also has its own built-in `@coderabbitai autofix`, which commits fixes directly without a model in the loop. Mention it to the user as an alternative if they'd rather skip the loop entirely, but this skill's whole point is the Claude-orchestrated version: don't default to autofix instead of doing the work.

**Rate limit is a real terminal state, not a bug.** A comment reading "you've reached your PR review limit" (confirmed live wording) means CodeRabbit will not review again this cycle: stop and report it, don't loop waiting for something that isn't coming.

## Instructions

### 1. Identify the PR

```bash
gh pr view --json number,headRefName,headRefOid -q '{number, branch: .headRefName, sha: .headRefOid}'
```

Check out the branch if not already on it, **then require a clean working tree**
(`git status --porcelain` empty) before touching anything:

```bash
[ -z "$(git status --porcelain)" ] || { echo "working tree not clean, refusing to start: would risk committing unrelated changes"; exit 1; }
```

This is the precondition step E's scoped `git add <file>` depends on: staging only the files a fix
touched is safe *because* the tree started clean. Skip it and a file with pre-existing local edits
gets those edits swept into the fix commit right along with it.

### 2. Loop (max 5 iterations)

Keep an explicit iteration counter and stop at 5: the cap is a real bound to enforce, not a figure
of speech. Each pass through A–F is one iteration; on hitting the cap, go straight to the report.

#### A. Wait for a review of the current head

On **iteration 1**, if there is nothing to push yet (you haven't fixed anything in this loop), don't
wait for a brand-new review: use whatever CodeRabbit has already posted as your starting point and
go straight to step B. Otherwise:

```bash
SINCE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
git push
```

If this repo doesn't auto-review on push, nudge it:

```bash
gh pr comment <PR_NUMBER> --body "@coderabbitai review"
```

Poll (every ~10s, timeout ~5min) for CodeRabbit's next formal review to land: a new entry in
`gh api --paginate repos/{owner}/{repo}/pulls/<PR_NUMBER>/reviews` from `coderabbitai[bot]`
(REST spelling carries the `[bot]` suffix) created after `$SINCE`. Follow pagination so a review
that lands on page two is not missed. If you instead see a rate-limit comment ("review limit"),
**stop the loop now** and go to Report with `blocked: rate-limited`. If no review arrives within
the timeout after one retry, stop and report the timeout honestly; do not claim success.

#### B. Fetch unresolved findings

Run the `unresolvedQodoThreads`-shaped query in `references/graphql-queries.md` (filter `author.login`
on GraphQL's bare `coderabbitai`, **not** REST's `coderabbitai[bot]`: see the reference's login-spelling
note, and follow `pageInfo.hasNextPage`/`endCursor` until exhausted). For each unresolved thread,
parse the finding text, its `databaseId`, and the **Prompt for AI Agents** block (heading text is
`🤖 Prompt for AI Agents`, not `Agent Prompt`: match case/emoji-insensitively).

Mark each thread as seen with an 👀 reaction on its first comment (`databaseId` from the query), so
the bot and a watching human can tell it is being worked:

```bash
gh api --method POST repos/{owner}/{repo}/pulls/comments/<DATABASE_ID>/reactions -f content=eyes
```

#### C. Check exit conditions

Stop if there are zero unresolved CodeRabbit threads, or max iterations reached.

#### D. Fix each finding

For each thread, in order: read the Prompt for AI Agents block in full: it names the issue and the fix. Apply it, staying inside its stated scope. If the suggestion is wrong, unsafe, or needs a product decision, don't force it: note that in step F instead. Track two lists as you go: **fixed** (thread id + the exact files you touched) and **skipped** (thread id + why). Do not resolve anything yet.

#### E. Commit and push FIRST, before resolving anything

Order matters. A resolved thread is a claim that the fix is on the branch, so the push has to succeed
before the claim is made: otherwise a failed commit or push leaves the PR unfixed with the finding
marked resolved, and nobody looks at it again.

If step D changed code:

```bash
# Stage ONLY the files your fixes touched: never `git add -A`, which sweeps up
# unrelated work and untracked secrets sitting in the worktree.
git status --short                     # look before you stage
git add <path> [<path>...]             # the files named in the findings you fixed
git commit -m "address CodeRabbit review feedback (coderabbitloop iteration N)"
git push
```

Confirm the push actually landed before continuing:

```bash
git rev-parse HEAD
gh pr view <PR_NUMBER> --json headRefOid -q .headRefOid   # must match
```

If they differ, stop: the fix is not on the PR, so nothing may be resolved yet.

#### F. Reply to each finding, react, then resolve

Only now, with the fixes pushed, reply and resolve. **Reply to the thread first, then react, then
resolve it**, the "answer to the comments" half of the job:

```bash
gh api graphql -f query='
  mutation($threadId: ID!, $body: String!) {
    addPullRequestReviewThreadReply(input: {pullRequestReviewThreadId: $threadId, body: $body}) {
      comment { id }
    }
  }' -f threadId="$THREAD_ID" -f body="$REPLY_TEXT"
```

Pass the thread id and reply text as GraphQL **variables** (`-f threadId=... -f body=...`), never
interpolated into the query string: a reply containing a quote, backtick, or newline breaks (or
injects into) a hand-built query.

Reply body: one or two sentences, what you changed and why, or exactly why you didn't fix it. A
finding you deliberately skip still gets a reply and gets resolved.

Then record the disposition as a reaction on the thread's first comment: the learning signal, and a
visible marker for a human scanning the PR. 👍 (`+1`) for fixed, 👎 (`-1`) for skipped/rebutted.
CodeRabbit reads a 👎 as feedback that the finding was wrong.

```bash
gh api --method POST repos/{owner}/{repo}/pulls/comments/<DATABASE_ID>/reactions -f content=+1
# skipped/rebutted finding:
gh api --method POST repos/{owner}/{repo}/pulls/comments/<DATABASE_ID>/reactions -f content=-1
```

Then resolve (`resolveReviewThread`). Reply first, react, then resolve: a reaction never replaces
the reply, and a resolved thread with no reply loses the reasoning.

Go back to step A.

### 3. Report

| Field              | Value |
|--------------------|-------|
| Iterations         | N     |
| Findings resolved  | N     |
| Findings skipped   | N (with reasons) |
| Remaining          | N (if any) |
| Blocked            | rate-limited / timeout / max-iterations / none |

```
Coderabbitloop complete.
  Iterations:  2
  Resolved:    4 (3 fixed, 1 false-positive, replied + resolved)
  Remaining:   0
```

## References

- `references/graphql-queries.md`: shared with `qodoloop`: the exact `reviewThreads` query (including the `databaseId` the reaction step needs) and the `resolveReviewThread` / `addPullRequestReviewThreadReply` mutations. Filter `author.login` on GraphQL's bare `coderabbitai` (never `coderabbitai[bot]`) and match the prompt heading `Prompt for AI Agents`.
