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(workflows): do not bill a push send that reached no recipient (#105196)

Daniel Marchuk136719e9090e
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 fd5f7e28b69c..b42a7bef0273 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -481,7 +481,6 @@ describe('HogFunctionHandler', () => {
it.each([
['fetch', 'workflow_billable_invocations'],
['email', 'workflow_emails_sent'],
- ['push', 'workflow_push_sent'],
['sms', 'workflow_sms_sent'],
] as const)(
'emits a single billable_invocation with %s kind matching the handler billing type',
@@ -524,6 +523,49 @@ describe('HogFunctionHandler', () => {
}
)
+ // A push with no device token finishes unskipped, so only the send knows nobody was reached.
+ it.each([
+ ['reached a device', true, true],
+ ['found no device token', false, false],
+ ] as const)('bills a push step only when the send %s', async (_label, delivered, shouldBill) => {
+ const usageReporter: Pick<CdpUsageReporterService, 'reportBillableInvocation'> = {
+ reportBillableInvocation: jest.fn(),
+ }
+ const handler = new HogFunctionHandler(
+ mockHogFlowFunctionsService,
+ mockRecipientPreferencesService,
+ mockEmailValidationService,
+ 'push',
+ usageReporter
+ )
+
+ const executeWithAsyncFunctions =
+ mockHogFlowFunctionsService.executeWithAsyncFunctions.bind(mockHogFlowFunctionsService)
+ jest.spyOn(mockHogFlowFunctionsService, 'executeWithAsyncFunctions').mockImplementation(
+ async (hogFunctionInvocation, options) => {
+ const functionResult = await executeWithAsyncFunctions(hogFunctionInvocation, options)
+ functionResult.deliveredToRecipient = delivered
+ return functionResult
+ }
+ )
+
+ const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {
+ queue: 'hog',
+ queuePriority: 0,
+ })
+
+ await handler.execute({ invocation, action, result: invocationResult })
+
+ expect(invocationResult.metrics.some((metric) => metric.metric_name === 'billable_invocation')).toBe(shouldBill)
+ if (shouldBill) {
+ expect(usageReporter.reportBillableInvocation).toHaveBeenCalledWith(
+ expect.objectContaining({ teamId: team.id, usageKey: 'workflow_push_sent' })
+ )
+ } else {
+ expect(usageReporter.reportBillableInvocation).not.toHaveBeenCalled()
+ }
+ })
+
// Live edits reach runs already in flight, so a run that entered on one version can send its
// message under a newer one. The conversion belongs to the version whose message the person
// received — the same version `email_sent` is counted under — so a send re-pins the attribution
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
index 575f32d8110b..71d81ef05133 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
@@ -187,17 +187,20 @@ export class HogFunctionHandler implements ActionHandler {
// Add billable_invocation metric only if the function actually executed (not skipped)
if (!functionResult.skipped) {
- trackHogFlowBillableInvocation(result, {
- invocation: functionResult.invocation,
- billingMetricType: this.hogFlowActionBillingType,
- })
+ // A step that does not send leaves this undefined and bills as before.
+ if (functionResult.deliveredToRecipient !== false) {
+ trackHogFlowBillableInvocation(result, {
+ invocation: functionResult.invocation,
+ billingMetricType: this.hogFlowActionBillingType,
+ })
- // actionStepCount holds across a retry of this step but changes on a loop revisit.
- this.usageReporter?.reportBillableInvocation({
- teamId: invocation.teamId,
- usageKey: WORKFLOW_USAGE_KEYS[this.hogFlowActionBillingType],
- recordId: `flow:${invocation.id}:${invocation.state.actionStepCount}:${this.hogFlowActionBillingType}`,
- })
+ // actionStepCount holds across a retry of this step but changes on a loop revisit.
+ this.usageReporter?.reportBillableInvocation({
+ teamId: invocation.teamId,
+ usageKey: WORKFLOW_USAGE_KEYS[this.hogFlowActionBillingType],
+ recordId: `flow:${invocation.id}:${invocation.state.actionStepCount}:${this.hogFlowActionBillingType}`,
+ })
+ }
// Re-pin the attribution version to the one that actually sent. Live edits reach runs
// already in flight, so a run that entered on v2 can send its email after v3 is
diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts b/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
index 6d142129ea7e..d5de0346e875 100644
--- a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
@@ -171,6 +171,7 @@ describe('PushNotificationService', () => {
})
)
expect(result.finished).toBe(true)
+ expect(result.deliveredToRecipient).toBe(true)
})
it('logs warning and records push_skipped when no device token found', async () => {
@@ -184,6 +185,8 @@ describe('PushNotificationService', () => {
// No token means nothing was delivered — record push_skipped, not push_sent.
expect(result.metrics).toContainEqual(expect.objectContaining({ metric_name: 'push_skipped', count: 1 }))
expect(result.metrics).not.toContainEqual(expect.objectContaining({ metric_name: 'push_sent' }))
+ // Billing reads this, so a regression here charges for a notification nobody received.
+ expect(result.deliveredToRecipient).toBe(false)
})
it('stamps workflow, action and invocation ids into the data payload so opens can be attributed', async () => {
diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.ts b/nodejs/src/cdp/services/messaging/push-notification.service.ts
index 214bce2afd68..e71bf631411e 100644
--- a/nodejs/src/cdp/services/messaging/push-notification.service.ts
+++ b/nodejs/src/cdp/services/messaging/push-notification.service.ts
@@ -331,6 +331,9 @@ export class PushNotificationService {
pushMetric('push_skipped', skippedCount)
pushMetric('push_failed', errorCount)
+ // Billing reads this rather than the metrics above, which a test send suppresses.
+ result.deliveredToRecipient = successCount > 0
+
// Captured at the terminal outcome for the same reason the business metrics are: a rescheduled
// attempt returns earlier, so a retried notification produces one asset rather than one per try.
// Only a delivered notification is captured, matching email: an asset is a snapshot of what a
diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.ts
index 297e28b0a229..3e9b6e1587a3 100644
--- a/nodejs/src/cdp/types.ts
+++ b/nodejs/src/cdp/types.ts
@@ -327,6 +327,12 @@ export type CyclotronJobInvocationResult<T extends CyclotronJobInvocation = Cycl
finished: boolean
/** The invocation deliberately finished without running because its trigger did not match. */
skipped?: boolean
+ /**
+ * Whether a send reached a recipient. Distinct from `skipped` above, which is set only for a
+ * trigger or recipient-preference skip: a push that found no device token sets neither that
+ * flag nor an error, so nothing else on the result tells the two apart.
+ */
+ deliveredToRecipient?: boolean
// The run was canceled rather than succeeding or failing. Only meaningful with
// finished=true and no error: the job row and the lifecycle row both flip to
// 'canceled'.