Skip to content

Refuse a diagram label the renderer would cut at its own closer - #312

Merged
max-sixty merged 4 commits into
mainfrom
fix/issue-311
Sep 6, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/issue-311

Conversation

@leaf-agent

@leaf-agent leaf-agent commented Sep 5, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A flowchart node whose quoted label carries the delimiter that closes its own shape rendered cut at that character, with the rest of the line — including the node the edge points at — silently dropped. A["names: list[str]"] --> B[plain] drew one box labelled "names: list[str and no B, and version check passed.

The cause is upstream, not the widget. beautiful-mermaid's NODE_PATTERNS read a label as (.+?) up to the first closer and never look at the quotes Mermaid uses to hold one, so the tokenizer stops inside the label and the remainder of the line no longer parses as an edge. Quotes are only stripped later, at normalize time, which is why the opening quote survives into the drawing. Tracked upstream as lukilabs/beautiful-mermaid#125, open since June.

Solution

lf-diagram refuses that source, alongside the click/accTitle/accDescr directives it already refuses for the same reason — a parser reading that produces a wrong picture rather than an error. The failure becomes a visible .lf-error box naming the label, and version check --render reports it (the render gate reports a widget that failed soft).

The reading is the parser's own rather than a blanket ban on ] in a label: each quoted span is compared against the delimiter run that actually follows it, so a doubled closer and a pipe-delimited edge label — both of which the renderer handles correctly today — still pass through whole. A subgraph title is the third working shape, and the scan skips its line: the parser reads that line with its own greedy, end-anchored regex rather than through NODE_PATTERNS, so subgraph S["Stage [1]"] carries the closer whole. A comment is the fourth: the parser trims each line and drops the ones starting with %% before any pattern sees them, so a commented-out label cannot be cut — and refusing one would leave an author no way to set a refused line aside while they work. Skipping by line exempts both without exempting the nodes around them.

The message names the delimiter rather than the quotes. The unquoted spelling is cut the same way and raises nothing — A[list[str]] --> B[plain] draws one box labelled list[str and no B — so a message blaming the quotes would send an author to the unguarded path.

references/authoring-evidence.md gains the restriction, scoped to node labels and to the delimiter rather than the quotes, and the shapes that carry the character: A[["names: list[str]"]], A(["names: list[str]"]), and subgraph S["Stage [1]"] all render it.

Testing

test_a_quoted_label_holding_its_own_closer_is_refused in tests/test_render_pages.py pins both halves — the two cut shapes refuse, the four working ones draw B and keep the bracket in the label. It fails on main with the drawing the issue describes and passes here.

uv run pytest tests/test_render_pages.py (50 passed) and uv run pytest tests (877 passed, 6 skipped) are green, as is pre-commit on the changed files.

Before: the aria snapshot from the test failing on main
- img: "\"names: list[str"
- 'button "Respond to diagram · \"names: list[str"'
- img: "\"m{k"
- 'button "Respond to diagram · \"m{k"'
- img: names: list[str] plain          # A[["names: list[str]"]] — already whole
- img: list[str] A B                   # A -->|"list[str]"| B — already whole

The first two are the bug: one box, the opening quote drawn literally, no B node and no edge. The last two are the control cases the guard must not touch.

Why not fix the renderer

The bundle under packages/diagram/vendor/ is a generated payload output, and CLAUDE.md rules out patching one directly; scripts/vendor.py has no patch step, and 1.1.3 is upstream's latest, so no pin bump carries a fix. The guard is the interim reading, and it can be dropped when a release closes lukilabs/beautiful-mermaid#125.


Closes #311 — automated triage

Beautiful Mermaid reads a flowchart node label as the text up to the first
closing delimiter without noticing the quotes Mermaid uses to hold one, so
A["names: list[str]"] is cut at the inner bracket and the rest of the line —
the node the edge points at included — is dropped. The reader saw a box short
of a diagram with nothing to say a node was missing.

lf-diagram now refuses that source alongside the directives it already refuses,
so the failure is a visible error box and version check --render reports it.
The reading is the parser's own: the label is compared against the delimiter run
that actually follows it, so a doubled closer and a pipe-delimited edge label
still render whole.

Closes #311

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One finding: the guard refuses a construct the renderer draws correctly today, so a page that renders on main becomes an error box.

A subgraph title does not go through NODE_PATTERNS. The vendored parser reads it with its own regex — /^([\w-]+)\s*\[(.+)\]$/, greedy and anchored at end of line — so it carries the closer whole. subgraph S["Stage [1]"] draws today; after this change the quoted span is followed by ], the label contains ], and the whole diagram fails soft. version check --render then reports a page that was fine.

Checked against packages/diagram/vendor/beautiful-mermaid.esm.js directly — renderMermaidSVG on

flowchart LR
  subgraph S["Stage [1]"]
    A[x] --> B[y]
  end

returns all three labels ("Stage [1]", "x", "y") with data-id for S, A and B, while rejectCutLabel throws on it.

The inline suggestion scopes the scan per line and skips subgraph ones; I ran it against the four sources the new test pins and the subgraph case, and it keeps all five verdicts right. The exemption is a third working shape, so it belongs in test_a_quoted_label_holding_its_own_closer_is_refused beside the doubled closer and the pipe-delimited edge label — and in the authoring-evidence.md sentence, which currently reads as though every quoted label is subject to the rule.

The vendored parser does not read a subgraph title through NODE_PATTERNS. It
matches the line with its own /^([\w-]+)\s*\[(.+)\]$/ — greedy and anchored at
end of line — so the title carries its own closer whole and
subgraph S["Stage [1]"] renders today. The refusal added for cut node labels
did not know that, so it took a working diagram down to an error box and made
version check --render report a page that was fine.

Scan the source by line and skip the subgraph title's line, which exempts the
title without exempting the nodes around it. The test pins the subgraph as a
third working shape beside the doubled closer and the pipe-delimited edge
label, and authoring-evidence.md says the rule is about node labels.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subgraph exemption is right and it is narrow — I drove the shipped rejectCutLabel against renderMermaidSVG from packages/diagram/vendor/beautiful-mermaid.esm.js and the five pinned shapes plus a node inside a subgraph all line up: subgraph S["Stage [1]"] passes and draws S, A, B, while A["x[1]"] on the line below it still refuses. Scanning by line is the same reading the parser uses — zPn splits on \n, trims, and there is no ; statement separator — so the skip cannot swallow a node declaration.

One more line the parser never reads: a comment. The same split-and-trim drops every line that startsWith("%%") before any pattern sees it, so a commented-out label cannot be cut, but the guard still refuses it — %% A["names: list[str]"] --> B[plain] above two ordinary nodes takes the whole diagram to an error box naming a label that is switched off. That path is one the guard itself steers authors onto: hit the refusal, comment the offending line out to keep working, get the same error back. Inline suggestion keeps all five verdicts above and adds the comment case.

The vendored parser trims each source line and drops the ones starting with %%
before any pattern sees them, so a label inside a comment cannot be cut. The
refusal did not know that, and it read the commented line anyway: a diagram
carrying %% A["names: list[str]"] --> B[plain] above two working nodes went to
an error box naming a label that is switched off. The guard steers authors onto
that path itself — hit the refusal, comment the line out to keep working, get
the same refusal back.

Skip a comment line the way the subgraph title's line is skipped, which leaves
the nodes around it subject to the rule. The test pins the commented-out label
as a fourth working shape.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment exemption is exactly the parser's reading — zPn trims each line and drops it on startsWith("%%") before any pattern runs, which /^\s*%%/ matches character for character. I drove the shipped rejectCutLabel against renderMermaidSVG from packages/diagram/vendor/beautiful-mermaid.esm.js on all six pinned shapes and the commented source renders A and B whole, so the new test case pins real behaviour.

One finding, on the message rather than the reading. It names the quotes as the thing that cannot hold the closer, and the spelling that leaves is cut silently: A[list[str]] --> B[plain] returns one node labelled list[str], no B, no error — the same wrong picture this guard exists to refuse, now reachable by following the guard's own advice. Same shape as the commented-out line: the refusal steers an author onto the unguarded path. The reading can't cheaply cover an unquoted label — the parser gives no signal there beyond "the rest of the line stopped parsing" — so the message is where it gets fixed, by not attributing the cut to the quotes.

Nothing else survived: the pipe-delimited (\|([^|]*)\|) and spaced ((--|-\.|==)\s+(.+?)\s+(-->|...)) edge-label forms both terminate on something other than a closer, so the guard's empty-closer skip is right for them, and A -->|"a|b"| B — which the parser does cut — is refused.

The message read as though quoting the label is what makes it fail, so the fix
it suggests is to drop the quotes — and the unquoted spelling is cut the same
way with no error at all. Driven against the vendored bundle,
A[list[str]] --> B[plain] renders one node labelled "list[str" and nothing else:
no B, no error, the same wrong picture this guard exists to refuse, reached by
following the guard's own advice.

The reading cannot cheaply cover an unquoted label, since the parser gives no
signal there beyond the rest of the line ceasing to parse, so the message is
where it gets fixed. Say the delimiter cuts the line quoted or not, and make
authoring-evidence.md say the same rather than leaving the quoted example to
carry the rule.
@max-sixty
max-sixty merged commit 70e3de0 into main Sep 6, 2026
7 checks passed
@max-sixty
max-sixty deleted the fix/issue-311 branch September 6, 2026 05:15
leaf-agent added a commit that referenced this pull request Sep 19, 2026
`A -->|"a|b"| B` is one line holding a pipe the edge label cannot carry,
and the cut guard has said so since #312. The label walk ran ahead of it
and reached the trailing pipe first, so the author was told to carry a
second line with `<br/>` when their label was already on one line. Run
the cut guard first: it needs both quotes on one line, so it stays silent
on every split-label source and speaks only where it holds the answer.

The walk's comment also named class and ER bodies among the grammars the
`graph`/`flowchart` gate protects. This renderer reads two grammars —
`stateDiagram` and the flowchart — and throws `Invalid mermaid header`
on anything else.
max-sixty added a commit that referenced this pull request Sep 21, 2026
Beautiful Mermaid's parsers are total: a statement they cannot read
still gets a reading, so a source the renderer only partly understands
draws a plausible, wrong diagram and reports nothing. `lf-diagram`
answered that with hand-written guards, each a slice of Mermaid grammar
kept beside the parser it second-guessed, and each refusing only the
divergence someone had already noticed (#312, #859, and the rounds on
#860).

Both libraries already describe what they found: official Mermaid in its
diagram database, Beautiful Mermaid in the `data-*` attributes of the
SVG it drew. `lf-diagram-fidelity.js` reads both into one shape and
reports where they differ, so Leaf holds the mapping between two
vocabularies and no grammar. It covers all six diagram types, and
because it compares against what was drawn it also catches a parser that
read too much.

Official Mermaid is too heavy for a reader's page, so the comparison
runs only in `version check --render`. `registerRenderCheck` is the
general form: a widget registers a check, the render gate alone calls
it, and what it finds arrives as that widget failing soft.
`mermaid-reader.esm.js` is official Mermaid 12.0.0 cut to the parts that
read (2.9MB, the undrawn dependencies aliased away in
`scripts/vendor.py`); it travels in the page directory beside the
renderer it answers for, and no reader's browser fetches it.

The contract for a diagram is now "valid Mermaid that Beautiful Mermaid
draws faithfully". Sources Mermaid refuses are refused even where the
renderer happened to draw them, unquoted parentheses in a label above
all; quoting the label is the fix. The same holds where Mermaid reads
less than the author meant: a line opening with `click` is a click
directive to it, so a node cannot be named `click`, which the deleted
guard had carved an exception for. A page never put through the render
gate draws whatever the renderer made of its source.

On a corpus of 59 misdrawn and 88 faithful sources measured before
building this, the comparison refused 54 of the 59 and falsely refused 1
of the 88.

## Where to look

- `skills/leaf/packages/diagram/widgets/lf-diagram-fidelity.js` — the
two readings and their comparison.
- `skills/leaf/assets/runtime/render-check.js`,
`render-checks/widgets.js`, `render_gate/readings.py` — the render-check
primitive and where the gate runs it.
- `skills/leaf/packages/diagram/widgets/lf-diagram.js` —
`UNSUPPORTED_DIRECTIVE` and `rejectCutLabel` are deleted; the widget
registers `checkDrawing`.
- `scripts/vendor.py` — the `mermaid-reader` bundle.
- `tests/test_render_gate.py` — one gate test over thirteen misdrawn
sources and ten faithful controls replaces the two browser tests for the
deleted guards.
- `examples/security-boundary.html` was itself misdrawn: a transition
named `S0` before `state "S0 · Gate passed" as S0` declared it, so the
box was labelled `S0`. The declarations now come first.

## Validation

- `uv run pytest tests/test_render_gate.py tests/test_render_pages.py -q
-n 4`: 164 passed, 1 failed —
`test_a_written_comment_keeps_its_originating_agent`, which fails the
same way on the merge-base `39d9cad0`.
- `uv run pytest tests -q`: 1212 passed. `npm run test:runtime`: 32
passed. `pre-commit run --all-files` clean.
- `version check --render` over the 21-page `examples/` corpus is clean
after the `security-boundary` fix.

Supersedes #860. Closes #859.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants