# Add Cursor Pagination to a Rocket App

Yes. An agent turns your Rocket app's load-everything index into a keyset-paginated page with a limit, an id cursor and an older-tasks link, tested, in a PR.

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

## What the agent does

The agent starts from the route that loads everything. In an app laid out like the `todo` example in rwf2/Rocket, `src/main.rs` mounts `index` at `/` and `new`, `toggle` and `delete` under `/todo`; `index` builds a `Context` through `Context::raw`, which calls `Task::all` and renders `index.html.tera` with `flash` and `tasks`. `src/task.rs` defines the Diesel table `tasks` with `id -> Nullable<Integer>`, `description -> Text` and `completed -> Bool`, and `Task::all` is `tasks::table.order(tasks::id.desc()).load::<Task>(c)` inside `conn.run`, so every request reads the whole table newest first. The connection is a `rocket_sync_db_pools` SQLite pool declared with `#[database("sqlite_database")]` and configured in `Rocket.toml` as `db/db.sqlite`; the migration in `migrations/` creates the table with `id INTEGER PRIMARY KEY AUTOINCREMENT` and two demo rows, and runs on ignite through an `AdHoc` fairing with `diesel_migrations`. The template loops `{% for task in tasks %}` and posts each toggle and delete as a form with a `_method` field.

Because the list is already ordered by `id` descending, the cursor is the id of the last task shown and a page is everything older than it. The agent changes `index` to `#[get("/?<after>&<limit>")]` with `after: Option<i32>` and `limit: Option<u8>`, adds `Task::page(after, limit, conn)` in `task.rs` that filters `tasks::id.lt(after)` when a cursor is given, keeps `order(tasks::id.desc())` and loads `limit + 1` rows with a default of 20 and a cap of 100, and gives `Context` a `next: Option<i32>` set to the last shown id when the extra row came back. `Context::raw` and `Context::err` take the page arguments too, so the error paths in `toggle` and `delete` keep rendering a page rather than the whole table. The template gains one block under the list: an `older tasks` link to `/?after={{ next }}&limit={{ limit }}` when `next` is set, and nothing otherwise, so the first page with twenty or fewer tasks looks exactly as it does today. `Task::all` stays for the tests that use it.

The agent checks how Rocket treats a bad query value before writing the tests, since `Option<T>` in a query string has its own rule, and pins the behaviour it reads in Rocket's form guard documentation with a test rather than assuming: `/?after=abc` either answers with the first page or is refused, and the test says which. `limit=500` is clamped to 100 and `limit=0` to 1, so a client cannot ask for the whole table through the new parameter. The redirects after `new`, `toggle` and `delete` still go to `/`, which is the first page, and the pull request notes that keeping the viewer on page three after a toggle would mean carrying `after` through the form's action, left as a follow-up because it changes three handlers and the template for a cosmetic gain.

Tests go in `src/tests.rs` next to the existing ones, using the same `run_test!` macro, the `DB_LOCK` mutex and `Task::delete_all` so the table is empty when each starts: insert 45 tasks through `POST /todo` as `test_many_insertions` does, then walk `/`, `/?after=<id>`, and so on with `Client::tracked`, asserting 20, 20 and 5 descriptions in descending id order with no repeats or gaps and no `older tasks` link on the last page; a test for the clamp; and one that the first page with two tasks renders no link. The agent runs `cargo test` for the example in its sandbox after installing `libsqlite3-dev` as the example's README requires, runs `cargo fmt --check` and `cargo clippy`, and pastes the test summary and the rendered HTML of a middle page into the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add cursor pagination to the index route of this Rocket app. Read Cargo.toml, Rocket.toml, src/main.rs, src/task.rs, src/tests.rs, migrations/*/up.sql and static/index.html.tera first. Change index to take optional after and limit query parameters; add Task::page in task.rs that keeps the existing order by id descending, filters id less than the cursor when one is given, and loads limit plus one rows with a default of 20 and a cap of 100; put a next id on Context when there is another page, and keep Context::raw and Context::err working for the error paths in toggle and delete. In the template, add an older tasks link to the next page only when there is one, so a first page with few tasks renders as before. Read Rocket's rule for an Option query parameter that fails to parse and pin it with a test rather than guessing. Add tests beside the existing ones using run_test!, DB_LOCK and Task::delete_all: 45 inserted tasks walk as pages of 20, 20 and 5 in descending id order with no repeats and no link on the last page; limit is clamped; a two-task first page has no link. Install the SQLite headers the README names, run cargo test, cargo fmt --check and cargo clippy, and open a pull request with the test output and the HTML of a middle page. Leave Task::all and the redirect targets as they are.",
  "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

The pull request changes `src/main.rs` (the `index` signature and `Context`), adds `Task::page` to `src/task.rs`, adds one block to `static/index.html.tera` and three tests to `src/tests.rs`. Its description quotes the `cargo test` summary with the existing five tests and the new three passing, the rendered HTML of the second page of 45 tasks showing twenty items and the link with the right `after`, and the line of Rocket documentation that decided the bad-cursor case.

Nothing changes for a reader of the first page with twenty or fewer tasks: the same template, the same order, no link. The add, toggle and delete forms and their redirects are untouched, and `Task::all` remains for the tests that call it. The table and the migration are unchanged, because `id` is already the primary key the cursor needs.

Before merging, a reviewer decides whether 20 is the right page size for their list, whether a toggle or delete from a later page should return to that page, which the pull request scopes as a follow-up, and whether the bad-cursor behaviour the test pinned, first page or refusal, is the one they want.

## Run it on every list route without a limit

An engineer who found the unbounded `load` while a list page grew slow starts this from the terminal with this body and goes back to their own work. The run reads the real handler, model and template, so the cursor matches the key the table has and the link matches the template the page already renders, rather than a pattern written for another framework.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. The pull request link is on the run's `GET /o/{org}/sessions/{id}` answer once it is open, and a key with a `webhook_url` hears `run.completed` when the turn ends. A service with several Rocket apps sends one body per repository, each with its own `resources` URL.

## Why run it on Rush?

Keyset pagination in Rocket touches the route signature, the Diesel query, the context struct and the template at once, and each has a way to go subtly wrong: a cursor on a non-unique column, an off-by-one on the extra row, a link that drops the limit, a bad query value that behaves differently from what the handler assumes. The agent proves each against the real app in its own sandbox, with the SQLite pool and the embedded migrations the example already uses, and the tests walk the pages the way a reader would.

The transcript keeps every command and its output, so the reviewer reads the `cargo test` run and the rendered pages without checking out the branch. Several services can be paginated at once, each in its own sandbox with its own database file, and nothing merges until a person reads the diff.

## Frequently asked questions

### Why is the cursor the last id and not a page number?

The list is ordered by `id` descending and `id` is the autoincrement primary key, so `id < after` is a single indexed range scan that stays correct when tasks are added or deleted between requests. A page number would shift as rows change and would make SQLite skip over earlier rows on every page.

### What happens to the existing tests?

They keep passing. `Task::all` stays, `run_test!` and `DB_LOCK` are reused, and the new tests clear the table first the way the existing ones do, so the suite still runs serially against the one SQLite file. The three new tests insert through `POST /todo` as `test_many_insertions` does.

### What does a bad after value do?

Rocket has a specific rule for an `Option` query parameter whose value does not parse, and the agent reads it rather than guessing, then writes a test that pins whichever it is, first page or refusal, and states it in the pull request. A reviewer who wants the other behaviour changes one type in the route signature.

### Does toggling a task on page three send me back to page one?

Yes, today and after this change, because `toggle` and `delete` redirect to `/`. Carrying `after` through the form's action so the redirect returns to the same page is a small follow-up the pull request describes, kept out of this change so the diff stays about the list query.

### Will the first page look different?

No. With twenty or fewer tasks, `next` is not set, the template renders no link, and the order and markup are the ones the example ships. The only visible change is on a list longer than the limit, where the page ends with an older tasks link.

## More Rocket jobs

- [Add a Dockerfile to a Rocket App](https://getrush.ai/run/add-dockerfile/rocket/)
- [Add GitHub Actions CI to a Rocket App](https://getrush.ai/run/add-github-actions-ci/rocket/)
- [Add Health Check Endpoints to a Rocket App](https://getrush.ai/run/add-health-checks/rocket/)

## The same job on other stacks

- [Add Cursor Pagination to a Sequelize + Express API](https://getrush.ai/run/add-cursor-pagination/sequelize/)
- [Add Cursor Pagination to a Serverless DynamoDB API](https://getrush.ai/run/add-cursor-pagination/serverless/)
- [Add Cursor Pagination to a Slim 4 API](https://getrush.ai/run/add-cursor-pagination/slim/)

## 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/)
