Skip to content

Commit 980687d

Browse files
committed
fix(daemon-client): stop the lease beat when the provider instance ended
A provider-instance-ended heartbeat was a transient beat error, so an upload kept running against an ended instance. Document that an attached instance that ends while reconnecting is recovered when its lease window expires.
1 parent e037bb0 commit 980687d

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎packages/daemon-client/src/__tests__/daemon-client-lease-beat.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,27 @@ describe('runProtectedLeaseWork', () => {
295295
});
296296
}
297297

298+
test('a beat that finds the provider instance ended ends the phase with that error', async () => {
299+
vi.useFakeTimers();
300+
const heartbeat = vi.fn(async () => {
301+
throw new AppError('COMMAND_FAILED', 'The Limrun instance ended (UserRequested).', {
302+
reason: 'provider-instance-ended',
303+
});
304+
});
305+
const upload = deferred<string>();
306+
const running = runProtectedLeaseWork({ task: () => upload.promise, heartbeat });
307+
308+
const rejected = assert.rejects(
309+
running,
310+
(error: unknown) =>
311+
error instanceof AppError && error.details?.reason === 'provider-instance-ended',
312+
);
313+
await vi.advanceTimersByTimeAsync(0);
314+
await rejected;
315+
assert.equal(heartbeat.mock.calls.length, 1, 'an ended instance is not beaten again');
316+
upload.resolve('too late');
317+
});
318+
298319
for (const reason of ['LEASE_SCOPE_REQUIRED', 'LEASE_SCOPE_MISMATCH']) {
299320
test(`a beat refused ${reason} ends the phase instead of beating to the lease's death`, async () => {
300321
vi.useFakeTimers();

‎packages/daemon-client/src/daemon-client-lease-beat.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { resolveCommandRequestTimeoutMs } from '@agent-device/command-registry/t
77
import {
88
MIN_LEASE_WINDOW_MS,
99
isInactiveLeaseError,
10+
isProviderInstanceEndedError,
1011
leaseScopeFromRequest,
1112
leaseScopeToRequestMeta,
1213
type LeaseScope,
@@ -49,7 +50,7 @@ const UNRENEWABLE_LEASE_BEAT_REASONS: ReadonlySet<unknown> = new Set([
4950

5051
/**
5152
* Whether a beat failed for a reason every successor will repeat: the lease is gone (the shared
52-
* taxonomy), the daemon refused a fact baked into the beat itself, or this request can never renew
53+
* taxonomy), the provider reported the device instance behind it ended, the daemon refused a fact baked into the beat itself, or this request can never renew
5354
* it. A beat's scope and ttl never change across the phase, so an `INVALID_ARGS` refusal — an
5455
* out-of-range ttl, an unusable lease id — is terminal without waiting out a window it can no
5556
* longer renew.
@@ -60,6 +61,7 @@ const UNRENEWABLE_LEASE_BEAT_REASONS: ReadonlySet<unknown> = new Set([
6061
function isTerminalLeaseBeatError(error: unknown): boolean {
6162
return (
6263
isInactiveLeaseError(error) ||
64+
isProviderInstanceEndedError(error) ||
6365
(error instanceof AppError &&
6466
(UNRENEWABLE_LEASE_BEAT_REASONS.has(error.details?.reason) || error.code === 'INVALID_ARGS'))
6567
);

‎website/docs/docs/limrun.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Limrun ends an instance after its inactivity timeout, which an idle session can
5656
Two timers can end an idle Limrun session.
5757

5858
- The session lease. Each command that uses the lease renews it for five minutes. After five minutes without such a command the lease expires, and the daemon ends the session. The next command that uses the lease allocates a new lease, and the app you opened is not restored.
59-
- The Limrun instance. An instance that agent-device creates ends after five minutes without activity, the same window as the lease. An instance you attach to keeps the inactivity timeout it was created with. When a created instance ends, the next command reports why it ended. Run `install` again to start a new instance and reinstall the app, then run `open`. For an attached instance, replace its connection details before you open again.
59+
- The Limrun instance. An instance that agent-device creates ends after five minutes without activity, the same window as the lease. An instance you attach to keeps the inactivity timeout it was created with. When a created instance ends, the next command reports why it ended. Run `install` again to start a new instance and reinstall the app, then run `open`. For an attached instance, replace its connection details before you open again. agent-device reports an attached instance as ended only after its connection is closed for good. If the instance ends while the connection is still reconnecting, the lease ends when its five-minute window expires.
6060

6161
`LIMRUN_KEEP_ALIVE` keeps the instance running past five minutes. It does not renew the lease.
6262

0 commit comments

Comments
 (0)