Skip to content

fix(describe): keep hyphenated Mermaid node ids whole when adapting diagram direction - #5110

Open
thejesh23 wants to merge 1 commit into
The-PR-Agent:mainfrom
thejesh23:fix/diagram-hyphenated-node-ids
Open

thejesh23 wants to merge 1 commit into
The-PR-Agent:mainfrom
thejesh23:fix/diagram-hyphenated-node-ids

Conversation

@thejesh23

@thejesh23 thejesh23 commented Nov 22, 2026 •

Copy link
Copy Markdown

User description

Summary

With pr_description.pr_diagram_direction = "adaptive" (the default), apply_diagram_direction parses the flowchart's edges and switches LR to TD when the longest chain is longer than pr_diagram_direction_threshold nodes. The edge parser reads each node id with DIAGRAM_NODE_ID_PATTERN = r'[A-Za-z0-9_]+', which keeps only the part before the first hyphen. Kebab-case ids therefore collapse into one node:

  • api-gateway --> api-auth is read as api --> api, a self-loop. _longest_diagram_chain raises CycleError, which is caught and logged at debug level, so the diagram is returned untouched.
  • Distinct ids that share a prefix (api-auth, api-users) merge, so chains are miscounted.

The net effect is that a diagram written with hyphenated ids never gets the adaptive direction: a 6-node kebab-case chain stays LR with the default threshold of 5. The same module already treats hyphens as part of a node id when it quotes labels (DIAGRAM_SQUARE_NODE_PATTERN uses [A-Za-z0-9_][A-Za-z0-9_-]*), and the prompt does not forbid them.

Repro (on main @ db4c4ad, Python 3.12)

>>> _parse_diagram_edges(['api-gateway["Gateway"] -- "routes" --> api-auth["Auth"]', 'api-auth --> db-users'])
[('api', 'api'), ('api', 'db')]
>>> apply_diagram_direction('flowchart LR\na-1 --> a-2 --> a-3 --> a-4 --> a-5 --> a-6', 'adaptive', 5)
DEBUG | Failed to adapt the diagram direction: ('nodes are in a cycle', ['a', 'a'])
'flowchart LR\na-1 --> a-2 --> a-3 --> a-4 --> a-5 --> a-6'

Fix

-DIAGRAM_NODE_ID_PATTERN = re.compile(r'[A-Za-z0-9_]+')
+DIAGRAM_NODE_ID_PATTERN = re.compile(r'[A-Za-z0-9_]+(?:-[A-Za-z0-9_]+)*')

Connectors (--, -->, -.->, ==>, ---, ...) are split out by DIAGRAM_CONNECTOR_PATTERN before this pattern runs, and it needs two or more connector characters, so only single inner hyphens reach the node-id pattern. The new pattern has no overlapping quantifiers, so it stays linear (checked on 100k-character inputs: under 3 ms).

Behaviour diff (old pattern vs new, over _parse_diagram_edges)

Unchanged: every existing TestDiagramEdgeParsing input, plus A-->B, A <--> B, A~~~B, A--oB, A-.-B, A["x"]-->B["y"]-->C["z"], A-- text-with-hyphen -->B, x- --> y, -x --> y, subgraph s-1, click A-1 call x().

Changed (only ids with an inner hyphen):

Line Before After
api-gateway --> api-auth [('api', 'api')] [('api-gateway', 'api-auth')]
a-b-->c [('a', 'c')] [('a-b', 'c')]
A & B-1 --> C-2 [('A', 'C'), ('B', 'C')] [('A', 'C-2'), ('B-1', 'C-2')]
node-1:::cls --> node-2 [('node', 'node')] [('node-1', 'node-2')]

Tests

  • TestDiagramEdgeParsing::test_hyphenated_node_ids_are_kept_whole and TestApplyDiagramDirection::test_long_chain_of_hyphenated_ids_becomes_vertical: both fail on main and pass with the change.
  • tests/unittest/test_pr_description.py: 149 passed.
  • Full unit suite on Python 3.12.13 (macOS): PYTHONPATH=. pytest -q tests/unittest -> 18719 passed, 66 skipped, 0 failed (baseline on main @ db4c4ad: 18717 passed, 66 skipped; the 2 extra are the new tests). tests/packaging/test_help_docs_package.py: 4 passed; uv lock --check: clean. Not run locally: Python 3.13/3.14, the Docker build, and scripts/review_context.
  • uv run ruff check and uvx pre-commit run --files on both changed files: clean.

AI disclosure: this change was prepared by an AI coding agent (Claude Code) and submitted with the authorization of the PR author.

🤖 Generated with Claude Code


PR Type

Bug fix, Tests


Description

  • Preserve hyphenated Mermaid node identifiers during parsing.

  • Restore adaptive direction for long hyphenated chains.

  • Add regression tests for parsing and adaptation.


Diagram Walkthrough

flowchart LR
  input["Mermaid diagram with hyphenated node IDs"]
  parser["Preserve complete node IDs"]
  chain["Calculate longest chain correctly"]
  direction["Adapt long diagrams to TD"]
  input --> parser
  parser --> chain
  chain --> direction
Loading

File Walkthrough

Relevant files
Bug fix
pr_description.py
Preserve hyphenated node IDs during diagram parsing           

pr_agent/commands/pr_description.py

  • Extend DIAGRAM_NODE_ID_PATTERN to retain single internal hyphens in
    node identifiers.
  • Document that connectors are split before identifier matching.
  • Prevent shared identifier prefixes from collapsing into merged nodes
    or self-loops.
+2/-1     
Tests
test_pr_description.py
Cover hyphenated identifiers and adaptive diagram direction

tests/unittest/test_pr_description.py

  • Verify edge parsing preserves complete hyphenated identifiers,
    including labeled nodes and edges.
  • Verify a six-node hyphenated chain changes from LR to TD under
    adaptive direction.
+8/-0     


⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 5,555 in / 313 out / 5,868 total
  • Time cost: 6.5s
  • AI calls: 1

…iagram direction

The adaptive diagram direction parser kept only the leading [A-Za-z0-9_]+ of each node id, so
kebab-case ids such as api-gateway and api-auth collapsed to the same node. That produced
self-loops (a swallowed CycleError) or merged nodes, and the direction was never adapted.
@the-pr-agent-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
⚠️ Risk level: Low
✅ Merge recommendation: No concerns found
🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 9,367 in / 128 out / 9,495 total
  • Time cost: 4.1s
  • AI calls: 1

@the-pr-agent-bot

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.


⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 5,413 in / 83 out / 5,496 total
  • Time cost: 2.9s
  • AI calls: 1

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant