: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(flags): preserve group aggregation in default release conditions (#62192)
Ben Leae58428abd9d4
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 4330a048bae4..0d46d8da0444 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -5373,13 +5373,13 @@ snapshots:scenes-app-feature-flags--new-feature-flag--light:hash: v1.k794b7964.727ad87e0926fee11742fcb9a3925cd8247cee3136c882319043b31ce2bec519.IMOClX-wgcfkL3mCHym-EwK4bPMzFWfC-Q1jS60AnR0scenes-app-feature-flags--new-multivariate-flag-variant-key-error--dark:- hash: v1.k794b7964.a16a6e7374ab04142aa5fe22a63c96a8b03161d3365ca9f20fe2b67c5f6508ae.d4UlULDadIKoQ8Myg3tlck49AXZikj8ZELd3jv2nxZc+ hash: v1.k794b7964.f61edcc3faee1780859338b411e5a411045cd9cdffc9c4e7c57bd36cfb688fb4.ii2nAjS44zy4OMsAN43Rv69G4pvsXrRjKX79RsLpoNsscenes-app-feature-flags--new-multivariate-flag-variant-key-error--light:- hash: v1.k794b7964.ec2cef31ba38e77b273db5f31fb8ebf0ab54fd2743a4215b64546d2fb3b6befd.8cXZyn3DSEXahZUzGmBzA9klphOGrJhkzRUmbbUgdlE+ hash: v1.k794b7964.803efe8f611461a4ab6bb9946df32b9d3fd1b8ae6c81893188818a26e6564180.rWrRAGlq9dRSu9y6qi9y8_PRDBVkvgvpmbuzBmp2_Ukscenes-app-feature-flags--new-remote-config-flag-payload-error--dark:- hash: v1.k794b7964.a16a6e7374ab04142aa5fe22a63c96a8b03161d3365ca9f20fe2b67c5f6508ae.2P2RmRIUmAgLjgOFWl5SxPABQeZg0TvQJC9bunJIARo+ hash: v1.k794b7964.8658214f05277d1993c4f3c3e2e26d2f7a1be4352cc9efd6459b641fe429ba61.tr0v5RTq0pkpUCq4bXSlwLYhHAwf8S3Xtn0gLOgli0Qscenes-app-feature-flags--new-remote-config-flag-payload-error--light:- hash: v1.k794b7964.3cd967b3c2f64308cdd76c0e0fccb2248924a34825fce47a39b799d21ef6359a.BniHMFmxEBBoWOap-0ks83HBkfqJytE4kGffhwlUkI8+ hash: v1.k794b7964.486a154f8e9065c1c617360149bbcfd4c32f7944eb8ecbc00108821608778900.q-nY4Dq5KNrxKwiH8vx1cTyjXUShFFYBg-r6JRDKyL8scenes-app-feature-flags-code-examples--code-instructions-node-with-group-multivariate-flag-local-evaluation--dark:hash: v1.k794b7964.3ce50a6c3abbe37a320a41b9b595595f1f563ee2810f7039c1077de0de2ce541.NshMoutmoo5MZNYuOO7ESAXGz5weprcVLeWqVRJevZAscenes-app-feature-flags-code-examples--code-instructions-node-with-group-multivariate-flag-local-evaluation--light:diff --git a/frontend/src/scenes/feature-flags/DefaultReleaseConditions.tsx b/frontend/src/scenes/feature-flags/DefaultReleaseConditions.tsxindex 94610c570139..4a08ff06e34f 100644--- a/frontend/src/scenes/feature-flags/DefaultReleaseConditions.tsx+++ b/frontend/src/scenes/feature-flags/DefaultReleaseConditions.tsx@@ -8,6 +8,7 @@ import { LemonSwitch } from 'lib/lemon-ui/LemonSwitch'import { FeatureFlagFilters } from '~/types'import { defaultReleaseConditionsLogic } from './defaultReleaseConditionsLogic'+import { distributeAggregationGroupTypeIndex } from './defaultReleaseConditionsUtils'import { FeatureFlagReleaseConditionsCollapsible } from './FeatureFlagReleaseConditionsCollapsible'export function DefaultReleaseConditions(): JSX.Element {@@ -43,7 +44,12 @@ export function DefaultReleaseConditions(): JSX.Element {id="default-release-conditions"filters={filtersForEditor}onChange={(updatedFilters: FeatureFlagFilters) => {- setLocalGroups(updatedFilters.groups)+ setLocalGroups(+ distributeAggregationGroupTypeIndex(+ updatedFilters.groups,+ updatedFilters.aggregation_group_type_index ?? null+ )+ )}}isDisabled={!!restrictedReason}hideEarlyExitdiff --git a/frontend/src/scenes/feature-flags/FeatureFlags.stories.tsx b/frontend/src/scenes/feature-flags/FeatureFlags.stories.tsxindex 87666a63266a..43654d6b453c 100644--- a/frontend/src/scenes/feature-flags/FeatureFlags.stories.tsx+++ b/frontend/src/scenes/feature-flags/FeatureFlags.stories.tsx@@ -108,6 +108,12 @@ const waitForMountedFeatureFlagLogic = async (): Promise<ReturnType<typeof featuif (!logic) {throw new Error('featureFlagLogic({ id: "new" }) not yet mounted')}+ // The new-flag loader awaits default release conditions, so wait for it to settle —+ // otherwise loadFeatureFlagSuccess resets the flag to NEW_FLAG after a play function+ // configures it below.+ if (logic.values.featureFlagLoading) {+ throw new Error('feature flag loader still pending')+ }return logic},{ timeout: 5000 }@@ -172,8 +178,6 @@ export const NewRemoteConfigFlagPayloadError: Story = {logic.actions.setFeatureFlagValue('key', 'demo-remote-config-flag')logic.actions.setFeatureFlagValue('is_remote_configuration', true)- // Yield to let React flush the state updates before triggering validation.- await new Promise((resolve) => setTimeout(resolve, 50))// Submit with empty payload: validatePayloadRequired fails, submitFeatureFlagFailure fires,// the listener expands the payload section, and the inline error is rendered.logic.actions.submitFeatureFlag()diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.tsindex 1ac9e1a19764..dbdb034c5a23 100644--- a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts@@ -8,6 +8,7 @@ import { teamLogic } from 'scenes/teamLogic'import { FeatureFlagFilters, FeatureFlagGroupType } from '~/types'import type { defaultReleaseConditionsLogicType } from './defaultReleaseConditionsLogicType'+import { uniformAggregationGroupTypeIndex } from './defaultReleaseConditionsUtils'export interface DefaultReleaseConditionsResponse {enabled: boolean@@ -122,6 +123,7 @@ export const defaultReleaseConditionsLogic = kea<defaultReleaseConditionsLogicTygroups: groups.length > 0 ? groups : [{ properties: [], rollout_percentage: 0, variant: null }],multivariate: null,payloads: {},+ aggregation_group_type_index: uniformAggregationGroupTypeIndex(groups),}),],diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.test.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.test.tsnew file mode 100644index 000000000000..8cd925b69e16--- /dev/null+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.test.ts@@ -0,0 +1,67 @@+import { FeatureFlagGroupType } from '~/types'++import { distributeAggregationGroupTypeIndex, uniformAggregationGroupTypeIndex } from './defaultReleaseConditionsUtils'++const group = (aggregation_group_type_index?: number | null): FeatureFlagGroupType => ({+ properties: [],+ rollout_percentage: 100,+ variant: null,+ aggregation_group_type_index,+})++describe('uniformAggregationGroupTypeIndex', () => {+ it('returns null for no groups', () => {+ expect(uniformAggregationGroupTypeIndex([])).toBeNull()+ })++ it('returns null when no group is aggregated', () => {+ expect(uniformAggregationGroupTypeIndex([group(), group()])).toBeNull()+ })++ it('returns the shared index when every group agrees', () => {+ expect(uniformAggregationGroupTypeIndex([group(1), group(1)])).toBe(1)+ })++ it('preserves index 0 (first group type)', () => {+ expect(uniformAggregationGroupTypeIndex([group(0)])).toBe(0)+ })++ it('returns null for mixed indices', () => {+ expect(uniformAggregationGroupTypeIndex([group(1), group(2)])).toBeNull()+ })++ it('returns null when some groups are aggregated and others are not', () => {+ expect(uniformAggregationGroupTypeIndex([group(1), group()])).toBeNull()+ })+})++describe('distributeAggregationGroupTypeIndex', () => {+ it('returns the groups unchanged when the index is null', () => {+ const groups = [group(1), group()]+ expect(distributeAggregationGroupTypeIndex(groups, null)).toBe(groups)+ })++ it('does not stamp a null index onto groups that omit the field', () => {+ const groups = [group()]+ const result = distributeAggregationGroupTypeIndex(groups, null)+ expect('aggregation_group_type_index' in result[0] && result[0].aggregation_group_type_index).toBeFalsy()+ })++ it('stamps the index onto every group', () => {+ const result = distributeAggregationGroupTypeIndex([group(), group(2)], 1)+ expect(result.map((g) => g.aggregation_group_type_index)).toEqual([1, 1])+ })++ it('distributes index 0 (first group type)', () => {+ const result = distributeAggregationGroupTypeIndex([group(), group(1)], 0)+ expect(result.map((g) => g.aggregation_group_type_index)).toEqual([0, 0])+ })++ it('leaves already-matching groups referentially unchanged', () => {+ const matching = group(1)+ const result = distributeAggregationGroupTypeIndex([matching, group(2)], 1)+ expect(result[0]).toBe(matching)+ expect(result[1]).not.toBe(matching)+ expect(result[1].aggregation_group_type_index).toBe(1)+ })+})diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.tsnew file mode 100644index 000000000000..f7fd9b4873e1--- /dev/null+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsUtils.ts@@ -0,0 +1,23 @@+import { FeatureFlagGroupType } from '~/types'++// The shared group aggregation when every group agrees, otherwise null (mixed or none).+export function uniformAggregationGroupTypeIndex(groups: FeatureFlagGroupType[]): number | null {+ const indices = groups.map((group) => group.aggregation_group_type_index ?? null)+ return indices.length > 0 && indices.every((index) => index === indices[0]) ? indices[0] : null+}++// Mirror a top-level group aggregation onto every group — the inverse of uniformAggregationGroupTypeIndex.+// A null index leaves each group's own index untouched (so mixed-targeting per-group values survive and+// loading a saved config doesn't dirty the form); groups already at the target index are returned as-is+// to keep the dirty-check stable.+export function distributeAggregationGroupTypeIndex(+ groups: FeatureFlagGroupType[],+ index: number | null+): FeatureFlagGroupType[] {+ if (index === null) {+ return groups+ }+ return groups.map((group) =>+ group.aggregation_group_type_index === index ? group : { ...group, aggregation_group_type_index: index }+ )+}diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.tsindex 79068110aa50..1f9b4f142321 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts@@ -9,6 +9,7 @@ import { urls } from 'scenes/urls'import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'import {+ FeatureFlagGroupType,FeatureFlagType,PropertyFilterType,PropertyOperator,@@ -689,6 +690,56 @@ describe('featureFlagLogic', () => {})})+ describe('default release conditions on new flags', () => {+ const groupDefault: FeatureFlagGroupType = {+ properties: [+ {+ key: 'is_dev',+ type: PropertyFilterType.Group,+ value: ['true'],+ operator: PropertyOperator.Exact,+ group_type_index: 1,+ },+ ],+ rollout_percentage: 100,+ variant: null,+ aggregation_group_type_index: 1,+ }++ async function mountNewFlag(defaultConditions: {+ enabled: boolean+ default_groups: FeatureFlagGroupType[]+ }): Promise<ReturnType<typeof featureFlagLogic.build>> {+ // Seed the shared singleton's cache before the new-flag loader reads it; useMocks lands+ // too late since the logic is warmed to a disabled value on mount in beforeEach.+ defaultReleaseConditionsLogic.actions.loadDefaultReleaseConditionsSuccess(defaultConditions)+ // Park at a non-matching path so the `new`-keyed logic doesn't prefetch off stale params.+ router.actions.push('/')+ const newLogic = featureFlagLogic({ id: 'new' })+ newLogic.mount()+ await expectLogic(newLogic).toFinishAllListeners()+ return newLogic+ }++ it('applies an enabled group-targeted default and mirrors the aggregation onto the new flag', async () => {+ const newLogic = await mountNewFlag({ enabled: true, default_groups: [groupDefault] })++ expect(newLogic.values.featureFlag.filters.groups).toEqual([groupDefault])+ expect(newLogic.values.featureFlag.filters.aggregation_group_type_index).toBe(1)+ newLogic.unmount()+ })++ it('leaves a new flag on user targeting when the default config is disabled', async () => {+ const newLogic = await mountNewFlag({ enabled: false, default_groups: [groupDefault] })++ expect(newLogic.values.featureFlag.filters.groups).toEqual([+ { properties: [], rollout_percentage: 0, variant: null },+ ])+ expect(newLogic.values.featureFlag.filters.aggregation_group_type_index).toBeUndefined()+ newLogic.unmount()+ })+ })+describe('experiment loading', () => {it('loads experiment data when feature flag has an experiment linked', async () => {const flagWithExperiment = {diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.tsindex b0b9d58f68ae..1c7e60b35764 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts@@ -85,6 +85,7 @@ import { organizationLogic } from '../organizationLogic'import { teamLogic } from '../teamLogic'import { defaultEvaluationContextsLogic } from './defaultEvaluationContextsLogic'import { defaultReleaseConditionsLogic, resolveDefaultReleaseConditions } from './defaultReleaseConditionsLogic'+import { uniformAggregationGroupTypeIndex } from './defaultReleaseConditionsUtils'import { checkFeatureFlagConfirmation } from './featureFlagConfirmationLogic'import type { FlagIntent } from './featureFlagIntentWarningLogic'import type { featureFlagLogicType } from './featureFlagLogicType'@@ -1313,6 +1314,9 @@ export const featureFlagLogic = kea<featureFlagLogicType>([filters: {...baseFlagConfig.filters,groups: conditionsConfig.default_groups,+ aggregation_group_type_index: uniformAggregationGroupTypeIndex(+ conditionsConfig.default_groups+ ),},}}diff --git a/posthog/api/test/test_team_default_release_conditions.py b/posthog/api/test/test_team_default_release_conditions.pyindex ea66ec741718..5540682dc7c6 100644--- a/posthog/api/test/test_team_default_release_conditions.py+++ b/posthog/api/test/test_team_default_release_conditions.py@@ -141,3 +141,40 @@ class TestTeamDefaultReleaseConditions(APIBaseTest):self.assertTrue(data["enabled"])self.assertEqual(len(data["default_groups"]), 1)self.assertEqual(data["default_groups"][0]["properties"][0]["key"], "plan")++ @parameterized.expand(+ [+ ("group_type_index_0", 0),+ ("group_type_index_1", 1),+ ]+ )+ def test_put_preserves_group_aggregation_on_conditions(self, _name, group_type_index):+ groups = [+ {+ "properties": [+ {+ "key": "is_dev",+ "type": "group",+ "value": ["true"],+ "operator": "exact",+ "group_type_index": group_type_index,+ }+ ],+ "rollout_percentage": 100,+ "variant": None,+ "aggregation_group_type_index": group_type_index,+ }+ ]++ put_response = self.client.put(+ self.url,+ {"enabled": True, "default_groups": groups},+ format="json",+ )+ self.assertEqual(put_response.status_code, status.HTTP_200_OK)++ get_response = self.client.get(self.url)+ self.assertEqual(get_response.status_code, status.HTTP_200_OK)+ stored_group = get_response.json()["default_groups"][0]+ self.assertEqual(stored_group["aggregation_group_type_index"], group_type_index)+ self.assertEqual(stored_group["properties"][0]["group_type_index"], group_type_index)