From 7765df8cee0b2164e191bd72155338cd6c2396b4 Mon Sep 17 00:00:00 2001 From: Krzysztof Moch Date: Tue, 10 Nov 2026 08:08:19 +0100 Subject: [PATCH] fix(limrun): let tests run on Limrun devices again The driver put the agent-device session on the lease's client (#1513), a key the mobile engine reserves, so the engine rejected every Limrun lease with ENGINE_FAILURE. The session now stays on the driver's and the heartbeat's own clients and off the client the engine receives. --- .changeset/limrun-lease-session.md | 5 +++ packages/limrun/src/driver.ts | 17 +++++---- packages/limrun/src/heartbeat.ts | 7 ++-- packages/limrun/src/session.ts | 10 +++++ packages/limrun/tests/unit/driver.test.ts | 39 +++++++++++++++----- packages/limrun/tests/unit/heartbeat.test.ts | 4 +- 6 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 .changeset/limrun-lease-session.md create mode 100644 packages/limrun/src/session.ts diff --git a/.changeset/limrun-lease-session.md b/.changeset/limrun-lease-session.md new file mode 100644 index 0000000000..9c3cd17e7b --- /dev/null +++ b/.changeset/limrun-lease-session.md @@ -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. diff --git a/packages/limrun/src/driver.ts b/packages/limrun/src/driver.ts index 6d9f3fdba2..f13cc3f072 100644 --- a/packages/limrun/src/driver.ts +++ b/packages/limrun/src/driver.ts @@ -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 { @@ -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; } @@ -26,22 +28,21 @@ process.once('message', (request: DriverRequest) => { async function attach(request: DriverRequest): Promise { 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'); diff --git a/packages/limrun/src/heartbeat.ts b/packages/limrun/src/heartbeat.ts index e64a4087f3..3fd1f63f49 100644 --- a/packages/limrun/src/heartbeat.ts +++ b/packages/limrun/src/heartbeat.ts @@ -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> { + 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(); diff --git a/packages/limrun/src/session.ts b/packages/limrun/src/session.ts new file mode 100644 index 0000000000..44895d2477 --- /dev/null +++ b/packages/limrun/src/session.ts @@ -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)}`; +} diff --git a/packages/limrun/tests/unit/driver.test.ts b/packages/limrun/tests/unit/driver.test.ts index 77ed7e66c4..aac0618d8d 100644 --- a/packages/limrun/tests/unit/driver.test.ts +++ b/packages/limrun/tests/unit/driver.test.ts @@ -1,15 +1,18 @@ 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 { 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; @@ -17,6 +20,22 @@ it.each(['ios', 'android'] as const)('uses the requested %s app when installatio let complete!: (message: unknown) => void; const sent = new Promise((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 }]]); }); diff --git a/packages/limrun/tests/unit/heartbeat.test.ts b/packages/limrun/tests/unit/heartbeat.test.ts index c9596ae06e..58275227ec 100644 --- a/packages/limrun/tests/unit/heartbeat.test.ts +++ b/packages/limrun/tests/unit/heartbeat.test.ts @@ -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) }); @@ -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;