#### Coding agents

# Reviewing and testing code

[Video: Reviewing and testing code](https://stream.mux.com/H9pe4ol02Gwai6JMWA849qbPQuguzO902ak003HPpJWesI.m3u8)

Coding agents produce a lot of code, and with it, tech debt. Keep your quality bar the same whether code was written by hand or by an agent.

AI-generated code can look correct and still be subtly wrong. It might compile and pass your tests, yet miss edge cases, have security issues, or duplicate logic that exists elsewhere. Code review catches these before they reach production.

## Self-review

Review your code before you ask others to look at it.

**Watch the agent work.** The diff view shows changes as they happen. If you see the agent heading in the wrong direction, click **Stop** or press `Cmd Shift Backspace` / `Ctrl Shift Backspace` to cancel and redirect. You don't have to wait until it finishes. For larger course corrections, revert the changes and refine your plan before running it again, as covered in [developing features](/build/agents-window/learn/creating-features#when-to-start-over).

**Ask the agent to review all changes at once.** Tag [`@Branch`](/build/agents-window/agent/prompting) in your prompt to give the agent the full diff of your current branch. Say something like "review the changes on this branch" or "what am I working on right now?" to give the agent rich context and catch issues across many different files.

For example, you can ask the agent to review its own work:

**Agent example: Self-review**

> Review the changes I've made to the discount code feature. Look for bugs, missing error handling, and anything that doesn't match our patterns in @src/services/PricingService.ts

**Agent example: Anticipate reviewer questions**

> What questions will reviewers have about these changes? What context should I include in the PR description?

## Prepare for peer review

Agents can produce one large commit with hundreds of changed lines, which is hard for anyone to review. Use small, semantic commits instead, each one logical change with a clear description, so a reviewer can step through the history.

This housekeeping is tedious by hand, but agents handle it well:

1. Build the feature freely. Don't worry about commit hygiene while you're iterating.
2. Once everything works, ask the agent to rework the commit history into reviewable pieces.
3. The agent resets to `main`, reads through all the changes, and plans a logical sequence to create clean commits with descriptive messages.
4. It validates the final diff matches the original, so none of your changes are lost.

Use this prompt to create a [skill](/build/agents-window/skills) so anyone on your team can run `/rework-commits` after finishing a feature:

## Agent review

After the agent finishes a task, click **Review** then **Find Issues** to run a dedicated code review. The agent analyzes proposed edits line by line and flags potential problems.

For all local changes, open the Source Control tab and run [Agent Review](/build/agents-window/agent/agent-review) to compare against your main branch. This catches issues across your full set of changes.

[Video: An agent reviewing a diff and leaving comments on the changed lines](https://media.x.ai/v1/docs/agents-window-blob-changelog-changelog-2-1-1-419d3d6a.mp4)

## Bugbot for pull requests

[Bugbot](/build/agents-window/bugbot) reviews pull requests in your source control provider when you push. It reads how the change connects to the rest of your codebase and looks for logic errors that linters miss: null pointer exceptions, race conditions, missing error handling, and security issues.

When Bugbot finds an issue, it can also propose a fix. With [autofix](/build/agents-window/bugbot#autofix) enabled, you can commit the fix directly from a comment on the pull request.

You can also customize Bugbot by providing [additional rules](/build/agents-window/bugbot), which the [customizing agents](/build/agents-window/learn/customizing-agents) chapter covers.

## Verifiable goals

To help ensure the correctness of your code, you want to give the agent clear signals to validate its own work:

* **Tests** catch behavioral regressions
* **Type checking** catches structural errors
* **Linting** catches style and pattern violations

The more of these checks you have in place, the more confidently you can delegate work to agents. Use typed languages with test coverage and linting rules alongside your agent.

**Check your understanding: Which combination provides the strongest safety net for AI-generated code?**

## Let agents write your tests

Thorough test coverage used to take significant effort, so many teams added tests only after something broke. Agents make it approachable: ask the agent to write tests, then verify they're correct. The agent can also do manual testing for you through the [browser](/build/agents-window/agent/tools/browser), checking UI states or flows you'd otherwise verify by hand.

**Agent example: Agent-written tests**

> Write integration tests for the discount code API endpoint and e2e tests for the checkout discount flow. Look at our existing test patterns and match them.

High-quality tests give you the confidence to let the agent work on its own without introducing regressions.

Good prompts for generating tests:

* "Plan how to get e2e coverage for our checkout flow. What scenarios should we test?"
* "Set up integration tests for the payments API. Use our existing test infrastructure in `src/__tests__/`."
* "What edge cases aren't covered by our current tests for the discount feature?"
* "Write regression tests for the bug we fixed in `PaymentService.ts`."

The agent can also set up testing infrastructure from scratch, such as configuring Playwright and writing your first test.

## Cloud Agents

[Cloud agents](/build/agents-window/cloud-agent) run in remote sandboxes, so you can close your laptop and check results later:

1. Describe the task and provide relevant context
2. The agent clones your repo and creates a branch
3. It works on its own, opening a pull request when finished
4. You get notified when it's done (via [Slack](/build/agents-window/integrations/slack), email, or the [web interface](https://cursor.com/agents))
5. Review the changes and merge when ready

They suit to-do list tasks: side bug fixes, test coverage for existing code, documentation updates, and refactors. They can also test many variations in parallel. For a new discount code feature, launch several to test every discount type, invalid inputs, stacked discounts, and boundary values. Each creates a branch with its results, and you turn the failures into local test cases before merging.

## Speed up your feedback loops

With more agents, the bottleneck moves to the slowest part of your system: the test suite, type and lint checks, or CI. Every agent pays these costs. If tests take 10 minutes and you run 10 agents, that's nearly two hours of waiting; make them 50% faster and you save almost an hour each time.

Faster tests, a smaller dependency tree, a quicker CI pipeline, type checking, and builds all compound when agents run them dozens of times a day. Agents can do this work too: ask one to profile your tests and fix the slowest, or audit your dependencies and remove what's unused.

## Common failure pattern

Passing tests don't guarantee the code works correctly. It's possible the tests are checking the wrong behavior. The agent might have written code that works for the happy path but doesn't consider all of the edge cases.

Understand the code changes yourself. If a change is too large to review comfortably, break it into smaller pieces.

## What's next

You now understand how to ship new features, debug when things go wrong, and review code to ensure it's high quality. The last piece is making your workflow faster by customizing agents to work with your specific codebase.
