-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(core): handle subgraph nesting better in graph_mermaid #7907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(core): handle subgraph nesting better in graph_mermaid #7907
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
Nice catch, thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense - CC @nfcampos
"Please adjust your graph to have subgraph nodes with unique names." | ||
); | ||
|
||
if (seenSubgraphs.has(prefix)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably still throw in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point; I restored this, and also needed to add one more protection to the recursion to prevent reaching deep paths twice
mermaidGraph += `\tsubgraph ${subgraph}\n`; | ||
} | ||
|
||
// all nested prefixes for this level, sorted by depth | ||
const nestedPrefixes = sortPrefixesByDepth( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will need to take a closer look, but shouldn't all prefixes passed into sortPrefixesByDepth
have the same length?
Currently,
graph_mermaid
can throw an error when subgraphs are deeply nested, due to its way of tracking subgraph names and its order of processing edges. We encountered this in production with a large graph which had a couple of levels of subgraphs and many edges.See the nodes/edges fixture added in
graph_mermaid.test.ts
, which is a minimal reproduction of one part of our graph, and will throw the following error on main branch:The graph (and original langgraph code that generated it) is valid, and doesn't have a duplicate subgraph, but the subgraph/prefix handling misrecognizes it as duplicate when traversing and tracking the set of subgraphs.
This PR fixes that, and can now render the graph correctly; and also pre-orders the edges so that subgraphs will appear in their correct nesting, which wasn't necessarily the case before.
Without the additional edge reordering, the test graph here was not properly nested:

due to incorrect ordering of subgraphs (excerpt):
With the fix, it is, thanks to ordering edges by depth before traversal:

I was able to test this rendering on our very large application graph and there were no unintended side effects / regressions; would be happy to have even more test cases for example graphs if needed--this particular script had little in the way of existing tests.