From a697b84508edc54e1ee5959470a965db591cd886 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 14:28:59 +0200 Subject: [PATCH 1/3] feat(android,contract,gateway,cli): mark custom Android models in the catalog, and document where models come from --- docs/CLI.md | 34 ++++++- docs/CLIENT.md | 9 +- docs/HTTP-API.md | 13 ++- docs/internal/ARCHITECTURE.md | 9 +- e2e/fake-driver/fake-driver.ts | 1 + e2e/fake-driver/types.ts | 2 + e2e/gateway-fleet.test.ts | 56 ++++++++++++ src/cli/index.test.ts | 36 ++++++++ src/cli/index.ts | 4 +- src/contract/operations.test.ts | 4 +- src/contract/schemas.ts | 10 +++ src/core/driver.ts | 5 ++ src/core/fake-driver.test.ts | 14 +++ src/core/fake-driver.ts | 5 ++ .../android/device-profile-source.test.ts | 89 +++++++++++++++++++ src/drivers/android/device-profile-source.ts | 13 ++- src/drivers/android/index.test.ts | 41 +++++++++ src/drivers/android/index.ts | 3 +- src/drivers/ios/index.test.ts | 9 ++ src/gateway/aggregate.test.ts | 39 ++++++++ src/gateway/aggregate.ts | 19 +++- 21 files changed, 403 insertions(+), 12 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 90b0ee0d9..36b0b25d1 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -1083,6 +1083,11 @@ Android also lists `images`: every installed system image with its API level (`runtime`), tag, and ABI. An image whose ABI the host cannot run natively is listed too, with its ABI. +`customModels` lists the models that exist because of something on that +machine rather than the platform's tools. The field is absent when there are +none, and iOS never has it. See [Where Android models come +from](#where-android-models-come-from). + A platform whose SDK is missing (e.g. Android without `ANDROID_HOME` on a non-macOS host, or iOS off macOS) is omitted rather than erroring the whole @@ -1097,7 +1102,7 @@ with, its other names on the line below it, and then each image: Platform: android Runtimes: 34, 35 (default: 35) Models: - My Tablet: 34, 35 + My Tablet (custom): 34, 35 Pixel 8: 34, 35 Other names: pixel_8 Images (runtime, tag, ABI): @@ -1111,14 +1116,35 @@ Platform: android {"platforms":[{"platform":"android","models":["My Tablet","Pixel 8"],"runtimes":["34","35"],"defaultRuntime":"35", "modelRuntimes":{"My Tablet":["34","35"],"Pixel 8":["34","35"]}, "modelAliases":{"Pixel 8":["pixel_8"]}, + "customModels":["My Tablet"], "images":[{"runtime":"34","tag":"default","abi":"x86_64"},{"runtime":"35","tag":"google_apis","abi":"arm64-v8a"}]}]} ``` Against a gateway, `modelAliases` is the union of each worker's other names -for a model, and `images` the union of their images. The gateway does not +for a model, and `images` the union of their images. A model is in +`customModels` when any worker that lists it marks it custom; +`simlock worker list --json` shows which worker that is. The gateway does not yet route by another name, so ask a gateway for a model by its name in `models`. +### Where Android models come from + +Simlock reads Android device models from two places and writes to neither: + +- **Built-in profiles** ship with the Android SDK's `cmdline-tools`: the + ones `avdmanager list device` prints. A model newer than your + `cmdline-tools` is not there; update `cmdline-tools` to get it. +- **Custom profiles** are the ones you make with Android Studio's device + manager. Android Studio keeps them in `devices.xml` in your `.android` + directory. A custom profile exists only on the machine where it was made, + so in a fleet only the worker that has it can lease it. To add one, create + it with Android Studio's device manager on that machine. + +When a name is in both, the built-in profile wins and the model is not +custom. If `devices.xml` cannot be read, the built-in models are still +listed. A custom model is marked `(custom)` in the human view of `simlock +catalog` and listed in `customModels` in `--json`. + ## `simlock component install ` Installs one iOS simulator runtime or Android system image, without leasing @@ -1370,7 +1396,9 @@ operation **on a gateway**; against a worker they answer `UNKNOWN_REQUEST` versions. Under each worker, one indented line per component install waiting or running there, in the form `simlock status` prints. A worker the gateway cannot speak to shows `incompatible` with both protocol ranges, which is what version skew looks like from here. `--json` - prints the raw worker views, which is what the console renders. + prints the raw worker views, which is what the console renders. Each + view's catalog carries that worker's own `customModels`, so this is where + you see which worker has a custom Android profile. - `drain ` / `undrain ` — a drained worker keeps its existing leases and receives no new dispatches: the way to take a machine out of service without killing anyone's device. The flag is persisted by the diff --git a/docs/CLIENT.md b/docs/CLIENT.md index e1a5a8cc8..6eab98492 100644 --- a/docs/CLIENT.md +++ b/docs/CLIENT.md @@ -209,6 +209,11 @@ const { platforms } = await client.getCatalog({ platform: "ios" }); - `images` is on Android entries only: every installed system image as `{ runtime, tag, abi }`, where `runtime` is a value from `runtimes`. An image whose ABI the host cannot run natively is listed too. +- `customModels` lists the models that exist because of something on that + machine rather than the platform's tools: on Android, a profile made with + Android Studio's device manager and read from `devices.xml`. A built-in + profile with the same name wins, and that model is not custom. The field + is absent when there are none, and iOS never has it. - `defaultRuntime` is the newest installed runtime, and is absent when none is installed. - The catalog lists only what is installed. It never lists a runtime the @@ -219,7 +224,9 @@ model is paired with a runtime when at least one worker pairs them, and `modelWorkers` and `runtimeWorkers` say which workers have each model and runtime. The gateway sends a request only to a worker that pairs the model with the runtime. `modelAliases` and `images` are the unions of each -worker's own. A model may be asked for by any name a worker lists for it, in +worker's own. A model is in `customModels` when any worker that lists it +marks it custom; each worker's own list is in its catalog on +`listWorkers()` from the admin client. A model may be asked for by any name a worker lists for it, in any letter case, and the gateway sends that worker its own name for it. `allowDownload` has no effect through a gateway: only installed runtimes count. diff --git a/docs/HTTP-API.md b/docs/HTTP-API.md index 78ec00800..28da9f682 100644 --- a/docs/HTTP-API.md +++ b/docs/HTTP-API.md @@ -240,6 +240,15 @@ for that platform has an `images` field. A gateway accepts any name a worker lists for a model, in any letter case, and sends that worker its own name for it. +An entry may also carry `customModels`: the names from `models` that exist +because of something on that machine rather than the platform's tools. On +Android that is a profile made with Android Studio's device manager, read +from `devices.xml`; when a built-in profile has the same name, the built-in +one wins and the model is not custom. The field is absent when there are +none, and iOS never has it. On a gateway a model is listed when any worker +that lists it marks it custom; each worker's own list is in its catalog in +[`GET /v1/workers`](#worker-routes). + ```json { "platforms": [ { "platform": "ios", @@ -255,6 +264,7 @@ it. "defaultRuntime": "35", "modelRuntimes": { "My Tablet": ["34", "35"], "Pixel 8": ["34", "35"] }, "modelAliases": { "Pixel 8": ["pixel_8"] }, + "customModels": ["My Tablet"], "images": [ { "runtime": "34", "tag": "default", "abi": "x86_64" }, { "runtime": "35", "tag": "google_apis", "abi": "arm64-v8a" } ] } ] } @@ -827,7 +837,8 @@ none. `catalog` is what that worker can lease, each model with the runtimes it pairs with, and lists a newly installed component as soon as its install -ends. `host` is the worker's machine, the same block its own +ends. Its `customModels` are that worker's own, so this is where you see +which worker has a custom Android profile. `host` is the worker's machine, the same block its own `GET /v1/status` reports, as of the gateway's last refresh: a tool installed or upgraded on the worker shows here without a restart of either side. diff --git a/docs/internal/ARCHITECTURE.md b/docs/internal/ARCHITECTURE.md index 05198fb25..85b5f0b60 100644 --- a/docs/internal/ARCHITECTURE.md +++ b/docs/internal/ARCHITECTURE.md @@ -726,7 +726,9 @@ union of what each connected worker pairs it with, never the cross product of fleet models and fleet runtimes: one worker with the model and another with the runtime is not a leasable pair. `modelAliases` is the union per model, deduplicated ignoring case, and `images` the union by runtime, tag, -and ABI, absent when no worker reports the field. Each worker's lists are +and ABI, absent when no worker reports the field. `customModels` lists a +model when any worker that lists it marks it custom; a name a worker marks +but does not list is dropped. Each worker's lists are within the contract's bounds but their union may not be, so the gateway cuts the sorted union to those bounds rather than answer with a catalog its own clients would refuse. Routing still reads only @@ -741,7 +743,10 @@ resolves. The same holds for names: the Android driver's `DeviceProfileRegistry` owns the only matcher (first profile, in source order, any of whose names equals the request ignoring case), and the catalog's `modelAliases` are a profile's other names that the matcher sends -back to that profile. The iOS driver matches a device type's name only. +back to that profile. A listed model is in `customModels` when the profile +the matcher sends its name to is a `devices.xml` one, so the mark and the +resolution cannot disagree. The iOS driver matches a device type's name +only. Worker business events are republished on the gateway's bus with `workerId` added to the payload and land in the gateway's own ring buffer and event diff --git a/e2e/fake-driver/fake-driver.ts b/e2e/fake-driver/fake-driver.ts index 48c86473b..d27193d9d 100644 --- a/e2e/fake-driver/fake-driver.ts +++ b/e2e/fake-driver/fake-driver.ts @@ -395,6 +395,7 @@ export class OutOfProcessFakeDriver implements Driver { return { defaultRuntime: newestVersion(runtimes), ...(script.images === undefined ? {} : { images: [...script.images] }), + ...(script.customModels === undefined ? {} : { customModels: [...script.customModels] }), modelAliases: { ...script.modelAliases }, modelRuntimes: Object.fromEntries( models.map((model) => [model, [...(script.modelRuntimes?.[model] ?? runtimes)]]), diff --git a/e2e/fake-driver/types.ts b/e2e/fake-driver/types.ts index 0d683a44f..9ccdb5d17 100644 --- a/e2e/fake-driver/types.ts +++ b/e2e/fake-driver/types.ts @@ -76,6 +76,8 @@ export interface FakeDriverPlatformScript { readonly modelRuntimes?: Readonly>; /** What `listCatalog` reports as other names per model; none unless set. */ readonly modelAliases?: Readonly>; + /** What `listCatalog` reports as custom models; the field is absent unless set. */ + readonly customModels?: readonly string[]; /** What `listCatalog` reports as installed images; the field is absent unless set. */ readonly images?: readonly { readonly runtime: string; diff --git a/e2e/gateway-fleet.test.ts b/e2e/gateway-fleet.test.ts index c510913e4..dc1c8e658 100644 --- a/e2e/gateway-fleet.test.ts +++ b/e2e/gateway-fleet.test.ts @@ -24,6 +24,7 @@ interface WorkerView { readonly models: readonly string[]; readonly modelRuntimes: Readonly>; readonly modelAliases: Readonly>; + readonly customModels?: readonly string[]; readonly images?: readonly { readonly runtime: string; readonly tag: string; @@ -374,6 +375,61 @@ describe("gateway fleet", () => { ]); }); + it("shows each worker's own custom models in the worker list, and marks a model custom in the catalog when any worker does", async () => { + const port = await freeLoopbackPort(); + const gateway = await withDaemon({ + configOverrides: { http: { host: "127.0.0.1", port }, mode: "gateway" }, + driver: "none", + }); + const minted = await gateway.cli(["token", "create", "--role", "worker"]); + const { secret } = minted.json as { secret: string }; + const uplink = { token: secret, url: `ws://127.0.0.1:${port}` }; + // Both workers list My Tablet; only worker-a has it as a custom profile. + await withDaemon({ + configOverrides: { gateway: { ...uplink, label: "worker-a" } }, + driverScript: { + android: { + availableOsVersions: ["35"], + customModels: ["My Tablet"], + knownModels: ["Pixel 8", "My Tablet"], + }, + }, + }); + await withDaemon({ + configOverrides: { gateway: { ...uplink, label: "worker-b" } }, + driverScript: { + android: { availableOsVersions: ["35"], knownModels: ["Pixel 8", "My Tablet"] }, + }, + }); + + const workers = await waitForWorkers( + gateway, + (views) => + views.length === 2 && + views.every((view) => view.connection === "connected" && view.catalog.length > 0), + "both workers connected with their catalogs", + ); + const androidOf = (label: string) => + workers + .find((view) => view.label === label) + ?.catalog.find((entry) => entry.platform === "android"); + expect(androidOf("worker-a")?.customModels).toEqual(["My Tablet"]); + expect(androidOf("worker-b")?.models).toEqual(["Pixel 8", "My Tablet"]); + expect(androidOf("worker-b")).not.toHaveProperty("customModels"); + + const catalog = await gateway.cli(["catalog", "--json"]); + expect(catalog.code).toBe(0); + const android = (catalog.json as { platforms: WorkerView["catalog"] }).platforms.find( + (entry) => entry.platform === "android", + ); + expect(android?.customModels).toEqual(["My Tablet"]); + + const human = await gateway.cli(["catalog"]); + expect(human.code).toBe(0); + expect(human.stdout).toContain(" My Tablet (custom): 35"); + expect(human.stdout).toContain(" Pixel 8: 35"); + }); + it("gives a request with a mode that mode on either worker, and a request with none the default of the worker it landed on", async () => { const port = await freeLoopbackPort(); const gateway = await withDaemon({ diff --git a/src/cli/index.test.ts b/src/cli/index.test.ts index 866f8a7bb..a493b553a 100644 --- a/src/cli/index.test.ts +++ b/src/cli/index.test.ts @@ -1791,6 +1791,42 @@ describe("CLI: catalog", () => { ].join("\n"), ); }); + + it("prints (custom) after a custom model and not after a built-in one", async () => { + const output = outputCapture(); + const environment = output.environmentWith({ + connectAdmin: async () => + fakeClient({ + getCatalog: () => + Promise.resolve({ + platforms: [ + { + customModels: ["My Tablet"], + defaultRuntime: "35", + modelAliases: {}, + modelRuntimes: { "My Tablet": ["35"], "Pixel 8": ["35"] }, + models: ["My Tablet", "Pixel 8"], + platform: "android", + runtimes: ["35"], + }, + ], + }), + }), + }); + + await expect(runCli(["catalog"], environment)).resolves.toBe(0); + + expect(output.stdout).toBe( + [ + "Platform: android", + " Runtimes: 35 (default: 35)", + " Models:", + " My Tablet (custom): 35", + " Pixel 8: 35", + "", + ].join("\n"), + ); + }); }); describe("CLI: component install (ADR 0010 §6)", () => { diff --git a/src/cli/index.ts b/src/cli/index.ts index 3278a44d9..f8889c15d 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -2146,6 +2146,8 @@ function formatCatalog(response: CatalogGetOutput): string { // Each model with the runtimes it pairs with: a model and a runtime both listed for the // platform are not necessarily leasable together. // A model's other names follow it on a line of their own. + // A model that exists only because of something on that machine is marked `(custom)`. + const custom = new Set(entry.customModels ?? []); const models = entry.models.flatMap((model) => { const paired = (Object.hasOwn(entry.modelRuntimes, model) ? entry.modelRuntimes[model] : undefined) ?? @@ -2153,7 +2155,7 @@ function formatCatalog(response: CatalogGetOutput): string { const aliases = (Object.hasOwn(entry.modelAliases, model) ? entry.modelAliases[model] : undefined) ?? []; return [ - ` ${model}: ${paired.length > 0 ? paired.join(", ") : "(no paired runtime)"}`, + ` ${model}${custom.has(model) ? " (custom)" : ""}: ${paired.length > 0 ? paired.join(", ") : "(no paired runtime)"}`, ...(aliases.length > 0 ? [` Other names: ${aliases.join(", ")}`] : []), ]; }); diff --git a/src/contract/operations.test.ts b/src/contract/operations.test.ts index 3a4a4ccb4..a2b193bff 100644 --- a/src/contract/operations.test.ts +++ b/src/contract/operations.test.ts @@ -517,7 +517,7 @@ describe("operation input/output round trips", () => { ]); }); - it("catalog.get bounds every string and list in modelAliases and images", () => { + it("catalog.get bounds every string and list in modelAliases, customModels and images", () => { const base = { platform: "android", models: ["Pixel 8"], @@ -541,6 +541,8 @@ describe("operation input/output round trips", () => { ], [{ modelAliases: { "Pixel 8": names(32) } }, { modelAliases: { "Pixel 8": names(33) } }], [{ modelAliases: aliasedModels(4096) }, { modelAliases: aliasedModels(4097) }], + [{ customModels: ["x".repeat(256)] }, { customModels: ["x".repeat(257)] }], + [{ customModels: names(4096) }, { customModels: names(4097) }], [ { images: [{ ...image, runtime: "x".repeat(128) }] }, { images: [{ ...image, runtime: "x".repeat(129) }] }, diff --git a/src/contract/schemas.ts b/src/contract/schemas.ts index e6b874eda..e035b78b5 100644 --- a/src/contract/schemas.ts +++ b/src/contract/schemas.ts @@ -300,6 +300,8 @@ const CATALOG_ALIASES_PER_MODEL_MAX = 32; const CATALOG_ALIASED_MODELS_MAX = 4096; const CATALOG_IMAGE_FIELD_MAX = 128; const CATALOG_IMAGES_MAX = 1024; +/** `customModels` is a subset of `models`; it takes the bound already on a per-model list. */ +const CATALOG_CUSTOM_MODELS_MAX = CATALOG_ALIASED_MODELS_MAX; /** * The list bounds above, for a producer that merges lists the schema checked one by one: a @@ -308,6 +310,7 @@ const CATALOG_IMAGES_MAX = 1024; export const CATALOG_LIST_LIMITS = { aliasedModels: CATALOG_ALIASED_MODELS_MAX, aliasesPerModel: CATALOG_ALIASES_PER_MODEL_MAX, + customModels: CATALOG_CUSTOM_MODELS_MAX, images: CATALOG_IMAGES_MAX, } as const; @@ -403,6 +406,13 @@ export const platformCatalogSchema = z.object({ * natively is listed too. On a gateway it is the union by runtime, tag, and ABI. */ images: z.array(catalogImageSchema).max(CATALOG_IMAGES_MAX).optional(), + /** + * Names from `models` that exist because of something on this machine rather than the + * platform's tools -- on Android, a custom profile from Android Studio's `devices.xml`. + * Absent when there are none. On a gateway a model is listed when any worker that lists it + * marks it custom. + */ + customModels: z.array(z.string().max(CATALOG_NAME_MAX)).max(CATALOG_CUSTOM_MODELS_MAX).optional(), /** * ADR 0005 §21: on a gateway, `models`/`runtimes` are the *union* over the fleet, and these * two maps say which workers each entry came from (`{"iPhone 16": ["wrk_a", "wrk_b"]}`). diff --git a/src/core/driver.ts b/src/core/driver.ts index 1070c3b2b..ebac4bdde 100644 --- a/src/core/driver.ts +++ b/src/core/driver.ts @@ -128,6 +128,10 @@ export type DriverEstimate = * `modelAliases` maps a name in `models` to the other names `resolveSpec` accepts for it, from * the same matcher `resolveSpec` uses; a model with no other name has no entry. `images` is * present only for a driver whose runtimes come as installed images, one entry per image. + * + * `customModels` names the values from `models` that exist because of something on this + * machine, not because of the platform's tools. The core carries it and never reads it; a + * driver with no such models omits the field. */ export interface DriverCatalogEntry { readonly models: readonly string[]; @@ -136,6 +140,7 @@ export interface DriverCatalogEntry { readonly modelRuntimes: Readonly>; readonly modelAliases: Readonly>; readonly images?: readonly DriverCatalogImage[]; + readonly customModels?: readonly string[]; } /** diff --git a/src/core/fake-driver.test.ts b/src/core/fake-driver.test.ts index 9ae1e180c..f96049531 100644 --- a/src/core/fake-driver.test.ts +++ b/src/core/fake-driver.test.ts @@ -261,6 +261,20 @@ describe("FakeDriver", () => { }); expect(await new FakeDriver(options).listCatalog()).not.toHaveProperty("images"); }); + + it("reports the custom models its options name, and no customModels field without them", async () => { + const options = { + availableOsVersions: ["35"], + clock: new FakeClock(), + knownModels: ["Pixel 8", "My Tablet"], + platform: "android" as const, + }; + + await expect( + new FakeDriver({ ...options, customModels: ["My Tablet"] }).listCatalog(), + ).resolves.toMatchObject({ customModels: ["My Tablet"] }); + expect(await new FakeDriver(options).listCatalog()).not.toHaveProperty("customModels"); + }); }); const prepare = { mode: "full", purpose: "prepare" } as const; diff --git a/src/core/fake-driver.ts b/src/core/fake-driver.ts index a8a8400f2..aefdb7270 100644 --- a/src/core/fake-driver.ts +++ b/src/core/fake-driver.ts @@ -82,6 +82,8 @@ export interface FakeDriverOptions { readonly modelAliases?: Readonly>; /** What `listCatalog` reports as installed images; the field is absent unless set. */ readonly images?: readonly DriverCatalogImage[]; + /** What `listCatalog` reports as custom models; the field is absent unless set. */ + readonly customModels?: readonly string[]; readonly latencyMs?: Partial>; /** What a grant for this driver's devices should carry; empty unless a test says otherwise. */ readonly leaseEnvironment?: Readonly>; @@ -140,6 +142,7 @@ export class FakeDriver implements Driver { readonly #modelRuntimes: FakeDriverOptions["modelRuntimes"]; readonly #modelAliases: FakeDriverOptions["modelAliases"]; readonly #images: FakeDriverOptions["images"]; + readonly #customModels: FakeDriverOptions["customModels"]; readonly #latencyMs: FakeDriverOptions["latencyMs"]; readonly #leaseEnvironment: Readonly>; readonly passthroughTool: string | undefined; @@ -168,6 +171,7 @@ export class FakeDriver implements Driver { this.#modelRuntimes = options.modelRuntimes; this.#modelAliases = options.modelAliases; this.#images = options.images; + this.#customModels = options.customModels; this.#latencyMs = options.latencyMs; this.#leaseEnvironment = options.leaseEnvironment ?? {}; this.#legacyDevices = new Map(Object.entries(options.legacyDevices ?? {})); @@ -402,6 +406,7 @@ export class FakeDriver implements Driver { return { defaultRuntime: newestVersion(this.#availableOsVersions), ...(this.#images === undefined ? {} : { images: [...this.#images] }), + ...(this.#customModels === undefined ? {} : { customModels: [...this.#customModels] }), modelAliases: { ...this.#modelAliases }, modelRuntimes: Object.fromEntries( models.map((model) => [model, [...(this.#modelRuntimes?.[model] ?? runtimes)]]), diff --git a/src/drivers/android/device-profile-source.test.ts b/src/drivers/android/device-profile-source.test.ts index 05905f647..5fb21a31c 100644 --- a/src/drivers/android/device-profile-source.test.ts +++ b/src/drivers/android/device-profile-source.test.ts @@ -302,6 +302,7 @@ describe("DeviceProfileRegistry", () => { const registry = new DeviceProfileRegistry([builtin, user]); await expect(registry.catalog()).resolves.toEqual({ + customModels: ["My Custom Phone"], modelAliases: { "Pixel 8": ["pixel_8"] }, models: ["Pixel 8", "My Custom Phone"], }); @@ -321,10 +322,98 @@ describe("DeviceProfileRegistry", () => { ]); await expect(registry.catalog()).resolves.toEqual({ + customModels: [], modelAliases: { "Pixel 8": ["pixel_8"] }, models: ["Pixel 8", "Pixel 8 Copy"], }); }); + + it("lists a model that comes only from devices.xml in customModels", async () => { + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices)]), + ), + new UserDeviceProfileSource(devicesXmlPath, await filesystemWithDevicesXml(devicesXml())), + ]); + + await expect(registry.catalog()).resolves.toMatchObject({ + customModels: ["My Custom Phone"], + }); + }); + + it("does not list a built-in model in customModels", async () => { + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices)]), + ), + new UserDeviceProfileSource(devicesXmlPath, await filesystemWithDevicesXml(devicesXml())), + ]); + + const catalog = await registry.catalog(); + expect(catalog.models).toContain("Pixel 8"); + expect(catalog.customModels).not.toContain("Pixel 8"); + }); + + it("lists a name in both sources once, and not in customModels", async () => { + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices)]), + ), + new UserDeviceProfileSource( + devicesXmlPath, + await filesystemWithDevicesXml(devicesXml().replace("My Custom Phone", "PIXEL 8")), + ), + ]); + + await expect(registry.catalog()).resolves.toMatchObject({ + customModels: [], + models: ["Pixel 8"], + }); + }); + + it("yields the built-in models and no customModels when devices.xml is unreadable", async () => { + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices)]), + ), + new UserDeviceProfileSource( + devicesXmlPath, + await filesystemWithDevicesXml("not even close to xml {{{"), + ), + ]); + + await expect(registry.catalog()).resolves.toMatchObject({ + customModels: [], + models: ["Pixel 8"], + }); + }); + + it("resolves every model in customModels to a profile of kind properties", async () => { + // `pixel_8` is listed, since no earlier profile is named that, but the built-in Pixel 8 + // answers to it, so it resolves to the built-in and must not be marked custom. + const twoDevices = devicesXml().replace( + "", + "pixel_8", + ); + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices), processResult(pixelDevices)]), + ), + new UserDeviceProfileSource(devicesXmlPath, await filesystemWithDevicesXml(twoDevices)), + ]); + + const catalog = await registry.catalog(); + expect(catalog.models).toEqual(["Pixel 8", "My Custom Phone", "pixel_8"]); + expect(catalog.customModels).toEqual(["My Custom Phone"]); + for (const model of catalog.customModels) { + await expect(registry.resolve(model)).resolves.toMatchObject({ kind: "properties" }); + } + }); }); function devicesXml(): string { diff --git a/src/drivers/android/device-profile-source.ts b/src/drivers/android/device-profile-source.ts index 5927ba3d7..5d0773994 100644 --- a/src/drivers/android/device-profile-source.ts +++ b/src/drivers/android/device-profile-source.ts @@ -54,6 +54,11 @@ export interface DeviceProfileCatalog { readonly models: readonly string[]; /** Only models with another name appear, keyed by their spelling in `models`. */ readonly modelAliases: Readonly>; + /** + * The listed models whose name resolves to a `properties` profile: a custom profile from + * this machine rather than one the SDK ships. In `models` order. + */ + readonly customModels: readonly string[]; } /** @@ -94,7 +99,13 @@ export class DeviceProfileRegistry { modelAliases[profile.name] = others; } } - return { modelAliases, models: [...listed.values()].map((profile) => profile.name) }; + const models = [...listed.values()].map((profile) => profile.name); + // Custom by the profile the name resolves to, not the one it was listed from: a + // devices.xml name that a built-in already answers to stays listed but is built-in. + const customModels = models.filter( + (model) => matchProfile(profiles, model)?.kind === "properties", + ); + return { customModels, modelAliases, models }; } /** diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 7da2fedb6..8e4265ac5 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -1074,6 +1074,47 @@ describe("AndroidDriver", () => { expect(Object.keys(catalog.modelRuntimes)).toEqual(catalog.models); }); + it("marks a model that comes only from devices.xml as custom, and no built-in model", async () => { + const filesystem = await androidFilesystem(); + await writeDevicesXml(filesystem, customDeviceXml("My Tablet", 4096)); + const runner = new ScriptedProcessRunner([ + processResult(binaries.avdmanager, ["list", "device"], twoPixelDevices), + ]); + const driver = await createDriver(filesystem, runner); + + const catalog = await driver.listCatalog(); + + expect(catalog.models).toEqual(["Pixel 8", "Pixel 9", "My Tablet"]); + expect(catalog.customModels).toEqual(["My Tablet"]); + }); + + it("has no customModels field when there is no custom profile", async () => { + const runner = new ScriptedProcessRunner([ + processResult(binaries.avdmanager, ["list", "device"], twoPixelDevices), + ]); + const driver = await createDriver(await androidFilesystem(), runner); + + const catalog = await driver.listCatalog(); + + expect(catalog.models).toEqual(["Pixel 8", "Pixel 9"]); + expect(catalog).not.toHaveProperty("customModels"); + }); + + it("lists the built-in models and no customModels field when devices.xml is unreadable", async () => { + const filesystem = await androidFilesystem(); + await filesystem.mkdirp(`${home}/.android`); + await filesystem.writeFileAtomic(`${home}/.android/devices.xml`, "not xml at all {{{"); + const runner = new ScriptedProcessRunner([ + processResult(binaries.avdmanager, ["list", "device"], twoPixelDevices), + ]); + const driver = await createDriver(filesystem, runner); + + const catalog = await driver.listCatalog(); + + expect(catalog.models).toEqual(["Pixel 8", "Pixel 9"]); + expect(catalog).not.toHaveProperty("customModels"); + }); + it("lists models with one avdmanager run", async () => { // A second answer is scripted, so a second run would be served and counted, not refused. const runner = new ScriptedProcessRunner([ diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index ee65e09f5..f4dfb3917 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -1104,12 +1104,13 @@ export class AndroidDriver implements Driver { } async listCatalog(): Promise { - const [{ modelAliases, models }, images] = await Promise.all([ + const [{ customModels, modelAliases, models }, images] = await Promise.all([ this.#deviceProfiles.catalog(), this.#installedImages(), ]); const runtimes = installedApiLevels(images); return { + ...(customModels.length === 0 ? {} : { customModels: [...customModels] }), defaultRuntime: runtimes.at(-1), // Installed only, a foreign ABI included: the same images `installedApiLevels` reads. images: images diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index f1b1a094f..d5721b8b9 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -1028,6 +1028,15 @@ describe("IosSimctlDriver", () => { expect(catalog).not.toHaveProperty("images"); }); + it("has no customModels field", async () => { + const driver = await createDriver(scriptedListRunner()); + + const catalog = await driver.listCatalog(); + + expect(catalog.models.length).toBeGreaterThan(0); + expect(catalog).not.toHaveProperty("customModels"); + }); + describe("model and runtime pairing", () => { function pairingRunner(calls = 1): ScriptedProcessRunner { return new ScriptedProcessRunner( diff --git a/src/gateway/aggregate.test.ts b/src/gateway/aggregate.test.ts index 8e08a5161..92d38ffa3 100644 --- a/src/gateway/aggregate.test.ts +++ b/src/gateway/aggregate.test.ts @@ -526,11 +526,13 @@ describe("aggregateCatalog", () => { modelAliases: Record; images: { runtime: string; tag: string; abi: string }[]; runtimes: string[]; + customModels: string[]; }>, ) => { const models = overrides.models ?? ["Pixel 8"]; const runtimes = overrides.runtimes ?? ["34", "35"]; return { + ...(overrides.customModels === undefined ? {} : { customModels: overrides.customModels }), ...(overrides.images === undefined ? {} : { images: overrides.images }), modelAliases: overrides.modelAliases ?? {}, modelRuntimes: Object.fromEntries(models.map((model) => [model, runtimes])), @@ -651,6 +653,7 @@ describe("aggregateCatalog", () => { ...Object.fromEntries(names(`${prefix}m`, 4095).map((model) => [model, ["x"]])), "Pixel 8": names(prefix, 32), }, + customModels: ["Pixel 8", ...names(`${prefix}m`, 4095)], models: ["Pixel 8", ...names(`${prefix}m`, 4095)], }); const valid = [worker("a"), worker("b")]; @@ -672,6 +675,42 @@ describe("aggregateCatalog", () => { expect(aliasedModels).toHaveLength(4096); expect(aliasedModels.filter((model) => model.startsWith("bm"))).toEqual([]); expect(platform?.modelAliases["Pixel 8"]).toEqual(names("a", 32).sort()); + expect(platform?.customModels).toHaveLength(4096); + expect(platform?.customModels?.filter((model) => model.startsWith("bm"))).toEqual([]); + }); + + it("marks a model custom when one worker marks it and another lists it as built-in", () => { + const catalog = aggregateCatalog([ + view({ catalog: [androidOn({ models: ["Pixel 8", "My Tablet"] })], id: "wrk_a" }), + view({ + catalog: [androidOn({ customModels: ["My Tablet"], models: ["My Tablet"] })], + id: "wrk_b", + }), + ]); + + expect(() => OPERATIONS["catalog.get"].output.parse(catalog)).not.toThrow(); + expect(catalog.platforms[0]?.customModels).toEqual(["My Tablet"]); + }); + + it("drops a custom name the worker does not list in models", () => { + const catalog = aggregateCatalog([ + view({ + catalog: [androidOn({ customModels: ["My Tablet", "Ghost"], models: ["My Tablet"] })], + id: "wrk_a", + }), + // Another worker lists Ghost, but only the worker that marks a name can make it custom. + view({ catalog: [androidOn({ models: ["Ghost"] })], id: "wrk_b" }), + ]); + + expect(catalog.platforms[0]?.customModels).toEqual(["My Tablet"]); + }); + + it("omits customModels when no worker marks a model", () => { + const catalog = aggregateCatalog([ + view({ catalog: [androidOn({ customModels: ["Ghost"] })], id: "wrk_a" }), + ]); + + expect(catalog.platforms[0]).not.toHaveProperty("customModels"); }); it("drops an image whose runtime the reporting worker does not list", () => { diff --git a/src/gateway/aggregate.ts b/src/gateway/aggregate.ts index dcdcd84d3..49482b373 100644 --- a/src/gateway/aggregate.ts +++ b/src/gateway/aggregate.ts @@ -178,7 +178,8 @@ function sumRamBudget( * §4). It is never built from the fleet's `models` and `runtimes`: one worker having a model and * another having a runtime does not make the pair leasable anywhere. * - * `modelAliases` is the union per model, deduplicated ignoring case. `images` is the union by + * `customModels` lists a model when any worker that lists it marks it custom, and is absent when + * none does. `modelAliases` is the union per model, deduplicated ignoring case. `images` is the union by * runtime, tag, and ABI, and is absent when no worker reports the field at all. Each worker's * lists fit the contract's bounds but their union may not, so both are cut to those bounds after * sorting: an answer its own clients refuse would lose the whole catalog, not a tail of it. @@ -207,6 +208,8 @@ interface CatalogBucket { readonly modelAliases: Map>; /** Every image reported, keyed by runtime, tag, and ABI; `undefined` until one worker has the field. */ images: Map | undefined; + /** Models any worker that lists them marks custom. */ + readonly customModels: Set; readonly defaults: Set; } @@ -233,6 +236,7 @@ function addCatalogEntry( workerId: string, ): void { const bucket = byPlatform.get(entry.platform) ?? { + customModels: new Set(), defaults: new Set(), images: undefined, modelAliases: new Map>(), @@ -245,6 +249,7 @@ function addCatalogEntry( for (const runtime of entry.runtimes) annotate(bucket.runtimes, runtime, workerId); for (const model of entry.models) addPairings(bucket.modelRuntimes, entry, model); for (const model of entry.models) addAliases(bucket.modelAliases, entry, model); + addCustomModels(bucket.customModels, entry); if (entry.images !== undefined) bucket.images = addImages(bucket.images, entry, entry.images); bucket.defaults.add(entry.defaultRuntime); } @@ -265,6 +270,13 @@ function addAliases( if (aliases.size > 0) index.set(model, aliases); } +/** Folds one worker's custom models into the fleet's; a name the worker does not list itself is dropped. */ +function addCustomModels(index: Set, entry: PlatformCatalog): void { + for (const model of entry.customModels ?? []) { + if (entry.models.includes(model)) index.add(model); + } +} + /** Folds one worker's images into the fleet's; one whose runtime the worker does not list is dropped. */ function addImages( index: Map | undefined, @@ -294,6 +306,11 @@ function renderPlatform(platform: Platform, bucket: CatalogBucket): PlatformCata const agreedDefault = bucket.defaults.size === 1 ? [...bucket.defaults][0] : undefined; const runtimes = [...bucket.runtimes.keys()].sort(); return { + ...(bucket.customModels.size === 0 + ? {} + : { + customModels: [...bucket.customModels].sort().slice(0, CATALOG_LIST_LIMITS.customModels), + }), ...(bucket.images === undefined ? {} : { From d7a75c9311a276f5e0b66874ba3a3d68d7e2d096 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 14:44:13 +0200 Subject: [PATCH 2/3] fix(contract,daemon): a custom model name too long for the mark loses the mark, not the catalog --- src/contract/schemas.test.ts | 36 ++++++++++++++++ src/contract/schemas.ts | 17 ++++++++ src/daemon/dispatcher.test.ts | 13 ++++++ src/daemon/dispatcher.ts | 3 +- .../android/device-profile-source.test.ts | 42 ++++++++++++------- src/drivers/android/index.test.ts | 2 +- 6 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/contract/schemas.test.ts b/src/contract/schemas.test.ts index d7131999d..69535ab8a 100644 --- a/src/contract/schemas.test.ts +++ b/src/contract/schemas.test.ts @@ -3,9 +3,11 @@ import { describe, expect, it } from "vitest"; import { deviceRecordSchema, fitHostFacts, + fitPlatformCatalog, grantedDeviceSchema, hostFactsSchema, leaseGrantSchema, + platformCatalogSchema, statusDeviceSchema, } from "./schemas.js"; @@ -269,3 +271,37 @@ describe("fitHostFacts", () => { expect(fitHostFacts(host)).toEqual(host); }); }); + +describe("fitPlatformCatalog", () => { + const entry = (customModels: string[]) => ({ + customModels, + defaultRuntime: "35", + modelAliases: {}, + modelRuntimes: Object.fromEntries(customModels.map((model) => [model, ["35"]])), + models: customModels, + platform: "android" as const, + runtimes: ["35"], + }); + + it("stops customModels at its maximum, in an entry the schema accepts", () => { + const names = Array.from({ length: 4097 }, (_, index) => `m${index}`); + + const fitted = fitPlatformCatalog(entry(names)); + + expect(fitted.customModels).toEqual(names.slice(0, 4096)); + expect(() => platformCatalogSchema.parse(fitted)).not.toThrow(); + }); + + it("leaves out customModels when no name fits", () => { + const fitted = fitPlatformCatalog(entry(["x".repeat(257)])); + + expect(fitted).not.toHaveProperty("customModels"); + expect(fitted.models).toEqual(["x".repeat(257)]); + }); + + it("leaves an entry that already fits as it is", () => { + const fits = entry(["My Tablet"]); + + expect(fitPlatformCatalog(fits)).toEqual(fits); + }); +}); diff --git a/src/contract/schemas.ts b/src/contract/schemas.ts index e035b78b5..f8024f13d 100644 --- a/src/contract/schemas.ts +++ b/src/contract/schemas.ts @@ -425,6 +425,23 @@ export const platformCatalogSchema = z.object({ runtimeWorkers: z.record(z.string(), z.array(z.string())).optional(), }); +/** + * Brings a catalog entry a daemon built from its own machine within `customModels`' bounds, so + * a custom profile name the platform's tools never checked cannot fail the whole `catalog.get` + * answer. A name that does not fit loses its mark and stays in `models`; the list stops at its + * maximum, and an empty list is left out. Lives beside the schema so the bounds are written once. + */ +export function fitPlatformCatalog( + entry: Entry, +): Entry { + if (entry.customModels === undefined) return entry; + const { customModels, ...rest } = entry; + const fitting = customModels + .filter((model) => model.length <= CATALOG_NAME_MAX) + .slice(0, CATALOG_CUSTOM_MODELS_MAX); + return (fitting.length === 0 ? rest : { ...rest, customModels: fitting }) as Entry; +} + export const proposalSchema = z.object({ action: z.enum(["shutdown", "destroy"]), reason: z.string(), diff --git a/src/daemon/dispatcher.test.ts b/src/daemon/dispatcher.test.ts index 68f9db556..b94fb3f9c 100644 --- a/src/daemon/dispatcher.test.ts +++ b/src/daemon/dispatcher.test.ts @@ -962,6 +962,19 @@ describe("Dispatcher: the download policy clamp applies regardless of caller", ( expect(listCalls.map((call) => call.arguments)).toEqual([[]]); }); + it("catalog.get keeps a custom model whose name is too long for the mark listed, without the mark, in an answer the contract accepts", async () => { + const long = "x".repeat(300); + const { dispatcher } = await buildDispatcher({ + driverOptions: { customModels: [long, "My Tablet"], knownModels: [long, "My Tablet"] }, + }); + + const catalog = await dispatcher.dispatch("catalog.get", {}, session()); + + expect(() => OPERATIONS["catalog.get"].output.parse(catalog)).not.toThrow(); + expect(catalog.platforms[0]?.models).toEqual([long, "My Tablet"]); + expect(catalog.platforms[0]?.customModels).toEqual(["My Tablet"]); + }); + it.each(["never", "on-request", "always"] as const)( "catalog.get lists exactly the images the driver finds installed under downloads.policy '%s'", async (downloadsPolicy) => { diff --git a/src/daemon/dispatcher.ts b/src/daemon/dispatcher.ts index 92df403e5..82c8a6dde 100644 --- a/src/daemon/dispatcher.ts +++ b/src/daemon/dispatcher.ts @@ -33,6 +33,7 @@ import type { } from "../ports/index.js"; import { exitCodeOf, NoopLogger } from "../ports/index.js"; import { + fitPlatformCatalog, OPERATIONS, type ComponentProgress, type GatewayOnlyOperationName, @@ -264,7 +265,7 @@ export class Dispatcher { // already did, per the ADR's ordering. #catalogGet: Handler<"catalog.get"> = async (input) => ({ - platforms: await this.options.catalog.listCatalog(input.platform), + platforms: (await this.options.catalog.listCatalog(input.platform)).map(fitPlatformCatalog), }); #statusGet: Handler<"status.get"> = () => { diff --git a/src/drivers/android/device-profile-source.test.ts b/src/drivers/android/device-profile-source.test.ts index 5fb21a31c..175fa4087 100644 --- a/src/drivers/android/device-profile-source.test.ts +++ b/src/drivers/android/device-profile-source.test.ts @@ -374,22 +374,32 @@ describe("DeviceProfileRegistry", () => { }); }); - it("yields the built-in models and no customModels when devices.xml is unreadable", async () => { - const registry = new DeviceProfileRegistry([ - new BuiltinDeviceProfileSource( - avdmanager, - new ScriptedProcessRunner([processResult(pixelDevices)]), - ), - new UserDeviceProfileSource( - devicesXmlPath, - await filesystemWithDevicesXml("not even close to xml {{{"), - ), - ]); - - await expect(registry.catalog()).resolves.toMatchObject({ - customModels: [], - models: ["Pixel 8"], - }); + it("yields the built-in models and no customModels when devices.xml cannot be read or parsed", async () => { + // A file that exists but whose read fails, the way a file without read permission does. + const unreadable = await filesystemWithDevicesXml(devicesXml()); + unreadable.readFile = () => + Promise.reject(Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" })); + const malformed = await filesystemWithDevicesXml("not even close to xml {{{"); + + for (const filesystem of [unreadable, malformed]) { + const diagnostics: DeviceProfileSourceDiagnostic[] = []; + const registry = new DeviceProfileRegistry([ + new BuiltinDeviceProfileSource( + avdmanager, + new ScriptedProcessRunner([processResult(pixelDevices)]), + ), + new UserDeviceProfileSource(devicesXmlPath, filesystem, (diagnostic) => + diagnostics.push(diagnostic), + ), + ]); + + await expect(registry.catalog()).resolves.toMatchObject({ + customModels: [], + models: ["Pixel 8"], + }); + // The source was reached and gave up, rather than never being asked. + expect(diagnostics).toHaveLength(1); + } }); it("resolves every model in customModels to a profile of kind properties", async () => { diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 8e4265ac5..9b50424d8 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -1100,7 +1100,7 @@ describe("AndroidDriver", () => { expect(catalog).not.toHaveProperty("customModels"); }); - it("lists the built-in models and no customModels field when devices.xml is unreadable", async () => { + it("lists the built-in models and no customModels field when devices.xml cannot be parsed", async () => { const filesystem = await androidFilesystem(); await filesystem.mkdirp(`${home}/.android`); await filesystem.writeFileAtomic(`${home}/.android/devices.xml`, "not xml at all {{{"); From dcf0bf02fd06dc7a34caead9bbda09476019bbc3 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 14:53:32 +0200 Subject: [PATCH 3/3] test(gateway): name the case the custom-models omission test proves --- src/gateway/aggregate.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gateway/aggregate.test.ts b/src/gateway/aggregate.test.ts index 92d38ffa3..ae1cf392b 100644 --- a/src/gateway/aggregate.test.ts +++ b/src/gateway/aggregate.test.ts @@ -705,7 +705,7 @@ describe("aggregateCatalog", () => { expect(catalog.platforms[0]?.customModels).toEqual(["My Tablet"]); }); - it("omits customModels when no worker marks a model", () => { + it("omits customModels when no worker marks a model it lists", () => { const catalog = aggregateCatalog([ view({ catalog: [androidOn({ customModels: ["Ghost"] })], id: "wrk_a" }), ]);