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.

chore(flags): add actionable management warning contracts (#99110)

Anders1518dc6c386b
diff --git a/docs/internal/feature-flags/management-warnings.md b/docs/internal/feature-flags/management-warnings.md
new file mode 100644
index 000000000000..b210bfbfbccf
--- /dev/null
+++ b/docs/internal/feature-flags/management-warnings.md
@@ -0,0 +1,45 @@
+# Management warnings
+
+The rules v2 management warning contract is dormant.
+It defines a diagnostic value and its wire representation; it has no detectors or production consumers.
+
+## Supported codes
+
+Management warnings describe an actionable consequence of an otherwise valid configuration or operation.
+The following triggers guide detector implementations.
+
+| Code | Trigger and placement | Suppress when |
+| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
+| `RULE_ORDER_CHANGES_TRAFFIC` | An allowed reorder changes which rule receives an overlapping population. Show it in the reorder preview. | Target populations are disjoint, or the reorder cannot change evaluation. Reject edits to a protected experiment prefix instead. |
+| `UNREACHABLE_LOWER_RULE` | An earlier terminal catch-all prevents evaluation from reaching a lower rule. Identify the blocking and unreachable rules so the user can move or remove them. | The earlier rule can continue to the lower rule. |
+| `ROLLOUT_MISS_CAN_ENTER_LOWER_RULE` | A continuing rollout miss reaches a lower rule that serves the same value outside the upper rule's percentage. Identify both rules and explain the effective reach. | Continuation alone is the only evidence. Ordinary composition of rules does not need a warning. |
+| `ASSIGNMENT_RESET_CHANGES_TRAFFIC` | Resetting a non-experiment percentage rollout's seed can change who receives its value. Show it in the reset preview. | The percentage is 0 or 100, the seed is unchanged, or evaluation cannot change. Started experiment resets remain invalid. |
+| `CONCLUSION_EXPANDS_POPULATION` | Shipping a winner or concluding to a terminal replacement extends the chosen value to people excluded by rollout or holdout. Show the expansion in the operation preview. | The removed gates do not expand who receives the value. Variant consolidation alone does not trigger it. |
+
+Reorder and rollout detectors must establish the relevant overlap and reachability before asserting an effect.
+They do not need a general solver for arbitrary targeting predicates.
+An inconclusive comparison must not claim a proven traffic change or an exact affected percentage.
+Reset and conclusion warnings belong to the operation preview, so they do not persist after the operation completes.
+
+## Other diagnostics
+
+SDK readiness checks need observed SDK capabilities and evaluation modes.
+They belong to activation and experiment-start validation rather than configuration-only warning detectors.
+Readiness explains remote request requirements and local-only or bulk-local omissions before activation; expected remote fallback alone is not a configuration defect.
+Missing attribution support is explained while drafting and blocks experiment start when the readiness policy fails.
+Equal-valued variants are valid, including A/A tests; the editor may explain that variant identity distinguishes them, and equality alone is not a warning.
+Number and object value compatibility behavior remains deferred until those values are enabled.
+None of these carry a management warning code.
+
+## Wire compatibility and detector ownership
+
+`ManagementWarning` requires `code` and permits optional `detail` and `attr` members.
+`detail` is presentation text; `attr` identifies a field when present.
+An omitted `attr` and `attr: null` both mean no field and parse as `None`.
+Serialization omits `attr` when it is `None`, so explicit null normalizes to an omitted member.
+
+The five codes are the complete `warning_codes` list of the harness registry at contract package 2.0.0 ([posthog-sdk-test-harness#57](https://github.com/PostHog/posthog-sdk-test-harness/pull/57), unreleased); the DTO rejects any other code.
+
+Share the diagnostic output type across callers.
+Define detector inputs alongside their implementations: rule checks need evaluated targeting and rollout fields, and lifecycle checks need the proposed operation.
+The structural `ConfigV2` reader omits these fields, so it cannot serve as a universal detector input.
diff --git a/products/feature_flags/backend/facade/warnings.py b/products/feature_flags/backend/facade/warnings.py
new file mode 100644
index 000000000000..bfd88e87fcb6
--- /dev/null
+++ b/products/feature_flags/backend/facade/warnings.py
@@ -0,0 +1,58 @@
+"""Dormant management warning contracts for feature flag rules v2."""
+
+from collections.abc import Mapping
+from typing import Any, Literal, NotRequired, TypedDict, get_args
+
+from posthog.dataclasses import frozen
+
+ManagementWarningCode = Literal[
+ "RULE_ORDER_CHANGES_TRAFFIC",
+ "UNREACHABLE_LOWER_RULE",
+ "ROLLOUT_MISS_CAN_ENTER_LOWER_RULE",
+ "ASSIGNMENT_RESET_CHANGES_TRAFFIC",
+ "CONCLUSION_EXPANDS_POPULATION",
+]
+
+
+class ManagementWarningWire(TypedDict):
+ code: ManagementWarningCode
+ detail: NotRequired[str]
+ attr: NotRequired[str | None]
+
+
+@frozen
+class ManagementWarning:
+ code: ManagementWarningCode
+ detail: str | None = None
+ attr: str | None = None
+
+ def __post_init__(self) -> None:
+ if self.code not in get_args(ManagementWarningCode):
+ raise ValueError("Unsupported management warning code")
+ if self.detail is not None and not isinstance(self.detail, str):
+ raise ValueError("Management warning detail must be a string")
+ if self.attr is not None and not isinstance(self.attr, str):
+ raise ValueError("Management warning attr must be a string or null")
+
+
+def serialize_management_warning(warning: ManagementWarning) -> ManagementWarningWire:
+ result: ManagementWarningWire = {"code": warning.code}
+ if warning.detail is not None:
+ result["detail"] = warning.detail
+ if warning.attr is not None:
+ result["attr"] = warning.attr
+ return result
+
+
+def parse_management_warning(value: Mapping[str, Any]) -> ManagementWarning:
+ if not isinstance(value, Mapping):
+ raise ValueError("Management warning must be an object")
+ if value.keys() - {"code", "detail", "attr"}:
+ raise ValueError("Unknown management warning member")
+ if "code" not in value:
+ raise ValueError("Management warning code is required")
+ # The DTO reads a None detail as omitted, so an explicit wire null must be rejected here.
+ if "detail" in value and value["detail"] is None:
+ raise ValueError("Management warning detail must be a string")
+ # Construction validates the member types and the closed code set.
+ return ManagementWarning(code=value["code"], detail=value.get("detail"), attr=value.get("attr"))
diff --git a/products/feature_flags/backend/test/test_facade_warnings.py b/products/feature_flags/backend/test/test_facade_warnings.py
new file mode 100644
index 000000000000..ac5c44e40c88
--- /dev/null
+++ b/products/feature_flags/backend/test/test_facade_warnings.py
@@ -0,0 +1,83 @@
+from typing import Any, get_args
+
+import pytest
+
+from parameterized import parameterized
+
+from products.feature_flags.backend.facade.warnings import (
+ ManagementWarning,
+ ManagementWarningCode,
+ ManagementWarningWire,
+ parse_management_warning,
+ serialize_management_warning,
+)
+
+
+class TestManagementWarning:
+ @parameterized.expand(
+ [
+ ("code_only", {}),
+ ("detail", {"detail": "The rule order can change traffic."}),
+ ("field_attr", {"attr": "filters.rules"}),
+ ("detail_and_attr", {"detail": "The rule order can change traffic.", "attr": "filters.rules"}),
+ ("empty_strings", {"detail": "", "attr": ""}),
+ ]
+ )
+ def test_wire_round_trip(self, _name: str, optional_members: dict[str, object]) -> None:
+ assert set(get_args(ManagementWarningCode)) == {
+ "RULE_ORDER_CHANGES_TRAFFIC",
+ "UNREACHABLE_LOWER_RULE",
+ "ROLLOUT_MISS_CAN_ENTER_LOWER_RULE",
+ "ASSIGNMENT_RESET_CHANGES_TRAFFIC",
+ "CONCLUSION_EXPANDS_POPULATION",
+ }
+ for code in get_args(ManagementWarningCode):
+ wire = {"code": code, **optional_members}
+ warning = parse_management_warning(wire)
+ assert serialize_management_warning(warning) == wire
+ assert parse_management_warning(serialize_management_warning(warning)) == warning
+
+ @parameterized.expand(
+ [
+ ("unknown_member", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "extra": True}, "Unknown.*member"),
+ ("unknown_code", {"code": "UNKNOWN_WARNING"}, "Unsupported.*code"),
+ ("missing_code", {}, "code is required"),
+ ("null_code", {"code": None}, "Unsupported.*code"),
+ ("number_code", {"code": 1}, "Unsupported.*code"),
+ ("bool_code", {"code": True}, "Unsupported.*code"),
+ ("list_code", {"code": []}, "Unsupported.*code"),
+ ("object_code", {"code": {}}, "Unsupported.*code"),
+ ("null_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": None}, "detail must be a string"),
+ ("number_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": 2}, "detail must be a string"),
+ ("bool_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": False}, "detail must be a string"),
+ ("list_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": []}, "detail must be a string"),
+ ("object_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": {}}, "detail must be a string"),
+ ("number_attr", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "attr": 1}, "attr must be a string or null"),
+ ("bool_attr", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "attr": True}, "attr must be a string or null"),
+ ("list_attr", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "attr": []}, "attr must be a string or null"),
+ ("object_attr", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "attr": {}}, "attr must be a string or null"),
+ ]
+ )
+ def test_invalid_wire_is_rejected(self, _name: str, wire: dict[str, object], message: str) -> None:
+ with pytest.raises(ValueError, match=message):
+ parse_management_warning(wire)
+
+ def test_absent_and_null_attr_are_equivalent(self) -> None:
+ wire: ManagementWarningWire = {"code": "RULE_ORDER_CHANGES_TRAFFIC"}
+ warning = ManagementWarning(code="RULE_ORDER_CHANGES_TRAFFIC")
+ assert warning.attr is None
+ assert parse_management_warning(wire) == warning
+ assert parse_management_warning({**wire, "attr": None}) == warning
+ assert serialize_management_warning(warning) == wire
+ assert serialize_management_warning(ManagementWarning(code="RULE_ORDER_CHANGES_TRAFFIC", attr=None)) == wire
+
+ @parameterized.expand(
+ [
+ ("unknown_code", {"code": "UNKNOWN_WARNING"}),
+ ("wrong_detail", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "detail": []}),
+ ("wrong_attr", {"code": "RULE_ORDER_CHANGES_TRAFFIC", "attr": False}),
+ ]
+ )
+ def test_invalid_construction_is_rejected(self, _name: str, members: dict[str, Any]) -> None:
+ with pytest.raises(ValueError):
+ ManagementWarning(**members)