fix: make timeline components hash deterministic - #585
Open
FelipeDefensor wants to merge 1 commit into
Open
Conversation
`hash_components` concatenated component hashes in `_components` order, and `restore_state` recreated components by iterating a set of hash strings. Set iteration order over strings depends on PYTHONHASHSEED, so after an undo the components landed in `_components` in a process-dependent order. That order only matters because `ORDERING_ATTRS` is not a total order across component kinds: a Staff at index 0 and a Clef at time 0 both have an ordinal of `(0,)`, and Clef ignores `staff_index` entirely, so the clefs of a multi-staff score all tie as well. `bisect.insort_left` leaves tied components in insertion order, so the digest changed even though the components were identical. Plain `sorted()` would not have fixed this — Timsort is stable, so tied components keep their input order. Hash over `sorted(self._components, key=lambda c: (c.ordinal, c.hash))` instead, which is total and reproducible while still matching the ordinal order whenever ordinals are distinct, and build `restore_state`'s delete/create lists by iterating the hash-keyed dicts rather than the sets so the resulting component order is reproducible too. Consequences of the digest change: timelines whose components have distinct ordinals (every kind other than score) hash exactly as before. Score timelines can hash differently, but the `components_hash` stored in a .tla is discarded on load — `App._do_open` overwrites `file.timelines` with freshly computed state — so files already on disk are unaffected. The string shape (`h1|h2|...|hn|`, empty string for no components) is unchanged and now pinned by a test.
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
TimelineComponentManager.hash_componentsconcatenated component hashes in_componentsorder, andrestore_staterecreated components by iterating a set of hash strings. Set iteration order over strings depends onPYTHONHASHSEED, so after an undo the components landed in_componentsin a process-dependent order and the digest changed even though the components were identical.That order only matters because
ORDERING_ATTRSis not a total order across component kinds.Stafforders byindexandClefbytime, so a staff at index 0 and a clef at time 0 both have an ordinal of(0,)and neither sorts before the other.Clefignoresstaff_indexentirely, so the clefs of a multi-staff score all tie as well.bisect.insort_leftleaves tied components in insertion order.Worth flagging for reviewers: plain
sorted(self._components)does not fix this. Timsort is stable, so tied components keep their input order and the digest still varies. A tiebreaker is required.Impact: any
undoable()test on a timeline with several components is flaky, andcomponents_hashfeeds the "is the file modified" comparison inare_tilia_data_equal.Fix
hash_componentsiteratessorted(self._components, key=lambda c: (c.ordinal, c.hash))— total and reproducible, and identical to the ordinal order whenever ordinals are distinct.restore_statebuilds its delete/create lists by iterating the hash-keyed dicts rather than the sets, so the resulting component order is reproducible too.Effect on hashes already saved in
.tlafilescomponents_hashis discarded on load, sinceApp._do_openoverwritesfile.timelineswith freshly computed state. Planting a pre-fix hash in a.tlaand reopening it leavesis_file_modifiedatFalse.The digest's string shape (
h1|h2|...|hn|, empty string when there are no components) is unchanged, and is now pinned by a test so it cannot drift silently.Tests
test_score_timeline.py::TestHashComponents(2 tests)test_score_timeline_ui.py::TestClear::test_undo_redotest_timeline_component_manager.py::TestHashComponents(2 tests)The two
test_score_timeline.pytests are the real regression guards: they are deterministic, so a single-seed CI run cannot pass them by luck.test_undo_redoreproduces the user-visible symptom, which is inherently seed-dependent.Full suite: 1834 passed. The two
tests/test_app.pyfailures underpytest -n autoare pre-existing xdist flakiness —devfails a different pair the same way, and all 90 pass serially.