Skip to content

Commit e0d5f4c

Browse files
committed
fix(daemon): bound a retained lease's idle hold by its own expiry
hasRetainedLeases counts a retainOnClose lease only while its own expiresAt is in the future, so a human-control hold (or admitted work) that keeps a past-due lease registered cannot keep an idle daemon alive forever. Tests: a registry case with a hold on the retained lease's device, and a runtime case where a plain lease does not block self-reap but a retained one does.
1 parent 887243c commit e0d5f4c

3 files changed

Lines changed: 87 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ test('only an unexpired retainOnClose lease counts as retained', () => {
8282

8383
now = 20_000;
8484
assert.equal(registry.hasRetainedLeases(), false);
85-
assert.deepEqual(registry.listActiveLeases(), []);
8685
});
8786

8887
test('heartbeatLease extends active lease and releaseLease is idempotent', () => {
@@ -424,6 +423,22 @@ test('human holds protect leases and release refreshes the original lease TTL at
424423
assert.equal(registry.consumeExpiredLeases()[0]?.leaseId, lease.leaseId);
425424
});
426425

426+
test('a human-control hold keeps a past-due retained lease registered but not retained', async () => {
427+
let now = 1_000;
428+
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 5_000 });
429+
const lease = registry.allocateLease({
430+
...HUMAN_CONTROL_LEASE_REQUEST,
431+
ttlMs: 10_000,
432+
retainOnClose: true,
433+
});
434+
await registry.putHumanControlHold({ kind: 'lease', leaseId: lease.leaseId }, 'console', {});
435+
assert.equal(registry.hasRetainedLeases(), true);
436+
437+
now = 25_000;
438+
assert.equal(registry.listActiveLeases()[0]?.leaseId, lease.leaseId);
439+
assert.equal(registry.hasRetainedLeases(), false);
440+
});
441+
427442
test('hold expiry refreshes from the expiry instant, without reviving abandoned leases', async () => {
428443
let now = 0;
429444
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 5_000 });

‎src/daemon/lease-registry.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,15 @@ export class LeaseRegistry {
183183
}
184184

185185
/**
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.
186+
* Whether a `retainOnClose` lease is still inside its own window. A human-control hold or admitted
187+
* work can keep a past-due lease registered, but only the lease's `expiresAt` bounds how long it
188+
* keeps an idle daemon alive.
188189
*/
189190
hasRetainedLeases(): boolean {
190-
return this.listActiveLeases().some((lease) => lease.retainOnClose === true);
191+
const now = this.now();
192+
return this.listActiveLeases().some(
193+
(lease) => lease.retainOnClose === true && lease.expiresAt > now,
194+
);
191195
}
192196

193197
listActiveLeases(): DeviceLease[] {

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import { afterEach, test, vi } from 'vitest';
4+
5+
const leaseProbe = vi.hoisted(() => ({
6+
registries: [] as import('../lease-registry.ts').LeaseRegistry[],
7+
}));
8+
vi.mock('../lease-registry.ts', async (importOriginal) => {
9+
const actual = await importOriginal<typeof import('../lease-registry.ts')>();
10+
class RecordedLeaseRegistry extends actual.LeaseRegistry {
11+
constructor(...args: ConstructorParameters<typeof actual.LeaseRegistry>) {
12+
super(...args);
13+
leaseProbe.registries.push(this);
14+
}
15+
}
16+
return { ...actual, LeaseRegistry: RecordedLeaseRegistry };
17+
});
18+
419
import { resolveDaemonPaths } from '../../daemon-resolution.ts';
520
import { startDaemonRuntime } from './daemon-runtime.ts';
621
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
722

823
afterEach(() => {
924
vi.useRealTimers();
25+
leaseProbe.registries.length = 0;
1026
});
1127

1228
test('daemon runtime self-reaps after the idle window when nothing ever uses it', async () => {
@@ -85,3 +101,51 @@ test('daemon runtime never self-reaps when AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS i
85101
fs.rmSync(stateDir, { recursive: true, force: true });
86102
}
87103
});
104+
105+
test('only a retained lease keeps the daemon runtime from self-reaping', async () => {
106+
vi.useFakeTimers();
107+
const stateDir = mkdtempForTestSync('agent-device-daemon-idle-reap-leases-');
108+
let exitCode: number | undefined;
109+
let resolveExit: () => void;
110+
const exited = new Promise<void>((resolve) => {
111+
resolveExit = resolve;
112+
});
113+
114+
try {
115+
const runtime = await startDaemonRuntime({
116+
env: {
117+
...process.env,
118+
AGENT_DEVICE_STATE_DIR: stateDir,
119+
AGENT_DEVICE_DAEMON_SERVER_MODE: 'http',
120+
AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS: '80',
121+
},
122+
exit: (code) => {
123+
exitCode = code;
124+
resolveExit();
125+
},
126+
registerProcessHandlers: false,
127+
stderr: { write: () => {} },
128+
stdout: { write: () => {} },
129+
});
130+
assert.notEqual(runtime, null);
131+
const [registry] = leaseProbe.registries;
132+
registry!.allocateLease({ tenantId: 'tenant-a', runId: 'run-plain', ttlMs: 60_000 });
133+
const retained = registry!.allocateLease({
134+
tenantId: 'tenant-a',
135+
runId: 'run-retained',
136+
ttlMs: 60_000,
137+
retainOnClose: true,
138+
});
139+
140+
await vi.advanceTimersByTimeAsync(80);
141+
assert.equal(exitCode, undefined);
142+
143+
registry!.releaseLease({ leaseId: retained.leaseId });
144+
await vi.advanceTimersByTimeAsync(80);
145+
await vi.runOnlyPendingTimersAsync();
146+
await exited;
147+
assert.equal(exitCode, 0);
148+
} finally {
149+
fs.rmSync(stateDir, { recursive: true, force: true });
150+
}
151+
});

0 commit comments

Comments
 (0)