agent-device 0.4.0 → 0.4.2

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.
Files changed (52) hide show
  1. package/README.md +20 -12
  2. package/dist/src/797.js +1 -0
  3. package/dist/src/bin.js +40 -29
  4. package/dist/src/daemon.js +21 -17
  5. package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +8 -2
  6. package/package.json +2 -2
  7. package/skills/agent-device/SKILL.md +23 -14
  8. package/skills/agent-device/references/permissions.md +7 -2
  9. package/skills/agent-device/references/session-management.md +5 -1
  10. package/src/__tests__/cli-close.test.ts +155 -0
  11. package/src/__tests__/cli-help.test.ts +102 -0
  12. package/src/cli.ts +68 -22
  13. package/src/core/__tests__/capabilities.test.ts +2 -1
  14. package/src/core/__tests__/dispatch-open.test.ts +25 -0
  15. package/src/core/__tests__/open-target.test.ts +40 -1
  16. package/src/core/capabilities.ts +1 -1
  17. package/src/core/dispatch.ts +22 -0
  18. package/src/core/open-target.ts +14 -0
  19. package/src/daemon/__tests__/device-ready.test.ts +52 -0
  20. package/src/daemon/__tests__/session-store.test.ts +23 -0
  21. package/src/daemon/device-ready.ts +146 -4
  22. package/src/daemon/handlers/__tests__/session.test.ts +477 -0
  23. package/src/daemon/handlers/session.ts +198 -93
  24. package/src/daemon/handlers/snapshot.ts +210 -185
  25. package/src/daemon/session-store.ts +16 -6
  26. package/src/daemon/types.ts +2 -1
  27. package/src/daemon-client.ts +138 -17
  28. package/src/daemon.ts +99 -9
  29. package/src/platforms/android/__tests__/index.test.ts +118 -1
  30. package/src/platforms/android/index.ts +77 -47
  31. package/src/platforms/ios/__tests__/index.test.ts +292 -4
  32. package/src/platforms/ios/__tests__/runner-client.test.ts +42 -0
  33. package/src/platforms/ios/apps.ts +358 -0
  34. package/src/platforms/ios/config.ts +28 -0
  35. package/src/platforms/ios/devicectl.ts +134 -0
  36. package/src/platforms/ios/devices.ts +15 -2
  37. package/src/platforms/ios/index.ts +20 -455
  38. package/src/platforms/ios/runner-client.ts +171 -69
  39. package/src/platforms/ios/simulator.ts +164 -0
  40. package/src/utils/__tests__/args.test.ts +66 -2
  41. package/src/utils/__tests__/daemon-client.test.ts +95 -0
  42. package/src/utils/__tests__/keyed-lock.test.ts +55 -0
  43. package/src/utils/__tests__/process-identity.test.ts +33 -0
  44. package/src/utils/args.ts +37 -1
  45. package/src/utils/command-schema.ts +58 -27
  46. package/src/utils/interactors.ts +2 -2
  47. package/src/utils/keyed-lock.ts +14 -0
  48. package/src/utils/process-identity.ts +100 -0
  49. package/src/utils/timeouts.ts +9 -0
  50. package/dist/src/274.js +0 -1
  51. package/src/daemon/__tests__/app-state.test.ts +0 -138
  52. package/src/daemon/app-state.ts +0 -65
@@ -1,10 +1,13 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
- import { openIosApp } from '../index.ts';
3
+ import { promises as fs } from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { listIosApps, openIosApp, parseIosDeviceAppsPayload, resolveIosApp } from '../index.ts';
4
7
  import type { DeviceInfo } from '../../../utils/device.ts';
5
8
  import { AppError } from '../../../utils/errors.ts';
6
9
 
7
- test('openIosApp rejects deep links on iOS physical devices', async () => {
10
+ test('openIosApp custom scheme deep links on iOS devices require app bundle context', async () => {
8
11
  const device: DeviceInfo = {
9
12
  platform: 'ios',
10
13
  id: 'ios-device-1',
@@ -14,11 +17,296 @@ test('openIosApp rejects deep links on iOS physical devices', async () => {
14
17
  };
15
18
 
16
19
  await assert.rejects(
17
- () => openIosApp(device, 'https://example.com/path'),
20
+ () => openIosApp(device, 'myapp://home'),
18
21
  (error: unknown) => {
19
22
  assert.equal(error instanceof AppError, true);
20
- assert.equal((error as AppError).code, 'UNSUPPORTED_OPERATION');
23
+ assert.equal((error as AppError).code, 'INVALID_ARGS');
21
24
  return true;
22
25
  },
23
26
  );
24
27
  });
28
+
29
+ test('openIosApp web URL on iOS device without app falls back to Safari', async () => {
30
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-safari-test-'));
31
+ const xcrunPath = path.join(tmpDir, 'xcrun');
32
+ const argsLogPath = path.join(tmpDir, 'args.log');
33
+ await fs.writeFile(
34
+ xcrunPath,
35
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
36
+ 'utf8',
37
+ );
38
+ await fs.chmod(xcrunPath, 0o755);
39
+
40
+ const previousPath = process.env.PATH;
41
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
42
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
43
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
44
+
45
+ const device: DeviceInfo = {
46
+ platform: 'ios',
47
+ id: 'ios-device-1',
48
+ name: 'iPhone Device',
49
+ kind: 'device',
50
+ booted: true,
51
+ };
52
+
53
+ try {
54
+ await openIosApp(device, 'https://example.com/path');
55
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
56
+ .trim()
57
+ .split('\n')
58
+ .filter(Boolean);
59
+ assert.deepEqual(args, [
60
+ 'devicectl',
61
+ 'device',
62
+ 'process',
63
+ 'launch',
64
+ '--device',
65
+ 'ios-device-1',
66
+ 'com.apple.mobilesafari',
67
+ '--payload-url',
68
+ 'https://example.com/path',
69
+ ]);
70
+ } finally {
71
+ process.env.PATH = previousPath;
72
+ if (previousArgsFile === undefined) {
73
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
74
+ } else {
75
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
76
+ }
77
+ await fs.rm(tmpDir, { recursive: true, force: true });
78
+ }
79
+ });
80
+
81
+ test('openIosApp custom scheme on iOS device uses active app context', async () => {
82
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-openurl-test-'));
83
+ const xcrunPath = path.join(tmpDir, 'xcrun');
84
+ const argsLogPath = path.join(tmpDir, 'args.log');
85
+ await fs.writeFile(
86
+ xcrunPath,
87
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
88
+ 'utf8',
89
+ );
90
+ await fs.chmod(xcrunPath, 0o755);
91
+
92
+ const previousPath = process.env.PATH;
93
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
94
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
95
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
96
+
97
+ const device: DeviceInfo = {
98
+ platform: 'ios',
99
+ id: 'ios-device-1',
100
+ name: 'iPhone Device',
101
+ kind: 'device',
102
+ booted: true,
103
+ };
104
+
105
+ try {
106
+ await openIosApp(device, 'myapp://item/42', { appBundleId: 'com.example.app' });
107
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
108
+ .trim()
109
+ .split('\n')
110
+ .filter(Boolean);
111
+ assert.deepEqual(args, [
112
+ 'devicectl',
113
+ 'device',
114
+ 'process',
115
+ 'launch',
116
+ '--device',
117
+ 'ios-device-1',
118
+ 'com.example.app',
119
+ '--payload-url',
120
+ 'myapp://item/42',
121
+ ]);
122
+ } finally {
123
+ process.env.PATH = previousPath;
124
+ if (previousArgsFile === undefined) {
125
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
126
+ } else {
127
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
128
+ }
129
+ await fs.rm(tmpDir, { recursive: true, force: true });
130
+ }
131
+ });
132
+
133
+ test('openIosApp with app and URL on iOS device launches app bundle with payload URL', async () => {
134
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-open-app-url-test-'));
135
+ const xcrunPath = path.join(tmpDir, 'xcrun');
136
+ const argsLogPath = path.join(tmpDir, 'args.log');
137
+ await fs.writeFile(
138
+ xcrunPath,
139
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
140
+ 'utf8',
141
+ );
142
+ await fs.chmod(xcrunPath, 0o755);
143
+
144
+ const previousPath = process.env.PATH;
145
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
146
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
147
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
148
+
149
+ const device: DeviceInfo = {
150
+ platform: 'ios',
151
+ id: 'ios-device-1',
152
+ name: 'iPhone Device',
153
+ kind: 'device',
154
+ booted: true,
155
+ };
156
+
157
+ try {
158
+ await openIosApp(device, 'MyApp', { appBundleId: 'com.example.app', url: 'myapp://screen/to' });
159
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
160
+ .trim()
161
+ .split('\n')
162
+ .filter(Boolean);
163
+ assert.deepEqual(args, [
164
+ 'devicectl',
165
+ 'device',
166
+ 'process',
167
+ 'launch',
168
+ '--device',
169
+ 'ios-device-1',
170
+ 'com.example.app',
171
+ '--payload-url',
172
+ 'myapp://screen/to',
173
+ ]);
174
+ } finally {
175
+ process.env.PATH = previousPath;
176
+ if (previousArgsFile === undefined) {
177
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
178
+ } else {
179
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
180
+ }
181
+ await fs.rm(tmpDir, { recursive: true, force: true });
182
+ }
183
+ });
184
+
185
+ test('parseIosDeviceAppsPayload maps devicectl app entries', () => {
186
+ const apps = parseIosDeviceAppsPayload({
187
+ result: {
188
+ apps: [
189
+ {
190
+ bundleIdentifier: 'com.apple.Maps',
191
+ name: 'Maps',
192
+ },
193
+ {
194
+ bundleIdentifier: 'com.example.NoName',
195
+ },
196
+ ],
197
+ },
198
+ });
199
+
200
+ assert.equal(apps.length, 2);
201
+ assert.deepEqual(apps[0], {
202
+ bundleId: 'com.apple.Maps',
203
+ name: 'Maps',
204
+ });
205
+ assert.equal(apps[1].bundleId, 'com.example.NoName');
206
+ assert.equal(apps[1].name, 'com.example.NoName');
207
+ });
208
+
209
+ test('parseIosDeviceAppsPayload ignores malformed entries', () => {
210
+ const apps = parseIosDeviceAppsPayload({
211
+ result: {
212
+ apps: [
213
+ null,
214
+ {},
215
+ { name: 'Missing bundle id' },
216
+ { bundleIdentifier: '' },
217
+ ],
218
+ },
219
+ });
220
+ assert.deepEqual(apps, []);
221
+ });
222
+
223
+ test('resolveIosApp resolves app display name on iOS physical devices', async () => {
224
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-app-resolve-'));
225
+ const xcrunPath = path.join(tmpDir, 'xcrun');
226
+ await fs.writeFile(
227
+ xcrunPath,
228
+ [
229
+ '#!/bin/sh',
230
+ 'if [ "$1" = "devicectl" ] && [ "$2" = "device" ] && [ "$3" = "info" ] && [ "$4" = "apps" ]; then',
231
+ ' out=""',
232
+ ' while [ "$#" -gt 0 ]; do',
233
+ ' if [ "$1" = "--json-output" ]; then',
234
+ ' out="$2"',
235
+ ' shift 2',
236
+ ' continue',
237
+ ' fi',
238
+ ' shift',
239
+ ' done',
240
+ " cat > \"$out\" <<'JSON'",
241
+ '{"result":{"apps":[{"bundleIdentifier":"com.apple.Maps","name":"Maps"},{"bundleIdentifier":"com.example.demo","name":"Demo"}]}}',
242
+ 'JSON',
243
+ ' exit 0',
244
+ 'fi',
245
+ 'echo "unexpected xcrun args: $@" >&2',
246
+ 'exit 1',
247
+ '',
248
+ ].join('\n'),
249
+ 'utf8',
250
+ );
251
+ await fs.chmod(xcrunPath, 0o755);
252
+
253
+ const previousPath = process.env.PATH;
254
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
255
+
256
+ const device: DeviceInfo = {
257
+ platform: 'ios',
258
+ id: 'ios-device-1',
259
+ name: 'iPhone Device',
260
+ kind: 'device',
261
+ booted: true,
262
+ };
263
+
264
+ try {
265
+ const bundleId = await resolveIosApp(device, 'Maps');
266
+ assert.equal(bundleId, 'com.apple.Maps');
267
+ } finally {
268
+ process.env.PATH = previousPath;
269
+ await fs.rm(tmpDir, { recursive: true, force: true });
270
+ }
271
+ });
272
+
273
+ test('listIosApps applies user-installed filter on simulator', async () => {
274
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-list-sim-'));
275
+ const xcrunPath = path.join(tmpDir, 'xcrun');
276
+ await fs.writeFile(
277
+ xcrunPath,
278
+ [
279
+ '#!/bin/sh',
280
+ 'if [ "$1" = "simctl" ] && [ "$2" = "listapps" ]; then',
281
+ " cat <<'JSON'",
282
+ '{"com.apple.Maps":{"CFBundleDisplayName":"Maps"},"com.example.demo":{"CFBundleDisplayName":"Demo"}}',
283
+ 'JSON',
284
+ ' exit 0',
285
+ 'fi',
286
+ 'echo "unexpected xcrun args: $@" >&2',
287
+ 'exit 1',
288
+ '',
289
+ ].join('\n'),
290
+ 'utf8',
291
+ );
292
+ await fs.chmod(xcrunPath, 0o755);
293
+
294
+ const previousPath = process.env.PATH;
295
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
296
+
297
+ const device: DeviceInfo = {
298
+ platform: 'ios',
299
+ id: 'sim-1',
300
+ name: 'iPhone Sim',
301
+ kind: 'simulator',
302
+ booted: true,
303
+ };
304
+
305
+ try {
306
+ const apps = await listIosApps(device, 'user-installed');
307
+ assert.deepEqual(apps, [{ bundleId: 'com.example.demo', name: 'Demo' }]);
308
+ } finally {
309
+ process.env.PATH = previousPath;
310
+ await fs.rm(tmpDir, { recursive: true, force: true });
311
+ }
312
+ });
@@ -1,12 +1,16 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
3
  import type { DeviceInfo } from '../../../utils/device.ts';
4
+ import { AppError } from '../../../utils/errors.ts';
4
5
  import {
5
6
  assertSafeDerivedCleanup,
7
+ isRetryableRunnerError,
8
+ resolveRunnerEarlyExitHint,
6
9
  resolveRunnerBuildDestination,
7
10
  resolveRunnerDestination,
8
11
  resolveRunnerMaxConcurrentDestinationsFlag,
9
12
  resolveRunnerSigningBuildSettings,
13
+ shouldRetryRunnerConnectError,
10
14
  } from '../runner-client.ts';
11
15
 
12
16
  const iosSimulator: DeviceInfo = {
@@ -111,3 +115,41 @@ test('assertSafeDerivedCleanup allows cleaning override path with explicit opt-i
111
115
  });
112
116
  });
113
117
  });
118
+
119
+ test('resolveRunnerEarlyExitHint surfaces busy-connecting guidance', () => {
120
+ const hint = resolveRunnerEarlyExitHint(
121
+ 'Runner did not accept connection (xcodebuild exited early)',
122
+ 'Ineligible destinations for the "AgentDeviceRunner" scheme:\n{ error:Device is busy (Connecting to iPhone) }',
123
+ '',
124
+ );
125
+ assert.match(hint, /still connecting/i);
126
+ });
127
+
128
+ test('resolveRunnerEarlyExitHint falls back to runner connect timeout hint', () => {
129
+ const hint = resolveRunnerEarlyExitHint(
130
+ 'Runner did not accept connection (xcodebuild exited early)',
131
+ '',
132
+ 'xcodebuild failed unexpectedly',
133
+ );
134
+ assert.match(hint, /retry runner startup/i);
135
+ });
136
+
137
+ test('shouldRetryRunnerConnectError does not retry xcodebuild early-exit errors', () => {
138
+ const err = new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)');
139
+ assert.equal(shouldRetryRunnerConnectError(err), false);
140
+ });
141
+
142
+ test('shouldRetryRunnerConnectError retries transient connect errors', () => {
143
+ const err = new AppError('COMMAND_FAILED', 'Runner endpoint probe failed');
144
+ assert.equal(shouldRetryRunnerConnectError(err), true);
145
+ });
146
+
147
+ test('isRetryableRunnerError does not retry xcodebuild early-exit errors', () => {
148
+ const err = new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)');
149
+ assert.equal(isRetryableRunnerError(err), false);
150
+ });
151
+
152
+ test('isRetryableRunnerError does not retry busy-connecting errors', () => {
153
+ const err = new AppError('COMMAND_FAILED', 'Device is busy (Connecting to iPhone)');
154
+ assert.equal(isRetryableRunnerError(err), false);
155
+ });