Skip to content

Make serialization output deterministic - #14

Merged
ftomassetti merged 2 commits into
mainfrom
fix/deterministic-serialization-order
Sep 26, 2026
Merged

ftomassetti merged 2 commits into
mainfrom
fix/deterministic-serialization-order

Conversation

@ftomassetti

@ftomassetti ftomassetti commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. Order of the nodes. JsonSerialization.serialize_tree_to_json_element and ProtoBufSerialization.serialize_tree collected the nodes into a set, then serialized them by iterating it. Nodes hash as hash(self.id), and string hashes change per process.
  2. Order of the properties within a node. Concept.inherited_features() and Interface.inherited_features() iterate Classifier.all_ancestors(), which returned a set. For example, the order of name (from INamed) and key (from IKeyed) 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

  • Both serializers collect the nodes into a list, in tree order: depth-first, each node followed by its annotations and then its children. This is the same order serialize_trees_to_json_element already produced. No deduplication is needed: in a tree each node is reached once. The set only mattered for a malformed model with the same node object contained twice, where it hid the duplicate.
  • Classifier.all_ancestors() returns a list in a deterministic order: breadth-first, following direct_ancestors() in declaration order, each ancestor once. This changes the return type from set to list. The only internal callers iterate it, and the existing tests already convert it to a set. The order of inherited features doesn't change which features a classifier has: combine_features only 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 set for already-seen classifiers, the same work the previous implementation did.

ftomassetti and others added 2 commits September 26, 2026 21:00
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>
@ftomassetti
ftomassetti merged commit 4416873 into main Sep 26, 2026
5 checks passed
@ftomassetti
ftomassetti deleted the fix/deterministic-serialization-order branch September 26, 2026 19:03
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.

1 participant