Skip to content

Commit 7262f17

Browse files
committed
refactor(daemon): route daemon-level diagnostics through one scope helper
withDaemonDiagnosticsScope owns the daemon session scope and the forced flush, which now also runs when the body throws, so a failing body keeps its record. It lives at the src root so both daemon-runtime and daemon-registration-owner use it; the eight hand-written copies and the call-site comments that repeated its contract are gone.
1 parent d043b44 commit 7262f17

4 files changed

Lines changed: 10 additions & 33 deletions

File tree

src/daemon/server/daemon-diagnostics-scope.test.ts renamed to src/daemon-diagnostics-scope.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path';
44
import { afterEach, beforeEach, test } from 'vitest';
55
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
66
import { withDaemonDiagnosticsScope } from './daemon-diagnostics-scope.ts';
7-
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
7+
import { mkdtempForTestSync } from './__tests__/test-utils/tmp-dir.ts';
88

99
let dir: string;
1010
let logPath: string;
@@ -56,21 +56,9 @@ test('forces the buffered events of a non-debug scope out once the body returns'
5656
assert.ok(events.every((event) => event.command === 'daemon-startup'));
5757
});
5858

59-
test('leaves buffered events unwritten when the body throws', async () => {
59+
test('keeps the record of a failed body and still rethrows', async () => {
6060
await assert.rejects(
61-
withDaemonDiagnosticsScope({ logPath, debug: false }, () => {
62-
emitDiagnostic({ level: 'warn', phase: 'lost' });
63-
throw new Error('boom');
64-
}),
65-
/boom/,
66-
);
67-
68-
assert.deepEqual(readLoggedEvents(), []);
69-
});
70-
71-
test('flushOnThrow keeps the record of a failed body and still rethrows', async () => {
72-
await assert.rejects(
73-
withDaemonDiagnosticsScope({ logPath, debug: false, flushOnThrow: true }, async () => {
61+
withDaemonDiagnosticsScope({ logPath, debug: false }, async () => {
7462
emitDiagnostic({ level: 'warn', phase: 'kept' });
7563
throw new Error('boom');
7664
}),

src/daemon/server/daemon-diagnostics-scope.ts renamed to src/daemon-diagnostics-scope.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,18 @@ export type DaemonDiagnosticsScopeOptions = Readonly<{
77
logPath: string;
88
command?: 'daemon' | 'daemon-startup';
99
debug?: boolean;
10-
/** Also force the flush when `body` throws, so the record of a failure is kept. */
11-
flushOnThrow?: boolean;
1210
}>;
1311

1412
/**
1513
* Runs daemon-level work that has no request scope inside a `daemon` session scope, then forces its
16-
* events to `logPath`. Without a scope `emitDiagnostic` drops every event.
14+
* events to `logPath`, also when the work throws. Without a scope `emitDiagnostic` drops every event.
1715
*/
1816
export async function withDaemonDiagnosticsScope<T>(
1917
options: DaemonDiagnosticsScopeOptions,
2018
body: () => Promise<T> | T,
2119
): Promise<T> {
22-
const { logPath, command = 'daemon', debug = true, flushOnThrow = false } = options;
20+
const { logPath, command = 'daemon', debug = true } = options;
2321
return await withDiagnosticsScope({ command, session: 'daemon', logPath, debug }, async () => {
24-
if (!flushOnThrow) {
25-
const result = await body();
26-
flushDiagnosticsToSessionFile({ force: true });
27-
return result;
28-
}
2922
try {
3023
return await body();
3124
} finally {

‎src/daemon-registration-owner.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
type ProcessLockAttempt,
2222
type ProcessLockAcquisition,
2323
} from '@agent-device/host-kit/file';
24-
import { emitDiagnostic, withDiagnosticsScope } from '@agent-device/host-kit/diagnostics';
24+
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
25+
import { withDaemonDiagnosticsScope } from './daemon-diagnostics-scope.ts';
2526
import type { DaemonCodeOrigin } from '@agent-device/host-kit/code-signature';
2627
import {
2728
resolveDaemonPaths,
@@ -525,7 +526,7 @@ async function recordRegistrationWarning(
525526
phase: string,
526527
error: unknown,
527528
): Promise<void> {
528-
await withDiagnosticsScope({ command: 'daemon', session: 'daemon', logPath, debug: true }, () => {
529+
await withDaemonDiagnosticsScope({ logPath }, () => {
529530
emitDiagnostic({ level: 'warn', phase, data: { error: normalizeError(error) } });
530531
});
531532
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type { DaemonInvokeFn } from '../daemon-request.ts';
3939
import type { SessionRef, SessionState } from '../session-state.ts';
4040
import type { RuntimeHintValues } from '@agent-device/contracts/application-lifecycle-runtime';
4141
import { createDaemonIdleReap } from './daemon-idle-reap.ts';
42-
import { withDaemonDiagnosticsScope } from './daemon-diagnostics-scope.ts';
42+
import { withDaemonDiagnosticsScope } from '../../daemon-diagnostics-scope.ts';
4343
import { createSessionIdleExpiry } from './daemon-session-idle-expiry.ts';
4444
import { resolveSessionIdleExpiryMs } from '../session-idle-expiry.ts';
4545
import { finalizeDaemonLeases } from './daemon-lease-finalizer.ts';
@@ -536,10 +536,7 @@ export async function startDaemonRuntime(
536536
// sweep waits, never the settle itself, so a stuck recorder cannot make a sweep hang but also
537537
// cannot make an expiry give up on a device that does come free.
538538
settleBudgetMs: (session) => resolveDaemonSessionTeardownTimeoutMs(session),
539-
// A sweep is out-of-request work, so it has no request scope and `emitDiagnostic` would drop
540-
// everything it reports — including the record of what was reclaimed and why a reclaim failed.
541-
withinDiagnosticsScope: async (run) =>
542-
await withDaemonDiagnosticsScope({ logPath, flushOnThrow: true }, run),
539+
withinDiagnosticsScope: async (run) => await withDaemonDiagnosticsScope({ logPath }, run),
543540
// An expiry can be the event that makes this daemon fully idle, and no request follows it to
544541
// arm the process-level reap.
545542
onSessionExpired: () => {
@@ -861,8 +858,6 @@ async function reconcileDeviceClaimsForDaemonStartup(
861858
reconcile: DeviceClaimReconciler,
862859
stateDir: string,
863860
): Promise<void> {
864-
// Startup runs outside any diagnostics scope, where emitDiagnostic is a no-op,
865-
// so reconciliation has to open one of its own for its events to be recorded.
866861
await withDaemonDiagnosticsScope({ logPath }, async () => {
867862
try {
868863
const summary = await reconcileOrphanedDeviceClaims(reconcile, stateDir);

0 commit comments

Comments
 (0)