diff --git a/src/agent-device/AGENTS.md b/src/agent-device/AGENTS.md index 594bbd1d8..12bf1a132 100644 --- a/src/agent-device/AGENTS.md +++ b/src/agent-device/AGENTS.md @@ -93,10 +93,10 @@ under leasing's signal (`lease.endWaitMs`). 1. CLI `open` → `POST /v1/agent-device/rpc` `agent_device.lease.allocate` with its lease id. 2. Front refuses a non-plain lease, no `--platform`, a bad `ttlMs`, no lease id, a `physical` that is not a boolean, a `deviceBaseline` that is not text, or a `--target` it does not map. 3. `lease.request`: `leaseId` = `requesterId` = the CLI's lease id; `--device` → model, - `--target` → class, `--os` → `osVersion`, `--image-tag` → `imageTag`, `deviceBaseline` → `baseline` (and then no class from `--target`), `physical: true` → `physical: true` (Simlock picks an enrolled phone); TTL = sent, capped by `capabilities.agent-device.leaseTtlCapMs`, at least 5 s; + `--target` → class, `--os` → `osVersion`, `--image-tag` → `imageTag`, `deviceBaseline` → `baseline` (and then no class from `--target`), `deviceMode` → `mode`, `physical: true` → `physical: true` (Simlock picks an enrolled phone); TTL = sent, capped by `capabilities.agent-device.leaseTtlCapMs`, at least 5 s; queue wait 330 s. CLI hangs up → `lease.cancel`, retried every 1 s. 4. Grant → allocate forwarded with the device address (`udid`/`serial`; a phone's is its enrolled id), to the phone daemon for a phone and the simulator daemon otherwise (`RoutedHostLink`, which remembers each lease's daemon until the lease ends), no - `device`/`deviceKey`/`target`/`osVersion`/`imageTag`/`deviceBaseline`/`physical` (commands lose `osVersion`, `imageTag` and `deviceBaseline` too), `ttlMs` = max(24 h, 2 × `lease.maxTtlMs`). + `device`/`deviceKey`/`target`/`osVersion`/`imageTag`/`deviceBaseline`/`deviceMode`/`physical` (commands lose `osVersion`, `imageTag`, `deviceBaseline` and `deviceMode` too), `ttlMs` = max(24 h, 2 × `lease.maxTtlMs`). 5. agent-device error → simlock lease released. Success → front map entry; reply relayed with `data.lease.device` `{ model, osVersion, physical, imageTag?, baseline? { name, version } }` added from the grant. 6. Commands carry the lease id; rules check, renewals run, `devices` filtered. 7. Release → agent-device first, then `lease.release` → the device is deleted; lease-end finds no scope left. diff --git a/src/agent-device/device-mode.test.ts b/src/agent-device/device-mode.test.ts new file mode 100644 index 000000000..bec235bd9 --- /dev/null +++ b/src/agent-device/device-mode.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from "vitest"; + +import { + adminSession, + allocate, + forwarded, + granted, + hex, + iosDriver, + leases, + receivedAllocate, + refused, + requestedSpecs, + runCommand, + setUp, + simlockMessage, + type Rig, +} from "./test-support.js"; + +async function grantedMode(rig: Rig, n: number): Promise { + const lease = (await leases(rig)).find((entry) => entry.id === hex(n)); + const devices = (await rig.daemon.dispatch( + "list.get", + { kind: "devices" }, + adminSession, + )) as unknown as readonly { readonly id: string; readonly mode?: string }[]; + return devices.find((device) => device.id === lease?.deviceId)?.mode; +} + +describe("the agent-device front's `deviceMode`", () => { + it('allocate with `deviceMode` "full" or "slim" makes a lease request whose `mode` is it, grants a device of that mode, and sends an allocate the agent-device daemon receives without it; `deviceMode: null` is absent; any other `deviceMode` answers `refused by simlock:` with the message `lease.request` gives it and no lease', async () => { + const rig = await setUp({ + drivers: (clock) => [iosDriver(clock, ["26.5"])], + maxRunning: 8, + }); + + await granted(rig, 1, { deviceMode: "full" }); + await granted(rig, 2, { deviceMode: "slim" }); + await granted(rig, 3, { deviceMode: null }); + for (const deviceMode of ["fast", 1]) { + const expected = await simlockMessage(rig, { + leaseId: hex(9), + mode: deviceMode, + platform: "ios", + }); + await refused(rig, () => allocate(rig, 4, { deviceMode }), `refused by simlock: ${expected}`); + } + + expect(await requestedSpecs(rig, hex(1))).toEqual([{ mode: "full", platform: "ios" }]); + expect(await requestedSpecs(rig, hex(2))).toEqual([{ mode: "slim", platform: "ios" }]); + expect(await requestedSpecs(rig, hex(3))).toEqual([{ platform: "ios" }]); + expect(await grantedMode(rig, 1)).toBe("full"); + expect(await grantedMode(rig, 2)).toBe("slim"); + expect(receivedAllocate(rig, 1)).not.toHaveProperty("deviceMode"); + expect(receivedAllocate(rig, 2)).not.toHaveProperty("deviceMode"); + expect((await leases(rig)).map((lease) => lease.id)).toEqual([hex(1), hex(2), hex(3)]); + }); + + it("a command with `deviceMode` on `flags`, on `input` or inside a batch step reaches agent-device without it", async () => { + const rig = await setUp(); + await granted(rig, 1); + const sent = { deviceMode: "full", platform: "ios" }; + + const onFlags = await forwarded(rig, () => runCommand(rig, "snapshot", 1, { flags: sent })); + const onInput = await forwarded(rig, () => runCommand(rig, "snapshot", 1, { input: sent })); + const inStep = await forwarded(rig, () => + runCommand(rig, "batch", 1, { + flags: { batchSteps: [{ command: "snapshot", flags: sent }] }, + }), + ); + + expect(onFlags["flags"]).toEqual({ platform: "ios" }); + expect(onInput["input"]).toEqual({ platform: "ios" }); + const steps = (inStep["flags"] as { batchSteps: { flags: unknown }[] }).batchSteps; + expect(steps[0]?.flags).toEqual({ platform: "ios" }); + }); +}); diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index ab0685a58..8b0974a89 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -53,6 +53,7 @@ interface Plan { readonly class?: "phone" | "tv" | "desktop"; readonly imageTag?: string; readonly osVersion?: string; + readonly mode?: "slim" | "full"; readonly physical?: true; readonly ttlMs?: number; } @@ -120,6 +121,7 @@ function stringFields(fields: ReturnType) { return { ...(typeof fields.imageTag === "string" ? { imageTag: fields.imageTag } : {}), ...(typeof fields.osVersion === "string" ? { osVersion: fields.osVersion } : {}), + ...(present(fields.deviceMode) ? { mode: fields.deviceMode as "slim" | "full" } : {}), }; } @@ -277,6 +279,7 @@ function forwardedBody( device: _device, deviceBaseline: _deviceBaseline, deviceKey: _deviceKey, + deviceMode: _deviceMode, imageTag: _imageTag, osVersion: _osVersion, physical: _physical, diff --git a/src/agent-device/front/request-rules.ts b/src/agent-device/front/request-rules.ts index 64a658304..74b3745c4 100644 --- a/src/agent-device/front/request-rules.ts +++ b/src/agent-device/front/request-rules.ts @@ -133,11 +133,11 @@ function checkTypes(unit: Unit): void { } } -// why: the daemon behind the front does not know these three keys, so a request that is not the flat allocate form must not carry them; the flat allocate is exempt because simlock reads them from it. +// why: the daemon behind the front does not know these keys, so a request that is not the flat allocate form must not carry them; the flat allocate is exempt because simlock reads them from it. function ruleLeaseKeys(check: Check, unit: Unit): void { if (isFlat(unit)) return; for (const place of places(unit)) { - for (const key of ["osVersion", "imageTag", "deviceBaseline"]) { + for (const key of ["osVersion", "imageTag", "deviceBaseline", "deviceMode"]) { if (!(key in place)) continue; delete place[key]; check.changed = true; diff --git a/src/agent-device/shared/rpc.ts b/src/agent-device/shared/rpc.ts index 501e89489..e5ca1373c 100644 --- a/src/agent-device/shared/rpc.ts +++ b/src/agent-device/shared/rpc.ts @@ -99,6 +99,7 @@ export interface LeaseCallFields { readonly imageTag: unknown; readonly osVersion: unknown; readonly deviceBaseline: unknown; + readonly deviceMode: unknown; } export function leaseCallFields(params: Record): LeaseCallFields { @@ -106,6 +107,7 @@ export function leaseCallFields(params: Record): LeaseCallField device: params["device"], deviceBaseline: params["deviceBaseline"], deviceKey: params["deviceKey"], + deviceMode: params["deviceMode"], imageTag: params["imageTag"], leaseId: params["leaseId"], osVersion: params["osVersion"], diff --git a/src/agent-device/test-support.ts b/src/agent-device/test-support.ts index deb5482b9..75d683514 100644 --- a/src/agent-device/test-support.ts +++ b/src/agent-device/test-support.ts @@ -91,8 +91,9 @@ export interface Rig { readonly restart: (between?: () => void) => Promise; } -export function iosDriver(clock: FakeClock): FakeDriver { +export function iosDriver(clock: FakeClock, slimmableOsVersions?: readonly string[]): FakeDriver { return new FakeDriver({ + ...(slimmableOsVersions === undefined ? {} : { slimmableOsVersions }), availableOsVersions: ["26.5"], clock, defaultModels: { desktop: ["Mac"], phone: ["iPhone 16"], tv: ["Apple TV 4K"] }, diff --git a/website/src/content/docs/docs/using-devices/agent-device.mdx b/website/src/content/docs/docs/using-devices/agent-device.mdx index 68b9c5309..2a887b06f 100644 --- a/website/src/content/docs/docs/using-devices/agent-device.mdx +++ b/website/src/content/docs/docs/using-devices/agent-device.mdx @@ -123,6 +123,7 @@ Each agent-device session holds one device. To use a second device, open a secon - `--os` names an OS version or a range: `26.0`, `>=18 <27`, `18 - 26`. A range gets the newest installed version inside it. - On Android, `--image-tag` names a system image tag, such as `google_apis_playstore`. - `--device-baseline` (the `deviceBaseline` key of an allocate) leases a fresh copy of a saved Simlock baseline. It needs the agent-device release that adds the flag. `--target` is ignored beside it; Simlock refuses a baseline that is unknown, stale or doesn't match `--platform`, `--device` or `--os`, and `--physical` with one. +- `--device-mode` (the `deviceMode` key of an allocate) asks for a `full` or `slim` iOS simulator. It needs the agent-device release that adds the flag. Without it, you get the worker's default. [Slim is best effort](/simlock/docs/using-devices/choosing-a-device#choose-slim-or-full-ios): below iOS 18.5 you get a full simulator. - `--physical` leases an enrolled phone (see [Use a real phone](#use-a-real-phone)). The reply to `open` names the model, OS and image tag you got, and the baseline name and version when you asked for one. If every matching device is taken, `open` waits for up to 330 seconds, then fails with `timed out waiting for a device; nothing was leased`. @@ -212,7 +213,7 @@ Restarting a worker's daemon ends every agent-device lease on that worker, even | `refused: malformed physical` | `physical` wasn't true or false. | | `refused: unknown --target ; use mobile, tv or desktop` | `--target` named a kind of device Simlock doesn't know. | | `refused: malformed osVersion`, `refused: malformed imageTag`, `refused: malformed deviceBaseline` | `--os`, `--image-tag` or `deviceBaseline` wasn't text. | -| `refused by simlock: ` | Simlock refused the lease, for example for a model it doesn't know, an OS range it can't read, or an image tag on iOS. | +| `refused by simlock: ` | Simlock refused the lease, for example for a model it doesn't know, an OS range it can't read, an image tag on iOS, or a `deviceMode` other than `slim` or `full` (`null` means absent). | | `refused: this agent-device is too old to use through a gateway; update it` | Update the agent's agent-device. | | `agent-device is not available on this simlock: ` | agent-device is missing, too old, starting or crashed on the worker. | | `agent-device is not available on this simlock: phone daemon: ` | Phone support on the worker is starting or down. Simulator leases keep working; `simlock doctor` says what is wrong. |