agent-device 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -9
- package/dist/src/797.js +1 -1
- package/dist/src/bin.js +5 -5
- package/dist/src/daemon.js +16 -20
- package/package.json +7 -9
- package/skills/agent-device/SKILL.md +3 -6
- package/skills/agent-device/references/permissions.md +3 -15
- package/skills/agent-device/references/snapshot-refs.md +1 -4
- package/dist/bin/axsnapshot +0 -0
- package/ios-runner/AXSnapshot/Package.swift +0 -18
- package/ios-runner/AXSnapshot/Sources/AXSnapshot/main.swift +0 -444
- package/src/__tests__/cli-close.test.ts +0 -155
- package/src/__tests__/cli-help.test.ts +0 -102
- package/src/bin.ts +0 -3
- package/src/cli.ts +0 -305
- package/src/core/__tests__/capabilities.test.ts +0 -75
- package/src/core/__tests__/dispatch-open.test.ts +0 -25
- package/src/core/__tests__/open-target.test.ts +0 -55
- package/src/core/capabilities.ts +0 -57
- package/src/core/dispatch.ts +0 -382
- package/src/core/open-target.ts +0 -27
- package/src/daemon/__tests__/device-ready.test.ts +0 -52
- package/src/daemon/__tests__/is-predicates.test.ts +0 -68
- package/src/daemon/__tests__/selectors.test.ts +0 -261
- package/src/daemon/__tests__/session-routing.test.ts +0 -108
- package/src/daemon/__tests__/session-selector.test.ts +0 -64
- package/src/daemon/__tests__/session-store.test.ts +0 -142
- package/src/daemon/__tests__/snapshot-processing.test.ts +0 -47
- package/src/daemon/action-utils.ts +0 -29
- package/src/daemon/context.ts +0 -48
- package/src/daemon/device-ready.ts +0 -155
- package/src/daemon/handlers/__tests__/find.test.ts +0 -99
- package/src/daemon/handlers/__tests__/interaction.test.ts +0 -22
- package/src/daemon/handlers/__tests__/replay-heal.test.ts +0 -509
- package/src/daemon/handlers/__tests__/session-reinstall.test.ts +0 -219
- package/src/daemon/handlers/__tests__/session.test.ts +0 -820
- package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +0 -92
- package/src/daemon/handlers/__tests__/snapshot.test.ts +0 -128
- package/src/daemon/handlers/find.ts +0 -324
- package/src/daemon/handlers/interaction.ts +0 -550
- package/src/daemon/handlers/parse-utils.ts +0 -8
- package/src/daemon/handlers/record-trace.ts +0 -154
- package/src/daemon/handlers/session.ts +0 -1137
- package/src/daemon/handlers/snapshot.ts +0 -439
- package/src/daemon/is-predicates.ts +0 -46
- package/src/daemon/selectors.ts +0 -540
- package/src/daemon/session-routing.ts +0 -22
- package/src/daemon/session-selector.ts +0 -39
- package/src/daemon/session-store.ts +0 -296
- package/src/daemon/snapshot-processing.ts +0 -131
- package/src/daemon/types.ts +0 -56
- package/src/daemon-client.ts +0 -272
- package/src/daemon.ts +0 -295
- package/src/platforms/__tests__/boot-diagnostics.test.ts +0 -59
- package/src/platforms/android/__tests__/index.test.ts +0 -274
- package/src/platforms/android/devices.ts +0 -196
- package/src/platforms/android/index.ts +0 -784
- package/src/platforms/android/ui-hierarchy.ts +0 -312
- package/src/platforms/boot-diagnostics.ts +0 -128
- package/src/platforms/ios/__tests__/index.test.ts +0 -312
- package/src/platforms/ios/__tests__/runner-client.test.ts +0 -155
- package/src/platforms/ios/apps.ts +0 -358
- package/src/platforms/ios/ax-snapshot.ts +0 -207
- package/src/platforms/ios/config.ts +0 -28
- package/src/platforms/ios/devicectl.ts +0 -134
- package/src/platforms/ios/devices.ts +0 -100
- package/src/platforms/ios/index.ts +0 -20
- package/src/platforms/ios/runner-client.ts +0 -994
- package/src/platforms/ios/simulator.ts +0 -164
- package/src/utils/__tests__/args.test.ts +0 -239
- package/src/utils/__tests__/daemon-client.test.ts +0 -95
- package/src/utils/__tests__/exec.test.ts +0 -16
- package/src/utils/__tests__/finders.test.ts +0 -34
- package/src/utils/__tests__/keyed-lock.test.ts +0 -55
- package/src/utils/__tests__/process-identity.test.ts +0 -33
- package/src/utils/__tests__/retry.test.ts +0 -44
- package/src/utils/args.ts +0 -239
- package/src/utils/command-schema.ts +0 -622
- package/src/utils/device.ts +0 -84
- package/src/utils/errors.ts +0 -35
- package/src/utils/exec.ts +0 -339
- package/src/utils/finders.ts +0 -101
- package/src/utils/interactive.ts +0 -4
- package/src/utils/interactors.ts +0 -173
- package/src/utils/keyed-lock.ts +0 -14
- package/src/utils/output.ts +0 -204
- package/src/utils/process-identity.ts +0 -100
- package/src/utils/retry.ts +0 -180
- package/src/utils/snapshot.ts +0 -64
- package/src/utils/timeouts.ts +0 -9
- package/src/utils/version.ts +0 -26
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
import type { DeviceInfo } from '../../utils/device.ts';
|
|
2
|
-
import { AppError } from '../../utils/errors.ts';
|
|
3
|
-
import { runCmd } from '../../utils/exec.ts';
|
|
4
|
-
import { Deadline, retryWithPolicy } from '../../utils/retry.ts';
|
|
5
|
-
import { isDeepLinkTarget, resolveIosDeviceDeepLinkBundleId } from '../../core/open-target.ts';
|
|
6
|
-
|
|
7
|
-
import { IOS_APP_LAUNCH_TIMEOUT_MS } from './config.ts';
|
|
8
|
-
import { listIosDeviceApps, runIosDevicectl, type IosAppInfo } from './devicectl.ts';
|
|
9
|
-
import { ensureBootedSimulator, ensureSimulator, getSimulatorState } from './simulator.ts';
|
|
10
|
-
|
|
11
|
-
const ALIASES: Record<string, string> = {
|
|
12
|
-
settings: 'com.apple.Preferences',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export async function resolveIosApp(device: DeviceInfo, app: string): Promise<string> {
|
|
16
|
-
const trimmed = app.trim();
|
|
17
|
-
if (trimmed.includes('.')) return trimmed;
|
|
18
|
-
|
|
19
|
-
const alias = ALIASES[trimmed.toLowerCase()];
|
|
20
|
-
if (alias) return alias;
|
|
21
|
-
|
|
22
|
-
const list =
|
|
23
|
-
device.kind === 'simulator'
|
|
24
|
-
? await listSimulatorApps(device)
|
|
25
|
-
: await listIosDeviceApps(device, 'all');
|
|
26
|
-
const matches = list.filter((entry) => entry.name.toLowerCase() === trimmed.toLowerCase());
|
|
27
|
-
if (matches.length === 1) return matches[0].bundleId;
|
|
28
|
-
if (matches.length > 1) {
|
|
29
|
-
throw new AppError('INVALID_ARGS', `Multiple apps matched "${app}"`, { matches });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
throw new AppError('APP_NOT_INSTALLED', `No app found matching "${app}"`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function openIosApp(
|
|
36
|
-
device: DeviceInfo,
|
|
37
|
-
app: string,
|
|
38
|
-
options?: { appBundleId?: string; url?: string },
|
|
39
|
-
): Promise<void> {
|
|
40
|
-
const explicitUrl = options?.url?.trim();
|
|
41
|
-
if (explicitUrl) {
|
|
42
|
-
if (!isDeepLinkTarget(explicitUrl)) {
|
|
43
|
-
throw new AppError('INVALID_ARGS', 'open <app> <url> requires a valid URL target');
|
|
44
|
-
}
|
|
45
|
-
if (device.kind === 'simulator') {
|
|
46
|
-
await ensureBootedSimulator(device);
|
|
47
|
-
await runCmd('open', ['-a', 'Simulator'], { allowFailure: true });
|
|
48
|
-
await runCmd('xcrun', ['simctl', 'openurl', device.id, explicitUrl]);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
const appBundleId = options?.appBundleId ?? (await resolveIosApp(device, app));
|
|
52
|
-
const bundleId = resolveIosDeviceDeepLinkBundleId(appBundleId, explicitUrl);
|
|
53
|
-
if (!bundleId) {
|
|
54
|
-
throw new AppError(
|
|
55
|
-
'INVALID_ARGS',
|
|
56
|
-
'Deep link open on iOS devices requires an active app bundle ID. Open the app first, then open the URL.',
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
await launchIosDeviceProcess(device, bundleId, { payloadUrl: explicitUrl });
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const deepLinkTarget = app.trim();
|
|
64
|
-
if (isDeepLinkTarget(deepLinkTarget)) {
|
|
65
|
-
if (device.kind === 'simulator') {
|
|
66
|
-
await ensureBootedSimulator(device);
|
|
67
|
-
await runCmd('open', ['-a', 'Simulator'], { allowFailure: true });
|
|
68
|
-
await runCmd('xcrun', ['simctl', 'openurl', device.id, deepLinkTarget]);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const bundleId = resolveIosDeviceDeepLinkBundleId(options?.appBundleId, deepLinkTarget);
|
|
72
|
-
if (!bundleId) {
|
|
73
|
-
throw new AppError(
|
|
74
|
-
'INVALID_ARGS',
|
|
75
|
-
'Deep link open on iOS devices requires an active app bundle ID. Open the app first, then open the URL.',
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
await launchIosDeviceProcess(device, bundleId, { payloadUrl: deepLinkTarget });
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const bundleId = options?.appBundleId ?? (await resolveIosApp(device, app));
|
|
83
|
-
if (device.kind === 'simulator') {
|
|
84
|
-
await launchIosSimulatorApp(device, bundleId);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
await launchIosDeviceProcess(device, bundleId);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export async function openIosDevice(device: DeviceInfo): Promise<void> {
|
|
92
|
-
if (device.kind !== 'simulator') return;
|
|
93
|
-
const state = await getSimulatorState(device.id);
|
|
94
|
-
if (state === 'Booted') return;
|
|
95
|
-
|
|
96
|
-
await ensureBootedSimulator(device);
|
|
97
|
-
await runCmd('open', ['-a', 'Simulator'], { allowFailure: true });
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export async function closeIosApp(device: DeviceInfo, app: string): Promise<void> {
|
|
101
|
-
const bundleId = await resolveIosApp(device, app);
|
|
102
|
-
if (device.kind === 'simulator') {
|
|
103
|
-
await ensureBootedSimulator(device);
|
|
104
|
-
const result = await runCmd('xcrun', ['simctl', 'terminate', device.id, bundleId], {
|
|
105
|
-
allowFailure: true,
|
|
106
|
-
});
|
|
107
|
-
if (result.exitCode !== 0) {
|
|
108
|
-
const stderr = result.stderr.toLowerCase();
|
|
109
|
-
if (stderr.includes('found nothing to terminate')) return;
|
|
110
|
-
throw new AppError('COMMAND_FAILED', `xcrun exited with code ${result.exitCode}`, {
|
|
111
|
-
cmd: 'xcrun',
|
|
112
|
-
args: ['simctl', 'terminate', device.id, bundleId],
|
|
113
|
-
stdout: result.stdout,
|
|
114
|
-
stderr: result.stderr,
|
|
115
|
-
exitCode: result.exitCode,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
await runIosDevicectl(['device', 'process', 'terminate', '--device', device.id, bundleId], {
|
|
122
|
-
action: 'terminate iOS app',
|
|
123
|
-
deviceId: device.id,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export async function uninstallIosApp(device: DeviceInfo, app: string): Promise<{ bundleId: string }> {
|
|
128
|
-
ensureSimulator(device, 'reinstall');
|
|
129
|
-
const bundleId = await resolveIosApp(device, app);
|
|
130
|
-
await ensureBootedSimulator(device);
|
|
131
|
-
|
|
132
|
-
const result = await runCmd('xcrun', ['simctl', 'uninstall', device.id, bundleId], {
|
|
133
|
-
allowFailure: true,
|
|
134
|
-
});
|
|
135
|
-
if (result.exitCode !== 0) {
|
|
136
|
-
const output = `${result.stdout}\n${result.stderr}`.toLowerCase();
|
|
137
|
-
if (!output.includes('not installed') && !output.includes('not found') && !output.includes('no such file')) {
|
|
138
|
-
throw new AppError('COMMAND_FAILED', `simctl uninstall failed for ${bundleId}`, {
|
|
139
|
-
stdout: result.stdout,
|
|
140
|
-
stderr: result.stderr,
|
|
141
|
-
exitCode: result.exitCode,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return { bundleId };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export async function installIosApp(device: DeviceInfo, appPath: string): Promise<void> {
|
|
150
|
-
ensureSimulator(device, 'reinstall');
|
|
151
|
-
await ensureBootedSimulator(device);
|
|
152
|
-
await runCmd('xcrun', ['simctl', 'install', device.id, appPath]);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export async function reinstallIosApp(
|
|
156
|
-
device: DeviceInfo,
|
|
157
|
-
app: string,
|
|
158
|
-
appPath: string,
|
|
159
|
-
): Promise<{ bundleId: string }> {
|
|
160
|
-
const { bundleId } = await uninstallIosApp(device, app);
|
|
161
|
-
await installIosApp(device, appPath);
|
|
162
|
-
return { bundleId };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export async function screenshotIos(device: DeviceInfo, outPath: string): Promise<void> {
|
|
166
|
-
if (device.kind === 'simulator') {
|
|
167
|
-
await ensureBootedSimulator(device);
|
|
168
|
-
await runCmd('xcrun', ['simctl', 'io', device.id, 'screenshot', outPath]);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
await runIosDevicectl(['device', 'screenshot', '--device', device.id, outPath], {
|
|
173
|
-
action: 'capture iOS screenshot',
|
|
174
|
-
deviceId: device.id,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export async function setIosSetting(
|
|
179
|
-
device: DeviceInfo,
|
|
180
|
-
setting: string,
|
|
181
|
-
state: string,
|
|
182
|
-
appBundleId?: string,
|
|
183
|
-
): Promise<void> {
|
|
184
|
-
ensureSimulator(device, 'settings');
|
|
185
|
-
await ensureBootedSimulator(device);
|
|
186
|
-
const normalized = setting.toLowerCase();
|
|
187
|
-
const enabled = parseSettingState(state);
|
|
188
|
-
|
|
189
|
-
switch (normalized) {
|
|
190
|
-
case 'wifi': {
|
|
191
|
-
const mode = enabled ? 'active' : 'failed';
|
|
192
|
-
await runCmd('xcrun', ['simctl', 'status_bar', device.id, 'override', '--wifiMode', mode]);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
case 'airplane': {
|
|
196
|
-
if (enabled) {
|
|
197
|
-
await runCmd('xcrun', [
|
|
198
|
-
'simctl',
|
|
199
|
-
'status_bar',
|
|
200
|
-
device.id,
|
|
201
|
-
'override',
|
|
202
|
-
'--dataNetwork',
|
|
203
|
-
'hide',
|
|
204
|
-
'--wifiMode',
|
|
205
|
-
'failed',
|
|
206
|
-
'--wifiBars',
|
|
207
|
-
'0',
|
|
208
|
-
'--cellularMode',
|
|
209
|
-
'failed',
|
|
210
|
-
'--cellularBars',
|
|
211
|
-
'0',
|
|
212
|
-
'--operatorName',
|
|
213
|
-
'',
|
|
214
|
-
]);
|
|
215
|
-
} else {
|
|
216
|
-
await runCmd('xcrun', ['simctl', 'status_bar', device.id, 'clear']);
|
|
217
|
-
}
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
case 'location': {
|
|
221
|
-
if (!appBundleId) {
|
|
222
|
-
throw new AppError('INVALID_ARGS', 'location setting requires an active app in session');
|
|
223
|
-
}
|
|
224
|
-
const action = enabled ? 'grant' : 'revoke';
|
|
225
|
-
await runCmd('xcrun', ['simctl', 'privacy', device.id, action, 'location', appBundleId]);
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
default:
|
|
229
|
-
throw new AppError('INVALID_ARGS', `Unsupported setting: ${setting}`);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export async function listIosApps(
|
|
234
|
-
device: DeviceInfo,
|
|
235
|
-
filter: 'user-installed' | 'all' = 'all',
|
|
236
|
-
): Promise<IosAppInfo[]> {
|
|
237
|
-
if (device.kind === 'simulator') {
|
|
238
|
-
const apps = await listSimulatorApps(device);
|
|
239
|
-
return filterIosAppsByBundlePrefix(apps, filter);
|
|
240
|
-
}
|
|
241
|
-
return await listIosDeviceApps(device, filter);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export async function listSimulatorApps(device: DeviceInfo): Promise<IosAppInfo[]> {
|
|
245
|
-
const result = await runCmd('xcrun', ['simctl', 'listapps', device.id], { allowFailure: true });
|
|
246
|
-
const stdout = result.stdout as string;
|
|
247
|
-
const trimmed = stdout.trim();
|
|
248
|
-
if (!trimmed) return [];
|
|
249
|
-
|
|
250
|
-
let parsed: Record<string, { CFBundleDisplayName?: string; CFBundleName?: string }> | null = null;
|
|
251
|
-
if (trimmed.startsWith('{')) {
|
|
252
|
-
try {
|
|
253
|
-
parsed = JSON.parse(trimmed) as Record<string, { CFBundleDisplayName?: string; CFBundleName?: string }>;
|
|
254
|
-
} catch {
|
|
255
|
-
parsed = null;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (!parsed && trimmed.startsWith('{')) {
|
|
260
|
-
try {
|
|
261
|
-
const converted = await runCmd('plutil', ['-convert', 'json', '-o', '-', '-'], {
|
|
262
|
-
allowFailure: true,
|
|
263
|
-
stdin: trimmed,
|
|
264
|
-
});
|
|
265
|
-
if (converted.exitCode === 0 && converted.stdout.trim().startsWith('{')) {
|
|
266
|
-
parsed = JSON.parse(converted.stdout) as Record<
|
|
267
|
-
string,
|
|
268
|
-
{ CFBundleDisplayName?: string; CFBundleName?: string }
|
|
269
|
-
>;
|
|
270
|
-
}
|
|
271
|
-
} catch {
|
|
272
|
-
parsed = null;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (!parsed) return [];
|
|
277
|
-
return Object.entries(parsed).map(([bundleId, info]) => ({
|
|
278
|
-
bundleId,
|
|
279
|
-
name: info.CFBundleDisplayName ?? info.CFBundleName ?? bundleId,
|
|
280
|
-
}));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function parseSettingState(state: string): boolean {
|
|
284
|
-
const normalized = state.toLowerCase();
|
|
285
|
-
if (normalized === 'on' || normalized === 'true' || normalized === '1') return true;
|
|
286
|
-
if (normalized === 'off' || normalized === 'false' || normalized === '0') return false;
|
|
287
|
-
throw new AppError('INVALID_ARGS', `Invalid setting state: ${state}`);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function isTransientSimulatorLaunchFailure(error: unknown): boolean {
|
|
291
|
-
if (!(error instanceof AppError)) return false;
|
|
292
|
-
if (error.code !== 'COMMAND_FAILED') return false;
|
|
293
|
-
|
|
294
|
-
const details = (error.details ?? {}) as { exitCode?: number; stderr?: unknown };
|
|
295
|
-
if (details.exitCode !== 4) return false;
|
|
296
|
-
const stderr = String(details.stderr ?? '').toLowerCase();
|
|
297
|
-
|
|
298
|
-
return (
|
|
299
|
-
stderr.includes('fbsopenapplicationserviceerrordomain') &&
|
|
300
|
-
stderr.includes('the request to open')
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
async function launchIosSimulatorApp(device: DeviceInfo, bundleId: string): Promise<void> {
|
|
305
|
-
await ensureBootedSimulator(device);
|
|
306
|
-
await runCmd('open', ['-a', 'Simulator'], { allowFailure: true });
|
|
307
|
-
|
|
308
|
-
const launchDeadline = Deadline.fromTimeoutMs(IOS_APP_LAUNCH_TIMEOUT_MS);
|
|
309
|
-
await retryWithPolicy(
|
|
310
|
-
async ({ deadline: attemptDeadline }) => {
|
|
311
|
-
if (attemptDeadline?.isExpired()) {
|
|
312
|
-
throw new AppError('COMMAND_FAILED', 'App launch deadline exceeded', {
|
|
313
|
-
timeoutMs: IOS_APP_LAUNCH_TIMEOUT_MS,
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const result = await runCmd('xcrun', ['simctl', 'launch', device.id, bundleId], {
|
|
318
|
-
allowFailure: true,
|
|
319
|
-
});
|
|
320
|
-
if (result.exitCode === 0) return;
|
|
321
|
-
|
|
322
|
-
throw new AppError('COMMAND_FAILED', `xcrun exited with code ${result.exitCode}`, {
|
|
323
|
-
cmd: 'xcrun',
|
|
324
|
-
args: ['simctl', 'launch', device.id, bundleId],
|
|
325
|
-
stdout: result.stdout,
|
|
326
|
-
stderr: result.stderr,
|
|
327
|
-
exitCode: result.exitCode,
|
|
328
|
-
});
|
|
329
|
-
},
|
|
330
|
-
{
|
|
331
|
-
maxAttempts: 30,
|
|
332
|
-
baseDelayMs: 1_000,
|
|
333
|
-
maxDelayMs: 5_000,
|
|
334
|
-
jitter: 0.2,
|
|
335
|
-
shouldRetry: isTransientSimulatorLaunchFailure,
|
|
336
|
-
},
|
|
337
|
-
{ deadline: launchDeadline },
|
|
338
|
-
);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
async function launchIosDeviceProcess(
|
|
342
|
-
device: DeviceInfo,
|
|
343
|
-
bundleId: string,
|
|
344
|
-
options?: { payloadUrl?: string },
|
|
345
|
-
): Promise<void> {
|
|
346
|
-
const args = ['device', 'process', 'launch', '--device', device.id, bundleId];
|
|
347
|
-
if (options?.payloadUrl) {
|
|
348
|
-
args.push('--payload-url', options.payloadUrl);
|
|
349
|
-
}
|
|
350
|
-
await runIosDevicectl(args, { action: 'launch iOS app', deviceId: device.id });
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function filterIosAppsByBundlePrefix(apps: IosAppInfo[], filter: 'user-installed' | 'all'): IosAppInfo[] {
|
|
354
|
-
if (filter === 'user-installed') {
|
|
355
|
-
return apps.filter((app) => !app.bundleId.startsWith('com.apple.'));
|
|
356
|
-
}
|
|
357
|
-
return apps;
|
|
358
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { fileURLToPath } from 'node:url';
|
|
4
|
-
import { AppError } from '../../utils/errors.ts';
|
|
5
|
-
import { runCmd } from '../../utils/exec.ts';
|
|
6
|
-
import { withRetry } from '../../utils/retry.ts';
|
|
7
|
-
import type { DeviceInfo } from '../../utils/device.ts';
|
|
8
|
-
import type { RawSnapshotNode } from '../../utils/snapshot.ts';
|
|
9
|
-
|
|
10
|
-
type AXFrame = { x: number; y: number; width: number; height: number };
|
|
11
|
-
type AXNode = {
|
|
12
|
-
role?: string;
|
|
13
|
-
subrole?: string;
|
|
14
|
-
label?: string;
|
|
15
|
-
value?: string;
|
|
16
|
-
identifier?: string;
|
|
17
|
-
frame?: AXFrame;
|
|
18
|
-
children?: AXNode[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export async function snapshotAx(
|
|
22
|
-
device: DeviceInfo,
|
|
23
|
-
options: { traceLogPath?: string } = {},
|
|
24
|
-
): Promise<{ nodes: RawSnapshotNode[] }> {
|
|
25
|
-
if (device.platform !== 'ios' || device.kind !== 'simulator') {
|
|
26
|
-
throw new AppError('UNSUPPORTED_OPERATION', 'AX snapshot is only supported on iOS simulators');
|
|
27
|
-
}
|
|
28
|
-
const binary = await ensureAxSnapshotBinary();
|
|
29
|
-
const result = await withRetry(
|
|
30
|
-
async () => {
|
|
31
|
-
const attemptResult = await runCmd(binary, [], { allowFailure: true });
|
|
32
|
-
if (options.traceLogPath) {
|
|
33
|
-
appendAxTrace(options.traceLogPath, attemptResult);
|
|
34
|
-
}
|
|
35
|
-
if (attemptResult.exitCode !== 0) {
|
|
36
|
-
const stderrText = (attemptResult.stderr ?? '').toString();
|
|
37
|
-
const hint = axHintFromStderr(stderrText);
|
|
38
|
-
const retryable = isRetryableAxError(stderrText);
|
|
39
|
-
const error = new AppError('COMMAND_FAILED', 'AX snapshot failed', {
|
|
40
|
-
stderr: `${stderrText}${hint}`,
|
|
41
|
-
stdout: attemptResult.stdout,
|
|
42
|
-
retryable,
|
|
43
|
-
});
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
return attemptResult;
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
shouldRetry: (err) => isRetryableError(err),
|
|
50
|
-
},
|
|
51
|
-
);
|
|
52
|
-
let tree: AXNode;
|
|
53
|
-
let originFrame: AXFrame | undefined;
|
|
54
|
-
try {
|
|
55
|
-
const payload = JSON.parse(result.stdout) as
|
|
56
|
-
| AXNode
|
|
57
|
-
| { root?: AXNode; windowFrame?: AXFrame | null };
|
|
58
|
-
if (payload && typeof payload === 'object' && 'root' in payload) {
|
|
59
|
-
const snapshot = payload as { root?: AXNode; windowFrame?: AXFrame | null };
|
|
60
|
-
if (!snapshot.root) throw new Error('AX snapshot missing root');
|
|
61
|
-
tree = snapshot.root;
|
|
62
|
-
originFrame = snapshot.windowFrame ?? undefined;
|
|
63
|
-
} else {
|
|
64
|
-
tree = payload as AXNode;
|
|
65
|
-
}
|
|
66
|
-
} catch (err) {
|
|
67
|
-
throw new AppError('COMMAND_FAILED', 'Invalid AX snapshot JSON', { error: String(err) });
|
|
68
|
-
}
|
|
69
|
-
const rootFrame = tree.frame ?? originFrame;
|
|
70
|
-
const frameSamples: AXFrame[] = [];
|
|
71
|
-
const nodes: Array<AXNode & { depth: number }> = [];
|
|
72
|
-
const walk = (node: AXNode, depth: number) => {
|
|
73
|
-
if (node.frame) frameSamples.push(node.frame);
|
|
74
|
-
const frame = node.frame && rootFrame
|
|
75
|
-
? {
|
|
76
|
-
x: node.frame.x - rootFrame.x,
|
|
77
|
-
y: node.frame.y - rootFrame.y,
|
|
78
|
-
width: node.frame.width,
|
|
79
|
-
height: node.frame.height,
|
|
80
|
-
}
|
|
81
|
-
: node.frame;
|
|
82
|
-
nodes.push({ ...node, frame, children: undefined, depth });
|
|
83
|
-
for (const child of node.children ?? []) {
|
|
84
|
-
walk(child, depth + 1);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
walk(tree, 0);
|
|
88
|
-
const normalized = normalizeFrames(nodes, rootFrame, frameSamples);
|
|
89
|
-
const mapped = normalized.map((node, index) => ({
|
|
90
|
-
index,
|
|
91
|
-
type: node.subrole ?? node.role,
|
|
92
|
-
label: node.label,
|
|
93
|
-
value: node.value,
|
|
94
|
-
identifier: node.identifier,
|
|
95
|
-
rect: node.frame
|
|
96
|
-
? {
|
|
97
|
-
x: node.frame.x,
|
|
98
|
-
y: node.frame.y,
|
|
99
|
-
width: node.frame.width,
|
|
100
|
-
height: node.frame.height,
|
|
101
|
-
}
|
|
102
|
-
: undefined,
|
|
103
|
-
depth: node.depth,
|
|
104
|
-
}));
|
|
105
|
-
return { nodes: mapped };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function axHintFromStderr(stderrText: string): string {
|
|
109
|
-
const lower = stderrText.toLowerCase();
|
|
110
|
-
if (lower.includes('accessibility permission')) {
|
|
111
|
-
return (
|
|
112
|
-
' Enable Accessibility for your terminal in System Settings > Privacy & Security > Accessibility, ' +
|
|
113
|
-
'or use --backend xctest (slower snapshots via XCTest).'
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
if (lower.includes('could not find ios app content')) {
|
|
117
|
-
return ' AX snapshot sometimes caches empty content. Try restarting the Simulator app.';
|
|
118
|
-
}
|
|
119
|
-
return '';
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function appendAxTrace(traceLogPath: string, result: { stdout?: string; stderr?: string; exitCode: number }): void {
|
|
123
|
-
const stdoutText = (result.stdout ?? '').toString();
|
|
124
|
-
const stderrText = (result.stderr ?? '').toString();
|
|
125
|
-
const header = `\n[axsnapshot] exit=${result.exitCode} stdoutBytes=${stdoutText.length} stderrBytes=${stderrText.length}\n`;
|
|
126
|
-
fs.appendFileSync(traceLogPath, header);
|
|
127
|
-
if (result.exitCode !== 0 || stderrText.length > 0) {
|
|
128
|
-
if (stderrText.length > 0) {
|
|
129
|
-
fs.appendFileSync(traceLogPath, `${stderrText}\n`);
|
|
130
|
-
}
|
|
131
|
-
if (result.exitCode !== 0 && stdoutText.length > 0) {
|
|
132
|
-
fs.appendFileSync(traceLogPath, `${stdoutText}\n`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function isRetryableAxError(stderrText: string): boolean {
|
|
138
|
-
const lower = stderrText.toLowerCase();
|
|
139
|
-
if (lower.includes('could not find ios app content')) return true;
|
|
140
|
-
if (lower.includes('timeout')) return true;
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function isRetryableError(err: unknown): boolean {
|
|
145
|
-
return err instanceof AppError && err.code === 'COMMAND_FAILED' && err.details?.retryable === true;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function normalizeFrames(
|
|
149
|
-
nodes: Array<AXNode & { depth: number }>,
|
|
150
|
-
originFrame: AXFrame | undefined,
|
|
151
|
-
frames: AXFrame[],
|
|
152
|
-
): Array<AXNode & { depth: number }> {
|
|
153
|
-
if (!originFrame || frames.length === 0) return nodes;
|
|
154
|
-
let minX = Number.POSITIVE_INFINITY;
|
|
155
|
-
let minY = Number.POSITIVE_INFINITY;
|
|
156
|
-
for (const frame of frames) {
|
|
157
|
-
if (frame.x < minX) minX = frame.x;
|
|
158
|
-
if (frame.y < minY) minY = frame.y;
|
|
159
|
-
}
|
|
160
|
-
const nearZero = minX <= 5 && minY <= 5;
|
|
161
|
-
if (nearZero) {
|
|
162
|
-
return nodes.map((node) => ({
|
|
163
|
-
...node,
|
|
164
|
-
frame: node.frame
|
|
165
|
-
? {
|
|
166
|
-
x: node.frame.x + originFrame.x,
|
|
167
|
-
y: node.frame.y + originFrame.y,
|
|
168
|
-
width: node.frame.width,
|
|
169
|
-
height: node.frame.height,
|
|
170
|
-
}
|
|
171
|
-
: undefined,
|
|
172
|
-
}));
|
|
173
|
-
}
|
|
174
|
-
return nodes;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async function ensureAxSnapshotBinary(): Promise<string> {
|
|
178
|
-
const projectRoot = findProjectRoot();
|
|
179
|
-
const packageDir = path.join(projectRoot, 'ios-runner', 'AXSnapshot');
|
|
180
|
-
const envPath = process.env.AGENT_DEVICE_AX_BINARY;
|
|
181
|
-
if (envPath && fs.existsSync(envPath)) return envPath;
|
|
182
|
-
const packagedPath = path.join(projectRoot, 'dist', 'bin', 'axsnapshot');
|
|
183
|
-
if (fs.existsSync(packagedPath)) return packagedPath;
|
|
184
|
-
const binaryPath = path.join(packageDir, '.build', 'release', 'axsnapshot');
|
|
185
|
-
if (fs.existsSync(binaryPath)) return binaryPath;
|
|
186
|
-
const result = await runCmd('swift', ['build', '-c', 'release'], {
|
|
187
|
-
cwd: packageDir,
|
|
188
|
-
allowFailure: true,
|
|
189
|
-
});
|
|
190
|
-
if (result.exitCode !== 0 || !fs.existsSync(binaryPath)) {
|
|
191
|
-
throw new AppError('COMMAND_FAILED', 'Failed to build AX snapshot tool', {
|
|
192
|
-
stderr: result.stderr,
|
|
193
|
-
stdout: result.stdout,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
return binaryPath;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function findProjectRoot(): string {
|
|
200
|
-
let current = path.dirname(fileURLToPath(import.meta.url));
|
|
201
|
-
for (let i = 0; i < 6; i += 1) {
|
|
202
|
-
const pkgPath = path.join(current, 'package.json');
|
|
203
|
-
if (fs.existsSync(pkgPath)) return current;
|
|
204
|
-
current = path.dirname(current);
|
|
205
|
-
}
|
|
206
|
-
return process.cwd();
|
|
207
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { isEnvTruthy, TIMEOUT_PROFILES } from '../../utils/retry.ts';
|
|
2
|
-
import { resolveTimeoutMs } from '../../utils/timeouts.ts';
|
|
3
|
-
|
|
4
|
-
export const IOS_BOOT_TIMEOUT_MS = resolveTimeoutMs(
|
|
5
|
-
process.env.AGENT_DEVICE_IOS_BOOT_TIMEOUT_MS,
|
|
6
|
-
TIMEOUT_PROFILES.ios_boot.totalMs,
|
|
7
|
-
5_000,
|
|
8
|
-
);
|
|
9
|
-
|
|
10
|
-
export const IOS_SIMCTL_LIST_TIMEOUT_MS = resolveTimeoutMs(
|
|
11
|
-
process.env.AGENT_DEVICE_IOS_SIMCTL_LIST_TIMEOUT_MS,
|
|
12
|
-
TIMEOUT_PROFILES.ios_boot.operationMs,
|
|
13
|
-
1_000,
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
export const IOS_APP_LAUNCH_TIMEOUT_MS = resolveTimeoutMs(
|
|
17
|
-
process.env.AGENT_DEVICE_IOS_APP_LAUNCH_TIMEOUT_MS,
|
|
18
|
-
30_000,
|
|
19
|
-
5_000,
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
export const IOS_DEVICECTL_TIMEOUT_MS = resolveTimeoutMs(
|
|
23
|
-
process.env.AGENT_DEVICE_IOS_DEVICECTL_TIMEOUT_MS,
|
|
24
|
-
20_000,
|
|
25
|
-
1_000,
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
export const RETRY_LOGS_ENABLED = isEnvTruthy(process.env.AGENT_DEVICE_RETRY_LOGS);
|