: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): accept flat and nested cohort filter shapes in evaluation (#63366)
Miron Tkachuk42e9eccb66b4
diff --git a/rust/feature-flags/src/cohorts/cohort_models.rs b/rust/feature-flags/src/cohorts/cohort_models.rsindex b62484f550e2..56c419aac685 100644--- a/rust/feature-flags/src/cohorts/cohort_models.rs+++ b/rust/feature-flags/src/cohorts/cohort_models.rs@@ -110,14 +110,30 @@ pub struct CohortProperty {pub struct InnerCohortProperty {#[serde(rename = "type")]pub prop_type: CohortPropertyType,- pub values: Vec<CohortValues>,+ pub values: Vec<CohortValuesItem>,}#[derive(Debug, Clone, Deserialize, Serialize)]pub struct CohortValues {#[serde(rename = "type")]pub prop_type: String,- pub values: Vec<PropertyFilter>,+ pub values: Vec<CohortValuesItem>,+}++/// A single entry inside a cohort property group: either a nested group or a leaf+/// property filter. Mirrors `FilterOrGroup` in posthog/api/cohort.py — the cohort UI+/// always wraps filters in an inner group, but the API contract allows filters+/// directly in a group and arbitrarily deep nesting, so evaluation must accept both.+///+/// `Filter` is listed first because `#[serde(untagged)]` tries variants in order:+/// `PropertyFilter` requires `key`, so a group (no `key`) falls through to `Group`,+/// while an ambiguous object carrying both `key` and `values` is kept as the filter+/// it most likely is rather than silently dropping its `key`/`value`/`operator`.+#[derive(Debug, Clone, Deserialize, Serialize)]+#[serde(untagged)]+pub enum CohortValuesItem {+ Filter(PropertyFilter),+ Group(CohortValues),}#[cfg(test)]diff --git a/rust/feature-flags/src/cohorts/cohort_operations.rs b/rust/feature-flags/src/cohorts/cohort_operations.rsindex f4d01f065355..e5f5d1da0e6c 100644--- a/rust/feature-flags/src/cohorts/cohort_operations.rs+++ b/rust/feature-flags/src/cohorts/cohort_operations.rs@@ -6,7 +6,9 @@ use std::collections::HashSet;use super::cohort_models::CohortPropertyType;use super::cohort_models::CohortValues;use crate::cohorts::cohort_cache_manager::CohortFetchError;-use crate::cohorts::cohort_models::{Cohort, CohortId, CohortProperty, InnerCohortProperty};+use crate::cohorts::cohort_models::{+ Cohort, CohortId, CohortProperty, CohortValuesItem, InnerCohortProperty,+};use crate::database::get_connection_with_metrics;use crate::properties::property_matching::match_property;use crate::properties::property_models::OperatorType;@@ -16,6 +18,11 @@ use chrono_tz::Tz;use common_database::PostgresReader;use common_types::TeamId;+/// Maximum cohort filter group nesting depth walked during evaluation and dependency+/// extraction. The cohort UI tops out at 3-4 levels; this generous bound guards the hot+/// `/flags` path against stack overflow from an adversarially deep filter tree.+const MAX_COHORT_FILTER_DEPTH: usize = 64;+/// Column list for `posthog_cohort` queries. Must match the fields in `Cohort` (sqlx::FromRow).const COHORT_COLUMNS: &str = r#"c.id, c.name, c.description, c.team_id, c.deleted, c.filters,@@ -175,8 +182,28 @@ impl Cohort {inner: &InnerCohortProperty,dependencies: &mut HashSet<CohortId>,) -> Result<(), FlagError> {- for cohort_values in &inner.values {- for filter in &cohort_values.values {+ for item in &inner.values {+ Self::traverse_item(item, dependencies, 0)?;+ }+ Ok(())+ }++ /// Visits a group-or-filter entry, recursing into nested groups.+ fn traverse_item(+ item: &CohortValuesItem,+ dependencies: &mut HashSet<CohortId>,+ depth: usize,+ ) -> Result<(), FlagError> {+ if depth > MAX_COHORT_FILTER_DEPTH {+ return Err(FlagError::CohortFiltersParsingError);+ }+ match item {+ CohortValuesItem::Group(group) => {+ for nested in &group.values {+ Self::traverse_item(nested, dependencies, depth + 1)?;+ }+ }+ CohortValuesItem::Filter(filter) => {if filter.is_cohort() {if let Some(cohort_id) = filter.get_cohort_id() {dependencies.insert(cohort_id);@@ -184,7 +211,6 @@ impl Cohort {return Err(FlagError::CohortFiltersParsingError);}}- // NB: we don't support nested cohort properties, so we don't need to traverse further}}Ok(())@@ -192,40 +218,6 @@ impl Cohort {}impl InnerCohortProperty {- /// Flattens the nested cohort property structure into a list of property filters.- ///- /// The cohort property structure in Postgres looks like:- /// ```json- /// {- /// "type": "OR",- /// "values": [- /// {- /// "type": "OR",- /// "values": [- /// {- /// "key": "email",- /// "value": "@posthog.com",- /// "type": "person",- /// "operator": "icontains"- /// },- /// {- /// "key": "age",- /// "value": 25,- /// "type": "person",- /// "operator": "gt"- /// }- /// ]- /// }- /// ]- /// }- /// ```- pub fn to_inner(self) -> Vec<PropertyFilter> {- self.values- .into_iter()- .flat_map(|value| value.values)- .collect()- }-/// Evaluates a cohort property based on its type (AND/OR) and values.////// This function recursively evaluates the cohort property tree structure, handling both@@ -238,11 +230,12 @@ impl InnerCohortProperty {) -> Result<bool, FlagError> {match self.prop_type {CohortPropertyType::OR => {- for cohort_values in &self.values {- if evaluate_cohort_values(- cohort_values,+ for item in &self.values {+ if evaluate_cohort_item(+ item,target_properties,cohort_matches,+ 0,team_timezone,)? {return Ok(true);@@ -251,11 +244,12 @@ impl InnerCohortProperty {Ok(false)}CohortPropertyType::AND => {- for cohort_values in &self.values {- if !evaluate_cohort_values(- cohort_values,+ for item in &self.values {+ if !evaluate_cohort_item(+ item,target_properties,cohort_matches,+ 0,team_timezone,)? {return Ok(false);@@ -267,51 +261,91 @@ impl InnerCohortProperty {}}+/// Evaluates a group-or-filter entry, recursing into nested groups.+fn evaluate_cohort_item(+ item: &CohortValuesItem,+ target_properties: &HashMap<String, Value>,+ cohort_matches: &HashMap<CohortId, bool>,+ depth: usize,+ team_timezone: Tz,+) -> Result<bool, FlagError> {+ if depth > MAX_COHORT_FILTER_DEPTH {+ return Err(FlagError::CohortFiltersParsingError);+ }+ match item {+ CohortValuesItem::Group(group) => evaluate_cohort_values(+ group,+ target_properties,+ cohort_matches,+ depth,+ team_timezone,+ ),+ CohortValuesItem::Filter(filter) => {+ evaluate_cohort_filter(filter, target_properties, cohort_matches, team_timezone)+ }+ }+}++/// Evaluates a leaf property filter, applying `negation` for both filter kinds.+fn evaluate_cohort_filter(+ filter: &PropertyFilter,+ target_properties: &HashMap<String, Value>,+ cohort_matches: &HashMap<CohortId, bool>,+ team_timezone: Tz,+) -> Result<bool, FlagError> {+ if filter.is_cohort() {+ // Handle cohort membership check with negation+ let cohort_result =+ apply_cohort_membership_logic(std::slice::from_ref(filter), cohort_matches)?;+ Ok(cohort_result != filter.negation.unwrap_or(false))+ } else {+ // Handle regular property check with negation+ Ok(evaluate_property_with_negation(+ filter,+ target_properties,+ team_timezone,+ ))+ }+}+/// Evaluates a set of cohort values against target properties.///-/// This function handles both regular property matching and cohort membership checks-/// based on the property type (OR/AND/property).+/// Entries may be property filters or nested groups, combined with the group's+/// logical type (OR/AND/property, where "property" is a legacy alias for AND).+/// `depth` is the caller's depth; the recursion guard lives in `evaluate_cohort_item`,+/// which every nested entry routes back through.fn evaluate_cohort_values(values: &CohortValues,target_properties: &HashMap<String, Value>,cohort_matches: &HashMap<CohortId, bool>,+ depth: usize,team_timezone: Tz,) -> Result<bool, FlagError> {match values.prop_type.as_str() {"OR" => {- for filter in &values.values {- if filter.is_cohort() {- // Handle cohort membership check- if apply_cohort_membership_logic(std::slice::from_ref(filter), cohort_matches)?- {- return Ok(true);- }- } else {- // Handle regular property check with negation- if evaluate_property_with_negation(filter, target_properties, team_timezone) {- return Ok(true);- }+ for item in &values.values {+ if evaluate_cohort_item(+ item,+ target_properties,+ cohort_matches,+ depth + 1,+ team_timezone,+ )? {+ return Ok(true);}}Ok(false)}"AND" | "property" => {- for filter in &values.values {- if filter.is_cohort() {- // Handle cohort membership check with negation- let cohort_result = apply_cohort_membership_logic(- std::slice::from_ref(filter),- cohort_matches,- )?;- // Apply negation if specified- if cohort_result == filter.negation.unwrap_or(false) {- return Ok(false);- }- } else {- // Handle regular property check with negation- if !evaluate_property_with_negation(filter, target_properties, team_timezone) {- return Ok(false);- }+ for item in &values.values {+ if !evaluate_cohort_item(+ item,+ target_properties,+ cohort_matches,+ depth + 1,+ team_timezone,+ )? {+ return Ok(false);}}Ok(true)@@ -469,11 +503,7 @@ impl DependencyProvider for Cohort {#[cfg(test)]mod tests {use super::*;- use crate::{- cohorts::cohort_models::{CohortPropertyType, CohortValues},- properties::property_models::PropertyType,- utils::test_utils::TestContext,- };+ use crate::utils::test_utils::TestContext;use serde_json::json;#[tokio::test]@@ -515,45 +545,6 @@ mod tests {assert!(names.contains("Cohort 2"));}- #[test]- fn test_cohort_property_to_inner() {- let cohort_property = InnerCohortProperty {- prop_type: CohortPropertyType::AND,- values: vec![CohortValues {- prop_type: "property".to_string(),- values: vec![- PropertyFilter {- key: "email".to_string(),- value: Some(json!("[email protected]")),- operator: None,- prop_type: PropertyType::Person,- group_type_index: None,- negation: None,- compiled_regex: None,- extra: Default::default(),- },- PropertyFilter {- key: "age".to_string(),- value: Some(json!(25)),- operator: None,- prop_type: PropertyType::Person,- group_type_index: None,- negation: None,- compiled_regex: None,- extra: Default::default(),- },- ],- }],- };-- let result = cohort_property.to_inner();- assert_eq!(result.len(), 2);- assert_eq!(result[0].key, "email");- assert_eq!(result[0].value, Some(json!("[email protected]")));- assert_eq!(result[1].key, "age");- assert_eq!(result[1].value, Some(json!(25)));- }-#[tokio::test]async fn test_extract_dependencies() {let context = TestContext::new(None).await;@@ -1037,4 +1028,330 @@ mod tests {"User with wrong domain should NOT match regardless of exclusion");}++ fn create_dynamic_cohort_with_filters(id: CohortId, filters: serde_json::Value) -> Cohort {+ Cohort {+ id,+ name: Some(format!("Cohort {id}")),+ description: None,+ team_id: 1,+ deleted: false,+ filters: Some(filters),+ query: None,+ version: None,+ pending_version: None,+ count: None,+ is_calculating: false,+ is_static: false,+ errors_calculating: 0,+ groups: json!({}),+ created_by_id: None,+ cohort_type: None,+ last_backfill_person_properties_at: None,+ }+ }++ #[test]+ fn test_extract_dependencies_with_flat_and_nested_filters() {+ // Cohorts created via the API can place property filters (including cohort+ // references) directly inside `properties.values` without an inner group, and+ // can nest groups deeper than the UI does.+ let cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {+ "type": "AND",+ "values": [+ {"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"},+ {"key": "id", "type": "cohort", "value": 5, "negation": false},+ {"type": "OR", "values": [+ {"type": "AND", "values": [+ {"key": "id", "type": "cohort", "value": 6, "negation": false}+ ]}+ ]}+ ]+ }+ }),+ );++ let dependencies = cohort+ .extract_dependencies()+ .expect("Flat and nested cohort filters should parse");+ let expected_dependencies: HashSet<CohortId> = [5, 6].iter().cloned().collect();+ assert_eq!(dependencies, expected_dependencies);+ }++ #[test]+ fn test_evaluate_dynamic_cohorts_with_flat_person_property_filters() {+ // Regression test: flags referencing a cohort with this shape returned+ // `cohort_filters_parsing_error`, even though the cohort itself worked fine in+ // the rest of the product (the Python query layer accepts it).+ let cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {+ "type": "OR",+ "values": [+ {"key": "tenantId", "type": "person", "value": ["A", "B", "C"], "operator": "exact", "negation": false}+ ]+ }+ }),+ );++ let cohorts = vec![cohort];+ let static_cohort_matches = HashMap::new();++ let test_cases = [+ (json!("B"), true), // listed tenantId should match+ (json!("Z"), false), // unlisted tenantId should NOT match+ ];++ for (tenant_id, expected) in test_cases {+ let target_properties = HashMap::from([("tenantId".to_string(), tenant_id.clone())]);+ let result = evaluate_dynamic_cohorts(+ 1,+ &target_properties,+ &cohorts,+ &static_cohort_matches,+ Tz::UTC,+ )+ .unwrap();+ assert_eq!(+ result, expected,+ "tenantId={tenant_id} should evaluate to {expected}"+ );+ }+ }++ #[test]+ fn test_evaluate_dynamic_cohorts_with_nested_groups() {+ // Matches people with: email contains @example.com AND (plan = pro OR age > 30)+ let cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {+ "type": "AND",+ "values": [{+ "type": "AND",+ "values": [+ {"key": "email", "type": "person", "value": "@example.com", "operator": "icontains"},+ {"type": "OR", "values": [+ {"key": "plan", "type": "person", "value": "pro", "operator": "exact"},+ {"key": "age", "type": "person", "value": 30, "operator": "gt"}+ ]}+ ]+ }]+ }+ }),+ );++ let cohorts = vec![cohort];+ let static_cohort_matches = HashMap::new();++ let test_cases = [+ (json!("[email protected]"), json!("pro"), json!(25), true),+ (json!("[email protected]"), json!("free"), json!(40), true),+ (json!("[email protected]"), json!("free"), json!(25), false),+ (json!("[email protected]"), json!("pro"), json!(40), false),+ ];++ for (email, plan, age, expected) in test_cases {+ let target_properties = HashMap::from([+ ("email".to_string(), email.clone()),+ ("plan".to_string(), plan.clone()),+ ("age".to_string(), age.clone()),+ ]);+ let result = evaluate_dynamic_cohorts(+ 1,+ &target_properties,+ &cohorts,+ &static_cohort_matches,+ Tz::UTC,+ )+ .unwrap();+ assert_eq!(+ result, expected,+ "email={email}, plan={plan}, age={age} should evaluate to {expected}"+ );+ }+ }++ #[test]+ fn test_evaluate_dynamic_cohorts_with_negated_cohort_reference_in_or_group() {+ // A "not in cohort" criterion is saved as a cohort reference with+ // `negation: true` and no operator — the negation must be respected inside OR+ // groups, not just AND groups.+ let inner_cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {"type": "OR", "values": [{"type": "OR", "values": [+ {"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"}+ ]}]}+ }),+ );+ let outer_cohort = create_dynamic_cohort_with_filters(+ 2,+ json!({+ "properties": {"type": "OR", "values": [{"type": "OR", "values": [+ {"key": "id", "type": "cohort", "value": 1, "negation": true}+ ]}]}+ }),+ );++ let cohorts = vec![inner_cohort, outer_cohort];+ let static_cohort_matches = HashMap::new();++ let test_cases = [+ (json!("[email protected]"), false), // in the negated cohort -> should NOT match+ (json!("[email protected]"), true), // outside the negated cohort -> should match+ ];++ for (email, expected) in test_cases {+ let target_properties = HashMap::from([("email".to_string(), email.clone())]);+ let result = evaluate_dynamic_cohorts(+ 2,+ &target_properties,+ &cohorts,+ &static_cohort_matches,+ Tz::UTC,+ )+ .unwrap();+ assert_eq!(+ result, expected,+ "email={email} should evaluate to {expected}"+ );+ }+ }++ /// Wraps `leaf` in `levels` nested AND groups: {type: AND, values: [{type: AND, values: [... leaf]}]}.+ fn nest_in_groups(leaf: serde_json::Value, levels: usize) -> serde_json::Value {+ let mut current = leaf;+ for _ in 0..levels {+ current = json!({"type": "AND", "values": [current]});+ }+ current+ }++ #[test]+ fn test_deeply_nested_cohort_filters_error_instead_of_overflowing() {+ // A filter tree deeper than MAX_COHORT_FILTER_DEPTH must fail with a parsing+ // error rather than recursing until the worker thread's stack overflows.+ let leaf = json!({"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"});+ let deep_properties = nest_in_groups(leaf, MAX_COHORT_FILTER_DEPTH + 10);+ let cohort =+ create_dynamic_cohort_with_filters(1, json!({ "properties": deep_properties }));++ // Dependency extraction bails out with the parsing error.+ assert!(matches!(+ cohort.extract_dependencies(),+ Err(FlagError::CohortFiltersParsingError)+ ));++ // Evaluation surfaces the same error instead of overflowing the stack.+ let cohorts = vec![cohort];+ let target_properties = HashMap::from([("email".to_string(), json!("[email protected]"))]);+ assert!(matches!(+ evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC),+ Err(FlagError::CohortFiltersParsingError)+ ));+ }++ #[test]+ fn test_cohort_filters_at_max_depth_still_evaluate() {+ // The deepest tree the guard allows must still evaluate, not error — pins the+ // off-by-one so a future tweak to MAX_COHORT_FILTER_DEPTH can't silently start+ // rejecting legitimate cohorts. InnerCohortProperty::evaluate consumes the+ // outermost group at depth 0, so the leaf sits at depth N-1; N = MAX + 1 is the+ // largest tree whose leaf lands exactly on the depth limit.+ let leaf = json!({"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"});+ let at_limit = nest_in_groups(leaf, MAX_COHORT_FILTER_DEPTH + 1);+ let cohort = create_dynamic_cohort_with_filters(1, json!({ "properties": at_limit }));++ cohort+ .extract_dependencies()+ .expect("a tree at the depth limit should parse");++ let cohorts = vec![cohort];+ let target_properties = HashMap::from([("email".to_string(), json!("[email protected]"))]);+ assert!(matches!(+ evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC),+ Ok(true)+ ));+ }++ #[test]+ fn test_evaluate_dynamic_cohorts_with_legacy_property_group_type() {+ // The cohort UI emits "property" as the inner group type — a legacy alias that+ // must behave like "AND".+ let cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {+ "type": "OR",+ "values": [{+ "type": "property",+ "values": [+ {"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"},+ {"key": "plan", "type": "person", "value": "pro", "operator": "exact"}+ ]+ }]+ }+ }),+ );++ let cohorts = vec![cohort];+ let static_cohort_matches = HashMap::new();++ let test_cases = [+ (json!("[email protected]"), json!("pro"), true), // both match -> AND true+ (json!("[email protected]"), json!("free"), false), // plan fails -> AND false+ (json!("[email protected]"), json!("pro"), false), // email fails -> AND false+ ];++ for (email, plan, expected) in test_cases {+ let target_properties = HashMap::from([+ ("email".to_string(), email.clone()),+ ("plan".to_string(), plan.clone()),+ ]);+ let result = evaluate_dynamic_cohorts(+ 1,+ &target_properties,+ &cohorts,+ &static_cohort_matches,+ Tz::UTC,+ )+ .unwrap();+ assert_eq!(+ result, expected,+ "email={email}, plan={plan} should evaluate to {expected}"+ );+ }+ }++ #[test]+ fn test_evaluate_dynamic_cohorts_with_unknown_group_type_errors() {+ // An unrecognized group type must fail loudly rather than silently matching or+ // not matching.+ let cohort = create_dynamic_cohort_with_filters(+ 1,+ json!({+ "properties": {+ "type": "OR",+ "values": [{+ "type": "XOR",+ "values": [+ {"key": "email", "type": "person", "value": "@posthog.com", "operator": "icontains"}+ ]+ }]+ }+ }),+ );++ let cohorts = vec![cohort];+ let target_properties = HashMap::from([("email".to_string(), json!("[email protected]"))]);+ assert!(matches!(+ evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC),+ Err(FlagError::CohortFiltersParsingError)+ ));+ }}