From 1d84007870d8f784fb37643d60f6f2b82c4d697c Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Mon, 19 Oct 2026 11:11:25 +0200 Subject: [PATCH 1/2] test: reproduce lease renewals after an unreachable command reply --- src/agent-device/gateway-front.test.ts | 28 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/agent-device/gateway-front.test.ts b/src/agent-device/gateway-front.test.ts index 7ddb9a9ab..808a43fab 100644 --- a/src/agent-device/gateway-front.test.ts +++ b/src/agent-device/gateway-front.test.ts @@ -1883,20 +1883,32 @@ describe( expect(beat.result).toMatchObject({ ok: true }); }); - it("the uplink dropping mid-command answers the command with `workerUnreachable()`", async () => { + it("a command answered WORKER_UNREACHABLE after an uplink drop stops renewing its lease after reconnect without releasing it", async () => { const rig = await setUp(); const member = first(rig); - await granted(rig, 1); + await granted(rig, 1, { ttlMs: 30_000 }); await listedByGateway(rig, 1); + member.fake.keepCommandsAfterAbort(); const running = await runningCommand(rig, member, 1); + const before = await deadlineOf(member, 1); + expect(before).toBeTypeOf("number"); + const tap = tapDispatch(rig.gateway); - await dropUplink(rig, member); + try { + await dropUplink(rig, member); - await vi.waitFor(() => expect(running.done()).toBe(true), { timeout: 5_000 }); - member.fake.releaseCommand(); - const answer = await running.promise; - expect(answer.status).toBe(503); - expectUnreachable(await envelopeOf(answer)); + await vi.waitFor(() => expect(running.done()).toBe(true), { timeout: 5_000 }); + const answer = await running.promise; + expect(answer.status).toBe(503); + expectUnreachable(await envelopeOf(answer)); + await untilConnected(rig, member); + await advance(rig, 20_000, 1_000); + + expect(tap.operations).not.toContain("lease.release"); + expect(await deadlineOf(member, 1)).toBe(before); + } finally { + member.fake.releaseCommand(); + } }); it("right after the worker reconnects and before its view is refreshed, a command answers `workerUnreachable()`; once it is refreshed, within the TTL, the next command reaches it, and the next heartbeat renews the lease", async () => { From 9efdb46988cbc7be0fba2ba865be4b3e6ed8e1fb Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Mon, 19 Oct 2026 11:24:40 +0200 Subject: [PATCH 2/2] fix(agent-device): stop renewing unreachable commands (#545) 1 failing -> 0 failing. End only the answered command renewal reference; preserve worker work and heartbeat recovery. --- docs/HTTP-API.md | 7 ++++++- src/agent-device/AGENTS.md | 6 ++++-- src/agent-device/front/front.ts | 5 ++++- src/agent-device/gateway-front.test.ts | 10 +++++++--- src/agent-device/shared/replies.ts | 1 - 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/HTTP-API.md b/docs/HTTP-API.md index d194ded30..70050f23f 100644 --- a/docs/HTTP-API.md +++ b/docs/HTTP-API.md @@ -1670,7 +1670,12 @@ agent-device session with an agent on a worker's own tokens. Through a gateway: `/rpc` answer carries `details.reason` `WORKER_UNREACHABLE`. Nothing is sent to the worker and the Simlock lease is untouched; once the worker is back and read, before the lease ran out, the next request works. A command running - when the connection drops answers the same. The same answer covers a lease + when the connection drops answers the same and stops its periodic lease + renewals, with one final renewal attempt when no other command is awaiting + a reply. This does not release the lease + or end the command on the worker. Other commands still awaiting replies + keep renewing; a later heartbeat can renew before the lease expires. + The same answer covers a lease the gateway does not know yet in the first 60 seconds after it starts. - **A gateway restart keeps the agent's device.** Once the worker reconnects, the agent's next request of any kind works on the same device, with no diff --git a/src/agent-device/AGENTS.md b/src/agent-device/AGENTS.md index 55e2b1f4f..c3ce18e54 100644 --- a/src/agent-device/AGENTS.md +++ b/src/agent-device/AGENTS.md @@ -108,8 +108,10 @@ under leasing's signal (`lease.endWaitMs`). chars with a hash. A lease not the token's → 403. No lease: only `GET /health`. 4. Worker: `agentDeviceRelayTarget` → `InProcessHostLink` → host, requester = tenant. No front rules run on the worker. -5. Worker unreachable → "try again" reply; a command so answered keeps its renewals - to the run cap. A dropped lease is still routed for the gateway's `lease.maxTtlMs`; +5. Worker unreachable → "try again" reply; a command so answered ends its renewal + reference without releasing the lease or ending worker work. Other live commands + keep their references; the last reference ends with one final renewal attempt. + A dropped lease is still routed for the gateway's `lease.maxTtlMs`; for 60 s after gateway start an unknown lease is unreachable. ## State and timers diff --git a/src/agent-device/front/front.ts b/src/agent-device/front/front.ts index 455fe22b0..8b1005172 100644 --- a/src/agent-device/front/front.ts +++ b/src/agent-device/front/front.ts @@ -167,7 +167,10 @@ async function runCommand( end(); throw error; } - if (isUnreachableAnswer(response)) return response; + if (isUnreachableAnswer(response)) { + end(); + return response; + } releaseEndedLeases(context, identity, response); return untilBodyEnds(response, end); } diff --git a/src/agent-device/gateway-front.test.ts b/src/agent-device/gateway-front.test.ts index 808a43fab..f313130d5 100644 --- a/src/agent-device/gateway-front.test.ts +++ b/src/agent-device/gateway-front.test.ts @@ -1967,16 +1967,18 @@ describe( expectUnreachable(await envelopeOf(await heartbeat(rig, 1))); }); - it("a running command's renewal tick while the worker is disconnected fails, and the entry and its timer stay; after the reconnect the next tick renews", async () => { + it("an unreachable command's final renewal fails while disconnected; after reconnect only a new heartbeat renews the retained lease", async () => { // why: a worker clock of its own, so moving the gateway's clock does not make the worker redial. const rig = await setUp({ workers: [{ clockSkewMs: 0, label: "w1" }] }); const member = first(rig); await granted(rig, 1, { ttlMs: 30_000 }); await listedByGateway(rig, 1); - await runningCommand(rig, member, 1); + const running = await runningCommand(rig, member, 1); const tap = tapDispatch(rig.gateway); const before = await deadlineOf(member, 1); + rig.clock.advance(1_000); await dropUplink(rig, member); + expectUnreachable(await envelopeOf(await running.promise)); rig.clock.advance(10_000); await sleep(50); @@ -1987,8 +1989,10 @@ describe( await untilConnected(rig, member); await advance(rig, 10_000, 5_000); await sleep(50); - expect(await deadlineOf(member, 1)).toBeGreaterThan(before ?? 0); + expect(before).toBe(31_000); + expect(await deadlineOf(member, 1)).toBe(before); expect((await envelopeOf(await heartbeat(rig, 1))).result).toMatchObject({ ok: true }); + expect(await deadlineOf(member, 1)).toBe(member.clock.now() + 30_000); member.fake.releaseCommand(); }); diff --git a/src/agent-device/shared/replies.ts b/src/agent-device/shared/replies.ts index 5fd48dfc3..a68ad9455 100644 --- a/src/agent-device/shared/replies.ts +++ b/src/agent-device/shared/replies.ts @@ -39,7 +39,6 @@ export function workerUnreachable(): FrontReply { const unreachableAnswers = new WeakSet(); -// why: a command answered "try again" may still run on its worker, so the front keeps renewing its lease until the run limit; this tells that answer from any other. export function markUnreachableAnswer(response: Response): Response { unreachableAnswers.add(response); return response;