Skip to content

Commit 70d3211

Browse files
thymikeeclaude
andcommitted
fix(capture-kit): report an unavailable PNG worker in the first diagnostics scope that sees it
A startup pre-warm runs outside any scope, where the event is dropped; it no longer latches the one-time report. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent f547d88 commit 70d3211

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, test, vi } from 'vitest';
2+
import { PNG } from './png.ts';
3+
4+
test('an unavailable worker is reported by the first diagnostics scope that reaches the fallback', async () => {
5+
vi.resetModules();
6+
vi.doMock('./internal-entry.ts', () => ({ resolveInternalEntryModulePath: () => null }));
7+
// Loaded after the reset so the client and the scope share one diagnostics module.
8+
const { countDiagnosticEventsByPhase, withDiagnosticsScope } =
9+
await import('@agent-device/host-kit/diagnostics');
10+
const { decodePngAsync, prewarmPngWorker } = await import('./png-worker-client.ts');
11+
const png = PNG.sync.write(new PNG({ width: 1, height: 1 }));
12+
13+
// The daemon pre-warms outside any request scope, where the report would be dropped.
14+
prewarmPngWorker();
15+
16+
const reported = await withDiagnosticsScope({ command: 'screenshot' }, async () => {
17+
await decodePngAsync(png, 'first');
18+
const afterFirst = countDiagnosticEventsByPhase(['png_worker_unavailable']);
19+
await decodePngAsync(png, 'second');
20+
return { afterFirst, afterSecond: countDiagnosticEventsByPhase(['png_worker_unavailable']) };
21+
});
22+
const laterScope = await withDiagnosticsScope({ command: 'screenshot' }, async () => {
23+
await decodePngAsync(png, 'third');
24+
return countDiagnosticEventsByPhase(['png_worker_unavailable']);
25+
});
26+
27+
expect(reported).toEqual({ afterFirst: 1, afterSecond: 1 });
28+
expect(laterScope).toBe(0);
29+
vi.doUnmock('./internal-entry.ts');
30+
});

‎packages/capture-kit/src/png-worker-client.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Worker } from 'node:worker_threads';
2-
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
2+
import { emitDiagnostic, getDiagnosticsMeta } from '@agent-device/host-kit/diagnostics';
33
import { AppError, toAppErrorCode } from '@agent-device/kernel/errors';
44
import type { Rect } from '@agent-device/kernel/snapshot';
55
import { resolveInternalEntryModulePath } from './internal-entry.ts';
@@ -39,16 +39,31 @@ type PendingJob = {
3939

4040
let worker: Worker | null = null;
4141
let workerUnavailable = false;
42-
let warnedWorkerUnavailable = false;
42+
let workerUnavailableReason: string | undefined;
43+
let reportedWorkerUnavailable = false;
4344
let nextJobId = 0;
4445
const pendingJobs = new Map<number, PendingJob>();
4546

46-
/** Permanently degrades to the sync path and reports the reason once. */
47+
/** Permanently degrades to the sync path; the reason is reported by the first scope that sees it. */
4748
function markWorkerUnavailable(reason: string): void {
4849
workerUnavailable = true;
49-
if (warnedWorkerUnavailable) return;
50-
warnedWorkerUnavailable = true;
51-
emitDiagnostic({ level: 'warn', phase: 'png_worker_unavailable', data: { reason } });
50+
workerUnavailableReason ??= reason;
51+
reportWorkerUnavailable();
52+
}
53+
54+
/**
55+
* A pre-warm at daemon startup runs outside any diagnostics scope, where the event would be
56+
* dropped, so the report waits for the first request scope that reaches the degraded path.
57+
*/
58+
function reportWorkerUnavailable(): void {
59+
if (reportedWorkerUnavailable || workerUnavailableReason === undefined) return;
60+
if (Object.keys(getDiagnosticsMeta()).length === 0) return;
61+
reportedWorkerUnavailable = true;
62+
emitDiagnostic({
63+
level: 'warn',
64+
phase: 'png_worker_unavailable',
65+
data: { reason: workerUnavailableReason },
66+
});
5267
}
5368

5469
function handleWorkerMessage(message: PngWorkerResponse): void {
@@ -97,7 +112,10 @@ function updateWorkerRef(): void {
97112
}
98113

99114
function obtainWorker(): Worker | null {
100-
if (workerUnavailable) return null;
115+
if (workerUnavailable) {
116+
reportWorkerUnavailable();
117+
return null;
118+
}
101119
if (worker) return worker;
102120
const modulePath = resolveInternalEntryModulePath(import.meta.url, PNG_WORKER_ENTRYPOINT);
103121
if (!modulePath) {

0 commit comments

Comments
 (0)