Files
e2e/docs/cache.mdx
T

240 lines
10 KiB
Plaintext

---
title: Caching agent steps
description: A passing agent.act records the actions it took. The next run replays them with no model call and hands the step back to the agent when the app stops matching.
---
A passing `agent.act()` records the actions it took: the taps, the typed
text, the picked options, and a description of each control it used.
The next run plays the recording back instead of asking the model, and the
live agent takes over the moment the app stops matching. Judgments always
run live: `agent.assert`, `agent.waitFor`, and `agent.extract`.
A recording is kept only once a later check proves the step did what it
claimed. An `act` nothing verifies is never replayed.
The run summary reports the outcomes:
```text
AI 4.1k tokens · 2 model calls · anthropic/claude-sonnet-4.5
Cache 4 replayed · 1 handed off · 1 missed
```
A replayed step made no model call. A handed-off step ran part of its
recording before the agent finished it. A missed step ran live from the
top. The report keeps the outcome on each agent step as `step.cache`, with
a `reason` on misses and hand-offs, and `agent.act()` resolves with the same
object. With the cache off there is no `step.cache`.
To rule the cache out of a failure, run once without it:
```bash
npx e2e run --no-cache
```
## How a replay works
<Steps>
<Step title="Check the starting point">
Only a first attempt replays. A retry runs live. The app has to be on
the path the step was recorded on, query string aside, unless the
recording opens with a `navigate`.
</Step>
<Step title="Replay the actions">
Before each action the runner finds the control on the current screen
by its role, name, test id, placeholder, input purpose, and the row it
sat in, waiting up to 15 seconds for one that is missing. Exactly one
has to match, unless the recording saw the same number of look-alikes
and noted which one it used. Otherwise the replay stops there. Typed
text replays verbatim. A secret replays by name, and the runner fills it.
</Step>
<Step title="Check the end state">
The recording keeps the path the step ended on and up to eight controls
that appeared during it, a `saved` status for example. The replay waits
for them to come back, for as long as the original step took plus ten
seconds, two minutes at most. Ids the app mints may differ, so a step
that creates a record replays on the next record's page too.
</Step>
<Step title="Pass, or hand off">
When everything is back, the step passes with zero model calls.
Otherwise the agent is told what already ran and why the replay
stopped, and continues from the current screen without redoing it.
</Step>
</Steps>
## What must match
A recording replays only for the exact step it was made for. The key holds:
- the project, `projectId` or else the package name, and the test's file
path and full title path
- the target name, its platform, the engine and its `major.minor` version,
and the app identity, which is `identity` when set, else the URL's origin
and base path, plus `environment`
- the instruction text, trimmed and normalized. Rewording it is a new key
- the `params` values, with a `Secret` reduced to its name and purpose
- which repeat of the same instruction it is within the test
Everything else can change without touching the key: the model, the agent,
the rest of the config, the code around the step. An e2e release that
changes the replay rules starts the cache over. A preview URL that changes
per deploy needs [`identity`](/reference/config#the-app-under-test) so its
entries survive. On a device the path is the
[screen title](/reference/agent-device#trace-cache-on-a-device).
## When a recording is written
In `read-write` mode every `act` step records the actions it commits,
whether the agent or a replay performed them, and the recording is written
at the end of the attempt if a verification step passed after it. A step
that committed no action records nothing.
| Counts as verification | Does not count |
| --- | --- |
| `expect(locator).toHaveText(...)` and other locator matchers | `expect(value).toBe(...)` on a plain value |
| `expect(web).toHaveURL(...)` and other engine assertions | `expect.poll(...)` |
| `locator.waitFor()`, `web.waitForURL()` | `locator.textContent()` and other reads |
| `agent.assert(...)`, `agent.waitFor(...)` | `agent.extract(...)`, another `act`, the attempt passing |
So pair every `act` with a check of its outcome:
```ts title="tests/cart.e2e.ts"
import { expect, test } from 'e2e';
test('adds an item', async ({ app, agent, screen }) => {
await app.open('/cart');
await agent.act('add one item to the cart');
await expect(screen.getByRole('status')).toHaveText('1 item');
});
```
An unconfirmed recording is deleted, so a broken flow re-records on the
next passing run instead of replaying forever. A failing attempt writes
what was verified before the failure and deletes the rest. A replayed step
that then fails, or that the agent had to repair after an `end-mismatch`,
deletes its entry. Ctrl-C writes nothing and deletes nothing. Deletion uses
the store's `delete`, which the file store has and a custom `cache.store`
may lack.
## Why a step was not replayed
`step.cache.reason` in the report says why:
| Reason | What happened | What to do |
| --- | --- | --- |
| `no-entry` | Nothing is stored under this key: a first run, a step no check confirmed, a step that committed no action, or a changed key, an engine or e2e upgrade included. | Nothing. The next verified pass records it. |
| `retry` | Retries never replay. | Nothing. |
| `invalid-entry` | The file is unreadable, over 1 MiB, or from another schema version. | `npx e2e cache clear`. |
| `truncated` | More than 50 actions, a typed value or URL over 4,096 characters or containing a registered secret, or a control with no usable description. | Split the step, or pass the secret as a `Secret`. |
| `wrong-context` | The app was on another path than when the step was recorded. The query string is ignored. | Open the same route before the step. |
| `target-not-found` | A recorded control did not appear within 15 seconds. | The UI changed. The next passing run re-records. |
| `target-ambiguous` | Several controls match where the recording found one, or a different number of look-alikes than it did. | Give repeated controls distinct names, or name your data uniquely per run. |
| `gap` | A mutating project tool ran here. A replay cannot reproduce it. | Expected. |
| `action-failed` | The app rejected a replayed action. | Check the app. |
| `action-uncertain` | A replayed action failed with an unknown commit state. The agent is told to check before acting again. | Check the app. |
| `viewport-changed` | A bare-point tap was recorded on a viewport of another size. | Keep the viewport fixed. |
| `end-mismatch` | Every action ran, but the recorded path or a control that should have appeared did not come back. | Leftover data from an earlier run, a banner that comes and goes, or a regression. |
The first five are misses. The rest are hand-offs, or misses when no action
had run yet. Most hand-offs go away when each run creates its data under
unique names and cleans up in `afterEach`.
## What a recording holds
One JSON file per key under `.e2e/cache/`, named after the key's hash. A
real entry, shortened:
```json
{
"schemaVersion": "trace-1",
"payload": {
"actions": [
{
"name": "type",
"summary": "type \"ada@example.test\" into textbox \"Email\"",
"target": { "role": "textbox", "name": "Email", "inputPurpose": "username" },
"value": "ada@example.test"
},
{
"name": "tap",
"summary": "tap button \"Increment\"",
"target": { "role": "button", "name": "Increment" }
}
],
"summary": "entered the email and the counter reads 1",
"startPath": "/",
"endPath": "/",
"endAnchors": [
{ "role": "status", "name": "Counter", "text": "1" },
{ "role": "button", "name": "Late arrival" }
]
}
}
```
`endAnchors` are up to eight controls that appeared during the step, text
included, which is why the counter reading `1` is part of the check.
Whether the step caused them does not matter. The second anchor is a button
the page adds shortly after load. Text that cannot read the same twice,
such as a minted id, a countdown, or a date, is skipped while a stable
anchor exists.
Never stored: the model's prompts and conversation, screenshots, the
observed tree, and secret values. The step's one-line verdict is kept as
`summary`. A secret fill stores the credential name only.
## Modes
| Mode | Replays | Records | Default |
| --- | --- | --- | --- |
| `'read-write'` | yes | yes | locally |
| `'read-only'` | yes | no | in CI when `cache` is unset |
| `'off'` | no | no | `--no-cache` for one run |
```ts title="e2e.config.ts fragment"
cache: 'off',
// or
cache: { mode: 'read-write', dir: '.e2e/cache' },
```
`--no-cache` wins over the config, in CI too. CI demotes only an unset
mode, so set `cache: 'read-write'` when CI restores the directory from its
own cache service. `read-only` never writes or deletes, so a stale entry stays
until a `read-write` run replaces it. A custom `cache.store`, for a cache
shared across machines, replaces the file store and is never demoted.
`e2e explore` and `e2e mcp` never use the cache. See the
[config reference](/reference/config#cache).
## Commit your traces
`e2e init` adds `.e2e/cache/` to `.gitignore`, so every machine records its
own and CI starts with none. To share replays, remove that line and commit
the directory. CI stays `read-only` unless you set `cache: 'read-write'`.
Treat committed entries as test data and review them in the pull request
that re-recorded them.
## Inspect the cache
```bash
npx e2e cache ls # one row per entry: test, target, instruction digest, age, actions
npx e2e cache stats # entry count and size
npx e2e cache clear # delete every entry
```
<Note>
**Not the same as the prompt cache**
`38% cached` in the `AI` row and the `cached` column of `--debug` are the
provider's prompt cache, the share of a model call's input tokens it served
from its own cache. That makes a live call cheaper. The trace cache removes
the call.
</Note>
<CardGroup cols={2}>
<Card title="Config reference" icon="sliders" href="/reference/config#cache">
`mode`, `store`, `dir`.
</Card>
<Card title="CLI reference" icon="terminal" href="/reference/cli#e2e-cache">
`e2e cache ls`, `stats`, `clear`.
</Card>
</CardGroup>