From 263ab2b62a4a551bb5e6b2e5aeab5ab34c52f763 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:14:06 +0200 Subject: [PATCH 01/21] test: allocate with osVersion makes a lease request that carries it as typed (#828) --- src/agent-device/edge.test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index 2a23bbb98..2debb758d 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -637,6 +637,38 @@ 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("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(); From 1707f32eea1470789e119c5c9b04d602ea1e32c7 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:15:32 +0200 Subject: [PATCH 02/21] feat(agent-device): allocate carries osVersion to the lease request (#828) --- src/agent-device/front/allocate.ts | 4 ++++ src/agent-device/shared/rpc.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 470e7b634..249886b6f 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -50,6 +50,7 @@ interface Plan { readonly platform: "ios" | "android"; readonly model?: string; readonly class?: "phone" | "tv" | "desktop"; + readonly osVersion?: string; readonly ttlMs?: number; } @@ -109,10 +110,12 @@ function planOf(params: Record, cap: number | undefined): Plan typeof fields.device === "string" && fields.device !== "" ? fields.device : undefined; const klass = typeof fields.target === "string" ? CLASS_OF_TARGET[fields.target] : undefined; const ttlMs = chosenTtl(fields.ttlMs, cap); + const osVersion = typeof fields.osVersion === "string" ? fields.osVersion : undefined; return { leaseId: fields.leaseId as string, platform: fields.platform as "ios" | "android", ...(device === undefined ? (klass === undefined ? {} : { class: klass }) : { model: device }), + ...(osVersion === undefined ? {} : { osVersion }), ...(ttlMs === undefined ? {} : { ttlMs }), }; } @@ -207,6 +210,7 @@ async function requestLease( timeoutMs: QUEUE_WAIT_MS, ...(plan.model === undefined ? {} : { model: plan.model }), ...(plan.class === undefined ? {} : { class: plan.class }), + ...(plan.osVersion === undefined ? {} : { osVersion: plan.osVersion }), ...(plan.ttlMs === undefined ? {} : { ttlMs: plan.ttlMs }), ...requiresOf(context), }, diff --git a/src/agent-device/shared/rpc.ts b/src/agent-device/shared/rpc.ts index c4d27fa4d..4c3dad10d 100644 --- a/src/agent-device/shared/rpc.ts +++ b/src/agent-device/shared/rpc.ts @@ -96,6 +96,7 @@ export interface LeaseCallFields { readonly udid: unknown; readonly serial: unknown; readonly deviceKey: unknown; + readonly osVersion: unknown; } export function leaseCallFields(params: Record): LeaseCallFields { @@ -103,6 +104,7 @@ export function leaseCallFields(params: Record): LeaseCallField device: params["device"], deviceKey: params["deviceKey"], leaseId: params["leaseId"], + osVersion: params["osVersion"], platform: params["platform"], serial: params["serial"], target: params["target"], From 9d8f8ca4a7e9fc0330e437139ffbb1eebd7eee41 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:16:23 +0200 Subject: [PATCH 03/21] test: allocate on Android with imageTag makes a lease request that carries it (#828) --- src/agent-device/edge.test.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index 2debb758d..0a55ca850 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -122,6 +122,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 { const server = createServer(); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); @@ -669,6 +685,21 @@ describe("the agent-device front, through the whole worker daemon", () => { 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 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(); From bea8769f575beaaadb5bf2a94a566580d7de9b47 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:16:38 +0200 Subject: [PATCH 04/21] feat(agent-device): allocate carries imageTag to the lease request (#828) --- src/agent-device/front/allocate.ts | 4 ++++ src/agent-device/shared/rpc.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 249886b6f..2545fe279 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -50,6 +50,7 @@ interface Plan { readonly platform: "ios" | "android"; readonly model?: string; readonly class?: "phone" | "tv" | "desktop"; + readonly imageTag?: string; readonly osVersion?: string; readonly ttlMs?: number; } @@ -110,11 +111,13 @@ function planOf(params: Record, cap: number | undefined): Plan typeof fields.device === "string" && fields.device !== "" ? fields.device : undefined; const klass = typeof fields.target === "string" ? CLASS_OF_TARGET[fields.target] : undefined; const ttlMs = chosenTtl(fields.ttlMs, cap); + const imageTag = typeof fields.imageTag === "string" ? fields.imageTag : undefined; const osVersion = typeof fields.osVersion === "string" ? fields.osVersion : undefined; return { leaseId: fields.leaseId as string, platform: fields.platform as "ios" | "android", ...(device === undefined ? (klass === undefined ? {} : { class: klass }) : { model: device }), + ...(imageTag === undefined ? {} : { imageTag }), ...(osVersion === undefined ? {} : { osVersion }), ...(ttlMs === undefined ? {} : { ttlMs }), }; @@ -210,6 +213,7 @@ async function requestLease( timeoutMs: QUEUE_WAIT_MS, ...(plan.model === undefined ? {} : { model: plan.model }), ...(plan.class === undefined ? {} : { class: plan.class }), + ...(plan.imageTag === undefined ? {} : { imageTag: plan.imageTag }), ...(plan.osVersion === undefined ? {} : { osVersion: plan.osVersion }), ...(plan.ttlMs === undefined ? {} : { ttlMs: plan.ttlMs }), ...requiresOf(context), diff --git a/src/agent-device/shared/rpc.ts b/src/agent-device/shared/rpc.ts index 4c3dad10d..e168d9e7f 100644 --- a/src/agent-device/shared/rpc.ts +++ b/src/agent-device/shared/rpc.ts @@ -96,6 +96,7 @@ export interface LeaseCallFields { readonly udid: unknown; readonly serial: unknown; readonly deviceKey: unknown; + readonly imageTag: unknown; readonly osVersion: unknown; } @@ -103,6 +104,7 @@ export function leaseCallFields(params: Record): LeaseCallField return { device: params["device"], deviceKey: params["deviceKey"], + imageTag: params["imageTag"], leaseId: params["leaseId"], osVersion: params["osVersion"], platform: params["platform"], From 87ef1a6a766f38adb8cc3a5aff0725813d003f4a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:16:58 +0200 Subject: [PATCH 05/21] test: the allocate the agent-device daemon receives carries neither osVersion nor imageTag (#828) --- src/agent-device/edge.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index 0a55ca850..03ce6e439 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -700,6 +700,21 @@ describe("the agent-device front, through the whole worker daemon", () => { ]); }); + 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 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(); From 7f858a237e90919710a93ce32ed5365f028b4eed Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:17:10 +0200 Subject: [PATCH 06/21] feat(agent-device): the allocate to the daemon drops osVersion and imageTag (#828) --- src/agent-device/front/allocate.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 2545fe279..2dcd25fc6 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -249,7 +249,14 @@ function forwardedBody( address: string, ceiling: number, ) { - const { device: _device, deviceKey: _deviceKey, target: _target, ...rest } = call.params; + const { + device: _device, + deviceKey: _deviceKey, + imageTag: _imageTag, + osVersion: _osVersion, + target: _target, + ...rest + } = call.params; return { ...call.body, params: { From 5c1a94f615842aeaf6864a652f51695bd0a48794 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:17:55 +0200 Subject: [PATCH 07/21] test: the relayed allocate reply carries data.lease.device (#828) --- src/agent-device/edge.test.ts | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index 03ce6e439..fd1dfb957 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -715,6 +715,43 @@ describe("the agent-device front, through the whole worker daemon", () => { } }); + 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 }; + }; + 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("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(); From 8954aff6735a4710a3467c81e3f93bfd9646aafb Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:19:39 +0200 Subject: [PATCH 08/21] feat(agent-device): the relayed allocate reply names the granted device (#828) --- src/agent-device/front/allocate.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 2dcd25fc6..828d1b239 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -342,7 +342,7 @@ async function keepOrRelease( ttlMs: grant.lease.ttlMs, ...replyFacts(reading.result), }); - return relay(reply); + return relay(withGrantedDevice(reply, grant.device.spec)); } const lease = replyData(reading.result)?.["lease"]; const named = @@ -375,6 +375,23 @@ async function settleNoReply( if (!holds) await releaseSimlock(context, identity, plan.leaseId); } +// why: agent-device's reply names the lease but not which model, OS and image Simlock picked, so the front adds that beside the lease for the CLI. +function withGrantedDevice( + reply: T, + spec: Grant["device"]["spec"], +): T { + if (reply.text === undefined) return reply; + const body = JSON.parse(reply.text) as Record; + const lease = asRecord(asRecord(asRecord(body["result"])?.["data"])?.["lease"]); + if (lease === undefined) return reply; + lease["device"] = { + model: spec.model, + osVersion: spec.osVersion, + ...(spec.imageTag === undefined ? {} : { imageTag: spec.imageTag }), + }; + return { ...reply, text: JSON.stringify(body) }; +} + function replyFacts(result: Record) { const data = replyData(result); const device = asRecord(data?.["device"]); From 87d42796784254980d80f31b2ba448c68c989a7a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:20:04 +0200 Subject: [PATCH 09/21] test: simlock's refusals for osVersion and imageTag come back to the CLI (#828) --- src/agent-device/edge.test.ts | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index fd1dfb957..fa1dc2865 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -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; @@ -184,6 +185,7 @@ async function setUp(options: SetUpOptions = {}): Promise { }, }), 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, @@ -752,6 +754,42 @@ describe("the agent-device front, through the whole worker daemon", () => { } }); + 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[] = [ + { 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("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(); From d7c8121a0d95414d9835e0d991357e8b9b48d124 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:20:37 +0200 Subject: [PATCH 10/21] test: a command's osVersion and imageTag never reach agent-device (#828) --- src/agent-device/edge.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index fa1dc2865..c0ca0fa5e 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -3277,6 +3277,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 }[]; + 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" }); From 2e18e286f8e49236985e3a18194e97e9951cee5b Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:21:15 +0200 Subject: [PATCH 11/21] feat(agent-device): commands drop osVersion and imageTag (#828) --- src/agent-device/front/request-rules.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/agent-device/front/request-rules.ts b/src/agent-device/front/request-rules.ts index db718fac8..c60999843 100644 --- a/src/agent-device/front/request-rules.ts +++ b/src/agent-device/front/request-rules.ts @@ -117,6 +117,18 @@ function checkTypes(unit: Unit): void { } } +// why: the daemon behind the front does not know these two keys, and only the allocate (read before the rules run) is meant to carry them to simlock. +function ruleLeaseKeys(check: Check, unit: Unit): void { + if (isFlat(unit)) return; + for (const place of places(unit)) { + for (const key of ["osVersion", "imageTag"]) { + if (!(key in place)) continue; + delete place[key]; + check.changed = true; + } + } +} + function rulePlatform(check: Check, unit: Unit): void { for (const place of places(unit, true)) { const platform = place["platform"]; @@ -290,6 +302,7 @@ async function applyRules(check: Check, unit: Unit): Promise { } } rulePlatform(check, unit); + ruleLeaseKeys(check, unit); ruleDeviceSet(unit); await ruleSelectors(check, unit); await ruleSharding(check, unit); From 17ec1bafef380e0e72236f95abfa2fdf700aa913 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:21:40 +0200 Subject: [PATCH 12/21] test: allocate with physical true is refused (#828) --- src/agent-device/edge.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index c0ca0fa5e..5a9a11fcc 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -790,6 +790,21 @@ describe("the agent-device front, through the whole worker daemon", () => { 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("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(); From 761bddb1b46efd96294842add4af9910ee9c6159 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:22:08 +0200 Subject: [PATCH 13/21] feat(agent-device): allocate refuses physical devices (#828) --- src/agent-device/front/allocate.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 828d1b239..b1062808b 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -92,6 +92,9 @@ function refusalFor(params: Record): FrontReply | undefined { return refusal("refused: this agent-device is too old for simlock; upgrade it"); } if (notPlain(params)) return refusal("refused: simlock leases only plain devices"); + if (params["physical"] === true) { + return refusal("refused: physical devices are not available through agent-device"); + } const device = deviceRefusal(fields); if (device !== undefined) return device; if (present(ttl) && !(typeof ttl === "number" && Number.isFinite(ttl) && ttl > 0)) { From c5d9f72179bd52a286fa32d8d5a9cf7f8737c918 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:22:34 +0200 Subject: [PATCH 14/21] test: allocate with an unmapped target is refused (#828) --- src/agent-device/edge.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index 5a9a11fcc..f84b3834c 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -805,6 +805,25 @@ describe("the agent-device front, through the whole worker daemon", () => { 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 ; 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(); From 9016bdd401f1a70a97dca952c071a1c07fa1d951 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:24:21 +0200 Subject: [PATCH 15/21] feat(agent-device): allocate refuses a target the front does not map (#828) --- src/agent-device/front/allocate.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index b1062808b..1b945982f 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -95,6 +95,9 @@ function refusalFor(params: Record): FrontReply | undefined { if (params["physical"] === true) { return refusal("refused: physical devices are not available through agent-device"); } + if (typeof fields.target === "string" && !Object.hasOwn(CLASS_OF_TARGET, fields.target)) { + return refusal(`refused: unknown --target ${fields.target}; use mobile, tv or desktop`); + } const device = deviceRefusal(fields); if (device !== undefined) return device; if (present(ttl) && !(typeof ttl === "number" && Number.isFinite(ttl) && ttl > 0)) { From ec3447c114b7c283e987fb868c2db30e78679243 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:24:53 +0200 Subject: [PATCH 16/21] test: a non-string osVersion or imageTag is refused as malformed (#828) --- src/agent-device/edge.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/agent-device/edge.test.ts b/src/agent-device/edge.test.ts index f84b3834c..3e64bcdbd 100644 --- a/src/agent-device/edge.test.ts +++ b/src/agent-device/edge.test.ts @@ -3499,7 +3499,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 } })); @@ -3514,6 +3514,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 () => { From a49b070550f89ecdb943c752d55e5da198b066c5 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:26:56 +0200 Subject: [PATCH 17/21] feat(agent-device): osVersion and imageTag are type-checked as strings (#828) --- src/agent-device/shared/selectors.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/agent-device/shared/selectors.ts b/src/agent-device/shared/selectors.ts index c95c55405..adb392281 100644 --- a/src/agent-device/shared/selectors.ts +++ b/src/agent-device/shared/selectors.ts @@ -8,7 +8,16 @@ export function asRecord(value: unknown): Fields | undefined { export const present = (value: unknown): boolean => value !== undefined && value !== null; -const STRING_FIELDS = ["platform", "target", "udid", "serial", "device", "deviceKey"] as const; +const STRING_FIELDS = [ + "platform", + "target", + "udid", + "serial", + "device", + "deviceKey", + "osVersion", + "imageTag", +] as const; const INTEGER_FIELDS = ["shardAll", "shardSplit"] as const; export function malformedField(source: Fields): string | undefined { From ff1e20ba35febab51ddfd6a0a04ffed8443324cd Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:27:21 +0200 Subject: [PATCH 18/21] test: allocate through the gateway carries osVersion and the reply names the device (#828) --- src/agent-device/gateway-front.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/agent-device/gateway-front.test.ts b/src/agent-device/gateway-front.test.ts index 12537f335..09736e6b3 100644 --- a/src/agent-device/gateway-front.test.ts +++ b/src/agent-device/gateway-front.test.ts @@ -818,6 +818,18 @@ describe("the agent-device front, through a gateway", { timeout: 60_000 }, () => expect(request?.input).toMatchObject({ requesterId: hex(1), requires: ["agent-device"] }); }); + it('allocate through the gateway with `osVersion` ">=18" reaches the lease request with it and `requires: ["agent-device"]`, and the reply carries `data.lease.device`', async () => { + const rig = await setUp(); + const tap = tapDispatch(rig.gateway); + + const answer = await envelopeOf(await allocate(rig, 1, { osVersion: ">=18" })); + + const request = tap.inputs.find((entry) => entry.operation === "lease.request"); + expect(request?.input).toMatchObject({ osVersion: ">=18", requires: ["agent-device"] }); + const lease = (answer.result as { data: { lease: Record } }).data.lease; + expect(lease["device"]).toMatchObject({ osVersion: "26.5" }); + }); + it("allocate on a worker, directly, sends no `requires`, as today", async () => { const rig = await setUp({ workers: [{ direct: true, label: "w1" }] }); const member = first(rig); From 614e2029f1c7b4de89dd05f84b761cbbdbc60241 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:27:42 +0200 Subject: [PATCH 19/21] docs(agent-device): --os and --image-tag, physical and unmapped target refusals (#828) --- ...3-agent-device-is-served-by-an-optional-edge-module.md | 3 ++- src/agent-device/AGENTS.md | 8 ++++---- .../src/content/docs/docs/using-devices/agent-device.mdx | 7 +++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md b/docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md index f9b0d7c3e..821072e8a 100644 --- a/docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md +++ b/docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md @@ -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. @@ -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. diff --git a/src/agent-device/AGENTS.md b/src/agent-device/AGENTS.md index db2e7402a..b1cbeb0be 100644 --- a/src/agent-device/AGENTS.md +++ b/src/agent-device/AGENTS.md @@ -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. 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 b97a5fd1c..280339455 100644 --- a/website/src/content/docs/docs/using-devices/agent-device.mdx +++ b/website/src/content/docs/docs/using-devices/agent-device.mdx @@ -89,7 +89,7 @@ agent-device close The URL is the worker's HTTP address, or the gateway's (port 4700 by default), followed by `/v1/agent-device`. -- **`open` takes a lease.** `--platform ios` or `android` is required. `--device "iPhone 16"` asks for that model. `--target mobile`, `tv` or `desktop` asks for a phone, TV or desktop device. With neither, Simlock leases a phone. If every match is taken, `open` waits in Simlock's queue for up to 330 seconds, then fails with `timed out waiting for a device; nothing was leased`. +- **`open` takes a lease.** `--platform ios` or `android` is required. `--device "iPhone 16"` asks for that model. `--target mobile`, `tv` or `desktop` asks for a phone, TV or desktop device. `--os` names an OS version or a range (`26.0`, `>=18 <27`, `18 - 26`; a range gets the newest installed version in it), and on Android `--image-tag` names a system image tag such as `google_apis_playstore`. With none of these, Simlock leases a phone. The reply to `open` names the model, OS and image tag you got. If every match is taken, `open` waits in Simlock's queue for up to 330 seconds, then fails with `timed out waiting for a device; nothing was leased`. - **The lease is an ordinary Simlock lease.** `simlock status` and the console show it, held by the token. Its id is the one agent-device chose. - **Commands keep the lease alive.** agent-device renews it before each command and while a command runs. An agent that stops sending commands loses the device one TTL later. The TTL is the one agent-device asks for, or the worker's default (15 minutes) when it asks for none. An admin can cap it with `capabilities.agent-device.leaseTtlCapMs`. - **`close` gives the device back.** Simlock ends the lease and wipes the device for the next agent. @@ -124,7 +124,10 @@ Restarting a worker's daemon ends every agent-device lease on that worker, even | `refused: name --platform ios or android` | `open` needs `--platform`. | | `refused: simlock picks the device` | A `--udid`, `--serial` or `--device` names a device that is not the one Simlock leased to you. | | `refused: simlock chooses the device set; run disconnect, then connect proxy without --ios-simulator-device-set` | Simlock decides which devices agent-device sees. | -| `refused by simlock: ` | Simlock refused the lease, for example a model it does not know. | +| `refused: physical devices are not available through agent-device` | `open` asked for a physical device; agent-device leases only simulators and emulators through Simlock. | +| `refused: unknown --target ; use mobile, tv or desktop` | `--target` named a device class Simlock does not map. | +| `refused: malformed osVersion`, `refused: malformed imageTag` | `--os` or `--image-tag` was not a string. | +| `refused by simlock: ` | Simlock refused the lease, for example a model it does not know, an OS range it cannot read, or an image tag on iOS. | | `refused: this agent-device is too old to use through a gateway; update it` | Upgrade 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. | From 0500152abe6333ee5becdc432af4a4cefa0224c2 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 19:29:30 +0200 Subject: [PATCH 20/21] refactor(agent-device): split allocate's refusal, plan and lease input to stay under the complexity limit (#828) --- src/agent-device/front/allocate.ts | 58 ++++++++++++++++++------------ 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/agent-device/front/allocate.ts b/src/agent-device/front/allocate.ts index 1b945982f..de8433bde 100644 --- a/src/agent-device/front/allocate.ts +++ b/src/agent-device/front/allocate.ts @@ -85,6 +85,19 @@ function deviceRefusal(fields: ReturnType): FrontReply | return undefined; } +function unsupportedSelector( + params: Record, + fields: ReturnType, +): FrontReply | undefined { + if (params["physical"] === true) { + return refusal("refused: physical devices are not available through agent-device"); + } + if (typeof fields.target === "string" && !Object.hasOwn(CLASS_OF_TARGET, fields.target)) { + return refusal(`refused: unknown --target ${fields.target}; use mobile, tv or desktop`); + } + return undefined; +} + function refusalFor(params: Record): FrontReply | undefined { const fields = leaseCallFields(params); const ttl = fields.ttlMs; @@ -92,12 +105,8 @@ function refusalFor(params: Record): FrontReply | undefined { return refusal("refused: this agent-device is too old for simlock; upgrade it"); } if (notPlain(params)) return refusal("refused: simlock leases only plain devices"); - if (params["physical"] === true) { - return refusal("refused: physical devices are not available through agent-device"); - } - if (typeof fields.target === "string" && !Object.hasOwn(CLASS_OF_TARGET, fields.target)) { - return refusal(`refused: unknown --target ${fields.target}; use mobile, tv or desktop`); - } + const unsupported = unsupportedSelector(params, fields); + if (unsupported !== undefined) return unsupported; const device = deviceRefusal(fields); if (device !== undefined) return device; if (present(ttl) && !(typeof ttl === "number" && Number.isFinite(ttl) && ttl > 0)) { @@ -111,20 +120,24 @@ function holderReply(holder: Awaited> return notAvailable(holder.state === "unavailable" ? holder.reason : NO_CEILING); } +function stringFields(fields: ReturnType) { + return { + ...(typeof fields.imageTag === "string" ? { imageTag: fields.imageTag } : {}), + ...(typeof fields.osVersion === "string" ? { osVersion: fields.osVersion } : {}), + }; +} + function planOf(params: Record, cap: number | undefined): Plan { const fields = leaseCallFields(params); const device = typeof fields.device === "string" && fields.device !== "" ? fields.device : undefined; const klass = typeof fields.target === "string" ? CLASS_OF_TARGET[fields.target] : undefined; const ttlMs = chosenTtl(fields.ttlMs, cap); - const imageTag = typeof fields.imageTag === "string" ? fields.imageTag : undefined; - const osVersion = typeof fields.osVersion === "string" ? fields.osVersion : undefined; return { leaseId: fields.leaseId as string, platform: fields.platform as "ios" | "android", ...(device === undefined ? (klass === undefined ? {} : { class: klass }) : { model: device }), - ...(imageTag === undefined ? {} : { imageTag }), - ...(osVersion === undefined ? {} : { osVersion }), + ...stringFields(fields), ...(ttlMs === undefined ? {} : { ttlMs }), }; } @@ -166,6 +179,18 @@ function requiresOf(context: FrontContext): { readonly requires?: string[] } { return required.length === 0 ? {} : { requires: [...required] }; } +function leaseInput(context: FrontContext, plan: Plan) { + const { leaseId, platform, ...constraints } = plan; + return { + ...constraints, + leaseId, + platform, + requesterId: leaseId, + timeoutMs: QUEUE_WAIT_MS, + ...requiresOf(context), + }; +} + async function requestLease( context: FrontContext, identity: Identity, @@ -212,18 +237,7 @@ async function requestLease( try { const grant = await context.dispatch( "lease.request", - { - leaseId: plan.leaseId, - platform: plan.platform, - requesterId: plan.leaseId, - timeoutMs: QUEUE_WAIT_MS, - ...(plan.model === undefined ? {} : { model: plan.model }), - ...(plan.class === undefined ? {} : { class: plan.class }), - ...(plan.imageTag === undefined ? {} : { imageTag: plan.imageTag }), - ...(plan.osVersion === undefined ? {} : { osVersion: plan.osVersion }), - ...(plan.ttlMs === undefined ? {} : { ttlMs: plan.ttlMs }), - ...requiresOf(context), - }, + leaseInput(context, plan), buildHttpSession(identity, { onRequestAdmitted: () => { admitted = true; From a97aa7abeb833a8e745e3e5b8cf9acdd2072a3e7 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 20:34:21 +0200 Subject: [PATCH 21/21] docs(agent-device): fix the ruleLeaseKeys why comment (#828) --- src/agent-device/front/request-rules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent-device/front/request-rules.ts b/src/agent-device/front/request-rules.ts index c60999843..f10f0ceb2 100644 --- a/src/agent-device/front/request-rules.ts +++ b/src/agent-device/front/request-rules.ts @@ -117,7 +117,7 @@ function checkTypes(unit: Unit): void { } } -// why: the daemon behind the front does not know these two keys, and only the allocate (read before the rules run) is meant to carry them to simlock. +// why: the daemon behind the front does not know these two 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)) {