| 1 | # GitHub App Setup (Codewhale Agent reviews) |
| 2 | |
| 3 | `codewhale review --pr N` writes an advisory code review of a pull request. With |
| 4 | `--post` (or from CI) the review is published to GitHub. Published reviews can |
| 5 | appear under two identities: |
| 6 | |
| 7 | - the default token the CI job already has (`github.token`), or |
| 8 | - a dedicated **GitHub App** so the review shows as a bot — e.g. |
| 9 | `codewhale-agent[bot]` — instead of a personal account. |
| 10 | |
| 11 | The App identity is optional. Nothing below is needed to run |
| 12 | `codewhale review --pr N` locally and print the report to your terminal. |
| 13 | |
| 14 | Related docs: |
| 15 | |
| 16 | - [Automatic Workflows](AUTOMATIC_WORKFLOWS.md) — the review workflow in context |
| 17 | - [Providers](PROVIDERS.md) — the model/key used to write the review |
| 18 | - [Receipts](RECEIPTS.md) — how posted reviews are anchored to a head SHA |
| 19 | |
| 20 | ## Actions setup and model selection |
| 21 | |
| 22 | Use [the reusable GitHub Action setup](GITHUB_ACTION.md) for the workflow, |
| 23 | account machine key, exact model, release pin, limits, outcomes and retries. |
| 24 | The repository workflow is a thin caller of the same analysis and publication |
| 25 | actions. Select your own provider and model; the checksummed CLI release does |
| 26 | not compile or execute a PR's candidate source. An App controls the publishing |
| 27 | identity independently of the model provider. The workflow generator can use |
| 28 | an existing App with `--app-login 'YOUR-APP[bot]'`. |
| 29 | |
| 30 | ## Review evidence and precision |
| 31 | |
| 32 | The Actions-backed GitHub App and the `review` tool use the same PR review |
| 33 | contract. Findings must explain an introduced defect's trigger, source evidence, |
| 34 | impact and a useful fix. Generic requests for more tests, style preferences and |
| 35 | unsupported compiler claims do not qualify as findings. An empty findings list |
| 36 | is valid; unresolved assumptions belong in the assessment. |
| 37 | |
| 38 | When the exact PR head is available locally, each pass also receives numbered |
| 39 | source excerpts around its changed hunks and nearby module declarations. These |
| 40 | come from regular Git blobs at the pinned head, never from dirty checkout files |
| 41 | or symlink targets. Source is not executed and no additional model call is made. |
| 42 | The excerpts use only the unused portion of `CODEWHALE_REVIEW_MAX_CHARS`, capped |
| 43 | at 50000 characters and 32 files per pass; individual blobs above 128 KiB are |
| 44 | omitted. The complete diff remains intact and remains the inline-comment scope. |
| 45 | |
| 46 | The request explicitly records unavailable files and omitted context. It does |
| 47 | not inspect unchanged caller files or run builds/tests, and a completed review |
| 48 | does not establish either. These source and local-fixture guarantees do not |
| 49 | establish a model's bug-detection rate or parity with another review product. |
| 50 | |
| 51 | ## Output budget |
| 52 | |
| 53 | `CODEWHALE_REVIEW_MAX_OUTPUT_TOKENS` optionally sets the CLI's output budget |
| 54 | through `CODEWHALE_MAX_OUTPUT_TOKENS`. Without it, the CLI chooses its automatic |
| 55 | cap. The workflow rejects values below **8192** to leave room for reasoning |
| 56 | and the final review. Provider accounting and supported limits vary; an empty |
| 57 | response is not proof of any one cause. A zero-exit review with empty output |
| 58 | fails the job. |
| 59 | |
| 60 | ## One-time setup, five steps |
| 61 | |
| 62 | You need owner access to the GitHub repository once. After setup, eligible non-draft |
| 63 | same-repository pull requests can post reviews as the App. |
| 64 | |
| 65 | 1. **Create the App.** GitHub → *Settings → Developer settings → GitHub Apps → |
| 66 | New GitHub App*. Name it (e.g. `Codewhale Agent`), set a homepage URL, and |
| 67 | **uncheck Webhook → Active** — the review is pulled on PR events by Actions, |
| 68 | so no webhook is needed. |
| 69 | 2. **Grant two repository permissions.** |
| 70 | - *Pull requests* → **Read & write** (to post the review and inline comments) |
| 71 | - *Contents* → **Read-only** (to read the diff; read-only is enough — avoid |
| 72 | write unless you have another reason) |
| 73 | Choose *Only on this account*, then **Create GitHub App**. |
| 74 | 3. **Download the private key.** On the App's page, *Private keys → Generate a |
| 75 | private key*. Keep the `.pem` file secret; it is the App's credential. |
| 76 | 4. **Install the App** on your account (*Install App* on the same page) and |
| 77 | select the repositories reviews should cover. |
| 78 | 5. **Add repository settings.** GitHub → *Settings → Secrets and |
| 79 | variables → Actions*: |
| 80 | |
| 81 | | Kind | Name | Value | |
| 82 | |----------|-----------------------------|-------------------------------------| |
| 83 | | Variable | `CODEWHALE_APP_ID` | the App ID shown on the App's page | |
| 84 | | Secret | `CODEWHALE_APP_PRIVATE_KEY` | the full `.pem` file contents | |
| 85 | | Secret | `CODEWHALE_API_KEY` | a Codewhale machine key for this repository workflow | |
| 86 | | Variable | `CODEWHALE_REVIEW_MODEL` | exact account catalog `provider/model` id (required) | |
| 87 | | Variable | `CODEWHALE_REVIEW_VERSION` | exact released CLI tag (override with `--version` in `setup.mjs`) | |
| 88 | |
| 89 | App settings control identity. Model access separately requires a review |
| 90 | key and, for account mode, the catalog model. Optional budget variables are |
| 91 | `CODEWHALE_REVIEW_MAX_CHARS`, `CODEWHALE_REVIEW_MAX_PASSES`, and |
| 92 | `CODEWHALE_REVIEW_MAX_OUTPUT_TOKENS`. |
| 93 | |
| 94 | ## How the pieces connect |
| 95 | |
| 96 | [The review workflow](../.github/workflows/codewhale-review.yml) uses |
| 97 | `pull_request` and a manual `workflow_dispatch` recovery trigger. The analysis action reads the PR's exact Git objects in a fresh repository |
| 98 | without checking them out. Automatic fork events receive no model key. Draft |
| 99 | and closed PRs are rejected. With `allow-forks: true`, an authorized |
| 100 | maintainer can review a fork diff through a PR issue comment or manual run |
| 101 | from the default branch. Both analysis and publication recheck permissions, |
| 102 | triggering comments and revisions. |
| 103 | |
| 104 | When generated with an App login, the publisher mints a short-lived |
| 105 | installation token restricted to contents:read and pull_requests:write. |
| 106 | App credentials and model credentials are kept in separate jobs. The publisher |
| 107 | verifies the returned author against that exact login. Without App setup, |
| 108 | generated workflows use `github.token` and `github-actions[bot]`. The action |
| 109 | emits one COMMENT review, never approval or a request for changes. CODEOWNERS |
| 110 | stays the human authority. Setup or provider failures fail the optional review |
| 111 | job and save a sanitized receipt; they do not post additional status comments. |
| 112 | |
| 113 | The Actions-only App setup above does not describe the managed hosted App. |
| 114 | Do not disable the webhook on an existing App that also serves hosted mentions. |
| 115 | |
| 116 | ## Running a review yourself |
| 117 | |
| 118 | ```sh |
| 119 | # print a report locally (uses your configured provider key) |
| 120 | codewhale review --pr 1234 |
| 121 | |
| 122 | # pin the route when a model is reachable through more than one provider |
| 123 | codewhale --provider deepseek --model MODEL_ID review --pr 1234 |
| 124 | |
| 125 | # account mode: check the agent, then use an exact id from the account catalog |
| 126 | codewhale --no-project-config account agent |
| 127 | codewhale --no-project-config --provider codewhale --model PROVIDER/MODEL_ID review --pr 1234 |
| 128 | |
| 129 | # explicitly increase a complete-diff input limit when needed |
| 130 | codewhale review --pr 1234 --repo OWNER/REPO --max-chars 6000000 |
| 131 | |
| 132 | # explicitly authorize at most 8 complete ordered model passes |
| 133 | codewhale review --pr 1234 --repo OWNER/REPO --max-passes 8 |
| 134 | |
| 135 | # publish it to GitHub as whichever identity GH_TOKEN carries |
| 136 | codewhale review --pr 1234 --post |
| 137 | ``` |
| 138 | |
| 139 | `GH_TOKEN` may be your `gh` CLI token (posts as you) or an App installation |
| 140 | token (posts as the App). The `--post` flag is always opt-in. |
| 141 | |
| 142 | ## Troubleshooting |
| 143 | |
| 144 | - **Review posts as you, not the bot.** Generate the workflow with the exact App |
| 145 | login, and set both App ID and private-key settings. App-enabled generated |
| 146 | workflows fail on missing credentials instead of changing the author. |
| 147 | - **No model review completed.** Read the outcome receipt and the |
| 148 | [repair guide](GITHUB_ACTION.md#outcomes-and-recovery). |
| 149 | - **Account model or provider error.** Set the provider to `codewhale` (or |
| 150 | unset it), choose the exact model from the account catalog, and check that |
| 151 | the machine key has the required scopes and the account has a configured |
| 152 | agent. A vendor key belongs in its own secret, never `CODEWHALE_API_KEY`. |
| 153 | - **Complete diff exceeds the input limit.** Inspect the reported size and |
| 154 | model context capacity before raising `CODEWHALE_REVIEW_MAX_CHARS`. An 8 MiB |
| 155 | transport-bound failure cannot be bypassed with that variable. |
| 156 | - **PR head changed or history is unavailable.** Rerun for the current |
| 157 | revision. The workflow refuses to review an unverified snapshot. |
| 158 | - **"available from configured provider route(s): ...".** Two provider keys are |
| 159 | configured and the model is reachable from both. Use an explicit provider in your own Action configuration. |
| 160 | - **Empty review.** The job fails. Inspect provider errors and output-budget |
| 161 | receipts; increasing `CODEWHALE_REVIEW_MAX_OUTPUT_TOKENS` may help when |
| 162 | reasoning exhausted the budget, but does not diagnose the cause by itself. |
| 163 | - **App token step fails.** The `.pem` was regenerated after the secret was |
| 164 | set — paste the newest key into `CODEWHALE_APP_PRIVATE_KEY` again, and |
| 165 | confirm the App is actually installed on the repository. |
| 166 | - **Name already taken.** GitHub App names are global; pick another name. The |
| 167 | bot's display login is `<slug>[bot]`, derived from the name. |
| 168 |