Skip to content

FIX GCG: drop unchanged suffixes with a separator space and survive all-rejected batches - #4769

Merged
Roman Lutz (romanlutz) merged 5 commits into
microsoft:mainfrom
devangpratap:fix-gcg-filter-strip-and-empty
Dec 1, 2026
Merged

Roman Lutz (romanlutz) merged 5 commits into
microsoft:mainfrom
devangpratap:fix-gcg-filter-strip-and-empty

Conversation

@devangpratap

@devangpratap devangpratap commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #4545 and fixes #4546, both in the GCG candidate filter. LengthPreservingFilter.filter_candidates and the legacy MultiPromptAttack.get_filtered_cands get the same change, so the parity tests still hold.

  • GCG candidate filter keeps the current suffix when it decodes with a leading space #4545: the no-op check now compares strings the way the prompt renders them, dropping one leading separator space like AttackPrompt._update_ids does. An unchanged suffix that decodes with its separator space is dropped again. I used removeprefix(" ") instead of strip() because stripping also hides trailing space tokens: on GPT-2 and Qwen2.5, " a " and " a" are different suffixes of the same length that both re-tokenize cleanly, but they strip to the same string.
  • GCG candidate filter raises IndexError when every candidate is rejected #4546: when every row is rejected, the batch is filled with the current suffix, so the step re-scores it instead of raising IndexError. _filter_control_candidates now passes the unstripped decode of the current control tokens, so that fallback re-tokenizes to the current ids. The legacy method raises a ValueError if this happens without a curr_control.
  • Updated the CandidateFilter docstring, since the all-rejected fallback is a case where the current suffix is returned.

Per the discussion on #4545, this supersedes #4637.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 00:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review reports no active issues with the GCG candidate-filtering and current-suffix fallback changes.

No open issues 🎉

What changed in this PR

Fixes PyRIT’s GCG candidate filtering for unchanged suffixes with a leading separator space and batches where every candidate is rejected, addressing #4545 and #4546.

Changes:

  • Aligns both filter implementations’ no-op comparisons with prompt rendering while preserving meaningful whitespace.
  • Falls back to the current suffix when all candidates are rejected, passing its unstripped decode from the GCG attack.
  • Updates the filter contract and regression tests to cover these behaviors and maintain legacy parity.
File Description
tests/​unit/​executor/​promptgen/​gcg/​test_gcg_proposal.py Updates callback expectations for the decoded current control.
tests/​unit/​executor/​promptgen/​gcg/​test_gcg_core.py Verifies custom filters receive the unstripped control decode.
tests/​unit/​executor/​promptgen/​gcg/​test_default_implementations.py Adds separator-space and all-rejected regression tests for both filters.
pyrit/​executor/​promptgen/​gcg/​extension_protocols.py Documents the current-control fallback exception.
pyrit/​executor/​promptgen/​gcg/​default_implementations.py Fixes no-op comparisons and adds the all-rejected fallback.
pyrit/​executor/​promptgen/​gcg/​attack/​gcg/​gcg_attack.py Passes the unstripped current-token decode to the filter.
pyrit/​executor/​promptgen/​gcg/​attack/​base/​attack_manager.py Applies equivalent legacy filtering fixes and rejects fallback without a current control.

Review effort: Balanced

@romanlutz Roman Lutz (romanlutz) self-assigned this Dec 1, 2026
…oser

step() passed the stripped control_str as propose_candidates'
current_control_str, so a direct GCGCandidateProposer user relying on
the proposer's own candidate_filter would get a stripped all-rejected
fallback that can re-tokenize to different ids than the current
control. Decode via a shared helper in both filter paths.

Co-Authored-By: Claude Fable 5 <[email protected]>
… type

main now sets unsound-assignment to error for tests/unit (microsoft#4759), and
the alias off pytest.importorskip's module object is Any, which made
the annotated legacy_out assignment fail ty. Import the class after
the importorskip guard like test_gcg_core.py does.

Co-Authored-By: Claude Fable 5 <[email protected]>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Dec 1, 2026
Merged via the queue into microsoft:main with commit 4494808 Dec 1, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants