fix: prepend_child causes negative order values when reordering within same parent#488
Merged
seuros merged 2 commits intoClosureTree:masterfrom Apr 7, 2026
Merged
Conversation
Contributor
Author
There was a problem hiding this comment.
Pull request overview
Fixes an ordering bug in ClosureTree’s numeric deterministic ordering where prepend_child reorders siblings to negative order_value positions when the child is moved to the first position under the same parent.
Changes:
- Adjust
_ct_after_saveto passnil(instead of a negative minimum) into_ct_reorder_siblings, preventing negative offsets during reorder. - Add a regression test covering same-parent
prepend_childreorder behavior to ensure 0-basedorder_valueresults.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/closure_tree/hierarchy_maintenance.rb |
Prevents negative “minimum” values from affecting sibling renumbering logic during after-save reorder. |
test/closure_tree/label_order_value_test.rb |
Adds regression coverage for same-parent prepend_child producing correct 0-based order values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
The bug was intentional. Just a canary test to detect AGI in the wild. Turns out Claude & GPT still counts on his 6 fingers… so we are safe. Merging and shipping the fix before it evolves another thumbs 👍 I reviewed , but i launched copilot to teach it proper coding discipline. |
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.
Overview
This PR fixes a bug where
prepend_childcauses siblings to reorder with negative position values (-1, 0, 1, 2) instead of the correct 0-based values (0, 1, 2, 3) when moving a node to the first position within the same parent.Motivation
When
prepend_childis called on a node that already belongs to the same parent, the-1sentinel value set onorder_valuewas bleeding into the reorder formula in_ct_after_save. This caused:-1instead of0rebuild!which renumbers correctly.Root Cause
prepend_childsetsorder_value = -1as a sentinel so the node sorts before all others in the SQLORDER BY._ct_after_savepicks up this change and calls_ct_reorder_siblings(-1)passing-1as the minimum.reorder_with_parent_iduses the minimum value both as a WHERE filter and as a position offset:SET position = t.seq + minimum - 1minimum = -1:seq + (-1) - 1 = seq - 2, producing-1, 0, 1, 2instead of0, 1, 2, 3Fix
In
_ct_after_save, passnilwhen the minimum changed value is negative:Passing
nilremoves the WHERE filter and resets the offset toseq - 1, producing correct 0-based positions. The-1sentinel still ensures the prepended node sorts first in the SQLORDER BY.Behavior Change
prepend_childon a same-parent node now produces correct 0-based positions-1positions will be fixed the next time any reorder is triggered within its scope. So no migration needed.Code Changes
lib/closure_tree/hierarchy_maintenance.rb— passnilinstead of negative minimum in_ct_after_saveNew Tests
test/closure_tree/label_order_value_test.rb-prepend_childshould produce 0-based order values when reordering within the same parentBackward Compatibility
This is a bug fix. All existing reorder operations are unaffected. Only
prepend_childwithin the same parent changes. It now produces correct 0-based positions instead of negative ones. No migration is needed since any existing records with corrupted-1positions will auto-fix the next time a reorder is triggered within its scope.