Skip to content

improve determinism#5755

Merged
adhami3310 merged 1 commit intomainfrom
improve-determinism
Sep 3, 2025
Merged

improve determinism#5755
adhami3310 merged 1 commit intomainfrom
improve-determinism

Conversation

@adhami3310
Copy link
Member

No description provided.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR systematically improves determinism across the Reflex codebase by addressing non-deterministic behavior in several key areas:

Set to Dictionary Conversions: The most significant change converts methods like _get_all_custom_code() and _get_all_refs() from returning set[str] to dict[str, None]. This addresses the fundamental issue that Python sets have non-deterministic iteration order due to hash randomization, which could cause the same code to generate different output across runs. Dictionaries in Python 3.7+ preserve insertion order, ensuring consistent compilation results.

Random Number Generation: The get_unique_variable_name() function now uses a seeded random number generator (seed=42) instead of Python's global random state. This ensures that generated variable names are deterministic while maintaining uniqueness within a single run.

Hash Function Replacement: The foreach component now uses MD5 hashing instead of Python's built-in hash() function for generating index variable names. This eliminates cross-session variability caused by hash randomization.

Set Operation Standardization: Various set operations are standardized to use the | operator instead of .union() method calls, and explicit set() conversions are added where needed to ensure type consistency.

Dynamic Import Sorting: The compiler now sorts dynamic imports to ensure consistent ordering in the generated output.

These changes collectively ensure that the Reflex framework produces identical output for identical input across different Python process invocations, which is crucial for build reproducibility, reliable testing, and consistent deployment behavior.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk as it maintains functional equivalence while improving determinism
  • Score reflects well-structured changes that address a clear architectural issue without breaking existing functionality
  • No files require special attention as all changes follow consistent patterns and maintain backward compatibility

11 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 3, 2025

CodSpeed Performance Report

Merging #5755 will not alter performance

Comparing improve-determinism (14cd91b) with main (314f485)

Summary

✅ 8 untouched benchmarks

@adhami3310 adhami3310 merged commit dd993fd into main Sep 3, 2025
41 checks passed
@adhami3310 adhami3310 deleted the improve-determinism branch September 3, 2025 13:53
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.

2 participants