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
6 changes: 6 additions & 0 deletions .changeset/name-lease-sessions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@e2e-dev/mobile': patch
'@e2e-dev/limrun': patch
---

Device-cloud leases are allocated and renewed under a named agent-device session, so a newer agent-device that refuses a provider lease to an unnamed client keeps working with BrowserStack, TestMu AI, and Limrun.
7 changes: 5 additions & 2 deletions packages/limrun/src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'node:crypto';
import { createAgentDeviceClient, type AgentDeviceClientConfig } from 'agent-device';

/** A separate process gives each daemon its own credentials without changing the runner's environment. */
Expand Down Expand Up @@ -25,13 +26,15 @@ process.once('message', (request: DriverRequest) => {

async function attach(request: DriverRequest): Promise<DriverBinding> {
const { stateDir, runId, slot, platform, app, appPath } = request;
const allocator = createAgentDeviceClient({ stateDir });
// 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 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, tenant: lease.tenantId, runId: lease.runId, leaseId: lease.leaseId,
stateDir, session, 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 }),
Expand Down
2 changes: 1 addition & 1 deletion packages/limrun/tests/unit/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ it.each(['ios', 'android'] as const)('uses the requested %s app when installatio
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' } });
await expect(sent).resolves.toMatchObject({ binding: { installedApp: 'com.example.app', client: { session: expect.stringMatching(/^e2e-lease-[0-9a-f]{8}$/) } } });
});
3 changes: 2 additions & 1 deletion packages/mobile/src/lease/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAgentDeviceClient, type LeaseAllocateOptions } from 'agent-device';
import { leaseSession } from './session.ts';

/** One lease allocation from the daemon in `stateDir`, run in its own process so the daemon gets that process's environment. */
export interface DriverRequest {
Expand All @@ -21,6 +22,6 @@ process.once('message', (request: DriverRequest) => {
});

async function allocate({ stateDir, allocate: options }: DriverRequest): Promise<DriverGrant> {
const lease = await createAgentDeviceClient({ stateDir }).leases.allocate(options);
const lease = await createAgentDeviceClient({ stateDir, session: leaseSession(stateDir) }).leases.allocate(options);
return { leaseId: lease.leaseId, tenantId: lease.tenantId, runId: lease.runId, retainOnClose: lease.retainOnClose === true };
}
3 changes: 2 additions & 1 deletion packages/mobile/src/lease/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createAgentDeviceClient, type LeaseScopedOptions } from 'agent-device';
import { leaseSession } from './session.ts';

export const LEASE_TTL_MS = 60_000;

/** Renew idle leases while other targets run; session retention alone does not prevent expiry. */
export async function startHeartbeat(stateDir: string, scope: LeaseScopedOptions, log: (line: string) => void): Promise<() => Promise<void>> {
// An explicit empty URL selects the local stateDir instead of an ambient remote daemon.
const client = createAgentDeviceClient({ stateDir, daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
const client = createAgentDeviceClient({ stateDir, session: leaseSession(stateDir), daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
const controller = new AbortController();
const beat = () => client.leases.heartbeat({ ...scope, ttlMs: LEASE_TTL_MS, signal: AbortSignal.any([controller.signal, AbortSignal.timeout(10_000)]) });
await beat();
Expand Down
9 changes: 9 additions & 0 deletions packages/mobile/src/lease/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createHash } from 'node:crypto';

/**
* The session a lease's daemon allocates and renews the lease under. agent-device refuses a
* provider lease to a client that names no session, and the daemon in `stateDir` holds one lease.
*/
export function leaseSession(stateDir: string): string {
return `e2e-lease-${createHash('sha256').update(stateDir).digest('hex').slice(0, 8)}`;
}
2 changes: 1 addition & 1 deletion packages/mobile/tests/unit/lease-driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const allocate = { tenant: 'browserstack', runId: 'run', leaseProvider: 'browser
it('allocates from the daemon in its state directory and reports the grant', async () => {
mocks.allocate.mockResolvedValue({ leaseId: 'lease', tenantId: 'browserstack', runId: 'run', backend: 'ios-instance', retainOnClose: true });
await expect(answer({ stateDir: '/daemon', allocate })).resolves.toEqual({ grant: { leaseId: 'lease', tenantId: 'browserstack', runId: 'run', retainOnClose: true } });
expect(mocks.create).toHaveBeenCalledWith({ stateDir: '/daemon' });
expect(mocks.create).toHaveBeenCalledWith({ stateDir: '/daemon', session: expect.stringMatching(/^e2e-lease-[0-9a-f]{8}$/) });
expect(mocks.allocate).toHaveBeenCalledWith(allocate);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/mobile/tests/unit/lease-heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ it("renews the lease on the lease's own daemon until stopped", async () => {
vi.useFakeTimers();
mocks.heartbeat.mockResolvedValue({});
const stop = await startHeartbeat('/state', scope, vi.fn());
expect(mocks.create).toHaveBeenCalledWith({ stateDir: '/state', daemonBaseUrl: '', daemonAuthToken: '', daemonTransport: 'auto' });
expect(mocks.create).toHaveBeenCalledWith({ stateDir: '/state', 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({ ...scope, ttlMs: 60_000, signal: expect.any(AbortSignal) });
Expand Down
Loading