fix(cache): stop a replay from acting on the twin of a control that is gone

Merged
MI
1d ago
+1168-61

What and why

  • A step recorded on one of two same-named controls replayed onto the other when its own control was gone. The replay acted on the twin, reported self-finalized, and the test passed. It happened two ways:
    • No test ids (ENG-1404): the recorder stored position {index: 0, of: 2}, but relocation accepted any lone match on a named tier, whatever count was recorded. docs/cache.mdx already promised target-ambiguous when the count changes.
    • With test ids (ENG-1405): the test id made the target unique, so no count was stored. Once the test id was gone, replay fell back to role and name (#1208's forgiveness for a changed test id) and took the twin.
  • Relocation (packages/core/src/cache/relocate.ts, relocateDescriptor) now accepts a lone match only on a tier where the recording saw no twin:
    • A position counted on the strict tier stops a lone match on that tier. It says nothing about the role-and-name tier a changed test id falls back to, where a text that tells two controls apart still finds the right one. A position counted by place, on a minted test id's shape, still lets the exact test id match alone (relocate-minted-test-id).
    • One exception keeps main's answer: a nameless control that shares its test id with controls its text tells apart (the strict tier ignores text when a test id is recorded). A lone match that shows the recorded text is still the node, unless the recording saw that text twice (twinsWithoutTestId). A target with no test id gets no such exception: its count already compares text, so two "Preview" buttons that lose one stop.
    • The recorder sets a new optional field, twinsWithoutTestId: true, when a test-id target's role and name also matched other controls in its container (hasTwinsWithoutTestId, called from placementOf). A lone match on the semantic tier then stops. An exact test id match ignores the field.
  • The replay paths that read a refused relocation follow it (packages/core/src/agent/replay.ts):
    • Relocation marks the refusal this change adds, a lone named match turned down because the recording saw twins of it, with twinLeft on the target-ambiguous result.
    • For that mark only, placeWithin does not settle the lone candidate by the recorded point, and a scroll on a list that filled the screen does not fall back to scrolling the screen. Every other ambiguity keeps main's fallbacks.
    • retryable looks again on that mark, as it already did for two of three: the recorded test id may come back, or the count catch up on a form that renders in steps, and then the control in the recorded place is used. After about 15 seconds it hands off. A count that catches up picks by order, so a twin that renders ahead of the recorded control takes its place, the same price the two-of-three wait pays on main.
    • Once a relocation refuses a twin, its later looks keep the mark, so a look-alike that renders while it waits is not settled by the recorded point or by scrolling the screen.
    • A drag finds both ends on one look (relocateEnds), for node drags and bare-point drags alike, so a box that moves while the other end loads is read where it is now. It judges the source first, as main did, so a destination that is ambiguous for one look does not end the wait for a source still loading. Each end keeps its refusal, and the drag stops with target-ambiguous while a refused end is missing, or target-not-found once it is back and the other end is not.
    • Repeated scrolls fold only when they carry the same flag.
  • A count taken on the look an earlier action of the same step already ran from is marked countedOnEarlierLook: true (placementOf in packages/core/src/agent/action-dispatcher.ts); a scrollUntil that found its text without paging takes no look and leaves the mark as it was. An executor may act twice from one look, and the first action may remove or add a twin, so replay does not apply the new stop to that count: such an action replays as on main. No extra look is taken, so what the executor's later actions address is unchanged. Node actions, point actions and drag destinations all record their place through placementOf. The page scrollUntil reads after its last swipe, on an engine without scrollTo, counts as a fresh look, so a press right after it keeps the stop.
flowchart LR
  A[recorded target] --> B{tier that matched}
  B -- "named, no count recorded" --> N{matches now}
  N -- one --> F[act]
  N -- several --> G[target-ambiguous: hand off at once, as on main]
  B -- "strict, count recorded on it (no test id, or a shared one)" --> C{matches now}
  C -- same count --> P[act on the recorded place]
  C -- "another count above one" --> L[look again, then hand off, as on main]
  C -- one left --> D["target-ambiguous, twinLeft: look again for the count, then hand off or REPLAY_STALE; no point or screen-scroll fallback"]
  B -- "semantic, twinsWithoutTestId" --> E{matches now}
  E -- one --> D2["target-ambiguous, twinLeft: look again for the test id, then hand off"]
  E -- "several, count recorded on a shared test id" --> P
  E -- several --> G
  B -- "unnamed or generated test id, by place" --> H[as on main]
  B -- "count marked countedOnEarlierLook" --> H
  • No REPLAY_POLICY_VERSION bump. What changes for recordings already on disk:
    • A target recorded by a unique test id carries no twinsWithoutTestId, so it replays as before, the ENG-1405 bug included, until the step is recorded again. A replay that passes is never re-recorded, so such an entry keeps the old behavior until it misses for another reason.
    • A target with a position on a named tier (no test id, or one shared by several controls) now stops when one of its twins is left. That is the ENG-1404 fix, and it reaches existing entries at once.
    • That includes an entry main recorded for a step that acted twice from one look on twins the first action removed: main stored the second action's count unmarked. Its first replay here hands off, and the step is recorded again as the executor now acts, after which it replays; under --strict-cache it fails with REPLAY_STALE until re-recorded.
    • Bumping the version would cold-start every cache; say so if you prefer that.
  • Committed recordings: none of the 31 web benchmark or 39 mobile benchmark entries has a position on a named target or the new field. One mobile entry has 4 positioned targets, all unnamed textboxes, a path this does not change.
  • Tradeoff: a duplicate that really went away (a second "Close" in a toast, or one of two controls sharing a test id) now hands off to the agent after up to about 15 seconds of looking for the missing control, and records again the next time it passes with a writable cache. Under --strict-cache with committed recordings it fails with REPLAY_STALE until someone re-records it. The runner cannot tell that case from the wrong-twin case.

Issue

ENG-1404, ENG-1405 (Linear)

Verified

Ran it locally: yes

A throwaway project on the built CLI: a fake engine (defineEngine) with two "Set up" buttons that lead to the same screen, and a scripted executor that taps the first one (no model). The first run records; the second run removes the first button. The same two cases run through the web engine in tests/integration/trace-cache-twin-gone.test.ts (pnpm --filter e2e exec vitest run tests/integration/trace-cache-twin-gone.test.ts), which fails on main (self-finalized) and passes here.

second runmain (b6e65ae37)this branch (cfc8b0392)
no test ids, recorded {"role":"button","name":"Set up","position":{"index":0,"of":2}}replay tapped the second button, self-finalized, test passedmissed, target-ambiguous, nothing tapped, handed to the executor
test ids, recorded {"role":"button","name":"Set up","testId":"setup-primary"} (plus twinsWithoutTestId: true on this branch)replay tapped the second button, self-finalized, test passedmissed, target-ambiguous, nothing tapped, handed to the executor
either, --strict-cache on the replaytapped the second button, test passedREPLAY_STALE, nothing tapped
  • 34 added tests: 22 fail on main and pass here; 12 pass on both on purpose, as guards for what must not change (an exact test id still on screen, a changed test id with no twin as in #1208, a text that tells two shared-test-id controls apart, a control recorded alone, an unnamed control in a named group that lost a twin, two twins of a flagged target handing off at once, a recorded point inside an unnamed control whose one look-alike is left, a full-screen list joined by a smaller namesake, a count marked countedOnEarlierLook replaying as main both with one match left and with the recorded count, a count taken on a fresh look between two presses, and a lone control acted on from an earlier look recorded with no count mark). Hand mutants of the changed relocation, recording, reading, dispatch and replay lines are each caught by an assertion: 28 on 8902df508 (this head without the empty-look rule in relocate), and 8 on da12d2e2b's replay lines, one of them dropping the refusal when a look shows no match.

  • 4 more tests on cfc8b0392, each failing on da12d2e2b and passing here: two "Preview" buttons with no name or test id that lose one (relocate-position); a drag end that keeps its refusal when a second look-alike renders (trace-replay); a drag whose destination is refused while its source is still loading (trace-replay); and a press right after scrollUntil on an engine without scrollTo, recorded with no countedOnEarlierLook (trace-cache-twin-gone).

  • Review fixes on 2443bd8c3 (kdy1's six threads): 6 new or rewritten tests in trace-replay.test.ts fail on cfc8b0392's replay.ts and pass here (one of two twins waits for the count, for each index and for a shared test id; it hands off after the wait and picks by order when a twin renders ahead; a drag waits for its source while the destination is ambiguous for one look; a drag keeps target-ambiguous for a refused end and reports target-not-found once it is back, at either end; a bare-point drag places both ends on one look when either end waits and the other moves). One new test in trace-cache-twin-gone (a press, a scrollUntil that takes no page, a press, all from one look, records countedOnEarlierLook) fails without the action-dispatcher.ts change. pnpm check passes; pnpm --filter e2e run test:unit 4380 passed, 2 skipped; the relocate, trace and twin-gone files 177 passed.

  • More cases, in tests/unit/trace-replay.test.ts and tests/unit/relocate-twins-without-test-id.test.ts:

    casemain (b6e65ae37)this branch (cfc8b0392)
    two "Set up" buttons share one test id; the recorded one shows alone, then a twin renders after itcompleteswaits for the count, taps the recorded one
    recorded by its own test id, which is gone; one twin shows, then a second renders under the recorded pointcompletes on a twintarget-ambiguous, nothing tapped
    the same with a list that filled the screen and a smaller namesakecompletes on a twintarget-ambiguous, nothing scrolled
    the same list, then the twin disappears while the replay looks for the test idcompletes on a twintarget-ambiguous, nothing scrolled
    two nameless buttons share a test id and differ by text: the recorded one is left / only the other is leftfinds it / takes the otherfinds it / stops

    In rows 2 to 4 the recorded control is on neither screen, so completing means acting on a twin. In row 1 main acts on the one button it sees, which here is the recorded one; this branch waits for the count and taps the button in the recorded place. trace-cache-twin-gone also runs the replay under --strict-cache (REPLAY_STALE, nothing pressed, no executor call) and checks the second run's exit code.

  • On cfc8b0392: pnpm check passes; pnpm --filter e2e run test:unit 4377 passed, 2 skipped; the relocate, trace and agent unit and integration files (40) 663 passed. On da12d2e2b: pnpm --filter @e2e-dev/testbed test 600 passed, 1 skipped. On b948c3c8f (this head without the empty-look rule and its tests): pnpm test passes except one @e2e-dev/desktop test (surface.test.ts, "quits the process carrying the launch token of a launch the app list never showed"), which fails the same way on main b6e65ae37 on this machine; pnpm test:web-benchmark 95 passed; the web benchmark's agent suite with --strict-cache and no model key: 26 passed, 0 REPLAY_STALE, and the same 8 steps that need a model fail on main too.

  • Merged with #2053's head (1b3d16189), before this change's last fixes: two conflicts, both resolved by keeping both sides. In readDescriptor the two new field blocks sit side by side. In placementOf this change's placement follows #2053's early return for a control that leads its row, and hasTwinsWithoutTestId passes false for #2053's new leads argument. On that merge, the six relocate-* unit files, trace-replay, trace-recorder, trace-cache-twin-gone, trace-cache-replay and trace-cache-minted-test-id: 159 of 159 pass.

  • Not run: the agentic suites with a model key (the mobile ones run in CI only) and a recording made by a real model.

For maintainers

  • A known exception, by design: an action an executor takes from the same look as an earlier action of the step (no look between) records its count with countedOnEarlierLook and replays as on main, the wrong-twin case included. The built-in loop and the ACP and MCP tools look after every action and the decision executor every turn, so this mostly reaches custom executors that batch.
  • Left for follow-ups, all on main too:
    • A recorded point tap still lands on a twin under the pixel when two or more look-alikes are left.
    • A stale-node retry during replay re-finds the target without its recorded place, so it can take a twin.
    • scrollUntil records no place for its list, so neither a position nor the new field reaches it, and its viewport fallback stays as on main.
    • A control with more than 1,000 twins records no position, and a lone survivor is still taken.
    • Duplicate test ids: a position counted on one shared test id is applied on the role-and-name tier once the ids change, and two unnamed controls with the same generated test id accept a lone exact match.
  • #2092 (one YAML file of recordings per test) merges with this one without a text conflict, and the merged tree fails until whichever lands second makes two changes. On a local merge of #2092's head e92a62efc with da12d2e2b:
    • trace-cache-twin-gone reads entries with the synchronous readEntries helper, which #2092 makes async, so the e2e package typecheck fails.
    • With only await added, typecheck passes and two of its four tests fail on an assertion: recording-file.ts writes a target's DESCRIPTOR_FIELDS, states and position only (renderTarget), its reader refuses any other key (TARGET_KEYS), and its store writes a test's file only when it reads back the same (readsBack in store.ts). A step that records twinsWithoutTestId or countedOnEarlierLook therefore leaves the test's whole file unwritten, and the next --strict-cache run finds no recording for that step and calls the executor, as for a step never recorded.
    • Writing both flags when they are true and adding both to TARGET_KEYS (two lines; readTraceEntry already validates them) makes the file pass, 3 runs of 3, and the six relocate-* unit files, trace-recorder, trace-replay, trace-store, trace-cache-replay and trace-cache-minted-test-id pass (213 tests).
    • Recordings converted from JSON carry no twinsWithoutTestId, so a target recorded by a unique test id keeps main's behavior until re-recorded, as for entries already on disk above.
  • Fewer twins than recorded now gets one answer, as kdy1 asked in review: look again for up to 15 seconds, then hand off. Once the count is back, the control in the recorded place is used, so a twin that renders ahead of the recorded control takes its place, as two of three already did on main.
  • #2053 touches the same code, and they merge with two conflicts, both resolved by keeping both sides (readDescriptor and placementOf). Whichever lands second must count twinsWithoutTestId in the row scope it records: pass false for leads on the plain path, and compute the flag inside #2053's early return for a control that leads its row too, with that row key and leads: true. Without the second part, a row-leading control that shares a test id and a row label with a twin can still be taken after it is removed.

šŸ¤– Generated with Claude Code