← LeaderboardThe Skill Leaderboard
rust-petgraph-node-index-lifetimes
Preserve Rust petgraph node lookups across removal. Reproduce compact graph index moves and verify stable indices without treating reusable slots as IDs.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-petgraph-node-index-lifetimesdescription: Preserve Rust petgraph node lookups across removal. Reproduce compact graph index moves and verify stable indices without treating reusable slots as IDs.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, petgraph, stable-graph, node-index, identity]Rust petgraph node index lifetimes
When to Use
- Removing one petgraph node breaks a lookup for an unrelated node or makes an old index resolve to a different node.
- A graph stores node indices in a separate map, selection, or task queue.
- An application needs to choose between compact graph storage and indices that survive removal of other nodes.
Prerequisites
- Identify the concrete graph type, petgraph version, mutation entry points, and any references kept outside the graph.
- Identify the application's own stable key and the lifetime of borrowed or cached graph indices.
- Use three distinct nodes and remove the middle one to exercise a possible compact-storage move.
1. Trace what removal invalidates
- Follow
remove_nodefrom the mutation entry point through dependent maps and caches. - A compact
Graphcan move the last node into the removed slot. Its former index becomes invalid, and the removed index can resolve to the moved node. - Graph-managed edge endpoints and graph-external index maps are separate. Check both when verifying a repair.
- Test the next insertion, too. A free slot can be reused. A stale numeric index may resolve successfully even though the original node has gone.
2. Keep graph positions within their lifetime
- When retained nodes must keep their indices through deletions, consider
StableGraphorStableDiGraph. Removing a node invalidates that node's index and preserves other live node indices. - Invalidate the application-key binding when removing a node. Keep additions, removals, and graph lookups in the same owner so readers see a coherent mapping.
- Retain an application ID for delayed messages, persisted references, and UI state. Resolve it against the current graph when consumed. When the contract needs to distinguish successive occupants of one slot, carry and check a generation or a new application ID.
- If compact storage is required, update or rebuild affected mappings in the same mutation boundary. Verify the new indices and any caches derived from them.
3. Run a removal and reinsertion regression
- Insert different labels, retain indices, and include an edge unaffected by the removal.
- Remove a middle node and check the removed application ID, the retained nodes, and the unaffected edge. Assert node identity as well as successful resolution.
- Insert a replacement. Verify that a stale handle cannot resolve the deleted identity through the public lookup, even if the graph reuses its index.
- Exercise the caller that consumes stored references. Test edge identifiers separately when callers keep them outside the graph.
Example
Remove a node with external lookups reproduces a moved node in a compact graph. It switches the graph representation and verifies both preservation of a retained node and reuse of the removed slot.
Limitations
- The fixture covers one directed graph and a local index map with one mutation owner. Concurrency and persisted references need their own validation.
- Slot reuse in the example is observed on petgraph 0.8.3. Model an index as valid for its live occupant rather than depending on the order in which vacant indices are reused.
- Edge handles, algorithms, and memory behavior may differ between graph types. Exercise any APIs the original representation relies on.
Safety and Side Effects
Use synthetic graph data while testing. If replacing a production graph type, account for stored node references and graph serialization at the caller boundary.
Related Skills and Documentation
- Rust Pro covers general Rust engineering workflows.
- Petgraph Graph::remove_node describes invalidation in compact storage.
- Petgraph StableGraph::remove_node describes retained-node stability.