Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f1186d9
remove promptchattarget ref in converters & add native target require…
hannahwestra25 Apr 21, 2026
229d3b9
whitespace
hannahwestra25 Apr 21, 2026
d9e0784
Merge branch 'main' of https://github.com/microsoft/PyRIT into hawest…
hannahwestra25 Apr 22, 2026
bf7fcad
remove required native capabilities and simplify targetrequirements
hannahwestra25 Apr 22, 2026
c00285c
move validation func
hannahwestra25 Apr 22, 2026
3aec335
Merge branch 'main' of https://github.com/microsoft/PyRIT into hawest…
hannahwestra25 Apr 22, 2026
8a43590
remove remaining promptchattarget and unused requirements
hannahwestra25 Apr 22, 2026
b1edf25
remove lingering chat target ref
hannahwestra25 Apr 22, 2026
1915c82
fix docstrings
hannahwestra25 Apr 23, 2026
1a51bb0
Merge branch 'main' of https://github.com/microsoft/PyRIT into hawest…
hannahwestra25 Apr 23, 2026
d2cb04b
correct chat definition
hannahwestra25 Apr 23, 2026
c354d6c
Merge branch 'main' of https://github.com/microsoft/PyRIT into hawest…
hannahwestra25 Apr 23, 2026
32e75d0
centralize validation and fix docstrings
hannahwestra25 Apr 23, 2026
e0978fe
Merge branch 'main' of https://github.com/microsoft/PyRIT into hawest…
hannahwestra25 Apr 23, 2026
922cb2d
fix tests
hannahwestra25 Apr 23, 2026
82fb70a
centralize converter validation and fix crescendo check
hannahwestra25 Apr 23, 2026
2afdcd3
add scenario validation
hannahwestra25 Apr 24, 2026
386357f
add known capabilities
hannahwestra25 Apr 24, 2026
890e11b
create adversarial target for TAP
hannahwestra25 Apr 24, 2026
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
26 changes: 17 additions & 9 deletions pyrit/executor/attack/component/conversation_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from pyrit.prompt_normalizer.prompt_normalizer import PromptNormalizer
from pyrit.prompt_target import PromptTarget
from pyrit.prompt_target.common.prompt_chat_target import PromptChatTarget
from pyrit.prompt_target.common.target_capabilities import CapabilityName

if TYPE_CHECKING:
from pyrit.executor.attack.core import AttackContext
Expand Down Expand Up @@ -242,7 +242,7 @@ def get_last_message(
def set_system_prompt(
self,
*,
target: PromptChatTarget,
target: PromptTarget,
conversation_id: str,
system_prompt: str,
labels: Optional[dict[str, str]] = None,
Expand All @@ -251,11 +251,16 @@ def set_system_prompt(
Set or update the system prompt for a conversation.

Args:
target: The chat target to set the system prompt on.
target: The target to set the system prompt on. Must handle the
SYSTEM_PROMPT capability (natively or via an ADAPT policy).
conversation_id: Unique identifier for the conversation.
system_prompt: The system prompt text.
labels: Optional labels to associate with the system prompt.

Raises:
ValueError: If target cannot handle the SYSTEM_PROMPT capability.
"""
target.configuration.ensure_can_handle(capability=CapabilityName.SYSTEM_PROMPT)
target.set_system_prompt(
system_prompt=system_prompt,
conversation_id=conversation_id,
Expand Down Expand Up @@ -283,7 +288,7 @@ async def initialize_context_async(
3. Updates context.executed_turns for multi-turn attacks
4. Sets context.next_message if there's an unanswered user message

For PromptChatTarget:
For chat-capable PromptTarget:
- Adds prepended messages to memory with simulated_assistant role
- All messages get new UUIDs

Expand All @@ -306,7 +311,7 @@ async def initialize_context_async(

Raises:
ValueError: If conversation_id is empty, or if prepended_conversation
requires a PromptChatTarget but target is not one.
requires a chat-capable PromptTarget but target is not one.
"""
if not conversation_id:
raise ValueError("conversation_id cannot be empty")
Expand All @@ -321,8 +326,11 @@ async def initialize_context_async(
logger.debug(f"No prepended conversation for context initialization: {conversation_id}")
return state

# Handle target type compatibility
is_chat_target = isinstance(target, PromptChatTarget)
# Targets that don't natively support editable history cannot consume a
# prepended multi-message conversation as-is — route them to the
# single-string fallback path. Type identity (PromptChatTarget) is a
# legacy signal for this; capability-based routing is the durable form.
is_chat_target = target.configuration.includes(capability=CapabilityName.EDITABLE_HISTORY)
if not is_chat_target:
return await self._handle_non_chat_target_async(
context=context,
Expand Down Expand Up @@ -366,8 +374,8 @@ async def _handle_non_chat_target_async(

if config.non_chat_target_behavior == "raise":
raise ValueError(
"prepended_conversation requires the objective target to be a PromptChatTarget. "
"Non-chat objective targets do not support conversation history. "
"prepended_conversation requires the objective target to be a chat-capable "
"PromptTarget. Non-chat objective targets do not support conversation history. "
"Use PrependedConversationConfig with non_chat_target_behavior='normalize_first_turn' "
"to normalize the conversation into the first message instead."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PrependedConversationConfig:
This class provides control over:
- Which message roles should have request converters applied
- How to normalize conversation history for non-chat objective targets
- What to do when the objective target is not a PromptChatTarget
- What to do when the objective target is not a chat-capable PromptTarget
"""

# Roles for which request converters should be applied to prepended messages.
Expand All @@ -36,13 +36,13 @@ class PrependedConversationConfig:
# ConversationContextNormalizer is used that produces "Turn N: User/Assistant" format.
message_normalizer: Optional[MessageStringNormalizer] = None

# Behavior when the target is a PromptTarget but not a PromptChatTarget:
# Behavior when the target is a PromptTarget but not a chat-capable PromptTarget:
# - "normalize_first_turn": Normalize the prepended conversation into a string and
# store it in ConversationState.normalized_prepended_context. This context will be
# prepended to the first message sent to the target. Uses objective_target_context_normalizer
# if provided, otherwise falls back to ConversationContextNormalizer.
# - "raise": Raise a ValueError. Use this when prepended conversation history must be
# maintained by the target (i.e., target must be a PromptChatTarget).
# maintained by the target (i.e., target must be a chat-capable PromptTarget).
non_chat_target_behavior: Literal["normalize_first_turn", "raise"] = "normalize_first_turn"

def get_message_normalizer(self) -> MessageStringNormalizer:
Expand Down
8 changes: 7 additions & 1 deletion pyrit/executor/attack/core/attack_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time
from abc import ABC
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar, Union, overload
from typing import TYPE_CHECKING, Any, ClassVar, Generic, Optional, TypeVar, Union, overload

from pyrit.common.logger import logger
from pyrit.executor.attack.core.attack_parameters import AttackParameters, AttackParamsT
Expand All @@ -27,6 +27,7 @@
ConversationReference,
Message,
)
from pyrit.prompt_target.common.target_requirements import TargetRequirements

if TYPE_CHECKING:
from pyrit.executor.attack.core.attack_config import AttackScoringConfig
Expand Down Expand Up @@ -233,6 +234,10 @@ class AttackStrategy(Strategy[AttackStrategyContextT, AttackStrategyResultT], Id
Defines the interface for executing attacks and handling results.
"""

#: Capability requirements placed on ``objective_target``. Subclasses
#: override to declare what the attack needs. Validated in ``__init__``.
TARGET_REQUIREMENTS: ClassVar[TargetRequirements] = TargetRequirements()

def __init__(
self,
*,
Expand All @@ -259,6 +264,7 @@ def __init__(
),
logger=logger,
)
type(self).TARGET_REQUIREMENTS.validate(target=objective_target)
self._objective_target = objective_target
self._params_type = params_type
# Guard so subclasses that set converters before calling super() aren't clobbered
Expand Down
20 changes: 11 additions & 9 deletions pyrit/executor/attack/multi_turn/chunked_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
)
from pyrit.prompt_normalizer import PromptNormalizer
from pyrit.prompt_target import PromptTarget
from pyrit.prompt_target.common.target_capabilities import CapabilityName
from pyrit.prompt_target.common.target_requirements import TargetRequirements

if TYPE_CHECKING:
from pyrit.score import TrueFalseScorer
Expand Down Expand Up @@ -82,6 +84,15 @@ class ChunkedRequestAttack(MultiTurnAttackStrategy[ChunkedRequestAttackContext,
"""
).strip()

# Chunked request issues multiple distinct turns that depend on the target
# remembering prior responses. History-squash adaptation would collapse
# them into a single prompt and silently break the attack's semantics.
# Declare MULTI_TURN as ``native_required`` so adaptation is rejected at
# construction time.
TARGET_REQUIREMENTS = TargetRequirements(
native_required=frozenset({CapabilityName.MULTI_TURN}),
)

@apply_defaults
def __init__(
self,
Expand Down Expand Up @@ -226,16 +237,7 @@ async def _setup_async(self, *, context: ChunkedRequestAttackContext) -> None:

Args:
context (ChunkedRequestAttackContext): The attack context containing attack parameters.

Raises:
ValueError: If the objective target does not support multi-turn conversations.
"""
if not self._objective_target.capabilities.supports_multi_turn:
raise ValueError(
"ChunkedRequestAttack requires a multi-turn target. "
"The objective target does not support multi-turn conversations."
)

# Ensure the context has a session
context.session = ConversationSession()

Expand Down
31 changes: 17 additions & 14 deletions pyrit/executor/attack/multi_turn/crescendo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
SeedPrompt,
)
from pyrit.prompt_normalizer import PromptNormalizer
from pyrit.prompt_target import PromptChatTarget
from pyrit.prompt_target import PromptTarget
from pyrit.prompt_target.common.target_capabilities import CapabilityName
from pyrit.prompt_target.common.target_requirements import TargetRequirements
from pyrit.score import (
FloatScaleThresholdScorer,
Scorer,
Expand Down Expand Up @@ -112,6 +114,16 @@ class CrescendoAttack(MultiTurnAttackStrategy[CrescendoAttackContext, CrescendoA
You can learn more about the Crescendo attack [@russinovich2024crescendo].
"""

# Crescendo fundamentally relies on multi-turn conversation history to
# gradually escalate prompts; history-squash adaptation would collapse the
# conversation into a single prompt and silently break the attack's
# semantics. Declare MULTI_TURN as native_required so adaptation is
# rejected at construction time.
TARGET_REQUIREMENTS = TargetRequirements(
Comment thread
hannahwestra25 marked this conversation as resolved.
required=frozenset({CapabilityName.EDITABLE_HISTORY, CapabilityName.MULTI_TURN}),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Editable" is a bit misleading. In truth, it means "branchable". We only append to history, and don't edit it.

native_required=frozenset({CapabilityName.MULTI_TURN}),
)

# Default system prompt template path for Crescendo attack
DEFAULT_ADVERSARIAL_CHAT_SYSTEM_PROMPT_TEMPLATE_PATH: Path = (
Path(EXECUTOR_SEED_PROMPT_PATH) / "crescendo" / "crescendo_variant_1.yaml"
Expand All @@ -121,7 +133,7 @@ class CrescendoAttack(MultiTurnAttackStrategy[CrescendoAttackContext, CrescendoA
def __init__(
self,
*,
objective_target: PromptChatTarget = REQUIRED_VALUE, # type: ignore[assignment]
objective_target: PromptTarget = REQUIRED_VALUE, # type: ignore[assignment]
attack_adversarial_config: AttackAdversarialConfig,
attack_converter_config: Optional[AttackConverterConfig] = None,
attack_scoring_config: Optional[AttackScoringConfig] = None,
Expand All @@ -134,7 +146,8 @@ def __init__(
Initialize the Crescendo attack strategy.

Args:
objective_target (PromptChatTarget): The target system to attack. Must be a PromptChatTarget.
objective_target (PromptTarget): The target system to attack. Must
support editable conversation history.
attack_adversarial_config (AttackAdversarialConfig): Configuration for the adversarial component,
including the adversarial chat target and optional system prompt path.
attack_converter_config (Optional[AttackConverterConfig]): Configuration for attack converters,
Expand All @@ -148,7 +161,7 @@ def __init__(
application by role, message normalization, and non-chat target behavior.

Raises:
ValueError: If objective_target is not a PromptChatTarget.
ValueError: If objective_target does not natively support editable history.
"""
# Initialize base class
super().__init__(objective_target=objective_target, logger=logger, context_type=CrescendoAttackContext)
Expand Down Expand Up @@ -257,17 +270,7 @@ async def _setup_async(self, *, context: CrescendoAttackContext) -> None:

Args:
context (CrescendoAttackContext): Attack context with configuration

Raises:
ValueError: If the objective target does not support multi-turn conversations.
"""
if not self._objective_target.capabilities.supports_multi_turn:
raise ValueError(
"CrescendoAttack requires a multi-turn target. Crescendo fundamentally relies on "
"multi-turn conversation history to gradually escalate prompts. "
"Use RedTeamingAttack or TreeOfAttacksWithPruning instead."
)

# Ensure the context has a session
context.session = ConversationSession()

Expand Down
20 changes: 11 additions & 9 deletions pyrit/executor/attack/multi_turn/multi_prompt_sending.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
)
from pyrit.prompt_normalizer import PromptNormalizer
from pyrit.prompt_target import PromptTarget
from pyrit.prompt_target.common.target_capabilities import CapabilityName
from pyrit.prompt_target.common.target_requirements import TargetRequirements
from pyrit.score import Scorer

if TYPE_CHECKING:
Expand Down Expand Up @@ -123,6 +125,15 @@ class MultiPromptSendingAttack(MultiTurnAttackStrategy[MultiTurnAttackContext[An
and multiple scorer types for comprehensive evaluation.
"""

# Sending a sequence of distinct prompts depends on the target maintaining
# conversation state between them. History-squash adaptation would collapse
# them into one message and silently break the attack's sequencing
# semantics. Declare MULTI_TURN as ``native_required`` so adaptation is
# rejected at construction time.
TARGET_REQUIREMENTS = TargetRequirements(
native_required=frozenset({CapabilityName.MULTI_TURN}),
)

@apply_defaults
def __init__(
self,
Expand Down Expand Up @@ -204,16 +215,7 @@ async def _setup_async(self, *, context: MultiTurnAttackContext[Any]) -> None:

Args:
context (MultiTurnAttackContext): The attack context containing attack parameters.

Raises:
ValueError: If the objective target does not support multi-turn conversations.
"""
if not self._objective_target.capabilities.supports_multi_turn:
raise ValueError(
"MultiPromptSendingAttack requires a multi-turn target. "
"The objective target does not support multi-turn conversations."
)

# Ensure the context has a session (like red_teaming.py does)
context.session = ConversationSession()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from pyrit.memory import CentralMemory
from pyrit.models import ConversationReference, ConversationType
from pyrit.prompt_target.common.target_capabilities import CapabilityName

if TYPE_CHECKING:
from pyrit.models import (
Expand Down Expand Up @@ -117,7 +118,7 @@ def _rotate_conversation_for_single_turn_target(
Args:
context: The current attack context.
"""
if self._objective_target.capabilities.supports_multi_turn:
if self._objective_target.configuration.includes(capability=CapabilityName.MULTI_TURN):
return

if context.executed_turns == 0:
Expand Down
Loading
Loading