chore: sort resource messages for deterministic generation#16756
chore: sort resource messages for deterministic generation#16756
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces deterministic ordering for resource messages by sorting them before they are returned, changing several return types from frozenset to tuple or sorted OrderedDict. This change ensures consistent output across different runs. A review comment suggests refining the sorting logic in wrappers.py by using a composite key to prevent non-deterministic behavior in case of attribute ties.
| sorted_messages = sorted( | ||
| unique_messages, | ||
| key=lambda m: m.resource_type_full_path or m.name | ||
| ) |
There was a problem hiding this comment.
The current sorting key m.resource_type_full_path or m.name may not guarantee deterministic ordering if multiple messages share the same resource type path or if a message name conflicts with a resource type path. Since the input is a frozenset (which is unordered), any tie in the sort key will result in non-deterministic output order across different runs.
Consider using a tuple as a sort key to provide a consistent tie-breaker, such as (m.resource_type_full_path or "", m.name).
| sorted_messages = sorted( | |
| unique_messages, | |
| key=lambda m: m.resource_type_full_path or m.name | |
| ) | |
| sorted_messages = sorted( | |
| unique_messages, | |
| key=lambda m: (m.resource_type_full_path or "", m.name) | |
| ) |
References
- To ensure dictionary keys remain sorted without manual effort, programmatically sort the dictionary before returning it instead of relying on manual ordering in the code.
sort resource messages for deterministic generation