Skip to content

: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.

fix(cdp): record batch email skips against the broadcast run (#115845)

Daniel Marchukf036f828cadd
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
index 892bdd8a5d9f..4b6bf58dc81b 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -434,33 +434,40 @@ describe('HogFunctionHandler', () => {
// Guards the fix that split suppression from opt-out: previously both branches collapsed to a
// single "opted out" log with no metric, so a customer couldn't tell why a workflow send was
// skipped or measure suppression volume from the app-metrics view.
- it('should skip execution, log a suppression message, and emit email_suppressed when recipient preferences returns suppressed', async () => {
- ;(mockRecipientPreferencesService.shouldSkipAction as jest.Mock).mockResolvedValueOnce('suppressed')
+ it.each([
+ ['a workflow run', undefined],
+ ['a broadcast run', 'broadcast-run-id'],
+ ])(
+ 'should skip execution, log a suppression message, and emit email_suppressed when recipient preferences returns suppressed for %s',
+ async (_label, parentRunId) => {
+ ;(mockRecipientPreferencesService.shouldSkipAction as jest.Mock).mockResolvedValueOnce('suppressed')
+ invocation.parentRunId = parentRunId
- const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {
- queue: 'hog',
- queuePriority: 0,
- })
+ const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {
+ queue: 'hog',
+ queuePriority: 0,
+ })
- const handlerResult = await hogFunctionHandler.execute({ invocation, action, result: invocationResult })
+ const handlerResult = await hogFunctionHandler.execute({ invocation, action, result: invocationResult })
- expect(handlerResult.nextAction?.id).toBe('exit')
- expect(invocationResult.logs).toHaveLength(1)
- expect(invocationResult.logs[0].message).toContain(
- `[Action:function] Skipping send: recipient is on the suppression list.`
- )
- expect(invocationResult.metrics).toEqual([
- {
- team_id: team.id,
- app_source_id: invocation.functionId,
- instance_id: action.id,
- metric_kind: 'email',
- metric_name: 'email_suppressed',
- count: 1,
- },
- ])
- expect(mockFetch).not.toHaveBeenCalled()
- })
+ expect(handlerResult.nextAction?.id).toBe('exit')
+ expect(invocationResult.logs).toHaveLength(1)
+ expect(invocationResult.logs[0].message).toContain(
+ `[Action:function] Skipping send: recipient is on the suppression list.`
+ )
+ expect(invocationResult.metrics).toEqual([
+ {
+ team_id: team.id,
+ app_source_id: parentRunId ?? invocation.functionId,
+ instance_id: action.id,
+ metric_kind: 'email',
+ metric_name: 'email_suppressed',
+ count: 1,
+ },
+ ])
+ expect(mockFetch).not.toHaveBeenCalled()
+ }
+ )
// A broadcast run carries a parentRunId, and its run view reads metrics keyed on that ID.
it.each([
@@ -497,33 +504,40 @@ describe('HogFunctionHandler', () => {
}
)
- it('should skip the send and emit email_bounce_prevented when validation predicts a hard bounce', async () => {
- ;(mockEmailValidationService.getSkipReason as jest.Mock).mockResolvedValueOnce(
- 'Skipping send: the domain "dead.invalid" has no reachable mail servers, so this message would hard bounce.'
- )
+ it.each([
+ ['a workflow run', undefined],
+ ['a broadcast run', 'broadcast-run-id'],
+ ])(
+ 'should skip the send and emit email_bounce_prevented when validation predicts a hard bounce for %s',
+ async (_label, parentRunId) => {
+ ;(mockEmailValidationService.getSkipReason as jest.Mock).mockResolvedValueOnce(
+ 'Skipping send: the domain "dead.invalid" has no reachable mail servers, so this message would hard bounce.'
+ )
+ invocation.parentRunId = parentRunId
- const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {
- queue: 'hog',
- queuePriority: 0,
- })
+ const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {
+ queue: 'hog',
+ queuePriority: 0,
+ })
- const handlerResult = await hogFunctionHandler.execute({ invocation, action, result: invocationResult })
+ const handlerResult = await hogFunctionHandler.execute({ invocation, action, result: invocationResult })
- // Flow continues to the next action (a skip, not a failure branch), and nothing was sent.
- expect(handlerResult.nextAction?.id).toBe('exit')
- expect(mockFetch).not.toHaveBeenCalled()
- expect(invocationResult.logs[0].message).toContain('no reachable mail servers')
- expect(invocationResult.metrics).toEqual([
- {
- team_id: team.id,
- app_source_id: invocation.functionId,
- instance_id: action.id,
- metric_kind: 'email',
- metric_name: 'email_bounce_prevented',
- count: 1,
- },
- ])
- })
+ // Flow continues to the next action (a skip, not a failure branch), and nothing was sent.
+ expect(handlerResult.nextAction?.id).toBe('exit')
+ expect(mockFetch).not.toHaveBeenCalled()
+ expect(invocationResult.logs[0].message).toContain('no reachable mail servers')
+ expect(invocationResult.metrics).toEqual([
+ {
+ team_id: team.id,
+ app_source_id: parentRunId ?? invocation.functionId,
+ instance_id: action.id,
+ metric_kind: 'email',
+ metric_name: 'email_bounce_prevented',
+ count: 1,
+ },
+ ])
+ }
+ )
// The billing kind is the whole point of the per-channel handlers: push bills at its own rate
// (roughly half of email), so a completed invocation must emit exactly one billable_invocation
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
index 434d1c0b6219..c48b7c897b47 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
@@ -377,7 +377,7 @@ export class HogFunctionHandler implements ActionHandler {
? [
{
team_id: hogFunctionInvocation.teamId,
- app_source_id: hogFunctionInvocation.functionId,
+ app_source_id: hogFunctionInvocation.parentRunId ?? hogFunctionInvocation.functionId,
instance_id: action.id,
metric_kind: 'email' as const,
metric_name: 'email_suppressed' as const,
@@ -414,7 +414,7 @@ export class HogFunctionHandler implements ActionHandler {
metrics: [
{
team_id: hogFunctionInvocation.teamId,
- app_source_id: hogFunctionInvocation.functionId,
+ app_source_id: hogFunctionInvocation.parentRunId ?? hogFunctionInvocation.functionId,
instance_id: action.id,
metric_kind: 'email',
metric_name: 'email_bounce_prevented',