Skip to content

Commit b2cb280

Browse files
thymikeeclaude
andcommitted
fix(interaction): let the hard deadline win in observe-until
Check the hard deadline before judging an initial value and before the soft budget's expiry, cover a capture that ends exactly at the deadline, bound the earliest-deadline test on both sides, and name the refusal reasons in the command docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 04e4a58 commit b2cb280

4 files changed

Lines changed: 76 additions & 13 deletions

File tree

‎packages/capture-kit/src/observe-until.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,53 @@ describe('observeUntil hard deadline', () => {
249249
assert.equal(observed.polls.length, 1);
250250
});
251251
});
252+
253+
describe('observeUntil hard deadline precedence', () => {
254+
test('a capture that finishes exactly at the deadline is not judged', async () => {
255+
const clock = fakeClock();
256+
let judged = 0;
257+
const observed = await observeUntil({
258+
capture: async () => {
259+
clock.advance(300);
260+
return 'on time?';
261+
},
262+
verdict: () => {
263+
judged += 1;
264+
return { kind: 'done', result: 'accepted' };
265+
},
266+
schedule: SCHEDULE,
267+
deadlineMs: clock.now() + 300,
268+
clock,
269+
});
270+
assert.equal(observed.kind, 'deadline');
271+
assert.equal(judged, 0);
272+
});
273+
274+
test('an initial value is not judged once the deadline has passed', async () => {
275+
const clock = fakeClock();
276+
const observed = await observeUntil({
277+
capture: async () => 2,
278+
verdict: (latest) => ({ kind: 'done', result: latest }),
279+
schedule: SCHEDULE,
280+
initial: 1,
281+
deadlineMs: clock.now(),
282+
clock,
283+
});
284+
assert.equal(observed.kind, 'deadline');
285+
});
286+
287+
test('the deadline wins when a slow verdict spends both the budget and the deadline', async () => {
288+
const clock = fakeClock();
289+
const observed = await observeUntil({
290+
capture: async () => 1,
291+
verdict: () => {
292+
clock.advance(2_000);
293+
return { kind: 'continue' };
294+
},
295+
schedule: SCHEDULE,
296+
deadlineMs: clock.now() + 1_500,
297+
clock,
298+
});
299+
assert.equal(observed.kind, 'deadline');
300+
});
301+
});

‎packages/capture-kit/src/observe-until.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export async function observeUntil<T, R>(
103103
},
104104
};
105105
if (params.initial !== undefined) {
106+
if (remainingMs(loop, 'hard') <= 0) return finish(loop, { kind: 'deadline', last: undefined });
106107
loop.state.observations += 1;
107108
const done = judge(loop, params.initial);
108109
if (done) return done;
@@ -132,7 +133,8 @@ function mustPoll<T, R>({ params, state }: Loop<T, R>): boolean {
132133
async function waitBeforePoll<T, R>(loop: Loop<T, R>): Promise<Observed<T, R> | undefined> {
133134
const { state, clock, params } = loop;
134135
const deadline = () => finish(loop, { kind: 'deadline', last: state.last });
135-
if (state.observations === 0) return remainingMs(loop, 'hard') > 0 ? undefined : deadline();
136+
if (remainingMs(loop, 'hard') <= 0) return deadline();
137+
if (state.observations === 0) return undefined;
136138
const forced = mustPoll(loop);
137139
if (!forced && remainingMs(loop) <= 0) return finish(loop, { kind: 'expired', last: state.last });
138140
const { intervalMs } = params.schedule;

‎packages/host-kit/src/internal/request-cancel.test.ts‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,28 @@ test('a canceled request aborts its signal with the typed canceled error', () =>
402402
});
403403

404404
test('a request runs under the earlier of its execution and result deadlines', async () => {
405-
const resultDeadlineMs = Date.now() + 60_000;
406-
const registration = registerRequestAbort('earliest-deadline', undefined, { resultDeadlineMs });
407-
try {
408-
assert.equal(getRequestDeadlineMs('earliest-deadline'), resultDeadlineMs);
409-
await withRequestDeadline('earliest-deadline', 1_000, async () => {
410-
const deadlineMs = getRequestDeadlineMs('earliest-deadline')!;
411-
assert.ok(deadlineMs < resultDeadlineMs, 'the shorter execution deadline wins');
412-
return { ok: true };
413-
});
414-
} finally {
415-
clearRequestAbortRegistration(registration);
405+
for (const [resultInMs, executionInMs] of [
406+
[60_000, 1_000],
407+
[1_000, 60_000],
408+
] as const) {
409+
const resultDeadlineMs = Date.now() + resultInMs;
410+
const registration = registerRequestAbort('earliest-deadline', undefined, { resultDeadlineMs });
411+
try {
412+
assert.equal(getRequestDeadlineMs('earliest-deadline'), resultDeadlineMs);
413+
const before = Date.now();
414+
await withRequestDeadline('earliest-deadline', executionInMs, async () => {
415+
const deadlineMs = getRequestDeadlineMs('earliest-deadline')!;
416+
const after = Date.now();
417+
if (executionInMs < resultInMs) {
418+
assert.ok(deadlineMs >= before + executionInMs && deadlineMs <= after + executionInMs);
419+
} else {
420+
assert.equal(deadlineMs, resultDeadlineMs);
421+
}
422+
return { ok: true };
423+
});
424+
} finally {
425+
clearRequestAbortRegistration(registration);
426+
}
416427
}
417428
assert.equal(getRequestDeadlineMs('unregistered-request'), undefined);
418429
});

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ If rotation stops repeated iOS presses, inspect the screen before continuing wit
495495
496496
A successful `click` or `press` means the action was sent to where the command found the target. It does not confirm that the element received it or that the screen changed. When the next step depends on it, add `--verify` (reports `changedFromBefore`), add `--settle` (returns the diff once the UI goes quiet), or follow with `wait <selector>`. Before retrying a failed interaction, see [Retry after a failed command](#retry-after-a-failed-command).
497497
498-
A `click` or `press` whose target the daemon resolves to a point (`@ref` or selector) re-checks that target's position before dispatching when the session's previous action animated the screen, and dispatches where it came to rest. When that check cannot confirm a stable position — the target was still moving, or a re-check could not measure it — the command refuses with `target_still_moving` and `dispatched: no`: nothing was dispatched, the point the command resolved is in `error.details.rect`, and where the target was last seen is in `error.details.movedToRect` when that last look measured one. Run `wait <selector>` on the target and retry the same command, or add `--settle` so the action waits for the UI to go quiet. A refused `@ref` press needs a fresh `snapshot -i` and a selector, because the wait retires the ref. Two dispatch paths hand off before the check: a press at raw coordinates has no resolved target to re-check, and a dispatch the backend performs on the element itself — `click @ref` on a backend with element dispatch, or a Maestro non-hittable coordinate fallback — goes to that backend rather than through a resolved point. The re-check stops when the request is canceled or reaches its deadline, and then nothing is dispatched (`dispatched: no`).
498+
A `click` or `press` whose target the daemon resolves to a point (`@ref` or selector) re-checks that target's position before dispatching when the session's previous action animated the screen, and dispatches where it came to rest. When that check cannot confirm a stable position — the target was still moving, or a re-check could not measure it — the command refuses with `target_still_moving` and `dispatched: no`: nothing was dispatched, the point the command resolved is in `error.details.rect`, and where the target was last seen is in `error.details.movedToRect` when that last look measured one. Run `wait <selector>` on the target and retry the same command, or add `--settle` so the action waits for the UI to go quiet. A refused `@ref` press needs a fresh `snapshot -i` and a selector, because the wait retires the ref. Two dispatch paths hand off before the check: a press at raw coordinates has no resolved target to re-check, and a dispatch the backend performs on the element itself — `click @ref` on a backend with element dispatch, or a Maestro non-hittable coordinate fallback — goes to that backend rather than through a resolved point. The re-check stops when the request is canceled or reaches its deadline, and nothing is dispatched (`dispatched: no`): a canceled request fails with `details.reason: request_canceled`, and a request out of time with `details.reason: daemon_request_timeout`.
499499
500500
### Text entry
501501

0 commit comments

Comments
 (0)