: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): rebuild mapping inputs on rerun so mapping destinations re-fire (#67854)
Meikel Ratze3df0b1e14cf
diff --git a/nodejs/src/cdp/services/hog-executor.service.test.ts b/nodejs/src/cdp/services/hog-executor.service.test.tsindex a279bfa43970..cfdc4c081ded 100644--- a/nodejs/src/cdp/services/hog-executor.service.test.ts+++ b/nodejs/src/cdp/services/hog-executor.service.test.ts@@ -620,6 +620,49 @@ describe('Hog Executor', () => {`"https://example.com/posthog-webhook"`)})++ it('rebuilds mapping inputs when an invocation arrives without inputs (rerun path)', async () => {+ // The rerun path strips `inputs` from the persisted globals and lets+ // the executor rebuild them. For mapping destinations the mapping's+ // own inputs (e.g. Google Ads `gclid`) must be re-merged — otherwise+ // they resolve to nothing and the function early-exits on rerun.+ const hog = `return inputs.gclid`+ const mappingFn = createHogFunction({+ hog,+ bytecode: await compileHog(hog),+ ...HOG_FILTERS_EXAMPLES.no_filters,+ inputs_schema: [],+ mappings: [+ {+ ...HOG_FILTERS_EXAMPLES.no_filters,+ inputs: {+ gclid: {+ order: 0,+ value: '{person.properties.gclid ?? person.properties.$initial_gclid}',+ bytecode: await compileHog(+ 'return person.properties.gclid ?? person.properties.$initial_gclid'+ ),+ },+ },+ },+ ],+ })++ const invocation = createExampleInvocation(mappingFn, {+ person: {+ id: 'uuid',+ name: 'test',+ url: 'http://localhost:8000/persons/1',+ properties: { email: '[email protected]', $initial_gclid: 'INITIAL_TOKEN_ABC' },+ },+ })+ // Simulate the rerun blob: inputs are stripped before persistence.+ expect(invocation.state.globals.inputs).toBeUndefined()++ const res = await executor.execute(invocation)+ expect(res.error).toBeUndefined()+ expect(res.execResult).toBe('INITIAL_TOKEN_ABC')+ })})describe('slow functions', () => {diff --git a/nodejs/src/cdp/services/hog-executor.service.ts b/nodejs/src/cdp/services/hog-executor.service.tsindex a95ff2742af9..7c98c195798a 100644--- a/nodejs/src/cdp/services/hog-executor.service.ts+++ b/nodejs/src/cdp/services/hog-executor.service.ts@@ -217,6 +217,51 @@ export class HogExecutorService {return this.hogInputsService.buildInputsWithGlobals(hogFunction, globals, additionalInputs)}+ /**+ * For mapping destinations the per-mapping inputs (e.g. the Google Ads+ * `gclid`) are resolved only for mappings whose filters match the event —+ * see `buildHogFunctionInvocations`, which merges `mapping.inputs` when it+ * first builds the invocation. The rerun path re-enqueues invocations with+ * `inputs` stripped and keeps no record of which mapping produced them, so+ * a plain rebuild against the top-level config drops those inputs entirely+ * and any function guarding on them (e.g. `if (empty(inputs.gclid))`)+ * early-exits. Re-match the mappings here against the (current) config to+ * rebuild the additional inputs before the executor resolves them.+ *+ * When several mappings match one event the original produced a separate+ * invocation per mapping; the stored row can't be tied back to a single+ * one, so we merge all matching mappings' inputs — exact for the common+ * single-mapping case and strictly better than dropping them.+ */+ private async resolveMappingInputs(+ hogFunction: HogFunctionType,+ globals: HogFunctionInvocationGlobals+ ): Promise<HogFunctionType['inputs'] | undefined> {+ const mappings = hogFunction.mappings+ if (!mappings || mappings.length === 0) {+ return undefined+ }++ const filterGlobals = convertToHogFunctionFilterGlobal(globals)+ let merged: HogFunctionType['inputs'] | undefined++ for (const mapping of mappings) {+ if (!mapping.inputs) {+ continue+ }+ const { match } = await filterFunctionInstrumented({+ fn: hogFunction,+ filters: mapping.filters,+ filterGlobals,+ })+ if (match) {+ merged = { ...(merged ?? {}), ...mapping.inputs }+ }+ }++ return merged+ }+async buildHogFunctionInvocations(hogFunctions: HogFunctionType[],triggerGlobals: HogFunctionInvocationGlobals@@ -516,10 +561,18 @@ export class HogExecutorService {if (invocation.state.globals.inputs) {globals = invocation.state.globals} else {- globals = await this.hogInputsService.buildInputsWithGlobals(+ // Mapping destinations need their per-mapping inputs+ // re-merged here — they aren't part of the top-level config+ // and were stripped from the rerun blob.+ const additionalInputs = await this.resolveMappingInputs(invocation.hogFunction,invocation.state.globals)+ globals = await this.hogInputsService.buildInputsWithGlobals(+ invocation.hogFunction,+ invocation.state.globals,+ additionalInputs+ )}} catch (e) {addLog('error', `Error building inputs: ${e}`)