# Add Code Coverage to CI for an Axum API

Yes. An agent adds cargo-llvm-cov to your existing Axum workflow, keeps the Postgres service the SQLx macros need, uploads an lcov report and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-coverage-to-ci/axum/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the workflow that exists before it adds a step, because coverage has to run under the same conditions as the tests. In an Axum service shaped like launchbadge/realworld-axum-sqlx, `.github/workflows/rust.yml` runs on push and pull request to `main` with a `postgres` service container, `DATABASE_URL` and `HMAC_KEY` set as job env, `actions/checkout@v2`, a `cargo install sqlx-cli --features postgres` followed by `sqlx db setup`, then `cargo build --verbose` and `cargo test --verbose`. After that it checks out a fork of the Realworld Postman collection at a pinned commit and runs `./run-api-tests.sh` against `cargo run` on port 8080. `Cargo.toml` pins `axum 0.3.4`, `sqlx 0.5` with `runtime-tokio-native-tls` and `postgres`, `tokio 1.14`, `tower-http 0.2`, `argon2 0.3.1` and `jwt 0.15`, edition 2021, and there is no `rust-toolchain` file.

Two facts shape the change. First, the handlers in `src/http/` use `sqlx::query!` macros and the repository has no offline query data, so the crate only compiles with `DATABASE_URL` pointing at a migrated database; the coverage build has to sit after `sqlx db setup` in the same job, not in a new job without the service. Second, the Rust test suite is one function, `test_slugify` in `src/http/articles/mod.rs`, and the comment above it says the author relies on the Realworld API suite for everything else. Coverage from `cargo test` alone will therefore be small, and the pull request says so instead of hiding it.

The agent installs `cargo-llvm-cov` with `taiki-e/install-action@cargo-llvm-cov`, the prebuilt route the tool documents, and adds `rustup component add llvm-tools-preview` so the step does not stop to ask. It replaces `cargo test --verbose` with `cargo llvm-cov --workspace --lcov --output-path lcov.info`, which runs the same tests under instrumentation, and adds `--html` output under `target/llvm-cov/html` as an uploaded artifact with `actions/upload-artifact`. The lcov file goes to Codecov through `codecov/codecov-action@v5` with `files: lcov.info` and a `CODECOV_TOKEN` secret the reader adds; the step is written so a missing token on a fork does not fail the job. `--ignore-filename-regex` keeps `src/main.rs` out of the percentage, since nothing exercises the binary's bootstrap in a unit test. No `--fail-under-lines` threshold is set on the first run, because the number is not known until the report exists.

The agent then explains how the integration suite could count too. The Postman run drives a server started with `cargo run`, and `cargo-llvm-cov` has a `run` subcommand and a `--no-report` mode that merges several runs into one report, but the profile is only written when the process exits normally, and `src/main.rs` installs no shutdown signal handler for `axum::Server`. So the pull request keeps the Postman step as it is, measures the unit tests, and proposes a graceful-shutdown change as the next step rather than folding it in. In the sandbox the agent starts Postgres, runs `sqlx db setup`, runs the new `cargo llvm-cov` command and pastes the summary table it prints.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add code coverage to the GitHub Actions workflow of this Axum and SQLx repository and open a pull request. Read .github/workflows/rust.yml, Cargo.toml, .env.sample, src/main.rs, src/http/mod.rs and every #[test] in src/ first. The handlers use sqlx::query! macros with no offline data, so the coverage step must run in the existing job after sqlx db setup with the Postgres service and DATABASE_URL in place. Install cargo-llvm-cov with taiki-e/install-action and the llvm-tools-preview component, replace the cargo test step with cargo llvm-cov --workspace producing lcov.info and an HTML report, upload the HTML report as a workflow artifact, and upload lcov.info with codecov/codecov-action@v5 using a CODECOV_TOKEN secret in a way that does not fail on forks. Exclude src/main.rs from the percentage and do not set a minimum threshold yet. Leave the Postman integration step unchanged and explain in the pull request why its run is not instrumented and what a graceful shutdown would make possible. Start Postgres in the sandbox, run sqlx db setup and the new coverage command, and paste the summary it prints in the pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that touches `.github/workflows/rust.yml` and nothing under `src/`: an install step for `cargo-llvm-cov`, a component step, the coverage command in place of `cargo test`, an artifact upload for the HTML report and a Codecov upload for `lcov.info`. The description reproduces the per-file table `cargo llvm-cov` printed in the sandbox, names the files in `src/http/` that have no coverage at all, and explains why the Postman run stays outside the measurement.

The workflow runs on the pull request itself, so the first real coverage report is produced by the change that adds it. A reviewer checks that the step still sits after `sqlx db setup` so the `query!` macros compile, that the Codecov step is tolerant of a missing token, that the ignore pattern excludes only `src/main.rs`, and that `cargo install sqlx-cli`, which the workflow runs on every push, was left alone rather than quietly cached.

The report is the starting point for the next job: it shows that `src/http/users.rs`, `src/http/profiles.rs` and `src/http/articles/` are exercised only by the external Postman suite, which is the case for writing Rust integration tests against the database.

## Run it on every new Rust service repository

A team that wants a coverage number on every service sends one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads its own workflow, so a crate with `sqlx` offline data gets the step placed differently from one that compiles against a live database, and a crate with a `rust-toolchain` file gets the component installed for that toolchain. Each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with `session_url` to read the pull request and its pasted summary from. A key keeps `max_concurrent` runs in progress, four by default and up to 100, yet a batch across many crates can go out in one loop: each repository past the limit answers `202` with `status: "queued"` and starts, oldest first, when a slot frees.

## Why run it on Rush?

Coverage on a crate that needs a database is not a one-line change: the instrumented build has to compile the `query!` macros, which means the service, the migrations and the env have to be in place before `cargo llvm-cov` starts. In a Rush sandbox the agent brings up Postgres, runs `sqlx db setup` and the coverage command for real, and the summary table it pastes comes from that run, with every command and its output kept in the record.

The honest part is as valuable as the YAML. A single `test_slugify` gives a low number, and a tool that pads it would be misleading; the agent reports what the suite covers, keeps the Postman run out of the measurement for a stated reason, and leaves a reviewer with a report and a next step rather than a green badge.

## Frequently asked questions

### Why not a separate coverage job?

Because the crate does not compile without `DATABASE_URL` pointing at a migrated database: the `sqlx::query!` macros check every query at build time and there is no offline query data in the repository. Reusing the job that already has the `postgres` service and runs `sqlx db setup` avoids duplicating that setup.

### Does it slow the workflow down?

The tests are built once more under instrumentation, and `taiki-e/install-action` downloads a prebuilt binary rather than compiling `cargo-llvm-cov`. The step that dominates this workflow today is `cargo install sqlx-cli` on every run, which the agent leaves in place and mentions in the pull request.

### Why is the Postman suite not counted?

The server it drives is started with `cargo run`, and the coverage profile is only written when the process exits normally, which `src/main.rs` has no signal handler for. The pull request proposes adding graceful shutdown and then switching that step to `cargo llvm-cov --no-report run` with a final `cargo llvm-cov report`.

### Can I set a minimum later?

Yes. Once the first report exists, `--fail-under-lines` with the number you choose turns the step into a gate, and the pull request names that flag. The agent does not pick the number for you.

## More Axum jobs

- [Add Cursor Pagination to an Axum API](https://getrush.ai/run/add-cursor-pagination/axum/)
- [Add Full-Text Search to an Axum API](https://getrush.ai/run/add-full-text-search/axum/)
- [Add Graceful Shutdown to an Axum API](https://getrush.ai/run/add-graceful-shutdown/axum/)

## The same job on other stacks

- [Add Code Coverage to CI for a Bazel Repository](https://getrush.ai/run/add-coverage-to-ci/bazel/)
- [Add Code Coverage to CI for a Chrome Extension](https://getrush.ai/run/add-coverage-to-ci/chrome-extension/)
- [Add Test Coverage Reporting to a CMake Project's CI](https://getrush.ai/run/add-coverage-to-ci/cmake/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
