Skip to content

Add random seed access and callback methods to temporalio.workflow#1320

Merged
tconley1428 merged 4 commits intomainfrom
add-random-seed-methods
Feb 5, 2026
Merged

Add random seed access and callback methods to temporalio.workflow#1320
tconley1428 merged 4 commits intomainfrom
add-random-seed-methods

Conversation

@tconley1428
Copy link
Contributor

@tconley1428 tconley1428 commented Feb 4, 2026

Summary

  • Add workflow.random_seed() to get current random seed from core
  • Add workflow.register_random_seed_callback() to register for seed change notifications
  • Add workflow.new_random() to create auto-reseeded Random instances

Changes

This PR adds three new methods to temporalio.workflow that provide access to and control over the workflow's random seed:

  1. workflow.random_seed() - Returns the current random seed value from core
  2. workflow.register_random_seed_callback(callback) - Registers callbacks that get invoked when the random seed changes (e.g., during workflow resets)
  3. workflow.new_random() - Creates a new Random instance that automatically reseeds itself when the workflow seed changes

Implementation Details

  • Added abstract methods to _Runtime class in workflow.py
  • Implemented concrete methods in _WorkflowInstanceImpl class
  • Enhanced _apply_update_random_seed to invoke registered callbacks
  • Added seed change tracking with proper error handling
  • Comprehensive test coverage including workflow reset scenarios

Use Cases

These methods enable workflows to:

  • Access the deterministic random seed for custom randomization logic
  • React to seed changes during workflow resets and replays
  • Create multiple synchronized random number generators
  • Maintain determinism while providing flexibility for complex random operations

Test Plan

  • Added comprehensive test with workflow reset to verify seed change callbacks
  • All existing tests pass
  • Type checking passes
  • Linting passes

Fixes #1319

🤖 Generated with Claude Code

This commit adds three new methods to temporalio.workflow to provide access to
and control over the workflow's random seed:

1. workflow.random_seed() - Returns the current random seed value from core
2. workflow.register_random_seed_callback() - Registers callbacks for seed changes
3. workflow.new_random() - Creates an auto-reseeded Random instance

These methods enable workflows to:
- Access the current deterministic random seed
- React to seed changes during workflow resets/replays
- Create additional random number generators that stay synchronized

The implementation includes:
- Abstract methods in _Runtime class
- Concrete implementation in _WorkflowInstanceImpl
- Proper callback invocation during seed updates
- Comprehensive test coverage with workflow reset scenarios
- Full type safety and documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@tconley1428 tconley1428 requested a review from a team as a code owner February 4, 2026 22:05
@tconley1428 tconley1428 merged commit 3b0b1f9 into main Feb 5, 2026
26 of 27 checks passed
@tconley1428 tconley1428 deleted the add-random-seed-methods branch February 5, 2026 22:55
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.

[Feature Request] Provide user access to randomness seed and changes

2 participants