mdd
DocsUse CasesGitHubOpen App

ASCII diagrams in code comments, drawn by a program

Packet layouts, state machines, module maps: embedded, systems, and protocol engineers keep them in header comments because that is the only format that ships with the code. Hand-drawn boxes drift, and agents mangle them. mdd computes the layout from JSON and splices it behind your comment prefix.

A state machine in a header comment

A connection state machine as it appears at the top of a C file. Every line carries the comment prefix, and re-rendering replaces exactly this block.

// mdd:begin id=conn-states src=conn_states.diagram.json
//
//    ┌─────────┐
//    │  IDLE   │
//    └┬───────▲┘
//  open()  timeout
//     │       │
//  ┌──▼───────┴─┐
//  │ CONNECTING │
//  └──────┬─────┘
//        ack
//         │
//   ┌─────▼─────┐
//   │ CONNECTED │
//   └─────┬─────┘
//      close()
//         │
//    ┌────▼───┐
//    │ CLOSED │
//    └────────┘
//
// mdd:end

The model your agent edits

{
  "version": 1,
  "boxes": [
    {
      "id": "idle",
      "label": "IDLE",
      "row": 0,
      "connections": [
        {
          "to": "connecting",
          "arrow": "to",
          "label": "open()"
        }
      ]
    },
    {
      "id": "connecting",
      "label": "CONNECTING",
      "row": 1,
      "connections": [
        {
          "to": "connected",
          "arrow": "to",
          "label": "ack"
        },
        {
          "to": "idle",
          "arrow": "to",
          "label": "timeout"
        }
      ]
    },
    {
      "id": "connected",
      "label": "CONNECTED",
      "row": 2,
      "connections": [
        {
          "to": "closed",
          "arrow": "to",
          "label": "close()"
        }
      ]
    },
    {
      "id": "closed",
      "label": "CLOSED",
      "row": 3
    }
  ]
}

Your agent draws it, you review it

  1. Model the state machine as JSON next to the source file.
  2. Run mdd render with --prefix "// " to splice the diagram into the header comment.
  3. When transitions change, your agent edits the JSON and re-renders — it never redraws box characters by hand.
  4. mdd check in CI fails the build if the comment block goes stale relative to the JSON.

A prompt to hand your agent

Keep the state machine diagram in src/conn.c current with mdd. Edit conn_states.diagram.json, then run: npx mdd render --json conn_states.diagram.json --file src/conn.c --id conn-states --prefix "// ".

Why mdd here

This is the purest case for a program computing the layout instead of a model eyeballing characters: comment blocks have no renderer, so alignment is the format. mdd's comment-prefix splicing keeps every line behind // or # and regenerates the block idempotently.

More use cases