fix(cache): preserve point targets while replay waits

This commit is contained in:
michal-e2e
2026-11-28 23:34:40 +01:00
parent fff0cc3b4e
commit ff9b79129c
7 changed files with 133 additions and 46 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@e2e-dev/core': patch
---
Cached coordinate drags wait for a drop target that is late or still hidden, including when the recorded point picks the source out of same-named controls. Replay stops if one of those controls disappears meanwhile. A cached tap, hover, or drag at a point recorded inside a control acts only where that control shows: a control that is listed but hidden, or that lies outside the viewport, is waited for instead of acted on. A point recorded outside any control replays as before.
Cached taps, hovers, and drags wait until the pixel they will send falls inside the recorded control. During a drag, replay can wait for either endpoint to appear. If a recorded point distinguishes same-named controls, replay stops when one of them leaves the visible area during the wait. Points recorded outside controls replay as before.
+2 -2
View File
@@ -140,8 +140,8 @@ model calls:
ID, and surrounding context. e2e waits for controls to appear and stops
if a match is ambiguous. A tap, hover, or drag at a point recorded inside
a control waits for that control while it is late, hidden, or outside the
viewport. A drag whose source the recorded point has to pick out of
same-named controls stops if one of them disappears meanwhile.
viewport. If a recorded point distinguishes same-named controls at either
end of a drag, replay stops when one leaves the visible area while the other end loads.
3. **Check the end state** (final route plus the controls that appeared,
disappeared, or changed state).
4. **Finish with zero model calls**, or hand off to the live agent with a
+58 -34
View File
@@ -26,7 +26,7 @@ import {
import type { SemanticNode, ViewportPoint } from '../engine/surface.ts';
import type { RedactedNode } from './observation.ts';
import { hasCause } from '../internal/errors.ts';
import { containsPoint, type Box } from '../internal/geometry.ts';
import { clampToViewport, containsPoint, type Box } from '../internal/geometry.ts';
import { sleep } from '../internal/time.ts';
import type { ScrollDirection } from '../types.ts';
import {
@@ -454,9 +454,15 @@ async function relocate(
): Promise<Relocated> {
let last: Relocated = { kind: 'failed', failure: 'target-not-found' };
let refused = false;
let waitingForBox = false;
const settled = await pollSettled(host, (screen): Relocated | undefined => {
const result = relocateDescriptor(descriptor, screen.nodes);
if (result.kind === 'failed') {
// A new look-alike cannot stand in for the unique control we saw without a usable box.
if (waitingForBox && result.failure === 'target-ambiguous') {
last = result;
return descriptor.position === undefined ? last : undefined;
}
// Keep the refusal while looking for the recorded test id. Later looks
// can show more twins or none, and neither justifies a fallback.
refused ||= result.failure === 'target-ambiguous' && result.twinLeft === true;
@@ -468,6 +474,7 @@ async function relocate(
const node = screen.nodes.get(result.id);
const found = node === undefined ? undefined : { ...result, node, screen };
if (found !== undefined && usable(found)) return found;
waitingForBox ||= found !== undefined;
// The look-alikes of an earlier look no longer stand for a node this one found.
if (!refused) last = { kind: 'failed', failure: 'target-not-found' };
return undefined;
@@ -477,7 +484,7 @@ async function relocate(
/**
* Both ends of a drag as one look found them, and whether an end refused as
* a twin is still not found on it. A source refused because its group lost
* a twin is still not found on it. An endpoint refused because its group lost
* a control leaves its slot empty.
*/
interface Ends {
@@ -500,12 +507,12 @@ interface Ends {
* has a place in what a look found of it (`placeWithin`). An end listed
* before its point can be placed (mounted hidden, with no box yet, or
* outside the viewport) is still loading, whichever look it turns up on,
* and is waited for like one that is not listed. A source whose look-alikes
* its point settles on this look lets such a destination wait. If that
* source loses a candidate during the wait, or is re-found where its point
* and is waited for like one that is not listed. An endpoint its point picks
* out of look-alikes can wait for the other end. If its group loses a candidate
* during the wait, or it is re-found where its point
* cannot be placed, the drag stops.
*
* That guard is a count of the source's visible look-alikes, compared with
* That guard is a count of the endpoint's visible look-alikes, compared with
* the look before, as the `twinLeft` refusal is a count: an engine may
* renumber its nodes between looks, so nothing else tells them apart. It
* stops on purpose when the group grows and drops back (2, 3, 2) and when
@@ -524,8 +531,9 @@ async function relocateEnds(
): Promise<Ends | undefined> {
const refused = new Set<TraceTargetDescriptor>();
let latest: Ends | undefined;
let countSources: ReturnType<typeof relocationGroupCounter>;
let sourceCount = 0;
const guardSource = pointGroupGuard(source, places?.source);
const guardDestination = pointGroupGuard(destination, places?.destination);
const waitingForBox = new Set<TraceTargetDescriptor>();
const judge = (descriptor: TraceTargetDescriptor, screen: SemanticScreen): { readonly relocated: Relocated; readonly wait: boolean } => {
const result = relocateDescriptor(descriptor, screen.nodes);
if (result.kind === 'found') {
@@ -533,6 +541,9 @@ async function relocateEnds(
if (node === undefined) return { relocated: { kind: 'failed', failure: 'target-not-found' }, wait: true };
return { relocated: { ...result, node, screen }, wait: false };
}
if (result.failure === 'target-ambiguous' && waitingForBox.has(descriptor)) {
return { relocated: result, wait: descriptor.position !== undefined };
}
if (result.failure === 'target-ambiguous' && result.twinLeft === true) refused.add(descriptor);
const marked = refused.has(descriptor) && result.failure === 'target-ambiguous' ? { ...result, twinLeft: true as const } : result;
return { relocated: marked.failure === 'target-ambiguous' ? { ...marked, screen } : marked, wait: retryable(marked, descriptor) };
@@ -544,37 +555,50 @@ async function relocateEnds(
const listedDestination = destination === undefined ? undefined : judge(destination, screen);
const from = unplaced(listedSource, places?.source) ? STILL_LOADING : listedSource;
const to = unplaced(listedDestination, places?.destination) ? STILL_LOADING : listedDestination;
if (countSources !== undefined) {
const count = countSources(screen.nodes);
if (count < sourceCount || unplaced(listedSource, places?.source)) {
// The source slot stays empty: `refused` names the failure, and a
// source left in the slot would be placed and dragged.
return { screen, ends: [undefined, to?.relocated], refused: true };
}
sourceCount = count;
if (source !== undefined && from === STILL_LOADING) waitingForBox.add(source);
if (destination !== undefined && to === STILL_LOADING) waitingForBox.add(destination);
const lostIdentity = (descriptor: TraceTargetDescriptor | undefined, judged: typeof from) =>
descriptor !== undefined && descriptor.position === undefined && waitingForBox.has(descriptor)
&& judged?.relocated.kind === 'failed' && judged.relocated.failure === 'target-ambiguous';
const waitingForDestination = from?.relocated.kind === 'failed' && to?.wait === true
&& places?.source(from.relocated) === true && !places.destination(to.relocated);
const wait = from?.relocated.kind === 'failed' ? from.wait || waitingForDestination : to?.wait === true;
if (lostIdentity(source, from) || lostIdentity(destination, to)
|| guardSource(screen, from?.relocated, from === STILL_LOADING, wait)
|| guardDestination(screen, to?.relocated, to === STILL_LOADING, wait)) {
return { screen, ends: [undefined, undefined], refused: true };
}
const unresolved = (descriptor: TraceTargetDescriptor | undefined, judged: typeof from) =>
descriptor !== undefined && refused.has(descriptor) && judged?.relocated.kind === 'failed';
latest = { screen, ends: [from?.relocated, to?.relocated], refused: unresolved(source, from) || unresolved(destination, to) };
if (from?.relocated.kind === 'failed') {
const waitingForDestination = to?.wait === true && places?.source(from.relocated) === true && !places.destination(to.relocated);
if (from.wait || waitingForDestination) {
// A recorded position already says which twin the source is once the
// count catches up (`retryable`), so its group is not held to a count.
if (waitingForDestination && countSources === undefined && source !== undefined
&& source.position === undefined && from.relocated.failure === 'target-ambiguous') {
countSources = relocationGroupCounter(source, screen.nodes, (node) => visibleBox(node) !== undefined);
sourceCount = countSources?.(screen.nodes) ?? 0;
}
return undefined;
}
return latest;
}
return to?.wait === true ? undefined : latest;
return wait ? undefined : latest;
}, look);
return settled ?? latest;
}
/** Once a point distinguishes twins, a later look must still contain the group it picked from. */
function pointGroupGuard(descriptor: TraceTargetDescriptor | undefined, places: ((relocated: Relocated) => boolean) | undefined) {
let countGroup: ReturnType<typeof relocationGroupCounter>;
let previousCount = 0;
return (screen: SemanticScreen, end: Relocated | undefined, unplaced: boolean, waiting: boolean): boolean => {
if (countGroup === undefined && waiting && descriptor !== undefined && descriptor.position === undefined
&& end?.kind === 'failed' && end.failure === 'target-ambiguous' && places?.(end)) {
countGroup = relocationGroupCounter(descriptor, screen.nodes);
}
if (countGroup === undefined) return false;
const count = countGroup(screen.nodes, (node) => {
const box = visibleBox(node);
if (box === undefined) return false;
// The first whole pixel inside the box must still be available in this viewport.
const first = { x: Math.ceil(box.x), y: Math.ceil(box.y) };
return containsPoint(box, clampToViewport(first, screen.viewport));
});
const lost = unplaced || count < previousCount;
previousCount = count;
return lost;
};
}
/** An end a look lists but cannot place a point in yet: not found on this look, and worth another. */
const STILL_LOADING = { relocated: { kind: 'failed', failure: 'target-not-found' }, wait: true } as const;
@@ -609,7 +633,7 @@ type Found = Extract<RelocationResult, { kind: 'found' }> & { readonly node: Red
type Relocated =
| Found
| Extract<RelocationResult, { failure: 'target-not-found' }>
| (Extract<RelocationResult, { failure: 'target-ambiguous' }> & { readonly screen: SemanticScreen });
| (Extract<RelocationResult, { failure: 'target-ambiguous' }> & { readonly screen?: SemanticScreen });
/**
* Where one recorded bare point lands on the live screen: inside its
@@ -703,7 +727,7 @@ function placeWithin(
): ViewportPoint | undefined {
const inside = (box: Box, live: TraceViewport): ViewportPoint | undefined => {
const at = { x: box.x + planned.fx * box.width, y: box.y + planned.fy * box.height };
const kept = { x: Math.min(Math.max(at.x, 0), Math.max(0, live.width - 1)), y: Math.min(Math.max(at.y, 0), Math.max(0, live.height - 1)) };
const kept = clampToViewport(at, live);
return containsPoint(box, kept) ? at : undefined;
};
if (relocated.kind === 'found') {
@@ -712,7 +736,7 @@ function placeWithin(
}
// A named match refused as a twin of the recorded node is no choice to
// settle: the point may sit on the twin.
if (relocated.failure !== 'target-ambiguous' || relocated.twinLeft === true) return undefined;
if (relocated.failure !== 'target-ambiguous' || relocated.twinLeft === true || relocated.screen === undefined) return undefined;
const { viewport, nodes } = relocated.screen;
if (viewport.width !== planned.viewport.width || viewport.height !== planned.viewport.height) return undefined;
const containing = relocated.candidates
+2 -3
View File
@@ -316,11 +316,10 @@ function tierMatches(tier: DescriptorTier, candidates: readonly DescribedNode[])
export function relocationGroupCounter(
descriptor: TraceTargetDescriptor,
nodes: ReadonlyMap<string, RedactedNode>,
eligible: (node: RedactedNode | undefined) => boolean,
): ((current: ReadonlyMap<string, RedactedNode>) => number) | undefined {
): ((current: ReadonlyMap<string, RedactedNode>, eligible: (node: RedactedNode | undefined) => boolean) => number) | undefined {
const { tier } = matchingIds(descriptor, nodes);
if (tier === undefined) return undefined;
return (current) => tierMatches(tier, candidatesWithin(descriptor.within, current)).filter((id) => eligible(current.get(id))).length;
return (current, eligible) => tierMatches(tier, candidatesWithin(descriptor.within, current)).filter((id) => eligible(current.get(id))).length;
}
/**
@@ -549,6 +549,55 @@ describe('replayTrace: bare-point taps', () => {
expect(points).toEqual([{ x: 300, y: 100 }, { x: 1330, y: 100 }]);
});
it('waits when rounding the recorded position would send input outside the control', async () => {
const within = { target: { role: 'img', name: 'Map' }, fx: 0.99, fy: 0.5 };
const map: SemanticNode = { ref: { id: 'm', revision: 'r1' }, role: 'img', name: 'Map' };
const points: unknown[] = [];
const host = makeHost({ onAction: (name, detail) => void (name === 'tapAt' && points.push(detail)) });
host.capture = async () => screen([{ ...map, rect: { x: 200, y: 0, width: host.looks.length === 1 ? 20 : 100, height: 100 } }]);
expect(await replayTrace(host, trace([{ ...pin, within }]))).toMatchObject({ completed: true, executed: 1 });
expect(points).toEqual([{ x: 299, y: 50 }]);
});
it('checks the dispatched pixel against the current viewport after a resize', async () => {
const map: SemanticNode = { ref: { id: 'm', revision: 'r1' }, role: 'img', name: 'Map', rect: { x: 500, y: 0, width: 100, height: 100 } };
const host = makeHost({ nodes: [map], viewport: { width: 390, height: 844 } });
const action = { ...pin, within: { target: { role: 'img', name: 'Map' }, fx: 0.5, fy: 0.5 } };
expect(await replayTrace(host, trace([action]))).toMatchObject({ completed: false, executed: 0, stopReason: 'target-not-found' });
expect(host.calls).not.toContain('tapAt');
});
it('replays a recorded fraction rounded to one when the dispatched pixel still lies inside the control', async () => {
const map: SemanticNode = { ref: { id: 'm', revision: 'r1' }, role: 'img', name: 'Map', rect: { x: 200, y: 0, width: 200.015625, height: 100 } };
const host = makeHost({ nodes: [map] });
const action = { ...pin, point: { x: 400, y: 50 }, within: { target: { role: 'img', name: 'Map' }, fx: 1, fy: 0.5 } };
expect(await replayTrace(host, trace([action]))).toMatchObject({ completed: true, executed: 1 });
expect(host.calls).toContain('tapAt');
});
it('waits for a positioned target to be identifiable again after it was found hidden', async () => {
const map = (id: string, x: number): SemanticNode => ({ ref: { id, revision: 'r1' }, role: 'img', name: 'Map', rect: { x, y: 0, width: 200, height: 120 } });
const original = map('original', 200);
const twin = map('twin', 500);
const host = makeHost({});
host.capture = async () => screen(host.looks.length === 1
? [{ ...original, states: { hidden: true } }, twin]
: [map('new', 200), map('original', 800), twin]);
const action = { ...pin, within: { target: { role: 'img', name: 'Map', position: { index: 0, of: 2 } }, fx: 0.5, fy: 0.5 } };
expect(await replayTrace(host, trace([action]))).toMatchObject({ completed: false, executed: 0, stopReason: 'target-ambiguous' });
expect(host.calls).not.toContain('tapAt');
});
it('refuses a newly visible twin when the unique target it was waiting for still has no box', async () => {
const map: SemanticNode = { ref: { id: 'm', revision: 'r1' }, role: 'img', name: 'Map' };
const twin: SemanticNode = { ...map, ref: { id: 'other', revision: 'r1' }, rect: { x: 200, y: 0, width: 200, height: 120 } };
const host = makeHost({});
host.capture = async () => screen(host.looks.length === 1 ? [map] : [map, twin]);
const action = { ...pin, within: { target: { role: 'img', name: 'Map' }, fx: 0.5, fy: 0.5 } };
expect(await replayTrace(host, trace([action]))).toMatchObject({ completed: false, executed: 0, stopReason: 'target-ambiguous' });
expect(host.calls).not.toContain('tapAt');
});
it('never places the point from an earlier look once the node turns up hidden', async () => {
// Recorded as the first of two maps. Three show at first, the point inside the first; then two are left and the first is hidden.
const within = { target: { role: 'img', name: 'Map', position: { index: 0, of: 2 } }, fx: 0.5, fy: 0.5 };
@@ -933,6 +982,21 @@ describe('replayTrace: bare-point drags', () => {
looks: [[card, at(lane, 1300)], [card, lane]],
dragsFrom: { x: 100, y: 50 },
},
{
label: 'refuses a twin under the source point after the original scrolls out of view',
looks: [[card, twin], [at(card, 1300), twinInPlace, lane]],
stops: 'target-ambiguous',
},
{
label: 'refuses the surviving drop target while the source gains a box',
looks: [[hidden(card), lane, at(as(lane, 'other'), 750)], [card, at(as(lane, 'other'), 750)]],
stops: 'target-ambiguous',
},
{
label: 'refuses a new drop target under the point while the original remains hidden',
looks: [[card, hidden(lane)], [card, hidden(lane), as(lane, 'other')]],
stops: 'target-ambiguous',
},
{
label: 'stops without a drag when the destination stays outside the viewport',
looks: [[card, twin, at(lane, 1300)]],
+3 -3
View File
@@ -239,9 +239,9 @@ takes over from the current screen when the app no longer matches.
- A step that changes nothing on screen or in the route records nothing
and runs live every run.
- A cached tap, hover, or drag at a point recorded inside a control waits
for that control while it is late, hidden, or outside the viewport. A drag
whose source the recorded point has to pick out of same-named controls
stops if one of them disappears meanwhile.
for that control while it is late, hidden, or outside the viewport. If a
recorded point distinguishes same-named controls at either end of a drag,
replay stops when one leaves the visible area while the other end loads.
- While you fix a test, keep the cache on so confirmed goals replay (topic
`debugging`, "Rerunning a test while you fix it"). Use `--no-cache` to
rule out a stale replay (topic `debugging`, "Tools") and for one live run
+3 -3
View File
@@ -239,9 +239,9 @@ takes over from the current screen when the app no longer matches.
- A step that changes nothing on screen or in the route records nothing
and runs live every run.
- A cached tap, hover, or drag at a point recorded inside a control waits
for that control while it is late, hidden, or outside the viewport. A drag
whose source the recorded point has to pick out of same-named controls
stops if one of them disappears meanwhile.
for that control while it is late, hidden, or outside the viewport. If a
recorded point distinguishes same-named controls at either end of a drag,
replay stops when one leaves the visible area while the other end loads.
- While you fix a test, keep the cache on so confirmed goals replay (topic
`debugging`, "Rerunning a test while you fix it"). Use `--no-cache` to
rule out a stale replay (topic `debugging`, "Tools") and for one live run