Skip to content

Commit 9c93b9e

Browse files
thymikeeclaude
andcommitted
fix(daemon): log orphan adoption in the daemon log, after the other lock-held recovery
emitDiagnostic needs a request scope, which daemon startup does not have, so the adoption report was dropped. It now goes through the startup diagnostics the daemon flushes to its log, and recovery starts next to the other recovery that runs once the registration is held. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 79692a9 commit 9c93b9e

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,14 @@ test('a daemon killed while holding a provider lease recovers it on restart', as
182182
killedDaemon.shutdown();
183183

184184
const restartedDaemon = createExpiredProviderLeaseReleaser(options);
185-
await restartedDaemon.recoverInherited();
185+
const startupDiagnostics: { phase: string; data: Record<string, unknown> }[] = [];
186+
await restartedDaemon.recoverInherited((diagnostic) => startupDiagnostics.push(diagnostic));
187+
expect(startupDiagnostics).toEqual([
188+
expect.objectContaining({
189+
phase: 'provider_lease_orphans_adopted',
190+
data: { leaseIds: [held.leaseId] },
191+
}),
192+
]);
186193
expect(recoverExpiredLease.mock.calls.map(([lease]) => lease.leaseId)).toEqual([held.leaseId]);
187194
expect(fs.existsSync(`${stateDir}/live-provider-leases.json`)).toBe(false);
188195
restartedDaemon.shutdown();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,6 @@ async function startDaemonRuntimeOnHost(
619619
});
620620
}
621621
const registration = acquisition.owner;
622-
// Only the registered daemon may read the state directory's lease records and act on them.
623-
void expiredProviderLeaseReleaser.recoverInherited();
624622
rollback.onRollback(async () => {
625623
await deps.ownerLifecycle.clearDaemonLockConfiguration();
626624
});
@@ -673,6 +671,11 @@ async function startDaemonRuntimeOnHost(
673671
scope: createRecoveryPlatformScope(),
674672
onDiagnostic: (diagnostic) => startupDiagnostics.push(diagnostic),
675673
});
674+
// Only the registered daemon may read the state directory's lease records and act on them. Loading and
675+
// adoption run before the first await, so their report reaches this startup's diagnostics.
676+
void expiredProviderLeaseReleaser.recoverInherited((diagnostic) =>
677+
startupDiagnostics.push(diagnostic),
678+
);
676679
await reapOwnedProcessRecordsAtStartup(deps.ownedProcessRecords, {
677680
openWebSessionNames: openWebSessionNames(sessionStore),
678681
purposes: ['simctl-screen-recording'],

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from '@agent-device/contracts/device';
88
import { publishFileSync } from '@agent-device/host-kit/file';
99
import { releaseExpiredProviderLease } from '../sessions/lease-lifecycle.ts';
10-
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
10+
import { emitDiagnostic, type ResourceDiagnostic } from '@agent-device/host-kit/diagnostics';
1111
import { sleep } from '@agent-device/host-kit/retry';
1212

1313
const DEFAULT_RETRY_DELAY_MS = 1_000;
@@ -47,7 +47,7 @@ export type ExpiredProviderLeaseReleaser = {
4747
* still running, and acting on them would end devices it is using. Until then the releaser touches no
4848
* file in the state directory.
4949
*/
50-
recoverInherited: () => Promise<void>;
50+
recoverInherited: (onDiagnostic?: (diagnostic: ResourceDiagnostic) => void) => Promise<void>;
5151
retryPending: () => Promise<void>;
5252
/**
5353
* `pending` holds every lease still unreleased; `unknown` is the part of it whose release was
@@ -108,7 +108,7 @@ export function createExpiredProviderLeaseReleaser(options: {
108108
}
109109
};
110110

111-
const adoptOrphanedLeases = (): void => {
111+
const adoptOrphanedLeases = (onDiagnostic?: (diagnostic: ResourceDiagnostic) => void): void => {
112112
const orphaned = loadLeaseRecord(options.stateDir, LIVE_LEASES_FILE);
113113
if (orphaned.size === 0 || !options.stateDir) return;
114114
for (const [leaseId, lease] of orphaned) {
@@ -124,9 +124,11 @@ export function createExpiredProviderLeaseReleaser(options: {
124124
// pending record already holds each of its leases once.
125125
emitJournalFailure('provider_lease_journal_remove_failed', orphaned.size, error);
126126
}
127-
emitDiagnostic({
128-
level: 'warn',
127+
// Startup runs outside any request, so the report goes to the daemon log through the startup
128+
// diagnostics rather than a request scope that does not exist yet.
129+
onDiagnostic?.({
129130
phase: 'provider_lease_orphans_adopted',
131+
resourcePath: leaseRecordPath(options.stateDir, LIVE_LEASES_FILE),
130132
data: { leaseIds: [...orphaned.keys()] },
131133
});
132134
};
@@ -160,14 +162,16 @@ export function createExpiredProviderLeaseReleaser(options: {
160162
journalRetryTimer.unref?.();
161163
}
162164
};
163-
const recoverInherited = async (): Promise<void> => {
165+
const recoverInherited = async (
166+
onDiagnostic?: (diagnostic: ResourceDiagnostic) => void,
167+
): Promise<void> => {
164168
if (inherited) return;
165169
inherited = true;
166170
for (const [leaseId, lease] of loadLeaseRecord(options.stateDir, PENDING_RELEASES_FILE)) {
167171
pendingRecoveryLeases.set(leaseId, lease);
168172
persistedRecoveryLeaseIds.add(leaseId);
169173
}
170-
adoptOrphanedLeases();
174+
adoptOrphanedLeases(onDiagnostic);
171175
await retryPending();
172176
};
173177
const recordLiveLeases = (leases: readonly DeviceLease[]): void => {

0 commit comments

Comments
 (0)