Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
263ab2b
test: allocate with osVersion makes a lease request that carries it a…
V3RON Oct 21, 2026
1707f32
feat(agent-device): allocate carries osVersion to the lease request (…
V3RON Oct 21, 2026
9d8f8ca
test: allocate on Android with imageTag makes a lease request that ca…
V3RON Oct 21, 2026
bea8769
feat(agent-device): allocate carries imageTag to the lease request (#…
V3RON Oct 21, 2026
87ef1a6
test: the allocate the agent-device daemon receives carries neither o…
V3RON Oct 21, 2026
7f858a2
feat(agent-device): the allocate to the daemon drops osVersion and im…
V3RON Oct 21, 2026
5c1a94f
test: the relayed allocate reply carries data.lease.device (#828)
V3RON Oct 21, 2026
8954aff
feat(agent-device): the relayed allocate reply names the granted devi…
V3RON Oct 21, 2026
87d4279
test: simlock's refusals for osVersion and imageTag come back to the …
V3RON Oct 21, 2026
d7c8121
test: a command's osVersion and imageTag never reach agent-device (#828)
V3RON Oct 21, 2026
2e18e28
feat(agent-device): commands drop osVersion and imageTag (#828)
V3RON Oct 21, 2026
17ec1ba
test: allocate with physical true is refused (#828)
V3RON Oct 21, 2026
761bddb
feat(agent-device): allocate refuses physical devices (#828)
V3RON Oct 21, 2026
c5d9f72
test: allocate with an unmapped target is refused (#828)
V3RON Oct 21, 2026
9016bdd
feat(agent-device): allocate refuses a target the front does not map …
V3RON Oct 21, 2026
ec3447c
test: a non-string osVersion or imageTag is refused as malformed (#828)
V3RON Oct 21, 2026
a49b070
feat(agent-device): osVersion and imageTag are type-checked as string…
V3RON Oct 21, 2026
ff1e20b
test: allocate through the gateway carries osVersion and the reply na…
V3RON Oct 21, 2026
614e202
docs(agent-device): --os and --image-tag, physical and unmapped targe…
V3RON Oct 21, 2026
0500152
refactor(agent-device): split allocate's refusal, plan and lease inpu…
V3RON Oct 21, 2026
a97aa7a
docs(agent-device): fix the ruleLeaseKeys why comment (#828)
V3RON Oct 21, 2026
322d9cf
Merge remote-tracking branch 'origin/main' into HEAD
V3RON Oct 21, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ An agent-device lease and its simlock lease share one id (the CLI chooses
it; ADR 0020 lets simlock keep it) and one lifetime.

- `allocate` becomes `lease.request` as the agent's principal, with the
CLI's selectors mapped to `platform`, and `model` or `class`. A request
CLI's selectors mapped to `platform`, `model` or `class`, `osVersion` and `imageTag`. A request
that waits in simlock's queue holds the CLI's request open. When the CLI
hangs up, the front cancels it; a grant that lands after that is released
at once.
Expand Down Expand Up @@ -124,6 +124,7 @@ Agents sharing a token share a tenant; the docs say not to.
The front also refuses what would reach past the agent's grant:

- a platform other than iOS or Android;
- a physical device (`physical: true`), or a `--target` the front does not map;
- a device selector naming any device but the granted one;
- a route outside a fixed list;
- an `install` or `reinstall` of a path on the worker rather than an upload.
Expand Down
8 changes: 4 additions & 4 deletions src/agent-device/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ under leasing's signal (`lease.endWaitMs`).
## Flow: agent on a worker

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`, or no lease id.
2. Front refuses a non-plain lease, no `--platform`, a bad `ttlMs`, no lease id, `physical: true`, or a `--target` it does not map.
3. `lease.request`: `leaseId` = `requesterId` = the CLI's lease id; `--device` → model,
`--target` → class; TTL = sent, capped by `capabilities.agent-device.leaseTtlCapMs`, at least 5 s;
`--target` → class, `--os` → `osVersion`, `--image-tag` → `imageTag`; 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`), no
`device`/`deviceKey`/`target`, `ttlMs` = max(24 h, 2 × `lease.maxTtlMs`).
5. agent-device error → simlock lease released. Success → front map entry; reply relayed.
`device`/`deviceKey`/`target`/`osVersion`/`imageTag` (commands lose both keys 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, imageTag? }` added from the grant.
6. Commands carry the lease id; rules check, renewals run, `devices` filtered.
7. Release → agent-device first, then `lease.release` → reclaim; lease-end finds no scope left.

Expand Down
223 changes: 222 additions & 1 deletion src/agent-device/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface Envelope {

interface SetUpOptions {
readonly leaseTtlCapMs?: number;
readonly downloads?: "always";
readonly drivers?: (clock: FakeClock) => FakeDriver[];
readonly enabled?: boolean | "unset";
readonly endWaitMs?: number;
Expand Down Expand Up @@ -122,6 +123,22 @@ function androidDriver(clock: FakeClock): FakeDriver {
});
}

function taggedAndroidDriver(clock: FakeClock): FakeDriver {
return new FakeDriver({
availableOsVersions: ["33", "34", "35"],
clock,
defaultModels: { phone: ["Pixel 8"] },
images: [
{ abi: "arm64-v8a", runtime: "33", tag: "google_apis_playstore" },
{ abi: "arm64-v8a", runtime: "34", tag: "google_apis_playstore" },
{ abi: "arm64-v8a", runtime: "35", tag: "default" },
],
knownModels: ["Pixel 8"],
modelClasses: { "Pixel 8": "phone" },
platform: "android",
});
}

async function freeLoopbackPort(): Promise<number> {
const server = createServer();
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
Expand Down Expand Up @@ -168,6 +185,7 @@ async function setUp(options: SetUpOptions = {}): Promise<Rig> {
},
}),
capacity: { config: { maxRunning: options.maxRunning ?? 4 }, strategy: "fixed" },
...(options.downloads === undefined ? {} : { downloads: { policy: options.downloads } }),
http: { enabled: true, host: "127.0.0.1", port },
lease: {
defaultTtlMs,
Expand Down Expand Up @@ -637,6 +655,175 @@ describe("the agent-device front, through the whole worker daemon", () => {
expect(bare.driver.calls.map((call) => call.operation)).not.toContain("installComponent");
});

it('allocate with `osVersion` "26.0", ">=18 <27" or, on Android, "34" makes a lease request that carries it as typed', async () => {
const rig = await setUp({
drivers: (clock) => [
new FakeDriver({
availableOsVersions: ["18.4", "26.0", "27.0"],
clock,
defaultModels: { phone: ["iPhone 16"] },
knownModels: ["iPhone 16"],
modelClasses: { "iPhone 16": "phone" },
platform: "ios",
}),
new FakeDriver({
availableOsVersions: ["34", "35"],
clock,
defaultModels: { phone: ["Pixel 8"] },
knownModels: ["Pixel 8"],
modelClasses: { "Pixel 8": "phone" },
platform: "android",
}),
],
maxRunning: 8,
});

await granted(rig, 1, { osVersion: "26.0" });
await granted(rig, 2, { osVersion: ">=18 <27" });
await granted(rig, 3, { osVersion: "34", platform: "android" });

expect(await requestedSpecs(rig, hex(1))).toEqual([{ osVersion: "26.0", platform: "ios" }]);
expect(await requestedSpecs(rig, hex(2))).toEqual([{ osVersion: ">=18 <27", platform: "ios" }]);
expect(await requestedSpecs(rig, hex(3))).toEqual([{ osVersion: "34", platform: "android" }]);
});

it('allocate on Android with `imageTag` "google_apis_playstore", with and without `osVersion`, makes a lease request that carries it', async () => {
const rig = await setUp({ drivers: (clock) => [taggedAndroidDriver(clock)], maxRunning: 8 });
const android = { platform: "android" };

await granted(rig, 1, { ...android, imageTag: "google_apis_playstore" });
await granted(rig, 2, { ...android, imageTag: "google_apis_playstore", osVersion: "33" });

expect(await requestedSpecs(rig, hex(1))).toEqual([
{ imageTag: "google_apis_playstore", platform: "android" },
]);
expect(await requestedSpecs(rig, hex(2))).toEqual([
{ imageTag: "google_apis_playstore", osVersion: "33", platform: "android" },
]);
});

it("the allocate the agent-device daemon receives carries neither `osVersion` nor `imageTag`, whether the CLI sent one or both", async () => {
const rig = await setUp({ drivers: (clock) => [taggedAndroidDriver(clock)], maxRunning: 8 });
const android = { platform: "android" };

await granted(rig, 1, { ...android, osVersion: "34" });
await granted(rig, 2, { ...android, imageTag: "google_apis_playstore" });
await granted(rig, 3, { ...android, imageTag: "google_apis_playstore", osVersion: "33" });

for (const n of [1, 2, 3]) {
const received = receivedAllocate(rig, n);
expect(received).not.toHaveProperty("osVersion");
expect(received).not.toHaveProperty("imageTag");
}
});

it("the relayed allocate reply carries `data.lease.device` as `{ model, osVersion }` on iOS and on Android without a tag, and `{ model, osVersion, imageTag }` on Android with one, the daemon's `deviceKey` and `data.device` unchanged", async () => {
const rig = await setUp({
drivers: (clock) => [
new FakeDriver({
availableOsVersions: ["18.4", "26.0", "27.0"],
clock,
defaultModels: { phone: ["iPhone 16"] },
knownModels: ["iPhone 16"],
modelClasses: { "iPhone 16": "phone" },
platform: "ios",
}),
taggedAndroidDriver(clock),
],
maxRunning: 8,
});
const cases = [
[1, { osVersion: ">=18 <27" }, { model: "iPhone 16", osVersion: "26.0" }],
[2, {}, { model: "iPhone 16", osVersion: "27.0" }],
[
3,
{ imageTag: "google_apis_playstore", platform: "android" },
{ imageTag: "google_apis_playstore", model: "Pixel 8", osVersion: "34" },
],
[4, { platform: "android" }, { model: "Pixel 8", osVersion: "35" }],
] as const;

for (const [n, extra, device] of cases) {
rig.fake.nameDeviceKeyInNextReply(`key-${n}`);
const result = (await envelopeOf(await allocate(rig, n, extra, {}))).result as {
data: { device: unknown; lease: Record<string, unknown> };
};
expect(result.data.lease["device"]).toEqual(device);
expect(result.data.lease["deviceKey"]).toBe(`key-${n}`);
expect(result.data.device).toMatchObject({ id: expect.any(String) });
}
});

it('allocate with `osVersion` "^18", with `imageTag` on iOS (with a model and without) or with an OS the driver lacks answers `refused by simlock:` with simlock\'s own message and leaves no lease; under `downloads.policy: always` the missing OS is installed for a named model and still refused without one', async () => {
const rig = await setUp();
const refusals: readonly Record<string, unknown>[] = [
{ osVersion: "^18" },
{ device: "iPhone 17 Pro", imageTag: "default" },
{ imageTag: "default" },
{ osVersion: "26.1" },
];

for (const [index, extra] of refusals.entries()) {
const expected = await simlockMessage(rig, {
leaseId: hex(90 + index),
platform: "ios",
...(typeof extra["device"] === "string" ? { model: extra["device"] } : {}),
...(extra["imageTag"] === undefined ? {} : { imageTag: extra["imageTag"] }),
...(extra["osVersion"] === undefined ? {} : { osVersion: extra["osVersion"] }),
});
const answer = await envelopeOf(await allocate(rig, index + 1, extra));
expect(answer.error?.message).toBe(`refused by simlock: ${expected}`);
}
expect(await leases(rig)).toEqual([]);
expect(rig.driver.calls.map((call) => call.operation)).not.toContain("installComponent");

const always = await setUp({ downloads: "always" });
const installs = () =>
always.driver.calls.filter((call) => call.operation === "installComponent").length;
const bare = await envelopeOf(await allocate(always, 1, { osVersion: "26.1" }));
expect(bare.error?.message).toContain("refused by simlock: ");
expect(installs()).toBe(0);
const named = await envelopeOf(
await allocate(always, 2, { device: "iPhone 17 Pro", osVersion: "26.1" }),
);
expect(named.result).toMatchObject({ ok: true });
expect(installs()).toBe(1);
});

it("allocate with `physical: true` answers `refused: physical devices are not available through agent-device` and leaves no lease; `physical: false` and `physical: null` are granted", async () => {
const rig = await setUp({ maxRunning: 8 });

const answer = await envelopeOf(await allocate(rig, 1, { physical: true }));

expect(answer.error).toMatchObject(
refusal("refused: physical devices are not available through agent-device"),
);
expect(await leases(rig)).toEqual([]);
expect(allocatesReceived(rig)).toEqual([]);
await granted(rig, 2, { physical: false });
await granted(rig, 3, { physical: null });
expect((await leases(rig)).map((lease) => lease.id)).toEqual([hex(2), hex(3)]);
});

it('allocate with an unmapped `target`, "watch" or "", alone or beside `device: "iPhone 17 Pro"`, answers `refused: unknown --target <value>; use mobile, tv or desktop` and leaves no lease', async () => {
const rig = await setUp();
const cases = [
[{ target: "watch" }, "watch"],
[{ target: "" }, ""],
[{ device: "iPhone 17 Pro", target: "watch" }, "watch"],
] as const;

for (const [index, [extra, value]] of cases.entries()) {
const answer = await envelopeOf(await allocate(rig, index + 1, extra));

expect(answer.error).toMatchObject(
refusal(`refused: unknown --target ${value}; use mobile, tv or desktop`),
);
}
expect(await leases(rig)).toEqual([]);
expect(allocatesReceived(rig)).toEqual([]);
});

it("the simlock lease's TTL is the CLI's `ttlMs`, 5 s when the CLI sent less, and `lease.defaultTtlMs` when it sent none; the allocate the agent-device daemon receives carries `ttlMs` equal to the ceiling in each case", async () => {
const rig = await setUp();

Expand Down Expand Up @@ -3113,6 +3300,31 @@ describe("the agent-device front's request rules", () => {
);
});

it("a command with `osVersion` or `imageTag` on `flags`, on `input` or inside a batch step reaches agent-device without them, and a command without them reaches it with its flags as sent", async () => {
const rig = await setUp();
await granted(rig, 1);
const keys = { imageTag: "default", osVersion: ">=26" };

const onFlags = await forwarded(rig, () =>
runCommand(rig, "snapshot", 1, { flags: { ...keys, platform: "ios" } }),
);
const onInput = await forwarded(rig, () =>
runCommand(rig, "snapshot", 1, { input: { ...keys, platform: "ios" } }),
);
const inStep = await forwarded(rig, () =>
batch(rig, 1, [{ command: "snapshot", flags: { ...keys, platform: "ios" } }]),
);
const plain = await forwarded(rig, () =>
runCommand(rig, "snapshot", 1, { flags: { platform: "ios" } }),
);

expect(flagsOf(onFlags)).toEqual({ platform: "ios" });
expect(onInput["input"]).toEqual({ platform: "ios" });
const steps = flagsOf(inStep)["batchSteps"] as { flags: Record<string, unknown> }[];
expect(steps[0]?.flags).toEqual({ platform: "ios" });
expect(flagsOf(plain)).toEqual({ platform: "ios" });
});

it("`--device` with the leased model in `input` is removed from `input` and sent as `flags.udid`", async () => {
const rig = await setUp();
await granted(rig, 1, { device: "iPhone 17 Pro" });
Expand Down Expand Up @@ -3276,7 +3488,7 @@ describe("the agent-device front's request rules", () => {
);
});

it("a `udid` that is a number is refused with `refused: malformed udid`; a `shardAll` that is a string with `refused: malformed shardAll`", async () => {
it("a `udid` that is a number is refused with `refused: malformed udid`; a `shardAll` that is a string with `refused: malformed shardAll`; on an allocate an `osVersion` that is a number is refused with `refused: malformed osVersion` and an `imageTag` that is an object with `refused: malformed imageTag`, as is an `osVersion` number on a command's flags, while `osVersion: null` is absent", async () => {
const rig = await setUp();
await granted(rig, 1);
await forwarded(rig, () => runCommand(rig, "test", 1, { flags: { shardAll: 2 } }));
Expand All @@ -3291,6 +3503,15 @@ describe("the agent-device front's request rules", () => {
() => runCommand(rig, "test", 1, { flags: { shardAll: "2" } }),
"refused: malformed shardAll",
);
await refused(rig, () => allocate(rig, 2, { osVersion: 26 }), "refused: malformed osVersion");
await refused(rig, () => allocate(rig, 2, { imageTag: {} }), "refused: malformed imageTag");
await refused(
rig,
() => runCommand(rig, "snapshot", 1, { flags: { osVersion: 26 } }),
"refused: malformed osVersion",
);
await granted(rig, 3, { osVersion: null });
expect(await requestedSpecs(rig, hex(3))).toEqual([{ platform: "ios" }]);
});

it("after simlock ended the lease, a command with that lease's udid is forwarded and agent-device's answer reaches the CLI; one TTL later the same command is refused with `refused: simlock picks the device`", async () => {
Expand Down
Loading
Loading