diff --git a/docs/internal/adr/0082-a-worker-may-serve-only-some-ios-device-modes.md b/docs/internal/adr/0082-a-worker-may-serve-only-some-ios-device-modes.md index dddd63e05..20b5e88ff 100644 --- a/docs/internal/adr/0082-a-worker-may-serve-only-some-ios-device-modes.md +++ b/docs/internal/adr/0082-a-worker-may-serve-only-some-ios-device-modes.md @@ -1,6 +1,6 @@ # 0082. A worker may serve only some iOS device modes -- **Status:** Accepted — not yet implemented +- **Status:** Accepted - **Date:** 2026-11-17 - **Issue:** [#1645](https://github.com/callstackincubator/simlock/issues/1645) (request [#1557](https://github.com/callstackincubator/simlock/issues/1557)) - **Narrows:** [ADR 0007](0007-a-lease-request-chooses-the-device-mode.md) diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index cb2ac0ea6..bf8979c59 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -116,7 +116,7 @@ the status is stale. | [0079](0079-one-capture-per-device-is-shared-by-every-viewer-and-every-capture-sends-a-key-frame-every-5-s.md) | One capture per device is shared by every viewer, and every capture sends a key frame at least every 5 s | Accepted; narrows 0045 §5 | | [0080](0080-an-android-worker-may-enrol-phones-reached-over-the-network.md) | An Android worker may enrol phones reached over the network | Accepted; narrows 0022 §6 and §7, and 0073 §2 and §5 | | [0081](0081-a-remote-caller-speaks-the-daemon-protocol-over-one-websocket-path-with-its-tokens-identity.md) | A remote caller speaks the daemon protocol over one WebSocket path, with its token's identity | Accepted; narrows 0003 §4, §5 and §10, and 0005 §19c | -| [0082](0082-a-worker-may-serve-only-some-ios-device-modes.md) | A worker may serve only some iOS device modes | Accepted — not yet implemented; narrows 0007 §1, §3 and §4, 0008 §1 and §9, 0009 §2, §3, §4 and §5 | +| [0082](0082-a-worker-may-serve-only-some-ios-device-modes.md) | A worker may serve only some iOS device modes | Accepted; narrows 0007 §1, §3 and §4, 0008 §1 and §9, 0009 §2, §3, §4 and §5 | | [0083](0083-a-worker-may-turn-single-slim-services-on-or-off-beside-its-categories.md) | A worker may turn single slim services on or off beside its categories | Accepted; narrows 0060 §1, §2 and 0066 §2 | | [0084](0084-a-worker-may-serve-a-platforms-enrolled-phones-only.md) | A worker may serve a platform's enrolled phones only | Accepted; narrows 0022 and 0009 §4 | | [0085](0085-an-ios-simulator-takes-keys-on-every-runtime-and-the-app-switcher-and-lock.md) | An iOS simulator takes keys on every runtime, and the app switcher and Lock | Accepted; narrows 0049 §4 | diff --git a/docs/internal/flows/device-selection.md b/docs/internal/flows/device-selection.md index fbe5f24ec..4e1d91836 100644 --- a/docs/internal/flows/device-selection.md +++ b/docs/internal/flows/device-selection.md @@ -102,6 +102,10 @@ Android `provision`: iOS `provision`: `simctl create` with the cached device type and runtime. +## Through a gateway + +The gateway reads no catalog entry but the worker's own: a request whose mode is named, or a baseline's, goes only to a worker whose iOS entry does not list `modes` without it, and fails `MODE_UNAVAILABLE` when none does (ADR 0082 §4). + ## Physical branch 1. The coordinator: no enrolled, non-deleted device fits (`fitsPhysical`: platform, model diff --git a/src/cli/commands/catalog.ts b/src/cli/commands/catalog.ts index c6b6654c0..d34f9f1fd 100644 --- a/src/cli/commands/catalog.ts +++ b/src/cli/commands/catalog.ts @@ -41,7 +41,7 @@ function phonesOnlyLine(entry: CatalogGetOutput["platforms"][number]): string[] ]; } -// why: the daemon sends `modes` only when one mode is served, so a missing field means both. +// why: a missing `modes` field means the entry serves both modes (a worker serving both, or the gateway merge leaving it out); a present one lists the modes served, which can be both for a split fleet. function modesLine(entry: CatalogGetOutput["platforms"][number]): string[] { if (entry.platform !== "ios") return []; return [` Modes served: ${(entry.modes ?? ["slim", "full"]).join(", ")}`]; diff --git a/src/cli/commands/list.ts b/src/cli/commands/list.ts index ca983718c..a6772690c 100644 --- a/src/cli/commands/list.ts +++ b/src/cli/commands/list.ts @@ -73,6 +73,7 @@ const GATEWAY_REASON_TEXT: Readonly< "baseline-mismatch": () => "Baseline is for another device", "baseline-stale": () => "Baseline is stale", "no-baseline": () => "Does not hold the baseline", + "mode-unavailable": () => "Mode not served", "no-driver": (platform) => `No ${platform === "ios" ? "iOS" : "Android"} support`, "no-free-physical": () => WAITING_REASON_TEXT["no-free-physical"](""), "not-chosen": () => "Not chosen this time", diff --git a/src/contract/lease-schemas.ts b/src/contract/lease-schemas.ts index 1a26ecc24..9b1d0d3f6 100644 --- a/src/contract/lease-schemas.ts +++ b/src/contract/lease-schemas.ts @@ -118,6 +118,7 @@ const gatewayWaitingReasonSchema = z.enum([ "updating", "busy", "no-driver", + "mode-unavailable", "unknown-model", "runtime-missing", "no-baseline", diff --git a/src/contract/protocol.ts b/src/contract/protocol.ts index 49a94bf6b..8fa570a01 100644 --- a/src/contract/protocol.ts +++ b/src/contract/protocol.ts @@ -10,7 +10,7 @@ export interface ProtocolRange { const protocolRangeSchema = z.object({ min: z.number().int(), max: z.number().int() }); /** Widen only once a second version is kept alive side by side with the first; a bump that leaves no shim behind moves both ends. */ -export const PROTOCOL_VERSION_RANGE: ProtocolRange = { min: 89, max: 96 }; +export const PROTOCOL_VERSION_RANGE: ProtocolRange = { min: 89, max: 97 }; const LEGACY_DAEMON_PROTOCOL_VERSION = 2; diff --git a/src/gateway/aggregate-catalog.test.ts b/src/gateway/aggregate-catalog.test.ts index 94270c527..0ae44a261 100644 --- a/src/gateway/aggregate-catalog.test.ts +++ b/src/gateway/aggregate-catalog.test.ts @@ -110,6 +110,26 @@ describe("aggregateCatalog", () => { expect(() => OPERATIONS["catalog.get"].output.parse(catalog)).not.toThrow(); }); + it("carries the union of the workers' iOS modes, and none when any worker serves both", () => { + const serving = (modes?: ("slim" | "full")[]) => ({ + ...iosOnA, + ...(modes === undefined ? {} : { modes }), + }); + const merged = (...entries: ReturnType[]) => + aggregateCatalog( + entries.map((entry, index) => view({ catalog: [entry], id: `wrk_${index}` })), + ); + + const slimmers = merged(serving(["slim"]), serving(["slim"])); + const split = merged(serving(["slim"]), serving(["full"])); + const withBoth = merged(serving(["slim"]), serving()); + + expect(slimmers.platforms[0]?.modes).toEqual(["slim"]); + expect(split.platforms[0]?.modes).toEqual(["full", "slim"]); + expect(withBoth.platforms[0]).not.toHaveProperty("modes"); + expect(() => OPERATIONS["catalog.get"].output.parse(split)).not.toThrow(); + }); + it("carries the union of the connected workers' model classes, and keeps the class of the first worker in id order when two disagree", () => { const classed = (modelClasses: Record) => ({ ...iosOnB, diff --git a/src/gateway/aggregate.ts b/src/gateway/aggregate.ts index 5cdf74d9c..e2ebc0d97 100644 --- a/src/gateway/aggregate.ts +++ b/src/gateway/aggregate.ts @@ -221,6 +221,9 @@ interface CatalogBucket { /** Models any worker that lists them marks custom. */ readonly customModels: Set; readonly slimCategories: Set; + /** why: ADR 0082 §4: the modes the workers that list `modes` serve; `anyServesBoth` is set by the first worker with no field, and then the merged entry has none. */ + readonly modes: Set<"slim" | "full">; + anyServesBoth: boolean; /** why: ADR 0084 §3: stays true only while every worker reporting the platform says `virtualDevices: false`. */ phonesOnly: boolean; readonly defaults: Set; @@ -261,6 +264,8 @@ function addCatalogEntry( addCustomModels(bucket.customModels, entry); bucket.phonesOnly &&= entry.virtualDevices === false; for (const category of entry.slimCategories ?? []) bucket.slimCategories.add(category); + if (entry.modes === undefined) bucket.anyServesBoth = true; + for (const mode of entry.modes ?? []) bucket.modes.add(mode); if (entry.images !== undefined) bucket.images = addImages(bucket.images, entry, entry.images); bucket.defaults.add(entry.defaultRuntime); bucket.imageTagDefaults.add(entry.defaultImageTag); @@ -286,6 +291,8 @@ function emptyBucket(): CatalogBucket { modelRuntimes: new Map>(), models: new Map(), runtimes: new Map(), + anyServesBoth: false, + modes: new Set<"slim" | "full">(), phonesOnly: true, slimCategories: new Set(), }; @@ -421,6 +428,16 @@ function renderPlatform(platform: Platform, bucket: CatalogBucket): PlatformCata platform, runtimes, runtimeWorkers: Object.fromEntries(bucket.runtimes), + ...slimFields(bucket), + ...(bucket.phonesOnly ? { virtualDevices: false as const } : {}), + ...(agreedDefault === undefined ? {} : { defaultRuntime: agreedDefault }), + ...(agreedImageTag === undefined ? {} : { defaultImageTag: agreedImageTag }), + }; +} + +/** why: the iOS-only fields of the merged entry: the slim categories any worker names, and the modes, left out once any worker serves both. */ +function slimFields(bucket: CatalogBucket) { + return { ...(bucket.slimCategories.size === 0 ? {} : { @@ -428,9 +445,7 @@ function renderPlatform(platform: Platform, bucket: CatalogBucket): PlatformCata .sort() .slice(0, CATALOG_LIST_LIMITS.slimCategories), }), - ...(bucket.phonesOnly ? { virtualDevices: false as const } : {}), - ...(agreedDefault === undefined ? {} : { defaultRuntime: agreedDefault }), - ...(agreedImageTag === undefined ? {} : { defaultImageTag: agreedImageTag }), + ...(bucket.anyServesBoth ? {} : { modes: [...bucket.modes].sort() }), }; } diff --git a/src/gateway/fleet-dispatch.ts b/src/gateway/fleet-dispatch.ts index 6afff9466..b71d53776 100644 --- a/src/gateway/fleet-dispatch.ts +++ b/src/gateway/fleet-dispatch.ts @@ -62,6 +62,7 @@ const CANNOT_SERVE_CODES: ReadonlySet = new Set([ "UNKNOWN_BASELINE", "BASELINE_MISMATCH", "BASELINE_STALE", + "MODE_UNAVAILABLE", ]); export interface FleetDispatchOptions { diff --git a/src/gateway/fleet-refusals.ts b/src/gateway/fleet-refusals.ts index 4b4192006..6d21929cf 100644 --- a/src/gateway/fleet-refusals.ts +++ b/src/gateway/fleet-refusals.ts @@ -22,6 +22,7 @@ const CANNOT_SERVE_REASONS: Readonly> = { UNKNOWN_BASELINE: "no-baseline", BASELINE_MISMATCH: "baseline-mismatch", BASELINE_STALE: "baseline-stale", + MODE_UNAVAILABLE: "mode-unavailable", }; export interface FleetRefusalsOptions { diff --git a/src/gateway/fleet-requests-waiting-reasons.test.ts b/src/gateway/fleet-requests-waiting-reasons.test.ts index b320365b9..024a4f9e7 100644 --- a/src/gateway/fleet-requests-waiting-reasons.test.ts +++ b/src/gateway/fleet-requests-waiting-reasons.test.ts @@ -179,6 +179,7 @@ describe("FleetLeaseCoordinator lists why each worker cannot take a waiting requ ["RUNTIME_MISSING", "runtime-missing"], ["NO_DRIVER", "no-driver"], ["CAPABILITY_MISSING", "capability-missing"], + ["MODE_UNAVAILABLE", "mode-unavailable"], ] as const)("lists a worker that answered %s before progress as %s", async (code, reason) => { const { clients, coordinator } = fleet(["wrk_a", "wrk_b"], saturatedExcept("wrk_a")); clients["wrk_a"]?.requestLeaseQueue.push({ diff --git a/src/gateway/fleet-routing-modes.test.ts b/src/gateway/fleet-routing-modes.test.ts new file mode 100644 index 000000000..ad5db73d1 --- /dev/null +++ b/src/gateway/fleet-routing-modes.test.ts @@ -0,0 +1,191 @@ +import { describe, expect, it } from "vitest"; + +import { SimlockError } from "../contract/index.js"; +import { createRoutingPolicy } from "./routing.js"; +import { + catalogFixture, + grantFixture, + physicalDeviceFixture, + ScriptedWorkerClient, + statusFixture, +} from "./test-support.js"; +import { buildFleet, connectWorker, REQUEST, requestOptions, tick } from "./test-fleet.js"; + +type Modes = ("slim" | "full")[]; + +interface WorkerSpec { + /** why: absent is a worker that sends no `modes` field: it serves both. */ + readonly modes?: Modes; + readonly baseline?: "slim" | "full"; + readonly physical?: true; +} + +function fleet(specs: Readonly>) { + const made = buildFleet(); + const clients: Record = {}; + for (const [id, spec] of Object.entries(specs)) { + const client = new ScriptedWorkerClient(); + client.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" }); + clients[id] = client; + made.directory.add(id, client); + connectWorker(made.workers, id); + made.workers.refresh(id, { + capacity: statusFixture().capacity, + catalog: catalogFixture([ + { + models: ["iPhone 17"], + platform: "ios", + runtimes: ["26.0"], + ...(spec.modes === undefined ? {} : { modes: spec.modes }), + }, + ]).platforms, + ...(spec.physical === true ? { devices: [physicalDeviceFixture("dev_p", "ready")] } : {}), + ...(spec.baseline === undefined + ? {} + : { + baselines: [ + { + mode: spec.baseline, + model: "iPhone 17", + name: "b", + osVersion: "26.0", + platform: "ios" as const, + savedAt: "2026-11-02T13:10:00Z", + version: 1, + }, + ], + }), + }); + } + return { ...made, clients }; +} + +// why: the coordinator's tests, split by subject; they build the fleet from its test-support files, which index.ts does not export. +describe("FleetLeaseCoordinator routes an iOS lease by the modes each worker serves (ADR 0082 §4)", () => { + it("routes an iOS lease through a gateway by mode: a named or baseline mode goes only to a worker that serves it, and the lease fails at once when none does", async () => { + const cases: { + label: string; + request: { + baseline?: string; + mode?: "slim" | "full"; + physical?: boolean; + model?: string; + osVersion?: string; + }; + workers: Record; + routedTo?: string; + rejected?: string; + }[] = [ + { + label: "--mode full, one slim-only worker", + request: { mode: "full" }, + workers: { wrk_a: { modes: ["slim"] } }, + rejected: + 'No worker serves full iOS simulators; ask for a slim one (--mode slim, or mode: "slim")', + }, + { + label: "slim-only plus a worker serving both", + request: { mode: "full" }, + workers: { wrk_a: { modes: ["slim"] }, wrk_b: { modes: ["slim", "full"] } }, + routedTo: "wrk_b", + }, + { + label: "slim-only plus a worker with no modes field", + request: { mode: "full" }, + workers: { wrk_a: { modes: ["slim"] }, wrk_b: {} }, + routedTo: "wrk_b", + }, + { + label: "--mode slim, one full-only worker", + request: { mode: "slim" }, + workers: { wrk_a: { modes: ["full"] } }, + rejected: + 'No worker serves slim iOS simulators; ask for a full one (--mode full, or mode: "full")', + }, + { + label: "no mode, only a slim-only worker", + request: {}, + workers: { wrk_a: { modes: ["slim"] } }, + routedTo: "wrk_a", + }, + { + label: "a full baseline on a slim-only worker", + request: { baseline: "b" }, + workers: { wrk_a: { baseline: "full", modes: ["slim"] } }, + rejected: 'Baseline "b" is full, and no worker that holds it serves full iOS simulators', + }, + { + label: "--physical to a slim-only worker", + request: { mode: "full", model: "iPhone 15 Pro", osVersion: "18.4", physical: true }, + workers: { wrk_a: { modes: ["slim"], physical: true } }, + routedTo: "wrk_a", + }, + ]; + + for (const { label, request, workers: specs, routedTo, rejected } of cases) { + const { clients, coordinator } = fleet(specs); + const outcome = await Promise.race([ + coordinator.request({ ...REQUEST, ...request }, requestOptions()).then( + () => "granted", + (error: unknown) => error, + ), + tick().then(() => "waiting"), + ]); + const received = Object.entries(clients) + .filter(([, client]) => client.lastRequestLeaseInput !== undefined) + .map(([id]) => id); + if (rejected !== undefined) { + expect(outcome, label).toMatchObject({ + code: "MODE_UNAVAILABLE", + details: { mode: request.mode ?? "full", platform: "ios", served: [] }, + message: rejected, + }); + expect(received, label).toEqual([]); + } else { + expect(outcome, label).toBe("granted"); + expect(received, label).toEqual([routedTo]); + } + } + + const { workers } = fleet({ wrk_a: { modes: ["slim"] }, wrk_b: {} }); + expect( + createRoutingPolicy("warm-then-free").explain({ ...REQUEST, mode: "full" }, workers.views()), + ).toMatchObject([ + { filter: { reason: "mode-unavailable" }, workerId: "wrk_a" }, + { workerId: "wrk_b" }, + ]); + }); + + it("walks on to another worker when a worker answers MODE_UNAVAILABLE before a progress push, and gives the caller the worker's own refusal when none is left", async () => { + const refusal = new SimlockError( + "MODE_UNAVAILABLE", + "domain", + "This worker does not serve full iOS simulators", + { mode: "full", platform: "ios", served: ["slim"] }, + ); + + // why: both views are stale, with no `modes` field, so only the worker's own refusal says it serves slim alone. + const both = fleet({ wrk_a: {}, wrk_b: {} }); + both.clients["wrk_a"]?.requestLeaseQueue.splice(0, 1, { error: refusal, kind: "error" }); + const granted = await both.coordinator.request({ ...REQUEST, mode: "full" }, requestOptions()); + + expect(granted.lease.worker?.id).toBe("wrk_b"); + expect(both.directory.catalogRefreshCalls).toEqual(["wrk_a"]); + + const alone = fleet({ wrk_a: {} }); + alone.clients["wrk_a"]?.requestLeaseQueue.splice(0, 1, { error: refusal, kind: "error" }); + const outcome = await Promise.race([ + alone.coordinator.request({ ...REQUEST, mode: "full" }, requestOptions()).then( + () => "granted", + (error: unknown) => error, + ), + tick().then(() => "waiting"), + ]); + + expect(outcome).toMatchObject({ + code: "MODE_UNAVAILABLE", + details: { mode: "full", platform: "ios", served: ["slim"] }, + message: "This worker does not serve full iOS simulators", + }); + }); +}); diff --git a/src/gateway/routing/AGENTS.md b/src/gateway/routing/AGENTS.md index 2d3fdb033..ca5fbb5a3 100644 --- a/src/gateway/routing/AGENTS.md +++ b/src/gateway/routing/AGENTS.md @@ -23,6 +23,7 @@ Serviceability (`assess`): - The first check that applies wins, so the order of the checks is the answer a caller gets. - A request naming a baseline is checked first; the rest of the table then runs over the fitting, fresh holders only, with the request narrowed by `withBaseline`. +- A request whose mode is named, or a baseline's, is rejected `MODE_UNAVAILABLE` when no known worker with the platform serves it (`servesMode`, after the `NO_DRIVER` row); `can-serve` passes a worker that does not serve it over as `mode-unavailable`. A mode-less or physical request is never affected. - A known worker is one that is not `incompatible`. What no known worker could serve is rejected with its own code; what a known worker could serve but none taking requests can is `NO_CAPACITY`. An updating worker counts as taking requests here, so a request only it can serve waits. diff --git a/src/gateway/routing/pipeline.ts b/src/gateway/routing/pipeline.ts index f1004217d..da72da65d 100644 --- a/src/gateway/routing/pipeline.ts +++ b/src/gateway/routing/pipeline.ts @@ -31,6 +31,7 @@ export type WorkerReasonCode = | "updating" | "busy" | "no-driver" + | "mode-unavailable" | "unknown-model" | "runtime-missing" | "no-baseline" diff --git a/src/gateway/routing/request-match.ts b/src/gateway/routing/request-match.ts index 79e99d599..dcff5fb60 100644 --- a/src/gateway/routing/request-match.ts +++ b/src/gateway/routing/request-match.ts @@ -45,6 +45,18 @@ export function baselineMismatchOf(worker: WorkerView, request: RoutableRequest) return baselineMismatch(entry, request, catalog ?? { modelAliases: {}, models: [] }); } +/** + * why: ADR 0082 §4: whether the worker serves the iOS mode the request names or a baseline gives it + * (`withBaseline`). A request with no mode, a physical one, another platform, or a catalog without + * `modes` (a worker that serves both, or one from before the field) is served. + */ +export function servesMode(worker: WorkerView, request: RoutableRequest): boolean { + if (request.physical === true) return true; + const { mode } = withBaseline(worker, request); + const modes = catalogOf(worker, request)?.modes; + return mode === undefined || modes === undefined || modes.includes(mode); +} + /** * why: ADR 0061 §5: a request naming a baseline takes the baseline's model, exact OS, mode and * image tag, so it is read as that request on a worker that holds the baseline; the class is diff --git a/src/gateway/routing/serviceability.test.ts b/src/gateway/routing/serviceability.test.ts index b025c20f2..194e0479c 100644 --- a/src/gateway/routing/serviceability.test.ts +++ b/src/gateway/routing/serviceability.test.ts @@ -132,6 +132,19 @@ describe("assess", () => { ); }); + it("row 5: when only a worker that does not take requests serves the named mode, says NO_CAPACITY, never MODE_UNAVAILABLE", () => { + const slimOnly = view("wrk_a", { ...IOS, modes: ["slim"] }); + const fullAway = view("wrk_b", { ...IOS, modes: ["full"] }, { connection: "disconnected" }); + const fullDrained = view("wrk_c", { ...IOS, modes: ["full"] }, { drained: true }); + + for (const away of [fullAway, fullDrained]) { + expect(rejectionOf([slimOnly, away], { ...REQUEST, mode: "full" })).toMatchObject({ + code: "NO_CAPACITY", + reason: "no-worker", + }); + } + }); + it("row 6: a worker that takes requests and can serve it makes the request route-or-wait, busy or not", () => { const capacity = statusFixture().capacity; const busy = view("wrk_a", IOS, { diff --git a/src/gateway/routing/serviceability.ts b/src/gateway/routing/serviceability.ts index 446f887df..452a041da 100644 --- a/src/gateway/routing/serviceability.ts +++ b/src/gateway/routing/serviceability.ts @@ -15,6 +15,7 @@ import { listsModel, matchRequest, physicalMatch, + servesMode, wantedClass, withBaseline, } from "./request-match.js"; @@ -25,6 +26,7 @@ import { takesRequests } from "./stages/takes-requests.js"; export type RejectionCode = | "NO_CAPACITY" | "NO_DRIVER" + | "MODE_UNAVAILABLE" | "UNKNOWN_MODEL" | "RUNTIME_MISSING" | "CAPABILITY_MISSING" @@ -50,7 +52,8 @@ export interface Rejection { | { readonly capabilities: readonly string[] } | { readonly name: string } | { readonly baseline: string; readonly field: string } - | { readonly baseline: string; readonly reason: string }; + | { readonly baseline: string; readonly reason: string } + | { readonly platform: "ios"; readonly mode: "slim" | "full"; readonly served: readonly [] }; readonly reason: "no-worker" | "unresolvable-spec"; } @@ -212,15 +215,9 @@ function assessLeading( reason: "unresolvable-spec", }; } - if (request.physical === true) { - if (known.some((worker) => physicalMatch(worker, request, "enrolled"))) return undefined; - return { - ...noPhysicalMatch(request), - code: "UNKNOWN_MODEL", - kind: "reject", - reason: "unresolvable-spec", - }; - } + const mode = assessMode(request, known); + if (mode !== undefined) return mode; + if (request.physical === true) return assessPhysical(request, known); if (phonesOnlyEverywhere(known, request)) return phonesOnly(request); if (!known.some((worker) => listsModel(worker, request))) return unknownModel(request); if (!known.some((worker) => matchRequest(worker, request) !== undefined)) { @@ -236,6 +233,20 @@ function assessLeading( return undefined; } +// why: a physical request is served by any known worker with an enrolled device that fits, whatever the catalog lists. +function assessPhysical( + request: RoutableRequest, + known: readonly WorkerView[], +): Rejection | undefined { + if (known.some((worker) => physicalMatch(worker, request, "enrolled"))) return undefined; + return { + ...noPhysicalMatch(request), + code: "UNKNOWN_MODEL", + kind: "reject", + reason: "unresolvable-spec", + }; +} + // why: `presence` plays no part in the table: a device last read not present makes the request wait, not fail. // why: the capacity row: a known worker could serve, but none taking requests can. function assessServing( @@ -246,7 +257,9 @@ function assessServing( const fits = request.physical === true ? takers.some((worker) => physicalMatch(worker, request, "enrolled")) - : takers.some((worker) => matchRequest(worker, request) !== undefined); + : takers.some( + (worker) => servesMode(worker, request) && matchRequest(worker, request) !== undefined, + ); return fits ? undefined : noWorker(); } @@ -296,6 +309,32 @@ function phonesOnly(request: RoutableRequest): Rejection { }; } +// why: ADR 0082 §4, I6: the row after `NO_DRIVER`: every known worker with the platform lists modes without the request's. +function assessMode(request: RoutableRequest, known: readonly WorkerView[]): Rejection | undefined { + const withPlatform = known.filter((worker) => hasPlatform(worker, request)); + if (withPlatform.some((worker) => servesMode(worker, request))) return undefined; + return modeUnavailable(request, withPlatform); +} + +// why: ADR 0082 §4, I6: the mode is the request's own or, for a baseline, the first holder's; the message names the other mode as the way out. +function modeUnavailable(request: RoutableRequest, withPlatform: readonly WorkerView[]): Rejection { + const [first] = withPlatform; + const mode = first === undefined ? undefined : withBaseline(first, request).mode; + if (mode === undefined) return noWorker(); + const other = mode === "full" ? "slim" : "full"; + const message = + request.baseline === undefined + ? `No worker serves ${mode} iOS simulators; ask for a ${other} one (--mode ${other}, or mode: "${other}")` + : `Baseline "${request.baseline}" is ${mode}, and no worker that holds it serves ${mode} iOS simulators`; + return { + code: "MODE_UNAVAILABLE", + details: { mode, platform: "ios", served: [] }, + kind: "reject", + message, + reason: "unresolvable-spec", + }; +} + function noWorker(): Rejection { return { code: "NO_CAPACITY", diff --git a/src/gateway/routing/stages/can-serve.ts b/src/gateway/routing/stages/can-serve.ts index 700b01535..e9b4905d6 100644 --- a/src/gateway/routing/stages/can-serve.ts +++ b/src/gateway/routing/stages/can-serve.ts @@ -1,9 +1,18 @@ -import { hasPlatform, listsModel, matchRequest, physicalMatch } from "../request-match.js"; +import { + hasPlatform, + listsModel, + matchRequest, + physicalMatch, + servesMode, +} from "../request-match.js"; import type { FilterStage } from "../pipeline.js"; export const canServe: FilterStage = { check(worker, request) { const physical = request.physical === true; + if (hasPlatform(worker, request) && !servesMode(worker, request)) { + return { reason: "mode-unavailable" }; + } if (physical ? physicalMatch(worker, request, "enrolled") : matchRequest(worker, request)) { return undefined; } diff --git a/ui/src/model/waiting.test.ts b/ui/src/model/waiting.test.ts index 5d6377654..cc2471fda 100644 --- a/ui/src/model/waiting.test.ts +++ b/ui/src/model/waiting.test.ts @@ -285,7 +285,7 @@ describe("the waiting table on a gateway", () => { field as { unwrap(): { element: { shape: { reason: { options: readonly string[] } } } } } ).unwrap().element.shape.reason.options; - expect(listed).toHaveLength(18); + expect(listed).toHaveLength(19); for (const reason of listed) { const text = rowOf({ workers: [{ reason, workerId: "wrk_1" } as never] }).workers?.[0]?.text; expect(text).toMatch(/^[A-Z]/); diff --git a/ui/src/model/waiting.ts b/ui/src/model/waiting.ts index 88564a9d1..917a22ed0 100644 --- a/ui/src/model/waiting.ts +++ b/ui/src/model/waiting.ts @@ -93,6 +93,7 @@ const WORKER_WHY_TEXT: Readonly< "baseline-mismatch": () => "Baseline is for another device", "baseline-stale": () => "Baseline is stale", "no-baseline": () => "Does not hold the baseline", + "mode-unavailable": () => "Mode not served", "no-driver": (platform) => `No ${platform} support`, "no-free-physical": () => WHY_TEXT["no-free-physical"](""), "not-chosen": () => "Not chosen this time", diff --git a/website/src/content/docs/docs/gateway/fleet-console.mdx b/website/src/content/docs/docs/gateway/fleet-console.mdx index 6d6677e8e..2bbda53c7 100644 --- a/website/src/content/docs/docs/gateway/fleet-console.mdx +++ b/website/src/content/docs/docs/gateway/fleet-console.mdx @@ -76,6 +76,7 @@ On **Queue**, a request's **Queue** field says where it waits: `the gateway's qu | Model not offered | The worker does not offer the model or device class asked for. | | OS not installed | The worker offers the model, but has no matching OS version installed. | | No iOS support, No Android support | The worker does not support the platform. | +| Mode not served | The request names an iOS device mode, or a baseline has one, and the worker's `ios.modes` does not list it. | | Missing capability | The worker lacks an optional capability the request needs. | | Cannot keep these slim categories | The request names `except` categories the worker does not slim, or the worker would not give a slim device (its default mode is full and the request names no mode, or the baseline is full). | diff --git a/website/src/content/docs/docs/gateway/routing.mdx b/website/src/content/docs/docs/gateway/routing.mdx index 58b2aad53..bd58ac659 100644 --- a/website/src/content/docs/docs/gateway/routing.mdx +++ b/website/src/content/docs/docs/gateway/routing.mdx @@ -16,6 +16,7 @@ A worker can take a request only when it: - is connected, not drained, healthy, and has finished reporting its devices since it connected; - has every optional capability the request needs (for example, a lease through the agent-device integration needs a worker with that integration turned on); - offers the model or device class asked for, with a matching OS version already installed; +- serves the iOS device mode the request names, or its baseline has (a worker lists the modes it serves in `ios.modes`; one that lists only `slim` is passed over for a `full` request). A request that names no mode goes to any worker. `simlock --gateway catalog` lists the iOS modes served across all workers on its "Modes served" line; - holds a fresh copy of the baseline, when the request names one (see [Baselines through a gateway](/simlock/docs/gateway/managing-workers#baselines-through-a-gateway)); - slims every category in the request's `except` list and would give a slim device: the request's mode, or the baseline's, or else the worker's `ios.defaultMode` is `slim`; - has no requests waiting in its own queue, for example from agents on that machine. This doesn't apply to physical devices. @@ -90,6 +91,7 @@ The gateway doesn't queue a request that no worker could serve. It fails at once | --- | --- | --- | --- | --- | | `NO_CAPACITY` | 11 | 503 | Workers could serve it, but none is connected and taking requests: they are all disconnected, drained or incompatible. | Bring a worker back, or undrain one. | | `NO_DRIVER` | 12 | 422 | No worker supports the platform, for example no worker has Android tools. | Add a worker that has the platform. | +| `MODE_UNAVAILABLE` | 12 | 422 | An iOS request names a device mode (`--mode`, or a baseline's mode), and every worker that could serve it lists only the other mode in `ios.modes`. A request that names no mode is not affected. | Ask for the other mode, as the message says, or let a worker serve this one. See [Choosing a device](/simlock/docs/using-devices/choosing-a-device). | | `UNKNOWN_MODEL` | 12 | 422 | No worker offers that model or device class, or, for a physical device, no worker has one enrolled. Also when every worker with the platform serves only enrolled physical devices and the request didn't ask for one. | Check the name with `simlock --gateway catalog`. When the message names `.virtualDevices`, ask for a physical device with `--physical`. | | `RUNTIME_MISSING` | 12 | 422 | Workers offer the model, but none has a matching OS version installed. The gateway never downloads one. | Install it on the workers. See [Install runtimes and images on workers](/simlock/docs/gateway/managing-workers#install-runtimes-and-images-on-workers). | | `CAPABILITY_MISSING` | 12 | 422 | No worker that could serve the request has the optional capability it needs turned on. | Turn the capability on for a worker. |