Repository navigation
Expand file tree
/
Copy pathruntime.ts
More file actions
552 lines (513 loc) · 19.7 KB
/
Copy pathruntime.ts
File metadata and controls
552 lines (513 loc) · 19.7 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import Limrun from '@limrun/api';
import type { Interactor, RunnerContext } from '@agent-device/contracts/interactor-types';
import type {
DeviceInventoryProvider,
DeviceLease,
LeaseLifecycleProvider,
LeaseLifecycleContext,
ProviderAppCatalogHandler,
ProviderDeviceInstallOptions,
ProviderDeviceInstallResult,
ProviderDeviceRuntime,
ProviderExpiredLeaseRecovery,
ProviderPortReverseOptions,
} from '@agent-device/contracts/device';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import {
cleanupLimrunAndroidAdbTunnel,
configureLimrunAndroidPortReverse,
createLimrunAndroidInteractor,
createLimrunAndroidSession,
installLimrunAndroidApp,
type LimrunAndroidSession,
} from './android.ts';
import {
buildLimrunDevice,
LIMRUN_PROVIDER,
parseLimrunDeviceId,
platformForLimrunLeaseBackend,
type LimrunPlatform,
} from './device.ts';
import {
createLimrunIosInteractor,
createLimrunIosSession,
installLimrunIosApp,
type LimrunIosSession,
} from './ios.ts';
import { createLimrunDeviceSession, type LimrunDeviceSession } from './device-session.ts';
import type { LimrunRuntimeDependencies } from './runtime-dependencies.ts';
import type {
PlatformRuntimeHost,
PlatformRuntimeOwner,
PlatformRuntimeProviderModule,
} from '@agent-device/contracts/platform-runtime-operations';
import { providerRuntimeOwner } from '@agent-device/contracts/platform-runtime';
import type { LimrunAppLogDescriptor } from './app-log-descriptor.ts';
import type { LimrunAppLogReader } from './app-log-poller.ts';
import { buildLimrunClientOptions, LIMRUN_CLIENT_HEADER } from './client-options.ts';
import { resolveLimrunRuntimeInstance } from './runtime-instance.ts';
import type { LimrunRequestOperationDrain } from './request-cancellation.ts';
import type { LimrunAppAsset } from './app-catalog.ts';
import {
attachedLimrunInstanceId,
isAttachedLimrunInstanceId,
type LimrunInstanceAccess,
} from './instance-access.ts';
type LimrunRuntimeSession = LimrunIosSession | LimrunAndroidSession;
const KEEP_ALIVE_INTERVAL_MS = 30_000;
export type LimrunRuntimeOptions = {
/** Organization API key. It creates and deletes instances for platforms without `instances`. */
apiKey?: string;
region?: string;
/** Pings each leased instance every 30 s so Limrun's inactivity timeout does not end idle sessions. */
keepAlive?: boolean;
runtimeInstance?: string;
instances?: LimrunInstanceAccess;
};
export type LimrunRuntime = ProviderDeviceRuntime & {
recoverExpiredLease: ProviderExpiredLeaseRecovery;
getDeviceSession(device: DeviceInfo): LimrunDeviceSession | undefined;
};
export type LimrunRuntimeRegistration = Readonly<{
runtime: LimrunRuntime;
platformModule: PlatformRuntimeProviderModule;
}>;
export function createLimrunRuntime(
options: LimrunRuntimeOptions,
dependencies: LimrunRuntimeDependencies,
mode: Readonly<{ includePlatformModule: true }>,
): LimrunRuntimeRegistration;
export function createLimrunRuntime(
options: LimrunRuntimeOptions,
dependencies: LimrunRuntimeDependencies,
): LimrunRuntime;
export function createLimrunRuntime(
options: LimrunRuntimeOptions,
dependencies: LimrunRuntimeDependencies,
mode?: Readonly<{ includePlatformModule: true }>,
): LimrunRuntime | LimrunRuntimeRegistration {
const runtime = new LimrunRuntimeImplementation(options, dependencies);
if (!mode?.includePlatformModule) return runtime;
const owner = providerRuntimeOwner(LIMRUN_PROVIDER, resolveLimrunRuntimeInstance(options));
if (owner.kind !== 'provider-runtime') throw new TypeError('Invalid Limrun runtime owner');
return Object.freeze({
runtime,
platformModule: Object.freeze({
owner,
loadRuntime: async (host: PlatformRuntimeHost) =>
await loadLimrunPlatformRuntime(runtime, owner.instance, host),
}),
});
}
class LimrunRuntimeImplementation implements ProviderDeviceRuntime {
private readonly limrun: Limrun | undefined;
private readonly sessions = new Map<string, LimrunRuntimeSession>();
private readonly keepAliveTimers = new Map<string, NodeJS.Timeout>();
private readonly appAliases = new Map<
string,
Readonly<{ assetName: string; installedAppId: string }>
>();
private readonly options: LimrunRuntimeOptions;
private readonly dependencies: LimrunRuntimeDependencies;
readonly provider = LIMRUN_PROVIDER;
readonly leaseLifecycle: LeaseLifecycleProvider = {
allocate: async (lease, context) => await this.allocate(lease, context),
release: async (lease) => await this.release(lease),
};
readonly appCatalog: ProviderAppCatalogHandler = async (query, signal) => {
const { assertLimrunUploadedAppAccess, listLimrunAppAssets } = await import('./app-catalog.ts');
assertLimrunUploadedAppAccess(query.publicNetworkOnly);
const limrun = this.orgClient(
'Listing uploaded apps',
'Run open <bundle-or-package-id> first; apps then lists the apps installed on the instance.',
);
return (await listLimrunAppAssets(limrun, query.platform, signal)).map((asset) => asset.name);
};
readonly recoverExpiredLease: ProviderExpiredLeaseRecovery = async (lease) => {
if (lease.leaseProvider !== this.provider || !platformForLimrunLeaseBackend(lease.backend)) {
throw new AppError('UNSUPPORTED_OPERATION', 'Limrun cannot recover this expired lease.', {
leaseId: lease.leaseId,
leaseProvider: lease.leaseProvider,
leaseBackend: lease.backend,
});
}
await this.release(lease);
};
readonly deviceInventoryProvider: DeviceInventoryProvider = async (request) => {
if (request.leaseProvider !== this.provider || !request.leaseId) return null;
const session = this.sessions.get(request.leaseId);
if (!session) return null;
if (request.platform && request.platform !== session.platform) return [];
return [session.device];
};
constructor(options: LimrunRuntimeOptions, dependencies: LimrunRuntimeDependencies) {
if (!options.apiKey && !options.instances?.ios && !options.instances?.android) {
throw new TypeError('Limrun runtime requires an apiKey or instance access');
}
this.options = options;
this.dependencies = dependencies;
this.limrun = options.apiKey
? new Limrun(
buildLimrunClientOptions({
apiKey: options.apiKey,
clientVersion: dependencies.clientVersion,
}),
)
: undefined;
}
ownsDevice(device: DeviceInfo): boolean {
return parseLimrunDeviceId(device.id) !== undefined;
}
hasLiveSession(device: DeviceInfo): boolean {
return this.getSessionForDevice(device) !== undefined;
}
getInteractor(device: DeviceInfo, _runner?: RunnerContext): Interactor | undefined {
const session = this.getSessionForDevice(device);
if (!session) return undefined;
return session.platform === 'ios'
? createLimrunIosInteractor(session)
: createLimrunAndroidInteractor(session);
}
getDeviceSession(device: DeviceInfo): LimrunDeviceSession | undefined {
const session = this.getSessionForDevice(device);
return session ? createLimrunDeviceSession(session) : undefined;
}
async installApp(
device: DeviceInfo,
app: string,
appPath: string,
options?: ProviderDeviceInstallOptions,
signal?: AbortSignal,
operationDrain?: LimrunRequestOperationDrain,
): Promise<ProviderDeviceInstallResult | undefined> {
return await this.installInstallablePath(
device,
appPath,
{
...options,
appIdentifierHint: options?.appIdentifierHint ?? app,
packageNameHint: options?.packageNameHint ?? app,
},
signal,
operationDrain,
);
}
async installInstallablePath(
device: DeviceInfo,
installablePath: string,
options?: ProviderDeviceInstallOptions,
signal?: AbortSignal,
operationDrain?: LimrunRequestOperationDrain,
): Promise<ProviderDeviceInstallResult | undefined> {
const session = this.getSessionForDevice(device);
if (!session) return undefined;
const limrun = this.orgClient('Uploading an app');
return session.platform === 'ios'
? await installLimrunIosApp(limrun, session, installablePath, options, signal, operationDrain)
: await installLimrunAndroidApp(
limrun,
session,
installablePath,
options,
signal,
operationDrain,
);
}
async configurePortReverse(
options: ProviderPortReverseOptions,
): Promise<Record<string, unknown> | undefined> {
const session = this.requireAndroidPortReverseSession(options.leaseId);
if (!session) return undefined;
await configureLimrunAndroidPortReverse(session, options);
return portReverseResult(options);
}
async shutdown(): Promise<void> {
const sessions = [...this.sessions.values()];
await Promise.allSettled(sessions.map(async (session) => await this.terminateSession(session)));
this.sessions.clear();
this.appAliases.clear();
}
private async allocate(
lease: DeviceLease,
context?: LeaseLifecycleContext,
): Promise<Record<string, unknown> | undefined> {
if (lease.leaseProvider !== this.provider) return undefined;
const platform = platformForLimrunLeaseBackend(lease.backend);
if (!platform) return undefined;
const existing = this.sessions.get(lease.leaseId);
if (existing) return { limrunInstanceId: existing.instanceId, device: existing.device };
const session =
(await this.attachSession(platform, lease)) ??
(await this.createSession(platform, lease, context));
this.sessions.set(lease.leaseId, session);
if (this.options.keepAlive) this.startKeepAlive(lease.leaseId, session);
return { limrunInstanceId: session.instanceId, device: session.device };
}
private async attachSession(
platform: LimrunPlatform,
lease: DeviceLease,
): Promise<LimrunRuntimeSession | undefined> {
const { ios, android } = this.options.instances ?? {};
const target = (apiUrl: string) => {
const instanceId = attachedLimrunInstanceId(apiUrl);
return { lease, instanceId, device: buildLimrunDevice(platform, lease, instanceId) };
};
if (platform === 'android') {
return (
android &&
(await createLimrunAndroidSession(
{ ...target(android.apiUrl), ...android },
this.dependencies,
))
);
}
return (
ios && (await createLimrunIosSession({ ...target(ios.apiUrl), ...ios }, this.dependencies))
);
}
private async createSession(
platform: LimrunPlatform,
lease: DeviceLease,
context?: LeaseLifecycleContext,
): Promise<LimrunRuntimeSession> {
const {
allocateLimrunAndroidSession,
allocateLimrunIosSession,
resolvePreinstalledAppId,
resolveRequestedLimrunAppAsset,
} = await import('./session-allocation.ts');
const limrun = this.orgClient('Creating an instance');
const requestedAsset = await resolveRequestedLimrunAppAsset(limrun, platform, context);
const params = this.sessionAllocationParams(limrun, lease, requestedAsset);
const session =
platform === 'ios'
? await allocateLimrunIosSession(params)
: await allocateLimrunAndroidSession(params);
if (requestedAsset) {
try {
const installedAppId = await resolvePreinstalledAppId(session, requestedAsset);
this.appAliases.set(lease.leaseId, {
assetName: requestedAsset.name,
installedAppId,
});
} catch (error) {
await this.terminateSession(session);
throw error;
}
}
return session;
}
private sessionAllocationParams(limrun: Limrun, lease: DeviceLease, app?: LimrunAppAsset) {
return {
limrun,
lease,
metadata: this.buildInstanceMetadata(lease),
region: this.options.region,
app,
dependencies: this.dependencies,
};
}
private buildInstanceMetadata(lease: DeviceLease) {
return {
displayName: `agent-device-${lease.tenantId}-${lease.runId}`,
labels: {
tenantId: lease.tenantId,
runId: lease.runId,
leaseId: lease.leaseId,
provider: lease.leaseProvider ?? LIMRUN_PROVIDER,
source: LIMRUN_CLIENT_HEADER,
},
};
}
private async release(lease: DeviceLease): Promise<Record<string, unknown> | undefined> {
const session = this.sessions.get(lease.leaseId);
if (!session) return await this.releaseRecoveredSession(lease);
await this.terminateSession(session);
this.sessions.delete(lease.leaseId);
this.appAliases.delete(lease.leaseId);
return { limrunInstanceId: session.instanceId };
}
private async releaseRecoveredSession(
lease: DeviceLease,
): Promise<Record<string, unknown> | undefined> {
const platform = platformForLimrunLeaseBackend(lease.backend);
if (!platform || this.options.instances?.[platform]) return undefined;
const limrun = this.orgClient('Recovering an expired lease');
const labelSelector = `provider=${LIMRUN_PROVIDER},leaseId=${lease.leaseId}`;
const instances =
platform === 'ios'
? await limrun.iosInstances.list({ labelSelector })
: await limrun.androidInstances.list({ labelSelector });
const instanceIds = instances.getPaginatedItems().map((instance) => instance.metadata.id);
for (const instanceId of instanceIds) {
if (platform === 'ios') {
await limrun.iosInstances.delete(instanceId);
} else {
await limrun.androidInstances.delete(instanceId);
}
}
if (instanceIds.length === 0) return undefined;
return { limrunInstanceId: instanceIds[0], limrunInstanceCount: instanceIds.length };
}
private startKeepAlive(leaseId: string, session: LimrunRuntimeSession): void {
const timer = setInterval(() => {
try {
session.client.keepAlive();
} catch {
// A dead connection surfaces on the next real command.
}
}, KEEP_ALIVE_INTERVAL_MS);
timer.unref();
this.keepAliveTimers.set(leaseId, timer);
}
private async terminateSession(session: LimrunRuntimeSession): Promise<void> {
const leaseId = session.lease.leaseId;
clearInterval(this.keepAliveTimers.get(leaseId));
this.keepAliveTimers.delete(leaseId);
session.client.disconnect();
if (session.platform === 'android') await cleanupLimrunAndroidAdbTunnel(session);
if (isAttachedLimrunInstanceId(session.instanceId)) return;
const limrun = this.orgClient('Deleting an instance');
if (session.platform === 'ios') {
await limrun.iosInstances.delete(session.instanceId);
} else {
await limrun.androidInstances.delete(session.instanceId);
}
}
private orgClient(
operation: string,
hint = 'This daemon drives an existing Limrun instance with its own token. Set LIMRUN_API_KEY, or ask the instance owner to do this.',
): Limrun {
if (this.limrun) return this.limrun;
throw new AppError('UNSUPPORTED_OPERATION', `${operation} requires a Limrun API key.`, {
hint,
});
}
private getSessionForDevice(device: DeviceInfo): LimrunRuntimeSession | undefined {
const parsed = parseLimrunDeviceId(device.id);
if (!parsed) return undefined;
const session = this.sessions.get(parsed.leaseId);
return session?.platform === parsed.platform ? session : undefined;
}
resolveAppReference(device: DeviceInfo, app: string): string {
const parsed = parseLimrunDeviceId(device.id);
if (!parsed) return app;
const alias = this.appAliases.get(parsed.leaseId);
return alias?.assetName === app ? alias.installedAppId : app;
}
currentAppLogReader(device: DeviceInfo): LimrunAppLogReader | undefined {
const session = this.getSessionForDevice(device);
if (!session) return undefined;
const publicSession = createLimrunDeviceSession(session);
return {
platform: session.platform,
leaseId: session.lease.leaseId,
instanceId: session.instanceId,
readLogs: async (appBundleId, lineLimit) =>
publicSession.platform === 'ios'
? await publicSession.readLogs(appBundleId, lineLimit)
: await publicSession.readLogs(lineLimit),
[Symbol.asyncDispose]: async () => undefined,
};
}
async reconnectAppLogReader(descriptor: LimrunAppLogDescriptor, signal?: AbortSignal) {
const { reconnectLimrunAppLogReader } = await import('./app-log-reconnect.ts');
return await reconnectLimrunAppLogReader({
orgClient: () => this.orgClient('Reconnecting app logs'),
instances: this.options.instances,
descriptor,
dependencies: this.dependencies,
signal,
});
}
private requireAndroidPortReverseSession(leaseId: string): LimrunAndroidSession | undefined {
const session = this.sessions.get(leaseId);
if (!session || session.platform === 'android') return session;
throw unsupported(
'port reverse',
'Direct Limrun iOS sessions cannot reach local host ports; use a bridge public URL.',
);
}
}
async function loadLimrunPlatformRuntime(
runtime: LimrunRuntimeImplementation,
runtimeInstance: string,
host: PlatformRuntimeHost,
): Promise<PlatformRuntimeOwner> {
const { createLimrunPlatformRuntimeOwner } = await import('./app-log-runtime.ts');
return createLimrunPlatformRuntimeOwner({
host,
runtimeInstance,
ownsDevice: (device) => runtime.ownsDevice(device),
hasLiveSession: (device) => runtime.hasLiveSession(device),
getInteractor: (device, runner) => runtime.getInteractor(device, runner),
getDeviceSession: (device) => runtime.getDeviceSession(device),
resolveAppReference: (device, app) => runtime.resolveAppReference(device, app),
openCurrent: async (device) => runtime.currentAppLogReader(device),
reconnect: async (descriptor, signal) =>
await runtime.reconnectAppLogReader(descriptor, signal),
listApps: async (device, filter, signal) => {
signal.throwIfAborted();
const session = runtime.getDeviceSession(device);
if (!session) {
throw new AppError('DEVICE_NOT_FOUND', 'Limrun app inventory session is unavailable', {
deviceId: device.id,
});
}
return (await session.listApps(filter)).map((app) => ({
id: app.id,
name: app.name ?? app.id,
}));
},
getAppState: async (device, signal) => {
signal.throwIfAborted();
const session = runtime.getDeviceSession(device);
if (session?.platform !== 'android') {
throw new AppError(
'UNSUPPORTED_OPERATION',
'Limrun Android appstate requires an active provider session',
);
}
const state = await session.getForegroundApp(signal);
signal.throwIfAborted();
return { package: state?.appId, activity: state?.activity };
},
deployApp: async (device, input, signal, operationDrain) =>
await runtime.installApp(
device,
input.app,
input.appPath,
{
relaunch: input.replaceExisting,
appIdentifierHint: input.app,
packageNameHint: input.app,
},
signal,
operationDrain,
),
deployMaterializedApp: async (device, input, signal, operationDrain) =>
await runtime.installInstallablePath(
device,
input.artifact.installablePath,
{
appIdentifierHint: input.artifact.bundleId,
packageNameHint: input.artifact.packageName,
},
signal,
operationDrain,
),
configurePortReverse: async (options) => await runtime.configurePortReverse(options),
});
}
function portReverseResult(options: ProviderPortReverseOptions): Record<string, unknown> {
return {
leaseId: options.leaseId,
devicePort: options.devicePort,
hostPort: options.hostPort,
name: options.name,
};
}
function unsupported(command: string, message: string): never {
throw new AppError('UNSUPPORTED_OPERATION', message, { command });
}