Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
969011f
feat(cdp): deliver a code send's content on its own transactional ema…
Silthus Oct 29, 2026
38613ba
feat(cdp): key a code send on the sender the caller chose
Silthus Oct 31, 2026
0c2b5bd
feat(cdp): send an accepted send from the workflow version Django graded
Silthus Oct 26, 2026
e274792
fix(cdp): run an accepted send on the workflow version Django graded
Silthus Oct 26, 2026
8559321
fix(cdp): share one primary read per workflow and keep its fresh copy
Silthus Oct 26, 2026
be15c53
fix(cdp): keep the version-behind 503 body internal
Silthus Oct 26, 2026
f9abed0
fix(cdp): mark a version-behind send failed on its email status
Silthus Oct 31, 2026
920e7c9
fix(cdp): read the primary once for concurrent jobs of a missing work…
Silthus Nov 1, 2026
12d3a95
Merge branch 'email-v2/01-cdp-code-send' into email-v2/09-route-idemp…
Silthus Nov 1, 2026
64d8940
fix(cdp): key a sender without an address on its integration alone
Silthus Nov 1, 2026
b9864ed
Merge branch 'email-v2/09-route-idempotency-cdp' into email-v2/10c-te…
Silthus Nov 1, 2026
5d66b71
fix(cdp): count jobs failed for a missing workflow and keep one prima…
Silthus Nov 1, 2026
8f9e76f
fix(cdp): finish a send behind its graded workflow version as a faile…
Silthus Nov 2, 2026
fa9f789
fix(cdp): record the failed run before dropping a send behind its ver…
Silthus Nov 2, 2026
d5861ad
fix(cdp): close a send behind its version through the result pipeline
Silthus Nov 2, 2026
ef92884
fix(cdp): record a send behind its version durably before failing it
Silthus Nov 2, 2026
2b99456
fix(cdp): keep workflow_version_behind on the run's later failed rows
Silthus Nov 2, 2026
4c7375d
Merge remote-tracking branch 'upstream/master' into email-v2/01-cdp-c…
Silthus Nov 2, 2026
2868055
Merge branch 'email-v2/01-cdp-code-send' into email-v2/09-route-idemp…
Silthus Nov 2, 2026
ae90557
Merge branch 'email-v2/09-route-idempotency-cdp' into email-v2/10c-te…
Silthus Nov 2, 2026
f9b0d6f
Merge master into the template version on send layer
Silthus Nov 6, 2026
028eb15
Merge remote-tracking branch 'origin/master' into email-v2/10c-templa…
Silthus Nov 8, 2026
ad849f7
chore(cdp): cover template sends with the attachments switch and copies
Silthus Nov 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 137 additions & 32 deletions nodejs/src/cdp/cdp-api.serial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,92 @@ describe('CDP API', () => {
})
})

describe('with the workflow version Django graded', () => {
const templateSend = { tracking: { captureEngagement: false } }
const UPLOADED_SUBJECT = 'Your new reset link'

const uploadWithoutReload = async (version: number) => {
const uploaded = hogFlow.actions.map((action) =>
action.type === 'function_email'
? {
...action,
config: {
...action.config,
inputs: {
email: {
value: {
from: { integrationId: codeSend.from.integrationId },
subject: UPLOADED_SUBJECT,
},
},
},
},
}
: action
)
await hub.postgres.query(
PostgresUse.COMMON_WRITE,
`UPDATE posthog_hogflow SET version = $2, actions = $3 WHERE id = $1`,
[hogFlow.id, version, JSON.stringify(uploaded)],
'upload-without-reload'
)
}

const queuedFlow = () => {
const [invocation] = mockQueueInvocations.mock.calls[0][0]
const email = invocation.hogFlow.actions.find((action: any) => action.type === 'function_email')
return { version: invocation.hogFlow.version, subject: email.config.inputs?.email?.value?.subject }
}

beforeEach(async () => {
await api['hogFlowManager'].getHogFlow(hogFlow.id)
await uploadWithoutReload(2)
})

it.each([
['newer than the cached copy sends the uploaded content', { hogFlowVersion: 2 }, 2, UPLOADED_SUBJECT],
['the cached copy holds sends the cached content', { hogFlowVersion: 1 }, 1, undefined],
['missing sends the cached content, as before the field', {}, 1, undefined],
])('a graded version %s', async (_desc, graded, version, subject) => {
const res = await sendAccepted(hogFlow.id, { ...body, send: { ...templateSend, ...graded } })

expect(res.status).toEqual(202)
expect(queuedFlow()).toEqual({ version, subject })
})

it('refuses a send whose graded version is newer than the workflow in the database', async () => {
const behindBefore = await outcomeCount('workflow_version_behind')

const res = await sendAccepted(hogFlow.id, { ...body, send: { ...templateSend, hogFlowVersion: 3 } })

expect([res.status, res.body]).toEqual([503, { error: 'workflow_version_behind' }])
expect(await outcomeCount('workflow_version_behind')).toEqual(behindBefore + 1)
expect(mockQueueInvocations).not.toHaveBeenCalled()
})

it('replays a keyed send whose retry Django graded at a newer version', async () => {
const idempotency_key = `key-${new UUIDT().toString()}`
const first = await sendAccepted(hogFlow.id, {
...body,
idempotency_key,
send: { ...templateSend, hogFlowVersion: 1 },
})
const retry = await sendAccepted(hogFlow.id, {
...body,
idempotency_key,
send: { ...templateSend, hogFlowVersion: 2 },
})

expect(first.status).toEqual(202)
expect(retry.body).toEqual({
status: 'queued',
invocation_id: first.body.invocation_id,
replayed: true,
})
expect(mockQueueInvocations).toHaveBeenCalledTimes(1)
})
})

it.each([
['a code send with rendered content', codeSend],
[
Expand All @@ -2556,6 +2642,19 @@ describe('CDP API', () => {
'a code send with copies to addresses that use atext symbols',
{ ...codeSend, cc: ['jane=tag@example.com'], bcc: ['jane~tag@example.com'] },
],
[
'a template send with the workflow version Django graded',
{ tracking: { captureEngagement: false }, hogFlowVersion: 1 },
],
[
'a template send with copies',
{
tracking: { captureEngagement: true },
cc: ['copy@example.com'],
bcc: ['audit@example.com'],
hogFlowVersion: 1,
},
],
])('stamps %s on the queued run', async (_desc, acceptedSend) => {
const res = await sendAccepted(hogFlow.id, { ...body, send: acceptedSend })

Expand Down Expand Up @@ -2740,41 +2839,47 @@ describe('CDP API', () => {
}
})

it('refuses a new code send with files while attachments are off, and still replays one sent before', async () => {
const sentBefore = {
...body,
idempotency_key: `key-${new UUIDT().toString()}`,
send: { ...codeSend, attachments: [attachment()] },
}
const accepted = await sendAccepted(hogFlow.id, sentBefore)
hub.CDP_EMAIL_ATTACHMENTS_ENABLED = false
const filename = `report-${new UUIDT().toString()}.pdf`
const refusedKey = `key-${new UUIDT().toString()}`

try {
const replay = await sendAccepted(hogFlow.id, sentBefore)
const refused = await sendAccepted(hogFlow.id, {
...body,
idempotency_key: refusedKey,
send: { ...codeSend, attachments: [attachment({ filename })] },
})
const withoutFiles = await sendAccepted(hogFlow.id, {
it.each([
['code send', codeSend],
['template send', { tracking: { captureEngagement: true }, hogFlowVersion: 1 }],
])(
'refuses a new %s with files while attachments are off, and still replays one sent before',
async (_desc, accepted) => {
const sentBefore = {
...body,
idempotency_key: refusedKey,
send: codeSend,
})
idempotency_key: `key-${new UUIDT().toString()}`,
send: { ...accepted, attachments: [attachment()] },
}
const first = await sendAccepted(hogFlow.id, sentBefore)
hub.CDP_EMAIL_ATTACHMENTS_ENABLED = false
const filename = `report-${new UUIDT().toString()}.pdf`
const refusedKey = `key-${new UUIDT().toString()}`

expect(replay.body).toMatchObject({ invocation_id: accepted.body.invocation_id, replayed: true })
expect(refused.status).toEqual(400)
expect(parseJSON(refused.body.error)).toEqual([
expect.objectContaining({ code: 'unrecognized_keys', keys: ['attachments'], path: ['send'] }),
])
expect(await storedAttachments(filename)).toEqual([])
expect(withoutFiles.status).toEqual(202)
} finally {
hub.CDP_EMAIL_ATTACHMENTS_ENABLED = true
try {
const replay = await sendAccepted(hogFlow.id, sentBefore)
const refused = await sendAccepted(hogFlow.id, {
...body,
idempotency_key: refusedKey,
send: { ...accepted, attachments: [attachment({ filename })] },
})
const withoutFiles = await sendAccepted(hogFlow.id, {
...body,
idempotency_key: refusedKey,
send: accepted,
})

expect(replay.body).toMatchObject({ invocation_id: first.body.invocation_id, replayed: true })
expect(refused.status).toEqual(400)
expect(parseJSON(refused.body.error)).toEqual([
expect.objectContaining({ code: 'unrecognized_keys', keys: ['attachments'], path: ['send'] }),
])
expect(await storedAttachments(filename)).toEqual([])
expect(withoutFiles.status).toEqual(202)
} finally {
hub.CDP_EMAIL_ATTACHMENTS_ENABLED = true
}
}
})
)

it('keeps the stored files when the queue insert fails, as the job may be queued already', async () => {
mockQueueInvocations.mockRejectedValueOnce(new Error('queue down'))
Expand Down
16 changes: 12 additions & 4 deletions nodejs/src/cdp/cdp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
decodeAttachments,
fieldNotAcceptedError,
isRecipientFormatIssue,
predatesGradedVersion,
queuedSend,
transactionalEmailRecipients,
transactionalEmailRequestsTotal,
Expand Down Expand Up @@ -189,16 +190,17 @@ const transactionalEmailFingerprint = (
): string => {
const templateRequest = [hogFlowId, to, distinct_id, variables]
return IdempotencyKeys.fingerprint(
send ? [...templateRequest, ACCEPTED_SEND_FINGERPRINT_MARKER, withSenderTheCallerChose(send)] : templateRequest
send ? [...templateRequest, ACCEPTED_SEND_FINGERPRINT_MARKER, whatTheCallerSent(send)] : templateRequest
)
}

type CallerChosenSender = Partial<NonNullable<AcceptedSend['from']>>

const withSenderTheCallerChose = ({
const whatTheCallerSent = ({
from,
hogFlowVersion,
...send
}: QueuedSend): Omit<QueuedSend, 'from'> & { from?: CallerChosenSender } => ({
}: QueuedSend): Omit<QueuedSend, 'from' | 'hogFlowVersion'> & { from?: CallerChosenSender } => ({
...send,
from: senderTheCallerChose(from),
})
Expand Down Expand Up @@ -1268,7 +1270,10 @@ export class CdpApi {

stages.start('load')
const teamLookup = this.deps.teamManager.getTeam(parseInt(req.params.team_id)).catch(() => null)
const hogFlowLookup = this.hogFlowManager.getHogFlowCheckingPrimaryOnMiss(req.params.id)
const hogFlowLookup = this.hogFlowManager.getHogFlowCheckingPrimaryOnMiss(
req.params.id,
send?.hogFlowVersion
)
// A missing team returns before the workflow lookup is awaited, so its failure must not go unhandled.
hogFlowLookup.catch(() => undefined)
const team = await teamLookup
Expand All @@ -1279,6 +1284,9 @@ export class CdpApi {
if (!hogFlow || hogFlow.team_id !== team.id) {
return reply('not_found', 404, { error: 'Workflow not found' })
}
if (predatesGradedVersion(hogFlow, send)) {
return reply('workflow_version_behind', 503, { error: 'workflow_version_behind' })
}
if (hogFlow.trigger?.type !== 'api') {
return reply('not_sendable', 400, { error: 'not_sendable' })
}
Expand Down
102 changes: 102 additions & 0 deletions nodejs/src/cdp/consumers/cdp-cyclotron-worker-email.consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createTestTeamFixture } from '~/tests/helpers/sql'
import { Hub } from '../../types'
import { FixtureHogFlowBuilder } from '../_tests/builders/hogflow.builder'
import { createHogFlowInvocationContext, insertHogFlow } from '../_tests/fixtures-hogflows'
import { TRANSACTIONAL_EMAIL_EVENT } from '../services/hogflows/hogflow-sensitivity'
import { CyclotronJobInvocation, CyclotronJobInvocationResult } from '../types'
import { CdpCyclotronWorkerEmail } from './cdp-cyclotron-worker-email.consumer'

Expand Down Expand Up @@ -99,4 +100,105 @@ describe('CdpCyclotronWorkerEmail', () => {
: []
)
})

it.each([
['closes the run as failed', true],
['keeps the job for a retry when the run cannot be recorded', false],
])('a transactional send whose graded workflow version is newer than the database %s', async (_desc, recorded) => {
const { team } = await createTestTeamFixture(hub.postgres)
const hogFlow: HogFlow = await insertHogFlow(
hub.postgres,
new FixtureHogFlowBuilder()
.withTeamId(team.id)
.withStatus('active')
.withWorkflow({
actions: {
trigger: { type: 'trigger', config: { type: 'api' } },
exit: { type: 'exit', config: {} },
},
edges: [{ from: 'trigger', to: 'exit', type: 'continue' }],
} as any)
.build()
)
const jobQueue = createMockJobQueue()
const worker = new CdpCyclotronWorkerEmail(hub, createCdpConsumerDeps(hub), jobQueue)
const job: CyclotronJobInvocation = {
id: new UUIDT().toString(),
teamId: team.id,
functionId: hogFlow.id,
queue: 'email',
queuePriority: 0,
state: createHogFlowInvocationContext({
event: { event: TRANSACTIONAL_EMAIL_EVENT, distinct_id: 'distinct_A_1', properties: {} } as any,
triggerMetricPending: true,
send: { tracking: { captureEngagement: false }, hogFlowVersion: hogFlow.version + 1 },
}),
}
const produce = jest
.spyOn(worker['invocationResultsService'].invocationResultsRowsService['outputs'], 'produce')
.mockImplementation((output) =>
output === 'hog_invocation_results' && !recorded
? Promise.reject(new Error('broker unavailable'))
: Promise.resolve()
)
const produced = (output: string): Record<string, unknown>[] =>
produce.mock.calls
.filter(([name]) => name === output)
.map(([, message]) => parseJSON(message.value!.toString()))
const resultsEnabled = hub.HOG_INVOCATION_RESULTS_ENABLED
hub.HOG_INVOCATION_RESULTS_ENABLED = true

try {
const { invocationResults, backgroundTask } = await worker.processBatch([job])
await backgroundTask

expect(
invocationResults.map((result) => ({
finished: result.finished,
error: result.error,
metrics: result.metrics.map((metric) => metric.metric_name),
}))
).toEqual(
recorded
? [
{
finished: true,
error: `Workflow version ${hogFlow.version} is older than version ${hogFlow.version + 1}, which this send was checked against`,
metrics: ['triggered'],
},
]
: []
)
expect(jobQueue.dequeueInvocations).toHaveBeenLastCalledWith([])
expect(
produced('hog_invocation_results').map((row) => ({
invocation_id: row.invocation_id,
function_kind: row.function_kind,
status: row.status,
error_kind: row.error_kind,
}))
).toEqual(
Array(recorded ? 2 : 1).fill({
invocation_id: job.id,
function_kind: 'hog_flow',
status: 'failed',
error_kind: 'workflow_version_behind',
})
)
expect(produced('email_status')).toEqual(
recorded
? [
expect.objectContaining({
message_id: job.id,
status: 'failed',
reason: 'workflow_version_behind',
}),
]
: []
)
} finally {
hub.HOG_INVOCATION_RESULTS_ENABLED = resultsEnabled
produce.mockRestore()
}
})
})
Loading
Loading