πͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): pin new Cloud PostHog and Mixpanel integrations to enriched events (#16242)
Niklas Semmlera3830aedbf7e
diff --git a/web/src/__tests__/server/mixpanel-integration.servertest.ts b/web/src/__tests__/server/mixpanel-integration.servertest.tsindex bb364c0f7c..65b334dd7d 100644--- a/web/src/__tests__/server/mixpanel-integration.servertest.ts+++ b/web/src/__tests__/server/mixpanel-integration.servertest.ts@@ -3,13 +3,34 @@ import { prisma, Prisma } from "@langfuse/shared/src/db";import { appRouter } from "@/src/server/api/root";import { createInnerTRPCContext } from "@/src/server/api/trpc";import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";-import { LEGACY_BLOB_EXPORT_CUTOFF } from "@langfuse/shared";+import {+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,+ LEGACY_BLOB_EXPORT_CUTOFF,+ LEGACY_BLOB_EXPORTER_CUTOFF,+} from "@langfuse/shared";import { decrypt } from "@langfuse/shared/encryption";import { env } from "@/src/env.mjs";const MS_PER_DAY = 24 * 60 * 60 * 1000;const PRE_CUTOFF = new Date(LEGACY_BLOB_EXPORT_CUTOFF.getTime() - MS_PER_DAY);const POST_CUTOFF = new Date(LEGACY_BLOB_EXPORT_CUTOFF.getTime() + MS_PER_DAY);+// Row ages derive from the cutoff constants, never from literals, so the+// NEXT_PUBLIC_LANGFUSE_*_CUTOFF dev overrides cannot fail the suite.+const ROW_PRE_ANALYTICS_CUTOFF = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() - MS_PER_DAY,+);+const ROW_POST_ANALYTICS_CUTOFF = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() + MS_PER_DAY,+);+// Grandfathered under BOTH exporter cutoffs, so the pre-flight assert provably+// cannot be what rejects β only the in-transaction backstop can.+const RACED_PREFLIGHT_CREATED_AT = new Date(+ Math.min(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime(),+ LEGACY_BLOB_EXPORTER_CUTOFF.getTime(),+ ) -+ 30 * MS_PER_DAY,+);const buildSession = (orgId: string, projectId: string): Session => ({expires: "1",@@ -115,7 +136,10 @@ describe("Mixpanel Integration legacy export source cutoff gate", () => {return { project, caller };};- it("Cloud + pre-cutoff project + legacy source β allow", async () => {+ // A pre-cutoff project no longer buys a *new* integration the right to a+ // legacy source. Existing integrations stay grandfathered β see+ // "Cloud new-integration enriched pin" below.+ it("Cloud + pre-cutoff project + legacy source on create β BAD_REQUEST", async () => {const originalRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";try {@@ -130,7 +154,7 @@ describe("Mixpanel Integration legacy export source cutoff gate", () => {...baseConfig,exportSource: "TRACES_OBSERVATIONS" as const,}),- ).resolves.not.toThrow();+ ).rejects.toMatchObject({ code: "BAD_REQUEST" });} finally {(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalRegion;}@@ -455,19 +479,21 @@ describe("Mixpanel Integration legacy export source cutoff gate", () => {expect(result.config?.exportSource).toBe("TRACES_OBSERVATIONS");});- it("Cloud + post-cutoff project + create without exportSource β BAD_REQUEST (validated default)", async () => {+ it("Cloud + post-cutoff project + create without exportSource β persists EVENTS", async () => {(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";const { caller, project } = await prepare();await prisma.project.update({where: { id: project.id },data: { createdAt: POST_CUTOFF },});- await expect(- caller.mixpanelIntegration.update({- projectId: project.id,- ...baseConfig,- }),- ).rejects.toMatchObject({ code: "BAD_REQUEST" });+ await caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ });+ const result = await caller.mixpanelIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");});it("raced delete between read and write: mis-created legacy row is rolled back (post-cutoff Cloud)", async () => {@@ -504,6 +530,63 @@ describe("Mixpanel Integration legacy export source cutoff gate", () => {).toBeNull();});+ // Both raced-CREATE cases below share one shape and differ only in whether+ // the caller sent an explicit exportSource. That difference is the point:+ // the real settings form always sends one (it lives in the form's+ // defaultValues and onSubmit spreads every value), so a backstop that only+ // runs when the field is omitted never runs in production.+ const expectRacedCreateRejected = async (+ extraInput: { exportSource?: "TRACES_OBSERVATIONS" } = {},+ ) => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ const { caller, project } = await prepare();+ // Pre-cutoff project, so the project-level gate cannot fire either.+ await prisma.project.update({+ where: { id: project.id },+ data: { createdAt: PRE_CUTOFF },+ });+ // The pre-flight read sees a grandfathered row and allows the legacy+ // source; the DB holds no row, so the upsert lands as a CREATE. Reusing+ // the stale createdAt, or skipping the backstop, persists a legacy row.+ const spy = vi+ .spyOn(prisma.mixpanelIntegration, "findUnique")+ .mockResolvedValueOnce({+ exportSource: "TRACES_OBSERVATIONS",+ createdAt: RACED_PREFLIGHT_CREATED_AT,+ } as never);+ try {+ await expect(+ caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ ...extraInput,+ }),+ ).rejects.toMatchObject({+ code: "BAD_REQUEST",+ // Pins the cutoff path: an unrelated BAD_REQUEST (credentials,+ // validation) must not satisfy this test.+ message: expect.stringContaining("created on or after"),+ });+ } finally {+ spy.mockRestore();+ }+ expect(+ await prisma.mixpanelIntegration.findUnique({+ where: { projectId: project.id },+ }),+ ).toBeNull();+ };++ it("raced delete with the source omitted: the backstop re-validates as a brand-new row, not the stale pre-flight row", async () => {+ await expectRacedCreateRejected();+ });++ it("raced delete with the source explicitly present: the backstop still re-validates as a brand-new row", async () => {+ await expectRacedCreateRejected({+ exportSource: "TRACES_OBSERVATIONS",+ });+ });+it("create without exportSource defaults to EVENTS on events_only", async () => {(env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = "events_only";const { caller, project } = await prepare();@@ -518,6 +601,148 @@ describe("Mixpanel Integration legacy export source cutoff gate", () => {});});+ // New Cloud integrations land on the enriched events source and cannot be+ // moved off it. Rows created before the analytics exporter cutoff are+ // grandfathered: they keep their legacy source and may opt into enriched, but+ // are never rewritten behind the user's back. Self-hosted is exempt β the+ // create defaults above still pass unchanged.+ describe("Cloud new-integration enriched pin", () => {+ const originalRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;+ const originalWriteMode = env.LANGFUSE_MIGRATION_V4_WRITE_MODE;++ beforeEach(() => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ // dual keeps legacy writes active, so any rejection below comes from the+ // Cloud cutoff rather than the write-mode capability gate.+ (env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = "dual";+ });++ afterEach(() => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalRegion;+ (env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = originalWriteMode;+ });++ // Pre-cutoff project so the only Cloud gate in play is the+ // integration-level one.+ const prepareOldProject = async () => {+ const { caller, project } = await prepare();+ await prisma.project.update({+ where: { id: project.id },+ data: { createdAt: PRE_CUTOFF },+ });+ return { caller, project };+ };++ // Seeds a legacy row of a chosen age: created while self-hosted (where+ // legacy is still selectable), then backdated.+ const seedLegacyRow = async (+ caller: Awaited<ReturnType<typeof prepare>>["caller"],+ projectId: string,+ createdAt: Date,+ exportSource: "TRACES_OBSERVATIONS" | "EVENTS" = "TRACES_OBSERVATIONS",+ ) => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ try {+ await caller.mixpanelIntegration.update({+ projectId,+ ...baseConfig,+ exportSource,+ });+ } finally {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ }+ await prisma.mixpanelIntegration.update({+ where: { projectId },+ data: { createdAt },+ });+ };++ it("create with no existing row persists EVENTS", async () => {+ const { caller, project } = await prepareOldProject();+ await caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ });+ const result = await caller.mixpanelIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");+ });++ it("pre-cutoff row with a persisted legacy source: omitted source is preserved, not rewritten", async () => {+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(caller, project.id, ROW_PRE_ANALYTICS_CUTOFF);+ await expect(+ caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ enabled: false,+ }),+ ).resolves.not.toThrow();+ const result = await caller.mixpanelIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("TRACES_OBSERVATIONS");+ expect(result.config?.enabled).toBe(false);+ });++ it("pre-cutoff row may re-assert legacy, opt into enriched, and switch back", async () => {+ // The gate keys on the row's creation date, not its current source, so+ // trying enriched must not be a one-way door: someone evaluating it needs+ // to be able to back out while legacy still exists. The row also predates+ // the analytics cutoff while postdating the blob one, so this catches an+ // adapter that forwards the blob cutoff by mistake.+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(caller, project.id, ROW_PRE_ANALYTICS_CUTOFF);+ await expect(+ caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ }),+ ).resolves.not.toThrow();+ await caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "EVENTS" as const,+ });+ expect(+ (await caller.mixpanelIntegration.get({ projectId: project.id })).config+ ?.exportSource,+ ).toBe("EVENTS");+ await caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ });+ expect(+ (await caller.mixpanelIntegration.get({ projectId: project.id })).config+ ?.exportSource,+ ).toBe("TRACES_OBSERVATIONS");+ });++ it("post-cutoff row requesting a legacy source β BAD_REQUEST", async () => {+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(+ caller,+ project.id,+ ROW_POST_ANALYTICS_CUTOFF,+ "EVENTS",+ );+ await expect(+ caller.mixpanelIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ }),+ ).rejects.toMatchObject({ code: "BAD_REQUEST" });+ const result = await caller.mixpanelIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");+ });+ });+// LFE-14384: the credential is write-only β get returns only a masked// display value; a blank token on update keeps the persisted encrypted value.describe("Mixpanel credential masking", () => {diff --git a/web/src/__tests__/server/posthog-integration.servertest.ts b/web/src/__tests__/server/posthog-integration.servertest.tsindex 57b92b7053..754a146873 100644--- a/web/src/__tests__/server/posthog-integration.servertest.ts+++ b/web/src/__tests__/server/posthog-integration.servertest.ts@@ -3,13 +3,34 @@ import { prisma, Prisma } from "@langfuse/shared/src/db";import { appRouter } from "@/src/server/api/root";import { createInnerTRPCContext } from "@/src/server/api/trpc";import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";-import { LEGACY_BLOB_EXPORT_CUTOFF } from "@langfuse/shared";+import {+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,+ LEGACY_BLOB_EXPORT_CUTOFF,+ LEGACY_BLOB_EXPORTER_CUTOFF,+} from "@langfuse/shared";import { decrypt } from "@langfuse/shared/encryption";import { env } from "@/src/env.mjs";const MS_PER_DAY = 24 * 60 * 60 * 1000;const PRE_CUTOFF = new Date(LEGACY_BLOB_EXPORT_CUTOFF.getTime() - MS_PER_DAY);const POST_CUTOFF = new Date(LEGACY_BLOB_EXPORT_CUTOFF.getTime() + MS_PER_DAY);+// Row ages derive from the cutoff constants, never from literals, so the+// NEXT_PUBLIC_LANGFUSE_*_CUTOFF dev overrides cannot fail the suite.+const ROW_PRE_ANALYTICS_CUTOFF = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() - MS_PER_DAY,+);+const ROW_POST_ANALYTICS_CUTOFF = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() + MS_PER_DAY,+);+// Grandfathered under BOTH exporter cutoffs, so the pre-flight assert provably+// cannot be what rejects β only the in-transaction backstop can.+const RACED_PREFLIGHT_CREATED_AT = new Date(+ Math.min(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime(),+ LEGACY_BLOB_EXPORTER_CUTOFF.getTime(),+ ) -+ 30 * MS_PER_DAY,+);const buildSession = (orgId: string, projectId: string): Session => ({expires: "1",@@ -159,7 +180,10 @@ describe("PostHog Integration legacy export source cutoff gate", () => {return { project, caller };};- it("Cloud + pre-cutoff project + legacy source β allow", async () => {+ // A pre-cutoff project no longer buys a *new* integration the right to a+ // legacy source. Existing integrations stay grandfathered β see+ // "Cloud new-integration enriched pin" below.+ it("Cloud + pre-cutoff project + legacy source on create β BAD_REQUEST", async () => {const originalRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";try {@@ -174,7 +198,7 @@ describe("PostHog Integration legacy export source cutoff gate", () => {...baseConfig,exportSource: "TRACES_OBSERVATIONS" as const,}),- ).resolves.not.toThrow();+ ).rejects.toMatchObject({ code: "BAD_REQUEST" });} finally {(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalRegion;}@@ -499,19 +523,21 @@ describe("PostHog Integration legacy export source cutoff gate", () => {expect(result.config?.exportSource).toBe("TRACES_OBSERVATIONS");});- it("Cloud + post-cutoff project + create without exportSource β BAD_REQUEST (validated default)", async () => {+ it("Cloud + post-cutoff project + create without exportSource β persists EVENTS", async () => {(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";const { caller, project } = await prepare();await prisma.project.update({where: { id: project.id },data: { createdAt: POST_CUTOFF },});- await expect(- caller.posthogIntegration.update({- projectId: project.id,- ...baseConfig,- }),- ).rejects.toMatchObject({ code: "BAD_REQUEST" });+ await caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ });+ const result = await caller.posthogIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");});it("raced delete between read and write: mis-created legacy row is rolled back (post-cutoff Cloud)", async () => {@@ -548,6 +574,63 @@ describe("PostHog Integration legacy export source cutoff gate", () => {).toBeNull();});+ // Both raced-CREATE cases below share one shape and differ only in whether+ // the caller sent an explicit exportSource. That difference is the point:+ // the real settings form always sends one (it lives in the form's+ // defaultValues and onSubmit spreads every value), so a backstop that only+ // runs when the field is omitted never runs in production.+ const expectRacedCreateRejected = async (+ extraInput: { exportSource?: "TRACES_OBSERVATIONS" } = {},+ ) => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ const { caller, project } = await prepare();+ // Pre-cutoff project, so the project-level gate cannot fire either.+ await prisma.project.update({+ where: { id: project.id },+ data: { createdAt: PRE_CUTOFF },+ });+ // The pre-flight read sees a grandfathered row and allows the legacy+ // source; the DB holds no row, so the upsert lands as a CREATE. Reusing+ // the stale createdAt, or skipping the backstop, persists a legacy row.+ const spy = vi+ .spyOn(prisma.posthogIntegration, "findUnique")+ .mockResolvedValueOnce({+ exportSource: "TRACES_OBSERVATIONS",+ createdAt: RACED_PREFLIGHT_CREATED_AT,+ } as never);+ try {+ await expect(+ caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ ...extraInput,+ }),+ ).rejects.toMatchObject({+ code: "BAD_REQUEST",+ // Pins the cutoff path: an unrelated BAD_REQUEST (credentials,+ // validation) must not satisfy this test.+ message: expect.stringContaining("created on or after"),+ });+ } finally {+ spy.mockRestore();+ }+ expect(+ await prisma.posthogIntegration.findUnique({+ where: { projectId: project.id },+ }),+ ).toBeNull();+ };++ it("raced delete with the source omitted: the backstop re-validates as a brand-new row, not the stale pre-flight row", async () => {+ await expectRacedCreateRejected();+ });++ it("raced delete with the source explicitly present: the backstop still re-validates as a brand-new row", async () => {+ await expectRacedCreateRejected({+ exportSource: "TRACES_OBSERVATIONS",+ });+ });+it("create without exportSource defaults to EVENTS on events_only", async () => {(env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = "events_only";const { caller, project } = await prepare();@@ -562,6 +645,148 @@ describe("PostHog Integration legacy export source cutoff gate", () => {});});+ // New Cloud integrations land on the enriched events source and cannot be+ // moved off it. Rows created before the analytics exporter cutoff are+ // grandfathered: they keep their legacy source and may opt into enriched, but+ // are never rewritten behind the user's back. Self-hosted is exempt β the+ // create defaults above still pass unchanged.+ describe("Cloud new-integration enriched pin", () => {+ const originalRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;+ const originalWriteMode = env.LANGFUSE_MIGRATION_V4_WRITE_MODE;++ beforeEach(() => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ // dual keeps legacy writes active, so any rejection below comes from the+ // Cloud cutoff rather than the write-mode capability gate.+ (env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = "dual";+ });++ afterEach(() => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalRegion;+ (env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = originalWriteMode;+ });++ // Pre-cutoff project so the only Cloud gate in play is the+ // integration-level one.+ const prepareOldProject = async () => {+ const { caller, project } = await prepare();+ await prisma.project.update({+ where: { id: project.id },+ data: { createdAt: PRE_CUTOFF },+ });+ return { caller, project };+ };++ // Seeds a row of a chosen age: created while self-hosted (where legacy is+ // still selectable), then backdated.+ const seedLegacyRow = async (+ caller: Awaited<ReturnType<typeof prepare>>["caller"],+ projectId: string,+ createdAt: Date,+ exportSource: "TRACES_OBSERVATIONS" | "EVENTS" = "TRACES_OBSERVATIONS",+ ) => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ try {+ await caller.posthogIntegration.update({+ projectId,+ ...baseConfig,+ exportSource,+ });+ } finally {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";+ }+ await prisma.posthogIntegration.update({+ where: { projectId },+ data: { createdAt },+ });+ };++ it("create with no existing row persists EVENTS", async () => {+ const { caller, project } = await prepareOldProject();+ await caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ });+ const result = await caller.posthogIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");+ });++ it("pre-cutoff row with a persisted legacy source: omitted source is preserved, not rewritten", async () => {+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(caller, project.id, ROW_PRE_ANALYTICS_CUTOFF);+ await expect(+ caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ enabled: false,+ }),+ ).resolves.not.toThrow();+ const result = await caller.posthogIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("TRACES_OBSERVATIONS");+ expect(result.config?.enabled).toBe(false);+ });++ it("pre-cutoff row may re-assert legacy, opt into enriched, and switch back", async () => {+ // The gate keys on the row's creation date, not its current source, so+ // trying enriched must not be a one-way door: someone evaluating it needs+ // to be able to back out while legacy still exists. The row also predates+ // the analytics cutoff while postdating the blob one, so this catches an+ // adapter that forwards the blob cutoff by mistake.+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(caller, project.id, ROW_PRE_ANALYTICS_CUTOFF);+ await expect(+ caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ }),+ ).resolves.not.toThrow();+ await caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "EVENTS" as const,+ });+ expect(+ (await caller.posthogIntegration.get({ projectId: project.id })).config+ ?.exportSource,+ ).toBe("EVENTS");+ await caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ });+ expect(+ (await caller.posthogIntegration.get({ projectId: project.id })).config+ ?.exportSource,+ ).toBe("TRACES_OBSERVATIONS");+ });++ it("post-cutoff row requesting a legacy source β BAD_REQUEST", async () => {+ const { caller, project } = await prepareOldProject();+ await seedLegacyRow(+ caller,+ project.id,+ ROW_POST_ANALYTICS_CUTOFF,+ "EVENTS",+ );+ await expect(+ caller.posthogIntegration.update({+ projectId: project.id,+ ...baseConfig,+ exportSource: "TRACES_OBSERVATIONS" as const,+ }),+ ).rejects.toMatchObject({ code: "BAD_REQUEST" });+ const result = await caller.posthogIntegration.get({+ projectId: project.id,+ });+ expect(result.config?.exportSource).toBe("EVENTS");+ });+ });+// LFE-14384: the credential is write-only β get returns only a masked// display value; a blank key on update keeps the persisted encrypted value.describe("PostHog credential masking", () => {diff --git a/web/src/features/analytics-integrations/exportSource.clienttest.ts b/web/src/features/analytics-integrations/exportSource.clienttest.tsindex 6bbd6d9590..fddfca5202 100644--- a/web/src/features/analytics-integrations/exportSource.clienttest.ts+++ b/web/src/features/analytics-integrations/exportSource.clienttest.ts@@ -1,5 +1,6 @@import {AnalyticsIntegrationExportSource,+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,LEGACY_BLOB_EXPORT_CUTOFF,LEGACY_BLOB_EXPORTER_CUTOFF,type BlobExportWriteMode,@@ -542,3 +543,107 @@ describe("getExportSourceUnavailableMessage", () => {expect(message).not.toContain("no longer available for this project");});});++// Mirrors the context the analytics settings pages build. Row ages derive from+// the constant, never from literals: it is overridable via+// NEXT_PUBLIC_LANGFUSE_ANALYTICS_EXPORTER_CUTOFF.+describe("analytics settings pages: the new-integration enriched pin", () => {+ const ROW_PRE_ANALYTICS = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() - MS_PER_DAY,+ );+ const ROW_POST_ANALYTICS = new Date(+ LEGACY_ANALYTICS_EXPORTER_CUTOFF.getTime() + MS_PER_DAY,+ );++ const analyticsCloudCtx = (+ integrationCreatedAt: Date | null,+ ): ExportSourceContext =>+ buildExportSourceContext({+ writeMode: "dual",+ isCloud: true,+ // Pre-cutoff project, so the only Cloud gate in play is the+ // integration-level one.+ projectCreatedAt: PROJECT_PRE,+ integrationCreatedAt,+ exporterCutoff: LEGACY_ANALYTICS_EXPORTER_CUTOFF,+ });++ it("brand-new Cloud integration: selector hidden, pinned to the enriched source", () => {+ const { showField, defaultValue, options } = getExportSourceFieldState(+ undefined,+ analyticsCloudCtx(null),+ );+ expect(showField).toBe(false);+ expect(defaultValue).toBe(AnalyticsIntegrationExportSource.EVENTS);+ // Hidden because there is nothing left to choose, not because the choice+ // was suppressed.+ expect(options.map((o) => o.value)).toEqual([+ AnalyticsIntegrationExportSource.EVENTS,+ ]);+ });++ it("grandfathered pre-cutoff Cloud integration: selector shown, legacy source kept and still selectable", () => {+ const ctx = analyticsCloudCtx(ROW_PRE_ANALYTICS);+ const { showField, defaultValue } = getExportSourceFieldState(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ ctx,+ );+ expect(showField).toBe(true);+ expect(defaultValue).toBe(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ );+ // A real choice: opting into enriched is offered, never forced.+ expect(+ isExportSourceSelectable(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ ctx,+ ),+ ).toBe(true);+ expect(+ isExportSourceSelectable(AnalyticsIntegrationExportSource.EVENTS, ctx),+ ).toBe(true);+ });++ it("post-cutoff Cloud integration on a legacy source: selector forced visible, source kept, never rewritten", () => {+ // Reachable: a Cloud row created after the cutoff date but before this gate+ // shipped still carries the legacy source. Pinning the form value to the+ // enriched source here would change what it exports on the next save of any+ // unrelated field.+ const ctx = analyticsCloudCtx(ROW_POST_ANALYTICS);+ const { showField, defaultValue, options } = getExportSourceFieldState(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ ctx,+ );+ expect(showField).toBe(true);+ expect(defaultValue).toBe(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ );+ // Kept, but blocked β the save must fail rather than substitute a source.+ expect(+ isExportSourceSelectable(+ AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ ctx,+ ),+ ).toBe(false);+ expect(+ options.find(+ (o) => o.value === AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ )?.unavailable,+ ).toBe(true);+ });++ it("a grandfathered row already on enriched still offers the way back to legacy", () => {+ // The UI half of the reversibility guarantee: hiding the selector once the+ // row sits on enriched would leave the escape hatch API-only.+ const { showField, options } = getExportSourceFieldState(+ AnalyticsIntegrationExportSource.EVENTS,+ analyticsCloudCtx(ROW_PRE_ANALYTICS),+ );+ expect(showField).toBe(true);+ expect(+ options.find(+ (o) => o.value === AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,+ ),+ ).toMatchObject({ unavailable: false });+ });+});diff --git a/web/src/features/analytics-integrations/exportSource.ts b/web/src/features/analytics-integrations/exportSource.tsindex b8b9f32d25..8dd86bb743 100644--- a/web/src/features/analytics-integrations/exportSource.ts+++ b/web/src/features/analytics-integrations/exportSource.ts@@ -22,11 +22,13 @@ export function buildExportSourceContext({isCloud,projectCreatedAt,integrationCreatedAt,+ exporterCutoff,}: {writeMode: BlobExportWriteMode;isCloud: boolean;projectCreatedAt?: Date;integrationCreatedAt?: Date | null;+ exporterCutoff?: Date;}): ExportSourceContext {return {isCloud,@@ -34,6 +36,7 @@ export function buildExportSourceContext({legacyWritesActive: areLegacyWritesActive(writeMode),projectCreatedAt,integrationCreatedAt,+ exporterCutoff,};}diff --git a/web/src/features/analytics-integrations/server/analyticsExportSource.ts b/web/src/features/analytics-integrations/server/analyticsExportSource.tsindex 28cf593df1..00a8ef767a 100644--- a/web/src/features/analytics-integrations/server/analyticsExportSource.ts+++ b/web/src/features/analytics-integrations/server/analyticsExportSource.ts@@ -3,6 +3,7 @@ import {areEnrichedWritesActive,areLegacyWritesActive,InvalidRequestError,+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,validateExportSource,type ExportSourceContext,} from "@langfuse/shared";@@ -40,13 +41,21 @@ function readDeployment(): Deployment {function buildContext(deployment: Deployment,projectCreatedAt: Date | undefined,+ integrationCreatedAt: Date | null,): ExportSourceContext {- return { ...deployment, projectCreatedAt };+ return {+ ...deployment,+ projectCreatedAt,+ integrationCreatedAt,+ exporterCutoff: LEGACY_ANALYTICS_EXPORTER_CUTOFF,+ };}/*** Validates the requested source (throwing InvalidRequestError when blocked)- * and returns the value the upsert's CREATE branch should carry.+ * and returns the value the upsert's CREATE branch should carry. That value+ * prefers the persisted source, because a concurrent delete can turn an upsert+ * meant as an UPDATE into a CREATE; assertRacedCreateAllowed judges what lands.*/export async function resolveAnalyticsExportSource({db,@@ -60,9 +69,12 @@ export async function resolveAnalyticsExportSource({existingIntegration: ExistingAnalyticsIntegration | null | undefined;}): Promise<AnalyticsIntegrationExportSource> {const deployment = readDeployment();- const createDefaultExportSource = deployment.legacyWritesActive- ? AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS- : AnalyticsIntegrationExportSource.EVENTS;+ // On Cloud the legacy source is unavailable to a new row, so defaulting to it+ // would only produce a rejection.+ const createDefaultExportSource =+ deployment.isCloud || !deployment.legacyWritesActive+ ? AnalyticsIntegrationExportSource.EVENTS+ : AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS;const nextExportSource =requestedExportSource ??@@ -81,33 +93,42 @@ export async function resolveAnalyticsExportSource({assertExportSourceAllowed({nextExportSource,persistedExportSource: existingIntegration?.exportSource,- ctx: buildContext(deployment, projectCreatedAt),+ ctx: buildContext(+ deployment,+ projectCreatedAt,+ existingIntegration?.createdAt ?? null,+ ),});- return createDefaultExportSource;+ return (+ requestedExportSource ??+ existingIntegration?.exportSource ??+ createDefaultExportSource+ );}/*** In-transaction backstop (mirrors blob storage's service.ts). The pre-flight- * read is racy: a concurrent delete can flip the expected UPDATE into a CREATE- * carrying the unvalidated legacy default. A changed createdAt detects that,- * and the row is then re-validated as an explicit choice.+ * read is racy: a concurrent delete can flip the expected UPDATE into a CREATE.+ * A changed createdAt detects that, and the row is re-validated as the brand-new+ * row it is β not as the grandfathered one the pre-flight read saw.+ *+ * This runs whether or not the request named a source: an explicit source is+ * also validated against the existing row's age, so a raced create leaves it+ * judged by a row that no longer exists.*/export async function assertRacedCreateAllowed({tx,projectId,- requestedExportSource,existingIntegration,result,}: {tx: Prisma.TransactionClient;projectId: string;- requestedExportSource: AnalyticsIntegrationExportSource | undefined;existingIntegration: ExistingAnalyticsIntegration | null | undefined;result: ExistingAnalyticsIntegration;}): Promise<void> {if (- requestedExportSource !== undefined ||!existingIntegration ||result.createdAt.getTime() === existingIntegration.createdAt.getTime()) {@@ -119,7 +140,7 @@ export async function assertRacedCreateAllowed({});const validation = validateExportSource(result.exportSource,- buildContext(readDeployment(), project.createdAt),+ buildContext(readDeployment(), project.createdAt, null),);if (!validation.ok) throw new InvalidRequestError(validation.message);}diff --git a/web/src/features/mixpanel-integration/mixpanel-integration-router.ts b/web/src/features/mixpanel-integration/mixpanel-integration-router.tsindex 67075e71b9..f4a537464c 100644--- a/web/src/features/mixpanel-integration/mixpanel-integration-router.ts+++ b/web/src/features/mixpanel-integration/mixpanel-integration-router.ts@@ -115,7 +115,7 @@ export const mixpanelIntegrationRouter = createTRPCRouter({message: "Mixpanel Project Token is required",});}- const createDefaultExportSource = await resolveAnalyticsExportSource({+ const createExportSource = await resolveAnalyticsExportSource({db: ctx.prisma,projectId: input.projectId,requestedExportSource: input.exportSource,@@ -140,7 +140,7 @@ export const mixpanelIntegrationRouter = createTRPCRouter({mixpanelRegion: config.mixpanelRegion,encryptedMixpanelProjectToken,enabled: config.enabled,- exportSource: config.exportSource ?? createDefaultExportSource,+ exportSource: createExportSource,},update: {encryptedMixpanelProjectToken,@@ -155,7 +155,6 @@ export const mixpanelIntegrationRouter = createTRPCRouter({await assertRacedCreateAllowed({tx,projectId: input.projectId,- requestedExportSource: input.exportSource,existingIntegration,result,});diff --git a/web/src/features/posthog-integration/posthog-integration-router.ts b/web/src/features/posthog-integration/posthog-integration-router.tsindex 1e0ee97cbf..c80afb1595 100644--- a/web/src/features/posthog-integration/posthog-integration-router.ts+++ b/web/src/features/posthog-integration/posthog-integration-router.ts@@ -128,7 +128,7 @@ export const posthogIntegrationRouter = createTRPCRouter({message: "PostHog Project API Key is required",});}- const createDefaultExportSource = await resolveAnalyticsExportSource({+ const createExportSource = await resolveAnalyticsExportSource({db: ctx.prisma,projectId: input.projectId,requestedExportSource: input.exportSource,@@ -153,7 +153,7 @@ export const posthogIntegrationRouter = createTRPCRouter({posthogHostName: config.posthogHostname,encryptedPosthogApiKey,enabled: config.enabled,- exportSource: config.exportSource ?? createDefaultExportSource,+ exportSource: createExportSource,},update: {encryptedPosthogApiKey,@@ -170,7 +170,6 @@ export const posthogIntegrationRouter = createTRPCRouter({await assertRacedCreateAllowed({tx,projectId: input.projectId,- requestedExportSource: input.exportSource,existingIntegration,result,});diff --git a/web/src/pages/project/[projectId]/settings/integrations/mixpanel.tsx b/web/src/pages/project/[projectId]/settings/integrations/mixpanel.tsxindex adc51ede0c..d0b484344e 100644--- a/web/src/pages/project/[projectId]/settings/integrations/mixpanel.tsx+++ b/web/src/pages/project/[projectId]/settings/integrations/mixpanel.tsx@@ -33,6 +33,7 @@ import {type MixpanelRegion,} from "@/src/features/mixpanel-integration/types";import {+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,validateExportSource,type BlobExportWriteMode,type ExportSourceContext,@@ -169,14 +170,19 @@ const MixpanelIntegrationSettingsForm = ({}) => {const capture = usePostHogClientCapture();const { isLangfuseCloud } = useLangfuseCloudRegion();+ const integrationCreatedAt = state?.createdAt;const exportSourceCtx: ExportSourceContext = useMemo(() =>buildExportSourceContext({writeMode,isCloud: isLangfuseCloud,projectCreatedAt: new Date(projectCreatedAt),+ integrationCreatedAt: integrationCreatedAt+ ? new Date(integrationCreatedAt)+ : null,+ exporterCutoff: LEGACY_ANALYTICS_EXPORTER_CUTOFF,}),- [writeMode, isLangfuseCloud, projectCreatedAt],+ [writeMode, isLangfuseCloud, projectCreatedAt, integrationCreatedAt],);const {options: exportSourceOptions,diff --git a/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx b/web/src/pages/project/[projectId]/settings/integrations/posthog.tsxindex 8c72160612..d4f2660e7d 100644--- a/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx+++ b/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx@@ -31,6 +31,7 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePosimport { posthogIntegrationFormSchema } from "@/src/features/posthog-integration/types";import { PostHogStatusSection } from "@/src/features/posthog-integration/components/PostHogStatusSection";import {+ LEGACY_ANALYTICS_EXPORTER_CUTOFF,validateExportSource,type BlobExportWriteMode,type ExportSourceContext,@@ -163,14 +164,19 @@ const PostHogIntegrationSettings = ({}) => {const capture = usePostHogClientCapture();const { isLangfuseCloud } = useLangfuseCloudRegion();+ const integrationCreatedAt = state?.createdAt;const exportSourceCtx: ExportSourceContext = useMemo(() =>buildExportSourceContext({writeMode,isCloud: isLangfuseCloud,projectCreatedAt: new Date(projectCreatedAt),+ integrationCreatedAt: integrationCreatedAt+ ? new Date(integrationCreatedAt)+ : null,+ exporterCutoff: LEGACY_ANALYTICS_EXPORTER_CUTOFF,}),- [writeMode, isLangfuseCloud, projectCreatedAt],+ [writeMode, isLangfuseCloud, projectCreatedAt, integrationCreatedAt],);const {options: exportSourceOptions,