: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
fix(mcp): size webhook-only batch audiences against the flat limit (#111156)
Ben Lea3a1bf3f2f4e7
diff --git a/services/mcp/src/tools/workflows/batch.ts b/services/mcp/src/tools/workflows/batch.tsindex f6107a021d24..a4a31e1ec48e 100644--- a/services/mcp/src/tools/workflows/batch.ts+++ b/services/mcp/src/tools/workflows/batch.ts@@ -6,27 +6,45 @@ type BlastRadius = { affected: number; total: number; limit: number; confirm_toktype WorkflowTrigger = { type?: string; filters?: unknown }+type WorkflowAction = { type?: string; config?: { template_id?: string } }+async function fetchWorkflow(context: Context,projectId: string,workflowId: string-): Promise<{ status: string | undefined; trigger: WorkflowTrigger }> {- const workflow = await context.api.request<{ status?: string; trigger?: unknown }>({+): Promise<{ status: string | undefined; trigger: WorkflowTrigger; sendsEmail: boolean }> {+ const workflow = await context.api.request<{ status?: string; trigger?: unknown; actions?: WorkflowAction[] }>({method: 'GET',path: `/api/projects/${encodeURIComponent(String(projectId))}/hog_flows/${encodeURIComponent(String(workflowId))}/`,})- return { status: workflow.status, trigger: (workflow.trigger ?? {}) as WorkflowTrigger }+ return {+ status: workflow.status,+ trigger: (workflow.trigger ?? {}) as WorkflowTrigger,+ sendsEmail: workflowSendsEmail(workflow.actions),+ }+}++// Kept in sync with hog_flow_sends_email on the backend, which applies the email tier cap only to workflows with an email step.+function workflowSendsEmail(actions: WorkflowAction[] | undefined): boolean {+ return (actions ?? []).some(+ (action) => action?.type === 'function_email' || action?.config?.template_id === 'template-email'+ )}function triggerFilters(trigger: WorkflowTrigger): unknown {return trigger.filters ?? { properties: [] }}-async function sizeAudience(context: Context, projectId: string, filters: unknown): Promise<BlastRadius> {+async function sizeAudience(+ context: Context,+ projectId: string,+ filters: unknown,+ sendsEmail: boolean+): Promise<BlastRadius> {return await context.api.request<BlastRadius>({method: 'POST',path: `/api/projects/${encodeURIComponent(String(projectId))}/hog_flows/user_blast_radius/`,- body: { filters },+ body: { filters, sends_email: sendsEmail },})}@@ -60,8 +78,8 @@ export const workflowsBlastRadius = (): ToolBase<typeof BlastRadiusSchema, Blastschema: BlastRadiusSchema,handler: async (context, params) => {const projectId = await context.stateManager.getProjectId()- const { trigger } = await fetchWorkflow(context, projectId, params.workflow_id)- return await sizeAudience(context, projectId, triggerFilters(trigger))+ const { trigger, sendsEmail } = await fetchWorkflow(context, projectId, params.workflow_id)+ return await sizeAudience(context, projectId, triggerFilters(trigger), sendsEmail)},})@@ -93,7 +111,7 @@ export const workflowsRunBatch = (): ToolBase<typeof RunBatchSchema, unknown> =>schema: RunBatchSchema,handler: async (context, params) => {const projectId = await context.stateManager.getProjectId()- const { status, trigger } = await fetchWorkflow(context, projectId, params.workflow_id)+ const { status, trigger, sendsEmail } = await fetchWorkflow(context, projectId, params.workflow_id)if (trigger.type !== 'batch') {throw new Error(@@ -110,7 +128,7 @@ export const workflowsRunBatch = (): ToolBase<typeof RunBatchSchema, unknown> =>}const filters = triggerFilters(trigger)- const { affected, limit } = await sizeAudience(context, projectId, filters)+ const { affected, limit } = await sizeAudience(context, projectId, filters, sendsEmail)assertAcknowledged(affected, params.acknowledged_affected_count, limit)return await context.api.request({@@ -162,7 +180,7 @@ export const workflowsScheduleCreate = (): ToolBase<typeof ScheduleCreateSchema,schema: ScheduleCreateSchema,handler: async (context, params) => {const projectId = await context.stateManager.getProjectId()- const { status, trigger } = await fetchWorkflow(context, projectId, params.workflow_id)+ const { status, trigger, sendsEmail } = await fetchWorkflow(context, projectId, params.workflow_id)if (trigger.type !== 'batch' && trigger.type !== 'schedule') {throw new Error(@@ -191,7 +209,7 @@ export const workflowsScheduleCreate = (): ToolBase<typeof ScheduleCreateSchema,'confirm the count with the user, then pass acknowledged_affected_count and confirm_token.')}- const { affected, limit } = await sizeAudience(context, projectId, triggerFilters(trigger))+ const { affected, limit } = await sizeAudience(context, projectId, triggerFilters(trigger), sendsEmail)assertAcknowledged(affected, params.acknowledged_affected_count, limit)confirmToken = params.confirm_token}diff --git a/services/mcp/tests/unit/workflows-batch-handlers.test.ts b/services/mcp/tests/unit/workflows-batch-handlers.test.tsindex 963eac4267c8..84c3ea3bf905 100644--- a/services/mcp/tests/unit/workflows-batch-handlers.test.ts+++ b/services/mcp/tests/unit/workflows-batch-handlers.test.ts@@ -24,10 +24,11 @@ function createMockContext(opts: {trigger: unknownblastRadius: { affected: number; total: number; limit?: number }status?: string+ actions?: unknown[]}): { context: Context; request: ReturnType<typeof vi.fn> } {const request = vi.fn(async ({ method, path }: RequestArgs) => {if (method === 'GET' && /\/hog_flows\/[^/]+\/$/.test(path)) {- return { id: 'wf-1', status: opts.status ?? 'active', trigger: opts.trigger }+ return { id: 'wf-1', status: opts.status ?? 'active', trigger: opts.trigger, actions: opts.actions ?? [] }}if (method === 'POST' && path.endsWith('/user_blast_radius/')) {return { limit: DEFAULT_LIMIT, confirm_token: 'tok-preview', ...opts.blastRadius }@@ -63,10 +64,15 @@ describe('workflows batch handlers', () => {})describe('workflows-blast-radius', () => {- it('resolves the workflow trigger filters and sizes them, returning the count', async () => {+ it.each([+ ['an email step', [{ type: 'function_email' }], true],+ ['an email template step', [{ type: 'function', config: { template_id: 'template-email' } }], true],+ ['only webhook steps', [{ type: 'function', config: { template_id: 'template-webhook' } }], false],+ ])('sizes the trigger filters for a workflow with %s', async (_, actions, sendsEmail) => {const { context, request } = createMockContext({trigger: { type: 'batch', filters: BATCH_FILTERS },blastRadius: { affected: 42, total: 100 },+ actions,})const result = await blastRadiusTool.handler(context, { workflow_id: 'wf-1' })@@ -76,7 +82,7 @@ describe('workflows batch handlers', () => {expect(c[1]).toMatchObject({method: 'POST',path: '/api/projects/1/hog_flows/user_blast_radius/',- body: { filters: BATCH_FILTERS },+ body: { filters: BATCH_FILTERS, sends_email: sendsEmail },})// confirm_token must surface to the agent: it is a required workflows-run-batch input.expect(result).toEqual({ affected: 42, total: 100, limit: DEFAULT_LIMIT, confirm_token: 'tok-preview' })