.NET: Fixing issue with invalid node Ids when visualizing dotnet workflows.#4269
Open
alliscode wants to merge 5 commits intomicrosoft:mainfrom
Open
.NET: Fixing issue with invalid node Ids when visualizing dotnet workflows.#4269alliscode wants to merge 5 commits intomicrosoft:mainfrom
alliscode wants to merge 5 commits intomicrosoft:mainfrom
Conversation
Fix two bugs in the Mermaid diagram output: 1. Use safe node aliases (node_0, node_1, ...) instead of raw executor IDs as Mermaid node identifiers. Raw IDs containing spaces, dots, or non-ASCII characters (e.g. Japanese) caused Mermaid parse errors. 2. Fix conditional edge arrow syntax from '.--> ' (invalid) to '.-> ' (valid Mermaid dotted arrow syntax). Fixes microsoft#1406 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…e generic node_0/node_1 aliases with IDs derived from the original\nexecutor names. ASCII letters, digits, and underscores are preserved;\nother characters become underscores (collapsed, trimmed). Leading digits\nget an n_ prefix. Collisions are resolved with a numeric suffix.\n\nThis keeps node IDs readable in the Mermaid source while the display\nlabels continue to show the full original names."
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes issue #1406 by improving the robustness of Mermaid diagram generation for workflows. The PR addresses two critical problems: (1) node identifiers containing spaces, special characters, or Unicode causing Mermaid rendering errors, and (2) incorrect conditional edge syntax using .--> instead of the valid ..-> format.
Changes:
- Introduced node ID sanitization logic that converts problematic identifiers into Mermaid-safe ASCII aliases while preserving original names in display labels
- Fixed conditional edge rendering to use correct Mermaid syntax (
-. label .->instead of-. label .-->) - Added comprehensive unit tests covering edge cases including spaces, Unicode characters, and special characters in node IDs
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs | Added SanitizeMermaidNodeId method and GetSafeId local function to generate Mermaid-safe node aliases with collision handling; updated edge rendering to use correct conditional arrow syntax |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs | Expanded test coverage with new tests for issue #1406 scenarios (spaces, Unicode, conditional syntax); updated existing tests to validate sanitized node IDs and display labels |
dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Show resolved
Hide resolved
…rd to SanitizeMermaidNodeId\n- Add safety limit (10,000) to collision resolution loop\n- Restore missing edge assertions (middle1/middle2 --> end)\n- Fix comment to show actual sanitized ID (n_1_User_input)\n- Use stricter regex in Unicode test (must start with letter/underscore)"
dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Show resolved
Hide resolved
…abels via EscapeMermaidLabel to handle quotes,\n brackets, and newlines in executor IDs\n- Fix XML doc on SanitizeMermaidNodeId to accurately describe that\n existing consecutive underscores in input are preserved\n- Restore specific edge assertion (mid --> end) in conditional edge test\n- Restore fan-in routing assertions (s1/s2 through intermediate node,\n no direct edges to t) in fan-in test"
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs
Show resolved
Hide resolved
TaoChenOSU
approved these changes
Feb 25, 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.
This pull request improves the generation of Mermaid diagrams for workflows by ensuring node identifiers are always safe for Mermaid syntax, even when workflow executor IDs contain spaces, special characters, or non-ASCII characters. It introduces a new sanitization method for node IDs, updates the diagram generation logic to use these sanitized aliases, and significantly expands unit tests to cover these cases and verify Mermaid compatibility.
Key changes include:
Mermaid Node ID Safety and Generation:
SanitizeMermaidNodeIdmethod to convert raw workflow node IDs into Mermaid-safe ASCII-only aliases, handling spaces, special characters, and non-ASCII text, and guaranteeing uniqueness with suffixes if needed. (dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs)dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs) [1] [2] [3] [4]Unit Test Improvements:
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs) [1] [2] [3] [4] [5] [6]Bug Fixes (Mermaid Syntax Compliance):
-. label .->instead of the invalid.-->). (dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs,dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs) [1] [2] [3] [4]Display Label Handling:
dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs,dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs) [1] [2]Regression and Issue Coverage:
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowVisualizerTests.cs)These changes together make Mermaid diagram output robust and compatible in all cases, preventing rendering errors due to problematic node identifiers or edge syntax.
Closes #1406