Skip to content

Commit 58eeb12

Browse files
author
Cameron Chisholm
committed
fix(gesture): propagate cancellation without recording partial tap series
1 parent c6e86e2 commit 58eeb12

11 files changed

Lines changed: 238 additions & 209 deletions

File tree

‎packages/contracts/src/daemon-command-surface.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,6 @@ export type DaemonCoordinateGestureResult = Readonly<{
249249
kind: GestureIntent;
250250
durationMs: number;
251251
pointerCount: GestureContactCount;
252-
/** A tap reports its contacts and how many of its repetitions ran. */
253-
tapSeries?: Readonly<{
254-
points: Point[];
255-
holdMs: number;
256-
count: number;
257-
pauseMs: number;
258-
completed: number;
259-
}>;
260252
from: Point;
261253
to: Point;
262254
backendResult?: Record<string, unknown>;

‎packages/contracts/src/gesture-normalization.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ export function normalizePublicGesture(input: GesturePayload): NormalizedPublicG
485485
intent: 'tap',
486486
points: input.points,
487487
holdMs: input.holdMs,
488-
count: input.count,
489-
pauseMs: input.pauseMs,
490488
},
491489
};
492490
case 'drag':

‎packages/contracts/src/gesture-plan-types.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ export type GestureSemanticInput =
6464
intent: 'tap';
6565
points: readonly Point[];
6666
holdMs?: number;
67-
/** Repeats the same stationary contacts; the plan is built once for the whole series. */
68-
count?: number;
69-
pauseMs?: number;
7067
};
7168

7269
export type GestureCommandInput = GestureSemanticInput | DragGestureInput;

‎packages/daemon/src/execution/interaction/internal/__tests__/interaction-gesture-response.test.ts‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,3 @@ test('gesture response builder preserves the dual-target drag disclosure and tim
3030
timing: { executionProfile: 'timed-pan', gestureDurationMs: 1_300 },
3131
});
3232
});
33-
34-
test('a tap response lists its contacts and how many repetitions completed', () => {
35-
const points = [
36-
{ x: 80, y: 300 },
37-
{ x: 200, y: 450 },
38-
{ x: 310, y: 600 },
39-
];
40-
expect(
41-
gestureResponseData({
42-
kind: 'tap',
43-
durationMs: 50,
44-
pointerCount: 3,
45-
from: { x: 196.66666666666666, y: 450 },
46-
to: { x: 196.66666666666666, y: 450 },
47-
tapSeries: { points, holdMs: 50, count: 4, pauseMs: 100, completed: 4 },
48-
message: 'Tapped 3 points 4 times',
49-
}),
50-
).toEqual({
51-
kind: 'tap',
52-
durationMs: 50,
53-
pointerCount: 3,
54-
from: { x: 196.66666666666666, y: 450 },
55-
to: { x: 196.66666666666666, y: 450 },
56-
points,
57-
holdMs: 50,
58-
count: 4,
59-
pauseMs: 100,
60-
completed: 4,
61-
message: 'Tapped 3 points 4 times',
62-
});
63-
});

‎packages/daemon/src/execution/interaction/internal/interaction-gesture-response.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ type GestureResponseResult = {
1010
pointerCount: GestureContactCount;
1111
from: Point;
1212
to: Point;
13-
tapSeries?: Record<string, unknown>;
1413
targets?: unknown;
1514
backendResult?: Record<string, unknown>;
1615
message?: string;
@@ -30,7 +29,6 @@ export function gestureResponseData(
3029
from: result.from,
3130
to: result.to,
3231
...(result.kind === 'drag' ? { targets: result.targets } : {}),
33-
...(result.tapSeries ?? {}),
3432
...(result.backendResult ?? {}),
3533
...extra,
3634
...(executionProfile

‎packages/daemon/src/execution/interaction/internal/interaction-gesture-tap.test.ts‎

Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,62 @@
1-
import { beforeEach, expect, test } from 'vitest';
1+
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
22
import {
33
makeIosSession,
44
authoringPublication,
55
} from '../../../__tests__/support/session-factories.ts';
66
import { makeSessionStore } from '../../../__tests__/support/store-factory.ts';
77
import { daemonPlatformServicesFixture } from '../../../__tests__/support/platform-services-fixture.ts';
88
import { touchCreateCommandSurface } from '../../../__tests__/support/touch-command-surface-fake.ts';
9+
import {
10+
registerRequestAbort,
11+
markRequestCanceled,
12+
clearRequestCanceled,
13+
} from '@agent-device/host-kit/request';
914
import { handleInteractionCommands } from '../index.ts';
1015
import { gestureRuntimeBindingsFixture } from './__tests__/gesture-runtime-bindings.fixtures.ts';
1116

1217
let gestures = gestureRuntimeBindingsFixture();
18+
const requestId = 'tap-series-cancel';
19+
const perform = vi.fn(async () => ({
20+
kind: 'tap' as const,
21+
pointerCount: 2 as const,
22+
durationMs: 40,
23+
from: { x: 140, y: 375 },
24+
to: { x: 140, y: 375 },
25+
message: 'Tapped 2 points',
26+
}));
1327

1428
beforeEach(() => {
1529
gestures = gestureRuntimeBindingsFixture();
30+
perform.mockClear();
31+
registerRequestAbort(requestId);
32+
});
33+
34+
afterEach(() => {
35+
clearRequestCanceled(requestId);
36+
vi.useRealTimers();
1637
});
1738

18-
async function runGesture(input: Record<string, unknown>) {
39+
async function runGesture(
40+
input: Record<string, unknown>,
41+
command: 'gesture' | 'swipe' = 'gesture',
42+
) {
1943
const sessionStore = makeSessionStore();
2044
const session = makeIosSession('tap', {
2145
appBundleId: 'com.example.tap',
2246
scriptPublication: authoringPublication('armed'),
2347
});
2448
sessionStore.publish('tap', session);
2549
const response = await handleInteractionCommands({
26-
createCommandSurface: touchCreateCommandSurface,
50+
createCommandSurface: async (config) => {
51+
const surface = await touchCreateCommandSurface(config);
52+
return { ...surface, interactions: { ...surface.interactions, gesture: perform } };
53+
},
2754
platformServices: daemonPlatformServicesFixture(),
2855
req: {
2956
token: 't',
3057
session: 'tap',
31-
command: 'gesture',
58+
command,
59+
meta: { requestId },
3260
positionals: [],
3361
flags: {},
3462
input,
@@ -40,7 +68,7 @@ async function runGesture(input: Record<string, unknown>) {
4068
inspectFacts: gestures.inspectFacts,
4169
bindDevice: gestures.bindDevice,
4270
});
43-
return { response, action: session.actions[0] };
71+
return { response, action: session.actions[0], actions: session.actions };
4472
}
4573

4674
test('a structured tap series records its contacts and series flags for .ad replay', async () => {
@@ -55,7 +83,38 @@ test('a structured tap series records its contacts and series flags for .ad repl
5583
pauseMs: 10,
5684
});
5785

58-
expect(response?.ok).toBe(true);
86+
expect(response).toMatchObject({
87+
ok: true,
88+
data: {
89+
kind: 'tap',
90+
points: [
91+
{ x: 80, y: 300 },
92+
{ x: 200, y: 450 },
93+
],
94+
holdMs: 40,
95+
count: 2,
96+
pauseMs: 10,
97+
message: 'Tapped 2 points 2 times',
98+
},
99+
});
100+
expect(response && 'data' in response ? response.data : undefined).not.toHaveProperty(
101+
'completed',
102+
);
103+
expect(perform).toHaveBeenCalledTimes(2);
104+
expect(perform).toHaveBeenNthCalledWith(
105+
2,
106+
expect.objectContaining({
107+
gesture: {
108+
intent: 'tap',
109+
points: [
110+
{ x: 80, y: 300 },
111+
{ x: 200, y: 450 },
112+
],
113+
holdMs: 40,
114+
},
115+
}),
116+
);
117+
expect(gestures.bindDevice).toHaveBeenCalledTimes(1);
59118
expect(action).toMatchObject({
60119
command: 'gesture',
61120
positionals: ['tap', '80', '300', '200', '450'],
@@ -76,3 +135,69 @@ test('a tap without series inputs records no series flags', async () => {
76135
expect(action?.flags).not.toHaveProperty('holdMs');
77136
expect(action?.flags).not.toHaveProperty('pauseMs');
78137
});
138+
139+
for (const command of ['gesture', 'swipe'] as const) {
140+
test(`${command} cancellation interrupts a pause with its typed reason and records no action`, async () => {
141+
vi.useFakeTimers();
142+
const input =
143+
command === 'gesture'
144+
? {
145+
kind: 'tap',
146+
points: [
147+
{ x: 80, y: 300 },
148+
{ x: 200, y: 450 },
149+
],
150+
}
151+
: { from: { x: 80, y: 300 }, to: { x: 200, y: 450 } };
152+
let outcome: Awaited<ReturnType<typeof runGesture>> | undefined;
153+
const pending = runGesture({ ...input, count: 3, pauseMs: 1000 }, command).then((result) => {
154+
outcome = result;
155+
});
156+
await vi.advanceTimersByTimeAsync(0);
157+
expect(perform).toHaveBeenCalledTimes(1);
158+
expect(outcome).toBeUndefined();
159+
markRequestCanceled(requestId);
160+
await vi.advanceTimersByTimeAsync(0);
161+
try {
162+
expect(outcome?.response).toMatchObject({
163+
ok: false,
164+
error: { code: 'COMMAND_FAILED', details: { reason: 'request_canceled' } },
165+
});
166+
expect(outcome?.actions).toEqual([]);
167+
expect(perform).toHaveBeenCalledTimes(1);
168+
expect(vi.getTimerCount()).toBe(0);
169+
} finally {
170+
await vi.runAllTimersAsync();
171+
await pending;
172+
}
173+
});
174+
}
175+
176+
test('cancellation during the last tap records no completed action', async () => {
177+
let performed = 0;
178+
perform.mockImplementation(async () => {
179+
if (++performed === 3) markRequestCanceled(requestId);
180+
return {
181+
kind: 'tap',
182+
pointerCount: 2,
183+
durationMs: 40,
184+
from: { x: 140, y: 375 },
185+
to: { x: 140, y: 375 },
186+
message: 'Tapped 2 points',
187+
};
188+
});
189+
const { response, actions } = await runGesture({
190+
kind: 'tap',
191+
points: [
192+
{ x: 80, y: 300 },
193+
{ x: 200, y: 450 },
194+
],
195+
count: 3,
196+
});
197+
expect(response).toMatchObject({
198+
ok: false,
199+
error: { code: 'COMMAND_FAILED', details: { reason: 'request_canceled' } },
200+
});
201+
expect(actions).toEqual([]);
202+
expect(performed).toBe(3);
203+
});

‎packages/daemon/src/execution/interaction/internal/interaction-gesture.ts‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from '../../../core/gesture-runtime.ts';
3232
import { isActiveProviderDevice } from '../../../core/provider-device-admission.ts';
3333
import { sleep } from '@agent-device/host-kit/retry';
34+
import { getRequestSignal } from '@agent-device/host-kit/request';
3435
import {
3536
ensureAndroidBlockingSystemDialogReady,
3637
type AndroidBlockingDialogReadinessResult,
@@ -110,7 +111,16 @@ async function runGestureInteraction(
110111
if (!bound.ok) return { refused: bound.response };
111112
const runtime = await createGestureRuntime(params, bound.gestures);
112113
const context = { session: params.sessionName, requestId: params.req.meta?.requestId };
113-
const result = await runPreparedGesture(runtime, context, gesture, params.req.internal);
114+
const result =
115+
input.kind === 'tap'
116+
? await runGestureRepetitions(
117+
runtime,
118+
params,
119+
input.count ?? 1,
120+
input.pauseMs ?? 0,
121+
() => gesture,
122+
)
123+
: await runPreparedGesture(runtime, context, gesture, params.req.internal);
114124
return buildGestureOutcome(input, gesture, result, params.req.flags);
115125
}
116126

@@ -147,6 +157,14 @@ function buildGestureOutcome(
147157
flags: gestureReplayFlags(input, flags),
148158
responseData: gestureResponseData(result, {
149159
executionProfile: resolveExecutionProfile(gesture),
160+
...(input.kind === 'tap'
161+
? {
162+
points: input.points,
163+
holdMs: result.durationMs,
164+
count: input.count ?? 1,
165+
pauseMs: input.pauseMs ?? 0,
166+
}
167+
: {}),
150168
}),
151169
...(input.kind === 'pinch'
152170
? { recordingResultExtra: { scale: input.scale } }
@@ -187,7 +205,13 @@ export async function dispatchSwipeViaRuntime(
187205
const pauseMs = input.pauseMs ?? 0;
188206
const pattern = input.pattern ?? 'one-way';
189207
const runtime = await createGestureRuntime(params, bound.gestures);
190-
const result = await runSwipeRepetitions(runtime, params, input, count, pauseMs, pattern);
208+
const result = await runGestureRepetitions(
209+
runtime,
210+
params,
211+
count,
212+
pauseMs,
213+
(index) => normalizePublicSwipeMotion(swipeMotionAtIndex(input, pattern, index)).gesture,
214+
);
191215
return {
192216
positionals: swipeReplayPositionals(input),
193217
flags: params.req.flags,
@@ -243,6 +267,7 @@ async function dispatchGestureInteraction(
243267
});
244268
const outcome = await run(session, readiness);
245269
if (isRefusal(outcome)) return outcome.refused;
270+
getRequestSignal(params.req.meta?.requestId)?.throwIfAborted();
246271
if (!providerDevice) {
247272
await ensureAndroidBlockingSystemDialogReady({
248273
session,
@@ -257,6 +282,7 @@ async function dispatchGestureInteraction(
257282
typeof responseData.warning === 'string' ? `${responseData.warning} ` : '';
258283
responseData.warning = `${existingWarning}${readiness.warning}`;
259284
}
285+
getRequestSignal(params.req.meta?.requestId)?.throwIfAborted();
260286
return finalizeTouchInteraction({
261287
ref: params.sessionRef!,
262288
sessionStore: params.sessionStore,
@@ -399,25 +425,30 @@ function swipeReplayPositionals(input: SwipePayload): string[] {
399425
return [String(input.from.x), String(input.from.y), String(input.to.x), String(input.to.y)];
400426
}
401427

402-
async function runSwipeRepetitions(
428+
async function runGestureRepetitions(
403429
runtime: GestureRuntime,
404430
params: InteractionRouteInput,
405-
input: SwipePayload,
406431
count: number,
407432
pauseMs: number,
408-
pattern: 'one-way' | 'ping-pong',
409-
) {
410-
let result: Awaited<ReturnType<typeof runtime.interactions.gesture>> | undefined;
433+
gestureAtIndex: (index: number) => GestureCommandInput,
434+
): Promise<GestureRuntimeResult> {
435+
const signal = getRequestSignal(params.req.meta?.requestId);
436+
let result: GestureRuntimeResult | undefined;
411437
for (let index = 0; index < count; index += 1) {
412-
const normalized = normalizePublicSwipeMotion(swipeMotionAtIndex(input, pattern, index));
413-
result = await runtime.interactions.gesture({
414-
session: params.sessionName,
415-
requestId: params.req.meta?.requestId,
416-
gesture: normalized.gesture,
417-
});
418-
if (pauseMs > 0 && index + 1 < count) await sleep(pauseMs);
438+
signal?.throwIfAborted();
439+
result = await runPreparedGesture(
440+
runtime,
441+
{ session: params.sessionName, requestId: params.req.meta?.requestId },
442+
gestureAtIndex(index),
443+
params.req.internal,
444+
);
445+
signal?.throwIfAborted();
446+
if (pauseMs > 0 && index + 1 < count) await sleep(pauseMs, signal);
447+
}
448+
if (!result) throw new Error('Gesture orchestration did not execute a gesture.');
449+
if (result.kind === 'tap' && count > 1) {
450+
return { ...result, message: `Tapped ${result.pointerCount} points ${count} times` };
419451
}
420-
if (!result) throw new Error('Swipe orchestration did not execute a gesture.');
421452
return result;
422453
}
423454

0 commit comments

Comments
 (0)