Repository: RasaHQ/rasa
Pull request: Bug restart fallback
**Proposed changes**:
- Fixes https://github.com/RasaHQ/rasa/issues/4601
- Changed `TwoStageFallbackPolicy._is_user_input_expected` to return `true` only if user input has not been given since executing `action_default_fallback`. 

When the two stage fallback policy is used together with the mapping policy with default priorities, and the user's implementation of `action_default_fallback` asks for user input to restart, the two stage fallback policy was still predicting `action_listen` with confidence 1.0 and therefore overriding the mapping policies prediction of `action_restart`

**Status (please check what you already did)**:
- [x] added some tests for the functionality
- [ ] updated the documentation
- [x] updated the changelog (please check [changelog](https://github.com/RasaHQ/rasa/tree/master/changelog) for instructions)
- [x] reformat files using `black` (please check [Readme](https://github.com/RasaHQ/rasa#code-style) for instructions)


Problem statement:
Restart button not working in two-stage fallback policy
For a question for which it is falling under 2-stage fallback and showing buttons, but if without selecting from those options I am again posting a question then it is giving Restart option. The conversation is getting hanged after this **Restart** button even though if I am clicking on the **Restart** button.

Rasa Version : 1.3.7

Below is my Action file code : 

class ActionDefaultFallback(Action):
    def name(self) -> Text:
        return "action_default_fallback"

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]
    ) -> List["Event"]:
        # Fallback caused by TwoStageFallbackPolicy

        logger.debug(tracker.events)
        logger.debug("tracker.events[-4].name = ''%s'", tracker.events[-4].get("name"))
        if (
            len(tracker.events) >= 4
            and tracker.events[-4].get("name") == "action_default_ask_affirmation"
        ):
            dispatcher.utter_template("utter_restart_with_button", tracker)
            return [SlotSet("feedback_value", "poor"), ConversationPaused()]

        # Fallback caused by Core
        else:
            dispatcher.utter_template("utter_default", tracker)
            return [UserUtteranceReverted()]

class ActionDefaultAskAffirmation(Action):
    """Asks for an affirmation of the intent if NLU threshold is not met."""

    def name(self) -> Text:
        return "action_default_ask_affirmation"

    def __init__(self) -> None:
        import pandas as pd

        self.intent_mappings = pd.read_csv("data/" "intent_description_mapping.csv")
        self.intent_mappings.fillna("", inplace=True)
        self.intent_mappings.entities = self.intent_mappings.entities.map(
            lambda entities: {e.strip() for e in entities.split(",")}
        )

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List["Event"]:

      intent_ranking = tracker.latest_message.get("intent_ranking", [])
        if len(intent_ranking) > 1:
            diff_intent_confidence = intent_ranking[0].get(
                "confidence"
            ) - intent_ranking[1].get("confidence")
            if diff_intent_confidence < 0.2:
                intent_ranking = intent_ranking[:2]
            else:
                intent_ranking = intent_ranking[:1]
        first_intent_names = [
            intent.get("name", "")
            for intent in intent_ranking
            if intent.get("name", "") != "out_of_scope"
        ]

        message_title = (
            "Sorry, I'm not sure I've understood " "you correctly. Do you mean..."
        )

        entities = tracker.latest_message.get("entities", [])
        entities = {e["entity"]: e["value"] for e in entities}

        entities_json = json.dumps(entities)

        buttons = []
        intent_max_count = 4
        intent_count = 0
        for intent in first_intent_names:
            if intent_count < intent_max_count:
                intent_count = intent_count + 1
                logger.debug(intent)
                logger.debug(entities)
                buttons.append(
                    {
                        "title": self.get_button_title(intent, entities),
                        "payload": "/{}{}".format(intent, entities_json),
                    }
                )

        buttons.append({"title": "Something else", "payload": "/ask_human_handoff"})

        dispatcher.utter_button_message(message_title, buttons=buttons)

        return []

Hints from issue discussion:
Thanks for raising this issue, @btotharye will get back to you about it soon✨

###### Please also check out the [docs](https://rasa.com/docs/) and the [forum](https://forum.rasa.com/) in case your issue was raised there too 🤗
Hey @Pratiksha1312 sorry for the late response was pretty busy the past few days.  I'm going to look into this now and see if I can reproduce it.

Do you have logs on this from say doing `rasa shell --debug` that you can post as well?

Also do you have the template reference for `utter_restart_with_button` so I can see what that is doing.

Thanks
hello @Pratiksha1312 just checking back again to see if you have the logs from this and the other items I mentioned in the previous comment?
reproducible on https://github.com/RasaHQ/rasa-demo. To reproduce:
```
> confirm@0.75
> /out_of_scope
> confirm@0.75
> /out_of_scope
> /restart
```
restart is predicted by the mapping policy, but because two stage fallback predicts action_listen, and it has higher priority, restart is not correctly executed.
@erohmensing when trying to reproduce, I am presented with a /restart option twice in a row, but it works the second time it is selected. Is that the same behaviour you were seeing?
Hm, I don't think so. I've also seen the regular restart button from `action_default_fallback` not work as well. Can you post the debug logs from a conversation? We have noticed it specifically in Sara
I don't see an intent `confirm` in the domain of Sara (incl. the version of master on 5 Nov) - should I be on some other branch?
This is on Sara on the current master branch - 
```
Your input ->  /affirm@0.75
2020-01-15 17:14:03 DEBUG    rasa.core.tracker_store  - Creating a new tracker for id 'default'.
2020-01-15 17:14:03 DEBUG    rasa.core.processor  - Starting a new session for conversation ID 'default'.
2020-01-15 17:14:03 DEBUG    rasa.core.processor  - Action 'action_session_start' ended with events '[<rasa.core.events.SessionStarted object at 0x15d955198>, <rasa.core.events.ActionExecuted object at 0x15d955160>]'.
2020-01-15 17:14:03 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: None
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:03 DEBUG    rasa.core.processor  - Received user message '/affirm@0.75' with intent '{'name': 'affirm', 'confidence': 0.75}' and entities '[]'
2020-01-15 17:14:03 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 4 events.
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:04 DEBUG    rasa.core.policies.fallback  - NLU confidence 0.75 is lower than NLU threshold 0.80.
2020-01-15 17:14:04 DEBUG    rasa.core.policies.two_stage_fallback  - User 'default' has to affirm intent 'affirm'.
2020-01-15 17:14:04 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:04 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:04 DEBUG    rasa.core.processor  - Predicted next action 'action_default_ask_affirmation' with confidence 1.00.
2020-01-15 17:14:04 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_default_ask_affirmation'.
2020-01-15 17:14:04 DEBUG    rasa.core.processor  - Action 'action_default_ask_affirmation' ended with events '[BotUttered('Sorry, I'm not sure I've understood you correctly 🤔 Do you mean...', {"elements": null, "quick_replies": null, "buttons": [{"title": "Yes", "payload": "/affirm{}"}, {"title": "Something else", "payload": "/out_of_scope"}], "attachment": null, "image": null, "custom": null}, {}, 1579104844.25521)]'.
2020-01-15 17:14:04 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: None
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_default_ask_affirmation': 1.0}]
2020-01-15 17:14:04 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:04 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:04 DEBUG    rasa.core.policies.mapping_policy  - There is no mapped action for the predicted intent, 'affirm'.
2020-01-15 17:14:04 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:04 DEBUG    rasa.core.processor  - Predicted next action 'action_listen' with confidence 1.00.
2020-01-15 17:14:04 DEBUG    rasa.core.processor  - Action 'action_listen' ended with events '[]'.
2020-01-15 17:14:04 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
? Sorry, I'm not sure I've understood you correctly 🤔 Do you mean...  2: Something else (/out_of_scope)
2020-01-15 17:14:08 DEBUG    rasa.core.tracker_store  - Recreating tracker for id 'default'
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Received user message '/out_of_scope' with intent '{'name': 'out_of_scope', 'confidence': 1.0}' and entities '[]'
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 8 events.
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - There is a memorised next action '163'
2020-01-15 17:14:08 DEBUG    rasa.core.policies.two_stage_fallback  - User 'default' denied suggested intents.
2020-01-15 17:14:08 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:08 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Predicted next action 'action_default_ask_rephrase' with confidence 1.00.
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Action 'action_default_ask_rephrase' ended with events '[]'.
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: None
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}]
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_default_ask_rephrase': 1.0}]
2020-01-15 17:14:08 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:08 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:08 DEBUG    rasa.core.policies.mapping_policy  - There is no mapped action for the predicted intent, 'out_of_scope'.
2020-01-15 17:14:08 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Predicted next action 'action_listen' with confidence 1.00.
2020-01-15 17:14:08 DEBUG    rasa.core.processor  - Action 'action_listen' ended with events '[]'.
2020-01-15 17:14:08 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
? Sorry, I'm not sure I've understood you correctly 🤔 Do you mean...  Type out your own message...
Your input ->  /affirm@0.75
2020-01-15 17:14:15 DEBUG    rasa.core.tracker_store  - Recreating tracker for id 'default'
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Received user message '/affirm@0.75' with intent '{'name': 'affirm', 'confidence': 0.75}' and entities '[]'
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 11 events.
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Current tracker state [{}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:15 DEBUG    rasa.core.policies.fallback  - NLU confidence 0.75 is lower than NLU threshold 0.80.
2020-01-15 17:14:15 DEBUG    rasa.core.policies.two_stage_fallback  - Ambiguous rephrasing of user 'default' for intent 'affirm'
2020-01-15 17:14:15 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:15 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Predicted next action 'action_default_ask_affirmation' with confidence 1.00.
2020-01-15 17:14:15 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_default_ask_affirmation'.
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Action 'action_default_ask_affirmation' ended with events '[BotUttered('Sorry, I'm not sure I've understood you correctly 🤔 Do you mean...', {"elements": null, "quick_replies": null, "buttons": [{"title": "Yes", "payload": "/affirm{}"}, {"title": "Something else", "payload": "/out_of_scope"}], "attachment": null, "image": null, "custom": null}, {}, 1579104855.49731)]'.
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: None
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Current tracker state [{'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}]
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'prev_action_default_ask_affirmation': 1.0}]
2020-01-15 17:14:15 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:15 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:15 DEBUG    rasa.core.policies.mapping_policy  - There is no mapped action for the predicted intent, 'affirm'.
2020-01-15 17:14:15 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Predicted next action 'action_listen' with confidence 1.00.
2020-01-15 17:14:15 DEBUG    rasa.core.processor  - Action 'action_listen' ended with events '[]'.
2020-01-15 17:14:15 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
? Sorry, I'm not sure I've understood you correctly 🤔 Do you mean...  2: Something else (/out_of_scope)
2020-01-15 17:14:18 DEBUG    rasa.core.tracker_store  - Recreating tracker for id 'default'
2020-01-15 17:14:18 DEBUG    rasa.core.processor  - Received user message '/out_of_scope' with intent '{'name': 'out_of_scope', 'confidence': 1.0}' and entities '[]'
2020-01-15 17:14:18 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 15 events.
2020-01-15 17:14:18 DEBUG    rasa.core.policies.memoization  - Current tracker state [{'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:18 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:18 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:18 DEBUG    rasa.core.policies.memoization  - There is a memorised next action '163'
2020-01-15 17:14:18 DEBUG    rasa.core.policies.two_stage_fallback  - User 'default' denied suggested intents.
2020-01-15 17:14:18 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:18 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:18 DEBUG    rasa.core.processor  - Predicted next action 'action_default_fallback' with confidence 1.00.
2020-01-15 17:14:18 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_default_fallback'.
2020-01-15 17:14:18 DEBUG    rasa.core.processor  - Action 'action_default_fallback' ended with events '[BotUttered('Click the button below if you want to start over.', {"elements": null, "quick_replies": null, "buttons": [{"payload": "/restart", "title": "Restart"}], "attachment": null, "image": null, "custom": null}, {}, 1579104858.9300601), <rasa.core.events.SlotSet object at 0x15e7f3c50>, <rasa.core.events.ConversationPaused object at 0x15e69d240>]'.
2020-01-15 17:14:18 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: negative
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:18 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
? Click the button below if you want to start over.  1: Restart (/restart)
2020-01-15 17:14:19 DEBUG    rasa.core.tracker_store  - Recreating tracker for id 'default'
2020-01-15 17:14:19 DEBUG    rasa.core.processor  - Received user message '/restart' with intent '{'name': 'restart', 'confidence': 1.0}' and entities '[]'
2020-01-15 17:14:19 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 20 events.
2020-01-15 17:14:19 WARNING  rasa.core.featurizers  - Feature 'intent_restart' could not be found in feature map.
2020-01-15 17:14:19 DEBUG    rasa.core.policies.memoization  - Current tracker state [{'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'intent_restart': 1.0, 'slot_feedback_value_1': 1.0, 'prev_action_default_fallback': 1.0}]
2020-01-15 17:14:19 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:19 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'intent_restart': 1.0, 'slot_feedback_value_1': 1.0, 'prev_action_default_fallback': 1.0}]
2020-01-15 17:14:19 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:19 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:19 DEBUG    rasa.core.policies.mapping_policy  - Restarting the conversation with action_restart.
2020-01-15 17:14:19 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_TwoStageFallbackPolicy
2020-01-15 17:14:19 DEBUG    rasa.core.processor  - Predicted next action 'action_listen' with confidence 1.00.
2020-01-15 17:14:19 DEBUG    rasa.core.processor  - Action 'action_listen' ended with events '[]'.
2020-01-15 17:14:19 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
? Click the button below if you want to start over.  1: Restart (/restart)
2020-01-15 17:14:20 DEBUG    rasa.core.tracker_store  - Recreating tracker for id 'default'
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Received user message '/restart' with intent '{'name': 'restart', 'confidence': 1.0}' and entities '[]'
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 22 events.
2020-01-15 17:14:20 WARNING  rasa.core.featurizers  - Feature 'intent_restart' could not be found in feature map.
2020-01-15 17:14:20 WARNING  rasa.core.featurizers  - Feature 'intent_restart' could not be found in feature map.
2020-01-15 17:14:20 DEBUG    rasa.core.policies.memoization  - Current tracker state [{'prev_action_default_ask_rephrase': 1.0, 'intent_out_of_scope': 1.0}, {'prev_action_listen': 1.0, 'intent_affirm': 1.0}, {'prev_action_default_ask_affirmation': 1.0, 'intent_affirm': 1.0}, {'intent_out_of_scope': 1.0, 'prev_action_listen': 1.0}, {'intent_restart': 1.0, 'slot_feedback_value_1': 1.0, 'prev_action_default_fallback': 1.0}, {'intent_restart': 1.0, 'slot_feedback_value_1': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:20 DEBUG    rasa.core.policies.memoization  - Launch DeLorean...
2020-01-15 17:14:20 DEBUG    rasa.core.policies.memoization  - Current tracker state [None, None, None, None, {}, {'intent_restart': 1.0, 'prev_action_listen': 1.0}]
2020-01-15 17:14:20 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2020-01-15 17:14:20 DEBUG    rasa.core.policies.two_stage_fallback  - NLU confidence threshold met, confidence of fallback action set to core threshold (0.3).
2020-01-15 17:14:20 DEBUG    rasa.core.policies.form_policy  - There is no active form
2020-01-15 17:14:20 DEBUG    rasa.core.policies.mapping_policy  - The predicted intent 'restart' is mapped to  action 'action_restart' in the domain.
2020-01-15 17:14:20 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_4_MappingPolicy
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Predicted next action 'action_restart' with confidence 1.00.
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Action 'action_restart' ended with events '[BotUttered('Congrats you've restarted me! 😉', {"elements": null, "quick_replies": null, "buttons": null, "attachment": null, "image": null, "custom": null}, {}, 1579104860.883595), <rasa.core.events.Restarted object at 0x15e977f60>]'.
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Current slot values:
	budget: None
	business_email: None
	can_use_spacy: None
	company: None
	current_api: None
	email: None
	entity_extractor: None
	feedback_message: None
	feedback_value: None
	job_function: None
	language: None
	name: None
	nlu_part: None
	onboarding: None
	person_name: None
	problem_description: None
	product: None
	requested_slot: None
	shown_privacy: None
	step: None
	suggestion: None
	unknown_nlu_part: None
	unknown_product: None
	use_case: None
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Predicted next action 'action_listen' with confidence 1.00.
2020-01-15 17:14:20 DEBUG    rasa.core.processor  - Action 'action_listen' ended with events '[]'.
2020-01-15 17:14:20 DEBUG    rasa.core.lock_store  - Deleted lock for conversation 'default'.
Congrats you've restarted me! 😉
```
 figured out how to replicate - when running in shell I get the `/restart` option twice as mentioned, but on the docs it just presents the button once and I have to manually `/restart` to get it to actually restart. will start working on it.

Commit message: add exception handling if no UserUttered/ActionExecuted in tracker

Patch to review:
diff --git a/changelog/4601.bugfix.rst b/changelog/4601.bugfix.rst
new file mode 100644
index 000000000000..48316f97cae1
--- /dev/null
+++ b/changelog/4601.bugfix.rst
@@ -0,0 +1,2 @@
+Fixed issue with ``action_restart`` getting overridden by ``action_listen`` when the ``MappingPolicy`` and 
+two stage fallback policy are used together.
diff --git a/rasa/core/policies/two_stage_fallback.py b/rasa/core/policies/two_stage_fallback.py
index 5bbd285b5251..02ab5a602407 100644
--- a/rasa/core/policies/two_stage_fallback.py
+++ b/rasa/core/policies/two_stage_fallback.py
@@ -12,6 +12,9 @@
     ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
     ACTION_LISTEN_NAME,
 )
+
+from rasa.core.events import UserUttered, ActionExecuted
+
 from rasa.core.constants import USER_INTENT_OUT_OF_SCOPE
 from rasa.core.domain import Domain, InvalidDomain
 from rasa.core.policies.fallback import FallbackPolicy
@@ -172,11 +175,19 @@ def predict_action_probabilities(
         return result
 
     def _is_user_input_expected(self, tracker: DialogueStateTracker) -> bool:
-        return tracker.latest_action_name in [
+        action_requires_input = tracker.latest_action_name in [
             ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
             ACTION_DEFAULT_ASK_REPHRASE_NAME,
             self.fallback_action_name,
         ]
+        try:
+            last_utterance_time = tracker.get_last_event_for(UserUttered).timestamp
+            last_action_time = tracker.get_last_event_for(ActionExecuted).timestamp
+            input_given = last_action_time < last_utterance_time
+        except AttributeError:
+            input_given = False
+
+        return action_requires_input and not input_given
 
     def _has_user_denied(
         self, last_intent: Text, tracker: DialogueStateTracker
diff --git a/tests/core/test_ensemble.py b/tests/core/test_ensemble.py
index 747effaaa898..d5dcd07776e1 100644
--- a/tests/core/test_ensemble.py
+++ b/tests/core/test_ensemble.py
@@ -11,6 +11,13 @@
 from rasa.core.trackers import DialogueStateTracker
 from rasa.core.events import UserUttered
 
+from tests.core import utilities
+from rasa.core.actions.action import ACTION_DEFAULT_FALLBACK_NAME, ACTION_RESTART_NAME
+from rasa.core.constants import USER_INTENT_RESTART
+from rasa.core.events import ActionExecuted
+from rasa.core.policies.two_stage_fallback import TwoStageFallbackPolicy
+from rasa.core.policies.mapping_policy import MappingPolicy
+
 
 class WorkingPolicy(Policy):
     @classmethod
@@ -87,6 +94,34 @@ def test_policy_priority():
     assert result == priority_2_result
 
 
+def test_fallback_mapping_restart():
+    domain = Domain.load("data/test_domains/default.yml")
+    events = [
+        ActionExecuted(ACTION_DEFAULT_FALLBACK_NAME),
+        utilities.user_uttered(USER_INTENT_RESTART, 1),
+    ]
+    tracker = DialogueStateTracker.from_events("test", events, [])
+
+    two_stage_fallback_policy = TwoStageFallbackPolicy(
+        priority=2, deny_suggestion_intent_name="deny"
+    )
+    mapping_policy = MappingPolicy(priority=1)
+
+    mapping_fallback_ensemble = SimplePolicyEnsemble(
+        [two_stage_fallback_policy, mapping_policy]
+    )
+
+    result, best_policy = mapping_fallback_ensemble.probabilities_using_best_policy(
+        tracker, domain
+    )
+    max_confidence_index = result.index(max(result))
+    i = 1
+    next_action = domain.action_for_index(max_confidence_index, None)
+
+    assert best_policy == "policy_{}_{}".format(i, MappingPolicy.__name__)
+    assert next_action.name() == ACTION_RESTART_NAME
+
+
 class LoadReturnsNonePolicy(Policy):
     @classmethod
     def load(cls, path):


Code review comments:
diff_hunk: @@ -87,6 +94,34 @@ def test_policy_priority():
     assert result == priority_2_result
 
 
+def test_fallback_mapping_restart():
+    domain = Domain.load("data/test_domains/default.yml")
+    events = [
+        ActionExecuted(ACTION_DEFAULT_FALLBACK_NAME),
+        utilities.user_uttered(USER_INTENT_RESTART, 1),
+    ]
+    tracker = DialogueStateTracker.from_events("test", events, [])
+
+    two_stage_fallback_policy = TwoStageFallbackPolicy(
+        priority=2, deny_suggestion_intent_name="deny"
+    )
+    mapping_policy = MappingPolicy(priority=1)
+
+    mapping_fallback_ensemble = SimplePolicyEnsemble(
+        [two_stage_fallback_policy, mapping_policy]
+    )
+
+    result, best_policy = mapping_fallback_ensemble.probabilities_using_best_policy(
+        tracker, domain
+    )
+    max_confidence_index = result.index(max(result))
+    i = 1
+    next_action = domain.action_for_index(max_confidence_index, None)
+
+    assert best_policy == "policy_{}_{}".format(i, MappingPolicy.__name__)
path: tests/core/test_ensemble.py
text: @user1:
please use f-strings here

diff_hunk: @@ -87,6 +94,34 @@ def test_policy_priority():
     assert result == priority_2_result
 
 
+def test_fallback_mapping_restart():
+    domain = Domain.load("data/test_domains/default.yml")
+    events = [
+        ActionExecuted(ACTION_DEFAULT_FALLBACK_NAME),
+        utilities.user_uttered(USER_INTENT_RESTART, 1),
+    ]
+    tracker = DialogueStateTracker.from_events("test", events, [])
+
+    two_stage_fallback_policy = TwoStageFallbackPolicy(
+        priority=2, deny_suggestion_intent_name="deny"
+    )
+    mapping_policy = MappingPolicy(priority=1)
+
+    mapping_fallback_ensemble = SimplePolicyEnsemble(
+        [two_stage_fallback_policy, mapping_policy]
+    )
+
+    result, best_policy = mapping_fallback_ensemble.probabilities_using_best_policy(
+        tracker, domain
+    )
+    max_confidence_index = result.index(max(result))
+    i = 1
path: tests/core/test_ensemble.py
text: @user1:
```suggestion
    index_of_mapping_policy = 1
```