fix!: Make AgentGraph traversal topological - #325
Conversation
There was a problem hiding this comment.
There are a lot of new dictionary calls, toDictionary, as well as several contains and order by. 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.
I'm guessing this would apply to the other PRs for the other SDKs if they are following similar implementations.
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 6ba4a7f. Configure here.
| var (reachable, order) = ReachableAndDiscovery(rootKey); | ||
|
|
||
| var outdeg = reachable.ToDictionary( | ||
| k => k, k => GetNode(k).Edges.Count(e => reachable.Contains(e.Key))); |
There was a problem hiding this comment.
Reverse outdeg waits on root
Medium Severity
ReverseTraverse counts edges to the root in outdeg, but the root is visited only after the loop, so those counts are never released. Nodes that point at the root therefore stay blocked and are chosen via cycle-break, which can run them before their real non-root descendants and omit those descendants from scoped context. Traverse already special-cases this dual with indeg[root] = 0.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 6ba4a7f. Configure here.


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 contextdictionary. 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
initialContext.What changed
pkgs/sdk/server-ai—Graph/AgentGraphDefinition.csonly (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.
initialContextplus only thatnode's true dependency results (transitive ancestors forward / descendants reverse). Results are
kept in a private map keyed by node; unrelated branches and self-loops are excluded, and the
caller's dictionary is never mutated. Cross-node data flows only through callback return values.
order), used only for tie-breaks. Ready-check uses
== 0to match the reference algorithm and theother SDKs.
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; self-loop excluded from its own context; deterministic across
runs.
Notes
CHANGELOG.mdedit — release-please generates it from the conventional-commit title.Note
Overview
TraverseandReverseTraverseonAgentGraphDefinitionno longer use plain BFS over one shared, mutating context dictionary. They now visit reachable nodes in topological order (predecessors first forward; descendants first backward, root last on reverse), with cycle-safe tie-breaking from BFS discovery order.Each callback gets a scoped dictionary:
initialContextplus only that node’s true dependency results (transitive ancestors or descendants). Node outputs live in a private map; unrelated parallel branches and self-loops are excluded, and the caller’sinitialContextis not mutated.Breaking: visit order and context keys change for convergent paths, cycles, and parallel branches; linear chains stay the same. Aligns traversal with other LaunchDarkly AI SDKs.
Tests add canonical G1–G6 / G2b vectors asserting exact order and context keys in both directions, plus updated cycle and scoped-context expectations.
Reviewed by Cursor Bugbot for commit 12826ec. Bugbot is set up for automated code reviews on this repo. Configure here.