feat(cache): report agent detours in replay recordings (ENG-1127)
What
A replay cache recording now keeps the screen each action ran on, and a step whose recording leaves a screen and comes back to it with nothing typed, chosen, or set on the way reports the detour:
cache: replayed all 5 recorded actions, no model call; detour: actions 1-3 left /settings for /settings/notifications and came back with nothing typed, chosen, or set
step.cache.detourcarries that text when the step records and when it replays (the trace page, the HTML report'sdetourtag, and adetouredcount in the run summary show it).e2e cache show <entry>prints one entry action by action with the screen each ran on.e2e cache checklists every detour for a person to read before committing recordings, and exits 0 whatever it finds: a correct recording takes a detour too when a tap changes data and returns (a delete that goes back to the list) or the step asks to go back. An entry recorded before this change has no screens.checkreads its actions for the shape tap, look around,back, and prints a match as a hint.e2e cache lsopens each row with the entry digest thatshowtakes, and marks(detour)or(detour hint).
Why
An agent on iOS opened the wrong settings page, scrolled it, backed out, then took the right one. The step passed, so the cache recorded the wrong turn and every replay repeats it, and a quality gate that checks "every agent step replayed" saw nothing wrong (ENG-1127). The trace kept only where a step started and ended, so nothing could tell that flow from a clean one. This records where each action ran and reads the recording for a return to a screen it left. A detour stays a hint, since an instruction can ask to look at a page and come back.
How it works
flowchart LR
A[action commits] --> B["at: location of the step's fresh look"]
B --> C[recorder]
C --> D["stage: findDetours on live locations, kept as spans"]
D --> E["settle: mask locations, write"]
E --> F["replay / e2e cache check read the spans"]
- Where each action ran. The dispatcher reads the location of the newest look, spelled with
appLocationlike the start and end paths, and only when the step looked again since its previous action, so a stale location is never stored (packages/core/src/agent/action-dispatcher.ts:819). A scroll to a text keeps the look it started from, since it looks at every page it turns. A tap also notes the role of the control at its target's center when that differs from the target's (an icon inside a switch,withControlat:830; point taps atrecorder.ts), so turning a switch on reads as a change. - Trace-1 fields.
aton every action anddetourson the trace are optional provenance (packages/core/src/cache/trace.ts:196,:515). The reader drops a malformed one, or a span shorter than two actions, and still replays (trace-read.ts:183); an older runner drops both. No cache key changes, and the same-flow check that keeps a committed entry ignoresatandcontrolRoleand comparesdetours, an absent list reading as an empty one, so an entry recorded before this change is kept, not rewritten, when its flow runs live with no detour (flowOf,packages/core/src/cache/context.ts:176). A recording that fits the store's 1 MiB limit only without this provenance is written without it, and kept when the store holds it so already (withoutProvenance,:162). A location that cannot be templated is left out instead of costing the recording (template.ts). Repeated scrolls still fold into one action, keeping the first known location, so replay pacing and the 50-action cap are unchanged. - The rule.
findDetours(packages/core/src/cache/detours.ts:148) scans the places before each action plus the end path for the first return to the same address after a departure, with no typed value, secret fill, select, check, upload, drag, key press, project tool, or tap on a state control between (byLocations,:284). Each wrong turn is a detour of its own, so two trips from one menu are two spans and a replay that stops between them still has the first whole. A tap counts as setting a state when either its target's role or the control at its centre is a state role (changesState,:119): a checkbox whose label holds a link reaches the link and still sets the checkbox. The same address means record ids and query included, fragment dropped (addressKey,route.ts:219):/orders/123/edit, the list, then/orders/456/editmoved on to another record and is no return. In an app that routes in the fragment the document path counts too (/#/menuis not/about#/menu); an empty path segment does not, as for replay's routes. Afile:location is read as a path on its authority, so an anchor on the page is no other page. Replay checks still compare routes. The span starts at the action that left. An ordinary button tap on the away screen does not end a detour: a hint may be a false positive, and the text claims only what the rule shows. Entries without locations fall back to the action shape (byActions,:349), and the text of such a detour ends in(recorded without locations: a hint only)wherever it is printed, the step's report included (describeDetour,:409). A stop spelled like the start readsfor another /orders/:id. Each place's key is computed once per scan: timed onfindDetoursalone, 300 entries of 50 same-route actions went from 726 ms to 32 ms. - Found once, on live values, stored without query values. The step finds detours on the addresses its run saw and stores them as
{from, to}spans (packages/core/src/agent/step-cache.ts:697). It then stores each action's location with every non-empty query value and every path parameter value (;jsessionid=) as:id, query keys kept unless they read as minted, and each path segment as:idwhen it reads as minted or as a token, a signed token's dots included (routeLocation,route.ts:247;storedSegment,:303;withRouteLocations,step-cache.ts:881). A callback code or magic-link token in a query never reaches the entry, even one of letters only that the route rule reads as a word. A path segment with fewer than two digits reads as a route word and stays, so a magic-link token in the path with one digit or none is kept. A fragment route keeps its document path and#(/app/#/orders/:id); afile:URL, or an address on another hierarchical scheme (app://bundle/index.html, as a desktop app's window can load from), is spelled as a path on its scheme and authority (unparsedLocation,:316). A web address that holds a configured secret's marker, percent-encoded or not and whatever the secret's name (config does not restrict names, so<secret:oauth:cb>and a credential's<secret:qa@acme.test.password>are markers too; a name is read up to its first>), is stored as its markers alone (<secret:token>): a secret can hold part of the address's punctuation (about?code=) and leave no boundary to read the rest by. The detour text names the same spelling. A stored location no longer tells two records apart, so a reader of the entry uses the spans instead of comparing again. A replay that finishes the step reads the stored spans and names them in that spelling, its filled-inunique()values included (:586). A replay that hands off computes the detours of the actions it ran once (findReplayedDetours,detours.ts:200, called atstep-cache.ts:506): a stored span that ends inside them as stored, and a trip in a span the hand-off cut through, read again on the stored locations, each span on its own, only inside it. When the replay saw where it stopped (a screen with no recorded target, or the last screen the end wait saw when the end did not match), a return there counts only if that screen is the address the trip left, which the replay saw itself in every cache mode (placed,step-cache.ts:154), for an entry with no stored spans too; with no such address, only by a recorded spelling that holds neither:idnor a secret's marker alone. A span whose return the stop disproves is read again inside. The report names those spans, and the step's new recording keeps them: it gives the replayed actions the locations their recording had (keepLocations,:501), compares only those copied locations by their spelling, and adds the run's own spans that end where the agent took over or after it. That scan starts in none of the entry's spans and crosses none, judges each trip before it moves past it, starts none from a copied:idlocation, and requires a return to match both the place it started from and the place the trip left (findDetoursAfterReplay,detours.ts:166). - Secrets. Besides the stored spelling, locations are redacted when recorded, again when the trace is finalized, and again right before the store write with the process ledger, which keeps every value any session learned, a closed guest session's included (
settleStagedTraces,context.ts:262). The detour text is cut to its 600-character bound between words (boundDetour,detours.ts:380): a stored location has no space, so a cut never leaves part of a percent-encoded value the ledger cannot read from a part. After an attempt settles, every step's detour text is masked the same way and bounded again (settledDetour,:394;packages/e2e/src/run/execute.ts:1094), and so is a hosted attempt's, ase2e mcp's test sessions run (run/standalone-test.ts:436). A run test visits a page named by a provider secret, fills that secret on a later line, and checks that both the entry's locations andstep.cache.detourread<secret:invite>. A guest session's mirrored step is masked the same way, its cut tail by the process ledger's cut-aware redaction (run/guest-steps.ts:167). A unit test of the guest mirror and a run test ofe2e mcp's hosted attempt each fail with their masking step removed. A lone surrogate a cache file holds is spelled as U+FFFD, so reading detours never throws. - Report.
StepCacheInfo.detour(packages/core/src/run/steps.ts:157) is in report-1 with a 600-character bound, both fixtures, and the schema test. The trace page prints it on the cache line (failure-text.ts:440), the list reporter countsdetouredfrom the attempt that passed (format.ts:290), and the HTML report tags the step and shows the text in its detail (packages/html-reporter/app/test.tsx:300, rendered intests/unit/data.test.ts). - CLI.
showreads a digest prefix from the store, or, for any argument but hex digits, a file path as the store reads one (size, JSON, trace-1 reader), without the config, a customcache.storeincluded (packages/e2e/src/cli/cache.ts:340,:308); an entry's own file name (<digest>.json) names its digest; a bare name that is no file says it is no digest either and namese2e cache ls.checklists the detours and exits 0 (:434). The rule ends a detour only at a typed value or a state control, so a correct recording whose tap changed data and returned takes one, and until a detour can be accepted, a failing exit would fail such a recording with no way out. Config and file errors still exit 2. Entry text is printed with terminal controls stripped,ls's cells included.
Docs: docs/cache.mdx "Find detours in a recording" (written again on main's rewritten page, which the rebase kept), reference/cli.mdx, reference/agent.mdx, and skills/e2e with its plugin copy. Two changesets (patch).
Known limits, in the docs: two device screens with the same title are one screen; on Android, a screen with no toolbar (a React Native header is a plain view) reports only the app, so no detour between such screens is found (docs/reference/mobile.mdx now says so); a route the page moves to only while scrolling is not seen; a plain button tap does not end a detour; a trip read again after a hand-off is judged on the stored spelling, so two records or query values of one route can read alike and give a false hint (docs/cache.mdx). A value the run learns only after the step that met it (a provider's later value, or a static secret of a host opened later from another installed copy of @e2e-dev/core) is masked in a stored location and the detour text only where they still spell it as the run saw it: where the origin was dropped, an id or query value became :id, or a unique() value became its placeholder, it can stay whole or in part, and in a serial group a later test's value can stay in an earlier test's entry (docs/reference/security.mdx, Cache entries). Not in this PR: a way to accept a detour the test asks for, spelling locations at settle time, and re-redacting a navigate action's own url and summary with a secret a later step resolves (main stores them the same way).
Issue
ENG-1127
Verified
Ran it locally: yes
Head ad47ed05, rebased onto d46f2ade (main had added Rust files, and the repository's CodeQL Rust job fails on a tree with none). Main has since moved to 47d50635; the head merges into it without a conflict (git merge-tree). Fourteen rounds of independent reviews (four models a round; one missing in round 10) since 0249effb added the fixes in steps 2, 3, 4, 5, and 7 above (secret markers in an address, file: apps, token path segments and path parameters, fragment document paths, one span per wrong turn, the hand-off spans, their scan and the live check at the stop in every cache mode, the word-boundary cut, hosted attempts, the reader's span check, the size fallback, show's file argument) and the documented limits for values learned late. The review loop stopped after round 14 by request, not on a clean round. The maintainer reviews of a035d75b then added four commits: a secret marker of any name (oauth:cb, a credential's qa@acme.test.password) is read as a marker, a custom-scheme location keeps no query value, the guide gives the two-digit token rule, and the detour run tests moved to agent-trace-detours.test.ts with masking tests for guest sessions and hosted attempts. The bot reviews of that head then added: a marker's name is read up to its first >; a return at a hand-off's stop is confirmed against the replay's own address first, for an entry with no stored spans too, never by a marker-only spelling, and against the last screen the end wait saw; a guest step's detour cut through a value is masked by the process ledger's cut-aware redaction; cache ls strips terminal controls from its cells; and the docs say what an entry holds, what check lists, and how show reads an entry's file name, and that an entry past the store's size limit is written without its screens and detours.
On this head:
pnpm build; everypnpm checkstep (oxlint with.t3-tmp/ignored, fallow, typecheck, docs and plugin checks) exits 0.e2eunit and integration suites together: 6827 passed, 15 skipped (405 files).@e2e-dev/html-reporter: 27 passed.- The new run tests fail with the fix they cover removed: the late-secret test under each of the two mutants the review named (staging without
processSecrets.redact, nosettledDetourpass inexecute.ts); the switch-icon taps without the dispatcher's control capture; the hand-off tests (two views of one page, in read-write and read-only mode; an inner trip; a stop on a screen that is not the recorded one; an end that never reached the recorded route) without the fix each covers; and the size-limit test without the fallback.
The review's flows, built CLI, scratch app, model-free executor: a list with "Order 123"; its page's "Delete order" deletes it and returns to the list; and a link to /orders/123?code=SplxlOBeZQQYbYSWxSbIAqrTuvXyz (letters only), then back:
$ e2e run
Tests 2 passed (2)
Cache 2 missed · 2 detoured
$ e2e cache check # exit 0
fdf3fd13c733 tests/orders.e2e.ts::deletes%20an%20order (web): actions 1-2 left /orders for /orders/:id and came back with nothing typed, chosen, or set
26d16486d8fa tests/orders.e2e.ts::opens%20order%20124%20after%20a%20callback (web): actions 1-2 left /orders for /orders/:id?code=:id and came back with nothing typed, chosen, or set
2 of 2 replay cache entries take a detour; e2e cache show <entry> prints its actions. A detour the step asks for, or one after a tap that changed data, is expected
$ CI=1 e2e run
Cache 2 replayed · 2 detoured
No file under .e2e/cache/ holds the code; the stored location is /orders/:id?code=:id. report.json still has it in the engine event's navigated to detail, as on main.
Not run locally: the web benchmark agent suite and pnpm bench:ab. Another session's benchmark server still holds port 4280 (load average 24 on 18 cores when checked), and the suite's app URL is part of its cache key, so another port would miss every committed recording. CI's benchmark job runs that suite with --strict-cache on this head. The bench:ab table below is from 5dbbb01c, before the reviews, and is stale in one respect: the reviews made a replay record where each action ran in every cache mode (placed, step-cache.ts:154), read-only included, which is the mode the table measures. That cost is not measured here.
Built CLI on a scratch project whose executor (no model) opens Settings, taps Notifications, scrolls, goes back, then taps Privacy and Clear history. Two runs, then the cache commands:
$ e2e run # records
Tests 1 passed (1)
Cache 1 missed · 1 detoured
$ e2e run # replays
Tests 1 passed (1)
Cache 1 replayed · 1 detoured
$ e2e cache ls
ENTRY TEST TARGET INSTRUCTION AGE ACTIONS
5c47d0e3648c tests/settings.e2e.ts::clears%20history web 1249e7e89263 <1m 5 (detour)
$ e2e cache check # exit 0
5c47d0e3648c tests/settings.e2e.ts::clears%20history (web): actions 1-3 left /settings for /settings/notifications and came back with nothing typed, chosen, or set
1 of 1 replay cache entry takes a detour; e2e cache show <entry> prints its actions. A detour the step asks for, or one after a tap that changed data, is expected
$ e2e cache show 5c47d0e3648c
# AT ACTION
1 /settings tap link "Notifications" (detour)
2 /settings/notifications scroll down (detour)
3 /settings/notifications navigate back (detour)
4 /settings tap link "Privacy"
5 /settings/privacy tap button "Clear history"
end state: shows status "History cleared", shows heading "Privacy", shows button "Clear history", ...
detour: actions 1-3 left /settings for /settings/notifications and came back with nothing typed, chosen, or set
The replay report has step.cache = {"mode": "self-finalized", "detour": "actions 1-3 ...", "write": "kept"}.
The same project with the Notifications link carrying ?token=Zx8aQ2pLmN4tR7vW stores and prints the route; no file under .e2e/cache/ holds the token:
$ e2e cache show 5c47d0e3648c
# AT ACTION
1 /settings tap link "Notifications" (detour)
2 /settings/notifications?token=:id scroll down (detour)
3 /settings/notifications?token=:id navigate back (detour)
4 /settings tap link "Privacy"
5 /settings/privacy tap button "Clear history"
...
detour: actions 1-3 left /settings for /settings/notifications?token=:id and came back with nothing typed, chosen, or set
The run's report.json still shows the URL in the step's navigated to detail, as it does on main.
On main the same runs print no detour, and e2e cache check is error: unknown command 'check'. On this branch, e2e cache check on the committed web benchmark cache, recorded before this change, exits 0:
d2a64638fa64 tests-agent/control-inventory.e2e.ts::...back%20returns%20from%20the%20details%20view%20to%20the%20inventory (web): actions 1-2 left a screen, only looked around, and went back (recorded without locations: a hint only)
no detours the recorded screens show in 31 replay cache entries in apps/web-benchmark/.e2e/cache
1 entry recorded without locations only looks like one. Delete an entry an earlier version recorded and run its test again to record them; an engine that reports no location records none
Web benchmark agent suite from its committed recordings, CI=1 ... --config e2e.agent.config.ts --strict-cache, no model key: 26 replayed · 3 handed off · 6 missed · 1 detoured, and the committed cache is unchanged. The 8 failures are MODEL_PROVIDER_FAILED on steps that need the model on main too: 5 gaps at a value read off the screen and the planted-bug tests, which have no recording.
pnpm bench:ab on the agentic control inventory, which replays from committed recordings without a model key. CI=1 makes the cache read-only, so this measures replay with the detour read on every replayed step, not recording:
bench-ab: a820da9b (merge base with origin/main) vs 5dbbb01c (this change before the rebase onto b7201fc9; the rebase and the later docs commit change none of its code)
apps/web-benchmark: e2e run --workers 1 tests-agent/control-inventory.e2e.ts --config e2e.agent.config.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 3.1.
Behavior
Every test ran the same steps with the same outcome on every run of both builds.
Timings
| Metric | base median (IQR) | head median (IQR) | change (95% CI) | verdict |
|---|---|---|---|---|
| wall (process) | 7.80 s (7.75 s-7.82 s) | 7.77 s (7.74 s-7.79 s) | -0.4% (-1.4% to +1.0%) | same |
| suite (sum of attempts) | 7.10 s (7.04 s-7.12 s) | 7.06 s (7.05 s-7.08 s) | -0.5% (-1.3% to +0.9%) | same |
| observe (events) | 5.23 s (5.19 s-5.25 s) | 5.20 s (5.20 s-5.23 s) | -0.5% (-1.5% to +1.3%) | same |
| action (events) | 1.00 s (991 ms-1.01 s) | 996 ms (978 ms-1.01 s) | -1.5% (-3.1% to +2.2%) | same |
| step agent.act | 6.36 s (6.30 s-6.38 s) | 6.33 s (6.32 s-6.35 s) | -0.5% (-1.7% to +1.2%) | same |
| step app.open | 504 ms (504 ms-506 ms) | 497 ms (495 ms-504 ms) | -1.0% (-2.5% to +0.0%) | same, under the floor |
| step expect.toHaveText | 31 ms (30 ms-33 ms) | 31 ms (29 ms-32 ms) | -1.8% (-15.7% to +5.0%) | same, under the floor |
Counters
| Counter | base | head |
|---|---|---|
| observed bytes | 42449 | 42449-42451 |
Tests outside the threshold: 0 of 34
With 34 tests compared at 95%, about 1.7 could land outside by chance.
The one counter that moved: in one of the six head runs, the observation after the right-click menu's Rename was 2 bytes longer with the same 59 nodes. The tree revision that observation lands on moves between runs of either build (b29 to b31 on base), so this is when the page settled, not what the step did; the steps and outcomes matched every run.
Not attached: a terminal recording (VHS and ffmpeg are not installed here; the output above is the CLI's text) and a mobile run (no simulator in this session; the location on a device is the app and screen title, covered by unit tests).
🤖 Generated with Claude Code
Summary by CodeRabbit
-
New Features
- Added
e2e cache showto inspect cached recordings ande2e cache checkto report detected detours. - Cache listings, run summaries, traces, and HTML reports now show detour information, including where a recording left and returned to a screen.
- Cached recordings now retain screen-location details to support inspection and detour reporting.
- Added
-
Documentation
- Expanded cache command guidance and explained detour reporting, recording behavior, and location-related limitations.