: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): stub empty person when rerun lookup misses (#68655)
Meikel Ratzb24667ad1a44
diff --git a/nodejs/src/cdp/consumers/cdp-cyclotron-worker.consumer.ts b/nodejs/src/cdp/consumers/cdp-cyclotron-worker.consumer.tsindex 75f571743ca9..f0318718a72c 100644--- a/nodejs/src/cdp/consumers/cdp-cyclotron-worker.consumer.ts+++ b/nodejs/src/cdp/consumers/cdp-cyclotron-worker.consumer.ts@@ -94,8 +94,15 @@ export class CdpCyclotronWorker<? this.personsManager.getCyclotronPerson(item.teamId, hogFuncState.globals.event.distinct_id, 'distinct_id').then((person) => {- if (person) {- hogFuncState.globals.person = person+ // Stub when the lookup misses (cookieless events don't persist to+ // posthog_persondistinctid; reruns may race with person deletes).+ // Leaving undefined would halt any bytecode dereferencing+ // person.properties.* with "Could not execute bytecode".+ hogFuncState.globals.person = person ?? {+ id: '',+ name: '',+ url: '',+ properties: {},}}): undefined,diff --git a/nodejs/src/cdp/rerun/rerun-e2e.test.ts b/nodejs/src/cdp/rerun/rerun-e2e.test.tsindex 043e31ed7695..c37b232d285b 100644--- a/nodejs/src/cdp/rerun/rerun-e2e.test.ts+++ b/nodejs/src/cdp/rerun/rerun-e2e.test.ts@@ -377,4 +377,140 @@ describe('CDP hog invocation rerun e2e', () => {// ── 6. The hog function's fetch was called twice — once original, once rerun ─expect(mockFetch.mock.calls.length).toBeGreaterThanOrEqual(2)})++ it('reruns successfully when person rehydration misses and the input reads person.properties', async () => {+ // On the rerun path the paginator strips globals.person; the worker+ // re-hydrates via getCyclotronPerson, which returns null when the+ // distinct_id has no row in posthog_persondistinctid (cookieless+ // events by design, deleted persons, etc.). The worker must fall back+ // to an empty person stub — leaving globals.person as undefined halts+ // any input bytecode dereferencing `person.properties.*` with "Could+ // not execute bytecode", before coalesce fallbacks can traverse to an+ // event-level branch.+ //+ // If the worker's null-lookup fallback regresses, this test's rerun+ // fetch never fires and the count assertion fails.++ // Custom function whose input specifically dereferences+ // `person.properties.foo` — the shape that crashed pre-fix.+ const FALLBACK_WEBHOOK_URL = 'https://example.com/person-props-fallback'+ const gclidLikeHog = `+ let res := fetch(inputs.url, { 'method': 'POST', 'body': f'foo={inputs.foo}' });+ print('Fetch response:', res);+ `+ const personPropsFn = await _insertHogFunction(hub.postgres, team.id, {+ type: 'destination',+ hog: gclidLikeHog,+ bytecode: await compileHog(gclidLikeHog),+ inputs_schema: [+ { key: 'url', type: 'string', label: 'URL', secret: false, required: true },+ { key: 'foo', type: 'string', label: 'foo', secret: false, required: false },+ ],+ inputs: {+ url: {+ value: FALLBACK_WEBHOOK_URL,+ bytecode: ['_h', 32, FALLBACK_WEBHOOK_URL],+ },+ // Same shape as the Google Ads template: person.properties first,+ // event.properties as the tail fallback.+ foo: {+ value: '{person.properties.foo ?? event.properties.foo}',+ bytecode: await compileHog('return person.properties.foo ?? event.properties.foo'),+ },+ },+ ...HOG_FILTERS_EXAMPLES.no_filters,+ })++ mockFetch.mockResolvedValue({+ status: 200,+ json: () => Promise.resolve({ ok: true }),+ text: () => Promise.resolve('{"ok":true}'),+ headers: { 'Content-Type': 'application/json' },+ dump: () => Promise.resolve(),+ })++ // Simulate cookieless / missing-person for the rerun: personRepo returns+ // no rows for any distinct_id, so getCyclotronPerson returns null. The+ // ORIGINAL invocation doesn't call the repo — events consumer uses the+ // person we attach to data.person directly — so this only affects the+ // rerun path's rehydration.+ const personRepo = cdpDeps.personRepository as jest.Mocked<PersonReadRepository>+ personRepo.fetchPersonsByDistinctIds.mockResolvedValue([])++ // Original event carries the fallback value on event.properties. The+ // person we hand-attach here has no `foo`, so even on the original the+ // coalesce falls through to event.properties.foo — proving the+ // fallback wiring is real (and letting the rerun's stubbed-empty+ // person hit the same branch).+ const cookielessGlobals = createHogExecutionGlobals({+ project: { id: team.id } as any,+ event: {+ uuid: 'aaaa1111-bbbb-2222-cccc-333333333333',+ event: '$pageview',+ distinct_id: 'cookieless_stub',+ properties: { foo: 'from-event' },+ timestamp: '2026-07-02T09:00:00Z',+ } as any,+ person: {+ id: '',+ name: 'cookieless_stub',+ url: 'http://localhost:8000/persons/cookieless_stub',+ properties: {}, // no `foo` — coalesce must reach event branch+ },+ })++ const { invocations } = await eventsConsumer.processBatch([cookielessGlobals])+ // Both fnFetch (from beforeEach) and personPropsFn match a $pageview — we+ // care about ours.+ const personPropsInvocations = invocations.filter((i) => i.functionId === personPropsFn.id)+ expect(personPropsInvocations).toHaveLength(1)++ await waitForExpect(async () => {+ const rows = await clickhouse.query<PersistedRow>(+ `SELECT invocation_id, status FROM hog_invocation_results+ WHERE team_id = ${team.id} AND function_id = '${personPropsFn.id}' AND status = 'succeeded'`+ )+ expect(rows.length).toBeGreaterThanOrEqual(1)+ }, 30_000)++ const originalRows = await clickhouse.query<PersistedRow>(+ `SELECT invocation_id, status FROM hog_invocation_results+ WHERE team_id = ${team.id} AND function_id = '${personPropsFn.id}' AND status = 'succeeded'`+ )+ const originalInvocationId = originalRows[0].invocation_id++ // Trigger the rerun scoped to just this invocation_id.+ const windowStart = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString()+ const windowEnd = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString()+ const rerunJobId = await rerunManager.enqueue(team.id, 'hog_function', personPropsFn.id, {+ filter: {+ window_start: windowStart,+ window_end: windowEnd,+ status: ['succeeded'],+ invocation_ids: [originalInvocationId],+ },+ })++ rerunWorker = new CdpRerunWorkerConsumer(+ { ...hub, CDP_CYCLOTRON_JOB_QUEUE_CONSUMER_MODE: 'postgres' },+ cdpDeps,+ { hog_function: kafkaQueue, hog_flow: postgresV2Queue }+ )+ await rerunWorker.start()++ await waitForExpect(async () => {+ const res = await nodeAssertPool.query('SELECT status FROM cyclotron_jobs WHERE id = $1', [rerunJobId])+ expect(res.rows[0]?.status).toBe('completed')+ }, 30_000)++ // The core assertion: the rerun re-fired the fetch to our specific URL,+ // with the value coming through the coalesce's event-level fallback.+ // Pre-fix, the input eval crashed on `undefined.properties` and this+ // count stayed at 1 (just the original). Post-fix, it's 2 (original ++ // rerun), and the rerun's body carries `foo=from-event`.+ const ourCalls = mockFetch.mock.calls.filter(([url]) => String(url).startsWith(FALLBACK_WEBHOOK_URL))+ expect(ourCalls.length).toBeGreaterThanOrEqual(2)+ const rerunBody = String((ourCalls[ourCalls.length - 1] as any)[1]?.body ?? '')+ expect(rerunBody).toContain('foo=from-event')+ })})