diff --git a/.sampo/changesets/local-flag-config-version-dispatch.md b/.sampo/changesets/local-flag-config-version-dispatch.md new file mode 100644 index 000000000..c31cd2eca --- /dev/null +++ b/.sampo/changesets/local-flag-config-version-dispatch.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Local evaluation now chooses the evaluator of each flag definition by its configuration version. Definitions in the current format are evaluated as before, and the definitions request is unchanged. diff --git a/posthog/_feature_flags_v2.py b/posthog/_feature_flags_v2.py index 738ddda71..1c24535b3 100644 --- a/posthog/_feature_flags_v2.py +++ b/posthog/_feature_flags_v2.py @@ -1,6 +1,7 @@ """Local evaluation of feature flags whose ``filters.version`` is 2. -Nothing in the SDK calls this module yet, and no name in it is public. +The client calls :func:`_evaluate_definition` for every loaded definition whose config +version is not 1. No name in this module is public. A config version 2 flag holds an ordered list of rules. The first rule whose targeting matches decides the result, unless its rollout misses and the rule says to continue. @@ -19,7 +20,9 @@ - :class:`_EvaluationError`: a property value or a regular expression is invalid. None of them is a guess. A caller that cannot use the result must fall back to the -flags service, not to ``False`` or to the flag default. +flags service, not to ``False`` or to the flag default. :func:`_evaluate_definition` +does that for the client: it turns a result into the record a ``/flags?v=4`` response +gives, and anything else into the unresolved reason of the client's fallback. The ``exact`` and text operators and regular expressions use the helpers of the version 1 evaluator, which already follow the flags service. Numbers, semantic versions and @@ -35,6 +38,7 @@ from __future__ import annotations +import copy import datetime import hashlib import math @@ -45,6 +49,7 @@ Any, Callable, ClassVar, + Dict, FrozenSet, List, Mapping, @@ -58,6 +63,7 @@ InconclusiveMatchError, _ascii_lower, _compile_regex, + _is_config_version_1, _match_exact_property, _match_regex, _match_text_property, @@ -65,7 +71,13 @@ _subtract_years, _value_to_string, ) -from .types import UnresolvedFlagReason +from .types import ( + FeatureFlag, + UnresolvedFlagReason, + _MalformedRecord, + _parse_record, + _read_int, +) _CONFIG_VERSION = 2 _ASSIGNMENT_ALGORITHM = "sha1_60_v1" @@ -1454,3 +1466,142 @@ def _instant(wall_clock: datetime.datetime, nanoseconds: int, offset: int) -> _I days = wall_clock.toordinal() - _EPOCH_ORDINAL seconds = wall_clock.hour * 3600 + wall_clock.minute * 60 + wall_clock.second return (days * 86_400 + seconds - offset, nanoseconds) + + +# --- Local evaluation in the client -------------------------------------------------- +# +# The client evaluates a loaded definition here when its config version is not 1. The +# definitions request does not select config version 2 flags yet, so none arrives from +# the API; one can still come from a definition cache that a newer release shares. + +# The reason description of a locally evaluated record, as for a version 1 flag. +_LOCAL_REASON_DESCRIPTION = "Evaluated locally" + + +class _ParsedDefinition(NamedTuple): + """A loaded definition and its ``filters``, parsed once when definitions load.""" + + definition: Mapping[str, Any] + config: Union[_Config, _Unsupported] + + +def _parse_definitions(flags: List[Any]) -> Dict[str, _ParsedDefinition]: + """Parse each config version 2 definition of a definitions list, by flag key.""" + # A definition that is not a dict reads as version 1, so each one here is a dict. + return { + flag["key"]: _ParsedDefinition(flag, _parse_config(flag.get("filters"))) + for flag in flags + if not _is_config_version_1(flag) and flag.get("key") is not None + } + + +def _config_for( + definition: Mapping[str, Any], parsed: Mapping[Any, _ParsedDefinition] +) -> Union[_Config, _Unsupported]: + """The parsed ``filters`` of a definition, read now if it was not parsed on load.""" + entry = parsed.get(definition.get("key")) + if entry is not None and entry.definition is definition: + return entry.config + return _parse_config(definition.get("filters")) + + +def _unresolved_reason( + definition: Mapping[str, Any], parsed: Mapping[Any, _ParsedDefinition] +) -> Optional[UnresolvedFlagReason]: + """The reason the definition alone leaves the flag unresolved, known on load.""" + if definition.get("active") and isinstance( + _config_for(definition, parsed), _Unsupported + ): + return UnresolvedFlagReason.UNSUPPORTED_DEFINITION + return None + + +def _evaluate_definition( + definition: Mapping[str, Any], + parsed: Mapping[Any, _ParsedDefinition], + *, + distinct_id: Any, + properties: Mapping[str, Any], + property_matching_version: int, +) -> FeatureFlag: + """Evaluate a config version 2 definition for one person. + + Returns the record a ``/flags?v=4`` response gives for the same result, so the + client reads it and sends its flag events as for a remote record. Raises + :class:`InconclusiveMatchError` with the unresolved reason when the evaluator + gives no value; the caller then falls back as for a version 1 flag. + + The flags service evaluates these flags with the distinct ID only, so experience + continuity and device bucketing do not apply. The caller never knows that its + properties are complete, and dates without an offset are read in UTC, as version + 1 local evaluation reads them. + """ + outcome = _evaluate( + _config_for(definition, parsed), + _Context( + identifier=str(distinct_id), + properties=properties, + properties_complete=False, + explicit_exact_matching=property_matching_version == 2, + timezone=datetime.timezone.utc, + ), + active=bool(definition.get("active")), + ) + if isinstance(outcome, _Evaluated): + record = _record(definition, outcome) + if not record._failed: + return record + outcome = _Unsupported("malformed", record._malformed_field or "record") + raise InconclusiveMatchError(_describe(outcome), reason=_reason_of(outcome)) + + +def _record(definition: Mapping[str, Any], outcome: _Evaluated) -> FeatureFlag: + key = definition["key"] + metadata: Dict[str, Any] = { + "config_version": _CONFIG_VERSION, + "description": None, + "payload": None, + } + for field in ("id", "version"): + try: + metadata[field] = _read_int(definition.get(field), field) + except _MalformedRecord: + pass # Left out, so that a bad value does not fail the whole record. + if isinstance(definition.get("has_experiment"), bool): + metadata["has_experiment"] = definition["has_experiment"] + for field, value in ( + ("rule_type", outcome.rule_type), + ("rule_id", outcome.rule_id), + ("variant_key", outcome.variant_key), + ): + if value is not None: + metadata[field] = value + response = { + "key": key, + # An object value is the configuration's own object. + "value": copy.deepcopy(outcome.value), + "reason": { + "code": outcome.reason, + "condition_index": outcome.rule_index, + "description": _LOCAL_REASON_DESCRIPTION, + }, + "metadata": metadata, + } + return _parse_record(FeatureFlag, key, response) + + +def _reason_of( + outcome: Union[_Inconclusive, _Unsupported, _EvaluationError], +) -> UnresolvedFlagReason: + if isinstance(outcome, _Inconclusive): + return outcome.reason + if isinstance(outcome, _EvaluationError) and outcome.kind == "invalid_property": + # The person property is not in a form the rule can compare. + return UnresolvedFlagReason.MISSING_CONTEXT + return UnresolvedFlagReason.UNSUPPORTED_DEFINITION + + +def _describe(outcome: Union[_Inconclusive, _Unsupported, _EvaluationError]) -> str: + if isinstance(outcome, _Unsupported): + return f"The config version 2 document is {outcome.kind} at '{outcome.field}'" + return outcome.detail diff --git a/posthog/client.py b/posthog/client.py index 7d4de2c8a..51cf1c95f 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -114,6 +114,7 @@ ) from posthog.feature_flag_evaluations import ( FeatureFlagEvaluations, + _LocalDetailsFlagRecord, _LocalFlagRecord, _EvaluatedFlagRecord, _RemoteFlagRecord, @@ -124,9 +125,16 @@ RequiresServerEvaluation, _get_definition_unresolved_reason, _group_key_to_string, + _is_config_version_1, match_feature_flag_properties, resolve_bucketing_value, ) +from ._feature_flags_v2 import ( + _evaluate_definition, + _parse_definitions, + _ParsedDefinition, + _unresolved_reason as _v2_unresolved_reason, +) from posthog.flag_definition_cache import ( FlagDefinitionCacheData, FlagDefinitionCacheProvider, @@ -204,6 +212,32 @@ class _LocalEvaluationSnapshot(FlagDefinitionCacheData): flags_by_key: Dict[str, Any] flag_definition_version: int + # The config version 2 definitions of ``flags``, parsed when they loaded. + parsed_v2_definitions: Dict[str, _ParsedDefinition] + + +# The field of the external definition cache that holds the definitions whose config +# version is not 1. ``flags`` keeps the version 1 definitions only, so a release that +# reads only ``flags`` never evaluates a definition it cannot read. +_V2_FLAGS_CACHE_FIELD = "flags_v2" + + +def _split_by_config_version(flags: List[Any]) -> Tuple[List[Any], List[Any]]: + """The version 1 definitions and the others, each in their loaded order, as the + definition cache stores them.""" + version_1: List[Any] = [] + others: List[Any] = [] + for flag in flags: + (version_1 if _is_config_version_1(flag) else others).append(flag) + return version_1, others + + +def _with_cached_v2_flags(data: FlagDefinitionCacheData) -> FlagDefinitionCacheData: + """Cached definitions with the config version 2 definitions back in ``flags``.""" + v2_flags = cast(Mapping[str, Any], data).get(_V2_FLAGS_CACHE_FIELD) + if not isinstance(v2_flags, list) or not v2_flags: + return data + return cast(FlagDefinitionCacheData, {**data, "flags": [*data["flags"], *v2_flags]}) def _supports_lane_synchronization(queue) -> bool: @@ -1473,6 +1507,7 @@ def __init__( None # private variable to store flags ) self.feature_flags_by_key: Optional[dict[str, Any]] = None + self._parsed_v2_definitions: Dict[str, _ParsedDefinition] = {} self.group_type_mapping: Optional[dict[str, str]] = None self.cohorts: Optional[dict[str, Any]] = None self._property_matching_version = 1 @@ -2066,12 +2101,20 @@ def feature_flags(self, flags): """ Set the local evaluation feature flags. """ - self._feature_flags = flags or [] + flags = flags or [] + self._set_feature_flags(flags, _parse_definitions(flags)) + + def _set_feature_flags( + self, flags: List[Any], parsed_v2_definitions: Dict[str, _ParsedDefinition] + ) -> None: + """Install definitions with the config version 2 documents parsed from them.""" + self._feature_flags = flags self.feature_flags_by_key = { flag["key"]: flag for flag in self._feature_flags if flag.get("key") is not None } + self._parsed_v2_definitions = parsed_v2_definitions assert self.feature_flags_by_key is not None, ( "feature_flags_by_key should be initialized when feature_flags is set" ) @@ -4144,12 +4187,33 @@ def _shutdown_flag_definition_cache_provider(self): self._flag_definition_cache_provider_async_runner.close() self._flag_definition_cache_provider_async_runner = None + def _flag_definitions_for_cache(self) -> FlagDefinitionCacheData: + """The loaded definitions as the external definition cache stores them.""" + v1_flags, v2_flags = _split_by_config_version(self.feature_flags or []) + data: Dict[str, Any] = { + "flags": v1_flags, + "group_type_mapping": self.group_type_mapping or {}, + "cohorts": self.cohorts or {}, + "minimal_flag_called_events": self._minimal_flag_called_events, + "property_matching_version": self._property_matching_version, + } + if v2_flags: + # Left out without such flags, so the data stays as before for them. + data[_V2_FLAGS_CACHE_FIELD] = v2_flags + return cast(FlagDefinitionCacheData, data) + @staticmethod def _hash_flag_definitions(data: FlagDefinitionCacheData) -> str: # Hash only evaluation inputs, not transport metadata such as the ETag. + flags = data["flags"] + if isinstance(flags, list): + # In the order the definition cache stores them, so a client that loads + # them from the cache gives the same hash. + v1_flags, v2_flags = _split_by_config_version(flags) + flags = [*v1_flags, *v2_flags] serialized = json.dumps( { - "flags": data["flags"], + "flags": flags, "cohorts": data["cohorts"], "group_type_mapping": data["group_type_mapping"], "property_matching_version": data.get("property_matching_version", 1), @@ -4165,12 +4229,19 @@ def _update_flag_state( data: FlagDefinitionCacheData, *, _fingerprint: Optional[str] = None, + _parsed_v2_definitions: Optional[Dict[str, _ParsedDefinition]] = None, ) -> Optional[str]: - """Publish definitions and return the old cache identity for unlocked cleanup.""" + """Publish definitions and return the old cache identity for unlocked cleanup. + + The fingerprint and the parsed config version 2 documents are prepared before + taking the publication lock; a caller that already holds it passes both.""" fingerprint = _fingerprint or self._hash_flag_definitions(data) + flags = data["flags"] or [] + if _parsed_v2_definitions is None: + _parsed_v2_definitions = _parse_definitions(flags) with self._flag_definition_publication_lock: old_fingerprint = self._flag_definition_fingerprint - self.feature_flags = data["flags"] + self._set_feature_flags(flags, _parsed_v2_definitions) self.group_type_mapping = data["group_type_mapping"] self.cohorts = data["cohorts"] self._property_matching_version = data.get("property_matching_version", 1) @@ -4237,12 +4308,17 @@ def _classify_unresolvable_flags( if flag.get("evaluation_runtime") == "client": continue key = flag["key"] - reason = _get_definition_unresolved_reason( - flag, - snapshot["flags_by_key"], - snapshot["cohorts"], - snapshot["group_type_mapping"], - ) + if not _is_config_version_1(flag): + reason = _v2_unresolved_reason( + flag, snapshot["parsed_v2_definitions"] + ) + else: + reason = _get_definition_unresolved_reason( + flag, + snapshot["flags_by_key"], + snapshot["cohorts"], + snapshot["group_type_mapping"], + ) except Exception: # Evaluation reports a definition this malformed when the flag is used. continue @@ -4300,6 +4376,7 @@ def _local_evaluation_snapshot(self) -> _LocalEvaluationSnapshot: "property_matching_version": self._property_matching_version, "minimal_flag_called_events": self._minimal_flag_called_events, "flag_definition_version": self.flag_definition_version, + "parsed_v2_definitions": self._parsed_v2_definitions, } def _load_feature_flags(self): @@ -4326,7 +4403,9 @@ def _load_feature_flags(self): self.log.debug( "[FEATURE FLAGS] Using cached flag definitions from external cache" ) - old_fingerprint = self._update_flag_state(cached_data) + old_fingerprint = self._update_flag_state( + _with_cached_v2_flags(cached_data) + ) self._warn_unresolvable_flags() if isinstance(self.flag_cache, RedisFlagCache): self.flag_cache._invalidate_snapshot(old_fingerprint) @@ -4373,12 +4452,13 @@ def _fetch_feature_flags_from_api(self): etag=request_etag, ) - # Canonical serialization can be expensive; do it before publication. - fingerprint = ( - self._hash_flag_definitions(response.data) - if response.data is not None and not response.not_modified - else None - ) + # Canonical serialization and parsing can be expensive; do them before + # publication. + fingerprint: Optional[str] = None + parsed_v2_definitions: Optional[Dict[str, _ParsedDefinition]] = None + if response.data is not None and not response.not_modified: + fingerprint = self._hash_flag_definitions(response.data) + parsed_v2_definitions = _parse_definitions(response.data["flags"] or []) with self._flag_definition_publication_lock: if fetch_generation <= self._flag_definition_published_generation: @@ -4414,17 +4494,13 @@ def _fetch_feature_flags_from_api(self): return old_fingerprint = self._update_flag_state( - response.data, _fingerprint=fingerprint + response.data, + _fingerprint=fingerprint, + _parsed_v2_definitions=parsed_v2_definitions, ) if self._flag_definition_cache_provider: - cache_data_to_store = { - "flags": self.feature_flags or [], - "group_type_mapping": self.group_type_mapping or {}, - "cohorts": self.cohorts or {}, - "minimal_flag_called_events": self._minimal_flag_called_events, - "property_matching_version": self._property_matching_version, - } + cache_data_to_store = self._flag_definitions_for_cache() # Publish the ETag only after its matching flag state is installed. self._flags_etag = response.etag @@ -4579,7 +4655,14 @@ def _compute_flag_locally( warn_on_unknown_groups=True, device_id=None, _definition_snapshot: Optional[_LocalEvaluationSnapshot] = None, - ) -> FlagValue: + ) -> Union[FlagValue, FeatureFlag]: + """Evaluate a loaded definition by the evaluator of its config version. + + Returns the value of a config version 1 flag, or the record of a config + version 2 flag, which is the record a ``/flags?v=4`` response gives. Raises + ``InconclusiveMatchError`` or ``RequiresServerEvaluation`` when local + evaluation cannot give the flag a value. + """ snapshot = ( _definition_snapshot if _definition_snapshot is not None @@ -4591,6 +4674,15 @@ def _compute_flag_locally( person_properties = person_properties or {} group_properties = group_properties or {} + if not _is_config_version_1(feature_flag): + return _evaluate_definition( + feature_flag, + snapshot["parsed_v2_definitions"], + distinct_id=distinct_id, + properties=person_properties, + property_matching_version=property_matching_version, + ) + # Create evaluation cache for flag dependencies evaluation_cache: dict[str, Optional[FlagValue]] = {} @@ -4827,7 +4919,26 @@ def _get_feature_flag_result( local_definition_version = snapshot["flag_definition_version"] flag_was_locally_evaluated = flag_value is not None - if flag_value is not None: + if isinstance(flag_value, FeatureFlag): + # A config version 2 flag: read its record as a remote one is read. + flag_details = flag_value + flag_result = FeatureFlagResult.from_flag_details( + flag_details, override_match_value + ) + if self.flag_cache: + # Cache the evaluation, not a payload lookup override. + cached_details_result = FeatureFlagResult.from_flag_details( + flag_details + ) + if cached_details_result: + self.flag_cache.set_cached_flag( + distinct_id, + key, + cached_details_result, + local_definition_version, + ) + value_from_details = override_match_value is None + elif flag_value is not None: lookup_match_value = ( override_match_value if override_match_value is not None else flag_value ) @@ -4953,8 +5064,9 @@ def _get_feature_flag_result( # Source the gate the same way as has_experiment above; see # _capture_feature_flag_called_if_needed for why. minimal_flag_called_events = self._minimal_flag_called_events - # Remotely-evaluated flags carry id and version in the response metadata; - # locally-evaluated flags read them from the definition that produced the value. + # Remotely-evaluated flags and the local record of a config version 2 flag + # carry id and version in the record metadata; locally-evaluated version 1 + # flags read them from the definition that produced the value. local_flag_id: Optional[int] = None local_flag_version: Optional[int] = None if flag_was_locally_evaluated: @@ -4963,8 +5075,9 @@ def _get_feature_flag_result( has_experiment = _parse_has_experiment( local_def.get("has_experiment") ) - local_flag_id = local_def.get("id") - local_flag_version = local_def.get("version") + if flag_details is None: + local_flag_id = local_def.get("id") + local_flag_version = local_def.get("version") elif isinstance(flag_details, FeatureFlag): has_experiment = _metadata_has_experiment(flag_details.metadata) minimal_flag_called_events = remote_minimal_flag_called_events @@ -5007,8 +5120,8 @@ def get_feature_flag_result( Get a FeatureFlagResult object which contains the flag result and payload for a key by evaluating locally or remotely depending on whether local evaluation is enabled and the flag can be locally evaluated. This also captures the `$feature_flag_called` event unless `send_feature_flag_events` is `False`. - When the flag was evaluated remotely and its config version 2 record is a complete experiment - split (an experiment rule with a rule ID and a variant key), it also captures the + When the flag's config version 2 record is a complete experiment split (an + experiment rule with a rule ID and a variant key), it also captures the `$experiment_exposure` event, once per distinct ID, groups, flag, rule and variant. Examples: @@ -5122,8 +5235,9 @@ def _locally_evaluate_flag( person_properties: dict[str, str], group_properties: dict[str, dict[str, Any]], device_id: Optional[str] = None, - ) -> tuple[Optional[FlagValue], _LocalEvaluationSnapshot]: - """Return the local value and its definitions for consistent payload lookup.""" + ) -> tuple[Optional[Union[FlagValue, FeatureFlag]], _LocalEvaluationSnapshot]: + """Return the local value, or the record of a config version 2 flag, and the + definitions it came from, for consistent payload lookup.""" if self.feature_flags is None and self.personal_api_key: self.load_feature_flags() response = None @@ -5357,8 +5471,9 @@ def _capture_feature_flag_called_if_needed( Shared by the single-flag evaluation path and the reads of a ``FeatureFlagEvaluations`` snapshot, so every read reports and dedupes - identically. ``details`` is the parsed ``/flags`` record of the read, or - ``None`` for a locally evaluated flag. ``value_from_details`` includes a + identically. ``details`` is the record of the read: the parsed ``/flags`` + record, the local record of a config version 2 flag, or ``None`` for a + locally evaluated version 1 flag. ``value_from_details`` includes a failed record supplying config-version and reason-code diagnostics despite having no value. Only a successful experiment split sends an exposure. """ @@ -5710,6 +5825,7 @@ def evaluate_flags( quota_limited = False locally_evaluated_keys: set[str] = set() unresolved: Dict[str, UnresolvedFlagReason] = {} + local_records: Dict[str, FeatureFlag] = {} # Try local evaluation first when the poller has loaded definitions. local_person_properties = self._person_properties_for_local_evaluation( distinct_id, person_properties @@ -5723,6 +5839,7 @@ def evaluate_flags( flag_keys_to_evaluate=flag_keys, device_id=device_id, unresolved=unresolved, + records=local_records, ) ) @@ -5740,6 +5857,10 @@ def evaluate_flags( fallback_to_server = True for key, value in local_flags.items(): + locally_evaluated_keys.add(key) + if key in local_records: + records[key] = _LocalDetailsFlagRecord(local_records[key]) + continue flag_def = feature_flags_by_key.get(key) or {} records[key] = _LocalFlagRecord( key=key, @@ -5752,7 +5873,6 @@ def evaluate_flags( locally_evaluated=True, has_experiment=_parse_has_experiment(flag_def.get("has_experiment")), ) - locally_evaluated_keys.add(key) # Fall back to remote evaluation for any flags the poller couldn't resolve locally. # Use the flags decision path directly so the resulting records carry id/version/reason @@ -5836,7 +5956,11 @@ def _get_all_flags_and_payloads_locally( flag_keys_to_evaluate: Optional[list[str]] = None, device_id: Optional[str] = None, unresolved: Optional[Dict[str, UnresolvedFlagReason]] = None, + records: Optional[Dict[str, FeatureFlag]] = None, ) -> tuple[FlagsAndPayloads, bool, _LocalEvaluationSnapshot]: + """Evaluate the loaded flags. ``unresolved`` receives the reason of each flag + left without a value, and ``records`` the record of each config version 2 flag. + """ person_properties = person_properties or {} group_properties = group_properties or {} @@ -5859,7 +5983,7 @@ def _get_all_flags_and_payloads_locally( for flag in flags_to_process: try: - flags[flag["key"]] = self._compute_flag_locally( + value = self._compute_flag_locally( flag, distinct_id, groups=groups, @@ -5869,11 +5993,24 @@ def _get_all_flags_and_payloads_locally( device_id=device_id, _definition_snapshot=snapshot, ) - matched_payload = self._compute_payload_locally( - flag["key"], - flags[flag["key"]], - _definition_snapshot=snapshot, - ) + if isinstance(value, FeatureFlag): + # A config version 2 record renders as a remote one does. + if records is not None: + records[flag["key"]] = value + flags[flag["key"]] = value.get_value() + matched_payload = ( + value.metadata.payload if value.enabled else None + ) + else: + if records is not None: + # A later definition with the same key replaces the record. + records.pop(flag["key"], None) + flags[flag["key"]] = value + matched_payload = self._compute_payload_locally( + flag["key"], + value, + _definition_snapshot=snapshot, + ) if matched_payload is not None: payloads[flag["key"]] = matched_payload except (InconclusiveMatchError, RequiresServerEvaluation) as e: diff --git a/posthog/feature_flag_evaluations.py b/posthog/feature_flag_evaluations.py index 870680c6f..29fb8f827 100644 --- a/posthog/feature_flag_evaluations.py +++ b/posthog/feature_flag_evaluations.py @@ -92,6 +92,18 @@ def has_experiment(self) -> Optional[bool]: return metadata.has_experiment if isinstance(metadata, _FlagMetadata) else None +class _LocalDetailsFlagRecord(_RemoteFlagRecord): + """Internal per-flag record of a locally evaluated config version 2 flag. + + Local evaluation gives the record a ``/flags?v=4`` response gives for the same + result, so the flag is read, and its events are built, as for a remote record. + """ + + __slots__ = () + + locally_evaluated = True + + # The per-flag record stored by a FeatureFlagEvaluations instance. _EvaluatedFlagRecord = Union[_LocalFlagRecord, _RemoteFlagRecord] @@ -128,9 +140,9 @@ class FeatureFlagEvaluations: from the server in the first place, pass ``flag_keys`` to :meth:`Client.evaluate_flags`. Reading a flag sends ``$feature_flag_called``, deduplicated as :meth:`is_enabled` - describes. When the flag was evaluated remotely and its config version 2 record is - a complete experiment split (an experiment rule with a rule ID and a variant key), - the read also sends ``$experiment_exposure``. The SDK sends one exposure per + describes. When the flag's config version 2 record is a complete experiment split + (an experiment rule with a rule ID and a variant key), the read also sends + ``$experiment_exposure``. The SDK sends one exposure per distinct ID, groups, flag, rule and variant. ``send_feature_flag_events=False`` suppresses both events. """ diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 6a2e98693..35434c5b5 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -220,6 +220,29 @@ def variant_lookup_table(feature_flag): return lookup_table +# Built once: a tuple written in the check would be built on every evaluation. +_NUMBER_TYPES = (int, float) + + +def _is_config_version_1(flag) -> bool: + """Whether a flag definition is config version 1, the format this module reads. + + It is when ``filters.version`` is absent or the number 1. Any other value, also + one that is not a number, belongs to the config version 2 evaluator, which + accepts only version 2. + """ + filters = flag.get("filters") if isinstance(flag, dict) else None + if not isinstance(filters, dict) or "version" not in filters: + return True + version = filters["version"] + # bool is an int subclass, so True == 1 would read as version 1 without this. + return ( + isinstance(version, _NUMBER_TYPES) + and not isinstance(version, bool) + and version == 1 + ) + + def _flag_dependency_shape_problem(prop) -> Optional[str]: """Describe why a flag dependency condition is malformed, or return None. @@ -297,6 +320,7 @@ def evaluate_flag_dependency( _assert_dependency_definitions_loaded( dependency_chain, flags_by_key, evaluation_cache ) + _assert_dependency_formats_supported(dependency_chain, flags_by_key) _evaluate_dependency_chain( dependency_chain, flags_by_key, @@ -377,6 +401,18 @@ def _assert_dependency_definitions_loaded( ) +def _assert_dependency_formats_supported(dependency_chain, flags_by_key): + # A config version 1 flag cannot depend on a config version 2 flag. Reading one + # here as version 1 would give it a value, and a later condition must not answer + # for the flag either, so the whole flag goes to the server. + for key in dependency_chain: + if not _is_config_version_1(flags_by_key.get(key)): + raise RequiresServerEvaluation( + f"Flag dependency '{key}' is not a config version 1 flag", + reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, + ) + + def _evaluate_dependency_chain( dependency_chain, flags_by_key, @@ -1868,7 +1904,10 @@ def _is_unsupported_dependency(prop, flags_by_key) -> bool: dependency_chain = prop.get("dependency_chain") if not isinstance(dependency_chain, list) or not dependency_chain: return True - return any(not flags_by_key.get(key) for key in dependency_chain) + return any( + not flags_by_key.get(key) or not _is_config_version_1(flags_by_key[key]) + for key in dependency_chain + ) def _is_unsupported_property_group( diff --git a/posthog/test/test_feature_flags_v2_dispatch.py b/posthog/test/test_feature_flags_v2_dispatch.py new file mode 100644 index 000000000..5f5184f97 --- /dev/null +++ b/posthog/test/test_feature_flags_v2_dispatch.py @@ -0,0 +1,1562 @@ +"""Local evaluation chooses the evaluator of each flag by its config version. + +A definition whose ``filters.version`` is absent or 1 keeps the version 1 evaluator. Any +other definition goes to the config version 2 evaluator. Its result is the record a +``/flags?v=4`` response gives for the same outcome, so the flag events are built as for +a remote record, with ``locally_evaluated`` true. A config version 2 flag that the +evaluator cannot decide falls back the way an inconclusive version 1 flag does: to a +remote request in the default mode, and to no value with ``only_evaluate_locally``. + +The corpora, the local outcome mapping and the whole definitions feed come from +``fixtures/feature_flag_rules_v2``. Every other flag, identity and property here is +invented. +""" + +from __future__ import annotations + +import contextlib +import copy +import datetime +import hashlib +import json +import threading +import warnings +from typing import Any, Dict, Iterator, List, Optional, Tuple +from unittest import mock + +import pytest +from freezegun import freeze_time + +from posthog import _feature_flags_v2 as v2 +from posthog.client import Client +from posthog.feature_flags import InconclusiveMatchError +from posthog.request import GetResponse +from posthog.test.test_experiment_exposure import ( + EXPOSURE, + EXPOSURE_SCHEMA, + FLAG_CALLED, + FLAG_CALLED_SCHEMA, + _attribution, + _named, + _projection, + _record, + _record_events, + _response, + _serving, + _split, +) +from posthog.test.test_feature_flags_v2 import ( + REPLAYED, + _as_expectation, + _binary64, + _local_outcome, + _same_json, +) +from posthog.test.test_flags_v4_response import _load as _load_contract_file +from posthog.test.test_utils import FAKE_TEST_API_KEY +from posthog.types import ( + FeatureFlag, + FeatureFlagResult, + FlagMetadata, + UnresolvedFlagReason, + normalize_flags_response, +) + +DISTINCT_ID = "reader-31" +# Drawn below 0.5 for the experiment seed, so a 50/50 split assigns the first variant. +SPLIT_SEED = "shelf-order" +# Drawn above 0.9 for the rollout seed, so a 50 percent rollout misses DISTINCT_ID. +MISSING_ROLLOUT_SEED = "reading-room" + +RULE = "5c2e9a14-7b3d-4f61-a8e0-2d4c6b8f1e3a" +SECOND_RULE = "9e1f3b27-4c5a-4d8e-b6f2-7a0c1e3d5b49" +SPLIT_RULE = "c47d2e90-1a3b-4c5d-9e6f-8b0a2c4e6d1f" + +UNSUPPORTED = UnresolvedFlagReason.UNSUPPORTED_DEFINITION +MISSING_CONTEXT = UnresolvedFlagReason.MISSING_CONTEXT + + +# --- Definitions ------------------------------------------------------------------- + + +def _person(key: str, operator: str, value: Any) -> Dict[str, Any]: + return { + "key": key, + "type": "person", + "operator": operator, + "value": value, + "negation": False, + } + + +def _targeted( + value: Any, *, rule_id: str = RULE, properties: Tuple[Dict[str, Any], ...] = () +) -> Dict[str, Any]: + return { + "id": rule_id, + "rule_type": "targeted_release", + "targeting": {"properties": list(properties)}, + "value": value, + } + + +def _rollout( + value: Any, + percentage: float, + *, + rule_id: str = RULE, + seed: str = MISSING_ROLLOUT_SEED, + on_rollout_miss: str = "continue", +) -> Dict[str, Any]: + return { + "id": rule_id, + "rule_type": "percentage_rollout", + "targeting": {"properties": []}, + "value": value, + "rollout_percentage": percentage, + "on_rollout_miss": on_rollout_miss, + "assignment_algorithm": "sha1_60_v1", + "seed": seed, + "assign_by": "person", + } + + +def _experiment( + *variants: Tuple[str, float, Any], rule_id: str = SPLIT_RULE +) -> Dict[str, Any]: + return { + "id": rule_id, + "rule_type": "experiment", + "targeting": {"properties": []}, + "paused": False, + "rollout_percentage": 100, + "on_rollout_miss": "continue", + "assignment_algorithm": "sha1_60_v1", + "seed": SPLIT_SEED, + "assign_by": "person", + "variants": [ + {"key": key, "weight": weight, "value": value} + for key, weight, value in variants + ], + } + + +def _filters( + *rules: Dict[str, Any], return_type: str = "boolean", default: Any = False +) -> Dict[str, Any]: + return { + "version": 2, + "return_type": return_type, + "default_value": default, + "rules": list(rules), + } + + +def _definition( + key: str, + filters: Any, + *, + active: bool = True, + flag_id: int = 41, + version: int = 6, + has_experiment: bool = False, +) -> Dict[str, Any]: + return { + "id": flag_id, + "name": f"Invented {key}", + "key": key, + "active": active, + "filters": filters, + "version": version, + "has_experiment": has_experiment, + "ensure_experience_continuity": False, + } + + +def _v1_definition( + key: str, *, variant: Optional[str] = None, **filters: Any +) -> Dict[str, Any]: + v1_filters: Dict[str, Any] = { + "groups": [{"properties": [], "rollout_percentage": 100}], + **filters, + } + if variant is not None: + v1_filters["multivariate"] = { + "variants": [{"key": variant, "rollout_percentage": 100}] + } + return {"id": 12, "key": key, "active": True, "filters": v1_filters} + + +BOOLEAN = _definition("reading-mode", _filters(_targeted(True))) +STRING = _definition( + "shelf-label", + _filters(_targeted("compact"), return_type="string", default="standard"), +) +NUMBER = _definition( + "page-size", _filters(_targeted(24), return_type="number", default=12) +) +OBJECT = _definition( + "reader-theme", + _filters( + _targeted({"palette": "sepia", "sizes": [12, 14]}), + return_type="object", + default={"palette": "plain"}, + ), +) +SPLIT = _definition( + "shelf-order", + _filters( + _experiment(("alphabetical", 50, "by-title"), ("recent", 50, "by-date")), + return_type="string", + default="by-title", + ), + has_experiment=True, +) +# Version 3 is a config version that no SDK reads. +UNKNOWN_VERSION = _definition( + "future-format", {**_filters(_targeted(True)), "version": 3} +) +# The first rule matches everyone, but the parser is closed: the unknown field of the +# second rule rejects the whole document before any rule is evaluated. +UNKNOWN_RULE_FIELD = _definition( + "closed-parser", + _filters( + _targeted(True), + {**_targeted(True, rule_id=SECOND_RULE), "schedule": "weekdays"}, + ), +) +NEEDS_PLAN = _definition( + "plan-gate", + _filters(_targeted(True, properties=(_person("plan", "exact", "pro"),))), +) + + +@pytest.fixture +def client() -> Iterator[Client]: + client = Client(FAKE_TEST_API_KEY, send=False, secret_key="secret-key") + yield client + client.shutdown() + + +def _load( + client: Client, *definitions: Dict[str, Any], property_matching_version: int = 2 +) -> None: + """Install definitions the way a definitions response does.""" + client._update_flag_state( + { + "flags": copy.deepcopy(list(definitions)), + "group_type_mapping": {"0": "company"}, + "cohorts": {}, + "property_matching_version": property_matching_version, + } + ) + + +@contextlib.contextmanager +def _no_remote_request() -> Iterator[None]: + with mock.patch( + "posthog.client.flags", side_effect=AssertionError("unexpected /flags request") + ) as remote: + yield + # The client catches the error of a failed request, so check after the reads. + assert remote.call_count == 0, "unexpected /flags request" + + +def _result(client: Client, key: str, **kwargs: Any) -> Optional[FeatureFlagResult]: + return client.get_feature_flag_result(key, DISTINCT_ID, **kwargs) + + +def _legacy(client: Client, method: str, key: str, **kwargs: Any) -> Any: + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return getattr(client, method)(key, DISTINCT_ID, **kwargs) + + +# --- Which evaluator ---------------------------------------------------------------- + + +@pytest.mark.parametrize( + "filters", + [ + pytest.param({"groups": [{"properties": [], "rollout_percentage": 100}]}), + pytest.param( + {"version": 1, "groups": [{"properties": [], "rollout_percentage": 100}]} + ), + pytest.param( + {"version": 1.0, "groups": [{"properties": [], "rollout_percentage": 100}]} + ), + ], + ids=["absent", "one", "one_as_float"], +) +def test_a_config_version_1_flag_keeps_the_version_1_evaluator(client, filters): + with ( + mock.patch.object(v2, "_evaluate", side_effect=AssertionError("v2")), + mock.patch.object(v2, "_parse_config", side_effect=AssertionError("v2")), + _no_remote_request(), + ): + _load(client, _definition("legacy-shelf", filters)) + assert _legacy(client, "get_feature_flag", "legacy-shelf") is True + snapshot = client.evaluate_flags(DISTINCT_ID) + assert snapshot.get_flag("legacy-shelf") is True + + +@pytest.mark.parametrize("version", [2, 2.0, 3, None, True, "1", "2"], ids=repr) +def test_any_other_config_version_goes_to_the_config_version_2_evaluator( + client, version +): + filters = {**_filters(_targeted(True)), "version": version} + _load(client, _definition("other-version", filters)) + + with ( + mock.patch.object(v2, "_evaluate", wraps=v2._evaluate) as evaluate, + _serving(_response(_record("targeting_match", "remote", key="other-version"))), + ): + result = _result(client, "other-version") + + assert evaluate.call_count == 1 + if version == 2: + assert result is not None and result.get_value() is True + else: + # Only version 2 parses. Every other version is unsupported and falls back. + assert result is not None and result.get_value() == "remote" + + +def test_the_definitions_request_is_unchanged(client): + # This release evaluates a config version 2 definition when one is loaded, but it + # does not ask for them yet: the request still selects the legacy definitions. + with mock.patch( + "posthog.client.get", + return_value=GetResponse( + data={"flags": [], "group_type_mapping": {}, "cohorts": {}} + ), + ) as get: + client.load_feature_flags() + + [request] = get.call_args_list + assert ( + request.args[1] == f"/flags/definitions?token={FAKE_TEST_API_KEY}&send_cohorts" + ) + + +# --- Results ------------------------------------------------------------------------ + + +@pytest.mark.parametrize( + "definition, value, payload", + [ + (BOOLEAN, True, None), + (STRING, "compact", None), + (NUMBER, True, 24), + (OBJECT, True, {"palette": "sepia", "sizes": [12, 14]}), + ], + ids=["boolean", "string", "number", "object"], +) +def test_a_config_version_2_flag_is_evaluated_locally_on_every_read( + client, definition, value, payload +): + key = definition["key"] + _load(client, _v1_definition("legacy-shelf"), definition) + + with _no_remote_request(): + result = _result(client, key) + assert result is not None + assert (result.get_value(), result.payload) == (value, payload) + assert _legacy(client, "get_feature_flag", key) == value + assert _legacy(client, "feature_enabled", key) is True + assert _legacy(client, "get_feature_flag_payload", key) == payload + assert client.get_all_flags(DISTINCT_ID) == {"legacy-shelf": True, key: value} + payloads = client.get_all_flags_and_payloads(DISTINCT_ID)["featureFlagPayloads"] + # The legacy bulk getter keeps payloads encoded, as for a remote record. + assert payloads == ( + {} if payload is None else {key: json.dumps(payload, separators=(",", ":"))} + ) + + snapshot = client.evaluate_flags(DISTINCT_ID) + assert snapshot.keys == ["legacy-shelf", key] + assert snapshot.get_flag(key) == value + assert snapshot.is_enabled(key) is True + assert snapshot.get_flag_payload(key) == payload + assert snapshot.unresolved_flags == {} + + +@pytest.mark.parametrize( + "definition, properties, record", + [ + ( + SPLIT, + {}, + { + "value": "by-title", + "reason": {"code": "experiment_split", "condition_index": 0}, + "metadata": { + "has_experiment": True, + "rule_type": "experiment", + "rule_id": SPLIT_RULE, + "variant_key": "alphabetical", + }, + }, + ), + ( + NUMBER, + {}, + { + "value": 24, + "reason": {"code": "targeting_match", "condition_index": 0}, + "metadata": {"rule_type": "targeted_release", "rule_id": RULE}, + }, + ), + ( + NEEDS_PLAN, + {"plan": "free"}, + { + "value": False, + "reason": {"code": "no_rule_match", "condition_index": None}, + "metadata": {}, + }, + ), + ], + ids=["split", "number", "no_rule_match"], +) +def test_the_local_record_is_the_record_a_v4_response_gives( + client, definition, properties, record +): + _load(client, definition) + [loaded] = client.feature_flags or [] + local = client._compute_flag_locally( + loaded, DISTINCT_ID, person_properties=properties + ) + + response = { + "flags": { + loaded["key"]: { + "key": loaded["key"], + "value": record["value"], + "reason": {**record["reason"], "description": "Evaluated locally"}, + "metadata": { + "id": 41, + "version": 6, + "config_version": 2, + "description": None, + "payload": None, + "has_experiment": False, + **record["metadata"], + }, + } + } + } + remote = normalize_flags_response(response)["flags"][loaded["key"]] + + assert isinstance(local, FeatureFlag) + assert vars(local) == vars(remote) + assert vars(local.metadata) == vars(remote.metadata) + + +@pytest.mark.parametrize( + "stored, read", + [(41.0, 41), (41.5, None), ("41", None), (True, None)], + ids=["integral_float", "fraction", "string", "boolean"], +) +def test_the_record_reads_the_id_and_version_as_the_v4_reader_does( + client, stored, read +): + # JSON has one number type, so the v4 reader reads 41.0 as 41. Any other value is + # left out, and the flag is still evaluated locally. + _load(client, {**BOOLEAN, "id": stored, "version": stored}) + [loaded] = client.feature_flags or [] + + local = client._compute_flag_locally(loaded, DISTINCT_ID) + + assert isinstance(local, FeatureFlag) and local.enabled is True + assert (local.metadata.id, local.metadata.version) == (read, read) + + +def test_a_null_value_reads_as_disabled(client): + _load( + client, + _definition( + "shelf-label", + _filters( + _targeted("compact", properties=(_person("plan", "exact", "pro"),)), + return_type="string", + default=None, + ), + ), + ) + + with _no_remote_request(): + result = _result(client, "shelf-label", person_properties={"plan": "free"}) + snapshot = client.evaluate_flags( + DISTINCT_ID, person_properties={"plan": "free"} + ) + + assert result is not None and result.get_value() is False + assert snapshot.get_flag("shelf-label") is False + assert snapshot.is_enabled("shelf-label", True) is False + + +def test_an_object_value_is_a_copy(client): + _load(client, OBJECT) + + with _no_remote_request(): + first = _result(client, "reader-theme") + assert first is not None + first.payload["palette"] = "changed" + first.payload["sizes"].append(99) + snapshot = client.evaluate_flags(DISTINCT_ID) + snapshot.get_flag_payload("reader-theme")["palette"] = "changed too" + # The record keeps the typed value for the typed getters to come. + [loaded] = client.feature_flags or [] + record = client._compute_flag_locally(loaded, DISTINCT_ID) + record._value["sizes"].append(99) + second = _result(client, "reader-theme") + + assert second is not None + assert second.payload == {"palette": "sepia", "sizes": [12, 14]} + assert loaded["filters"] == OBJECT["filters"] + assert client._parsed_v2_definitions["reader-theme"].config.rules[0].value == { + "palette": "sepia", + "sizes": [12, 14], + } + + +@pytest.mark.parametrize("unsupported_too", [False, True]) +def test_an_inactive_flag_is_disabled_without_evaluating_its_rules( + client, unsupported_too +): + filters = ( + UNKNOWN_RULE_FIELD["filters"] if unsupported_too else _filters(_targeted(True)) + ) + _load(client, _definition("paused-shelf", filters, active=False)) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + result = _result(client, "paused-shelf") + snapshot = client.evaluate_flags(DISTINCT_ID, only_evaluate_locally=True) + assert snapshot.get_flag("paused-shelf") is False + + assert result is not None and result.get_value() is False + assert snapshot.unresolved_flags == {} + assert [_attribution(event.properties) for event in events] == [ + { + "$feature_flag_config_version": 2, + "$feature_flag_reason_code": "flag_disabled", + } + ] + + +def test_experience_continuity_and_device_bucketing_do_not_apply(client): + # The flags service evaluates a config version 2 flag with the person's distinct + # ID only, so these version 1 settings do not stop a local result. + definition = { + **SPLIT, + "ensure_experience_continuity": True, + "bucketing_identifier": "device_id", + } + _load(client, definition) + + with _no_remote_request(): + result = _result(client, "shelf-order") + + assert result is not None and result.get_value() == "by-title" + + +def test_the_property_matching_version_selects_exact_matching(client): + # Version 1 matching compares a filter of boolean-like strings by truthiness. + definition = _definition( + "opted-in", + _filters( + _targeted(True, properties=(_person("plan", "exact", ["true", "false"]),)) + ), + ) + with _no_remote_request(): + _load(client, definition, property_matching_version=1) + loose = _result(client, "opted-in", person_properties={"plan": "pro"}) + _load(client, definition, property_matching_version=2) + explicit = _result(client, "opted-in", person_properties={"plan": "pro"}) + + assert loose is not None and loose.get_value() is True + assert explicit is not None and explicit.get_value() is False + + +def test_dates_are_read_in_utc(client): + definition = _definition( + "early-reader", + _filters( + _targeted( + True, + properties=( + _person("joined_at", "is_date_before", "2025-06-15 00:30:00"), + ), + ) + ), + ) + _load(client, definition) + with _no_remote_request(): + # In any timezone east of UTC, 00:15 UTC is after the boundary. + before = _result( + client, + "early-reader", + person_properties={"joined_at": "2025-06-15T00:15:00Z"}, + ) + after = _result( + client, + "early-reader", + person_properties={"joined_at": "2025-06-15T00:45:00Z"}, + ) + + assert before is not None and before.get_value() is True + assert after is not None and after.get_value() is False + + +def test_the_distinct_id_is_read_as_a_string(client): + definition = _definition( + "numbered-reader", + _filters( + _rollout( + True, 50, seed=MISSING_ROLLOUT_SEED, on_rollout_miss="return_default" + ) + ), + ) + _load(client, definition) + + with _no_remote_request(): + numeric = client.get_feature_flag_result("numbered-reader", 58) + text = client.get_feature_flag_result("numbered-reader", "58") + + assert numeric is not None and text is not None + assert numeric.get_value() is text.get_value() is True + + +def test_each_definition_is_parsed_once_when_it_loads(client): + with mock.patch.object(v2, "_parse_config", wraps=v2._parse_config) as parse: + _load(client, _v1_definition("legacy-shelf"), BOOLEAN, STRING, UNKNOWN_VERSION) + assert parse.call_count == 3 + + with _serving(_response(_record("targeting_match", True, key="future-format"))): + for _ in range(3): + _result(client, "reading-mode") + client.evaluate_flags(DISTINCT_ID) + client.get_all_flags(DISTINCT_ID) + + assert parse.call_count == 3 + + +# --- Falling back --------------------------------------------------------------------- + +DATE_GATE = _definition( + "early-reader", + _filters( + _targeted( + True, properties=(_person("joined_at", "is_date_before", "2025-06-15"),) + ) + ), +) +NUMBER_GATE = _definition( + "long-reads", + _filters(_targeted(True, properties=(_person("pages", "gt", 300),))), +) +REGEX_GATE = _definition( + "pattern-gate", + _filters(_targeted(True, properties=(_person("email", "regex", "(unclosed"),))), +) + +# Each definition, the person properties of the read, and the unresolved reason. +UNDECIDED = { + "unknown_version": (UNKNOWN_VERSION, {}, UNSUPPORTED), + "unknown_rule_field": (UNKNOWN_RULE_FIELD, {}, UNSUPPORTED), + "unknown_rule_type": ( + _definition( + "new-rule-type", + _filters({**_targeted(True), "rule_type": "scheduled_release"}), + ), + {}, + UNSUPPORTED, + ), + "group_aggregation": ( + _definition( + "company-shelf", + {**_filters(_targeted(True)), "aggregation_group_type_index": 0}, + ), + {}, + UNSUPPORTED, + ), + "missing_property": (NEEDS_PLAN, {}, MISSING_CONTEXT), + "invalid_regex": (REGEX_GATE, {"email": "reader@example.com"}, UNSUPPORTED), + "invalid_property": (NUMBER_GATE, {"pages": "many"}, MISSING_CONTEXT), + "datetime_property": ( + DATE_GATE, + {"joined_at": datetime.datetime(2025, 1, 2, tzinfo=datetime.timezone.utc)}, + MISSING_CONTEXT, + ), +} + + +@pytest.mark.parametrize("case", list(UNDECIDED), ids=str) +def test_an_undecided_flag_falls_back_to_a_remote_request(client, case): + definition, properties, _ = UNDECIDED[case] + key = definition["key"] + _load(client, _v1_definition("legacy-shelf"), definition) + response = _response(_record("targeting_match", "from-remote", key=key)) + + with _serving(response) as remote: + result = _result(client, key, person_properties=properties) + snapshot = client.evaluate_flags(DISTINCT_ID, person_properties=properties) + + assert result is not None and result.get_value() == "from-remote" + assert remote.call_count == 2 + assert snapshot.get_flag(key) == "from-remote" + assert snapshot.get_flag("legacy-shelf") is True + assert snapshot.unresolved_flags == {} + + +@pytest.mark.parametrize("case", list(UNDECIDED), ids=str) +def test_an_undecided_flag_is_unresolved_when_evaluating_only_locally(client, case): + definition, properties, reason = UNDECIDED[case] + key = definition["key"] + _load(client, _v1_definition("legacy-shelf"), definition) + + with _no_remote_request(): + result = _result( + client, key, person_properties=properties, only_evaluate_locally=True + ) + snapshot = client.evaluate_flags( + DISTINCT_ID, person_properties=properties, only_evaluate_locally=True + ) + flags = client.get_all_flags( + DISTINCT_ID, person_properties=properties, only_evaluate_locally=True + ) + + assert result is None + assert snapshot.keys == ["legacy-shelf"] + assert snapshot.unresolved_flags == {key: reason} + assert snapshot.get_flag(key) is None + assert flags == {"legacy-shelf": True} + + +def test_the_no_request_check_sees_a_request_the_client_recovers_from(client): + # The client catches the error of a failed /flags request, so the check must not + # rely on that error reaching the test. + _load(client, NEEDS_PLAN) + + with pytest.raises(AssertionError, match="unexpected /flags request"): + with _no_remote_request(): + _result(client, "plan-gate") + + +def test_a_rule_that_needs_a_missing_property_ends_the_evaluation(client): + # The second rule would match everyone, but a rule that matches is terminal, so + # the evaluator cannot skip the first one. + definition = _definition( + "plan-gate", + _filters( + _targeted(True, properties=(_person("plan", "exact", "pro"),)), + _targeted(True, rule_id=SECOND_RULE), + ), + ) + _load(client, definition) + + with _no_remote_request(): + snapshot = client.evaluate_flags(DISTINCT_ID, only_evaluate_locally=True) + + assert snapshot.unresolved_flags == {"plan-gate": MISSING_CONTEXT} + + +# --- The contract's definitions feed ------------------------------------------------- + +# One whole definitions feed: version 1 and config version 2 flags, active and +# inactive, a cohort flag, a group flag, and three entries that the parser does not +# support, each between supported ones. For each entry the contract states the +# evaluator that reads it, what the reader does with it in each mode, and the result of +# a config version 2 entry for the fixture's context. +FEED = _load_contract_file("definitions_feed.json") +FEED_ENTRIES = list(zip(FEED["feed"]["flags"], FEED["expectations"])) +FEED_CONTEXT = FEED["context"] + + +def _load_feed(client: Client) -> None: + # The client never knows that a property map is complete, reads dates in UTC and + # takes exact matching from the feed, so the fixture's context must be one it uses. + assert FEED_CONTEXT["properties_complete"] is False + assert FEED_CONTEXT["timezone"] == "UTC" + assert FEED_CONTEXT["explicit_exact_matching"] is ( + FEED["feed"]["property_matching_version"] == 2 + ) + client._update_flag_state(copy.deepcopy(FEED["feed"])) + + +def _record_as_expectation(record: FeatureFlag) -> Dict[str, Any]: + """A local record in the shape of a corpus ``expected`` member.""" + metadata = record.metadata + assert record.reason is not None and isinstance(metadata, FlagMetadata) + expectation: Dict[str, Any] = { + "status": "success", + "value": record._value, + "reason": record.reason.code, + } + rule = { + "id": metadata._rule_id, + "rule_type": metadata._rule_type, + "index": record.reason.condition_index, + } + # The evaluator sets all three rule fields or none of them. + present = [field is not None for field in rule.values()] + assert all(present) or not any(present), f"{record.key} has a partial rule {rule}" + if all(present): + expectation["rule"] = rule + if metadata._variant_key is not None: + expectation["variant"] = metadata._variant_key + return expectation + + +@pytest.mark.parametrize( + "entry, expectation", FEED_ENTRIES, ids=[entry["key"] for entry, _ in FEED_ENTRIES] +) +def test_each_feed_entry_gets_the_evaluator_and_result_the_contract_states( + client, entry, expectation +): + _load_feed(client) + definition = (client.feature_flags_by_key or {})[entry["key"]] + + def read() -> Any: + return client._compute_flag_locally( + definition, + FEED_CONTEXT["identifier"], + person_properties=FEED_CONTEXT["properties"], + ) + + if expectation["reader"]["local_only"] == "unsupported": + with pytest.raises(InconclusiveMatchError) as raised: + read() + assert raised.value.reason is UNSUPPORTED + elif expectation["evaluator"] == "v1": + with mock.patch.object(v2, "_evaluate", side_effect=AssertionError("v2")): + assert not isinstance(read(), FeatureFlag) + else: + record = read() + assert isinstance(record, FeatureFlag) + assert _same_json(_record_as_expectation(record), expectation["expected"]) + + +@pytest.mark.parametrize("mode", ["auto", "local_only"]) +def test_only_the_feed_entries_the_reader_does_not_support_fall_back(client, mode): + _load_feed(client) + remote = _response( + *( + _record("targeting_match", "from-remote", key=entry["key"]) + for entry, _ in FEED_ENTRIES + ) + ) + recording, events = _record_events(client) + + with _serving(remote) as served, recording: + snapshot = client.evaluate_flags( + FEED_CONTEXT["identifier"], + person_properties=FEED_CONTEXT["properties"], + only_evaluate_locally=mode == "local_only", + ) + values = { + entry["key"]: snapshot.get_flag(entry["key"]) for entry, _ in FEED_ENTRIES + } + + outcomes = { + expectation["key"]: expectation["reader"][mode] + for expectation in FEED["expectations"] + } + assert served.call_count == (1 if mode == "auto" else 0) + assert snapshot.unresolved_flags == { + key: UNSUPPORTED + for key, outcome in outcomes.items() + if outcome == "unsupported" + } + for entry, expectation in FEED_ENTRIES: + key, outcome = entry["key"], outcomes[entry["key"]] + if outcome == "unsupported": + assert values[key] is None + continue + [called] = _named(events, FLAG_CALLED, key) + assert called.properties["locally_evaluated"] is (outcome == "local_evaluation") + if outcome == "remote_fallback": + assert values[key] == "from-remote" + elif "expected" in expectation: + # A sibling of an entry that falls back keeps its own result. + value, payload = _legacy_rendering(expectation["expected"]["value"]) + assert values[key] == value + assert _same_json(snapshot.get_flag_payload(key), payload) + + +# --- Flag dependencies ------------------------------------------------------------- + + +def _depends_on(key: str, dependency: str, value: Any, *later: Dict[str, Any]): + """A version 1 flag whose first condition depends on ``dependency``.""" + return { + "id": 13, + "key": key, + "active": True, + "filters": { + "groups": [ + { + "properties": [ + { + "key": dependency, + "type": "flag", + "value": value, + "operator": "flag_evaluates_to", + "dependency_chain": [dependency], + } + ], + "rollout_percentage": 100, + "variant": None, + }, + *later, + ], + }, + } + + +def test_a_version_1_flag_never_evaluates_a_config_version_2_dependency(client): + # Read as version 1, the dependency has no conditions and is false, so the first + # condition would match. A later condition would also answer for the flag. + dependent = _depends_on( + "after-reading-mode", + "reading-mode", + False, + {"properties": [], "rollout_percentage": 100}, + ) + dependency = _definition("reading-mode", _filters(_targeted(True))) + _load(client, dependency, dependent) + + with _no_remote_request(): + snapshot = client.evaluate_flags(DISTINCT_ID, only_evaluate_locally=True) + result = _result(client, "after-reading-mode", only_evaluate_locally=True) + + assert result is None + assert snapshot.get_flag("reading-mode") is True + assert snapshot.unresolved_flags == {"after-reading-mode": UNSUPPORTED} + + response = _response(_record("targeting_match", "remote", key="after-reading-mode")) + with _serving(response): + result = _result(client, "after-reading-mode") + assert result is not None and result.get_value() == "remote" + + +def test_a_version_1_dependency_is_evaluated_as_before(client): + _load( + client, + _v1_definition("legacy-shelf"), + _depends_on("after-legacy-shelf", "legacy-shelf", True), + ) + + with _no_remote_request(): + result = _result(client, "after-legacy-shelf", only_evaluate_locally=True) + + assert result is not None and result.get_value() is True + + +def test_the_load_warning_names_a_config_version_2_dependency_and_document(client): + with mock.patch.object(client.log, "warning") as warning: + _load( + client, + BOOLEAN, + _depends_on("after-reading-mode", "reading-mode", True), + UNKNOWN_VERSION, + _definition("paused-future", UNKNOWN_VERSION["filters"], active=False), + ) + client._warn_unresolvable_flags() + + [message] = [call.args[0] for call in warning.call_args_list] + assert "(reason: unsupported_definition): after-reading-mode, future-format." in ( + message + ) + + +# --- Events ---------------------------------------------------------------------------- + +EXPECTED_SPLIT_ATTRIBUTION = { + "$feature_flag_config_version": 2, + "$feature_flag_reason_code": "experiment_split", + "$feature_flag_rule_id": SPLIT_RULE, + "$feature_flag_rule_type": "experiment", + "$feature_flag_variant_key": "alphabetical", +} + + +def _single_read(client: Client) -> None: + _result(client, "shelf-order") + + +def _snapshot_read(client: Client) -> None: + client.evaluate_flags(DISTINCT_ID).get_flag("shelf-order") + + +READS = {"single": _single_read, "snapshot": _snapshot_read} + + +@pytest.mark.parametrize("read", list(READS)) +def test_a_local_split_sends_both_events_as_locally_evaluated(client, read): + _load(client, SPLIT) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + READS[read](client) + + assert [event.name for event in events] == [FLAG_CALLED, EXPOSURE] + called, exposure = events + assert called.properties == { + "$feature_flag": "shelf-order", + "$feature_flag_response": "by-title", + "locally_evaluated": True, + "$feature/shelf-order": "by-title", + "$feature_flag_id": 41, + "$feature_flag_version": 6, + "$feature_flag_has_experiment": True, + **EXPECTED_SPLIT_ATTRIBUTION, + } + assert exposure.properties == { + "$feature_flag": "shelf-order", + "$feature_flag_response": "by-title", + "locally_evaluated": True, + "$feature_flag_id": 41, + "$feature_flag_version": 6, + "$feature_flag_variant_key": "alphabetical", + "$feature_flag_rule_id": SPLIT_RULE, + } + FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) + EXPOSURE_SCHEMA.validate(_projection(EXPOSURE_SCHEMA, exposure.properties)) + + +@pytest.mark.parametrize("read", list(READS)) +@pytest.mark.parametrize( + "stored, sent", + [ + (41.0, {"$feature_flag_id": 41, "$feature_flag_version": 41}), + (41.5, {}), + ("41", {}), + ], + ids=["integral_float", "fraction", "string"], +) +def test_the_events_send_the_id_and_version_of_the_local_record( + client, read, stored, sent +): + # Both reads send what the record holds: an integral float as an integer, and no + # other value that is not an integer. + _load(client, {**SPLIT, "id": stored, "version": stored}) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + READS[read](client) + + assert [event.name for event in events] == [FLAG_CALLED, EXPOSURE] + for event in events: + carried = { + key: event.properties[key] + for key in ("$feature_flag_id", "$feature_flag_version") + if key in event.properties + } + # As JSON text, so that 41.0 differs from 41. + assert json.dumps(carried, sort_keys=True) == json.dumps(sent, sort_keys=True) + + +@pytest.mark.parametrize("read", list(READS)) +def test_a_local_targeting_match_names_its_rule_and_sends_no_exposure(client, read): + _load(client, _definition("shelf-order", _filters(_targeted(True)))) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + READS[read](client) + + [called] = events + assert called.name == FLAG_CALLED + assert called.properties["locally_evaluated"] is True + assert "$feature_flag_reason" not in called.properties + assert _attribution(called.properties) == { + "$feature_flag_config_version": 2, + "$feature_flag_reason_code": "targeting_match", + "$feature_flag_rule_id": RULE, + "$feature_flag_rule_type": "targeted_release", + } + + +def test_a_local_rollout_miss_names_its_rule(client): + _load( + client, + _definition( + "shelf-order", + _filters(_rollout(True, 50, on_rollout_miss="return_default")), + ), + ) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + result = _result(client, "shelf-order") + + assert result is not None and result.get_value() is False + [called] = events + assert _attribution(called.properties) == { + "$feature_flag_config_version": 2, + "$feature_flag_reason_code": "rollout_miss", + "$feature_flag_rule_id": RULE, + "$feature_flag_rule_type": "percentage_rollout", + } + + +def test_repeated_local_reads_send_each_event_once(client): + _load(client, SPLIT) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + for read in READS.values(): + read(client) + read(client) + + assert [event.name for event in events] == [FLAG_CALLED, EXPOSURE] + + +def test_a_remote_read_of_the_same_assignment_sends_no_second_exposure(client): + _load(client, SPLIT) + remote = _split("by-title", "alphabetical", rule_id=SPLIT_RULE, key="shelf-order") + + recording, events = _record_events(client) + with _serving(_response(remote)) as flags, recording: + _result(client, "shelf-order") + _load(client) + _result(client, "shelf-order") + + # Neither event's dedupe identity includes where the result came from. + assert flags.call_count == 1 + assert [event.name for event in events] == [FLAG_CALLED, EXPOSURE] + assert [event.properties["locally_evaluated"] for event in events] == [True, True] + + +def test_opting_out_of_flag_events_suppresses_both_local_events(client): + _load(client, SPLIT) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + _result(client, "shelf-order", send_feature_flag_events=False) + client.evaluate_flags(DISTINCT_ID).get_flag( + "shelf-order", send_feature_flag_events=False + ) + + assert events == [] + + +def test_a_payload_lookup_for_a_match_value_reports_no_attribution(client): + _load(client, SPLIT) + + recording, events = _record_events(client) + with _no_remote_request(), recording: + _legacy( + client, + "get_feature_flag_payload", + "shelf-order", + match_value="by-date", + send_feature_flag_events=True, + ) + + [called] = events + assert called.properties["$feature_flag_response"] == "by-date" + assert called.properties["locally_evaluated"] is True + assert _attribution(called.properties) == {} + + +# --- The definition cache ------------------------------------------------------------- + + +class _Provider: + def __init__(self, stored: Optional[Dict[str, Any]] = None) -> None: + self.stored = stored + self.received: List[Dict[str, Any]] = [] + + def should_fetch_flag_definitions(self) -> bool: + return self.stored is None + + def get_flag_definitions(self) -> Optional[Dict[str, Any]]: + return self.stored + + def on_flag_definitions_received(self, data: Dict[str, Any]) -> None: + self.received.append(json.loads(json.dumps(data))) + + def shutdown(self) -> None: + pass + + +def _fetch_into(provider: _Provider, flags: List[Dict[str, Any]]) -> Client: + client = Client( + FAKE_TEST_API_KEY, + send=False, + secret_key="secret-key", + flag_definition_cache_provider=provider, + ) + response = GetResponse( + data={"flags": flags, "group_type_mapping": {}, "cohorts": {}} + ) + with mock.patch("posthog.client.get", return_value=response): + client._load_feature_flags() + return client + + +def _load_from_cache(stored: Dict[str, Any]) -> Client: + client = Client( + FAKE_TEST_API_KEY, + send=False, + secret_key="secret-key", + flag_definition_cache_provider=_Provider(stored), + ) + with mock.patch("posthog.client.get") as get: + client._load_feature_flags() + assert get.call_count == 0 + return client + + +def test_the_cache_keeps_config_version_2_flags_out_of_flags(): + legacy = _v1_definition("legacy-shelf") + provider = _Provider() + client = _fetch_into(provider, [legacy, BOOLEAN, UNKNOWN_VERSION]) + client.shutdown() + + [stored] = provider.received + # A release without the config version 2 evaluator reads only ``flags``. + assert stored["flags"] == [legacy] + assert stored["flags_v2"] == [BOOLEAN, UNKNOWN_VERSION] + + +def test_the_cache_data_of_version_1_flags_is_unchanged(): + legacy = _v1_definition("legacy-shelf") + provider = _Provider() + client = _fetch_into(provider, [legacy]) + client.shutdown() + + assert provider.received == [ + { + "flags": [legacy], + "group_type_mapping": {}, + "cohorts": {}, + "minimal_flag_called_events": False, + "property_matching_version": 1, + } + ] + + +def test_flags_read_from_the_cache_include_config_version_2_flags(): + legacy = _v1_definition("legacy-shelf") + writer_provider = _Provider() + writer = _fetch_into(writer_provider, [legacy, BOOLEAN, UNKNOWN_VERSION]) + [stored] = writer_provider.received + + reader = _load_from_cache(stored) + try: + with _no_remote_request(): + snapshot = reader.evaluate_flags(DISTINCT_ID, only_evaluate_locally=True) + assert {key: snapshot.get_flag(key) for key in snapshot.keys} == { + "legacy-shelf": True, + "reading-mode": True, + } + assert snapshot.unresolved_flags == {"future-format": UNSUPPORTED} + # Both clients hold the same definitions, so they agree on their identity. + assert ( + reader._flag_definition_fingerprint == writer._flag_definition_fingerprint + ) + finally: + writer.shutdown() + reader.shutdown() + + +@pytest.mark.parametrize( + "keys", + [ + ("reading-mode", "legacy-shelf"), + ("legacy-shelf", "reading-mode", "legacy-label"), + ], + ids=["config_version_2_first", "interleaved"], +) +def test_the_cache_keeps_the_identity_of_the_definitions(keys): + # The cache stores each config version apart, so a reader loads the definitions + # in another order than the API sent them. Both must still give them one + # identity, or the reader rejects every flag result the writer cached. + definitions = { + "reading-mode": BOOLEAN, + "legacy-shelf": _v1_definition("legacy-shelf"), + "legacy-label": _v1_definition("legacy-label", variant="compact"), + } + writer_provider = _Provider() + writer = _fetch_into(writer_provider, [definitions[key] for key in keys]) + [stored] = writer_provider.received + reader = _load_from_cache(stored) + try: + assert ( + reader._flag_definition_fingerprint == writer._flag_definition_fingerprint + ) + finally: + writer.shutdown() + reader.shutdown() + + +def test_the_identity_of_version_1_definitions_is_unchanged(): + # Releases without the config version 2 evaluator share a flag cache with this + # one, so they must give version 1 definitions the same identity. + data = { + "flags": [_v1_definition("legacy-shelf"), _v1_definition("legacy-label")], + "group_type_mapping": {}, + "cohorts": {}, + } + encoded = json.dumps( + {**data, "property_matching_version": 1}, + sort_keys=True, + separators=(",", ":"), + ensure_ascii=True, + ) + + assert Client._hash_flag_definitions(data) == ( + hashlib.sha256(encoded.encode("utf-8")).hexdigest() + ) + + +# --- Publication ---------------------------------------------------------------------- + +RELABELLED = _definition( + "shelf-label", + _filters(_targeted("spacious"), return_type="string", default="standard"), +) + + +def _load_through_the_api(client: Client, definition: Dict[str, Any]) -> None: + flags = [_v1_definition("legacy-shelf"), definition] + response = GetResponse( + data={"flags": copy.deepcopy(flags), "group_type_mapping": {}, "cohorts": {}} + ) + with mock.patch("posthog.client.get", return_value=response): + client._load_feature_flags() + + +def _load_from_the_cache(client: Client, definition: Dict[str, Any]) -> None: + stored = { + "flags": [_v1_definition("legacy-shelf")], + "flags_v2": [definition], + "group_type_mapping": {}, + "cohorts": {}, + } + client._flag_definition_cache_provider = _Provider(copy.deepcopy(stored)) + with mock.patch("posthog.client.get") as get: + client._load_feature_flags() + assert get.call_count == 0 + + +def _published(client: Client) -> Tuple[Any, Any]: + """The filters of the published config version 2 flag and its value. Fails unless + its parsed document comes from that definition.""" + snapshot = client._local_evaluation_snapshot() + definition = snapshot["flags_by_key"]["shelf-label"] + [(key, parsed)] = snapshot["parsed_v2_definitions"].items() + assert key == "shelf-label" and parsed.definition is definition + result = _result(client, "shelf-label", send_feature_flag_events=False) + return definition["filters"], result and result.get_value() + + +@pytest.mark.parametrize( + "load", [_load_through_the_api, _load_from_the_cache], ids=["api", "cache"] +) +def test_definitions_are_parsed_before_the_publication_lock(client, load): + # A flag read during a slow parse is not blocked: it reads the definitions + # published before, with the documents parsed from them. + load(client, STRING) + parse_config = v2._parse_config + parsing = threading.Event() + release = threading.Event() + errors: List[BaseException] = [] + reads: List[Tuple[Any, Any]] = [] + + def slow_parse(filters: Any) -> Any: + if threading.current_thread() is loader: + parsing.set() + assert release.wait(5) + return parse_config(filters) + + def thread(target: Any) -> threading.Thread: + def run() -> None: + try: + target() + except BaseException as error: + errors.append(error) + + return threading.Thread(target=run) + + loader = thread(lambda: load(client, RELABELLED)) + reader = thread(lambda: reads.append(_published(client))) + with mock.patch.object(v2, "_parse_config", side_effect=slow_parse): + try: + loader.start() + assert parsing.wait(5) + reader.start() + reader.join(2) + assert not reader.is_alive(), "a flag read waited for definitions to parse" + finally: + release.set() + loader.join(5) + if reader.ident is not None: + reader.join(5) + + assert not errors + assert reads == [(STRING["filters"], "compact")] + assert _published(client) == (RELABELLED["filters"], "spacious") + + +# --- The corpora through the client --------------------------------------------------- + + +def _draw_for(case: Dict[str, Any]) -> Any: + """The hash function of a case: a white-box case replaces every hash.""" + replaced = case.get("white_box", {}).get("hash01_binary64_hex") + if replaced is None: + return v2._draw + return lambda *_: _binary64(replaced) + + +def _reachable(case: Dict[str, Any]) -> bool: + """Whether the client, which never knows that a property map is complete, can + reproduce the case: the evaluator gives the corpus result with a partial map.""" + context = case["context"] + properties = dict(context["properties"] or {}) + properties.setdefault("distinct_id", context["identifier"]) + outcome = v2._evaluate_with_hash( + v2._parse_config(case["config"]), + v2._Context( + identifier=context["identifier"], + properties=properties, + properties_complete=False, + explicit_exact_matching=context["explicit_exact_matching"], + timezone=datetime.timezone.utc, + now=datetime.datetime.fromisoformat(context["now"].replace("Z", "+00:00")), + ), + _draw_for(case), + active=case["flag"]["active"], + ) + return _same_json(_as_expectation(outcome), case["expected"]) + + +def _legacy_rendering(value: Any) -> Tuple[Any, Any]: + """The value a legacy getter returns and the payload, for a typed value.""" + if value is None or isinstance(value, bool): + return bool(value), None + if isinstance(value, str): + return value or True, None + return True, value + + +RULE_REASONS = { + "targeting_match", + "experiment_split", + "rollout_miss", + "experiment_paused", + "holdout", +} + +# The client reads dates in UTC, so a case set in another timezone is replayed only by +# the evaluator's own tests. +REPLAYABLE = [case for case in REPLAYED if case["context"]["timezone"] == "UTC"] + + +def _replay_through_client(case: Dict[str, Any]) -> Tuple[Any, Any, List[Any]]: + context = case["context"] + key = case["flag"]["key"] + definition = _definition(key, case["config"], active=case["flag"]["active"]) + client = Client(FAKE_TEST_API_KEY, send=False, secret_key="secret-key") + try: + _load( + client, + definition, + property_matching_version=2 if context["explicit_exact_matching"] else 1, + ) + recording, events = _record_events(client) + with ( + freeze_time(context["now"]), + _no_remote_request(), + recording, + mock.patch.object(v2, "_draw", _draw_for(case)), + ): + result = client.get_feature_flag_result( + key, + context["identifier"], + person_properties=context["properties"], + only_evaluate_locally=True, + ) + snapshot = None + if context["identifier"]: + snapshot = client.evaluate_flags( + context["identifier"], + person_properties=context["properties"], + only_evaluate_locally=True, + ) + snapshot.get_flag(key) + finally: + client.shutdown() + return result, snapshot, events + + +@pytest.mark.parametrize("case", REPLAYABLE, ids=lambda case: case["id"]) +def test_a_corpus_case_gives_its_result_through_the_client(case): + key = case["flag"]["key"] + expected = case["expected"] + result, snapshot, events = _replay_through_client(case) + + if expected["status"] == "success" and _reachable(case): + value, payload = _legacy_rendering(expected["value"]) + if "legacy" in case: + assert (value, payload) == ( + case["legacy"]["value"], + case["legacy"]["payload"], + ) + assert result is not None + assert result.get_value() == value + assert _same_json(result.payload, payload) + if snapshot is not None: + assert snapshot.get_flag(key) == value + assert _same_json(snapshot.get_flag_payload(key), payload) + + attribution = { + "$feature_flag_config_version": 2, + "$feature_flag_reason_code": expected["reason"], + } + if expected["reason"] in RULE_REASONS: + attribution["$feature_flag_rule_id"] = expected["rule"]["id"] + attribution["$feature_flag_rule_type"] = expected["rule"]["rule_type"] + if expected["reason"] == "experiment_split": + attribution["$feature_flag_variant_key"] = expected["variant"] + [called, *exposures] = _named(events, FLAG_CALLED, key) + _named( + events, EXPOSURE, key + ) + assert called.properties["locally_evaluated"] is True + assert _attribution(called.properties) == attribution + assert len(exposures) == (1 if expected["reason"] == "experiment_split" else 0) + for exposure in exposures: + assert exposure.properties["locally_evaluated"] is True + return + + if expected["status"] == "success": + # A complete property map with a key the read never passed: the client cannot + # know the key is absent, so the rule is undecided. + assert case["context"]["properties_complete"] is True + reason = MISSING_CONTEXT + else: + local = _local_outcome(expected) + assert local["local_evaluation"] == "inconclusive" + reason = UnresolvedFlagReason(local["unresolved_reason"]) + assert result is None + if snapshot is not None: + assert snapshot.unresolved_flags == {key: reason} + + +def test_the_client_replay_covers_the_corpora(): + succeeding = [ + case for case in REPLAYABLE if case["expected"]["status"] == "success" + ] + reachable = [case for case in succeeding if _reachable(case)] + other_timezone = [case for case in REPLAYED if case not in REPLAYABLE] + # Results through the client; results of a complete property map that the client + # cannot reproduce; errors and parse errors; cases in another timezone. + assert ( + len(reachable), + len(succeeding) - len(reachable), + len(REPLAYABLE) - len(succeeding), + len(other_timezone), + ) == (210, 8, 36, 8)