fix!: Make AgentGraph traversal topological - #199
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit da3503b. Configure here.
| for (GraphEdge e : node.getEdges()) { | ||
| if (reachable.contains(e.getKey())) { | ||
| d++; | ||
| } |
There was a problem hiding this comment.
Reverse outdeg counts root edges
High Severity
reverseTraverse counts outgoing edges to the root in outdeg, but the Kahn loop never visits the root, so those edges are never satisfied. Nodes that only point at the root (or whose remaining degree is inflated by a back-edge to root) stay non-ready and get picked via cycle-break too early, before real descendants, so visit order and scoped dependency context are wrong for cycles that include the root.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit da3503b. Configure here.
There was a problem hiding this comment.
Do you have any performance requirements wrt throughput or memory allocation delays? May be worth a quick benchmark to see if your expected cases are well behaved.


Summary
Makes
AgentGraphDefinitiontraversal topological and gives each visitor a dependency-scopedexecution context, aligning the traversal behavior across the LaunchDarkly AI SDKs.
Previously
traverse/reverseTraversewere plain BFS over a single shared, accumulating contextmap. That had two problems:
discovery — before all of its predecessors had run.
(including unrelated parallel branches), and results were written back into the caller's context
map.
What changed
lib/sdk/server-ai—AgentGraphDefinition.javaonly (no public API/signature changes):traversenow visits a node only after all of its reachable predecessors have been visited(Kahn over in-degree; the root is always released first). On cycles, the unvisited node with the
lowest remaining in-degree is chosen next, ties broken by discovery order.
reverseTraversenow visits a node only after all of its reachable descendants have beenvisited, so the root is visited last (Kahn over out-degree, root excluded from cycle-break
selection). Pure cycles now visit every node instead of being a no-op.
true dependency results (transitive ancestors forward / descendants reverse). Dependencies are
accumulated before the node is marked visited in both directions, so a self-loop node never
includes itself; unrelated branches are excluded and the caller's map is never mutated. Cross-node
data flows only through callback return values.
order), used only for tie-breaks.
Behavior change (breaking)
Titled
fix!because visit order and callback-context contents change for graphs with convergentpaths, cycles, or parallel branches. Simple linear graphs are unaffected. Cross-SDK parity with
JS/Python/.NET/Java.
Tests
G1–G6(+G2b) asserts exact visitorder and exact context keys in both directions.
mutated; unrelated branches excluded; a dedicated self-loop test confirms a node is not in its own
context; deterministic across runs. Redundant per-vector order-only tests were dropped in favor of
the data-driven vector test.
Notes
CHANGELOG.mdedit — release-please generates it from the conventional-commit title.updating to "topological / dependency-order" so the docs match the new behavior.
Note
Medium Risk
Same public method signatures but visit order and callback context contents change for convergent graphs, cycles, and parallel branches—callers that relied on BFS order or writing results into the shared map will break.
Overview
Breaking behavior change for
AgentGraphDefinition.traverseandreverseTraverse: they no longer use shared BFS over one accumulating context map. Forward traversal now runs each node only after all reachable predecessors (Kahn-style, cycle-safe with discovery-order tie-breaks); reverse traversal runs descendants-first with the root last, including visiting all nodes on pure cycles.Each visitor callback gets a fresh map (initial
ctxplus only that node’s true dependency results—ancestors forward, descendants reverse). The caller’sctxis not mutated with node outputs; unrelated parallel branches are excluded, and self-loops do not put a node in its own context.Javadoc is updated to describe topological, deterministic traversal. Tests add canonical cross-SDK vectors (G1–G6), exact visit order and context-key assertions, determinism, and coverage for convergence, cycles, and caller-context immutability.
Reviewed by Cursor Bugbot for commit da3503b. Bugbot is set up for automated code reviews on this repo. Configure here.