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.

feat(approvals): mark a change request stale when its flag changes owner (#113117)

Yasend19a7fbd014e
diff --git a/products/approvals/backend/actions/feature_flags.py b/products/approvals/backend/actions/feature_flags.py
index 708f870661ff..59a7c234b942 100644
--- a/products/approvals/backend/actions/feature_flags.py
+++ b/products/approvals/backend/actions/feature_flags.py
@@ -7,7 +7,7 @@ from django.db.models import Model
from products.approvals.backend.actions.base import BaseAction
from products.approvals.backend.exceptions import ApplyFailed, PreconditionFailed
-from products.approvals.backend.ownership import OWNER_KIND_UNOWNED
+from products.approvals.backend.ownership import OWNER_KIND_UNOWNED, owner_kind_changed
from products.feature_flags.backend.api.feature_flag import FeatureFlagSerializer
from products.feature_flags.backend.models.feature_flag import FeatureFlag
from products.feature_flags.backend.ownership import flag_owner_kind
@@ -130,6 +130,24 @@ def _derive_flag_owner_kind(team, resource_id: Optional[str], intent_data: dict[
return flag_owner_kind(flag) or OWNER_KIND_UNOWNED
+def _check_flag_staleness(intent_data: dict[str, Any], context: Optional[dict[str, Any]] = None) -> bool:
+ """Whether the flag moved out from under a pending change request.
+
+ Two ways it can: the flag itself was edited, or a different product adopted it. The second
+ matters on its own, because which policy applies is keyed on the owner, so an owner change
+ puts the request in front of the wrong approvers.
+ """
+ if _check_version_staleness(intent_data, context):
+ return True
+
+ instance = context.get("instance") if context else None
+ if instance is None:
+ return False
+
+ recorded = context.get("recorded_owner_kind") if context else None
+ return owner_kind_changed(recorded, flag_owner_kind(instance) or OWNER_KIND_UNOWNED)
+
+
def _resolve_existing_flag(change_request) -> Optional[FeatureFlag]:
"""Find the flag a change request operates on, if it already exists.
@@ -230,7 +248,7 @@ class FeatureFlagActionBase(BaseAction):
intent_data: dict[str, Any],
context: Optional[dict[str, Any]] = None,
) -> bool:
- return _check_version_staleness(intent_data, context)
+ return _check_flag_staleness(intent_data, context)
@classmethod
def validate_intent(
@@ -259,6 +277,8 @@ class FeatureFlagActionBase(BaseAction):
if instance is not None:
context["instance"] = instance
+ context["recorded_owner_kind"] = change_request.owner_kind
+
return context
# Whether this action should fire when a brand-new flag is born in the target state.
@@ -480,7 +500,7 @@ class UpdateFeatureFlagAction(BaseAction):
intent_data: dict[str, Any],
context: Optional[dict[str, Any]] = None,
) -> bool:
- return _check_version_staleness(intent_data, context)
+ return _check_flag_staleness(intent_data, context)
@classmethod
def _extract_rollout_percentages(cls, filters: dict[str, Any]) -> list[dict[str, Any]]:
@@ -659,6 +679,8 @@ class UpdateFeatureFlagAction(BaseAction):
if instance is not None:
context["instance"] = instance
+ context["recorded_owner_kind"] = change_request.owner_kind
+
return context
@classmethod
diff --git a/products/approvals/backend/tests/test_update_feature_flag_action.py b/products/approvals/backend/tests/test_update_feature_flag_action.py
index eff2bc783f8c..2d572b6119cc 100644
--- a/products/approvals/backend/tests/test_update_feature_flag_action.py
+++ b/products/approvals/backend/tests/test_update_feature_flag_action.py
@@ -20,6 +20,7 @@ from products.approvals.backend.models import ApprovalPolicy, ChangeRequest
from products.approvals.backend.policies import PolicyEngine
from products.approvals.backend.services import ChangeRequestService
from products.dashboards.backend.models.dashboard import Dashboard
+from products.experiments.backend.models.experiment import Experiment
from products.feature_flags.backend.models.feature_flag import FeatureFlag
SINGLE_DICT_PATHS = {"holdout"}
@@ -391,6 +392,26 @@ class TestCheckStaleness(APIBaseTest):
assert result is True
+ @parameterized.expand(
+ [
+ ("adopted by an experiment", "unowned", True, True),
+ ("released by its experiment", "experiment", False, True),
+ ("owner unchanged", "unowned", False, False),
+ ("still owned by the same experiment", "experiment", True, False),
+ ("never classified", None, True, False),
+ ]
+ )
+ def test_staleness_by_owner(self, _name, recorded, owned_now, expected_stale):
+ flag = self._create_flag()
+ if owned_now:
+ Experiment.objects.create(team=self.team, name="exp", feature_flag=flag)
+
+ intent = {"preconditions": {"version": flag.version}}
+ context = {"instance": flag, "recorded_owner_kind": recorded}
+
+ assert EnableFeatureFlagAction.check_staleness(intent, context) is expected_stale
+ assert UpdateFeatureFlagAction.check_staleness(intent, context) is expected_stale
+
def test_base_action_check_staleness_always_returns_false(self):
from products.approvals.backend.actions.base import BaseAction