Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/HTTP-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/agent-device/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/agent-device/front/front.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 27 additions & 11 deletions src/agent-device/gateway-front.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -1955,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);
Expand All @@ -1975,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();
});

Expand Down
1 change: 0 additions & 1 deletion src/agent-device/shared/replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export function workerUnreachable(): FrontReply {

const unreachableAnswers = new WeakSet<Response>();

// 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;
Expand Down
Loading