|
| 1 | +import path from 'node:path'; |
| 2 | +import { performance } from 'node:perf_hooks'; |
| 3 | +import { pathToFileURL } from 'node:url'; |
| 4 | +import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client'; |
| 5 | +import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution'; |
| 6 | +import { createRequestId } from '@agent-device/host-kit/diagnostics'; |
| 7 | +import { captureHostEnvironment } from '@agent-device/host-kit/process'; |
| 8 | +import type { SnapshotPreferredBackend } from '@agent-device/kernel/snapshot'; |
| 9 | +import { readDaemonInfo } from '../../src/daemon-client/daemon-client-metadata.ts'; |
| 10 | +import { sendRequest } from '../../src/daemon-client/daemon-client-transport.ts'; |
| 11 | +import { REPO_ROOT } from './config.ts'; |
| 12 | +import type { CliResult } from './types.ts'; |
| 13 | + |
| 14 | +type AgentDeviceSdk = typeof import('../../src/sdk/index.ts'); |
| 15 | + |
| 16 | +const FORCED_CAPTURE_TIMEOUT_MS = 90_000; |
| 17 | + |
| 18 | +/** |
| 19 | + * Captures one raw snapshot with the internal backend pin that the CLI does not expose. The |
| 20 | + * transport talks to the daemon that the built CLI already started and never starts or replaces |
| 21 | + * it, so the measured daemon stays the daemon under test. |
| 22 | + */ |
| 23 | +export async function captureWithForcedBackend(options: { |
| 24 | + stateDir: string; |
| 25 | + session: string; |
| 26 | + udid: string; |
| 27 | + backend: SnapshotPreferredBackend; |
| 28 | +}): Promise<{ result: CliResult; requestId: string }> { |
| 29 | + const requestId = createRequestId(); |
| 30 | + const sdk = (await import( |
| 31 | + pathToFileURL(path.join(REPO_ROOT, 'dist/src/index.js')).href |
| 32 | + )) as AgentDeviceSdk; |
| 33 | + const client = sdk.createAgentDeviceClient( |
| 34 | + { session: options.session, stateDir: options.stateDir }, |
| 35 | + { transport: forcedBackendTransport(options.backend, requestId) }, |
| 36 | + ); |
| 37 | + const t0 = performance.now(); |
| 38 | + try { |
| 39 | + const snapshot = await client.capture.snapshot({ |
| 40 | + raw: true, |
| 41 | + platform: 'ios', |
| 42 | + udid: options.udid, |
| 43 | + timeoutMs: FORCED_CAPTURE_TIMEOUT_MS, |
| 44 | + }); |
| 45 | + const json = { success: true, data: snapshot }; |
| 46 | + const wallClockMs = performance.now() - t0; |
| 47 | + return { |
| 48 | + result: { exitCode: 0, wallClockMs, stdout: '', stderr: '', json, ok: true }, |
| 49 | + requestId, |
| 50 | + }; |
| 51 | + } catch (error) { |
| 52 | + const normalized = sdk.normalizeAgentDeviceError(error); |
| 53 | + const json = { success: false, error: normalized }; |
| 54 | + const wallClockMs = performance.now() - t0; |
| 55 | + return { |
| 56 | + result: { exitCode: 1, wallClockMs, stdout: '', stderr: normalized.message, json, ok: false }, |
| 57 | + requestId, |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function forcedBackendTransport( |
| 63 | + backend: SnapshotPreferredBackend, |
| 64 | + requestId: string, |
| 65 | +): AgentDeviceDaemonTransport { |
| 66 | + const host = captureHostEnvironment(process); |
| 67 | + return async (request, context) => { |
| 68 | + const stateDir = request.flags?.stateDir; |
| 69 | + if (request.session === undefined || typeof stateDir !== 'string') { |
| 70 | + throw new Error('A forced-backend capture requires an explicit session and state dir.'); |
| 71 | + } |
| 72 | + const paths = resolveDaemonPaths(stateDir, { env: process.env }); |
| 73 | + const info = readDaemonInfo(paths.infoPath); |
| 74 | + if (!info) throw new Error(`No daemon is running for state dir ${stateDir}.`); |
| 75 | + return await sendRequest( |
| 76 | + info, |
| 77 | + { |
| 78 | + ...request, |
| 79 | + session: request.session, |
| 80 | + token: info.token, |
| 81 | + flags: { ...request.flags, snapshotPreferredBackend: backend }, |
| 82 | + meta: { ...request.meta, debug: true, requestId }, |
| 83 | + }, |
| 84 | + 'auto', |
| 85 | + paths, |
| 86 | + FORCED_CAPTURE_TIMEOUT_MS, |
| 87 | + { host, signal: context?.signal, timeoutRecovery: 'none' }, |
| 88 | + ); |
| 89 | + }; |
| 90 | +} |
0 commit comments