fix(cache): stop replays self-finalizing when the effect did not happen
The replay cache could self-finalize an agent.act step with zero executor calls on a screen where the step's effect never happened, or for the wrong persona, query, or origin. A deterministic drift probe reproduced 14 such false passes on main. This branch fixes all of them in packages/e2e/src/cache/* and the key plumbing, and the replay now hands off to the executor in each case.
What changed
Key identity (cache/identity.ts, cache/context.ts, agent/act.ts)
- The key now holds the configured agent name and
agentContextDigest, a SHA-256 of the agent's redacted context (context, then the test'sagentContext). A registered secret in the context contributes only its name. One persona no longer replays another's recording (B22), and changingagentContextcauses a miss (B16). - The occurrence index counts per agent and signature, so another agent's call of the same instruction doesn't renumber this one's.
- The executor stays out of the key and is kept as provenance. The OQ10 note is removed.
Route identity (cache/route.ts, cache/decide.ts)
- A route is now origin + path + query. A location on the app's own origin is compared as its path (
appLocation), so a preview deployment that sharesapp.identitystill replays. A page on another origin is a different route (B26). - Query values that look minted are read as
:id, using the same rules path segments already use: digits, hex, uuids, long mixed tokens, prefixed record ids (INV-2041, from #777), whitespace, plus signed tokens, ISO timestamps, and dates. Every other value is part of the route:?mode=safeand?mode=unsafeare different screens (B21). - The end route must now match exactly, on every look the end wait takes.
undecidedis gone, so a link that lands on a lookalike page hands off (B30). - Locations are kept whole up to 4,096 characters. Longer ones poison the trace instead of being cut.
Anchors: the recorded delta (cache/anchors.ts, cache/trace.ts, agent/step-cache.ts)
- New
goneAnchors: nodes the step made vanish. They must be gone again on replay (B20). - Anchors record
checked,expanded,pressed, andselectedstates, plus a field's input value (redacted, never for a secure field). A switch turned on is no longer the same anchor as the switch left off (B27). - Announcements (
status,alert,alertdialog) sort first, so the cap of 8 no longer drops the line that names the outcome (B23). - An
alertraised during the replay that the recording never saw fails the end check (B24). Volatile parts of an alert's text are read as placeholders. - A count phrase is part of the effect when the step made it appear on the screen it acted on (B25,
3 records importedvs0). It is still treated as volatile when it only moved, or when it sits on a page the step navigated to. - Evidence rule: at least one recorded change must happen during the replay. It is measured from the first screen the replay saw on its end route (B29: a status that already read
Submitted). A field's value or a switch the step itself set counts only when nothing else changed. Before a free action (navigate, typed text, key), the replay looks at the screen until it reaches the end route, so the baseline exists. - A step with no observable delta and no route move is not recorded. If an entry was read for that step, it is evicted.
Unnamed controls (agent/actions.ts, cache/relocate.ts, cache/recorder.ts)
- When a row has no other text, its own label (
<li>Alpha <button><svg/></li>) becomes the row key (B19). - An unnamed control among twins needs a named nearest row, list item, or group. Without one, the trace is marked
truncatedinstead of replaying by order.
flowchart TD
R[replayed every action] --> E{end route same?<br/>origin, path, query}
E -- no --> H[end-mismatch: hand off]
E -- yes --> V{recorded change happened<br/>since first screen on end route?}
V -- no --> H
V -- yes --> D{appeared present, gone absent,<br/>states and values equal,<br/>no unseen alert}
D -- no --> H
D -- yes --> S[self-finalized]
REPLAY_POLICY_VERSION is bumped to conservative/6. Every existing entry re-keys and records again on the first run after the upgrade. Oskar accepted this.
Committed recordings
All 29 entries in apps/web-benchmark/.e2e/cache/ and all 39 in apps/mobile-benchmark/.e2e/cache/ are now unreadable (no-entry). I could not re-record them: this machine has no AI_GATEWAY_API_KEY and no e2e login. Until someone re-records them, the web agent job runs every step live under --strict-cache. A no-entry step is not stale, so the job should still pass, but it spends model calls. To re-record: AI_GATEWAY_API_KEY=... pnpm --filter @e2e-dev/web-benchmark test:agent after e2e cache clear, then the mobile benchmark's test:agent on an iOS simulator, and commit the new entries.
Verified
Ran it locally: yes
- Drift probe: a scratch http fixture where
PROBE_MODE=record|driftchanges the screen under each recording, and a deterministicStepExecutorthat logs every call. For each case: record, then a drift run in read-write,--strict-cache, and--no-cache, readingresult.cacheand the fixture's own state log. Probe files live outside the repo and are not committed. pnpm check,pnpm test,pnpm test:testbed, andpnpm test:web-benchmarkall pass.- Rebased onto main
4ce01b29(after #729, #725, #723, #784, #785, #779). #729 made every observed node aRedactedNode; the anchor delta now reads only those: an anchor'svalueis the node's already-redacted value (a secure field has none),statesare booleans, and no local redact pass came back. The start capture (on record) and the replay's baseline and start captures (on replay) are redacted again with the ledger as it is now before the delta is derived or checked, andredactNodesAgainnow relinks children so the gone side keeps its leaves-before-containers order. Checks and suites pass again, and the drift probe table below is identical on the rebased build. - Secret in an anchor: a
secretsentry filled bytypeSecretinto a plain text field records the anchor asvalue: "<secret:api-key>"(and the echo asStored <secret:api-key>), replays self-finalized, and no file under.e2eholds the plaintext.
Drift run, main vs this branch. "Calls" counts executor calls for the step; "state" is what the fixture saw.
| ID | Drift | main | this branch | --no-cache (reference) |
|---|---|---|---|---|
| B16 | agentContext Monthly -> Daily | self-finalized, 0 calls, picked Monthly (test fails) | missed no-entry, 1 call, picked Daily (passes) | 1 call, Daily |
| B22 | agent: ['buyer','admin'], admin refuses | admin self-finalized buyer's flow, approved | admin no-entry, 1 call, refuses | admin refuses |
| B21 | ?mode=safe -> ?mode=unsafe | self-finalized, ran task in unsafe mode | wrong-context, executor refuses | refuses |
| B26 | absolute URL on another origin | self-finalized on localhost | wrong-context, 1 call | 1 call |
| B26 | preview URL, same app.identity | self-finalized | self-finalized (kept) | - |
| B20 | delete is a no-op | self-finalized, Item A still there | end-mismatch, handed off | 1 call |
| B19 | unnamed row buttons, rows reordered | self-finalized, marked Beta | self-finalized, marked Alpha | Alpha |
| B23 | 10 rows + status Report failed | self-finalized | end-mismatch | 1 call |
| B24 | Payment saved + alert Card declined | self-finalized | end-mismatch | 1 call |
| B25 | 3 records imported -> 0 | self-finalized | end-mismatch | 1 call |
| B27 | switch click prevented | self-finalized, switch off | end-mismatch | 1 call |
| B29 | status already Submitted, submit no-op | self-finalized | end-mismatch | 1 call |
| B30 | link now goes to a lookalike /profile | self-finalized on /profile | end-mismatch | 1 call |
| B31 | alert container after rows | end-mismatch (did not reproduce) | end-mismatch | 1 call |
| B28 | row Record 1 now links to /records/2 | self-finalized, opened 2 | self-finalized, opened 2 | opened 2 |
Under --strict-cache on the branch, every drifted ID above fails with REPLAY_STALE and 0 calls. On main every one self-finalized except B31. A record-then-replay run with no drift self-finalizes all 16 probe steps on both builds. The one exception is the branch's B22 admin step, whose refusal never confirms a recording.
B28 is not a cache bug. Record ids are :id by design, and the live executor, given the same instruction, opens the same record (the --no-cache column).
Behavior worth reviewing
- A query value the recording did not have (
?tab=notes, a tracking parameter) is now a miss. A slug that differs at the end (/products/summer-sneaker->/products/winter-boot) now hands off instead of letting anchors decide. - A step with nothing to check never replays: a lone password fill, or a page-and-back round trip (
agent-act-verbs.test.ts,trace-secrets.test.ts). - Integration fixtures pick their variant through
FixtureApp.setVariantinstead of?variant=, since the query is now part of the screen. - The evidence rule asks for at least one change rather than every change. Transient UI left by earlier steps (a toast fading, lazily loaded widgets) would otherwise hand off every run.