:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(mcp-analytics): add the weekly report offer logic (#128665)
Lucas Fariaaa471a938f2e
diff --git a/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.test.ts b/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.test.tsindex ac425b2f0259..813f93bcadd1 100644--- a/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.test.ts+++ b/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.test.ts@@ -1,22 +1,36 @@+import { MOCK_DEFAULT_ORGANIZATION } from 'lib/api.mock'+import { expectLogic } from 'kea-test-utils'+import { ApiError } from 'lib/api-error'+import { FEATURE_FLAGS } from 'lib/constants'import { integrationsLogic } from 'lib/integrations/integrationsLogic'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'+import { preflightLogic } from 'lib/logic/preflightLogic'+import posthog from 'lib/posthog-typed'import { deleteWithUndo } from 'lib/utils/deleteWithUndo'+import { appLogic } from 'scenes/appLogic'+import { organizationLogic } from 'scenes/organizationLogic'import { userLogic } from 'scenes/userLogic'+import { aiConsentLogic } from '~/layout/ai/aiConsentLogic'+import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'-import { IntegrationType, UserType } from '~/types'+import { IntegrationType, OrganizationType, UserType } from '~/types'import { toggleSubscriptionEnabled } from 'products/subscriptions/frontend/components/Subscriptions/toggleSubscriptionEnabled'-import { subscriptionsList } from 'products/subscriptions/frontend/generated/api'+import { subscriptionsCreate, subscriptionsList } from 'products/subscriptions/frontend/generated/api'import type { SubscriptionApi } from 'products/subscriptions/frontend/generated/api.schemas'import { isMCPRecurringReport, mcpRecurringReportsLogic } from './mcpRecurringReportsLogic'-import { MCP_RECURRING_REPORTS } from './recurringReportDefinitions'+import { MCP_RECURRING_REPORTS, TOOL_HEALTH_REPORT } from './recurringReportDefinitions'+jest.mock('lib/lemon-ui/LemonToast', () => ({ lemonToast: { success: jest.fn(), error: jest.fn() } }))jest.mock('lib/utils/deleteWithUndo', () => ({ deleteWithUndo: jest.fn() }))+jest.mock('lib/posthog-typed', () => ({ __esModule: true, default: { capture: jest.fn(), captureRaw: jest.fn() } }))jest.mock('products/subscriptions/frontend/generated/api', () => ({subscriptionsList: jest.fn(),+ subscriptionsCreate: jest.fn(),}))jest.mock('products/subscriptions/frontend/components/Subscriptions/toggleSubscriptionEnabled', () => ({toggleSubscriptionEnabled: jest.fn(),@@ -24,6 +38,8 @@ jest.mock('products/subscriptions/frontend/components/Subscriptions/toggleSubscrconst mockedDeleteWithUndo = jest.mocked(deleteWithUndo)const mockedList = jest.mocked(subscriptionsList)+const mockedCreate = jest.mocked(subscriptionsCreate)+const mockedCaptureRaw = jest.mocked(posthog.captureRaw)const mockedToggle = jest.mocked(toggleSubscriptionEnabled)const MCP_PROMPT = 'Summarize $mcp_intent on $mcp_tool_call events this week.'@@ -53,6 +69,8 @@ describe('mcpRecurringReportsLogic', () => {mockedDeleteWithUndo.mockReset().mockResolvedValue()mockedToggle.mockReset().mockResolvedValue(true)mockedList.mockReset()+ mockedCreate.mockReset()+ mockedCaptureRaw.mockClear()listReturns([])logic = mcpRecurringReportsLogic()@@ -243,4 +261,229 @@ describe('mcpRecurringReportsLogic', () => {expect(logic.values.reports.map((report) => report.id)).toEqual([first.id])})++ describe('creating the weekly report', () => {+ const SAVED = makeReport(9, {+ title: TOOL_HEALTH_REPORT.title,+ target_type: 'email',+ } as Partial<SubscriptionApi>)+ const FALLBACK_MESSAGE = "Couldn't create the report. Try again, or set it up from the Notifications tab."++ async function becomeReadyToCreate(): Promise<void> {+ // Let the mounted preflight request land first, so it cannot overwrite the answer set here.+ await expectLogic(preflightLogic).toFinishAllListeners()+ preflightLogic.actions.loadPreflightSuccess({ cloud: true, is_debug: false } as any)+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.SUBSCRIPTION_AI_PROMPT], {+ [FEATURE_FLAGS.SUBSCRIPTION_AI_PROMPT]: true,+ })+ userLogic.actions.loadUserSuccess({ uuid: 'user-me', email: '[email protected]' } as UserType)+ await expectLogic(logic, () => logic.actions.loadReports()).toFinishAllListeners()+ }++ it('is ready once everything it depends on has loaded', async () => {+ await becomeReadyToCreate()++ expect(logic.values.weeklyReportOffer).toEqual({ state: 'ready', email: '[email protected]' })+ })++ it('hides instead of loading forever when the app stops waiting for flags', async () => {+ await expectLogic(preflightLogic).toFinishAllListeners()+ preflightLogic.actions.loadPreflightSuccess({ cloud: true, is_debug: false } as any)+ userLogic.actions.loadUserSuccess({ uuid: 'user-me', email: '[email protected]' } as UserType)+ await expectLogic(logic, () => logic.actions.loadReports()).toFinishAllListeners()+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'loading' })++ appLogic.actions.ignoreFeatureFlags()++ expect(logic.values.weeklyReportOffer).toEqual({ state: 'hidden', reason: 'not-enabled' })+ })++ it('creates one report when clicked twice while the first request is in flight', async () => {+ await becomeReadyToCreate()+ let finishCreate: (report: SubscriptionApi) => void = () => {}+ mockedCreate.mockReturnValue(new Promise((resolve) => (finishCreate = resolve)))++ logic.actions.createWeeklyReport()+ logic.actions.createWeeklyReport()+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'creating', email: '[email protected]' })+ finishCreate(SAVED)+ await expectLogic(logic).toFinishAllListeners()++ expect(mockedCreate).toHaveBeenCalledTimes(1)+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'created', report: SAVED })+ })++ it('sends the tool health report to the user, weekly, with no immediate send', async () => {+ await becomeReadyToCreate()+ mockedCreate.mockResolvedValue(SAVED)++ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()++ expect(mockedCreate).toHaveBeenCalledWith(+ expect.any(String),+ expect.objectContaining({+ title: TOOL_HEALTH_REPORT.title,+ prompt: TOOL_HEALTH_REPORT.prompt,+ target_type: 'email',+ target_value: '[email protected]',+ frequency: 'weekly',+ send_test_now: false,+ })+ )+ })++ it('files the new report under its template and reports the creation', async () => {+ await becomeReadyToCreate()+ mockedCreate.mockResolvedValue(SAVED)++ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()++ expect(logic.values.reportsByTemplateTitle[TOOL_HEALTH_REPORT.title]).toEqual([SAVED])+ expect(mockedCaptureRaw).toHaveBeenCalledWith('subscription created', {+ creation_source: 'mcp_analytics_one_click',+ resource_type: 'ai',+ subscription_id: 9,+ target_type: 'email',+ mcp_report_template: 'tool-health',+ })+ })++ it.each([+ [+ 'the server explains',+ new ApiError('Bad Request', 400, undefined, { detail: 'Your plan is limited to 5 subscriptions.' }),+ 'Your plan is limited to 5 subscriptions.',+ false,+ ],+ ['the server fails', new ApiError('Server Error', 500), FALLBACK_MESSAGE, true],+ ['the network fails', new TypeError('Failed to fetch'), FALLBACK_MESSAGE, true],+ ])('fails with a message and allows a retry when %s', async (_name, error, message, reloadsList) => {+ await becomeReadyToCreate()+ const listCallsBefore = mockedList.mock.calls.length+ mockedCreate.mockRejectedValueOnce(error).mockResolvedValueOnce(SAVED)++ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'failed', email: '[email protected]', message })+ expect(logic.values.reports).toEqual([])+ expect(mockedList).toHaveBeenCalledTimes(listCallsBefore + (reloadsList ? 1 : 0))+ expect(mockedCaptureRaw).toHaveBeenCalledWith(+ 'mcp analytics weekly report failed',+ expect.objectContaining({ mcp_report_template: 'tool-health', error_message: message })+ )++ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'created', report: SAVED })+ })++ it('keeps the new report when a list request started before the create answers after it', async () => {+ await becomeReadyToCreate()+ let finishList: (results: SubscriptionApi[]) => void = () => {}+ mockedList.mockReturnValueOnce(+ new Promise(+ (resolve) => (finishList = (results) => resolve({ next: null, previous: null, results } as any))+ )+ )+ mockedCreate.mockResolvedValue(SAVED)++ logic.actions.loadReports()+ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toDispatchActions([+ 'createWeeklyReportSucceeded',+ ])+ finishList([])+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.weeklyReportOffer).toEqual({ state: 'created', report: SAVED })+ })++ it('holds the retry until its own reload answers, then resolves to has-report when the request landed despite the failure', async () => {+ await becomeReadyToCreate()+ const heldList = (): { promise: Promise<any>; finish: (results: SubscriptionApi[]) => void } => {+ let finish: (results: SubscriptionApi[]) => void = () => {}+ const promise = new Promise(+ (resolve) => (finish = (results) => resolve({ next: null, previous: null, results }))+ )+ return { promise, finish }+ }+ const olderList = heldList()+ const reconcileList = heldList()+ mockedList.mockReturnValueOnce(olderList.promise).mockReturnValueOnce(reconcileList.promise)+ mockedCreate.mockRejectedValueOnce(new TypeError('Failed to fetch'))++ logic.actions.loadReports()+ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toDispatchActions([+ 'createWeeklyReportFailed',+ ])+ logic.actions.createWeeklyReport()+ olderList.finish([])+ await expectLogic(logic).toDispatchActions(['loadReportsSuccess'])+ expect(mockedCreate).toHaveBeenCalledTimes(1)+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'creating', email: '[email protected]' })++ reconcileList.finish([SAVED])+ await expectLogic(logic).toFinishAllListeners()+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'hidden', reason: 'has-report' })+ })++ it('creates the report once the organization approves AI processing', async () => {+ useMocks({+ patch: {+ '/api/organizations/:id': async ({ request }) => [+ 200,+ { ...MOCK_DEFAULT_ORGANIZATION, ...((await request.json()) as object) },+ ],+ },+ })+ await becomeReadyToCreate()+ organizationLogic.actions.loadCurrentOrganizationSuccess({+ ...organizationLogic.values.currentOrganization,+ is_ai_data_processing_approved: false,+ } as OrganizationType)+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'needs-ai-consent', email: '[email protected]' })+ mockedCreate.mockResolvedValue(SAVED)+ const consent = aiConsentLogic.build()+ consent.mount()++ // The order the consent popover runs on approve: the approval resolves, then the report is created.+ await consent.asyncActions.acceptDataProcessing()+ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()+ consent.unmount()++ expect(mockedCreate).toHaveBeenCalledTimes(1)+ expect(logic.values.weeklyReportOffer).toEqual({ state: 'created', report: SAVED })+ })++ it.each([+ [+ 'the project already has an MCP report',+ async () => {+ listReturns([makeReport(1)])+ await expectLogic(logic, () => logic.actions.loadReports()).toFinishAllListeners()+ },+ 'hidden',+ ],+ [+ 'the organization has not approved AI processing',+ () =>+ organizationLogic.actions.loadCurrentOrganizationSuccess({+ ...organizationLogic.values.currentOrganization,+ is_ai_data_processing_approved: false,+ } as OrganizationType),+ 'needs-ai-consent',+ ],+ [+ 'the instance is self-hosted',+ () => preflightLogic.actions.loadPreflightSuccess({ cloud: false, is_debug: false } as any),+ 'hidden',+ ],+ ['the feature flag is off', () => featureFlagLogic.actions.setFeatureFlags([], {}), 'hidden'],+ ])('does not create when %s', async (_name, block, expectedState) => {+ await becomeReadyToCreate()+ await block()++ await expectLogic(logic, () => logic.actions.createWeeklyReport()).toFinishAllListeners()++ expect(mockedCreate).not.toHaveBeenCalled()+ expect(logic.values.weeklyReportOffer.state).toBe(expectedState)+ })+ })})diff --git a/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.ts b/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.tsindex 947cb5b8b7f9..ed63f2be668d 100644--- a/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.ts+++ b/products/mcp_analytics/frontend/notifications/mcpRecurringReportsLogic.ts@@ -2,22 +2,32 @@ import { actions, connect, kea, listeners, MakeLogicType, path, reducers, selectimport { loaders } from 'kea-loaders'import api from 'lib/api'+import { ApiError } from 'lib/api-error'+import { FEATURE_FLAGS } from 'lib/constants'+import { dayjs } from 'lib/dayjs'import { integrationsLogic } from 'lib/integrations/integrationsLogic'+import { type FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'+import { preflightLogic } from 'lib/logic/preflightLogic'+import posthog from 'lib/posthog-typed'import { deleteWithUndo } from 'lib/utils/deleteWithUndo'import { getCurrentTeamId } from 'lib/utils/getAppContext'+import { appLogic } from 'scenes/appLogic'+import { organizationLogic } from 'scenes/organizationLogic'import { userLogic } from 'scenes/userLogic'-import { IntegrationType, UserType } from '~/types'+import { IntegrationType, OrganizationType, UserType } from '~/types'import { toggleSubscriptionEnabled } from 'products/subscriptions/frontend/components/Subscriptions/toggleSubscriptionEnabled'-import { subscriptionsList } from 'products/subscriptions/frontend/generated/api'+import { subscriptionsCreate, subscriptionsList } from 'products/subscriptions/frontend/generated/api'import {SubscriptionApi,SubscriptionsListResourceType,SubscriptionTargetEnumApi,} from 'products/subscriptions/frontend/generated/api.schemas'-import { MCP_RECURRING_REPORTS } from './recurringReportDefinitions'+import { MCP_RECURRING_REPORTS, TOOL_HEALTH_REPORT, weeklyReportPayload } from './recurringReportDefinitions'+import { createErrorMessage, createMayHaveSucceeded, resolveWeeklyReportOffer } from './weeklyReportOffer'+import type { WeeklyReportOffer, WeeklyReportSubmission } from './weeklyReportOffer'// A ceiling on what one tab render will pull. The server-side `search` below already narrows to MCP// reports, so reaching this means a project with hundreds of them — at which point the list is the@@ -43,7 +53,12 @@ export function isMCPRecurringReport(subscription: Pick<SubscriptionApi, 'prompt// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface mcpRecurringReportsLogicValues {+ featureFlagsTimedOut: boolean // appLogic+ featureFlags: FeatureFlagsSet // featureFlagLogic+ receivedFeatureFlags: boolean // featureFlagLogicslackIntegrations: IntegrationType[] | undefined // integrationsLogic+ currentOrganization: OrganizationType | null // organizationLogic+ isCloudOrDev: boolean | undefined // preflightLogicuser: UserType | null // userLogicdefaultTargetType: SubscriptionTargetEnumApi | nullotherReports: SubscriptionApi[]@@ -54,10 +69,31 @@ export interface mcpRecurringReportsLogicValues {reportsLoaded: booleanreportsLoading: booleanreportsTruncated: boolean+ weeklyReportOffer: WeeklyReportOffer+ weeklyReportSubmission: WeeklyReportSubmission}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface mcpRecurringReportsLogicActions {+ createWeeklyReport: () => {+ value: true+ }+ createWeeklyReportFailed: (+ message: string,+ reconciling: boolean+ ) => {+ message: string+ reconciling: boolean+ }+ createWeeklyReportReconciled: () => {+ value: true+ }+ createWeeklyReportStarted: () => {+ value: true+ }+ createWeeklyReportSucceeded: (report: SubscriptionApi) => {+ report: SubscriptionApi+ }deleteReport: (report: SubscriptionApi) => {report: SubscriptionApi}@@ -108,6 +144,19 @@ export interface mcpRecurringReportsLogicMeta {user: UserType | null,slackIntegrations: IntegrationType[] | undefined) => SubscriptionTargetEnumApi | null+ weeklyReportOffer: (+ isCloudOrDev: boolean | undefined,+ featureFlags: FeatureFlagsSet,+ receivedFeatureFlags: boolean,+ featureFlagsTimedOut: boolean,+ currentOrganization: OrganizationType | null,+ user: UserType | null,+ reports: SubscriptionApi[],+ reportsLoaded: boolean,+ reportsFailed: boolean,+ reportsTruncated: boolean,+ weeklyReportSubmission: WeeklyReportSubmission+ ) => WeeklyReportOffer}}@@ -123,7 +172,20 @@ const TEMPLATE_TITLES = new Set(MCP_RECURRING_REPORTS.map((report) => report.titexport const mcpRecurringReportsLogic = kea<mcpRecurringReportsLogicType>([path(['products', 'mcp_analytics', 'frontend', 'notifications', 'mcpRecurringReportsLogic']),connect(() => ({- values: [integrationsLogic, ['slackIntegrations'], userLogic, ['user']],+ values: [+ integrationsLogic,+ ['slackIntegrations'],+ userLogic,+ ['user'],+ preflightLogic,+ ['isCloudOrDev'],+ organizationLogic,+ ['currentOrganization'],+ featureFlagLogic,+ ['featureFlags', 'receivedFeatureFlags'],+ appLogic,+ ['featureFlagsTimedOut'],+ ],})),actions({toggleReportEnabled: (reportId: number, enabled: boolean) => ({ reportId, enabled }),@@ -134,8 +196,13 @@ export const mcpRecurringReportsLogic = kea<mcpRecurringReportsLogicType>([setReports: (reports: SubscriptionApi[]) => ({ reports }),setReportsTruncated: (reportsTruncated: boolean) => ({ reportsTruncated }),deleteReport: (report: SubscriptionApi) => ({ report }),+ createWeeklyReport: true,+ createWeeklyReportStarted: true,+ createWeeklyReportSucceeded: (report: SubscriptionApi) => ({ report }),+ createWeeklyReportFailed: (message: string, reconciling: boolean) => ({ message, reconciling }),+ createWeeklyReportReconciled: true,}),- loaders(({ actions }) => ({+ loaders(({ actions, values, cache }) => ({reports: [[] as SubscriptionApi[],{@@ -144,11 +211,16 @@ export const mcpRecurringReportsLogic = kea<mcpRecurringReportsLogicType>([// already the MCP ones rather than every AI report the project has. It also spans// title and insight/dashboard names, so `isMCPRecurringReport` still runs as the// precise test — a report merely *titled* "$mcp_" is not one.+ const localWritesAtStart = cache.localReportWrites ?? 0const response = await subscriptionsList(String(getCurrentTeamId()), {resource_type: SubscriptionsListResourceType.AiPrompt,search: MCP_PROPERTY_PREFIX,limit: AI_REPORT_LIST_LIMIT,})+ // A create, delete or undo that landed while this request was out is newer than its answer.+ if ((cache.localReportWrites ?? 0) !== localWritesAtStart) {+ return values.reports+ }actions.setReportsTruncated(!!response.next)return response.results.filter((subscription) => !subscription.deleted && isMCPRecurringReport(subscription)@@ -186,6 +258,19 @@ export const mcpRecurringReportsLogic = kea<mcpRecurringReportsLogicType>([setReportsTruncated: (_, { reportsTruncated }) => reportsTruncated,},],+ weeklyReportSubmission: [+ { status: 'idle' } as WeeklyReportSubmission,+ {+ createWeeklyReportStarted: (): WeeklyReportSubmission => ({ status: 'creating' }),+ createWeeklyReportSucceeded: (_, { report }): WeeklyReportSubmission => ({ status: 'created', report }),+ createWeeklyReportFailed: (_, { message, reconciling }): WeeklyReportSubmission =>+ reconciling ? { status: 'reconciling', message } : { status: 'failed', message },+ createWeeklyReportReconciled: (submission): WeeklyReportSubmission =>+ submission.status === 'reconciling'+ ? { status: 'failed', message: submission.message }+ : submission,+ },+ ],// The list loads when the tab mounts; without this the first render would flash the empty state.reportsLoaded: [false,@@ -235,8 +320,101 @@ export const mcpRecurringReportsLogic = kea<mcpRecurringReportsLogicType>([return slackIntegrations.length ? SubscriptionTargetEnumApi.Slack : SubscriptionTargetEnumApi.Email},],+ weeklyReportOffer: [+ (s) => [+ s.isCloudOrDev,+ s.featureFlags,+ s.receivedFeatureFlags,+ s.featureFlagsTimedOut,+ s.currentOrganization,+ s.user,+ s.reports,+ s.reportsLoaded,+ s.reportsFailed,+ s.reportsTruncated,+ s.weeklyReportSubmission,+ ],+ (+ isCloudOrDev: boolean | undefined,+ featureFlags: FeatureFlagsSet,+ receivedFeatureFlags: boolean,+ featureFlagsTimedOut: boolean,+ currentOrganization: OrganizationType | null,+ user: UserType | null,+ reports: SubscriptionApi[],+ reportsLoaded: boolean,+ reportsFailed: boolean,+ reportsTruncated: boolean,+ weeklyReportSubmission: WeeklyReportSubmission+ ): WeeklyReportOffer =>+ resolveWeeklyReportOffer({+ isCloudOrDev,+ // The app stops waiting for flags after a timeout, and so does the offer: it hides rather than loads forever.+ aiFlagEnabled: receivedFeatureFlags+ ? !!featureFlags[FEATURE_FLAGS.SUBSCRIPTION_AI_PROMPT]+ : featureFlagsTimedOut+ ? false+ : null,+ aiConsentApproved: currentOrganization+ ? !!currentOrganization.is_ai_data_processing_approved+ : null,+ userEmail: user?.email ?? null,+ reports: !reportsLoaded ? 'loading' : reportsFailed || reportsTruncated ? 'unknown' : reports,+ submission: weeklyReportSubmission,+ }),+ ],}),- listeners(({ actions, values }) => ({+ listeners(({ actions, asyncActions, values, cache }) => ({+ setReports: () => {+ cache.localReportWrites = (cache.localReportWrites ?? 0) + 1+ },+ createWeeklyReport: async () => {+ const offer = values.weeklyReportOffer+ // The same union drives the UI, so a double click and every closed gate end here.+ if (offer.state !== 'ready' && offer.state !== 'failed') {+ return+ }+ // Dispatched before the await, so a second click sees `creating`.+ actions.createWeeklyReportStarted()++ let report: SubscriptionApi+ try {+ report = await subscriptionsCreate(+ String(getCurrentTeamId()),+ weeklyReportPayload(TOOL_HEALTH_REPORT, offer.email, dayjs())+ )+ } catch (error) {+ const message = createErrorMessage(error)+ // A request that failed in transit may still have saved the report. Reloading turns+ // that case into `has-report` instead of offering a retry that would duplicate it.+ const reconciling = createMayHaveSucceeded(error)+ actions.createWeeklyReportFailed(message, reconciling)+ posthog.captureRaw('mcp analytics weekly report failed', {+ mcp_report_template: TOOL_HEALTH_REPORT.key,+ error_status: error instanceof ApiError ? (error.status ?? null) : null,+ error_message: message,+ })+ if (reconciling) {+ // Awaits this reload only. A list request already in flight answers from before the create.+ try {+ await asyncActions.loadReports()+ } catch {+ // A failed reload leaves duplicates unknown, so the offer hides instead of retrying.+ }+ actions.createWeeklyReportReconciled()+ }+ return+ }+ actions.setReports([report, ...values.reports])+ actions.createWeeklyReportSucceeded(report)+ posthog.captureRaw('subscription created', {+ creation_source: 'mcp_analytics_one_click',+ resource_type: 'ai',+ subscription_id: report.id,+ target_type: report.target_type,+ mcp_report_template: TOOL_HEALTH_REPORT.key,+ })+ },toggleReportEnabled: async ({ reportId, enabled }) => {if (values.pendingToggleIds[reportId]) {returndiff --git a/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.test.ts b/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.test.tsnew file mode 100644index 000000000000..38f8b29007ca--- /dev/null+++ b/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.test.ts@@ -0,0 +1,40 @@+import { dayjs } from 'lib/dayjs'++import { TOOL_HEALTH_REPORT, weeklyReportPayload } from './recurringReportDefinitions'++describe('recurringReportDefinitions', () => {+ // The server evaluates byweekday in UTC against start_date, so the weekday must come from the UTC+ // instant of the first delivery. The +13:00 row is the one where the local and UTC weekdays differ.+ it.each([+ ['2026-10-12T08:00:00+00:00', 0, '2026-10-13T09:00:00.000Z', 'tuesday'],+ ['2026-10-18T23:30:00+00:00', 0, '2026-10-19T09:00:00.000Z', 'monday'],+ ['2026-10-31T10:00:00+00:00', 0, '2026-11-01T09:00:00.000Z', 'sunday'],+ ['2026-10-12T20:00:00-08:00', -8, '2026-10-13T17:00:00.000Z', 'tuesday'],+ ['2026-10-12T20:00:00+13:00', 13, '2026-10-12T20:00:00.000Z', 'monday'],+ ])('schedules %s (offset %s) for %s on %s', (now, offsetHours, startDate, weekday) => {+ const payload = weeklyReportPayload(+ TOOL_HEALTH_REPORT,+ '[email protected]',+ dayjs(now).utcOffset(offsetHours * 60)+ )++ expect(payload).toMatchObject({ start_date: startDate, byweekday: [weekday] })+ })++ it('emails the template weekly with no immediate send', () => {+ const payload = weeklyReportPayload(TOOL_HEALTH_REPORT, '[email protected]', dayjs('2026-10-12T08:00:00Z'))++ expect(payload).toEqual({+ title: 'MCP tool health',+ prompt: TOOL_HEALTH_REPORT.prompt,+ ai_prompt_config: { window: { mode: 'since_last_sent' } },+ target_type: 'email',+ target_value: '[email protected]',+ frequency: 'weekly',+ interval: 1,+ byweekday: ['tuesday'],+ start_date: '2026-10-13T09:00:00.000Z',+ send_test_now: false,+ })+ })+})diff --git a/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.ts b/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.tsindex c6876dcec282..e8938f123738 100644--- a/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.ts+++ b/products/mcp_analytics/frontend/notifications/recurringReportDefinitions.ts@@ -1,8 +1,15 @@import { combineUrl } from 'kea-router'+import type { Dayjs } from 'lib/dayjs'import { urls } from 'scenes/urls'-import { SubscriptionTargetEnumApi } from 'products/subscriptions/frontend/generated/api.schemas'+import type { subscriptionsCreate } from 'products/subscriptions/frontend/generated/api'+import {+ RecurrenceIntervalEnumApi,+ SubscriptionAIWindowModeEnumApi,+ SubscriptionTargetEnumApi,+ SubscriptionWriteApiByweekdayItem,+} from 'products/subscriptions/frontend/generated/api.schemas'export interface MCPRecurringReport {key: string@@ -19,6 +26,33 @@ export interface MCPRecurringReport {covers: string[]}+export const TOOL_HEALTH_REPORT: MCPRecurringReport = {+ key: 'tool-health',+ headline: 'How your tools held up',+ lead: 'Volume, error rate and latency per tool, with the failures worth acting on.',+ // Weekly, not daily: a typical MCP server sees a few dozen calls a day, so a daily error+ // rate or p95 is noise. A week gives those numbers enough data to mean something.+ frequency: 'weekly',+ title: 'MCP tool health',+ prompt: [+ 'Report on our MCP server’s health for the last week using $mcp_tool_call events.',+ 'Cover total calls and calls per tool. Use $mcp_exec_tool_call_name when non-empty.',+ 'Otherwise, for $mcp_tool_name = exec and $mcp_exec_verb = call, use $mcp_exec_target_tool',+ 'when it is non-empty and not unrecognized. Fall back to $mcp_tool_name in all other cases.',+ 'Include discovery requests under exec, not the tool they describe. Cover',+ 'the error rate from $mcp_is_error, the most common $mcp_error_type values with example',+ '$mcp_error_message text, and p95 of $mcp_duration_ms per tool.',+ 'Highlight tools whose error rate or latency is clearly worse than the weeks before,',+ 'and skip sections where nothing notable happened.',+ ].join(' '),+ covers: [+ 'Total calls and calls per tool',+ 'Error rate, the most common error types and example messages',+ 'p95 latency per tool',+ 'Tools whose errors or latency got clearly worse than the weeks before',+ ],+}+export const MCP_RECURRING_REPORTS: MCPRecurringReport[] = [{key: 'intent-roundup',@@ -45,32 +79,7 @@ export const MCP_RECURRING_REPORTS: MCPRecurringReport[] = ['One change to your tools that would help the most agents',],},- {- key: 'tool-health',- headline: 'How your tools held up',- lead: 'Volume, error rate and latency per tool, with the failures worth acting on.',- // Weekly, not daily: a typical MCP server sees a few dozen calls a day, so a daily error- // rate or p95 is noise. A week gives those numbers enough data to mean something.- frequency: 'weekly',- title: 'MCP tool health',- prompt: [- 'Report on our MCP server’s health for the last week using $mcp_tool_call events.',- 'Cover total calls and calls per tool. Use $mcp_exec_tool_call_name when non-empty.',- 'Otherwise, for $mcp_tool_name = exec and $mcp_exec_verb = call, use $mcp_exec_target_tool',- 'when it is non-empty and not unrecognized. Fall back to $mcp_tool_name in all other cases.',- 'Include discovery requests under exec, not the tool they describe. Cover',- 'the error rate from $mcp_is_error, the most common $mcp_error_type values with example',- '$mcp_error_message text, and p95 of $mcp_duration_ms per tool.',- 'Highlight tools whose error rate or latency is clearly worse than the weeks before,',- 'and skip sections where nothing notable happened.',- ].join(' '),- covers: [- 'Total calls and calls per tool',- 'Error rate, the most common error types and example messages',- 'p95 latency per tool',- 'Tools whose errors or latency got clearly worse than the weeks before',- ],- },+ TOOL_HEALTH_REPORT,]/**@@ -89,3 +98,29 @@ export function urlForRecurringReport(target_type: targetType ?? undefined,}).url}++type SubscriptionCreateBody = Parameters<typeof subscriptionsCreate>[1]++/**+ * The report emailed to `email`, first on the next day at 09:00 in the caller's time zone and then+ * every week. There is no immediate send: a report over a few minutes of data is thin, and the point is+ * a reason to come back on a later day.+ *+ * The server evaluates `byweekday` in UTC against `start_date`, so the weekday is the UTC weekday of the+ * first delivery. The local weekday would push the first delivery a day late wherever the two differ.+ */+export function weeklyReportPayload(report: MCPRecurringReport, email: string, now: Dayjs): SubscriptionCreateBody {+ const firstDelivery = now.add(1, 'day').hour(9).minute(0).second(0).millisecond(0)+ return {+ title: report.title,+ prompt: report.prompt,+ ai_prompt_config: { window: { mode: SubscriptionAIWindowModeEnumApi.SinceLastSent } },+ target_type: SubscriptionTargetEnumApi.Email,+ target_value: email,+ frequency: RecurrenceIntervalEnumApi.Weekly,+ interval: 1,+ byweekday: [firstDelivery.utc().format('dddd').toLowerCase() as SubscriptionWriteApiByweekdayItem],+ start_date: firstDelivery.toISOString(),+ send_test_now: false,+ }+}diff --git a/products/mcp_analytics/frontend/notifications/weeklyReportOffer.test.ts b/products/mcp_analytics/frontend/notifications/weeklyReportOffer.test.tsnew file mode 100644index 000000000000..984db7dfa7cc--- /dev/null+++ b/products/mcp_analytics/frontend/notifications/weeklyReportOffer.test.ts@@ -0,0 +1,146 @@+import { ApiError } from 'lib/api-error'++import type { SubscriptionApi } from 'products/subscriptions/frontend/generated/api.schemas'++import {+ createErrorMessage,+ createMayHaveSucceeded,+ resolveWeeklyReportOffer,+ WeeklyReportOfferInputs,+} from './weeklyReportOffer'++const SAVED = { id: 7, enabled: true } as SubscriptionApi+const DISABLED = { id: 8, enabled: false } as SubscriptionApi++const ELIGIBLE: WeeklyReportOfferInputs = {+ isCloudOrDev: true,+ aiFlagEnabled: true,+ aiConsentApproved: true,+ userEmail: '[email protected]',+ reports: [],+ submission: { status: 'idle' },+}++describe('weeklyReportOffer', () => {+ it.each<[string, Partial<WeeklyReportOfferInputs>, unknown]>([+ ['is ready when nothing blocks it', {}, { state: 'ready', email: '[email protected]' }],+ ['loads until preflight answers', { isCloudOrDev: undefined }, { state: 'loading' }],+ ['loads until the flags arrive', { aiFlagEnabled: null }, { state: 'loading' }],+ ['loads until the organization arrives', { aiConsentApproved: null }, { state: 'loading' }],+ ['loads until the user arrives', { userEmail: null }, { state: 'loading' }],+ ['loads until the reports arrive', { reports: 'loading' }, { state: 'loading' }],+ ['loads before it hides', { isCloudOrDev: false, reports: 'loading' }, { state: 'loading' }],+ ['hides when self-hosted', { isCloudOrDev: false }, { state: 'hidden', reason: 'self-hosted' }],+ ['hides when the flag is off', { aiFlagEnabled: false }, { state: 'hidden', reason: 'not-enabled' }],+ [+ 'hides on the flag before it asks for consent',+ { aiFlagEnabled: false, aiConsentApproved: false },+ { state: 'hidden', reason: 'not-enabled' },+ ],+ [+ 'hides when the project already has an MCP report',+ { reports: [SAVED] },+ { state: 'hidden', reason: 'has-report' },+ ],+ [+ 'hides when the report list is unknown',+ { reports: 'unknown' },+ { state: 'hidden', reason: 'unknown-reports' },+ ],+ [+ 'hides for an existing report before it asks for consent',+ { reports: [SAVED], aiConsentApproved: false },+ { state: 'hidden', reason: 'has-report' },+ ],+ ['asks for consent', { aiConsentApproved: false }, { state: 'needs-ai-consent', email: '[email protected]' }],+ [+ 'carries the server message when creating failed',+ { submission: { status: 'failed', message: 'Your plan is limited to 5 subscriptions.' } },+ { state: 'failed', email: '[email protected]', message: 'Your plan is limited to 5 subscriptions.' },+ ],+ [+ 'asks for consent again when creating failed and consent is gone',+ { aiConsentApproved: false, submission: { status: 'failed', message: 'nope' } },+ { state: 'needs-ai-consent', email: '[email protected]' },+ ],+ ['is creating', { submission: { status: 'creating' } }, { state: 'creating', email: '[email protected]' }],+ [+ 'keeps creating while the report list reloads',+ { reports: 'loading', submission: { status: 'creating' } },+ { state: 'creating', email: '[email protected]' },+ ],+ [+ 'ignores a disabled report, which delivers nothing',+ { reports: [DISABLED] },+ { state: 'ready', email: '[email protected]' },+ ],+ [+ 'hides when an enabled report sits beside a disabled one',+ { reports: [DISABLED, SAVED] },+ { state: 'hidden', reason: 'has-report' },+ ],+ [+ 'is created while the new report is in the list',+ { reports: [SAVED], submission: { status: 'created', report: SAVED } },+ { state: 'created', report: SAVED },+ ],+ [+ 'is created while an unrelated enabled report sits beside the new one',+ {+ reports: [{ id: 9, enabled: true } as SubscriptionApi, SAVED],+ submission: { status: 'created', report: SAVED },+ },+ { state: 'created', report: SAVED },+ ],+ [+ 'offers again after the created report is deleted',+ { reports: [], submission: { status: 'created', report: SAVED } },+ { state: 'ready', email: '[email protected]' },+ ],+ [+ 'offers again after the created report is disabled',+ { reports: [{ ...SAVED, enabled: false }], submission: { status: 'created', report: SAVED } },+ { state: 'ready', email: '[email protected]' },+ ],+ [+ 'hides after the created report is deleted and another one exists',+ {+ reports: [{ id: 9, enabled: true } as SubscriptionApi],+ submission: { status: 'created', report: SAVED },+ },+ { state: 'hidden', reason: 'has-report' },+ ],+ ])('%s', (_name, overrides, expected) => {+ expect(resolveWeeklyReportOffer({ ...ELIGIBLE, ...overrides })).toEqual(expected)+ })++ it.each([+ [+ 'the server detail',+ new ApiError('Bad Request', 400, undefined, { detail: 'Plan limit reached.' }),+ 'Plan limit reached.',+ ],+ [+ 'a server error without detail',+ new ApiError('Server Error', 500),+ "Couldn't create the report. Try again, or set it up from the Notifications tab.",+ ],+ [+ 'a network failure',+ new TypeError('Failed to fetch'),+ "Couldn't create the report. Try again, or set it up from the Notifications tab.",+ ],+ ])('explains a rejection with %s', (_name, error, expected) => {+ expect(createErrorMessage(error)).toBe(expected)+ })++ it.each([+ ['the server refuses the request', new ApiError('Bad Request', 400), false],+ ['the plan limit is hit', new ApiError('Forbidden', 403), false],+ ['the server fails', new ApiError('Server Error', 500), true],+ ['a gateway times out', new ApiError('Gateway Timeout', 504), true],+ ['the network drops', new TypeError('Failed to fetch'), true],+ ])('knows whether a report may have been saved when %s', (_name, error, expected) => {+ expect(createMayHaveSucceeded(error)).toBe(expected)+ })+})diff --git a/products/mcp_analytics/frontend/notifications/weeklyReportOffer.ts b/products/mcp_analytics/frontend/notifications/weeklyReportOffer.tsnew file mode 100644index 000000000000..b222be37dfbb--- /dev/null+++ b/products/mcp_analytics/frontend/notifications/weeklyReportOffer.ts@@ -0,0 +1,112 @@+import { ApiError } from 'lib/api-error'++import type { SubscriptionApi } from 'products/subscriptions/frontend/generated/api.schemas'++export type WeeklyReportHiddenReason =+ | 'self-hosted'+ | 'not-enabled'+ | 'has-report'+ // The list failed or was cut short, so a duplicate cannot be ruled out.+ | 'unknown-reports'++export type WeeklyReportOffer =+ | { state: 'loading' }+ | { state: 'hidden'; reason: WeeklyReportHiddenReason }+ | { state: 'needs-ai-consent'; email: string }+ | { state: 'ready'; email: string }+ | { state: 'creating'; email: string }+ | { state: 'failed'; email: string; message: string }+ | { state: 'created'; report: SubscriptionApi }++export type ResolvedWeeklyReportOffer = Exclude<WeeklyReportOffer, { state: 'loading' }>+export type ActionableWeeklyReportOffer = Exclude<ResolvedWeeklyReportOffer, { state: 'hidden' }>++export type WeeklyReportSubmission =+ | { status: 'idle' }+ | { status: 'creating' }+ // The create may have saved the report despite the error, so the reload decides whether a retry is safe.+ | { status: 'reconciling'; message: string }+ | { status: 'failed'; message: string }+ | { status: 'created'; report: SubscriptionApi }++/** Each input is `undefined` or `null` until it loads. */+export interface WeeklyReportOfferInputs {+ isCloudOrDev: boolean | undefined+ aiFlagEnabled: boolean | null+ aiConsentApproved: boolean | null+ userEmail: string | null+ reports: SubscriptionApi[] | 'loading' | 'unknown'+ submission: WeeklyReportSubmission+}++/**+ * Precedence, top wins:+ * created > creating (and reconciling) > loading > self-hosted > not-enabled > has-report / unknown-reports+ * > needs-ai-consent > failed > ready.+ *+ * `created` holds only while the new report is still in `reports` and enabled. Once it is deleted or+ * turned off, nothing will arrive, so the offer comes back.+ *+ * `has-report` is project-wide on purpose: one enabled MCP report already gives the organization a+ * reason to return, whoever made it. A disabled report delivers nothing, so it does not count.+ *+ * The flag check comes before consent because approving consent cannot help someone the server still refuses.+ */+export function resolveWeeklyReportOffer(inputs: WeeklyReportOfferInputs): WeeklyReportOffer {+ const { isCloudOrDev, aiFlagEnabled, aiConsentApproved, userEmail, reports, submission } = inputs++ if (submission.status === 'created') {+ const created = Array.isArray(reports)+ ? reports.find((report) => report.id === submission.report.id)+ : undefined+ if (created?.enabled) {+ return { state: 'created', report: created }+ }+ }+ if ((submission.status === 'creating' || submission.status === 'reconciling') && userEmail) {+ return { state: 'creating', email: userEmail }+ }+ if (+ isCloudOrDev === undefined ||+ aiFlagEnabled === null ||+ aiConsentApproved === null ||+ userEmail === null ||+ reports === 'loading'+ ) {+ return { state: 'loading' }+ }+ if (!isCloudOrDev) {+ return { state: 'hidden', reason: 'self-hosted' }+ }+ if (!aiFlagEnabled) {+ return { state: 'hidden', reason: 'not-enabled' }+ }+ if (reports === 'unknown') {+ return { state: 'hidden', reason: 'unknown-reports' }+ }+ if (reports.some((report) => report.enabled)) {+ return { state: 'hidden', reason: 'has-report' }+ }+ if (!aiConsentApproved) {+ return { state: 'needs-ai-consent', email: userEmail }+ }+ if (submission.status === 'failed') {+ return { state: 'failed', email: userEmail, message: submission.message }+ }+ return { state: 'ready', email: userEmail }+}++const CREATE_FAILED_MESSAGE = "Couldn't create the report. Try again, or set it up from the Notifications tab."++export function createErrorMessage(error: unknown): string {+ return (error instanceof ApiError && error.detail) || CREATE_FAILED_MESSAGE+}++/** A 4xx means the server refused the request. Any other failure can leave a saved report behind. */+export function createMayHaveSucceeded(error: unknown): boolean {+ return !(error instanceof ApiError && error.status !== undefined && error.status >= 400 && error.status < 500)+}++export function weeklyReportOfferLabel(offer: WeeklyReportOffer): string {+ return offer.state === 'hidden' ? `hidden:${offer.reason}` : offer.state+}