Skip to content

[FSSDK-11175] Update: Implement Decision Service methods to handle CMAB #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
88b4f1e
update: integrate CMAB components into OptimizelyFactory
FarhanAnjum-opti Jun 4, 2025
2563c7b
update: add cmab_service parameter to Optimizely constructor for CMAB…
FarhanAnjum-opti Jun 4, 2025
fac8946
update: add docstring to DefaultCmabService class for improved docume…
FarhanAnjum-opti Jun 4, 2025
f74bc8c
update: implement CMAB support in bucketer and decision service, reve…
FarhanAnjum-opti Jun 13, 2025
6d1f73d
linting fix
FarhanAnjum-opti Jun 13, 2025
91d53b6
update: add cmab_uuid handling in DecisionService and related tests
FarhanAnjum-opti Jun 16, 2025
3eb755f
- updated function bucket_to_entity_id
FarhanAnjum-opti Jun 16, 2025
a5e4993
update: add None parameter to Decision constructor in user context tests
FarhanAnjum-opti Jun 16, 2025
c1cd97a
update: enhance CMAB decision handling and add related tests
FarhanAnjum-opti Jun 16, 2025
fd7c723
update: fix logger message formatting in CMAB experiment tests
FarhanAnjum-opti Jun 16, 2025
ec19c3b
mypy fix
FarhanAnjum-opti Jun 16, 2025
029262d
update: refine traffic allocation type hints and key naming in bucket…
FarhanAnjum-opti Jun 16, 2025
180fdee
update: remove unused import of cast in bucketer.py
FarhanAnjum-opti Jun 16, 2025
cd5ba39
update: fix return type for numeric_metric_value in get_numeric_value…
FarhanAnjum-opti Jun 16, 2025
92a3258
update: specify type hint for numeric_metric_value in get_numeric_val…
FarhanAnjum-opti Jun 16, 2025
fe100cb
update: fix logger reference in DefaultCmabClient initialization and …
FarhanAnjum-opti Jun 17, 2025
60a4ada
update: enhance error logging for CMAB fetch failures with detailed m…
FarhanAnjum-opti Jun 20, 2025
265d82b
update: enhance decision result handling by introducing VariationResu…
FarhanAnjum-opti Jun 20, 2025
6ca1102
update: refactor get_variation return structure and change tests acco…
FarhanAnjum-opti Jun 20, 2025
c2b3d96
-Error propagated to optimizely.py
FarhanAnjum-opti Jun 23, 2025
b901c5f
update: modify get_variation to return VariationResult and adjust rel…
FarhanAnjum-opti Jun 27, 2025
d2fc631
update: unit test fixes
FarhanAnjum-opti Jun 27, 2025
b9a8555
Revert "update: unit test fixes"
FarhanAnjum-opti Jun 30, 2025
a129854
Revert "update: modify get_variation to return VariationResult and ad…
FarhanAnjum-opti Jun 30, 2025
c637878
update: enhance decision service to handle error states and improve b…
FarhanAnjum-opti Jul 3, 2025
0bc4fbd
update: remove debug print statement from Optimizely class
FarhanAnjum-opti Jul 3, 2025
fcdad1f
update: enhance bucketing logic to support CMAB traffic allocations
FarhanAnjum-opti Jul 3, 2025
aca7df4
update: improve error logging for CMAB decision fetch failures
FarhanAnjum-opti Jul 3, 2025
72955a0
update: improve logging and error handling in bucketer and decision s…
FarhanAnjum-opti Jul 7, 2025
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
64 changes: 47 additions & 17 deletions optimizely/decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ def get_variation(
b. If so, fetch the CMAB decision and assign the corresponding variation and cmab_uuid.
7. For non-CMAB experiments, bucket the user into a variation.
8. If a variation is assigned, optionally update the user profile.
9. Return the assigned variation, decision reasons, and cmab_uuid (if applicable).

Args:
project_config: Instance of ProjectConfig.
Expand All @@ -374,10 +373,11 @@ def get_variation(
options: Decide options.

Returns:
A tuple of:
- The assigned Variation (or None if not assigned).
- A list of log messages representing decision making.
- The cmab_uuid if the experiment is a CMAB experiment, otherwise None.
A VariationResult dictionary with:
- 'variation': The assigned Variation (or None if not assigned).
- 'reasons': A list of log messages representing decision making.
- 'cmab_uuid': The cmab_uuid if the experiment is a CMAB experiment, otherwise None.
- 'error': Boolean indicating if an error occurred during the decision process.
"""
user_id = user_context.user_id
if options:
Expand Down Expand Up @@ -657,7 +657,7 @@ def get_variation_for_feature(
feature: entities.FeatureFlag,
user_context: OptimizelyUserContext,
options: Optional[list[str]] = None
) -> tuple[Decision, list[str]]:
) -> DecisionResult:
""" Returns the experiment/variation the user is bucketed in for the given feature.

Args:
Expand All @@ -667,8 +667,11 @@ def get_variation_for_feature(
options: Decide options.

Returns:
Decision namedtuple consisting of experiment and variation for the user.
"""
A DecisionResult dictionary containing:
- 'decision': Decision namedtuple with experiment, variation, source, and cmab_uuid.
- 'error': Boolean indicating if an error occurred during the decision process.
- 'reasons': List of log messages representing decision making for the feature.
"""
return self.get_variations_for_feature_list(project_config, [feature], user_context, options)[0]

def validated_forced_decision(
Expand Down Expand Up @@ -740,17 +743,21 @@ def get_variations_for_feature_list(
features: list[entities.FeatureFlag],
user_context: OptimizelyUserContext,
options: Optional[Sequence[str]] = None
) -> list[tuple[Decision, list[str]]]:
) -> list[DecisionResult]:
"""
Returns the list of experiment/variation the user is bucketed in for the given list of features.

Args:
project_config: Instance of ProjectConfig.
features: List of features for which we are determining if it is enabled or not for the given user.
user_context: user context for user.
options: Decide options.
project_config: Instance of ProjectConfig.
features: List of features for which we are determining if it is enabled or not for the given user.
user_context: user context for user.
options: Decide options.

Returns:
List of Decision namedtuple consisting of experiment and variation for the user.
A list of DecisionResult dictionaries, each containing:
- 'decision': Decision namedtuple with experiment, variation, source, and cmab_uuid.
- 'error': Boolean indicating if an error occurred during the decision process.
- 'reasons': List of log messages representing decision making for each feature.
"""
decide_reasons: list[str] = []

Expand Down Expand Up @@ -786,27 +793,45 @@ def get_variations_for_feature_list(
if forced_decision_variation:
decision_variation = forced_decision_variation
cmab_uuid = None
error = False
else:
variation_result = self.get_variation(
project_config, experiment, user_context, user_profile_tracker, feature_reasons, options
)
cmab_uuid = variation_result['cmab_uuid']
variation_reasons = variation_result['reasons']
decision_variation = variation_result['variation']
error = variation_result['error']
feature_reasons.extend(variation_reasons)

if error:
decision = Decision(experiment, None, enums.DecisionSources.FEATURE_TEST, cmab_uuid)
decision_result: DecisionResult = {
'decision': decision,
'error': True,
'reasons': feature_reasons
}
decisions.append(decision_result)
experiment_decision_found = True
break

if decision_variation:
self.logger.debug(
f'User "{user_context.user_id}" '
f'bucketed into experiment "{experiment.key}" of feature "{feature.key}".'
)
decision = Decision(experiment, decision_variation,
enums.DecisionSources.FEATURE_TEST, cmab_uuid)
decisions.append((decision, feature_reasons))
decision_result = {
'decision': decision,
'error': False,
'reasons': feature_reasons
}
decisions.append(decision_result)
experiment_decision_found = True # Mark that a decision was found
break # Stop after the first successful experiment decision

# Only process rollout if no experiment decision was found
# Only process rollout if no experiment decision was found and no error
if not experiment_decision_found:
rollout_decision, rollout_reasons = self.get_variation_for_rollout(project_config,
feature,
Expand All @@ -820,7 +845,12 @@ def get_variations_for_feature_list(
self.logger.debug(f'User "{user_context.user_id}" '
f'not bucketed into any rollout for feature "{feature.key}".')

decisions.append((rollout_decision, feature_reasons))
decision_result = {
'decision': rollout_decision,
'error': False,
'reasons': feature_reasons
}
decisions.append(decision_result)

if self.user_profile_service is not None and user_profile_tracker is not None and ignore_ups is False:
user_profile_tracker.save_user_profile()
Expand Down
16 changes: 11 additions & 5 deletions optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ def _get_feature_variable_for_type(

user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)

decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
decision_result = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
decision = decision_result['decision']

if decision.variation:

Expand Down Expand Up @@ -444,7 +445,9 @@ def _get_all_feature_variables_for_type(

user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)

decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
decision = self.decision_service.get_variation_for_feature(project_config,
feature_flag,
user_context)['decision']

if decision.variation:

Expand Down Expand Up @@ -715,7 +718,7 @@ def is_feature_enabled(self, feature_key: str, user_id: str, attributes: Optiona

user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)

decision, _ = self.decision_service.get_variation_for_feature(project_config, feature, user_context)
decision = self.decision_service.get_variation_for_feature(project_config, feature, user_context)['decision']
is_source_experiment = decision.source == enums.DecisionSources.FEATURE_TEST
is_source_rollout = decision.source == enums.DecisionSources.ROLLOUT

Expand Down Expand Up @@ -1368,8 +1371,11 @@ def _decide_for_keys(
)

for i in range(0, len(flags_without_forced_decision)):
decision = decision_list[i][0]
reasons = decision_list[i][1]
decision = decision_list[i]['decision']
reasons = decision_list[i]['reasons']
# Can catch errors now. Not used as decision logic implicitly handles error decision.
# Will be required for impression events
# error = decision_list[i]['error']
flag_key = flags_without_forced_decision[i].key
flag_decisions[flag_key] = decision
decision_reasons_dict[flag_key] += reasons
Expand Down
Loading