Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions pyrit/executor/attack/core/attack_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,11 @@ async def _on_error_async(
collector = get_retry_collector()
retry_events = collector.events if collector else []

# Build a conversation_id — use context's if available, otherwise generate one
conversation_id = getattr(context, "conversation_id", None) or str(uuid.uuid4())
# Multi-turn contexts keep the active ID on their conversation session.
conversation_id = getattr(context, "conversation_id", None)
if not conversation_id:
conversation_id = getattr(getattr(context, "session", None), "conversation_id", None)
conversation_id = conversation_id or str(uuid.uuid4())

error_result = AttackResult(
conversation_id=conversation_id,
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/executor/attack/core/test_attack_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AttackStrategy,
_DefaultAttackStrategyEventHandler,
)
from pyrit.executor.attack.multi_turn.multi_turn_attack_strategy import ConversationSession, MultiTurnAttackContext
from pyrit.executor.core import StrategyEvent, StrategyEventData
from pyrit.memory.central_memory import CentralMemory
from pyrit.models import (
Expand Down Expand Up @@ -631,6 +632,27 @@ async def test_on_error_persists_result_to_memory(self, sample_attack_context, m
assert stored_result.error_type == "ValueError"
assert stored_result.execution_time_ms == 500

async def test_on_error_uses_multi_turn_session_conversation_id(self, mock_memory):
"""Test that multi-turn failures remain correlated with their active conversation."""
context = MultiTurnAttackContext(
params=AttackParameters(objective="Test harmful objective"),
session=ConversationSession(conversation_id="active-conversation-id"),
)

with patch("pyrit.memory.central_memory.CentralMemory.get_memory_instance", return_value=mock_memory):
handler = _DefaultAttackStrategyEventHandler()
event_data = StrategyEventData(
event=StrategyEvent.ON_ERROR,
strategy_name="TestStrategy",
strategy_id="test-id",
context=context,
error=TimeoutError("target timed out"),
)
await handler.on_event_async(event_data)

stored_result = mock_memory.add_attack_results_to_memory.call_args.kwargs["attack_results"][0]
assert stored_result.conversation_id == "active-conversation-id"

async def test_on_error_skips_when_no_error_or_context(self, mock_memory):
"""Test that error handler returns early when error or context is None"""
with patch("pyrit.memory.central_memory.CentralMemory.get_memory_instance", return_value=mock_memory):
Expand Down