CodeWiki icon indicating copy to clipboard operation
CodeWiki copied to clipboard

fix: LLM generates mermaid syntax that conflicts with parser delimiters

Open albertsikkema-elix opened this issue 1 month ago • 0 comments

LLM-generated Mermaid diagrams contain text that conflicts with Mermaid's parser syntax, causing 23 out of 872 generated blocks from my codebases to fail rendering (2.6%). Three categories of failure.

Problem

The Mermaid Syntax Guidelines in SKILL.md (lines 166-172) show only a trivial example (A[ComponentA] --> B[ComponentB]) and do not warn the LLM about characters Mermaid reserves as syntax.

Category 1: Parentheses/brackets in flowchart node labels (3 failures). C{msg.error()?} breaks because ( starts a new shape token. Fix: C{"msg.error()?"}.

Category 2: Special characters in sequence diagram messages (10 failures). Text after : on message arrows contains (), {}, [], ;, or , which Mermaid parses as syntax. Example: A->>B: call process(data) breaks. Fix: wrap in backticks or rephrase.

Category 3: Double-dash -- in text content (unknown count, worked around downstream). Labels and messages contain -- which Mermaid parses as an arrow. Example: |start -- stop| breaks. Fix: use an alternative like "to" or HTML entity --.

The validate_mermaid_diagrams post-step (utils.py) catches the parse errors and returns them to the LLM for retry, but the LLM's retry often reproduces the same mistake because nothing in the prompt tells it what the problem is or how to fix it.

Why it matters

2.6% of generated mermaid blocks fail rendering. Each failure shows a parse-error overlay in the browser and VS Code preview, hiding the documentation content. The retry loop wastes tokens and often does not fix the issue.

How to see it

Category 1 -- flowchart node labels:

flowchart TD
    A["consumer.poll(1.0)"] --> B{msg is None?}
    B -- no --> C{msg.error()?}

Categories 2 and 3 do not always fail on GitHub's renderer but produce errors in mermaid-py validation and other renderers.

Definition of done

  • The Mermaid Syntax Guidelines in SKILL.md cover all three categories with examples of bad and good syntax.
  • The same guidance is added to prompt_template.py if its mermaid instructions are separate from the skill.
  • Generated documentation for repos with function-call names, sequence diagrams, and labels containing -- produces valid Mermaid without needing retry.

Context

Mermaid's flowchart parser uses () for rounded rectangles, [] for rectangles, {} for diamonds, and "" for quoted text. Its sequence diagram parser treats (){}[];, after : as syntax. -- is an arrow in all diagram types.

Existing validation machinery

validate_mermaid_diagrams in codewiki/src/be/utils.py validates every mermaid block after the LLM writes or edits a .md file (str_replace_editor.py:931-933, caw_toolkit.py:230). It tries PythonMonkey's local parser first, falls back to mermaid-py (remote mermaid.ink), and returns the parse error to the LLM for retry via str_replace.

The validation detects these errors. But a prompt-only fix is not reliable on its own -- LLMs skip instructions. A code-level auto-fix would be a stronger second line of defence.

Solution direction

Two layers, prompt and code, working together:

Layer 1 -- Prompt fix (SKILL.md + prompt_template.py): expand the Mermaid Syntax Guidelines to cover all three categories:

## Mermaid Syntax Guidelines

Characters that conflict with Mermaid's parser:

- `()` `[]` `{}` in node labels: always quote the label
  BAD:  C{msg.error()?}     GOOD: C{"msg.error()?"}
  BAD:  D[config[key]]      GOOD: D["config[key]"]

- `()` `{}` `[]` `;` `,` in sequence diagram messages:
  wrap in backticks
  BAD:  A->>B: call process(data)
  GOOD: A->>B: call `process(data)`

- `--` anywhere in text content: Mermaid reads it as an arrow
  BAD:  |start -- stop|     GOOD: |start to stop|

Layer 2 -- Auto-fix in code: a pass over mermaid blocks that fixes known delimiter conflicts before or after validation. Two options:

  • (a) Pre-validation: always normalise before validating. The LLM never sees the error. Simplest.
  • (b) Fix-on-failure: attempt auto-fix only when validation fails with a shape-delimiter parse error, then re-validate. More conservative.

Maintainer input wanted: which combination?

  • Layer 1 alone (prompt only)
  • Layers 1 + 2a (prompt + pre-validation auto-fix)
  • Layers 1 + 2b (prompt + fix-on-failure)
  • Something else

albertsikkema-elix avatar Oct 22 '26 03:10 albertsikkema-elix