agent-device 0.4.1 → 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.
Files changed (87) hide show
  1. package/README.md +19 -20
  2. package/dist/src/797.js +1 -1
  3. package/dist/src/bin.js +32 -32
  4. package/dist/src/daemon.js +17 -17
  5. package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +8 -2
  6. package/package.json +7 -9
  7. package/skills/agent-device/SKILL.md +22 -19
  8. package/skills/agent-device/references/permissions.md +10 -17
  9. package/skills/agent-device/references/session-management.md +3 -1
  10. package/skills/agent-device/references/snapshot-refs.md +1 -4
  11. package/dist/bin/axsnapshot +0 -0
  12. package/ios-runner/AXSnapshot/Package.swift +0 -18
  13. package/ios-runner/AXSnapshot/Sources/AXSnapshot/main.swift +0 -444
  14. package/src/__tests__/cli-help.test.ts +0 -102
  15. package/src/bin.ts +0 -3
  16. package/src/cli.ts +0 -289
  17. package/src/core/__tests__/capabilities.test.ts +0 -74
  18. package/src/core/__tests__/open-target.test.ts +0 -16
  19. package/src/core/capabilities.ts +0 -57
  20. package/src/core/dispatch.ts +0 -360
  21. package/src/core/open-target.ts +0 -13
  22. package/src/daemon/__tests__/app-state.test.ts +0 -138
  23. package/src/daemon/__tests__/is-predicates.test.ts +0 -68
  24. package/src/daemon/__tests__/selectors.test.ts +0 -261
  25. package/src/daemon/__tests__/session-routing.test.ts +0 -108
  26. package/src/daemon/__tests__/session-selector.test.ts +0 -64
  27. package/src/daemon/__tests__/session-store.test.ts +0 -142
  28. package/src/daemon/__tests__/snapshot-processing.test.ts +0 -47
  29. package/src/daemon/action-utils.ts +0 -29
  30. package/src/daemon/app-state.ts +0 -65
  31. package/src/daemon/context.ts +0 -48
  32. package/src/daemon/device-ready.ts +0 -13
  33. package/src/daemon/handlers/__tests__/find.test.ts +0 -99
  34. package/src/daemon/handlers/__tests__/interaction.test.ts +0 -22
  35. package/src/daemon/handlers/__tests__/replay-heal.test.ts +0 -509
  36. package/src/daemon/handlers/__tests__/session-reinstall.test.ts +0 -219
  37. package/src/daemon/handlers/__tests__/session.test.ts +0 -343
  38. package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +0 -92
  39. package/src/daemon/handlers/__tests__/snapshot.test.ts +0 -128
  40. package/src/daemon/handlers/find.ts +0 -324
  41. package/src/daemon/handlers/interaction.ts +0 -550
  42. package/src/daemon/handlers/parse-utils.ts +0 -8
  43. package/src/daemon/handlers/record-trace.ts +0 -154
  44. package/src/daemon/handlers/session.ts +0 -1032
  45. package/src/daemon/handlers/snapshot.ts +0 -439
  46. package/src/daemon/is-predicates.ts +0 -46
  47. package/src/daemon/selectors.ts +0 -540
  48. package/src/daemon/session-routing.ts +0 -22
  49. package/src/daemon/session-selector.ts +0 -39
  50. package/src/daemon/session-store.ts +0 -298
  51. package/src/daemon/snapshot-processing.ts +0 -131
  52. package/src/daemon/types.ts +0 -56
  53. package/src/daemon-client.ts +0 -172
  54. package/src/daemon.ts +0 -295
  55. package/src/platforms/__tests__/boot-diagnostics.test.ts +0 -59
  56. package/src/platforms/android/__tests__/index.test.ts +0 -157
  57. package/src/platforms/android/devices.ts +0 -196
  58. package/src/platforms/android/index.ts +0 -754
  59. package/src/platforms/android/ui-hierarchy.ts +0 -312
  60. package/src/platforms/boot-diagnostics.ts +0 -128
  61. package/src/platforms/ios/__tests__/index.test.ts +0 -24
  62. package/src/platforms/ios/__tests__/runner-client.test.ts +0 -113
  63. package/src/platforms/ios/ax-snapshot.ts +0 -207
  64. package/src/platforms/ios/devices.ts +0 -87
  65. package/src/platforms/ios/index.ts +0 -455
  66. package/src/platforms/ios/runner-client.ts +0 -938
  67. package/src/utils/__tests__/args.test.ts +0 -221
  68. package/src/utils/__tests__/daemon-client.test.ts +0 -78
  69. package/src/utils/__tests__/exec.test.ts +0 -16
  70. package/src/utils/__tests__/finders.test.ts +0 -34
  71. package/src/utils/__tests__/keyed-lock.test.ts +0 -55
  72. package/src/utils/__tests__/process-identity.test.ts +0 -33
  73. package/src/utils/__tests__/retry.test.ts +0 -44
  74. package/src/utils/args.ts +0 -234
  75. package/src/utils/command-schema.ts +0 -629
  76. package/src/utils/device.ts +0 -84
  77. package/src/utils/errors.ts +0 -35
  78. package/src/utils/exec.ts +0 -339
  79. package/src/utils/finders.ts +0 -101
  80. package/src/utils/interactive.ts +0 -4
  81. package/src/utils/interactors.ts +0 -173
  82. package/src/utils/keyed-lock.ts +0 -14
  83. package/src/utils/output.ts +0 -204
  84. package/src/utils/process-identity.ts +0 -100
  85. package/src/utils/retry.ts +0 -180
  86. package/src/utils/snapshot.ts +0 -64
  87. package/src/utils/version.ts +0 -26
@@ -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,87 +0,0 @@
1
- import { promises as fs } from 'node:fs';
2
- import os from 'node:os';
3
- import path from 'node:path';
4
- import { runCmd, whichCmd } from '../../utils/exec.ts';
5
- import { AppError } from '../../utils/errors.ts';
6
- import type { DeviceInfo } from '../../utils/device.ts';
7
-
8
- export async function listIosDevices(): Promise<DeviceInfo[]> {
9
- if (process.platform !== 'darwin') {
10
- throw new AppError('UNSUPPORTED_PLATFORM', 'iOS tools are only available on macOS');
11
- }
12
-
13
- const simctlAvailable = await whichCmd('xcrun');
14
- if (!simctlAvailable) {
15
- throw new AppError('TOOL_MISSING', 'xcrun not found in PATH');
16
- }
17
-
18
- const devices: DeviceInfo[] = [];
19
-
20
- const simResult = await runCmd('xcrun', ['simctl', 'list', 'devices', '-j']);
21
- try {
22
- const payload = JSON.parse(simResult.stdout as string) as {
23
- devices: Record<
24
- string,
25
- { name: string; udid: string; state: string; isAvailable: boolean }[]
26
- >;
27
- };
28
- for (const runtimes of Object.values(payload.devices)) {
29
- for (const device of runtimes) {
30
- if (!device.isAvailable) continue;
31
- devices.push({
32
- platform: 'ios',
33
- id: device.udid,
34
- name: device.name,
35
- kind: 'simulator',
36
- booted: device.state === 'Booted',
37
- });
38
- }
39
- }
40
- } catch (err) {
41
- throw new AppError('COMMAND_FAILED', 'Failed to parse simctl devices JSON', undefined, err);
42
- }
43
-
44
- let jsonPath: string | null = null;
45
- try {
46
- jsonPath = path.join(
47
- os.tmpdir(),
48
- `agent-device-devicectl-${process.pid}-${Date.now()}.json`,
49
- );
50
- await runCmd('xcrun', ['devicectl', 'list', 'devices', '--json-output', jsonPath]);
51
- const jsonText = await fs.readFile(jsonPath, 'utf8');
52
- const payload = JSON.parse(jsonText) as {
53
- result?: {
54
- devices?: Array<{
55
- identifier?: string;
56
- name?: string;
57
- hardwareProperties?: { platform?: string; udid?: string };
58
- deviceProperties?: { name?: string };
59
- connectionProperties?: { tunnelState?: string };
60
- }>;
61
- };
62
- };
63
- for (const device of payload.result?.devices ?? []) {
64
- const platform = device.hardwareProperties?.platform ?? '';
65
- if (platform.toLowerCase().includes('ios')) {
66
- const id = device.hardwareProperties?.udid ?? device.identifier ?? '';
67
- const name = device.name ?? device.deviceProperties?.name ?? id;
68
- if (!id) continue;
69
- devices.push({
70
- platform: 'ios',
71
- id,
72
- name,
73
- kind: 'device',
74
- booted: true,
75
- });
76
- }
77
- }
78
- } catch {
79
- // Ignore devicectl failures; simulators are still supported.
80
- } finally {
81
- if (jsonPath) {
82
- await fs.rm(jsonPath, { force: true }).catch(() => {});
83
- }
84
- }
85
-
86
- return devices;
87
- }