diff --git a/packages/studio/src/captions/hooks/useCaptionSync.ts b/packages/studio/src/captions/hooks/useCaptionSync.ts index 8f22e5dd6e..1e4cee4193 100644 --- a/packages/studio/src/captions/hooks/useCaptionSync.ts +++ b/packages/studio/src/captions/hooks/useCaptionSync.ts @@ -118,7 +118,10 @@ export function useCaptionSync(projectId: string | null) { .catch((error: unknown) => { // Caption auto-save is a data-loss path: surface it to the user, not // just telemetry. pendingRef stays true so beforeunload still warns. - trackEvent("studio_caption_autosave_failed", { error: String(error) }); + trackEvent("studio_caption_autosave_failed", { + error_name: error instanceof Error ? error.name : typeof error, + error: String(error), + }); useCaptionStore.getState().setSyncError(held ?? "Caption changes couldn't be saved"); }); }, []); diff --git a/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx b/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx index 48f24c53fa..4e8f743eb8 100644 --- a/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx +++ b/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx @@ -797,6 +797,7 @@ describe("useSdkSession unavailable telemetry", () => { expect(trackMock).toHaveBeenCalledWith("sdk_session_unavailable", { stage: "open", error: "unparseable composition", + error_name: "Error", }); await act(async () => root.unmount()); }); diff --git a/packages/studio/src/hooks/useSdkSession.ts b/packages/studio/src/hooks/useSdkSession.ts index b6e433a130..51e3dde287 100644 --- a/packages/studio/src/hooks/useSdkSession.ts +++ b/packages/studio/src/hooks/useSdkSession.ts @@ -342,6 +342,7 @@ function disposeSdkSession(session: Composition): void { session.dispose(); } catch (error) { trackStudioEvent("sdk_session_dispose_failed", { + error_name: error instanceof Error ? error.name : typeof error, error: error instanceof Error ? error.message : String(error), }); } @@ -510,6 +511,7 @@ export function useSdkSession( // bucket was a network error wearing a parser's label. trackStudioEvent("sdk_session_unavailable", { stage: "open", + error_name: error instanceof Error ? error.name : typeof error, error: error instanceof Error ? error.message : String(error), }); } diff --git a/packages/studio/src/telemetry/eventShape.test.ts b/packages/studio/src/telemetry/eventShape.test.ts index 6e1091f41c..1f6f87d03f 100644 --- a/packages/studio/src/telemetry/eventShape.test.ts +++ b/packages/studio/src/telemetry/eventShape.test.ts @@ -82,8 +82,9 @@ describe("Studio analytics leave the machine without paths or selectors", () => selector_kind: "attribute", target_id_kind: "id", status_code: 403, - dropped_fields: "detail,error_message", + dropped_fields: "detail", }); + expect(first?.error_message_shape).toMatch(/^[0-9a-f]{8}$/); expect(first?.file_path_hash).toMatch(/^[0-9a-f]{8}$/); // The same path from the same install groups; the salt that keys it never leaves. expect(second?.file_path_hash).toBe(first?.file_path_hash); @@ -100,6 +101,51 @@ describe("Studio analytics leave the machine without paths or selectors", () => expect(batch[0]?.properties.tab_id).not.toBe("custom"); }); + const PROJECT_FRAME = + "http://localhost:3002/api/projects/acme-launch/preview/compositions/intro.html:12:3"; + const STACK = [ + "Error: Failed to save compositions/intro.html (403): forbidden path: /Users/ana/客户/x.html", + ` at mountClip (${PROJECT_FRAME})`, + ` at commitEdit (${location.origin}/assets/index-B4x2.js:40:7)`, + ` at ${location.origin}/assets/vendor-C9.js:1:99`, + ].join("\n"); + const LEAKS = ["acme-launch", "intro.html", "客户", "/Users/ana", "forbidden path", "mountClip"]; + + it.each(["studio:*", "studio_*"] as const)( + "the %s transport sends an error's class, a shape and its own frames, never its words", + async (transport) => { + await send(transport, "crash", { + error_message: + "Failed to save compositions/intro.html (403): forbidden path: /Users/ana/客户/x.html", + error_name: "StudioSaveHttpError", + stack_trace: STACK, + component_stack: `\n at ClipRow (${PROJECT_FRAME})\n at Timeline (${location.origin}/assets/index-B4x2.js:9:1)`, + }); + await send(transport, "unhandled_promise_rejection", { + error_message: "acme-launch secret", + error_name: null, + stack_trace: null, + }); + await send(transport, "unhandled_error", { + error: "x", + filename: PROJECT_FRAME.replace(/:\d+:\d+$/, ""), + }); + const { body, batch } = await sent(); + + for (const text of LEAKS) expect(body).not.toContain(text); + const [crash, rejection, uncaught] = batch.map((e) => e.properties); + expect(crash).toMatchObject({ + error_name: "StudioSaveHttpError", + error_message_status: 403, + stack_trace: "?\ncommitEdit@index-B4x2.js:40:7\n@vendor-C9.js:1:99", + component_stack: "ClipRow\nTimeline", + }); + expect(crash?.error_message_shape).toMatch(/^[0-9a-f]{8}$/); + expect(rejection?.error_message_shape).toMatch(/^[0-9a-f]{8}$/); + expect(uncaught?.dropped_fields).toBe("filename"); + }, + ); + it("leaves feedback text the person typed to send", async () => { await send("studio_*", "studio_feedback", { comment: "export of compositions/intro.html froze", @@ -108,3 +154,36 @@ describe("Studio analytics leave the machine without paths or selectors", () => expect(batch[0]?.properties.comment).toBe("export of compositions/intro.html froze"); }); }); + +describe("errors leave no project name, even shaped", () => { + const shape = async (properties: Record) => + (await import("./eventShape")).shapeEventProperties("studio:crash", properties); + + it("keeps only frames with an absolute address on Studio's own origin", async () => { + const shaped = await shape({ + stack_trace: [ + "Error: acme-launch@acme-launch.js:1:2", + " at secretHandler (/home/ana/acme-launch/server.js:10:5)", + " at secretHandler (acme-launch-intro.js:10:5)", + ].join("\n"), + filename: "acme-launch.js", + }); + expect(JSON.stringify(shaped)).not.toMatch(/acme|secretHandler/); + expect(shaped.dropped_fields).toBe("filename"); + }); + + it("gives two projects' same failure the same shape", async () => { + const pairs = [ + ["acme-launch", "other-proj"].map( + (name) => + `Failed to fetch dynamically imported module: http://localhost/api/projects/${name}/preview/x.js`, + ), + ["#acme-title", "#other-title"].map((id) => `GSAP target ${id} not found.`), + ]; + for (const [a, b] of pairs) { + expect((await shape({ error_message: a! })).error_message_shape).toBe( + (await shape({ error_message: b! })).error_message_shape, + ); + } + }); +}); diff --git a/packages/studio/src/telemetry/eventShape.ts b/packages/studio/src/telemetry/eventShape.ts index 740059dbbd..b3bd76daed 100644 --- a/packages/studio/src/telemetry/eventShape.ts +++ b/packages/studio/src/telemetry/eventShape.ts @@ -14,9 +14,9 @@ const PATH_KEYS = new Set([ "path", "composition", "render_composition", - "filename", ]); const SELECTOR_KEYS = new Set(["target_selector", "selector", "target_id", "animationId"]); +const MESSAGE_KEYS = new Set(["error_message", "error", "render_error", "render_stage"]); // Text the person typed to send to us on purpose. const KEPT_AS_TYPED: Record> = { studio_feedback: new Set(["comment"]), @@ -96,6 +96,52 @@ function selectorFacts(key: string, value: string): Facts { return { [`${key}_kind`]: kind, [`${key}_hash`]: hashOf(s) }; } +function messageFacts(key: string, value: string): Facts { + const line = redactTelemetryString(value.split("\n", 1)[0] ?? "", Number.MAX_SAFE_INTEGER) + .replace(/\b(?:https?|blob|data|file|[a-z-]+-extension):\S*/gi, "") + .replace(/(^|[\s(])[#.][A-Za-z_][\w-]*/g, "$1") + .replace(/(["'`]).*?\1/g, "?") + .replace(/\d+/g, "#"); + const status = /(?:\(|HTTP |status |failed: )([1-5]\d\d)\b/.exec(value)?.[1]; + return { + [`${key}_shape`]: hashOf(line), + ...(status ? { [`${key}_status`]: Number(status) } : {}), + }; +} + +function bundleFile(url: string): string | null { + if (!/^https?:\/\//i.test(url)) return null; + try { + const at = new URL(url); + if (at.origin !== location.origin || at.pathname.startsWith("/api/")) return null; + const name = at.pathname.split("/").pop() ?? ""; + return /^[\w.-]+\.(?:m?js|tsx?|jsx?)$/.test(name) ? name : null; + } catch { + return null; + } +} + +const V8_FRAME = /^\s*at (?:(.+?) \()?(.+?):(\d+):(\d+)\)?\s*$/; +const GECKO_FRAME = /^(.*?)@(.+?):(\d+):(\d+)\s*$/; + +function shapedFrames(stack: string): string { + const frames: string[] = []; + for (const line of stack.split("\n")) { + const m = V8_FRAME.exec(line) ?? GECKO_FRAME.exec(line); + if (!m) continue; + const file = bundleFile(m[2] ?? ""); + frames.push(file ? `${m[1]?.trim() || ""}@${file}:${m[3]}:${m[4]}` : "?"); + if (frames.length === 20) break; + } + return frames.join("\n"); +} + +const componentNames = (stack: string): string => + [...stack.matchAll(/^\s*(?:at|in)\s+([A-Za-z_$][\w$.]*)/gm)] + .slice(0, 30) + .map((m) => m[1]) + .join("\n"); + const ATTRIBUTE_SELECTOR = /\[[\w:-]+(?:[~|^$*]?=[^\]]*)?\]/; const ID_OR_CLASS = /^\s*[#.][A-Za-z_-][\w-]*/; const HEX_COLOUR = /^#[0-9a-f]{3,8}$/i; @@ -107,6 +153,23 @@ function readsAsPathOrSelector(value: string): boolean { ); } +type Shaper = (key: string, value: string) => Facts | null; +const single = + (shape: (value: string) => string | null): Shaper => + (key, value) => { + const shaped = shape(value); + return shaped === null ? null : { [key]: shaped }; + }; +const otherValue: Shaper = (key, value) => (readsAsPathOrSelector(value) ? null : { [key]: value }); +const SHAPERS: Record = { + ...Object.fromEntries([...MESSAGE_KEYS].map((key) => [key, messageFacts])), + ...Object.fromEntries([...PATH_KEYS].map((key) => [key, pathFacts])), + ...Object.fromEntries([...SELECTOR_KEYS].map((key) => [key, selectorFacts])), + stack_trace: single(shapedFrames), + component_stack: single(componentNames), + filename: single(bundleFile), +}; + /** A caller's properties as they may leave the machine; runs before the transport's own fields. */ export function shapeEventProperties( event: string, @@ -118,15 +181,11 @@ export function shapeEventProperties( for (const [key, value] of Object.entries(properties)) { if (typeof value !== "string" || value === "" || kept?.has(key)) { out[key] = value; - } else if (PATH_KEYS.has(key)) { - Object.assign(out, pathFacts(key, value)); - } else if (SELECTOR_KEYS.has(key)) { - Object.assign(out, selectorFacts(key, value)); - } else if (readsAsPathOrSelector(value)) { - dropped.push(key); - } else { - out[key] = value; + continue; } + const facts = (SHAPERS[key] ?? otherValue)(key, value); + if (facts) Object.assign(out, facts); + else dropped.push(key); } if (dropped.length > 0) out.dropped_fields = dropped.join(","); return out; diff --git a/packages/studio/src/utils/sdkEditTransaction.ts b/packages/studio/src/utils/sdkEditTransaction.ts index 69f6c70a94..7cb1153f4e 100644 --- a/packages/studio/src/utils/sdkEditTransaction.ts +++ b/packages/studio/src/utils/sdkEditTransaction.ts @@ -140,6 +140,7 @@ export function failedCutover( family, error_kind: cutoverErrorKind(err), ...(err instanceof StudioSaveHttpError ? { status: err.statusCode } : {}), + error_name: err.name, error: err.message, }); return { status: "failed", error: err }; @@ -273,7 +274,8 @@ function refreshCommittedEdit(after: string, deps: CutoverDeps, options?: Cutove if (deps.refresh) deps.refresh(after); else if (!options?.skipRefresh) deps.reloadPreview(); } catch (error) { - trackStudioEvent("sdk_cutover_refresh_failed", { error: asCutoverError(error).message }); + const err = asCutoverError(error); + trackStudioEvent("sdk_cutover_refresh_failed", { error_name: err.name, error: err.message }); } } @@ -306,9 +308,11 @@ async function commitCandidateEdit( // candidate here can make all three authorities disagree (or dispose the // now-live session). Production publishers are non-throwing; keep the // durable commit authoritative and surface this post-commit fault. + const err = asCutoverError(error); trackStudioEvent("sdk_cutover_publish_failed", { path: targetPath, - error: asCutoverError(error).message, + error_name: err.name, + error: err.message, }); } if (refreshTarget) refreshCommittedEdit(edit.after, deps, options); diff --git a/packages/studio/src/utils/sdkResolverShadow.ts b/packages/studio/src/utils/sdkResolverShadow.ts index bac30f7d6a..15264c855f 100644 --- a/packages/studio/src/utils/sdkResolverShadow.ts +++ b/packages/studio/src/utils/sdkResolverShadow.ts @@ -255,20 +255,23 @@ import { recordAttempt } from "./sdkResolverAttempts"; // ─── Telemetry ──────────────────────────────────────────────────────────────── -// Redact all user-content values before telemetry: style values and text both -// carry user data. Keep only the length so we can detect truncation without -// leaking the actual bytes. +// User content leaves as its length only (style values, text, error words); the selector-built +// animationId goes too, as the event's own animationId is shaped. function redactValue(value: string | null | undefined): string | null | undefined { if (value == null) return value; return `[redacted len=${value.length}]`; } -function redactMismatches(mismatches: SdkResolverMismatch[]): SdkResolverMismatch[] { - return mismatches.map((m) => ({ - ...m, - expected: redactValue(m.expected), - actual: redactValue(m.actual), - })); +function redactMismatches(mismatches: SdkResolverMismatch[]): string { + return JSON.stringify( + mismatches.map((m) => ({ + ...m, + expected: redactValue(m.expected), + actual: redactValue(m.actual), + error: redactValue(m.error), + animationId: undefined, + })), + ); } /** @@ -303,7 +306,7 @@ function reportEmptySession(session: Composition, opLabel: string): boolean { sessionEmpty: true, sessionElementCount: 0, mismatchCount: 1, - mismatches: JSON.stringify([{ kind: "session_empty" } satisfies SdkResolverMismatch]), + mismatches: redactMismatches([{ kind: "session_empty" } satisfies SdkResolverMismatch]), }); } return true; @@ -372,7 +375,7 @@ export function runResolverShadow( // attribute count came back 0 — see the sourceHfIdCount comment above. ...(strictCount === 0 ? { sourceLooseMatchOnly: true } : {}), mismatchCount: mismatches.length, - mismatches: JSON.stringify(redactMismatches(mismatches)), + mismatches: redactMismatches(mismatches), }); } catch { // never propagate from the shadow path @@ -457,7 +460,7 @@ export async function recordResolverParity( // fail-open without the suppression/count checks" from "no reader wired". ...(sourceReadFailed ? { sourceReadFailed: true } : {}), mismatchCount: 1, - mismatches: JSON.stringify([ + mismatches: redactMismatches([ { kind: "element_not_found", hfId } satisfies SdkResolverMismatch, ]), }); @@ -566,7 +569,7 @@ export async function recordAnimationResolverParity( ...(diskChecked ? { diskChecked: true } : {}), ...(sourceReadFailed ? { sourceReadFailed: true } : {}), mismatchCount: 1, - mismatches: JSON.stringify([{ kind: "animation_not_found" } satisfies SdkResolverMismatch]), + mismatches: redactMismatches([{ kind: "animation_not_found" } satisfies SdkResolverMismatch]), }); } catch { // never propagate from the shadow path