AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): PostHog flag sync can create a flag that serves nobody, and says why a request was refused (#15181)
Reinier van der Leer984aacf5b8b1
diff --git a/autogpt_platform/backend/scripts/feature_flag_sync.py b/autogpt_platform/backend/scripts/feature_flag_sync.pyindex 58fb806228..2ef6a8e871 100644--- a/autogpt_platform/backend/scripts/feature_flag_sync.py+++ b/autogpt_platform/backend/scripts/feature_flag_sync.py@@ -114,7 +114,7 @@ def map_segment(segment: dict[str, Any], env: str) -> MappedCohort:payload={"name": cohort_name(key),"description": (- f"{segment.get('name') or key}: synced from LaunchDarkly segment "+ f"{_text(segment.get('name'), key)}: synced from LaunchDarkly segment "f"`{key}` ({env})."),"is_static": False,@@ -175,7 +175,7 @@ def map_flag(mapped.payload = {"key": key,- "name": flag.get("name") or key,+ "name": _text(flag.get("name"), key),"active": active,"filters": filters,"ensure_experience_continuity": False,@@ -310,10 +310,16 @@ def describe_property(prop: dict[str, Any]) -> str:elif prop.get("key") == "distinct_id":shown = "<user-id pattern>"else:- shown = _redact(json.dumps(value))+ shown = redact(json.dumps(value))return f"{prop.get('key')} {prop.get('operator') or 'exact'} {shown}"+def redact(text: str) -> str:+ """Payloads and patterns can carry addresses and user ids; the output never does."""+ text = _LOCAL_PART.sub("<email>@", _EMAIL.sub("<email>", text))+ return _UUID.sub("<user-id>", text)++class Unmappable(Exception):"""A LaunchDarkly construct with no exact PostHog equivalent."""@@ -520,6 +526,11 @@ def _iso_date(value: Any) -> datetime:return datetime.fromisoformat(str(value).replace("Z", "+00:00"))+def _text(value: str | None, fallback: str) -> str:+ """LaunchDarkly free text as PostHog stores it: trimmed, or a re-plan never matches."""+ return (value or "").strip() or fallback++def _boolean_filters(flag: dict[str, Any], paths: list[_Path]) -> tuple[dict[str, Any], bool]:@@ -548,8 +559,12 @@ def _boolean_filters(if not path.is_target:raise Unmappable("a rule serves false ahead of a rule serving true")exclusions += path.groups[0][0]["value"]+ serves_anyone = bool(groups)+ # PostHog refuses a flag with no condition group; one at 0% serves nobody.+ if not groups:+ groups = [{"properties": [], "rollout_percentage": 0, "variant": None}]filters = {"groups": groups, "multivariate": None, "payloads": {}}- return filters, bool(groups)+ return filters, serves_anyonedef _multivariate_filters(@@ -700,11 +715,5 @@ def _canonical_payload(payload: Any) -> Any:def _short(value: Any, limit: int = 80) -> str:- text = _redact(json.dumps(value, sort_keys=True))+ text = redact(json.dumps(value, sort_keys=True))return text if len(text) <= limit else text[: limit - 1] + "…"---def _redact(text: str) -> str:- """Payloads and patterns can carry addresses and user ids; the output never does."""- text = _LOCAL_PART.sub("<email>@", _EMAIL.sub("<email>", text))- return _UUID.sub("<user-id>", text)diff --git a/autogpt_platform/backend/scripts/feature_flag_sync_test.py b/autogpt_platform/backend/scripts/feature_flag_sync_test.pyindex 6477fb0493..d1ab4656f5 100644--- a/autogpt_platform/backend/scripts/feature_flag_sync_test.py+++ b/autogpt_platform/backend/scripts/feature_flag_sync_test.py@@ -5,7 +5,11 @@ are evaluated with the PostHog SDK's own local evaluator, so a test assertswhat a user would be served, not just the payload's shape."""+import ioimport json+import urllib.error+import urllib.request+from email.message import Messagefrom typing import Anyimport pytest@@ -341,6 +345,25 @@ def test_rule_on_only_empty_segments_matches_nobody():assert _payload(mapped)["active"] is False+ "config, rollout, active",+ [+ ({"fallthrough": 1}, 0, False),+ ({"on": False, "off": 1}, 0, False),+ ({"fallthrough": 0}, 100, True),+ ],+)+def test_boolean_flag_always_sends_one_condition_group(config, rollout, active):+ """PostHog answers HTTP 400 to a flag with no group, so one serving nobody stands in."""+ payload = _payload(map_flag(_flag("boolean", **config), ENV, _cohorts()))++ assert payload["active"] is active+ assert payload["filters"]["groups"] == [+ {"properties": [], "rollout_percentage": rollout, "variant": None}+ ]+ assert _served({**payload, "active": True})("user-x", ADMIN) is active++def test_target_on_a_context_kind_no_client_sends_is_dropped_with_a_note():flag = _flag("boolean",@@ -578,6 +601,21 @@ def test_plan_updates_a_cohort_whose_redacted_members_changed():assert change.action == "update"+def test_replan_is_unchanged_after_posthog_trims_a_padded_launchdarkly_name():+ cohort = map_segment({"key": "vip", "name": " VIP ", "included": [ADA]}, ENV)+ flag = map_flag({**_flag("boolean", key="padded"), "name": " Padded "}, ENV, {})+ stored_cohort = {**_payload(cohort), "id": 8}+ stored_cohort["description"] = stored_cohort["description"].strip()+ stored_flag = {**_payload(flag), "id": 9, "name": "Padded"}++ changes = plan_sync([cohort], [flag], [stored_cohort], [stored_flag])++ assert [(c.action, c.diff) for c in changes] == [+ ("unchanged", []),+ ("unchanged", []),+ ]++def test_described_filters_never_show_addresses_or_user_ids():uid = "123e4567-e89b-12d3-a456-426614174000"flag = _flag(@@ -649,6 +687,33 @@ def test_apply_creates_cohorts_before_the_flags_that_reference_them(monkeypatch)]+def test_refused_request_reports_the_reason_and_what_was_sent(monkeypatch, capsys):+ reason = "Feature flags must have at least one condition set (group)."++ def refuse(request: urllib.request.Request, timeout: float):+ body = json.dumps({"type": "validation_error", "detail": reason})+ raise urllib.error.HTTPError(+ request.full_url, 400, "Bad Request", Message(), io.BytesIO(body.encode())+ )++ monkeypatch.setattr(urllib.request, "urlopen", refuse)+ client = cli._Client("https://posthog.test", {})+ sent = {+ "key": "staff-only",+ "filters": {+ "groups": [{"properties": [_prop("email", "exact", [STAFF["email"]])]}]+ },+ }++ with pytest.raises(urllib.error.HTTPError):+ client.send("POST", "/api/projects/42/feature_flags/", sent)++ err = capsys.readouterr().err+ assert "400" in err and "https://posthog.test/api/projects/42/feature_flags/" in err+ assert reason in err+ assert "staff-only" in err and STAFF["email"] not in err++def _with_cohort_ids(payload: dict[str, Any], ids: dict[str, int]) -> dict[str, Any]:return {**payload, "filters": resolve_cohort_refs(payload["filters"], ids)}diff --git a/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.py b/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.pyindex 7ca85ba1c9..7b28523ed1 100644--- a/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.py+++ b/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.py@@ -36,11 +36,13 @@ from scripts.feature_flag_sync import (map_flag,map_segment,plan_sync,+ redact,resolve_cohort_refs,)LD_API = "https://app.launchdarkly.com/api/v2"DEFAULT_POSTHOG_HOST = "https://eu.posthog.com"+_SHOWN_CHARS = 2000def main(argv: list[str] | None = None) -> int:@@ -267,8 +269,18 @@ class _Client:headers={**self.headers, "Content-Type": "application/json"},data=json.dumps(body).encode() if body is not None else None,)- with urllib.request.urlopen(request, timeout=60) as response:- return json.load(response)+ try:+ with urllib.request.urlopen(request, timeout=60) as response:+ return json.load(response)+ except urllib.error.HTTPError as e:+ detail = e.read().decode("utf-8", "replace")+ print(+ f"HTTP {e.code} on {method} {url}\n"+ f" response: {redact(detail)[:_SHOWN_CHARS]}\n"+ f" request: {redact(json.dumps(body))[:_SHOWN_CHARS]}",+ file=sys.stderr,+ )+ raisedef _env(name: str) -> str: