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