Skip to content

Commit 3d0aad9

Browse files
committed
fix(android): guard touch session reuse on helper identity, stop mismatched sessions (PR #1281 re-review)
Persistent helper sessions are keyed by device, so touch reuse must also prove the live session runs the helper binary the command selected. The session record now stores its helper identity (packageName, runner, helperVersion, helperVersionCode — the same values that feed the snapshot session identity), and runAndroidSnapshotHelperSessionTouchCommand takes the requesting helper identity: on mismatch (packageName/runner always; version/versionCode when both sides define them) it stops the session and returns undefined, so the touch command runs one-shot against the selected artifact — gestures never start sessions; the next snapshot restarts one with the right artifact. Matching identity reuses the session as before. Snapshot capture identity and behavior are unchanged. Regression: 'a provider artifact that mismatches the live session helper stops it and runs one-shot' — a live fake session from the bundled fixture artifact, then a gesture through an ADB provider supplying an already-current artifact with a distinct packageName/runner (no install): the old session socket receives zero gesture commands, the session is stopped, the one-shot am instrument args end with the provider runner, and helperTransport is 'instrumentation'.
1 parent ff6031b commit 3d0aad9

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎src/platforms/android/__tests__/touch-helper-session.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,73 @@ test('an APK replacement stops the stale session and the gesture runs one-shot',
258258
assert.equal(await session.isSessionAlive(), false);
259259
});
260260

261+
test('a provider artifact that mismatches the live session helper stops it and runs one-shot', async () => {
262+
const device = makeIsolatedDevice();
263+
let sessionGestureCallCount = 0;
264+
const session = await startFakeTouchHelperSession(device, (command, requestId) => {
265+
if (command.startsWith('gesture')) {
266+
sessionGestureCallCount += 1;
267+
return sessionHeaderResponse({
268+
agentDeviceProtocol: 'android-snapshot-helper-v1',
269+
requestId,
270+
ok: 'true',
271+
kind: 'swipe',
272+
injectedEvents: '6',
273+
elapsedMs: '9',
274+
});
275+
}
276+
return sessionHeaderResponse({
277+
agentDeviceProtocol: 'android-snapshot-helper-v1',
278+
requestId,
279+
ok: 'true',
280+
});
281+
});
282+
283+
const providerArtifact = {
284+
...ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT,
285+
manifest: {
286+
...ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT.manifest,
287+
packageName: 'com.example.provider.snapshothelper',
288+
instrumentationRunner: 'com.example.provider.snapshothelper/.SnapshotInstrumentation',
289+
},
290+
};
291+
292+
let instrumentArgs: string[] | undefined;
293+
const result = await withAndroidAdbProvider(
294+
{
295+
// Artifact B is already current on the device, so no install happens: the session started
296+
// by artifact A must be invalidated by the helper-identity guard, not the install path.
297+
exec: async (args) => {
298+
if (args.includes('--show-versioncode')) {
299+
return {
300+
exitCode: 0,
301+
stdout: `package:${providerArtifact.manifest.packageName} versionCode:999999`,
302+
stderr: '',
303+
};
304+
}
305+
instrumentArgs = [...args];
306+
return {
307+
exitCode: 0,
308+
stdout: [
309+
resultRecord({ ok: 'true', kind: 'swipe', injectedEvents: '4', elapsedMs: '12' }),
310+
'INSTRUMENTATION_CODE: 0',
311+
].join('\n'),
312+
stderr: '',
313+
};
314+
},
315+
snapshotHelperArtifact: providerArtifact,
316+
},
317+
{ serial: device.id },
318+
async () => await executeAndroidTouchHelperPlan(device, flingPlan()),
319+
);
320+
321+
assert.equal(result.installReason, 'current');
322+
assert.equal(result.helperTransport, 'instrumentation');
323+
assert.equal(instrumentArgs?.at(-1), providerArtifact.manifest.instrumentationRunner);
324+
assert.equal(sessionGestureCallCount, 0);
325+
assert.equal(await session.isSessionAlive(), false);
326+
});
327+
261328
test('a structured ok=false session gesture response throws but leaves the session alive', async () => {
262329
const device = makeIsolatedDevice();
263330
const session = await startFakeTouchHelperSession(device, (command, requestId) => {

‎src/platforms/android/snapshot-helper-session.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ const SESSION_PROCESS_EXIT_TIMEOUT_MS = 2_000;
2525
const SESSION_REQUEST_OVERHEAD_MS = 10_000;
2626
const FORWARD_TIMEOUT_MS = 5_000;
2727

28+
type AndroidSnapshotHelperSessionHelperIdentity = {
29+
packageName: string;
30+
runner: string;
31+
helperVersion?: string;
32+
helperVersionCode?: number;
33+
};
34+
2835
type AndroidSnapshotHelperSession = {
2936
identity: string;
3037
deviceKey: string;
38+
helper: AndroidSnapshotHelperSessionHelperIdentity;
3139
port: number;
3240
adb: AndroidAdbExecutor;
3341
process: AndroidAdbProcess;
@@ -100,11 +108,28 @@ export async function captureAndroidSnapshotWithHelperSession(
100108
export async function runAndroidSnapshotHelperSessionTouchCommand(params: {
101109
deviceKey: string;
102110
action: 'gesture' | 'viewport';
111+
helper: AndroidSnapshotHelperSessionHelperIdentity;
103112
payloadBase64?: string;
104113
timeoutMs: number;
105114
}): Promise<Record<string, string> | undefined> {
106115
const session = sessions.get(params.deviceKey);
107116
if (!session) return undefined;
117+
if (!matchesSessionHelperIdentity(session.helper, params.helper)) {
118+
// A different helper binary was selected for this device (e.g. a provider-supplied artifact).
119+
// The live session belongs to the previous helper, so stop it and let the touch command run
120+
// one-shot against the selected artifact; the next snapshot restarts the session with it.
121+
emitDiagnostic({
122+
level: 'warn',
123+
phase: 'android_snapshot_helper_session_touch_identity_mismatch',
124+
data: {
125+
deviceKey: params.deviceKey,
126+
sessionHelper: session.helper,
127+
requestedHelper: params.helper,
128+
},
129+
});
130+
await stopAndroidSnapshotHelperSession(params.deviceKey);
131+
return undefined;
132+
}
108133
const requestId = `${params.action}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
109134
const command = params.payloadBase64
110135
? `${params.action} ${requestId} ${params.payloadBase64}`
@@ -131,6 +156,22 @@ export async function runAndroidSnapshotHelperSessionTouchCommand(params: {
131156
return headers;
132157
}
133158

159+
function matchesSessionHelperIdentity(
160+
session: AndroidSnapshotHelperSessionHelperIdentity,
161+
requested: AndroidSnapshotHelperSessionHelperIdentity,
162+
): boolean {
163+
return (
164+
session.packageName === requested.packageName &&
165+
session.runner === requested.runner &&
166+
matchesWhenBothDefined(session.helperVersion, requested.helperVersion) &&
167+
matchesWhenBothDefined(session.helperVersionCode, requested.helperVersionCode)
168+
);
169+
}
170+
171+
function matchesWhenBothDefined<Value>(a: Value | undefined, b: Value | undefined): boolean {
172+
return a === undefined || b === undefined || a === b;
173+
}
174+
134175
function findSessionHeaderEnd(response: string): number {
135176
const separator = response.indexOf('\n\n');
136177
return separator < 0 ? response.length : separator;
@@ -248,6 +289,12 @@ async function startAndroidSnapshotHelperSession(params: {
248289
const session: AndroidSnapshotHelperSession = {
249290
identity: params.identity,
250291
deviceKey: params.deviceKey,
292+
helper: {
293+
packageName: params.resolved.packageName,
294+
runner: params.resolved.runner,
295+
helperVersion: params.options.helperVersion,
296+
helperVersionCode: params.options.helperVersionCode,
297+
},
251298
port,
252299
adb: params.options.adb,
253300
process,

‎src/platforms/android/touch-helper.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export async function executeAndroidTouchHelperPlan(
7373
const sessionHeaders = await runAndroidSnapshotHelperSessionTouchCommand({
7474
deviceKey: prepared.deviceKey,
7575
action: 'gesture',
76+
helper: touchSessionHelperIdentity(prepared.artifact),
7677
payloadBase64,
7778
timeoutMs,
7879
});
@@ -109,6 +110,7 @@ export async function readAndroidTouchHelperViewport(device: DeviceInfo): Promis
109110
const sessionHeaders = await runAndroidSnapshotHelperSessionTouchCommand({
110111
deviceKey: prepared.deviceKey,
111112
action: 'viewport',
113+
helper: touchSessionHelperIdentity(prepared.artifact),
112114
timeoutMs: HELPER_VIEWPORT_TIMEOUT_MS,
113115
});
114116
if (sessionHeaders) return readViewportResult(sessionHeaders);
@@ -132,6 +134,22 @@ export async function readAndroidTouchHelperViewport(device: DeviceInfo): Promis
132134
});
133135
}
134136

137+
// Sessions are keyed by device, so reuse must also prove the live session runs the helper binary
138+
// this command selected; a mismatch stops the session and the command falls back to one-shot.
139+
function touchSessionHelperIdentity(artifact: AndroidSnapshotHelperArtifact): {
140+
packageName: string;
141+
runner: string;
142+
helperVersion: string;
143+
helperVersionCode: number;
144+
} {
145+
return {
146+
packageName: artifact.manifest.packageName,
147+
runner: artifact.manifest.instrumentationRunner,
148+
helperVersion: artifact.manifest.version,
149+
helperVersionCode: artifact.manifest.versionCode,
150+
};
151+
}
152+
135153
async function prepareAndroidTouchHelper(device: DeviceInfo): Promise<PreparedAndroidTouchHelper> {
136154
const adbProvider = resolveAndroidAdbProvider(device);
137155
// Same artifact precedence as snapshot capture: a provider-supplied helper artifact overrides

0 commit comments

Comments
 (0)