Skip to content

Commit 95e6b33

Browse files
thymikeeclaude
andcommitted
fix(daemon): report an unreadable lease record with its path in the daemon log
The read now happens at startup, where emitDiagnostic has no scope, so the warning goes through the startup diagnostics and names the record's path; an unreadable record is left on disk for an operator. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ecd2caf commit 95e6b33

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,31 @@ test('daemon startup recovers inherited lease records only after it holds the re
388388
expect(recovery).toBeGreaterThan(refused);
389389
expect(source.indexOf('expiredProviderLeaseReleaser.retryPending()')).toBe(-1);
390390
});
391+
392+
test('an unreadable live journal is reported with its path and left on disk', async () => {
393+
const stateDir = mkdtempForTestSync('agent-device-live-provider-lease-');
394+
const journalPath = `${stateDir}/live-provider-leases.json`;
395+
fs.writeFileSync(journalPath, JSON.stringify({ version: 99, leases: [] }));
396+
const recoverExpiredLease = vi.fn(async () => undefined);
397+
const daemon = createExpiredProviderLeaseReleaser({
398+
recoverExpiredLease,
399+
stateDir,
400+
recoverableProviderIds: ['revyl'],
401+
});
402+
try {
403+
const startupDiagnostics: { phase: string; resourcePath: string }[] = [];
404+
await daemon.recoverInherited((diagnostic) => startupDiagnostics.push(diagnostic));
405+
406+
expect(startupDiagnostics).toEqual([
407+
expect.objectContaining({
408+
phase: 'provider_lease_record_read_failed',
409+
resourcePath: journalPath,
410+
}),
411+
]);
412+
expect(fs.existsSync(journalPath)).toBe(true);
413+
expect(recoverExpiredLease).not.toHaveBeenCalled();
414+
} finally {
415+
daemon.shutdown();
416+
fs.rmSync(stateDir, { recursive: true, force: true });
417+
}
418+
});

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function createExpiredProviderLeaseReleaser(options: {
110110

111111
const adoptOrphanedLeases = (onDiagnostic?: (diagnostic: ResourceDiagnostic) => void): void => {
112112
if (!options.stateDir) return;
113-
const orphaned = loadLeaseRecord(options.stateDir, LIVE_LEASES_FILE);
113+
const orphaned = loadLeaseRecord(options.stateDir, LIVE_LEASES_FILE, onDiagnostic);
114114
if (orphaned.size === 0) return;
115115
for (const [leaseId, lease] of orphaned) {
116116
if (!pendingRecoveryLeases.has(leaseId)) pendingRecoveryLeases.set(leaseId, lease);
@@ -169,7 +169,11 @@ export function createExpiredProviderLeaseReleaser(options: {
169169
if (inherited) return;
170170
inherited = true;
171171
// Everything in the expired record is durable by definition, which is what the persisted ids track.
172-
for (const [leaseId, lease] of loadLeaseRecord(options.stateDir, PENDING_RELEASES_FILE)) {
172+
for (const [leaseId, lease] of loadLeaseRecord(
173+
options.stateDir,
174+
PENDING_RELEASES_FILE,
175+
onDiagnostic,
176+
)) {
173177
pendingRecoveryLeases.set(leaseId, lease);
174178
}
175179
for (const leaseId of pendingRecoveryLeases.keys()) persistedRecoveryLeaseIds.add(leaseId);
@@ -414,22 +418,30 @@ function writeLeaseRecord(stateDir: string, file: string, leases: readonly Devic
414418
fs.chmodSync(filePath, 0o600);
415419
}
416420

417-
function loadLeaseRecord(stateDir: string | undefined, file: string): Map<string, DeviceLease> {
421+
/**
422+
* Reads one lease record. A record that exists but cannot be read is left on disk and reported with its
423+
* path, so an operator can tell which record it is and end its leases by hand.
424+
*/
425+
function loadLeaseRecord(
426+
stateDir: string | undefined,
427+
file: string,
428+
onDiagnostic?: (diagnostic: ResourceDiagnostic) => void,
429+
): Map<string, DeviceLease> {
418430
if (!stateDir) return new Map();
419431
const filePath = leaseRecordPath(stateDir, file);
420432
try {
421433
const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8')) as PersistedProviderLeaseRecord;
422434
if (parsed.version !== LEASE_RECORD_VERSION || !Array.isArray(parsed.leases)) {
423-
throw new Error('unsupported expiry release record');
435+
throw new Error(`unsupported lease record (version ${String(parsed.version)})`);
424436
}
425437
return new Map(
426438
parsed.leases.filter(isPersistedDeviceLease).map((lease) => [lease.leaseId, lease]),
427439
);
428440
} catch (error) {
429441
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return new Map();
430-
emitDiagnostic({
431-
level: 'warn',
432-
phase: 'provider_lease_expiry_record_read_failed',
442+
onDiagnostic?.({
443+
phase: 'provider_lease_record_read_failed',
444+
resourcePath: filePath,
433445
data: { error: error instanceof Error ? error.message : String(error) },
434446
});
435447
return new Map();

0 commit comments

Comments
 (0)