Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(platform): stop duplicate and noisy Sentry reports (#15433)

Nicholas Tindlef2d9d6e8c9ff
diff --git a/autogpt_platform/backend/backend/copilot/sdk/limit_stop_logging_test.py b/autogpt_platform/backend/backend/copilot/sdk/limit_stop_logging_test.py
new file mode 100644
index 0000000000..8a5bd854ed
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/limit_stop_logging_test.py
@@ -0,0 +1,77 @@
+"""A turn the platform stops on purpose must not page anyone.
+
+The SDK ends a turn with ``error_max_budget_usd`` or ``error_max_turns`` when a
+limit we set does its job. The user is told how to carry on, and the message
+must survive a refresh, but Sentry's LoggingIntegration files every ERROR record
+as an issue, so these stops buried AUTOGPT-SERVER-8C4 under expected outcomes.
+"""
+
+from __future__ import annotations
+
+import logging
+from datetime import datetime, timezone
+from unittest.mock import MagicMock
+
+import pytest
+from claude_agent_sdk import ResultMessage, SystemMessage
+
+from backend.copilot.constants import COPILOT_ERROR_PREFIX
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.response_model import StreamError
+from backend.copilot.sdk.response_adapter import SDKResponseAdapter
+from backend.copilot.sdk.service import _dispatch_response, _StreamAccumulator
+
+_NOW = datetime(2024, 1, 1, tzinfo=timezone.utc)
+
+
+ "subtype, code, level",
+ [
+ ("error_max_budget_usd", "max_budget_exhausted", logging.INFO),
+ ("error_max_turns", "max_turns_exhausted", logging.INFO),
+ # A turn that really failed still reports.
+ ("error_during_execution", "sdk_error", logging.ERROR),
+ ],
+)
+def test_a_limit_stop_reaches_the_user_without_an_error_log(
+ subtype: str, code: str, level: int, caplog: pytest.LogCaptureFixture
+):
+ session = ChatSession(
+ session_id="session-1",
+ user_id="user-1",
+ title="A titled chat",
+ messages=[],
+ usage=[],
+ started_at=_NOW,
+ updated_at=_NOW,
+ )
+ ctx = MagicMock(session=session, codex_gateway=None, log_prefix="[test]")
+ acc = _StreamAccumulator(
+ assistant_response=ChatMessage(role="assistant", content=""),
+ accumulated_tool_calls=[],
+ )
+ adapter = SDKResponseAdapter(message_id="msg-1", session_id="session-1")
+ adapter.convert_message(SystemMessage(subtype="init", data={}))
+ responses = adapter.convert_message(
+ ResultMessage(
+ subtype=subtype,
+ duration_ms=100,
+ duration_api_ms=50,
+ is_error=True,
+ num_turns=1,
+ session_id="sdk-session-1",
+ result="stopped",
+ usage={"output_tokens": 0},
+ )
+ )
+
+ with caplog.at_level(logging.INFO, logger="backend.copilot.sdk.service"):
+ for response in responses:
+ _dispatch_response(response, acc, ctx, MagicMock(), False, "[test]")
+
+ [error] = [r for r in responses if isinstance(r, StreamError)]
+ assert error.code == code
+ sent = [r for r in caplog.records if "Sending error to frontend" in r.message]
+ assert [r.levelno for r in sent] == [level]
+ # Logging it quietly must not cost the user the explanation.
+ assert session.messages[-1].content == f"{COPILOT_ERROR_PREFIX} {error.errorText}"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 59f022f420..6f80f976b2 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -1164,6 +1164,15 @@ _RETRYABLE_STREAM_ERROR_CODES: frozenset[str] = frozenset(
)
+# Limits the platform sets on purpose: the turn spent its USD budget or made its
+# maximum number of LLM calls, and the user is told how to carry on. They are
+# outcomes, not failures, so they log at info: Sentry's LoggingIntegration files
+# every ERROR record as an issue (AUTOGPT-SERVER-8C4).
+_EXPECTED_STOP_CODES: frozenset[str] = frozenset(
+ {"max_budget_exhausted", "max_turns_exhausted"}
+)
+
+
# Handled-error codes whose StreamError the outer retry loop yields itself,
# after it has persisted the history marker, rather than the attempt.
_OUTER_LOOP_YIELDS_ERROR_CODES: frozenset[str] = frozenset(
@@ -3828,7 +3837,8 @@ def _dispatch_response(
# Persist error markers so they survive page refresh
if isinstance(response, StreamError):
- logger.error(
+ logger.log(
+ logging.INFO if response.code in _EXPECTED_STOP_CODES else logging.ERROR,
"%s Sending error to frontend: %s (code=%s)",
log_prefix,
response.errorText,
diff --git a/autogpt_platform/frontend/instrumentation-client.ts b/autogpt_platform/frontend/instrumentation-client.ts
index f03a13bf9e..d97d81a288 100644
--- a/autogpt_platform/frontend/instrumentation-client.ts
+++ b/autogpt_platform/frontend/instrumentation-client.ts
@@ -2,9 +2,11 @@
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
+import { isLoggedRequestFailure } from "@/app/api/mutators/request-failure-log";
import {
CLIENT_IGNORE_ERRORS,
isFromBot,
+ isInjectedIframeProbe,
isNextRSCNavigationFallback,
} from "@/lib/sentry-filters";
import { getSentryTunnelURL } from "@/lib/sentry-tunnel";
@@ -33,8 +35,14 @@ Sentry.init({
// Next's handled RSC fetch fallback reaches us only through console capture
// and falls back to a full navigation, so it is noise (BUILDER-3QB).
// Crawlers render our pages too; what breaks for them is not a user's bug.
+ // A failed API request the request logger reports, titled by its route,
+ // would otherwise arrive again from the HttpClient integration (BUILDER-95D).
+ // Some browsers inject a script that logs every cross-origin iframe it may
+ // not read (BUILDER-943).
beforeSend(event) {
if (isFromBot(navigator.userAgent)) return null;
+ if (isLoggedRequestFailure(event)) return null;
+ if (isInjectedIframeProbe(event)) return null;
return isNextRSCNavigationFallback(event) ? null : event;
},
beforeSendTransaction(event) {
diff --git a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
index 42cb94d97f..7558b53f70 100644
--- a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
+++ b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
@@ -96,4 +96,43 @@ describe("Sentry client instrumentation", () => {
expect(beforeSend(rscFallback, {})).toBeNull();
expect(beforeSend(fetchError, {})).toBe(fetchError);
});
+
+ it("drops a browser-injected script's iframe probe but keeps our own frame errors", async () => {
+ await import("../../instrumentation-client");
+
+ const { beforeSend } = initMock.mock.calls[0][0];
+ const blocked = {
+ exception: {
+ values: [
+ {
+ type: "SecurityError",
+ value:
+ 'Blocked a frame with origin "https://platform.agpt.co" from accessing a cross-origin frame.',
+ },
+ ],
+ },
+ };
+ // As HeyTap Browser's injected script logs it on /onboarding (BUILDER-943).
+ const injectedProbe = {
+ ...blocked,
+ logger: "console",
+ extra: {
+ arguments: ["无法访问 iframe 的内容:", { name: "SecurityError" }],
+ },
+ };
+ const ourWarning = {
+ ...blocked,
+ logger: "console",
+ extra: {
+ arguments: [
+ "Could not read the checkout frame",
+ { name: "SecurityError" },
+ ],
+ },
+ };
+
+ expect(beforeSend(injectedProbe, {})).toBeNull();
+ expect(beforeSend(blocked, {})).toBe(blocked);
+ expect(beforeSend(ourWarning, {})).toBe(ourWarning);
+ });
});
diff --git a/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts b/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts
new file mode 100644
index 0000000000..0b047a3b0f
--- /dev/null
+++ b/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts
@@ -0,0 +1,106 @@
+import { listCopilotFollowupSchedules } from "@/app/api/__generated__/endpoints/schedules/schedules";
+import { resetClientRequestFailureLog } from "@/app/api/mutators/request-failure-log";
+import { server } from "@/mocks/mock-server";
+import * as Sentry from "@sentry/nextjs";
+import { http, HttpResponse } from "msw";
+import {
+ afterAll,
+ afterEach,
+ beforeAll,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+} from "vitest";
+
+// Vitest resolves @sentry/nextjs to its Node build, which has no HttpClient
+// integration; the client bundle runs the browser SDK, so load that one.
+vi.mock("@sentry/nextjs", async () => {
+ const { createRequire } = await import("node:module");
+ const nextjs = createRequire(import.meta.url).resolve(
+ "@sentry/nextjs/package.json",
+ );
+ const react = createRequire(nextjs).resolve("@sentry/react/package.json");
+ const browser = createRequire(react).resolve("@sentry/browser/package.json");
+ const sdk = await import(
+ browser.replace("package.json", "build/npm/esm/prod/index.js")
+ );
+ return { ...sdk, captureRouterTransitionStart: () => undefined };
+});
+
+// Events as the production client config would send them.
+const sent: Sentry.ErrorEvent[] = [];
+
+beforeAll(async () => {
+ vi.spyOn(console, "info").mockImplementation(() => undefined);
+ vi.stubEnv("NEXT_PUBLIC_SENTRY_DSN", "https://[email protected]/0");
+ // HttpClient only hooks a fetch it takes for the browser's own, which MSW's
+ // is not; Sentry trusts any fetch on an edge runtime.
+ vi.stubGlobal("EdgeRuntime", "vitest");
+ await import("../../instrumentation-client");
+ vi.spyOn(Sentry.getClient()!.getTransport()!, "send").mockImplementation(
+ async ([, items]) => {
+ for (const [header, payload] of items) {
+ if (header.type === "event") sent.push(payload as Sentry.ErrorEvent);
+ }
+ return {};
+ },
+ );
+});
+
+beforeEach(() => {
+ sent.length = 0;
+});
+
+afterEach(() => {
+ resetClientRequestFailureLog();
+});
+
+afterAll(() => {
+ vi.unstubAllEnvs();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+const FILE = "6f1e2b3c-4d5e-4b7e-9d0a-9c1f6a523c0e";
+
+describe("a failed API request in Sentry", () => {
+ it("is reported once, under its route, when the generated client made it", async () => {
+ server.use(
+ http.get("*/api/proxy/api/schedules/followups", () =>
+ HttpResponse.json(
+ { detail: "All connection attempts failed" },
+ { status: 500 },
+ ),
+ ),
+ );
+
+ await expect(listCopilotFollowupSchedules()).rejects.toThrow(
+ "All connection attempts failed",
+ );
+ await Sentry.flush(2000);
+
+ expect(sent.map((event) => event.message)).toEqual([
+ "Request failed on client: GET /api/schedules/followups 500",
+ ]);
+ });
+
+ it("is still reported once when the request bypassed the generated client", async () => {
+ server.use(
+ http.get("*/api/proxy/api/workspace/files/:id/download", () =>
+ HttpResponse.text("upstream unavailable", { status: 503 }),
+ ),
+ );
+
+ const response = await fetch(
+ `/api/proxy/api/workspace/files/${FILE}/download`,
+ );
+ await Sentry.flush(2000);
+
+ expect(response.status).toBe(503);
+ expect(sent.map((event) => event.message)).toEqual([
+ "HTTP Client Error with status code: 503",
+ ]);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/__tests__/server-request-failure-log.test.ts b/autogpt_platform/frontend/src/app/api/mutators/__tests__/server-request-failure-log.test.ts
new file mode 100644
index 0000000000..d44e5116c0
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/api/mutators/__tests__/server-request-failure-log.test.ts
@@ -0,0 +1,82 @@
+// @vitest-environment node
+import { getV2GetLibraryAgent } from "@/app/api/__generated__/endpoints/library/library";
+import { server } from "@/mocks/mock-server";
+import * as Sentry from "@sentry/nextjs";
+import { http, HttpResponse } from "msw";
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
+
+// A signed-in render; reading the session cookie needs a live Next request.
+vi.mock("@/lib/autogpt-server-api/helpers", async (importActual) => ({
+ ...(await importActual<typeof import("@/lib/autogpt-server-api/helpers")>()),
+ getServerAuthToken: async () => "session-token",
+}));
+
+// A real server-side Sentry client: its console integration turns every log
+// line into a breadcrumb on the next server event, which is where a logged
+// object used to read "[object Object]".
+const sent: Sentry.ErrorEvent[] = [];
+// Spied before init, so Sentry's console patch wraps the spies.
+const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => undefined);
+const consoleInfo = vi
+ .spyOn(console, "info")
+ .mockImplementation(() => undefined);
+
+beforeAll(() => {
+ vi.spyOn(console, "warn").mockImplementation(() => undefined);
+ Sentry.init({
+ dsn: "https://[email protected]/0",
+ transport: () => ({
+ send: () => Promise.resolve({}),
+ flush: () => Promise.resolve(true),
+ }),
+ beforeSend(event) {
+ sent.push(event);
+ return null;
+ },
+ });
+});
+
+afterAll(() => {
+ vi.restoreAllMocks();
+});
+
+const AGENT = "9c1f6a52-3c0e-4b7e-9d0a-6f1e2b3c4d5e";
+
+function failWith(status: number, detail: string) {
+ server.use(
+ http.get("*/api/library/agents/:id", () =>
+ HttpResponse.json({ detail }, { status }),
+ ),
+ );
+}
+
+describe("a request that fails while rendering on the server", () => {
+ it("logs one readable line, and Sentry's breadcrumb reads the same", async () => {
+ failWith(502, "Backend sent no response within 30000ms");
+
+ await expect(getV2GetLibraryAgent(AGENT)).rejects.toThrow();
+ Sentry.captureMessage("Server render failed");
+ await Sentry.flush(2000);
+
+ const line =
+ "Request failed on server: GET /api/library/agents/{id} 502: Backend sent no response within 30000ms";
+ expect(consoleError).toHaveBeenCalledExactlyOnceWith(line);
+ const crumbs = sent[0].breadcrumbs?.filter((b) => b.category === "console");
+ expect(crumbs?.map((b) => b.message)).toContain(line);
+ expect(JSON.stringify(crumbs)).not.toContain("[object Object]");
+ });
+
+ it("logs an outcome the app handles, like a deleted agent, as info", async () => {
+ consoleError.mockClear();
+ failWith(404, "Library agent not found");
+
+ await expect(getV2GetLibraryAgent(AGENT)).rejects.toThrow();
+
+ expect(consoleError).not.toHaveBeenCalled();
+ expect(consoleInfo).toHaveBeenCalledWith(
+ "Request failed on server: GET /api/library/agents/{id} 404: Library agent not found",
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
index 8e58a01eaf..806297decb 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
@@ -9,7 +9,11 @@ import { getSystemHeaders } from "@/lib/impersonation";
import { getDatafastAttribution } from "@/services/analytics/datafast-attribution";
import { environment } from "@/services/environment";
import { transformDates } from "./date-transformer";
-import { logClientRequestFailure } from "./request-failure-log";
+import {
+ fetchWithFailureLog,
+ logClientRequestFailure,
+ logServerRequestFailure,
+} from "./request-failure-log";
function getBaseURL(): string {
if (!environment.isServerSide()) {
@@ -109,7 +113,7 @@ export const customMutator = async <
}
}
- const response = await fetch(fullUrl, {
+ const response = await fetchWithFailureLog(fullUrl, {
...requestOptions,
method,
headers,
@@ -170,12 +174,7 @@ export const customMutator = async <
};
if (environment.isServerSide()) {
- // Server-side module state is shared across every user's request, so
- // collapsing there would hide one user's failure behind another's.
- console.error("Request failed on server", {
- ...failure,
- responseData: responseData || "No response data",
- });
+ logServerRequestFailure(failure);
} else {
logClientRequestFailure(failure);
}
diff --git a/autogpt_platform/frontend/src/app/api/mutators/request-failure-log.ts b/autogpt_platform/frontend/src/app/api/mutators/request-failure-log.ts
index e787f57586..5645d2d97f 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/request-failure-log.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/request-failure-log.ts
@@ -39,6 +39,60 @@ export function logClientRequestFailure(failure: RequestFailure) {
});
}
+/**
+ * Log one server-side request failure as one readable line. Not collapsed:
+ * server module state is shared across every user's request, so a window
+ * would hide one user's failure behind another's.
+ */
+export function logServerRequestFailure(failure: RequestFailure) {
+ const detail = failure.errorMessage
+ .slice(0, MAX_DETAIL_LENGTH)
+ .replace(/\s+/g, " ");
+ const line = `${summarize(failure, "server")}: ${detail}`;
+ if (EXPECTED_STATUSES.has(failure.status)) {
+ console.info(line);
+ } else {
+ console.error(line);
+ }
+}
+
+/**
+ * Fetch a request whose failure the caller reports with this module. Sentry's
+ * HttpClient integration files every 5xx on its own as "HTTP Client Error with
+ * status code: 500", grouped by call stack, so each failure arrived twice
+ * (BUILDER-95D beside BUILDER-95C). The integration captures before fetch
+ * resolves, so while the request is in flight `isLoggedRequestFailure` can
+ * pick that copy out for `beforeSend` to drop.
+ */
+export async function fetchWithFailureLog(url: string, init: RequestInit) {
+ const key = requestKey(init.method ?? "GET", url);
+ inFlight.set(key, (inFlight.get(key) ?? 0) + 1);
+ try {
+ return await fetch(url, init);
+ } finally {
+ const remaining = (inFlight.get(key) ?? 1) - 1;
+ if (remaining > 0) inFlight.set(key, remaining);
+ else inFlight.delete(key);
+ }
+}
+
+export function isLoggedRequestFailure(event: Sentry.ErrorEvent) {
+ const mechanism = event.exception?.values?.[0]?.mechanism?.type ?? "";
+ const { method, url } = event.request ?? {};
+ if (!mechanism.startsWith("auto.http.client.") || !method || !url) {
+ return false;
+ }
+ return inFlight.has(requestKey(method, url));
+}
+
+// The integration sees the absolute URL, while callers pass a relative one.
+function requestKey(method: string, url: string) {
+ const base = globalThis.location?.href ?? "http://localhost";
+ return `${method.toUpperCase()} ${new URL(url, base).href}`;
+}
+
+const inFlight = new Map<string, number>();
+
// The open windows outlive a test file; reset them between cases.
export function resetClientRequestFailureLog() {
openWindows.forEach(({ timer }) => clearTimeout(timer));
@@ -103,8 +157,11 @@ function emit(failure: RequestFailure) {
});
}
-function summarize({ method, url, status }: RequestFailure) {
- return `Request failed on client: ${method} ${routeTemplate(url)} ${status}`;
+function summarize(
+ { method, url, status }: RequestFailure,
+ side: "client" | "server" = "client",
+) {
+ return `Request failed on ${side}: ${method} ${routeTemplate(url)} ${status}`;
}
function statusClass(status: number) {
diff --git a/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx
index 4588ea204e..0545441272 100644
--- a/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx
+++ b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx
@@ -1,5 +1,5 @@
import { cleanup, render, waitFor } from "@testing-library/react";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { MessageResponse } from "../message";
// KaTeX keeps the TeX source in a MathML annotation, so prose assertions have to
@@ -48,6 +48,18 @@ describe("MessageResponse math", () => {
expect(texOf(container)).toEqual(["\\frac{1}{s+1}"]);
});
+ it("renders a formula holding non-Latin text without a console warning", async () => {
+ // Console capture filed each of KaTeX's strict-mode warnings as a Sentry
+ // issue, fourteen for one Bengali reader's replies (BUILDER-94S…956).
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
+
+ const container = await renderReply("ধরা যাক $ভ + ২ = x$ তাহলে");
+
+ await waitFor(() => expect(texOf(container)).toEqual(["ভ + ২ = x"]));
+ expect(warn).not.toHaveBeenCalled();
+ warn.mockRestore();
+ });
+
it("leaves two currency amounts in one sentence as literal text", async () => {
const container = await renderReply("$5 and $10 buys lunch.");
diff --git a/autogpt_platform/frontend/src/components/ai-elements/message.tsx b/autogpt_platform/frontend/src/components/ai-elements/message.tsx
index 8b2f21c7e1..c5ded4db90 100644
--- a/autogpt_platform/frontend/src/components/ai-elements/message.tsx
+++ b/autogpt_platform/frontend/src/components/ai-elements/message.tsx
@@ -13,9 +13,10 @@ import { Dialog } from "@/components/molecules/Dialog/Dialog";
import { cn } from "@/lib/utils";
import { cjk } from "@streamdown/cjk";
import { code } from "@/lib/streamdown-code-plugin";
-import { createMathPlugin } from "@streamdown/math";
+import { createMathPlugin, type MathPlugin } from "@streamdown/math";
import { escapeCurrencyAmounts } from "@/lib/markdown-math";
import { mermaid } from "@streamdown/mermaid";
+import rehypeKatex from "rehype-katex";
import type { UIMessage } from "ai";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import type { ComponentProps, HTMLAttributes, ReactElement } from "react";
@@ -354,7 +355,17 @@ function ExternalLinkModal({
// Single-dollar math is off by default in @streamdown/math, so "$x^2$" reached the
// reader raw; escapeCurrencyAmounts keeps prices out of the formulas it enables.
-const math = createMathPlugin({ singleDollarTextMath: true });
+// KaTeX renders text real LaTeX would reject, such as Bengali in a formula, but
+// its default strict mode console.warns each time, and console capture filed
+// every warning as a Sentry issue (BUILDER-94S). The plugin has no option for
+// strict mode, so its rehype step is rebuilt with it off.
+const math: MathPlugin = {
+ ...createMathPlugin({ singleDollarTextMath: true }),
+ rehypePlugin: [
+ rehypeKatex,
+ { errorColor: "var(--color-muted-foreground)", strict: "ignore" },
+ ],
+};
export const MessageResponse = memo(
({ className, children, components, ...props }: MessageResponseProps) => (
diff --git a/autogpt_platform/frontend/src/lib/sentry-filters.ts b/autogpt_platform/frontend/src/lib/sentry-filters.ts
index f973f7bde2..3cca79c152 100644
--- a/autogpt_platform/frontend/src/lib/sentry-filters.ts
+++ b/autogpt_platform/frontend/src/lib/sentry-filters.ts
@@ -21,6 +21,29 @@ export function isNextRSCNavigationFallback(event: Event) {
return typeof message === "string" && NEXT_RSC_FALLBACK_MESSAGE.test(message);
}
+// Script some browsers inject into every page (HeyTap Browser), and scrapers
+// driving one (QQ Browser, Playwright), walk the page's iframes and log, in
+// Chinese, "cannot access the iframe's content" for each cross-origin one the
+// browser rightly refuses (BUILDER-943, 942, 8KH). None of it is our code.
+const INJECTED_IFRAME_PROBE_MESSAGE = /^无法访问\s*iframe/i;
+
+/**
+ * True for that console warning. The event's title is the browser's own
+ * "SecurityError: Blocked a frame … from accessing a cross-origin frame",
+ * which our code could raise too, so only the injected script's console text
+ * in `extra.arguments` marks it.
+ */
+export function isInjectedIframeProbe(event: Event) {
+ if (event.logger !== "console") return false;
+ if (event.exception?.values?.[0]?.type !== "SecurityError") return false;
+ const args = event.extra?.arguments;
+ if (!Array.isArray(args)) return false;
+ const [message] = args;
+ return (
+ typeof message === "string" && INJECTED_IFRAME_PROBE_MESSAGE.test(message)
+ );
+}
+
export const CLIENT_IGNORE_ERRORS = [
// Sentry Replay (rrweb) serializing DOM snapshots with cross-origin
// stylesheets (e.g. from browser extensions or CDN-loaded CSS)
diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx
index 111f99d992..c60f53312e 100644
--- a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx
+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx
@@ -282,6 +282,31 @@ describe("dual backend", () => {
expect(postHog.capture).toHaveBeenCalledTimes(1);
});
+ it("stays quiet when a flag PostHog has switched off is off in LaunchDarkly too", async () => {
+ // PostHog leaves an inactive flag out of its answer and LaunchDarkly says
+ // `false`: both serve the default, so no user sees a difference
+ // (BUILDER-94P).
+ const { Flag, useGetFlag } = await loadWithBackend("dual");
+ launchDarkly.flags = { [Flag.CHAT_PINNING]: false };
+ postHog.enabled.mockReturnValue(undefined);
+
+ const { result } = renderHook(() => useGetFlag(Flag.CHAT_PINNING));
+
+ expect(result.current).toBe(false);
+ expect(postHog.capture).not.toHaveBeenCalled();
+ expect(sentry.captureMessage).not.toHaveBeenCalled();
+ });
+
+ it("stays quiet when PostHog's bare toggle stands in for a JSON flag's default", async () => {
+ const { Flag, useGetFlag } = await loadWithBackend("dual");
+ launchDarkly.flags = { [Flag.COPILOT_BOT_PLATFORMS]: {} };
+ postHog.enabled.mockReturnValue(true);
+
+ renderHook(() => useGetFlag(Flag.COPILOT_BOT_PLATFORMS));
+
+ expect(postHog.capture).not.toHaveBeenCalled();
+ });
+
it("reports a flag LaunchDarkly has and PostHog has never heard of", async () => {
const { Flag, useGetFlag } = await loadWithBackend("dual");
vi.spyOn(console, "info").mockImplementation(() => {});
diff --git a/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts b/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts
index e51c757fb9..9443c2574f 100644
--- a/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts
+++ b/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts
@@ -8,6 +8,7 @@ import isEqual from "lodash/isEqual";
import { useEffect } from "react";
import { isPostHogFlagsEnabled } from "../flag-backend";
import type { FlagSourceResult } from "../flag-source";
+import { type Flag, resolveFlagValue } from "../use-get-flag";
import { useLaunchDarklyFlag } from "./launchdarkly";
import { usePostHogFlag } from "./posthog";
@@ -48,9 +49,14 @@ function useReportMismatch(
isPostHogFlagsEnabled() &&
launchDarkly.resolved &&
postHog.resolved;
- // Key-order-insensitive, like the backend's dict comparison: the vendors
- // need not serialise a JSON flag's keys in the same order.
- const agree = isEqual(launchDarkly.value, postHog.value);
+ // What each vendor would serve, not its raw answer: LaunchDarkly's `false`
+ // and PostHog's empty answer for a flag it has switched off both serve the
+ // default (BUILDER-94P). Key-order-insensitive, like the backend's dict
+ // comparison: the vendors need not serialise a JSON flag's keys in one order.
+ const agree = isEqual(
+ resolveFlagValue(key as Flag, launchDarkly.value),
+ resolveFlagValue(key as Flag, postHog.value),
+ );
useEffect(() => {
if (!comparable || agree) return;