Repository navigation
Expand file tree
/
Copy pathplatform-runtime-resource-cleanup.ts
More file actions
115 lines (109 loc) · 4.41 KB
/
Copy pathplatform-runtime-resource-cleanup.ts
File metadata and controls
115 lines (109 loc) · 4.41 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { DeviceInfo } from '@agent-device/kernel/device';
import type { HostEnvironment } from '@agent-device/kernel/source-value';
import { isIosFamily } from '@agent-device/kernel/device';
import type { PlatformResourceCleanup } from '@agent-device/daemon';
import { emitDiagnostic, type ResourceDiagnostic } from '@agent-device/host-kit/diagnostics';
import { type OwnedProcessRecordStore } from '@agent-device/host-kit/process';
import { loadAndroidMechanics } from './platform-runtime-android-mechanics.ts';
/** Focused durable-resource cleanup composed above daemon policy and concrete platforms. */
export async function resetAndroidSnapshotHelperRuntime(): Promise<void> {
const { resetAndroidSnapshotHelperSessions } = await loadAndroidMechanics();
await resetAndroidSnapshotHelperSessions();
}
async function stopAndroidSnapshotHelperRuntimeForDevice(device: DeviceInfo): Promise<void> {
const { stopAndroidSnapshotHelperSessionForDevice } = await loadAndroidMechanics();
await stopAndroidSnapshotHelperSessionForDevice(device);
}
/** Only a macOS host has an `XCTestDevices` an older agent-device could have redirected. */
export async function restoreLegacyXctestDeviceSetRedirectRuntime(
onDiagnostic: (diagnostic: ResourceDiagnostic) => void,
): Promise<void> {
if (process.platform !== 'darwin') return;
const { restoreLegacyXctestDeviceSetRedirect } =
await import('@agent-device/platform-apple/runner-owner');
await restoreLegacyXctestDeviceSetRedirect(onDiagnostic);
}
export async function cleanupManagedWebRuntimeOrphans(
params: {
stateDir: string;
openWebSessionNames: readonly string[];
ownedProcessRecords?: OwnedProcessRecordStore;
},
host: HostEnvironment,
): Promise<void> {
const { getManagedAgentBrowserStatus } = await import('@agent-device/platform-web');
const status = await getManagedAgentBrowserStatus({ stateDir: params.stateDir, host });
if (!status.installed) return;
const { cleanupManagedAgentBrowserOrphans } = await import('@agent-device/platform-web');
await cleanupManagedAgentBrowserOrphans(status, 'daemon-startup', {
env: host.env,
openWebSessionNames: params.openWebSessionNames,
...(params.ownedProcessRecords === undefined
? {}
: { ownedProcessRecords: params.ownedProcessRecords }),
});
}
async function closeManagedWebRuntimeSession(
params: {
sessionName: string;
stateDir: string;
openWebSessionNames: () => readonly string[];
},
host: HostEnvironment,
): Promise<void> {
const { createAgentBrowserWebProvider } = await import('@agent-device/platform-web');
const provider = await createAgentBrowserWebProvider({
host,
session: params.sessionName,
stateDir: params.stateDir,
openWebSessionNames: params.openWebSessionNames,
});
await provider.close();
}
export function createPlatformResourceCleanup(host: HostEnvironment): PlatformResourceCleanup {
return Object.freeze({
async stopSnapshotHelper(device) {
if (device.platform !== 'android') return;
await stopAndroidSnapshotHelperRuntimeForDevice(device);
},
async closeManagedBrowser(params) {
if (params.device.platform !== 'web') return;
await closeManagedWebRuntimeSession(
{
sessionName: params.sessionName,
stateDir: params.stateDir,
openWebSessionNames: params.openSessionNames,
},
host,
);
},
async cleanupSessionlessExecutionHost(device) {
if (!isIosFamily(device)) return;
const { resolveRunnerAppBundleId, stopIosRunnerSession } =
await import('@agent-device/platform-apple/runner/operations');
await stopIosRunnerSession(device.id);
const bundleId = await resolveRunnerAppBundleId();
const { closeIosApp } = await import('@agent-device/platform-apple/app-lifecycle');
await closeIosApp(device, bundleId, undefined, host.env).catch((error) => {
emitDiagnostic({
level: 'debug',
phase: 'ios_sessionless_runner_host_close_failed',
data: {
deviceId: device.id,
bundleId,
error: error instanceof Error ? error.message : String(error),
},
});
});
},
retainExecutionHostAfterClose(params) {
return (
isIosFamily(params.device) &&
params.device.kind === 'simulator' &&
!params.shutdownRequested &&
!params.hasScreenRecording &&
!params.leaseWillEnd
);
},
});
}