fix!: Make AgentGraph traversal topological - #214
Open
mattrmc1 wants to merge 2 commits into
Open
Conversation
mattrmc1
marked this pull request as ready for review
July 29, 2026 21:54
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 3f801da. Configure here.
| ancestors[nxt] = anc | ||
| node = self.get_node(nxt) | ||
| assert node is not None | ||
| results[nxt] = fn(node, scoped(anc)) |
There was a problem hiding this comment.
Self-loop crashes scoped context
High Severity
traverse and reverse_traverse mark a node visited before building its dependency set, then treat a self-edge as a ready dependency. scoped then reads that node from results before it has been written, so any graph with a self-loop raises KeyError instead of excluding the node from its own context.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 3f801da. Configure here.
andrewklatzke
approved these changes
Jul 29, 2026
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/reverse_traverseused depth-layering / level-by-level BFS over a singleshared, accumulating context dict. That had two problems:
of its predecessors on a longer path had run.
(including unrelated parallel branches), and results were written back into the caller's
execution_context.What changed
packages/sdk/server-ai—ldai/agent_graph/__init__.pyonly (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.
reverse_traversenow 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.
execution_contextplus only that node'strue 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 dict 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) assert exact visit order and exact contextkeys 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 change to traversal order and callback context for non-linear agent graphs; any production code depending on old depth ordering or full accumulated context will behave differently.
Overview
Replaces depth-layer BFS in
AgentGraphDefinition.traverse/reverse_traversewith Kahn-style topological ordering (predecessors-first forward, descendants-first reverse, BFS discovery order for ties, explicit cycle breaks). Convergence nodes now run only after all reachable predecessors; pure cycles still visit every node with the root last on reverse.Each visitor callback gets a fresh, dependency-scoped context (initial
execution_contextplus only transitive ancestor/descendant results), stored in a private results map—no mutation of the caller’s dict and no leakage from unrelated parallel branches.Tests drop the old “every prior node in context” assertions, add G1–G6 / G2b parity vectors for exact visit order and context keys, plus determinism checks—aligning behavior with other LaunchDarkly AI SDKs. Breaking for convergent paths, cycles, and parallel graphs; linear graphs largely unchanged.
Reviewed by Cursor Bugbot for commit 3f801da. Bugbot is set up for automated code reviews on this repo. Configure here.