What the agent does
The agent reads the procedure and every caller before changing the shape, because a tRPC change is a type change on both ends. In a monorepo like t3-oss/create-t3-turbo the list lives in packages/api/src/router/post.ts: all is a publicProcedure.query that returns ctx.db.query.Post.findMany({ orderBy: desc(Post.id), limit: 10 }). The Post table in packages/db/src/schema.ts has a uuid id with defaultRandom(), a createdAt timestamp with defaultNow(), and a nullable updatedAt. Ordering by a random UUID is not a chronological order, so a cursor on id alone would page in an arbitrary sequence; the stable key is (createdAt, id). Three apps call the procedure: apps/nextjs/src/app/_components/posts.tsx with useSuspenseQuery(trpc.post.all.queryOptions()) after apps/nextjs/src/app/page.tsx prefetches it, apps/tanstack-start/src/routes/index.tsx the same way, and apps/expo/src/app/index.tsx with useQuery and an invalidateQueries(trpc.post.all.queryFilter()) after a delete.
The procedure gains an input schema in zod/v4, the version the router already imports: limit as an integer between 1 and 50 defaulting to 10, and an optional cursor object of createdAt and id. The query orders by desc(Post.createdAt), desc(Post.id) and, when a cursor is present, adds a where of or(lt(Post.createdAt, cursor.createdAt), and(eq(Post.createdAt, cursor.createdAt), lt(Post.id, cursor.id))), all from the drizzle operators packages/db/src/index.ts re-exports. It fetches limit + 1 rows and returns { items, nextCursor }, with nextCursor taken from the extra row when there is one. Because the project uses drizzle-kit push rather than migration files (pnpm db:push in the root package.json), the matching composite index goes into schema.ts as an index on (createdAt, id) and the pull request names the push command to run.
Then the three callers move to useInfiniteQuery with trpc.post.all.infiniteQueryOptions({ limit: 10 }, { getNextPageParam: (last) => last.nextCursor }), the shape the TanStack React Query integration the apps already use provides, and the list rendering flattens data.pages. CreatePostForm in the Next.js app invalidates trpc.post.pathFilter() after a create, which already covers the infinite query; the Expo delete invalidates trpc.post.all.queryFilter(), which still matches. A load-more control goes where each list ends, kept to the components each app already has from @acme/ui.
The proof is the repository's own checks: pnpm typecheck across the turbo workspace, which fails on any caller still reading the old array shape, pnpm lint and pnpm format, the three jobs .github/workflows/ci.yml runs on every pull request. The agent also writes a short test of the cursor predicate against a local Postgres in the sandbox, inserting posts with equal createdAt values to show the id tiebreak pages without skipping or repeating.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add cursor pagination to the post.all procedure in packages/api/src/router/post.ts. Use a keyset cursor on (createdAt, id) because id is a random UUID, with a zod input of limit (1 to 50, default 10) and an optional cursor, order by createdAt then id descending, fetch limit + 1 and return { items, nextCursor }. Add a composite index on (createdAt, id) to packages/db/src/schema.ts and say in the pull request that drizzle-kit push applies it. Move every caller in apps/nextjs, apps/tanstack-start and apps/expo to useInfiniteQuery through infiniteQueryOptions, flatten the pages, and add a load-more control using the existing UI package. Check that the create and delete invalidations still match. Run pnpm typecheck, pnpm lint and pnpm format, and test the tiebreak against a local Postgres with posts that share a createdAt. Open a pull request quoting each result.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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 packages/api/src/router/post.ts, adds the index to packages/db/src/schema.ts, and updates the list component in each of the three apps, with a load-more control built from the existing @acme/ui button. Its description shows the new input and output types, the pnpm typecheck, pnpm lint and pnpm format results, and the tiebreak test output: a page of posts with identical timestamps fetched twice with the cursor, no row repeated and none skipped.
A reviewer reads the where clause once for the keyset logic, confirms the order and the cursor fields agree, and checks each app's list for the flattened pages and the invalidation after create and delete. Because the input has defaults, an existing call with no arguments still type-checks, which the typecheck step proves. The transcript at GET /o/{org}/sessions/{id}/messages has every command and the local database session.
The index is not applied anywhere by the run. pnpm db:push against your database is the step after the merge, and the pull request says so.
Run it on every new list procedure
The second list procedure in a router tends to copy the first, so the time to add a cursor is when it is written. Send POST /o/{org}/sessions with a prompt naming the procedure and the table's stable ordering key, and the agent applies the same shape: zod input, keyset where, limit + 1, nextCursor, callers on infinite queries. A request_id that is a UUID v5 of the procedure name makes a retried send return the same run.
A webhook URL on the API key delivers run.completed with the session URL and the pull request. Several routers across repositories are separate runs, each proving its own typecheck.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
The server half of this change is a dozen lines; the cost is in the callers and the proof. Three apps in three frameworks read the same procedure, and the only way to know they all still compile is to run the workspace typecheck, which means installing a turbo monorepo with pnpm and building the packages in order. The sandbox does that once, runs the three checks CI runs, and keeps the output.
A cursor change that is wrong in a quiet way, a tiebreak that skips a row when timestamps collide, does not show up in a type check. Running the predicate against a real Postgres with colliding timestamps, inside the sandbox, turns that into a quoted result in the pull request rather than a bug report later.
Frequently asked questions
Why not paginate on id alone?
Because Post.id is a uuid with defaultRandom(), so ordering by it is arbitrary and a new post can land anywhere in the sequence. createdAt gives the order people expect and id breaks ties between posts created in the same instant, which is why the cursor carries both.
Will existing calls to post.all break?
The input has defaults, so a call with no arguments is still valid and returns the first page. The return type changes from an array to { items, nextCursor }, and that is exactly what pnpm typecheck catches in every caller the pull request updates.
Does it run drizzle-kit push against my database?
No. The index is added to schema.ts, the pull request names pnpm db:push as the step to apply it, and the sandbox uses its own local Postgres only for the tiebreak test.
Why fetch limit plus one?
The extra row tells the server whether a next page exists without a separate count query. It becomes nextCursor and is not returned in items, so the client gets exactly limit rows and a cursor only when there is more.
More tRPC jobs
The same job on other stacks
- Add Cursor Pagination to a TypeORM + NestJS API (TypeORM)
- Add Cursor Pagination to an Actix Web API (Actix Web)
- Add Cursor Pagination to an aiohttp App (aiohttp)