: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): sort evaluation contexts in the Rust cache builder (#92164)
James Gill4ed30c65d3dd
diff --git a/products/feature_flags/backend/test/test_flags_cache.py b/products/feature_flags/backend/test/test_flags_cache.pyindex 0a52705863fc..317e138c0f0e 100644--- a/products/feature_flags/backend/test/test_flags_cache.py+++ b/products/feature_flags/backend/test/test_flags_cache.py@@ -2437,14 +2437,19 @@ class TestManagementCommands(BaseTest):created_by=self.user,filters={"groups": [{"properties": [], "rollout_percentage": 100}]},)- ctx = EvaluationContext.objects.create(team=self.team, name="original-context-name")+ # Out of alphabetical order on purpose. The assertions below also cover the sort that+ # ArrayAgg(distinct=True) applies, which the Rust builder matches with+ # ARRAY_AGG(DISTINCT ...). Remove distinct=True and the two write different arrays.+ ctx = EvaluationContext.objects.create(team=self.team, name="zulu-context")+ other_ctx = EvaluationContext.objects.create(team=self.team, name="alpha-context")FeatureFlagEvaluationContext.objects.create(feature_flag=flag, evaluation_context=ctx)+ FeatureFlagEvaluationContext.objects.create(feature_flag=flag, evaluation_context=other_ctx)# Warm the cacheupdate_flags_cache(self.team)# Rename the context directly in DB (bypassing signals to simulate stale cache)- EvaluationContext.objects.filter(id=ctx.id).update(name="renamed-context-name")+ EvaluationContext.objects.filter(id=ctx.id).update(name="mike-context")# Verify should detect the mismatchresult = verify_team_flags(self.team, verbose=True)@@ -2456,8 +2461,8 @@ class TestManagementCommands(BaseTest):field_diffs = result["diffs"][0]["field_diffs"]eval_tag_diff = next(d for d in field_diffs if d["field"] == "evaluation_contexts")- self.assertEqual(eval_tag_diff["cached_value"], ["original-context-name"])- self.assertEqual(eval_tag_diff["db_value"], ["renamed-context-name"])+ self.assertEqual(eval_tag_diff["cached_value"], ["alpha-context", "zulu-context"])+ self.assertEqual(eval_tag_diff["db_value"], ["alpha-context", "mike-context"])# Mismatch result should include db_data for cache fix optimizationself.assertIn("db_data", result)diff --git a/rust/feature-flags/src/flags/feature_flag_list.rs b/rust/feature-flags/src/flags/feature_flag_list.rsindex 1f88d6d7edcc..e28d717737ad 100644--- a/rust/feature-flags/src/flags/feature_flag_list.rs+++ b/rust/feature-flags/src/flags/feature_flag_list.rs@@ -146,8 +146,15 @@ impl FeatureFlagList {f.ensure_experience_continuity,f.version,f.evaluation_runtime,+ -- DISTINCT makes Postgres sort the names before it aggregates them, which+ -- matches ArrayAgg(..., distinct=True) on the two Python paths that write+ -- this cache entry: products/feature_flags/backend/flags_cache.py (batch) and+ -- products/feature_flags/backend/models/feature_flag.py (single team, which is+ -- also what the cache verifier compares against). Without DISTINCT the array+ -- order follows the join, so the sides disagree for a flag with two or more+ -- contexts. Keep all three in lockstep.COALESCE(- ARRAY_AGG(ctx.name) FILTER (WHERE ctx.name IS NOT NULL),+ ARRAY_AGG(DISTINCT ctx.name) FILTER (WHERE ctx.name IS NOT NULL),'{}'::text[]) AS evaluation_tags,bucketing_identifier,@@ -504,15 +511,13 @@ mod tests {assert_eq!(flag.key, "flag1");assert_eq!(flag.team_id, team.id);- // Check that evaluation tags were properly fetched+ // ARRAY_AGG(DISTINCT ...) sorts, so this is not the insertion order.let tags = flag.evaluation_tags.as_ref().expect("Should have evaluation tags");- assert_eq!(tags.len(), 3);- assert!(tags.contains(&"docs-page".to_string()));- assert!(tags.contains(&"marketing-site".to_string()));- assert!(tags.contains(&"app".to_string()));+ let tag_names: Vec<&str> = tags.iter().map(String::as_str).collect();+ assert_eq!(tag_names, ["app", "docs-page", "marketing-site"]);}#[tokio::test]