Skip to content

Reduce likelyhood of generated number conflicts - #1007

Merged
zetter-rpf merged 2 commits into
mainfrom
reduce-class-code-generation-errors
Sep 9, 2026
Merged

Reduce likelyhood of generated number conflicts#1007
zetter-rpf merged 2 commits into
mainfrom
reduce-class-code-generation-errors

Conversation

@zetter-rpf

Copy link
Copy Markdown
Contributor

Status

What's changed?

Switch to using the SecureRandom generator. This is an approach similar to taken for Join Codes in #952

See commit for more.

By using the non-secure random generator, and caching the class that may be forked in Puma, we're making conflicts more likely.

This is still a possibility of collisions - we've almost used 1% of available codes, however this might reduce the change.

I've removed the random number seed as part of this - SecureRandom can't be seeded in the same way. Looking at the commit history, this was a tool to help make test runs more deterministic but isn't required. If we do, we should add the ability to swap out random number generators.
@cla-bot cla-bot Bot added the cla-signed label Sep 8, 2026
@zetter-rpf
zetter-rpf marked this pull request as ready for review September 8, 2026 15:13
Copilot AI lite review requested due to automatic review settings September 8, 2026 15:13

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.

🟡 Changes recommended

The updated randomness approach makes the existing “different code each time” spec effectively non-reproducible and probabilistic, increasing the risk of rare flaky CI failures.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Updates ForEducation code generation to avoid RNG state collisions across forked app workers (similar to the SecureRandom change previously made for join codes).

Changes:

  • Switch ForEducationCodeGenerator from a shared Random instance to SecureRandom.random_number.
  • Remove the RSpec suite seed-based initialization of ForEducationCodeGenerator.random.
  • Update the generator spec to stub SecureRandom.random_number instead of Random#rand.
File summaries
File Description
lib/for_education_code_generator.rb Replace shared Random usage with SecureRandom for fork-safe randomness.
spec/lib/for_education_code_generator_spec.rb Update unit test to stub SecureRandom; (note: one test should be made deterministic to avoid rare flakiness).
spec/support/for_education_code_generator.rb Remove suite-level seeding/configuration for the old injectable RNG.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread spec/lib/for_education_code_generator_spec.rb
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

Test coverage

93.57% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/34244014757

@raspberrypiherokubot
raspberrypiherokubot temporarily deployed to editor-api-p-reduce-cla-og9nyr September 8, 2026 15:18 Inactive
@zetter-rpf
zetter-rpf temporarily deployed to editor-api-p-reduce-cla-og9nyr September 8, 2026 15:19 Inactive
@zetter-rpf zetter-rpf changed the title Reduce likelyhood of random number conflicts Reduce likelyhood of generated number conflicts Sep 8, 2026
@zetter-rpf
zetter-rpf merged commit 7831e6b into main Sep 9, 2026
8 checks passed
@zetter-rpf
zetter-rpf deleted the reduce-class-code-generation-errors branch September 9, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants