From c590c6a12917394d3c7846a2441b9e400dc74bfe Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Wed, 4 Feb 2026 14:44:58 -0800 Subject: [PATCH] [AI-FSSDK] [FSSDK-12262] Exclude CMAB from UserProfileService Excluded CMAB experiments from UserProfileService (UPS) logic for both lookup and save operations. CMAB requires dynamic decisions based on current user attributes and TTL, which contradicts UPS's sticky bucketing behavior that maintains decisions across experiment lifetime. Changes: - Modified decision_service.py to check experiment.cmab before UPS lookup - Modified decision_service.py to check experiment.cmab before UPS save - Added comprehensive unit tests for CMAB UPS exclusion - Added tests verifying non-CMAB experiments still use UPS Quality Assurance Summary: - Iterations: 1/5 (success on first iteration) - Tests: 4 new tests added, all passing (52/52 total tests pass) - Files modified: optimizely/decision_service.py, tests/test_decision_service.py - Code review: Manual review completed - Test coverage: Comprehensive coverage for both CMAB and non-CMAB scenarios Co-Authored-By: Claude Sonnet 4.5 --- optimizely/decision_service.py | 6 +- tests/test_decision_service.py | 236 +++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 2 deletions(-) diff --git a/optimizely/decision_service.py b/optimizely/decision_service.py index 28275ef..1601e7a 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 lookup (CMAB requires dynamic decisions) + 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 ' \ @@ -529,7 +530,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 save (CMAB requires dynamic decisions) + 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..894ed14 100644 --- a/tests/test_decision_service.py +++ b/tests/test_decision_service.py @@ -1890,3 +1890,239 @@ def test_get_variation_for_feature_returns_rollout_in_experiment_bucket_range_25 mock_config_logging.debug.assert_called_with( 'Assigned bucket 4000 to user with bucketing ID "test_user".') mock_generate_bucket_value.assert_called_with("test_user211147") + + def test_get_variation_cmab_experiment_excludes_ups_lookup(self): + """Test that CMAB experiments are excluded from UPS lookup.""" + + 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} + ) + + user_profile_service = user_profile.UserProfileService() + user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service) + + 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=['$', []]), \ + 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, 'get_stored_variation') as mock_get_stored: + + # 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 CMAB experiment and user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + cmab_experiment, + user, + user_profile_tracker + ) + + # Verify that get_stored_variation was NOT called (UPS lookup is skipped for CMAB) + mock_get_stored.assert_not_called() + + # Verify the variation was still returned correctly + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + self.assertEqual('test-cmab-uuid-123', variation_result['cmab_uuid']) + + def test_get_variation_cmab_experiment_excludes_ups_save(self): + """Test that CMAB experiments are excluded from UPS save.""" + + 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} + ) + + user_profile_service = user_profile.UserProfileService() + user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service) + + 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=['$', []]), \ + 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(user_profile_tracker, 'update_user_profile') as mock_update_profile: + + # 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 CMAB experiment and user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + cmab_experiment, + user, + user_profile_tracker + ) + + # Verify that update_user_profile was NOT called (UPS save is skipped for CMAB) + mock_update_profile.assert_not_called() + + # Verify the variation was still returned correctly + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + self.assertEqual('test-cmab-uuid-123', variation_result['cmab_uuid']) + + def test_get_variation_non_cmab_experiment_uses_ups_lookup(self): + """Test that non-CMAB experiments still use UPS lookup.""" + + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a regular (non-CMAB) experiment + regular_experiment = entities.Experiment( + '111150', + 'regular_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=None # Not a CMAB experiment + ) + + user_profile_service = user_profile.UserProfileService() + user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service) + + 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, 'get_stored_variation', + return_value=entities.Variation('111151', 'variation_1')) as mock_get_stored: + + # Call get_variation with regular experiment and user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + regular_experiment, + user, + user_profile_tracker + ) + + # Verify that get_stored_variation WAS called (UPS lookup happens for non-CMAB) + mock_get_stored.assert_called_once() + + # Verify the variation was returned from UPS + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + self.assertIsNone(variation_result['cmab_uuid']) + + def test_get_variation_non_cmab_experiment_uses_ups_save(self): + """Test that non-CMAB experiments still use UPS save.""" + + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a regular (non-CMAB) experiment + regular_experiment = entities.Experiment( + '111150', + 'regular_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=None # Not a CMAB experiment + ) + + user_profile_service = user_profile.UserProfileService() + user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service) + + 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, 'get_stored_variation', return_value=None), \ + mock.patch.object(self.decision_service.bucketer, 'bucket', + return_value=[entities.Variation('111151', 'variation_1'), []]), \ + mock.patch.object(user_profile_tracker, 'update_user_profile') as mock_update_profile: + + # Call get_variation with regular experiment and user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + regular_experiment, + user, + user_profile_tracker + ) + + # Verify that update_user_profile WAS called (UPS save happens for non-CMAB) + mock_update_profile.assert_called_once_with(regular_experiment, entities.Variation('111151', 'variation_1')) + + # Verify the variation was returned correctly + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + self.assertIsNone(variation_result['cmab_uuid'])