Module spatial_invariant

Module spatial_invariant 

Source
Expand description

Structural invariant over a finished spatial-node set, split out of spatial.rs to stay under the module-size ratchet (the same reason spatial_tree.rs and spatial_elevation.rs exist).

#3973 fixed the same defect shape twice, pointwise, in two different code paths: a dangling children_ids entry naming an entity with no corresponding node in nodes_map (first a depth-capped node resurrected as a fake root, then a rescued orphan’s children_ids populated straight from spatial_children_map with no filtering). Each fix closed one path into the tree without ruling the shape out structurally, so this checks the finished tree instead of any one code path.

Functions§

reachability_and_depth_violations 🔒
Global check, added for #4022: BFS every children_ids edge starting from every node whose parent_id is the sentinel 0 (there can legitimately be more than one, see the module doc above), tracking depth as it goes. Any node the walk never reaches is flagged - this is what catches a cycle isolated from every root (e.g. two nodes that are each other’s parent and only listed child: each satisfies the local clauses above, but neither has a parent_id == 0 node in its ancestry, so neither is ever enqueued). Any node the walk does reach has its level compared against the depth the walk assigned it, catching a level that disagrees with the node’s actual position in the tree.
spatial_hierarchy_consistency_violations 🔒
Check a finished node set for the dangling-reference shape #3973 fixed twice pointwise: (a) every id in every node’s children_ids must resolve to a node in the set, (b) every node’s parent_id must either resolve to a node or be the root sentinel (0 - never a real STEP entity id), and (c) the two directions must agree - A.children_ids containing B and B.parent_id == A are two representations of the same fact. Clause (c) is the one that actually catches the family: a fake root with a real parent_id disagreement passes (a) and (b) - it names no nonexistent id and has a resolvable-or-sentinel parent - while still being wrong.