From bb76d211a53436336884534cc4545b86775d0dcf Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 24 Mar 2026 23:51:49 +0000 Subject: [PATCH] fix: clean up orphaned BVH nodes when remove creates a partial root When multiple removes occur without an intervening refit, orphaned wide nodes accumulate in the nodes vector. If the root is then reduced to a single leaf (partial root), those orphaned nodes remain, causing optimize_incremental to operate on a corrupt tree and eventually crash. Truncate nodes and parents to 1 after creating a partial root, since only node[0] is reachable in that state. Co-Authored-By: Claude Opus 4.6 --- src/partitioning/bvh/bvh_tree.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/partitioning/bvh/bvh_tree.rs b/src/partitioning/bvh/bvh_tree.rs index 35baba88..8f89f45a 100644 --- a/src/partitioning/bvh/bvh_tree.rs +++ b/src/partitioning/bvh/bvh_tree.rs @@ -2376,6 +2376,14 @@ impl Bvh { // Now we can just clear the right leaf. self.nodes[0].right = BvhNode::zeros(); + + // Clean up orphaned nodes. With a partial root, only node[0] is + // reachable. Previous removes may have left orphaned wide nodes + // that were waiting for refit to compact them. If we don't truncate + // here, the tree appears as a single-leaf tree with unreachable + // nodes, which corrupts optimize_incremental. + self.nodes.truncate(1); + self.parents.truncate(1); } else { // The sibling isn’t a leaf. It becomes the new root at index 0. self.nodes[0] = self.nodes[self.nodes[sibling].children as usize];