|
1 | 1 | import { Worker } from 'node:worker_threads'; |
2 | | -import { emitDiagnostic, getDiagnosticsMeta } from '@agent-device/host-kit/diagnostics'; |
| 2 | +import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; |
3 | 3 | import { AppError, toAppErrorCode } from '@agent-device/kernel/errors'; |
4 | 4 | import type { Rect } from '@agent-device/kernel/snapshot'; |
5 | 5 | import { resolveInternalEntryModulePath } from './internal-entry.ts'; |
@@ -40,29 +40,20 @@ type PendingJob = { |
40 | 40 | let worker: Worker | null = null; |
41 | 41 | let workerUnavailable = false; |
42 | 42 | let workerUnavailableReason: string | undefined; |
43 | | -let reportedWorkerUnavailable = false; |
44 | 43 | let nextJobId = 0; |
45 | 44 | const pendingJobs = new Map<number, PendingJob>(); |
46 | 45 |
|
47 | | -/** Permanently degrades to the sync path; the reason is reported by the first scope that sees it. */ |
| 46 | +/** Permanently degrades to the sync path; a scope that reaches the fallback reports the reason. */ |
48 | 47 | function markWorkerUnavailable(reason: string): void { |
49 | 48 | workerUnavailable = true; |
50 | 49 | workerUnavailableReason ??= reason; |
51 | | - reportWorkerUnavailable(); |
52 | 50 | } |
53 | 51 |
|
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; |
| 52 | +function reportWorkerUnavailable(fallbackReason: string): void { |
62 | 53 | emitDiagnostic({ |
63 | 54 | level: 'warn', |
64 | 55 | phase: 'png_worker_unavailable', |
65 | | - data: { reason: workerUnavailableReason }, |
| 56 | + data: { reason: workerUnavailableReason ?? fallbackReason }, |
66 | 57 | }); |
67 | 58 | } |
68 | 59 |
|
@@ -112,10 +103,7 @@ function updateWorkerRef(): void { |
112 | 103 | } |
113 | 104 |
|
114 | 105 | function obtainWorker(): Worker | null { |
115 | | - if (workerUnavailable) { |
116 | | - reportWorkerUnavailable(); |
117 | | - return null; |
118 | | - } |
| 106 | + if (workerUnavailable) return null; |
119 | 107 | if (worker) return worker; |
120 | 108 | const modulePath = resolveInternalEntryModulePath(import.meta.url, PNG_WORKER_ENTRYPOINT); |
121 | 109 | if (!modulePath) { |
@@ -192,17 +180,19 @@ async function runPngJob<Kind extends PngWorkerJobKind>( |
192 | 180 | return await runWorkerJob(job); |
193 | 181 | } catch (error) { |
194 | 182 | if (error instanceof PngWorkerUnavailableError) { |
195 | | - // A worker can fail while this request waits on it, from an event callback with no scope. |
196 | | - reportWorkerUnavailable(); |
| 183 | + reportWorkerUnavailable(error.message); |
197 | 184 | return await runSync(); |
198 | 185 | } |
199 | 186 | throw error; |
200 | 187 | } |
201 | 188 | } |
202 | 189 |
|
203 | | -/** Daemon startup hook: spawns the worker before the first screenshot job. */ |
| 190 | +/** |
| 191 | + * Daemon startup hook: spawns the worker before the first screenshot job. Run it inside a |
| 192 | + * diagnostics scope so a worker that cannot start leaves its reason in the daemon log. |
| 193 | + */ |
204 | 194 | export function prewarmPngWorker(): void { |
205 | | - obtainWorker(); |
| 195 | + if (obtainWorker() === null) reportWorkerUnavailable('PNG worker is unavailable'); |
206 | 196 | } |
207 | 197 |
|
208 | 198 | /** Stops the worker thread (used by tests and shutdown); later calls respawn it. */ |
|
0 commit comments