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
4 changes: 2 additions & 2 deletions src/agent-device/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
77 changes: 77 additions & 0 deletions src/agent-device/device-mode.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> {
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" });
});
});
3 changes: 3 additions & 0 deletions src/agent-device/front/allocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -120,6 +121,7 @@ function stringFields(fields: ReturnType<typeof leaseCallFields>) {
return {
...(typeof fields.imageTag === "string" ? { imageTag: fields.imageTag } : {}),
...(typeof fields.osVersion === "string" ? { osVersion: fields.osVersion } : {}),
...(present(fields.deviceMode) ? { mode: fields.deviceMode as "slim" | "full" } : {}),
};
}

Expand Down Expand Up @@ -277,6 +279,7 @@ function forwardedBody(
device: _device,
deviceBaseline: _deviceBaseline,
deviceKey: _deviceKey,
deviceMode: _deviceMode,
imageTag: _imageTag,
osVersion: _osVersion,
physical: _physical,
Expand Down
4 changes: 2 additions & 2 deletions src/agent-device/front/request-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/agent-device/shared/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ export interface LeaseCallFields {
readonly imageTag: unknown;
readonly osVersion: unknown;
readonly deviceBaseline: unknown;
readonly deviceMode: unknown;
}

export function leaseCallFields(params: Record<string, unknown>): LeaseCallFields {
return {
device: params["device"],
deviceBaseline: params["deviceBaseline"],
deviceKey: params["deviceKey"],
deviceMode: params["deviceMode"],
imageTag: params["imageTag"],
leaseId: params["leaseId"],
osVersion: params["osVersion"],
Expand Down
3 changes: 2 additions & 1 deletion src/agent-device/test-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ export interface Rig {
readonly restart: (between?: () => void) => Promise<Rig>;
}

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"] },
Expand Down
3 changes: 2 additions & 1 deletion website/src/content/docs/docs/using-devices/agent-device.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 <value>; 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: <message>` | 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: <message>` | 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: <reason>` | agent-device is missing, too old, starting or crashed on the worker. |
| `agent-device is not available on this simlock: phone daemon: <message>` | Phone support on the worker is starting or down. Simulator leases keep working; `simlock doctor` says what is wrong. |
Expand Down