From 6b5105f4e3e25c769cd68d5bc74ad7073615beb4 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 09:34:45 +0100 Subject: [PATCH 1/6] =?UTF-8?q?test:=20reproduce=20#984=20=E2=80=94=20a=20?= =?UTF-8?q?requester=20whose=20only=20lease=20is=20past=20its=20deadline?= =?UTF-8?q?=20on=20a=20disconnected=20worker=20is=20refused=20a=20new=20on?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/fleet-coordinator.test.ts | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/gateway/fleet-coordinator.test.ts b/src/gateway/fleet-coordinator.test.ts index 7cf2dec08..46f206b15 100644 --- a/src/gateway/fleet-coordinator.test.ts +++ b/src/gateway/fleet-coordinator.test.ts @@ -1836,6 +1836,45 @@ describe("drain and unreachable lifecycle (ADR §9/§28/§29, #119)", () => { expect((retry as RequesterAlreadyLeasedError).existingLeaseId).toBe(rebuiltLeaseId); expect(client.calls.filter((call) => call.startsWith("lease.request"))).toHaveLength(1); }); + + it("grants a requester a new lease once its only lease, on a disconnected worker, is past its last reported deadline, and keeps that lease routable to its worker (#984)", async () => { + const { clock, coordinator, directory, leaseIndex, workers } = harness(); + const clientA = new ScriptedWorkerClient(); + const clientB = new ScriptedWorkerClient(); + directory.add("wrk_a", clientA); + directory.add("wrk_b", clientB); + connectWorker(workers, "wrk_a", { + leases: [ + { + ...leaseFixture("lse_old", "dev_old"), + requesterId: `${GATEWAY_PREFIX}agent-1`, + ttlDeadline: 900_001, + }, + ], + }); + connectWorker(workers, "wrk_b"); + workers.disconnected("wrk_a"); + directory.unreachable.add("wrk_a"); + + clock.advance(900_000 - clock.now()); + const beforeDeadline = await coordinator + .request(REQUEST, requestOptions({ noWait: true })) + .catch((error: unknown) => error); + expect(beforeDeadline).toBeInstanceOf(RequesterAlreadyLeasedError); + expect((beforeDeadline as RequesterAlreadyLeasedError).existingLeaseId).toBe("wrk_a.lse_old"); + + clock.advance(2); + clientB.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" }); + const afterDeadline = await coordinator + .request(REQUEST, requestOptions({ noWait: true })) + .catch((error: unknown) => error); + + expect(afterDeadline).toMatchObject({ lease: { id: "wrk_b.lse_1", worker: { id: "wrk_b" } } }); + expect(leaseIndex.resolve("wrk_a.lse_old")).toMatchObject({ + requesterId: "agent-1", + workerId: "wrk_a", + }); + }); }); describe("FleetLeaseCoordinator sends a worker only requests its catalog can serve (ADR 0009 §3)", () => { From 69d39a3abdc63627caea051c6921682087fc4454 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 10:32:50 +0100 Subject: [PATCH 2/6] fix(gateway): a lease past its deadline on a disconnected worker no longer counts against the requester limit (#984) --- src/gateway/fleet-coordinator.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/gateway/fleet-coordinator.ts b/src/gateway/fleet-coordinator.ts index ee8a945d6..3f7464f71 100644 --- a/src/gateway/fleet-coordinator.ts +++ b/src/gateway/fleet-coordinator.ts @@ -309,7 +309,9 @@ export class FleetLeaseCoordinator { requestId: string, ): void { const refusal = requesterLimitDecision({ - held: this.options.leaseIndex.held(requesterId), + held: this.options.leaseIndex + .held(requesterId) + .filter((lease) => !this.#isPastDeadlineOnDisconnectedWorker(lease.id)), maxPerRequester: this.options.maxPerRequester, platform: deviceRequest.platform, probe: false, @@ -326,6 +328,20 @@ export class FleetLeaseCoordinator { throw refusal; } + /** + * why: a lease past its last reported deadline on a disconnected worker cannot have been renewed + * through this gateway, so it must not lock its requester out of the fleet. The entry stays in + * the index: it is still routed to its worker (ADR 0005 §28). + */ + #isPastDeadlineOnDisconnectedWorker(gatewayLeaseId: string): boolean { + const entry = this.options.leaseIndex.resolve(gatewayLeaseId); + if (entry === undefined) return false; + const view = this.options.views.view(entry.workerId); + if (view === undefined || view.connection !== "disconnected") return false; + const reported = view.leases?.find((lease) => lease.id === entry.workerLeaseId); + return reported !== undefined && reported.ttlDeadline <= this.options.clock.now(); + } + /** * ADR 0020: the one clash check on a gateway, after the one-lease check and in the same * admission section. A caller-chosen id is held by one of this gateway's leases and by one of From b2950a0b4699ff21bfb14469c682c76764a0d4b2 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 10:33:30 +0100 Subject: [PATCH 3/6] docs(gateway): the requester limit skips an expired lease on a disconnected worker (#984) --- src/gateway/AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gateway/AGENTS.md b/src/gateway/AGENTS.md index 45b364d69..de2da5d96 100644 --- a/src/gateway/AGENTS.md +++ b/src/gateway/AGENTS.md @@ -62,7 +62,7 @@ Flows: [device selection](../../docs/internal/flows/device-selection.md), [devic 1. `GatewayDispatcher` refuses `fleetRequestId` and, off an uplink session, `owner`. Fills `ttlMs` from the gateway's `lease.defaultTtlMs`; rejects one above `lease.maxTtlMs`. Drops `allowDownload`. -2. Admit: idempotent replay → `lease.maxPerRequester` (1; a number, or one per platform) over the index plus a pending request +2. Admit: idempotent replay → `lease.maxPerRequester` (1; a number, or one per platform) over the index (not counting a lease past its last reported deadline on a disconnected worker) plus a pending request (`already-leased`, `requesterLimitDecision`) → `leaseId` held by the index or an open request (`lease-id-taken`) → `requests.admit` stores it, creates the waiter. Emits `lease.requested`. 3. Walk (on admit, on any view change, after each settled attempt), oldest first: `assess` the From 69408df5c061fb91df3f69eb30af2c19ef1ae16e Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 10:39:34 +0100 Subject: [PATCH 4/6] test: straddle the #984 deadline, cover a connected worker, fail loudly --- src/gateway/fleet-coordinator.test.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/gateway/fleet-coordinator.test.ts b/src/gateway/fleet-coordinator.test.ts index 46f206b15..f7127364d 100644 --- a/src/gateway/fleet-coordinator.test.ts +++ b/src/gateway/fleet-coordinator.test.ts @@ -1837,7 +1837,7 @@ describe("drain and unreachable lifecycle (ADR §9/§28/§29, #119)", () => { expect(client.calls.filter((call) => call.startsWith("lease.request"))).toHaveLength(1); }); - it("grants a requester a new lease once its only lease, on a disconnected worker, is past its last reported deadline, and keeps that lease routable to its worker (#984)", async () => { + it("grants a requester a new lease once its only lease, on a disconnected worker, is past its last reported deadline (not one ms before it), still counts a lease past its deadline on a connected worker, and keeps the lease routable to its worker (#984)", async () => { const { clock, coordinator, directory, leaseIndex, workers } = harness(); const clientA = new ScriptedWorkerClient(); const clientB = new ScriptedWorkerClient(); @@ -1853,17 +1853,34 @@ describe("drain and unreachable lifecycle (ADR §9/§28/§29, #119)", () => { ], }); connectWorker(workers, "wrk_b"); + connectWorker(workers, "wrk_c", { + leases: [ + { + ...leaseFixture("lse_conn", "dev_conn"), + requesterId: `${GATEWAY_PREFIX}agent-2`, + ttlDeadline: 1, + }, + ], + }); workers.disconnected("wrk_a"); directory.unreachable.add("wrk_a"); + clientB.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" }); clock.advance(900_000 - clock.now()); const beforeDeadline = await coordinator .request(REQUEST, requestOptions({ noWait: true })) .catch((error: unknown) => error); expect(beforeDeadline).toBeInstanceOf(RequesterAlreadyLeasedError); expect((beforeDeadline as RequesterAlreadyLeasedError).existingLeaseId).toBe("wrk_a.lse_old"); + const connected = await coordinator + .request( + REQUEST, + requestOptions({ noWait: true, ownerId: "agent-2", requesterId: "agent-2" }), + ) + .catch((error: unknown) => error); + expect(connected).toBeInstanceOf(RequesterAlreadyLeasedError); - clock.advance(2); + clock.advance(1); clientB.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" }); const afterDeadline = await coordinator .request(REQUEST, requestOptions({ noWait: true })) From a89d7acf3740aef7ebcd3766fd58da54c01fc9a5 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 10:40:03 +0100 Subject: [PATCH 5/6] docs(gateway): an expired lease on a disconnected worker does not count against the limit (#984) --- website/src/content/docs/docs/gateway/routing.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/content/docs/docs/gateway/routing.mdx b/website/src/content/docs/docs/gateway/routing.mdx index 08d101009..27fa36d28 100644 --- a/website/src/content/docs/docs/gateway/routing.mdx +++ b/website/src/content/docs/docs/gateway/routing.mdx @@ -58,7 +58,7 @@ This check runs again while a request waits. If the last worker that could serve If a worker starts to boot a device for the request and then fails, the request fails with that worker's error. If every worker that could serve a request fails to create the device, the request fails with `PROVISION_FAILED` (exit 1). -A requester holds at most `lease.maxPerRequester` leases through the gateway (1 by default), counted across the whole fleet. When the limit is set per platform, each platform is counted on its own. Another request fails with `REQUESTER_ALREADY_LEASED` (exit 13). +A requester holds at most `lease.maxPerRequester` leases through the gateway (1 by default), counted across the whole fleet. When the limit is set per platform, each platform is counted on its own. Another request fails with `REQUESTER_ALREADY_LEASED` (exit 13). A lease on a worker that is disconnected does not count once its last reported deadline has passed, because no renew can have reached it through the gateway. All codes are listed in [Error reference](/simlock/docs/reference/errors). From 3d5f29f3cab29eae1464d706db8af0e7ca916af4 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 10:41:26 +0100 Subject: [PATCH 6/6] test: a lease its disconnected worker's last view does not list still counts against the requester limit (#984) --- src/gateway/fleet-coordinator.test.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/gateway/fleet-coordinator.test.ts b/src/gateway/fleet-coordinator.test.ts index f7127364d..3eb03b612 100644 --- a/src/gateway/fleet-coordinator.test.ts +++ b/src/gateway/fleet-coordinator.test.ts @@ -1837,7 +1837,7 @@ describe("drain and unreachable lifecycle (ADR §9/§28/§29, #119)", () => { expect(client.calls.filter((call) => call.startsWith("lease.request"))).toHaveLength(1); }); - it("grants a requester a new lease once its only lease, on a disconnected worker, is past its last reported deadline (not one ms before it), still counts a lease past its deadline on a connected worker, and keeps the lease routable to its worker (#984)", async () => { + it("grants a requester a new lease once its only lease, on a disconnected worker, is past its last reported deadline (not one ms before it), still counts a lease past its deadline on a connected worker, and keeps the lease routable to its worker, and still counts a lease its worker's last view does not list (#984)", async () => { const { clock, coordinator, directory, leaseIndex, workers } = harness(); const clientA = new ScriptedWorkerClient(); const clientB = new ScriptedWorkerClient(); @@ -1891,6 +1891,25 @@ describe("drain and unreachable lifecycle (ADR §9/§28/§29, #119)", () => { requesterId: "agent-1", workerId: "wrk_a", }); + + connectWorker(workers, "wrk_d"); + leaseIndex.add({ + gatewayLeaseId: "wrk_d.lse_unlisted", + grantedAt: 1, + ownerId: "agent-3", + requesterId: "agent-3", + workerId: "wrk_d", + workerLeaseId: "lse_unlisted", + }); + workers.disconnected("wrk_d"); + const unlisted = await coordinator + .request( + REQUEST, + requestOptions({ noWait: true, ownerId: "agent-3", requesterId: "agent-3" }), + ) + .catch((error: unknown) => error); + expect(unlisted).toBeInstanceOf(RequesterAlreadyLeasedError); + expect((unlisted as RequesterAlreadyLeasedError).existingLeaseId).toBe("wrk_d.lse_unlisted"); }); });