Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/limrun-lease-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2e-dev/limrun': patch
---

A Limrun device lease no longer names an agent-device `session` in its client configuration, which `@e2e-dev/mobile` rejected with `ENGINE_FAILURE` ("returned a lease without an id and a daemon baseUrl or JSON client configuration") before any test or session could start. The provider still allocates and renews the lease under its own session.
17 changes: 9 additions & 8 deletions packages/limrun/src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from 'node:crypto';
import { createAgentDeviceClient, type AgentDeviceClientConfig } from 'agent-device';
import { createAgentDeviceClient } from 'agent-device';
import type { DeviceClientConfig } from '@e2e-dev/mobile';
import { leaseSession } from './session.ts';

/** A separate process gives each daemon its own credentials without changing the runner's environment. */
export interface DriverRequest {
Expand All @@ -12,7 +13,8 @@ export interface DriverRequest {
}

export interface DriverBinding {
client: AgentDeviceClientConfig;
/** The lease scope the engine creates its client with. No `session`: the engine rejects a lease that names one. */
client: DeviceClientConfig;
deviceId: string;
installedApp?: string;
}
Expand All @@ -26,22 +28,21 @@ process.once('message', (request: DriverRequest) => {

async function attach(request: DriverRequest): Promise<DriverBinding> {
const { stateDir, runId, slot, platform, app, appPath } = request;
// agent-device refuses a provider lease to a client that names no session.
const session = `e2e-lease-${createHash('sha256').update(stateDir).digest('hex').slice(0, 8)}`;
const session = leaseSession(stateDir);
const allocator = createAgentDeviceClient({ stateDir, session });
const lease = await allocator.leases.allocate({
tenant: 'e2e', runId, clientId: `worker-${slot}`, leaseBackend: `${platform}-instance`, leaseProvider: 'limrun', retainOnClose: true,
});
if (!lease.retainOnClose) throw new Error('agent-device did not retain the Limrun lease across session close');
const client: AgentDeviceClientConfig = {
stateDir, session, tenant: lease.tenantId, runId: lease.runId, leaseId: lease.leaseId,
const client: DeviceClientConfig = {
stateDir, tenant: lease.tenantId, runId: lease.runId, leaseId: lease.leaseId,
leaseBackend: lease.backend, leaseProvider: 'limrun',
...(lease.deviceKey === undefined ? {} : { deviceKey: lease.deviceKey }),
...(lease.clientId === undefined ? {} : { clientId: lease.clientId }),
};
const binding: DriverBinding = { client, deviceId: `limrun:${platform}:${lease.leaseId}` };
if (appPath !== undefined) {
const device = createAgentDeviceClient(client);
const device = createAgentDeviceClient({ ...client, session });
const installed = await device.apps.install({ appPath, ...(platform === 'ios' ? { udid: binding.deviceId } : { serial: binding.deviceId }), ...(app === undefined ? {} : { app }), platform });
const id = installed.appId ?? installed.bundleId ?? installed.package ?? app;
if (!id) throw new Error('Limrun installed the build but agent-device returned no app identifier');
Expand Down
7 changes: 4 additions & 3 deletions packages/limrun/src/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createAgentDeviceClient } from 'agent-device';
import type { DriverBinding } from './driver.ts';
import { leaseSession } from './session.ts';

/** Renew idle leases while other targets run; session retention alone does not prevent expiry. */
export async function startHeartbeat(binding: DriverBinding, log: (line: string) => void): Promise<() => Promise<void>> {
const { stateDir, leaseId } = binding.client;
if (!stateDir || !leaseId) throw new Error('Limrun driver returned no agent-device state directory or lease id');
// An explicit empty URL selects the local stateDir instead of an ambient remote daemon.
const client = createAgentDeviceClient({ ...binding.client, daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
const leaseId = binding.client.leaseId;
if (!leaseId) throw new Error('Limrun driver returned no agent-device lease id');
const client = createAgentDeviceClient({ ...binding.client, session: leaseSession(stateDir), daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
const controller = new AbortController();
const beat = () => client.leases.heartbeat({ leaseId, ttlMs: 60_000, signal: AbortSignal.any([controller.signal, AbortSignal.timeout(10_000)]) });
await beat();
Expand Down
10 changes: 10 additions & 0 deletions packages/limrun/src/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createHash } from 'node:crypto';

/**
* The session the driver allocates a lease under and the heartbeat renews it under. agent-device
* refuses a provider lease to a client that names no session, and the daemon in `stateDir` holds
* one lease. The lease the engine receives never carries it: the engine names its own session.
*/
export function leaseSession(stateDir: string): string {
return `e2e-lease-${createHash('sha256').update(stateDir).digest('hex').slice(0, 8)}`;
}
39 changes: 29 additions & 10 deletions packages/limrun/tests/unit/driver.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import { afterEach, expect, it, vi } from 'vitest';
import type { DriverRequest } from '../../src/driver.ts';

const mocks = vi.hoisted(() => ({ install: vi.fn() }));
vi.mock('agent-device', () => ({ createAgentDeviceClient: () => ({
leases: { allocate: async () => ({ retainOnClose: true, tenantId: 'e2e', runId: 'run', leaseId: 'lease', backend: 'ios-instance' }) },
apps: { install: mocks.install },
}) }));
afterEach(() => { vi.restoreAllMocks(); vi.resetModules(); });
const mocks = vi.hoisted(() => ({ install: vi.fn(), create: vi.fn() }));
vi.mock('agent-device', () => ({ createAgentDeviceClient: (config: unknown) => {
mocks.create(config);
return {
leases: { allocate: async () => ({ retainOnClose: true, tenantId: 'e2e', runId: 'run', leaseId: 'lease', backend: 'ios-instance' }) },
apps: { install: mocks.install },
};
} }));
afterEach(() => { vi.restoreAllMocks(); vi.resetAllMocks(); vi.resetModules(); });

it.each(['ios', 'android'] as const)('uses the requested %s app when installation returns no identifier', async (platform) => {
mocks.install.mockResolvedValue({});
/** Runs the driver process's `attach` on `request` and returns the message it sends back. */
async function attach(request: DriverRequest): Promise<unknown> {
const once = vi.spyOn(process, 'once');
await import('../../src/driver.ts');
const listener = once.mock.calls.find(([event]) => event === 'message')![1] as (request: DriverRequest) => void;
process.removeListener('message', listener);
let complete!: (message: unknown) => void;
const sent = new Promise<unknown>((resolve) => { complete = resolve; });
vi.spyOn(process, 'send').mockImplementation((message) => { complete(message); return true; });
listener({ stateDir: '/daemon', runId: 'run', slot: 0, platform, app: 'com.example.app', appPath: '/build' });
await expect(sent).resolves.toMatchObject({ binding: { installedApp: 'com.example.app', client: { session: expect.stringMatching(/^e2e-lease-[0-9a-f]{8}$/) } } });
listener(request);
return sent;
}

it.each(['ios', 'android'] as const)('uses the requested %s app when installation returns no identifier', async (platform) => {
mocks.install.mockResolvedValue({});
await expect(attach({ stateDir: '/daemon', runId: 'run', slot: 0, platform, app: 'com.example.app', appPath: '/build' })).resolves.toMatchObject({ binding: { installedApp: 'com.example.app' } });
});

it('names a session for its own clients and keeps it out of the client configuration the engine receives', async () => {
mocks.install.mockResolvedValue({ appId: 'com.example.app' });
const session = expect.stringMatching(/^e2e-lease-[0-9a-f]{8}$/);
const scope = { stateDir: '/daemon', tenant: 'e2e', runId: 'run', leaseId: 'lease', leaseBackend: 'ios-instance', leaseProvider: 'limrun' };
// Exact: `@e2e-dev/mobile` rejects a lease whose client names `session` or the daemon connection.
await expect(attach({ stateDir: '/daemon', runId: 'run', slot: 0, platform: 'ios', appPath: '/build' })).resolves.toEqual({
binding: { client: scope, deviceId: 'limrun:ios:lease', installedApp: 'com.example.app' },
});
expect(mocks.create.mock.calls).toEqual([[{ stateDir: '/daemon', session }], [{ ...scope, session }]]);
});
4 changes: 2 additions & 2 deletions packages/limrun/tests/unit/heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it('renews an idle scoped lease beyond its expiry window and stops before daemon
mocks.heartbeat.mockResolvedValue({});
const binding = { client: { stateDir: '/worker-2', tenant: 'e2e', runId: 'run', leaseId: 'lease-2' }, deviceId: 'device-2' };
const stop = await startHeartbeat(binding, vi.fn());
expect(mocks.create).toHaveBeenCalledWith({ ...binding.client, daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
expect(mocks.create).toHaveBeenCalledWith({ ...binding.client, session: expect.stringMatching(/^e2e-lease-[0-9a-f]{8}$/), daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
await vi.advanceTimersByTimeAsync(140_000);
expect(mocks.heartbeat).toHaveBeenCalledTimes(8);
expect(mocks.heartbeat).toHaveBeenLastCalledWith({ leaseId: 'lease-2', ttlMs: 60_000, signal: expect.any(AbortSignal) });
Expand All @@ -26,7 +26,7 @@ it('reports transient renewal failures, retries, and cancels an in-flight beat o
vi.useFakeTimers();
const log = vi.fn();
mocks.heartbeat.mockResolvedValueOnce({}).mockRejectedValueOnce(new Error('transport unavailable'));
const stop = await startHeartbeat({ client: { leaseId: 'lease' }, deviceId: 'device' }, log);
const stop = await startHeartbeat({ client: { stateDir: '/worker', leaseId: 'lease' }, deviceId: 'device' }, log);
await vi.advanceTimersByTimeAsync(20_000);
expect(log).toHaveBeenCalledWith('driver lease renewal failed: transport unavailable');
let signal: AbortSignal | undefined;
Expand Down
Loading