Skip to content

Editorial diagram design for Claude Code, Codex, GitHub Copilot, Cursor, Factory Droid, Qoder, and Pi. 45 diagram types. Self-contained HTML + SVG. No shadows. No Mermaid slop.

fix(import): retain standalone bare states (#392)

Rudy Mizrahi Celeklid2efbfb47292
diff --git a/docs/adr/0015-state-note-termination.md b/docs/adr/0015-state-note-termination.md
new file mode 100644
index 0000000..a6c5708
--- /dev/null
+++ b/docs/adr/0015-state-note-termination.md
@@ -0,0 +1,35 @@
+# ADR 0015: Preserve standalone states without accepting unfinished notes
+
+Status: Implemented
+Date: 2026-10-12
+
+## Context
+
+PR #392 retains bare state declarations. Note bodies can contain text that looks
+like states, transitions or closing braces. An unfinished multiline note previously
+silently consumed the remaining source and returned success.
+
+## Decision
+
+Track the opening source line while ignoring an opaque multiline state note.
+Require `end note`; otherwise use the existing exit-2 diagnostic path. Inline notes
+remain single-line annotations. Keep the standalone-state regression separately
+named alongside the three import regressions merged in #410.
+
+## Alternatives
+
+Returning partial diagrams would conceal malformed input. Parsing note bodies as
+states would introduce phantom structure. Both violate the import fidelity contract.
+
+## Consequences
+
+Valid note and state forms retain their meaning. Previously accepted unfinished
+notes now report an error with the opening line number. No plugin version change.
+
+## Validation
+
+The new unterminated-left-note regression fails on the original PR head with exit
+0, then passes for left and right notes after the fix. The full Mermaid import
+verifier passes on the rebased candidate and independently in a separate review.
+Tests also cover inline/adjacent notes, composite membership, concurrency separators
+and special state shapes. Other gate outcomes are recorded in local review evidence.
diff --git a/scripts/verify-mermaid-import.py b/scripts/verify-mermaid-import.py
index bda12ad..4ab3f5f 100644
--- a/scripts/verify-mermaid-import.py
+++ b/scripts/verify-mermaid-import.py
@@ -1203,12 +1203,79 @@ def check_standalone_er_regression(tmp: Path) -> None:
ok("standalone ER entity declarations, including Unicode names, become tables")
+def check_standalone_state_regression(tmp: Path) -> None:
+ path = tmp / "supported-form.mmd"
+ path.write_text('stateDiagram-v2\nDisconnected\n[*] --> Active\n', encoding="utf-8")
+ payload = json.loads(run_extract([str(path), "--json"]))["diagrams"][0]
+ nodes = {node["id"]: node for node in payload["nodes"]}
+ if "Disconnected" not in nodes or nodes["Disconnected"]["shape"] != "state":
+ fail("Standalone bare state was discarded")
+ concurrent = tmp / "concurrency.mmd"
+ concurrent.write_text('stateDiagram-v2\nstate Active {\nA --> B\n--\nC --> D\n}\n', encoding="utf-8")
+ payload = json.loads(run_extract([str(concurrent), "--json"]))["diagrams"][0]
+ if "--" in {node["id"] for node in payload["nodes"]}:
+ fail("The documented concurrency separator became a phantom state")
+
+ # Note bodies are annotation text, even when they resemble diagram syntax.
+ for side in ("left", "right"):
+ note = tmp / f"state-note-{side}.mmd"
+ note.write_text(
+ "stateDiagram-v2\nstate Group {\nstate Idle\n"
+ f"note {side} of Idle\nDisconnected\nstate Phantom\n"
+ "Ghost --> Imaginary\n}\nIdle: false description\nend note\n"
+ "Active\n}\nOutside\n",
+ encoding="utf-8",
+ )
+ payload = json.loads(run_extract([str(note), "--json"]))["diagrams"][0]
+ nodes = {node["id"]: node for node in payload["nodes"]}
+ if set(nodes) != {"Group", "Idle", "Active", "Outside"} or payload["edges"]:
+ fail(f"A {side} state note body became diagram structure")
+ if nodes["Idle"]["label"] != "Idle" or nodes["Active"]["parent"] != "Group":
+ fail(f"A {side} state note body altered labels or container membership")
+ if nodes["Outside"]["parent"] is not None:
+ fail("Parsing did not resume after the note and composite state")
+
+ notes = tmp / "state-notes-adjacent-inline.mmd"
+ notes.write_text(
+ "stateDiagram-v2\nstate Idle\nnote left of Idle\nGhost\nend note\n"
+ "note right of Idle\nPhantom\nend note\n"
+ "note left of Idle: inline annotation\n"
+ "note right of Idle: inline annotation\nActive\nnote\nend\nnotebook\n",
+ encoding="utf-8",
+ )
+ payload = json.loads(run_extract([str(notes), "--json"]))["diagrams"][0]
+ if {node["id"] for node in payload["nodes"]} != {"Idle", "Active", "note", "end", "notebook"}:
+ fail("State note boundaries swallowed real states or retained annotation bodies")
+
+ for side in ("left", "right"):
+ unterminated = tmp / f"state-note-unterminated-{side}.mmd"
+ unterminated.write_text(
+ f"stateDiagram-v2\nstate Idle\nnote {side} of Idle\n"
+ "Annotation\nActive\n",
+ encoding="utf-8",
+ )
+ expect_error([str(unterminated), "--json"], "unterminated state note at line 3")
+
+ special = tmp / "state-special-bare.mmd"
+ special.write_text(
+ "stateDiagram-v2\nstate Decision <<choice>>\nDecision\n"
+ "state Fork <<fork>>\nFork\nstate Join <<join>>\nJoin\n",
+ encoding="utf-8",
+ )
+ payload = json.loads(run_extract([str(special), "--json"]))["diagrams"][0]
+ shapes = {node["id"]: node["shape"] for node in payload["nodes"]}
+ if shapes != {"Decision": "choice", "Fork": "fork", "Join": "join"}:
+ fail("A repeated bare declaration replaced a special state shape")
+ ok("bare states retain shapes and exclude multiline state note bodies")
+
+
def main() -> int:
with tempfile.TemporaryDirectory(prefix="diagram-design-mermaid-") as directory:
tmp = Path(directory)
check_unicode_state_regression(tmp)
check_unicode_er_regression(tmp)
check_standalone_er_regression(tmp)
+ check_standalone_state_regression(tmp)
check_files()
expect_error([str(FLOW), "--diagram", "²"], "--diagram must be an index")
check_flowchart()
diff --git a/skills/diagram-design/references/import-mermaid.md b/skills/diagram-design/references/import-mermaid.md
index 2a33de3..e0d468f 100644
--- a/skills/diagram-design/references/import-mermaid.md
+++ b/skills/diagram-design/references/import-mermaid.md
@@ -127,3 +127,7 @@ Markdown is the Mermaid analogue of multi-page draw.io. The header lists every f
| One-to-one node mapping regardless of budget | A faithful wiring dump is not an editorial diagram |
| Dropping sequence fragments or ER cardinality | Those structures carry meaning, not styling |
| Silently dropping content | Every import ships a fidelity ledger |
+
+Bare state declarations are retained even when disconnected from transitions.
+Multiline state notes are opaque annotation text through `end note`; a missing
+terminator exits with the extractor's exit-2 diagnostic and the opening line number.
diff --git a/skills/diagram-design/scripts/mermaid_extract.py b/skills/diagram-design/scripts/mermaid_extract.py
index 751832c..efcf1b2 100644
--- a/skills/diagram-design/scripts/mermaid_extract.py
+++ b/skills/diagram-design/scripts/mermaid_extract.py
@@ -978,12 +978,25 @@ def _parse_state(
diagram: Diagram, lines: list[tuple[int, str]], header_position: int
) -> None:
containers: list[str] = []
+ note_start: int | None = None
descriptions: dict[str, list[str]] = {}
pending_descriptions = diagram._state_descriptions
for line_number, raw in lines[header_position + 1 :]:
text = raw.strip()
if not text:
continue
+ if note_start is not None:
+ if re.fullmatch(r"end\s+note", text, re.I):
+ note_start = None
+ continue
+ note = re.fullmatch(
+ r"note\s+(?:left|right)\s+of\s+[^\s:]+(?:\s*:\s*(.*))?", text, re.I
+ )
+ if note:
+ # A colon marks a single-line note; otherwise ignore its opaque body
+ # through `end note` before interpreting states, edges or braces.
+ note_start = line_number if note.group(1) is None else None
+ continue
if _discard_nonsemantic(diagram, text):
continue
if text == "}":
@@ -1045,6 +1058,13 @@ def _parse_state(
plain = re.match(r"^state\s+([\w.:-]+)$", text, re.I)
if plain:
diagram.add_node(plain.group(1), plain.group(1), "state", parent)
+ continue
+ if text != "--" and re.fullmatch(r"[\w.:-]+", text):
+ if text not in diagram.node_map:
+ diagram.add_node(text, text, "state", parent)
+
+ if note_start is not None:
+ _fail(f"unterminated state note at line {note_start}")
# Materialize each accumulated label once; later explicit aliases still win.
for node_id, parts in pending_descriptions.items():