Repository navigation
Make serialization output deterministic - #14
Merged
Merged
Conversation
Serializing the same tree gave different output in different processes, because two things depended on Python's hash randomization: - serialize_tree_to_json_element and the protobuf serialize_tree collected the nodes into a set, so the order of the nodes changed; they now use a list, in tree order. - Classifier.all_ancestors() returned a set, which inherited_features() iterated, so the order of the properties within a node changed; it now returns a list in a deterministic breadth-first order. A new test serializes the same tree with different PYTHONHASHSEED values and checks that the output is identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The ancestor tests compared sets, so they could not catch a duplicated ancestor. They now check the exact list, including an interface reachable through two paths, and test_iface, which had no assertions, is completed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Serializing the same tree gives different output in different processes: two runs of the same program produce JSON (or protobuf) with nodes and properties in a different order. This shows up, for example, as spurious diffs in generated language files that are committed to git, such as
lwlanguages/in starlasu-specs. Two things depend on Python's hash randomization:JsonSerialization.serialize_tree_to_json_elementandProtoBufSerialization.serialize_treecollected the nodes into aset, then serialized them by iterating it. Nodes hash ashash(self.id), and string hashes change per process.Concept.inherited_features()andInterface.inherited_features()iterateClassifier.all_ancestors(), which returned aset. For example, the order ofname(fromINamed) andkey(fromIKeyed) in serialized M3 nodes changed from run to run.Both have to be fixed; either one alone still leaves the output nondeterministic. The LionWeb serialization format doesn't give meaning to these orders, so any order is valid, and this PR just makes it stable.
Changes
list, in tree order: depth-first, each node followed by its annotations and then its children. This is the same orderserialize_trees_to_json_elementalready produced. No deduplication is needed: in a tree each node is reached once. Thesetonly mattered for a malformed model with the same node object contained twice, where it hid the duplicate.Classifier.all_ancestors()returns alistin a deterministic order: breadth-first, followingdirect_ancestors()in declaration order, each ancestor once. This changes the return type fromsettolist. The only internal callers iterate it, and the existing tests already convert it to aset. The order of inherited features doesn't change which features a classifier has:combine_featuresonly drops duplicates by meta-pointer.Performance
Collecting nodes into a list is O(n), like the set, and avoids hashing each node. The ancestor traversal only adds a
setfor already-seen classifiers, the same work the previous implementation did.