Skip to content

Check a diagram against Mermaid's own reading of its source - #899

Merged
max-sixty merged 3 commits into
mainfrom
diagram-differential-check
Sep 21, 2026
Merged

max-sixty merged 3 commits into
mainfrom
diagram-differential-check

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 21, 2026 •

Copy link
Copy Markdown
Owner

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

max-sixty and others added 2 commits September 21, 2026 14:43
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).

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, which a record
of unread text cannot.

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 cut to the parts that read (2.9MB); 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. A page never put through the render gate draws
whatever the renderer made of its source.

`UNSUPPORTED_DIRECTIVE` and `rejectCutLabel` are deleted, and their two
browser tests give way to one gate test over twelve misdrawn sources and
nine faithful controls. `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.

Supersedes #860. Closes #859.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The differential check is the right level to fix this at — one mapping between two vocabularies beats a growing set of grammar guards, and reading the drawing rather than the source is what lets it catch a parser reading too much. Two things hold the verdict.

The gate refuses a flowchart node named click

Official Mermaid's flowchart grammar is total in the one place this check depends on it not being: it swallows click <anything> as a click directive and reports no node, no edge, and no error. So flowchart LR\n click --> done — which Beautiful Mermaid draws correctly — now fails the gate. I ran it through render_version:

[light] a widget failed soft: <lf-diagram> failed: the drawing does not match its
source as Mermaid reads it — node drawn but not in the source: click "click"
(rectangle); edge drawn but not in the source: click -> done

This is the case the deleted guard carved an exception for on purpose — /^\s*(?:click\s+[A-Za-z_]|…)/ required an identifier after click precisely so an ordinary node named click survived — and test_unsupported_directives_fail_without_rejecting_keyword_nodes pinned it, asserting g[data-id="click"] visible with no .lf-error, with parts="node:click …" so the node was an anchor target. That test is deleted and the case is not among FAITHFUL_DIAGRAMS, so nothing reports the narrowing.

The seam under it is that drawn but not in the source conflates two different things: the renderer inventing a node, and Mermaid silently dropping a line. click-directive and acc-title in MISDRAWN_DIAGRAMS both depend on that direction, so the direction can't simply go — the mapping needs to know when Mermaid's own reading lost a statement. Worth settling before this lands, since the module's stated rule is that a divergence it misses is answered by reading more of what either library already reports, and this one is a divergence it invents.

scripts/vendor.py no longer reproduces the bundle it ships

The alias list spells the package cytoscape-case-bilkent; Mermaid 12.0.0 depends on cytoscape-cose-bilkent, so that alias matches nothing and the layout library is bundled. Building entry.mjs with esbuild 0.28.2 against mermaid 12.0.0:

  • with cytoscape-cose-bilkent aliased: byte-identical to the tracked mermaid-reader.esm.js (cmp clean, 2,848,304 bytes);
  • with the committed spelling: 2,926,566 bytes.

So the tracked payload is correct and the script that claims to produce it is not. The next scripts/vendor.py mermaid-reader adds 78,262 bytes of cose-bilkent/cose-base/layout-base to every diagram page directory and dirties git status — and this diff also files mermaid-reader in scripts/CLAUDE.md under the bundles whose diffs "can be an upstream patch rather than drift", so that growth reads as ordinary rather than as the check failing. The inline suggestion fixes the spelling.

Verification run here
  • uv run pytest tests/test_render_gate.py -q -n2 -k diagram — 2 passed, including the new test_the_gate_refuses_a_diagram_that_does_not_draw_its_source.
  • uv run pytest tests/test_render_pages.py -q -n2 -k diagram — 4 passed.
  • The click finding came from a scratch page carrying click --> done, accTitle --> done, and A[Start] --> B[End] through render_gate.version.render_version; accTitle and the control passed, click --> done and the three-line keyword-nodes source were both refused in both schemes. Reading the same sources through the vendored readMermaid directly confirms the cause: flowchart LR\n click --> done yields zero nodes, while the accTitle line yields accTitle and done.
  • The bundle comparison used a clean npm install [email protected] [email protected] and the PR's own entry.mjs/undrawn.mjs text.

Comment thread scripts/vendor.py Resolved
… alias

Mermaid reads any line opening with `click` as a click directive, so it finds
no node in `click --> done` and the comparison refuses the two nodes Beautiful
Mermaid draws. That follows from taking Mermaid's reading as the authority:
the source is a diagram only under this renderer. The deleted guard had carved
an exception for it, so the narrowing is now stated where a reader meets it:
a `click-node` case among the misdrawn sources with `accTitle`/`accDescr` ids
as its faithful control, the fidelity module's header, and the authoring
reference.

The typos hook had rewritten `cytoscape-cose-bilkent` to `case` in
`scripts/vendor.py`, so the alias matched no package and a rerun would have
bundled the layout library. The tracked bundle was built before that rewrite;
the corrected script reproduces it byte for byte, and `_typos.toml` keeps the
spelling.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@max-sixty
max-sixty merged commit f66cf83 into main Sep 21, 2026
6 checks passed
@max-sixty
max-sixty deleted the diagram-differential-check branch September 21, 2026 22:55
max-sixty added a commit that referenced this pull request Sep 21, 2026
`vendor/mermaid-reader.esm.js` (2.9MB) travels in every page directory that
draws a diagram, and only `version check --render` is meant to load it (#899).
Nothing checked that. `test_diagrams_load_one_renderer_bundle_when_they_draw`
read the requests for Beautiful Mermaid's bundle alone, so a widget that ran
its render check on connect would have put the reader on every reader's page
with the suite green.

The test now requires the renderer bundle to be the only Mermaid file the page
asks for. Planting `checkDrawing()` in `connectedCallback` turns it red on
`/vendor/mermaid-reader.esm.js`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
max-sixty added a commit that referenced this pull request Sep 21, 2026
A widget that fails soft echoes its source in a `pre` under its error
message, and the render gate read the error box's whole text. So each
refusal ran the reason straight into the source (`… node drawn but not
in the source: S0 "S0" (rounded)stateDiagram-v2`) and printed the full
source twice, once per colour scheme. The report never said which widget
failed: on a page with two diagrams, only the pasted source told them
apart, and the diagram fidelity test relied on that source to attribute
each refusal.

`failSoftErrors` now returns the owning widget's tag and id alongside
the box's message, and leaves out the `pre`. The gate reports a refusal
in the `<tag id='…'>` form its other findings already use:

```
[light] <lf-diagram id='security-proposed-states'> failed soft: <lf-diagram> failed: the drawing does not match its source as Mermaid reads it — node in the source but not drawn: S0 "S0 · Gate passed" (rounded); node drawn but not in the source: S0 "S0" (rounded)
```

The fidelity test now attributes refusals by id. It also gains the
state-diagram shape that misdrew `examples/security-boundary.html` until
#899: a state used before `state "…" as S0` labels it is drawn labelled
`S0`. The render gate already refused that shape; the case pins it, and
`declared-label`, with the declaration first, is its control.

## Validation

- Rendered in Chrome, the pre-#899 copy of `security-boundary.html`
draws its first box as `S0`, and main's copy draws `S0 · Gate passed`.
On this branch, `version check --render` refuses the first with the
message above and passes the second.
- `uv run pytest tests/test_render_gate.py tests/test_render_commands.py
tests/test_render_pages.py -n 4` on a Mac: 180 passed, 2 failed. The
failures, `test_render_reports_a_word_the_printed_page_loses` and
`test_a_written_comment_keeps_its_originating_agent`, fail the same way
on `1888c6ec`, the commit this branch started from. CI's `test` job
passes on this head.
- `pre-commit` is clean on the changed files.

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5 <[email protected]>
max-sixty added a commit that referenced this pull request Sep 22, 2026
`lf-diagram` now draws with agentic-mermaid 0.4.1, a fork of Beautiful
Mermaid 1.1.3 that documents the SVG it writes and draws more of
Mermaid. With a renderer that holds up on its own, the render gate stops
re-reading each diagram with official Mermaid: #899's differential
check, the 2.85MB Mermaid reader bundle it imported, and the
`registerRenderCheck` hook only it used are removed. The gate reports a
diagram only when the renderer refuses it or draws it empty, and every
family the renderer draws passes.

## What a reader sees

- Most drawings improve or are unchanged. In `pr-walkthrough`,
TempIndex's three edges now leave TempIndex rather than running up a
trunk from Committed changes. Sequence diagrams draw notes, `autonumber`
and `-x` heads, which Beautiful Mermaid dropped. `[/lean/]` stops at its
own closer instead of swallowing the next node.
- Pie, Gantt, mindmap, timeline, journey, quadrant, git graph,
architecture and radar diagrams draw; the check refused every family
outside the six Leaf documents.
- The XY chart is heavier. agentic-mermaid replaced Beautiful Mermaid's
own chart design with a port of Mermaid's xychart layout and defaults
(`xychart/layout.ts`): bars fill 95% of their band from a constant with
no option, fills are solid and unstroked, and axes carry lines and
ticks. The title overlapping the top gridline happens with the library's
defaults too. Mermaid config and a tint on the documented
`data-role="bar"` lighten it but keep the bars full-width, so this
change leaves the chart as drawn and the authoring guide sends
quantities to `lf-chart`.
- A diagram page directory carries 2.90MB of diagram vendor files,
against 4.41MB before (Beautiful Mermaid 1.56MB plus the reader 2.85MB).
The renderer itself is larger: 869KB gzipped against 471KB, because the
package ships its whole toolkit in one module esbuild cannot cut down.

## What the gate no longer catches

agentic-mermaid still departs from Mermaid in places, and those now draw
silently: no `click` or `link` target (the box draws as a plain one), no
frontmatter or sequence `title`, a state statement ending in `;`
dropped, a state labelled with its bare id when a transition names it
before `state "…" as S0` declares it, ER relationships read only in
symbol form, and an entity code such as `#36;` drawn as written.
`skills/leaf/references/authoring-evidence.md` lists them and asks for a
look at each diagram. A diagram drawn empty (a lone `[*] --> A;`, or
only word cardinalities) is still reported, as a widget with no usable
size.

## Where to look

- `skills/leaf/packages/diagram/widgets/lf-diagram.js` — the renderer's
own `font` and `idPrefix` options replace Leaf's font rewrite and id
namespacing; `security: "strict"` leaves out a `click` or `link` target,
which the default mode draws as a focusable link nothing navigates;
`interactive: true` gives chart values a hover tooltip and a line series
its dots. Parts are read from the renderer's documented `data-role`
contract; a namespace gives way to a class of the same name, and an id
that is not one token (a quoted ER name, a multi-word mindmap node) is
not offered as a part.
- `scripts/vendor.py` — `build_agentic_mermaid` replaces the Beautiful
Mermaid build; `elkjs`, `entities` and `yaml` are held by
agentic-mermaid's declared ranges. The reader build and the `mermaid`
pin are gone.
- `skills/leaf/assets/runtime/render-check.js`,
`render-checks/widgets.js`, `render_gate/readings.py` — the hook and its
gate wiring removed; #902's per-widget failure naming stays.
- `tests/test_render_gate.py` —
`test_the_gate_passes_what_the_renderer_draws` covers every added family
plus the multi-word and quoted ids, and fails on a head where the check
still ran. `test_the_shim_runs_the_gate_from_anywhere` now holds #902's
`<lf-diagram id='d-broken'> failed soft:` form without the source.
Fixtures that relied on pie failing use `sankey-beta`, which the
renderer does not implement.

## Validation

- `uv run pytest tests -q` on `e3317831`: 1215 passed. The one commit
since adds `security: "strict"`, which renders all 39 drawable shipped
and test diagrams byte-identically, and
`test_a_diagram_link_draws_no_tab_stop`, which fails without it.
- `npm run test:runtime`: 33 passed.
- The diagram part tests in `test_render_aim.py`,
`test_render_reactions.py` and `test_render_semantic_selection.py` pass:
Alt+click and `s` still target a declared box.
- `version check --render` passed on the four shipped pages with
diagrams (`security-boundary`, `pr-walkthrough`, `corpus`,
`how-it-works`) during the trial, when the check still ran; removing the
check only relaxes the gate.
- Locally, a ten-diagram page presents about 200ms later (480ms → 680ms)
and the largest shipped diagram renders in 101ms against 8ms. The
`scripts/verify-site-local.sh` phase profile was not run: it needs
Docker, which was not available.

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

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

Development

Successfully merging this pull request may close these issues.

A quoted flowchart label spanning two lines renders as the node id plus a stray node, and version check --render passes it

2 participants