Diagram Generator
Migration note: diagram contracts are v3. Consumers of v2 must replace the
singular feature-preparation evidence ref with exact
{evidence_ref, item_id} bindings at the root and for every traced node and
edge. source_refs remain the underlying code, configuration, test, or
user-provided evidence; coverage_gaps still disclose unavailable behavior.
Contracts
| File |
Purpose |
contracts/input.yaml |
diagram_type, scope, source_files[], format, optional exact feature-preparation evidence-row bindings |
contracts/output.yaml |
diagram_code, diagram_type, description, evidence sources, element trace, coverage gaps |
diagram_type and scope are required; format defaults to mermaid
diagram_code must be valid syntax parseable by the target renderer
diagram_type is echoed back in output to confirm what was generated
- Feature-preparation diagrams carry exact
{evidence_ref, item_id} bindings at the root and for every traced element, and disclose untraced elements as coverage gaps
Creates accurate Mermaid diagrams from code analysis. Covers the developer's visual documentation weakness.
Core principle: Diagrams should be generated from code, not drawn from memory.
Goal
Generate diagrams that make real code structure or behavior easier to understand without inventing components.
Success Criteria
- Diagram type, scope, evidence, placement, and gaps are explicit.
- Every node and edge is traceable to inspected code, configuration, or user-provided context.
- Mermaid output is small enough to read and structured enough to render.
Constraints
- Ask only when diagram type or scope materially changes the result and cannot be inferred from prompt/context.
- If multiple diagram types are plausible, choose the best fit from source evidence or ask every material, non-discoverable question needed to select safely; group them by decision topic.
- Do not add aspirational boxes, future architecture, or unlabeled relationships unless the user explicitly asks for a proposed design.
Available Diagram Types
| Type |
File |
Best for |
| Architecture |
arch-diagram.md |
System overview, component relationships |
| Sequence |
sequence-diagram.md |
Request flows, interaction patterns |
| Entity-Relationship |
er-diagram.md |
Data models, database schema |
| Flow |
flow-diagram.md |
Business logic, decision trees, algorithms |
| Component |
component-diagram.md |
Module boundaries, dependencies |
| Class |
class-diagram.md |
Type hierarchies, interfaces, relationships |
| State |
state-diagram.md |
State machines, lifecycle transitions |
Auto-Selection
Input arrives
│
├─ "architecture" / "system overview" → arch-diagram.md
├─ "flow" / "how does X work" → sequence-diagram.md or flow-diagram.md
├─ "data model" / "entities" / "schema" → er-diagram.md
├─ "dependencies" / "modules" → component-diagram.md
├─ "class hierarchy" / "types" → class-diagram.md
├─ "state" / "lifecycle" / "transitions" → state-diagram.md
└─ ambiguous → ask user or pick best fit
Prefer picking the best fit when the user's intent and code context point clearly to one type. Ask only when the choice would materially change what gets inspected or drawn.
General Protocol
For all diagram types:
- Read the relevant code — trace the actual paths, don't guess
- Right-size the diagram — show what matters, omit noise
- Verify accuracy — every box/arrow must correspond to real code
- Use project terminology — names from code, not generic labels
- Output as Mermaid — embeddable in markdown, renderable everywhere
Diagram Complexity Guidelines
| Project Size |
Guideline |
| Small (< 10 files) |
Single diagram can show everything |
| Medium (10-50 files) |
One overview + detail diagrams per area |
| Large (50+ files) |
Layered: L0 overview → L1 component → L2 detail |
Output
Return:
- Result - the diagram type generated and a brief description of what it shows.
- Diagram - valid Mermaid inside a fenced code block:
```mermaid
[diagram content]
```
- Evidence - code files, symbols, or paths used to derive the diagram.
- Placement - where the diagram was written, or "inline only" if no file changed.
- Gaps - any missing context, assumptions, or follow-up questions affecting accuracy.
Where to Place Diagrams
- If generating docs (via
assistant-docs): embed in the doc
- If user asks directly: output inline in conversation
- If generating architecture doc: embed in
docs/architecture.md
- For standalone diagrams:
docs/diagrams/[name].md
Mermaid Best Practices
- Keep node labels short (2-4 words)
- Use meaningful edge labels (verb phrases: "calls", "reads from", "publishes to")
- Group related nodes with
subgraph
- Use direction that reads naturally (TD for hierarchies, LR for flows)
- Limit to ~15-20 nodes per diagram — split larger ones
Rules
- Every element must exist in code — no aspirational boxes
- Don't show everything — show what helps understanding
- Label relationships — unlabeled arrows are useless
- Use consistent styling — same type of component gets same shape
- Test rendering — Mermaid syntax errors are common, validate mentally
Stop Rules
- Stop and ask every material, non-discoverable question when neither scope nor diagram type can be inferred safely; group them by decision topic and keep each concise.
- Stop and report gaps when required source files or relationships cannot be inspected.
- Do not finalize if the diagram contains elements without source evidence.
1---2name: assistant-diagrams3description: Create Mermaid architecture, sequence, ER, flow, class, or state diagrams. Use for explicit diagram or system-flow visualization requests.4---56# Diagram Generator78Migration note: diagram contracts are v3. Consumers of v2 must replace the9singular feature-preparation evidence ref with exact10`{evidence_ref, item_id}` bindings at the root and for every traced node and11edge. `source_refs` remain the underlying code, configuration, test, or12user-provided evidence; `coverage_gaps` still disclose unavailable behavior.1314## Contracts1516| File | Purpose |17|---|---|18| [`contracts/input.yaml`](contracts/input.yaml) | diagram_type, scope, source_files[], format, optional exact feature-preparation evidence-row bindings |19| [`contracts/output.yaml`](contracts/output.yaml) | diagram_code, diagram_type, description, evidence sources, element trace, coverage gaps |2021- `diagram_type` and `scope` are required; `format` defaults to mermaid22- `diagram_code` must be valid syntax parseable by the target renderer23- `diagram_type` is echoed back in output to confirm what was generated24- Feature-preparation diagrams carry exact `{evidence_ref, item_id}` bindings at the root and for every traced element, and disclose untraced elements as coverage gaps2526Creates accurate Mermaid diagrams from code analysis. Covers the developer's visual documentation weakness.2728Core principle: **Diagrams should be generated from code, not drawn from memory.**2930## Goal3132Generate diagrams that make real code structure or behavior easier to understand without inventing components.3334## Success Criteria3536- Diagram type, scope, evidence, placement, and gaps are explicit.37- Every node and edge is traceable to inspected code, configuration, or user-provided context.38- Mermaid output is small enough to read and structured enough to render.3940## Constraints4142- Ask only when diagram type or scope materially changes the result and cannot be inferred from prompt/context.43- If multiple diagram types are plausible, choose the best fit from source evidence or ask every material, non-discoverable question needed to select safely; group them by decision topic.44- Do not add aspirational boxes, future architecture, or unlabeled relationships unless the user explicitly asks for a proposed design.4546## Available Diagram Types4748| Type | File | Best for |49|---|---|---|50| **Architecture** | `arch-diagram.md` | System overview, component relationships |51| **Sequence** | `sequence-diagram.md` | Request flows, interaction patterns |52| **Entity-Relationship** | `er-diagram.md` | Data models, database schema |53| **Flow** | `flow-diagram.md` | Business logic, decision trees, algorithms |54| **Component** | `component-diagram.md` | Module boundaries, dependencies |55| **Class** | `class-diagram.md` | Type hierarchies, interfaces, relationships |56| **State** | `state-diagram.md` | State machines, lifecycle transitions |5758## Auto-Selection5960```61Input arrives62 │63 ├─ "architecture" / "system overview" → arch-diagram.md64 ├─ "flow" / "how does X work" → sequence-diagram.md or flow-diagram.md65 ├─ "data model" / "entities" / "schema" → er-diagram.md66 ├─ "dependencies" / "modules" → component-diagram.md67 ├─ "class hierarchy" / "types" → class-diagram.md68 ├─ "state" / "lifecycle" / "transitions" → state-diagram.md69 └─ ambiguous → ask user or pick best fit70```7172Prefer picking the best fit when the user's intent and code context point clearly to one type. Ask only when the choice would materially change what gets inspected or drawn.7374## General Protocol7576For all diagram types:77781. **Read the relevant code** — trace the actual paths, don't guess792. **Right-size the diagram** — show what matters, omit noise803. **Verify accuracy** — every box/arrow must correspond to real code814. **Use project terminology** — names from code, not generic labels825. **Output as Mermaid** — embeddable in markdown, renderable everywhere8384## Diagram Complexity Guidelines8586| Project Size | Guideline |87|---|---|88| Small (< 10 files) | Single diagram can show everything |89| Medium (10-50 files) | One overview + detail diagrams per area |90| Large (50+ files) | Layered: L0 overview → L1 component → L2 detail |9192## Output9394Return:95- **Result** - the diagram type generated and a brief description of what it shows.96- **Diagram** - valid Mermaid inside a fenced code block:9798````markdown99```mermaid100[diagram content]101```102````103104- **Evidence** - code files, symbols, or paths used to derive the diagram.105- **Placement** - where the diagram was written, or "inline only" if no file changed.106- **Gaps** - any missing context, assumptions, or follow-up questions affecting accuracy.107108## Where to Place Diagrams109110- If generating docs (via `assistant-docs`): embed in the doc111- If user asks directly: output inline in conversation112- If generating architecture doc: embed in `docs/architecture.md`113- For standalone diagrams: `docs/diagrams/[name].md`114115## Mermaid Best Practices116117- Keep node labels short (2-4 words)118- Use meaningful edge labels (verb phrases: "calls", "reads from", "publishes to")119- Group related nodes with `subgraph`120- Use direction that reads naturally (TD for hierarchies, LR for flows)121- Limit to ~15-20 nodes per diagram — split larger ones122123## Rules124125- **Every element must exist in code** — no aspirational boxes126- **Don't show everything** — show what helps understanding127- **Label relationships** — unlabeled arrows are useless128- **Use consistent styling** — same type of component gets same shape129- **Test rendering** — Mermaid syntax errors are common, validate mentally130131## Stop Rules132133- Stop and ask every material, non-discoverable question when neither scope nor diagram type can be inferred safely; group them by decision topic and keep each concise.134- Stop and report gaps when required source files or relationships cannot be inspected.135- Do not finalize if the diagram contains elements without source evidence.