Repository navigation
fix(cli): keep graph edges attached to node names with a colon #7546
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
Open
kavishkartha05
wants to merge
7
commits into
google:main
Choose a base branch
from
kavishkartha05:fix/graph-viz-colon-node-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+280
−4
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb82fb7
fix(cli): keep graph edges attached to node names with a colon
kavishkartha05 eed3bce
Merge branch 'main' into fix/graph-viz-colon-node-names
kavishkartha05 f2e36d0
test(cli): cover colon node names as edge target, source and terminal
kavishkartha05 a0d97e8
Merge branch 'main' into fix/graph-viz-colon-node-names
kavishkartha05 b10c024
fix(cli): quote tool names with a colon in the agent graph
kavishkartha05 19ff6a9
Merge branch 'main' into fix/graph-viz-colon-node-names
kavishkartha05 56dc3d6
Merge branch 'main' into fix/graph-viz-colon-node-names
kavishkartha05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for plot_workflow_graph DOT output.""" | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from google.adk.cli.utils.graph_visualization import _Digraph | ||
| from google.adk.cli.utils.graph_visualization import plot_workflow_graph | ||
| from google.adk.workflow._node_status import NodeStatus | ||
| import graphviz | ||
|
|
||
| _TOKEN = r'"(?:[^"\\]|\\.)*"|[^\s"\[]+' | ||
| _NODE_LINE = re.compile(rf'^\t({_TOKEN}) \[', re.MULTILINE) | ||
| _EDGE_LINE = re.compile(rf'^\t({_TOKEN}) -> ({_TOKEN})', re.MULTILINE) | ||
|
|
||
|
|
||
| def _workflow_info( | ||
| nodes: list[dict[str, Any]], edges: list[tuple[str, str]] | ||
| ) -> dict[str, Any]: | ||
| return { | ||
| 'root_agent': { | ||
| 'name': 'wf', | ||
| 'graph': { | ||
| 'nodes': nodes, | ||
| 'edges': [ | ||
| {'from_node': {'name': a}, 'to_node': {'name': b}} | ||
| for a, b in edges | ||
| ], | ||
| }, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def _plot(info: dict[str, Any], state: dict[str, Any] | None = None) -> str: | ||
| dot = plot_workflow_graph(info, state, format='dot') | ||
| assert isinstance(dot, str) | ||
| return dot | ||
|
|
||
|
|
||
| def _node_line(dot: str, name: str) -> str: | ||
| return next( | ||
| line for line in dot.splitlines() if f' {name}>' in line and '[' in line | ||
| ) | ||
|
|
||
|
|
||
| def test_edges_attach_to_nodes_whose_names_contain_colons(): | ||
| """Edges to colon-named nodes hit the real nodes, not phantom ports. | ||
|
|
||
| The real name must stay the DOT id because adk-web reads the SVG title as | ||
| the node name for click handling. | ||
| """ | ||
| info = _workflow_info( | ||
| [ | ||
| {'name': '__START__'}, | ||
| { | ||
| 'name': 'a', | ||
| 'type': 'agent', | ||
| 'tools': [{'name': 'srv:tool', 'type': 'tool'}], | ||
| }, | ||
| {'name': 'step:two', 'type': 'function'}, | ||
| ], | ||
| [('__START__', 'a'), ('a', 'step:two')], | ||
| ) | ||
|
|
||
| dot = _plot(info) | ||
|
|
||
| declared = set(_NODE_LINE.findall(dot)) | ||
| endpoints = {end for edge in _EDGE_LINE.findall(dot) for end in edge} | ||
| assert {'"srv:tool"', '"step:two"'} <= declared | ||
| assert endpoints <= declared | ||
| assert not {'srv', 'step'} & endpoints | ||
|
|
||
|
|
||
| def _assert_colon_node_is_wired(dot: str, edge: str) -> None: | ||
| """Checks `edge` is present and the colon node "b:x" has no phantom port.""" | ||
| declared = set(_NODE_LINE.findall(dot)) | ||
| endpoints = {end for pair in _EDGE_LINE.findall(dot) for end in pair} | ||
| assert '"b:x"' in declared | ||
| assert endpoints <= declared | ||
| assert 'b' not in endpoints | ||
| assert f'\t{edge}' in dot | ||
|
|
||
|
|
||
| def test_workflow_node_with_colon_can_be_an_edge_target(): | ||
| """An edge into a node named "b:x" lands on the declared node.""" | ||
| info = _workflow_info( | ||
| [ | ||
| {'name': 'a', 'type': 'agent'}, | ||
| {'name': 'b:x', 'type': 'function'}, | ||
| ], | ||
| [('a', 'b:x')], | ||
| ) | ||
|
|
||
| _assert_colon_node_is_wired(_plot(info), 'a -> "b:x"') | ||
|
|
||
|
|
||
| def test_workflow_node_with_colon_can_be_an_edge_source(): | ||
| """An edge out of a node named "b:x" starts at the declared node.""" | ||
| info = _workflow_info( | ||
| [ | ||
| {'name': 'b:x', 'type': 'function'}, | ||
| {'name': 'c', 'type': 'agent'}, | ||
| ], | ||
| [('b:x', 'c')], | ||
| ) | ||
|
|
||
| _assert_colon_node_is_wired(_plot(info), '"b:x" -> c') | ||
|
|
||
|
|
||
| def test_workflow_terminal_node_with_colon_is_linked_to_end(): | ||
| """A terminal node named "b:x" gets its edge to END from the real node.""" | ||
| info = _workflow_info( | ||
| [ | ||
| {'name': 'a', 'type': 'agent'}, | ||
| {'name': 'b:x', 'type': 'function'}, | ||
| ], | ||
| [('a', 'b:x')], | ||
| ) | ||
|
|
||
| dot = _plot(info) | ||
|
|
||
| _assert_colon_node_is_wired(dot, '"b:x" -> __END__') | ||
| assert '\t__END__ [label=END' in dot | ||
|
|
||
|
|
||
| def test_digraph_edge_hook_quotes_colon_endpoints(): | ||
| """The graphviz hook that keeps colon names whole still exists and works.""" | ||
| assert hasattr(graphviz.Digraph, '_quote_edge'), ( | ||
| 'graphviz renamed Digraph._quote_edge; update _Digraph in' | ||
| ' graph_visualization.py' | ||
| ) | ||
|
|
||
| dot = _Digraph() | ||
| dot.edge('a', 'srv:tool') | ||
|
|
||
| assert 'a -> "srv:tool"' in dot.source | ||
|
|
||
|
|
||
| def test_start_and_end_nodes_keep_their_identifiers(): | ||
| """START and END keep the __START__ and __END__ identifiers.""" | ||
| info = _workflow_info( | ||
| [{'name': '__START__'}, {'name': 'a', 'type': 'agent'}], | ||
| [('__START__', 'a')], | ||
| ) | ||
|
|
||
| dot = _plot(info) | ||
|
|
||
| assert '\t__START__ [label=START' in dot | ||
| assert '\t__START__ -> a' in dot | ||
| assert '\t__END__ [label=END' in dot | ||
| assert '\ta -> __END__' in dot | ||
|
|
||
|
|
||
| def test_tools_are_injected_as_dashed_nodes_with_edges_from_their_agent(): | ||
| """A node's tools show up as dashed tool nodes linked from the node.""" | ||
| info = _workflow_info( | ||
| [{'name': 'a', 'type': 'agent', 'tools': [{'name': 'search'}]}], [] | ||
| ) | ||
|
|
||
| dot = _plot(info) | ||
|
|
||
| assert 'rounded,filled,dashed' in _node_line(dot, 'search') | ||
| assert '\ta -> search [' in dot | ||
| assert 'style=dashed' in dot | ||
|
|
||
|
|
||
| def test_completed_node_is_filled_with_completed_status_color(): | ||
| """A completed node is filled with the completed color, others are not.""" | ||
| info = _workflow_info( | ||
| [ | ||
| {'name': 'a', 'type': 'agent'}, | ||
| {'name': 'b', 'type': 'agent'}, | ||
| ], | ||
| [('a', 'b')], | ||
| ) | ||
| state = {'nodes': {'a': {'status': NodeStatus.COMPLETED.value}}} | ||
|
|
||
| dot = _plot(info, state) | ||
|
|
||
| assert 'fillcolor="#16A34A"' in _node_line(dot, 'a') | ||
| assert 'fillcolor="#16A34A"' not in _node_line(dot, 'b') |
Oops, something went wrong.
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.
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.
Overriding the private
_quote_edgemethod ofgraphviz.Digraphintroduces a dependency on the internal implementation details of thegraphvizlibrary. While you have mitigated this risk with a unit test (test_digraph_edge_hook_quotes_colon_endpoints), private methods (those prefixed with an underscore) are not part of the public API and can be renamed, removed, or have their signatures changed in future minor or patch releases ofgraphvizwithout warning.\n\nAn alternative, more robust approach that avoids subclassing and overriding private methods is to bypassdot.edge()and append the formatted edge strings directly todot.body(which is a public, documented list of lines for the graph body).\n\nFor example, you could define a helper function:\n\npython\ndef _add_edge(\n dot: graphviz.Digraph,\n from_node: str,\n to_node: str,\n label: str | None = None,\n style: str | None = None,\n color: str | None = None,\n) -> None:\n from_quoted = quoting.quote(from_node)\n to_quoted = quoting.quote(to_node)\n attrs = []\n if label:\n attrs.append(f\"label={quoting.quote(label)}\")\n if style:\n attrs.append(f\"style={quoting.quote(style)}\")\n if color:\n attrs.append(f\"color={quoting.quote(color)}\")\n \n attr_str = f\" [{', '.join(attrs)}]\" if attrs else \"\"\n dot.body.append(f'\\t{from_quoted} -> {to_quoted}{attr_str}')\n\n\nAnd then use standardgraphviz.Digraphand replacedot.edge(...)calls with_add_edge(dot, ...). This completely eliminates the need for subclassing, thetype: ignore[misc]comment, and the fragile private method override.Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks. I considered the
dot.bodyroute. It's public API, but it needsquoting.attr_listand an additional manual trailing newline and replaces all threeedge()call sites, so it changes output paths for graphs without colon names. The subclass keeps those byte-identical (checked on 12 scenarios), andtest_digraph_edge_hook_quotes_colon_endpointsfails with an explicit message ifgraphvizrenames or changes the hook (verified on 0.20.2 and 0.21). Happy to switch to thedot.bodyapproach if maintainers prefer avoiding the private hook.