-
Notifications
You must be signed in to change notification settings - Fork 54
test(redteam): cover RedTeamCase metadata sync and _build_system_prompt assembly #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeroshotmind
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
zeroshotmind:test/redteam-case-and-common-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Tests for RedTeamCase metadata sync behavior.""" | ||
|
|
||
| from strands_evals.experimental.redteam.case import RedTeamCase | ||
| from strands_evals.experimental.redteam.types import AttackGoal, RedTeamConfig | ||
|
|
||
|
|
||
| def _config(**attack_goal_kwargs) -> RedTeamConfig: | ||
| kwargs = {"risk_category": "guideline_bypass", "actor_goal": "goal"} | ||
| kwargs.update(attack_goal_kwargs) | ||
| return RedTeamConfig(attack_goal=AttackGoal(**kwargs)) | ||
|
|
||
|
|
||
| def test_metadata_none_is_populated_from_attack_goal(): | ||
| """A case with no metadata gets the full attack_goal dump as its metadata.""" | ||
| case = RedTeamCase(name="c0", input="hello", config=_config()) | ||
|
|
||
| assert case.metadata == case.config.attack_goal.model_dump() | ||
| assert case.metadata["risk_category"] == "guideline_bypass" | ||
| assert case.metadata["actor_goal"] == "goal" | ||
| assert case.metadata["severity"] == "medium" | ||
|
|
||
|
|
||
| def test_existing_metadata_keys_are_preserved_not_overwritten(): | ||
| """Caller-provided metadata wins over the attack_goal dump for overlapping keys.""" | ||
| case = RedTeamCase( | ||
| name="c0", | ||
| input="hello", | ||
| config=_config(severity="critical"), | ||
| metadata={"severity": "low", "custom": "value"}, | ||
| ) | ||
|
|
||
| # setdefault semantics: caller's "severity" survives, config's does not override it. | ||
| assert case.metadata["severity"] == "low" | ||
| # Caller-only keys are untouched. | ||
| assert case.metadata["custom"] == "value" | ||
| # Keys absent from caller metadata are still filled in from attack_goal. | ||
| assert case.metadata["risk_category"] == "guideline_bypass" | ||
| assert case.metadata["actor_goal"] == "goal" | ||
|
|
||
|
|
||
| def test_partial_metadata_is_filled_in_from_attack_goal(): | ||
| """Keys the caller didn't set are backfilled from attack_goal without touching the rest.""" | ||
| case = RedTeamCase( | ||
| name="c0", | ||
| input="hello", | ||
| config=_config(context="ctx", success_criteria="criteria"), | ||
| metadata={"unrelated": "kept"}, | ||
| ) | ||
|
|
||
| assert case.metadata["unrelated"] == "kept" | ||
| assert case.metadata["context"] == "ctx" | ||
| assert case.metadata["success_criteria"] == "criteria" | ||
|
|
||
|
|
||
| def test_empty_dict_metadata_is_filled_in_from_attack_goal(): | ||
| """An explicit empty dict (falsy but not None) still gets backfilled, exercising the else branch.""" | ||
| case = RedTeamCase(name="c0", input="hello", config=_config(), metadata={}) | ||
|
|
||
| assert case.metadata == case.config.attack_goal.model_dump() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Tests for shared prompt-block assembly in strategies/_common.py.""" | ||
|
|
||
| from strands_evals.experimental.redteam.strategies._common import ( | ||
| EXIT_BLOCK, | ||
| FORMAT_BLOCK, | ||
| PROFILE_BLOCK, | ||
| ROLE_BLOCK, | ||
| RULES_BLOCK, | ||
| _build_system_prompt, | ||
| ) | ||
|
|
||
|
|
||
| def test_build_system_prompt_includes_all_common_blocks(): | ||
| prompt = _build_system_prompt("PLAYBOOK CONTENT") | ||
|
|
||
| assert ROLE_BLOCK in prompt | ||
| assert PROFILE_BLOCK in prompt | ||
| assert "PLAYBOOK CONTENT" in prompt | ||
| assert RULES_BLOCK in prompt | ||
| assert EXIT_BLOCK in prompt | ||
| assert FORMAT_BLOCK in prompt | ||
|
|
||
|
|
||
| def test_build_system_prompt_orders_blocks_role_profile_playbook_rules_exit_format(): | ||
| """Downstream .format() calls fill {actor_goal}/{max_turns} placeholders positionally | ||
| within this fixed layout, so the block order is load-bearing, not incidental.""" | ||
| prompt = _build_system_prompt("PLAYBOOK") | ||
|
|
||
| positions = [ | ||
| prompt.index(ROLE_BLOCK), | ||
| prompt.index(PROFILE_BLOCK), | ||
| prompt.index("PLAYBOOK"), | ||
| prompt.index(RULES_BLOCK), | ||
| prompt.index(EXIT_BLOCK), | ||
| prompt.index(FORMAT_BLOCK), | ||
| ] | ||
|
|
||
| assert positions == sorted(positions) | ||
|
|
||
|
|
||
| def test_profile_and_exit_blocks_retain_format_placeholders(): | ||
| """These blocks are filled in later via str.format(actor_profile=..., max_turns=...); | ||
| _build_system_prompt itself must not consume those placeholders.""" | ||
| prompt = _build_system_prompt("PLAYBOOK") | ||
|
|
||
| assert "{actor_profile}" in prompt | ||
| assert "{max_turns}" in prompt |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, I would recommend placing this under the directory redteam/strategies/. This also helps directly map the test module to its src module.