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 diff --git a/src/gateway/fleet-coordinator.test.ts b/src/gateway/fleet-coordinator.test.ts index 7cf2dec08..3eb03b612 100644 --- a/src/gateway/fleet-coordinator.test.ts +++ b/src/gateway/fleet-coordinator.test.ts @@ -1836,6 +1836,81 @@ 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 (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(); + 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"); + 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(1); + 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", + }); + + 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"); + }); }); describe("FleetLeaseCoordinator sends a worker only requests its catalog can serve (ADR 0009 §3)", () => { 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 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).