Skip to content

feat(workflow): add nondeterministic unsafe.random for read-only contexts#2141

Open
xumaple wants to merge 4 commits into
mainfrom
maplexu/2110-unsafe-random
Open

feat(workflow): add nondeterministic unsafe.random for read-only contexts#2141
xumaple wants to merge 4 commits into
mainfrom
maplexu/2110-unsafe-random

Conversation

@xumaple

@xumaple xumaple commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Adds workflowInfo().unsafe.random — a nondeterministic random source (random(), uuid4(), fillRandom()) for read-only contexts (Query handlers, update validators, interceptors), where the seed-derived random stream would otherwise repeat across evaluations.

It mirrors unsafe.now: the native Math.random is captured before the Workflow sandbox overrides it, and the closures are stripped across the worker-thread boundary and reinstalled on each side without mutating the live Workflow info.

Fixes #2110

@xumaple xumaple requested a review from a team as a code owner June 24, 2026 22:59
Comment thread packages/workflow/src/random-helpers.ts Outdated
Comment thread packages/test/src/test-random-stream-reset.ts Outdated
Comment thread packages/workflow/src/random-helpers.ts
Comment thread packages/workflow/src/random-helpers.ts Outdated
Adds workflowInfo().unsafe.random — a nondeterministic random source (random, uuid4, fillRandom) for read-only contexts (Query handlers, update validators, interceptors), where the seed-derived random stream would otherwise repeat across evaluations. Mirrors unsafe.now: captures native Math.random before the Workflow sandbox overrides it, and strips the closures across the worker-thread boundary (reinstalling on each side without mutating the live Workflow info).
@xumaple xumaple force-pushed the maplexu/2110-unsafe-random branch from a718100 to fe17a65 Compare June 25, 2026 16:55
xumaple added 2 commits June 25, 2026 15:23
Per PR review: toJSON returns undefined so a JSON-serialized WorkflowInfo omits unsafe.random (matching now) instead of an empty {}. Also compares the filled byte arrays with notDeepEqual.

@eamsden eamsden 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.

Most of this looks good but please cherry-pick the reproducers in 7089ca9. The serialization fix seems fragile and non-obvious and those should go in as regression tests.

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] Nondeterministic random generation API for use in query plugins

3 participants