Repository navigation
Expand file tree
/
Copy pathsnapshot-session.ts
More file actions
60 lines (57 loc) · 2.13 KB
/
Copy pathsnapshot-session.ts
File metadata and controls
60 lines (57 loc) · 2.13 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
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
import type { PlatformResourceCleanup } from './platform-resource-cleanup.ts';
import type { DaemonRequest } from './daemon-request.ts';
import type { SessionRef, SessionState } from './session-state.ts';
import type { SessionScope } from '@agent-device/contracts/session';
import { isActiveProviderDevice } from './provider-device-admission.ts';
import { SessionStore } from './session-store.ts';
export async function resolveSessionDevice(
sessionStore: SessionStore,
sessionName: string,
flags: DaemonRequest['flags'],
boundRef?: SessionRef,
) {
const ref = boundRef ?? sessionStore.lookup(sessionName);
const session = ref ? sessionStore.requireCurrent(ref) : undefined;
const device = session?.device ?? (await resolveTargetDevice(flags ?? {}));
return { ref, session, device };
}
export async function withSessionlessRunnerCleanup<T>(
session: SessionState | undefined,
device: SessionState['device'],
task: () => Promise<T>,
platformCleanup?: PlatformResourceCleanup,
): Promise<T> {
if (!session && !platformCleanup) {
throw new Error('Platform resource cleanup was not injected');
}
try {
return await task();
} finally {
// Only a device this daemon prepared a local execution host for can have one to release. A
// provider-owned device runs on provider infrastructure, where local teardown drives host
// tooling at a device id this host does not own.
if (!session && !isActiveProviderDevice(device)) {
await platformCleanup!.cleanupSessionlessExecutionHost(device);
}
}
}
export function createSnapshotSession(params: {
sessionName: string;
sessionScope: SessionScope;
device: SessionState['device'];
snapshot: SessionState['snapshot'];
appBundleId?: string;
}): SessionState {
const { sessionName, sessionScope, device, snapshot, appBundleId } = params;
return {
name: sessionName,
sessionScope,
device,
createdAt: Date.now(),
appBundleId,
snapshot,
...(snapshot?.comparisonSafe === true ? { lastComparisonSafeSnapshot: snapshot } : {}),
actions: [],
};
}