Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/plugins/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export type RunHostCommand = (
// declaration would exist twice as two unrelated copies and a branded or Node-primitive type would
// never match across them. Every mirror stays structurally identical to its platform original, so
// the composing root hands the same values to the platform's mechanics unchanged.
//
// Keeping the closure structural is what makes two plugin packages sharing one installed core still
// typecheck: each bundles this contract into its own declaration, and a consumer that hands such a
// plugin to `startAgentDevice` compares the bundled copy of `setup` against core's, member by member.
// An interactor cannot be named here for that reason. Its snapshot answer reaches
// `SnapshotViewportSize`, branded with a `unique symbol`, and no answer written by one copy of this
// contract answers to the other copy's brand. The composing root builds that operation and names it
// on its own facet type. The runtime value carries it for built-in composition; the published
// type does not declare it.
type ProviderAdbWaitTransport = '' | 'usb-' | 'local-' | 'any-';
type ProviderAdbWaitState = 'device' | 'recovery' | 'bootloader' | 'sideload' | 'sideload-window';

Expand Down Expand Up @@ -109,10 +118,25 @@ export type ProviderAndroidPortReverse = Readonly<{
list?(options?: ProviderAndroidPortReverseOptions): Promise<ProviderAndroidPortReverseMapping[]>;
}>;

/** The provider-scoped executor the facet scopes, mirrored from the platform's provider minus its optional extras. */
/** One text-entry request adb or the helper would have performed, mirrored from the platform's typed grammar. */
export type ProviderAndroidTextInjectionRequest = Readonly<{
/** Whether this is a plain entry or a replace-the-target's-text fill. */
action: 'type' | 'fill';
text: string;
delayMs?: number;
/** Present exactly for `fill`: the input the entry must land in, which the provider must focus first. */
target?: Readonly<{ x: number; y: number }>;
}>;

/**
* The provider-scoped executor the facet scopes, mirrored from the platform's provider minus its
* optional extras. `text` is carried because the Android family reads it off the provider it installs
* its scope over: a provider that can enter text natively keeps that route.
*/
export type ProviderAndroidAdbProvider = Readonly<{
exec: ProviderAndroidExecutor;
reverse?: ProviderAndroidPortReverse;
text?: (request: ProviderAndroidTextInjectionRequest) => Promise<void>;
}>;
Comment thread
thymikee marked this conversation as resolved.
Resolved

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/provider-limrun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
"@agent-device/capture-kit": "workspace:*",
"@agent-device/contracts": "workspace:*",
"@agent-device/kernel": "workspace:*",
"@agent-device/platform-android": "workspace:*"
"@agent-device/platform-android": "workspace:*",
"@agent-device/plugins": "workspace:*"
},
"exports": {
".": {
"types": "./src/index.ts",
"default": "./src/index.ts"
},
"./internal": {
"types": "./src/internal.ts",
"default": "./src/internal.ts"
}
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { readLimrunCredentials } from './provider-limrun-credentials.ts';
import { readLimrunCredentials } from './credentials.ts';

const IOS_ENV = {
LIM_IOS_INSTANCE_URL: ' https://region.limrun.example/v1/ios_x/api ',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { LimrunInstanceAccess } from '@agent-device/provider-limrun';
import { AppError } from '@agent-device/kernel/errors';
import type { EnvMap } from '@agent-device/kernel/source-value';
import { platformForLimrunLeaseBackend, type LimrunPlatform } from './device.ts';
import type { LimrunInstanceAccess } from './instance-access.ts';

/** The Limrun account and instance facts one host gives the provider. */
export type LimrunCredentials = Readonly<{
apiKey?: string;
region?: string;
keepAlive?: boolean;
instances?: LimrunInstanceAccess;
/** Directory the `--provider-network-logs` HAR files are written under. */
networkHarDir?: string;
}>;

const ACCOUNT_VARS = { apiKey: 'LIMRUN_API_KEY', region: 'LIMRUN_REGION' } as const;
Expand All @@ -23,48 +23,43 @@ const INSTANCE_VARS = {
} as const;

/** Every variable that selects which Limrun account or instance the credentials reach. */
export const LIMRUN_CREDENTIAL_VARIABLES: readonly string[] = [
const LIMRUN_CREDENTIAL_VARIABLES: readonly string[] = [
...Object.values(ACCOUNT_VARS),
...INSTANCE_VARS.ios,
...INSTANCE_VARS.android,
];

// Mirrors platformForLimrunLeaseBackend in provider-limrun, which exposes only its root entry, and
// that entry loads the Limrun SDK; importing it here would load the SDK on every credential read.
const LEASE_BACKEND_PLATFORMS: ReadonlyMap<string, 'ios' | 'android'> = new Map([
['ios-instance', 'ios'],
['android-instance', 'android'],
]);

/** The platform whose instance a Limrun lease backend reaches. */
export function limrunPlatformForLeaseBackend(
leaseBackend: string | undefined,
): 'ios' | 'android' | undefined {
return leaseBackend === undefined ? undefined : LEASE_BACKEND_PLATFORMS.get(leaseBackend);
): LimrunPlatform | undefined {
return leaseBackend === undefined ? undefined : platformForLimrunLeaseBackend(leaseBackend);
}

/**
* The variables a Limrun lease reads: the account variables plus the leased platform's instance
* variables, or every variable when the lease backend names no platform.
* variables, or every variable when the lease backend names no platform. The per-backend narrowing
* is what lets a client fingerprint the credentials of the one platform its lease reaches.
*/
export function limrunCredentialDeclaration(): Readonly<{
variables: readonly string[];
byBackend: Readonly<Record<string, readonly string[]>>;
}> {
const account = Object.values(ACCOUNT_VARS);
const backends: readonly [string, LimrunPlatform][] = [
['ios-instance', 'ios'],
['android-instance', 'android'],
];
return {
variables: LIMRUN_CREDENTIAL_VARIABLES,
byBackend: Object.fromEntries(
[...LEASE_BACKEND_PLATFORMS].map(([backend, platform]) => [
backend,
[...account, ...INSTANCE_VARS[platform]],
]),
backends.map(([backend, platform]) => [backend, [...account, ...INSTANCE_VARS[platform]]]),
),
};
}

/** The variables that give access to an existing instance of a platform. */
export function limrunInstanceVariables(platform: 'ios' | 'android'): readonly string[] {
export function limrunInstanceVariables(platform: LimrunPlatform): readonly string[] {
return INSTANCE_VARS[platform];
}

Expand Down
1 change: 0 additions & 1 deletion packages/provider-limrun/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { LIMRUN_PROFILE_FIELDS } from './device.ts';
export { createLimrunRuntime, type LimrunRuntime, type LimrunRuntimeOptions } from './runtime.ts';
export { verifyLimrunConnection } from './connection-verification.ts';
export type { LimrunInstanceAccess } from './instance-access.ts';

export type { LimrunFileDownload, LimrunRuntimeDependencies } from './runtime-dependencies.ts';

Expand Down
11 changes: 11 additions & 0 deletions packages/provider-limrun/src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* The seam core composes Limrun through while the provider is still built in. Nothing here loads the
* Limrun SDK: the runtime, the platform adapters, and the connection verification each load when
* their own path asks for them. The root entry keeps the SDK-side surface.
*/
export { limrunPlugin } from './plugin.ts';
export {
limrunInstanceVariables,
limrunPlatformForLeaseBackend,
readLimrunCredentials,
} from './credentials.ts';
157 changes: 157 additions & 0 deletions packages/provider-limrun/src/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import { test, vi } from 'vitest';
import type { ProviderPluginHost } from '@agent-device/plugins/host';
import { AppError } from '@agent-device/kernel/errors';
import { mkdtempForTestSync } from '@agent-device/test-kit';
import type { LimrunRuntimeDependencies } from './runtime-dependencies.ts';
import { stubLimrunRuntimeDependencies } from './runtime-dependencies.fixtures.ts';
import { limrunPlugin } from './plugin.ts';

const sdk = vi.hoisted(() => ({
runtimeCalls: [] as { options: Record<string, unknown>; mode: unknown }[],
}));

// The runtime module is stubbed so a case reads what the plugin hands the runtime builder without
// constructing a Limrun client. The provider's own runtime contract is what the plugin must satisfy.
vi.mock('./runtime.ts', () => ({
createLimrunRuntime: (options: Record<string, unknown>, _d: unknown, mode: unknown) => {
sdk.runtimeCalls.push({ options, mode });
return {
runtime: {
provider: 'limrun',
shutdown: async () => undefined,
leaseLifecycle: {},
ownsDevice: () => false,
getInteractor: () => undefined,
deviceInventoryProvider: async () => null,
},
platformModule: {
owner: { kind: 'provider-runtime', provider: 'limrun', instance: 'limrun:test' },
loadRuntime: async () => ({}),
},
};
},
}));

function pluginHost(env: Record<string, string | undefined> = {}): ProviderPluginHost {
return {
env,
options: {},
clientVersion: '1.2.3',
createError: (code, message, details) => new AppError(code, message, details),
runHostCommand: async () => {
throw new Error('this scenario runs no host commands');
},
};
}

function stubDependencies(): LimrunRuntimeDependencies {
return stubLimrunRuntimeDependencies();
}

test('the plugin declares its provider and the credential variables per lease backend', () => {
const plugin = limrunPlugin({ createDependencies: () => stubDependencies() });
assert.equal(plugin.apiVersion, 2);
assert.equal(plugin.provider, 'limrun');
assert.deepEqual(plugin.credentials, {
variables: [
'LIMRUN_API_KEY',
'LIMRUN_REGION',
'LIM_IOS_INSTANCE_URL',
'LIM_IOS_INSTANCE_TOKEN',
'LIM_ANDROID_INSTANCE_URL',
'LIM_ANDROID_INSTANCE_TOKEN',
'LIM_ANDROID_INSTANCE_ADB_URL',
],
byBackend: {
'ios-instance': [
'LIMRUN_API_KEY',
'LIMRUN_REGION',
'LIM_IOS_INSTANCE_URL',
'LIM_IOS_INSTANCE_TOKEN',
],
'android-instance': [
'LIMRUN_API_KEY',
'LIMRUN_REGION',
'LIM_ANDROID_INSTANCE_URL',
'LIM_ANDROID_INSTANCE_TOKEN',
'LIM_ANDROID_INSTANCE_ADB_URL',
],
},
});
});

test('setup builds the runtime over the credentials, host and HAR directory it was given', async () => {
const host = pluginHost({ LIMRUN_API_KEY: 'lim_test_key', LIMRUN_KEEP_ALIVE: '1' });
const dependencies = stubDependencies();
const createDependencies = vi.fn(() => dependencies);
const stateDir = mkdtempForTestSync('limrun-plugin-state-');

const setup = await limrunPlugin({ createDependencies, stateDir }).setup(host);

if (!('runtime' in setup)) throw new Error('Limrun sets up its own runtime');
assert.equal(setup.runtime.provider, 'limrun');
assert.deepEqual(createDependencies.mock.calls, [[host]]);
assert.deepEqual(sdk.runtimeCalls, [
{
options: {
apiKey: 'lim_test_key',
region: undefined,
keepAlive: true,
instances: undefined,
networkHarDir: path.join(stateDir, 'network-har'),
},
mode: { includePlatformModule: true },
},
]);
});

test('setup with only an attached instance builds a runtime without an API key or HAR directory', async () => {
await limrunPlugin({ createDependencies: () => stubDependencies() }).setup(
pluginHost({ LIM_IOS_INSTANCE_URL: 'https://x/api', LIM_IOS_INSTANCE_TOKEN: 'token' }),
);
const options = sdk.runtimeCalls.at(-1)?.options;
assert.equal(options?.apiKey, undefined);
assert.deepEqual(options?.instances, {
ios: { apiUrl: 'https://x/api', token: 'token' },
android: undefined,
});
assert.equal('networkHarDir' in (options ?? {}), false);
});

test('setup refuses a host with no Limrun credentials before asking for host adapters', async () => {
const createDependencies = vi.fn(() => stubDependencies());
await assert.rejects(
async () => {
await limrunPlugin({ createDependencies }).setup(pluginHost({ LIMRUN_REGION: 'eu' }));
},
(error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.code, 'INVALID_ARGS');
assert.equal(error.details?.reason, 'missing_provider_credentials');
return true;
},
);
assert.equal(createDependencies.mock.calls.length, 0);
});

test('setup refuses a partial instance set naming the variable it still needs', async () => {
const createDependencies = vi.fn(() => stubDependencies());
await assert.rejects(
async () => {
await limrunPlugin({ createDependencies }).setup(
pluginHost({
LIMRUN_API_KEY: 'lim_test_key',
LIM_ANDROID_INSTANCE_URL: 'https://region.limrun.example/v1/android_x/api',
LIM_ANDROID_INSTANCE_TOKEN: 'android-token',
}),
);
},
{
code: 'INVALID_ARGS',
message: 'Limrun instance access is missing LIM_ANDROID_INSTANCE_ADB_URL.',
},
);
assert.equal(createDependencies.mock.calls.length, 0);
});
Loading
Loading