Skip to content

Commit 1641aa5

Browse files
thymikeeclaude
andcommitted
test(daemon): prove a refused host leaves the state dir's lease records untouched
A host started on a held state directory is refused, and the running host's live journal stays byte-identical with no expired record created; the test fails when recovery runs before the registration. A source-order check pins recoverInherited after the refusal branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9c93b9e commit 1641aa5

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎packages/daemon/src/server/__tests__/provider-lease-expiry.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,13 @@ test('a daemon refused the registration leaves the live journal byte-identical a
378378
fs.rmSync(stateDir, { recursive: true, force: true });
379379
}
380380
});
381+
382+
test('daemon startup recovers inherited lease records only after it holds the registration', () => {
383+
const source = fs.readFileSync(new URL('../http/daemon-runtime.ts', import.meta.url), 'utf8');
384+
const refused = source.indexOf("reason: 'daemon_registration_refused'");
385+
const recovery = source.indexOf('expiredProviderLeaseReleaser.recoverInherited(');
386+
387+
expect(refused).toBeGreaterThanOrEqual(0);
388+
expect(recovery).toBeGreaterThan(refused);
389+
expect(source.indexOf('expiredProviderLeaseReleaser.retryPending()')).toBe(-1);
390+
});

‎src/host.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// for `process.exit`, would take the test runner down with it.
55

66
import fs from 'node:fs';
7+
import path from 'node:path';
78
import { expect, test, vi } from 'vitest';
89
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
910
import { readDaemonStopIdentity } from '@agent-device/daemon-contracts/daemon-registration';
@@ -365,6 +366,42 @@ test('a host never takes a state directory somebody else holds', async () => {
365366
}
366367
});
367368

369+
test('a host refused a held state directory leaves its provider lease records untouched', async () => {
370+
const held = await bootHost();
371+
// The running host's crash journal, as it would hold a rented device's lease.
372+
const journalPath = path.join(held.paths.baseDir, 'live-provider-leases.json');
373+
const journal = `${JSON.stringify({
374+
version: 1,
375+
leases: [
376+
{
377+
leaseId: 'lease-held',
378+
tenantId: 'tenant',
379+
runId: 'run',
380+
backend: 'ios-instance',
381+
leaseProvider: 'limrun',
382+
createdAt: 1,
383+
heartbeatAt: 1,
384+
expiresAt: 2,
385+
},
386+
],
387+
})}\n`;
388+
fs.writeFileSync(journalPath, journal);
389+
try {
390+
await expect(
391+
startAgentDevice({ stateDir: held.stateDir, host: createTestHostEnvironment() }),
392+
).rejects.toMatchObject({ details: { reason: 'daemon_registration_refused' } });
393+
394+
// Only the registered daemon may read, move or act on the lease records of its state directory.
395+
expect(fs.readFileSync(journalPath, 'utf8')).toBe(journal);
396+
expect(fs.existsSync(path.join(held.paths.baseDir, 'expired-provider-leases.json'))).toBe(
397+
false,
398+
);
399+
} finally {
400+
await held.host.close();
401+
removeStateDir(held.stateDir);
402+
}
403+
});
404+
368405
/**
369406
* The signals this code sent at a process. `0` is a liveness probe and is not one; a call with no
370407
* signal argument is SIGTERM, so an omitted signal counts as the signal it really is.

0 commit comments

Comments
 (0)