guidance layer
Mermaid diagrams
General rules for every Mermaid diagram in this project. Read this layer only when you draw or change a diagram; no phase reads it on entry.
Mermaid is the only diagram format. Diagrams live as fenced ```mermaid blocks in Markdown, not as committed images: a text diagram is reviewable in a diff, an exported PNG is not. An agent can add one node or one edge to Mermaid without disturbing the rest, and the diff reads as the change it is.
Where diagrams land
architecture-diagram.md, in the instance's state directory, is drawn by design for one change. It is drawn only when the change adds or removes a service or a store, adds a data flow between components, or moves a boundary (account, network, tenant or trust)..aidlc/architecture/system.mdis the system as it stands: one##section per diagram. The instance that drewarchitecture-diagram.mdupdates it in its own branch, before merge, to match what was built. Edit only the diagrams the work touched; never regenerate the whole file.
Choosing the diagram type
| Diagram type | Use for | Mermaid header |
|---|---|---|
| Flowchart | Components and the data flowing between them; process or decision flow | flowchart TD (or LR) |
| Sequence | Ordered messages between actors over time | sequenceDiagram |
| State | Lifecycle or state machine | stateDiagram-v2 |
| Class | Type structure and relationships | classDiagram |
| ER | Database entities and relationships | erDiagram |
| Gantt | Schedule | gantt |
| Timeline | Chronological events | timeline |
| Mindmap | Hierarchical breakdown | mindmap |
| Journey | User journey with sentiment scores | journey |
| Pie | Proportion of categorical data | pie |
If unsure, a flowchart with action nodes is rarely wrong. Sequence is for ordered communication; state is for what state a thing can be in; ER is for data models, not behaviour.
Authoring workflow
- Draft in a standalone
.mmdfile in a scratch directory outside the repository. A fenced block inside Markdown cannot be checked directly, and a scratch.mmdleft in the tree is clutter someone has to clean up. - Write the diagram.
- Check it before pasting it anywhere — see below.
- Copy the checked contents into the target Markdown file as a fenced ```mermaid block.
Checking a diagram before commit
Checking is not optional. Markdown renderers fail Mermaid blocks silently in some setups: the
block looks fine in a local preview and breaks on the published page, and review will not catch it
because the source diff reads as plausible Mermaid. $SCRATCH below is that scratch directory,
for example SCRATCH="$(mktemp -d)".
npx -y @mermaid-js/mermaid-cli -i "$SCRATCH/diagram.mmd" -o "$SCRATCH/diagram.svg" -q
A non-zero exit means invalid syntax. Keep the SVG only if you want to look at it; it is a by-product, not an artifact to commit.
First-run cost. The Mermaid CLI drives Puppeteer, which downloads a headless Chromium on first
run (about 100 MB, one time). Where that download is blocked — CI sandboxes, restricted networks —
set PUPPETEER_EXECUTABLE_PATH to an existing Chrome or Chromium binary rather than skipping the
check.
To check every block already in a Markdown file, extract them first:
awk -v d="$SCRATCH" '/^```mermaid$/{f=1;n++;o=d"/d-"n".mmd";next} /^```$/{f=0;next} f{print > o}' doc.md
for f in "$SCRATCH"/d-*.mmd; do npx -y @mermaid-js/mermaid-cli -i "$f" -o "${f%.mmd}.svg" -q || echo "FAILED: $f"; done
Labels
Label nodes, participants and subgraphs with the project's own names for its components — the names its code, its context documents and its people use. A diagram that invents a second name for a component teaches the wrong model faster than prose does.
Syntax cheatsheets
Flowchart
flowchart TD
Start([Start]) --> Decide{Has account?}
Decide -->|Yes| Login[Login]
Decide -->|No| Signup[Signup]
Login --> Dashboard[Dashboard]
Signup --> Dashboard
Direction: TD (top-down), BT, LR, RL. Edges: --> solid, -.-> dotted, ==> thick,
~~~ invisible (for layout ordering only).
Sequence
sequenceDiagram
participant User
participant Browser
participant API
participant DB
User->>Browser: Click "Sign in"
Browser->>API: POST /auth/login
API->>DB: SELECT user WHERE email=?
DB-->>API: user row
API-->>Browser: 200 + session cookie
Browser-->>User: Dashboard
->> solid arrow, -->> dashed (response), -> open arrow, --) async.
Note over A,B: text for annotations. loop, alt/else, opt, par for control flow.
State
stateDiagram-v2
[*] --> Draft
Draft --> Submitted: submit
Submitted --> Approved: approve
Submitted --> Rejected: reject
Approved --> [*]
Rejected --> Draft: revise
[*] is the start/end pseudo-state. Use state X { ... } for nested composite states.
Class
classDiagram
class Animal {
+String name
+int age
+makeSound() void
}
class Dog {
+String breed
+bark() void
}
Animal <|-- Dog
Relationships: <|-- inheritance, *-- composition, o-- aggregation, --> association,
..> dependency.
ER
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
USER {
uuid id PK
string email
timestamp created_at
}
ORDER {
uuid id PK
uuid user_id FK
decimal total
}
Cardinality: || exactly one, }o zero or many, }| one or many, o| zero or one — mirrored on
the other side of the relation.
Gantt
gantt
title Release schedule
dateFormat YYYY-MM-DD
section Backend
API rewrite :a1, 2026-01-01, 14d
Migration :after a1, 7d
section Frontend
Component refresh:2026-01-08, 21d
Layout gotchas
- Linear chains in subgraphs sometimes render horizontally even under
flowchart TD. Force vertical withdirection TBinside the subgraph. - Long labels truncate in some renderers. Keep node and subgraph labels short and push detail into the prose around the diagram.
- HTML in labels requires the label be quoted:
id("text<br/>more text"). An unquoted label cannot contain<,>,(,)or,. - Reserved words.
end,class,styleandlinkStyleare keywords. Prefix a colliding node id (n_end, notend).
Anti-patterns
- Wrong diagram type. A flowchart of states is usually a state diagram; force-fitting it loses the implicit time semantics. A sequence diagram of components is usually a class diagram.
- All-rectangle flowcharts. Mermaid's shape vocabulary is a free communication channel — rounded boxes for processes, diamonds for decisions, stadiums for start, end and external systems, cylinders for data stores.
- Colour as the only channel. Shape must carry the same distinction colour does, per
.aidlc/guidance/accessibility.md. A reader with a colour-vision deficiency, or anyone reading a greyscale print, gets the diagram from shape alone or not at all. - Skipping the check. See above.
- Committing a rendered image beside the source. The two drift, and the image is the one readers trust. Keep the Mermaid source as the single definition.
- Regenerating the living document. Redrawing
.aidlc/architecture/system.mdwhole turns a one-edge change into a diff nobody reads. Edit the diagrams the work touched.
References
- Mermaid syntax: https://mermaid.js.org/syntax/
- Mermaid CLI: https://github.com/mermaid-js/mermaid-cli
- Live editor, for ad-hoc tweaks: https://mermaid.live/
- Imported and adapted from
sontek/sontek-skills—skills/draw-mermaid-diagram, itself adapted frommitsuhiko/agent-stuff.