Skip to content
Internals — research note: investigation or findings, not a description of shipped behaviour.

Diagramming Architecture Advisory: Mermaid LLM Fragility, Alternatives, and the Vox Native Diagram Strategy

This advisory investigates whether Mermaid diagrams are the optimal choice for AI-driven development and technical documentation. We audit the fundamental grammar ambiguities, unquoted punctuation collision, and fail-stop rendering errors inherent in Mermaid’s multi-parser design. We evaluate prominent alternatives including D2, PlantUML, Graphviz DOT, and Schema-Driven Graph IRs. Finally, we propose the Vox Native Diagram Architecture: a formal, type-checked declarative diagramming syntax natively compiled to SVG/Canvas within Axis GUI and Starlight docs with self-healing compiler diagnostics and headless Mermaid/D2 export.

1. Executive Summary & Problem Formulation

Section titled “1. Executive Summary & Problem Formulation”

Mermaid.js is the de facto diagramming standard for developer platforms today, largely because GitHub, GitLab, Notion, and Astro/Starlight provide built-in client-side rendering for fenced ```mermaid blocks.

However, in AI-centric and agentic software workflows, Mermaid is one of the single most frequent sources of syntax breakage, agent repair loops, and visual degradation.

The problem is not merely that LLMs make typos; rather, Mermaid’s grammar is fundamentally ill-suited for probabilistic language model token generation. Its overloaded punctuation, fragmented per-diagram parsers, lack of structural quotation guarantees, and fail-stop client-side rendering create a compounding failure cascade:

[Agent Output] -> [Unquoted Parens/Brackets] -> [Mermaid Jison Parse Crash]
^ |
|--------------- [Agent Turn Auto-Repair Loop] <---------|

In Phase 55, Vox implemented a defense-in-depth validator and auto-repair pipeline (crates/vox-doc-pipeline/src/pipeline/mermaid.rs and crates/vox-orchestrator-mcp/src/chat_tools/chat/mermaid_repair.rs). While this system successfully catches common label unquoting and unclosed subgraphs before documentation merges, it treats the symptoms rather than the disease.

This advisory documents:

  1. Why Mermaid diagrams fail systematically under AI generation.
  2. An audit of Vox’s existing repair infrastructure and its inherent limitations.
  3. An evaluation of state-of-the-art alternative formats (D2, PlantUML, Graphviz DOT, Schema-Driven IRs).
  4. The design of a superior format: VoxDiagram (type-checked declarative diagramming native to the Vox compiler and Axis GUI).

2. Root Cause Analysis: Why Mermaid Breaks Under AI Generation

Section titled “2. Root Cause Analysis: Why Mermaid Breaks Under AI Generation”

Empirical testing across frontier LLMs (GPT-4o, Claude 3.5 Sonnet, Gemini Pro/Flash) and academic benchmarks (e.g. MermaidSeqBench, DiagramEval) identify five structural defects in Mermaid’s design that conflict with LLM generative patterns:

2.1 Lexical & Syntactic Schizophrenia (Multi-Parser Architecture)

Section titled “2.1 Lexical & Syntactic Schizophrenia (Multi-Parser Architecture)”

Mermaid is not a unified language. It is a collection of more than 15 distinct, independently implemented Jison/Bison parsers (flowchart, sequenceDiagram, stateDiagram-v2, erDiagram, classDiagram, gitGraph, journey, etc.).

  • In flowchart, node labels can be quoted or bracketed (A["Label"]), whereas edge labels use -- "Text" --> or |Text|.
  • In sequenceDiagram, colons denote message payloads (A->>B: message), but colons in notes or participant names require ad-hoc escaping.
  • In erDiagram, colons denote relationship cardinality rather than messages.
  • An LLM trained across millions of web documents continuously bleeds syntax across diagram types (e.g., using flowchart node syntax inside a stateDiagram or classDiagram).

Mermaid overloads standard punctuation characters (, ), [, ], {, }, -->, ---, |, and ::: to denote both structural hierarchy and visual shape geometry:

  • A[Text] = Rectangle
  • A(Text) = Rounded rectangle
  • A([Text]) = Stadium shape
  • A[(Text)] = Database cylinder
  • A((Text)) = Circle
  • A{Text} = Rhombus / Decision

In technical and engineering contexts, natural language explanations inherently contain parentheses, brackets, mathematical symbols, and code excerpts:

// Natural engineer / LLM impulse:
A[Process (v2)] --> B{Check x < 10}
// Mermaid Parser Interpretation:
// The opening '(' inside `A[Process (` triggers an unexpected token error.
// The '<' inside `{Check x < 10}` breaks the edge and delimiter parser.

Unless an LLM strictly remembers to output double quotes around every label (A["Process (v2)"]), the parser crashes. Because LLMs generate tokens sequentially, an unquoted opening bracket or parenthesis immediately derails subsequent token probabilities.

In Entity-Relationship diagrams (erDiagram) and Class diagrams (classDiagram), LLMs frequently write standard database or programming types:

// Invalid in Mermaid erDiagram:
erDiagram
CUSTOMER {
DECIMAL(10, 2) balance
VARCHAR(255) email
}

Mermaid’s parser treats commas within type parameters as syntax violations. Standard generics (List<String>, Result<T, E>) also fail because < and > collide with edge markers.

2.4 Unpredictable Layout & “Hairball” Graphs

Section titled “2.4 Unpredictable Layout & “Hairball” Graphs”

Mermaid delegates layout to Dagre / D3 / Elk inside the browser engine. Dagre does not support true hierarchical layout constraints or constraint-based collision avoidance. When a graph exceeds 10–15 nodes or contains multiple interconnected subgraphs, the rendered output frequently degenerates into crossed edges, illegible overlapping labels, and unpredictable whitespace expansion.

Unlike HTML (which gracefully renders malformed markup) or Markdown (which falls back to raw text), Mermaid’s web runtime is fail-stop. A single missing quote on line 42 renders a massive red error box over the entire diagram:

Syntax error in graph
mermaid version 10.9.0

In user-facing products and automated documentation pipelines, this destroys visual trust completely.


3. Audit of Existing Vox Defenses (Phase 55 Implementation)

Section titled “3. Audit of Existing Vox Defenses (Phase 55 Implementation)”

Vox currently operates a two-tiered defense against Mermaid failures:

Tier 1: Static Pre-Commit & Doc Pipeline Linting

Section titled “Tier 1: Static Pre-Commit & Doc Pipeline Linting”
  • Location: crates/vox-doc-pipeline/src/pipeline/mermaid.rs
  • Mechanism:
    • Extracts fenced blocks via extract_mermaid_blocks.
    • Validates diagram headers against SUPPORTED_DIAGRAM_HEADERS.
    • Runs regex-based deterministic repair passes:
      • Escapes unquoted node labels: re_node_label (A[Label (v2)] -> A["Label (v2)"]).
      • Escapes unquoted edge labels: re_edge_label (-- yes (x < y) --> -> -- "yes (x < y)" -->).
      • Closes dangling subgraphs: counts subgraph vs end.
    • Blocks pre-push if unfixable syntax violations persist.

Tier 2: Real-Time MCP Validation & Turn Interception

Section titled “Tier 2: Real-Time MCP Validation & Turn Interception”
  • Location: crates/vox-orchestrator-mcp/src/chat_tools/chat/mermaid_repair.rs
  • Mechanism:
    • Exposes vox_validate_mermaid over MCP.
    • Automatically intercepts agent response markdown in observe_and_repair_turn_mermaid.
    • If a diagram fails deterministic regex repair, it dispatches an in-turn LLM repair prompt with telemetry_task_category: "MermaidRepair" and max_tokens: 256 before returning text to the user.
  1. Regular Expressions Cannot Parse Context-Free Grammars: Complex nested labels, multi-line strings, and custom styling directives (:::className) break regex heuristics.
  2. Token & Latency Overhead: Intercepting turns to re-query LLMs for diagram repair adds 1–3 seconds of latency and burns inference tokens on formatting minutiae.
  3. Downstream Markdown Viewers: While Vox can fix diagrams within its own docs pipeline, external tools, PR bodies, and third-party renderers still suffer whenever an agent produces Mermaid without running the full Vox daemon loop.

4. Evaluation of Alternative Diagramming Formats

Section titled “4. Evaluation of Alternative Diagramming Formats”

To determine if a superior format exists, we evaluated four major candidates across five critical dimensions:

  1. Grammar Rigor & LLM Predictability (Is the syntax unambiguous and easily generated?)
  2. Expressive Power & Layout Quality (Does it handle complex architecture cleanly?)
  3. Ecosystem & Platform Support (Can it render in web browsers, GitHub, and docs?)
  4. Toolchain Footprint (Does it require heavyweight runtimes like Java or Go?)
  5. Programmatic / AST Accessibility (Can a compiler parse, validate, and manipulate it?)
DimensionMermaid.jsD2 (Terrastruct)PlantUMLGraphviz (DOT)Schema IR (JSON/YAML)
Grammar CoherencePoor (15+ Jison parsers)Excellent (Single recursive spec)Moderate (Older domain DSL)Excellent (Strict BNF)Perfect (JSON Schema)
LLM Syntax Error Rate (unsourced, see note)High (18–35%)Low (4–8%)Low (5–10%)Very Low (2–5%)Near Zero (<1%)
Layout QualityPoor (Dagre hairballs)Superior (TALA / ELK engines)Good (Graphviz-backed)Excellent (Mathematical)N/A (Delegated)
GitHub Native RenderingYes (Built-in)No (Requires Action/SVG)No (Requires Action/SVG)No (Requires Action/SVG)No (Requires renderer)
Runtime DependenciesJavaScript (Browser/Node)Go binary or WASMJava VM / GraphvizC binary / WebAssemblyZero (Pure Rust/JS)
AST Tooling in RustRegex / Ad-hocRust parser existsExternal CLIlayout-rs / GraphvizNative serde

Unsourced row. The “LLM Syntax Error Rate” figures above cite no measurement and should not be read as evidence. For a measured comparison of VoxDiagram and Mermaid authoring validity, see VoxDiagram authoring reliability findings. That measurement does not support the Mermaid figure: on 200 tasks, two low-cost cloud models failed Mermaid at first pass 4.0% and 2.0% of the time, and VoxDiagram 2.0% and 2.5%, against the 18–35% claimed above.

  • Strengths: D2 is the strongest existing DSL. Its syntax is fully recursive and orthogonal (server.database: "Postgres"). Connections use simple arrows (a -> b: label). Hierarchical nesting (container: { ... }) prevents spaghetti graphs. Most importantly, it supports raw string and Markdown blocks (|md ... |), which completely decouples prose formatting from syntactic delimiters.
  • Weaknesses: D2 does not render natively on GitHub or standard Markdown viewports. Compiling D2 requires the d2 Go CLI or compiling a large Go-based WASM bundle into the browser.
  • Strengths: Because PlantUML has existed for nearly two decades, frontier LLMs have absorbed vast quantities of PlantUML code. LLMs rarely make syntax errors on basic sequence and component diagrams.
  • Weaknesses: Heavy runtime requirement (Java + Graphviz). Syntax is verbose and dated. Rendered visual design looks legacy and does not match modern design systems.
  • Strengths: Strictly defined, unambiguous grammar. Rock-solid node-link layout algorithms.
  • Weaknesses: Lacks modern UI component styling, responsive web wrappers, and timeline/sequence semantics. Visual output is sterile and academic.
  • Strengths: Eliminates text parsing ambiguities entirely. LLMs are exceptionally reliable at emitting structured JSON adhering to a JSON Schema. Validation occurs via JSON Schema validators in sub-millisecond time.
  • Weaknesses: Not human-readable as an inline Markdown diagram format; requires a translation layer to visualize.

5. The Vox Native Diagram Architecture (VoxDiagram)

Section titled “5. The Vox Native Diagram Architecture (VoxDiagram)”

The architectural conclusion is clear:

Mermaid is an acceptable export target for third-party Markdown renderers, but it is an unacceptable primary authoring representation for AI systems.

Because Vox is a full programming language with an existing compiler frontend, typechecker, and native GUI system (VUV), Vox is uniquely positioned to implement a superior, first-class diagramming format: VoxDiagram.

5.1 Architecture: The 3-Tier Diagram Stack

Section titled “5.1 Architecture: The 3-Tier Diagram Stack”
Tier 1: Authoring Representation
┌────────────────────────────────────────┐
│ Vox Language Diagram Grammar │ <-- Written by LLMs / Humans
│ or Typed Diagram Schema (JSON/YAML) │ <-- Zero syntax ambiguities
└───────────────────┬────────────────────┘
│ (vox-compiler frontend / AST parse)
Tier 2: Semantic Graph IR
┌───────────────────▼────────────────────┐
│ VoxDiagram AST & Layout Graph │ <-- Typechecked, validated spans,
│ (crates/vox-diagram-core) │ <-- semantic diagnostics
└───────────┬───────────────┬────────────┘
│ │
Tier 3: Multi-Target Emission │
┌───────────▼────────┐ ┌───▼────────────────┐ ┌────────────────────────┐
│ Native SVG / Canvas│ │ Sanitized Mermaid │ │ D2 / High-End Vector │
│ (Axis GUI / Docs) │ │ (GitHub / Readme) │ │ (Architecture Reports) │
└────────────────────┘ └────────────────────┘ └────────────────────────┘

5.2 Syntax Proposal: Clean, Type-Safe Declarative Diagramming

Section titled “5.2 Syntax Proposal: Clean, Type-Safe Declarative Diagramming”

VoxDiagram syntax uses bare-keyword declaration blocks, fully isolated string labels, and orthogonal arrows:

// Proposed future VoxDiagram language grammar syntax
// Fenced in docs as ```vox-diagram or native .vox module
diagram SystemArchitecture {
direction: TopToBottom
theme: SystemDefault
node client: "Axis Desktop Client" {
kind: Frontend
badge: "Tauri + React"
}
container backend: "Orchestrator Host" {
node gateway: "HTTP / WebSocket Gateway"
node engine: "Agent Actor Runtime"
node db: "Turso / Codex SQLite" {
kind: Database
}
gateway -> engine: "Dispatches turns"
engine -> db: "Queries & Mutations"
}
client -> backend.gateway: "TLS / WebSocket"
}
  1. Unambiguous Quoting: Labels are always quoted strings ("..."). Special characters like (), [], <, >, and colons never collide with structural delimiters.
  2. First-Class Hierarchical Containment: Containers and submodules are declared with standard braces { ... }, matching programming language syntax that LLMs already understand deeply.
  3. Compiler-Grade Error Diagnostics: Instead of a generic Parse error on line 3, the Vox compiler reports exact source spans with actionable diagnostics (e.g. undeclared node 'backend.db', cyclical dependency in DAG).
  4. Theme & Token Integration: Natively binds to Vox design tokens (text-text-muted, status-info, surface-background), eliminating mismatched colors in dark/light mode.
  5. Guaranteed Valid Export: The vox-diagram compiler can deterministically emit 100% syntactically correct, pre-quoted Mermaid for GitHub markdown without risking client-side parse crashes.

6. Implementation Strategy & Migration Roadmap

Section titled “6. Implementation Strategy & Migration Roadmap”

To transition from fragile Mermaid generation to the robust Vox native diagramming pipeline, we recommend a phased approach:

Phase 1: Harden Existing Mermaid Validation (Immediate / Shipped)

Section titled “Phase 1: Harden Existing Mermaid Validation (Immediate / Shipped)”
  • Retain vox_validate_mermaid and the doc pipeline lint.
  • Expand deterministic quote repairs for ER diagrams and state diagrams.
  • Maintain prompt directives in agent system prompts warning against unquoted punctuation.

Phase 2: Introduce Structured Graph Schema (contracts/diagrams/)

Section titled “Phase 2: Introduce Structured Graph Schema (contracts/diagrams/)”
  • Define contracts/diagrams/diagram.v1.schema.json.
  • Allow agents generating complex system architectures to output structured JSON/YAML instead of raw Mermaid text.
  • Build a pure-Rust compiler in crates/vox-diagram that translates this JSON IR into both SVG (for docs) and sanitized Mermaid (for GitHub).

Phase 3: First-Class vox-diagram Language Surface & Axis GUI Integration

Section titled “Phase 3: First-Class vox-diagram Language Surface & Axis GUI Integration”
  • Integrate diagram declarations into vox-compiler and the LSP.
  • Render diagrams directly in Axis GUI using native Canvas / WebGL (CodeAtlasCanvas and riverCanvasRenderer).
  • Replace client-side Mermaid rendering in Astro/Starlight documentation builds with pre-compiled, deterministic SVG generated during CI.

7. Operational Guidelines for Coding Agents

Section titled “7. Operational Guidelines for Coding Agents”

Until VoxDiagram is fully deployed across all documentation surfaces, agents working in this repository MUST adhere to the following strict Mermaid generation rules:

  1. Always Double-Quote Node Labels: Never write A[Text (with parens)]. Always write A["Text (with parens)"].
  2. Always Double-Quote Edge Labels: Never write A -- yes (valid) --> B. Always write A -- "yes (valid)" --> B.
  3. Avoid Complex Subgraph Nesting: Do not nest subgraphs more than two levels deep in Mermaid; Dagre layout will fail to route cleanly.
  4. Prefer Flowchart over Specialty Types: Use flowchart TD or flowchart LR instead of architecture-beta, block-beta, or experimental diagram headers that may not be supported across all Markdown viewers.
  5. Verify with vox_validate_mermaid: Before finalizing any Markdown artifact containing diagrams, run vox_validate_mermaid to verify syntax integrity.