Skip to content

Commit 887243c

Browse files
committed
fix(daemon): keep an idle daemon alive only for retained leases
Idle reap now waits only while an unexpired retainOnClose lease is active, through LeaseRegistry.hasRetainedLeases, which also expires leases past their window. Other leases, including human-control holds with no expiry, reap as before; ADR 0007 says so. A lifecycle-shutdown test restores the coverage that a session lease is released when its session teardown rejects.
1 parent 2b0737e commit 887243c

7 files changed

Lines changed: 74 additions & 17 deletions

File tree

‎docs/adr/0007-remote-device-leases.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ its provider device in place, and only `leases.release`, expiry, or daemon
4848
shutdown ends it. The default is unchanged so the CLI's proxy sharing still
4949
frees devices on `close`. The allocated lease reports `retainOnClose: true`
5050
only when the daemon honored it, and only the lease's own client can turn it
51-
on for a lease the run already holds. An unexpired lease keeps an idle daemon
52-
alive; one the caller stops heartbeating expires after its `ttlMs`, and idle
51+
on for a lease the run already holds. An unexpired `retainOnClose` lease keeps
52+
an idle daemon alive; other leases, including human-control holds, do not. A
53+
retained lease the caller stops heartbeating expires after its `ttlMs`, and idle
5354
reap then shuts the daemon down within one more idle window.
5455

5556
## Consequences

‎src/daemon/__tests__/lease-registry.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ test('a request without the owning clientId cannot turn retainOnClose on for a r
7171
assert.equal(reused.retainOnClose, undefined);
7272
});
7373

74+
test('only an unexpired retainOnClose lease counts as retained', () => {
75+
let now = 1_000;
76+
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 10_000 });
77+
registry.allocateLease({ tenantId: 'tenant-a', runId: 'run-plain' });
78+
assert.equal(registry.hasRetainedLeases(), false);
79+
80+
registry.allocateLease({ tenantId: 'tenant-a', runId: 'run-retained', retainOnClose: true });
81+
assert.equal(registry.hasRetainedLeases(), true);
82+
83+
now = 20_000;
84+
assert.equal(registry.hasRetainedLeases(), false);
85+
assert.deepEqual(registry.listActiveLeases(), []);
86+
});
87+
7488
test('heartbeatLease extends active lease and releaseLease is idempotent', () => {
7589
let now = 1_000;
7690
const registry = new LeaseRegistry({

‎src/daemon/lease-registry.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ export class LeaseRegistry {
182182
assertLeaseScopeMatch(this.getActiveLease(scope.leaseId), scope);
183183
}
184184

185+
/**
186+
* Whether an unexpired `retainOnClose` lease outlives its sessions. Reading it also expires leases
187+
* past their window, so an abandoned retained lease still ends.
188+
*/
189+
hasRetainedLeases(): boolean {
190+
return this.listActiveLeases().some((lease) => lease.retainOnClose === true);
191+
}
192+
185193
listActiveLeases(): DeviceLease[] {
186194
this.cleanupExpiredLeases();
187195
return Array.from(this.leases.values()).map((entry) => ({ ...entry }));

‎src/daemon/server/daemon-idle-reap.test.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ test('idle reap fires after the idle window when nothing is using the daemon', a
149149
const idleReap = createDaemonIdleReap({
150150
sessionStore,
151151
getInFlightRequestCount: () => 0,
152-
hasActiveLeases: () => false,
152+
hasRetainedLeases: () => false,
153153
onIdleReap: () => {
154154
reaped++;
155155
},
@@ -164,14 +164,14 @@ test('idle reap fires after the idle window when nothing is using the daemon', a
164164
assert.equal(reaped, 1);
165165
});
166166

167-
test('idle reap waits another window while a lease no session holds is still active', async () => {
167+
test('idle reap waits another window while a retained lease is still active', async () => {
168168
vi.useFakeTimers();
169169
let reaped = 0;
170-
let leaseActive = true;
170+
let retainedLeaseActive = true;
171171
const idleReap = createDaemonIdleReap({
172172
sessionStore,
173173
getInFlightRequestCount: () => 0,
174-
hasActiveLeases: () => leaseActive,
174+
hasRetainedLeases: () => retainedLeaseActive,
175175
onIdleReap: () => {
176176
reaped++;
177177
},
@@ -181,7 +181,7 @@ test('idle reap waits another window while a lease no session holds is still act
181181
idleReap.noteActivity();
182182
await vi.advanceTimersByTimeAsync(120);
183183
assert.equal(reaped, 0);
184-
leaseActive = false;
184+
retainedLeaseActive = false;
185185
await vi.advanceTimersByTimeAsync(40);
186186

187187
assert.equal(reaped, 1);
@@ -194,7 +194,7 @@ test('idle reap does not fire while a session is open', async () => {
194194
const idleReap = createDaemonIdleReap({
195195
sessionStore,
196196
getInFlightRequestCount: () => 0,
197-
hasActiveLeases: () => false,
197+
hasRetainedLeases: () => false,
198198
onIdleReap: () => {
199199
reaped++;
200200
},
@@ -223,7 +223,7 @@ test('idle reap does not fire while a recording is active', async () => {
223223
const idleReap = createDaemonIdleReap({
224224
sessionStore,
225225
getInFlightRequestCount: () => 0,
226-
hasActiveLeases: () => false,
226+
hasRetainedLeases: () => false,
227227
onIdleReap: () => {
228228
reaped++;
229229
},
@@ -243,7 +243,7 @@ test('idle reap does not fire while a request is in flight', async () => {
243243
const idleReap = createDaemonIdleReap({
244244
sessionStore,
245245
getInFlightRequestCount: () => inFlightRequestCount,
246-
hasActiveLeases: () => false,
246+
hasRetainedLeases: () => false,
247247
onIdleReap: () => {
248248
reaped++;
249249
},
@@ -266,7 +266,7 @@ test('idle reap is disabled when the window is zero', async () => {
266266
const idleReap = createDaemonIdleReap({
267267
sessionStore,
268268
getInFlightRequestCount: () => 0,
269-
hasActiveLeases: () => false,
269+
hasRetainedLeases: () => false,
270270
onIdleReap: () => {
271271
reaped++;
272272
},
@@ -285,7 +285,7 @@ test('cancel prevents a scheduled reap from firing', async () => {
285285
const idleReap = createDaemonIdleReap({
286286
sessionStore,
287287
getInFlightRequestCount: () => 0,
288-
hasActiveLeases: () => false,
288+
hasRetainedLeases: () => false,
289289
onIdleReap: () => {
290290
reaped++;
291291
},

‎src/daemon/server/daemon-idle-reap.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export function createDaemonIdleReap(params: {
8484
sessionStore: SessionStore;
8585
getInFlightRequestCount: () => number;
8686
/**
87-
* An unexpired lease is a client's claim on this daemon, so a reap that finds one waits another
88-
* idle window. The check must also expire leases past their window, or none would ever end.
87+
* An unexpired `retainOnClose` lease is a caller's claim on this daemon beyond its sessions, so a
88+
* reap that finds one waits another idle window. The check must also expire leases past their
89+
* window, or none would ever end.
8990
*/
90-
hasActiveLeases: () => boolean;
91+
hasRetainedLeases: () => boolean;
9192
onIdleReap: () => void;
9293
env?: NodeJS.ProcessEnv;
9394
}): DaemonIdleReapController {
@@ -115,7 +116,7 @@ export function createDaemonIdleReap(params: {
115116
// session open or new request must never lose a race against a
116117
// previously scheduled reap.
117118
if (!isIdleNow()) return;
118-
if (params.hasActiveLeases()) {
119+
if (params.hasRetainedLeases()) {
119120
schedule();
120121
return;
121122
}

‎src/daemon/server/daemon-runtime-lifecycle-shutdown.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,39 @@ test.each([false, true])(
399399
},
400400
);
401401

402+
test('daemon shutdown releases a session lease whose session teardown rejects', async () => {
403+
const stateDir = mkdtempForTestSync('agent-device-daemon-session-lease-shutdown-');
404+
const runtime = await startDaemonRuntime({
405+
env: {
406+
...process.env,
407+
AGENT_DEVICE_STATE_DIR: stateDir,
408+
AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS: '0',
409+
AGENT_DEVICE_DAEMON_SERVER_MODE: 'http',
410+
},
411+
exit: () => {},
412+
registerProcessHandlers: false,
413+
stderr: { write: () => {} },
414+
stdout: { write: () => {} },
415+
});
416+
const [leaseRegistry] = leaseProbe.registries;
417+
const lease = leaseRegistry!.allocateLease({
418+
tenantId: 'tenant-a',
419+
runId: 'run-1',
420+
leaseProvider: 'limrun',
421+
});
422+
shutdownProbe.store!.publish('bound', {
423+
...makeIosSession('bound'),
424+
lease: { leaseId: lease.leaseId, tenantId: lease.tenantId, runId: lease.runId },
425+
});
426+
shutdownProbe.finalize.mockRejectedValueOnce(new Error('teardown failed'));
427+
428+
await runtime?.shutdown();
429+
430+
expect(shutdownProbe.finalize).toHaveBeenCalledOnce();
431+
expect(leaseProbe.released).toEqual([lease]);
432+
expect(leaseRegistry!.listActiveLeases()).toEqual([]);
433+
});
434+
402435
test('daemon shutdown releases a retainOnClose lease that no session holds', async () => {
403436
const stateDir = mkdtempForTestSync('agent-device-daemon-retained-lease-shutdown-');
404437
try {

‎src/daemon/server/daemon-runtime.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ export async function startDaemonRuntime(
579579
const idleReap = createDaemonIdleReap({
580580
sessionStore,
581581
getInFlightRequestCount: () => inFlightRequests.size,
582-
hasActiveLeases: () => leaseRegistry.listActiveLeases().length > 0,
582+
hasRetainedLeases: () => leaseRegistry.hasRetainedLeases(),
583583
onIdleReap: () => {
584584
void shutdown();
585585
},

0 commit comments

Comments
 (0)