<!-- KeepSnappy docs for keepsnappy CLI 0.1.0-alpha.35 -->

# AI investigation and fixes in CI

> Add keepsnappy act to your KeepSnappy job, to have Claude look for the cause of a pull request's alarms and propose a fix. What the policy sets, what act investigates, what fix mode changes, and what is posted.

`keepsnappy act` works with Claude only. It runs the same review as [`keepsnappy review`](https://keepsnappy.com/docs/ai-review.md), then Claude looks for the cause of the alarms your policy names. It can also propose a fix. What the AI steps cost and what leaves your CI are on [AI review and investigation in CI](https://keepsnappy.com/docs/ai-in-ci.md); every flag is under [act](https://keepsnappy.com/docs/reference/cli.md#act).

## Add the act step

Add the step after the measuring step of your KeepSnappy job. Here is that job with `act` added, cut down ([the complete workflow](https://keepsnappy.com/docs/ci.md#a-complete-workflow)).

```yaml
name: perf
on:
  pull_request:

jobs:
  keepsnappy:
    runs-on: macos-15
    permissions:
      contents: read
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0
      - id: measure
        run: npx --no-install keepsnappy run --upload --wait --commit ${{ github.event.pull_request.head.sha || github.sha }}
        env:
          KEEPSNAPPY_KEY: ${{ secrets.KEEPSNAPPY_KEY }}
      - if: ${{ !cancelled() }}
        run: npm install --global @anthropic-ai/claude-code
      - if: ${{ !cancelled() }}
        run: npx --no-install keepsnappy act
        env:
          KEEPSNAPPY_READ_KEY: ${{ secrets.KEEPSNAPPY_READ_KEY }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GH_TOKEN: ${{ github.token }}
```

- `if: ${{ !cancelled() }}` runs the AI steps even when the measuring step fails, such as on a timeout.
- `fetch-depth: 0` fetches the commits the comparison needs.
- `KEEPSNAPPY_READ_KEY` is a [read key](https://keepsnappy.com/docs/glossary.md#read-key). Make it on **Setup** with **Connect a coding agent**. `act` never gives Claude your CI key.
- `ANTHROPIC_API_KEY` is your own AI key. Without it, `act` uses `claude`'s login on the machine.
- `GH_TOKEN` and `pull-requests: write` let `act` comment on the pull request. Claude never sees the token.

Claude Code (`claude`) must be 2.1.308 or newer. An older one stops the step before any AI call.

To preview on your Mac: `keepsnappy act --run-set ID --dry-run`, with the run set ID from `keepsnappy-result.json` or the dashboard link. It publishes nothing, but still sends code to your AI provider. From a fork checkout, set `GITHUB_REPOSITORY` to the owner and repository that contain the pull request before running act locally, for example `your-team/my-app-fork`. When the variable is unset, the CLI uses the checkout's `origin` remote.

## Set the policy

<span id="the-policy"></span>`act` reads its policy from the `on_alarm` section of `keepsnappy.yml` in your repository's root. If the root also has a `.keepsnappy.yml`, `act` reads the section from that file instead. Without the section, `act` runs the review only.

```yaml
app:
  bundle_id: com.example.app
platform: ios-sim
flows:
  runner: command
  command: maestro test --include-tags perf e2e/flows/
on_alarm:
  investigate: [worse, slightly_worse]
  fix: [worse_blocks]
  fix_as: pr
  max_runs_per_day: 5
```

| Key | Default | What it sets |
|---|---|---|
| `investigate` | off | The alarms that get an investigation: `worse_blocks` (a **Worse** that blocks the merge), `worse` (any **Worse**) and `slightly_worse`. |
| `fix` | off | The alarms that get a fix, with the same words. See [Fix mode](#fix-mode). |
| `fix_as` | `pr` | `pr` opens a draft pull request. `suggestions` posts a small fix as a suggestion. |
| `fix_dispatch_workflow` | the running workflow | The file name of the workflow that measures a fix, for example `perf.yml`. |
| `agent` | `claude` | Only `claude` is accepted. |
| `max_runs_per_day` | `5` | Investigation and fix runs of the app per UTC day, from 0 to 50. |
| `max_fix_prs_per_finding` | `1` | Open fix pull requests one alarm may have, from 0 to 5. |
| `max_dispatches_per_fix` | `1` | Workflow runs one fix push may have, from 0 to 3. |
| `max_usd_per_run` | `1.00` | What one investigation may spend, in US dollars, at most 20. |

`worse_blocks` follows KeepSnappy's own blocking, so `verdict_gate: advisory` doesn't change it ([advisory or blocking](https://keepsnappy.com/docs/ci.md#required-or-advisory-checks)). A wrong value or an unknown key stops `act` with `2`, naming the key.

### What a pull request can change

<span id="the-daily-limit"></span>The policy is read from the checked-out commit, so a pull request can edit its own policy. The CLI still holds each limit to the ceiling in the table. KeepSnappy counts the day's runs itself, across all your CI jobs, from 00:00 UTC. A looser limit comes only from a flag or your own config ([details](https://keepsnappy.com/docs/ai-in-ci.md#what-review-and-act-share)).

## What act investigates

Keep the source map from the exact Release build you measure. Run the [source-map upload](https://keepsnappy.com/docs/reference/cli.md#sourcemaps) in the build job, with that build's ID or full commit SHA. When a Metro dependency graph was generated, leave it next to the map so the uploader can send both. Without the matching map, act falls back to following imports in the checkout, which may miss a changed parent component. See [What the graph contains](https://keepsnappy.com/docs/data.md#the-metro-dependency-graph).

For each open alarm the policy names, Claude reads the code that changed. It returns a hypothesis (shown as **Likely cause** on the dashboard): the likely cause, the files and lines, and the change that would prove it. The next measured session decides. A hypothesis never changes a result.

Alarms that share a changed file are investigated once, as a group. `act` skips an alarm a person marked a false alarm. It also skips an alarm that doesn't block when the review called it a false alarm with confidence 0.8 or more. A CI re-run doesn't investigate the same session twice.

<span id="what-the-agent-can-do"></span>Claude reads your code in a separate worktree, at the measured commit, with `Read`, `Grep` and `Glob`. It has no shell, no web, no write access and no GitHub token. If the checkout has a symlink that points outside it, nothing starts.

When no cause is clear, the pull request says "not sure what caused it", and nothing is stored or fixed.

## Which models it asks

An investigation shows Claude the code level by level, from the changed lines out to the whole app. Levels 1 and 2 ask `claude-haiku-5-5`, then `claude-sonnet-5-5` once if the answer is unusable or unsure. Level 3 asks `claude-sonnet-5-5`. Level 4 asks `claude-opus-5-5`, only for an alarm that blocks. `keepsnappy act --max-level 3` stops before level 4. A fix asks `claude-haiku-5-5`, and `claude-sonnet-5-5` once if a check rejects its change. The review part asks [the review's models](https://keepsnappy.com/docs/ai-review.md#which-model-answers).

## Fix mode

`act` fixes the alarms of the kinds `fix` lists that have a cause of medium or high confidence. Claude changes the code in a separate worktree, and `act` publishes the change. The next measured session says whether it helped.

The fix may not touch workflow files, lockfiles, credentials, links or binaries, or delete files, and its size is limited. If the change holds a key or token shape, nothing is pushed.

For fix mode, add `workflow_dispatch` and two permissions:

```yaml
on:
  pull_request:
  workflow_dispatch:

jobs:
  keepsnappy:
    permissions:
      contents: write
      pull-requests: write
      actions: write
```

Also turn on "Allow GitHub Actions to create and approve pull requests" in your repository's settings, under Actions, General.

<span id="as-a-draft-pull-request-fix_as-pr"></span>With `fix_as` set to `pr`, `act` pushes one branch, `keepsnappy/fix-…`, and opens a draft pull request into the measured branch. It never pushes to your branch or forces. Then it starts your workflow on the fix branch, and that run posts each place's result before and after on the fix pull request. When the fix changes a file that runs in CI, such as `package.json`, `act` starts nothing and says to measure it by hand.

We recommend a ruleset that requires a pull request on your other branches, with pushes allowed to `keepsnappy/fix-*`.

With `fix_as` set to `suggestions`, a fix of one file and at most 20 changed lines inside the pull request's diff is posted as a suggestion. A larger fix gets a comment that says why. This mode needs no `contents: write` and doesn't work for an alarm on `main`.

## What act posts on a pull request

- One inline comment at the cause of each changed hunk, on the lines it points at, else on the file. A re-run edits it instead of posting it again. When the cause moves, the old comment is marked superseded.
- A short likely cause in the one general verdict comment, the comment `pr-comment` writes. It links to the inline comments, worst first, at most three. A cause in a file the pull request does not change gets one line with the file, the line and the first sentence of the explanation. When nothing was investigated, act removes its earlier cause.
- In fix mode, each draft fix pull request gets a line in that cause, and the fix branch's run later adds the result. The cause also names any settings ignored from the pull request's `keepsnappy.yml`.
- Fix caps and reasons to measure a fix by hand go to the workflow job summary and the log.

Text the AI wrote can't add links, mentions or HTML: it is posted as plain text, with only its inline code kept. Its review comment names each place in words, gives each AI answer once, and links the result's public share page, when there is one, before the dashboard, which needs a sign-in. Real code appears only in comments on the pull request's own repository. The copies KeepSnappy stores are redacted.

`act` changes only its own part of the general comment and reads it back after each write. GitHub replaces the whole comment on each write, so a later writer can still replace text another job already confirmed. Run `act` and the phone and final commands of `pr-comment` in one GitHub Actions concurrency group per pull request, such as `keepsnappy-pr-<number>`, and let each job finish. The run on a fix branch writes the source pull request's comment, so its job uses the source pull request's group, `keepsnappy-pr-<source>`. `act` names the fix branch `keepsnappy/fix-<finding>-pr<source>`, and the run it starts is a `workflow_dispatch` on that branch. A `concurrency` expression can't cut the number out of `github.ref_name`, so read it in an earlier job (the digits after the last `-pr`), pass it as a job output, and build the writer job's `concurrency.group` from `keepsnappy-pr-` and that output (`needs.<job>.outputs`). A fix branch without the `-pr<source>` ending, such as a fix of `main`, names no source pull request.

After confirming its cause, `act` removes its old review summary and any shared-summary pointers it owns. If it has neither a general verdict comment nor readable verdict data, it logs why and posts no general comment. With no investigation and no general verdict comment on the pull request, `act` writes its review in a comment of its own and edits that comment on each run. It skips the write when the pull request has a newer head than the measured commit, and it deletes that comment once a general verdict comment appears. Two `act` runs of one pull request at the same time can each post one, so give `act` the per-pull-request concurrency group described below.

A fork's pull request has a read-only token, so `act` prints the summary to the log instead. So does a run on `main`, or with `--no-github`.

## Exit codes

| Code | Meaning |
|---|---|
| `0` | Finished, also when the AI, the provider or a post failed (a warning says so). |
| `1` | Something you must fix, such as no AI key, `claude` missing or too old, or investigation on without a read key. |
| `2` | The command line or the policy is wrong, or there is no session. |
| `130` | Interrupted. |

All KeepSnappy docs for agents: https://keepsnappy.com/llms.txt
