Repository navigation
FIX: preserve winning score metadata in all-categories float aggregators - #4330
Chen Yufeiyang (feiiiiii5) wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the change preserving winning-score metadata in all-categories MAX and MIN aggregation.
No open issues 🎉
What changed in this PR
This PR aligns all-categories float-score aggregation with per-category behavior by preserving the winning score’s metadata when keys conflict.
Changes:
- Add selected-score metadata handling when category grouping is disabled.
- Enable that behavior for
FloatScaleScorerAllCategories.MAXandMIN. - Add tests for metadata merging and first-winner tie-breaking for
MAX.
| File | Description |
|---|---|
tests/unit/score/test_float_scale_score_aggregator.py |
Tests conflicting metadata resolution for extrema and tie-breaking for MAX. |
pyrit/score/float_scale/float_scale_score_aggregator.py |
Preserves winning metadata in all-categories MAX and MIN aggregation. |
Review effort: Balanced
Varun Poojari (Varunpoojari)
left a comment
There was a problem hiding this comment.
Thanks for the PR; the tie-breaking test for MAX is a good addition. A few observations from the diff:
metadata.update(selected_metadata)mutatesmetadata. I can't see from the hunk wheremetadatais created. If it is a variable of the enclosing factory (closed over byaggregator) rather than built per call, the update would carry keys from one invocation into the next. Could you confirm it is built fresh insideaggregator? If it is, a test that callsMAXtwice on different inputs would guard against this.next(... if value == raw_result)has no default, so it raisesStopIterationif nothing matches, for example if_float_valuescould ever yield a NaN. I may be missing that it filters those out. Passing a default, or an explicit assertion with a clear message, would make a failure easier to diagnose.- The tie test covers
MAXonly.MINuses the same code path withuse_selected_score_metadata=True, so a tie case forMINwould complete the coverage. - The new code selects by the unclamped
raw_result, while the returned value is clamped. A test with an out-of-range score would show the two agree.
… breaking, isolation, and clamping
|
Thanks for the thorough review and helpful observations, Varun Poojari (@Varunpoojari)! I've addressed each of the points in the latest commit:
|
| result = max(0.0, min(1.0, result)) | ||
| raw_result = result_func(float_values) | ||
| if use_selected_score_metadata: | ||
| selected_score = next( |
There was a problem hiding this comment.
[P2] If no constituent matches raw_result (for example, a NaN score_value assigned after construction), this branch now drops the winner's metadata without any error and returns 1.0. Before this PR, the grouped branch raised here. Please raise an error in both branches (here and line 319). A small shared helper would also remove the duplicated block:
def _select_winner(scores, float_values, raw_result):
winner = next((s for s, v in zip(scores, float_values, strict=True) if v == raw_result), None)
if winner is None:
raise ValueError(f"No constituent score matches aggregate value {raw_result!r}")
return winner
if use_selected_score_metadata:
metadata.update(_select_winner(scores_list, float_values, raw_result).score_metadata or {})| assert min_result.metadata == {"severity": 2, "low_only": 1, "high_only": 1} | ||
|
|
||
|
|
||
| def test_all_categories_max_uses_first_winner_metadata_for_ties() -> None: |
There was a problem hiding this comment.
[P2] AVERAGE runs through the same changed branch, but no test checks that it still drops conflicting metadata. Please add a test where the average equals one of the constituent scores, and consider parametrizing the MAX/MIN tie tests:
def test_all_categories_average_omits_conflicting_metadata() -> None:
scores = [
_mk_score(0.3, category=["Hate"], metadata={"severity": 1}),
_mk_score(0.5, category=["Violence"], metadata={"severity": 3}),
_mk_score(0.7, category=["Sexual"], metadata={"severity": 5}),
]
result = FloatScaleScorerAllCategories.AVERAGE(scores)[0]
assert result.value == 0.5
assert "severity" not in result.metadata@pytest.mark.parametrize("aggregator, tied, other", [
(FloatScaleScorerAllCategories.MAX, 0.7, 0.3),
(FloatScaleScorerAllCategories.MIN, 0.2, 0.8),
])
def test_all_categories_extrema_use_first_winner_metadata_for_ties(aggregator, tied, other) -> None:
...Share one winner selector between the grouped and ungrouped branches so an unattributable aggregate raises instead of silently dropping the winner's metadata. Add coverage for AVERAGE and for both branches with a NaN value.
|
Thanks hannahwestra25 — both P2 findings are addressed in
Verification: |
Description
When aggregating scores with
FloatScaleScorerAllCategories.MAXorMIN, conflicting metadata keys between constituent scores were previously discarded becauseuse_selected_score_metadatawas neither supported whengroup_by_category=Falsein_create_aggregator_by_categorynor enabled onFloatScaleScorerAllCategories.This aligns
FloatScaleScorerAllCategories.MAXandMINwith theirFloatScaleScorerByCategorycounterparts:_create_aggregator_by_categoryto resolve conflicting metadata from the winning score whenuse_selected_score_metadata=Trueandgroup_by_category=False.use_selected_score_metadata=TrueonFloatScaleScorerAllCategories.MAXandMIN.FloatScaleScorerAllCategories.