Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/person-metadata-filters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Local flag evaluation treats a filter type it cannot evaluate, such as a cohort's `person_metadata` filter on first seen, as unsupported and falls back to `/flags`, instead of matching it against a person property with the same key.
4 changes: 2 additions & 2 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4279,8 +4279,8 @@ def _log_unresolvable_flags(
self.log.warning(
f"[FEATURE FLAGS] {len(unsupported_definition_keys)} flag(s) use "
"something local evaluation doesn't support, such as a static "
"cohort, an unknown operator, a malformed value, or a missing or "
"circular flag dependency "
"cohort, an unknown operator or filter type, a malformed value, or a "
"missing or circular flag dependency "
f"(reason: {UnresolvedFlagReason.UNSUPPORTED_DEFINITION.value}): "
f"{', '.join(unsupported_definition_keys)}. Evaluating them makes a "
"remote request when evaluation reaches that part, or leaves them "
Expand Down
49 changes: 45 additions & 4 deletions posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ def __init__(self, *args, reason: Optional[UnresolvedFlagReason] = None):
self.reason = reason


class _RequiresServerFilter(RequiresServerEvaluation):
"""
Raised for a single filter that only the server can check. Like any
RequiresServerEvaluation it stops later conditions from deciding the flag, but in a
cohort another filter can still make the result certain.
"""


# When several causes apply to one flag, the spec reports the first reason in this
# order, so every SDK picks the same reason whatever order it inspects filters in.
_UNRESOLVED_REASON_PRECEDENCE = (
Expand Down Expand Up @@ -458,8 +466,9 @@ def _evaluate_dependency(
except RequiresServerEvaluation as e:
# The dependent flag needs the server too, so its later conditions must
# not resolve it locally. Nothing is cached, so another filter on this
# dependency also raises this error.
raise RequiresServerEvaluation(
# dependency also raises this error. The type is kept, so a cohort treats the
# dependency like the filter that stopped it.
raise type(e)(
f"Cannot evaluate flag dependency '{dep_flag_key}': {e}",
reason=UnresolvedFlagReason.UNRESOLVED_DEPENDENCY,
) from e
Expand Down Expand Up @@ -629,6 +638,7 @@ def _match_flag_conditions(
cohort_properties,
group_type_mapping,
)
e.reason = _pick_unresolved_reason(inconclusive_reason, e.reason)
raise
except InconclusiveMatchError as e:
# Evaluation error (bad regex, invalid date, missing property, etc.)
Expand Down Expand Up @@ -795,6 +805,24 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool:
return enrollment is True or enrollment == "true"


# Filter types matched against the person or group properties the caller passes. Cohort
# and flag filters have their own matching. Any other type, such as "person_metadata"
# (a value /flags reads from the persons table, not from person properties), can't be
# evaluated locally: matching it against a caller property with the same key could
# disagree with /flags. No caller can supply the value, so a later condition must not
# decide the flag either.
_PROPERTY_FILTER_TYPES = ("person", "group")


def _assert_property_filter_type_supported(prop) -> None:
property_type = prop.get("type")
if property_type and property_type not in _PROPERTY_FILTER_TYPES:
raise _RequiresServerFilter(
f"Can't evaluate a '{property_type}' filter on '{prop.get('key')}' locally",
reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION,
)


def _match_condition_filter(
prop,
properties,
Expand Down Expand Up @@ -829,6 +857,7 @@ def _match_condition_filter(
device_id=device_id,
property_matching_version=property_matching_version,
)
_assert_property_filter_type_supported(prop)
return match_property(prop, properties, property_matching_version)


Expand Down Expand Up @@ -930,6 +959,7 @@ def _raise_condition_errors(feature_flag, server_evaluation_error, inconclusive_
# Re-raise the original error type: match_feature_flag_properties stops at it
# and falls back to the server instead of trying later conditions.
if server_evaluation_error is not None:
server_evaluation_error.reason = inconclusive_reason
raise server_evaluation_error
if inconclusive_reason is not None:
raise InconclusiveMatchError(
Expand Down Expand Up @@ -1715,6 +1745,7 @@ def match_property_group(

decisive_result = None
inconclusive_reason: Optional[UnresolvedFlagReason] = None
server_filter_error: Optional[_RequiresServerFilter] = None

for prop in properties:
try:
Expand All @@ -1730,16 +1761,25 @@ def match_property_group(
)
if effective_match != is_and:
decisive_result = effective_match
except RequiresServerEvaluation:
except _RequiresServerFilter as e:
server_filter_error = server_filter_error or e
Comment thread
marandaneto marked this conversation as resolved.
Resolved
inconclusive_reason = _pick_unresolved_reason(inconclusive_reason, e.reason)
except RequiresServerEvaluation as e:
# Static/missing cohorts and malformed definitions always require server evaluation,
# even when another branch would otherwise resolve the group locally.
e.reason = _pick_unresolved_reason(inconclusive_reason, e.reason)
raise
except InconclusiveMatchError as e:
log.debug(f"Failed to compute property {prop} locally: {e}")
inconclusive_reason = _pick_unresolved_reason(inconclusive_reason, e.reason)

if decisive_result is not None:
return decisive_result
if server_filter_error is not None:
server_filter_error.reason = _pick_unresolved_reason(
inconclusive_reason, server_filter_error.reason
)
raise server_filter_error
if inconclusive_reason is not None:
raise InconclusiveMatchError(
"Can't match cohort without a given cohort property value",
Expand Down Expand Up @@ -1841,8 +1881,9 @@ def _is_unsupported_property(
return _is_unsupported_dependency(prop, flags_by_key)
if property_type != "cohort":
try:
_assert_property_filter_type_supported(prop)
Comment thread
marandaneto marked this conversation as resolved.
Resolved
_assert_filter_supported(prop)
except InconclusiveMatchError:
except (InconclusiveMatchError, RequiresServerEvaluation):
return True
return False
cohort_id = str(prop.get("value"))
Expand Down
Loading
Loading