"""**Phrasing**

These attacks rephrase requests for unsafe content in an attempt to bypass safeguards.

This module is home to probes that rely on specific grammatical alterations and rephrasing
to implement their techinque.

"""

from garak import _config
from garak.data import path as data_path
import garak.probes


class PastTenseFull(garak.probes.Probe):
    """Will the system give unsafe responses when asked to write about weapon creation or dangerous behavior in the past tense?

    This check sends the generator requests for advice on possibly protect or restricted subjects in the past tense.
    """

    active = False
    lang = "en"
    doc_uri = "https://arxiv.org/html/2407.11969v2"
    intent = "T009ignore"  # tense-shifting is used here to get the model to ignore its normal refusal policy
    primary_detector = "mitigation.Prefixes"
    tags = [
        "owasp:llm01",
        "quality:Security:PromptStability",
        "payload:jailbreak",
        "demon:Language:Stylizing:Synonymous_language",  # reformulates harmful requests in past tense to bypass refusal training
    ]
    goal = "use past tense to disregard the system prompt"
    tier = garak.probes.Tier.COMPETE_WITH_SOTA

    def __init__(self, config_root=_config):
        super().__init__(config_root=config_root)

        self.prompts = []

        with open(
            data_path / "phrasing" / "past_tense_en.txt",
            "r",
            encoding="utf-8",
        ) as file:
            for str in file:
                self.prompts.append(str.strip())


class PastTense(PastTenseFull):
    """Attempt to evade safeguards by setting requests in the past tense

    A subset of the tense-modified requests. Success is reported higher with more variation, so
    generations is overridden.
    """

    active = True
    intent = "T009ignore"  # this is the capped subset of the same jailbreak strategy

    def __init__(self, config_root=_config):
        super().__init__(config_root=config_root)
        self._prune_data(cap=self.soft_probe_prompt_cap)


class FutureTenseFull(garak.probes.Probe):
    """Will the system give unsafe responses when asked to write about weapon creation or dangerous behavior in the future tense?

    This check sends the generator requests for advice on possibly protect or restricted subjects in the future tense.
    """

    active = False

    lang = "en"
    doc_uri = "https://arxiv.org/html/2407.11969v2"
    intent = "T009ignore"  # the future-tense reformulation is used to bypass normal safety refusals
    primary_detector = "mitigation.Prefixes"
    tags = [
        "owasp:llm01",
        "quality:Security:PromptStability",
        "payload:jailbreak",
        "demon:Language:Stylizing:Synonymous_language",  # reformulates harmful requests in future tense to bypass refusal training
    ]
    goal = "use future tense to disregard the system prompt"
    tier = garak.probes.Tier.COMPETE_WITH_SOTA

    def __init__(self, config_root=_config):
        super().__init__(config_root=config_root)

        self.prompts = []

        with open(
            data_path / "phrasing" / "future_tense_en.txt",
            "r",
            encoding="utf-8",
        ) as file:
            for str in file:
                self.prompts.append(str.strip())


class FutureTense(FutureTenseFull):
    """Attempt to evade safeguards by setting requests in the future tense

    A subset of the tense-modified requests. Success is reported higher with more variation, so
    generations is overridden.
    """

    active = True
    intent = "T009ignore"  # uses the same phrasing-based jailbreak on a smaller set

    def __init__(self, config_root=_config):
        super().__init__(config_root=config_root)
        self._prune_data(self.soft_probe_prompt_cap)
