Skip to content

[BREAKING] MAINT Move in-repo scorers onto the expectation-aware piece hook (phase 12, part 2) - #3312

Merged
Richard Lundeen (richlundeen) merged 11 commits into
microsoft:mainfrom
WatchTree-19:feat/scorer-hook-migration
Nov 3, 2026
Merged

Richard Lundeen (richlundeen) merged 11 commits into
microsoft:mainfrom
WatchTree-19:feat/scorer-hook-migration

Conversation

@WatchTree-19

@WatchTree-19 WatchTree-19 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Second part of phase 12 of the scorer roadmap, stacked on #3311. Every in-repo message leaf now implements the expectation-aware _score_piece_with_expectation_async hook instead of the objective-only _score_piece_async hook.

This branch merges the updated #3311, so it no longer contains ScorerCapabilities. The changes specific to this PR are in b672c9d4e and the follow-up merge 7800693c3. The earlier commits belong to #3311.

Migrated scorers (16): SubStringScorer, RegexScorer, CredentialLeakScorer, SQLInjectionOutputScorer, AgentThreatRulesScorer, PackageHallucinationScorer, DecodingScorer, GarakExploitationScorer, PromptShieldScorer, LocalRefusalClassifierScorer, AzureContentFilterScorer, LocalViolenceClassifierScorer, RobloxPiiScorer, PlagiarismScorer, SystemPromptExtractionScorer, and the ConversationScorer stub. Scorers built on these classes, such as the keyword and regex families, inherit the change. GandalfScorer was removed from main, so this PR no longer migrates it.

Each migrated hook reads the objective from the expectation, so scores do not change. The two regex subclasses that delegate to RegexScorer now pass the complete expectation to super().

The objective-only hook stays on MessageScorer for external scorers until 2.0. If code calls _score_piece_async on a scorer that implements only the typed hook, the call raises NotImplementedError and names the hook to call.

Breaking change

An external subclass of a migrated scorer that overrides _score_piece_async now raises LegacyScorerHookError at construction, with the message "Move the custom policy to _score_piece_with_expectation_async". Before this PR, the override worked because its base scorer used the legacy hook.

PyRIT does not call the old override silently or combine both hooks, because either choice can skip or repeat custom policy. To migrate, move the override and its super() call to _score_piece_with_expectation_async, and forward the expectation. Direct calls to _score_piece_async on a migrated scorer also fail; call _score_piece_with_expectation_async instead.

Tests and Documentation

  • Tests that called or patched _score_piece_async on built-in scorers now use the typed hook with the same assertions. This includes tests that main added after this PR was opened: the JavaScript, Perl, and Raku package-hallucination tests, and _PolicyScorer in test_group_adversarial_prompts.py.
  • The legacy-adapter tests in test_question_answer_scorer.py use a test-local legacy scorer, so the legacy runtime paths stay covered.
  • The guards moved to tests/unit/score/test_scorer_condition_contracts.py, because FEAT Legacy scorer hook errors and explicit objective conditions (phase 12, part 1) #3311 removed test_scorer_capabilities.py:
    • An AST scan of every module under pyrit/ requires MessageScorer to be the only class that defines _score_piece_async. The scan does not import modules, so a missing optional dependency cannot hide a scorer.
    • An external override of a migrated scorer fails at construction.
    • A legacy call to a migrated scorer names the typed hook.
    • A migrated scorer still records the objective.
  • .github/instructions/scorers.instructions.md says that in-repo leaves use the typed hook. No notebook changed, so I did not run JupyText.

Validation on 7800693c3:

  • uv run --no-sync pre-commit run --all-files: all hooks passed.
  • uv run --no-sync pytest -q -n 4 tests\unit\score tests\unit\executor\attack\test_unscored_response_outcome.py --disable-warnings: before the test fixes, 4951 passed, 7 skipped, and 10 failed. All 10 failures were package-hallucination tests from main that called _score_piece_async.
  • uv run --no-sync pytest -q tests\unit\score\test_package_hallucination_scorer.py tests\unit\score\test_scorer_condition_contracts.py tests\unit\scenario\core\test_group_adversarial_prompts.py --disable-warnings: 266 passed after the test fixes.
  • uv run --no-sync pytest -q tests\unit\score\test_scorer_condition_contracts.py --disable-warnings: 26 passed after the final lint change.
  • uv run --no-sync pytest -q -n 4 tests\unit\executor tests\unit\scenario tests\unit\registry tests\unit\backend\test_scorer_service.py tests\unit\backend\test_chat_scoring.py tests\partner_integration\azure_ai_evaluation\test_scorer_contract.py --disable-warnings: 5210 passed and 11 failed. The failures were in test_target_objective_generator.py, test_scenario_crescendo_resume.py, and test_crescendo_resilience.py.
  • uv run --no-sync pytest -q tests\unit\executor\promptgen\test_target_objective_generator.py tests\unit\scenario\core\test_scenario_crescendo_resume.py --disable-warnings -p no:randomly: 56 passed on this branch and on the FEAT Legacy scorer hook errors and explicit objective conditions (phase 12, part 1) #3311 head 5a94265c3.
  • uv run --no-sync pytest -q tests\unit\executor\attack\multi_turn\test_crescendo_resilience.py --disable-warnings -p no:randomly: 9 passed on this branch and on 5a94265c3.

Description updated with GitHub Copilot after this branch merged the capability-free #3311.

Add ScorerCapabilities, exposed as the final Scorer.capabilities property.
It reports the declared condition with the evidence a scorer accepts
(data types, roles, required metadata), so configuration and preflight
code can check a scorer before acquiring evidence. Message scorers derive
the evidence fields from their validator; behaviour flags stay on the
validator.

Add LegacyScorerHookError (a TypeError). A leaf that declares a typed
CONDITION_TYPE other than MatchesObjective, but whose only reachable hook
is an objective-only legacy hook, now fails at construction (or at class
definition for a direct Scorer subclass) instead of at score time.
Objective-based and constructor-configured legacy hooks keep working.

Objective scorers declare MatchesObjective at class level instead of
setting is_objective_required on their default validators, and the
audio and video preflight read capabilities instead of the private
validator fields. Remove the unused ambient expectation context.
Every in-repo message leaf now implements
_score_piece_with_expectation_async instead of the objective-only
_score_piece_async. Each one reads the objective from the expectation,
so scoring behaviour is unchanged. The legacy hook stays on
MessageScorer for external scorers until 2.0.

Calling _score_piece_async on a scorer that only implements the typed
hook now names the hook to call instead of reporting that no hook
exists. Tests that called or patched the legacy hook on built-in
scorers use the typed hook, and the legacy adapter tests use a
test-local legacy scorer so that path stays covered. A guard test
fails if an in-repo scorer adds the legacy hook again.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Derive condition and scorable coverage, keep message filters local, and preserve legacy evidence declarations. Centralize objective validation, retain configurable general float rubrics, and propagate legacy hook errors consistently.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Defer ScorerCapabilities to the scorer requirements design. Keep legacy hook errors, objective validation, MatchesObjective declarations, removal of ambient expectations, and scorable type declarations. Restore media helper validation from main.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Require every concrete in-repo leaf scorer to declare message evidence. Scorer requirements can then rely on derived scorable support without a separate evidence requirement.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

# Conflicts:
#	pyrit/score/true_false/gandalf_scorer.py
#	tests/unit/score/test_scorer_capabilities.py
@richlundeen Richard Lundeen (richlundeen) changed the title MAINT Move in-repo scorers onto the expectation-aware piece hook (phase 12, part 2) [BREAKING] MAINT Move in-repo scorers onto the expectation-aware piece hook (phase 12, part 2) Oct 29, 2026

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.

AI-assisted review by Copilot (trial run of a persona-based review automation). Two findings are inline. This review does not approve the PR or request changes.

Comment thread pyrit/score/message_scorer.py Resolved
Reject a legacy piece override when a migrated scorer subclass defines both hooks. Assert that typed conditions reach the dispatched scoring hook.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Both inline findings are already addressed in the selected PR head. No source changes are required by the checked plan.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Nov 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 3, 2026
Keep LDAP and Markdown leaves compatible with the expectation-aware scorer contract.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 3, 2026 06:52

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

No active review issues were identified for the expectation-aware scorer-hook migration.

No open issues 🎉

What changed in this PR

This PR continues #3311 by moving PyRIT’s remaining in-repository message scorers to the expectation-aware piece hook while preserving scoring behavior. External subclasses of migrated scorers must also migrate their legacy overrides.

Changes:

  • Replace objective-only piece hooks with expectation-aware hooks and forward expectations through superclass calls.
  • Strengthen legacy-hook rejection and provide clearer guidance for unsupported direct calls.
  • Update tests and scorer guidance, including guards against reintroducing legacy hooks.
File Description
tests/​unit/​score/​test_system_prompt_extraction_scorer.py Update extraction tests to call the typed hook.
tests/​unit/​score/​test_scorer_condition_contracts.py Add migration guards and objective-preservation checks.
tests/​unit/​score/​test_question_answer_scorer.py Preserve legacy-adapter coverage using a test-local scorer.
tests/​unit/​score/​test_package_hallucination_scorer.py Update package-detection tests to call the typed hook.
tests/​unit/​score/​test_local_violence_classifier_scorer.py Patch the typed hook in fallback tests.
tests/​unit/​score/​test_decoding_scorer.py Update decoding tests to call the typed hook.
tests/​unit/​score/​test_concurrent_scoring_lifetime.py Adapt concurrency mocks to forward expectations.
tests/​unit/​score/​test_azure_content_filter.py Update content-filter tests to call the typed hook.
tests/​unit/​score/​regex/​test_regex_scorer.py Check LDAP and Markdown hook behavior and objective preservation.
tests/​unit/​score/​regex/​test_credential_leak_scorer.py Compare credential and base regex behavior through typed hooks.
tests/​unit/​scenario/​core/​test_group_adversarial_prompts.py Migrate the test policy scorer’s override and superclass call.
tests/​unit/​executor/​attack/​test_unscored_response_outcome.py Patch the typed hook for empty scoring results.
pyrit/​score/​true_false/​wildguard_scorer.py Update a comment to name the typed hook; no behavior change.
pyrit/​score/​true_false/​substring_scorer.py Migrate substring scoring and extract the expectation’s objective.
pyrit/​score/​true_false/​regex/​sql_injection_output_scorer.py Migrate SQL scoring and forward expectations for custom patterns.
pyrit/​score/​true_false/​regex/​regex_scorer.py Migrate the shared regex scoring hook.
pyrit/​score/​true_false/​regex/​package_hallucination_scorer.py Migrate package scoring while retaining objective metadata.
pyrit/​score/​true_false/​regex/​markdown_injection.py Migrate Markdown injection scoring to the typed hook.
pyrit/​score/​true_false/​regex/​ldap_injection_output_scorer.py Migrate LDAP scoring and superclass delegation.
pyrit/​score/​true_false/​regex/​credential_leak_scorer.py Migrate credential scoring and forward complete expectations.
pyrit/​score/​true_false/​regex/​agent_threat_rules_scorer.py Migrate threat-rule scoring to the typed hook.
pyrit/​score/​true_false/​prompt_shield_scorer.py Migrate Prompt Shield scoring to the typed hook.
pyrit/​score/​true_false/​local_refusal_classifier_scorer.py Migrate refusal scoring while preserving objective handling.
pyrit/​score/​true_false/​garak_exploitation_scorer.py Migrate exploitation detection to the typed hook.
pyrit/​score/​true_false/​decoding_scorer.py Migrate decoding scoring while preserving objective handling.
pyrit/​score/​message_scorer.py Reject conflicting legacy overrides and clarify direct-call errors.
pyrit/​score/​float_scale/​system_prompt_extraction_scorer.py Migrate system-prompt extraction scoring.
pyrit/​score/​float_scale/​roblox_pii_scorer.py Migrate PII scoring and extract the expectation’s objective.
pyrit/​score/​float_scale/​plagiarism_scorer.py Adopt the typed hook while continuing to ignore scoring criteria.
pyrit/​score/​float_scale/​local_violence_classifier_scorer.py Migrate violence scoring while preserving objective handling.
pyrit/​score/​float_scale/​azure_content_filter_scorer.py Migrate Azure content-filter scoring.
pyrit/​score/​conversation_scorer.py Rename the unused piece-hook stub to the typed hook.
.github/​instructions/​scorers.instructions.md Document typed-hook requirements and legacy override rejection.

Review effort: Balanced

@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Nov 3, 2026
Merged via the queue into microsoft:main with commit 28be42d Nov 3, 2026
66 checks passed
@romanlutz Roman Lutz (romanlutz) self-assigned this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants