: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(cdp): coerce non-UUID personId to null in cyclotron-v2 schema (#57904)
Daniel Marchuk7022d860f07e
diff --git a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.tsindex c38c53b0b8b6..5ae9489d8614 100644--- a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts+++ b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts@@ -265,21 +265,22 @@ describe('Cyclotron V2', () => {}})- it('createJob rejects non-UUID personId before reaching the database', async () => {- await expect(manager.createJob({ teamId: 1, queueName: QUEUE, personId: 'not-a-uuid' })).rejects.toThrow(- /uuid/i- )+ it('createJob coerces non-UUID personId to null', async () => {+ const id = await manager.createJob({ teamId: 1, queueName: QUEUE, personId: 'not-a-uuid' as any })+ const row = await queryJob(id)+ expect(row.person_id).toBeNull()})- it('bulkCreateJobs rejects non-UUID values without writing any rows', async () => {- const before = await totalJobCount()- await expect(- manager.bulkCreateJobs([- { teamId: 1, queueName: QUEUE, personId: uuidv7() },- { teamId: 1, queueName: QUEUE, personId: 'bad-uuid' as any },- ])- ).rejects.toThrow(/uuid/i)- expect(await totalJobCount()).toBe(before)+ it('bulkCreateJobs coerces non-UUID personIds to null instead of failing the batch', async () => {+ const validPersonId = uuidv7()+ const ids = await manager.bulkCreateJobs([+ { teamId: 1, queueName: QUEUE, personId: validPersonId },+ { teamId: 1, queueName: QUEUE, personId: 'group-key-not-a-uuid' as any },+ ])+ expect(ids).toHaveLength(2)+ const rows = await Promise.all(ids.map(queryJob))+ expect(rows[0].person_id).toBe(validPersonId)+ expect(rows[1].person_id).toBeNull()})})@@ -422,9 +423,12 @@ describe('Cyclotron V2', () => {expect(job.actionId).toBe('a-on-job')})- it('reschedule rejects non-UUID personId before reaching the database', async () => {- const { job } = await seedAndDequeue()- await expect(job.reschedule({ personId: 'bad-uuid' as any })).rejects.toThrow(/uuid/i)+ it('reschedule coerces non-UUID personId to null instead of failing', async () => {+ const { id, job } = await seedAndDequeue({ personId: uuidv7() })+ await job.reschedule({ personId: 'bad-uuid' as any })++ const row = await queryJob(id)+ expect(row.person_id).toBeNull()})it('reschedule({ distinctId }) updates distinct_id column', async () => {diff --git a/nodejs/src/cdp/services/cyclotron-v2/types.test.ts b/nodejs/src/cdp/services/cyclotron-v2/types.test.tsnew file mode 100644index 000000000000..ad7d92ca1062--- /dev/null+++ b/nodejs/src/cdp/services/cyclotron-v2/types.test.ts@@ -0,0 +1,67 @@+import { v7 as uuidv7 } from 'uuid'++import { CyclotronV2JobInitSchema, CyclotronV2RescheduleOptionsSchema } from './types'++describe('CyclotronV2 schema validation', () => {+ describe('personId coercion in CyclotronV2JobInitSchema', () => {+ it('accepts a valid UUID and preserves it', () => {+ const personId = uuidv7()+ const parsed = CyclotronV2JobInitSchema.parse({ teamId: 1, queueName: 'q', personId })+ expect(parsed.personId).toBe(personId)+ })++ it('coerces non-UUID strings to null (e.g. group keys from blast radius)', () => {+ const parsed = CyclotronV2JobInitSchema.parse({+ teamId: 1,+ queueName: 'q',+ personId: 'group-key-not-a-uuid',+ })+ expect(parsed.personId).toBeNull()+ })++ it.each([+ ['empty string', ''],+ ['arbitrary text', 'not-a-uuid'],+ ['nearly-uuid', '12345678-1234-1234-1234-12345678901'],+ ['numeric id', '12345'],+ ['email-like', '[email protected]'],+ ])('coerces %s to null', (_, value) => {+ const parsed = CyclotronV2JobInitSchema.parse({ teamId: 1, queueName: 'q', personId: value })+ expect(parsed.personId).toBeNull()+ })++ it('passes through null', () => {+ const parsed = CyclotronV2JobInitSchema.parse({ teamId: 1, queueName: 'q', personId: null })+ expect(parsed.personId).toBeNull()+ })++ it('passes through undefined', () => {+ const parsed = CyclotronV2JobInitSchema.parse({ teamId: 1, queueName: 'q' })+ expect(parsed.personId).toBeUndefined()+ })++ it('accepts a UUID v4', () => {+ const personId = '550e8400-e29b-41d4-a716-446655440000'+ const parsed = CyclotronV2JobInitSchema.parse({ teamId: 1, queueName: 'q', personId })+ expect(parsed.personId).toBe(personId)+ })+ })++ describe('personId coercion in CyclotronV2RescheduleOptionsSchema', () => {+ it('accepts a valid UUID', () => {+ const personId = uuidv7()+ const parsed = CyclotronV2RescheduleOptionsSchema.parse({ personId })+ expect(parsed.personId).toBe(personId)+ })++ it('coerces non-UUID strings to null', () => {+ const parsed = CyclotronV2RescheduleOptionsSchema.parse({ personId: 'group-key' })+ expect(parsed.personId).toBeNull()+ })++ it('passes through null', () => {+ const parsed = CyclotronV2RescheduleOptionsSchema.parse({ personId: null })+ expect(parsed.personId).toBeNull()+ })+ })+})diff --git a/nodejs/src/cdp/services/cyclotron-v2/types.ts b/nodejs/src/cdp/services/cyclotron-v2/types.tsindex ad648c0b7612..5b86fd10419a 100644--- a/nodejs/src/cdp/services/cyclotron-v2/types.ts+++ b/nodejs/src/cdp/services/cyclotron-v2/types.ts@@ -11,8 +11,17 @@ export type CyclotronV2PoolConfig = {// `id` and `functionId` use PostHog's UUIDT-style identifiers which don't set// valid UUID version bits, so we only validate them as non-empty strings here.-// `personId` comes from posthog_person.uuid (a real UUID v4/v7) and ingestion's-// resolution, so we validate it strictly to catch any bad data at the boundary.+// `personId` comes from posthog_person.uuid (a real UUID v4/v7) for person-based+// blast radius, but group-based blast radius returns group keys (non-UUID strings).+// We coerce non-UUID values to null so the postgres UUID column accepts them+// rather than failing the whole batch insert.+const personIdSchema = z.preprocess((val) => {+ if (typeof val === 'string' && !z.uuid().safeParse(val).success) {+ return null+ }+ return val+}, z.uuid().nullish())+export const CyclotronV2JobInitSchema = z.object({id: z.string().min(1).optional(),teamId: z.number().int(),@@ -23,7 +32,7 @@ export const CyclotronV2JobInitSchema = z.object({parentRunId: z.string().nullish(),state: z.instanceof(Buffer).nullish(),distinctId: z.string().nullish(),- personId: z.uuid().nullish(),+ personId: personIdSchema,actionId: z.string().nullish(),})@@ -33,7 +42,7 @@ export const CyclotronV2RescheduleOptionsSchema = z.object({scheduledAt: z.date().optional(),state: z.instanceof(Buffer).nullish(),distinctId: z.string().nullish(),- personId: z.uuid().nullish(),+ personId: personIdSchema,actionId: z.string().nullish(),})