Skip to content

Commit 6997818

Browse files
committed
fix(tv-cdp): load the diagnostics emitter only on the error path
The listener guard imported host-kit diagnostics eagerly, which grew the tv-cdp eager import closure from 6 to 8 modules and failed the eager closure budget. The emitter now loads with a function-scoped import when an unreadable frame or a failing listener is reported. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent cfed7a3 commit 6997818

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

‎packages/tv-cdp/src/cdp.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
21
import { AppError } from '@agent-device/kernel/errors';
32

43
import { createReplyWaiters } from './replies.ts';
@@ -38,6 +37,16 @@ function parseCdpMessage(data: unknown): CdpMessage | undefined {
3837
}
3938
}
4039

40+
/**
41+
* Records a debug event in the request diagnostics. The emitter loads on demand because these
42+
* events are rare and this module sits in the package's eager import closure.
43+
*/
44+
function emitCdpDiagnostic(phase: string, data: Record<string, unknown>): void {
45+
void import('@agent-device/host-kit/diagnostics')
46+
.then(({ emitDiagnostic }) => emitDiagnostic({ level: 'debug', phase, data }))
47+
.catch(() => undefined);
48+
}
49+
4150
/** Never throws: a listener can throw a value that `String()` cannot convert. */
4251
function describeThrown(error: unknown): string {
4352
if (error instanceof Error) return error.message;
@@ -88,11 +97,7 @@ export async function connectCdp(target: CdpTarget): Promise<CdpSession> {
8897
socket.onmessage = (event) => {
8998
const message = parseCdpMessage(event.data);
9099
if (message === undefined) {
91-
emitDiagnostic({
92-
level: 'debug',
93-
phase: 'tv_cdp_frame_unreadable',
94-
data: { target: target.url },
95-
});
100+
emitCdpDiagnostic('tv_cdp_frame_unreadable', { target: target.url });
96101
return;
97102
}
98103
if (message.method !== undefined) {
@@ -104,10 +109,9 @@ export async function connectCdp(target: CdpTarget): Promise<CdpSession> {
104109
try {
105110
listener(message.method, params);
106111
} catch (error) {
107-
emitDiagnostic({
108-
level: 'debug',
109-
phase: 'tv_cdp_event_listener_failed',
110-
data: { method: message.method, error: describeThrown(error) },
112+
emitCdpDiagnostic('tv_cdp_event_listener_failed', {
113+
method: message.method,
114+
error: describeThrown(error),
111115
});
112116
}
113117
}

0 commit comments

Comments
 (0)