Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,10 @@ got. A class is a claim about the device type the platform's tools report:
on iOS the product family, on Android the profile's tag, and every untagged
Android profile is a `phone`, so `tablet` holds no Android model.

Through a gateway, a lease that names no model is refused with `BAD_REQUEST`
for now.
Through a gateway the same requests work: a class, no model at all, and an
`--os` range are served by any worker that has something fitting, and the
gateway passes them on to that worker as they arrived (see
[Against a gateway](#against-a-gateway)).

### `simlock lease renew <lease-id> [--ttl <duration>]`

Expand Down Expand Up @@ -731,6 +733,16 @@ is preferred over one at it, and the one with the most free capacity gets the
request. You do not name a machine and there is no flag to; where
a device lives is the gateway's decision.

A request with `--class`, or with neither `--device` nor `--class` (which means
`--class phone`), is served by a worker whose catalog lists a model of that
class paired with an installed runtime that satisfies `--os` (any installed
runtime when you give none). With `--os` as a range, a model matches when one of
its paired runtimes is in the range. A warm device matches a class request when
its model is of that class on that worker, its runtime is in the range (any
installed runtime with no `--os`), and its mode and image tag fit as above. The
gateway does not pick the model or the runtime: the worker does, by its own
rules, so the grant names what you got.

### A request no worker can serve

A lease through a gateway never waits for something that cannot arrive. A
Expand All @@ -745,8 +757,8 @@ catalog has not arrived, is neither.
| --- | --- |
| No worker takes requests | `NO_CAPACITY` (exit 11) at once |
| At least one worker takes requests, and no known worker has the platform | `NO_DRIVER` (exit 12) at once |
| ... and no known worker lists the model | `UNKNOWN_MODEL` (exit 12) at once |
| ... and no known worker has the runtime, or can pair it with the model | `RUNTIME_MISSING` (exit 12) at once; `downloadable` is `false`, and `osVersion` is `default` when you named none |
| ... and no known worker lists the model, or, for a class, a model of it | `UNKNOWN_MODEL` (exit 12) at once |
| ... and no known worker has the runtime, or can pair it with the model (or with a model of the class) | `RUNTIME_MISSING` (exit 12) at once; `downloadable` is `false`, and `osVersion` is the range as you typed it, or `default` when you named none |
| A known worker could serve it, but none that takes requests can | `NO_CAPACITY` (exit 11) at once |
| A worker that takes requests can serve it but is busy | Waits in the queue; `NO_CAPACITY` only with `--no-wait` |

Expand Down
6 changes: 4 additions & 2 deletions docs/CLIENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ await client.requestLease({ platform: "ios", class: "tablet" });
await client.requestLease({ platform: "android" }); // a phone
```

Through a gateway, a request that names no `model` is a `BAD_REQUEST` for now.
Through a gateway the same requests work: any worker that has something fitting
serves one, a warm device first, and one nothing in the fleet can serve fails at
once with the code a single machine gives.

`requestLease` takes an optional `mode`, `"slim"` or `"full"`: the device
mode the lease asks for. Without it the lease gets the default mode of the
Expand Down Expand Up @@ -83,7 +85,7 @@ or a hyphen range (`"18 - 26"`). A short version covers everything under it, so
by an idle device whose OS satisfies it or by a new device on the newest
installed runtime in it, and when none is installed it fails at once with
`RUNTIME_MISSING`, whatever `allowDownload` says. Through a gateway a range is
`BAD_REQUEST` for now. A request with `imageTag` never downloads:
served by any worker with a paired runtime in it. A request with `imageTag` never downloads:
when no image of that tag is installed for the API level it fails with
`RUNTIME_MISSING`, whatever `allowDownload` says. On iOS it is a
`BAD_REQUEST`, and so is a tag that is not 1 to 64 letters, digits, `_`, `.`
Expand Down
2 changes: 1 addition & 1 deletion docs/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ above on its own behalf, because it owns no devices.
| `worker.removed` | worker id, label, reason (operator/retention) | a disconnected worker's view was forgotten: by `simlock worker remove`, or because every lease on it had expired and `gateway.disconnectedRetentionMs` elapsed. Never emitted for a connected worker | WorkerRegistry | implemented |
| `worker.drain-started` | worker id, label | `simlock worker drain` flagged a worker: it keeps its leases and receives no new dispatches | WorkerRegistry | implemented |
| `worker.drain-ended` | worker id, label | `simlock worker undrain` cleared that flag. The flag lives in the gateway's persisted worker registry, so it survives both a worker reconnect and a gateway restart | WorkerRegistry | implemented |
| `request.dispatched` | request id, worker id, requester id, platform, model, the mode when the request named one, reason (warm-hit/free-capacity), the routing stage that decided, queued duration | the gateway's fleet queue sent a queued request to a worker, and the worker took it — the grant itself, or its first `progress` push, whichever arrives first. An immediate `NO_CAPACITY` is a stale view, not a dispatch, and emits nothing — the request stays queued, and that worker is not sent it again until its reported state changes; a `--no-wait` request fails with `NO_CAPACITY` instead, unless another worker takes it at once | FleetLeaseCoordinator | implemented |
| `request.dispatched` | request id, worker id, requester id, platform, model when the request named one, device class when the request named one, the mode when the request named one, reason (warm-hit/free-capacity), the routing stage that decided, queued duration | the gateway's fleet queue sent a queued request to a worker, and the worker took it — the grant itself, or its first `progress` push, whichever arrives first. An immediate `NO_CAPACITY` is a stale view, not a dispatch, and emits nothing — the request stays queued, and that worker is not sent it again until its reported state changes; a `--no-wait` request fails with `NO_CAPACITY` instead, unless another worker takes it at once | FleetLeaseCoordinator | implemented |

Drain and undrain are idempotent, and an event is a fact about a *change*:
draining an already-drained worker succeeds and emits nothing.
Expand Down
14 changes: 12 additions & 2 deletions docs/HTTP-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ class's preference list that the machine lists (see
with no listed model fails with `422 UNKNOWN_MODEL`, naming the config key, and
one whose listed models pair with no installed runtime fails with `RUNTIME_MISSING`.
The granted lease's `device`, `os`, `mode` and `imageTag` always name the device
you got. Through a gateway a request that names no `device`, or whose `os` is a
range, is `400 BAD_REQUEST` for now. `os` is an exact version (`"18.4"`) or a
you got. `os` is an exact version (`"18.4"`) or a
range: one or more of `>=`, `>`, `<=`, `<` followed by a version, joined by single
spaces (`">=18 <26"`), or a hyphen range (`"18 - 26"`); a short version covers
everything under it, so `">=18"` is 18.0 and newer and `"<=26"` includes every
Expand Down Expand Up @@ -433,6 +432,17 @@ and pairs that model with `os` (or, without `os`, with at least one installed
runtime). With `imageTag`, the worker's catalog must also list an image of that
tag, for `os` when it is given. The worker is sent its own name for the model.

A request with `class`, or with neither `device` nor `class` (which means
`phone`), goes to a worker whose catalog lists a model of that class paired with
an installed runtime that satisfies `os`, or, without `os`, with at least one
installed runtime. An `os` range is forwarded as you wrote it, and so is the
class: the worker picks the model and the runtime by its own rules. A worker
holding a ready device of that class, in the range and in the mode the request
needs, is preferred over one that would create a device. A class no worker
lists a model of fails at once with `422 UNKNOWN_MODEL`, and a range none of
their models of the class pair with an installed runtime in fails at once with
`422 RUNTIME_MISSING` carrying the range as `osVersion`.

A gateway fails a request no worker can serve at once, with or without
`noWait` and `timeoutMs`, instead of queueing it. A worker *takes requests*
when it is connected, not drained, and the gateway has read its catalog since
Expand Down
42 changes: 27 additions & 15 deletions docs/internal/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,22 +655,29 @@ The v1 policy (`warm-then-free`) is eight stages:
2. `can-serve` (filter): drop workers whose catalog cannot serve the request
(ADR 0009 §3, `routing/request-match.ts`). The model is the first entry of
the worker's `models` whose name or `modelAliases` entry equals the
requested name, ignoring letter case. A named runtime must be in that
model's `modelRuntimes`; with none named the list must be non-empty. Only
installed runtimes count, so a download never makes a worker able to
serve;
requested name, ignoring letter case (`findCatalogModel`, the core's one
matcher). A class request (the class `requestedClass` gives, `phone` for
neither a model nor a class, ADR 0015 §8) matches when the catalog's
`modelClasses` puts any model in that class. Either way a model needs a
paired runtime in `modelRuntimes` that satisfies the request's OS
constraint (`os-range`, the contract's one grammar: an exact version or a
range); with none named the list must be non-empty. Only installed
runtimes count, so a download never makes a worker able to serve;
3. `healthy` (filter): keep a worker whose health is `running`;
4. `idle-queue` (filter): keep a worker whose own `queueDepth` is zero, since a
worker with a local waiter refuses every `noWait` request (ADR 0005
requirement 12). A worker that has not reported health or queue depth is
not known to be healthy or idle, so it is dropped. Stages 3 and 4 drop a
worker that is busy, not unable: its requests wait;
5. `warm-hit` (rank, settles): prefer a worker with an unleased `ready` device
matching the request, compared against the worker's own name for the
model — a **warm hit**, and a sub-second grant. The runtime must be the
one requested or, with none requested, the one the worker would pick (the
catalog's `defaultRuntime` when the model pairs with it, otherwise its only
paired runtime; none when several pair and no default does). The mode must
matching the request — a **warm hit**, and a sub-second grant. "Matching"
is the core's `fits` (ADR 0015 §6, §8) over a requirement built from the
worker's own catalog: its own name for an exact model, or the class with
the worker's `modelClasses` as `classOf`. The OS is the one requested or
the range requested; with none requested, an exact model needs the runtime
the worker would pick (the catalog's `defaultRuntime` when the model pairs
with it, otherwise its only paired runtime; none when several pair and no
default does) and a class accepts any installed runtime. The mode must
fit (ADR 0009 §6): `full` needs a device reporting `mode: "full"`, `slim` one
reporting `slim`, and a request naming none a device reporting
`servesDefaultMode`, which the worker computes in
Expand All @@ -692,9 +699,12 @@ same function `tryReserveProvisioning` calls, so status and the planner cannot
disagree.

The same matcher gives the name the gateway forwards: the worker is sent its
own name for the model, so it resolves exactly what routing matched, and
`allowDownload` is always forwarded as `false`. `lease.requested` keeps the
name the client sent.
own name for an exact model, so it resolves exactly what routing matched, and
`allowDownload` is always forwarded as `false`. A class and an OS range are
forwarded as they arrived, with no `model` for a class request: the worker
resolves them by its own rules (ADR 0015 §5), and the gateway compares no
versions. `lease.requested` keeps the request as the client sent it, and
`request.dispatched` carries `model` or `class`, whichever the request named.

#### A request that cannot be served

Expand All @@ -706,9 +716,11 @@ arriving request on rows 1 to 5 is rejected without entering the queue (no
`lease.queued`, no `queued` progress), and a drain or a lost uplink re-runs the
walk, which rejects a waiting request that has moved onto those rows. In
order: no worker takes requests, `NO_CAPACITY` (reason `no-worker`); no known
worker has the platform, `NO_DRIVER`; none lists the model, `UNKNOWN_MODEL`;
none has the runtime or pairs it with the model, `RUNTIME_MISSING` with
`downloadable: false` and `osVersion: "default"` for an unnamed runtime (the
worker has the platform, `NO_DRIVER`; none lists the model, or for a class
request any model of it, `UNKNOWN_MODEL` (its details carry `class` instead of
`model`); none has the runtime or pairs it with the model, or with a model of
the class, `RUNTIME_MISSING` with `downloadable: false` and `osVersion` the
constraint as typed, `"default"` for an unnamed runtime (the
last three with reason `unresolvable-spec`); a known worker can serve it but
none that takes requests can, `NO_CAPACITY` (`no-worker`); otherwise route or
wait. A worker *takes requests* when it passes `takes-requests`; the gateway
Expand Down
6 changes: 4 additions & 2 deletions docs/internal/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ in short: `subject.past-tense-fact`, emitted post-commit, facts not commands.
> it a third time: `lease.requested` and `lease.rejected` carry the request
> as it arrived, so `requestSpec.model` becomes optional (a request naming a
> class or nothing has none) and `requestSpec.class` enters. Making a
> required key optional is not additive.
> required key optional is not additive. The exception covers
> `request.dispatched` too, which names the same request: `model` becomes
> optional and `class` enters, for the same reason.

## Lease lifecycle

Expand Down Expand Up @@ -154,7 +156,7 @@ because it owns no devices.
| `worker.removed` | worker id, label, reason (operator/retention) | a disconnected worker's view was forgotten: by `simlock worker remove`, or because every gateway-issued lease on it had expired and `gateway.disconnectedRetentionMs` elapsed. Never emitted for a connected worker (`WORKER_CONNECTED` refuses that) | WorkerRegistry | implemented |
| `worker.drain-started` | worker id, label | `simlock worker drain` flagged a worker: it keeps its leases and receives no new dispatches | WorkerRegistry | implemented |
| `worker.drain-ended` | worker id, label | `simlock worker undrain` cleared that flag. Nothing else ends a drain: the flag lives in the gateway's persisted worker registry, so it survives both a worker reconnect and a gateway restart | WorkerRegistry | implemented |
| `request.dispatched` | `{ requestId, workerId, requesterId, platform, model, mode?, reason, stage, queuedMs }` — `mode` is the device mode the request named, `slim` or `full`, and absent when it named none (ADR 0009 §8); `reason` is the routing policy's own `warm-hit`/`free-capacity` distinction (ADR 0005 §13): `warm-hit` when the deciding stage is `warm-hit`, `free-capacity` otherwise; `stage` names the routing stage that decided (ADR 0009 §1, §8); `queuedMs` how long the request sat queued before this dispatch | the gateway's fleet queue sent a queued request to a worker with `noWait: true`, and the worker took it — the grant itself, or its first `progress` push, whichever arrives first (ADR 0005 §11: device work having started means the request is that worker's now). An immediate `NO_CAPACITY` is a stale view, not a dispatch, and emits nothing — the request stays queued, and that worker is not sent it again until its reported state changes (ADR 0009 §5); a `noWait` request fails with `NO_CAPACITY` instead, unless another worker takes it at once | FleetLeaseCoordinator | implemented |
| `request.dispatched` | `{ requestId, workerId, requesterId, platform, model?, class?, mode?, reason, stage, queuedMs }` — `model` is the exact model the request named and `class` the class it named; a request names at most one, and one naming neither carries neither (ADR 0015 §9); `mode` is the device mode the request named, `slim` or `full`, and absent when it named none (ADR 0009 §8); `reason` is the routing policy's own `warm-hit`/`free-capacity` distinction (ADR 0005 §13): `warm-hit` when the deciding stage is `warm-hit`, `free-capacity` otherwise; `stage` names the routing stage that decided (ADR 0009 §1, §8); `queuedMs` how long the request sat queued before this dispatch | the gateway's fleet queue sent a queued request to a worker with `noWait: true`, and the worker took it — the grant itself, or its first `progress` push, whichever arrives first (ADR 0005 §11: device work having started means the request is that worker's now). An immediate `NO_CAPACITY` is a stale view, not a dispatch, and emits nothing — the request stays queued, and that worker is not sent it again until its reported state changes (ADR 0009 §5); a `noWait` request fails with `NO_CAPACITY` instead, unless another worker takes it at once | FleetLeaseCoordinator | implemented |

Drain and undrain are idempotent, and an event is a fact about a *change*:
draining an already-drained worker succeeds and emits nothing.
Expand Down
39 changes: 39 additions & 0 deletions e2e/gateway-fast-fail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,45 @@ describe("a gateway fails a request no worker can serve at once", () => {
expect((await gateway.cli(["release", held.id], FAST)).code).toBe(0);
});

it("fails a class or range request nothing in the fleet can serve at once, with the code a single worker gives", async () => {
const { gateway, join } = await startGateway();
await join("worker-a", {
ios: {
availableOsVersions: ["18.4", "26.0"],
defaultModels: { phone: ["iPhone 17"] },
knownModels: ["iPhone 17"],
modelClasses: { "iPhone 17": "phone" },
},
});
await waitForWorkers(gateway, ["worker-a"]);
const ask = (...args: string[]) =>
gateway.cli(
["lease", "--platform", "ios", ...args, "--agent-id", "agent-1", "--detach"],
FAST,
);

// No worker lists a model of the class.
const watch = await ask("--class", "watch");
expectFailure(watch, "UNKNOWN_MODEL", EXIT_UNSERVABLE);
expect(watch.stderr).toContain("watch");
// Every model of the class pairs with a runtime outside the range, with or without a download.
expectFailure(
await ask("--class", "phone", "--os", "<=17"),
"RUNTIME_MISSING",
EXIT_UNSERVABLE,
);
expectFailure(
await ask("--class", "phone", "--os", "<=17", "--allow-download"),
"RUNTIME_MISSING",
EXIT_UNSERVABLE,
);
// A request that names neither model nor class means phone, and the fleet has one.
const granted = await ask();
expect(granted.code, granted.stderr).toBe(0);
const { lease: held } = granted.json as { lease: { id: string } };
expect((await gateway.cli(["release", held.id], FAST)).code).toBe(0);
});

it("fails the HTTP POST of a request no worker can serve, with allowDownload too", async () => {
const { baseUrl, gateway, join } = await startGateway();
await join("worker-a", { ios: IPHONE_17 });
Expand Down
Loading
Loading