From cd9ed82dab4f787759163a77a009ad96cbaf849f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:16:55 +0100 Subject: [PATCH 1/9] test: a gateway ranks a worker whose new devices are paused below one that is not (#1364) --- src/gateway/routing.test.ts | 36 +++++++++++++++++++ src/gateway/routing/index.ts | 1 + src/gateway/routing/stages/memory-pressure.ts | 14 ++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/gateway/routing/stages/memory-pressure.ts diff --git a/src/gateway/routing.test.ts b/src/gateway/routing.test.ts index dc530bc88..40b78ac2d 100644 --- a/src/gateway/routing.test.ts +++ b/src/gateway/routing.test.ts @@ -203,6 +203,42 @@ describe("the registered warm-then-free policy", () => { expect(policy.select(REQUEST, [full])).toMatchObject({ workerId: "wrk_b" }); }); + it("ranks a worker whose new devices are paused below one that is not, still picks it for a warm device or when every worker is paused, explains it as memory-pressure, and ranks warming-paused as calm", () => { + const paused = ( + id: string, + step: "none" | "warming-paused" | "new-devices-paused", + maxRunning: number, + devices: WorkerView["devices"] = [], + ) => + view(id, { + capacity: { ...withIos({ maxRunning }), memoryPressure: { step } }, + devices, + }); + // why: wrk_a has more free capacity, so only the pressure rank can prefer wrk_b over it. + const pausedRoomy = paused("wrk_a", "new-devices-paused", 5); + const calm = paused("wrk_b", "none", 1); + + expect(policy.select(REQUEST, [pausedRoomy, calm])).toMatchObject({ workerId: "wrk_b" }); + expect( + policy.select(REQUEST, [ + paused("wrk_a", "new-devices-paused", 5, [deviceFixture("dev_1", "ready")]), + calm, + ]), + ).toMatchObject({ reason: "warm-hit", workerId: "wrk_a" }); + expect( + policy.select(REQUEST, [pausedRoomy, paused("wrk_b", "new-devices-paused", 1)]), + ).toMatchObject({ + workerId: "wrk_a", + }); + expect(policy.explain(REQUEST, [pausedRoomy, calm])).toMatchObject([ + { workerId: "wrk_a", zero: { capacityReason: "memory-pressure", reason: "full" } }, + { workerId: "wrk_b" }, + ]); + expect(policy.select(REQUEST, [paused("wrk_a", "warming-paused", 5), calm])).toMatchObject({ + workerId: "wrk_a", + }); + }); + it("still picks a worker at its RAM budget for a warm device it holds", () => { const capacity = withIos({ maxRunning: 1 }); const atBudget = view("wrk_a", { diff --git a/src/gateway/routing/index.ts b/src/gateway/routing/index.ts index 24302c915..31a48a8e0 100644 --- a/src/gateway/routing/index.ts +++ b/src/gateway/routing/index.ts @@ -16,6 +16,7 @@ export { hasCapabilities } from "./stages/has-capabilities.js"; export { keepsExcept } from "./stages/keeps-except.js"; export { healthy } from "./stages/healthy.js"; export { idleQueue } from "./stages/idle-queue.js"; +export { memoryPressure } from "./stages/memory-pressure.js"; export { ramBudget } from "./stages/ram-budget.js"; export { takesRequests } from "./stages/takes-requests.js"; export { warmHit } from "./stages/warm-hit.js"; diff --git a/src/gateway/routing/stages/memory-pressure.ts b/src/gateway/routing/stages/memory-pressure.ts new file mode 100644 index 000000000..b975feba8 --- /dev/null +++ b/src/gateway/routing/stages/memory-pressure.ts @@ -0,0 +1,14 @@ +import type { RankStage } from "../pipeline.js"; + +export const memoryPressure: RankStage = { + appliesToPhysical: false, + kind: "rank", + name: "memory-pressure", + score() { + return 1; + }, + settles: false, + zeroReason() { + return undefined; + }, +}; From 51f75182b5aa4071482caa8970edf5022e1784d1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:17:25 +0100 Subject: [PATCH 2/9] feat(gateway): a paused worker is ranked below a calm one (#1364) --- src/gateway/routing.test.ts | 12 ++++++++++-- src/gateway/routing.ts | 2 ++ src/gateway/routing/stages/memory-pressure.ts | 13 +++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/gateway/routing.test.ts b/src/gateway/routing.test.ts index 40b78ac2d..a92e6c657 100644 --- a/src/gateway/routing.test.ts +++ b/src/gateway/routing.test.ts @@ -7,6 +7,7 @@ import { freeSlot, healthy, idleQueue, + memoryPressure, ramBudget, takesRequests, warmHit, @@ -411,7 +412,7 @@ describe("the warm-then-free policy for a physical request", () => { expect(policy.select(REQUEST, [queued])).toBeUndefined(); }); - it("picks, for a physical request, a worker that idle-queue and free-slot would drop and ram-budget would demote, by warm-hit; the four stages are the ones that skip a physical request", () => { + it("picks, for a physical request, a worker that idle-queue and free-slot would drop and ram-budget would demote, by warm-hit; the five stages are the ones that skip a physical request", () => { const blocked = view("wrk_a", { capacity: withIos({ atRamBudget: true, maxRunning: 0 }), devices: [iphone()], @@ -435,6 +436,7 @@ describe("the warm-then-free policy for a physical request", () => { freeSlot, healthy, idleQueue, + memoryPressure, ramBudget, takesRequests, warmHit, @@ -443,7 +445,13 @@ describe("the warm-then-free policy for a physical request", () => { .filter(([, stage]) => (stage as { appliesToPhysical?: boolean }).appliesToPhysical === false) .map(([name]) => name) .sort(); - expect(skipped).toEqual(["freeCapacity", "freeSlot", "idleQueue", "ramBudget"]); + expect(skipped).toEqual([ + "freeCapacity", + "freeSlot", + "idleQueue", + "memoryPressure", + "ramBudget", + ]); }); it("gives a virtual request the same pick, reason and stage with and without a ready physical device of its model in the views", () => { diff --git a/src/gateway/routing.ts b/src/gateway/routing.ts index 3b1933443..079e4ab5e 100644 --- a/src/gateway/routing.ts +++ b/src/gateway/routing.ts @@ -9,6 +9,7 @@ import { healthy, idleQueue, keepsExcept, + memoryPressure, ramBudget, type RoutableRequest, type RoutingStage, @@ -80,6 +81,7 @@ const routingPolicies = { idleQueue, warmHit, freeSlot, + memoryPressure, ramBudget, freeCapacity, ], diff --git a/src/gateway/routing/stages/memory-pressure.ts b/src/gateway/routing/stages/memory-pressure.ts index b975feba8..c24e1bb92 100644 --- a/src/gateway/routing/stages/memory-pressure.ts +++ b/src/gateway/routing/stages/memory-pressure.ts @@ -1,14 +1,19 @@ import type { RankStage } from "../pipeline.js"; +/** why: ADR 0068 ยง6 -- a rank, so a paused worker is still asked when it is the only one or holds a + * warm device. It reads only the step the worker reports; `warming-paused` ranks as calm because + * that worker still takes leases. */ export const memoryPressure: RankStage = { appliesToPhysical: false, kind: "rank", name: "memory-pressure", - score() { - return 1; + score(worker) { + return worker.capacity?.memoryPressure?.step === "new-devices-paused" ? 0 : 1; }, settles: false, - zeroReason() { - return undefined; + zeroReason(worker) { + return worker.capacity?.memoryPressure?.step === "new-devices-paused" + ? { capacityReason: "memory-pressure", reason: "full" } + : undefined; }, }; From 3bdb3ea7e89ab7723da6eb40998a4bfb3576cac0 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:18:09 +0100 Subject: [PATCH 3/9] test: a gateway refreshes a worker's view at once on its memory.pressure-changed (#1364) --- src/gateway/service.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/gateway/service.test.ts b/src/gateway/service.test.ts index c7086d44a..2db0341b5 100644 --- a/src/gateway/service.test.ts +++ b/src/gateway/service.test.ts @@ -779,6 +779,29 @@ describe("GatewayService", () => { await harness.service.stop(); }); + it("refreshes a worker's view at once on its memory.pressure-changed", 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)); + await vi.waitFor(() => expect(harness.service.workers.view("wrk_1")?.capacity).toBeDefined()); + const capacity = statusFixture().capacity; + worker.status = statusFixture({ + capacity: { ...capacity, memoryPressure: { step: "new-devices-paused" } }, + }); + + worker.pushEvent({ event: "memory.pressure-changed" }); + + await vi.waitFor(() => + expect(harness.service.workers.view("wrk_1")?.capacity?.memoryPressure?.step).toBe( + "new-devices-paused", + ), + ); + + await harness.service.stop(); + }); + it("does not re-read the fleet for an event that changes neither", async () => { const harness = fleet(); await harness.service.start(); From 17e0cdc486db332e13adfc47d0789a02ed28f23c Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:18:19 +0100 Subject: [PATCH 4/9] feat(gateway): memory.pressure-changed refreshes the worker's view (#1364) --- src/gateway/worker-link.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gateway/worker-link.ts b/src/gateway/worker-link.ts index 1b3f7830c..952f584c4 100644 --- a/src/gateway/worker-link.ts +++ b/src/gateway/worker-link.ts @@ -630,6 +630,7 @@ function changesCapacityOrLeases(event: string): boolean { return ( event.startsWith("lease.") || event.startsWith("device.") || + event === "memory.pressure-changed" || INSTALL_EVENTS.has(event) || (WORKER_VIEW_BASELINE_EVENTS as readonly string[]).includes(event) || (WORKER_VIEW_UPDATE_EVENTS as readonly string[]).includes(event) From 7cb4a88d85772bdf8ac4440c66d0f3bcdebc0550 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:18:45 +0100 Subject: [PATCH 5/9] test: an operator's simlock worker list shows each worker's memory step (#1364) --- src/cli/index.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/cli/index.test.ts b/src/cli/index.test.ts index b82cd41ce..a328a3506 100644 --- a/src/cli/index.test.ts +++ b/src/cli/index.test.ts @@ -10051,6 +10051,43 @@ describe("CLI: a worker's capabilities", () => { expect(shown).toMatch(/^wrk_4 .* -- agent-device unavailable: starting$/m); }); + it("simlock worker list shows each worker's memory step as the end of its first line, and nothing at none, with no field, or for a disconnected worker", async () => { + const withStep = ( + id: string, + step: "none" | "warming-paused" | "new-devices-paused", + extra = {}, + ) => + worker({ + capabilities: [{ name: "agent-device", state: "available" }], + capacity: { ...worker().capacity, memoryPressure: { step } }, + id, + ...extra, + }); + const shown = await listed([ + withStep("wrk_1", "warming-paused"), + withStep("wrk_2", "new-devices-paused"), + withStep("wrk_3", "none"), + worker({ id: "wrk_4" }), + withStep("wrk_5", "new-devices-paused", { connection: "disconnected" }), + ]); + + const lines = shown.trimEnd().split("\n"); + const first = (id: string) => lines.find((line) => line.startsWith(`${id} `)); + expect(first("wrk_1")).toBe( + "wrk_1 (mac-mini): connected -- ios 0/2, android 0/2, 0 lease(s) -- agent-device available -- memory: warming paused", + ); + expect(first("wrk_2")).toBe( + "wrk_2 (mac-mini): connected -- ios 0/2, android 0/2, 0 lease(s) -- agent-device available -- memory: new devices paused", + ); + expect(first("wrk_3")).toBe( + "wrk_3 (mac-mini): connected -- ios 0/2, android 0/2, 0 lease(s) -- agent-device available", + ); + expect(first("wrk_4")).toBe("wrk_4 (mac-mini): connected -- ios 0/2, android 0/2, 0 lease(s)"); + expect(first("wrk_5")).toBe( + "wrk_5 (mac-mini): disconnected -- ios 0/2, android 0/2, 0 lease(s) -- agent-device available", + ); + }); + it("worker list prints two entries in the order the worker sent them", async () => { const shown = await listed([ worker({ From 16469791e9805cbd21ce106382a1cb255614e63f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:19:04 +0100 Subject: [PATCH 6/9] feat(cli): worker list shows a worker's memory step (#1364) --- src/cli/format.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cli/format.ts b/src/cli/format.ts index 0297d6316..3fc36e4a3 100644 --- a/src/cli/format.ts +++ b/src/cli/format.ts @@ -45,11 +45,24 @@ export function formatWorkers( : ` -- ${esc(entry.name)} ${esc(entry.state)}`, ) .join(""); - return `${label}: ${state} -- ${capacity}, ${leaseCount(worker)}${skew}${workerVersion(worker)}${host}${capabilities}${load}${workerBaselinesLine(worker)}${installs}`; + const memory = memoryStep(worker); + return `${label}: ${state} -- ${capacity}, ${leaseCount(worker)}${skew}${workerVersion(worker)}${host}${capabilities}${memory}${load}${workerBaselinesLine(worker)}${installs}`; }) .join("\n"); } +function memoryStep(worker: WorkerView): string { + if (worker.connection === "disconnected") return ""; + switch (worker.capacity?.memoryPressure?.step) { + case "warming-paused": + return " -- memory: warming paused"; + case "new-devices-paused": + return " -- memory: new devices paused"; + default: + return ""; + } +} + function workerLoadLine(worker: WorkerView, now: number): string { if (worker.connection === "disconnected") return "\n Load: no reading while disconnected"; if (worker.load === undefined) return ""; From 7d78e9cb3e56dac23fff67e122bee9965de691a7 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:19:13 +0100 Subject: [PATCH 7/9] docs(gateway): routing ranks a memory-paused worker last; worker list shows the step (#1364) --- src/gateway/routing/AGENTS.md | 1 + website/src/content/docs/docs/gateway/managing-workers.mdx | 1 + website/src/content/docs/docs/gateway/routing.mdx | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gateway/routing/AGENTS.md b/src/gateway/routing/AGENTS.md index 2d3fdb033..f75771217 100644 --- a/src/gateway/routing/AGENTS.md +++ b/src/gateway/routing/AGENTS.md @@ -11,6 +11,7 @@ Stages: - A filter returns its own reason and a rank its own zero reason. The pipeline and the policy name no stage and compare no stage name. - `gateway.routing` names a whole list; stage order is code, never config. +- `memory-pressure` reads only the step a worker reports (`new-devices-paused` scores 0, `warming-paused` ranks as calm); the gateway applies no threshold. - A rank abstains when its best score is 0 or less, and a settling rank ends the walk. A worker is picked only if some rank decided: filters alone pick no one. - Workers are walked in id order, and the pick is the first one left. diff --git a/website/src/content/docs/docs/gateway/managing-workers.mdx b/website/src/content/docs/docs/gateway/managing-workers.mdx index 50232fc16..ba721bb2d 100644 --- a/website/src/content/docs/docs/gateway/managing-workers.mdx +++ b/website/src/content/docs/docs/gateway/managing-workers.mdx @@ -27,6 +27,7 @@ Each worker's line shows: - devices running out of the limit per platform, and how many leases it holds; - its Simlock version, with the newest release in brackets when npm has a newer one, and `updating to : ` while it updates (the stage is `waiting for leases`, `installing` or `restarting`); - its operating system, platform tools and optional modules; +- `memory: warming paused` or `memory: new devices paused` while the worker reports host memory pressure at that step; nothing otherwise, and nothing for a disconnected worker; - under it, the last load reading, the baselines it holds, and any runtime or system image being installed. The `Baselines:` line gives each baseline's name and version, its device, and `stale` for one that cannot be used now, for example `Baselines: checkout v2: android Pixel 9, Android 36 (google_apis), full; smoke v1: ios iPhone 17, iOS 27.0, slim, stale`. It says `none` for a worker with no baselines, and is missing while a worker starts up. diff --git a/website/src/content/docs/docs/gateway/routing.mdx b/website/src/content/docs/docs/gateway/routing.mdx index 79ec7f3b5..f5cb65876 100644 --- a/website/src/content/docs/docs/gateway/routing.mdx +++ b/website/src/content/docs/docs/gateway/routing.mdx @@ -23,7 +23,7 @@ A worker can take a request only when it: Among those workers, the gateway prefers: 1. **A worker with a warm device.** A booted, unleased device that matches the request, including its slim or full mode, means the agent gets a device without waiting for a boot. For a request that names a baseline, only a copy of the baseline's current version counts. -2. **A worker with room to boot one.** It needs a free device slot, or a warm device it can shut down to make room. Workers under their RAM budget come first, then the worker with the most free device slots. +2. **A worker with room to boot one.** It needs a free device slot, or a warm device it can shut down to make room. Workers whose new devices are not paused by host memory pressure come first (a worker that only paused warming still counts as calm), then workers under their RAM budget, then the worker with the most free device slots. A worker that paused new devices still gets the request when it has a warm match or when every worker is paused. A request for a physical device goes only to a worker with a matching enrolled device that is free now. If none is free, the request waits. From f12dd06a5d9139a6bb54b97f5c044c62878fb81d Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:23:33 +0100 Subject: [PATCH 8/9] test: a worker that refused a request is asked again once memory.pressure-changed lifts the step (#1364) --- src/gateway/service.test.ts | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/gateway/service.test.ts b/src/gateway/service.test.ts index 2db0341b5..bd32b52b7 100644 --- a/src/gateway/service.test.ts +++ b/src/gateway/service.test.ts @@ -802,6 +802,67 @@ describe("GatewayService", () => { await harness.service.stop(); }); + it("asks a worker that refused a request again once its memory.pressure-changed lifts the step, and not before", async () => { + const harness = fleet(); + await harness.service.start(); + const coordinator = new FleetLeaseCoordinator({ + clock: harness.clock, + describeFailure: (error) => ({ code: "INTERNAL", message: String(error) }), + directory: harness.service, + eventBus: harness.eventBus, + execTimeoutMs: 11 * 60_000, + idGenerator: { + generate: (() => { + let next = 1; + return () => `${next++}`; + })(), + }, + leaseIndex: new FleetLeaseIndex("gw:instance-1:"), + leaseRequestLimits: { maxRecords: 10_000, retentionMs: 600_000 }, + leaseRequestTimeoutMs: 5 * 60_000, + routing: createRoutingPolicy("warm-then-free"), + views: harness.service.workers, + }); + const worker = new ScriptedWorkerClient(); + const capacity = statusFixture().capacity; + worker.status = statusFixture({ + capacity: { ...capacity, memoryPressure: { step: "new-devices-paused" } }, + }); + worker.catalog = catalogFixture([ + { models: ["iPhone 17"], platform: "ios", runtimes: ["26.0"] }, + ]); + await harness.join("wrk_1", worker); + await vi.waitFor(() => + expect(harness.service.workers.view("wrk_1")?.capacity?.memoryPressure?.step).toBe( + "new-devices-paused", + ), + ); + await vi.waitFor(() => expect(harness.service.workers.view("wrk_1")?.catalog).toHaveLength(1)); + const leaseRequests = () => worker.calls.filter((call) => call.startsWith("lease.request")); + + void coordinator + .request( + { model: "iPhone 17", platform: "ios" }, + { allowDownload: false, noWait: false, ownerId: "agent-1", requesterId: "agent-1" }, + ) + .catch(() => undefined); + await vi.waitFor(() => expect(leaseRequests()).toHaveLength(1)); + + worker.pushEvent({ event: "memory.pressure-changed" }); + await vi.waitFor(() => + expect(worker.calls.filter((c) => c === "status.get").length).toBeGreaterThan(1), + ); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(leaseRequests()).toHaveLength(1); + + worker.status = statusFixture({ capacity: { ...capacity, memoryPressure: { step: "none" } } }); + worker.pushEvent({ event: "memory.pressure-changed" }); + + await vi.waitFor(() => expect(leaseRequests()).toHaveLength(2)); + + await harness.service.stop(); + }); + it("does not re-read the fleet for an event that changes neither", async () => { const harness = fleet(); await harness.service.start(); From c8ffb03729625e7879ab002587985daf557afb6e Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 6 Nov 2026 19:42:48 +0100 Subject: [PATCH 9/9] fix: drop a restating manual line, raise the shrink-only size of cli/index.test.ts (#1364) --- src/gateway/routing/AGENTS.md | 1 - src/source-hygiene.test.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/gateway/routing/AGENTS.md b/src/gateway/routing/AGENTS.md index f75771217..2d3fdb033 100644 --- a/src/gateway/routing/AGENTS.md +++ b/src/gateway/routing/AGENTS.md @@ -11,7 +11,6 @@ Stages: - A filter returns its own reason and a rank its own zero reason. The pipeline and the policy name no stage and compare no stage name. - `gateway.routing` names a whole list; stage order is code, never config. -- `memory-pressure` reads only the step a worker reports (`new-devices-paused` scores 0, `warming-paused` ranks as calm); the gateway applies no threshold. - A rank abstains when its best score is 0 or less, and a settling rank ends the walk. A worker is picked only if some rank decided: filters alone pick no one. - Workers are walked in id order, and the pick is the first one left. diff --git a/src/source-hygiene.test.ts b/src/source-hygiene.test.ts index 5804a214d..ee567dd39 100644 --- a/src/source-hygiene.test.ts +++ b/src/source-hygiene.test.ts @@ -27,7 +27,7 @@ type SizedFile = { path: string; lines: number }; * only shrink. A PR that has to grow one raises its number here, as a visible line of its diff. */ const SHRINK_ONLY: Record = { - "src/cli/index.test.ts": 11637, + "src/cli/index.test.ts": 11674, "src/drivers/ios/index.test.ts": 5996, "src/drivers/ios/index.ts": 2700, "src/leasing/create-leasing.test.ts": 5276,