Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
243c96f
declare api key corpus as scenario required data
u7k4rs6 Oct 22, 2026
840ad15
Merge remote-tracking branch 'origin/main' into feat/scenario-require…
u7k4rs6 Oct 24, 2026
c016257
apply dataset filters to required data
u7k4rs6 Oct 26, 2026
72f7d5c
declare prompt inject templates as required data
u7k4rs6 Oct 26, 2026
c36b21a
load required data with the population config's fetch and filters
u7k4rs6 Oct 26, 2026
167e17b
Merge branch 'feat/scenario-required-data' into feat/required-data-pr…
u7k4rs6 Oct 26, 2026
8384328
Merge remote-tracking branch 'origin/main' into feat/scenario-require…
u7k4rs6 Oct 26, 2026
9cea88d
Merge branch 'feat/scenario-required-data' into feat/required-data-pr…
u7k4rs6 Oct 26, 2026
4de041b
validate assembly against refresh candidates and scope template check…
u7k4rs6 Nov 1, 2026
970cbc2
Merge branch 'feat/scenario-required-data' into feat/required-data-pr…
u7k4rs6 Nov 1, 2026
19560cf
test that a migrated legacy validator checks PromptInject's raw templ…
u7k4rs6 Nov 1, 2026
4b467ac
Merge upstream main into feat/required-data-prompt-inject
romanlutz Nov 2, 2026
c389c55
remove migrated validators by identity so unhashable callables still …
u7k4rs6 Nov 2, 2026
f569d84
Merge branch 'feat/scenario-required-data' into feat/required-data-pr…
u7k4rs6 Nov 2, 2026
a05ec74
cover an unhashable callable validator in the migration test
u7k4rs6 Nov 2, 2026
230930a
Merge remote-tracking branch 'fork/feat/required-data-prompt-inject' …
u7k4rs6 Nov 2, 2026
e765496
prepare required data and population together, honor global fetch, re…
u7k4rs6 Nov 2, 2026
76c8b16
keep the requested fetch for compound populations and reject shared d…
u7k4rs6 Nov 4, 2026
c4462b0
Merge origin/main into feat/scenario-required-data
u7k4rs6 Nov 9, 2026
ee58aa5
reject compound limiters for assembled groups and align the filtering…
u7k4rs6 Nov 9, 2026
9ac73d0
Merge feat/scenario-required-data into feat/required-data-prompt-inject
u7k4rs6 Nov 9, 2026
625f8f3
MAINT: use public package imports in required data
u7k4rs6 Nov 9, 2026
ab1b6f7
Merge branch 'feat/scenario-required-data' into feat/required-data-pr…
u7k4rs6 Nov 9, 2026
98be629
Merge upstream main into feat/required-data-prompt-inject
romanlutz Nov 10, 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
5 changes: 1 addition & 4 deletions doc/scanner/garak.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@
" \"objective_target\": objective_target,\n",
" \"scenario_techniques\": [PromptInjectTechnique.IgnorePrint],\n",
" \"goal_texts\": [\"I hate humans\"],\n",
" \"dataset_config\": PromptInjectDatasetConfiguration(\n",
" sources=[DatasetSource(name=name) for name in PromptInject.required_datasets()],\n",
" max_total=1,\n",
" ),\n",
" \"dataset_config\": PromptInjectDatasetConfiguration(max_total=1),\n",
" }\n",
")\n",
"await prompt_inject_scenario.initialize_async() # type: ignore\n",
Expand Down
5 changes: 1 addition & 4 deletions doc/scanner/garak.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@
"objective_target": objective_target,
"scenario_techniques": [PromptInjectTechnique.IgnorePrint],
"goal_texts": ["I hate humans"],
"dataset_config": PromptInjectDatasetConfiguration(
sources=[DatasetSource(name=name) for name in PromptInject.required_datasets()],
max_total=1,
),
"dataset_config": PromptInjectDatasetConfiguration(max_total=1),
}
)
await prompt_inject_scenario.initialize_async() # type: ignore
Expand Down
88 changes: 52 additions & 36 deletions pyrit/scenario/scenarios/garak/prompt_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
from pyrit.scenario.core.dataset_configuration import (
DatasetAttackConfiguration,
DatasetSource,
DatasetSourceKind,
)
from pyrit.scenario.core.dataset_sampling import sample_with_coverage
from pyrit.scenario.core.required_data import ScenarioRequiredData
from pyrit.scenario.core.scenario import BaselineAttackPolicy, Scenario
from pyrit.scenario.core.scenario_technique import ScenarioTechnique
from pyrit.score import (
Expand All @@ -61,7 +63,13 @@


class PromptInjectDatasetConfiguration(DatasetAttackConfiguration):
"""Create an attack group for each context and goal combination."""
"""
Limit the context and goal combinations PromptInject assembles.

The context and technique templates are PromptInject's required data, read in full.
This configuration holds the goal texts and limits the assembled groups with
``max_total``, keeping at least one context per goal.
"""

CONTEXT_DATASET_NAME: ClassVar[str] = "prompt_inject_contexts"
DEFAULT_MAX_DATASET_SIZE: ClassVar[int] = 12
Expand Down Expand Up @@ -106,16 +114,6 @@ def _set_goal_texts(self, *, goal_texts: Sequence[str]) -> None:
raise ValueError("goal_texts must not contain duplicate values.")
self._goal_texts = list(goal_texts)

async def get_attack_seed_groups_async(self, *, apply_sampling: bool = True) -> list[AttackSeedGroup]:
"""
Resolve flat groups with the same goal coverage as the grouped resolver.

Returns:
list[AttackSeedGroup]: The validated context and goal groups.
"""
groups_by_dataset = await self.get_attack_groups_by_dataset_async(apply_sampling=apply_sampling)
return [group for groups in groups_by_dataset.values() for group in groups]

def _sample_groups_by_dataset(
self, groups_by_dataset: dict[str, list[AttackSeedGroup]]
) -> dict[str, list[AttackSeedGroup]]:
Expand All @@ -135,24 +133,31 @@ def _sample_groups_by_dataset(
key=lambda _, group: (group.objective.metadata or {})["goal_text"],
)

def size_caps_by_dataset(self) -> dict[str, list[tuple[str, int, Literal["dataset", "configuration", "compound"]]]]:
"""
Describe the shared cap on the assembled context groups.

Returns:
dict: The context population mapped to its configuration cap.
"""
caps: list[tuple[str, int, Literal["dataset", "configuration", "compound"]]] = (
[] if self.max_total == "all" else [("combined configuration cap", self.max_total, "configuration")]
)
return {self.CONTEXT_DATASET_NAME: caps}

def validate_configuration(self) -> None:
"""
Check the sources and goal coverage budget without reading contexts.
Check the goal coverage budget without reading contexts.

Raises:
DatasetConstraintError: If the sources are unsupported or the cap cannot cover all goals.
DatasetConstraintError: If the configuration names datasets or the cap cannot cover all goals.
"""
super().validate_configuration()
if any(self.source_limit(source.name) != "all" for source in self.sources):
raise DatasetConstraintError("PromptInject ingredient sources must be uncapped; use max_total.")
if not self.dataset_names:
raise DatasetConstraintError(
"PromptInject requires the prompt_inject_contexts dataset; inline seeds are not supported."
)
required_dataset_names = set(PromptInject.required_datasets())
if set(self.dataset_names) != required_dataset_names:
if self.sources or self.source_kind is DatasetSourceKind.INLINE:
raise DatasetConstraintError(
f"PromptInject requires exactly these datasets: {sorted(required_dataset_names)}."
f"PromptInject reads its templates as required data: {PromptInject.required_datasets()}. "
"PromptInjectDatasetConfiguration only takes goal_texts and max_total; "
"inline seeds and other datasets are not supported."
)
cap = self.max_total
if cap != "all" and cap < len(self._goal_texts):
Expand All @@ -161,15 +166,14 @@ def validate_configuration(self) -> None:
f"({len(self._goal_texts)})."
)

def _build_attack_groups(self, seeds: list[Seed[Any]]) -> list[AttackSeedGroup]:
def assemble_groups(self, contexts: list[Seed[Any]]) -> dict[str, list[AttackSeedGroup]]:
"""
Create attack groups from the context templates.
Create one attack group per context and goal.

Returns:
list[AttackSeedGroup]: The context and goal attack groups.
dict[str, list[AttackSeedGroup]]: The unsampled groups under the context dataset name.
"""
groups: list[AttackSeedGroup] = []
contexts = [seed for seed in seeds if seed.dataset_name == self.CONTEXT_DATASET_NAME]
for context in contexts:
groups.extend(
[
Expand All @@ -190,7 +194,7 @@ def _build_attack_groups(self, seeds: list[Seed[Any]]) -> list[AttackSeedGroup]:
for goal_text in self._goal_texts
]
)
return groups
return {self.CONTEXT_DATASET_NAME: groups}


class PromptInjectTechnique(ScenarioTechnique):
Expand Down Expand Up @@ -222,7 +226,8 @@ class PromptInject(Scenario):

Each attack places a selected injection technique inside one of 35 benign tasks.
``goal_texts`` defines the text that the injection asks the target to return.
Responses are scored by checking for that text.
Responses are scored by checking for that text. The context and technique
templates are required data, always read in full.
"""

VERSION: int = 3
Expand Down Expand Up @@ -279,15 +284,22 @@ def __init__(
version=self.VERSION,
technique_class=PromptInjectTechnique,
default_dataset_config=PromptInjectDatasetConfiguration(
sources=[DatasetSource(name=name) for name in self.required_datasets()],
max_per_dataset="all",
max_total=PromptInjectDatasetConfiguration.DEFAULT_MAX_DATASET_SIZE,
goal_texts=self.DEFAULT_GOAL_TEXTS,
),
objective_scorer=objective_scorer,
scenario_result_id=scenario_result_id,
)

def get_required_data(self) -> ScenarioRequiredData:
"""
Return the context and technique templates, read in full.

Returns:
ScenarioRequiredData: The PromptInject template datasets.
"""
return ScenarioRequiredData(sources=[DatasetSource(name=name) for name in self.required_datasets()])

def _validate_runtime_configuration(self) -> None:
self._dataset_config = self._get_promptinject_dataset_config(
goal_texts=cast("list[str]", self.params["goal_texts"])
Expand All @@ -311,9 +323,14 @@ async def _resolve_seed_groups_by_dataset_async(
if self._use_goal_scorers:
self._objective_scorer = self._build_goal_scorer(goal_texts=goal_texts)
self._objective_scorer_identifier = self._objective_scorer.get_identifier()
self._dataset_config = config
groups = await config.get_attack_groups_by_dataset_async(apply_sampling=apply_sampling)
self._technique_templates = await self._load_technique_templates_async()
templates = await cast("ScenarioRequiredData", self._required_data).load_async()
Comment thread
romanlutz marked this conversation as resolved.
Resolved
groups = config.select_assembled_groups(
groups_by_population=config.assemble_groups(
templates[PromptInjectDatasetConfiguration.CONTEXT_DATASET_NAME]
),
apply_sampling=apply_sampling,
)
self._technique_templates = self._select_technique_templates(templates[self.TECHNIQUE_DATASET_NAME])
return groups

async def _build_atomic_attacks_async(self, *, context: ScenarioContext) -> list[AtomicAttack]:
Expand Down Expand Up @@ -392,17 +409,16 @@ def _get_promptinject_dataset_config(self, *, goal_texts: Sequence[str]) -> Prom
config._set_goal_texts(goal_texts=goal_texts)
return config

async def _load_technique_templates_async(self) -> dict[str, SeedPrompt]:
def _select_technique_templates(self, seeds: list[Seed[Any]]) -> dict[str, SeedPrompt]:
"""
Load the selected technique templates from memory.
Pick the technique templates by name.

Returns:
dict[str, SeedPrompt]: Technique templates keyed by technique name.

Raises:
DatasetConstraintError: If a selected technique has no template.
"""
seeds = await self._dataset_config._collect_seeds_for_dataset_async(dataset_name=self.TECHNIQUE_DATASET_NAME)
templates: dict[str, SeedPrompt] = {}
for seed in seeds:
if isinstance(seed, SeedPrompt) and seed.name:
Expand Down
Loading
Loading