Skip to content
Merged
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
18 changes: 8 additions & 10 deletions pyrit/executor/attack/multi_turn/chunked_request.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import asyncio
import logging
import textwrap
from dataclasses import dataclass, field
from string import Formatter
from typing import TYPE_CHECKING, Any

from pyrit.common import REQUIRED_VALUE, apply_defaults
from pyrit.common.task_utils import gather_with_cleanup_async
from pyrit.exceptions import ComponentRole, execution_context
from pyrit.executor.attack.component import ConversationManager, PrependedConversationConfig
from pyrit.executor.attack.core.attack_config import (
Expand Down Expand Up @@ -419,16 +419,14 @@ async def _score_combined_value_async(
attack_strategy_name=self.__class__.__name__,
objective=objective,
):
results = await asyncio.gather(
*(
Scorer._score_with_context_async(
scorable=ContentScorable(value=combined_value),
scorer=scorer,
expectation=selected,
component_role=role,
)
for scorer, selected, role in calls
results = await gather_with_cleanup_async(
Scorer._score_with_context_async(
scorable=ContentScorable(value=combined_value),
scorer=scorer,
expectation=selected,
component_role=role,
)
for scorer, selected, role in calls
)
objective_scores = results[0] if self._objective_scorer is not None else []
return objective_scores[0] if objective_scores else None
Expand Down
49 changes: 47 additions & 2 deletions tests/unit/executor/attack/multi_turn/test_chunked_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
Tests for ChunkedRequestAttack.
"""

import asyncio
import uuid
from unittest.mock import AsyncMock, MagicMock

import pytest
from unit.mocks import get_mock_prompt_normalizer
from unit.mocks import get_mock_prompt_normalizer, get_mock_scorer_identifier

from pyrit.executor.attack import (
AttackParameters,
AttackScoringConfig,
ChunkedRequestAttack,
ChunkedRequestAttackContext,
PrependedConversationConfig,
Expand All @@ -21,13 +23,14 @@
PrependedHistorySendContext,
)
from pyrit.message_normalizer import HistorySquashNormalizer, MessageStringNormalizer
from pyrit.models import ComponentIdentifier, Message, MessagePiece
from pyrit.models import ComponentIdentifier, Message, MessagePiece, Score
from pyrit.prompt_target import (
CapabilityName,
PromptTarget,
TargetCapabilities,
TargetConfiguration,
)
from pyrit.score import Scorer, TrueFalseScorer


def _mock_target_id(name: str = "MockTarget") -> ComponentIdentifier:
Expand Down Expand Up @@ -325,3 +328,45 @@ async def test_perform_async_sets_atomic_attack_identifier(self):
assert result.atomic_attack_identifier is not None
assert result.atomic_attack_identifier.class_name == "AtomicAttack"
assert result.get_attack_strategy_identifier() == attack.get_identifier()

async def test_perform_async_scorer_failure_cancels_and_drains_sibling_scorers(self):
mock_normalizer = get_mock_prompt_normalizer()
mock_normalizer.send_prompt_async = AsyncMock(
return_value=Message.from_prompt(prompt="chunk response", role="assistant")
)
objective_scorer = MagicMock(spec=TrueFalseScorer)
objective_scorer.get_identifier.return_value = get_mock_scorer_identifier()
auxiliary_scorer = MagicMock(spec=Scorer)
auxiliary_scorer.get_identifier.return_value = get_mock_scorer_identifier()
objective_started = asyncio.Event()
objective_task: asyncio.Task[list[Score]] | None = None

async def slow_objective_score_async(**kwargs) -> list[Score]:
nonlocal objective_task
objective_task = asyncio.current_task()
objective_started.set()
await asyncio.Event().wait()
return []

async def failing_auxiliary_score_async(**kwargs) -> list[Score]:
await objective_started.wait()
raise RuntimeError("auxiliary scorer failed")

objective_scorer.score_async = slow_objective_score_async
auxiliary_scorer.score_async = failing_auxiliary_score_async
attack = ChunkedRequestAttack(
objective_target=_make_mock_target(),
prompt_normalizer=mock_normalizer,
attack_scoring_config=AttackScoringConfig(
objective_scorer=objective_scorer, auxiliary_scorers=[auxiliary_scorer]
),
chunk_size=100,
total_length=100,
)
context = ChunkedRequestAttackContext(params=AttackParameters(objective="Extract the secret"))

with pytest.raises(RuntimeError, match="auxiliary scorer failed"):
await attack._perform_async(context=context)

assert objective_task is not None
assert objective_task.cancelled()
Loading