fix(cache): stop replays self-finalizing when the effect did not happen

Merged
OK
2mo ago
+1454-529

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's agentContext). A registered secret in the context contributes only its name. One persona no longer replays another's recording (B22), and changing agentContext causes 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 shares app.identity still 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=safe and ?mode=unsafe are different screens (B21).
  • The end route must now match exactly, on every look the end wait takes. undecided is 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, and selected states, 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 alert raised 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 imported vs 0). 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 truncated instead 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|drift changes the screen under each recording, and a deterministic StepExecutor that logs every call. For each case: record, then a drift run in read-write, --strict-cache, and --no-cache, reading result.cache and the fixture's own state log. Probe files live outside the repo and are not committed.
  • pnpm check, pnpm test, pnpm test:testbed, and pnpm test:web-benchmark all pass.
  • Rebased onto main 4ce01b29 (after #729, #725, #723, #784, #785, #779). #729 made every observed node a RedactedNode; the anchor delta now reads only those: an anchor's value is the node's already-redacted value (a secure field has none), states are 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, and redactNodesAgain now 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 secrets entry filled by typeSecret into a plain text field records the anchor as value: "<secret:api-key>" (and the echo as Stored <secret:api-key>), replays self-finalized, and no file under .e2e holds the plaintext.

Drift run, main vs this branch. "Calls" counts executor calls for the step; "state" is what the fixture saw.

IDDriftmainthis branch--no-cache (reference)
B16agentContext Monthly -> Dailyself-finalized, 0 calls, picked Monthly (test fails)missed no-entry, 1 call, picked Daily (passes)1 call, Daily
B22agent: ['buyer','admin'], admin refusesadmin self-finalized buyer's flow, approvedadmin no-entry, 1 call, refusesadmin refuses
B21?mode=safe -> ?mode=unsafeself-finalized, ran task in unsafe modewrong-context, executor refusesrefuses
B26absolute URL on another originself-finalized on localhostwrong-context, 1 call1 call
B26preview URL, same app.identityself-finalizedself-finalized (kept)-
B20delete is a no-opself-finalized, Item A still thereend-mismatch, handed off1 call
B19unnamed row buttons, rows reorderedself-finalized, marked Betaself-finalized, marked AlphaAlpha
B2310 rows + status Report failedself-finalizedend-mismatch1 call
B24Payment saved + alert Card declinedself-finalizedend-mismatch1 call
B253 records imported -> 0self-finalizedend-mismatch1 call
B27switch click preventedself-finalized, switch offend-mismatch1 call
B29status already Submitted, submit no-opself-finalizedend-mismatch1 call
B30link now goes to a lookalike /profileself-finalized on /profileend-mismatch1 call
B31alert container after rowsend-mismatch (did not reproduce)end-mismatch1 call
B28row Record 1 now links to /records/2self-finalized, opened 2self-finalized, opened 2opened 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.setVariant instead 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.