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(dags): raise on partial FF responses in PoE backfill (#58111)

Robbiea0337cfdc078
diff --git a/posthog/dags/backfill_persons_on_events_mode.py b/posthog/dags/backfill_persons_on_events_mode.py
index 64bf306dfd87..6229a0b3b132 100644
--- a/posthog/dags/backfill_persons_on_events_mode.py
+++ b/posthog/dags/backfill_persons_on_events_mode.py
@@ -60,6 +60,14 @@ def _resolve_persons_on_events_mode(team: Team, client: Client) -> PersonsOnEven
Encoding their result into `team.modifiers` would freeze the override into
DB state, so unsetting the env var wouldn't restore each team's intended
flag-driven mode.
+
+ Hard transport failures (network errors, 5xx, 4xx, quota limits) already
+ propagate as exceptions out of `get_flags_decision`. The two soft-failure
+ cases — a 200 with `errorsWhileComputingFlags=true`, or a 200 missing one
+ of the requested flag keys — are NOT exceptional from the SDK's point of
+ view but are indistinguishable from "flag genuinely returned False" once
+ they reach the resolver. Treat them as errors so the team is counted as
+ `skipped_errored` rather than silently frozen onto the v2 fallback.
"""
decision = client.get_flags_decision(
distinct_id=str(team.uuid),
@@ -80,17 +88,27 @@ def _resolve_persons_on_events_mode(team: Team, client: Client) -> PersonsOnEven
},
flag_keys_to_evaluate=[POE_V2_FLAG, POE_V1_FLAG],
)
+
+ if decision.get("errorsWhileComputingFlags"):
+ raise RuntimeError("FF service returned errorsWhileComputingFlags=true")
+
flags = decision.get("flags", {}) or {}
+ missing = [key for key in (POE_V2_FLAG, POE_V1_FLAG) if key not in flags]
+ if missing:
+ raise RuntimeError(f"FF response missing required flag(s): {missing}")
- v2_flag = flags.get(POE_V2_FLAG)
- if v2_flag and v2_flag.enabled:
+ v2_flag = flags[POE_V2_FLAG]
+ if v2_flag.enabled:
return PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS
- v1_flag = flags.get(POE_V1_FLAG)
- if v1_flag and v1_flag.enabled:
+ v1_flag = flags[POE_V1_FLAG]
+ if v1_flag.enabled:
return PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS
- # Fallback: v2 (current platform default since the 2024-06-14 100% rollout).
+ # Both flags returned False — fall back to v2 (current platform default
+ # since the 2024-06-14 100% rollout). This branch is reached only when the
+ # FF service explicitly answered False for both keys, never when it failed
+ # to answer.
return PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS
diff --git a/posthog/dags/tests/test_backfill_persons_on_events_mode.py b/posthog/dags/tests/test_backfill_persons_on_events_mode.py
index f81b0e5fafc7..860846583e94 100644
--- a/posthog/dags/tests/test_backfill_persons_on_events_mode.py
+++ b/posthog/dags/tests/test_backfill_persons_on_events_mode.py
@@ -1,5 +1,6 @@
"""Tests for the persons-on-events-mode backfill job."""
+import pytest
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
@@ -58,16 +59,6 @@ class TestResolvePersonsOnEventsMode(BaseTest):
{POE_V2_FLAG: _make_flag(POE_V2_FLAG, False), POE_V1_FLAG: _make_flag(POE_V1_FLAG, False)},
PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,
),
- (
- "fallback_when_flags_absent_from_response",
- {},
- PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,
- ),
- (
- "v1_when_v2_absent_and_v1_enabled",
- {POE_V1_FLAG: _make_flag(POE_V1_FLAG, True)},
- PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS,
- ),
]
)
def test_resolves_mode_from_flag_decision(
@@ -92,6 +83,67 @@ class TestResolvePersonsOnEventsMode(BaseTest):
"project": str(self.team.id),
}
+ @parameterized.expand(
+ [
+ (
+ "errors_while_computing_flags_with_full_response",
+ # The FF service can return both keys *and* set the error flag
+ # if it partially failed (e.g. one cohort lookup timed out).
+ # Even with seemingly-complete data, treat the whole answer
+ # as untrusted so the team isn't frozen onto a wrong value.
+ {
+ "flags": {
+ POE_V2_FLAG: _make_flag(POE_V2_FLAG, False),
+ POE_V1_FLAG: _make_flag(POE_V1_FLAG, True),
+ },
+ "errorsWhileComputingFlags": True,
+ "requestId": "test",
+ },
+ "errorsWhileComputingFlags",
+ ),
+ (
+ "errors_while_computing_flags_with_empty_flags",
+ {"flags": {}, "errorsWhileComputingFlags": True, "requestId": "test"},
+ "errorsWhileComputingFlags",
+ ),
+ (
+ "v2_flag_missing_from_response",
+ {"flags": {POE_V1_FLAG: _make_flag(POE_V1_FLAG, True)}, "requestId": "test"},
+ "missing required flag",
+ ),
+ (
+ "v1_flag_missing_from_response",
+ {"flags": {POE_V2_FLAG: _make_flag(POE_V2_FLAG, False)}, "requestId": "test"},
+ "missing required flag",
+ ),
+ (
+ "both_flags_missing_from_response",
+ {"flags": {}, "requestId": "test"},
+ "missing required flag",
+ ),
+ (
+ "no_flags_key_at_all",
+ {"requestId": "test"},
+ "missing required flag",
+ ),
+ ]
+ )
+ def test_raises_on_partial_or_incomplete_response(
+ self,
+ _name: str,
+ decision: dict,
+ expected_substring: str,
+ ):
+ """Soft failures (200 OK with partial/error data) must raise so the
+ per-team try/except in `persist_persons_on_events_mode_op` counts the
+ team as `skipped_errored` rather than silently writing the v2 fallback.
+ """
+ client = MagicMock()
+ client.get_flags_decision.return_value = decision
+
+ with pytest.raises(RuntimeError, match=expected_substring):
+ _resolve_persons_on_events_mode(self.team, client)
+
class TestBackfillPersonsOnEventsMode(BaseTest):
def setUp(self):