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.

fix(flags): ignore stale hash-key override when continuity off (#65991)

Andersce48d8a3365b
diff --git a/rust/feature-flags/src/flags/flag_matching.rs b/rust/feature-flags/src/flags/flag_matching.rs
index 09ef71dd9110..47a97552f79e 100644
--- a/rust/feature-flags/src/flags/flag_matching.rs
+++ b/rust/feature-flags/src/flags/flag_matching.rs
@@ -1804,16 +1804,20 @@ impl FeatureFlagMatcher {
}
}
- // Use hash key overrides for experience continuity
+ // Use hash key overrides for experience continuity. Only consult overrides
+ // when the flag *currently* has continuity enabled — a stored override row
+ // is never deleted when continuity is turned off, so a stale row must not
+ // steer a flag whose continuity is now off (which would keep every distinct_id
+ // of the person bucketing on the old key).
// Priority: DB override > request's anon_distinct_id > distinct_id
- if let Some(hash_key_override) = hash_key_overrides
- .as_ref()
- .and_then(|h| h.get(&feature_flag.key))
- {
- Ok(hash_key_override.clone())
- } else if feature_flag.has_experience_continuity() {
- // For EEC flags, use the request's anon_distinct_id as fallback when no DB override exists
- if let Some(request_override) = request_hash_key_override {
+ if feature_flag.has_experience_continuity() {
+ if let Some(hash_key_override) = hash_key_overrides
+ .as_ref()
+ .and_then(|h| h.get(&feature_flag.key))
+ {
+ Ok(hash_key_override.clone())
+ } else if let Some(request_override) = request_hash_key_override {
+ // Use the request's anon_distinct_id as fallback when no DB override exists
Ok(request_override.clone())
} else {
Ok(self.distinct_id.clone())
@@ -2447,6 +2451,53 @@ mod tests {
}
}
+ /// A stored hash-key override must only steer bucketing while the flag *currently*
+ /// has experience continuity enabled. When continuity is turned off, the row written
+ /// back while it was on is never deleted — so the matcher must fall back to the raw
+ /// distinct_id and ignore the stale override, otherwise every distinct_id of the
+ /// person keeps bucketing on the old key and resolves to the same stale value.
+ #[rstest::rstest]
+ #[case::continuity_off_ignores_stale_override(Some(false), "logged-in-username")]
+ #[case::continuity_unset_ignores_stale_override(None, "logged-in-username")]
+ #[case::continuity_on_applies_override(Some(true), "stale-anon-id")]
+ #[tokio::test]
+ async fn test_hashed_identifier_respects_current_continuity_for_stored_override(
+ #[case] ensure_experience_continuity: Option<bool>,
+ #[case] expected_identifier: &str,
+ ) {
+ use crate::utils::test_utils::{mock_group_type_cache, TestContext};
+
+ let context = TestContext::new(None).await;
+ let cohort_cache = Arc::new(CohortCacheManager::new(
+ context.non_persons_reader.clone(),
+ None,
+ None,
+ ));
+ let matcher = FeatureFlagMatcher::new(
+ "logged-in-username".to_string(),
+ None, // device_id
+ 1,
+ context.create_postgres_router(),
+ cohort_cache,
+ mock_group_type_cache(HashMap::new()),
+ None,
+ );
+
+ // Override left behind from when this flag still had continuity enabled.
+ let overrides = HashMap::from([("my_flag".to_string(), "stale-anon-id".to_string())]);
+ let flag = FeatureFlag {
+ key: "my_flag".to_string(),
+ ensure_experience_continuity,
+ ..Default::default()
+ };
+
+ let identifier = matcher
+ .hashed_identifier(&flag, None, Some(&overrides), &None)
+ .unwrap();
+
+ assert_eq!(identifier, expected_identifier);
+ }
+
#[rstest::rstest]
#[case::injects_when_overrides_absent("user_42", None, "user_42", vec![])]
#[case::injects_when_overrides_lack_distinct_id(