: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(mcp): keep person targeting when a set repeats a group property (#119876)
Phil Haack0381feae545d
diff --git a/services/mcp/src/tools/featureFlags/preserveGroupTargeting.ts b/services/mcp/src/tools/featureFlags/preserveGroupTargeting.tsindex 82596dedbef0..810db12baad0 100644--- a/services/mcp/src/tools/featureFlags/preserveGroupTargeting.ts+++ b/services/mcp/src/tools/featureFlags/preserveGroupTargeting.ts@@ -1,8 +1,9 @@/*** Preserve group-based feature-flag targeting when MCP agents send partial `filters`.- * Agents rebuild release conditions with only `key` / `operator` / `value`, and the API- * reads an omitted property `type` as person, which silently converts a group flag to a- * person flag (PostHog/posthog#46501).+ * Agents rebuild release conditions with only `key` / `operator` / `value`. The API rejects a+ * property that has no `type`. A condition set without `aggregation_group_type_index` takes the+ * flag-level value, so it can lose its group targeting. The merge therefore restores these+ * omitted fields from the stored flag (PostHog/posthog#46501).** An incoming condition set takes the stored set at its own index as its source when every stored* set keeps its property keys at its index. New sets may only follow the stored ones. On a flag@@ -29,6 +30,8 @@* From a group-aggregated source, or from another stored set, it restores only person-aggregated* types. A group property in a group-aggregated set says nothing about a person set. An echo of a* group property that a person-aggregated source holds does not move the set to group aggregation.+ * On a flag whose stored sets all aggregate on persons, a set without a source compares its echo+ * against every stored set instead. An appended set also has no source.* check_property_types_match_aggregation in products/feature_flags/backend/filters_validation.py* reports a group property in a person set and a person-aggregated property in a group set. The* evaluator supports both shapes. The merge keeps them where the stored flag already has them.@@ -287,27 +290,29 @@ type MergeConditionOptions = {payloadChangesFlagAggregation: boolean/** This set never gains group aggregation. See isPinnedToPerson. */pinnedToPerson: boolean+ /** Stored properties that a typed group property can repeat without naming a group type. */+ personSetPropsByKey: Map<string, FlagProperty[]> | undefined}-function holdsGroupProperty(existingSet: ExistingSet | undefined, prop: FlagProperty): boolean {+function holdsGroupProperty(propsByKey: Map<string, FlagProperty[]> | undefined, prop: FlagProperty): boolean {return (typeof prop.key === 'string' &&- !!existingSet?.propsByKey- .get(prop.key)+ !!propsByKey+ ?.get(prop.key)?.some((stored) => stored.type === 'group' && stored.group_type_index === prop.group_type_index))}function explicitGroupPropertyIndex(group: FlagConditionGroup,- personSource: ExistingSet | undefined+ personSetPropsByKey: Map<string, FlagProperty[]> | undefined): number | undefined {const properties = Array.isArray(group.properties) ? group.properties : []const typed = properties.filter((prop): prop is FlagProperty & { group_type_index: number } =>prop?.type === 'group' && isPresentGroupIndex(prop.group_type_index))- if (typed.every((prop) => holdsGroupProperty(personSource, prop))) {+ if (typed.every((prop) => holdsGroupProperty(personSetPropsByKey, prop))) {return undefined}const unique = [...new Set(typed.map((prop) => prop.group_type_index))]@@ -328,19 +333,18 @@ function resolveSetAggregation(out.aggregation_group_type_index = null}- const { pinnedToPerson, payloadChangesFlagAggregation } = options+ const { pinnedToPerson, payloadChangesFlagAggregation, personSetPropsByKey } = options// Fill only when the key is absent. An explicit null means person aggregation. A payload// that changes the flag level already decides this set, the same way the API distributes// the flag level into every set that sends no index of its own. A group property that the// agent typed with an index names the group type of its own set. A retarget that the agent// sends only on the property therefore survives. A set whose typed group properties all- // repeat ones its person-aggregated source holds names no group type, because that set- // filters on them without aggregating on their group type.+ // repeat ones in personSetPropsByKey names no group type, because the stored+ // person-aggregated set filters on them without aggregating on their group type.if (!pinnedToPerson && !payloadChangesFlagAggregation && !hasKey(out, 'aggregation_group_type_index')) {const restored =- explicitGroupPropertyIndex(incoming, sourceSet?.aggregation === null ? sourceSet : undefined) ??- sourceSet?.group.aggregation_group_type_index+ explicitGroupPropertyIndex(incoming, personSetPropsByKey) ?? sourceSet?.group.aggregation_group_type_indexif (isPresentGroupIndex(restored)) {out.aggregation_group_type_index = restored}@@ -438,7 +442,15 @@ export function preserveGroupTargetingFilters((group) => isRecord(group) && isPinnedToPerson(group, incomingClearsAggregation))const aggregations = existingSets.map((existingSet) => existingSet?.aggregation)- const mixedAggregation = new Set(aggregations.filter((aggregation) => aggregation !== undefined)).size > 1+ const storedAggregations = new Set(aggregations.filter((aggregation) => aggregation !== undefined))+ const mixedAggregation = storedAggregations.size > 1+ const aggregatesOnPersonsOnly = storedAggregations.size === 1 && storedAggregations.has(null)+ const personSetPropsFor = (sourceSet: ExistingSet | undefined): Map<string, FlagProperty[]> | undefined => {+ if (sourceSet) {+ return sourceSet.aggregation === null ? sourceSet.propsByKey : undefined+ }+ return aggregatesOnPersonsOnly ? crossSetPropsByKey : undefined+ }const incomingGroups = result.groupsconst sharesKeysAcrossAggregations = (existingSet: ExistingSet, index: number): boolean =>existingSets.some(@@ -466,6 +478,7 @@ export function preserveGroupTargetingFilters(? resolveSetAggregation(group, sourceSets[index], {payloadChangesFlagAggregation,pinnedToPerson: pinnedToPerson[index] ?? false,+ personSetPropsByKey: personSetPropsFor(sourceSets[index]),}): group)diff --git a/services/mcp/tests/unit/preserve-group-targeting.test.ts b/services/mcp/tests/unit/preserve-group-targeting.test.tsindex 3b91596df096..42bcf6fec6ea 100644--- a/services/mcp/tests/unit/preserve-group-targeting.test.ts+++ b/services/mcp/tests/unit/preserve-group-targeting.test.ts@@ -187,6 +187,18 @@ describe('preserveGroupTargetingFilters', () => {],}+ const existingPersonSetWithGroupPropertyFlag = {+ groups: [+ {+ properties: [+ { key: 'email', type: 'person', operator: 'icontains', value: '@acme.com' },+ { key: 'region', type: 'group', group_type_index: 1, operator: 'exact', value: 'us' },+ ],+ rollout_percentage: 100,+ },+ ],+ }+it('preserves aggregation_group_type_index when omitted on update', () => {const incoming = {groups: [@@ -621,20 +633,15 @@ describe('preserveGroupTargetingFilters', () => {name: 'the agent echoes the property with its group type and index',region: { type: 'group', group_type_index: 1 },},+ {+ name: 'a changed key leaves the set with no source and the agent echoes the group property',+ email: { key: 'country' },+ region: { type: 'group', group_type_index: 1 },+ },])('keeps a stored group property in a person-aggregated set when $name',({ flag = {}, email = {}, region = {} }) => {- const existing = {- groups: [- {- properties: [- { key: 'email', type: 'person', operator: 'icontains', value: '@acme.com' },- { key: 'region', type: 'group', group_type_index: 1, operator: 'exact', value: 'us' },- ],- rollout_percentage: 100,- },- ],- }+ const existing = existingPersonSetWithGroupPropertyFlagconst merged = preserveGroupTargetingFilters(existing, {...flag,@@ -691,6 +698,32 @@ describe('preserveGroupTargetingFilters', () => {expect(merged?.groups?.[0]?.properties?.map((prop) => prop.group_type_index)).toEqual([1, 0])})+ // The oracle needs a stored set at the same index to accept a group property. An appended set has none.+ it('keeps an appended set on persons when it repeats a group property that a stored set holds', () => {+ const existing = existingPersonSetWithGroupPropertyFlag++ const merged = preserveGroupTargetingFilters(existing, {+ groups: [+ ...existing.groups,+ {+ properties: [{ key: 'region', type: 'group', group_type_index: 1, operator: 'exact', value: 'eu' }],+ rollout_percentage: 100,+ },+ ],+ })++ expect(merged?.groups?.[0]?.properties).toEqual(existing.groups[0]?.properties)+ expect(merged?.groups?.[0]?.aggregation_group_type_index ?? null).toBeNull()+ expect(merged?.groups?.[1]?.properties?.[0]).toEqual({+ key: 'region',+ type: 'group',+ group_type_index: 1,+ operator: 'exact',+ value: 'eu',+ })+ expect(merged?.groups?.[1]?.aggregation_group_type_index ?? null).toBeNull()+ })+it('passes through multivariate and payloads without restoring from existing', () => {const existing = {aggregation_group_type_index: 0,@@ -1209,6 +1242,60 @@ describe('preserveGroupTargetingFilters', () => {},expected: [[2, 'group', 2]],},+ {+ name: 'a person set drops a key and moves its group property to another group type',+ existing: existingPersonSetWithGroupPropertyFlag,+ incoming: {+ groups: [+ {+ properties: [+ { key: 'region', type: 'group', group_type_index: 2, operator: 'exact', value: 'eu' },+ ],+ rollout_percentage: 100,+ },+ ],+ },+ expected: [[2, 'group', 2]],+ },+ {+ name: 'a set appended to a mixed flag repeats the group property of its person set',+ existing: {+ aggregation_group_type_index: null,+ groups: [+ {+ aggregation_group_type_index: 0,+ properties: [+ { key: 'plan', type: 'group', group_type_index: 0, operator: 'exact', value: 'enterprise' },+ ],+ rollout_percentage: 100,+ },+ {+ aggregation_group_type_index: null,+ properties: [+ { key: 'region', type: 'group', group_type_index: 1, operator: 'exact', value: 'us' },+ ],+ rollout_percentage: 100,+ },+ ],+ },+ incoming: {+ groups: [+ { properties: [{ key: 'plan', operator: 'exact', value: 'pro' }], rollout_percentage: 100 },+ { properties: [{ key: 'region', operator: 'exact', value: 'eu' }], rollout_percentage: 100 },+ {+ properties: [+ { key: 'region', type: 'group', group_type_index: 1, operator: 'exact', value: 'ca' },+ ],+ rollout_percentage: 100,+ },+ ],+ },+ expected: [+ [0, 'group', 0],+ [undefined, 'group', 1],+ [1, 'group', 1],+ ],+ },{name: 'a stored set that inherits a group flag level moves to persons',existing: {