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 .fallowrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
"file": "packages/provider-*/src/index.ts",
"exports": ["default"]
},
{
// Test runners that drive Revyl's own API beside the plugin (e2e's video recording) find a lease's session by this id; the repository has no consumer of it.
"file": "packages/provider-revyl/src/index.ts",
"exports": ["revylSessionIdForLease"]
},
{
// The version 1 entry a provider package's `agentDevicePlugin.entry` names; hosts without the version 2 contract import the built file and read `default`.
"file": "packages/provider-*/src/plugin.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/provider-revyl/src/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type RevylFixtureBackend,
} from './revyl-backend.fixtures.ts';
import { deployRevylApp } from './deployment.ts';
import { revylSessionIdForLease } from './session-identity.ts';
import { revylSessionIdForLease } from './device.ts';

const openBackends: RevylFixtureBackend[] = [];

Expand Down
25 changes: 25 additions & 0 deletions packages/provider-revyl/src/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isSupportedRevylRuntimeDevice,
parseRevylDeviceId,
platformForRevylLeaseBackend,
revylSessionIdForLease,
} from './device.ts';

test('only the two instance backends name a Revyl platform', () => {
Expand Down Expand Up @@ -101,3 +102,27 @@ test('only a device this module could have built is one it may act on', () => {
assert.equal(isSupportedRevylRuntimeDevice(device as DeviceInfo), false, name);
}
});

/** Revyl's own UUID pattern for the `session_id` field. */
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;

test('one lease always derives the same session id', () => {
const first = revylSessionIdForLease('lease-1');
assert.match(first, UUID, 'the field is declared as a UUID');
assert.equal(revylSessionIdForLease('lease-1'), first);
// Recovery in another process works because the id depends on nothing but the lease.
assert.equal(revylSessionIdForLease('lease-1'), first);
});

test('two leases never derive one session id', () => {
const ids = new Set(
['lease-1', 'lease-2', 'lease-9', 'revyl:ios:lease-1', ''].map((leaseId) =>
revylSessionIdForLease(leaseId),
),
);
assert.equal(ids.size, 5);
assert.deepEqual(
[...ids].filter((id) => !UUID.test(id)),
[],
);
});
31 changes: 31 additions & 0 deletions packages/provider-revyl/src/device.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'node:crypto';
import type { DeviceLease } from '@agent-device/contracts/device';
import type { DeviceInfo } from '@agent-device/kernel/device';

Expand Down Expand Up @@ -69,3 +70,33 @@ export function isSupportedRevylRuntimeDevice(device: DeviceInfo): boolean {
device.kind === 'emulator' &&
device.iosPhysicalDeviceBackend === undefined;
}

/** Names this derivation, so a re-issue can change it without colliding with ids already issued. */
const REVYL_SESSION_ID_NAMESPACE = 'agent-device:revyl:session:v1';

/**
* The session id one lease's device is started and stopped under.
*
* Revyl's `start_device` takes a caller-generated `session_id` and uses it as the session's canonical
* identity, which is what Revyl's own clients do for a hosted session. Deriving it from the lease id
* therefore gives the runtime a durable identity for one lease: an expired-lease recovery in a process
* that never started the device can name the session it has to stop.
*
* It is a UUID because that is the field's declared format, and it is a hash rather than the lease id
* itself because two daemons that rent for the same account must derive the same id for the same lease
* and never collide across providers. Revyl's UUID regex accepts lowercase hex with the version and
* variant bits set.
*/
export function revylSessionIdForLease(leaseId: string): string {
const digest = createHash('sha256')
.update(`${REVYL_SESSION_ID_NAMESPACE}:${leaseId}`)
.digest('hex');
const hex = `${digest.slice(0, 32)}`;
return [
hex.slice(0, 8),
hex.slice(8, 12),
`4${hex.slice(13, 16)}`,
`${((Number.parseInt(hex.slice(16, 17), 16) & 0x3) | 0x8).toString(16)}${hex.slice(17, 20)}`,
hex.slice(20, 32),
].join('-');
}
1 change: 1 addition & 0 deletions packages/provider-revyl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ function revyl(options: RevylPluginOptions = {}): AgentDevicePlugin {
}

export { revyl as default };
export { revylSessionIdForLease } from './device.ts';
13 changes: 12 additions & 1 deletion packages/provider-revyl/src/interactor-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,19 @@ test('appearance sets the named mode, and toggle flips the mode the worker repor
);
});

test('location sets the coordinates and refuses switching location services', async () => {
test('iOS refuses location on and off, which are the app grant there, and still sets coordinates', async () => {
const { fixture, interactor } = await liveInteractor('ios');
await assert.rejects(interactor.setSetting('location', 'on'), refusedForMode);
await assert.rejects(interactor.setSetting('location', 'off'), refusedForMode);
await interactor.setSetting('location', 'set', undefined, { latitude: 43.65, longitude: -79.38 });

assert.deepEqual(fixture.bodyFor('set_location'), { latitude: 43.65, longitude: -79.38 });
assert.equal(fixture.paths().filter((request) => request.endsWith('/set_location')).length, 1);
});

test('Android sets the coordinates, answers on as already on, and refuses switching it off', async () => {
const { fixture, interactor } = await liveInteractor('android');
assert.equal(await interactor.setSetting('location', 'on'), undefined);
await interactor.setSetting('location', 'set', undefined, { latitude: 43.65, longitude: -79.38 });
await assert.rejects(interactor.setSetting('location', 'off'), refusedForMode);

Expand Down
15 changes: 12 additions & 3 deletions packages/provider-revyl/src/interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,24 @@ class RevylInteractor implements Interactor {
}

/**
* `/set_location` moves the device's own coordinates. The worker has no action that switches
* location services, so `on` and `off` are refused rather than reported as applied.
* `/set_location` moves the device's own coordinates. On Android, `on` switches location services
* on, and Revyl starts every Android device with them on, so it is answered without a request. On
* iOS, `on|off` is the session app's location grant, which this does not set, so it is refused
* like Android's `off`.
*/
private async setLocation(
state: string,
options: SettingOptions | undefined,
): Promise<{ latitude: number; longitude: number }> {
): Promise<{ latitude: number; longitude: number } | void> {
const latitude = options?.latitude;
const longitude = options?.longitude;
if (
state === 'on' &&
this.session.platform === 'android' &&
latitude === undefined &&
longitude === undefined
)
return;
if (latitude === undefined || longitude === undefined) {
throw unsupported(
'settings',
Expand Down
14 changes: 14 additions & 0 deletions packages/provider-revyl/src/platform-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ test('a live session binds the interactor-backed operations and refuses the rest
assert.equal(binding.operations.appLogInspect, undefined);
});

test('a live session binds settings and reaches the interactor with the setting and its state', async () => {
const calls: unknown[][] = [];
const interactor = {
setSetting: async (...args: unknown[]) => {
calls.push(args);
return { appearance: 'dark' };
},
} as unknown as Interactor;
const binding = await bind(owner({ hasLiveSession: true, interactor }));
assert.ok(binding.operations.setSetting);
await binding.operations.setSetting({ setting: 'appearance', state: 'dark' } as never);
assert.deepEqual(calls[0]?.slice(0, 2), ['appearance', 'dark']);
});

test('a session that has ended reports the refused facts and binds no device action', async () => {
const facts = await owner({ hasLiveSession: false }).inspectFacts(IOS_DEVICE);
assert.equal(facts?.operations.captureSnapshot.available, false);
Expand Down
11 changes: 7 additions & 4 deletions packages/provider-revyl/src/revyl-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { AppError } from '@agent-device/kernel/errors';
import { setTimeout as sleep } from 'node:timers/promises';
import type { DeviceLease } from '@agent-device/contracts/device';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { buildRevylDevice, type RevylPlatform } from './device.ts';
import { buildRevylDevice, revylSessionIdForLease, type RevylPlatform } from './device.ts';
import {
REVYL_START_DEVICE_TIMEOUT_MS,
isRevylSessionGone,
revylFailureRefusedTheRequest,
type RevylClient,
} from './revyl-client.ts';
import { revylSessionIdForLease } from './session-identity.ts';
import type {
RevylStartDeviceRequest,
RevylStartDeviceResponse,
Expand Down Expand Up @@ -487,7 +487,8 @@ async function sleepBefore(deadline: RevylDeadline, signal?: AbortSignal): Promi
* Refreshes a held session's idle clock on a timer, with a read that changes nothing on the device:
* the worker counts a state read as device use and `health` as not. A failed refresh is not reported,
* since the next one or any command refreshes the clock again; only a holder gone for the whole idle
* timeout loses the device. The timer never keeps the process alive.
* timeout loses the device. A session Revyl reports gone stops the timer, since no later read can
* refresh it. The timer never keeps the process alive.
*/
export function keepRevylSessionAlive(client: RevylClient, runId: string): () => void {
let refreshing = false;
Expand All @@ -496,7 +497,9 @@ export function keepRevylSessionAlive(client: RevylClient, runId: string): () =>
refreshing = true;
void client
.workerAction(runId, 'appearance')
.catch(() => undefined)
.catch((error: unknown) => {
if (isRevylSessionGone(error)) clearInterval(timer);
})
.finally(() => {
refreshing = false;
});
Expand Down
18 changes: 18 additions & 0 deletions packages/provider-revyl/src/runtime-keep-alive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ test('a held session reads appearance on each keep-alive beat, and its release s
assert.equal(fixture.paths().filter((path) => path === KEEP_ALIVE).length, 2);
});

test('a keep-alive read that finds the session gone stops the beat', async () => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const fixture = await startRevylFixtureBackend({
respond: { [KEEP_ALIVE]: { status: 404, body: { detail: 'Session not found' } } },
});
openBackends.push(fixture);
const runtime = fixture.createRuntime();
const lease = revylLease('android');
await runtime.leaseLifecycle?.allocate?.(lease);

vi.advanceTimersByTime(REVYL_KEEP_ALIVE_INTERVAL_MS);
await keepAlivesReach(fixture, 1);
await vi.waitFor(() => assert.equal(vi.getTimerCount(), 0));
vi.advanceTimersByTime(REVYL_KEEP_ALIVE_INTERVAL_MS * 3);
assert.equal(fixture.paths().filter((path) => path === KEEP_ALIVE).length, 1);
await runtime.leaseLifecycle?.release?.(lease);
});

test('a failed keep-alive read leaves the session held and the next beat reads again', async () => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const fixture = await startRevylFixtureBackend({
Expand Down
5 changes: 5 additions & 0 deletions packages/provider-revyl/src/runtime-transient-reads.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { afterEach, test } from 'vitest';
import { AppError } from '@agent-device/kernel/errors';
import {
revylLease,
startRevylFixtureBackend,
Expand Down Expand Up @@ -59,6 +60,10 @@ test('a lease start still fails on a worker-connection answer that is not a serv
initialApp: 'https://apps.test/app.apk',
flags: { device: 'Pixel 7' },
}),
(error: unknown) =>
error instanceof AppError &&
error.code === 'UNAUTHORIZED' &&
error.details?.providerStatus === 403,
);
assert.deepEqual(
fixture.paths().filter((path) => path === CONNECTION),
Expand Down
2 changes: 1 addition & 1 deletion packages/provider-revyl/src/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
startRevylFixtureBackend,
type RevylFixtureBackend,
} from './revyl-backend.fixtures.ts';
import { revylSessionIdForLease } from './session-identity.ts';
import { revylSessionIdForLease } from './device.ts';

const openBackends: RevylFixtureBackend[] = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/provider-revyl/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
parseRevylDeviceId,
platformForRevylLeaseBackend,
REVYL_PROVIDER,
revylSessionIdForLease,
type RevylPlatform,
} from './device.ts';
import { createRevylClient, isRevylSessionGone, type RevylClient } from './revyl-client.ts';
Expand All @@ -42,7 +43,6 @@ import {
import { createRevylInteractor } from './interactor.ts';
import { providerRuntimeOwner } from '@agent-device/contracts/platform-runtime';
import { resolveRevylRuntimeInstance } from './runtime-instance.ts';
import { revylSessionIdForLease } from './session-identity.ts';
import type { RevylStartRequest } from './session-allocation.ts';
import type { RevylDeploymentSession } from './deployment.ts';

Expand Down
27 changes: 0 additions & 27 deletions packages/provider-revyl/src/session-identity.test.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/provider-revyl/src/session-identity.ts

This file was deleted.

13 changes: 8 additions & 5 deletions website/docs/docs/revyl.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ A Revyl session runs the commands that reach a device through Revyl's device act
bundle identifier or package name, `open` with a deep link or URL, and `close` for the app the
session opened.
- `home`, and `back` on Android. iOS has no system back button; click the app's own back control.
- `orientation`. An Android emulator rotates to all four sides. An iOS simulator rotates to
`portrait` and `landscape-left` only, and refuses the other two. Neither releases the rotation lock,
- `orientation`. A worker that reports precise orientation, as Revyl's Android emulators do, rotates
to all four sides. Any other worker, such as an iOS simulator, rotates to `portrait` and
`landscape-left` only, and refuses the other two. Neither releases the rotation lock,
so `orientation auto` refuses.
- `settings appearance` (`light`, `dark`, or `toggle`), `settings location set <lat> <lon>`,
`settings permission`, and `settings clear-app-state`. On iOS, `settings reset-keychain` also
works. `settings location on|off` refuses, because Revyl moves the device's coordinates but cannot
switch location services. `settings permission` refuses `location-always`, `contacts-limited`, and
`--mode limited`. Revyl's iOS simulators refuse `camera` and `notifications`.
works. Revyl starts every Android device with location services on, so `settings location on`
succeeds there without changing anything, and `settings location off` refuses, because Revyl
cannot switch location services off. On iOS, `settings location on|off` refuses; grant the app
location with `settings permission grant location`. `settings permission` refuses
`location-always`, `contacts-limited`, and `--mode limited`. Revyl's iOS simulators refuse `camera` and `notifications`.

The plugin refuses every other command with a reason that names the substitute. It does not
report a refused command as a success:
Expand Down
Loading