Skip to content

Commit d974424

Browse files
thymikeeclaude
andcommitted
fix(interaction): arm the deadline timer lazily and explain an early refusal
Only a look arms the request-deadline timer, so an idle press builds no timer. A deadline refusal carries the request id and the remaining time, and its hint says the remaining time cannot cover the wait. A daemon test pins the deadlineAtMs hand-off to press and click. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2836365 commit d974424

3 files changed

Lines changed: 100 additions & 24 deletions

File tree

‎packages/command-runtime/src/commands/interaction/runtime/pre-dispatch-admission.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert/strict';
2-
import { test } from 'vitest';
2+
import { test, vi } from 'vitest';
33
import type { AgentDeviceBackend } from '@agent-device/contracts/backend';
44
import type { Point } from '@agent-device/kernel/snapshot';
55
import type { SnapshotState } from '@agent-device/contracts/snapshot-model';
@@ -129,7 +129,8 @@ for (const history of [
129129
{ name: 'a mutation outside the window', baselineAgeMs: 5_000 },
130130
{ name: 'a target the previous action left in place', baselineAgeMs: 100, stationary: true },
131131
] as const) {
132-
test(`an idle press after ${history.name} pays no extra capture or sleep`, async () => {
132+
test(`an idle press after ${history.name} pays no extra capture, sleep or timer`, async () => {
133+
const timers = vi.spyOn(AbortSignal, 'timeout');
133134
for (const target of ['selector', 'ref'] as const) {
134135
const harness = createAdmissionDevice({
135136
platform: 'android',
@@ -140,14 +141,16 @@ for (const history of [
140141

141142
await harness.device.interactions.press(
142143
target === 'selector' ? selector('label="Slide target"') : ref('@e2'),
143-
{ session: 'default' },
144+
{ session: 'default', deadlineAtMs: Date.now() + 60_000 },
144145
);
145146

146147
assert.deepEqual(harness.taps, [{ x: 70, y: BUTTON_Y + 25 }], target);
147148
// A selector spends its resolution capture; a ref resolves against its stored tree.
148149
assert.equal(harness.captureStarts.length, target === 'selector' ? 1 : 0, target);
149150
assert.deepEqual(harness.sleeps, [], target);
150151
}
152+
assert.equal(timers.mock.calls.length, 0, 'no deadline timer on the accept path');
153+
timers.mockRestore();
151154
});
152155
}
153156

‎packages/command-runtime/src/commands/interaction/runtime/pre-dispatch-admission.ts‎

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,29 @@ type AdmissionBudget = Readonly<{
6363
deadlineMs: number;
6464
clock: ObservationClock;
6565
/**
66-
* Runs one awaited read with the budget checked before and after it. The read receives a signal
67-
* that also aborts at the request deadline, so a read in flight ends there and refuses.
66+
* Runs one awaited read with the budget checked before and after it. With `until: 'deadline'`,
67+
* the read receives a signal that also aborts at the request deadline, so a read in flight ends
68+
* there and refuses.
6869
*/
69-
read<T>(read: (signal: AbortSignal | undefined) => Promise<T>): Promise<T>;
70+
read<T>(read: (signal: AbortSignal | undefined) => Promise<T>, until?: 'deadline'): Promise<T>;
71+
/** The refusal of a wait the remaining request time cannot cover, before any send. */
72+
deadlineRefusal(): AppError;
7073
}>;
7174

7275
function requestBudget(runtime: AgentDeviceRuntime, options: CommandContext): AdmissionBudget {
7376
const signal = options.signal ?? runtime.signal;
74-
// A timer arms only within its 32-bit range; a later, infinite or absent deadline is still held
75-
// by the clock checks.
76-
const timerRemainingMs = Math.max(0, Math.ceil((options.deadlineAtMs ?? Infinity) - Date.now()));
77-
const deadlineSignal =
78-
timerRemainingMs <= 2 ** 31 - 1 ? AbortSignal.timeout(timerRemainingMs) : undefined;
79-
const readSignal =
80-
signal && deadlineSignal
81-
? AbortSignal.any([signal, deadlineSignal])
82-
: (signal ?? deadlineSignal);
77+
// The first read that runs until the deadline arms its timer, so an idle press builds none. The
78+
// timer arms only within its 32-bit range; a later, infinite or absent deadline is still held by
79+
// the clock checks.
80+
let deadlineSignals:
81+
| Readonly<{ read: AbortSignal | undefined; deadline: AbortSignal | undefined }>
82+
| undefined;
83+
const armDeadline = () => {
84+
const remainingMs = Math.max(0, Math.ceil((options.deadlineAtMs ?? Infinity) - Date.now()));
85+
const deadline = remainingMs <= 2 ** 31 - 1 ? AbortSignal.timeout(remainingMs) : undefined;
86+
const read = signal && deadline ? AbortSignal.any([signal, deadline]) : (signal ?? deadline);
87+
return { read, deadline };
88+
};
8389
const budget: AdmissionBudget = {
8490
signal,
8591
deadlineMs: now(runtime) + ((options.deadlineAtMs ?? Infinity) - Date.now()),
@@ -90,25 +96,39 @@ function requestBudget(runtime: AgentDeviceRuntime, options: CommandContext): Ad
9096
await sleep(runtime, ms, signal);
9197
},
9298
},
93-
read: async (read) => {
99+
read: async (read, until) => {
94100
assertWithinBudget(budget);
101+
const { read: readSignal, deadline } = until
102+
? (deadlineSignals ??= armDeadline())
103+
: { read: signal, deadline: undefined };
95104
let value;
96105
try {
97106
value = await read(readSignal);
98107
} catch (error) {
99-
if (readSignal?.aborted) assertWithinBudget(budget, deadlineSignal?.aborted || undefined);
108+
if (readSignal?.aborted) assertWithinBudget(budget, deadline?.aborted || undefined);
100109
throw error;
101110
}
102-
assertWithinBudget(budget, deadlineSignal?.aborted || undefined);
111+
assertWithinBudget(budget, deadline?.aborted || undefined);
103112
return value;
104113
},
114+
deadlineRefusal: () => {
115+
const remainingMs = Math.max(0, Math.round(budget.deadlineMs - budget.clock.now()));
116+
return discloseDispatch(
117+
createRequestDeadlineError({
118+
...(options.requestId ? { requestId: options.requestId } : {}),
119+
remainingMs,
120+
hint: `The request had ${remainingMs} ms left, which cannot cover the wait for the target to come to rest, so nothing was dispatched. Retry once the UI is idle, or allow the request more time.`,
121+
}),
122+
'no',
123+
);
124+
},
105125
};
106126
return budget;
107127
}
108128

109129
/**
110-
* Refuses before any send once the request is canceled, or once its deadline passed or the wait
111-
* left no time to reach it (`deadlineReached`).
130+
* Refuses before any send once the request is canceled, or once its deadline passed or the
131+
* remaining time cannot cover the wait (`deadlineReached`).
112132
*/
113133
function assertWithinBudget(
114134
budget: AdmissionBudget,
@@ -119,7 +139,7 @@ function assertWithinBudget(
119139
const details = signal.reason instanceof AppError ? signal.reason.details : undefined;
120140
throw discloseDispatch(createRequestCanceledError(details, signal.reason), 'no');
121141
}
122-
if (deadlineReached) throw discloseDispatch(createRequestDeadlineError(), 'no');
142+
if (deadlineReached) throw budget.deadlineRefusal();
123143
}
124144

125145
/**
@@ -145,7 +165,7 @@ async function awaitRest(
145165
Exclude<ReturnType<typeof judgeMotionLook>, { kind: 'wait' }>
146166
>({
147167
capture: () =>
148-
budget.read((signal) => reObserveTarget(runtime, { ...options, signal }, params)),
168+
budget.read((signal) => reObserveTarget(runtime, { ...options, signal }, params), 'deadline'),
149169
verdict: (look, previous) => {
150170
const verdict = judgeMotionLook(look, previous, restingSinceMs, wait.policy);
151171
if (verdict.kind !== 'wait') return { kind: 'done', result: verdict };
@@ -170,7 +190,7 @@ async function awaitRest(
170190
throw observed.error;
171191
case 'deadline':
172192
assertWithinBudget(budget);
173-
throw discloseDispatch(createRequestDeadlineError(), 'no');
193+
throw budget.deadlineRefusal();
174194
case 'expired':
175195
throw motionRefusal(
176196
resolved,

‎packages/daemon/src/execution/interaction/internal/interaction-touch-press.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { test, expect, beforeEach } from 'vitest';
1+
import { test, expect, beforeEach, vi } from 'vitest';
2+
import type { PressCommandResult } from '@agent-device/contracts/interaction';
23
import { attachRefs } from '@agent-device/kernel/snapshot';
4+
import {
5+
clearRequestAbortRegistration,
6+
registerRequestAbort,
7+
} from '@agent-device/host-kit/request';
38
import { makeIosSession } from '../../../__tests__/support/session-factories.ts';
49
import { makeSessionStore } from '../../../__tests__/support/store-factory.ts';
510
import {
611
installInteractorSnapshotResolution,
712
mockInteractorSnapshot,
813
} from '../../../__tests__/support/interactor-resolution-fixture.ts';
914
import { touchCreateCommandSurface } from '../../../__tests__/support/touch-command-surface-fake.ts';
15+
import { commandSurfaceWithMembers } from '../../../__tests__/support/command-surface-override-fake.ts';
1016
import { runFindInternalClick } from '../../../__tests__/support/touch-interaction-runner.ts';
1117
import {
1218
getRuntimeBindings,
@@ -199,3 +205,50 @@ test('a read right after a press captures the post-tap screen instead of reusing
199205
expect(read).toMatchObject({ ok: true, data: { text: 'Step 2' } });
200206
expect(mockInteractorSnapshot).toHaveBeenCalledTimes(2);
201207
});
208+
209+
test('press and click hand the earliest request deadline to pre-dispatch admission', async () => {
210+
const requestId = 'press-deadline-handoff';
211+
const resultDeadlineMs = Date.now() + 30_000;
212+
const registration = registerRequestAbort(requestId, undefined, { resultDeadlineMs });
213+
try {
214+
const handedOff: Array<number | undefined> = [];
215+
const answer = (options: { deadlineAtMs?: number } | undefined) => {
216+
handedOff.push(options?.deadlineAtMs);
217+
return { kind: 'point', point: { x: 10, y: 20 } } as unknown as PressCommandResult;
218+
};
219+
const createCommandSurface = commandSurfaceWithMembers(() => ({
220+
interactions: {
221+
press: vi.fn(async (_target, options) => answer(options)),
222+
click: vi.fn(async (_target, options) => answer(options)),
223+
},
224+
}));
225+
const sessionStore = makeSessionStore();
226+
const sessionName = 'press-deadline-handoff';
227+
sessionStore.publish(
228+
sessionName,
229+
makeIosSession(sessionName, { appBundleId: 'com.example.app' }),
230+
);
231+
232+
for (const command of ['press', 'click']) {
233+
await handleInteractionCommands({
234+
createCommandSurface,
235+
req: {
236+
token: 't',
237+
session: sessionName,
238+
command,
239+
positionals: ['10', '20'],
240+
flags: {},
241+
meta: { requestId },
242+
},
243+
sessionName,
244+
sessionStore,
245+
contextFromFlags,
246+
...getRuntimeBindings(),
247+
});
248+
}
249+
250+
expect(handedOff).toEqual([resultDeadlineMs, resultDeadlineMs]);
251+
} finally {
252+
clearRequestAbortRegistration(registration);
253+
}
254+
});

0 commit comments

Comments
 (0)