Skip to content

core/priority: use stable sort for scored priorities - #4611

Merged
pinebit merged 1 commit into
mainfrom
pinebit/priority-stable-sort
Jul 29, 2026
Merged

core/priority: use stable sort for scored priorities#4611
pinebit merged 1 commit into
mainfrom
pinebit/priority-stable-sort

Conversation

@pinebit

@pinebit pinebit commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Replaces slices.SortFunc with slices.SortStableFunc when ordering scored priorities, so equal-score priorities keep first-seen order, with messages processed in ascending peer ID order.

Go's sort is unstable for slices longer than ~12 elements (below that, pdqsort falls back to insertion sort, which is stable in practice). For topics with 13+ priorities, the order of equal-score entries was unspecified: it could change whenever Go changes its sort implementation, making mixed-version clusters compute different PriorityResult values from identical inputs, and other implementations could not reproduce Charon's result bytes. The serialized PriorityResult feeds QBFT proposals, so ordering must be a pure deterministic function of the message set.

Adds a regression test with 4 peers and a 24-priority topic where tied pairs arrive out of score order; it fails with SortFunc and passes with SortStableFunc.

category: bug
ticket: none

Replace slices.SortFunc with slices.SortStableFunc when ordering scored
priorities so that equal-score priorities keep first-seen order, with
messages processed in ascending peer ID order. Go's sort is unstable
for slices longer than ~12 elements, so the equal-score order depended
on the sort implementation and could diverge across versions and other
implementations. Add a regression test with 24 priorities where tied
pairs arrive out of score order.

category: bug
ticket: none

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pinebit
pinebit requested a review from KaloyanTanev July 29, 2026 12:57
@sonarqubecloud

Copy link
Copy Markdown

@pinebit
pinebit enabled auto-merge (squash) July 29, 2026 13:03
@pinebit
pinebit merged commit 6054bcb into main Jul 29, 2026
12 checks passed
@pinebit
pinebit deleted the pinebit/priority-stable-sort branch July 29, 2026 13:06
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.54%. Comparing base (3335e6e) to head (4e1c63b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4611      +/-   ##
==========================================
- Coverage   57.59%   57.54%   -0.06%     
==========================================
  Files         246      246              
  Lines       33707    33707              
==========================================
- Hits        19415    19398      -17     
- Misses      11849    11864      +15     
- Partials     2443     2445       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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