fix(fleet-data): cycle-safe toposort::waves#162
Merged
Conversation
fixes review item from PR feedback: a cyclic depends_on graph (hand-edited plan.json, partial write, future-schema bug) used to overflow the stack in the recursive resolver and crash fleet-plan-tree / fleet-waves. Track an in-flight visiting set; on re-entry treat the back-edge as level 0 instead of recursing. New tests cover a 2-cycle and a self-cycle.
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.
Summary
fleet-data::toposort::wavesresolves each subtask's wave level via a recursiveresolve()helper. The module doc previously warned that cycles independs_onwould overflow the stack and trusted callers to guarantee acyclic input. That was fragile for a dashboard that parsesplan.jsonstraight off disk — a hand-edited file, a partial write, or a future schema bug could crashfleet-plan-treeandfleet-wavesoutright.This patch adds an in-flight
visiting: &mut HashSet<u32>set threaded through the recursion. On entry, ifvisiting.insert(idx)returnsfalsewe're already resolving that node further up the stack, so we return0and break the back-edge. On exit (memoised or not) wevisiting.remove(&idx)so siblings are evaluated independently.Acyclic behaviour is preserved verbatim (all six original tests still pass).
Changes
resolve()signature gainsvisiting: &mut HashSet<u32>.waves()allocates a freshHashSetper outer-loop iteration and threads it in.Test plan
cargo test -p fleet-data— 68 passed, 0 failed (60 prior + 2 new + the existing toposort suite).cargo check -p fleet-waves -p fleet-plan-tree— both compile against the new signature (call-site is internal, unchanged).cycle_broken_to_level_zerotest exercises a 2-cycle (A→B→A) and asserts no panic, both nodes emitted, wave count bounded by node count.self_cycle_brokentest exercises A→A and asserts node lands in wave 1 exactly once with no overflow.