Repository navigation
Expand file tree
/
Copy pathreplay-device-selection.ts
More file actions
88 lines (82 loc) · 3.56 KB
/
Copy pathreplay-device-selection.ts
File metadata and controls
88 lines (82 loc) · 3.56 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
import { parseReplayInput, resolveReplayFormat } from '@agent-device/ad-script';
import type { ResolveTargetDeviceOptions } from '@agent-device/device-selection/dispatch-resolve';
import type { CommandFlags } from '@agent-device/contracts/command';
import { readReplayScriptSourceFile } from '@agent-device/replay-port/script-source';
import type { DaemonRequest } from './daemon-request.ts';
import {
appTargetResolutionOptions,
buildMaestroReplayTargetDeviceResolutionOptions,
buildReplayScriptPlatformFlags,
readScriptReplaySelection,
} from '@agent-device/replay-port/replay-script-selection';
export type ReplayTargetDeviceResolution = {
flags: CommandFlags;
options: ResolveTargetDeviceOptions | undefined;
};
/**
* Finds a static first app target for fresh replay binding. Request binding
* must leave a first deep-link or dynamic open to normal device resolution.
*
* #1802: reads the request's replay script source bundle, never the
* filesystem. Before that this opened the caller's path here too, so against a
* remote daemon it silently no-opped (the read threw and the catch below
* swallowed it) and every remote replay lost its pre-binding.
*
* The Maestro engine loads only once the entry resolves to a flow: request
* binding is in the daemon's startup closure, and most replays are `.ad`.
*/
export async function buildReplayTargetDeviceResolution(
req: DaemonRequest,
): Promise<ReplayTargetDeviceResolution | undefined> {
if (req.command !== 'replay' || req.flags?.replayFrom !== undefined) return undefined;
const bundle = req.flags?.replayScriptSource;
if (!bundle) return undefined;
return readAdvisoryResolution(async () => {
const resolved = bundle.entry;
const source = readReplayScriptSourceFile(bundle, resolved);
if (resolveReplayFormat(resolved, req.flags?.replayBackend) === 'maestro') {
return await readMaestroReplayResolution(source, resolved, req.flags);
}
return readAdScriptResolution(source, req.flags);
});
}
async function readMaestroReplayResolution(
source: string,
resolvedPath: string,
flags: DaemonRequest['flags'],
): Promise<ReplayTargetDeviceResolution> {
const { inspectMaestroFlow } = await import('@agent-device/maestro');
const flow = inspectMaestroFlow(source, resolvedPath);
return {
flags: flags ?? {},
options: buildMaestroReplayTargetDeviceResolutionOptions(flow.appTarget, flags?.platform),
};
}
function readAdScriptResolution(
source: string,
flags: DaemonRequest['flags'],
): ReplayTargetDeviceResolution | undefined {
const parsed = parseReplayInput(source, flags);
const selection = readScriptReplaySelection(parsed.actions);
if (!selection.appTarget) return undefined;
const scriptFlags = buildReplayScriptPlatformFlags(flags, parsed.actions);
const platform = scriptFlags.platform ?? parsed.metadata.platform;
return {
flags:
platform && scriptFlags.platform === undefined ? { ...scriptFlags, platform } : scriptFlags,
options: platform === 'ios' ? appTargetResolutionOptions(selection.appTarget) : undefined,
};
}
async function readAdvisoryResolution(
read: () => Promise<ReplayTargetDeviceResolution | undefined>,
): Promise<ReplayTargetDeviceResolution | undefined> {
try {
return await read();
} catch {
// Parsing and validation stay in the replay handler. Lock binding is only
// advisory, so an unreadable/invalid plan must not mask its real error. The
// whole probe sits behind this guard, including the wire bundle's entry and
// format, which the request boundary only checks as an object.
return undefined;
}
}