Skip to content

Commit a0c4c84

Browse files
committed
fix(android): keep a missing-helper refusal out of capture failure recovery
A transient capture on a device without the current helper refused at the install check, and that refusal went through the capture failure handler, which logged an error and force-stopped the helper runtime on every new device's first open. The refusal now reaches the caller directly. The transient-capture tests move to their own file, so snapshot.test.ts stays under the test-file size ratchet. Refs #1571
1 parent 7118c6b commit a0c4c84

6 files changed

Lines changed: 248 additions & 191 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
re-capture usually recovered, but inside that command's own budget. The open now captures the
1010
launched app once, with the same content check and up to 3 re-captures, and starts no re-capture
1111
after 6 s. The 6 s never cancels work: the helper start and each capture keep their own
12-
timeouts, and only cancelling the open stops them. This capture never installs the snapshot helper, does not reset it after a content verdict, and
13-
stops a helper session only if it started that session. So an open-only session does not keep
14-
UiAutomation, and the first read after the open starts the helper as before. `open_timing` reports
12+
timeouts, and only cancelling the open stops them. This capture never installs the snapshot
13+
helper, does not reset it after a content verdict or on a device without it, and stops a helper
14+
session only if it started that session. So an open-only session does not keep UiAutomation,
15+
and the first read after the open starts the helper as before. `open_timing` reports
1516
`postOpenObservation` (see `PostOpenObservation`): `observable`; `unobservable` for a content
1617
verdict or a system surface over the app; `probe-failed` with a typed
1718
`postOpenObservationFailure` when the capture could not run, for example before the helper is
18-
installed on a new device; and `app-unidentified` when the launched package could not be read. A
19-
URL or deep-link open reports none. The open succeeds in all of these cases, and a cancelled open
20-
still rejects. Limrun and WebDriver opens do not change. The iOS half of #1571 is #2838.
19+
installed on a new device; and `app-unidentified` when the launched package could not be read.
20+
A URL or deep-link open reports none. The open succeeds in all of these cases, and a cancelled
21+
open still rejects. Limrun and WebDriver opens do not change. The iOS half of #1571 is #2838.
2122
- Fixed (ios): `perf cpu profile report --kind xctrace` on Xcode 27 no longer fails with
2223
`Apple xctrace CPU report contained no samples` on a trace that holds thousands of samples. Xcode
2324
27 exports each `time-profile` sample stack as `<tagged-backtrace>` instead of `<backtrace>`, and

‎packages/platform-android/src/__tests__/snapshot-helper-session.fixtures.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,16 @@ function readSessionPort(args: readonly string[]): number {
521521
assert.notEqual(index, -1);
522522
return Number(args[index + 1]);
523523
}
524+
525+
/** A capture holding only system-UI chrome: the helper answered, but no app content. */
526+
export function androidSystemWindowOnlyXml(): string {
527+
return [
528+
'<?xml version="1.0" encoding="UTF-8"?>',
529+
'<hierarchy rotation="0">',
530+
' <node window-index="0" window-type="3" window-layer="30" window-active="true" window-focused="true" class="android.widget.FrameLayout" package="com.android.systemui" bounds="[0,0][390,844]" enabled="true" visible-to-user="true">',
531+
' <node content-desc="Back" class="android.widget.ImageButton" package="com.android.systemui" bounds="[0,792][96,844]" clickable="true" enabled="true" focusable="true" visible-to-user="true" />',
532+
' <node content-desc="Home" class="android.widget.ImageButton" package="com.android.systemui" bounds="[147,792][243,844]" clickable="true" enabled="true" focusable="true" visible-to-user="true" />',
533+
' </node>',
534+
'</hierarchy>',
535+
].join('\n');
536+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import { afterEach, beforeEach, test, vi } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
4+
vi.mock('../adb.ts', async (importOriginal) => {
5+
const actual = await importOriginal<typeof import('../adb.ts')>();
6+
return { ...actual, sleep: vi.fn(async () => {}) };
7+
});
8+
9+
import { isUnreadableCaptureContentError } from '@agent-device/contracts/android-snapshot-quality';
10+
import type { DeviceInfo } from '@agent-device/kernel/device';
11+
import type { AppError } from '@agent-device/kernel/errors';
12+
import { snapshotAndroid } from '../snapshot.ts';
13+
import { resetAndroidSnapshotHelperInstallCache } from '../snapshot-helper-install.ts';
14+
import { resetAndroidSnapshotHelperSessions } from '../snapshot-helper-session-lifecycle.ts';
15+
import type { AndroidAdbExecutor } from '../snapshot-helper.ts';
16+
import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from './test-utils/android-snapshot-helper.ts';
17+
import {
18+
androidSystemWindowOnlyXml,
19+
createPersistentSnapshotHelperProvider,
20+
isAndroidHelperRuntimeForceStop as isHelperRuntimeReset,
21+
type FakeAndroidProcess,
22+
} from './snapshot-helper-session.fixtures.ts';
23+
24+
const device: DeviceInfo = {
25+
platform: 'android',
26+
id: 'emulator-5554',
27+
name: 'Pixel',
28+
kind: 'emulator',
29+
booted: true,
30+
};
31+
32+
const helperArtifact = ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT;
33+
34+
beforeEach(async () => {
35+
await resetAndroidSnapshotHelperSessions();
36+
resetAndroidSnapshotHelperInstallCache();
37+
});
38+
39+
afterEach(async () => {
40+
await resetAndroidSnapshotHelperSessions();
41+
});
42+
43+
function openSettleWindow(): { settleBy: number } {
44+
return { settleBy: Date.now() + 60_000 };
45+
}
46+
47+
function isHelperVersionProbe(args: readonly string[]): boolean {
48+
return args.includes('--show-versioncode');
49+
}
50+
51+
test('a transient capture releases a helper session it had to start', async () => {
52+
const adbCalls: (readonly string[])[] = [];
53+
const spawnArgs: (readonly string[])[] = [];
54+
const processes: FakeAndroidProcess[] = [];
55+
const provider = createPersistentSnapshotHelperProvider({
56+
calls: adbCalls,
57+
spawnArgs,
58+
processes,
59+
});
60+
61+
await snapshotAndroid(device, {
62+
helperAdb: provider,
63+
helperArtifact,
64+
transient: openSettleWindow(),
65+
});
66+
67+
assert.equal(spawnArgs.length, 1);
68+
assert.equal(processes[0]?.exitCode, 0);
69+
assert.equal(
70+
adbCalls.some((args) => args[0] === 'forward' && args[1] === '--remove'),
71+
true,
72+
);
73+
});
74+
75+
test('a transient capture leaves a warm daemon-session helper running', async () => {
76+
const adbCalls: (readonly string[])[] = [];
77+
const spawnArgs: (readonly string[])[] = [];
78+
const processes: FakeAndroidProcess[] = [];
79+
const provider = createPersistentSnapshotHelperProvider({
80+
calls: adbCalls,
81+
spawnArgs,
82+
processes,
83+
});
84+
85+
await snapshotAndroid(device, {
86+
helperAdb: provider,
87+
helperArtifact,
88+
helperSessionScope: 'daemon-session',
89+
});
90+
const borrowed = await snapshotAndroid(device, {
91+
helperAdb: provider,
92+
helperArtifact,
93+
transient: openSettleWindow(),
94+
});
95+
96+
assert.equal(borrowed.androidSnapshot.helperSessionReused, true);
97+
assert.equal(spawnArgs.length, 1);
98+
assert.equal(processes[0]?.exitCode, null);
99+
assert.equal(
100+
adbCalls.some((args) => args[0] === 'forward' && args[1] === '--remove'),
101+
false,
102+
);
103+
});
104+
105+
test('a transient capture reports its content verdict without retiring the warm helper', async () => {
106+
const adbCalls: (readonly string[])[] = [];
107+
const spawnArgs: (readonly string[])[] = [];
108+
const processes: FakeAndroidProcess[] = [];
109+
const provider = createPersistentSnapshotHelperProvider({
110+
calls: adbCalls,
111+
spawnArgs,
112+
processes,
113+
sessionXml: (_sessionIndex, snapshotCount) =>
114+
snapshotCount === 1
115+
? '<hierarchy><node text="warm helper" bounds="[0,0][10,10]" /></hierarchy>'
116+
: androidSystemWindowOnlyXml(),
117+
});
118+
await snapshotAndroid(device, {
119+
helperAdb: provider,
120+
helperArtifact,
121+
helperSessionScope: 'daemon-session',
122+
});
123+
124+
await assert.rejects(
125+
snapshotAndroid(device, { helperAdb: provider, helperArtifact, transient: openSettleWindow() }),
126+
(error: unknown) => isUnreadableCaptureContentError(error),
127+
);
128+
129+
assert.equal(processes[0]?.exitCode, null, 'the session helper is still running');
130+
assert.equal(adbCalls.some(isHelperRuntimeReset), false);
131+
assert.equal(spawnArgs.length, 1);
132+
});
133+
134+
test('a settle window that passes during the helper start never cancels the start', async () => {
135+
const adbCalls: (readonly string[])[] = [];
136+
const spawnArgs: (readonly string[])[] = [];
137+
const processes: FakeAndroidProcess[] = [];
138+
const provider = createPersistentSnapshotHelperProvider({
139+
calls: adbCalls,
140+
spawnArgs,
141+
processes,
142+
sessionXml: () => androidSystemWindowOnlyXml(),
143+
sessionReadyDelayMs: 150,
144+
});
145+
146+
await assert.rejects(
147+
snapshotAndroid(device, {
148+
helperAdb: provider,
149+
helperArtifact,
150+
transient: { settleBy: Date.now() + 30 },
151+
}),
152+
(error: unknown) =>
153+
isUnreadableCaptureContentError(error) && (error as AppError).details?.attempts === 1,
154+
);
155+
156+
assert.equal(spawnArgs.length, 1);
157+
assert.equal(processes[0]?.killed, false, 'the started helper was not signalled');
158+
assert.equal(processes[0]?.exitCode, 0, 'the started helper quit on its own');
159+
assert.equal(adbCalls.some(isHelperRuntimeReset), false);
160+
});
161+
162+
test('a settle window that passes during a warm session capture leaves the session running', async () => {
163+
const adbCalls: (readonly string[])[] = [];
164+
const spawnArgs: (readonly string[])[] = [];
165+
const processes: FakeAndroidProcess[] = [];
166+
const provider = createPersistentSnapshotHelperProvider({
167+
calls: adbCalls,
168+
spawnArgs,
169+
processes,
170+
sessionXml: (_sessionIndex, snapshotCount) =>
171+
snapshotCount === 1
172+
? '<hierarchy><node text="warm helper" bounds="[0,0][10,10]" /></hierarchy>'
173+
: androidSystemWindowOnlyXml(),
174+
captureResponseDelayMs: (snapshotCount) => (snapshotCount === 1 ? 0 : 150),
175+
});
176+
await snapshotAndroid(device, {
177+
helperAdb: provider,
178+
helperArtifact,
179+
helperSessionScope: 'daemon-session',
180+
});
181+
182+
await assert.rejects(
183+
snapshotAndroid(device, {
184+
helperAdb: provider,
185+
helperArtifact,
186+
transient: { settleBy: Date.now() + 30 },
187+
}),
188+
(error: unknown) =>
189+
isUnreadableCaptureContentError(error) && (error as AppError).details?.attempts === 1,
190+
);
191+
192+
assert.equal(spawnArgs.length, 1);
193+
assert.equal(processes[0]?.killed, false);
194+
assert.equal(processes[0]?.exitCode, null, 'the session helper is still running');
195+
assert.equal(adbCalls.some(isHelperRuntimeReset), false);
196+
});
197+
198+
test('a transient capture on a device without the current helper installs and resets nothing', async () => {
199+
const adbCalls: (readonly string[])[] = [];
200+
const helperAdb: AndroidAdbExecutor = async (args) => {
201+
adbCalls.push(args);
202+
if (isHelperVersionProbe(args)) return { exitCode: 1, stdout: '', stderr: 'not found' };
203+
return { exitCode: 0, stdout: '', stderr: '' };
204+
};
205+
206+
await assert.rejects(
207+
snapshotAndroid(device, { helperAdb, helperArtifact, transient: openSettleWindow() }),
208+
(error: unknown) =>
209+
(error as AppError).details?.reason === 'android-snapshot-helper-not-current',
210+
);
211+
212+
assert.equal(
213+
adbCalls.some((args) => args.includes('install') || args.includes('instrument')),
214+
false,
215+
);
216+
assert.equal(adbCalls.some(isHelperRuntimeReset), false, 'a refusal is not a helper failure');
217+
});

0 commit comments

Comments
 (0)