Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): stop serving web workers from URLs that 404 after a deploy (#18315)

Nikita Kabardin242583d50f52
diff --git a/knip.jsonc b/knip.jsonc
index 66b4ebb599..34cc44d5af 100644
--- a/knip.jsonc
+++ b/knip.jsonc
@@ -48,6 +48,9 @@
".storybook/docs/**/*.tsx",
// Runtime worker entry loaded by filename through Piscina.
"src/server/otel/otelIngestionWorker.ts",
+ // Web Worker entry bundled into a string module by
+ // scripts/build-json-parser-worker.mjs, never imported by the app.
+ "src/workers/json-parser.worker.ts",
],
"ignoreFiles": [
// Standalone benchmark invoked directly with Node rather than imported by the application.
diff --git a/packages/shared/package.json b/packages/shared/package.json
index eadaf6a89c..5d168ce136 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -55,6 +55,10 @@
"import": "./dist/src/utils/chatml/index.js",
"require": "./dist/src/utils/chatml/index.js"
},
+ "./src/utils/json": {
+ "import": "./dist/src/utils/json.js",
+ "require": "./dist/src/utils/json.js"
+ },
"./src/utils/normalized-io": {
"import": "./dist/src/utils/normalized-io/index.js",
"require": "./dist/src/utils/normalized-io/index.js"
diff --git a/web/next.config.mjs b/web/next.config.mjs
index 421d2d2d52..8938270e6e 100644
--- a/web/next.config.mjs
+++ b/web/next.config.mjs
@@ -164,6 +164,11 @@ const nextConfig = {
// imports have to stay same-origin. Empty string is a literal prefix, not
// a fallback: it emits `/_next/...` on the page origin. Unset (undefined)
// would inherit assetPrefix and break workers on Cloud.
+ //
+ // No worker is started from a bundler URL any more — same-origin is exactly
+ // what pins those chunks to a host that only serves the current build, so
+ // they 404 in a tab that outlives a deploy. This stays as the guard for
+ // anything that reintroduces one.
turbopackWorkerAssetPrefix: "",
},
@@ -185,6 +190,10 @@ const nextConfig = {
"./node_modules/@scalar/api-reference/dist/browser/standalone.js",
"./third-party-licenses/scalar-api-reference.LICENSE.txt",
],
+ // elkjs's prebuilt layout worker, served verbatim so the trace graph's
+ // worker URL keeps resolving across a deploy. See
+ // pages/api/workers/elk-worker.ts.
+ "/api/workers/elk-worker": ["./node_modules/elkjs/lib/elk-worker.min.js"],
},
async redirects() {
diff --git a/web/package.json b/web/package.json
index ac8175e69b..c75d662da0 100644
--- a/web/package.json
+++ b/web/package.json
@@ -11,6 +11,7 @@
"build:test": "INLINE_RUNTIME_CHUNK=false dotenv -e ../.env -- next build --experimental-build-mode compile",
"build:check": "NEXT_DIST_DIR=.next-check INLINE_RUNTIME_CHUNK=false dotenv -e ../.env -- next build && NEXT_DIST_DIR=.next-check pnpm run build:otel-worker",
"build:otel-worker": "DOCKER_BUILD=1 dotenv -e ../.env -- node scripts/build-otel-ingestion-worker.mjs",
+ "build:json-parser-worker": "node scripts/build-json-parser-worker.mjs",
"typecheck": "dotenv -e ../.env -- tsc -p tsconfig.json --noEmit --skipLibCheck --incremental --tsBuildInfoFile .tsbuildinfo",
"dev": "dotenv -e ../.env -- next dev",
"dev:http": "dotenv -e ../.env -- next dev --experimental-https --experimental-https-key ./localhost+1-key.pem --experimental-https-cert ./localhost+1.pem",
diff --git a/web/scripts/build-json-parser-worker.mjs b/web/scripts/build-json-parser-worker.mjs
new file mode 100644
index 0000000000..e05ba769d8
--- /dev/null
+++ b/web/scripts/build-json-parser-worker.mjs
@@ -0,0 +1,103 @@
+/**
+ * Bundle the JSON-parser Web Worker into a string module.
+ *
+ * The client starts the worker from a blob built out of that string, so the
+ * worker has no URL at all: it cannot 404 after a deploy, and worker and main
+ * thread always come from the same build. See `jsonParserWorkerClient.ts`.
+ *
+ * The output is committed. `json-parser.worker.servertest.ts` re-runs this
+ * script and fails when the committed file no longer matches, so the two can
+ * never drift.
+ *
+ * pnpm --filter web run build:json-parser-worker
+ */
+import { readFile, writeFile } from "node:fs/promises";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { build } from "esbuild";
+import prettier from "prettier";
+
+const webDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
+
+export const WORKER_ENTRY = "src/workers/json-parser.worker.ts";
+export const GENERATED_FILE = path.join(
+ webDir,
+ "src/workers/generated/json-parser.worker.source.ts",
+);
+
+/**
+ * Bundle the worker and return the module text to write.
+ *
+ * `@langfuse/shared/src/utils/json` is aliased to shared's TypeScript source:
+ * the published entry point resolves to `dist`, whose emitted imports drag the
+ * rest of shared (and zod) into a bundle that only needs one function.
+ */
+export async function buildWorkerSourceModule() {
+ const result = await build({
+ absWorkingDir: webDir,
+ entryPoints: [WORKER_ENTRY],
+ bundle: true,
+ format: "iife",
+ platform: "browser",
+ target: "es2022",
+ tsconfig: "tsconfig.json",
+ minify: true,
+ legalComments: "none",
+ write: false,
+ alias: {
+ "@langfuse/shared/src/utils/json": path.join(
+ webDir,
+ "../packages/shared/src/utils/json.ts",
+ ),
+ },
+ });
+
+ const [output] = result.outputFiles;
+ if (!output) throw new Error("esbuild produced no output for the worker");
+
+ const bundled = output.text;
+
+ // A bundle that still references these never runs in a Worker; catching it
+ // here beats catching it as a runtime error in a user's browser.
+ for (const forbidden of ["importScripts(", "require(", "process.env"]) {
+ if (bundled.includes(forbidden)) {
+ throw new Error(
+ `JSON-parser worker bundle contains \`${forbidden}\` — it must be self-contained`,
+ );
+ }
+ }
+
+ const moduleText = `/**
+ * GENERATED FILE — DO NOT EDIT.
+ *
+ * Bundled from ${WORKER_ENTRY} by scripts/build-json-parser-worker.mjs.
+ * Regenerate with: pnpm --filter web run build:json-parser-worker
+ */
+
+export const JSON_PARSER_WORKER_SOURCE = ${JSON.stringify(bundled)};
+`;
+
+ return prettier.format(moduleText, {
+ ...((await prettier.resolveConfig(GENERATED_FILE)) ?? {}),
+ filepath: GENERATED_FILE,
+ });
+}
+
+/** True when the committed module is already what a fresh build produces. */
+export async function isGeneratedFileCurrent() {
+ const [fresh, committed] = await Promise.all([
+ buildWorkerSourceModule(),
+ readFile(GENERATED_FILE, "utf8").catch(() => null),
+ ]);
+ return { current: fresh === committed, fresh, committed };
+}
+
+// Only write when run as a script, so tests can import the builder.
+if (process.argv[1] === fileURLToPath(import.meta.url)) {
+ const moduleText = await buildWorkerSourceModule();
+ await writeFile(GENERATED_FILE, moduleText);
+ console.log(
+ `JSON-parser worker bundled into ${GENERATED_FILE} (${moduleText.length} B)`,
+ );
+}
diff --git a/web/src/__tests__/server/unit/elk-worker-route.servertest.ts b/web/src/__tests__/server/unit/elk-worker-route.servertest.ts
new file mode 100644
index 0000000000..a9260e7e2d
--- /dev/null
+++ b/web/src/__tests__/server/unit/elk-worker-route.servertest.ts
@@ -0,0 +1,85 @@
+import { readFileSync } from "node:fs";
+import { createRequire } from "node:module";
+import { dirname, join } from "node:path";
+import type { NextApiRequest, NextApiResponse } from "next";
+import { createMocks } from "node-mocks-http";
+
+import handler from "@/src/pages/api/workers/elk-worker";
+
+const requireFrom = createRequire(import.meta.url);
+const elkWorkerFile = join(
+ dirname(requireFrom.resolve("elkjs/package.json")),
+ "lib/elk-worker.min.js",
+);
+
+const callHandler = (
+ method: "GET" | "HEAD" | "POST",
+ headers: Record<string, string> = {},
+) => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method,
+ headers,
+ });
+ handler(req, res);
+ return res;
+};
+
+/**
+ * The trace graph's layout worker is served from this route rather than from a
+ * bundler chunk. Worker chunks live under `/_next/static/chunks/...` on the app
+ * origin, which only ever serves the current build, so every tab that outlives
+ * a deploy 404s on its worker and silently loses the large-graph layout budget
+ * (Sentry LANGFUSE-5YT / -57K, ~590 events / 14d). This path carries no content
+ * hash, so it keeps resolving.
+ */
+describe("/api/workers/elk-worker", () => {
+ it("serves elkjs's own prebuilt worker, byte for byte", () => {
+ const res = callHandler("GET");
+
+ expect(res.statusCode).toBe(200);
+ expect(res.getHeader("Content-Type")).toBe(
+ "text/javascript; charset=utf-8",
+ );
+ const served = res._getData() as Buffer;
+ expect(served.equals(readFileSync(elkWorkerFile))).toBe(true);
+ });
+
+ it("serves a worker that fetches nothing", () => {
+ // Why elkjs's prebuilt file can be served as-is instead of bundled: it has
+ // no loader of its own, so there is nothing left under it to 404.
+ const source = (callHandler("GET")._getData() as Buffer).toString("utf8");
+
+ expect(source).not.toContain("importScripts(");
+ expect(source).not.toContain("require(");
+ expect(source).toContain("onmessage");
+ });
+
+ it("is cacheable and revalidates with an ETag", () => {
+ const res = callHandler("GET");
+ const etag = res.getHeader("ETag");
+
+ expect(res.getHeader("Cache-Control")).toBe("public, max-age=86400");
+ expect(etag).toMatch(/^"[a-f0-9]{32}"$/);
+
+ const revalidated = callHandler("GET", { "if-none-match": String(etag) });
+ expect(revalidated.statusCode).toBe(304);
+ expect(revalidated._getData()).toBe("");
+ });
+
+ it("answers HEAD without a body", () => {
+ const res = callHandler("HEAD");
+
+ expect(res.statusCode).toBe(200);
+ expect(res.getHeader("Content-Type")).toBe(
+ "text/javascript; charset=utf-8",
+ );
+ expect(res._getData()).toBe("");
+ });
+
+ it("rejects anything that is not a read", () => {
+ const res = callHandler("POST");
+
+ expect(res.statusCode).toBe(405);
+ expect(res.getHeader("Allow")).toBe("GET, HEAD");
+ });
+});
diff --git a/web/src/__tests__/server/unit/json-parser-worker-bundle.servertest.ts b/web/src/__tests__/server/unit/json-parser-worker-bundle.servertest.ts
new file mode 100644
index 0000000000..ca485fa648
--- /dev/null
+++ b/web/src/__tests__/server/unit/json-parser-worker-bundle.servertest.ts
@@ -0,0 +1,112 @@
+import { createContext, runInContext } from "node:vm";
+
+import {
+ buildWorkerSourceModule,
+ GENERATED_FILE,
+} from "../../../../scripts/build-json-parser-worker.mjs";
+import { JSON_PARSER_WORKER_SOURCE } from "@/src/workers/generated/json-parser.worker.source";
+import type {
+ ParseRequest,
+ ParseResponse,
+} from "@/src/workers/json-parser.protocol";
+import { readFileSync } from "node:fs";
+
+/**
+ * The parse worker is bundled at build time into a committed string module and
+ * started from a blob, so that it has no URL that can 404 after a deploy. Two
+ * things have to hold for that to be safe:
+ *
+ * - the committed bundle is what the current sources produce, and
+ * - the bundle actually runs with nothing but a Worker global scope.
+ */
+describe("JSON parser worker bundle", () => {
+ it("is what a fresh build of the current sources produces", async () => {
+ const fresh = await buildWorkerSourceModule();
+ const committed = readFileSync(GENERATED_FILE, "utf8");
+
+ expect(
+ fresh === committed
+ ? ""
+ : `${GENERATED_FILE} is stale. Regenerate it with: pnpm --filter web run build:json-parser-worker`,
+ ).toBe("");
+ });
+
+ it("carries the header that keeps people from editing it by hand", () => {
+ expect(readFileSync(GENERATED_FILE, "utf8")).toContain(
+ "GENERATED FILE — DO NOT EDIT",
+ );
+ });
+
+ it("is self-contained, so nothing can 404 under it", () => {
+ // `importScripts` is exactly how the bundler-chunked version failed: the
+ // worker bootstrap loaded and then fetched chunks that no longer existed.
+ expect(JSON_PARSER_WORKER_SOURCE).not.toContain("importScripts(");
+ expect(JSON_PARSER_WORKER_SOURCE).not.toContain("require(");
+ // The 1MB `@langfuse/shared` barrel is what forced the chunk split.
+ expect(JSON_PARSER_WORKER_SOURCE.length).toBeLessThan(50_000);
+ });
+
+ /** Run the bundle with only the globals a dedicated worker really has. */
+ function startWorker() {
+ const posted: ParseResponse[] = [];
+ const scope = {
+ self: {
+ onmessage: null as ((event: { data: ParseRequest }) => void) | null,
+ postMessage: (response: ParseResponse) => posted.push(response),
+ },
+ performance,
+ };
+ runInContext(JSON_PARSER_WORKER_SOURCE, createContext(scope));
+ expect(scope.self.onmessage).toBeTypeOf("function");
+
+ return {
+ posted,
+ send: (request: ParseRequest) =>
+ scope.self.onmessage?.({ data: request }),
+ };
+ }
+
+ it("parses nested JSON strings the way the main thread would", () => {
+ const worker = startWorker();
+
+ worker.send({
+ id: "req-1",
+ input: JSON.stringify({ tool: JSON.stringify({ depth: 2 }) }),
+ output: "[1,2,3]",
+ metadata: null,
+ });
+
+ expect(worker.posted).toHaveLength(1);
+ expect(worker.posted[0]).toMatchObject({
+ id: "req-1",
+ parsedInput: { tool: { depth: 2 } },
+ parsedOutput: [1, 2, 3],
+ parsedMetadata: null,
+ });
+ expect(worker.posted[0]!.error).toBeUndefined();
+ });
+
+ it("answers with the raw payload when a parse throws, never silently", () => {
+ const worker = startWorker();
+ const exploding = {
+ get boom(): never {
+ throw new Error("payload blew up");
+ },
+ };
+
+ worker.send({
+ id: "req-2",
+ input: exploding,
+ output: null,
+ metadata: null,
+ });
+
+ expect(worker.posted).toHaveLength(1);
+ expect(worker.posted[0]!.id).toBe("req-2");
+ // `toContain`, not equality: an Error crossing the vm realm boundary is not
+ // `instanceof Error` inside it, so the worker stringifies it instead.
+ expect(worker.posted[0]!.error).toContain("payload blew up");
+ // Identity, not deep equality: reading the getter is what throws.
+ expect(worker.posted[0]!.parsedInput).toBe(exploding);
+ });
+});
diff --git a/web/src/features/trace-graph-view/layout/elkLayout.ts b/web/src/features/trace-graph-view/layout/elkLayout.ts
index 9e2ae49ae6..fdf0a70df0 100644
--- a/web/src/features/trace-graph-view/layout/elkLayout.ts
+++ b/web/src/features/trace-graph-view/layout/elkLayout.ts
@@ -278,8 +278,8 @@ let elkInstance: Promise<ELK> | null = null;
/**
* Lazy-load ELK (~1MB) and reuse the instance. Only the main-thread fallback
- * path uses this — the worker imports elkjs directly (see
- * `workers/elk-layout.worker.ts`).
+ * path uses this — the worker runs elkjs's own prebuilt worker bundle, served
+ * by `pages/api/workers/elk-worker.ts`.
*/
function getElk(): Promise<ELK> {
if (!elkInstance) {
diff --git a/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.clienttest.ts b/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.clienttest.ts
index 767b97dfc5..b10bd962bf 100644
--- a/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.clienttest.ts
+++ b/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.clienttest.ts
@@ -1,6 +1,15 @@
import { type GraphCanvasData, type GraphNodeData } from "../types";
import { MAX_MAIN_THREAD_LAYOUT_NODES } from "./elkLayout";
+const { mockCaptureException } = vi.hoisted(() => ({
+ mockCaptureException: vi.fn(),
+}));
+
+vi.mock("@sentry/nextjs", () => ({
+ captureException: mockCaptureException,
+ addBreadcrumb: vi.fn(),
+}));
+
const node = (id: string): GraphNodeData => ({ id, label: id, type: "AGENT" });
const graph: GraphCanvasData = {
@@ -36,7 +45,7 @@ type ElkMessage = {
graph?: { edges?: unknown[]; layoutOptions?: Record<string, string> };
};
-/** Stands in for the bundled elkjs worker: same message protocol, no layout. */
+/** Stands in for elkjs's prebuilt worker: same message protocol, no layout. */
class FakeWorker {
static instances: FakeWorker[] = [];
onmessage: ((event: MessageEvent) => void) | null = null;
@@ -44,7 +53,7 @@ class FakeWorker {
posted: ElkMessage[] = [];
terminated = false;
- constructor() {
+ constructor(public readonly url: string) {
FakeWorker.instances.push(this);
}
@@ -80,13 +89,53 @@ const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
describe("requestGraphLayout", () => {
beforeEach(() => {
+ vi.clearAllMocks();
FakeWorker.instances = [];
vi.stubGlobal("Worker", FakeWorker);
+ vi.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ });
+
+ it("starts the worker from a URL that outlives a deploy", async () => {
+ const client = await loadClient();
+ client.requestGraphLayout(graph, {}, "DOWN").catch(() => {});
+
+ // A `/_next/static/chunks/...` worker URL is content-hashed and pinned to
+ // the app origin, which only serves the current build — so it 404s in every
+ // tab that was open across a deploy.
+ expect(FakeWorker.instances[0]!.url).toBe(client.ELK_WORKER_URL);
+ expect(client.ELK_WORKER_URL).not.toContain("/_next/");
+ expect(client.ELK_WORKER_URL).toMatch(/\/api\/workers\/elk-worker$/);
+ });
+
+ it("degrades to this thread and cancels the error when the worker will not load", async () => {
+ const client = await loadClient();
+ const pending = client.requestGraphLayout(graph, {}, "DOWN");
+ const worker = FakeWorker.instances[0]!;
+ await flush();
+
+ const event = new ErrorEvent("error", { message: "failed to load" });
+ const preventDefault = vi.spyOn(event, "preventDefault");
+ worker.onerror?.(event);
+
+ // Uncanceled, the browser reports the same failure again on the owning
+ // document, where Sentry captures it a second time as unhandled.
+ expect(preventDefault).toHaveBeenCalledTimes(1);
+ expect(mockCaptureException).toHaveBeenCalledTimes(1);
+
+ const layout = await pending;
+ expect(layout.tooLarge).toBeFalsy();
+ expect(layout.nodes).toHaveLength(2);
+
+ // The worker is retired, not retried: the script will not start next time
+ // either, and every attempt costs another failure.
+ client.requestGraphLayout(graph, {}, "DOWN").catch(() => {});
+ expect(FakeWorker.instances).toHaveLength(1);
});
it("resolves with the layout the worker sends back", async () => {
diff --git a/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.ts b/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.ts
index 7513b52a5b..4a3c7dd388 100644
--- a/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.ts
+++ b/web/src/features/trace-graph-view/layout/graphLayoutWorkerClient.ts
@@ -1,6 +1,7 @@
import ELKConstructor from "elkjs/lib/elk-api";
import type { ELK } from "elkjs";
+import { env } from "@/src/env.mjs";
import { reportError } from "@/src/utils/reportError";
import { reportWorkerLoadError } from "@/src/utils/reportWorkerLoadError";
@@ -26,6 +27,15 @@ import {
*/
export const GRAPH_LAYOUT_DEADLINE_MS = 60_000;
+/**
+ * Where the layout worker is served from. NOT a bundler chunk: a content-hashed
+ * `/_next/static/chunks/...` worker URL 404s in every tab that outlives a
+ * deploy, because worker chunks are deliberately kept on the app origin and the
+ * app origin only serves the current build. This route resolves across deploys
+ * — see `pages/api/workers/elk-worker.ts`.
+ */
+export const ELK_WORKER_URL = `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/workers/elk-worker`;
+
type PendingLayout = {
id: string;
request: GraphLayoutRequest;
@@ -94,9 +104,7 @@ function getLayoutWorker(): LayoutWorker | null {
if (typeof window === "undefined" || !window.Worker) return null;
if (active) return active;
try {
- const worker = new Worker(
- new URL("@/src/workers/elk-layout.worker.ts", import.meta.url),
- );
+ const worker = new Worker(ELK_WORKER_URL);
worker.onerror = handleWorkerError;
// elk-api only sets `onmessage` on the worker we hand it, so the instance
// above stays ours to terminate.
diff --git a/web/src/features/traces/hooks/useParsedObservation.ts b/web/src/features/traces/hooks/useParsedObservation.ts
index 7db2d1b96d..11ed9b1a79 100644
--- a/web/src/features/traces/hooks/useParsedObservation.ts
+++ b/web/src/features/traces/hooks/useParsedObservation.ts
@@ -8,7 +8,7 @@
* - Non-blocking: Parsing happens in Web Worker
* - Cached: React Query caches both raw data AND parsed data independently
* - Progressive: UI renders immediately, data populates when ready
- * - Graceful fallback: Uses sync parsing if Web Workers unavailable
+ * - Graceful fallback: parses on this thread whenever the worker cannot
*/
import { useQuery } from "@tanstack/react-query";
@@ -21,12 +21,8 @@ import {
type ObservationReturnType,
} from "@/src/server/api/routers/traces";
import { stringifyMetadata } from "@/src/utils/clientSideDomainTypes";
-import type {
- ParseRequest,
- ParseResponse,
-} from "@/src/workers/json-parser.worker";
import { cheapHash } from "@/src/hooks/parsedIoCacheKey";
-import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";
+import { parseIoOffThread } from "@/src/workers/jsonParserWorkerClient";
type ObservationWithStringifiedIO = ObservationReturnTypeWithMetadata & {
input: string | null;
@@ -38,67 +34,6 @@ type ParsedObservationResult =
| EventBatchIOOutput
| undefined;
-/**
- * Threshold for using Web Worker vs sync parsing (in characters).
- * Below this: sync parse (faster, no message-passing overhead)
- * Above this: Web Worker (non-blocking, prevents UI freeze)
- */
-const PARSE_IN_WEBWORKER_THRESHOLD = 100_000; // 100KB
-
-/**
- * Estimate the size of a value in characters (for threshold check)
- */
-function estimateSize(value: unknown): number {
- if (value === null || value === undefined) return 0;
- if (typeof value === "string") return value.length;
- // For objects/arrays, estimate via JSON stringification length
- // This is approximate but good enough for threshold decisions
- try {
- return JSON.stringify(value).length;
- } catch {
- return 0;
- }
-}
-
-// Singleton worker instance shared across all hook calls
-let workerInstance: Worker | null = null;
-const pendingCallbacks = new Map<
- string,
- (data: ParseResponse & { error?: string }) => void
->();
-
-function getOrCreateWorker(): Worker | null {
- if (typeof window === "undefined" || !window.Worker) {
- return null; // SSR or no Worker support
- }
-
- if (!workerInstance) {
- try {
- // Next.js will bundle this as a separate chunk
- workerInstance = new Worker(
- new URL("@/src/workers/json-parser.worker.ts", import.meta.url),
- );
-
- workerInstance.onmessage = (e: MessageEvent<ParseResponse>) => {
- const callback = pendingCallbacks.get(e.data.id);
- if (callback) {
- callback(e.data);
- pendingCallbacks.delete(e.data.id);
- }
- };
-
- workerInstance.onerror = (event) => {
- reportParserWorkerError("useParsedObservation", event);
- };
- } catch (error) {
- console.error("[useParsedObservation] Failed to create worker:", error);
- return null;
- }
- }
-
- return workerInstance;
-}
-
interface UseParsedObservationParams {
observationId: string;
traceId: string;
@@ -108,98 +43,6 @@ interface UseParsedObservationParams {
baseObservation?: ObservationReturnType | ObservationReturnTypeWithMetadata;
}
-interface ParsedData {
- input: unknown;
- output: unknown;
- metadata: unknown;
- parseTime: number;
-}
-
-/**
- * Sync parse helper - used for small payloads or when Web Worker unavailable
- */
-async function syncParseObservationData(
- input: unknown,
- output: unknown,
- metadata: unknown,
-): Promise<ParsedData> {
- const { deepParseJsonIterative } = await import("@langfuse/shared");
- const startTime = performance.now();
-
- return {
- input: deepParseJsonIterative(input, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- output: deepParseJsonIterative(output, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- metadata: deepParseJsonIterative(metadata, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- parseTime: performance.now() - startTime,
- };
-}
-
-/**
- * Parse observation data in Web Worker (or sync for small payloads)
- * Returns a promise that resolves with parsed data
- */
-async function parseObservationData(
- input: unknown,
- output: unknown,
- metadata: unknown,
-): Promise<ParsedData> {
- // Estimate total size to decide sync vs worker
- const totalSize =
- estimateSize(input) + estimateSize(output) + estimateSize(metadata);
-
- // Small payloads: sync parse (faster, no message-passing overhead)
- if (totalSize < PARSE_IN_WEBWORKER_THRESHOLD) {
- return syncParseObservationData(input, output, metadata);
- }
-
- const worker = getOrCreateWorker();
-
- // Fallback to sync parsing if no worker support
- if (!worker) {
- return syncParseObservationData(input, output, metadata);
- }
-
- // Large payloads: parse in Web Worker (non-blocking)
- return new Promise<ParsedData>((resolve, reject) => {
- const parseId = `${Date.now()}-${Math.random()}`;
-
- pendingCallbacks.set(parseId, (result) => {
- pendingCallbacks.delete(parseId);
-
- if (result.error) {
- console.error(`[useParsedObservation] Parse error: ${result.error}`);
- reject(new Error(result.error));
- return;
- }
-
- resolve({
- input: result.parsedInput,
- output: result.parsedOutput,
- metadata: result.parsedMetadata,
- parseTime: result.parseTime ?? 0,
- });
- });
-
- const request: ParseRequest = {
- id: parseId,
- input,
- output,
- metadata,
- };
-
- worker.postMessage(request);
- });
-}
-
export function useParsedObservation({
observationId,
traceId,
@@ -307,11 +150,12 @@ export function useParsedObservation({
throw new Error("No observation data to parse");
}
- return parseObservationData(
- mergedObservation.input,
- mergedObservation.output,
- mergedObservation.metadata,
- );
+ return parseIoOffThread({
+ source: "useParsedObservation",
+ input: mergedObservation.input,
+ output: mergedObservation.output,
+ metadata: mergedObservation.metadata,
+ });
},
enabled: !!mergedObservation, // Only run when we have data
staleTime: Infinity, // Parsed data never goes stale (input data is the source of truth)
diff --git a/web/src/hooks/parserWorkerError.clienttest.ts b/web/src/hooks/parserWorkerError.clienttest.ts
index cb5aeca77a..f0e3c55c8c 100644
--- a/web/src/hooks/parserWorkerError.clienttest.ts
+++ b/web/src/hooks/parserWorkerError.clienttest.ts
@@ -32,10 +32,33 @@ describe("reportParserWorkerError", () => {
lineno: 1,
colno: 42,
error: undefined,
+ preventDefault: vi.fn(),
...overrides,
} as ErrorEvent;
}
+ it("cancels the event so the browser does not report it again on window", () => {
+ // A dedicated worker's uncaught error fires at the Worker object and, if it
+ // is not canceled there, is reported a second time on the owning document —
+ // where Sentry's global handler captures it unhandled, without any of the
+ // context above. That echo was ~half of this error family's volume.
+ const event = makeErrorEvent();
+ reportParserWorkerError("useParsedTrace", event);
+
+ expect(event.preventDefault).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps caller context alongside the hook name", () => {
+ reportParserWorkerError("jsonParserWorker", makeErrorEvent(), {
+ waiting: ["useParsedTrace"],
+ });
+
+ const [, opts] = mockCaptureException.mock.calls[0]!;
+ expect(opts).toMatchObject({
+ extra: { workerHook: "jsonParserWorker", waiting: ["useParsedTrace"] },
+ });
+ });
+
it("captures a synthesized, legible Error with structured context when event.error is absent", () => {
const event = makeErrorEvent();
reportParserWorkerError("useParsedTrace", event);
diff --git a/web/src/hooks/parserWorkerError.ts b/web/src/hooks/parserWorkerError.ts
index 7719a91332..7485732f60 100644
--- a/web/src/hooks/parserWorkerError.ts
+++ b/web/src/hooks/parserWorkerError.ts
@@ -14,11 +14,12 @@ import { reportWorkerLoadError } from "@/src/utils/reportWorkerLoadError";
export function reportParserWorkerError(
hookName: string,
event: ErrorEvent,
+ extra?: Record<string, unknown>,
): void {
reportWorkerLoadError({
area: "io-parse-worker",
source: hookName,
event,
- extra: { workerHook: hookName },
+ extra: { ...extra, workerHook: hookName },
});
}
diff --git a/web/src/hooks/useParsedTrace.ts b/web/src/hooks/useParsedTrace.ts
index e5d437ac70..2e724ed252 100644
--- a/web/src/hooks/useParsedTrace.ts
+++ b/web/src/hooks/useParsedTrace.ts
@@ -13,78 +13,13 @@
* - Non-blocking: Parsing happens in Web Worker
* - Cached: React Query caches parsed data
* - Progressive: UI renders immediately, data populates when ready
- * - Graceful fallback: Uses sync parsing if Web Workers unavailable
+ * - Graceful fallback: parses on this thread whenever the worker cannot
*/
import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
-import type {
- ParseRequest,
- ParseResponse,
-} from "@/src/workers/json-parser.worker";
import { cheapHash } from "@/src/hooks/parsedIoCacheKey";
-import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";
-
-/**
- * Threshold for using Web Worker vs sync parsing (in characters).
- * Below this: sync parse (faster, no message-passing overhead)
- * Above this: Web Worker (non-blocking, prevents UI freeze)
- */
-const PARSE_IN_WEBWORKER_THRESHOLD = 100_000; // 100KB
-
-/**
- * Estimate the size of a value in characters (for threshold check)
- */
-function estimateSize(value: unknown): number {
- if (value === null || value === undefined) return 0;
- if (typeof value === "string") return value.length;
- // For objects/arrays, estimate via JSON stringification length
- // This is approximate but good enough for threshold decisions
- try {
- return JSON.stringify(value).length;
- } catch {
- return 0;
- }
-}
-
-// Singleton worker instance shared across all hook calls
-let workerInstance: Worker | null = null;
-const pendingCallbacks = new Map<
- string,
- (data: ParseResponse & { error?: string }) => void
->();
-
-function getOrCreateWorker(): Worker | null {
- if (typeof window === "undefined" || !window.Worker) {
- return null; // SSR or no Worker support
- }
-
- if (!workerInstance) {
- try {
- // Next.js will bundle this as a separate chunk
- workerInstance = new Worker(
- new URL("@/src/workers/json-parser.worker.ts", import.meta.url),
- );
-
- workerInstance.onmessage = (e: MessageEvent<ParseResponse>) => {
- const callback = pendingCallbacks.get(e.data.id);
- if (callback) {
- callback(e.data);
- pendingCallbacks.delete(e.data.id);
- }
- };
-
- workerInstance.onerror = (event) => {
- reportParserWorkerError("useParsedTrace", event);
- };
- } catch (error) {
- console.error("[useParsedTrace] Failed to create worker:", error);
- return null;
- }
- }
-
- return workerInstance;
-}
+import { parseIoOffThread } from "@/src/workers/jsonParserWorkerClient";
interface UseParsedTraceParams {
traceId: string;
@@ -93,98 +28,6 @@ interface UseParsedTraceParams {
metadata: unknown;
}
-interface ParsedData {
- input: unknown;
- output: unknown;
- metadata: unknown;
- parseTime: number;
-}
-
-/**
- * Sync parse helper - used for small payloads or when Web Worker unavailable
- */
-async function syncParseTraceData(
- input: unknown,
- output: unknown,
- metadata: unknown,
-): Promise<ParsedData> {
- const { deepParseJsonIterative } = await import("@langfuse/shared");
- const startTime = performance.now();
-
- return {
- input: deepParseJsonIterative(input, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- output: deepParseJsonIterative(output, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- metadata: deepParseJsonIterative(metadata, {
- maxDepth: 50,
- maxSize: 500_000,
- }),
- parseTime: performance.now() - startTime,
- };
-}
-
-/**
- * Parse trace data in Web Worker (or sync for small payloads)
- * Returns a promise that resolves with parsed data
- */
-async function parseTraceData(
- input: unknown,
- output: unknown,
- metadata: unknown,
-): Promise<ParsedData> {
- // Estimate total size to decide sync vs worker
- const totalSize =
- estimateSize(input) + estimateSize(output) + estimateSize(metadata);
-
- // Small payloads: sync parse (faster, no message-passing overhead)
- if (totalSize < PARSE_IN_WEBWORKER_THRESHOLD) {
- return syncParseTraceData(input, output, metadata);
- }
-
- const worker = getOrCreateWorker();
-
- // Fallback to sync parsing if no worker support
- if (!worker) {
- return syncParseTraceData(input, output, metadata);
- }
-
- // Large payloads: parse in Web Worker (non-blocking)
- return new Promise<ParsedData>((resolve, reject) => {
- const parseId = `${Date.now()}-${Math.random()}`;
-
- pendingCallbacks.set(parseId, (result) => {
- pendingCallbacks.delete(parseId);
-
- if (result.error) {
- console.error(`[useParsedTrace] Parse error: ${result.error}`);
- reject(new Error(result.error));
- return;
- }
-
- resolve({
- input: result.parsedInput,
- output: result.parsedOutput,
- metadata: result.parsedMetadata,
- parseTime: result.parseTime ?? 0,
- });
- });
-
- const request: ParseRequest = {
- id: parseId,
- input,
- output,
- metadata,
- };
-
- worker.postMessage(request);
- });
-}
-
export function useParsedTrace({
traceId,
input,
@@ -204,9 +47,8 @@ export function useParsedTrace({
// Parse the data in Web Worker (React Query caches this)
const parseQuery = useQuery({
queryKey: ["parsed-trace", traceId, inputSig, outputSig, metadataSig],
- queryFn: async () => {
- return parseTraceData(input, output, metadata);
- },
+ queryFn: async () =>
+ parseIoOffThread({ source: "useParsedTrace", input, output, metadata }),
enabled: !!(input || output || metadata), // Only run if we have data
staleTime: Infinity, // Parsed data never goes stale (input data is the source of truth)
gcTime: 10 * 60 * 1000, // Keep in cache for 10 minutes after unmount
diff --git a/web/src/pages/api/workers/elk-worker.ts b/web/src/pages/api/workers/elk-worker.ts
new file mode 100644
index 0000000000..ee6b67ecbe
--- /dev/null
+++ b/web/src/pages/api/workers/elk-worker.ts
@@ -0,0 +1,69 @@
+import { createHash } from "node:crypto";
+import { existsSync, readFileSync } from "node:fs";
+import { join } from "node:path";
+import type { NextApiRequest, NextApiResponse } from "next";
+
+/**
+ * Serve elkjs's prebuilt layout worker from a URL that survives a deploy.
+ *
+ * The trace graph used to start this worker from a Turbopack chunk under
+ * `/_next/static/chunks/...`. Worker entrypoints and their chunks are pinned to
+ * the app origin on purpose (browsers reject a cross-origin classic worker), and
+ * the app origin only ever serves the current build — so in any tab that
+ * outlives a deploy the worker 404s and the graph silently drops to the much
+ * smaller main-thread layout budget.
+ *
+ * This path is not content-hashed, so it keeps resolving across deploys: the
+ * build in front of it always has an elkjs to serve. `elk-worker.min.js` is
+ * genuinely standalone (no `importScripts`, no `require`, it installs its own
+ * `self.onmessage`), which is what makes serving the dependency's own file
+ * possible instead of bundling it.
+ *
+ * Reading a prebuilt dependency bundle off disk and serving it from an API route
+ * is the same shape as `/api/docs`; `outputFileTracingIncludes` in
+ * `next.config.mjs` keeps the file in the standalone output.
+ */
+const elkWorkerPath = [
+ join(process.cwd(), "node_modules/elkjs/lib/elk-worker.min.js"),
+ join(process.cwd(), "web/node_modules/elkjs/lib/elk-worker.min.js"),
+].find(existsSync);
+
+if (!elkWorkerPath) {
+ throw new Error("elkjs worker bundle is missing");
+}
+
+const elkWorker = readFileSync(elkWorkerPath);
+const elkWorkerETag = `"${createHash("sha256")
+ .update(elkWorker)
+ .digest("hex")
+ .slice(0, 32)}"`;
+
+/**
+ * A day. The file only changes when elkjs is upgraded, and a tab holding the
+ * previous one is fine: elk-api's message protocol is what both sides speak,
+ * and it is stable across elkjs releases. Revalidation after that is a 304.
+ */
+const MAX_AGE_SECONDS = 86_400;
+
+export default function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+): void {
+ if (req.method !== "GET" && req.method !== "HEAD") {
+ res.setHeader("Allow", "GET, HEAD");
+ res.status(405).end();
+ return;
+ }
+
+ res.setHeader("Content-Type", "text/javascript; charset=utf-8");
+ res.setHeader("Cache-Control", `public, max-age=${MAX_AGE_SECONDS}`);
+ res.setHeader("ETag", elkWorkerETag);
+
+ if (req.headers["if-none-match"] === elkWorkerETag) {
+ res.status(304).end();
+ return;
+ }
+
+ res.status(200);
+ req.method === "HEAD" ? res.end() : res.send(elkWorker);
+}
diff --git a/web/src/utils/reportWorkerLoadError.ts b/web/src/utils/reportWorkerLoadError.ts
index 58731301ec..bfcfe3e9c3 100644
--- a/web/src/utils/reportWorkerLoadError.ts
+++ b/web/src/utils/reportWorkerLoadError.ts
@@ -16,6 +16,15 @@ import { reportError } from "@/src/utils/reportError";
* captureConsoleIntegration — which captures the "error" level — does not
* double-capture a second, opaque event for the same failure).
*
+ * It also CANCELS the event. Per the HTML spec an uncaught error inside a
+ * dedicated worker fires at the `Worker` object and, when that event is not
+ * canceled, is reported again on the owning document — where Sentry's
+ * `globalHandlers` integration captures it a second time, as an unhandled
+ * error, with none of the context below and a separate issue of its own.
+ * Cancelling is what "this error is handled" means, so it belongs in the one
+ * place that handles it rather than in each `onerror`, where it was missing
+ * from all of them.
+ *
* One implementation for every worker: each caller supplies its `area` tag, the
* `source` name the message should read as, and any extra context.
*/
@@ -31,6 +40,8 @@ export function reportWorkerLoadError({
event: ErrorEvent;
extra?: Record<string, unknown>;
}): void {
+ event.preventDefault();
+
const details = `${event.message || "unknown"} @ ${event.filename || "?"}:${event.lineno ?? "?"}:${event.colno ?? "?"}`;
// Prefer the real Error when the worker threw during init; otherwise
diff --git a/web/src/workers/elk-layout.worker.ts b/web/src/workers/elk-layout.worker.ts
deleted file mode 100644
index dd4dc2212e..0000000000
--- a/web/src/workers/elk-layout.worker.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-/**
- * Worker entry for trace-graph ELK layout.
- *
- * elkjs ships its own worker build: loaded without a `document` (i.e. inside a
- * Worker) `elk-worker.min.js` installs its own `self.onmessage` and speaks
- * elk-api's message protocol, so hosting it IS the whole worker — the main thread
- * talks to it through `new ELK({ workerFactory })`.
- *
- * `elk.bundled.js` is NOT usable here: it takes the same branch, then exports no
- * in-process worker class, so constructing ELK inside a worker throws.
- */
-import "elkjs/lib/elk-worker.min.js";
diff --git a/web/src/workers/generated/json-parser.worker.source.ts b/web/src/workers/generated/json-parser.worker.source.ts
new file mode 100644
index 0000000000..4575a7607b
--- /dev/null
+++ b/web/src/workers/generated/json-parser.worker.source.ts
@@ -0,0 +1,9 @@
+/**
+ * GENERATED FILE — DO NOT EDIT.
+ *
+ * Bundled from src/workers/json-parser.worker.ts by scripts/build-json-parser-worker.mjs.
+ * Regenerate with: pnpm --filter web run build:json-parser-worker
+ */
+
+export const JSON_PARSER_WORKER_SOURCE =
+ '"use strict";(()=>{function w(e){return I.test(e)}var I=/^-?[0-9]+$/;function k(e,o){let t=Number.parseFloat(e),u=String(t);if(e===u)return!0;let r=S(e),c=S(u);return!!(r===c||o?.approx===!0&&!w(e)&&c.length>=14&&r.startsWith(c.substring(0,14)))}function S(e){let{start:o,end:t}=R(e),u=e.substring(o,t),r=u.indexOf(".");return r===-1?u:u.substring(0,r)+u.substring(r+1)}function R(e){let o=0;for(e[0]==="-"&&o++;e[o]==="0"||e[o]===".";)o++;let t=e.lastIndexOf("e");for(t===-1&&(t=e.lastIndexOf("E")),t===-1&&(t=e.length);(e[t-1]==="0"||e[t-1]===".")&&t>o;)t--;return{start:o,end:t}}var A=new Set(["__proto__","constructor","prototype"]);function F(e){if(!e.includes("\'")||!(e.includes("{")||e.includes("["))||e.length>1e6)return e;let o=e.trim();if(o[0]!=="{"&&o[0]!=="[")return e;try{let t=o.replace(/\\bTrue\\b/g,"true").replace(/\\bFalse\\b/g,"false").replace(/\\bNone\\b/g,"null").replace(/\'/g,\'"\');return O(t)}catch{return e}}function M(e,o){let t=[],u=new Set,r=e,c=0;for(;;){if(typeof r=="string")c+=r.length>o?4:r.length+2;else if(typeof r=="object"&&r!==null){if(u.has(r))return!0;let n=Array.isArray(r)?null:Object.keys(r),s=Array.isArray(r)?r.length:n.length;if(c+=2+Math.max(0,s-1),c+s>o)return!0;u.add(r),t.push({value:r,keys:n,length:s,index:0})}else c+=String(r).length;if(c>o)return!0;for(;t.length>0;){let n=t[t.length-1];if(n.index<n.length)break;u.delete(n.value),t.pop()}if(t.length===0)return!1;let i=t[t.length-1],a=i.index++;if(i.keys){let n=i.keys[a];if(c+=n.length+3,c>o)return!0;r=i.value[n]}else r=i.value[a]}}function b(e,o={}){let{maxSize:t=5e5,maxDepth:u=3}=o,r=1/0;if(typeof e=="object"&&e!==null){if(M(e,t))return e;r=t}let c={input:e,parent:null,key:null,depth:0,childrenToProcess:0},i=[c],a=new Set;for(;i.length>0;){let n=i[i.length-1];if(a.has(n)){i.pop();continue}let{input:s,depth:m}=n;if(m>=u){n.output=s,a.add(n);continue}if(typeof s=="string"){if(s.length>r){n.output=s,a.add(n);continue}if(K(s)){n.output=s,a.add(n);continue}let d,y=!1;try{d=O(s),y=!0}catch{let l=F(s);l!==s&&(d=l,y=!0)}if(y&&d!==void 0){if(m+1>u){n.output=s,a.add(n);continue}if(n.parsedEntry){let l=n.parsedEntry;if(a.has(l)){n.output=l.output,a.add(n);continue}else continue}else{let l={input:d,parent:n,key:null,depth:m+1,childrenToProcess:0};n.parsedEntry=l,i.push(l);continue}}else{n.output=s,a.add(n);continue}}if(typeof s=="object"&&s!==null){let d=Array.isArray(s),y=d?null:Object.keys(s);if((d?s.length:y.length)===0){n.output=s,a.add(n);continue}n.childrenResults||(n.childrenResults=[]);let g=n.childrenResults;if(g.length===0){if(d){let f=s;for(let p=f.length-1;p>=0;p--){let h={input:f[p],parent:n,key:p,depth:m+1,childrenToProcess:0};g.push(h),i.push(h)}}else{let f=s;for(let p=y.length-1;p>=0;p--){let h=y[p];if(Object.prototype.hasOwnProperty.call(f,h)){let E={input:f[h],parent:n,key:h,depth:m+1,childrenToProcess:0};g.push(E),i.push(E)}}}g.reverse();continue}if(!g.every(f=>a.has(f)))continue;let C=g.some(f=>f.output!==f.input),P=!d&&Object.keys(s).some(f=>A.has(f));if(!C&&!P)n.output=s;else if(d)n.output=g.map(f=>f.output);else{let f={};for(let p of g){let h=p.key;A.has(h)||(f[h]=p.output)}n.output=f}a.add(n);continue}n.output=s,a.add(n)}return c.output}var B=/[\\d.]{13,}|\\d[eE]/,U=/^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/,K=e=>U.test(e.trim()),V=(e,o,t)=>typeof o=="number"&&t?.source!==void 0&&!k(t.source)?t.source:o,O=e=>B.test(e)?JSON.parse(e,V):JSON.parse(e);var x={maxDepth:1/0,maxSize:1e7};self.onmessage=function(e){let{id:o,input:t,output:u,metadata:r}=e.data,c=performance.now();try{let i={id:o,parsedInput:b(t,x),parsedOutput:b(u,x),parsedMetadata:b(r,x),parseTime:performance.now()-c};self.postMessage(i)}catch(i){let a={id:o,parsedInput:t,parsedOutput:u,parsedMetadata:r,parseTime:performance.now()-c,error:i instanceof Error?i.message:String(i)};self.postMessage(a)}};})();\n';
diff --git a/web/src/workers/json-parser.protocol.ts b/web/src/workers/json-parser.protocol.ts
new file mode 100644
index 0000000000..5fa3294316
--- /dev/null
+++ b/web/src/workers/json-parser.protocol.ts
@@ -0,0 +1,41 @@
+/**
+ * Message protocol between the JSON-parser Web Worker and its client.
+ *
+ * Kept in its own module because the worker is compiled separately from the app
+ * bundle (see `scripts/build-json-parser-worker.mjs`): both sides have to agree
+ * on these shapes, and neither may pull the other's runtime code in to do it.
+ */
+
+export interface ParseRequest {
+ id: string;
+ input: unknown;
+ output: unknown;
+ metadata: unknown;
+}
+
+export interface ParseResponse {
+ id: string;
+ parsedInput: unknown;
+ parsedOutput: unknown;
+ parsedMetadata: unknown;
+ parseTime: number;
+ error?: string;
+}
+
+/**
+ * Parse limits off the main thread: no depth cap and 10MB per field, because a
+ * long parse there costs nothing but worker time.
+ */
+export const WORKER_PARSE_LIMITS = {
+ maxDepth: Infinity,
+ maxSize: 10_000_000,
+} as const;
+
+/**
+ * Parse limits on the main thread, used whenever the worker is unavailable.
+ * Deliberately far stricter: this parse blocks rendering.
+ */
+export const MAIN_THREAD_PARSE_LIMITS = {
+ maxDepth: 50,
+ maxSize: 500_000,
+} as const;
diff --git a/web/src/workers/json-parser.worker.ts b/web/src/workers/json-parser.worker.ts
index 29d3326fed..fea853da51 100644
--- a/web/src/workers/json-parser.worker.ts
+++ b/web/src/workers/json-parser.worker.ts
@@ -1,26 +1,23 @@
/**
- * Web Worker for background JSON parsing
+ * Web Worker for background JSON parsing.
*
- * Parses large JSON data off the main thread using the iterative
- * deepParseJsonIterative function to prevent UI blocking.
+ * Parses large trace I/O off the main thread so the UI keeps rendering.
+ *
+ * This file is NOT compiled by Next.js. `scripts/build-json-parser-worker.mjs`
+ * bundles it with esbuild into `generated/json-parser.worker.source.ts`, a plain
+ * string the client starts as a blob (see `jsonParserWorkerClient.ts`). That is
+ * why it imports `deepParseJsonIterative` from its own module rather than from
+ * the `@langfuse/shared` barrel: the barrel pulls ~1MB into a bundle whose whole
+ * job is one 3.7KB function.
*/
-import { deepParseJsonIterative } from "@langfuse/shared";
-
-export interface ParseRequest {
- id: string;
- input: unknown;
- output: unknown;
- metadata: unknown;
-}
+import { deepParseJsonIterative } from "@langfuse/shared/src/utils/json";
-export interface ParseResponse {
- id: string;
- parsedInput: unknown;
- parsedOutput: unknown;
- parsedMetadata: unknown;
- parseTime: number;
-}
+import {
+ WORKER_PARSE_LIMITS,
+ type ParseRequest,
+ type ParseResponse,
+} from "./json-parser.protocol";
self.onmessage = function (e: MessageEvent<ParseRequest>) {
const { id, input, output, metadata } = e.data;
@@ -28,43 +25,27 @@ self.onmessage = function (e: MessageEvent<ParseRequest>) {
const startTime = performance.now();
try {
- // Parse with high limits since we're off the main thread
- const parsedInput = deepParseJsonIterative(input, {
- maxDepth: Infinity,
- maxSize: 10_000_000, // 10MB
- });
-
- const parsedOutput = deepParseJsonIterative(output, {
- maxDepth: Infinity,
- maxSize: 10_000_000,
- });
-
- const parsedMetadata = deepParseJsonIterative(metadata, {
- maxDepth: Infinity,
- maxSize: 10_000_000,
- });
-
- const elapsed = performance.now() - startTime;
-
const response: ParseResponse = {
id,
- parsedInput,
- parsedOutput,
- parsedMetadata,
- parseTime: elapsed,
+ parsedInput: deepParseJsonIterative(input, WORKER_PARSE_LIMITS),
+ parsedOutput: deepParseJsonIterative(output, WORKER_PARSE_LIMITS),
+ parsedMetadata: deepParseJsonIterative(metadata, WORKER_PARSE_LIMITS),
+ parseTime: performance.now() - startTime,
};
self.postMessage(response);
} catch (error) {
- console.error("[json-parser.worker] Parse error:", error);
- // Send back unparsed data on error
- self.postMessage({
+ // Answer with the unparsed data: an exception in here must never leave a
+ // request unanswered, and the client reports the carried error message.
+ const response: ParseResponse = {
id,
parsedInput: input,
parsedOutput: output,
parsedMetadata: metadata,
parseTime: performance.now() - startTime,
error: error instanceof Error ? error.message : String(error),
- });
+ };
+
+ self.postMessage(response);
}
};
diff --git a/web/src/workers/jsonParserWorkerClient.clienttest.ts b/web/src/workers/jsonParserWorkerClient.clienttest.ts
new file mode 100644
index 0000000000..2b8715f0af
--- /dev/null
+++ b/web/src/workers/jsonParserWorkerClient.clienttest.ts
@@ -0,0 +1,350 @@
+import type { ParseRequest, ParseResponse } from "./json-parser.protocol";
+
+const { mockCaptureException } = vi.hoisted(() => ({
+ mockCaptureException: vi.fn(),
+}));
+
+vi.mock("@sentry/nextjs", () => ({
+ captureException: mockCaptureException,
+ addBreadcrumb: vi.fn(),
+}));
+
+/**
+ * The contract this module exists for: a parse request ALWAYS settles.
+ *
+ * The worker used to be started from a bundler chunk that 404s in any tab
+ * outliving a deploy, and `onerror` only reported — it never rejected the
+ * pending callbacks, never cleared the singleton and had no deadline. So the
+ * promise never settled, the Raw tab sat on "Parsing in background…" forever,
+ * and every later payload posted into the same dead worker: one Sentry event,
+ * unboundedly many stuck panels (Sentry LANGFUSE-5TT / -546 / -57N, ~796
+ * events / 14d).
+ */
+
+/** Stands in for the browser Worker: records posts, never runs anything. */
+class FakeWorker {
+ static instances: FakeWorker[] = [];
+ onmessage: ((event: MessageEvent<ParseResponse>) => void) | null = null;
+ onerror: ((event: ErrorEvent) => void) | null = null;
+ posted: ParseRequest[] = [];
+ terminated = false;
+
+ constructor(public readonly url: string) {
+ FakeWorker.instances.push(this);
+ }
+
+ postMessage(request: ParseRequest) {
+ this.posted.push(request);
+ }
+
+ terminate() {
+ this.terminated = true;
+ }
+
+ /** Answer a request the way the real worker would. */
+ answer(request: ParseRequest, response: Partial<ParseResponse> = {}) {
+ this.onmessage?.({
+ data: {
+ id: request.id,
+ parsedInput: "worker-parsed",
+ parsedOutput: null,
+ parsedMetadata: null,
+ parseTime: 12,
+ ...response,
+ },
+ } as MessageEvent<ParseResponse>);
+ }
+
+ fail() {
+ const event = new ErrorEvent("error", {
+ message: "Uncaught NetworkError: the worker script failed to load",
+ filename: "blob:https://app.example/9f2c",
+ lineno: 1,
+ colno: 1,
+ });
+ const preventDefault = vi.spyOn(event, "preventDefault");
+ this.onerror?.(event);
+ return preventDefault;
+ }
+}
+
+/** Over PARSE_IN_WEBWORKER_THRESHOLD, so it is worth a round trip. */
+const BIG_IO = JSON.stringify({ deep: "x".repeat(110_000) });
+/** Under it: parsed on this thread, the worker is never involved. */
+const SMALL_IO = JSON.stringify({ deep: "small" });
+
+// jsdom implements neither, and `URL` itself must stay a real constructor.
+const createObjectURL = vi.fn(() => "blob:fake-worker-url");
+const revokeObjectURL = vi.fn();
+const urlStatics = URL as unknown as {
+ createObjectURL?: unknown;
+ revokeObjectURL?: unknown;
+};
+
+// Module state (the worker instance, the pending map) is per-import.
+async function loadClient() {
+ vi.resetModules();
+ return import("./jsonParserWorkerClient");
+}
+
+const parse = async (
+ client: Awaited<ReturnType<typeof loadClient>>,
+ io: unknown,
+) =>
+ client.parseIoOffThread({
+ source: "useParsedTrace",
+ input: io,
+ output: null,
+ metadata: null,
+ });
+
+describe("parseIoOffThread", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ FakeWorker.instances = [];
+ vi.stubGlobal("Worker", FakeWorker);
+ urlStatics.createObjectURL = createObjectURL;
+ urlStatics.revokeObjectURL = revokeObjectURL;
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ delete urlStatics.createObjectURL;
+ delete urlStatics.revokeObjectURL;
+ });
+
+ it("parses small payloads on this thread without starting a worker", async () => {
+ const client = await loadClient();
+
+ await expect(parse(client, SMALL_IO)).resolves.toMatchObject({
+ input: { deep: "small" },
+ });
+ expect(FakeWorker.instances).toHaveLength(0);
+ expect(createObjectURL).not.toHaveBeenCalled();
+ });
+
+ it("starts the worker from a blob, never from a URL that can 404", async () => {
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+
+ const [worker] = FakeWorker.instances;
+ expect(worker).toBeDefined();
+ expect(worker!.url).toBe("blob:fake-worker-url");
+ expect(createObjectURL).toHaveBeenCalledTimes(1);
+
+ worker!.answer(worker!.posted[0]!);
+ await expect(pending).resolves.toMatchObject({ input: "worker-parsed" });
+ });
+
+ it("reuses one worker and hands it one request at a time", async () => {
+ const client = await loadClient();
+ const first = parse(client, BIG_IO);
+ const second = parse(client, BIG_IO);
+
+ expect(FakeWorker.instances).toHaveLength(1);
+ const worker = FakeWorker.instances[0]!;
+ // The worker is single-threaded; queueing inside it would only hide the
+ // wait from the deadline.
+ expect(worker.posted).toHaveLength(1);
+
+ worker.answer(worker.posted[0]!, { parsedInput: "first" });
+ await expect(first).resolves.toMatchObject({ input: "first" });
+
+ expect(worker.posted).toHaveLength(2);
+ worker.answer(worker.posted[1]!, { parsedInput: "second" });
+ await expect(second).resolves.toMatchObject({ input: "second" });
+ });
+
+ it("ignores an answer to a request it is no longer waiting on", async () => {
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+ const request = worker.posted[0]!;
+
+ worker.answer({ ...request, id: "not-the-one-in-flight" });
+ worker.answer(request, { parsedInput: "the right one" });
+
+ await expect(pending).resolves.toMatchObject({ input: "the right one" });
+ });
+
+ it("rejects when the worker reports a parse failure", async () => {
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+
+ worker.answer(worker.posted[0]!, { error: "Maximum call stack exceeded" });
+
+ await expect(pending).rejects.toThrow("Maximum call stack exceeded");
+ });
+
+ describe("when the worker script fails to load", () => {
+ it("settles the in-flight request on this thread instead of hanging", async () => {
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+
+ worker.fail();
+
+ // The payload is really parsed, not handed back untouched.
+ await expect(pending).resolves.toMatchObject({
+ input: { deep: "x".repeat(110_000) },
+ });
+ expect(worker.terminated).toBe(true);
+ expect(revokeObjectURL).toHaveBeenCalledWith("blob:fake-worker-url");
+ });
+
+ it("cancels the event so the browser does not re-report it on window", async () => {
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+
+ const preventDefault = worker.fail();
+ await pending;
+
+ // Uncanceled, the same failure fires again at window.onerror, where
+ // Sentry captures it unhandled: ~half of this error family's volume.
+ expect(preventDefault).toHaveBeenCalledTimes(1);
+ expect(mockCaptureException).toHaveBeenCalledTimes(1);
+ });
+
+ it("stops starting a worker that cannot run", async () => {
+ const client = await loadClient();
+ const first = parse(client, BIG_IO);
+ FakeWorker.instances[0]!.fail();
+ await first;
+
+ await expect(parse(client, BIG_IO)).resolves.toMatchObject({
+ input: { deep: "x".repeat(110_000) },
+ });
+ expect(FakeWorker.instances).toHaveLength(1);
+ });
+ });
+
+ describe("waiting in the queue", () => {
+ it("starts a request's deadline when the worker takes it, not when it is asked for", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const first = parse(client, BIG_IO);
+ const second = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+
+ // A slow but entirely healthy first parse, answered just inside its own
+ // deadline. The second request has been waiting for all of it.
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS - 1_000);
+ worker.answer(worker.posted[0]!, { parsedInput: "slow but fine" });
+ await expect(first).resolves.toMatchObject({ input: "slow but fine" });
+
+ // Timing the queued request from when it was asked for would fire its
+ // deadline here and kill a worker that is answering normally.
+ await vi.advanceTimersByTimeAsync(2_000);
+ expect(worker.terminated).toBe(false);
+ expect(mockCaptureException).not.toHaveBeenCalled();
+
+ worker.answer(worker.posted[1]!, { parsedInput: "second" });
+ await expect(second).resolves.toMatchObject({ input: "second" });
+ });
+
+ it("re-dispatches what was only queued instead of parsing it here", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const inFlight = parse(client, BIG_IO);
+ const queued = parse(client, BIG_IO);
+ const dead = FakeWorker.instances[0]!;
+
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+ // Only the request the worker actually held is unanswerable; the one
+ // behind it was never tried, so it does not belong on the main thread.
+ await expect(inFlight).resolves.toMatchObject({
+ input: { deep: "x".repeat(110_000) },
+ });
+ expect(dead.terminated).toBe(true);
+
+ expect(FakeWorker.instances).toHaveLength(2);
+ const fresh = FakeWorker.instances[1]!;
+ expect(fresh.posted).toHaveLength(1);
+ fresh.answer(fresh.posted[0]!, { parsedInput: "ran on the new worker" });
+ await expect(queued).resolves.toMatchObject({
+ input: "ran on the new worker",
+ });
+ });
+ });
+
+ describe("when the worker dies without saying so", () => {
+ it("falls back to this thread once the deadline passes", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+
+ await expect(pending).resolves.toMatchObject({
+ input: { deep: "x".repeat(110_000) },
+ });
+ expect(worker.terminated).toBe(true);
+ });
+
+ it("reports the outcome, which is otherwise invisible", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+ await pending;
+
+ expect(mockCaptureException).toHaveBeenCalledTimes(1);
+ const [error, options] = mockCaptureException.mock.calls[0]!;
+ expect((error as Error).message).toContain("did not answer within");
+ expect(options).toMatchObject({
+ tags: { area: "io-parse-worker", outcome: "deadline-fallback" },
+ extra: { workerHook: "useParsedTrace", payloadChars: BIG_IO.length },
+ });
+ });
+
+ it("does not retire the worker on a single miss", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const first = parse(client, BIG_IO);
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+ await first;
+
+ parse(client, BIG_IO).catch(() => undefined);
+ expect(FakeWorker.instances).toHaveLength(2);
+ });
+
+ it("retires it once missing the deadline is the pattern", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+
+ for (let attempt = 0; attempt < 2; attempt++) {
+ const pending = parse(client, BIG_IO);
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+ await pending;
+ }
+
+ await parse(client, BIG_IO);
+ expect(FakeWorker.instances).toHaveLength(2);
+ });
+
+ it("ignores an answer that arrives after the deadline", async () => {
+ vi.useFakeTimers();
+ const client = await loadClient();
+ const pending = parse(client, BIG_IO);
+ const worker = FakeWorker.instances[0]!;
+ const request = worker.posted[0]!;
+
+ await vi.advanceTimersByTimeAsync(client.PARSE_DEADLINE_MS);
+ await pending;
+
+ // A terminated worker cannot really answer, but a late message must not
+ // settle a request twice or throw.
+ expect(() => worker.answer(request)).not.toThrow();
+ await expect(pending).resolves.toMatchObject({
+ input: { deep: "x".repeat(110_000) },
+ });
+ });
+ });
+});
diff --git a/web/src/workers/jsonParserWorkerClient.ts b/web/src/workers/jsonParserWorkerClient.ts
new file mode 100644
index 0000000000..d01ce2084d
--- /dev/null
+++ b/web/src/workers/jsonParserWorkerClient.ts
@@ -0,0 +1,327 @@
+/**
+ * Client for the JSON-parser Web Worker.
+ *
+ * Owns the single worker instance, the in-flight requests, and every way the
+ * worker can stop answering. Both `useParsedTrace` and `useParsedObservation`
+ * go through here; they used to carry a copy of this logic each, which is how
+ * the same missing recovery path came to exist twice.
+ *
+ * Two properties this module exists to guarantee:
+ *
+ * 1. **The worker has no URL.** It is started from a blob built out of a string
+ * bundled at build time (`generated/json-parser.worker.source.ts`). A worker
+ * loaded from `/_next/static/chunks/...` 404s in any tab that outlives a
+ * deploy: those chunks are deliberately pinned to the app origin, and the app
+ * origin only ever serves the current build. A blob cannot 404, and worker
+ * and main thread always come from the same build.
+ * 2. **A request always settles.** Every way the worker can stop answering —
+ * script error, silent death, a parse that never comes back — falls back to
+ * parsing on this thread. A request that never settles leaves the Raw tab on
+ * "Parsing in background…" for as long as the tab is open, and because the
+ * instance is reused, every later payload posts into the same dead worker:
+ * one reported error, unboundedly many stuck panels.
+ */
+
+import { deepParseJsonIterative } from "@langfuse/shared/src/utils/json";
+
+import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";
+import { reportError } from "@/src/utils/reportError";
+
+import { JSON_PARSER_WORKER_SOURCE } from "./generated/json-parser.worker.source";
+import {
+ MAIN_THREAD_PARSE_LIMITS,
+ type ParseRequest,
+ type ParseResponse,
+} from "./json-parser.protocol";
+
+/**
+ * Payload size (in characters) from which parsing moves off the main thread.
+ * Below it the message round trip costs more than the parse.
+ */
+export const PARSE_IN_WEBWORKER_THRESHOLD = 100_000; // 100KB
+
+/**
+ * How long one request may stay unanswered before the worker counts as dead.
+ * A worker whose script fails reports through `onerror` within milliseconds;
+ * this covers the silent deaths — OOM, a killed thread, an extension — where
+ * nothing is reported at all. Generous on purpose: it is a liveness check, not
+ * a performance budget.
+ */
+export const PARSE_DEADLINE_MS = 30_000;
+
+/**
+ * After this many missed deadlines the worker is not started again for the life
+ * of the tab. One slow payload should not retire it; a worker that keeps dying
+ * should not cost every later parse another full deadline.
+ */
+const MAX_DEADLINE_MISSES = 2;
+
+export interface ParsedIo {
+ input: unknown;
+ output: unknown;
+ metadata: unknown;
+ parseTime: number;
+}
+
+interface QueuedParse {
+ request: ParseRequest;
+ /** Owning hook, for Sentry context. */
+ source: string;
+ payloadChars: number;
+ resolve: (parsed: ParsedIo) => void;
+ reject: (error: Error) => void;
+}
+
+interface DispatchedParse extends QueuedParse {
+ timer: ReturnType<typeof setTimeout>;
+}
+
+let workerInstance: Worker | null = null;
+let workerObjectUrl: string | null = null;
+/** Set when the worker will not run — parse on this thread from now on. */
+let workerUnavailable = false;
+let deadlineMisses = 0;
+let requestCounter = 0;
+/**
+ * One request is with the worker at a time, the rest wait here.
+ *
+ * The worker is single-threaded, so posting everything at once only moves the
+ * queue inside it — where the deadline cannot see it. A request waiting behind
+ * a slow parse would then burn its deadline without the worker ever having
+ * looked at it, and kill a perfectly healthy worker. Holding the queue on this
+ * side is what makes the deadline mean "the worker stopped answering the
+ * request it is actually working on".
+ */
+let inFlight: DispatchedParse | null = null;
+const queue: QueuedParse[] = [];
+
+/** Estimate a value's size in characters, for the threshold check only. */
+function estimateSize(value: unknown): number {
+ if (value === null || value === undefined) return 0;
+ if (typeof value === "string") return value.length;
+ try {
+ return JSON.stringify(value).length;
+ } catch {
+ return 0;
+ }
+}
+
+/**
+ * Parse on this thread, under much stricter limits than the worker uses: this
+ * one blocks rendering, so a pathological payload must not block it for long.
+ */
+function parseOnThisThread(
+ input: unknown,
+ output: unknown,
+ metadata: unknown,
+): ParsedIo {
+ const startTime = performance.now();
+ return {
+ input: deepParseJsonIterative(input, MAIN_THREAD_PARSE_LIMITS),
+ output: deepParseJsonIterative(output, MAIN_THREAD_PARSE_LIMITS),
+ metadata: deepParseJsonIterative(metadata, MAIN_THREAD_PARSE_LIMITS),
+ parseTime: performance.now() - startTime,
+ };
+}
+
+/** {@link parseOnThisThread} as a promise, so a throwing parse rejects. */
+function parseHere(
+ input: unknown,
+ output: unknown,
+ metadata: unknown,
+): Promise<ParsedIo> {
+ try {
+ return Promise.resolve(parseOnThisThread(input, output, metadata));
+ } catch (error) {
+ return Promise.reject(
+ error instanceof Error ? error : new Error(String(error)),
+ );
+ }
+}
+
+/** Terminate the worker and release its blob URL. Leaves the queue alone. */
+function disposeWorker() {
+ workerInstance?.terminate();
+ workerInstance = null;
+ if (workerObjectUrl) {
+ URL.revokeObjectURL(workerObjectUrl);
+ workerObjectUrl = null;
+ }
+}
+
+function settleHere(entry: QueuedParse) {
+ const { input, output, metadata } = entry.request;
+ parseHere(input, output, metadata).then(entry.resolve, entry.reject);
+}
+
+/**
+ * Drop the worker and decide what happens to the work it was holding.
+ *
+ * Terminating is the only way to stop a worker mid-parse, and it is also what
+ * makes answering elsewhere safe: nothing can arrive twice afterwards. The
+ * request that was with the worker is settled on this thread, because it is the
+ * one nobody answered; whatever was still queued has not been tried yet, so it
+ * goes to a fresh worker rather than blocking rendering for no reason.
+ */
+function restartWorker() {
+ disposeWorker();
+ const abandoned = inFlight;
+ inFlight = null;
+ if (abandoned) {
+ clearTimeout(abandoned.timer);
+ settleHere(abandoned);
+ }
+ pump();
+}
+
+/** Hand the next queued request to the worker, or to this thread if there is none. */
+function pump() {
+ if (inFlight || queue.length === 0) return;
+
+ const worker = getOrCreateWorker();
+ if (!worker) {
+ // No worker to wait for, so nothing is gained by holding the queue.
+ while (queue.length > 0) settleHere(queue.shift()!);
+ return;
+ }
+
+ const next = queue.shift()!;
+ inFlight = {
+ ...next,
+ // Started here, not when the caller asked: a request that spent ten seconds
+ // behind another parse has not been ignored for ten seconds.
+ timer: setTimeout(onDeadline, PARSE_DEADLINE_MS),
+ };
+ worker.postMessage(next.request);
+}
+
+function handleMessage(event: MessageEvent<ParseResponse>) {
+ const entry = inFlight;
+ // Anything but the request in flight is a straggler from a worker that was
+ // already given up on, and its caller has been answered.
+ if (!entry || entry.request.id !== event.data.id) return;
+ clearTimeout(entry.timer);
+ inFlight = null;
+
+ if (event.data.error) {
+ entry.reject(new Error(event.data.error));
+ } else {
+ entry.resolve({
+ input: event.data.parsedInput,
+ output: event.data.parsedOutput,
+ metadata: event.data.parsedMetadata,
+ parseTime: event.data.parseTime ?? 0,
+ });
+ }
+
+ pump();
+}
+
+function handleError(event: ErrorEvent) {
+ // `onerror` means the worker SCRIPT failed, not that a parse threw — a parse
+ // that throws answers over the message channel. Nothing will make this blob
+ // run, so stop trying for the life of the tab and degrade to this thread.
+ workerUnavailable = true;
+ reportParserWorkerError("jsonParserWorker", event, {
+ waiting: [
+ ...new Set([inFlight, ...queue].flatMap((e) => (e ? [e.source] : []))),
+ ],
+ });
+ restartWorker();
+}
+
+function onDeadline() {
+ const entry = inFlight;
+ if (!entry) return;
+
+ deadlineMisses += 1;
+ if (deadlineMisses >= MAX_DEADLINE_MISSES) workerUnavailable = true;
+
+ // The only instrument that can see this symptom at all. A worker that dies
+ // without firing `onerror` reports nothing, and the stuck panel it used to
+ // leave behind was invisible in Sentry by construction.
+ reportError(
+ new Error(
+ `[${entry.source}] JSON parse worker did not answer within ${PARSE_DEADLINE_MS}ms`,
+ ),
+ {
+ area: "io-parse-worker",
+ tags: { outcome: "deadline-fallback" },
+ extra: {
+ workerHook: entry.source,
+ payloadChars: entry.payloadChars,
+ queued: queue.length,
+ deadlineMisses,
+ workerRetired: workerUnavailable,
+ },
+ warnMessage: `${entry.source} JSON parse worker missed its deadline, parsing on the main thread instead`,
+ },
+ );
+
+ restartWorker();
+}
+
+function getOrCreateWorker(): Worker | null {
+ if (workerUnavailable) return null;
+ if (typeof window === "undefined" || !window.Worker) return null;
+ if (workerInstance) return workerInstance;
+
+ try {
+ workerObjectUrl = URL.createObjectURL(
+ new Blob([JSON_PARSER_WORKER_SOURCE], { type: "text/javascript" }),
+ );
+ const worker = new Worker(workerObjectUrl);
+ worker.onmessage = handleMessage;
+ worker.onerror = handleError;
+ workerInstance = worker;
+ } catch (error) {
+ workerUnavailable = true;
+ disposeWorker();
+ reportError(error, {
+ area: "io-parse-worker",
+ warnMessage: "could not start the JSON parse worker",
+ });
+ return null;
+ }
+
+ return workerInstance;
+}
+
+/**
+ * Parse trace or observation I/O, off the main thread when the payload is big
+ * enough to be worth the round trip.
+ *
+ * Never rejects because of worker trouble: anything the worker cannot do is
+ * done on this thread instead. It rejects only when the parse itself fails.
+ */
+export function parseIoOffThread({
+ source,
+ input,
+ output,
+ metadata,
+}: {
+ /** Owning hook, for Sentry context. */
+ source: string;
+ input: unknown;
+ output: unknown;
+ metadata: unknown;
+}): Promise<ParsedIo> {
+ const payloadChars =
+ estimateSize(input) + estimateSize(output) + estimateSize(metadata);
+
+ if (payloadChars < PARSE_IN_WEBWORKER_THRESHOLD || workerUnavailable) {
+ return parseHere(input, output, metadata);
+ }
+
+ return new Promise<ParsedIo>((resolve, reject) => {
+ const id = `${Date.now()}-${++requestCounter}`;
+ queue.push({
+ request: { id, input, output, metadata },
+ source,
+ payloadChars,
+ resolve,
+ reject,
+ });
+ pump();
+ });
+}