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(web-analytics): resolve the cookieless modifier the same way in every process (#108525)

Lucas Ricoyae5ceedb894c
diff --git a/products/web_analytics/backend/hogql_queries/cookieless_flag.py b/products/web_analytics/backend/hogql_queries/cookieless_flag.py
index c25172b1dcfe..ec2ae37150cf 100644
--- a/products/web_analytics/backend/hogql_queries/cookieless_flag.py
+++ b/products/web_analytics/backend/hogql_queries/cookieless_flag.py
@@ -2,24 +2,16 @@ from typing import TYPE_CHECKING, Optional
from posthog.cloud_utils import is_cloud
-from products.web_analytics.backend.hogql_queries.first_pageview_flag import evaluate_team_rollout_flag
-
if TYPE_CHECKING:
from posthog.models import Team
-COOKIELESS_TRAFFIC_IS_REGULAR_FEATURE_FLAG = "cookieless-traffic-is-regular"
-
def resolve_cookieless_traffic_is_regular_modifier(team: "Team", current: Optional[bool]) -> Optional[bool]:
+ # The default must not depend on the process. Precompute puts this value in its job
+ # hash, so a process that resolves it differently (for example one without local flag
+ # definitions) builds jobs that no other process reads.
if current is not None:
return current
if not is_cloud():
return None
- if not evaluate_team_rollout_flag(
- team,
- COOKIELESS_TRAFFIC_IS_REGULAR_FEATURE_FLAG,
- "cookieless_traffic_is_regular_flag_failed",
- log_unresolved=False,
- ):
- return None
return True
diff --git a/products/web_analytics/backend/hogql_queries/first_pageview_flag.py b/products/web_analytics/backend/hogql_queries/first_pageview_flag.py
index ad97c1290c4a..c7317d91c2ab 100644
--- a/products/web_analytics/backend/hogql_queries/first_pageview_flag.py
+++ b/products/web_analytics/backend/hogql_queries/first_pageview_flag.py
@@ -30,9 +30,7 @@ SESSION_PROPERTY_TO_FIRST_PAGEVIEW: dict[str, WebStatsBreakdown] = {
}
-def evaluate_team_rollout_flag(
- team: "Team", flag_key: str, failure_log_event: str, *, log_unresolved: bool = True
-) -> bool:
+def evaluate_team_rollout_flag(team: "Team", flag_key: str, failure_log_event: str) -> bool:
"""Evaluate a team-scoped rollout flag locally, failing closed on flag-service errors.
A raised exception here must never fail the query: evaluation failure degrades
@@ -63,8 +61,7 @@ def evaluate_team_rollout_flag(
send_feature_flag_events=False,
)
if enabled is None:
- if log_unresolved:
- logger.warning(failure_log_event, reason="feature_enabled_returned_none", team_id=team.pk)
+ logger.warning(failure_log_event, reason="feature_enabled_returned_none", team_id=team.pk)
return False
return bool(enabled)
except Exception as e:
diff --git a/products/web_analytics/backend/hogql_queries/test/test_cookieless_flag.py b/products/web_analytics/backend/hogql_queries/test/test_cookieless_flag.py
index a57efe267b31..93f73bd9efdc 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_cookieless_flag.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_cookieless_flag.py
@@ -6,40 +6,27 @@ from products.web_analytics.backend.hogql_queries.cookieless_flag import resolve
IS_CLOUD = "products.web_analytics.backend.hogql_queries.cookieless_flag.is_cloud"
FEATURE_ENABLED = "posthoganalytics.feature_enabled"
-FLAG_LOGGER = "products.web_analytics.backend.hogql_queries.first_pageview_flag.logger"
-
-
-def _team() -> MagicMock:
- team = MagicMock()
- team.uuid = "team-uuid"
- team.organization_id = "org-uuid"
- team.id = 1
- team.pk = 1
- return team
class TestResolveCookielessTrafficIsRegularModifier:
- @parameterized.expand([("on", True, True), ("off", False, None), ("missing", None, None)])
- def test_the_flag_decides_on_cloud_without_logging(self, _name, flag_value, expected):
- with (
- patch(IS_CLOUD, return_value=True),
- patch(FEATURE_ENABLED, return_value=flag_value) as feature_enabled,
- patch(FLAG_LOGGER) as logger,
- ):
- assert resolve_cookieless_traffic_is_regular_modifier(_team(), None) is expected
-
- feature_enabled.assert_called_once()
- logger.warning.assert_not_called()
-
- @parameterized.expand([("opted in", True), ("opted out", False)])
- def test_an_explicit_team_setting_skips_the_flag(self, _name, current):
- with patch(IS_CLOUD, return_value=True), patch(FEATURE_ENABLED) as feature_enabled:
- assert resolve_cookieless_traffic_is_regular_modifier(_team(), current) is current
+ @parameterized.expand(
+ [
+ ("flags_on", True),
+ ("flags_off", False),
+ ("flags_unavailable", None),
+ ]
+ )
+ def test_cloud_default_does_not_depend_on_local_flag_state(self, _name, flag_value):
+ with patch(IS_CLOUD, return_value=True), patch(FEATURE_ENABLED, return_value=flag_value) as feature_enabled:
+ assert resolve_cookieless_traffic_is_regular_modifier(MagicMock(), None) is True
feature_enabled.assert_not_called()
- def test_self_hosted_never_consults_the_flag(self):
- with patch(IS_CLOUD, return_value=False), patch(FEATURE_ENABLED) as feature_enabled:
- assert resolve_cookieless_traffic_is_regular_modifier(_team(), None) is None
+ @parameterized.expand([("opted in", True), ("opted out", False)])
+ def test_an_explicit_team_setting_wins(self, _name, current):
+ with patch(IS_CLOUD, return_value=True):
+ assert resolve_cookieless_traffic_is_regular_modifier(MagicMock(), current) is current
- feature_enabled.assert_not_called()
+ def test_self_hosted_keeps_the_legacy_default(self):
+ with patch(IS_CLOUD, return_value=False):
+ assert resolve_cookieless_traffic_is_regular_modifier(MagicMock(), None) is None
diff --git a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
index a296cc9f1137..1767426de737 100644
--- a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
+++ b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
@@ -618,7 +618,7 @@ def web_ensure_precomputed(
family = kwargs.pop("family", None)
modifiers = create_default_modifiers_for_team(team, kwargs.get("modifiers"))
if runner is not None:
- # Pin the runner's decision so a flag refresh cannot change INSERT semantics after hashing.
+ # Use the runner's value, which can carry a per-query override, so the INSERT and the job hash match the read.
modifiers.cookielessTrafficIsRegular = runner.modifiers.cookielessTrafficIsRegular
kwargs["modifiers"] = modifiers
if modifiers.cookielessTrafficIsRegular: