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

# Self-healing tests

> Learn how self-healing tests work in e2e, where a replayed agent step recovers when the UI changes and the recording repairs itself for the next run.

A self-healing test keeps passing even when the UI changes under a recorded
`agent.act()` step. e2e replays the recording as far as it still matches the
screen, then hands off to the live agent to finish the goal. In `read-write`
mode, the updated flow is saved automatically once a later assertion passes.

The run summary counts each step that healed this way as `handed off`:

```text
        Cache  4 replayed · 1 handed off · 1 missed
```

## What happens when the UI changes

Take a step recorded with two actions, and a button that was renamed
since:

```ts title="tests/checkout.e2e.ts"

test('applies a discount code', async ({ app, agent, screen }) => {
  await app.open('/checkout');
  await agent.act('apply the discount code SUMMER');
  await expect(screen.getByRole('status')).toHaveText('10% off');
});
```

1. Replay types `SUMMER` into the `Discount code` field without a model call.
2. Replay looks for the recorded button `Apply`. The button is now
   `Redeem`, so no control matches within 15 seconds. Replay stops with
   `target-not-found`.
3. The agent gets the step with a record of what replay did. It reads the
   screen, sees `Redeem`, and taps it.
4. The `expect` passes. At the end of the attempt, the runner saves the new
   recording, with `Redeem` in place of `Apply`.
5. The next run replays the whole step without a model call.

Replay never guesses. A recorded control that no longer matches is a
hand-off, never a tap on the closest control. See
[How a replay works](https://e2e.com/docs/cache#how-a-replay-works) for how controls are
matched.

## What the agent is told

The agent's first prompt lists the actions replay already performed and
why replay stopped. For the step above, it reads:

```text
Cached replay already performed these recorded actions for this step:
1. type "SUMMER" into textbox "Discount code"
Replay stopped (target-not-found) after 1 of 2 recorded actions.
Continue the step from the CURRENT screen state shown below — do NOT redo the actions above.
```

The prompt tells the agent not to repeat those actions, such as a submit
that already went through. Two stop reasons add a warning:

- `end-mismatch`: every action ran, but replay can't show it produced the
  recorded result. The result didn't show, or it was already on screen
  before replay acted. The agent is told to check whether the step took
  effect before it acts.
- `action-uncertain`: an action failed, and it may have reached the app.
  The agent is told to check the current state before it tries anything
  like it again.

The replayed actions and the wait count toward the step's
[budgets](https://e2e.com/docs/reference/agent#budgets) and deadline. The
[cache page](https://e2e.com/docs/cache#why-a-step-was-not-replayed) lists every stop reason.
When replay stops before its first action, the step is a miss, not a
hand-off: the agent runs it from the beginning with no notice.

## How the recording repairs itself

A healed step saves a fresh recording in `read-write` mode once a later
verification check (like a locator or engine assertion, or `agent.assert()`)
passes. Otherwise the stale entry is removed, for instance if the step
failed, wasn't confirmed by a later check, changed nothing on screen, or
needed a repair after an `end-mismatch`.

If the run was cancelled or the model couldn't be reached, the existing
entry stays intact. See
[When a recording is written](https://e2e.com/docs/cache#when-a-recording-is-written) for the
full rules.

## Catch regressions, not only UI changes

The agent heals toward the goal in the instruction, not toward the recorded
clicks. If the discount button moved, the agent finds it. If discounts stop
working, the step or the check after it fails.

A goal can still pass in a way you didn't intend, for example with a
different discount. End the test with a deterministic check of the
result, such as the `toHaveText('10% off')` above. That check runs on every
run, replayed or healed.

## See which steps healed

Each healed step reports `step.cache.mode: 'agent-concluded'` with the stop
reason, and `step.cache.detail` says where and why replay stopped. The
[trace page](https://e2e.com/docs/debugging#traces) shows it under the step:

```text
cache: replayed 1 of 2 recorded actions, then the agent took over: a recorded target is not on the screen; recorded with `openai/gpt-6-luna-fast`; at action 2 of 2, tap button "Apply"; recording saved (`.e2e/cache/<entry>.json`)
```

`agent.act()` returns the same record, without `write`, as `result.cache`.
A [custom executor](https://e2e.com/docs/executors#replay-cache) gets the hand-off in
`ctx.replayedPrefix` and must continue from the current screen too.

## Keep healing from costing model calls in CI

In CI, `cache` defaults to `read-only`, so a healed step isn't saved back to
disk and will hand off (and pay for model calls) on every CI run until you
update it. Rerun locally in `read-write` mode and
[commit the updated cache](https://e2e.com/docs/cache#commit-the-replay-cache).

If you'd rather fail CI immediately instead of calling a model, pass
`--strict-cache`. See
[Fail on a stale recording](https://e2e.com/docs/cache#fail-on-a-stale-recording).

## Next

<CardGroup cols={2}>
  <Card title="Caching" icon="box-archive" href="/cache">
    Modes, stop reasons, and what a recording holds.
  </Card>
  <Card title="How agent steps work" icon="list-check" href="/agent-steps">
    What the agent sees and how a step ends.
  </Card>
</CardGroup>
