-
Notifications
You must be signed in to change notification settings - Fork 832
FIX: stop seeded converters from reseeding the global RNG #2397
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import random | ||
|
|
||
| import pytest | ||
|
|
||
| from pyrit.converter import ZalgoConverter | ||
|
|
@@ -23,6 +25,32 @@ async def test_zalgo_reproducible_seed(): | |
| assert result1.output_text == result2.output_text | ||
|
|
||
|
|
||
| async def test_zalgo_seed_does_not_disturb_global_rng(): | ||
| """A seeded converter must not reseed the process-wide RNG.""" | ||
| random.seed(0) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regression test mutates the process-wide RNG and leaves it seeded at |
||
| state_before = random.getstate() | ||
|
|
||
| converter = ZalgoConverter(intensity=5, seed=42) | ||
| await converter.convert_async(prompt="seed test") | ||
|
|
||
| assert random.getstate() == state_before | ||
|
|
||
|
|
||
| async def test_zalgo_seed_is_repeatable_on_same_instance(): | ||
| prompt = "seed test" | ||
| converter = ZalgoConverter(intensity=5, seed=123) | ||
| first = await converter.convert_async(prompt=prompt) | ||
| second = await converter.convert_async(prompt=prompt) | ||
| assert first.output_text == second.output_text | ||
|
|
||
|
|
||
| async def test_zalgo_unseeded_converters_stay_independent(): | ||
| """An unseeded converter must keep producing varied output.""" | ||
| converter = ZalgoConverter(intensity=5) | ||
| outputs = {(await converter.convert_async(prompt="seed test")).output_text for _ in range(5)} | ||
| assert len(outputs) > 1 | ||
|
|
||
|
|
||
| async def test_zalgo_zero_intensity_returns_original(): | ||
| prompt = "no chaos please" | ||
| converter = ZalgoConverter(intensity=0) | ||
|
|
||
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.
This no longer guarantees the converter's documented reproducibility when Zalgo is composed with an unseeded random word selector.
WordLevelConverter.convert_async()performs word selection after this reset, butWordProportionSelectionStrategynow owns an independent RNG, so repeated calls toZalgoConverter(seed=42, word_selection_strategy=WordProportionSelectionStrategy(proportion=0.5))can select different words and produce different outputs. Please either use one operation-local RNG across selection and mark generation, or explicitly define seeds as component-scoped and update the public contract accordingly. Add a regression test for this composed case.