Fix WeightedNeighbors queue growth#5
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes unbounded queue growth in Graph.WeightedNeighbors by changing the weighted traversal to a bounded, hop-layer frontier approach that preserves hop-limited expansion semantics, and adds a regression test covering the shorter-path expansion under a hop cap.
Changes:
- Replaced the re-enqueueing relaxation loop in
WeightedNeighborswith a per-hop frontier (frontier→next) to bound work bymaxHops. - Updated
WeightedNeighborsdoc comment to describe the hop-layer semantics and why they matter for hop-limited reachability. - Added a regression test ensuring a shorter (lower-weight) path can still enable further expansion within a hop budget.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/graph/graph.go | Refactors WeightedNeighbors to use bounded hop-layer frontiers and avoid unbounded re-enqueueing. |
| internal/graph/graph_test.go | Adds a regression test for preserving shorter-path expansion behavior under maxHops. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/graph/graph.go (1)
340-348: Make the non-negative weight contract explicit.Keeping only the max score per node per hop is safe only if edge weights are non-negative.
Edge.Weight/AddEdgestill accept anyfloat64, so this pruning can change results if a negative weight ever slips in. Please document or validate that invariant at the API boundary.Also applies to: 363-375
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/graph/graph.go` around lines 340 - 348, Make the non-negative edge-weight contract explicit by validating and/or documenting it at the API boundary: ensure Edge.Weight and the graph mutation API (e.g., AddEdge) reject or normalize negative float64 weights and/or add a clear comment in the public API that weights must be non-negative; then update the traversal code (WeightedNeighbors and the hop-limited pruning logic referenced around the comment blocks) to either assert the invariant at start or handle negative weights safely if you choose to allow them. Reference Edge.Weight, AddEdge, and WeightedNeighbors when adding the validation/comment so future callers cannot supply negative weights that would break the per-hop best-score pruning.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@internal/graph/graph.go`:
- Around line 340-348: Make the non-negative edge-weight contract explicit by
validating and/or documenting it at the API boundary: ensure Edge.Weight and the
graph mutation API (e.g., AddEdge) reject or normalize negative float64 weights
and/or add a clear comment in the public API that weights must be non-negative;
then update the traversal code (WeightedNeighbors and the hop-limited pruning
logic referenced around the comment blocks) to either assert the invariant at
start or handle negative weights safely if you choose to allow them. Reference
Edge.Weight, AddEdge, and WeightedNeighbors when adding the validation/comment
so future callers cannot supply negative weights that would break the per-hop
best-score pruning.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6f91c999-2861-4b8a-a3c3-25d4e7f9dcbb
📒 Files selected for processing (2)
internal/graph/graph.gointernal/graph/graph_test.go
Summary
Validation
Closes #2
Summary by CodeRabbit
Refactor
Tests