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): label the dev deployment as dev, not preview, in Sentry and PostHog (#15481)
Nicholas Tindlea6a4e32236c8
diff --git a/autogpt_platform/frontend/next.config.mjs b/autogpt_platform/frontend/next.config.mjsindex 905941905f..62f2b76711 100644--- a/autogpt_platform/frontend/next.config.mjs+++ b/autogpt_platform/frontend/next.config.mjs@@ -10,10 +10,13 @@ const skipBuildChecks = process.env.NEXT_SKIP_BUILD_CHECKS === "true";/** @type {import('next').NextConfig} */const nextConfig = {- // VERCEL_ENV is server-only. Mirror it into the browser bundle so Sentry can- // tag preview deployments as previews rather than as production.+ // VERCEL_ENV and VERCEL_TARGET_ENV are server-only. Mirror them into the+ // browser bundle so Sentry and PostHog can tag branch previews as previews+ // rather than as production, and custom environments (the dev deployment's+ // `staging`) as themselves.env: {NEXT_PUBLIC_VERCEL_ENV: process.env.VERCEL_ENV ?? "",+ NEXT_PUBLIC_VERCEL_TARGET_ENV: process.env.VERCEL_TARGET_ENV ?? "",// Sentry initialises in the browser too, so its kill switch must reach it.DISABLE_SENTRY: process.env.DISABLE_SENTRY ?? "",},diff --git a/autogpt_platform/frontend/src/__tests__/next-config-vercel-env.test.ts b/autogpt_platform/frontend/src/__tests__/next-config-vercel-env.test.tsnew file mode 100644index 0000000000..7cb335b304--- /dev/null+++ b/autogpt_platform/frontend/src/__tests__/next-config-vercel-env.test.ts@@ -0,0 +1,37 @@+import { join } from "node:path";+import type { NextConfig } from "next";+import { afterEach, describe, expect, it, vi } from "vitest";++async function loadMirroredEnv() {+ vi.resetModules();+ const { default: nextConfig } = await vi.importActual<{+ default: NextConfig;+ }>(join(process.cwd(), "next.config.mjs"));+ return nextConfig.env;+}++afterEach(() => {+ vi.unstubAllEnvs();+});++describe("next.config.mjs Vercel environment mirror", () => {+ it("mirrors the deployment's environment and target environment into the browser bundle", async () => {+ vi.stubEnv("VERCEL_ENV", "preview");+ vi.stubEnv("VERCEL_TARGET_ENV", "staging");++ expect(await loadMirroredEnv()).toMatchObject({+ NEXT_PUBLIC_VERCEL_ENV: "preview",+ NEXT_PUBLIC_VERCEL_TARGET_ENV: "staging",+ });+ });++ it("mirrors empty strings for a build outside Vercel", async () => {+ vi.stubEnv("VERCEL_ENV", undefined);+ vi.stubEnv("VERCEL_TARGET_ENV", undefined);++ expect(await loadMirroredEnv()).toMatchObject({+ NEXT_PUBLIC_VERCEL_ENV: "",+ NEXT_PUBLIC_VERCEL_TARGET_ENV: "",+ });+ });+});diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.tsindex 25447c7440..0d91eacca7 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.ts@@ -5,10 +5,16 @@ afterEach(() => {vi.unstubAllEnvs();});+function stubDeployment(appEnv: string, vercelEnv: string, targetEnv?: string) {+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", appEnv);+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", vercelEnv);+ vi.stubEnv("NEXT_PUBLIC_VERCEL_TARGET_ENV", targetEnv);+ vi.stubEnv("VERCEL_TARGET_ENV", undefined);+}+describe("getPostHogBaseProperties", () => {it("names the browser as the emitter and carries the app environment", () => {- vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");- vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "production");+ stubDeployment("prod", "production", "production");expect(getPostHogBaseProperties()).toEqual({source: "web",@@ -17,9 +23,23 @@ describe("getPostHogBaseProperties", () => {});it("tells a Vercel preview apart from production", () => {- vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");- vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "preview");+ stubDeployment("prod", "preview", "preview");++ expect(getPostHogBaseProperties().environment).toBe("preview");+ });++ it("tells a preview apart when the build carries no target environment", () => {+ stubDeployment("prod", "preview");expect(getPostHogBaseProperties().environment).toBe("preview");});++ it("reports the dev deployment as dev although Vercel calls its staging environment a preview", () => {+ stubDeployment("dev", "preview", "staging");++ expect(getPostHogBaseProperties()).toEqual({+ source: "web",+ environment: "dev",+ });+ });});diff --git a/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.ts b/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.tsindex 80cd386000..d9ad4fdbf6 100644--- a/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.ts+++ b/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.ts@@ -3,9 +3,10 @@ import { environment } from "@/services/environment";// The base properties every PostHog event carries (see// docs/engineering/tracking-plan.md), registered once as super properties so// no call site has to repeat them. `source` names the emitter; the backend-// sends `platform` or `chat_copilot`. Vercel previews share the production-// NEXT_PUBLIC_APP_ENV, so they are told apart here, as in-// `environment.getEnvironmentStr`.+// sends `platform` or `chat_copilot`. Vercel branch previews share the+// production NEXT_PUBLIC_APP_ENV, so they are told apart here, as in+// `environment.getEnvironmentStr`. A custom Vercel environment, such as the dev+// deployment's `staging`, is not a preview and reports its own app env.export function getPostHogBaseProperties() {return {source: "web",diff --git a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.tsindex ecd2c3a9f2..9f92ce2801 100644--- a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts+++ b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts@@ -85,20 +85,38 @@ describe("AutoGPT server URL resolution", () => {});describe("Sentry environment tagging", () => {- function stubDeployment(appEnv: string, vercelEnv: string) {+ function stubDeployment(+ appEnv: string,+ vercelEnv: string,+ targetEnv?: string,+ ) {vi.stubEnv("NEXT_PUBLIC_APP_ENV", appEnv);vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "CLOUD");vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", vercelEnv);+ vi.stubEnv("NEXT_PUBLIC_VERCEL_TARGET_ENV", targetEnv);+ vi.stubEnv("VERCEL_TARGET_ENV", undefined);}it("tags a Vercel preview as preview, not as the app env it inherits", () => {- stubDeployment("prod", "preview");+ stubDeployment("prod", "preview", "preview");expect(environment.getEnvironmentStr()).toBe("app:preview-behave:cloud");});+ it.each([+ ["unset", undefined],+ ["empty", ""],+ ])(+ "tags a preview by VERCEL_ENV alone when the target environment is %s",+ (_, targetEnv) => {+ stubDeployment("prod", "preview", targetEnv);++ expect(environment.getEnvironmentStr()).toBe("app:preview-behave:cloud");+ },+ );+it("tags the production deployment as prod", () => {- stubDeployment("prod", "production");+ stubDeployment("prod", "production", "production");expect(environment.getEnvironmentStr()).toBe("app:prod-behave:cloud");});@@ -109,6 +127,12 @@ describe("Sentry environment tagging", () => {expect(environment.getEnvironmentStr()).toBe("app:dev-behave:cloud");});+ it("tags the dev deployment as dev although Vercel calls its staging environment a preview", () => {+ stubDeployment("dev", "preview", "staging");++ expect(environment.getEnvironmentStr()).toBe("app:dev-behave:cloud");+ });+it("tags a self-hosted instance by its own env", () => {vi.stubEnv("NEXT_PUBLIC_APP_ENV", "local");vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "LOCAL");@@ -125,7 +149,7 @@ describe("Sentry environment tagging", () => {});it("tags events with the environment a deployment names for itself", () => {- stubDeployment("prod", "preview");+ stubDeployment("prod", "preview", "preview");vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", "pr-123");expect(environment.getSentryEnvironment()).toBe("pr-123");@@ -158,6 +182,72 @@ describe("Sentry environment tagging", () => {);});+describe("Vercel preview detection", () => {+ const deployments = [+ {+ scenario: "a branch preview",+ vercelEnv: "preview",+ targetEnv: "preview",+ isPreview: true,+ },+ {+ scenario: "a custom environment such as staging",+ vercelEnv: "preview",+ targetEnv: "staging",+ isPreview: false,+ },+ {+ scenario: "a build with no target environment",+ vercelEnv: "preview",+ targetEnv: undefined,+ isPreview: true,+ },+ {+ scenario: "a build with an empty target environment",+ vercelEnv: "preview",+ targetEnv: "",+ isPreview: true,+ },+ {+ scenario: "production",+ vercelEnv: "production",+ targetEnv: "production",+ isPreview: false,+ },+ {+ scenario: "local development on Vercel",+ vercelEnv: "development",+ targetEnv: "development",+ isPreview: false,+ },+ {+ scenario: "a build outside Vercel",+ vercelEnv: undefined,+ targetEnv: undefined,+ isPreview: false,+ },+ ];++ describe.each([+ ["the browser bundle", "NEXT_PUBLIC_"],+ ["the server", ""],+ ])("as read by %s", (_, prefix) => {+ it.each(deployments)(+ "reports $scenario as preview: $isPreview",+ ({ vercelEnv, targetEnv, isPreview }) => {+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", undefined);+ vi.stubEnv("VERCEL_ENV", undefined);+ vi.stubEnv("NEXT_PUBLIC_VERCEL_TARGET_ENV", undefined);+ vi.stubEnv("VERCEL_TARGET_ENV", undefined);+ vi.stubEnv(`${prefix}VERCEL_ENV`, vercelEnv);+ vi.stubEnv(`${prefix}VERCEL_TARGET_ENV`, targetEnv);++ expect(environment.isVercelPreview()).toBe(isPreview);+ },+ );+ });+});+describe("Sentry enablement", () => {const HOSTED_DSN_PROJECT = "/4507946746380288";diff --git a/autogpt_platform/frontend/src/services/environment/index.ts b/autogpt_platform/frontend/src/services/environment/index.tsindex e6aebf46b1..cb99ef07df 100644--- a/autogpt_platform/frontend/src/services/environment/index.ts+++ b/autogpt_platform/frontend/src/services/environment/index.ts@@ -148,11 +148,16 @@ function isClientSide() {}function isVercelPreview() {- // VERCEL_ENV is server-only; next.config.mjs mirrors it into the- // NEXT_PUBLIC_ copy so the browser bundle can read it too.- return (- (process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL_ENV) === "preview"- );+ // VERCEL_ENV and VERCEL_TARGET_ENV are server-only; next.config.mjs mirrors+ // them into NEXT_PUBLIC_ copies so the browser bundle can read them too.+ // VERCEL_ENV is "preview" for every custom environment (the dev deployment's+ // "staging") as well as for branch previews, while VERCEL_TARGET_ENV names+ // the environment itself. Builds that don't carry it fall back to VERCEL_ENV.+ const vercelEnv =+ process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL_ENV;+ const targetEnv =+ process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV || process.env.VERCEL_TARGET_ENV;+ return vercelEnv === "preview" && (!targetEnv || targetEnv === "preview");}function isVercel() {diff --git a/docs/engineering/tracking-plan.md b/docs/engineering/tracking-plan.mdindex 1ceb1cd6c1..d6752626d7 100644--- a/docs/engineering/tracking-plan.md+++ b/docs/engineering/tracking-plan.md@@ -64,7 +64,9 @@ Every backend event goes through `capture()` in`backend/util/posthog_client.py`, which applies both last, so a caller cannotoverwrite them. The browser registers them once as PostHog super properties(`src/services/analytics/posthog-base-properties.ts`), again after `reset()`-at logout; a Vercel preview reports `environment: preview`.+at logout; a Vercel preview reports `environment: preview`, while the dev+deployment, which is Vercel's `staging` custom environment rather than a+preview, reports `environment: dev`.`source` is reserved for the emitter. The three events that used it forsomething else now use `hire_started.surface`,