Skill Tracer
Skills › Scientific Agent Skills › markdown-mermaid-writing › state.md

state.md

155 lines · skills/markdown-mermaid-writing/references/diagrams/state.md at 5f2bc03

Open on GitHub
1<!-- Source: https://github.com/SuperiorByteWorks-LLC/agent-project | License: Apache-2.0 | Author: Clayton Young / Superior Byte Works, LLC (Boreal Bytes) -->
2
3# State Diagram
4
5> **Back to [Style Guide](../mermaid_style_guide.md)** — Read the style guide first for emoji, color, and accessibility rules.
6
7**Syntax keyword:** `stateDiagram-v2`
8**Best for:** State machines, lifecycle flows, status transitions, object lifecycles
9**When NOT to use:** Sequential processes with many steps (use [Flowchart](flowchart.md)), timing-critical interactions (use [Sequence](sequence.md))
11---

<!-- Source: https://github.com/SuperiorByteWorks-LLC/agent-project | License: Apache-2.0 | Author: Clayton Young / Superior Byte Works, LLC (Boreal Bytes) -->

State Diagram

Back to Style Guide — Read the style guide first for emoji, color, and accessibility rules.

Syntax keyword: stateDiagram-v2 Best for: State machines, lifecycle flows, status transitions, object lifecycles When NOT to use: Sequential processes with many steps (use Flowchart), timing-critical interactions (use Sequence)


Raw lines 1 to 12
13## Exemplar Diagram
15```mermaid
16stateDiagram-v2
17 accTitle: Order Fulfillment Lifecycle
18 accDescr: State machine for an e-commerce order from placement through payment, fulfillment, and delivery with cancellation paths
20 [*] --> Placed: 📋 Customer submits
22 Placed --> PaymentPending: 💰 Initiate payment
23 PaymentPending --> PaymentFailed: ❌ Declined
24 PaymentPending --> Confirmed: ✅ Payment received
26 PaymentFailed --> Placed: 🔄 Retry payment
27 PaymentFailed --> Cancelled: 🚫 Customer cancels
29 Confirmed --> Picking: 📦 Warehouse picks
30 Picking --> Shipped: 🚚 Carrier collected
31 Shipped --> Delivered: ✅ Proof of delivery
32 Delivered --> [*]: 🏁 Complete
34 Cancelled --> [*]: 🏁 Closed
36 note right of Confirmed
37 📋 Inventory reserved
38 💰 Invoice generated
39 end note
40```
42---

Exemplar Diagram

stateDiagram-v2
    accTitle: Order Fulfillment Lifecycle
    accDescr: State machine for an e-commerce order from placement through payment, fulfillment, and delivery with cancellation paths

    [*] --> Placed: 📋 Customer submits

    Placed --> PaymentPending: 💰 Initiate payment
    PaymentPending --> PaymentFailed: ❌ Declined
    PaymentPending --> Confirmed: ✅ Payment received

    PaymentFailed --> Placed: 🔄 Retry payment
    PaymentFailed --> Cancelled: 🚫 Customer cancels

    Confirmed --> Picking: 📦 Warehouse picks
    Picking --> Shipped: 🚚 Carrier collected
    Shipped --> Delivered: ✅ Proof of delivery
    Delivered --> [*]: 🏁 Complete

    Cancelled --> [*]: 🏁 Closed

    note right of Confirmed
        📋 Inventory reserved
        💰 Invoice generated
    end note

Raw lines 13 to 43
44## Tips
46- Use `[*]` for initial/final pseudostates when the modeled lifecycle has them; a continuously running process need not have a final state
47- Label transitions with **emoji + action** for visual clarity
48- Use `note right of` / `note left of` for contextual details
49- State names: `CamelCase` (Mermaid convention for state diagrams)
50- Use nested states sparingly: `state "name" as s1 { ... }`
51- Keep to **8–10 states** maximum
53---

Tips

  • Use [*] for initial/final pseudostates when the modeled lifecycle has them; a continuously running process need not have a final state
  • Label transitions with emoji + action for visual clarity
  • Use note right of / note left of for contextual details
  • State names: CamelCase (Mermaid convention for state diagrams)
  • Use nested states sparingly: state "name" as s1 { ... }
  • Keep to 8–10 states maximum

Raw lines 44 to 54
55## Template
57```mermaid
58stateDiagram-v2
59 accTitle: Your Title Here
60 accDescr: Describe the entity lifecycle and key transitions between states
62 [*] --> InitialState: ⚡ Trigger event
64 InitialState --> ActiveState: ▶️ Action taken
65 ActiveState --> CompleteState: ✅ Success
66 ActiveState --> FailedState: ❌ Error
68 CompleteState --> [*]: 🏁 Done
69 FailedState --> [*]: 🏁 Closed
70```
72---

Template

stateDiagram-v2
    accTitle: Your Title Here
    accDescr: Describe the entity lifecycle and key transitions between states

    [*] --> InitialState: ⚡ Trigger event

    InitialState --> ActiveState: ▶️ Action taken
    ActiveState --> CompleteState: ✅ Success
    ActiveState --> FailedState: ❌ Error

    CompleteState --> [*]: 🏁 Done
    FailedState --> [*]: 🏁 Closed

Raw lines 55 to 73
74## Complex Example
76A CI/CD pipeline modeled as a state machine with 3 composite (nested) states, each containing internal substates. Shows how source changes flow through build, test, and deploy phases with failure recovery and rollback transitions.
78```mermaid
79stateDiagram-v2
80 accTitle: CI/CD Pipeline State Machine
81 accDescr: Composite state diagram for a CI/CD pipeline showing source detection, build and test phases with sequential scanning, and a three-stage deployment with approval gate and rollback path
83 [*] --> Source: ⚡ Commit pushed
85 state "📥 Source" as Source {
86 [*] --> Idle
87 Idle --> Fetching: 🔄 Poll detected change
88 Fetching --> Validating: 📋 Checkout complete
89 Validating --> [*]: ✅ Config valid
90 }
92 Source --> Build: ⚙️ Pipeline triggered
94 state "🔧 Build & Test" as Build {
95 [*] --> Compiling
96 Compiling --> UnitTests: ✅ Build artifact ready
97 UnitTests --> IntegrationTests: ✅ Unit tests pass
98 IntegrationTests --> SecurityScan: ✅ Integration pass
99 SecurityScan --> [*]: ✅ No vulnerabilities
101 note right of Compiling
102 📦 Docker image built
103 🏷️ Tagged with commit SHA
104 end note
105 }
107 Build --> Deploy: 📦 Artifact published
108 Build --> Failed: ❌ Build or test failure
110 state "🚀 Deployment" as Deploy {
111 [*] --> Staging
112 Staging --> WaitApproval: ✅ Staging healthy
113 WaitApproval --> Production: ✅ Approved
114 WaitApproval --> Cancelled: 🚫 Rejected
115 Production --> Monitoring: 🚀 Deployed
116 Monitoring --> [*]: ✅ Stable 30 min
118 note right of WaitApproval
119 👤 Requires team lead approval
120 ⏰ Auto-reject after 24h
121 end note
122 }
124 Deploy --> Rollback: ❌ Health check failed
125 Rollback --> Deploy: 🔄 Revert to previous
126 Deploy --> Complete: 🏁 Pipeline finished
127 Failed --> Source: 🔧 Fix pushed
128 Cancelled --> [*]: 🏁 Pipeline aborted
129 Complete --> [*]: 🏁 Done
131 state Failed {
132 [*] --> AnalyzeFailure
133 AnalyzeFailure --> NotifyTeam: 📤 Alert sent
134 NotifyTeam --> [*]
135 }
137 state Rollback {
138 [*] --> RevertArtifact
139 RevertArtifact --> RestorePrevious: 🔄 Previous version
140 RestorePrevious --> VerifyRollback: 🔍 Health check
141 VerifyRollback --> [*]
142 }
143```

Complex Example

A CI/CD pipeline modeled as a state machine with 3 composite (nested) states, each containing internal substates. Shows how source changes flow through build, test, and deploy phases with failure recovery and rollback transitions.

stateDiagram-v2
    accTitle: CI/CD Pipeline State Machine
    accDescr: Composite state diagram for a CI/CD pipeline showing source detection, build and test phases with sequential scanning, and a three-stage deployment with approval gate and rollback path

    [*] --> Source: ⚡ Commit pushed

    state "📥 Source" as Source {
        [*] --> Idle
        Idle --> Fetching: 🔄 Poll detected change
        Fetching --> Validating: 📋 Checkout complete
        Validating --> [*]: ✅ Config valid
    }

    Source --> Build: ⚙️ Pipeline triggered

    state "🔧 Build & Test" as Build {
        [*] --> Compiling
        Compiling --> UnitTests: ✅ Build artifact ready
        UnitTests --> IntegrationTests: ✅ Unit tests pass
        IntegrationTests --> SecurityScan: ✅ Integration pass
        SecurityScan --> [*]: ✅ No vulnerabilities

        note right of Compiling
            📦 Docker image built
            🏷️ Tagged with commit SHA
        end note
    }

    Build --> Deploy: 📦 Artifact published
    Build --> Failed: ❌ Build or test failure

    state "🚀 Deployment" as Deploy {
        [*] --> Staging
        Staging --> WaitApproval: ✅ Staging healthy
        WaitApproval --> Production: ✅ Approved
        WaitApproval --> Cancelled: 🚫 Rejected
        Production --> Monitoring: 🚀 Deployed
        Monitoring --> [*]: ✅ Stable 30 min

        note right of WaitApproval
            👤 Requires team lead approval
            ⏰ Auto-reject after 24h
        end note
    }

    Deploy --> Rollback: ❌ Health check failed
    Rollback --> Deploy: 🔄 Revert to previous
    Deploy --> Complete: 🏁 Pipeline finished
    Failed --> Source: 🔧 Fix pushed
    Cancelled --> [*]: 🏁 Pipeline aborted
    Complete --> [*]: 🏁 Done

    state Failed {
        [*] --> AnalyzeFailure
        AnalyzeFailure --> NotifyTeam: 📤 Alert sent
        NotifyTeam --> [*]
    }

    state Rollback {
        [*] --> RevertArtifact
        RevertArtifact --> RestorePrevious: 🔄 Previous version
        RestorePrevious --> VerifyRollback: 🔍 Health check
        VerifyRollback --> [*]
    }
Raw lines 74 to 144
145### Why this works
147- **Composite states group pipeline phases** — Source, Build & Test, and Deployment each contain their internal flow, readable in isolation or as part of the whole
148- **Failure and rollback are first-class states** — not just transition labels. The Failed and Rollback states have their own internal substates showing what actually happens during recovery
149- **Notes on key states** add operational context — the approval gate has timeout rules, the compile step documents the artifact format. This is the kind of detail operators need.
150- **Transitions between composite states** are the high-level flow (Source → Build → Deploy → Complete), while transitions within composites are the detailed steps. Two levels of reading for two audiences.

Why this works

  • Composite states group pipeline phases — Source, Build & Test, and Deployment each contain their internal flow, readable in isolation or as part of the whole
  • Failure and rollback are first-class states — not just transition labels. The Failed and Rollback states have their own internal substates showing what actually happens during recovery
  • Notes on key states add operational context — the approval gate has timeout rules, the compile step documents the artifact format. This is the kind of detail operators need.
  • Transitions between composite states are the high-level flow (Source → Build → Deploy → Complete), while transitions within composites are the detailed steps. Two levels of reading for two audiences.
Raw lines 145 to 152
153## Verified reference
155Syntax examples reviewed against [official Mermaid documentation](https://mermaid.js.org/syntax/stateDiagram.html) and rendered with Mermaid 12.0.0 (2026-10-01). Check the destination version; appearance and accessibility are not guaranteed by a successful parse.

Verified reference

Syntax examples reviewed against official Mermaid documentation and rendered with Mermaid 12.0.0 (2026-10-01). Check the destination version; appearance and accessibility are not guaranteed by a successful parse.

Raw lines 153 to 155