fix!: Make AgentGraph traversal topological - #325
Open
mattrmc1 wants to merge 5 commits into
Open
Conversation
mattrmc1
marked this pull request as ready for review
July 29, 2026 21:54
Contributor
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.
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
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
High Risk
Breaking visit order and callback context for convergent graphs, cycles, and parallel branches; integrators relying on old BFS behavior or full accumulated context will need updates.
Overview
Breaking:
AgentGraphDefinition.TraverseandReverseTraverseno longer use BFS over one shared, mutating context. They now visit nodes in topological order (predecessors first forward; descendants first reverse, root last), with dependency-scoped callback dictionaries aligned to other LaunchDarkly AI SDKs.Forward traversal waits until all reachable predecessors have run before a node (Kahn on in-degree; ties broken by BFS discovery order). Reverse traversal does the same on out-degree and always processes the root last. On cycles, nodes still visit once with deterministic tie-breaking; pure cycles now run on reverse traverse instead of being a no-op.
Each callback gets
initialContextplus only transitive ancestor (forward) or descendant (reverse) results from private storage—no writes to the caller’s dictionary and no context from unrelated parallel branches or self-loops.Tests add canonical G1–G6 / G2b vectors for exact visit order and context keys in both directions.
Reviewed by Cursor Bugbot for commit 04bbbe9. Bugbot is set up for automated code reviews on this repo. Configure here.