Skip to content

: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.

67.2 KB
Raw Blame
use once_cell::sync::Lazy;
use serde_json::Value;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::{Mutex, RwLock};

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, CohortValuesItem, InnerCohortProperty, MembershipStampPolicy,
};
use crate::database::get_connection_with_metrics;
use crate::metrics::consts::{
    COHORT_MALFORMED_FILTER_COUNTER, COHORT_UNSUPPORTED_FILTER_COUNTER,
    FLAG_COHORT_STAMP_POLICY_DIVERGENCE_COUNTER,
};
use crate::properties::property_matching::{match_property, PropertyMatchingContext};
use crate::properties::property_models::OperatorType;
use crate::utils::graph_utils::{DependencyGraph, DependencyProvider, DependencyType};
use crate::{api::errors::FlagError, properties::property_models::PropertyFilter};
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;

/// Cohorts already warned about by `record_malformed_cohort_filter`, so a persistently
/// malformed cohort on the hot `/flags` path logs its identifying context once per
/// process lifetime instead of flooding logs on every request.
static WARNED_MALFORMED_COHORTS: Lazy<Mutex<HashSet<(TeamId, CohortId)>>> =
    Lazy::new(|| Mutex::new(HashSet::new()));

/// Logs and counts a cohort whose filters failed dependency extraction or evaluation
/// with `CohortFiltersParsingError` (malformed leaf, excessive nesting, or other
/// structural error surfaced by `traverse_filters`/`InnerCohortProperty::evaluate`).
///
/// The counter is unlabeled (cardinality), so this log is the only place the cohort
/// and team ids are recorded; it's deduped per cohort so it stays visible at `warn`
/// without flooding logs when the same cohort keeps failing.
fn record_malformed_cohort_filter(cohort_id: CohortId, team_id: TeamId, phase: &str) {
    common_metrics::inc(COHORT_MALFORMED_FILTER_COUNTER, &[], 1);

    let mut warned = WARNED_MALFORMED_COHORTS.lock().unwrap();
    if warned.insert((team_id, cohort_id)) {
        tracing::warn!(
            cohort_id,
            team_id,
            "Cohort filters contain a malformed or unparsable leaf; failing {phase}"
        );
    }
}

/// Cohorts already warned about by `record_stamp_policy_divergence`, deduped as
/// `WARNED_MALFORMED_COHORTS` above is. An `RwLock` because divergence is common rather
/// than exceptional, so past the first warn every request takes only the read side.
static WARNED_DIVERGENT_COHORTS: Lazy<RwLock<HashSet<(TeamId, CohortId)>>> =
    Lazy::new(|| RwLock::new(HashSet::new()));

/// Counts and logs a cohort the two membership stamp policies route differently. The
/// counter carries no ids (cardinality), so the deduped log is the only place to learn
/// which cohort diverged.
pub(crate) fn record_stamp_policy_divergence(
    cohort: &Cohort,
    active_policy: MembershipStampPolicy,
) {
    let Some(divergence) = MembershipStampPolicy::divergence(cohort) else {
        return;
    };
    common_metrics::inc(
        FLAG_COHORT_STAMP_POLICY_DIVERGENCE_COUNTER,
        &[
            ("direction".to_string(), divergence.as_label().to_string()),
            (
                "active_policy".to_string(),
                active_policy.as_label().to_string(),
            ),
        ],
        1,
    );

    if WARNED_DIVERGENT_COHORTS
        .read()
        .unwrap()
        .contains(&(cohort.team_id, cohort.id))
    {
        return;
    }
    let mut warned = WARNED_DIVERGENT_COHORTS.write().unwrap();
    if warned.insert((cohort.team_id, cohort.id)) {
        tracing::warn!(
            cohort_id = cohort.id,
            team_id = cohort.team_id,
            direction = divergence.as_label(),
            active_policy = active_policy.as_label(),
            "Membership stamp policies disagree on this cohort's realtime routing; the REALTIME_COHORT_MEMBERSHIP_STAMP_POLICY flip would change it"
        );
    }
}

/// 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,
    c.query, c.version, c.pending_version, c.count, c.is_calculating,
    c.is_static, c.errors_calculating, c.groups, c.created_by_id,
    c.cohort_type, c.last_backfill_person_properties_at, c.last_backfill_events_at,
    c.condition_type, c.last_realtime_cohort_calculation_at
"#;

impl Cohort {
    /// Returns all cohorts for a given team
    pub async fn list_from_pg(
        client: PostgresReader,
        team_id: TeamId,
    ) -> Result<Vec<Cohort>, CohortFetchError> {
        let mut conn = get_connection_with_metrics(&client, "non_persons_reader", "fetch_cohorts")
            .await
            .map_err(|e| {
                tracing::error!(
                    "Failed to get database connection for team {}: {}",
                    team_id,
                    e
                );
                CohortFetchError::DatabaseUnavailable
            })?;

        let query = format!(
            "SELECT {COHORT_COLUMNS} FROM posthog_cohort AS c \
             JOIN posthog_team AS t ON (c.team_id = t.id) \
             WHERE t.id = $1 AND c.deleted = false"
        );
        let cohorts = sqlx::query_as::<_, Cohort>(&query)
            .bind(team_id)
            .fetch_all(&mut *conn)
            .await
            .map_err(|e| {
                tracing::error!(
                    "Failed to fetch cohorts from database for team {}: {}",
                    team_id,
                    e
                );
                CohortFetchError::QueryFailed(format!("Database query error: {e}"))
            })?;

        Ok(cohorts)
    }

    /// Fetch cohorts by a set of IDs, filtered to non-deleted cohorts for the given team.
    /// Used by the cache builder for BFS cohort dependency resolution.
    pub async fn list_by_ids_from_pg(
        client: &PostgresReader,
        team_id: TeamId,
        ids: &[CohortId],
    ) -> Result<Vec<Cohort>, CohortFetchError> {
        if ids.is_empty() {
            return Ok(vec![]);
        }

        let mut conn =
            get_connection_with_metrics(client, "non_persons_reader", "fetch_cohorts_for_cache")
                .await
                .map_err(|e| {
                    tracing::error!(
                        "Failed to get database connection for team {}: {}",
                        team_id,
                        e
                    );
                    CohortFetchError::DatabaseUnavailable
                })?;

        let query = format!(
            "SELECT {COHORT_COLUMNS} FROM posthog_cohort AS c \
             WHERE c.id = ANY($1) AND c.deleted = false AND c.team_id = $2"
        );

        let cohorts = sqlx::query_as::<_, Cohort>(&query)
            .bind(ids)
            .bind(team_id)
            .fetch_all(&mut *conn)
            .await
            .map_err(|e| {
                tracing::error!(
                    "Failed to fetch cohorts from database for team {}: {}",
                    team_id,
                    e
                );
                CohortFetchError::QueryFailed(format!("Database query error: {e}"))
            })?;

        Ok(cohorts)
    }

    /// Extracts dependent CohortIds from the cohort's filters
    ///
    /// # Returns
    /// * `HashSet<CohortId>` - A set of dependent cohort IDs
    /// * `FlagError` - If there is an error parsing the filters
    pub fn extract_dependencies(&self) -> Result<HashSet<CohortId>, FlagError> {
        // NB: static cohorts have no filters, so they have no dependencies
        // BUT, sometimes instead of having `None` or `{}`, they have an object like this
        // `{"properties": {}}`
        // So we need to explicitly check for this case and just return an empty set rather than trying to parse the filters at all
        if self.is_static {
            return Ok(HashSet::new());
        }

        let filters = match &self.filters {
            Some(filters) => filters,
            None => return Ok(HashSet::new()), // Return empty set if no filters
        };

        let cohort_property: CohortProperty =
            serde_json::from_value(filters.clone()).map_err(|e| {
                tracing::error!(
                    "Failed to parse filters for cohort {} (team {}): {}",
                    self.id,
                    self.team_id,
                    e
                );
                FlagError::CohortFiltersParsingError
            })?;

        let mut dependencies = HashSet::new();
        Self::traverse_filters(&cohort_property.properties, &mut dependencies).inspect_err(
            |_| record_malformed_cohort_filter(self.id, self.team_id, "dependency extraction"),
        )?;
        Ok(dependencies)
    }

    /// Recursively traverses the filter tree to find cohort dependencies
    ///
    /// Example filter tree structure:
    /// ```json
    /// {
    ///   "properties": {
    ///     "type": "OR",
    ///     "values": [
    ///       {
    ///         "type": "OR",
    ///         "values": [
    ///           {
    ///             "key": "id",
    ///             "value": 123,
    ///             "type": "cohort",
    ///             "operator": "exact"
    ///           },
    ///           {
    ///             "key": "email",
    ///             "value": "@posthog.com",
    ///             "type": "person",
    ///             "operator": "icontains"
    ///           }
    ///         ]
    ///       }
    ///     ]
    ///   }
    /// }
    /// ```
    fn traverse_filters(
        inner: &InnerCohortProperty,
        dependencies: &mut HashSet<CohortId>,
    ) -> Result<(), FlagError> {
        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);
                    } else {
                        return Err(FlagError::CohortFiltersParsingError);
                    }
                }
            }
            // A known filter type (e.g. `cohort`) that's otherwise malformed, such as a
            // cohort reference missing `key`. Fail loud rather than silently dropping
            // what may be a real dependency.
            CohortValuesItem::MalformedKnownType(_) => {
                return Err(FlagError::CohortFiltersParsingError);
            }
            // No cohort dependency to contribute; count it and continue.
            CohortValuesItem::Unsupported(_) => {
                common_metrics::inc(COHORT_UNSUPPORTED_FILTER_COUNTER, &[], 1);
            }
        }
        Ok(())
    }
}

impl InnerCohortProperty {
    /// Evaluates a cohort property based on its type (AND/OR) and values.
    ///
    /// This function recursively evaluates the cohort property tree structure, handling both
    /// property matches and nested cohort membership checks.
    pub fn evaluate(
        &self,
        target_properties: &HashMap<String, Value>,
        cohort_matches: &HashMap<CohortId, bool>,
        matching_context: PropertyMatchingContext,
    ) -> Result<bool, FlagError> {
        match self.prop_type {
            CohortPropertyType::OR => {
                for item in &self.values {
                    if evaluate_cohort_item(
                        item,
                        target_properties,
                        cohort_matches,
                        0,
                        matching_context,
                    )? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            CohortPropertyType::AND => {
                for item in &self.values {
                    if !evaluate_cohort_item(
                        item,
                        target_properties,
                        cohort_matches,
                        0,
                        matching_context,
                    )? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
        }
    }
}

/// 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,
    matching_context: PropertyMatchingContext,
) -> 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,
            matching_context,
        ),
        CohortValuesItem::Filter(filter) => {
            evaluate_cohort_filter(filter, target_properties, cohort_matches, matching_context)
        }
        // A known filter type that's otherwise malformed; fail loud rather than
        // silently resolving to non-match (see `traverse_item`).
        CohortValuesItem::MalformedKnownType(_) => Err(FlagError::CohortFiltersParsingError),
        // Non-match, so sibling leaves decide membership via their AND/OR combination.
        CohortValuesItem::Unsupported(_) => Ok(false),
    }
}

/// 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>,
    matching_context: PropertyMatchingContext,
) -> 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,
            matching_context,
        ))
    }
}

/// Evaluates a set of cohort values against target properties.
///
/// 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,
    matching_context: PropertyMatchingContext,
) -> Result<bool, FlagError> {
    match values.prop_type.as_str() {
        "OR" => {
            for item in &values.values {
                if evaluate_cohort_item(
                    item,
                    target_properties,
                    cohort_matches,
                    depth + 1,
                    matching_context,
                )? {
                    return Ok(true);
                }
            }
            Ok(false)
        }
        "AND" | "property" => {
            for item in &values.values {
                if !evaluate_cohort_item(
                    item,
                    target_properties,
                    cohort_matches,
                    depth + 1,
                    matching_context,
                )? {
                    return Ok(false);
                }
            }
            Ok(true)
        }
        _ => Err(FlagError::CohortFiltersParsingError),
    }
}

/// Evaluates a property filter against target properties, applying negation if specified.
///
/// Cohort filters use the `negation` field to invert results, unlike flag filters
/// which use specific operators like `NotIContains`.
fn evaluate_property_with_negation(
    filter: &PropertyFilter,
    target_properties: &HashMap<String, Value>,
    matching_context: PropertyMatchingContext,
) -> bool {
    let property_result =
        match_property(filter, target_properties, false, matching_context).unwrap_or(false);

    // Apply negation if specified
    if filter.negation.unwrap_or(false) {
        !property_result
    } else {
        property_result
    }
}

/// Evaluates a single cohort against target properties and existing evaluation results.
/// Returns true if the cohort matches, false otherwise.
fn evaluate_single_cohort(
    cohort: &Cohort,
    target_properties: &HashMap<String, Value>,
    evaluation_results: &HashMap<CohortId, bool>,
    matching_context: PropertyMatchingContext,
) -> Result<bool, FlagError> {
    // Get the filters for this cohort
    let filters = match &cohort.filters {
        Some(filters) => filters,
        None => return Ok(false),
    };

    // Parse and evaluate using the hierarchical structure
    let cohort_property: CohortProperty = match serde_json::from_value(filters.clone()) {
        Ok(prop) => prop,
        Err(_) => return Ok(false),
    };

    // Use our evaluation method that respects OR/AND structure
    cohort_property
        .properties
        .evaluate(target_properties, evaluation_results, matching_context)
        .inspect_err(|_| record_malformed_cohort_filter(cohort.id, cohort.team_id, "evaluation"))
}

pub fn evaluate_dynamic_cohorts(
    initial_cohort_id: CohortId,
    target_properties: &HashMap<String, Value>,
    cohorts: &[Cohort],
    static_cohort_matches: &HashMap<CohortId, bool>,
    matching_context: PropertyMatchingContext,
) -> Result<bool, FlagError> {
    // First check if this is a static cohort
    let initial_cohort = cohorts
        .iter()
        .find(|c| c.id == initial_cohort_id)
        .ok_or_else(|| {
            FlagError::DependencyNotFound(DependencyType::Cohort, initial_cohort_id.into())
        })?
        .clone();

    // If it's static, we don't need to evaluate dependencies
    if initial_cohort.is_static {
        return Ok(false); // Static cohorts are handled by evaluate_static_cohorts
    }

    // Build the dependency graph
    let graph = DependencyGraph::new(initial_cohort, cohorts)?;

    // Use for_each_dependencies_first to evaluate each cohort in the correct order
    let results = graph.for_each_dependencies_first(|cohort, results, result| {
        // If this is a static cohort dependency, use the cached result
        if cohort.is_static {
            let cached_result = static_cohort_matches
                .get(&cohort.id)
                .copied()
                .unwrap_or(false);
            *result = cached_result;
            return Ok(());
        }

        *result = evaluate_single_cohort(cohort, target_properties, results, matching_context)?;
        Ok(())
    })?;

    // Return the evaluation result for the initial cohort
    results.get(&initial_cohort_id).copied().ok_or_else(|| {
        FlagError::DependencyNotFound(DependencyType::Cohort, initial_cohort_id.into())
    })
}

/// Applies cohort membership logic for a set of cohort filters.
///
/// This function evaluates whether a person matches a set of cohort filters by:
/// 1. Checking each filter's cohort ID
/// 2. Looking up the match result in the cohort_matches map
/// 3. Applying the appropriate operator (IN/NOT_IN)
pub fn apply_cohort_membership_logic<F: Borrow<PropertyFilter>>(
    cohort_filters: &[F],
    cohort_matches: &HashMap<CohortId, bool>,
) -> Result<bool, FlagError> {
    for filter in cohort_filters {
        let filter = filter.borrow();
        let cohort_id = filter
            .get_cohort_id()
            .ok_or(FlagError::CohortFiltersParsingError)?;
        let matches = cohort_matches.get(&cohort_id).copied().unwrap_or(false);

        // `cohort_operator_is_supported` holds the supported set, so an explanation built from
        // that helper cannot disagree with the verdict here. An absent operator reads as `in`.
        let membership_match = filter.cohort_operator_is_supported()
            && match filter.operator {
                Some(OperatorType::NotIn) => !matches,
                _ => matches,
            };

        // If any filter does not match, return false early
        if !membership_match {
            return Ok(false);
        }
    }
    // All filters matched
    Ok(true)
}

// Implement DependencyProvider for Cohort
impl DependencyProvider for Cohort {
    type Id = CohortId;
    type Error = FlagError;

    fn get_id(&self) -> Self::Id {
        self.id
    }

    fn extract_dependencies(&self) -> Result<HashSet<Self::Id>, Self::Error> {
        self.extract_dependencies()
    }

    fn dependency_type() -> DependencyType {
        DependencyType::Cohort
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cohorts::cohort_models::CohortType;
    use crate::utils::test_utils::TestContext;
    use chrono_tz::Tz;
    use serde_json::json;

    fn evaluate_dynamic_cohorts(
        initial_cohort_id: CohortId,
        target_properties: &HashMap<String, Value>,
        cohorts: &[Cohort],
        static_cohort_matches: &HashMap<CohortId, bool>,
        team_timezone: Tz,
    ) -> Result<bool, FlagError> {
        super::evaluate_dynamic_cohorts(
            initial_cohort_id,
            target_properties,
            cohorts,
            static_cohort_matches,
            PropertyMatchingContext::new(team_timezone, false),
        )
    }

    #[tokio::test]
    async fn test_list_from_pg() {
        let context = TestContext::new(None).await;
        let team = context
            .insert_new_team(None)
            .await
            .expect("Failed to insert team");

        // Insert multiple cohorts for the team
        context
            .insert_cohort(
                team.id,
                Some("Cohort 1".to_string()),
                json!({"properties": {"type": "AND", "values": [{"type": "property", "values": [{"key": "age", "type": "person", "value": [30], "negation": false, "operator": "gt"}]}]}}),
                false,
            )
            .await
            .expect("Failed to insert cohort1");

        context
            .insert_cohort(
                team.id,
                Some("Cohort 2".to_string()),
                json!({"properties": {"type": "OR", "values": [{"type": "property", "values": [{"key": "country", "type": "person", "value": ["USA"], "negation": false, "operator": "exact"}]}]}}),
                false,
            )
            .await
            .expect("Failed to insert cohort2");

        let cohorts = Cohort::list_from_pg(context.non_persons_reader, team.id)
            .await
            .expect("Failed to list cohorts");

        assert_eq!(cohorts.len(), 2);
        let names: HashSet<String> = cohorts.into_iter().filter_map(|c| c.name).collect();
        assert!(names.contains("Cohort 1"));
        assert!(names.contains("Cohort 2"));
    }

    #[tokio::test]
    async fn test_extract_dependencies() {
        let context = TestContext::new(None).await;
        let team = context
            .insert_new_team(None)
            .await
            .expect("Failed to insert team");

        // Insert a single cohort that is dependent on another cohort
        let dependent_cohort = context
            .insert_cohort(
                team.id,
                Some("Dependent Cohort".to_string()),
                json!({"properties": {"type": "OR", "values": [{"type": "OR", "values": [{"key": "$browser", "type": "person", "value": ["Safari"], "negation": false, "operator": "exact"}]}]}}),
                false,
            )
            .await
            .expect("Failed to insert dependent_cohort");

        // Insert main cohort with a single dependency
        let main_cohort = context
            .insert_cohort(
                team.id,
                Some("Main Cohort".to_string()),
                json!({"properties": {"type": "OR", "values": [{"type": "OR", "values": [{"key": "id", "type": "cohort", "value": dependent_cohort.id, "negation": false}]}]}}),
                false,
            )
            .await
            .expect("Failed to insert main_cohort");

        let cohorts = Cohort::list_from_pg(context.non_persons_reader.clone(), team.id)
            .await
            .expect("Failed to fetch cohorts");

        let fetched_main_cohort = cohorts
            .into_iter()
            .find(|c| c.id == main_cohort.id)
            .expect("Failed to find main cohort");

        let dependencies = fetched_main_cohort.extract_dependencies().unwrap();
        let expected_dependencies: HashSet<CohortId> =
            [dependent_cohort.id].iter().cloned().collect();

        assert_eq!(dependencies, expected_dependencies);
    }

    #[tokio::test]
    async fn test_static_cohort_with_malformed_filters() {
        // Test that static cohorts with malformed filters don't cause parsing errors
        let static_cohort_with_bad_filters = Cohort {
            id: 999,
            name: Some("Static Cohort with Bad Filters".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            // This is the problematic case - static cohorts sometimes have {"properties": {}}
            // instead of null, which would fail JSON parsing if we tried to parse it
            filters: Some(json!({"properties": {}})),
            query: None,
            version: None,
            pending_version: None,
            count: Some(100),
            is_calculating: false,
            is_static: true, // This is the key - it's static
            errors_calculating: 0,
            groups: json!({}),
            created_by_id: None,
            cohort_type: None,
            last_backfill_person_properties_at: None,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        // This should not fail even though the filters are malformed
        let dependencies = static_cohort_with_bad_filters
            .extract_dependencies()
            .unwrap();
        assert_eq!(dependencies, HashSet::new());

        // Test another malformed case - empty object
        let static_cohort_empty_filters = Cohort {
            id: 998,
            name: Some("Static Cohort Empty".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: Some(json!({})), // Empty object
            query: None,
            version: None,
            pending_version: None,
            count: Some(50),
            is_calculating: false,
            is_static: true,
            errors_calculating: 0,
            groups: json!({}),
            created_by_id: None,
            cohort_type: None,
            last_backfill_person_properties_at: None,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        let dependencies = static_cohort_empty_filters.extract_dependencies().unwrap();
        assert_eq!(dependencies, HashSet::new());

        // Verify that a dynamic cohort with the same malformed filters WOULD fail
        let dynamic_cohort_with_bad_filters = Cohort {
            id: 997,
            name: Some("Dynamic Cohort with Bad Filters".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: Some(json!({"properties": {}})), // Same malformed filters
            query: None,
            version: None,
            pending_version: None,
            count: None,
            is_calculating: false,
            is_static: false, // This is dynamic, so it should fail
            errors_calculating: 0,
            groups: json!({}),
            created_by_id: None,
            cohort_type: None,
            last_backfill_person_properties_at: None,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        // This should fail because it's dynamic and the filters are malformed
        let result = dynamic_cohort_with_bad_filters.extract_dependencies();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            FlagError::CohortFiltersParsingError
        ));
    }

    fn create_test_cohort_instance(id: CohortId, depends_on: Option<CohortId>) -> Cohort {
        Cohort {
            id,
            name: Some(format!("Cohort {id}")),
            description: None,
            team_id: 1,
            deleted: false,
            filters: depends_on.map(|dep_id| {
                json!({
                    "properties": {
                        "type": "OR",
                        "values": [{
                            "type": "OR",
                            "values": [{
                                "key": "id",
                                "type": "cohort",
                                "value": dep_id,
                                "negation": false
                            }]
                        }]
                    }
                })
            }),
            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,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        }
    }

    #[test]
    fn test_build_cohort_dependency_graph_cycle_detection() {
        // Create four cohorts that form a cycle: 2 -> 3 -> 4 -> 2
        // Cohort 1 is not part of the cycle but depends on 2
        let cohort_1 = create_test_cohort_instance(1, Some(2)); // 1 depends on 2
        let cohort_2 = create_test_cohort_instance(2, Some(3)); // 2 depends on 3
        let cohort_3 = create_test_cohort_instance(3, Some(4)); // 3 depends on 4
        let cohort_4 = create_test_cohort_instance(4, Some(2)); // 4 depends on 2 (starts the cycle)

        let cohorts = vec![cohort_1.clone(), cohort_2, cohort_3, cohort_4];

        // Try to build the graph starting from cohort 1
        let result = DependencyGraph::new(cohort_1, &cohorts);

        // Verify we got a cycle error
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(
            err,
            FlagError::DependencyCycle(DependencyType::Cohort, 4)
        ));
    }

    #[test]
    fn test_evaluate_dynamic_cohorts_with_static_cohort_dependency() {
        // Create a static cohort (cohort 10)
        let static_cohort = Cohort {
            id: 10,
            name: Some("Static Cohort".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: None,
            query: None,
            version: None,
            pending_version: None,
            count: Some(100),
            is_calculating: false,
            is_static: true,
            errors_calculating: 0,
            groups: json!({}),
            created_by_id: None,
            cohort_type: None,
            last_backfill_person_properties_at: None,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        // Create a dynamic cohort (cohort 20) that depends on the static cohort
        let dynamic_cohort = Cohort {
            id: 20,
            name: Some("Dynamic Cohort with Static Dependency".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: Some(json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [{
                            "key": "id",
                            "type": "cohort",
                            "value": 10,
                            "negation": false
                        }]
                    }]
                }
            })),
            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,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        let cohorts = vec![static_cohort, dynamic_cohort];
        let target_properties = HashMap::new();

        // Test case 1: Static cohort is in cache and matches
        let mut static_cohort_matches = HashMap::new();
        static_cohort_matches.insert(10, true);

        let result = evaluate_dynamic_cohorts(
            20,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            result,
            "Dynamic cohort should match when its static cohort dependency matches"
        );

        // Test case 2: Static cohort is in cache but doesn't match
        let mut static_cohort_matches = HashMap::new();
        static_cohort_matches.insert(10, false);

        let result = evaluate_dynamic_cohorts(
            20,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            !result,
            "Dynamic cohort should not match when its static cohort dependency doesn't match"
        );

        // Test case 3: Static cohort is not in cache (defaults to false)
        let static_cohort_matches = HashMap::new();

        let result = evaluate_dynamic_cohorts(
            20,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            !result,
            "Dynamic cohort should not match when static cohort is not in cache"
        );
    }

    #[test]
    fn test_dynamic_cohort_datetime_filter_uses_team_timezone() {
        // A dynamic cohort with a naive datetime person-property filter must be
        // evaluated in the team timezone so Rust flag membership agrees with the
        // HogQL/ClickHouse cohort path. For a Pacific team, the filter "2024-06-01"
        // (is_date_after) resolves to 2024-06-01 07:00 UTC (PDT, UTC-7 in June).
        let cohort = Cohort {
            id: 30,
            name: Some("Datetime Cohort".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: Some(json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [{
                            "key": "joined_at",
                            "type": "person",
                            "value": "2024-06-01",
                            "negation": false,
                            "operator": "is_date_after"
                        }]
                    }]
                }
            })),
            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,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        let cohorts = vec![cohort];
        let static_matches = HashMap::new();

        // Person joined 03:00 UTC on June 1 — after UTC midnight, but before
        // Pacific midnight (07:00 UTC). This is the offset window where the two
        // timezone interpretations disagree.
        let person = HashMap::from([("joined_at".to_string(), json!("2024-06-01T03:00:00Z"))]);

        // Pacific: the person is not strictly after the local-midnight boundary,
        // so they are not a member — matching HogQL cohort membership.
        assert!(!evaluate_dynamic_cohorts(
            30,
            &person,
            &cohorts,
            &static_matches,
            Tz::America__Los_Angeles
        )
        .unwrap());

        // UTC (the pre-fix interpretation) would include them, proving the team
        // timezone actually changes the cohort decision in this window.
        assert!(evaluate_dynamic_cohorts(30, &person, &cohorts, &static_matches, Tz::UTC).unwrap());
    }

    #[test]
    fn test_evaluate_dynamic_cohorts_with_negation_filters() {
        // Create a cohort with filters that include negation
        // This cohort should match people with emails ending in @example.com
        // BUT exclude those containing "excluded.user"
        let cohort_with_negation = Cohort {
            id: 1,
            name: Some("Cohort with Negation".to_string()),
            description: None,
            team_id: 1,
            deleted: false,
            filters: Some(json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "AND",
                        "values": [
                            {
                                "key": "email",
                                "type": "person",
                                "value": "^.*@example.com$",
                                "negation": false,
                                "operator": "regex"
                            },
                            {
                                "key": "email",
                                "type": "person",
                                "value": "excluded.user",
                                "negation": true,  // This should be inverted
                                "operator": "icontains"
                            }
                        ]
                    }]
                }
            })),
            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,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        };

        let cohorts = vec![cohort_with_negation];
        let static_cohort_matches = HashMap::new();

        // Test case 1: User with @example.com email but NOT excluded
        // Should match because: regex matches AND (icontains doesn't match -> negated to true)
        let mut target_properties = HashMap::new();
        target_properties.insert("email".to_string(), json!("[email protected]"));

        let result = evaluate_dynamic_cohorts(
            1,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            result,
            "User with @example.com email should match when not excluded"
        );

        // Test case 2: User with @example.com email but IS excluded
        // Should NOT match because: regex matches BUT (icontains matches -> negated to false)
        target_properties.insert("email".to_string(), json!("[email protected]"));

        let result = evaluate_dynamic_cohorts(
            1,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            !result,
            "User with @example.com email should NOT match when excluded"
        );

        // Test case 3: User without @example.com email
        // Should NOT match because: regex doesn't match (regardless of negation)
        target_properties.insert("email".to_string(), json!("[email protected]"));

        let result = evaluate_dynamic_cohorts(
            1,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(!result, "User without @example.com email should NOT match");

        // Test case 4: User with excluded term but wrong domain
        // Should NOT match because: regex doesn't match (regardless of negation)
        target_properties.insert("email".to_string(), json!("[email protected]"));

        let result = evaluate_dynamic_cohorts(
            1,
            &target_properties,
            &cohorts,
            &static_cohort_matches,
            Tz::UTC,
        )
        .unwrap();
        assert!(
            !result,
            "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,
            last_backfill_events_at: None,
            condition_type: None,
            last_realtime_cohort_calculation_at: None,
        }
    }

    #[test]
    fn test_dynamic_cohort_exact_matching_preserves_legacy_behavior_without_rollout() {
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [
                        {"key": "enabled", "type": "person", "value": false, "operator": "exact", "negation": true}
                    ]
                }
            }),
        );
        let target_properties = HashMap::from([("enabled".to_string(), json!("banana"))]);

        assert!(!super::evaluate_dynamic_cohorts(
            1,
            &target_properties,
            std::slice::from_ref(&cohort),
            &HashMap::new(),
            PropertyMatchingContext::new(Tz::UTC, false),
        )
        .unwrap());
        assert!(super::evaluate_dynamic_cohorts(
            1,
            &target_properties,
            &[cohort],
            &HashMap::new(),
            PropertyMatchingContext::new(Tz::UTC, true),
        )
        .unwrap());
    }

    #[test]
    fn test_record_stamp_policy_divergence_only_records_divergent_cohorts() {
        let behavioral = json!({
            "person_properties": false, "behavioral": true, "lifecycle": false, "cohorts": false
        });
        let routed = |id| {
            let mut cohort = create_dynamic_cohort_with_filters(id, json!({}));
            cohort.cohort_type = Some(CohortType::Realtime);
            cohort.condition_type = Some(behavioral.clone());
            cohort
        };

        let mut divergent = routed(987_654);
        divergent.last_backfill_person_properties_at = Some(chrono::Utc::now());
        record_stamp_policy_divergence(&divergent, MembershipStampPolicy::AnyBackfillStamp);
        record_stamp_policy_divergence(&divergent, MembershipStampPolicy::AnyBackfillStamp);

        let mut agreed = routed(987_655);
        agreed.last_backfill_events_at = Some(chrono::Utc::now());
        record_stamp_policy_divergence(&agreed, MembershipStampPolicy::AnyBackfillStamp);

        let warned = WARNED_DIVERGENT_COHORTS.read().unwrap();
        assert!(warned.contains(&(divergent.team_id, divergent.id)));
        assert!(!warned.contains(&(agreed.team_id, agreed.id)));
    }

    #[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)
        ));
    }

    /// A `behavioral` filter leaf — unresolvable from person/group properties, so it
    /// always parses to `CohortValuesItem::Unsupported`.
    fn behavioral_leaf_json() -> serde_json::Value {
        json!({"key": "$pageview", "type": "behavioral", "value": "performed_event",
               "negation": false, "event_type": "events", "time_value": "30", "time_interval": "day"})
    }

    #[test]
    fn test_extract_dependencies_tolerates_behavioral_leaf() {
        // A `behavioral` leaf can't be resolved from person properties here, but it
        // must not abort dependency extraction — the sibling cohort reference should
        // still be discovered. Before the fix this returned CohortFiltersParsingError.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "AND",
                    "values": [{
                        "type": "OR",
                        "values": [
                            behavioral_leaf_json(),
                            {"key": "id", "type": "cohort", "value": 7, "negation": false}
                        ]
                    }]
                }
            }),
        );

        let dependencies = cohort
            .extract_dependencies()
            .expect("a behavioral leaf must not fail cohort dependency extraction");
        assert_eq!(dependencies, [7].into_iter().collect());
    }

    #[test]
    fn test_evaluate_dynamic_cohorts_with_behavioral_leaf_in_or_group() {
        // In an OR group, an unresolvable behavioral leaf is a non-match, so an
        // evaluable person-property sibling still decides membership.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [
                            behavioral_leaf_json(),
                            {"key": "plan", "type": "person", "value": "pro", "operator": "exact"}
                        ]
                    }]
                }
            }),
        );

        let cohorts = vec![cohort];
        let static_cohort_matches = HashMap::new();

        let test_cases = [
            (json!("pro"), true),   // person-property sibling matches -> cohort matches
            (json!("free"), false), // sibling misses, behavioral leaf non-matches -> no match
        ];
        for (plan, expected) in test_cases {
            let target_properties = HashMap::from([("plan".to_string(), plan.clone())]);
            let result = evaluate_dynamic_cohorts(
                1,
                &target_properties,
                &cohorts,
                &static_cohort_matches,
                Tz::UTC,
            )
            .unwrap();
            assert_eq!(
                result, expected,
                "plan={plan} should evaluate to {expected}"
            );
        }
    }

    #[test]
    fn test_evaluate_dynamic_cohorts_with_behavioral_leaf_in_and_group_never_matches() {
        // In an AND group, the unresolvable behavioral leaf (treated as a non-match)
        // prevents the group from matching even when the person-property leaf is satisfied.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "AND",
                    "values": [{
                        "type": "AND",
                        "values": [
                            {"key": "plan", "type": "person", "value": "pro", "operator": "exact"},
                            behavioral_leaf_json()
                        ]
                    }]
                }
            }),
        );

        let cohorts = vec![cohort];
        let target_properties = HashMap::from([("plan".to_string(), json!("pro"))]);
        assert!(matches!(
            evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC),
            Ok(false)
        ));
    }

    #[test]
    fn test_purely_behavioral_cohort_resolves_to_non_match_with_no_dependencies() {
        // A cohort whose only criterion is behavioral has no evaluable leaves at all:
        // dependency extraction finds nothing, and evaluation resolves to non-match
        // instead of aborting the whole cohort parse.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [behavioral_leaf_json()]
                    }]
                }
            }),
        );

        let dependencies = cohort
            .extract_dependencies()
            .expect("a purely behavioral cohort must not fail dependency extraction");
        assert!(dependencies.is_empty());

        let cohorts = vec![cohort];
        assert!(matches!(
            evaluate_dynamic_cohorts(1, &HashMap::new(), &cohorts, &HashMap::new(), Tz::UTC),
            Ok(false)
        ));
    }

    #[test]
    fn test_cohort_filter_of_known_type_missing_required_field_still_errors() {
        // A `type: "cohort"` leaf missing the required `key` matches neither `Filter`
        // (no `key`) nor `Group` (no `values`). Its `type` is still a recognized
        // `PropertyType`, though, so this must surface as a parsing error rather than
        // silently falling through to `Unsupported` like a genuinely unrecognized type
        // (e.g. `behavioral`) does — losing a real cohort dependency edge silently
        // would be worse than the loud failure this replaces.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "AND",
                    "values": [{
                        "type": "OR",
                        "values": [{"type": "cohort", "value": 7, "negation": false}]
                    }]
                }
            }),
        );

        assert!(matches!(
            cohort.extract_dependencies(),
            Err(FlagError::CohortFiltersParsingError)
        ));

        let cohorts = vec![cohort];
        assert!(matches!(
            evaluate_dynamic_cohorts(1, &HashMap::new(), &cohorts, &HashMap::new(), Tz::UTC),
            Err(FlagError::CohortFiltersParsingError)
        ));
    }

    #[test]
    fn test_cohort_with_between_leaf_parses_and_evaluates() {
        // A person-property leaf with the `between` operator (valid in the cohort UI
        // and HogQL, but previously unknown to this evaluator) must parse as a real
        // filter and evaluate with inclusive bounds — not fall through to
        // MalformedKnownType and fail the whole cohort with CohortFiltersParsingError.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [{"key": "tenant_id", "type": "person", "value": [70000, 80000],
                                    "operator": "between", "negation": false}]
                    }]
                }
            }),
        );

        let dependencies = cohort
            .extract_dependencies()
            .expect("a between leaf must not fail cohort dependency extraction");
        assert!(dependencies.is_empty());

        let cohorts = vec![cohort];
        let test_cases = [
            (Some(json!(70000)), true),   // inclusive lower bound
            (Some(json!(80000)), true),   // inclusive upper bound
            (Some(json!(75000)), true),   // inside range
            (Some(json!("75000")), true), // string number coerces
            (Some(json!(90000)), false),  // outside range
            (None, false),                // missing property
        ];
        for (tenant_id, expected) in test_cases {
            let target_properties = match &tenant_id {
                Some(value) => HashMap::from([("tenant_id".to_string(), value.clone())]),
                None => HashMap::new(),
            };
            let result =
                evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC)
                    .unwrap();
            assert_eq!(
                result, expected,
                "tenant_id={tenant_id:?} should evaluate to {expected}"
            );
        }
    }

    #[test]
    fn test_cohort_with_malformed_between_value_is_non_match_not_parse_error() {
        // A between leaf with a malformed value (not a two-element array) deserializes
        // as a regular filter and resolves to a non-match during evaluation, rather
        // than failing the whole cohort parse.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [{"key": "tenant_id", "type": "person", "value": [1, 2, 3],
                                    "operator": "between", "negation": false}]
                    }]
                }
            }),
        );

        assert!(cohort.extract_dependencies().unwrap().is_empty());

        let cohorts = vec![cohort];
        let target_properties = HashMap::from([("tenant_id".to_string(), json!(2))]);
        assert!(matches!(
            evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC),
            Ok(false)
        ));
    }

    #[test]
    fn test_cohort_with_min_operator_alias_evaluates_as_gte() {
        // Legacy `min`/`max` operator aliases are only normalized to gte/lte on the
        // flag write path; cohort filters can still carry them and must evaluate.
        let cohort = create_dynamic_cohort_with_filters(
            1,
            json!({
                "properties": {
                    "type": "OR",
                    "values": [{
                        "type": "OR",
                        "values": [{"key": "seats", "type": "person", "value": "30",
                                    "operator": "min", "negation": false}]
                    }]
                }
            }),
        );

        let cohorts = vec![cohort];
        let test_cases = [(json!(40), true), (json!(30), true), (json!(20), false)];
        for (seats, expected) in test_cases {
            let target_properties = HashMap::from([("seats".to_string(), seats.clone())]);
            let result =
                evaluate_dynamic_cohorts(1, &target_properties, &cohorts, &HashMap::new(), Tz::UTC)
                    .unwrap();
            assert_eq!(
                result, expected,
                "seats={seats} should evaluate to {expected}"
            );
        }
    }
}