> ## Documentation Index
> Fetch the complete documentation index at: https://docs.testsprite.com/llms.txt
> Use this file to discover all available pages before exploring further.

# The Agent Loop

> How your coding agent creates, runs, reads, fixes, and reruns tests, and how coverage compounds with every pass.

Verification should be a loop your coding agent runs continuously, not a gate at the end. Each time the agent changes code, it verifies the behavior it touched and banks the result.

> [card]
>
> ```mermaid theme={null}
> flowchart LR
>     A["Your coding agent<br/>writes / changes code"] --> B{"Already covered<br/>by the suite?"}
>     B -->|yes| C["Rerun tests<br/>test rerun"]
>     B -->|"no · new"| D["Create a test<br/>test create"]
>     C --> E["TestSprite cloud<br/>real browsers & APIs"]
>     D --> E
>     E -->|fail| F["Read failure bundle<br/>test failure get"]
>     E -->|pass| G["Durable suite<br/>grows every pass"]
>     F -->|"read & fix"| A
>     G -.->|coverage| B
> ```

**1. Not covered → create**

Describe the behavior as a plan file (frontend) or code (backend), then create and run it:

```bash theme={null}
testsprite test create \
  --project proj_8f0f6 --type frontend \
  --plan-from ./checkout-flow.plan.json \
  --run --wait --output json
```

`--run --wait` chains create, trigger, and poll. Exit 0 means the test passed and is banked.

**2. Already covered → rerun**

```bash theme={null}
testsprite test rerun --all --project proj_8f0f6 --wait
```

A frontend rerun has [auto-heal](https://docs.testsprite.com/cli/core/rerun-and-auto-heal.md) on by default: if the app changed, the test is repaired and the repaired version saved. For a strict regression gate that replays the saved script exactly, add `--no-auto-heal`. A rerun is billed the same as a fresh run, so rerun the relevant suite after every significant change.

**3. Failed → read the bundle, fix, rerun**

On exit 1, pull the [failure bundle](https://docs.testsprite.com/cli/concepts/key-terms.md#failure-bundle), fix the code, and rerun:

```bash theme={null}
mkdir -p .testsprite
testsprite test failure get test_3a9f21c7 --out ./.testsprite/failure
testsprite test rerun test_3a9f21c7 --wait
```

A test that has never passed, or has only run with `--local`, has no saved code, so there is no bundle (exit 4, reason `no_code`). Use `testsprite test steps <test-id> --run-id <run-id>` and `testsprite test failure summary <test-id>` instead.

## Why this design works

> [accordion] One self-consistent bundle
>
> Every artifact from `test failure get` and `test artifact get` shares one `snapshotId`, and the CLI refuses to stitch data across runs or code versions. The agent never "fixes" the wrong thing from mixed context.

> [accordion] Coverage compounds
>
> Every passing test joins a durable suite, so "already covered?" is answered by replayable tests rather than the agent's memory, and regressions are caught on the next run.

> [accordion] Real browsers and APIs
>
> Tests run against your live product, with nothing to install on your side. A run uses the project's Default environment unless you pass `--env <name>`. To test an app on your machine, see [Environments](https://docs.testsprite.com/cli/core/local-apps.md).

> [accordion] Machine-readable contract
>
> `--output json` and stable exit codes let the agent branch without defensive parsing. See [Output & Scripting](https://docs.testsprite.com/cli/reference/output-and-scripting.md#the-json-contract).

> [accordion] Safe retries
>
> `project create`, `test create`, `test run`, and `test rerun` carry an idempotency key (random per invocation), so retrying never duplicates a project, test, or run. Pin your own with `--idempotency-key my-agent-step-42`. Replacing backend code is guarded by a `codeVersion` token; see [Editing & Deleting Tests](https://docs.testsprite.com/cli/core/editing-tests.md#editing-a-test).

> [accordion] Run-scoped vs latest
>
> `test failure get` follows the *latest* failing run, which can shift if a Portal or scheduled run lands mid-loop; `test artifact get <run-id>` is pinned to one run. In agent loops and CI, capture the `runId` from `--output json` and use `test artifact get`. See [Reading Results](https://docs.testsprite.com/cli/core/reading-results.md#pinning-to-a-specific-run).

## Where the CLI fits

The CLI is one of three surfaces over the same backend; see [One Platform, Three Surfaces](https://docs.testsprite.com/cli/getting-started/overview.md#one-platform-three-surfaces). It covers the test lifecycle, schedules, and test lists (subject to your plan). Billing, site discovery, and per-step regeneration stay in the Web Portal.

## Where to Go Next

> [card] [Key Terms](https://docs.testsprite.com/cli/concepts/key-terms.md)
>
> Core concepts defined

> [card] [Quickstart](https://docs.testsprite.com/cli/getting-started/quickstart.md)
>
> Your first test end to end

> [card] [Running Tests](https://docs.testsprite.com/cli/core/running-tests.md)
>
> Triggering runs and handling exit codes

> [card] [Agent Integration](https://docs.testsprite.com/cli/core/agent-integration.md)
>
> Let your coding agent drive the loop


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.