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(frontend): give LaunchDarkly a 5s init timeout instead of 5000s (#14694)
Bently7519f50926c9
diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsxnew file mode 100644index 0000000000..82fb4939e2--- /dev/null+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsx@@ -0,0 +1,84 @@+import { render, screen } from "@testing-library/react";+import type { ReactNode } from "react";+import { describe, expect, it, vi } from "vitest";+import { LD_INIT_TIMEOUT_SECONDS } from "../constants";+import { LaunchDarklyProvider } from "../feature-flag-provider";+import { Flag, useGetFlag } from "../use-get-flag";++// launchdarkly-js-sdk-common warns above this many SECONDS ("We recommend a+// timeout of 5 seconds or less") and instrumentation-client.ts turns+// console.warn into a Sentry event, so a millisecond value in the `timeout`+// prop — which the React SDK hands straight to `waitForInitialization` —+// floods Sentry on every page load and never really times out.+const LD_HIGH_TIMEOUT_THRESHOLD_SECONDS = 5;++const ld = vi.hoisted(() => ({ timeouts: [] as unknown[] }));++interface Props {+ timeout?: number;+ children: ReactNode;+}++vi.mock("launchdarkly-react-client-sdk", () => {+ // Declared inside the factory: vi.mock is hoisted above module scope.+ function MockLDProvider({ timeout, children }: Props) {+ ld.timeouts.push(timeout);+ return <>{children}</>;+ }+ return {+ LDProvider: MockLDProvider,+ // LaunchDarkly has not answered for any key yet: init lag, or an outage+ // that will end in a timeout.+ useFlags: () => ({}),+ useLDClient: () => undefined,+ };+});++vi.mock("@/lib/auth/hooks/useAuth", () => ({+ useAuth: () => ({ user: null, isUserLoading: false }),+}));++vi.mock("@/services/environment", () => ({+ environment: {+ areFeatureFlagsEnabled: () => true,+ getLaunchDarklyClientId: () => "test-client-id",+ },+}));++vi.mock("@/app/(platform)/marketplace/components/HeroSection/helpers", () => ({+ DEFAULT_SEARCH_TERMS: [],+}));++vi.mock("@sentry/nextjs", () => ({+ buildLaunchDarklyFlagUsedHandler: () => ({}),+}));++function FlagProbe() {+ const enabled = useGetFlag(Flag.ENABLE_PLATFORM_PAYMENT);+ return <span>payment: {String(enabled)}</span>;+}++describe("LaunchDarkly initialisation timeout", () => {+ it("gives the SDK a timeout in seconds, below the threshold it warns about", () => {+ render(+ <LaunchDarklyProvider>+ <FlagProbe />+ </LaunchDarklyProvider>,+ );++ expect(ld.timeouts).toEqual([LD_INIT_TIMEOUT_SECONDS]);+ expect(LD_INIT_TIMEOUT_SECONDS).toBeLessThanOrEqual(+ LD_HIGH_TIMEOUT_THRESHOLD_SECONDS,+ );+ });++ it("renders children on the default flag values while LaunchDarkly has not answered", () => {+ render(+ <LaunchDarklyProvider>+ <FlagProbe />+ </LaunchDarklyProvider>,+ );++ expect(screen.getByText("payment: false")).toBeDefined();+ });+});diff --git a/autogpt_platform/frontend/src/services/feature-flags/constants.ts b/autogpt_platform/frontend/src/services/feature-flags/constants.tsnew file mode 100644index 0000000000..dc82c45c38--- /dev/null+++ b/autogpt_platform/frontend/src/services/feature-flags/constants.ts@@ -0,0 +1,14 @@+/**+ * How long to wait for the LaunchDarkly client to initialise.+ *+ * The unit is **seconds**, not milliseconds: `LDProvider`'s `timeout` prop and+ * `LDClient.waitForInitialization()` both take seconds, and the JS SDK logs a+ * warning for anything above 5 ("we recommend a timeout of 5 seconds or less").+ * Sentry's captureConsoleIntegration turns that warning into an event on every+ * page load, so keep this at 5 or below.+ *+ * On timeout the SDK rejects, `LDProvider` swallows the rejection and+ * subscribes to `ready` instead, and the UI runs on the defaults in+ * `use-get-flag.ts` until LaunchDarkly answers.+ */+export const LD_INIT_TIMEOUT_SECONDS = 5;diff --git a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsxindex a2a33314cf..43e7c37d3f 100644--- a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx+++ b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx@@ -8,10 +8,9 @@ import type { ReactNode } from "react";import { useMemo } from "react";import { getAnonymousID } from "../analytics/anonymous-id";import { environment } from "../environment";+import { LD_INIT_TIMEOUT_SECONDS } from "./constants";import { buildLDContext } from "./helpers";-const LAUNCHDARKLY_INIT_TIMEOUT_MS = 5000;-export function LaunchDarklyProvider({ children }: { children: ReactNode }) {const { user, isUserLoading } = useAuth();const envEnabled = environment.areFeatureFlagsEnabled();@@ -34,7 +33,7 @@ export function LaunchDarklyProvider({ children }: { children: ReactNode }) {<LDProviderclientSideID={clientId ?? ""}context={context}- timeout={LAUNCHDARKLY_INIT_TIMEOUT_MS}+ timeout={LD_INIT_TIMEOUT_SECONDS}reactOptions={{ useCamelCaseFlagKeys: false }}options={{inspectors: [Sentry.buildLaunchDarklyFlagUsedHandler()],