fix: make generate_session_id independent of the global random seed - #321
Open
pcbeingused333 wants to merge 1 commit into
Open
fix: make generate_session_id independent of the global random seed#321pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
`generate_session_id()` picked its adjective/noun with `random.choice`. `set_seed()` reseeds Python's global `random`, so every run started with the same seed produced the same word pair (only the `secrets.token_hex` suffix varied), which defeats the point of a per-run identifier. Use `secrets.choice` for the words too. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
Contributor
Author
|
The |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
generate_session_id()picks its adjective and noun withrandom.choice:set_seed()reseeds Python's globalrandomstate. So any run that callsset_seed(N)and thengenerate_session_id()gets the same word pair every time — only thesecrets.token_hex(2)suffix varies:Since
set_seed()is the normal way to use this reproducibility-focused framework, session ids collapse to near-identical strings across runs, which defeats the point of a human-readable per-run identifier.Fix
Use
secrets.choicefor the words too (secretsis already imported for the hex suffix).set_seed()still reseedsrandom/numpy/torchexactly as before — only the session-id word choice is moved off the seeded RNG.Tests
Added
test_word_choice_is_independent_of_the_global_seed: seeding with a fixed value and callinggenerate_session_id()repeatedly still yields varying word parts. It fails onmain(frosty_morningpinned by the seed) and passes with the change.pytest tests/unit/test_reproducibility.pyis green (19 passed);ruff check/ruff formatclean.