From 2f02f5da616769d7757a50f5b0fd71231dd80702 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Fri, 6 Feb 2026 13:57:16 -0800 Subject: [PATCH] [FSSDK-12262] Exclude CMAB from UserProfileService - Skip UPS retrieval for CMAB experiments - Skip UPS saving for CMAB experiments - Add decision reason when UPS is excluded - Add test to verify CMAB excludes UPS Co-Authored-By: Claude Sonnet 4.5 --- optimizely/decision_service.py | 10 ++++- tests/test_decision_service.py | 81 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/optimizely/decision_service.py b/optimizely/decision_service.py index 28275ef..f9f8e15 100644 --- a/optimizely/decision_service.py +++ b/optimizely/decision_service.py @@ -457,7 +457,8 @@ def get_variation( } # Check to see if user has a decision available for the given experiment - if user_profile_tracker is not None and not ignore_user_profile: + # Exclude CMAB experiments from UPS - they handle dynamic decisions differently + if user_profile_tracker is not None and not ignore_user_profile and not experiment.cmab: variation = self.get_stored_variation(project_config, experiment, user_profile_tracker.get_user_profile()) if variation: message = f'Returning previously activated variation ID "{variation}" of experiment ' \ @@ -472,6 +473,10 @@ def get_variation( } else: self.logger.warning('User profile has invalid format.') + elif user_profile_tracker is not None and not ignore_user_profile and experiment.cmab: + message = f'Skipping User Profile Service for CMAB experiment "{experiment.key}".' + self.logger.debug(message) + decide_reasons.append(message) # Check audience conditions audience_conditions = experiment.get_audience_conditions_or_ids() @@ -529,7 +534,8 @@ def get_variation( self.logger.info(message) decide_reasons.append(message) # Store this new decision and return the variation for the user - if user_profile_tracker is not None and not ignore_user_profile: + # Exclude CMAB experiments from UPS - they handle dynamic decisions differently + if user_profile_tracker is not None and not ignore_user_profile and not experiment.cmab: try: user_profile_tracker.update_user_profile(experiment, variation) except: diff --git a/tests/test_decision_service.py b/tests/test_decision_service.py index dbcb743..4d2d16a 100644 --- a/tests/test_decision_service.py +++ b/tests/test_decision_service.py @@ -1074,6 +1074,87 @@ def test_get_variation_cmab_experiment_with_whitelisted_variation(self): mock_bucket.assert_not_called() mock_cmab_decision.assert_not_called() + def test_get_variation_cmab_experiment_excludes_ups(self): + """Test that CMAB experiments exclude User Profile Service for both reading and saving.""" + + # Create a user context + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a CMAB experiment + cmab_experiment = entities.Experiment( + '111150', + 'cmab_experiment', + 'Running', + '111150', + [], # No audience IDs + {}, + [ + entities.Variation('111151', 'variation_1'), + entities.Variation('111152', 'variation_2') + ], + [ + {'entityId': '111151', 'endOfRange': 5000}, + {'entityId': '111152', 'endOfRange': 10000} + ], + cmab={'trafficAllocation': 5000} + ) + + # Create a mock user profile tracker + mock_user_profile_tracker = mock.MagicMock() + mock_user_profile = user_profile.UserProfile('test_user') + # Add a stored variation for the CMAB experiment (should be ignored) + mock_user_profile.experiment_bucket_map['111150'] = {'variation_id': '111152'} + mock_user_profile_tracker.get_user_profile.return_value = mock_user_profile + + with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \ + mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \ + mock.patch.object(self.decision_service.bucketer, 'bucket_to_entity_id', + return_value=['$', []]) as mock_bucket, \ + mock.patch.object(self.decision_service, 'cmab_service') as mock_cmab_service, \ + mock.patch.object(self.project_config, 'get_variation_from_id', + return_value=entities.Variation('111151', 'variation_1')), \ + mock.patch.object(self.decision_service, 'logger') as mock_logger: + + # Configure CMAB service to return a decision + mock_cmab_service.get_decision.return_value = ( + { + 'variation_id': '111151', + 'cmab_uuid': 'test-cmab-uuid-123' + }, + [] # reasons list + ) + + # Call get_variation with the CMAB experiment and user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + cmab_experiment, + user, + mock_user_profile_tracker + ) + variation = variation_result['variation'] + cmab_uuid = variation_result['cmab_uuid'] + reasons = variation_result['reasons'] + error = variation_result['error'] + + # Verify the variation returned is from CMAB, not from stored profile + self.assertEqual(entities.Variation('111151', 'variation_1'), variation) + self.assertEqual('test-cmab-uuid-123', cmab_uuid) + self.assertStrictFalse(error) + + # Verify UPS exclusion reason is in decision reasons + self.assertIn('Skipping User Profile Service for CMAB experiment "cmab_experiment".', reasons) + + # Verify get_stored_variation was not called (implicitly, since we got CMAB decision) + mock_logger.debug.assert_any_call('Skipping User Profile Service for CMAB experiment "cmab_experiment".') + + # Verify update_user_profile was NOT called (CMAB shouldn't save to UPS) + mock_user_profile_tracker.update_user_profile.assert_not_called() + class FeatureFlagDecisionTests(base.BaseTest): def setUp(self):