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.
feat(backend): script that re-creates LaunchDarkly's flags and targeting in PostHog (#14806)
Reinier van der Leer9776bf86095d
diff --git a/autogpt_platform/backend/scripts/feature_flag_sync.py b/autogpt_platform/backend/scripts/feature_flag_sync.pynew file mode 100644index 0000000000..176002afea--- /dev/null+++ b/autogpt_platform/backend/scripts/feature_flag_sync.py@@ -0,0 +1,687 @@+"""Map LaunchDarkly flags and segments onto PostHog feature flags and cohorts.++Pure functions over the two vendors' REST shapes, with no I/O, so the mapping+can be tested against a synthetic export. ``sync_feature_flags_to_posthog.py``+does the fetching, planning output and applying.++Anything PostHog cannot express exactly becomes a :class:`Decision` and the+flag or cohort is left out of the plan, never approximated.+"""++from __future__ import annotations++import difflib+import itertools+import json+import re+from datetime import datetime, timezone+from typing import Any, Literal++from pydantic import BaseModel++# What `_person_properties` in `backend.util.feature_flag` sends for a user;+# the SDK adds `distinct_id` (the user id, LaunchDarkly's context key) itself.+PERSON_PROPERTIES = frozenset({"email", "email_domain", "role", "created_at"})+ATTRIBUTE_ALIASES = {+ "/custom/role": "role",+ "custom.role": "role",+ "key": "distinct_id",+}+COHORT_PREFIX = "ld:"+# Context kinds our LaunchDarkly clients send: `user` everywhere, `device` for+# signed-out browsers. A target on any other kind has never matched anyone.+_SENT_CONTEXT_KINDS = frozenset({"user", "device"})++_REGEX_OPS = {"endsWith": "{}$", "startsWith": "^{}", "contains": "{}"}+_NUMERIC_OPS = {+ "lessThan": "lt",+ "lessThanOrEqual": "lte",+ "greaterThan": "gt",+ "greaterThanOrEqual": "gte",+}+_REDACTED_VALUE_KEYS = frozenset({"email", "distinct_id"})+_EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+")+# An address inside a regex has escaped dots the pattern above misses.+_LOCAL_PART = re.compile(r"[A-Za-z0-9._%+-]+@(?=[A-Za-z0-9-])")+_UUID = re.compile(+ r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", re.I+)+++class Decision(BaseModel):+ subject: str+ reason: str+++class MappedCohort(BaseModel):+ segment_key: str+ payload: dict[str, Any] | None+ decisions: list[Decision] = []+ notes: list[str] = []+++class MappedFlag(BaseModel):+ key: str+ kind: Literal["boolean", "multivariate"]+ targets: int+ segments: list[str]+ rules: int+ payload: dict[str, Any] | None+ decisions: list[Decision] = []+ notes: list[str] = []++ @property+ def targeted(self) -> bool:+ return bool(self.targets or self.segments or self.rules)+++class Change(BaseModel):+ kind: Literal["cohort", "flag"]+ key: str+ action: Literal["create", "update", "unchanged", "needs-decision"]+ diff: list[str] = []+ payload: dict[str, Any] | None = None+ existing_id: int | None = None+++def map_segment(segment: dict[str, Any], env: str) -> MappedCohort:+ """A LaunchDarkly segment as a dynamic PostHog cohort payload.++ ``payload`` is None for a segment with no members, which a flag then+ treats as matching nobody.+ """+ key = segment["key"]+ subject = f"segment `{key}`"+ notes: list[str] = []+ try:+ groups = _segment_groups(segment, notes)+ except Unmappable as e:+ return MappedCohort(+ segment_key=key,+ payload=None,+ decisions=[Decision(subject=subject, reason=str(e))],+ )+ if not groups:+ return MappedCohort(segment_key=key, payload=None, notes=notes)+ return MappedCohort(+ segment_key=key,+ notes=notes,+ payload={+ "name": cohort_name(key),+ "description": (+ f"{segment.get('name') or key}: synced from LaunchDarkly segment "+ f"`{key}` ({env})."+ ),+ "is_static": False,+ "filters": {+ "properties": {+ "type": "OR",+ "values": [{"type": "AND", "values": g} for g in groups],+ }+ },+ },+ )+++def map_flag(+ flag: dict[str, Any], env: str, cohorts: dict[str, MappedCohort]+) -> MappedFlag:+ """A LaunchDarkly flag's configuration in *env* as a PostHog flag payload.++ LaunchDarkly serves the first matching target, rule or fallthrough;+ PostHog serves the first matching condition group, so each path becomes+ groups in the same order. Cohort conditions carry the cohort's NAME until+ :func:`resolve_cohort_refs` swaps in the target project's id.+ """+ key = flag["key"]+ cfg = flag["environments"][env]+ boolean = flag.get("kind") == "boolean"+ rules = cfg.get("rules") or []+ segments = sorted(+ {+ s+ for r in rules+ for c in r.get("clauses", [])+ if c.get("op") == "segmentMatch"+ for s in c.get("values", [])+ }+ )+ mapped = MappedFlag(+ key=key,+ kind="boolean" if boolean else "multivariate",+ targets=sum(+ len(t.get("values") or [])+ for t in (cfg.get("targets") or []) + (cfg.get("contextTargets") or [])+ ),+ segments=segments,+ rules=len(rules),+ payload=None,+ )+ try:+ paths = _serving_paths(cfg, cohorts, mapped.notes)+ filters, active = (+ _boolean_filters(flag, paths)+ if boolean+ else _multivariate_filters(flag, paths)+ )+ except Unmappable as e:+ mapped.decisions.append(Decision(subject=f"flag `{key}`", reason=str(e)))+ return mapped++ mapped.payload = {+ "key": key,+ "name": flag.get("name") or key,+ "active": active,+ "filters": filters,+ "ensure_experience_continuity": False,+ "evaluation_runtime": (+ "all"+ if (flag.get("clientSideAvailability") or {}).get("usingEnvironmentId")+ else "server"+ ),+ }+ return mapped+++def cohort_name(segment_key: str) -> str:+ return f"{COHORT_PREFIX}{segment_key}"+++def plan_sync(+ cohorts: list[MappedCohort],+ flags: list[MappedFlag],+ existing_cohorts: list[dict[str, Any]],+ existing_flags: list[dict[str, Any]],+) -> list[Change]:+ """Create / update / unchanged per cohort and flag; nothing is ever deleted.++ Cohorts match by name and flags by key. A flag's cohort references are+ resolved against the cohorts already in the project, so a flag waiting on+ a cohort this plan creates shows the cohort's name in its diff.+ """+ live_cohorts = {c["name"]: c for c in existing_cohorts if not c.get("deleted")}+ live_flags = {f["key"]: f for f in existing_flags if not f.get("deleted")}+ cohort_ids = {name: c["id"] for name, c in live_cohorts.items()}+ changes: list[Change] = []++ for cohort in cohorts:+ if cohort.decisions:+ changes.append(+ Change(+ kind="cohort",+ key=cohort.segment_key,+ action="needs-decision",+ diff=[d.reason for d in cohort.decisions],+ )+ )+ elif cohort.payload is not None:+ changes.append(+ _change(+ "cohort",+ cohort.payload["name"],+ cohort.payload,+ live_cohorts.get(cohort.payload["name"]),+ )+ )++ for flag in flags:+ if flag.payload is None:+ changes.append(+ Change(+ kind="flag",+ key=flag.key,+ action="needs-decision",+ diff=[d.reason for d in flag.decisions],+ )+ )+ continue+ payload = {+ **flag.payload,+ "filters": resolve_cohort_refs(flag.payload["filters"], cohort_ids),+ }+ changes.append(_change("flag", flag.key, payload, live_flags.get(flag.key)))+ return changes+++def resolve_cohort_refs(+ filters: dict[str, Any], cohort_ids: dict[str, int]+) -> dict[str, Any]:+ """Replace cohort-name placeholders with ids, leaving unknown names as-is."""+ return {+ **filters,+ "groups": [+ {+ **g,+ "properties": [+ (+ {**p, "value": cohort_ids.get(p["value"], p["value"])}+ if p.get("type") == "cohort"+ else p+ )+ for p in g.get("properties") or []+ ],+ }+ for g in filters.get("groups", [])+ ],+ }+++def describe_filters(filters: dict[str, Any]) -> list[str]:+ """Readable lines for a flag's filters, with email and user-id values counted, not shown."""+ variants = (filters.get("multivariate") or {}).get("variants") or []+ payloads = filters.get("payloads") or {}+ lines = [+ f"group {i}: "+ + (+ " AND ".join(describe_property(p) for p in g.get("properties") or [])+ or "everyone"+ )+ + f" @ {_rollout(g)}%"+ + (f" -> {g['variant']}" if g.get("variant") else "")+ for i, g in enumerate(filters.get("groups", []))+ ]+ lines += [+ f"variant {v['key']} ({v.get('rollout_percentage', 0)}%): "+ f"{_short(_canonical_payload(payloads.get(v['key'])))}"+ for v in variants+ ]+ return lines+++def describe_cohort(filters: dict[str, Any]) -> list[str]:+ groups = (filters.get("properties") or {}).get("values") or []+ return [+ "OR " + " AND ".join(describe_property(p) for p in g.get("values") or [])+ for g in groups+ ]+++def describe_property(prop: dict[str, Any]) -> str:+ if prop.get("type") == "cohort":+ return f"in cohort {prop.get('value')}"+ value = prop.get("value")+ if prop.get("key") in _REDACTED_VALUE_KEYS and isinstance(value, list):+ shown = f"[{len(value)} values]"+ elif prop.get("key") == "distinct_id":+ shown = "<user-id pattern>"+ else:+ shown = _redact(json.dumps(value))+ return f"{prop.get('key')} {prop.get('operator') or 'exact'} {shown}"+++class Unmappable(Exception):+ """A LaunchDarkly construct with no exact PostHog equivalent."""+++def _segment_groups(+ segment: dict[str, Any], notes: list[str]+) -> list[list[dict[str, Any]]]:+ if segment.get("unbounded"):+ raise Unmappable("big (unbounded) segment: membership lives outside the API")+ included = _user_keys(+ [{"values": segment.get("included")}, *(segment.get("includedContexts") or [])],+ notes,+ )+ excluded = _user_keys(+ [{"values": segment.get("excluded")}, *(segment.get("excludedContexts") or [])],+ notes,+ )+ groups: list[list[dict[str, Any]]] = []+ if included:+ groups.append([_person("distinct_id", "exact", included)])+ for rule in segment.get("rules") or []:+ if rule.get("weight") is not None:+ raise Unmappable("segment rule has a percentage weight")+ clauses = rule.get("clauses") or []+ if any(c.get("op") == "segmentMatch" for c in clauses):+ raise Unmappable("segment rule references another segment")+ if _reachable(clauses, notes):+ groups.append([_clause_property(c) for c in clauses])+ if excluded:+ groups = [g + [_person("distinct_id", "is_not", excluded)] for g in groups]+ return groups+++def _targets(cfg: dict[str, Any], notes: list[str]) -> dict[int, list[str]]:+ """Individual user targets by variation; ``contextTargets`` of kind user mirror ``targets``."""+ entries = (cfg.get("targets") or []) + (cfg.get("contextTargets") or [])+ by_variation = {+ variation: _user_keys(+ [t for t in entries if t["variation"] == variation], notes+ )+ for variation in sorted({t["variation"] for t in entries})+ }+ return {v: keys for v, keys in by_variation.items() if keys}+++def _user_keys(entries: list[dict[str, Any]], notes: list[str]) -> list[str]:+ keys: set[str] = set()+ for entry in entries:+ values = entry.get("values") or []+ if values and _kind_is_sent(+ entry.get("contextKind"), f"{len(values)} targets", notes+ ):+ keys.update(values)+ return sorted(keys)+++def _reachable(clauses: list[dict[str, Any]], notes: list[str]) -> bool:+ """False for a rule conditioned on a context kind no client sends: it never matched."""+ return all(_kind_is_sent(c.get("contextKind"), "a rule", notes) for c in clauses)+++def _kind_is_sent(kind: str | None, what: str, notes: list[str]) -> bool:+ if kind in (None, "user"):+ return True+ if kind in _SENT_CONTEXT_KINDS:+ raise Unmappable(+ f"{what} on context kind `{kind}`, which has no PostHog person"+ )+ notes.append(+ f"dropped {what} on context kind `{kind}`: no client sends it, so it never matched"+ )+ return False+++class _Path(BaseModel):+ """One LaunchDarkly serving path: any of ``groups`` matches -> ``variation``."""++ groups: list[list[dict[str, Any]]]+ variation: int | None+ is_target: bool = False+++def _serving_paths(+ cfg: dict[str, Any], cohorts: dict[str, MappedCohort], notes: list[str]+) -> list[_Path]:+ if cfg.get("prerequisites"):+ raise Unmappable("has prerequisite flags")+ if not cfg.get("on"):+ return [_Path(groups=[[]], variation=cfg.get("offVariation"))]++ paths = [+ _Path(+ groups=[[_person("distinct_id", "exact", keys)]],+ variation=v,+ is_target=True,+ )+ for v, keys in _targets(cfg, notes).items()+ ]+ for i, rule in enumerate(cfg.get("rules") or []):+ if rule.get("variation") is None:+ raise Unmappable(f"rule {i} serves a percentage rollout")+ clauses = rule.get("clauses") or []+ groups = _rule_groups(clauses, cohorts) if _reachable(clauses, notes) else []+ if groups:+ paths.append(_Path(groups=groups, variation=rule["variation"]))+ fallthrough = cfg.get("fallthrough") or {}+ if fallthrough.get("variation") is None:+ raise Unmappable("fallthrough serves a percentage rollout")+ paths.append(_Path(groups=[[]], variation=fallthrough["variation"]))+ return paths+++def _rule_groups(+ clauses: list[dict[str, Any]], cohorts: dict[str, MappedCohort]+) -> list[list[dict[str, Any]]]:+ """A rule's clauses as condition groups: AND within, a segment list fans out into OR."""+ alternatives: list[list[dict[str, Any]]] = []+ for clause in clauses:+ if clause.get("op") != "segmentMatch":+ alternatives.append([_clause_property(clause)])+ continue+ if clause.get("negate"):+ raise Unmappable("negated segment match")+ options = []+ for seg in clause.get("values") or []:+ cohort = cohorts.get(seg)+ if cohort is None:+ raise Unmappable(f"references segment `{seg}`, which the export lacks")+ if cohort.decisions:+ raise Unmappable(f"references segment `{seg}`, which needs a decision")+ if cohort.payload is not None:+ options.append(+ {"key": "id", "type": "cohort", "value": cohort_name(seg)}+ )+ # A rule naming only empty segments matches nobody.+ if not options:+ return []+ alternatives.append(options)+ return [list(combo) for combo in itertools.product(*alternatives)]+++def _clause_property(clause: dict[str, Any]) -> dict[str, Any]:+ attribute = ATTRIBUTE_ALIASES.get(clause["attribute"], clause["attribute"])+ if attribute not in PERSON_PROPERTIES | {"distinct_id"}:+ raise Unmappable(+ f"clause on `{clause['attribute']}`, which the backend never sends"+ )+ op, values, negate = (+ clause["op"],+ clause.get("values") or [],+ bool(clause.get("negate")),+ )++ if op == "in":+ return _person(attribute, "is_not" if negate else "exact", values)+ if op in _REGEX_OPS or op == "matches":+ parts = (+ values+ if op == "matches"+ else [_REGEX_OPS[op].format(re.escape(str(v))) for v in values]+ )+ pattern = parts[0] if len(parts) == 1 else "|".join(f"(?:{p})" for p in parts)+ return _person(attribute, "not_regex" if negate else "regex", pattern)+ if negate:+ raise Unmappable(f"negated `{op}` clause")+ if op in ("after", "before"):+ # LaunchDarkly matches ANY listed date: after the earliest, before the latest.+ dates = [_iso_date(v) for v in values]+ return _person(+ attribute,+ f"is_date_{op}",+ (min if op == "after" else max)(dates).isoformat(),+ )+ if op in _NUMERIC_OPS and len(values) == 1:+ return _person(attribute, _NUMERIC_OPS[op], values[0])+ raise Unmappable(f"`{op}` clause on `{clause['attribute']}`")+++def _person(key: str, operator: str, value: Any) -> dict[str, Any]:+ return {"key": key, "type": "person", "operator": operator, "value": value}+++def _iso_date(value: Any) -> datetime:+ if isinstance(value, (int, float)):+ return datetime.fromtimestamp(value / 1000, tz=timezone.utc)+ return datetime.fromisoformat(str(value).replace("Z", "+00:00"))+++def _boolean_filters(+ flag: dict[str, Any], paths: list[_Path]+) -> tuple[dict[str, Any], bool]:+ """A boolean flag serves true on any matching group, so a path serving false+ can only be expressed ahead of a true path as a user-id exclusion."""+ values = [v["value"] for v in flag["variations"]]+ exclusions: list[str] = []+ groups: list[dict[str, Any]] = []+ for i, path in enumerate(paths):+ serves = values[path.variation] if path.variation is not None else None+ if serves is True:+ for g in path.groups:+ props = g + (+ [_person("distinct_id", "is_not", exclusions)] if exclusions else []+ )+ groups.append(+ {"properties": props, "rollout_percentage": 100, "variant": None}+ )+ continue+ later_true = any(+ p.variation is not None and values[p.variation] is True+ for p in paths[i + 1 :]+ )+ if not later_true:+ continue+ if not path.is_target:+ raise Unmappable("a rule serves false ahead of a rule serving true")+ exclusions += path.groups[0][0]["value"]+ filters = {"groups": groups, "multivariate": None, "payloads": {}}+ return filters, bool(groups)+++def _multivariate_filters(+ flag: dict[str, Any], paths: list[_Path]+) -> tuple[dict[str, Any], bool]:+ """Every path becomes groups overriding to its variant; the payload is LaunchDarkly's value."""+ variations = flag["variations"]+ keys = _variant_keys(variations)+ if paths[0].groups == [[]] and paths[0].variation is None:+ raise Unmappable(+ "off with no off variation: LaunchDarkly returns the caller's default, "+ "PostHog would return false"+ )+ groups: list[dict[str, Any]] = []+ for path in paths:+ if path.variation is None:+ raise Unmappable("a path serves no variation")+ for g in path.groups:+ groups.append(+ {+ "properties": g,+ "rollout_percentage": 100,+ "variant": keys[path.variation],+ }+ )+ filters = {+ "groups": groups,+ "multivariate": {+ "variants": [+ {+ "key": k,+ "name": v.get("name") or None,+ "rollout_percentage": 100 if i == 0 else 0,+ }+ for i, (k, v) in enumerate(zip(keys, variations))+ ]+ },+ "payloads": {k: json.dumps(v["value"]) for k, v in zip(keys, variations)},+ }+ return filters, True+++def _variant_keys(variations: list[dict[str, Any]]) -> list[str]:+ keys: list[str] = []+ for i, v in enumerate(variations):+ source = v.get("name") or (v["value"] if isinstance(v["value"], str) else "")+ slug = re.sub(r"[^a-z0-9_-]+", "-", str(source).lower()).strip("-")[:40]+ key = candidate = slug or f"variation-{i}"+ suffix = i+ while candidate in keys:+ candidate = f"{key}-{suffix}"+ suffix += 1+ keys.append(candidate)+ return keys+++def _change(+ kind: Literal["cohort", "flag"],+ key: str,+ payload: dict[str, Any],+ existing: dict[str, Any] | None,+) -> Change:+ if existing is None:+ return Change(+ kind=kind,+ key=key,+ action="create",+ payload=payload,+ diff=_render(kind, payload),+ )+ diff: list[str] = []+ for field, desired in payload.items():+ if field == "filters":+ if _canonical(kind, existing) == _canonical(kind, payload):+ continue+ before, after = _render(kind, existing), _render(kind, payload)+ lines = [+ line+ for line in difflib.unified_diff(before, after, lineterm="", n=0)+ if not line.startswith(("---", "+++", "@@"))+ ]+ diff += lines or ["filters: changed (only in redacted values)"]+ elif field != "key" and existing.get(field) != desired:+ diff.append(f"{field}: {existing.get(field)!r} -> {desired!r}")+ return Change(+ kind=kind,+ key=key,+ action="update" if diff else "unchanged",+ payload=payload,+ diff=diff,+ existing_id=existing.get("id"),+ )+++def _canonical(kind: str, payload: dict[str, Any]) -> Any:+ filters = payload.get("filters") or {}+ if kind == "flag":+ return _canonical_filters(filters)+ return [+ [_canonical_property(p) for p in g.get("values") or []]+ for g in (filters.get("properties") or {}).get("values") or []+ ]+++def _render(kind: str, payload: dict[str, Any]) -> list[str]:+ filters = (+ _canonical_filters(payload.get("filters") or {})+ if kind == "flag"+ else payload.get("filters") or {}+ )+ return describe_filters(filters) if kind == "flag" else describe_cohort(filters)+++def _canonical_filters(filters: dict[str, Any]) -> dict[str, Any]:+ """Drop the defaults PostHog fills in, so a round-tripped flag compares equal."""+ return {+ "groups": [+ {+ "properties": [+ _canonical_property(p) for p in g.get("properties") or []+ ],+ "rollout_percentage": _rollout(g),+ "variant": g.get("variant"),+ }+ for g in filters.get("groups") or []+ ],+ "multivariate": filters.get("multivariate"),+ "payloads": filters.get("payloads") or {},+ }+++def _canonical_property(prop: dict[str, Any]) -> dict[str, Any]:+ return {k: prop.get(k) for k in ("key", "type", "operator", "value")}+++def _rollout(group: dict[str, Any]) -> int:+ rollout = group.get("rollout_percentage")+ return 100 if rollout is None else rollout+++def _canonical_payload(payload: Any) -> Any:+ if isinstance(payload, str):+ try:+ return json.loads(payload)+ except json.JSONDecodeError:+ return payload+ return payload+++def _short(value: Any, limit: int = 80) -> str:+ 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.pynew file mode 100644index 0000000000..3572ac5775--- /dev/null+++ b/autogpt_platform/backend/scripts/feature_flag_sync_test.py@@ -0,0 +1,737 @@+"""Tests for the LaunchDarkly -> PostHog flag mapping and the sync script.++Every export here is synthetic: invented keys, ids and addresses. Mapped flags+are evaluated with the PostHog SDK's own local evaluator, so a test asserts+what a user would be served, not just the payload's shape.+"""++import json+from typing import Any++import pytest+from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties++from scripts import sync_feature_flags_to_posthog as cli+from scripts.feature_flag_sync import (+ MappedCohort,+ MappedFlag,+ describe_filters,+ map_flag,+ map_segment,+ plan_sync,+ resolve_cohort_refs,+)++ENV = "test"+ADA, BOB, CAROL = "user-ada", "user-bob", "user-carol"+STAFF = {+ "email": "[email protected]",+ "email_domain": "example.org",+ "role": "authenticated",+}+ADMIN = {+ "email": "[email protected]",+ "email_domain": "elsewhere.test",+ "role": "admin",+}+OUTSIDER = {+ "email": "[email protected]",+ "email_domain": "elsewhere.test",+ "role": "authenticated",+ "created_at": "2026-01-01T00:00:00+00:00",+}++SEGMENTS = {+ "staff": {+ "key": "staff",+ "name": "Staff",+ "included": [CAROL],+ "excluded": [BOB],+ "rules": [+ {+ "clauses": [+ {+ "attribute": "email",+ "op": "endsWith",+ "values": ["@example.org"],+ "contextKind": "user",+ "negate": False,+ }+ ]+ }+ ],+ },+ "admins": {+ "key": "admins",+ "name": "Admins",+ "rules": [+ {+ "clauses": [+ {+ "attribute": "/custom/role",+ "op": "in",+ "values": ["admin"],+ "negate": False,+ }+ ]+ }+ ],+ },+ "empty": {"key": "empty", "name": "Empty", "rules": []},+}+++def test_segment_becomes_dynamic_cohort_with_members_rules_and_exclusions():+ cohort = map_segment(SEGMENTS["staff"], ENV)++ assert cohort.payload is not None+ assert cohort.payload["name"] == "ld:staff"+ assert cohort.payload["is_static"] is False+ assert cohort.payload["filters"]["properties"] == {+ "type": "OR",+ "values": [+ {+ "type": "AND",+ "values": [+ _prop("distinct_id", "exact", [CAROL]),+ _prop("distinct_id", "is_not", [BOB]),+ ],+ },+ {+ "type": "AND",+ "values": [+ _prop("email", "regex", r"@example\.org$"),+ _prop("distinct_id", "is_not", [BOB]),+ ],+ },+ ],+ }+++ "segment, reason",+ [+ ({"key": "big", "unbounded": True}, "unbounded"),+ (+ {"key": "w", "rules": [{"weight": 50000, "clauses": []}]},+ "percentage weight",+ ),+ (+ {+ "key": "n",+ "rules": [+ {+ "clauses": [+ {+ "attribute": "segmentMatch",+ "op": "segmentMatch",+ "values": ["x"],+ }+ ]+ }+ ],+ },+ "another segment",+ ),+ (+ {+ "key": "d",+ "includedContexts": [{"contextKind": "device", "values": ["dev-1"]}],+ },+ "device",+ ),+ ],+)+def test_segment_without_posthog_equivalent_needs_a_decision(segment, reason):+ cohort = map_segment(segment, ENV)++ assert cohort.payload is None+ assert reason in cohort.decisions[0].reason+++def test_boolean_flag_serves_what_launchdarkly_served():+ flag = _flag(+ "boolean",+ targets=[{"values": [ADA], "variation": 0}],+ rules=[+ _rule([_segment_clause("staff", "admins")], 0),+ _rule(+ [{"attribute": "created_at", "op": "after", "values": [1778112000000]}],+ 0,+ ),+ ],+ fallthrough=1,+ )++ mapped = map_flag(flag, ENV, _cohorts())++ assert mapped.decisions == []+ assert (mapped.targets, mapped.segments, mapped.rules) == (+ 1,+ ["admins", "staff"],+ 2,+ )+ served = _served(mapped.payload)+ assert served(ADA, {"email": "[email protected]"}) is True+ assert served("user-x", STAFF) is True+ assert served("user-x", ADMIN) is True+ assert (+ served(+ CAROL,+ {"email": "[email protected]", "created_at": "2026-01-01T00:00:00+00:00"},+ )+ is True+ )+ assert served(BOB, {**STAFF, "created_at": "2026-01-01T00:00:00+00:00"}) is False+ assert (+ served("user-x", {**OUTSIDER, "created_at": "2026-06-01T00:00:00+00:00"})+ is True+ )+ assert served("user-x", OUTSIDER) is False+++def test_false_serving_target_ahead_of_true_fallthrough_becomes_an_exclusion():+ flag = _flag("boolean", targets=[{"values": [BOB], "variation": 1}], fallthrough=0)++ served = _served(map_flag(flag, ENV, _cohorts()).payload)++ assert served(BOB, OUTSIDER) is False+ assert served(ADA, OUTSIDER) is True+++def test_false_serving_rule_ahead_of_true_rule_needs_a_decision():+ flag = _flag(+ "boolean",+ rules=[+ _rule([_email_domain("elsewhere.test")], 1),+ _rule([_segment_clause("admins")], 0),+ ],+ fallthrough=1,+ )++ mapped = map_flag(flag, ENV, _cohorts())++ assert mapped.payload is None+ assert "serves false ahead" in mapped.decisions[0].reason+++ "off_variation, active", [(1, False), (0, True), (None, False)]+)+def test_off_boolean_flag_serves_its_off_variation_to_everyone(off_variation, active):+ flag = _flag(+ "boolean",+ on=False,+ off=off_variation,+ rules=[_rule([_segment_clause("admins")], 0)],+ )++ payload = _payload(map_flag(flag, ENV, _cohorts()))++ assert payload["active"] is active+ assert _served(payload)("user-x", ADMIN) is active+++def test_multivariate_flag_serves_launchdarkly_values_as_payloads():+ flag = _flag(+ "multivariate",+ variations=[+ {"value": {"daily": 1}, "name": "Low"},+ {"value": {"daily": 9}, "name": "High"},+ {"value": "price_abc"},+ ],+ rules=[_rule([_email_domain("example.org")], 1)],+ fallthrough=0,+ )++ payload = _payload(map_flag(flag, ENV, _cohorts()))++ assert [v["key"] for v in payload["filters"]["multivariate"]["variants"]] == [+ "low",+ "high",+ "price_abc",+ ]+ served = _served(payload)+ assert served("user-x", STAFF) == {"daily": 9}+ assert served("system", {}) == {"daily": 1}+++def test_variant_keys_stay_unique_when_a_fallback_collides_with_a_name():+ flag = _flag(+ "multivariate",+ variations=[+ {"value": 1, "name": "x"},+ {"value": 2, "name": "x-2"},+ {"value": 3, "name": "x"},+ ],+ fallthrough=2,+ )++ payload = _payload(map_flag(flag, ENV, _cohorts()))++ keys = [v["key"] for v in payload["filters"]["multivariate"]["variants"]]+ assert len(set(keys)) == 3+ assert _served(payload)("user-x", {}) == 3+++def test_system_keyed_config_flag_resolves_without_person_properties():+ flag = _flag(+ "multivariate", variations=[{"value": {"PRO": 5}}, {"value": {}}], fallthrough=0+ )++ assert _served(map_flag(flag, ENV, _cohorts()).payload)("system", {}) == {"PRO": 5}+++def test_rule_on_only_empty_segments_matches_nobody():+ flag = _flag("boolean", rules=[_rule([_segment_clause("empty")], 0)], fallthrough=1)++ mapped = map_flag(flag, ENV, _cohorts())++ assert _payload(mapped)["active"] is False+++def test_target_on_a_context_kind_no_client_sends_is_dropped_with_a_note():+ flag = _flag(+ "boolean",+ context_targets=[+ {"contextKind": "email", "values": ["[email protected]"], "variation": 0}+ ],+ fallthrough=1,+ )++ mapped = map_flag(flag, ENV, _cohorts())++ assert _payload(mapped)["active"] is False+ assert "never matched" in mapped.notes[0]+++ "overrides, reason",+ [+ ({"prerequisites": [{"key": "other", "variation": 0}]}, "prerequisite"),+ ({"fallthrough": {"rollout": {"variations": []}}}, "percentage rollout"),+ (+ {"rules": [{"rollout": {"variations": []}, "clauses": []}]},+ "percentage rollout",+ ),+ (+ {+ "rules": [+ {+ "clauses": [+ {"attribute": "country", "op": "in", "values": ["NL"]}+ ],+ "variation": 0,+ }+ ]+ },+ "never sends",+ ),+ (+ {+ "rules": [+ {+ "clauses": [+ {+ "attribute": "segmentMatch",+ "op": "segmentMatch",+ "values": ["unknown"],+ }+ ],+ "variation": 0,+ }+ ]+ },+ "export lacks",+ ),+ (+ {+ "rules": [+ {+ "clauses": [+ {+ "attribute": "segmentMatch",+ "op": "segmentMatch",+ "values": ["admins"],+ "negate": True,+ }+ ],+ "variation": 0,+ }+ ]+ },+ "negated segment",+ ),+ (+ {+ "rules": [+ {+ "clauses": [+ {+ "attribute": "created_at",+ "op": "after",+ "values": [0],+ "negate": True,+ }+ ],+ "variation": 0,+ }+ ]+ },+ "negated",+ ),+ (+ {+ "contextTargets": [+ {"contextKind": "device", "values": ["d"], "variation": 0}+ ]+ },+ "device",+ ),+ ],+)+def test_unmappable_targeting_needs_a_decision(overrides, reason):+ flag = _flag("boolean", fallthrough=1)+ flag["environments"][ENV].update(overrides)++ mapped = map_flag(flag, ENV, _cohorts())++ assert mapped.payload is None+ assert reason in mapped.decisions[0].reason+++def test_multivariate_off_without_off_variation_needs_a_decision():+ flag = _flag(+ "multivariate", variations=[{"value": {}}, {"value": []}], on=False, off=None+ )++ assert "caller's default" in map_flag(flag, ENV, _cohorts()).decisions[0].reason+++def test_server_only_flag_keeps_off_the_browser():+ flag = _flag("boolean", fallthrough=0)+ flag["clientSideAvailability"] = {"usingEnvironmentId": False}++ assert _payload(map_flag(flag, ENV, _cohorts()))["evaluation_runtime"] == "server"+++def test_plan_creates_missing_updates_changed_and_leaves_matching_alone():+ cohorts = [map_segment(SEGMENTS["admins"], ENV)]+ by_key = {c.segment_key: c for c in cohorts}+ admins_only = map_flag(+ _flag(+ "boolean",+ key="admins-only",+ rules=[_rule([_segment_clause("admins")], 0)],+ fallthrough=1,+ ),+ ENV,+ by_key,+ )+ everyone = map_flag(_flag("boolean", key="everyone", fallthrough=0), ENV, by_key)+ fresh = map_flag(_flag("boolean", key="fresh", fallthrough=0), ENV, by_key)+ existing_cohort = {**_payload(cohorts[0]), "id": 7}+ existing_flags = [+ {**_with_cohort_ids(_payload(admins_only), {"ld:admins": 7}), "id": 1},+ {**_payload(everyone), "id": 2, "active": False},+ {"key": "session-recording", "id": 3, "filters": {}},+ {**_payload(fresh), "id": 4, "deleted": True},+ ]++ changes = {+ c.key: c+ for c in plan_sync(+ cohorts, [admins_only, everyone, fresh], [existing_cohort], existing_flags+ )+ }++ assert changes["ld:admins"].action == "unchanged"+ assert changes["admins-only"].action == "unchanged"+ assert changes["everyone"].action == "update"+ assert changes["everyone"].diff == ["active: False -> True"]+ assert changes["everyone"].existing_id == 2+ assert changes["fresh"].action == "create"+ assert "session-recording" not in changes+++def test_plan_names_a_cohort_it_has_yet_to_create_and_skips_undecided_flags():+ cohorts = [+ map_segment(SEGMENTS["admins"], ENV),+ map_segment({"key": "big", "unbounded": True}, ENV),+ ]+ by_key = {c.segment_key: c for c in cohorts}+ admins_only = map_flag(+ _flag(+ "boolean", key="admins-only", rules=[_rule([_segment_clause("admins")], 0)]+ ),+ ENV,+ by_key,+ )+ big_only = map_flag(+ _flag("boolean", key="big-only", rules=[_rule([_segment_clause("big")], 0)]),+ ENV,+ by_key,+ )++ changes = {c.key: c for c in plan_sync(cohorts, [admins_only, big_only], [], [])}++ assert changes["ld:admins"].action == "create"+ assert changes["admins-only"].diff == ["group 0: in cohort ld:admins @ 100%"]+ assert changes["big"].action == "needs-decision"+ assert changes["big-only"].action == "needs-decision"+ assert changes["big-only"].payload is None+++def test_plan_updates_a_change_hidden_by_redaction():+ target = map_flag(+ _flag("boolean", key="t", targets=[{"values": [ADA], "variation": 0}]), ENV, {}+ )+ existing = _payload(target)+ existing = {+ **existing,+ "id": 5,+ "filters": {+ **existing["filters"],+ "groups": [+ {+ **existing["filters"]["groups"][0],+ "properties": [_prop("distinct_id", "exact", [BOB])],+ }+ ],+ },+ }++ [change] = plan_sync([], [target], [], [existing])++ assert change.action == "update"+ assert change.diff == ["filters: changed (only in redacted values)"]+++def test_plan_updates_a_cohort_whose_redacted_members_changed():+ cohort = map_segment({"key": "vip", "included": [ADA]}, ENV)+ existing = {+ **_payload(cohort),+ "id": 8,+ "filters": {+ "properties": {+ "type": "OR",+ "values": [+ {"type": "AND", "values": [_prop("distinct_id", "exact", [BOB])]}+ ],+ }+ },+ }++ [change] = plan_sync([cohort], [], [existing], [])++ assert change.action == "update"+++def test_described_filters_never_show_addresses_or_user_ids():+ uid = "123e4567-e89b-12d3-a456-426614174000"+ flag = _flag(+ "multivariate",+ variations=[{"value": [uid, "[email protected]"]}, {"value": []}],+ targets=[{"values": [uid], "variation": 0}],+ rules=[+ _rule(+ [{"attribute": "email", "op": "in", "values": ["[email protected]"]}], 0+ ),+ _rule(+ [+ {+ "attribute": "email",+ "op": "startsWith",+ "values": ["[email protected]"],+ }+ ],+ 0,+ ),+ _rule(+ [{"attribute": "key", "op": "matches", "values": ["^customer-123$"]}], 0+ ),+ ],+ fallthrough=1,+ )++ text = "\n".join(+ describe_filters(_payload(map_flag(flag, ENV, _cohorts()))["filters"])+ )++ assert uid not in text and "[email protected]" not in text+ assert "ada@" not in text and "customer-123" not in text+ assert "[1 values]" in text+++def test_apply_without_an_explicit_project_is_refused(monkeypatch, capsys):+ monkeypatch.setattr(cli._Client, "send", _no_network)++ with pytest.raises(SystemExit) as exc:+ cli.main(["--ld-env", ENV, "--apply"])++ assert exc.value.code == 2+ assert "--project" in capsys.readouterr().err+++def test_dry_run_reads_but_never_writes(monkeypatch):+ calls = _fake_apis(monkeypatch)++ assert cli.main(["--ld-env", ENV]) == 0++ assert {method for method, _ in calls} == {"GET"}+ assert any("/api/projects/@current/" in url for _, url in calls)+++def test_apply_creates_cohorts_before_the_flags_that_reference_them(monkeypatch):+ calls = _fake_apis(monkeypatch)++ assert cli.main(["--ld-env", ENV, "--apply", "--project", "42"]) == 0++ writes = [(m, url, body) for m, url, body in calls.bodies if m != "GET"]+ assert [(m, url.rsplit("/api/projects/42/", 1)[1]) for m, url, _ in writes] == [+ ("POST", "cohorts/"),+ ("POST", "feature_flags/"),+ ]+ flag_body = writes[1][2]+ assert flag_body["filters"]["groups"][0]["properties"] == [+ {"key": "id", "type": "cohort", "value": 99}+ ]+++def _with_cohort_ids(payload: dict[str, Any], ids: dict[str, int]) -> dict[str, Any]:+ return {**payload, "filters": resolve_cohort_refs(payload["filters"], ids)}+++def _served(payload: dict[str, Any] | None):+ """What the PostHog SDK's local evaluator serves a user for this mapped flag."""+ assert payload is not None+ cohorts = [_payload(c) for c in _cohorts().values() if c.payload is not None]+ ids = {c["name"]: i for i, c in enumerate(cohorts, start=1)}+ cohort_properties = {+ str(ids[c["name"]]): c["filters"]["properties"] for c in cohorts+ }+ flag = _with_cohort_ids(payload, ids)++ def serve(distinct_id: str, properties: dict[str, Any]) -> Any:+ if not flag["active"]:+ return False+ try:+ value = match_feature_flag_properties(+ flag,+ distinct_id,+ {"distinct_id": distinct_id, **properties},+ cohort_properties=cohort_properties,+ bucketing_value=distinct_id,+ )+ except InconclusiveMatchError:+ return None+ if isinstance(value, bool):+ return value+ return json.loads(flag["filters"]["payloads"][value])++ return serve+++def _payload(mapped: MappedFlag | MappedCohort) -> dict[str, Any]:+ assert mapped.payload is not None, mapped.decisions+ return mapped.payload+++def _cohorts() -> dict[str, MappedCohort]:+ return {key: map_segment(segment, ENV) for key, segment in SEGMENTS.items()}+++def _flag(+ kind: str,+ *,+ key: str = "flag",+ variations: list[dict[str, Any]] | None = None,+ on: bool = True,+ off: int | None = 1,+ targets: list[dict[str, Any]] | None = None,+ context_targets: list[dict[str, Any]] | None = None,+ rules: list[dict[str, Any]] | None = None,+ fallthrough: int = 1,+) -> dict[str, Any]:+ return {+ "key": key,+ "name": key.title(),+ "kind": kind,+ "clientSideAvailability": {"usingEnvironmentId": True},+ "variations": variations or [{"value": True}, {"value": False}],+ "environments": {+ ENV: {+ "on": on,+ "offVariation": off,+ "targets": targets or [],+ "contextTargets": context_targets or [],+ "rules": rules or [],+ "fallthrough": {"variation": fallthrough},+ "prerequisites": [],+ }+ },+ }+++def _rule(clauses: list[dict[str, Any]], variation: int) -> dict[str, Any]:+ return {"clauses": clauses, "variation": variation}+++def _segment_clause(*segments: str) -> dict[str, Any]:+ return {+ "attribute": "segmentMatch",+ "op": "segmentMatch",+ "values": list(segments),+ "negate": False,+ }+++def _email_domain(domain: str) -> dict[str, Any]:+ return {+ "attribute": "email_domain",+ "op": "in",+ "values": [domain],+ "negate": False,+ }+++def _prop(key: str, operator: str, value: Any) -> dict[str, Any]:+ return {"key": key, "type": "person", "operator": operator, "value": value}+++def _no_network(*_args, **_kwargs):+ raise AssertionError("no request may be made")+++class _Calls(list):+ bodies: list[tuple[str, str, Any]]+++def _fake_apis(monkeypatch) -> _Calls:+ """Canned LaunchDarkly export and an empty PostHog project, recording every request."""+ monkeypatch.setenv("LAUNCHDARKLY_API_TOKEN", "ld-test-token")+ monkeypatch.setenv("POSTHOG_PERSONAL_API_KEY", "phx-test-key")+ flag = _flag(+ "boolean", key="admins-only", rules=[_rule([_segment_clause("admins")], 0)]+ )+ calls = _Calls()+ calls.bodies = []++ def send(self, method: str, path_or_url: str, body: Any = None) -> Any:+ url = path_or_url if path_or_url.startswith("http") else self.base + path_or_url+ calls.append((method, url))+ calls.bodies.append((method, url, body))+ if "/flags/default" in url:+ return {"items": [flag], "totalCount": 1}+ if "/segments/default/" in url:+ return SEGMENTS["admins"]+ if url.endswith(("/@current/", "/42/")):+ return {"id": 42, "name": "Test project"}+ if method == "POST" and url.endswith("/cohorts/"):+ return {"id": 99}+ if method == "GET":+ return {"results": [], "next": None}+ return {}++ monkeypatch.setattr(cli._Client, "send", send)+ return callsdiff --git a/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.py b/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.pynew file mode 100644index 0000000000..7ca85ba1c9--- /dev/null+++ b/autogpt_platform/backend/scripts/sync_feature_flags_to_posthog.py@@ -0,0 +1,282 @@+"""Re-create LaunchDarkly's feature flags and segments in a PostHog project.++Reads one LaunchDarkly environment's flags with their targeting, maps them+with ``scripts/feature_flag_sync.py`` (segments become dynamic cohorts,+individual targets ``distinct_id`` conditions, rules condition groups, JSON+variations payloads) and prints a plan against what the PostHog project+already holds. Dry run by default::++ LAUNCHDARKLY_API_TOKEN=… POSTHOG_PERSONAL_API_KEY=… \\+ poetry run python -m scripts.sync_feature_flags_to_posthog --ld-env test++``--apply --project <id>`` performs the plan: creates and updates by key,+never deletes. It is refused without an explicit project id. Flags or+cohorts listed under "needs a decision" are never written.++The output counts email and user-id values instead of printing them.+"""++from __future__ import annotations++import argparse+import json+import os+import sys+import urllib.error+import urllib.parse+import urllib.request+from typing import Any++from scripts.feature_flag_sync import (+ Change,+ MappedCohort,+ MappedFlag,+ describe_cohort,+ describe_filters,+ map_flag,+ map_segment,+ plan_sync,+ resolve_cohort_refs,+)++LD_API = "https://app.launchdarkly.com/api/v2"+DEFAULT_POSTHOG_HOST = "https://eu.posthog.com"+++def main(argv: list[str] | None = None) -> int:+ parser = argparse.ArgumentParser(description=(__doc__ or "").split("\n\n")[0])+ parser.add_argument(+ "--ld-env",+ required=True,+ help="LaunchDarkly environment: test (Dev) or production",+ )+ parser.add_argument("--ld-project", default="default")+ parser.add_argument(+ "--project",+ type=int,+ help="PostHog project id (default for a dry run: the key's own)",+ )+ parser.add_argument("--posthog-host", default=DEFAULT_POSTHOG_HOST)+ parser.add_argument(+ "--flag", action="append", default=[], help="limit to these LaunchDarkly keys"+ )+ parser.add_argument(+ "--apply", action="store_true", help="perform the plan (requires --project)"+ )+ args = parser.parse_args(argv)+ if args.apply and args.project is None:+ parser.error("--apply writes to PostHog and needs an explicit --project <id>")++ ld = _Client(LD_API, {"Authorization": _env("LAUNCHDARKLY_API_TOKEN")})+ posthog = _Client(+ args.posthog_host,+ {"Authorization": f"Bearer {_env('POSTHOG_PERSONAL_API_KEY')}"},+ )++ flags = fetch_ld_flags(ld, args.ld_project, args.ld_env, set(args.flag))+ cohorts = [+ map_segment(s, args.ld_env)+ for s in fetch_ld_segments(ld, args.ld_project, args.ld_env, flags)+ ]+ by_segment = {c.segment_key: c for c in cohorts}+ mapped = [map_flag(f, args.ld_env, by_segment) for f in flags]++ project = posthog.get(f"/api/projects/{args.project or '@current'}/")+ project_path = f"/api/projects/{project['id']}"+ existing_flags = posthog.paginate(f"{project_path}/feature_flags/?limit=100")+ existing_cohorts = posthog.paginate(f"{project_path}/cohorts/?limit=100")+ changes = plan_sync(cohorts, mapped, existing_cohorts, existing_flags)++ print(+ f"LaunchDarkly `{args.ld_project}`/{args.ld_env} -> PostHog project {project['id']} ({project['name']})\n"+ )+ print_mapping(mapped, cohorts)+ print_plan(+ changes,+ {f["key"] for f in existing_flags if not f.get("deleted")}+ - {m.key for m in mapped},+ )+ if not args.apply:+ print(+ "\nDry run: nothing was written. Re-run with --apply --project <id> to perform it."+ )+ return 0+ apply_plan(posthog, project_path, changes)+ return 0+++def fetch_ld_flags(+ ld: _Client, project: str, env: str, only: set[str]+) -> list[dict[str, Any]]:+ """Every non-archived flag with *env*'s full configuration (``summary=0``)."""+ flags: list[dict[str, Any]] = []+ while True:+ page = ld.get(+ f"/flags/{project}?env={env}&summary=0&limit=100&offset={len(flags)}"+ )+ flags += page["items"]+ if not page["items"] or len(flags) >= page["totalCount"]:+ break+ return [+ f for f in flags if not f.get("archived") and (not only or f["key"] in only)+ ]+++def fetch_ld_segments(+ ld: _Client, project: str, env: str, flags: list[dict[str, Any]]+) -> list[dict[str, Any]]:+ """Each segment a flag's rule references, fetched whole: a listing may truncate member lists."""+ keys = sorted(+ {+ s+ for f in flags+ for r in f["environments"][env].get("rules") or []+ for c in r.get("clauses") or []+ if c.get("op") == "segmentMatch"+ for s in c.get("values") or []+ }+ )+ segments = []+ for key in keys:+ try:+ segments.append(+ ld.get(f"/segments/{project}/{env}/{urllib.parse.quote(key)}")+ )+ except urllib.error.HTTPError as e:+ if e.code != 404:+ raise+ return segments+++def print_mapping(flags: list[MappedFlag], cohorts: list[MappedCohort]) -> None:+ print("## Cohorts (one per referenced LaunchDarkly segment)")+ for c in cohorts:+ if c.payload is None:+ print(+ f"- {c.segment_key}: "+ + (+ "needs a decision"+ if c.decisions+ else "no members; conditions on it match nobody"+ )+ )+ continue+ print(f"- {c.segment_key} -> cohort `{c.payload['name']}`")+ for line in describe_cohort(c.payload["filters"]) + c.notes:+ print(f" {line}")++ targeted = [f for f in flags if f.targeted]+ print(f"\n## Flags with targeting ({len(targeted)} of {len(flags)})")+ for f in targeted:+ print(+ f"- {f.key} [{f.kind}] targets={f.targets} segments={','.join(f.segments) or '-'} rules={f.rules}"+ )+ for note in f.notes:+ print(f" note: {note}")+ if f.payload is None:+ print(" needs a decision")+ continue+ print(+ f" active={f.payload['active']} runtime={f.payload['evaluation_runtime']}"+ )+ for line in describe_filters(f.payload["filters"]):+ print(f" {line}")++ decisions = [d for x in [*cohorts, *flags] for d in x.decisions]+ print(f"\n## Needs a decision ({len(decisions)}; never written)")+ for d in decisions:+ print(f"- {d.subject}: {d.reason}")+++def print_plan(changes: list[Change], untouched: set[str]) -> None:+ counts: dict[str, int] = {}+ for c in changes:+ counts[f"{c.kind} {c.action}"] = counts.get(f"{c.kind} {c.action}", 0) + 1+ print("\n## Plan")+ print(" " + ", ".join(f"{n} {k}" for k, n in sorted(counts.items())))+ for c in changes:+ if c.action == "unchanged":+ continue+ print(f"- {c.kind} {c.key}: {c.action}")+ for line in c.diff:+ print(f" {line}")+ if untouched:+ print(f"\nIn PostHog only, left alone: {', '.join(sorted(untouched))}")+++def apply_plan(posthog: _Client, project_path: str, changes: list[Change]) -> None:+ """Cohorts first, so flags can reference their ids; creates and updates only."""+ cohort_ids: dict[str, int] = {}+ for c in changes:+ if c.kind != "cohort" or c.payload is None:+ continue+ if c.action == "create":+ cohort_ids[c.key] = posthog.send(+ "POST", f"{project_path}/cohorts/", c.payload+ )["id"]+ elif c.existing_id is not None:+ if c.action == "update":+ posthog.send(+ "PATCH", f"{project_path}/cohorts/{c.existing_id}/", c.payload+ )+ cohort_ids[c.key] = c.existing_id+ print(f"cohort {c.key}: {c.action}")+ for c in changes:+ if (+ c.kind != "flag"+ or c.action not in ("create", "update")+ or c.payload is None+ ):+ continue+ payload = {+ **c.payload,+ "filters": resolve_cohort_refs(c.payload["filters"], cohort_ids),+ }+ if c.action == "create":+ posthog.send("POST", f"{project_path}/feature_flags/", payload)+ else:+ posthog.send(+ "PATCH", f"{project_path}/feature_flags/{c.existing_id}/", payload+ )+ print(f"flag {c.key}: {c.action}")+++class _Client:+ def __init__(self, base: str, headers: dict[str, str]):+ self.base = base.rstrip("/")+ self.headers = headers++ def get(self, path_or_url: str) -> Any:+ return self.send("GET", path_or_url)++ def paginate(self, path: str) -> list[dict[str, Any]]:+ """Every page of a PostHog listing, following ``next`` until it is absent."""+ results: list[dict[str, Any]] = []+ url: str | None = path+ while url:+ page = self.get(url)+ results += page["results"]+ url = page.get("next")+ return results++ def send(self, method: str, path_or_url: str, body: Any = None) -> Any:+ url = path_or_url if path_or_url.startswith("http") else self.base + path_or_url+ request = urllib.request.Request(+ url,+ method=method,+ 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)+++def _env(name: str) -> str:+ value = os.environ.get(name)+ if not value:+ sys.exit(f"{name} is not set")+ return value+++if __name__ == "__main__":+ sys.exit(main())