diff --git a/docs/internal/EVENTS.md b/docs/internal/EVENTS.md index ea25c74b9..af11edcae 100644 --- a/docs/internal/EVENTS.md +++ b/docs/internal/EVENTS.md @@ -168,7 +168,7 @@ because it owns no devices. | `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?, class?, mode?, physical?, 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); `physical` is `true` when the request asked for a physical device and absent otherwise; `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; a physical pick is `warm-hit` with stage `warm-hit`, matching `lease.granted.source` `warm` (ADR 0022 §9); `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.granted` | `{ requestId, worker, leaseId, workerLeaseId }` -- `leaseId` is the gateway lease id and `workerLeaseId` the worker's; `worker`, never `workerId`, because `payload.workerId` marks a relayed event (ADR 0014 §6) | the gateway handed a fleet request's grant to its caller (ADR 0021 §4): after the lease index accepted it, and only if the waiter was still open. A grant given back for ADR 0020's mismatched id, one the lease index refused (the request ends in `lease-id-taken`), and one that lands after the waiter was settled (timeout, cancel, or the gateway stopping) emit nothing. A worker's push of this name is refused by the worker link (it is in `GATEWAY_OWN_EVENTS` with the six `worker.*` events; `request.dispatched` is not in that set) | FleetLeaseCoordinator | implemented | +| `request.granted` | `{ requestId, worker, leaseId, workerLeaseId }` -- `leaseId` is the gateway lease id and `workerLeaseId` the worker's; `worker`, never `workerId`, because `payload.workerId` marks a relayed event (ADR 0014 §6) | the gateway handed a fleet request's grant to its caller (ADR 0021 §4): after the lease index accepted it, and only if the waiter was still open. A grant given back for ADR 0020's mismatched id, one the lease index refused (the request ends in `lease-id-taken`), and one that lands after the waiter was settled (timeout, cancel, or the gateway stopping) emit nothing. A worker's push of this name is refused by the worker link (it is in `GATEWAY_OWN_EVENTS` with the six `worker.*` events and `request.dispatched`) | 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. diff --git a/src/gateway/AGENTS.md b/src/gateway/AGENTS.md index 270d1e465..1fb1f7a97 100644 --- a/src/gateway/AGENTS.md +++ b/src/gateway/AGENTS.md @@ -57,7 +57,7 @@ Flows: [device selection](../../docs/internal/flows/device-selection.md), `catalog.get` and `config.get` on the tick, after `component.installed`, or with no catalog yet. One at a time; refreshes asked meanwhile merge into one. 2 timed-out refreshes in a row → close. 6. Worker events are republished with `workerId`. `lease.*`, `device.*`, `component.install*` - trigger a refresh. A worker event named like a gateway fact (`worker.*`, `request.granted`) is dropped. + trigger a refresh. A worker event named like a gateway fact (`worker.*`, `request.dispatched`, `request.granted`) is dropped. **Fleet request** (`FleetLeaseCoordinator.request`, under its own `SerializedDecision`; routing's `assess` and nine stages are in [routing](routing/AGENTS.md)): diff --git a/src/gateway/service.test.ts b/src/gateway/service.test.ts index bef1fd6c9..b53b89677 100644 --- a/src/gateway/service.test.ts +++ b/src/gateway/service.test.ts @@ -681,11 +681,10 @@ describe("GatewayService", () => { await harness.service.stop(); }); - // Hardening: a worker's event name is taken on faith from its own `events.subscribe` push -- - // `workerId` is merged in last so it cannot be spoofed, but nothing about the protocol proves - // the *name* is genuinely the worker's own. Without a guard, a worker (or anything speaking - // its admin protocol) could forge one of the six subjects `docs/internal/EVENTS.md` calls "the - // gateway's own" -- e.g. `worker.drain-ended` -- straight into the operator's audit trail. + // why: nothing about the admin protocol proves that a pushed event's *name* is genuinely the + // worker's own -- `workerId` is merged in last so it cannot be spoofed, but a worker (or + // anything speaking its protocol) could otherwise forge a fact only a gateway authors, such as + // `worker.drain-ended`, straight into the operator's audit trail. it("refuses to republish a worker's event under a name reserved for the gateway itself", async () => { const harness = fleet(); await harness.service.start(); @@ -698,8 +697,6 @@ describe("GatewayService", () => { module: "gateway", payload: { workerId: "wrk_forged", label: "not-this-worker" }, }); - // A genuine event from the same worker still goes through -- the guard is on the six names, - // not on this link entirely. worker.pushEvent({ event: "lease.granted", module: "lease-engine", @@ -743,6 +740,40 @@ describe("GatewayService", () => { await harness.service.stop(); }); + it("refuses to republish a request.dispatched a worker pushes, which only the gateway itself emits", async () => { + const harness = fleet(); + await harness.service.start(); + const worker = new ScriptedWorkerClient(); + await harness.join("wrk_1", worker); + await vi.waitFor(() => expect(worker.subscribed).toBe(true)); + + worker.pushEvent({ + event: "request.dispatched", + module: "fleet-lease-coordinator", + payload: { + platform: "ios", + queuedMs: 0, + reason: "warm-hit", + requestId: "req_forged", + requesterId: "agent-1", + stage: "warm-hit", + workerId: "wrk_forged", + }, + }); + worker.pushEvent({ + event: "lease.granted", + module: "lease-engine", + payload: { deviceId: "dev_1", leaseId: "lease_1", requester: "agent-1" }, + }); + await vi.waitFor(() => + expect(harness.events.some((event) => event.event === "lease.granted")).toBe(true), + ); + + expect(harness.events.filter((event) => event.event === "request.dispatched")).toEqual([]); + + await harness.service.stop(); + }); + it("refreshes the view on a worker event that changes capacity or leases", async () => { const harness = fleet(); await harness.service.start(); diff --git a/src/gateway/worker-link.ts b/src/gateway/worker-link.ts index 72bac0729..8c678126a 100644 --- a/src/gateway/worker-link.ts +++ b/src/gateway/worker-link.ts @@ -506,21 +506,14 @@ export class WorkerLink { } /** - * ADR 0005 §22: a worker's business events are republished on the gateway's bus with - * `workerId` added, so they land in its ring buffer and `simlock events --follow` against a - * gateway shows the fleet. The name, the emitting module, the `id` and the `timestamp` travel - * unchanged (ADR 0014 §2: one fact, one id, and the time it happened) -- the fact came - * from that worker's reaper or its leasing module, and rewriting either would make the audit trail - * lie about where it happened; `workerId` is what says which machine. - * - * Hardening: `envelope.event` is whatever string a worker's own `events.subscribe` push sends - * -- a client of this same admin protocol, on a machine this gateway does not otherwise - * control -- and `workerId` is merged in last, so it cannot be spoofed, but the *name* is - * taken on faith. Refusing the names in `GATEWAY_OWN_EVENTS` (the six `worker.*` facts and - * `request.granted`; `request.dispatched` is not in the set) keeps a worker from forging - * `worker.drain-ended`, `worker.removed`, `request.granted`, etc. into the operator's audit trail -- facts this gateway itself is supposed to be the only - * author of. Every other name still forwards unchanged, including ones this gateway's own - * `EventMap` has never heard of (a newer worker's own vocabulary). + * why: a push names its own event with a plain string nothing proves the worker owns, while + * `workerId` is merged in here and cannot be spoofed -- so the names only a gateway authors + * (`GATEWAY_OWN_EVENTS`) are refused by name, or a worker would write `worker.removed` or + * `request.granted` straight into the operator's audit trail. Any other name is forwarded + * unchanged: a newer worker's vocabulary is a fact its consumers need, and a gateway that + * dropped it would lose it from `events.replay`. The name, module, `id` and `timestamp` travel + * as they arrived -- the fact happened on that machine at that time, and rewriting either would + * make the audit trail lie about where it happened; `workerId` is what says which machine. */ #onWorkerEvent(envelope: { readonly id: string; @@ -561,11 +554,10 @@ export class WorkerLink { } /** - * ADR 0005 §22 / `docs/internal/EVENTS.md`: the facts only a gateway itself ever emits -- six about - * the workers connected to it, and (ADR 0021 §4) `request.granted`, the outcome of a fleet request. - * Never forwarded from a worker's own event stream (see - * `#onWorkerEvent`) -- a worker is a client of the same admin protocol, and nothing about - * `events.subscribe` proves the name it pushes is genuinely its own. + * why: a worker's `events.subscribe` push carries an event name it simply claims, and the uplink + * proves only that it holds a worker credential -- so the names whose facts a gateway is the sole + * author of have to be dropped rather than forwarded, or a worker would put a dispatch or a grant + * it never served into the operator's audit trail, beside the gateway's genuine ones. */ const GATEWAY_OWN_EVENTS: ReadonlySet = new Set([ "worker.connected", @@ -574,6 +566,7 @@ const GATEWAY_OWN_EVENTS: ReadonlySet = new Set([ "worker.removed", "worker.drain-started", "worker.drain-ended", + "request.dispatched", "request.granted", ]);