|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from datetime import datetime, timezone |
| 9 | +from typing import Any |
9 | 10 | from unittest.mock import AsyncMock, MagicMock, patch |
10 | 11 |
|
11 | 12 | import pytest |
|
20 | 21 | ScenarioRunService, |
21 | 22 | ) |
22 | 23 | from pyrit.models import AttackOutcome |
| 24 | +from pyrit.scenario.core import DatasetConfiguration |
23 | 25 |
|
24 | 26 | _REGISTRY_PATCH_BASE = "pyrit.registry" |
25 | 27 | _MEMORY_PATCH = "pyrit.memory.CentralMemory.get_memory_instance" |
@@ -279,6 +281,139 @@ async def test_start_run_max_dataset_size_uses_default_config(self, mock_all_reg |
279 | 281 | init_call = scenario_instance.initialize_async.await_args |
280 | 282 | assert init_call.kwargs["dataset_config"] is default_config |
281 | 283 |
|
| 284 | + async def test_start_run_dataset_names_preserves_subclass_config_type(self, mock_all_registries) -> None: |
| 285 | + """``dataset_names`` rebuilds the config using the scenario's own DatasetConfiguration subclass. |
| 286 | +
|
| 287 | + Regression: passing ``dataset_names`` via the backend used to construct |
| 288 | + a plain ``DatasetConfiguration``, silently losing subclass behavior |
| 289 | + (e.g. ``EncodingDatasetConfiguration``'s objective shaping). |
| 290 | + """ |
| 291 | + |
| 292 | + # Create a marker subclass so we can verify type preservation without |
| 293 | + # depending on any concrete scenario implementation. |
| 294 | + class _MarkerDatasetConfiguration(DatasetConfiguration): |
| 295 | + pass |
| 296 | + |
| 297 | + default_config = _MarkerDatasetConfiguration(dataset_names=["original"], max_dataset_size=100) |
| 298 | + scenario_instance = mock_all_registries["scenario_instance"] |
| 299 | + scenario_instance._default_dataset_config = default_config |
| 300 | + |
| 301 | + service = ScenarioRunService() |
| 302 | + await service.start_run_async(request=_make_request(dataset_names=["custom_a", "custom_b"], max_dataset_size=3)) |
| 303 | + |
| 304 | + init_call = scenario_instance.initialize_async.await_args |
| 305 | + built_config = init_call.kwargs["dataset_config"] |
| 306 | + |
| 307 | + # Type is preserved (this is the regression assertion) |
| 308 | + assert type(built_config) is _MarkerDatasetConfiguration |
| 309 | + # And carries the caller-supplied values, not the scenario defaults |
| 310 | + assert built_config.get_default_dataset_names() == ["custom_a", "custom_b"] |
| 311 | + assert built_config.max_dataset_size == 3 |
| 312 | + # The original default config is not mutated when a fresh dataset_names is supplied |
| 313 | + assert default_config.get_default_dataset_names() == ["original"] |
| 314 | + assert default_config.max_dataset_size == 100 |
| 315 | + |
| 316 | + async def test_start_run_dataset_names_without_max_dataset_size_preserves_subclass( |
| 317 | + self, mock_all_registries |
| 318 | + ) -> None: |
| 319 | + """``dataset_names`` alone (no ``max_dataset_size``) still preserves the subclass type.""" |
| 320 | + |
| 321 | + class _MarkerDatasetConfiguration(DatasetConfiguration): |
| 322 | + pass |
| 323 | + |
| 324 | + scenario_instance = mock_all_registries["scenario_instance"] |
| 325 | + scenario_instance._default_dataset_config = _MarkerDatasetConfiguration(dataset_names=["original"]) |
| 326 | + |
| 327 | + service = ScenarioRunService() |
| 328 | + await service.start_run_async(request=_make_request(dataset_names=["only_this"])) |
| 329 | + |
| 330 | + init_call = scenario_instance.initialize_async.await_args |
| 331 | + built_config = init_call.kwargs["dataset_config"] |
| 332 | + assert type(built_config) is _MarkerDatasetConfiguration |
| 333 | + assert built_config.get_default_dataset_names() == ["only_this"] |
| 334 | + assert built_config.max_dataset_size is None |
| 335 | + |
| 336 | + async def test_start_run_dataset_names_falls_back_when_subclass_constructor_incompatible( |
| 337 | + self, mock_all_registries, caplog |
| 338 | + ) -> None: |
| 339 | + """If the subclass __init__ rejects standard kwargs, fall back to plain ``DatasetConfiguration``.""" |
| 340 | + |
| 341 | + class _RequiresExtraArgConfiguration(DatasetConfiguration): |
| 342 | + def __init__(self, *, required_extra: str, **kwargs: Any) -> None: |
| 343 | + super().__init__(**kwargs) |
| 344 | + self._required_extra = required_extra |
| 345 | + |
| 346 | + scenario_instance = mock_all_registries["scenario_instance"] |
| 347 | + # Build the default with the required kwarg so introspection succeeds. |
| 348 | + scenario_instance._default_dataset_config = _RequiresExtraArgConfiguration( |
| 349 | + required_extra="seeded", dataset_names=["original"] |
| 350 | + ) |
| 351 | + |
| 352 | + service = ScenarioRunService() |
| 353 | + with caplog.at_level("WARNING", logger=_svc_mod.logger.name): |
| 354 | + await service.start_run_async(request=_make_request(dataset_names=["custom"])) |
| 355 | + |
| 356 | + init_call = scenario_instance.initialize_async.await_args |
| 357 | + built_config = init_call.kwargs["dataset_config"] |
| 358 | + |
| 359 | + # Fallback is the generic base class, not the subclass |
| 360 | + assert type(built_config) is DatasetConfiguration |
| 361 | + assert built_config.get_default_dataset_names() == ["custom"] |
| 362 | + # Warning was logged so the operator can see the silent degradation |
| 363 | + assert any( |
| 364 | + "_RequiresExtraArgConfiguration" in record.message |
| 365 | + and "Falling back to a generic DatasetConfiguration" in record.message |
| 366 | + for record in caplog.records |
| 367 | + ) |
| 368 | + |
| 369 | + async def test_start_run_dataset_names_introspection_failure_raises(self, mock_memory) -> None: |
| 370 | + """Passing ``dataset_names`` against a non-no-arg-instantiable scenario fails fast.""" |
| 371 | + # Mirrors test_start_run_scenario_not_no_arg_instantiable_raises but for the dataset_names path. |
| 372 | + mock_scenario_class = MagicMock( |
| 373 | + side_effect=[ |
| 374 | + TypeError("missing 1 required positional argument: 'objective_target'"), |
| 375 | + ] |
| 376 | + ) |
| 377 | + mock_sr = MagicMock() |
| 378 | + mock_sr.get_class.return_value = mock_scenario_class |
| 379 | + |
| 380 | + mock_tr = MagicMock() |
| 381 | + mock_tr.get_instance_by_name.return_value = MagicMock() |
| 382 | + mock_tr.get_names.return_value = ["my_target"] |
| 383 | + |
| 384 | + mock_ir = MagicMock() |
| 385 | + |
| 386 | + service = ScenarioRunService() |
| 387 | + |
| 388 | + with ( |
| 389 | + patch(f"{_REGISTRY_PATCH_BASE}.ScenarioRegistry.get_registry_singleton", return_value=mock_sr), |
| 390 | + patch(f"{_REGISTRY_PATCH_BASE}.TargetRegistry.get_registry_singleton", return_value=mock_tr), |
| 391 | + patch(f"{_REGISTRY_PATCH_BASE}.InitializerRegistry.get_registry_singleton", return_value=mock_ir), |
| 392 | + ): |
| 393 | + with pytest.raises(ValueError, match="not instantiable without arguments"): |
| 394 | + await service.start_run_async(request=_make_request(dataset_names=["custom"])) |
| 395 | + |
| 396 | + async def test_start_run_max_dataset_size_with_dataset_names_uses_subclass_with_both( |
| 397 | + self, mock_all_registries |
| 398 | + ) -> None: |
| 399 | + """When both ``dataset_names`` and ``max_dataset_size`` are supplied, both flow into the subclass instance.""" |
| 400 | + |
| 401 | + class _MarkerDatasetConfiguration(DatasetConfiguration): |
| 402 | + pass |
| 403 | + |
| 404 | + scenario_instance = mock_all_registries["scenario_instance"] |
| 405 | + scenario_instance._default_dataset_config = _MarkerDatasetConfiguration( |
| 406 | + dataset_names=["original"], max_dataset_size=99 |
| 407 | + ) |
| 408 | + |
| 409 | + service = ScenarioRunService() |
| 410 | + await service.start_run_async(request=_make_request(dataset_names=["a", "b"], max_dataset_size=7)) |
| 411 | + |
| 412 | + built_config = scenario_instance.initialize_async.await_args.kwargs["dataset_config"] |
| 413 | + assert type(built_config) is _MarkerDatasetConfiguration |
| 414 | + assert built_config.get_default_dataset_names() == ["a", "b"] |
| 415 | + assert built_config.max_dataset_size == 7 |
| 416 | + |
282 | 417 | async def test_start_run_exceeds_concurrent_limit(self, mock_all_registries) -> None: |
283 | 418 | """Test that exceeding concurrent run limit raises ValueError.""" |
284 | 419 | service = ScenarioRunService() |
|
0 commit comments